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: PR 10515


G++ (accidentally) handling the designated initializer extension
correctly; it started silently generating wrong code.

This patch fixes that problem.

Tested on i686-pc-linux-gnu, applied on the mainline and on the
branch.

--
Mark Mitchell
CodeSourcery, LLC
mark@codesourcery.com

2003-04-29  Mark Mitchell  <mark@codesourcery.com>

	PR c++/10515
	* g++.dg/ext/desig1.C: New test.
	* g++.dg/ext/init1.C: Likewise.

2003-04-29  Mark Mitchell  <mark@codesourcery.com>

	PR c++/10515
	* cp-tree.h (lookup_field_1): Declare it.
	* search.c (lookup_field_1): Make it public.
	* decl.c (reshape_init): Handle designated initializers.

Index: cp/cp-tree.h
===================================================================
RCS file: /cvs/gcc/gcc/gcc/cp/cp-tree.h,v
retrieving revision 1.840
diff -c -5 -p -r1.840 cp-tree.h
*** cp/cp-tree.h	28 Apr 2003 06:06:58 -0000	1.840
--- cp/cp-tree.h	29 Apr 2003 17:57:04 -0000
*************** extern bool emit_tinfo_decl (tree);
*** 4012,4021 ****
--- 4012,4022 ----
  extern tree lookup_base (tree, tree, base_access, base_kind *);
  extern int types_overlap_p			(tree, tree);
  extern tree get_vbase				(tree, tree);
  extern tree get_dynamic_cast_base_type          (tree, tree);
  extern int accessible_p                         (tree, tree);
+ extern tree lookup_field_1                      (tree, tree, bool);
  extern tree lookup_field			(tree, tree, int, bool);
  extern int lookup_fnfields_1                    (tree, tree);
  extern tree lookup_fnfields			(tree, tree, int);
  extern tree lookup_member			(tree, tree, int, bool);
  extern int look_for_overrides			(tree, tree);
Index: decl.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/cp/decl.c,v
retrieving revision 1.1042
retrieving revision 1.1043
diff -c -5 -p -r1.1042 -r1.1043
*** decl.c	28 Apr 2003 06:13:23 -0000	1.1042
--- decl.c	29 Apr 2003 07:13:31 -0000	1.1043
*************** reshape_init (tree type, tree *initp)
*** 7549,7562 ****
  	    }
  	  else
  	    {
  	      /* Loop through the initializable fields, gathering
  		 initializers.  */
!               /* FIXME support non-trivial labeled initializers.  */
! 	      while (*initp && field)
  		{
  		  tree field_init;
  
  		  field_init = reshape_init (TREE_TYPE (field), initp);
  		  TREE_CHAIN (field_init) = CONSTRUCTOR_ELTS (new_init);
  		  CONSTRUCTOR_ELTS (new_init) = field_init;
  		  /* [dcl.init.aggr] 
--- 7549,7575 ----
  	    }
  	  else
  	    {
  	      /* Loop through the initializable fields, gathering
  		 initializers.  */
! 	      while (*initp)
  		{
  		  tree field_init;
+ 
+ 		  /* Handle designated initializers, as an extension.  */
+ 		  if (TREE_PURPOSE (*initp))
+ 		    {
+ 		      if (pedantic)
+ 			pedwarn ("ISO C++ does not allow designated initializers");
+ 		      field = lookup_field_1 (type, TREE_PURPOSE (*initp),
+ 					      /*want_type=*/false);
+ 		      if (!field || TREE_CODE (field) != FIELD_DECL)
+ 			error ("`%T' has no non-static data member named `%D'",
+ 			       type, TREE_PURPOSE (*initp));
+ 		    }
+ 		  if (!field)
+ 		    break;
  
  		  field_init = reshape_init (TREE_TYPE (field), initp);
  		  TREE_CHAIN (field_init) = CONSTRUCTOR_ELTS (new_init);
  		  CONSTRUCTOR_ELTS (new_init) = field_init;
  		  /* [dcl.init.aggr] 
Index: cp/search.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/cp/search.c,v
retrieving revision 1.260
diff -c -5 -p -r1.260 search.c
*** cp/search.c	22 Apr 2003 22:53:00 -0000	1.260
--- cp/search.c	29 Apr 2003 17:57:06 -0000
*************** struct vbase_info 
*** 79,89 ****
    /* A pointer to a complete object of the indicated TYPE.  */
    tree decl_ptr;
    tree inits;
  };
  
- static tree lookup_field_1 (tree, tree, bool);
  static tree dfs_check_overlap (tree, void *);
  static tree dfs_no_overlap_yet (tree, int, void *);
  static base_kind lookup_base_r (tree, tree, base_access,
  				bool, bool, bool, tree *);
  static int dynamic_cast_base_recurse (tree, tree, bool, tree *);
--- 79,88 ----
*************** get_dynamic_cast_base_type (tree subtype
*** 428,438 ****
  
  /* Do a 1-level search for NAME as a member of TYPE.  The caller must
     figure out whether it can access this field.  (Since it is only one
     level, this is reasonable.)  */
  
! static tree
  lookup_field_1 (tree type, tree name, bool want_type)
  {
    register tree field;
  
    if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
--- 427,437 ----
  
  /* Do a 1-level search for NAME as a member of TYPE.  The caller must
     figure out whether it can access this field.  (Since it is only one
     level, this is reasonable.)  */
  
! tree
  lookup_field_1 (tree type, tree name, bool want_type)
  {
    register tree field;
  
    if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
Index: testsuite/g++.dg/ext/desig1.C
===================================================================
RCS file: testsuite/g++.dg/ext/desig1.C
diff -N testsuite/g++.dg/ext/desig1.C
*** /dev/null	1 Jan 1970 00:00:00 -0000
--- testsuite/g++.dg/ext/desig1.C	29 Apr 2003 17:57:06 -0000
***************
*** 0 ****
--- 1,24 ----
+ // { dg-options "" }
+ 
+ struct a {
+         int x;
+ };
+ 
+ struct b {
+         int x;
+         int y;
+ };
+ 
+ struct foo {
+         union {
+                 struct a a;
+                 struct b b;
+         } u;
+ };
+ 
+ int main(void)
+ {
+         struct foo bar = { u: { b: { x: 0, y: 0, }}};
+         (void)bar;
+         return 0;
+ }
Index: testsuite/g++.dg/ext/init1.C
===================================================================
RCS file: /cvs/gcc/gcc/gcc/testsuite/g++.dg/ext/init1.C,v
retrieving revision 1.1
diff -c -5 -p -r1.1 init1.C
*** testsuite/g++.dg/ext/init1.C	18 Feb 2003 18:16:40 -0000	1.1
--- testsuite/g++.dg/ext/init1.C	29 Apr 2003 17:57:06 -0000
***************
*** 1,5 ****
--- 1,6 ----
  // PR c++/9623
  // Test for trivial use of named initializer extension
+ // { dg-options "" }
  
  struct S { int x;  int y; };
  S s = { x:1, y:2 };


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