Comment 6 for bug 662324

Revision history for this message
In , Dodji-gcc (dodji-gcc) wrote :

Following the (IMHO correect) analysis of Jakub, an interesting
question would be:

  Why is the type of *c1 a typedef variant of "A" instead
  of being just "A"?

It's actually the "self reference type" of A. In struct A, the
standard asks to inject the name A into the struct A itself, so that
looking up A::A or C::A succeeds. G++ creates a special typedef of A
(named self reference type in G++ speak) and injects that typedef into
A.

So from inside struct C, when considering "A *c1", the lookup of A
returns the self reference type of A. I think after the lookup
succeeds, G++ should retain A as the type of *c1; not the self
reference. Otherwise that confuses the debug info emitter as this bug
suggests.

I am about to test the patch below that hopefully does what I would
want. It's actually the second hunk that fixes this case because it
does what I am saying for the simple-type-specifier production.

The first hunk is something I noticed while looking at this issue. The
initial code in check_elaborated_type_specifier actually tries to do
what I am saying (for the elaborated-type-specifier production) but I
believe it fails in doing so, probably because the way self references
are represented has changed since the code was written.

diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c
index fb5ca7f..feba130 100644
--- a/gcc/cp/decl.c
+++ b/gcc/cp/decl.c
@@ -10902,7 +10902,7 @@ check_elaborated_type_specifier (enum tag_types tag_code,
      name lookup will find the TYPE_DECL for the implicit "S::S"
      typedef. Adjust for that here. */
   if (DECL_SELF_REFERENCE_P (decl))
- decl = TYPE_NAME (TREE_TYPE (decl));
+ decl = TYPE_NAME (DECL_ORIGINAL_TYPE (decl));

   type = TREE_TYPE (decl);

diff --git a/gcc/cp/parser.c b/gcc/cp/parser.c
index 6a9e4d7..d70c621 100644
--- a/gcc/cp/parser.c
+++ b/gcc/cp/parser.c
@@ -12791,6 +12791,11 @@ cp_parser_simple_type_specifier (cp_parser* parser,
       /* Otherwise, look for a type-name. */
       else
  type = cp_parser_type_name (parser);
+
+ if (type && TREE_CODE (type) == TYPE_DECL
+ && DECL_SELF_REFERENCE_P (type))
+ type = TYPE_NAME (DECL_ORIGINAL_TYPE (type));
+
       /* Keep track of all name-lookups performed in class scopes. */
       if (type
    && !global_p