]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
Use type allocator for range types
authorTom Tromey <tom@tromey.com>
Mon, 13 Mar 2023 18:53:48 +0000 (12:53 -0600)
committerTom Tromey <tom@tromey.com>
Sat, 18 Mar 2023 17:12:38 +0000 (11:12 -0600)
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>
gdb/ada-lang.c
gdb/coffread.c
gdb/ctfread.c
gdb/dwarf2/read.c
gdb/f-exp.y
gdb/f-lang.c
gdb/gdbtypes.c
gdb/gdbtypes.h
gdb/mdebugread.c
gdb/stabsread.c
gdb/valops.c

index b1c8912b0ea5bac76c85393b402c087acad140d1..cfb8d6e11102de2398220ce0e4f86bdb312b1452 100644 (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.  */
index 1b0442a2ad4d32d07287a30b5ba80cc4c119bdec..ef92ffdc571b9e4283ded1e9da33fe2151a54907 100644 (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);
        }
index ea5571959979c52efc98e7932bc83df1b435804d..3453800079e02e25ecc96ffb7f78f49aee3a05af 100644 (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.  */
     {
index 4279f392a0703b87057c5885fe8acad55889e64d..4d6cbfe6aa0f31db9f2b473674c594a9f99ac6aa 100644 (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;
index 30da537ea843ef10c51c4fff85968b3ebfb75665..9ff8e1988fc80000f7a25c67c042f0d73ca60f90 100644 (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,
index 3fc43346b057fcdd132602e25e6ac023cc19535e..2da50b27267a765f67510c1f9d6cc16d5ef02ab9 100644 (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);
index 103818fae9beccf1f7f7018ece7718b911ba07bd..ebf559a47c92773b0ca5c9038af89b9d2f0b03f5 100644 (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;
index 33937dd0fd66d89271b066108546b372a33a1d96..7f94a5736473279ca0d342386339f12a54d70a4c 100644 (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);
index 0c663cb344695eba23bb362718793ce34436de60..58959e7605efd77591ddd0886ae724466f144b0f 100644 (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.
index 7ed0ebf9b1c439d13ab5dfbb2be9b4c90353d926..1e96102dfe4c95afe79ee61c78ceff6fcbb21ca8 100644 (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);
 }
 
index 2cef24fc4c592f0ae6529613d2c9d765befd673d..f655427f7cc8318ca7cfd44ab09af43dd8625835 100644 (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);