[cfe-dev] Type exploration in clang AST

Douglas Gregor dgregor at apple.com
Mon Dec 13 08:12:18 PST 2010


On Dec 13, 2010, at 4:03 AM, Olivier wrote:

> Hi,
> 
> 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 
> clang::TemplateSpecializationType.
> 
> 
> void process( clang::TemplateSpecializationType const *pType )
> {
>  // Get the associated template declaration.
>  clang::TemplateDecl const *pDecl
>    = pType->getTemplateName().getAsTemplateDecl();
> 
>  if(pDecl)
>  {
>    // 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' ?


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



More information about the cfe-dev mailing list