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

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

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

Hubert Tong via cfe-dev
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?

_______________________________________________
cfe-dev mailing list
[hidden email]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: [AST] how to check if a type is template specification type and get the template paramter?

Hubert Tong via cfe-dev
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
> [hidden email]
> https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
>
_______________________________________________
cfe-dev mailing list
[hidden email]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: [AST] how to check if a type is template specification type and get the template paramter?

Hubert Tong via cfe-dev
Hi Bruno, it works, are there suggestions saying that always using Type::getAs<> rather than llvm::dyn_cast<>?

On Tue, Aug 4, 2020 at 7:32 PM Bruno Ricci <[hidden email]> 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
> [hidden email]
> https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
>

_______________________________________________
cfe-dev mailing list
[hidden email]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: [AST] how to check if a type is template specification type and get the template paramter?

Hubert Tong via cfe-dev
On Tue, 4 Aug 2020 at 04:43, Yafei Liu via cfe-dev <[hidden email]> 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 <[hidden email]> 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
> [hidden email]
> https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
>
_______________________________________________
cfe-dev mailing list
[hidden email]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev

_______________________________________________
cfe-dev mailing list
[hidden email]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev