<html>
<head>
<style><!--
.hmmessage P
{
margin:0px;
padding:0px
}
body.hmmessage
{
font-size: 12pt;
font-family:Calibri
}
--></style></head>
<body class='hmmessage'><div dir='ltr'>Hi everyone,<br><br>There is a task when using Clang libraries which
I've never been able to undertake. I asked some time ago, but now it
seems that the libraries have changed and I would like to resume it.<br><br>I have a memberExpr object. I just want to know <b>the class in which that memberExpr object is</b>. For example:<br><br><i>class A{<br> protected: <br> int field;<br>};<br><br>class B: public A{<br><br> void method() {<br> ...<br> if( field == 1 )<br> ...<br> }<br><br>};</i><br><br>In
this example, field is the memberExpr and what I want to know is that
the memberExpr is inside class B. I know that there is a method in
ASTContext called "getParents". When using this method, I know that
you'll not always end up in a CXXRecordDecl - sometimes you'll also end
up in a CXXMethodDcl that is defined out-of-line. <br><br>I have tried with this (just an attempt...):<br><br><i>ArrayRef<ast_type_traits::DynTypedNode> pv = Context->getParents<MemberExpr>(*member);<br>CXXRecordDecl* rec;<br><br>size_t size = pv.size();<br><br>//Retrieve the outermost parent<br>if(llvm::isa<CXXMethodDecl>(pv[size-1]))<br>{<br> CXXMethodDecl* md = (CXXMethodDecl*)(pv[size-1]);<br> if(llvm::isa<CXXRecordDecl>(md->getParent()))<br> rec = (CXXRecordDecl*)(md->getParent());<br>}<br>else<br>{<br> if(llvm::isa<CXXRecordDecl>(pv[size-1]))<br> rec = (CXXRecordDecl*)(pv[size-1]);<br>}<br><br><br></i>The compiler gives the following error (among others):<br><br> <b>error</b>: cannot cast from type 'const clang::ast_type_traits::DynTypedNode' to pointer type 'clang::CXXMethodDecl *'<br> CXXMethodDecl* md = (CXXMethodDecl*)(pv[size-1]);<br> ^~~~~~~~~~~~~~~~~~~~~~~~~<br><br>Am I in the right path? How can I perform the cast?<br><br>Thanks in advance. </div></body>
</html>