]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
Introduce type::is_array_like and value_to_array
authorTom Tromey <tromey@adacore.com>
Mon, 7 Aug 2023 12:35:51 +0000 (06:35 -0600)
committerTom Tromey <tromey@adacore.com>
Tue, 5 Sep 2023 17:03:00 +0000 (11:03 -0600)
This adds the type::is_array_like method and the value_to_array
function.

The former can be used to see whether a given type is known to be
"array-like".  This is the currently the case for certain
compiler-generated structure types; in particular both the Ada and
Rust compilers do this.

gdb/gdbtypes.c
gdb/gdbtypes.h
gdb/valarith.c
gdb/value.h

index 6b2adc8fe8cf681fdfac0f457fc90f6cb1d1293c..c932210829991d471689a4d6e5c1cc83b6591242 100644 (file)
@@ -43,6 +43,8 @@
 #include "f-lang.h"
 #include <algorithm>
 #include "gmp-utils.h"
+#include "rust-lang.h"
+#include "ada-lang.h"
 
 /* The value of an invalid conversion badness.  */
 #define INVALID_CONVERSION 100
@@ -5945,6 +5947,20 @@ type::copy_fields (std::vector<struct field> &src)
   size_t size = nfields * sizeof (*this->fields ());
   memcpy (this->fields (), src.data (), size);
 }
+
+bool
+type::is_array_like ()
+{
+  if (code () == TYPE_CODE_ARRAY)
+    return true;
+  if (HAVE_GNAT_AUX_INFO (this))
+    return (ada_is_constrained_packed_array_type (this)
+           || ada_is_array_descriptor_type (this));
+  if (HAVE_RUST_SPECIFIC (this))
+    return rust_slice_type_p (this);
+  return false;
+}
+
 \f
 
 static const registry<gdbarch>::key<struct builtin_type> gdbtypes_data;
index 4668fba59c7f7400f5ba5f4763915b70feb93f57..f667b10ec7d6990d198493f53190fc42973397de 100644 (file)
@@ -1430,6 +1430,11 @@ struct type
     return this->code () == TYPE_CODE_PTR || TYPE_IS_REFERENCE (this);
   }
 
+  /* Return true if this type is "array-like".  This includes arrays,
+     but also some forms of structure type that are recognized as
+     representations of arrays by the type's language.  */
+  bool is_array_like ();
+
   /* * Type that is a pointer to this type.
      NULL if no such pointer-to type is known yet.
      The debugger may add the address of such a type
index 637047fd853f1c01ec48072a1f3fd402c9b22426..ef376f05936341179d7863eb61b16bb350df965c 100644 (file)
@@ -28,6 +28,8 @@
 #include "infcall.h"
 #include "gdbsupport/byte-vector.h"
 #include "gdbarch.h"
+#include "rust-lang.h"
+#include "ada-lang.h"
 
 /* Forward declarations.  */
 static struct value *value_subscripted_rvalue (struct value *array,
@@ -249,6 +251,25 @@ value_subscripted_rvalue (struct value *array, LONGEST index,
   return value_from_component (array, elt_type, elt_offs);
 }
 
+/* See value.h.  */
+
+struct value *
+value_to_array (struct value *val)
+{
+  struct type *type = check_typedef (val->type ());
+  if (type->code () == TYPE_CODE_ARRAY)
+    return val;
+
+  if (type->is_array_like ())
+    {
+      if (HAVE_GNAT_AUX_INFO (type))
+       return ada_coerce_to_simple_array (val);
+      if (HAVE_RUST_SPECIFIC (type))
+       return rust_slice_to_array (val);
+    }
+  return nullptr;
+}
+
 \f
 /* Check to see if either argument is a structure, or a reference to
    one.  This is called so we know whether to go ahead with the normal
index c28b731cc42f84560c6f15e4520eb7d0c59016a2..1d5a0018f9208ae5511413740a1eaa50109e8923 100644 (file)
@@ -1319,6 +1319,10 @@ extern struct value *value_repeat (struct value *arg1, int count);
 
 extern struct value *value_subscript (struct value *array, LONGEST index);
 
+/* Assuming VAL is array-like (see type::is_array_like), return an
+   array form of VAL.  */
+extern struct value *value_to_array (struct value *val);
+
 extern struct value *value_bitstring_subscript (struct type *type,
                                                struct value *bitstring,
                                                LONGEST index);