Type exploration in clang AST


I'm trying to figure out how to extract information from the Type classes
hierarchy but I'm bumping into a few problems.

Given a template instantiation TI - that would be represented by a
clang::TemplateSpecializationType node - I'm trying to determine if any of the
template arguments of TI are used as a member or a base class of TI.

But I can't seem to figure out how to get that information from a

void process( clang::TemplateSpecializationType const *pType )
  // Get the associated template declaration.
  clang::TemplateDecl const *pDecl
    = pType->getTemplateName().getAsTemplateDecl();
    // Get the templated record associated to the template specialization.
    clang::CXXRecordDecl const *pRecD
      = cast<clang::CXXRecordDecl>(pDecl->getTemplatedDecl());
    // Get a pointer to the instantiation arguments array.
    clang::TemplateArgument const *pArgs = pType->getArgs();
    // Get the size of the instantiation arguments array.
    unsigned nbArgs = pType->getNumArgs();

How with this information can I now get the complete types of all base classes
and members of the type represented by 'pType' ?

Thanks for your time,


The pRecD you've computed is the class template definition itself, e.g., in something like

  template<typename Mixin>
  struct MyType : public Mixin { };

it represents the definition of MyType, not any particular instantiation of MyType. I don't think that's what you want.

I think you want to look through the template instantiation itself. In that case, you want to map the TemplateSpecializationType down to the RecordType produced after the instantiation, e.g.,

  if (const RecordType *TI = pType->getAs<RecordType>()) {
    CXXRecordDecl *TIRec = cast<CXXRecordDecl>(TI->getDecl());

Then, you can walk the base classes and fields of TIRec (see RecordDecl and CXXRecordDecl for the appropriate member functions) and see what the instantiation actually looks like.

  - Doug

TST->getAsCXXRecordDecl(). If that's null, the type refers to an unknown
specialization; otherwise, you can ask the decl if it's complete, and if it is
then you can walk over the members and base types. Be aware that a type
can be a known specialization and still be dependent, so the direct (and
indirect) base classes may still be dependent, unresolved types.