Hi, all.
I want to check if a given clang::Type
is a std::vector 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?
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'
Hi Bruno, it works, are there suggestions saying that always using Type::getAs<> rather than llvm::dyn_cast<>?
Hi Bruno, it works, are there suggestions saying that always using Type::getAs<> rather than llvm::dyn_cast<>?
Yes, generally you should use T->getAs<…> instead of dyn_cast<…>(T) if you want to inspect the semantics of the type rather than the outermost level of type sugar. See also http://clang.llvm.org/docs/InternalsManual.html#canonical-types