aboutsummaryrefslogtreecommitdiff
path: root/gcc/c
diff options
context:
space:
mode:
authorNathan Sidwell <nathan@acm.org>2017-09-12 14:25:17 +0000
committerNathan Sidwell <nathan@gcc.gnu.org>2017-09-12 14:25:17 +0000
commit6836632e96e894513c605a2eb6e2dfae77a9a069 (patch)
treed01c2a6b39749135ebf8d37977e049108baac8f6 /gcc/c
parent13bdca744bda9321d6e0f4beca7bf9ac2e0870c0 (diff)
downloadgcc-6836632e96e894513c605a2eb6e2dfae77a9a069.zip
gcc-6836632e96e894513c605a2eb6e2dfae77a9a069.tar.gz
gcc-6836632e96e894513c605a2eb6e2dfae77a9a069.tar.bz2
c-common.c (field_decl_cmp, [...]): Move to c/c-decl.c.
c-family/ * c-common.c (field_decl_cmp, resort_data, resort_field_decl_cmp, resort_sorted_fields): Move to c/c-decl.c. * c-common.h (field_decl_cmp, resort_sorted_fields): Delete. (struct sorted_fields_type): Move to c/c-lang.h. c/ * c-decl.c (field_decl_cmp, resort_data, resort_field_decl_cmp, resort_sorted_fields): Moved from c-family/c-common.c. * c-lang.h (struct sorted_fields_type): Moved from c-family/c-common.h. From-SVN: r252023
Diffstat (limited to 'gcc/c')
-rw-r--r--gcc/c/ChangeLog6
-rw-r--r--gcc/c/c-decl.c67
-rw-r--r--gcc/c/c-lang.h7
3 files changed, 80 insertions, 0 deletions
diff --git a/gcc/c/ChangeLog b/gcc/c/ChangeLog
index 2d25c48..d55b5ac 100644
--- a/gcc/c/ChangeLog
+++ b/gcc/c/ChangeLog
@@ -1,3 +1,9 @@
+2017-09-12 Nathan Sidwell <nathan@acm.org>
+
+ * c-decl.c (field_decl_cmp, resort_data, resort_field_decl_cmp,
+ resort_sorted_fields): Moved from c-family/c-common.c.
+ * c-lang.h (struct sorted_fields_type): Moved from c-family/c-common.h.
+
2017-09-01 Joseph Myers <joseph@codesourcery.com>
PR c/82071
diff --git a/gcc/c/c-decl.c b/gcc/c/c-decl.c
index d526f0e..7121498 100644
--- a/gcc/c/c-decl.c
+++ b/gcc/c/c-decl.c
@@ -7840,6 +7840,26 @@ warn_cxx_compat_finish_struct (tree fieldlist, enum tree_code code,
b->in_struct = 0;
}
+/* Function to help qsort sort FIELD_DECLs by name order. */
+
+static int
+field_decl_cmp (const void *x_p, const void *y_p)
+{
+ const tree *const x = (const tree *) x_p;
+ const tree *const y = (const tree *) y_p;
+
+ if (DECL_NAME (*x) == DECL_NAME (*y))
+ /* A nontype is "greater" than a type. */
+ return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
+ if (DECL_NAME (*x) == NULL_TREE)
+ return -1;
+ if (DECL_NAME (*y) == NULL_TREE)
+ return 1;
+ if (DECL_NAME (*x) < DECL_NAME (*y))
+ return -1;
+ return 1;
+}
+
/* Fill in the fields of a RECORD_TYPE or UNION_TYPE node, T.
LOC is the location of the RECORD_TYPE or UNION_TYPE's definition.
FIELDLIST is a chain of FIELD_DECL nodes for the fields.
@@ -8165,6 +8185,53 @@ finish_struct (location_t loc, tree t, tree fieldlist, tree attributes,
return t;
}
+static struct {
+ gt_pointer_operator new_value;
+ void *cookie;
+} resort_data;
+
+/* This routine compares two fields like field_decl_cmp but using the
+pointer operator in resort_data. */
+
+static int
+resort_field_decl_cmp (const void *x_p, const void *y_p)
+{
+ const tree *const x = (const tree *) x_p;
+ const tree *const y = (const tree *) y_p;
+
+ if (DECL_NAME (*x) == DECL_NAME (*y))
+ /* A nontype is "greater" than a type. */
+ return (TREE_CODE (*y) == TYPE_DECL) - (TREE_CODE (*x) == TYPE_DECL);
+ if (DECL_NAME (*x) == NULL_TREE)
+ return -1;
+ if (DECL_NAME (*y) == NULL_TREE)
+ return 1;
+ {
+ tree d1 = DECL_NAME (*x);
+ tree d2 = DECL_NAME (*y);
+ resort_data.new_value (&d1, resort_data.cookie);
+ resort_data.new_value (&d2, resort_data.cookie);
+ if (d1 < d2)
+ return -1;
+ }
+ return 1;
+}
+
+/* Resort DECL_SORTED_FIELDS because pointers have been reordered. */
+
+void
+resort_sorted_fields (void *obj,
+ void * ARG_UNUSED (orig_obj),
+ gt_pointer_operator new_value,
+ void *cookie)
+{
+ struct sorted_fields_type *sf = (struct sorted_fields_type *) obj;
+ resort_data.new_value = new_value;
+ resort_data.cookie = cookie;
+ qsort (&sf->elts[0], sf->len, sizeof (tree),
+ resort_field_decl_cmp);
+}
+
/* Lay out the type T, and its element type, and so on. */
static void
diff --git a/gcc/c/c-lang.h b/gcc/c/c-lang.h
index 872bd58..8ecafb3 100644
--- a/gcc/c/c-lang.h
+++ b/gcc/c/c-lang.h
@@ -22,6 +22,13 @@ along with GCC; see the file COPYING3. If not see
#include "c-family/c-common.h"
+/* In a RECORD_TYPE, a sorted array of the fields of the type, not a
+ tree for size reasons. */
+struct GTY(()) sorted_fields_type {
+ int len;
+ tree GTY((length ("%h.len"))) elts[1];
+};
+
struct GTY(()) lang_type {
/* In a RECORD_TYPE, a sorted array of the fields of the type. */
struct sorted_fields_type * GTY ((reorder ("resort_sorted_fields"))) s;