This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: Traversing typedef hierarchy in C/C++ tree
Hello Boris,
Boris Kolpackov <boris@codesynthesis.com> a Ãcrit:
> Hi Dodji,
>
> Dodji Seketeli <dodji@seketeli.org> writes:
>
>> Boris Kolpackov <boris@codesynthesis.com> a =C3=A9crit:
>>
>> > struct s {};
>> >
>> > typedef s s_t;
>> > typedef s_t my_s_t;
>> >
>> > my_s_t x;
>> >
>>
>> In G++, let's say that the tree node representing my_s_t is t. Then,
>> DECL_ORIGINAL_TYPE (TYPE_NAME (t)) points to the tree node of s_t. You
>> can walk the relationship "t is a typedef of foo" like that.
>
> Yes, that's exactly what I was looking for. Thanks for the pointer!
You are welcome.
>
> While it works well for the above case, a template argument in the
> path seems to break things. For example:
>
> template <typename T>
> struct wrap
> {
> typedef T w_s;
> };
>
> typedef wrap<my_s_t>::w_s w_s_t;
>
> Now if I traverse from w_s_t using DECL_ORIGINAL_TYPE I get:
>
> w_s_t->w_s->s
>
> Instead of:
>
> w_s_t->w_s->my_s_t->s_t->s
Ah. Indeed. We strip typedefs from template arguments because G++
keeps only one instance of each template specialization. So it chooses
to keep the "canonical" one. In other words wrap<my_s_t> and wrap<s>
ultimately representing the same specialization, G++ only construct one
of them. And it chooses to construct wrap<s> because 's' is the
canonical type here and not "my_s_t". If did choose to keep "my_s_t",
error messages would refer to wrap<my_s_t> even for cases where it
really is wrap<s> that has actually been written by the user. That
would be confusing.
> Do you know if there is a way to get this information?
I don't know, sorry. I am afraid to say that in that particular case,
the information you are looking for is lost.
--
Dodji