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

Richard Smith via cfe-dev cfe-dev at lists.llvm.org
Thu Aug 20 12:33:55 PDT 2020


On Tue, 4 Aug 2020 at 04:43, Yafei Liu via cfe-dev <cfe-dev at lists.llvm.org>
wrote:

> 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


> On Tue, Aug 4, 2020 at 7:32 PM Bruno Ricci <riccibrun at gmail.com> wrote:
>
>> 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
>> >
>>
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at lists.llvm.org
> https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20200820/819fff2c/attachment.html>


More information about the cfe-dev mailing list