rtype = type_allocator (type).new_type ();
rtype->set_code (TYPE_CODE_STRUCT);
INIT_NONE_SPECIFIC (rtype);
- rtype->set_num_fields (nfields);
- rtype->set_fields
- ((struct field *) TYPE_ZALLOC (rtype, nfields * sizeof (struct field)));
+ rtype->alloc_fields (nfields);
rtype->set_name (ada_type_name (type));
rtype->set_is_fixed_instance (true);
type0->set_target_type (type);
type->set_code (type0->code ());
INIT_NONE_SPECIFIC (type);
- type->set_num_fields (nfields);
- field *fields =
- ((struct field *)
- TYPE_ALLOC (type, nfields * sizeof (struct field)));
- memcpy (fields, type0->fields (),
- sizeof (struct field) * nfields);
- type->set_fields (fields);
+ type->copy_fields (type0);
type->set_name (ada_type_name (type0));
type->set_is_fixed_instance (true);
rtype = type_allocator (type).new_type ();
rtype->set_code (TYPE_CODE_STRUCT);
INIT_NONE_SPECIFIC (rtype);
- rtype->set_num_fields (nfields);
-
- field *fields =
- (struct field *) TYPE_ALLOC (rtype, nfields * sizeof (struct field));
- memcpy (fields, type->fields (), sizeof (struct field) * nfields);
- rtype->set_fields (fields);
+ rtype->copy_fields (type);
rtype->set_name (ada_type_name (type));
rtype->set_is_fixed_instance (true);
.new_type (TYPE_CODE_ENUM, enum_type.bit_size (),
enum_type.name ().c_str ()));
- gdb_type->set_num_fields (enum_type.size ());
- gdb_type->set_fields
- ((struct field *) TYPE_ZALLOC (gdb_type, (sizeof (struct field)
- * enum_type.size ())));
+ gdb_type->alloc_fields (enum_type.size ());
gdb_type->set_is_unsigned (true);
for (size_t i = 0; i < enum_type.size (); ++i)
}
if (nparams > 0)
{
- ftype->set_num_fields (nparams);
- ftype->set_fields
- ((struct field *)
- TYPE_ZALLOC (ftype, nparams * sizeof (struct field)));
+ ftype->alloc_fields (nparams);
iparams = 0;
/* Here we want to directly access the dictionary, because
{
struct type *target = type->target_type ();
struct type *real_target = real_type->target_type ();
- int field_size = real_target->num_fields () * sizeof (struct field);
target->set_length (real_target->length ());
- target->set_num_fields (real_target->num_fields ());
-
- field *fields = (struct field *) TYPE_ALLOC (target, field_size);
- memcpy (fields, real_target->fields (), field_size);
- target->set_fields (fields);
+ target->copy_fields (real_target);
if (real_target->name ())
{
}
/* Now create the vector of fields, and record how big it is. */
- type->set_num_fields (nfields);
- type->set_fields
- ((struct field *) TYPE_ALLOC (type, sizeof (struct field) * nfields));
+ type->alloc_fields (nfields, false);
/* Copy the saved-up fields into the field vector. */
else /* Assume ints. */
type->set_length (gdbarch_int_bit (gdbarch) / TARGET_CHAR_BIT);
type->set_code (TYPE_CODE_ENUM);
- type->set_num_fields (nsyms);
- type->set_fields
- ((struct field *) TYPE_ALLOC (type, sizeof (struct field) * nsyms));
+ type->alloc_fields (nsyms, false);
/* Find the symbols for the values and put them into the type.
The symbols can be found in the symlist that we put them on
return;
/* Record the field count, allocate space for the array of fields. */
- type->set_num_fields (nfields);
- type->set_fields
- ((struct field *) TYPE_ZALLOC (type, sizeof (struct field) * nfields));
+ type->alloc_fields (nfields);
/* Copy the saved-up fields into the field vector. */
for (int i = 0; i < nfields; ++i)
if (ctf_func_type_args (fp, tid, argc, argv.data ()) == CTF_ERR)
return nullptr;
- type->set_fields
- ((struct field *) TYPE_ZALLOC (type, argc * sizeof (struct field)));
+ type->alloc_fields (argc);
struct type *void_type = builtin_type (of)->builtin_void;
/* If failed to find the argument type, fill it with void_type. */
for (int iparam = 0; iparam < argc; iparam++)
/* 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->set_num_fields (3);
/* Save the field we care about. */
struct field saved_field = type->field (0);
- type->set_fields
- ((struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field)));
+ type->alloc_fields (3);
/* Put the discriminant at index 0. */
type->field (0).set_type (field_type);
/* Record the field count, allocate space for the array of fields,
and create blank accessibility bitfields if necessary. */
- type->set_num_fields (nfields);
- type->set_fields
- ((struct field *) TYPE_ZALLOC (type, sizeof (struct field) * nfields));
+ type->alloc_fields (nfields);
if (fip->non_public_fields && cu->lang () != language_ada)
{
updated. Either way we want to copy the type and update
everything. */
struct type *copy = copy_type (type);
- int nfields = copy->num_fields ();
- field *new_fields
- = ((struct field *) TYPE_ZALLOC (copy,
- nfields * sizeof (struct field)));
- memcpy (new_fields, copy->fields (), nfields * sizeof (struct field));
- copy->set_fields (new_fields);
+ copy->copy_fields (type);
if (new_target != nullptr)
copy->set_target_type (new_target);
}
if (!fields.empty ())
- {
- type->set_num_fields (fields.size ());
- type->set_fields
- ((struct field *)
- TYPE_ALLOC (type, sizeof (struct field) * fields.size ()));
- memcpy (type->fields (), fields.data (),
- sizeof (struct field) * fields.size ());
- }
+ type->copy_fields (fields);
else
flag_enum = 0;
struct type *bounds = alloc.new_type ();
bounds->set_code (TYPE_CODE_STRUCT);
- bounds->set_num_fields (range_fields.size ());
- bounds->set_fields
- ((struct field *) TYPE_ALLOC (bounds, (bounds->num_fields ()
- * sizeof (struct field))));
- memcpy (bounds->fields (), range_fields.data (),
- bounds->num_fields () * sizeof (struct field));
+ bounds->copy_fields (range_fields);
int last_fieldno = range_fields.size () - 1;
int bounds_size = (bounds->field (last_fieldno).loc_bitpos () / 8
struct type *result = type_allocator (objfile).new_type ();
result->set_code (TYPE_CODE_STRUCT);
- result->set_num_fields (2);
- result->set_fields
- ((struct field *) TYPE_ZALLOC (result, (result->num_fields ()
- * sizeof (struct field))));
+ result->alloc_fields (2);
/* The names are chosen to coincide with what the compiler does with
-fgnat-encodings=all, which the Ada code in gdb already
}
/* Allocate storage for parameters and fill them in. */
- ftype->set_num_fields (nparams);
- ftype->set_fields
- ((struct field *) TYPE_ZALLOC (ftype, nparams * sizeof (struct field)));
+ ftype->alloc_fields (nparams);
/* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
even if we error out during the parameters reading below. */
fn->set_is_prototyped (true);
}
- fn->set_num_fields (nparams);
- fn->set_fields
- ((struct field *) TYPE_ZALLOC (fn, nparams * sizeof (struct field)));
+ fn->alloc_fields (nparams);
for (i = 0; i < nparams; ++i)
fn->field (i).set_type (param_types[i]);
result_type->set_code (TYPE_CODE_ARRAY);
result_type->set_target_type (element_type);
- result_type->set_num_fields (1);
- result_type->set_fields
- ((struct field *) TYPE_ZALLOC (result_type, sizeof (struct field)));
+ result_type->alloc_fields (1);
result_type->set_index_type (range_type);
if (byte_stride_prop != NULL)
result_type->add_dyn_prop (DYN_PROP_BYTE_STRIDE, *byte_stride_prop);
struct type *result_type = alloc.new_type ();
result_type->set_code (TYPE_CODE_SET);
- result_type->set_num_fields (1);
- result_type->set_fields
- ((struct field *) TYPE_ZALLOC (result_type, sizeof (struct field)));
+ result_type->alloc_fields (1);
if (!domain_type->is_stub ())
{
gdb_assert (type->code () == TYPE_CODE_UNION);
resolved_type = copy_type (type);
- resolved_type->set_fields
- ((struct field *)
- TYPE_ALLOC (resolved_type,
- resolved_type->num_fields () * sizeof (struct field)));
- memcpy (resolved_type->fields (),
- type->fields (),
- resolved_type->num_fields () * sizeof (struct field));
+ resolved_type->copy_fields (type);
for (i = 0; i < resolved_type->num_fields (); ++i)
{
struct type *t;
for (const auto &part : parts)
compute_variant_fields_inner (type, addr_stack, part, flags);
- resolved_type->set_num_fields
- (std::count (flags.begin (), flags.end (), true));
- resolved_type->set_fields
- ((struct field *)
- TYPE_ALLOC (resolved_type,
- resolved_type->num_fields () * sizeof (struct field)));
+ unsigned int nfields = std::count (flags.begin (), flags.end (), true);
+ /* No need to zero-initialize the newly allocated fields, they'll be
+ initialized by the copy in the loop below. */
+ resolved_type->alloc_fields (nfields, false);
int out = 0;
for (int i = 0; i < type->num_fields (); ++i)
}
else
{
- resolved_type->set_fields
- ((struct field *)
- TYPE_ALLOC (resolved_type,
- resolved_type->num_fields () * sizeof (struct field)));
- if (type->num_fields () > 0)
- memcpy (resolved_type->fields (),
- type->fields (),
- resolved_type->num_fields () * sizeof (struct field));
+ resolved_type->copy_fields (type);
}
for (i = 0; i < resolved_type->num_fields (); ++i)
int i, nfields;
nfields = type->num_fields ();
- new_type->set_fields
- ((struct field *)
- TYPE_ZALLOC (new_type, nfields * sizeof (struct field)));
+ new_type->alloc_fields (type->num_fields ());
for (i = 0; i < nfields; i++)
{
type = type_allocator (gdbarch).new_type (TYPE_CODE_FLAGS, bit, name);
type->set_is_unsigned (true);
- type->set_num_fields (0);
/* Pre-allocate enough space assuming every field is one bit. */
- type->set_fields
- ((struct field *) TYPE_ZALLOC (type, bit * sizeof (struct field)));
+ type->alloc_fields (bit);
+ type->set_num_fields (0);
return type;
}
return type->fixed_point_info ().scaling_factor;
}
+/* See gdbtypes.h. */
+
+void
+type::alloc_fields (unsigned int nfields, bool init)
+{
+ this->set_num_fields (nfields);
+
+ if (nfields == 0)
+ {
+ this->main_type->flds_bnds.fields = nullptr;
+ return;
+ }
+
+ size_t size = nfields * sizeof (*this->fields ());
+ struct field *fields
+ = (struct field *) (init
+ ? TYPE_ZALLOC (this, size)
+ : TYPE_ALLOC (this, size));
+
+ this->main_type->flds_bnds.fields = fields;
+}
+
+/* See gdbtypes.h. */
+
+void
+type::copy_fields (struct type *src)
+{
+ unsigned int nfields = src->num_fields ();
+ alloc_fields (nfields, false);
+ if (nfields == 0)
+ return;
+
+ size_t size = nfields * sizeof (*this->fields ());
+ memcpy (this->fields (), src->fields (), size);
+}
+
+/* See gdbtypes.h. */
+
+void
+type::copy_fields (std::vector<struct field> &src)
+{
+ unsigned int nfields = src.size ();
+ alloc_fields (nfields, false);
+ if (nfields == 0)
+ return;
+
+ size_t size = nfields * sizeof (*this->fields ());
+ memcpy (this->fields (), src.data (), size);
+}
\f
static const registry<gdbarch>::key<struct builtin_type> gdbtypes_data;
this->main_type->flds_bnds.fields = fields;
}
+ /* Allocate the fields array of this type, with NFIELDS elements. If INIT,
+ zero-initialize the allocated memory. */
+ void alloc_fields (unsigned int nfields, bool init = true);
+
+ /* Allocate the fields array of this type, and copy the fields from SRC. */
+ void copy_fields (struct type *src);
+ void copy_fields (std::vector<struct field> &src);
+
type *index_type () const
{
return this->field (0).type ();
get_gdb_vtable_type (struct gdbarch *arch)
{
struct type *t;
- struct field *field_list, *field;
int offset;
struct type *result = vtable_type_gdbarch_data.get (arch);
struct type *ptrdiff_type
= init_integer_type (alloc, gdbarch_ptr_bit (arch), 0, "ptrdiff_t");
+ t = alloc.new_type (TYPE_CODE_STRUCT, 0, nullptr);
+
/* We assume no padding is necessary, since GDB doesn't know
anything about alignment at the moment. If this assumption bites
us, we should add a gdbarch method which, given a type, returns
the alignment that type requires, and then use that here. */
/* Build the field list. */
- field_list = XCNEWVEC (struct field, 4);
- field = &field_list[0];
+ t->alloc_fields (4);
+
offset = 0;
/* ptrdiff_t vcall_and_vbase_offsets[0]; */
- field->set_name ("vcall_and_vbase_offsets");
- field->set_type (lookup_array_range_type (ptrdiff_type, 0, -1));
- field->set_loc_bitpos (offset * TARGET_CHAR_BIT);
- offset += field->type ()->length ();
- field++;
+ {
+ struct field &field0 = t->field (0);
+ field0.set_name ("vcall_and_vbase_offsets");
+ field0.set_type (lookup_array_range_type (ptrdiff_type, 0, -1));
+ field0.set_loc_bitpos (offset * TARGET_CHAR_BIT);
+ offset += field0.type ()->length ();
+ }
/* ptrdiff_t offset_to_top; */
- field->set_name ("offset_to_top");
- field->set_type (ptrdiff_type);
- field->set_loc_bitpos (offset * TARGET_CHAR_BIT);
- offset += field->type ()->length ();
- field++;
+ {
+ struct field &field1 = t->field (1);
+ field1.set_name ("offset_to_top");
+ field1.set_type (ptrdiff_type);
+ field1.set_loc_bitpos (offset * TARGET_CHAR_BIT);
+ offset += field1.type ()->length ();
+ }
/* void *type_info; */
- field->set_name ("type_info");
- field->set_type (void_ptr_type);
- field->set_loc_bitpos (offset * TARGET_CHAR_BIT);
- offset += field->type ()->length ();
- field++;
+ {
+ struct field &field2 = t->field (2);
+ field2.set_name ("type_info");
+ field2.set_type (void_ptr_type);
+ field2.set_loc_bitpos (offset * TARGET_CHAR_BIT);
+ offset += field2.type ()->length ();
+ }
/* void (*virtual_functions[0]) (); */
- field->set_name ("virtual_functions");
- field->set_type (lookup_array_range_type (ptr_to_void_fn_type, 0, -1));
- field->set_loc_bitpos (offset * TARGET_CHAR_BIT);
- offset += field->type ()->length ();
- field++;
-
- /* We assumed in the allocation above that there were four fields. */
- gdb_assert (field == (field_list + 4));
-
- t = alloc.new_type (TYPE_CODE_STRUCT, offset * TARGET_CHAR_BIT, NULL);
- t->set_num_fields (field - field_list);
- t->set_fields (field_list);
+ {
+ struct field &field3 = t->field (3);
+ field3.set_name ("virtual_functions");
+ field3.set_type (lookup_array_range_type (ptr_to_void_fn_type, 0, -1));
+ field3.set_loc_bitpos (offset * TARGET_CHAR_BIT);
+ offset += field3.type ()->length ();
+ }
+
+ t->set_length (offset);
+
t->set_name ("gdb_gnu_v3_abi_vtable");
INIT_CPLUS_SPECIFIC (t);
build_std_type_info_type (struct gdbarch *arch)
{
struct type *t;
- struct field *field_list, *field;
int offset;
struct type *void_ptr_type
= builtin_type (arch)->builtin_data_ptr;
struct type *char_ptr_type
= make_pointer_type (make_cv_type (1, 0, char_type, NULL), NULL);
- field_list = XCNEWVEC (struct field, 2);
- field = &field_list[0];
+ t = type_allocator (arch).new_type (TYPE_CODE_STRUCT, 0, nullptr);
+
+ t->alloc_fields (2);
+
offset = 0;
/* The vtable. */
- field->set_name ("_vptr.type_info");
- field->set_type (void_ptr_type);
- field->set_loc_bitpos (offset * TARGET_CHAR_BIT);
- offset += field->type ()->length ();
- field++;
+ {
+ struct field &field0 = t->field (0);
+ field0.set_name ("_vptr.type_info");
+ field0.set_type (void_ptr_type);
+ field0.set_loc_bitpos (offset * TARGET_CHAR_BIT);
+ offset += field0.type ()->length ();
+ }
/* The name. */
- field->set_name ("__name");
- field->set_type (char_ptr_type);
- field->set_loc_bitpos (offset * TARGET_CHAR_BIT);
- offset += field->type ()->length ();
- field++;
-
- gdb_assert (field == (field_list + 2));
-
- t = type_allocator (arch).new_type (TYPE_CODE_STRUCT,
- offset * TARGET_CHAR_BIT, nullptr);
- t->set_num_fields (field - field_list);
- t->set_fields (field_list);
+ {
+ struct field &field1 = t->field (1);
+ field1.set_name ("__name");
+ field1.set_type (char_ptr_type);
+ field1.set_loc_bitpos (offset * TARGET_CHAR_BIT);
+ offset += field1.type ()->length ();
+ }
+
+ t->set_length (offset);
+
t->set_name ("gdb_gnu_v3_type_info");
INIT_CPLUS_SPECIFIC (t);
t->set_code (type_code);
t->set_length (sh->value);
- t->set_num_fields (nfields);
- f = ((struct field *) TYPE_ALLOC (t, nfields * sizeof (struct field)));
- t->set_fields (f);
+ t->alloc_fields (nfields, false);
if (type_code == TYPE_CODE_ENUM)
{
if (nparams > 0)
{
- ftype->set_num_fields (nparams);
- ftype->set_fields
- ((struct field *)
- TYPE_ALLOC (ftype, nparams * sizeof (struct field)));
+ ftype->alloc_fields (nparams, false);
iparams = 0;
for (struct symbol *sym : block_iterator_range (cblock))
result->set_code (TYPE_CODE_STRUCT);
result->set_name (name);
- result->set_num_fields (nfields);
- result->set_fields
- ((struct field *) TYPE_ZALLOC (result, nfields * sizeof (struct field)));
+ result->alloc_fields (nfields);
i = 0;
bitpos = 0;
}
/* Allocate parameter information fields and fill them in. */
- ftype->set_fields
- ((struct field *)
- TYPE_ALLOC (ftype, nsemi * sizeof (struct field)));
+ ftype->alloc_fields (nsemi, false);
while (*p++ == ';')
{
struct type *ptype;
&& arg_types->type->code () == TYPE_CODE_VOID)
num_args = 0;
- func_type->set_fields
- ((struct field *) TYPE_ALLOC (func_type,
- num_args * sizeof (struct field)));
- memset (func_type->fields (), 0, num_args * sizeof (struct field));
+ func_type->alloc_fields (num_args);
{
int i;
struct type_list *t;
non-public fields. Record the field count, allocate space for the
array of fields, and create blank visibility bitfields if necessary. */
- type->set_num_fields (nfields);
- type->set_fields
- ((struct field *)
- TYPE_ALLOC (type, sizeof (struct field) * nfields));
- memset (type->fields (), 0, sizeof (struct field) * nfields);
+ type->alloc_fields (nfields);
if (non_public_fields)
{
type->set_is_stub (false);
if (unsigned_enum)
type->set_is_unsigned (true);
- type->set_num_fields (nsyms);
- type->set_fields
- ((struct field *)
- TYPE_ALLOC (type, sizeof (struct field) * nsyms));
- memset (type->fields (), 0, sizeof (struct field) * nsyms);
+ type->alloc_fields (nsyms);
/* Find the symbols for the values and put them into the type.
The symbols can be found in the symlist that we put them on
int i;
type = type_allocator (gdbarch).new_type (TYPE_CODE_ENUM, bit, name);
- type->set_num_fields (count);
- type->set_fields
- ((struct field *) TYPE_ZALLOC (type, sizeof (struct field) * count));
+ type->alloc_fields (count);
type->set_is_unsigned (true);
for (i = 0; i < count; i++)