[cfe-dev] Getting RecordDecl from CXXBaseSpecifier

Sebastian Redl sebastian.redl at getdesigned.at
Mon May 13 05:55:39 PDT 2013


On 13.05.2013, at 06:15, Justin Arruda wrote:

> Hello all.  New to cfe-dev (and participating in mailing lists in general).
> 
> The question I'm hoping to have answered is as simple as the subject
> implies. I have a CXXRecordDecl,
> and I am trying to determine which of its bases is derived from a
> class that I know this CXXRecordDecl
> is also derived from.
> 
> i.e.
> class A;
> class B : public A;
> class C;
> 
> class D : public B, public C
> Looking for which base D gets its A from.
> 
> I am also seeking general advice for my project as it pertains to my
> usage of clang.  I am using
> libtooling and ASTMatchers to create Python bindings for a game engine.
> 
> This is some sample input I am using for my generator: [test-input].
> 
> When attempting to locate which base of MyBaz and MyDerived gets their
> inheritance of Colossus::MyBase,
> I've tried the following strategy: Obtain the type of the base
> specifier, and using an appropriate
> DeclContext, look up the declaration.  Once I have the declaration, I
> can use its methods to determine if
> it's derived from Colossus::MyBase (or is that class).
> 
> // Get the qualified name of the base
> QualType type = aBaseSpecifier->getType();
> DeclarationName baseDeclName( type.getBaseTypeIdentifier() );
> 
> // Grab a context to look up the decl
> const DeclContext* lookupContext = theCxxRecordDecl->getLookupContext();
> 
> DeclContext::lookup_const_result lr = lookupContext->lookup(baseDeclName);

Looking up the name of the type is too complicated. Also, why do you want the decl? If you want to compare for equality, the type is more interesting.

In any case, the QualType returned by CXXBaseSpecifier::getType() will always (perhaps unless you're dealing with a template definition) be a RecordType underneath; just use type->getAs<RecordType>() to get the RecordType. Then you can use getDecl() on the result, which returns a RecordDecl, but in C++ every RecordDecl is a CXXRecordDecl, so you can just cast<CXXRecordDecl> it.

Sebastian



More information about the cfe-dev mailing list