Parsing field types from ClassTemplateSpecializationDecl

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Parsing field types from ClassTemplateSpecializationDecl

Sumner, Brian via cfe-dev
Hello,

I'm trying to parse fields of an explicitly instantiated template, like this:
~~~
template <class T>
class Foo
{
T bar;
};

template<> class Foo<float>;
~~~

In the example above, I'd like to know that "Foo<float>" has a field "bar" of type "float".

I am able to access "ClassTemplateSpecializationDecl" of "Foo<float>", but it reports no fields. It does contain a list of template arguments (a "float" for the specific example above), but since there are no fields I am unsure how to interpret them.

I can also access "CXXRecordDecl" of "Foo<T>" by calling "ClassTemplateSpecializationDecl::getSpecializedTemplate()->GetTemplatedDecl()". This does report the "bar" field, but I do not appear to have information about its actual type from the explicit instantiation.

How can I learn the type of "bar" in an explicit instantiation of "Foo<T>"?

Thanks!

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

Re: Parsing field types from ClassTemplateSpecializationDecl

Sumner, Brian via cfe-dev
Not sure if I understand correctly, but you specialize your template
without any fields in your example.

What you want to do is I think:

   template class Foo<float>;

which yields the correct result for me:

teemperor@ftlserver ~/test> cat test.cpp
template <class T>
class Foo
{
       T bar;
};

template class Foo<float>;
teemperor@ftlserver ~/test> clang++ -cc1 -fcolor-diagnostics -ast-dump
-x c++ test.cpp
TranslationUnitDecl 0x5578e347d9a0 <<invalid sloc>> <invalid sloc>
|-TypedefDecl 0x5578e347df30 <<invalid sloc>> <invalid sloc> implicit
__int128_t '__int128'
| `-BuiltinType 0x5578e347dc10 '__int128'
|-TypedefDecl 0x5578e347df98 <<invalid sloc>> <invalid sloc> implicit
__uint128_t 'unsigned __int128'
| `-BuiltinType 0x5578e347dc30 'unsigned __int128'
|-TypedefDecl 0x5578e347e2d8 <<invalid sloc>> <invalid sloc> implicit
__NSConstantString 'struct __NSConstantString_tag'
| `-RecordType 0x5578e347e080 'struct __NSConstantString_tag'
|   `-CXXRecord 0x5578e347dfe8 '__NSConstantString_tag'
|-TypedefDecl 0x5578e347e370 <<invalid sloc>> <invalid sloc> implicit
__builtin_ms_va_list 'char *'
| `-PointerType 0x5578e347e330 'char *'
|   `-BuiltinType 0x5578e347da30 'char'
|-TypedefDecl 0x5578e34b2320 <<invalid sloc>> <invalid sloc> implicit
__builtin_va_list 'struct __va_list_tag [1]'
| `-ConstantArrayType 0x5578e347e650 'struct __va_list_tag [1]' 1
|   `-RecordType 0x5578e347e460 'struct __va_list_tag'
|     `-CXXRecord 0x5578e347e3c0 '__va_list_tag'
|-ClassTemplateDecl 0x5578e34b24b8 <test.cpp:1:1, line:5:1> line:2:7 Foo
| |-TemplateTypeParmDecl 0x5578e34b2370 <line:1:11, col:17> col:17
referenced class T
| |-CXXRecordDecl 0x5578e34b2420 <line:2:1, line:5:1> line:2:7 class
Foo definition
| | |-CXXRecordDecl 0x5578e34b2728 <col:1, col:7> col:7 implicit class Foo
| | `-FieldDecl 0x5578e34b27d0 <line:4:2, col:4> col:4 bar 'T'
| `-ClassTemplateSpecialization 0x5578e34b2850 'Foo'
`-ClassTemplateSpecializationDecl 0x5578e34b2850 <line:7:1, col:25>
col:16 class Foo definition
 |-TemplateArgument type 'float'
 |-CXXRecordDecl 0x5578e34b2a48 prev 0x5578e34b2850 <line:2:1, col:7>
col:7 implicit class Foo
 `-FieldDecl 0x5578e34b2b18 <line:4:2, col:4> col:4 bar 'float':'float'


- Raphael

2017-07-19 16:30 GMT+02:00 Marko Pintera via cfe-dev <[hidden email]>:

> Hello,
>
> I'm trying to parse fields of an explicitly instantiated template, like
> this:
> ~~~
> template <class T>
> class Foo
> {
> T bar;
> };
>
> template<> class Foo<float>;
> ~~~
>
> In the example above, I'd like to know that "Foo<float>" has a field "bar"
> of type "float".
>
> I am able to access "ClassTemplateSpecializationDecl" of "Foo<float>", but
> it reports no fields. It does contain a list of template arguments (a
> "float" for the specific example above), but since there are no fields I am
> unsure how to interpret them.
>
> I can also access "CXXRecordDecl" of "Foo<T>" by calling
> "ClassTemplateSpecializationDecl::getSpecializedTemplate()->GetTemplatedDecl()".
> This does report the "bar" field, but I do not appear to have information
> about its actual type from the explicit instantiation.
>
> How can I learn the type of "bar" in an explicit instantiation of "Foo<T>"?
>
> Thanks!
>
> _______________________________________________
> cfe-dev mailing list
> [hidden email]
> http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
>
_______________________________________________
cfe-dev mailing list
[hidden email]
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Parsing field types from ClassTemplateSpecializationDecl

Sumner, Brian via cfe-dev
You are right, embarrassingly my test case was incorrectly written, it works now. Thank you.

On 19 July 2017 at 16:56, Raphael Isemann <[hidden email]> wrote:
Not sure if I understand correctly, but you specialize your template
without any fields in your example.

What you want to do is I think:

   template class Foo<float>;

which yields the correct result for me:

teemperor@ftlserver ~/test> cat test.cpp
template <class T>
class Foo
{
       T bar;
};

template class Foo<float>;
teemperor@ftlserver ~/test> clang++ -cc1 -fcolor-diagnostics -ast-dump
-x c++ test.cpp
TranslationUnitDecl 0x5578e347d9a0 <<invalid sloc>> <invalid sloc>
|-TypedefDecl 0x5578e347df30 <<invalid sloc>> <invalid sloc> implicit
__int128_t '__int128'
| `-BuiltinType 0x5578e347dc10 '__int128'
|-TypedefDecl 0x5578e347df98 <<invalid sloc>> <invalid sloc> implicit
__uint128_t 'unsigned __int128'
| `-BuiltinType 0x5578e347dc30 'unsigned __int128'
|-TypedefDecl 0x5578e347e2d8 <<invalid sloc>> <invalid sloc> implicit
__NSConstantString 'struct __NSConstantString_tag'
| `-RecordType 0x5578e347e080 'struct __NSConstantString_tag'
|   `-CXXRecord 0x5578e347dfe8 '__NSConstantString_tag'
|-TypedefDecl 0x5578e347e370 <<invalid sloc>> <invalid sloc> implicit
__builtin_ms_va_list 'char *'
| `-PointerType 0x5578e347e330 'char *'
|   `-BuiltinType 0x5578e347da30 'char'
|-TypedefDecl 0x5578e34b2320 <<invalid sloc>> <invalid sloc> implicit
__builtin_va_list 'struct __va_list_tag [1]'
| `-ConstantArrayType 0x5578e347e650 'struct __va_list_tag [1]' 1
|   `-RecordType 0x5578e347e460 'struct __va_list_tag'
|     `-CXXRecord 0x5578e347e3c0 '__va_list_tag'
|-ClassTemplateDecl 0x5578e34b24b8 <test.cpp:1:1, line:5:1> line:2:7 Foo
| |-TemplateTypeParmDecl 0x5578e34b2370 <line:1:11, col:17> col:17
referenced class T
| |-CXXRecordDecl 0x5578e34b2420 <line:2:1, line:5:1> line:2:7 class
Foo definition
| | |-CXXRecordDecl 0x5578e34b2728 <col:1, col:7> col:7 implicit class Foo
| | `-FieldDecl 0x5578e34b27d0 <line:4:2, col:4> col:4 bar 'T'
| `-ClassTemplateSpecialization 0x5578e34b2850 'Foo'
`-ClassTemplateSpecializationDecl 0x5578e34b2850 <line:7:1, col:25>
col:16 class Foo definition
 |-TemplateArgument type 'float'
 |-CXXRecordDecl 0x5578e34b2a48 prev 0x5578e34b2850 <line:2:1, col:7>
col:7 implicit class Foo
 `-FieldDecl 0x5578e34b2b18 <line:4:2, col:4> col:4 bar 'float':'float'


- Raphael

2017-07-19 16:30 GMT+02:00 Marko Pintera via cfe-dev <[hidden email]>:
> Hello,
>
> I'm trying to parse fields of an explicitly instantiated template, like
> this:
> ~~~
> template <class T>
> class Foo
> {
> T bar;
> };
>
> template<> class Foo<float>;
> ~~~
>
> In the example above, I'd like to know that "Foo<float>" has a field "bar"
> of type "float".
>
> I am able to access "ClassTemplateSpecializationDecl" of "Foo<float>", but
> it reports no fields. It does contain a list of template arguments (a
> "float" for the specific example above), but since there are no fields I am
> unsure how to interpret them.
>
> I can also access "CXXRecordDecl" of "Foo<T>" by calling
> "ClassTemplateSpecializationDecl::getSpecializedTemplate()->GetTemplatedDecl()".
> This does report the "bar" field, but I do not appear to have information
> about its actual type from the explicit instantiation.
>
> How can I learn the type of "bar" in an explicit instantiation of "Foo<T>"?
>
> Thanks!
>
> _______________________________________________
> cfe-dev mailing list
> [hidden email]
> http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
>


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