From 1887fb461b2c370aeeedaba95b503fde9b2c70f9 Mon Sep 17 00:00:00 2001 From: Nathan Sidwell Date: Wed, 6 Sep 2017 13:14:16 +0000 Subject: [PATCH] name-lookup.c (count_fields): Rename to ... * 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. From-SVN: r251794 --- gcc/cp/ChangeLog | 12 +++++ gcc/cp/name-lookup.c | 112 +++++++++++++++++++++++-------------------- 2 files changed, 73 insertions(+), 51 deletions(-) diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index b9fab9153a9..ed98902fd42 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,5 +1,17 @@ 2017-09-06 Nathan Sidwell + * 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. + * name-lookup.h (lookup_fnfields_slot_nolazy, lookup_fnfields_slot): Rename to ... (get_class_binding_direct, get_class_binding): ... here. diff --git a/gcc/cp/name-lookup.c b/gcc/cp/name-lookup.c index 81635fffdcf..ccfb630e4dd 100644 --- a/gcc/cp/name-lookup.c +++ b/gcc/cp/name-lookup.c @@ -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. */ +/* 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 int -add_fields_to_record_type (tree fields, struct sorted_fields_type *field_vec, - int idx) +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; } -- 2.30.2