[cfe-dev] Getting RecordDecl from CXXBaseSpecifier

Justin Arruda paradoxum at gmail.com
Tue May 14 08:50:18 PDT 2013


On Mon, May 13, 2013 at 4:02 PM, Richard Smith <richard at metafoo.co.uk> wrote:
> You can also use getType()->getAsCXXRecordDecl(), which is a little more
> convenient and also copes with the obscure case of deriving from the
> injected-class-name within a class template.
>
> On Mon, May 13, 2013 at 8:20 AM, Justin Arruda <paradoxum at gmail.com> wrote:
>>
>> I want to retrieve the Decl so that I may use its isDerivedFrom
>> method.  I know that my target class is derived from class A.  I am
>> trying to determine which base of class is derived from class A.
>>
>> My main misunderstanding was not knowing how Decls and Types are
>> related.  I believe your solution of using getAs and getDecl is
>> precisely what I was looking for!
>>
>> Thanks
>> --
>> Justin Arruda
>> 401.338.4310
>> http://justinarruda.com/
>> http://www.linkedin.com/in/justinarruda
>>
>>
>> On Mon, May 13, 2013 at 8:55 AM, Sebastian Redl
>> <sebastian.redl at getdesigned.at> wrote:
>> >
>> > 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
>>
>> _______________________________________________
>> cfe-dev mailing list
>> cfe-dev at cs.uiuc.edu
>> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
>
>

Ugh, I can't believe that I missed that method in the docs for Type.
Sorry to waste everyone's time with (what I now realize) is a trivial
thing.

Thanks for the quick responses!

--
Justin Arruda



More information about the cfe-dev mailing list