Understanding why TemplateTypeParmType::getDecl returns NULL (What is a canonical type)

Hello,

I am encountering some odd behavior when writing a Clang frontend action and was hoping someone could explain the behavior I am seeing.

Consider the following code:

template
T identity(T input) {
return input;
}

In my frontend action I would like to identify the return type of a function. Which, in the above code should be the template type parameter “T”.

bool MyASTVisitor::VisitFunctionDecl(clang::FunctionDecl* decl) {
LG << “Checking function return type (” << decl->getNameAsString() << “)”;
const clang::QualType return_type = decl->getResultType();

LG << “Origin decl:”;
decl->dump();

LG << "Dumping return type with class name: " << return_type->getTypeClassName();
return_type->dump();

if (return_type->isTemplateTypeParmType()) {
const clang::TemplateTypeParmType* template_param_type =
return_type->castAsclang::TemplateTypeParmType();
CHECK_NOTNULL(template_param_type);

const clang::TemplateTypeParmDecl* template_param_type_decl =
template_param_type->getDecl();
CHECK_NOTNULL(template_param_type_decl);

template_param_type_decl->dump();
}

return true;
}

However, TemplateTypeParmType::getDecl always returns NULL. Digging into the Clang code, this is because the template type parameter is ‘canonical’. http://clang.llvm.org/doxygen/Type_8h_source.html#l03073

TemplateTypeParmDecl *getDecl() const {
return isCanonicalUnqualified() ? 0 : TTPDecl;
}

In this context does canonical actually mean? That the template parameter isn’t introduced via a typedef? Is there any way I can massage the QualType in such a way that I can get access to its original declaration and IdentifierInfo?

Thanks,
-Chris