This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[C++ patch] Fix 7598


Hi,
I've installed this obvious patch for 7598. My recent fix of packed
fields broke the constantness of offsetof things. Resurrect some code.

bootd & tested on i686-pc-x-gnu.

nathan
-- 
Dr Nathan Sidwell   ::   http://www.codesourcery.com   ::   CodeSourcery LLC
         'But that's a lie.' - 'Yes it is. What's your point?'
nathan@codesourcery.com : http://www.cs.bris.ac.uk/~nathan/ : nathan@acm.org
2002-08-15  Nathan Sidwell  <nathan@codesourcery.com>

	PR c++/7598
	* typeck.c (build_unary_op): Fold offsetof idiom. Fixes
	regression caused by my 2002-08-08 patch.

Index: cp/typeck.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/cp/typeck.c,v
retrieving revision 1.421
diff -c -3 -p -r1.421 typeck.c
*** cp/typeck.c	9 Aug 2002 19:57:01 -0000	1.421
--- cp/typeck.c	15 Aug 2002 10:21:44 -0000
*************** build_unary_op (code, xarg, noconvert)
*** 4256,4261 ****
--- 4256,4279 ----
  		   TREE_OPERAND (arg, 1));
  	    return error_mark_node;
  	  }
+ 	else if (TREE_CODE (arg) == COMPONENT_REF
+ 		 && TREE_CODE (TREE_OPERAND (arg, 0)) == INDIRECT_REF
+ 		 && (TREE_CODE (TREE_OPERAND (TREE_OPERAND (arg, 0), 0))
+ 		     == INTEGER_CST))
+ 	  {
+ 	    /* offsetof idiom, fold it. */
+ 	    tree field = TREE_OPERAND (arg, 1);
+ 	    tree rval = build_unary_op (ADDR_EXPR, TREE_OPERAND (arg, 0), 0);
+ 	    tree binfo = lookup_base (TREE_TYPE (TREE_TYPE (rval)),
+ 				      decl_type_context (field),
+ 				      ba_check, NULL);
+ 	    
+ 	    rval = build_base_path (PLUS_EXPR, rval, binfo, 1);
+ 	    rval = build1 (NOP_EXPR, argtype, rval);
+ 	    TREE_CONSTANT (rval) = TREE_CONSTANT (TREE_OPERAND (rval, 0));
+ 	    addr = fold (build (PLUS_EXPR, argtype, rval,
+ 				cp_convert (argtype, byte_position (field))));
+ 	  }
  	else
  	  addr = build1 (ADDR_EXPR, argtype, arg);
  
// { dg-do compile }

// Copyright (C) 2002 Free Software Foundation, Inc.
// Contributed by Nathan Sidwell 14 Aug 2002 <nathan@codesourcery.com>

// PR c++ 7598, offsetof broke

struct F
{
  char i;
  char j;
};

static int ary[((unsigned) &((struct F *)0)->j)];

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]