diff mbox series

[C++] class FIELD_VEC initialization

Message ID 613620bc-e929-446c-f307-74daa97d6f24@acm.org
State New
Headers show
Series [C++] class FIELD_VEC initialization | expand

Commit Message

Nathan Sidwell Sept. 6, 2017, 1:17 p.m. UTC
Here's some cleanup of the SORTED_FIELDS vector initialization.  Some 
function renaming, to be more specific.  The functionality change is a 
minor bug in late enums.  We only add them to the field vec, if there's 
already a field vec.  But of course, their addition could have cause the 
class's TYPE_FIELD length to cross the threshold for wanting a field vector.

Applied to trunk.

nathan
diff mbox series

Patch

2017-09-06  Nathan Sidwell  <nathan@acm.org>

	* name-lookup.c (count_fields): Rename to ...
	(count_class_fields): ... here.  Take a class, don't count
	NULL-named fields.
	(add_fields_to_record_type): Rename to ...
	(field_vec_append_class_fields): ... here.  Take a class, don't
	add NULL-named fields.
	(add_enum_fields_to_record_type): Rename to ...
	(field_vec_append_enum_values): ... here.
	(set_class_bindings): Adjust, assert we added expected number.
	(insert_late_enum_def_bindings): Reimplement.  Create vector if
	there are now sufficient entries.

Index: name-lookup.c
===================================================================
--- name-lookup.c	(revision 251782)
+++ name-lookup.c	(working copy)
@@ -1452,59 +1452,57 @@  sorted_fields_type_new (int n)
   return sft;
 }
 
-/* Subroutine of insert_into_classtype_sorted_fields.  Recursively
-   count the number of fields in TYPE, including anonymous union
-   members.  */
+/* Recursively count the number of fields in KLASS, including anonymous
+   union members.  */
 
-static int
-count_fields (tree fields)
+static unsigned
+count_class_fields (tree klass)
 {
-  tree x;
-  int n_fields = 0;
-  for (x = fields; x; x = DECL_CHAIN (x))
-    {
-      if (DECL_DECLARES_FUNCTION_P (x))
-	/* Functions are dealt with separately.  */;
-      else if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
-	n_fields += count_fields (TYPE_FIELDS (TREE_TYPE (x)));
-      else
-	n_fields += 1;
-    }
+  unsigned n_fields = 0;
+
+  for (tree fields = TYPE_FIELDS (klass); fields; fields = DECL_CHAIN (fields))
+    if (DECL_DECLARES_FUNCTION_P (fields))
+      /* Functions are dealt with separately.  */;
+    else if (TREE_CODE (fields) == FIELD_DECL
+	     && ANON_AGGR_TYPE_P (TREE_TYPE (fields)))
+      n_fields += count_class_fields (TREE_TYPE (fields));
+    else if (DECL_NAME (fields))
+      n_fields += 1;
+
   return n_fields;
 }
 
-/* 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)
+/* Append all the nonfunction members fields of KLASS to FIELD_VEC
+   starting at IDX. Recurse for anonymous members.  The array must
+   have space.  Returns the next available index.  */
+
+static unsigned
+field_vec_append_class_fields (struct sorted_fields_type *field_vec,
+			       tree klass, unsigned idx)
 {
-  tree x;
-  for (x = fields; x; x = DECL_CHAIN (x))
-    {
-      if (DECL_DECLARES_FUNCTION_P (x))
-	/* Functions are handled separately.  */;
-      else if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
-	idx = add_fields_to_record_type (TYPE_FIELDS (TREE_TYPE (x)), field_vec, idx);
-      else
-	field_vec->elts[idx++] = x;
-    }
+  for (tree fields = TYPE_FIELDS (klass); fields; fields = DECL_CHAIN (fields))
+    if (DECL_DECLARES_FUNCTION_P (fields))
+      /* Functions are handled separately.  */;
+    else if (TREE_CODE (fields) == FIELD_DECL
+	     && ANON_AGGR_TYPE_P (TREE_TYPE (fields)))
+      idx = field_vec_append_class_fields (field_vec, TREE_TYPE (fields), idx);
+    else if (DECL_NAME (fields))
+      field_vec->elts[idx++] = fields;
+
   return idx;
 }
 
-/* Add all of the enum values of ENUMTYPE, to the FIELD_VEC elts,
-   starting at offset IDX.  */
+/* Append all of the enum values of ENUMTYPE to FIELD_VEC starting at IDX.
+   FIELD_VEC must have space.  */
 
-static int
-add_enum_fields_to_record_type (tree enumtype,
-				struct sorted_fields_type *field_vec,
-				int idx)
+static unsigned
+field_vec_append_enum_values (struct sorted_fields_type *field_vec,
+			      tree enumtype, unsigned idx)
 {
-  tree values;
-  for (values = TYPE_VALUES (enumtype); values; values = TREE_CHAIN (values))
+  for (tree values = TYPE_VALUES (enumtype);
+       values; values = TREE_CHAIN (values))
     field_vec->elts[idx++] = TREE_VALUE (values);
+
   return idx;
 }
 
@@ -1518,12 +1516,12 @@  set_class_bindings (tree klass)
     qsort (method_vec->address (), method_vec->length (),
 	   sizeof (tree), method_name_cmp);
 
-  tree fields = TYPE_FIELDS (klass);
-  int n_fields = count_fields (fields);
+  int n_fields = count_class_fields (klass);
   if (n_fields >= 8)
     {
       struct sorted_fields_type *field_vec = sorted_fields_type_new (n_fields);
-      add_fields_to_record_type (fields, field_vec, 0);
+      unsigned idx = field_vec_append_class_fields (field_vec, klass, 0);
+      gcc_assert (idx == unsigned (field_vec->len));
       qsort (field_vec->elts, n_fields, sizeof (tree), field_decl_cmp);
       CLASSTYPE_SORTED_FIELDS (klass) = field_vec;
     }
@@ -1534,19 +1532,31 @@  set_class_bindings (tree klass)
 void
 insert_late_enum_def_bindings (tree klass, tree enumtype)
 {
+  unsigned n_fields;
   struct sorted_fields_type *sorted_fields = CLASSTYPE_SORTED_FIELDS (klass);
+
+  /* The enum values will already be on the TYPE_FIELDS, so don't
+     count them twice.  */
   if (sorted_fields)
+    n_fields = list_length (TYPE_VALUES (enumtype)) + sorted_fields->len;
+  else
+    n_fields = count_class_fields (klass);
+
+  if (n_fields >= 8)
     {
-      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];
+      unsigned idx;
+
+      if (sorted_fields)
+	{
+	  for (idx = 0; idx < unsigned (sorted_fields->len); ++idx)
+	    field_vec->elts[idx] = sorted_fields->elts[idx];
 
-      add_enum_fields_to_record_type (enumtype, field_vec,
-				      sorted_fields->len);
+	  idx = field_vec_append_enum_values (field_vec, enumtype, idx);
+	}
+      else
+	idx = field_vec_append_class_fields (field_vec, klass, 0);
+      gcc_assert (idx == unsigned (field_vec->len));
       qsort (field_vec->elts, n_fields, sizeof (tree), field_decl_cmp);
       CLASSTYPE_SORTED_FIELDS (klass) = field_vec;
     }