[c++] fold offsetof by hand

Richard Henderson rth@redhat.com
Fri Aug 27 23:00:00 GMT 2004


Yaye.  This was easier than I thought.  All it takes for C++ is to
delay resolution in the template case.

Tested on i686-linux and committed.


r~


        * cp-tree.def (OFFSETOF_EXPR): New.
        * parser.c (cp_parser_builtin_offsetof): Either built an
        OFFSETOF_EXPR, or call fold_offsetof immediately.
        * pt.c (tsubst_expr): Handle OFFSETOF_EXPR.

Index: cp-tree.def
===================================================================
RCS file: /cvs/gcc/gcc/gcc/cp/cp-tree.def,v
retrieving revision 1.88
diff -c -p -d -r1.88 cp-tree.def
*** cp-tree.def	20 Jul 2004 23:20:04 -0000	1.88
--- cp-tree.def	27 Aug 2004 21:45:41 -0000
*************** DEFTREECODE (TAG_DEFN, "tag_defn", 'e', 
*** 282,287 ****
--- 282,290 ----
  
  DEFTREECODE (TINST_LEVEL, "TINST_LEVEL", 'x', 0)
  
+ /* Represents an 'offsetof' expression during template expansion.  */
+ DEFTREECODE (OFFSETOF_EXPR, "offsetof_expr", 'e', 1)
+ 
  /*
  Local variables:
  mode:c
Index: parser.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/cp/parser.c,v
retrieving revision 1.239
diff -c -p -d -r1.239 parser.c
*** parser.c	27 Aug 2004 17:59:28 -0000	1.239
--- parser.c	27 Aug 2004 21:45:42 -0000
*************** cp_parser_builtin_offsetof (cp_parser *p
*** 5876,5893 ****
      }
  
   success:
!   /* We've finished the parsing, now finish with the semantics.  At present
!      we're just mirroring the traditional macro implementation.  Better
!      would be to do the lowering of the ADDR_EXPR to flat pointer arithmetic
!      here rather than in build_x_unary_op.  */
!   
!   expr = (build_reinterpret_cast 
! 	  (build_reference_type (cp_build_qualified_type 
! 				 (char_type_node, 
! 				  TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)), 
! 	   expr));
!   expr = build_x_unary_op (ADDR_EXPR, expr);
!   expr = build_reinterpret_cast (size_type_node, expr);
  
   failure:
    parser->integral_constant_expression_p = save_ice_p;
--- 5876,5887 ----
      }
  
   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);
  
   failure:
    parser->integral_constant_expression_p = save_ice_p;
Index: pt.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/cp/pt.c,v
retrieving revision 1.916
diff -c -p -d -r1.916 pt.c
*** pt.c	27 Aug 2004 17:59:29 -0000	1.916
--- pt.c	27 Aug 2004 21:45:43 -0000
*************** tsubst_expr (tree t, tree args, tsubst_f
*** 8092,8097 ****
--- 8092,8102 ----
        tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
        break;
  
+     case OFFSETOF_EXPR:
+       t = tsubst_copy_and_build (TREE_OPERAND (t, 0), args, complain,
+ 				 in_decl, false);
+       return fold_offsetof (t);
+ 
      default:
        if (!STATEMENT_CODE_P (TREE_CODE (t)))
  	return tsubst_copy_and_build (t, args, complain, in_decl,



More information about the Gcc-patches mailing list