Use type allocator for range types

This changes the range type creation functions to accept a type
allocator, and updates all the callers.  Note that symbol readers
should generally allocate on the relevant objfile, regardless of the
underlying type of the range, which is what this patch implements.

Reviewed-By: Simon Marchi <simon.marchi@efficios.com>
This commit is contained in:
Tom Tromey
2023-03-13 12:53:48 -06:00
parent 9c794d2d46
commit e727c536c6
11 changed files with 92 additions and 65 deletions

View File

@@ -2141,15 +2141,15 @@ ada_type_of_array (struct value *arr, int bounds)
while (arity > 0)
{
type_allocator alloc (arr->type ());
struct type *range_type = alloc.new_type ();
struct type *array_type = alloc.new_type ();
struct value *low = desc_one_bound (descriptor, arity, 0);
struct value *high = desc_one_bound (descriptor, arity, 1);
arity -= 1;
create_static_range_type (range_type, low->type (),
longest_to_int (value_as_long (low)),
longest_to_int (value_as_long (high)));
struct type *range_type
= create_static_range_type (alloc, low->type (),
longest_to_int (value_as_long (low)),
longest_to_int (value_as_long (high)));
elt_type = create_array_type (array_type, elt_type, range_type);
if (ada_is_unconstrained_packed_array_type (arr->type ()))
@@ -3094,8 +3094,9 @@ ada_value_slice_from_ptr (struct value *array_ptr, struct type *type,
{
struct type *type0 = ada_check_typedef (type);
struct type *base_index_type = type0->index_type ()->target_type ();
type_allocator alloc (base_index_type);
struct type *index_type
= create_static_range_type (NULL, base_index_type, low, high);
= create_static_range_type (alloc, base_index_type, low, high);
struct type *slice_type = create_array_type_with_stride
(NULL, type0->target_type (), index_type,
type0->dyn_prop (DYN_PROP_BYTE_STRIDE),
@@ -3128,8 +3129,9 @@ ada_value_slice (struct value *array, int low, int high)
{
struct type *type = ada_check_typedef (array->type ());
struct type *base_index_type = type->index_type ()->target_type ();
type_allocator alloc (type->index_type ());
struct type *index_type
= create_static_range_type (NULL, type->index_type (), low, high);
= create_static_range_type (alloc, type->index_type (), low, high);
struct type *slice_type = create_array_type_with_stride
(NULL, type->target_type (), index_type,
type->dyn_prop (DYN_PROP_BYTE_STRIDE),
@@ -3400,9 +3402,10 @@ static struct value *
empty_array (struct type *arr_type, int low, int high)
{
struct type *arr_type0 = ada_check_typedef (arr_type);
type_allocator alloc (arr_type0->index_type ()->target_type ());
struct type *index_type
= create_static_range_type
(NULL, arr_type0->index_type ()->target_type (), low,
(alloc, arr_type0->index_type ()->target_type (), low,
high < low ? low - 1 : high);
struct type *elt_type = ada_array_element_type (arr_type0, 1);
@@ -11515,8 +11518,10 @@ to_fixed_range_type (struct type *raw_type, struct value *dval)
if (L < INT_MIN || U > INT_MAX)
return raw_type;
else
return create_static_range_type (type_allocator (raw_type).new_type (),
raw_type, L, U);
{
type_allocator alloc (raw_type);
return create_static_range_type (alloc, raw_type, L, U);
}
}
else
{
@@ -11567,8 +11572,8 @@ to_fixed_range_type (struct type *raw_type, struct value *dval)
}
}
type = create_static_range_type (type_allocator (raw_type).new_type (),
base_type, L, U);
type_allocator alloc (raw_type);
type = create_static_range_type (alloc, base_type, L, U);
/* create_static_range_type alters the resulting type's length
to match the size of the base_type, which is not what we want.
Set it back to the original range type's length. */

View File

@@ -1780,8 +1780,9 @@ decode_type (struct coff_symbol *cs, unsigned int c_type,
base_type = decode_type (cs, new_c_type, aux, objfile);
index_type = objfile_type (objfile)->builtin_int;
type_allocator alloc (objfile);
range_type
= create_static_range_type (NULL, index_type, 0, n - 1);
= create_static_range_type (alloc, index_type, 0, n - 1);
type =
create_array_type (NULL, base_type, range_type);
}

View File

@@ -831,7 +831,8 @@ read_array_type (struct ctf_context *ccp, ctf_id_t tid)
if (idx_type == nullptr)
idx_type = objfile_type (objfile)->builtin_int;
range_type = create_static_range_type (NULL, idx_type, 0, ar.ctr_nelems - 1);
type_allocator alloc (objfile);
range_type = create_static_range_type (alloc, idx_type, 0, ar.ctr_nelems - 1);
type = create_array_type (NULL, element_type, range_type);
if (ar.ctr_nelems <= 1) /* Check if undefined upper bound. */
{

View File

@@ -13547,7 +13547,8 @@ quirk_ada_thick_pointer (struct die_info *die, struct dwarf2_cu *cu,
range_fields[i + 1].set_name (objfile->intern (name));
}
struct type *bounds = type_allocator (objfile).new_type ();
type_allocator alloc (objfile);
struct type *bounds = alloc.new_type ();
bounds->set_code (TYPE_CODE_STRUCT);
bounds->set_num_fields (range_fields.size ());
@@ -13571,7 +13572,7 @@ quirk_ada_thick_pointer (struct die_info *die, struct dwarf2_cu *cu,
gdb_assert (iter->code () == TYPE_CODE_ARRAY);
iter->main_type->dyn_prop_list = nullptr;
iter->set_index_type
(create_static_range_type (NULL, bounds->field (i).type (), 1, 0));
(create_static_range_type (alloc, bounds->field (i).type (), 1, 0));
iter = iter->target_type ();
}
@@ -13655,7 +13656,8 @@ read_array_type (struct die_info *die, struct dwarf2_cu *cu)
if (die->child == NULL)
{
index_type = objfile_type (objfile)->builtin_int;
range_type = create_static_range_type (NULL, index_type, 0, -1);
type_allocator alloc (objfile);
range_type = create_static_range_type (alloc, index_type, 0, -1);
type = create_array_type_with_stride (NULL, element_type, range_type,
byte_stride_prop, bit_stride);
return set_die_type (die, type, cu);
@@ -14496,14 +14498,15 @@ read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
}
index_type = objfile_type (objfile)->builtin_int;
type_allocator alloc (objfile);
if (length_is_constant)
range_type = create_static_range_type (NULL, index_type, 1, length);
range_type = create_static_range_type (alloc, index_type, 1, length);
else
{
struct dynamic_prop low_bound;
low_bound.set_const_val (1);
range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
range_type = create_range_type (alloc, index_type, &low_bound, &prop, 0);
}
char_type = language_string_char_type (cu->language_defn, gdbarch);
type = create_string_type (NULL, char_type, range_type);
@@ -15707,6 +15710,7 @@ read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
}
}
type_allocator alloc (cu->per_objfile->objfile);
if (attr_byte_stride != nullptr
|| attr_bit_stride != nullptr)
{
@@ -15715,11 +15719,11 @@ read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
= byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
range_type
= create_range_type_with_stride (NULL, orig_base_type, &low,
= create_range_type_with_stride (alloc, orig_base_type, &low,
&high, bias, stride, byte_stride_p);
}
else
range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
range_type = create_range_type (alloc, orig_base_type, &low, &high, bias);
if (high_bound_is_count)
range_type->bounds ()->flag_upper_bound_is_count = 1;

View File

@@ -669,10 +669,11 @@ ptype : typebase
array_size = type_stack->pop_int ();
if (array_size != -1)
{
struct type *idx_type
= parse_f_type (pstate)->builtin_integer;
type_allocator alloc (idx_type);
range_type =
create_static_range_type ((struct type *) NULL,
parse_f_type (pstate)
->builtin_integer,
create_static_range_type (alloc, idx_type,
0, array_size - 1);
follow_type =
create_array_type ((struct type *) NULL,

View File

@@ -131,8 +131,9 @@ fortran_bounds_all_dims (bool lbound_p,
int ndimensions = calc_f77_array_dims (array_type);
/* Allocate a result value of the correct type. */
type_allocator alloc (gdbarch);
struct type *range
= create_static_range_type (nullptr,
= create_static_range_type (alloc,
builtin_f_type (gdbarch)->builtin_integer,
1, ndimensions);
struct type *elm_type = builtin_f_type (gdbarch)->builtin_integer;
@@ -714,8 +715,9 @@ fortran_array_shape (struct gdbarch *gdbarch, const language_defn *lang,
ndimensions = calc_f77_array_dims (val_type);
/* Allocate a result value of the correct type. */
type_allocator alloc (gdbarch);
struct type *range
= create_static_range_type (nullptr,
= create_static_range_type (alloc,
builtin_type (gdbarch)->builtin_int,
1, ndimensions);
struct type *elm_type = builtin_f_type (gdbarch)->builtin_integer;
@@ -1393,8 +1395,9 @@ fortran_undetermined::value_subarray (value *array,
p_high.set_const_val (d.high);
p_stride.set_const_val (d.stride);
type_allocator alloc (d.index->target_type ());
struct type *new_range
= create_range_type_with_stride ((struct type *) NULL,
= create_range_type_with_stride (alloc,
d.index->target_type (),
&p_low, &p_high, 0, &p_stride,
true);
@@ -1429,8 +1432,9 @@ fortran_undetermined::value_subarray (value *array,
p_high.set_const_val (d.high);
p_stride.set_const_val (repacked_array_type->length ());
type_allocator alloc (d.index->target_type ());
struct type *new_range
= create_range_type_with_stride ((struct type *) NULL,
= create_range_type_with_stride (alloc,
d.index->target_type (),
&p_low, &p_high, 0, &p_stride,
true);

View File

@@ -929,11 +929,10 @@ operator== (const range_bounds &l, const range_bounds &r)
#undef FIELD_EQ
}
/* Create a range type with a dynamic range from LOW_BOUND to
HIGH_BOUND, inclusive. See create_range_type for further details. */
/* See gdbtypes.h. */
struct type *
create_range_type (struct type *result_type, struct type *index_type,
create_range_type (type_allocator &alloc, struct type *index_type,
const struct dynamic_prop *low_bound,
const struct dynamic_prop *high_bound,
LONGEST bias)
@@ -943,8 +942,7 @@ create_range_type (struct type *result_type, struct type *index_type,
gdb_assert (index_type->code () != TYPE_CODE_VOID);
gdb_assert (index_type->length () > 0);
if (result_type == NULL)
result_type = type_allocator (index_type).new_type ();
struct type *result_type = alloc.new_type ();
result_type->set_code (TYPE_CODE_RANGE);
result_type->set_target_type (index_type);
if (index_type->is_stub ())
@@ -996,7 +994,7 @@ create_range_type (struct type *result_type, struct type *index_type,
/* See gdbtypes.h. */
struct type *
create_range_type_with_stride (struct type *result_type,
create_range_type_with_stride (type_allocator &alloc,
struct type *index_type,
const struct dynamic_prop *low_bound,
const struct dynamic_prop *high_bound,
@@ -1004,8 +1002,8 @@ create_range_type_with_stride (struct type *result_type,
const struct dynamic_prop *stride,
bool byte_stride_p)
{
result_type = create_range_type (result_type, index_type, low_bound,
high_bound, bias);
struct type *result_type = create_range_type (alloc, index_type, low_bound,
high_bound, bias);
gdb_assert (stride != nullptr);
result_type->bounds ()->stride = *stride;
@@ -1014,20 +1012,10 @@ create_range_type_with_stride (struct type *result_type,
return result_type;
}
/* Create a range type using either a blank type supplied in
RESULT_TYPE, or creating a new type, inheriting the objfile from
INDEX_TYPE.
Indices will be of type INDEX_TYPE, and will range from LOW_BOUND
to HIGH_BOUND, inclusive.
FIXME: Maybe we should check the TYPE_CODE of RESULT_TYPE to make
sure it is TYPE_CODE_UNDEF before we bash it into a range type? */
/* See gdbtypes.h. */
struct type *
create_static_range_type (struct type *result_type, struct type *index_type,
create_static_range_type (type_allocator &alloc, struct type *index_type,
LONGEST low_bound, LONGEST high_bound)
{
struct dynamic_prop low, high;
@@ -1035,7 +1023,8 @@ create_static_range_type (struct type *result_type, struct type *index_type,
low.set_const_val (low_bound);
high.set_const_val (high_bound);
result_type = create_range_type (result_type, index_type, &low, &high, 0);
struct type *result_type = create_range_type (alloc, index_type,
&low, &high, 0);
return result_type;
}
@@ -1439,12 +1428,13 @@ lookup_array_range_type (struct type *element_type,
struct type *index_type;
struct type *range_type;
type_allocator alloc (element_type);
if (element_type->is_objfile_owned ())
index_type = objfile_type (element_type->objfile_owner ())->builtin_int;
else
index_type = builtin_type (element_type->arch_owner ())->builtin_int;
range_type = create_static_range_type (NULL, index_type,
range_type = create_static_range_type (alloc, index_type,
low_bound, high_bound);
return create_array_type (NULL, element_type, range_type);
@@ -2272,8 +2262,9 @@ resolve_dynamic_range (struct type *dyn_range_type,
= resolve_dynamic_type_internal (dyn_range_type->target_type (),
addr_stack, 0);
LONGEST bias = dyn_range_type->bounds ()->bias;
type_allocator alloc (dyn_range_type);
static_range_type = create_range_type_with_stride
(copy_type (dyn_range_type), static_target_type,
(alloc, static_target_type,
&low_bound, &high_bound, bias, &stride, byte_stride_p);
static_range_type->bounds ()->flag_bound_evaluated = 1;
return static_range_type;

View File

@@ -2473,25 +2473,38 @@ extern struct type *lookup_function_type_with_arguments (struct type *,
int,
struct type **);
extern struct type *create_static_range_type (struct type *, struct type *,
LONGEST, LONGEST);
/* Create a range type using ALLOC.
Indices will be of type INDEX_TYPE, and will range from LOW_BOUND
to HIGH_BOUND, inclusive. */
extern struct type *create_static_range_type (type_allocator &alloc,
struct type *index_type,
LONGEST low_bound,
LONGEST high_bound);
extern struct type *create_array_type_with_stride
(struct type *, struct type *, struct type *,
struct dynamic_prop *, unsigned int);
extern struct type *create_range_type (struct type *, struct type *,
const struct dynamic_prop *,
const struct dynamic_prop *,
LONGEST);
/* Create a range type using ALLOC with a dynamic range from LOW_BOUND
to HIGH_BOUND, inclusive. INDEX_TYPE is the underlying type. BIAS
is the bias to be applied when storing or retrieving values of this
type. */
extern struct type *create_range_type (type_allocator &alloc,
struct type *index_type,
const struct dynamic_prop *low_bound,
const struct dynamic_prop *high_bound,
LONGEST bias);
/* Like CREATE_RANGE_TYPE but also sets up a stride. When BYTE_STRIDE_P
is true the value in STRIDE is a byte stride, otherwise STRIDE is a bit
stride. */
extern struct type * create_range_type_with_stride
(struct type *result_type, struct type *index_type,
extern struct type *create_range_type_with_stride
(type_allocator &alloc, struct type *index_type,
const struct dynamic_prop *low_bound,
const struct dynamic_prop *high_bound, LONGEST bias,
const struct dynamic_prop *stride, bool byte_stride_p);

View File

@@ -1862,9 +1862,12 @@ upgrade_type (int fd, struct type **tpp, int tq, union aux_ext *ax, int bigend,
ax++;
rf = AUX_GET_WIDTH (bigend, ax); /* bit size of array element */
range = create_static_range_type (NULL, indx, lower, upper);
{
type_allocator alloc (indx);
range = create_static_range_type (alloc, indx, lower, upper);
t = create_array_type (NULL, *tpp, range);
t = create_array_type (NULL, *tpp, range);
}
/* We used to fill in the supplied array element bitsize
here if the TYPE_LENGTH of the target type was zero.

View File

@@ -877,8 +877,9 @@ define_symbol (CORE_ADDR valu, const char *string, int desc, int type,
/* NULL terminate the string. */
string_local[ind] = 0;
type_allocator alloc (objfile);
range_type
= create_static_range_type (NULL,
= create_static_range_type (alloc,
objfile_type (objfile)->builtin_int,
0, ind);
sym->set_type
@@ -3547,8 +3548,9 @@ read_array_type (const char **pp, struct type *type,
upper = -1;
}
type_allocator alloc (objfile);
range_type =
create_static_range_type (NULL, index_type, lower, upper);
create_static_range_type (alloc, index_type, lower, upper);
type = create_array_type (type, element_type, range_type);
return type;
@@ -4180,7 +4182,7 @@ handle_true_range:
}
result_type
= create_static_range_type (NULL, index_type, n2, n3);
= create_static_range_type (alloc, index_type, n2, n3);
return (result_type);
}

View File

@@ -485,7 +485,8 @@ value_cast (struct type *type, struct value *arg2)
"divide object size in cast"));
/* FIXME-type-allocation: need a way to free this type when
we are done with it. */
range_type = create_static_range_type (NULL,
type_allocator alloc (range_type->target_type ());
range_type = create_static_range_type (alloc,
range_type->target_type (),
low_bound,
new_length + low_bound - 1);
@@ -4057,7 +4058,8 @@ value_slice (struct value *array, int lowbound, int length)
/* FIXME-type-allocation: need a way to free this type when we are
done with it. */
slice_range_type = create_static_range_type (NULL,
type_allocator alloc (range_type->target_type ());
slice_range_type = create_static_range_type (alloc,
range_type->target_type (),
lowbound,
lowbound + length - 1);