[14/20] Use type allocator for range types

Message ID 20230313-split-objfile-type-allocator-2-v1-14-69ba773ac17b@tromey.com
State New
Headers
Series Remove objfile_type |

Commit Message

Tom Tromey March 13, 2023, 10:08 p.m. UTC
  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.
---
 gdb/ada-lang.c    | 27 ++++++++++++++++-----------
 gdb/coffread.c    |  3 ++-
 gdb/ctfread.c     |  3 ++-
 gdb/dwarf2/read.c | 18 +++++++++++-------
 gdb/f-exp.y       |  7 ++++---
 gdb/f-lang.c      | 12 ++++++++----
 gdb/gdbtypes.c    | 34 +++++++++++++---------------------
 gdb/gdbtypes.h    | 13 +++++++++----
 gdb/mdebugread.c  |  7 +++++--
 gdb/stabsread.c   |  8 +++++---
 gdb/valops.c      |  6 ++++--
 11 files changed, 79 insertions(+), 59 deletions(-)
  

Comments

Simon Marchi March 14, 2023, 2:41 p.m. UTC | #1
> diff --git a/gdb/gdbtypes.h b/gdb/gdbtypes.h
> index dd9d59d5ae8..83fbfdbcee0 100644
> --- a/gdb/gdbtypes.h
> +++ b/gdb/gdbtypes.h
> @@ -2470,7 +2470,12 @@ extern struct type *lookup_function_type_with_arguments (struct type *,
>  							 int,
>  							 struct type **);
>  
> -extern struct type *create_static_range_type (struct type *, struct type *,
> +/* 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 &, struct type *,
>  					      LONGEST, LONGEST);
I don't know what you think about declarations without names, but when I
touch one, I try to add the names.  Especially if the comment refers to
them, it makes things a bit clearer.

Simon
  
Tom Tromey March 18, 2023, 3:56 p.m. UTC | #2
>>>>> "Simon" == Simon Marchi <simark@simark.ca> writes:

>> +extern struct type *create_static_range_type (type_allocator &, struct type *,
>> LONGEST, LONGEST);

Simon> I don't know what you think about declarations without names, but when I
Simon> touch one, I try to add the names.  Especially if the comment refers to
Simon> them, it makes things a bit clearer.

Yeah, that makes sense.  I went through this series and did that in
every spot I touched.

Tom
  

Patch

diff --git a/gdb/ada-lang.c b/gdb/ada-lang.c
index b1c8912b0ea..cfb8d6e1110 100644
--- a/gdb/ada-lang.c
+++ b/gdb/ada-lang.c
@@ -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.  */
diff --git a/gdb/coffread.c b/gdb/coffread.c
index 6b2ba96c997..170f7761444 100644
--- a/gdb/coffread.c
+++ b/gdb/coffread.c
@@ -1778,8 +1778,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);
 	}
diff --git a/gdb/ctfread.c b/gdb/ctfread.c
index ea557195997..3453800079e 100644
--- a/gdb/ctfread.c
+++ b/gdb/ctfread.c
@@ -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.  */
     {
diff --git a/gdb/dwarf2/read.c b/gdb/dwarf2/read.c
index 73efd295422..83be77da3ea 100644
--- a/gdb/dwarf2/read.c
+++ b/gdb/dwarf2/read.c
@@ -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);
@@ -15713,6 +15716,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)
     {
@@ -15721,11 +15725,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;
diff --git a/gdb/f-exp.y b/gdb/f-exp.y
index 30da537ea84..9ff8e1988fc 100644
--- a/gdb/f-exp.y
+++ b/gdb/f-exp.y
@@ -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,
diff --git a/gdb/f-lang.c b/gdb/f-lang.c
index 3fc43346b05..2da50b27267 100644
--- a/gdb/f-lang.c
+++ b/gdb/f-lang.c
@@ -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);
diff --git a/gdb/gdbtypes.c b/gdb/gdbtypes.c
index 103818fae9b..587f953fa09 100644
--- a/gdb/gdbtypes.c
+++ b/gdb/gdbtypes.c
@@ -933,7 +933,7 @@  operator== (const range_bounds &l, const range_bounds &r)
    HIGH_BOUND, inclusive.  See create_range_type for further details. */
 
 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 +943,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 +995,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 +1003,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 +1013,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 +1024,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 +1429,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 +2263,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;
diff --git a/gdb/gdbtypes.h b/gdb/gdbtypes.h
index dd9d59d5ae8..83fbfdbcee0 100644
--- a/gdb/gdbtypes.h
+++ b/gdb/gdbtypes.h
@@ -2470,7 +2470,12 @@  extern struct type *lookup_function_type_with_arguments (struct type *,
 							 int,
 							 struct type **);
 
-extern struct type *create_static_range_type (struct type *, struct type *,
+/* 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 &, struct type *,
 					      LONGEST, LONGEST);
 
 
@@ -2478,7 +2483,7 @@  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 *,
+extern struct type *create_range_type (type_allocator &, struct type *,
 				       const struct dynamic_prop *,
 				       const struct dynamic_prop *,
 				       LONGEST);
@@ -2487,8 +2492,8 @@  extern struct type *create_range_type (struct type *, struct type *,
    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);
diff --git a/gdb/mdebugread.c b/gdb/mdebugread.c
index c1ba0d31b00..3c299d739fe 100644
--- a/gdb/mdebugread.c
+++ b/gdb/mdebugread.c
@@ -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.
diff --git a/gdb/stabsread.c b/gdb/stabsread.c
index 7ed0ebf9b1c..1e96102dfe4 100644
--- a/gdb/stabsread.c
+++ b/gdb/stabsread.c
@@ -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 @@  read_range_type (const char **pp, int typenums[2], int type_size,
     }
 
   result_type
-    = create_static_range_type (NULL, index_type, n2, n3);
+    = create_static_range_type (alloc, index_type, n2, n3);
   return (result_type);
 }
 
diff --git a/gdb/valops.c b/gdb/valops.c
index 3a1b14c3d44..6fb393fc2ac 100644
--- a/gdb/valops.c
+++ b/gdb/valops.c
@@ -490,7 +490,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);
@@ -4062,7 +4063,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);