gdb: add type::num_fields / type::set_num_fields
authorSimon Marchi <simon.marchi@efficios.com>
Fri, 22 May 2020 20:55:14 +0000 (16:55 -0400)
committerSimon Marchi <simon.marchi@efficios.com>
Fri, 22 May 2020 20:55:14 +0000 (16:55 -0400)
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.

Change-Id: I5ad9de5be4097feaf942d111077434bf91d13dc5

14 files changed:
gdb/ChangeLog
gdb/ada-lang.c
gdb/buildsym.c
gdb/coffread.c
gdb/ctfread.c
gdb/dwarf2/read.c
gdb/eval.c
gdb/gdbtypes.c
gdb/gdbtypes.h
gdb/gnu-v3-abi.c
gdb/mdebugread.c
gdb/rust-lang.c
gdb/stabsread.c
gdb/windows-tdep.c

index 4662ff34923e2cd2f9079f389acac42dce3f22fc..9656bf907d908c214860a5f4c35f5afec57d61e7 100644 (file)
@@ -1,3 +1,11 @@
+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
index 9f6485e04ab74069b5eff00968f694ab7a1ec794..a477f27797e24b03d1454a2e5363399225f8fc0e 100644 (file)
@@ -8021,7 +8021,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>");
@@ -8078,7 +8077,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);
@@ -8246,7 +8245,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
         {
@@ -8353,7 +8352,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),
@@ -8402,7 +8401,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),
@@ -8425,7 +8424,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
     {
index b9bcc33080abece60428d1dc2feb426a6694fb05..f66607cd736575377bfb24a7554b0961d266b02b 100644 (file)
@@ -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));
 
index 0f7a6e3e5ad0565e50d357379b04d96f54757e45..2732421a35cd5d2ccb59e2c7ff18b98c85198750 100644 (file)
@@ -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);
 
@@ -1883,7 +1883,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
        {
@@ -1903,7 +1903,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
        {
@@ -1924,7 +1924,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
        {
@@ -2040,7 +2040,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);
 
@@ -2120,7 +2120,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);
 
index b5bdb5f9cf1eeef3af644b3cfae322b107dd9b32..31f927e3bc9fe86bf5f358b4f0eb1136f717bba8 100644 (file)
@@ -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)
index ded71f53b591ba74f9795789eeca909f230b70c8..975227ea7a271d076175798604d617ddc37d5894 100644 (file)
@@ -9362,7 +9362,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)
@@ -9461,7 +9461,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;
@@ -9509,7 +9509,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;
@@ -14804,7 +14804,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);
 
@@ -15933,7 +15933,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 (),
@@ -17084,7 +17084,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));
 
index ea08602788085d9cb8d6ab56b28d5fc347b65a6f..023b629c1aa7bb363e78b5e1d3d33326fa005cf8 100644 (file)
@@ -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);
 
index 68d4c0c4a24051269e1ec20109ee282aa6fe1440..f42657ab8f9208cedde979b4a8c59c1426c6f52d 100644 (file)
@@ -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]);
index ba8e6f837aaf91bca07abbaa123e266822f76c8f..da8d5e2a1199b457af540e4d066a2620230dc40f 100644 (file)
@@ -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)
index df5818b300ec2e288650eb9357a6e83131a2c36d..b05443193832313f06f385ef5b9fb5f7ce0cb43b 100644 (file)
@@ -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);
index 9ad9c664bcbd5df020a0619c981e05d797f755a4..1b499ccad34c47e66c7a28070f4d945aa0a92401 100644 (file)
@@ -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);
index 2c76762eff2090e22eff476a014f8e0512e1ae66..f0bea374ecb20f40ece3cc7b8c41a6c9c96a1eb3 100644 (file)
@@ -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));
 
index a632856404b9b15a30a1a72154b9557ac234464a..c3da9a239b4b33f9cce7bc5df4ab430e924c33e5 100644 (file)
@@ -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 @@ again:
           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);
index 20a18e6b68398163daebcf4cb8b425a25abe3d9d..d7c498f2e93bac7c6eff75de6d2712aa1ff70e96 100644 (file)
@@ -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;
This page took 0.045857 seconds and 4 git commands to generate.