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]

Patch for PR c++/80


It's taken a while, but I've finally managed to finish off this patch
which implements packed enums for C++ programs.  The documentation
states that this extended syntax is supported by the front-end, but it
is not.  The PR outlines the problem in greater detail.

I am not convinced that the changes to the Bison grammar specification
are safe -- I would appreciate some additional review there.  A test
case appears beneath the patch.


[gcc/cp/ChangeLog]
2001-06-19  Ben Elliston  <bje@redhat.com>

	PR c++/80
	* decl.c (finish_enum): New "attributes" argument; pass it to
	cplus_decl_attributes.  Use a narrower type if the enum is packed.
	* cp-tree.h (finish_enum): Adjust prototype.
	* parse.y (enum_head): New non-terminal.
	(structsp): Use it. Enums now may be preceded or followed by
	optional attributes -- pass their chained tree to finish_enum().
	* pt.c (tsubst_enum): Pass NULL_TREE for the new argument.

Index: cp-tree.h
===================================================================
RCS file: /cvs/cvsfiles/devo/gcc/cp/cp-tree.h,v
retrieving revision 1.421
diff -u -r1.421 cp-tree.h
--- cp-tree.h	2001/06/04 18:09:44	1.421
+++ cp-tree.h	2001/06/26 14:43:06
@@ -3864,7 +3864,7 @@
 extern tree xref_tag_from_type			PARAMS ((tree, tree, int));
 extern void xref_basetypes			PARAMS ((tree, tree, tree, tree));
 extern tree start_enum				PARAMS ((tree));
-extern void finish_enum				PARAMS ((tree));
+extern void finish_enum				PARAMS ((tree, tree));
 extern void build_enumerator			PARAMS ((tree, tree, tree));
 extern int start_function			PARAMS ((tree, tree, tree, int));
 extern tree finish_function			PARAMS ((int));
Index: decl.c
===================================================================
RCS file: /cvs/cvsfiles/devo/gcc/cp/decl.c,v
retrieving revision 1.885
diff -u -r1.885 decl.c
--- decl.c	2001/06/04 18:09:44	1.885
+++ decl.c	2001/06/26 14:43:18
@@ -12983,8 +12983,9 @@
    ENUMTYPE is the type object and VALUES a list of name-value pairs.  */
 
 void
-finish_enum (enumtype)
+finish_enum (enumtype, attributes)
      tree enumtype;
+     tree attributes;
 {
   tree pair;
   tree minnode;
@@ -12995,6 +12996,8 @@
   int highprec; 
   int precision;
 
+  cplus_decl_attributes (enumtype, attributes, NULL_TREE);
+  
   /* We built up the VALUES in reverse order.  */
   TYPE_VALUES (enumtype) = nreverse (TYPE_VALUES (enumtype));
 
@@ -13073,7 +13076,7 @@
   else
     fixup_signed_type (enumtype);
 
-  if (flag_short_enums || (precision > TYPE_PRECISION (integer_type_node)))
+  if (flag_short_enums || TYPE_PACKED (enumtype) || (precision > TYPE_PRECISION (integer_type_node)))
     /* Use the width of the narrowest normal C type which is wide
        enough.  */
     TYPE_PRECISION (enumtype) = TYPE_PRECISION (type_for_size
Index: parse.y
===================================================================
RCS file: /cvs/cvsfiles/devo/gcc/cp/parse.y,v
retrieving revision 1.386
diff -u -r1.386 parse.y
--- parse.y	2001/06/04 18:09:47	1.386
+++ parse.y	2001/06/26 14:43:20
@@ -340,7 +340,7 @@
 %type <ttype> init initlist maybeasm maybe_init defarg defarg1
 %type <ttype> asm_operands nonnull_asm_operands asm_operand asm_clobbers
 %type <ttype> maybe_attribute attributes attribute attribute_list attrib
-%type <ttype> any_word
+%type <ttype> any_word enum_head
 
 %type <itype> save_lineno
 %type <ttype> simple_stmt simple_if
@@ -2240,22 +2240,29 @@
 		{ do_pending_defargs (); }
 	;
 
+enum_head:
+	  ENUM
+		{ $$ = NULL_TREE; }
+	| ENUM attributes
+		{ $$ = $2; }
+	;
+
 structsp:
-	  ENUM identifier '{'
+	  enum_head identifier '{'
 		{ $<ttype>$ = current_enum_type;
 		  current_enum_type = start_enum ($2); }
-	  enumlist_opt '}'
+	  enumlist_opt '}' maybe_attribute 
 		{ $$.t = current_enum_type;
-		  finish_enum (current_enum_type);
+		  finish_enum (current_enum_type, chainon ($1, $7));
 		  $$.new_type_flag = 1;
 		  current_enum_type = $<ttype>4;
 		  check_for_missing_semicolon ($$.t); }
-	| ENUM '{'
+	| enum_head '{'
 		{ $<ttype>$ = current_enum_type;
 		  current_enum_type = start_enum (make_anon_name ()); }
-	  enumlist_opt '}'
+	  enumlist_opt '}' maybe_attribute 
                 { $$.t = current_enum_type;
-		  finish_enum (current_enum_type);
+		  finish_enum (current_enum_type, chainon ($1, $6));
 		  $$.new_type_flag = 1;
 		  current_enum_type = $<ttype>3;
 		  check_for_missing_semicolon ($$.t); }
Index: pt.c
===================================================================
RCS file: /cvs/cvsfiles/devo/gcc/cp/pt.c,v
retrieving revision 1.398
diff -u -r1.398 pt.c
--- pt.c	2001/06/04 18:09:47	1.398
+++ pt.c	2001/06/26 14:43:26
@@ -10119,7 +10119,7 @@
       build_enumerator (TREE_PURPOSE (e), value, newtag); 
     }
 
-  finish_enum (newtag);
+  finish_enum (newtag, NULL_TREE);
   DECL_SOURCE_LINE (TYPE_NAME (newtag)) = DECL_SOURCE_LINE (TYPE_NAME (tag));
   DECL_SOURCE_FILE (TYPE_NAME (newtag)) = DECL_SOURCE_FILE (TYPE_NAME (tag));
 }



[gcc/testsuite/ChangeLog]
2001-06-19  Ben Elliston  <bje@redhat.com>

	* g++.old-deja/g++.other/enum5.C: New test.

PR c++/80 test case


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