+2020-05-22  Simon Marchi  <simon.marchi@efficios.com>
+
+       * 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.
+
 2020-05-22  Tom Tromey  <tromey@adacore.com>
 
        * compile/compile-object-load.h (munmap_list_free): Don't
 
   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>");
   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);
         {
           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
         {
              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),
   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),
 
       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
     {
 
            }
          if (nparams > 0)
            {
-             TYPE_NFIELDS (ftype) = nparams;
+             ftype->set_num_fields (nparams);
              TYPE_FIELDS (ftype) = (struct field *)
                TYPE_ALLOC (ftype, nparams * sizeof (struct field));
 
 
   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);
 
          INIT_CPLUS_SPECIFIC (type);
          TYPE_LENGTH (type) = 0;
          TYPE_FIELDS (type) = 0;
-         TYPE_NFIELDS (type) = 0;
+         type->set_num_fields (0);
        }
       else
        {
          INIT_CPLUS_SPECIFIC (type);
          TYPE_LENGTH (type) = 0;
          TYPE_FIELDS (type) = 0;
-         TYPE_NFIELDS (type) = 0;
+         type->set_num_fields (0);
        }
       else
        {
          type->set_name (NULL);
          TYPE_LENGTH (type) = 0;
          TYPE_FIELDS (type) = 0;
-         TYPE_NFIELDS (type) = 0;
+         type->set_num_fields (0);
        }
       else
        {
     }
   /* 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);
 
   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);
 
 
     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);
 
   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)
 
       /* 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)
       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;
          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;
 
   /* 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);
 
 
   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 (),
        }
 
       /* 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));
 
 
      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);
 
 
        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)
   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;
     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));
 
   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.  */
   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)
 
   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));
   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.
 {
   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]);
 
     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;
    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)
 
   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);
   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);
 
 
        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)));
                {
                  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));
 
   /* 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);
 
   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));
 
 
              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;
           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;
      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);
   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);
 
   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;