[c++] fold offsetof by hand

Richard Henderson rth@redhat.com
Tue Aug 31 08:52:00 GMT 2004


On Mon, Aug 30, 2004 at 04:16:54PM -0700, Mark Mitchell wrote:
> struct S { int i; };
> template <unsigned int I> struct A;
> template <> struct A<0> { typedef int T; };
> template <typename T>
> void f() {
>   A<offsetof (S, i)>::T x
> }

This is way way more complicated than it ought to be.  As far as I
can tell, if everything else in the parser were working properly,
this ought to be just the appended.

But that's not the case.  As far as I can tell, we do not bother
resolving IDs to FIELD_DECLs at all when processing_template_decl
is true.  As evidence of this, I submit the ARROW_EXPR created in
this test case, which is not resolved until tsubst_copy.

I tried fixing that, but kept finding more and more places that
needed changing.

Am I missing something?



r~



Index: parser.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/cp/parser.c,v
retrieving revision 1.241
diff -c -p -d -r1.241 parser.c
*** parser.c	30 Aug 2004 16:03:45 -0000	1.241
--- parser.c	31 Aug 2004 06:49:11 -0000
*************** cp_parser_builtin_offsetof (cp_parser *p
*** 5876,5884 ****
      }
  
   success:
!   /* If we're processing a template, we can't finish the semantics yet.
       Otherwise we can fold the entire expression now.  */
!   if (processing_template_decl)
      expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
    else
      expr = fold_offsetof (expr);
--- 5876,5885 ----
      }
  
   success:
!   /* If the base type (first operand) is type dependent, then we must
!      delay expansion of the expression until that can be resolved.
       Otherwise we can fold the entire expression now.  */
!   if (dependent_type_p (type))
      expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
    else
      expr = fold_offsetof (expr);



More information about the Gcc-patches mailing list