[PATCH 15/20] Use type allocator for array types

Tom Tromey tom@tromey.com
Mon Mar 13 22:08:15 GMT 2023


This changes the array 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
placement of the index type of the array, which is what this patch
implements.
---
 gdb/ada-lang.c    | 25 ++++++++++++-----------
 gdb/coffread.c    |  3 +--
 gdb/ctfread.c     |  2 +-
 gdb/dwarf2/read.c |  9 +++++----
 gdb/f-exp.y       |  6 +++---
 gdb/f-lang.c      |  8 ++++----
 gdb/gdbtypes.c    | 59 +++++++++++++------------------------------------------
 gdb/gdbtypes.h    | 32 +++++++++++++++++++++++++++---
 gdb/mdebugread.c  |  2 +-
 gdb/stabsread.c   |  5 +++--
 gdb/valops.c      |  4 ++--
 11 files changed, 76 insertions(+), 79 deletions(-)

diff --git a/gdb/ada-lang.c b/gdb/ada-lang.c
index cfb8d6e1110..067816c2708 100644
--- a/gdb/ada-lang.c
+++ b/gdb/ada-lang.c
@@ -2141,7 +2141,6 @@ ada_type_of_array (struct value *arr, int bounds)
       while (arity > 0)
 	{
 	  type_allocator alloc (arr->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);
 
@@ -2150,7 +2149,7 @@ ada_type_of_array (struct value *arr, int bounds)
 	    = 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);
+	  elt_type = create_array_type (alloc, elt_type, range_type);
 
 	  if (ada_is_unconstrained_packed_array_type (arr->type ()))
 	    {
@@ -2169,7 +2168,7 @@ ada_type_of_array (struct value *arr, int bounds)
 		  int array_bitsize =
 			(hi - lo + 1) * TYPE_FIELD_BITSIZE (elt_type, 0);
 
-		  array_type->set_length ((array_bitsize + 7) / 8);
+		  elt_type->set_length ((array_bitsize + 7) / 8);
 		}
 	    }
 	}
@@ -2383,11 +2382,11 @@ constrained_packed_array_type (struct type *type, long *elt_bits)
   else
     index_type = type->index_type ();
 
-  new_type = type_allocator (type).new_type ();
+  type_allocator alloc (type);
   new_elt_type =
     constrained_packed_array_type (ada_check_typedef (type->target_type ()),
 				   elt_bits);
-  create_array_type (new_type, new_elt_type, index_type);
+  new_type = create_array_type (alloc, new_elt_type, index_type);
   TYPE_FIELD_BITSIZE (new_type, 0) = *elt_bits;
   new_type->set_name (ada_type_name (type));
 
@@ -3098,7 +3097,7 @@ ada_value_slice_from_ptr (struct value *array_ptr, struct type *type,
   struct type *index_type
     = 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,
+			      (alloc, type0->target_type (), index_type,
 			       type0->dyn_prop (DYN_PROP_BYTE_STRIDE),
 			       TYPE_FIELD_BITSIZE (type0, 0));
   int base_low =  ada_discrete_type_low_bound (type0->index_type ());
@@ -3133,7 +3132,7 @@ ada_value_slice (struct value *array, int low, int high)
   struct type *index_type
     = 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,
+			      (alloc, type->target_type (), index_type,
 			       type->dyn_prop (DYN_PROP_BYTE_STRIDE),
 			       TYPE_FIELD_BITSIZE (type, 0));
   gdb::optional<LONGEST> low_pos, high_pos;
@@ -3409,7 +3408,7 @@ empty_array (struct type *arr_type, int low, int high)
 	 high < low ? low - 1 : high);
   struct type *elt_type = ada_array_element_type (arr_type0, 1);
 
-  return value::allocate (create_array_type (NULL, elt_type, index_type));
+  return value::allocate (create_array_type (alloc, elt_type, index_type));
 }
 

 
@@ -8474,8 +8473,10 @@ to_fixed_array_type (struct type *type0, struct value *dval,
       if (elt_type0 == elt_type && !constrained_packed_array_p)
 	result = type0;
       else
-	result = create_array_type (type_allocator (type0).new_type (),
-				    elt_type, type0->index_type ());
+	{
+	  type_allocator alloc (type0);
+	  result = create_array_type (alloc, elt_type, type0->index_type ());
+	}
     }
   else
     {
@@ -8506,8 +8507,8 @@ to_fixed_array_type (struct type *type0, struct value *dval,
 	  struct type *range_type =
 	    to_fixed_range_type (index_type_desc->field (i).type (), dval);
 
-	  result = create_array_type (type_allocator (elt_type0).new_type (),
-				      result, range_type);
+	  type_allocator alloc (elt_type0);
+	  result = create_array_type (alloc, result, range_type);
 	  elt_type0 = elt_type0->target_type ();
 	}
     }
diff --git a/gdb/coffread.c b/gdb/coffread.c
index 170f7761444..365e47cb12c 100644
--- a/gdb/coffread.c
+++ b/gdb/coffread.c
@@ -1781,8 +1781,7 @@ decode_type (struct coff_symbol *cs, unsigned int c_type,
 	  type_allocator alloc (objfile);
 	  range_type
 	    = create_static_range_type (alloc, index_type, 0, n - 1);
-	  type =
-	    create_array_type (NULL, base_type, range_type);
+	  type = create_array_type (alloc, base_type, range_type);
 	}
       return type;
     }
diff --git a/gdb/ctfread.c b/gdb/ctfread.c
index 3453800079e..d1a57d77a50 100644
--- a/gdb/ctfread.c
+++ b/gdb/ctfread.c
@@ -833,7 +833,7 @@ read_array_type (struct ctf_context *ccp, ctf_id_t tid)
 
   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);
+  type = create_array_type (alloc, element_type, range_type);
   if (ar.ctr_nelems <= 1)	/* Check if undefined upper bound.  */
     {
       range_type->bounds ()->high.set_undefined ();
diff --git a/gdb/dwarf2/read.c b/gdb/dwarf2/read.c
index 83be77da3ea..db5606c31cf 100644
--- a/gdb/dwarf2/read.c
+++ b/gdb/dwarf2/read.c
@@ -13658,7 +13658,7 @@ read_array_type (struct die_info *die, struct dwarf2_cu *cu)
       index_type = objfile_type (objfile)->builtin_int;
       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,
+      type = create_array_type_with_stride (alloc, element_type, range_type,
 					    byte_stride_prop, bit_stride);
       return set_die_type (die, type, cu);
     }
@@ -13695,13 +13695,14 @@ read_array_type (struct die_info *die, struct dwarf2_cu *cu)
 
   type = element_type;
 
+  type_allocator alloc (cu->per_objfile->objfile);
   if (read_array_order (die, cu) == DW_ORD_col_major)
     {
       int i = 0;
 
       while (i < range_types.size ())
 	{
-	  type = create_array_type_with_stride (NULL, type, range_types[i++],
+	  type = create_array_type_with_stride (alloc, type, range_types[i++],
 						byte_stride_prop, bit_stride);
 	  type->set_is_multi_dimensional (true);
 	  bit_stride = 0;
@@ -13713,7 +13714,7 @@ read_array_type (struct die_info *die, struct dwarf2_cu *cu)
       size_t ndim = range_types.size ();
       while (ndim-- > 0)
 	{
-	  type = create_array_type_with_stride (NULL, type, range_types[ndim],
+	  type = create_array_type_with_stride (alloc, type, range_types[ndim],
 						byte_stride_prop, bit_stride);
 	  type->set_is_multi_dimensional (true);
 	  bit_stride = 0;
@@ -14509,7 +14510,7 @@ read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
       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);
+  type = create_string_type (alloc, char_type, range_type);
 
   return set_die_type (die, type, cu);
 }
diff --git a/gdb/f-exp.y b/gdb/f-exp.y
index 9ff8e1988fc..0b4ee48ce01 100644
--- a/gdb/f-exp.y
+++ b/gdb/f-exp.y
@@ -675,9 +675,9 @@ ptype	:	typebase
 			    range_type =
 			      create_static_range_type (alloc, idx_type,
 							0, array_size - 1);
-			    follow_type =
-			      create_array_type ((struct type *) NULL,
-						 follow_type, range_type);
+			    follow_type = create_array_type (alloc,
+							     follow_type,
+							     range_type);
 			  }
 			else
 			  follow_type = lookup_pointer_type (follow_type);
diff --git a/gdb/f-lang.c b/gdb/f-lang.c
index 2da50b27267..365e0c0489b 100644
--- a/gdb/f-lang.c
+++ b/gdb/f-lang.c
@@ -137,7 +137,7 @@ fortran_bounds_all_dims (bool lbound_p,
 				builtin_f_type (gdbarch)->builtin_integer,
 				1, ndimensions);
   struct type *elm_type = builtin_f_type (gdbarch)->builtin_integer;
-  struct type *result_type = create_array_type (nullptr, elm_type, range);
+  struct type *result_type = create_array_type (alloc, elm_type, range);
   struct value *result = value::allocate (result_type);
 
   /* Walk the array dimensions backwards due to the way the array will be
@@ -721,7 +721,7 @@ fortran_array_shape (struct gdbarch *gdbarch, const language_defn *lang,
 				builtin_type (gdbarch)->builtin_int,
 				1, ndimensions);
   struct type *elm_type = builtin_f_type (gdbarch)->builtin_integer;
-  struct type *result_type = create_array_type (nullptr, elm_type, range);
+  struct type *result_type = create_array_type (alloc, elm_type, range);
   struct value *result = value::allocate (result_type);
   LONGEST elm_len = elm_type->length ();
 
@@ -1402,7 +1402,7 @@ fortran_undetermined::value_subarray (value *array,
 					 &p_low, &p_high, 0, &p_stride,
 					 true);
       array_slice_type
-	= create_array_type (nullptr, array_slice_type, new_range);
+	= create_array_type (alloc, array_slice_type, new_range);
     }
 
   if (fortran_array_slicing_debug)
@@ -1439,7 +1439,7 @@ fortran_undetermined::value_subarray (value *array,
 					     &p_low, &p_high, 0, &p_stride,
 					     true);
 	  repacked_array_type
-	    = create_array_type (nullptr, repacked_array_type, new_range);
+	    = create_array_type (alloc, repacked_array_type, new_range);
 	}
 
       /* Now copy the elements from the original ARRAY into the packed
diff --git a/gdb/gdbtypes.c b/gdb/gdbtypes.c
index 587f953fa09..026f6492398 100644
--- a/gdb/gdbtypes.c
+++ b/gdb/gdbtypes.c
@@ -1337,30 +1337,10 @@ update_static_array_size (struct type *type)
   return false;
 }
 
-/* Create an array type using either a blank type supplied in
-   RESULT_TYPE, or creating a new type, inheriting the objfile from
-   RANGE_TYPE.
-
-   Elements will be of type ELEMENT_TYPE, the indices will be of type
-   RANGE_TYPE.
-
-   BYTE_STRIDE_PROP, when not NULL, provides the array's byte stride.
-   This byte stride property is added to the resulting array type
-   as a DYN_PROP_BYTE_STRIDE.  As a consequence, the BYTE_STRIDE_PROP
-   argument can only be used to create types that are objfile-owned
-   (see add_dyn_prop), meaning that either this function must be called
-   with an objfile-owned RESULT_TYPE, or an objfile-owned RANGE_TYPE.
-
-   BIT_STRIDE is taken into account only when BYTE_STRIDE_PROP is NULL.
-   If BIT_STRIDE is not zero, build a packed array type whose element
-   size is BIT_STRIDE.  Otherwise, ignore this parameter.
-
-   FIXME: Maybe we should check the TYPE_CODE of RESULT_TYPE to make
-   sure it is TYPE_CODE_UNDEF before we bash it into an array
-   type?  */
+/* See gdbtypes.h.  */
 
 struct type *
-create_array_type_with_stride (struct type *result_type,
+create_array_type_with_stride (type_allocator &alloc,
 			       struct type *element_type,
 			       struct type *range_type,
 			       struct dynamic_prop *byte_stride_prop,
@@ -1377,8 +1357,7 @@ create_array_type_with_stride (struct type *result_type,
       byte_stride_prop = NULL;
     }
 
-  if (result_type == NULL)
-    result_type = type_allocator (range_type).new_type ();
+  struct type *result_type = alloc.new_type ();
 
   result_type->set_code (TYPE_CODE_ARRAY);
   result_type->set_target_type (element_type);
@@ -1410,15 +1389,14 @@ create_array_type_with_stride (struct type *result_type,
   return result_type;
 }
 
-/* Same as create_array_type_with_stride but with no bit_stride
-   (BIT_STRIDE = 0), thus building an unpacked array.  */
+/* See gdbtypes.h.  */
 
 struct type *
-create_array_type (struct type *result_type,
+create_array_type (type_allocator &alloc,
 		   struct type *element_type,
 		   struct type *range_type)
 {
-  return create_array_type_with_stride (result_type, element_type,
+  return create_array_type_with_stride (alloc, element_type,
 					range_type, NULL, 0);
 }
 
@@ -1438,29 +1416,19 @@ lookup_array_range_type (struct type *element_type,
   range_type = create_static_range_type (alloc, index_type,
 					 low_bound, high_bound);
 
-  return create_array_type (NULL, element_type, range_type);
+  return create_array_type (alloc, element_type, range_type);
 }
 
-/* Create a string type using either a blank type supplied in
-   RESULT_TYPE, or creating a new type.  String types are similar
-   enough to array of char types that we can use create_array_type to
-   build the basic type and then bash it into a string type.
-
-   For fixed length strings, the range type contains 0 as the lower
-   bound and the length of the string minus one as the upper bound.
-
-   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 string
-   type?  */
+/* See gdbtypes.h.  */
 
 struct type *
-create_string_type (struct type *result_type,
+create_string_type (type_allocator &alloc,
 		    struct type *string_char_type,
 		    struct type *range_type)
 {
-  result_type = create_array_type (result_type,
-				   string_char_type,
-				   range_type);
+  struct type *result_type = create_array_type (alloc,
+						string_char_type,
+						range_type);
   result_type->set_code (TYPE_CODE_STRING);
   return result_type;
 }
@@ -2372,7 +2340,8 @@ resolve_dynamic_array_or_string_1 (struct type *type,
   else
     bit_stride = TYPE_FIELD_BITSIZE (type, 0);
 
-  return create_array_type_with_stride (type, elt_type, range_type, NULL,
+  type_allocator alloc (type, type_allocator::SMASH);
+  return create_array_type_with_stride (alloc, elt_type, range_type, NULL,
 					bit_stride);
 }
 
diff --git a/gdb/gdbtypes.h b/gdb/gdbtypes.h
index 83fbfdbcee0..c1964b42e67 100644
--- a/gdb/gdbtypes.h
+++ b/gdb/gdbtypes.h
@@ -2478,9 +2478,24 @@ extern struct type *lookup_function_type_with_arguments (struct type *,
 extern struct type *create_static_range_type (type_allocator &, struct type *,
 					      LONGEST, LONGEST);
 
+/* Create an array type using ALLOC.
+
+   Elements will be of type ELEMENT_TYPE, the indices will be of type
+   RANGE_TYPE.
+
+   BYTE_STRIDE_PROP, when not NULL, provides the array's byte stride.
+   This byte stride property is added to the resulting array type
+   as a DYN_PROP_BYTE_STRIDE.  As a consequence, the BYTE_STRIDE_PROP
+   argument can only be used to create types that are objfile-owned
+   (see add_dyn_prop), meaning that either this function must be called
+   with an objfile-owned RESULT_TYPE, or an objfile-owned RANGE_TYPE.
+
+   BIT_STRIDE is taken into account only when BYTE_STRIDE_PROP is NULL.
+   If BIT_STRIDE is not zero, build a packed array type whose element
+   size is BIT_STRIDE.  Otherwise, ignore this parameter.  */
 
 extern struct type *create_array_type_with_stride
-  (struct type *, struct type *, struct type *,
+  (type_allocator &, struct type *, struct type *,
    struct dynamic_prop *, unsigned int);
 
 extern struct type *create_range_type (type_allocator &, struct type *,
@@ -2498,13 +2513,24 @@ extern struct type *create_range_type_with_stride
    const struct dynamic_prop *high_bound, LONGEST bias,
    const struct dynamic_prop *stride, bool byte_stride_p);
 
-extern struct type *create_array_type (struct type *, struct type *,
+/* Same as create_array_type_with_stride but with no bit_stride
+   (BIT_STRIDE = 0), thus building an unpacked array.  */
+
+extern struct type *create_array_type (type_allocator &, struct type *,
 				       struct type *);
 
 extern struct type *lookup_array_range_type (struct type *, LONGEST, LONGEST);
 
-extern struct type *create_string_type (struct type *, struct type *,
+/* Create a string type using ALLOC.  String types are similar enough
+   to array of char types that we can use create_array_type to build
+   the basic type and then bash it into a string type.
+
+   For fixed length strings, the range type contains 0 as the lower
+   bound and the length of the string minus one as the upper bound.  */
+
+extern struct type *create_string_type (type_allocator &, struct type *,
 					struct type *);
+
 extern struct type *lookup_string_range_type (struct type *, LONGEST, LONGEST);
 
 extern struct type *create_set_type (struct type *, struct type *);
diff --git a/gdb/mdebugread.c b/gdb/mdebugread.c
index 3c299d739fe..eb6f593580b 100644
--- a/gdb/mdebugread.c
+++ b/gdb/mdebugread.c
@@ -1866,7 +1866,7 @@ upgrade_type (int fd, struct type **tpp, int tq, union aux_ext *ax, int bigend,
 	type_allocator alloc (indx);
 	range = create_static_range_type (alloc, indx, lower, upper);
 
-	t = create_array_type (NULL, *tpp, range);
+	t = create_array_type (alloc, *tpp, range);
       }
 
       /* We used to fill in the supplied array element bitsize
diff --git a/gdb/stabsread.c b/gdb/stabsread.c
index 1e96102dfe4..c8c49e15403 100644
--- a/gdb/stabsread.c
+++ b/gdb/stabsread.c
@@ -883,7 +883,7 @@ define_symbol (CORE_ADDR valu, const char *string, int desc, int type,
 					  objfile_type (objfile)->builtin_int,
 					  0, ind);
 	    sym->set_type
-	      (create_array_type (NULL, objfile_type (objfile)->builtin_char,
+	      (create_array_type (alloc, objfile_type (objfile)->builtin_char,
 				  range_type));
 	    string_value
 	      = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, ind + 1);
@@ -3551,7 +3551,8 @@ read_array_type (const char **pp, struct type *type,
   type_allocator alloc (objfile);
   range_type =
     create_static_range_type (alloc, index_type, lower, upper);
-  type = create_array_type (type, element_type, range_type);
+  type_allocator smash_alloc (type, type_allocator::SMASH);
+  type = create_array_type (smash_alloc, element_type, range_type);
 
   return type;
 }
diff --git a/gdb/valops.c b/gdb/valops.c
index 6fb393fc2ac..657bc4af4d4 100644
--- a/gdb/valops.c
+++ b/gdb/valops.c
@@ -495,7 +495,7 @@ value_cast (struct type *type, struct value *arg2)
 						 range_type->target_type (),
 						 low_bound,
 						 new_length + low_bound - 1);
-	  arg2->deprecated_set_type (create_array_type (NULL,
+	  arg2->deprecated_set_type (create_array_type (alloc,
 							element_type, 
 							range_type));
 	  return arg2;
@@ -4074,7 +4074,7 @@ value_slice (struct value *array, int lowbound, int length)
     LONGEST offset
       = (lowbound - lowerbound) * check_typedef (element_type)->length ();
 
-    slice_type = create_array_type (NULL,
+    slice_type = create_array_type (alloc,
 				    element_type,
 				    slice_range_type);
     slice_type->set_code (array_type->code ());

-- 
2.39.1



More information about the Gdb-patches mailing list