public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: "Fabien Chêne" <fabien.chene@gmail.com>
To: Jason Merrill <jason@redhat.com>
Cc: GCC Patches <gcc-patches@gcc.gnu.org>
Subject: Re: [C++ Patch] for c++/51214
Date: Wed, 23 May 2012 20:38:00 -0000	[thread overview]
Message-ID: <CAFH4-dijEm-1mVQjBVhuJTXWzPS+-U+AMMsO6WETD=w4btA9pg@mail.gmail.com> (raw)
In-Reply-To: <4FA7DF23.7060005@redhat.com>

[-- Attachment #1: Type: text/plain, Size: 1230 bytes --]

2012/5/7 Jason Merrill <jason@redhat.com>:
> On 05/06/2012 04:06 PM, Fabien Chêne wrote:
>>
>> +      if (late_enum_values)
>> +          VEC_safe_push (tree, gc, late_enum_values, decl);
>
> I would think you could walk the TYPE_VALUES list directly, rather than copy
> it into a temporary VEC.

Indeed, let's use good old tree lists. Tested
x86_64-unknown-linux-gnu, OK for trunk and 4.7 ?
gcc/testsuite/ChangeLog

2012-05-23  Fabien Chêne  <fabien@gcc.gnu.org>

	PR c++/51214
	* g++.dg/cpp0x/forw_enum11.C: New.

gcc/cp/ChangeLog

2012-05-23  Fabien Chêne  <fabien@gcc.gnu.org>

	PR c++/51214
	* cp-tree.h (insert_late_enum_def_into_classtype_sorted_fields):
	Declare.
	* class.c (insert_into_classtype_sorted_fields): New.
	(add_enum_fields_to_record_type): New.
	(count_fields): Adjust the comment.
	(add_fields_to_record_type): Likewise.
	(finish_struct_1): Move the code that inserts the fields for the
	sorted case, into insert_into_classtype_sorted_fields, and call
	it.
	(insert_late_enum_def_into_classtype_sorted_fields): Define.
	* decl.c (finish_enum_value_list): Call
	insert_late_enum_def_into_classtype_sorted_fields if a late enum
	definition is encountered.


-- 
Fabien

[-- Attachment #2: 51214.patch --]
[-- Type: application/octet-stream, Size: 6580 bytes --]

Index: gcc/testsuite/g++.dg/cpp0x/forw_enum11.C
===================================================================
--- gcc/testsuite/g++.dg/cpp0x/forw_enum11.C	(revision 0)
+++ gcc/testsuite/g++.dg/cpp0x/forw_enum11.C	(revision 0)
@@ -0,0 +1,23 @@
+// { dg-do compile { target c++11 } }
+
+enum { A = 1 };
+struct T
+{
+  int i1, i2, i3, i4, i5, i6, i7;
+  enum E2 : int;
+
+  void f();
+};
+
+enum T::E2 : int { A1 = A, A2 = 23 };
+
+static_assert(int(T::A1) == 1, "error");
+static_assert(int(T::A2) == 23, "error");
+
+void T::f()
+{
+  static_assert(int(T::A1) == 1, "error");
+  static_assert(int(T::A2) == 23, "error");
+  static_assert(int(A1) == 1, "error");
+  static_assert(int(A2) == 23, "error");
+}
Index: gcc/cp/class.c
===================================================================
--- gcc/cp/class.c	(revision 186674)
+++ gcc/cp/class.c	(working copy)
@@ -139,6 +139,7 @@ static void build_vtbl_initializer (tree
 				    VEC(constructor_elt,gc) **);
 static int count_fields (tree);
 static int add_fields_to_record_type (tree, struct sorted_fields_type*, int);
+static void insert_into_classtype_sorted_fields (tree, tree, int);
 static bool check_bitfield_decl (tree);
 static void check_field_decl (tree, tree, int *, int *, int *);
 static void check_field_decls (tree, tree *, int *, int *);
@@ -2832,8 +2833,9 @@ add_implicitly_declared_members (tree t,
   declare_virt_assop_and_dtor (t);
 }
 
-/* Subroutine of finish_struct_1.  Recursively count the number of fields
-   in TYPE, including anonymous union members.  */
+/* Subroutine of insert_into_classtype_sorted_fields.  Recursively
+   count the number of fields in TYPE, including anonymous union
+   members.  */
 
 static int
 count_fields (tree fields)
@@ -2850,8 +2852,9 @@ count_fields (tree fields)
   return n_fields;
 }
 
-/* Subroutine of finish_struct_1.  Recursively add all the fields in the
-   TREE_LIST FIELDS to the SORTED_FIELDS_TYPE elts, starting at offset IDX.  */
+/* Subroutine of insert_into_classtype_sorted_fields.  Recursively add
+   all the fields in the TREE_LIST FIELDS to the SORTED_FIELDS_TYPE
+   elts, starting at offset IDX.  */
 
 static int
 add_fields_to_record_type (tree fields, struct sorted_fields_type *field_vec, int idx)
@@ -2867,6 +2870,20 @@ add_fields_to_record_type (tree fields,
   return idx;
 }
 
+/* Add all of the enum values of ENUMTYPE, to the FIELD_VEC elts,
+   starting at offset IDX.  */
+
+static int
+add_enum_fields_to_record_type (tree enumtype,
+				struct sorted_fields_type *field_vec,
+				int idx)
+{
+  tree values;
+  for (values = TYPE_VALUES (enumtype); values; values = TREE_CHAIN (values))
+      field_vec->elts[idx++] = TREE_VALUE (values);
+  return idx;
+}
+
 /* FIELD is a bit-field.  We are finishing the processing for its
    enclosing type.  Issue any appropriate messages and set appropriate
    flags.  Returns false if an error has been diagnosed.  */
@@ -5998,7 +6015,6 @@ finish_struct_1 (tree t)
   tree x;
   /* A TREE_LIST.  The TREE_VALUE of each node is a FUNCTION_DECL.  */
   tree virtuals = NULL_TREE;
-  int n_fields = 0;
 
   if (COMPLETE_TYPE_P (t))
     {
@@ -6116,15 +6132,7 @@ finish_struct_1 (tree t)
      ultimately as the search bores through the inheritance
      hierarchy), and we want this failure to occur quickly.  */
 
-  n_fields = count_fields (TYPE_FIELDS (t));
-  if (n_fields > 7)
-    {
-      struct sorted_fields_type *field_vec = sorted_fields_type_new (n_fields);
-      add_fields_to_record_type (TYPE_FIELDS (t), field_vec, 0);
-      qsort (field_vec->elts, n_fields, sizeof (tree),
-	     field_decl_cmp);
-      CLASSTYPE_SORTED_FIELDS (t) = field_vec;
-    }
+  insert_into_classtype_sorted_fields (TYPE_FIELDS (t), t, 8);
 
   /* Complain if one of the field types requires lower visibility.  */
   constrain_class_visibility (t);
@@ -6197,6 +6205,45 @@ finish_struct_1 (tree t)
     }
 }
 
+/* Insert FIELDS into T for the sorted case if the FIELDS count is
+   equal to THRESHOLD or greater than THRESHOLD.  */
+
+static void 
+insert_into_classtype_sorted_fields (tree fields, tree t, int threshold)
+{
+  int n_fields = count_fields (fields);
+  if (n_fields >= threshold)
+    {
+      struct sorted_fields_type *field_vec = sorted_fields_type_new (n_fields);
+      add_fields_to_record_type (fields, field_vec, 0);
+      qsort (field_vec->elts, n_fields, sizeof (tree), field_decl_cmp);
+      CLASSTYPE_SORTED_FIELDS (t) = field_vec;
+    }
+}
+
+/* Insert lately defined enum ENUMTYPE into T for the sorted case.  */
+
+void
+insert_late_enum_def_into_classtype_sorted_fields (tree enumtype, tree t)
+{
+  struct sorted_fields_type *sorted_fields = CLASSTYPE_SORTED_FIELDS (t);
+  if (sorted_fields)
+    {
+      int i;
+      int n_fields
+	= list_length (TYPE_VALUES (enumtype)) + sorted_fields->len;
+      struct sorted_fields_type *field_vec = sorted_fields_type_new (n_fields);
+      
+      for (i = 0; i < sorted_fields->len; ++i)
+	field_vec->elts[i] = sorted_fields->elts[i];
+
+      add_enum_fields_to_record_type (enumtype, field_vec,
+				      sorted_fields->len);
+      qsort (field_vec->elts, n_fields, sizeof (tree), field_decl_cmp);
+      CLASSTYPE_SORTED_FIELDS (t) = field_vec;
+    }
+}
+
 /* When T was built up, the member declarations were added in reverse
    order.  Rearrange them to declaration order.  */
 
Index: gcc/cp/decl.c
===================================================================
--- gcc/cp/decl.c	(revision 186674)
+++ gcc/cp/decl.c	(working copy)
@@ -12385,6 +12385,12 @@ finish_enum_value_list (tree enumtype)
   for (t = TYPE_MAIN_VARIANT (enumtype); t; t = TYPE_NEXT_VARIANT (t))
     TYPE_VALUES (t) = TYPE_VALUES (enumtype);
 
+  if (current_class_type
+      && COMPLETE_TYPE_P (current_class_type)
+      && UNSCOPED_ENUM_P (enumtype))
+    insert_late_enum_def_into_classtype_sorted_fields (enumtype,
+						       current_class_type);
+
   /* Finish debugging output for this type.  */
   rest_of_type_compilation (enumtype, namespace_bindings_p ());
 }
Index: gcc/cp/cp-tree.h
===================================================================
--- gcc/cp/cp-tree.h	(revision 186674)
+++ gcc/cp/cp-tree.h	(working copy)
@@ -4979,6 +4979,7 @@ extern tree* decl_cloned_function_p		(co
 extern void clone_function_decl			(tree, int);
 extern void adjust_clone_args			(tree);
 extern void deduce_noexcept_on_destructor       (tree);
+extern void insert_late_enum_def_into_classtype_sorted_fields (tree, tree);
 
 /* in cvt.c */
 extern tree convert_to_reference		(tree, tree, int, int, tree);

  reply	other threads:[~2012-05-23 20:38 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-02-28 20:57 Fabien Chêne
2012-02-28 22:06 ` Paolo Carlini
2012-02-28 23:22 ` Jason Merrill
2012-02-28 23:28   ` Fabien Chêne
2012-02-29  7:56     ` Fabien Chêne
2012-02-29 15:31     ` Jason Merrill
2012-05-06 20:07       ` Fabien Chêne
2012-05-07 14:41         ` Jason Merrill
2012-05-23 20:38           ` Fabien Chêne [this message]
2012-05-24 13:18             ` Jason Merrill
2012-06-03  4:47               ` Jason Merrill
2012-06-03 15:56                 ` Fabien Chêne
2012-06-03 17:06                   ` Gabriel Dos Reis
2012-06-04 12:41                     ` Fabien Chêne
2012-06-07  5:40                   ` Fabien Chêne
2012-06-27 18:26                     ` Fabien Chêne
2012-07-17 11:57                       ` Richard Guenther

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAFH4-dijEm-1mVQjBVhuJTXWzPS+-U+AMMsO6WETD=w4btA9pg@mail.gmail.com' \
    --to=fabien.chene@gmail.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=jason@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).