[cfe-dev] [AST] how to check if a type is template specification type and get the template paramter?

Bruno Ricci via cfe-dev cfe-dev at lists.llvm.org
Tue Aug 4 04:32:35 PDT 2020

On 04/08/2020 12:08, Yafei Liu via cfe-dev wrote:
> Hi, all.
> I want to check if a given `clang::Type` is a std::vector<xxx> and get the type of xxx.
> I tried this `if (const auto *tst = llvm::dyn_cast<clang::TemplateSpecializationType>(type)) {}` but the condition is always false, it seems that TemplateSpecializationType is not what I think it is.
> So can what I want be done?

Assuming the template specialization type is not dependent (so you have a TemplateSpecializationType
and not a DependentTemplateSpecializationType), use Type::getAs<TemplateSpecializationType> to
look through sugar. Consider:

namespace N {
  template <typename T> struct S {};

using type = N::S<int>;

The AST for the type alias declaration is:

-TypeAliasDecl 0x559082acee80 <line:5:1, col:22> col:7 type 'N::S<int>':'N::S<int>'
  `-ElaboratedType 0x559082acedc0 'N::S<int>' sugar                <-------------------
    `-TemplateSpecializationType 0x559082aced70 'S<int>' sugar S
      |-TemplateArgument type 'int'
      | `-BuiltinType 0x559082a882e0 'int'
      `-RecordType 0x559082aced40 'N::S<int>'
        `-ClassTemplateSpecialization 0x559082acec40 'S'

> _______________________________________________
> cfe-dev mailing list
> cfe-dev at lists.llvm.org
> https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev

More information about the cfe-dev mailing list