[cfe-dev] Understanding why TemplateTypeParmType::getDecl returns NULL (What is a canonical type)

Chris Smith chrsmith at google.com
Wed Jun 8 13:37:07 PDT 2011


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 <typename T>
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->castAs<clang::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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20110608/9b298a2c/attachment.html>


More information about the cfe-dev mailing list