Change type::fields to return an array_view

This patch changes type::fields to return an array_view of the fields,
then fixes up the fallout.

More cleanups would be possible here (in particular in the field
initialization code) but I haven't done so.

The main motivation for this patch was to make it simpler to iterate
over the fields of a type.

Regression tested on x86-64 Fedora 41.

Approved-By: Simon Marchi <simon.marchi@efficios.com>
This commit is contained in:
Tom Tromey
2025-08-01 11:11:41 -06:00
parent 55c91b7e5c
commit 89495c3326
14 changed files with 115 additions and 139 deletions

View File

@@ -1080,10 +1080,10 @@ get_discrete_low_bound (struct type *type)
entries. */
LONGEST low = type->field (0).loc_enumval ();
for (int i = 0; i < type->num_fields (); i++)
for (const auto &field : type->fields ())
{
if (type->field (i).loc_enumval () < low)
low = type->field (i).loc_enumval ();
if (field.loc_enumval () < low)
low = field.loc_enumval ();
}
return low;
@@ -1147,10 +1147,10 @@ get_discrete_high_bound (struct type *type)
entries. */
LONGEST high = type->field (0).loc_enumval ();
for (int i = 0; i < type->num_fields (); i++)
for (const auto &field : type->fields ())
{
if (type->field (i).loc_enumval () > high)
high = type->field (i).loc_enumval ();
if (field.loc_enumval () > high)
high = field.loc_enumval ();
}
return high;
@@ -1602,15 +1602,15 @@ smash_to_methodptr_type (struct type *type, struct type *to_type)
void
smash_to_method_type (struct type *type, struct type *self_type,
struct type *to_type, struct field *args,
int nargs, int varargs)
struct type *to_type, gdb::array_view<struct field> args,
int varargs)
{
smash_type (type);
type->set_code (TYPE_CODE_METHOD);
type->set_target_type (to_type);
set_type_self_type (type, self_type);
type->set_fields (args);
type->set_num_fields (nargs);
type->set_fields (args.data ());
type->set_num_fields (args.size ());
if (varargs)
type->set_has_varargs (true);
@@ -2494,23 +2494,22 @@ resolve_dynamic_union (struct type *type,
const frame_info_ptr &frame)
{
struct type *resolved_type;
int i;
unsigned int max_len = 0;
gdb_assert (type->code () == TYPE_CODE_UNION);
resolved_type = copy_type (type);
resolved_type->copy_fields (type);
for (i = 0; i < resolved_type->num_fields (); ++i)
for (auto &field : resolved_type->fields ())
{
struct type *t;
if (type->field (i).is_static ())
if (field.is_static ())
continue;
t = resolve_dynamic_type_internal (resolved_type->field (i).type (),
addr_stack, frame, false);
resolved_type->field (i).set_type (t);
t = resolve_dynamic_type_internal (field.type (), addr_stack,
frame, false);
field.set_type (t);
struct type *real_type = check_typedef (t);
if (real_type->length () > max_len)
@@ -2791,7 +2790,6 @@ resolve_dynamic_struct (struct type *type,
const frame_info_ptr &frame)
{
struct type *resolved_type;
int i;
unsigned resolved_type_bit_length = 0;
gdb_assert (type->code () == TYPE_CODE_STRUCT);
@@ -2812,22 +2810,21 @@ resolve_dynamic_struct (struct type *type,
resolved_type->copy_fields (type);
}
for (i = 0; i < resolved_type->num_fields (); ++i)
for (auto &field : resolved_type->fields ())
{
unsigned new_bit_length;
if (resolved_type->field (i).is_static ())
if (field.is_static ())
continue;
resolve_dynamic_field (resolved_type->field (i), addr_stack, frame);
resolve_dynamic_field (field, addr_stack, frame);
new_bit_length = resolved_type->field (i).loc_bitpos ();
if (resolved_type->field (i).bitsize () != 0)
new_bit_length += resolved_type->field (i).bitsize ();
new_bit_length = field.loc_bitpos ();
if (field.bitsize () != 0)
new_bit_length += field.bitsize ();
else
{
struct type *real_type
= check_typedef (resolved_type->field (i).type ());
struct type *real_type = check_typedef (field.type ());
new_bit_length += (real_type->length () * TARGET_CHAR_BIT);
}
@@ -3394,7 +3391,8 @@ check_stub_method (struct type *type, int method_id, int signature_id)
/* MTYPE may currently be a function (TYPE_CODE_FUNC).
We want a method (TYPE_CODE_METHOD). */
smash_to_method_type (mtype, type, mtype->target_type (),
argtypes, argcount, p[-2] == '.');
gdb::make_array_view (argtypes, argcount),
p[-2] == '.');
mtype->set_is_stub (false);
TYPE_FN_FIELD_STUB (f, signature_id) = 0;
}
@@ -3698,12 +3696,12 @@ type_align (struct type *type)
case TYPE_CODE_UNION:
{
int number_of_non_static_fields = 0;
for (unsigned i = 0; i < type->num_fields (); ++i)
for (const auto &field : type->fields ())
{
if (!type->field (i).is_static ())
if (!field.is_static ())
{
number_of_non_static_fields++;
ULONGEST f_align = type_align (type->field (i).type ());
ULONGEST f_align = type_align (field.type ());
if (f_align == 0)
{
/* Don't pretend we know something we don't. */
@@ -5038,19 +5036,14 @@ rank_one_type (struct type *parm, struct type *arg, struct value *value)
situation. */
static void
print_args (struct field *args, int nargs, int spaces)
print_args (gdb::array_view<struct field> args, int spaces)
{
if (args != NULL)
for (int i = 0; i < args.size (); i++)
{
int i;
for (i = 0; i < nargs; i++)
{
gdb_printf
("%*s[%d] name '%s'\n", spaces, "", i,
args[i].name () != NULL ? args[i].name () : "<NULL>");
recursive_dump_type (args[i].type (), spaces + 2);
}
gdb_printf
("%*s[%d] name '%s'\n", spaces, "", i,
args[i].name () != NULL ? args[i].name () : "<NULL>");
recursive_dump_type (args[i].type (), spaces + 2);
}
}
@@ -5091,9 +5084,8 @@ dump_fn_fieldlists (struct type *type, int spaces)
gdb_printf
("%*sargs %s\n", spaces + 8, "",
host_address_to_string (TYPE_FN_FIELD_ARGS (f, overload_idx)));
host_address_to_string (TYPE_FN_FIELD_ARGS (f, overload_idx).data ()));
print_args (TYPE_FN_FIELD_ARGS (f, overload_idx),
TYPE_FN_FIELD_TYPE (f, overload_idx)->num_fields (),
spaces + 8 + 2);
gdb_printf
("%*sfcontext %s\n", spaces + 8, "",
@@ -5375,7 +5367,7 @@ recursive_dump_type (struct type *type, int spaces)
}
gdb_printf ("\n");
}
gdb_printf ("%s\n", host_address_to_string (type->fields ()));
gdb_printf ("%s\n", host_address_to_string (type->fields ().data ()));
for (idx = 0; idx < type->num_fields (); idx++)
{
field &fld = type->field (idx);
@@ -5756,7 +5748,7 @@ append_composite_type_field_raw (struct type *t, const char *name,
struct field *f;
t->set_num_fields (t->num_fields () + 1);
t->set_fields (XRESIZEVEC (struct field, t->fields (),
t->set_fields (XRESIZEVEC (struct field, t->fields ().data (),
t->num_fields ()));
f = &t->field (t->num_fields () - 1);
memset (f, 0, sizeof f[0]);
@@ -5907,7 +5899,7 @@ type::alloc_fields (unsigned int nfields, bool init)
return;
}
size_t size = nfields * sizeof (*this->fields ());
size_t size = nfields * sizeof (struct field);
struct field *fields
= (struct field *) (init
? TYPE_ZALLOC (this, size)
@@ -5926,8 +5918,8 @@ type::copy_fields (struct type *src)
if (nfields == 0)
return;
size_t size = nfields * sizeof (*this->fields ());
memcpy (this->fields (), src->fields (), size);
size_t size = nfields * sizeof (struct field);
memcpy (this->fields ().data (), src->fields ().data (), size);
}
/* See gdbtypes.h. */
@@ -5940,8 +5932,8 @@ type::copy_fields (std::vector<struct field> &src)
if (nfields == 0)
return;
size_t size = nfields * sizeof (*this->fields ());
memcpy (this->fields (), src.data (), size);
size_t size = nfields * sizeof (struct field);
memcpy (this->fields ().data (), src.data (), size);
}
/* See gdbtypes.h. */