[1/4] gdb: add type::num_fields / type::set_num_fields

Message ID 20200519184441.7838-1-simon.marchi@efficios.com
State New
Headers show
Series
  • [1/4] gdb: add type::num_fields / type::set_num_fields
Related show

Commit Message

Add the `num_fields` and `set_num_fields` methods on `struct type`, in
order to remove the `TYPE_NFIELDS` macro.  In this patch, the
`TYPE_NFIELDS` macro is changed to use `type::num_fields`, so all the
call sites that are used to set the number of fields are changed to use
`type::set_num_fields`.  The next patch will remove `TYPE_NFIELDS`
completely.

I think that in the future, we should consider making the interface of
`struct type` better.  For example, right now it's possible for the
number of fields property and the actual number of fields set to be out
of sync.  However, I want to keep the existing behavior in this patch,
just translate from macros to methods.

gdb/ChangeLog:

	* gdbtypes.h (struct type) <num_fields, set_num_fields>: New
	methods.
	(TYPE_NFIELDS): Use type::num_fields.  Change all call sites
	that modify the number of fields to use type::set_num_fields
	instead.
---
 gdb/ada-lang.c     | 11 +++++------
 gdb/buildsym.c     |  2 +-
 gdb/coffread.c     | 12 ++++++------
 gdb/ctfread.c      |  4 ++--
 gdb/dwarf2/read.c  | 12 ++++++------
 gdb/eval.c         |  2 +-
 gdb/gdbtypes.c     | 18 +++++++++---------
 gdb/gdbtypes.h     | 14 +++++++++++++-
 gdb/gnu-v3-abi.c   |  4 ++--
 gdb/mdebugread.c   |  6 +++---
 gdb/rust-lang.c    |  2 +-
 gdb/stabsread.c    |  8 ++++----
 gdb/windows-tdep.c |  2 +-
 13 files changed, 54 insertions(+), 43 deletions(-)

-- 
2.26.2

Comments

Tom Tromey May 22, 2020, 8:16 p.m. | #1
>>>>> "Simon" == Simon Marchi via Gdb-patches <gdb-patches@sourceware.org> writes:


Simon> Add the `num_fields` and `set_num_fields` methods on `struct type`, in
Simon> order to remove the `TYPE_NFIELDS` macro.  In this patch, the
Simon> `TYPE_NFIELDS` macro is changed to use `type::num_fields`, so all the
Simon> call sites that are used to set the number of fields are changed to use
Simon> `type::set_num_fields`.  The next patch will remove `TYPE_NFIELDS`
Simon> completely.

This looks good.

Simon> I think that in the future, we should consider making the interface of
Simon> `struct type` better.  For example, right now it's possible for the
Simon> number of fields property and the actual number of fields set to be out
Simon> of sync.  However, I want to keep the existing behavior in this patch,
Simon> just translate from macros to methods.

Fully agreed.

Tom

Patch

diff --git a/gdb/ada-lang.c b/gdb/ada-lang.c
index 825549d86e9..b740b3f8894 100644
--- a/gdb/ada-lang.c
+++ b/gdb/ada-lang.c
@@ -8026,7 +8026,6 @@  empty_record (struct type *templ)
   struct type *type = alloc_type_copy (templ);
 
   type->set_code (TYPE_CODE_STRUCT);
-  TYPE_NFIELDS (type) = 0;
   TYPE_FIELDS (type) = NULL;
   INIT_NONE_SPECIFIC (type);
   type->set_name ("<empty>");
@@ -8083,7 +8082,7 @@  ada_template_to_fixed_record_type_1 (struct type *type,
   rtype = alloc_type_copy (type);
   rtype->set_code (TYPE_CODE_STRUCT);
   INIT_NONE_SPECIFIC (rtype);
-  TYPE_NFIELDS (rtype) = nfields;
+  rtype->set_num_fields (nfields);
   TYPE_FIELDS (rtype) = (struct field *)
     TYPE_ALLOC (rtype, nfields * sizeof (struct field));
   memset (TYPE_FIELDS (rtype), 0, sizeof (struct field) * nfields);
@@ -8251,7 +8250,7 @@  ada_template_to_fixed_record_type_1 (struct type *type,
         {
           for (f = variant_field + 1; f < TYPE_NFIELDS (rtype); f += 1)
             TYPE_FIELDS (rtype)[f - 1] = TYPE_FIELDS (rtype)[f];
-          TYPE_NFIELDS (rtype) -= 1;
+	  rtype->set_num_fields (rtype->num_fields () - 1);
         }
       else
         {
@@ -8358,7 +8357,7 @@  template_to_static_fixed_type (struct type *type0)
 	      TYPE_TARGET_TYPE (type0) = type = alloc_type_copy (type0);
 	      type->set_code (type0->code ());
 	      INIT_NONE_SPECIFIC (type);
-	      TYPE_NFIELDS (type) = nfields;
+	      type->set_num_fields (nfields);
 	      TYPE_FIELDS (type) = (struct field *)
 		TYPE_ALLOC (type, nfields * sizeof (struct field));
 	      memcpy (TYPE_FIELDS (type), TYPE_FIELDS (type0),
@@ -8407,7 +8406,7 @@  to_record_with_fixed_variant_part (struct type *type, const gdb_byte *valaddr,
   rtype = alloc_type_copy (type);
   rtype->set_code (TYPE_CODE_STRUCT);
   INIT_NONE_SPECIFIC (rtype);
-  TYPE_NFIELDS (rtype) = nfields;
+  rtype->set_num_fields (nfields);
   TYPE_FIELDS (rtype) =
     (struct field *) TYPE_ALLOC (rtype, nfields * sizeof (struct field));
   memcpy (TYPE_FIELDS (rtype), TYPE_FIELDS (type),
@@ -8430,7 +8429,7 @@  to_record_with_fixed_variant_part (struct type *type, const gdb_byte *valaddr,
 
       for (f = variant_field + 1; f < nfields; f += 1)
         TYPE_FIELDS (rtype)[f - 1] = TYPE_FIELDS (rtype)[f];
-      TYPE_NFIELDS (rtype) -= 1;
+      rtype->set_num_fields (rtype->num_fields () - 1);
     }
   else
     {
diff --git a/gdb/buildsym.c b/gdb/buildsym.c
index b9bcc33080a..f66607cd736 100644
--- a/gdb/buildsym.c
+++ b/gdb/buildsym.c
@@ -271,7 +271,7 @@  buildsym_compunit::finish_block_internal
 	    }
 	  if (nparams > 0)
 	    {
-	      TYPE_NFIELDS (ftype) = nparams;
+	      ftype->set_num_fields (nparams);
 	      TYPE_FIELDS (ftype) = (struct field *)
 		TYPE_ALLOC (ftype, nparams * sizeof (struct field));
 
diff --git a/gdb/coffread.c b/gdb/coffread.c
index fc44e53cc4c..59d6d231151 100644
--- a/gdb/coffread.c
+++ b/gdb/coffread.c
@@ -1455,7 +1455,7 @@  patch_type (struct type *type, struct type *real_type)
   int field_size = TYPE_NFIELDS (real_target) * sizeof (struct field);
 
   TYPE_LENGTH (target) = TYPE_LENGTH (real_target);
-  TYPE_NFIELDS (target) = TYPE_NFIELDS (real_target);
+  target->set_num_fields (TYPE_NFIELDS (real_target));
   TYPE_FIELDS (target) = (struct field *) TYPE_ALLOC (target,
 						      field_size);
 
@@ -1884,7 +1884,7 @@  decode_base_type (struct coff_symbol *cs,
 	  INIT_CPLUS_SPECIFIC (type);
 	  TYPE_LENGTH (type) = 0;
 	  TYPE_FIELDS (type) = 0;
-	  TYPE_NFIELDS (type) = 0;
+	  type->set_num_fields (0);
 	}
       else
 	{
@@ -1904,7 +1904,7 @@  decode_base_type (struct coff_symbol *cs,
 	  INIT_CPLUS_SPECIFIC (type);
 	  TYPE_LENGTH (type) = 0;
 	  TYPE_FIELDS (type) = 0;
-	  TYPE_NFIELDS (type) = 0;
+	  type->set_num_fields (0);
 	}
       else
 	{
@@ -1925,7 +1925,7 @@  decode_base_type (struct coff_symbol *cs,
 	  type->set_name (NULL);
 	  TYPE_LENGTH (type) = 0;
 	  TYPE_FIELDS (type) = 0;
-	  TYPE_NFIELDS (type) = 0;
+	  type->set_num_fields (0);
 	}
       else
 	{
@@ -2041,7 +2041,7 @@  coff_read_struct_type (int index, int length, int lastsym,
     }
   /* Now create the vector of fields, and record how big it is.  */
 
-  TYPE_NFIELDS (type) = nfields;
+  type->set_num_fields (nfields);
   TYPE_FIELDS (type) = (struct field *)
     TYPE_ALLOC (type, sizeof (struct field) * nfields);
 
@@ -2121,7 +2121,7 @@  coff_read_enum_type (int index, int length, int lastsym,
   else /* Assume ints.  */
     TYPE_LENGTH (type) = gdbarch_int_bit (gdbarch) / TARGET_CHAR_BIT;
   type->set_code (TYPE_CODE_ENUM);
-  TYPE_NFIELDS (type) = nsyms;
+  type->set_num_fields (nsyms);
   TYPE_FIELDS (type) = (struct field *)
     TYPE_ALLOC (type, sizeof (struct field) * nsyms);
 
diff --git a/gdb/ctfread.c b/gdb/ctfread.c
index b5bdb5f9cf1..31f927e3bc9 100644
--- a/gdb/ctfread.c
+++ b/gdb/ctfread.c
@@ -308,7 +308,7 @@  attach_fields_to_type (struct ctf_field_info *fip, struct type *type)
     return;
 
   /* Record the field count, allocate space for the array of fields.  */
-  TYPE_NFIELDS (type) = nfields;
+  type->set_num_fields (nfields);
   TYPE_FIELDS (type)
     = (struct field *) TYPE_ZALLOC (type, sizeof (struct field) * nfields);
 
@@ -1139,7 +1139,7 @@  add_stt_func (struct ctf_context *ccp, unsigned long idx)
   ftype = get_tid_type (ccp->of, tid);
   if (finfo.ctc_flags & CTF_FUNC_VARARG)
     TYPE_VARARGS (ftype) = 1;
-  TYPE_NFIELDS (ftype) = argc;
+  ftype->set_num_fields (argc);
 
   /* If argc is 0, it has a "void" type.  */
   if (argc != 0)
diff --git a/gdb/dwarf2/read.c b/gdb/dwarf2/read.c
index 2ab7c5c3313..89ca038414e 100644
--- a/gdb/dwarf2/read.c
+++ b/gdb/dwarf2/read.c
@@ -9326,7 +9326,7 @@  quirk_rust_enum (struct type *type, struct objfile *objfile)
       /* Smash this type to be a structure type.  We have to do this
 	 because the type has already been recorded.  */
       type->set_code (TYPE_CODE_STRUCT);
-      TYPE_NFIELDS (type) = 3;
+      type->set_num_fields (3);
       /* Save the field we care about.  */
       struct field saved_field = TYPE_FIELD (type, 0);
       TYPE_FIELDS (type)
@@ -9425,7 +9425,7 @@  quirk_rust_enum (struct type *type, struct objfile *objfile)
       memcpy (new_fields + 1, TYPE_FIELDS (type),
 	      TYPE_NFIELDS (type) * sizeof (struct field));
       TYPE_FIELDS (type) = new_fields;
-      TYPE_NFIELDS (type) = TYPE_NFIELDS (type) + 1;
+      type->set_num_fields (TYPE_NFIELDS (type) + 1);
 
       /* Install the discriminant at index 0 in the union.  */
       TYPE_FIELD (type, 0) = *disr_field;
@@ -9473,7 +9473,7 @@  quirk_rust_enum (struct type *type, struct objfile *objfile)
 	  struct type *sub_type = TYPE_FIELD_TYPE (type, i);
 	  if (TYPE_NFIELDS (sub_type) > 0)
 	    {
-	      --TYPE_NFIELDS (sub_type);
+	      sub_type->set_num_fields (sub_type->num_fields () - 1);
 	      ++TYPE_FIELDS (sub_type);
 	    }
 	  TYPE_FIELD_NAME (type, i) = variant_name;
@@ -14768,7 +14768,7 @@  dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
 
   /* Record the field count, allocate space for the array of fields,
      and create blank accessibility bitfields if necessary.  */
-  TYPE_NFIELDS (type) = nfields;
+  type->set_num_fields (nfields);
   TYPE_FIELDS (type) = (struct field *)
     TYPE_ZALLOC (type, sizeof (struct field) * nfields);
 
@@ -15897,7 +15897,7 @@  update_enumeration_type_from_children (struct die_info *die,
 
   if (!fields.empty ())
     {
-      TYPE_NFIELDS (type) = fields.size ();
+      type->set_num_fields (fields.size ());
       TYPE_FIELDS (type) = (struct field *)
 	TYPE_ALLOC (type, sizeof (struct field) * fields.size ());
       memcpy (TYPE_FIELDS (type), fields.data (),
@@ -17048,7 +17048,7 @@  read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
 	}
 
       /* Allocate storage for parameters and fill them in.  */
-      TYPE_NFIELDS (ftype) = nparams;
+      ftype->set_num_fields (nparams);
       TYPE_FIELDS (ftype) = (struct field *)
 	TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
 
diff --git a/gdb/eval.c b/gdb/eval.c
index ea086027880..023b629c1aa 100644
--- a/gdb/eval.c
+++ b/gdb/eval.c
@@ -682,7 +682,7 @@  fake_method::fake_method (type_instance_flags flags,
      neither an objfile nor a gdbarch.  As a result we must manually
      allocate memory for auxiliary fields, and free the memory ourselves
      when we are done with it.  */
-  TYPE_NFIELDS (type) = num_types;
+  type->set_num_fields (num_types);
   TYPE_FIELDS (type) = (struct field *)
     xzalloc (sizeof (struct field) * num_types);
 
diff --git a/gdb/gdbtypes.c b/gdb/gdbtypes.c
index 68d4c0c4a24..f42657ab8f9 100644
--- a/gdb/gdbtypes.c
+++ b/gdb/gdbtypes.c
@@ -562,7 +562,7 @@  lookup_function_type_with_arguments (struct type *type,
 	TYPE_PROTOTYPED (fn) = 1;
     }
 
-  TYPE_NFIELDS (fn) = nparams;
+  fn->set_num_fields (nparams);
   TYPE_FIELDS (fn)
     = (struct field *) TYPE_ZALLOC (fn, nparams * sizeof (struct field));
   for (i = 0; i < nparams; ++i)
@@ -1281,7 +1281,7 @@  create_array_type_with_stride (struct type *result_type,
   result_type->set_code (TYPE_CODE_ARRAY);
   TYPE_TARGET_TYPE (result_type) = element_type;
 
-  TYPE_NFIELDS (result_type) = 1;
+  result_type->set_num_fields (1);
   TYPE_FIELDS (result_type) =
     (struct field *) TYPE_ZALLOC (result_type, sizeof (struct field));
   TYPE_INDEX_TYPE (result_type) = range_type;
@@ -1380,7 +1380,7 @@  create_set_type (struct type *result_type, struct type *domain_type)
     result_type = alloc_type_copy (domain_type);
 
   result_type->set_code (TYPE_CODE_SET);
-  TYPE_NFIELDS (result_type) = 1;
+  result_type->set_num_fields (1);
   TYPE_FIELDS (result_type)
     = (struct field *) TYPE_ZALLOC (result_type, sizeof (struct field));
 
@@ -1550,7 +1550,7 @@  smash_to_method_type (struct type *type, struct type *self_type,
   TYPE_TARGET_TYPE (type) = to_type;
   set_type_self_type (type, self_type);
   TYPE_FIELDS (type) = args;
-  TYPE_NFIELDS (type) = nargs;
+  type->set_num_fields (nargs);
   if (varargs)
     TYPE_VARARGS (type) = 1;
   TYPE_LENGTH (type) = 1;	/* In practice, this is never needed.  */
@@ -2402,8 +2402,8 @@  compute_variant_fields (struct type *type,
   for (const auto &part : parts)
     compute_variant_fields_inner (type, addr_stack, part, flags);
 
-  TYPE_NFIELDS (resolved_type) = std::count (flags.begin (), flags.end (),
-					     true);
+  resolved_type->set_num_fields
+    (std::count (flags.begin (), flags.end (), true));
   TYPE_FIELDS (resolved_type)
     = (struct field *) TYPE_ALLOC (resolved_type,
 				   TYPE_NFIELDS (resolved_type)
@@ -5558,7 +5558,7 @@  arch_flags_type (struct gdbarch *gdbarch, const char *name, int bit)
 
   type = arch_type (gdbarch, TYPE_CODE_FLAGS, bit, name);
   TYPE_UNSIGNED (type) = 1;
-  TYPE_NFIELDS (type) = 0;
+  type->set_num_fields (0);
   /* Pre-allocate enough space assuming every field is one bit.  */
   TYPE_FIELDS (type)
     = (struct field *) TYPE_ZALLOC (type, bit * sizeof (struct field));
@@ -5587,7 +5587,7 @@  append_flags_type_field (struct type *type, int start_bitpos, int nr_bits,
   TYPE_FIELD_TYPE (type, field_nr) = field_type;
   SET_FIELD_BITPOS (TYPE_FIELD (type, field_nr), start_bitpos);
   TYPE_FIELD_BITSIZE (type, field_nr) = nr_bits;
-  ++TYPE_NFIELDS (type);
+  type->set_num_fields (type->num_fields () + 1);
 }
 
 /* Special version of append_flags_type_field to add a flag field.
@@ -5630,7 +5630,7 @@  append_composite_type_field_raw (struct type *t, const char *name,
 {
   struct field *f;
 
-  TYPE_NFIELDS (t) = TYPE_NFIELDS (t) + 1;
+  t->set_num_fields (TYPE_NFIELDS (t) + 1);
   TYPE_FIELDS (t) = XRESIZEVEC (struct field, TYPE_FIELDS (t),
 				TYPE_NFIELDS (t));
   f = &(TYPE_FIELDS (t)[TYPE_NFIELDS (t) - 1]);
diff --git a/gdb/gdbtypes.h b/gdb/gdbtypes.h
index ba8e6f837aa..da8d5e2a119 100644
--- a/gdb/gdbtypes.h
+++ b/gdb/gdbtypes.h
@@ -901,6 +901,18 @@  struct type
     this->main_type->name = name;
   }
 
+  /* Get the number of fields of this type.  */
+  int num_fields () const
+  {
+    return this->main_type->nfields;
+  }
+
+  /* Set the number of fields of this type.  */
+  void set_num_fields (int num_fields)
+  {
+    this->main_type->nfields = num_fields;
+  }
+
   /* * Return the dynamic property of the requested KIND from this type's
      list of dynamic properties.  */
   dynamic_prop *dyn_prop (dynamic_prop_node_kind kind) const;
@@ -1446,7 +1458,7 @@  extern unsigned type_align (struct type *);
    space in struct type.  */
 extern bool set_type_align (struct type *, ULONGEST);
 
-#define TYPE_NFIELDS(thistype) TYPE_MAIN_TYPE(thistype)->nfields
+#define TYPE_NFIELDS(thistype) ((thistype)->num_fields ())
 #define TYPE_FIELDS(thistype) TYPE_MAIN_TYPE(thistype)->flds_bnds.fields
 
 #define TYPE_INDEX_TYPE(type) TYPE_FIELD_TYPE (type, 0)
diff --git a/gdb/gnu-v3-abi.c b/gdb/gnu-v3-abi.c
index df5818b300e..b0544319383 100644
--- a/gdb/gnu-v3-abi.c
+++ b/gdb/gnu-v3-abi.c
@@ -165,7 +165,7 @@  build_gdb_vtable_type (struct gdbarch *arch)
   gdb_assert (field == (field_list + 4));
 
   t = arch_type (arch, TYPE_CODE_STRUCT, offset * TARGET_CHAR_BIT, NULL);
-  TYPE_NFIELDS (t) = field - field_list;
+  t->set_num_fields (field - field_list);
   TYPE_FIELDS (t) = field_list;
   t->set_name ("gdb_gnu_v3_abi_vtable");
   INIT_CPLUS_SPECIFIC (t);
@@ -1054,7 +1054,7 @@  build_std_type_info_type (struct gdbarch *arch)
   gdb_assert (field == (field_list + 2));
 
   t = arch_type (arch, TYPE_CODE_STRUCT, offset * TARGET_CHAR_BIT, NULL);
-  TYPE_NFIELDS (t) = field - field_list;
+  t->set_num_fields (field - field_list);
   TYPE_FIELDS (t) = field_list;
   t->set_name ("gdb_gnu_v3_type_info");
   INIT_CPLUS_SPECIFIC (t);
diff --git a/gdb/mdebugread.c b/gdb/mdebugread.c
index 9ad9c664bcb..1b499ccad34 100644
--- a/gdb/mdebugread.c
+++ b/gdb/mdebugread.c
@@ -1017,7 +1017,7 @@  parse_symbol (SYMR *sh, union aux_ext *ax, char *ext_sh, int bigend,
 
 	t->set_code (type_code);
 	TYPE_LENGTH (t) = sh->value;
-	TYPE_NFIELDS (t) = nfields;
+	t->set_num_fields (nfields);
 	TYPE_FIELDS (t) = f = ((struct field *)
 			       TYPE_ALLOC (t,
 					   nfields * sizeof (struct field)));
@@ -1186,7 +1186,7 @@  parse_symbol (SYMR *sh, union aux_ext *ax, char *ext_sh, int bigend,
 		{
 		  struct block_iterator iter;
 
-		  TYPE_NFIELDS (ftype) = nparams;
+		  ftype->set_num_fields (nparams);
 		  TYPE_FIELDS (ftype) = (struct field *)
 		    TYPE_ALLOC (ftype, nparams * sizeof (struct field));
 
@@ -1733,7 +1733,7 @@  parse_type (int fd, union aux_ext *ax, unsigned int aux_index, int *bs,
   /* Deal with range types.  */
   if (t->bt == btRange)
     {
-      TYPE_NFIELDS (tp) = 0;
+      tp->set_num_fields (0);
       TYPE_RANGE_DATA (tp) = ((struct range_bounds *)
 			  TYPE_ZALLOC (tp, sizeof (struct range_bounds)));
       TYPE_LOW_BOUND (tp) = AUX_GET_DNLOW (bigend, ax);
diff --git a/gdb/rust-lang.c b/gdb/rust-lang.c
index 2c76762eff2..f0bea374ecb 100644
--- a/gdb/rust-lang.c
+++ b/gdb/rust-lang.c
@@ -984,7 +984,7 @@  rust_composite_type (struct type *original,
   result->set_code (TYPE_CODE_STRUCT);
   result->set_name (name);
 
-  TYPE_NFIELDS (result) = nfields;
+  result->set_num_fields (nfields);
   TYPE_FIELDS (result)
     = (struct field *) TYPE_ZALLOC (result, nfields * sizeof (struct field));
 
diff --git a/gdb/stabsread.c b/gdb/stabsread.c
index a632856404b..c3da9a239b4 100644
--- a/gdb/stabsread.c
+++ b/gdb/stabsread.c
@@ -1009,7 +1009,7 @@  define_symbol (CORE_ADDR valu, const char *string, int desc, int type,
 	      TYPE_FIELD_TYPE (ftype, nparams) = ptype;
 	      TYPE_FIELD_ARTIFICIAL (ftype, nparams++) = 0;
 	    }
-	  TYPE_NFIELDS (ftype) = nparams;
+	  ftype->set_num_fields (nparams);
 	  TYPE_PROTOTYPED (ftype) = 1;
 	}
       break;
@@ -1850,7 +1850,7 @@  read_type (const char **pp, struct objfile *objfile)
           for (t = arg_types, i = num_args - 1; t; t = t->next, i--)
             TYPE_FIELD_TYPE (func_type, i) = t->type;
         }
-        TYPE_NFIELDS (func_type) = num_args;
+        func_type->set_num_fields (num_args);
         TYPE_PROTOTYPED (func_type) = 1;
 
         type = func_type;
@@ -3308,7 +3308,7 @@  attach_fields_to_type (struct stab_field_info *fip, struct type *type,
      non-public fields.  Record the field count, allocate space for the
      array of fields, and create blank visibility bitfields if necessary.  */
 
-  TYPE_NFIELDS (type) = nfields;
+  type->set_num_fields (nfields);
   TYPE_FIELDS (type) = (struct field *)
     TYPE_ALLOC (type, sizeof (struct field) * nfields);
   memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
@@ -3654,7 +3654,7 @@  read_enum_type (const char **pp, struct type *type,
   TYPE_STUB (type) = 0;
   if (unsigned_enum)
     TYPE_UNSIGNED (type) = 1;
-  TYPE_NFIELDS (type) = nsyms;
+  type->set_num_fields (nsyms);
   TYPE_FIELDS (type) = (struct field *)
     TYPE_ALLOC (type, sizeof (struct field) * nsyms);
   memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nsyms);
diff --git a/gdb/windows-tdep.c b/gdb/windows-tdep.c
index 20a18e6b683..d7c498f2e93 100644
--- a/gdb/windows-tdep.c
+++ b/gdb/windows-tdep.c
@@ -751,7 +751,7 @@  create_enum (struct gdbarch *gdbarch, int bit, const char *name,
   int i;
 
   type = arch_type (gdbarch, TYPE_CODE_ENUM, bit, name);
-  TYPE_NFIELDS (type) = count;
+  type->set_num_fields (count);
   TYPE_FIELDS (type) = (struct field *)
     TYPE_ZALLOC (type, sizeof (struct field) * count);
   TYPE_UNSIGNED (type) = 1;