[commit/Ada] Add support for unconstrained packed arrays

Joel Brobecker
2009-11-19T22:55:30Z

[-- Attachment #1: Type: text/plain, Size: 175 bytes --]

Suprisingly, we recently found out that we had not implemented support
for unconstrained packed arrays. This patch fixes that.

Tested on x86_64-linux, checked in.

--
Joel

[-- Attachment #2: 0004--Ada-Add-support-for-unconstrained-packed-arrays.patch --]
[-- Type: text/x-diff, Size: 17101 bytes --]

gdb/
2009-11-19 Jerome Guitton <guitton@adacore.com>

* ada-lang.c (packed_array_type): Rename to...
(constrained_packed_array_type): ...and update comment.
(decode_packed_array, decode_constrained_packed_array): Ditto.
(decode_packed_array_type, decode_constrained_packed_array_type):
Ditto.
(ada_is_constrained_packed_array_type): New function.
(ada_is_unconstrained_packed_array_type): New function.
(decode_packed_array_bitsize): New function, extracted from
decode_packed_array_type.
(ada_type_of_array): Add support for unconstrained packed arrays.
(ada_coerce_to_simple_array_ptr, ada_coerce_to_simple_array)
(ada_array_bound_from_type, ada_array_bound, ada_array_length)
(ada_prefer_type, find_parallel_type_by_descriptive_type)
(to_fixed_array_type, ada_evaluate_subexp): Resync.
* ada-lang.h (ada_is_packed_array_type,
ada_is_constrained_packed_array_type): Renaming.
* ada-valprint.c (ada_val_print_1): Resync.
* ada-typeprint.c (print_array_type, ada_print_type): Resync.

Tested on x86_64-linux.
---
gdb/ada-lang.c | 165 +++++++++++++++++++++++++++++++++++----------------
gdb/ada-lang.h | 2 +-
gdb/ada-typeprint.c | 4 +-
gdb/ada-valprint.c | 3 +-
4 files changed, 119 insertions(+), 55 deletions(-)

diff --git a/gdb/ada-lang.c b/gdb/ada-lang.c
index 173eb09..e7a8e3a 100644
--- a/gdb/ada-lang.c
+++ b/gdb/ada-lang.c
@@ -176,11 +176,17 @@ static struct type *static_unwrap_type (struct type *type);

static struct value *unwrap_value (struct value *);

-static struct type *packed_array_type (struct type *, long *);
+static struct type *constrained_packed_array_type (struct type *, long *);

-static struct type *decode_packed_array_type (struct type *);
+static struct type *decode_constrained_packed_array_type (struct type *);

-static struct value *decode_packed_array (struct value *);
+static long decode_packed_array_bitsize (struct type *);
+
+static struct value *decode_constrained_packed_array (struct value *);
+
+static int ada_is_packed_array_type (struct type *);
+
+static int ada_is_unconstrained_packed_array_type (struct type *);

static struct value *value_subscript_packed (struct value *, int,
struct value **);
@@ -1625,15 +1631,23 @@ ada_is_bogus_array_descriptor (struct type *type)
struct type *
ada_type_of_array (struct value *arr, int bounds)
{
- if (ada_is_packed_array_type (value_type (arr)))
- return decode_packed_array_type (value_type (arr));
+ if (ada_is_constrained_packed_array_type (value_type (arr)))
+ return decode_constrained_packed_array_type (value_type (arr));

if (!ada_is_array_descriptor_type (value_type (arr)))
return value_type (arr);

if (!bounds)
- return
- ada_check_typedef (desc_data_target_type (value_type (arr)));
+ {
+ struct type *array_type =
+ ada_check_typedef (desc_data_target_type (value_type (arr)));
+
+ if (ada_is_unconstrained_packed_array_type (value_type (arr)))
+ TYPE_FIELD_BITSIZE (array_type, 0) =
+ decode_packed_array_bitsize (value_type (arr));
+
+ return array_type;
+ }
else
{
struct type *elt_type;
@@ -1661,6 +1675,10 @@ ada_type_of_array (struct value *arr, int bounds)
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 (value_type (arr)))
+ TYPE_FIELD_BITSIZE (elt_type, 0) =
+ decode_packed_array_bitsize (value_type (arr));
}

return lookup_pointer_type (elt_type);
@@ -1682,8 +1700,8 @@ ada_coerce_to_simple_array_ptr (struct value *arr)
return NULL;
return value_cast (arrType, value_copy (desc_data (arr)));
}
- else if (ada_is_packed_array_type (value_type (arr)))
- return decode_packed_array (arr);
+ else if (ada_is_constrained_packed_array_type (value_type (arr)))
+ return decode_constrained_packed_array (arr);
else
return arr;
}
@@ -1703,8 +1721,8 @@ ada_coerce_to_simple_array (struct value *arr)
check_size (TYPE_TARGET_TYPE (value_type (arrVal)));
return value_ind (arrVal);
}
- else if (ada_is_packed_array_type (value_type (arr)))
- return decode_packed_array (arr);
+ else if (ada_is_constrained_packed_array_type (value_type (arr)))
+ return decode_constrained_packed_array (arr);
else
return arr;
}
@@ -1716,8 +1734,8 @@ ada_coerce_to_simple_array (struct value *arr)
struct type *
ada_coerce_to_simple_array_type (struct type *type)
{
- if (ada_is_packed_array_type (type))
- return decode_packed_array_type (type);
+ if (ada_is_constrained_packed_array_type (type))
+ return decode_constrained_packed_array_type (type);

if (ada_is_array_descriptor_type (type))
return ada_check_typedef (desc_data_target_type (type));
@@ -1727,8 +1745,8 @@ ada_coerce_to_simple_array_type (struct type *type)

/* Non-zero iff TYPE represents a standard GNAT packed-array type. */

-int
-ada_is_packed_array_type (struct type *type)
+static int
+ada_is_packed_array_type (struct type *type)
{
if (type == NULL)
return 0;
@@ -1739,6 +1757,54 @@ ada_is_packed_array_type (struct type *type)
&& strstr (ada_type_name (type), "___XP") != NULL;
}

+/* Non-zero iff TYPE represents a standard GNAT constrained
+ packed-array type. */
+
+int
+ada_is_constrained_packed_array_type (struct type *type)
+{
+ return ada_is_packed_array_type (type)
+ && !ada_is_array_descriptor_type (type);
+}
+
+/* Non-zero iff TYPE represents an array descriptor for a
+ unconstrained packed-array type. */
+
+static int
+ada_is_unconstrained_packed_array_type (struct type *type)
+{
+ return ada_is_packed_array_type (type)
+ && ada_is_array_descriptor_type (type);
+}
+
+/* Given that TYPE encodes a packed array type (constrained or unconstrained),
+ return the size of its elements in bits. */
+
+static long
+decode_packed_array_bitsize (struct type *type)
+{
+ char *raw_name = ada_type_name (ada_check_typedef (type));
+ char *tail;
+ long bits;
+
+ if (!raw_name)
+ raw_name = ada_type_name (desc_base_type (type));
+
+ if (!raw_name)
+ return 0;
+
+ tail = strstr (raw_name, "___XP");
+
+ if (sscanf (tail + sizeof ("___XP") - 1, "%ld", &bits) != 1)
+ {
+ lim_warning
+ (_("could not understand bit size information on packed array"));
+ return 0;
+ }
+
+ return bits;
+}
+
/* Given that TYPE is a standard GDB array type with all bounds filled
in, and that the element size of its ultimate scalar constituents
(that is, either its elements, or, if it is an array of arrays, its
@@ -1749,7 +1815,7 @@ ada_is_packed_array_type (struct type *type)
in bits. */

static struct type *
-packed_array_type (struct type *type, long *elt_bits)
+constrained_packed_array_type (struct type *type, long *elt_bits)
{
struct type *new_elt_type;
struct type *new_type;
@@ -1760,8 +1826,9 @@ packed_array_type (struct type *type, long *elt_bits)
return type;

new_type = alloc_type_copy (type);
- new_elt_type = packed_array_type (ada_check_typedef (TYPE_TARGET_TYPE (type)),
- elt_bits);
+ new_elt_type =
+ constrained_packed_array_type (ada_check_typedef (TYPE_TARGET_TYPE (type)),
+ elt_bits);
create_array_type (new_type, new_elt_type, TYPE_INDEX_TYPE (type));
TYPE_FIELD_BITSIZE (new_type, 0) = *elt_bits;
TYPE_NAME (new_type) = ada_type_name (type);
@@ -1782,10 +1849,11 @@ packed_array_type (struct type *type, long *elt_bits)
return new_type;
}

-/* The array type encoded by TYPE, where ada_is_packed_array_type (TYPE). */
+/* The array type encoded by TYPE, where
+ ada_is_constrained_packed_array_type (TYPE). */

static struct type *
-decode_packed_array_type (struct type *type)
+decode_constrained_packed_array_type (struct type *type)
{
struct symbol *sym;
struct block **blocks;
@@ -1824,24 +1892,18 @@ decode_packed_array_type (struct type *type)
return NULL;
}

- if (sscanf (tail + sizeof ("___XP") - 1, "%ld", &bits) != 1)
- {
- lim_warning
- (_("could not understand bit size information on packed array"));
- return NULL;
- }
-
- return packed_array_type (shadow_type, &bits);
+ bits = decode_packed_array_bitsize (type);
+ return constrained_packed_array_type (shadow_type, &bits);
}

-/* Given that ARR is a struct value *indicating a GNAT packed array,
- returns a simple array that denotes that array. Its type is a
+/* Given that ARR is a struct value *indicating a GNAT constrained packed
+ array, returns a simple array that denotes that array. Its type is a
standard GDB array type except that the BITSIZEs of the array
target types are set to the number of bits in each element, and the
type length is set appropriately. */

static struct value *
-decode_packed_array (struct value *arr)
+decode_constrained_packed_array (struct value *arr)
{
struct type *type;

@@ -1856,7 +1918,7 @@ decode_packed_array (struct value *arr)
if (TYPE_CODE (value_type (arr)) == TYPE_CODE_PTR)
arr = value_ind (arr);

- type = decode_packed_array_type (value_type (arr));
+ type = decode_constrained_packed_array_type (value_type (arr));
if (type == NULL)
{
error (_("can't unpack array"));
@@ -2489,8 +2551,8 @@ ada_array_bound_from_type (struct type * arr_type, int n, int which)

gdb_assert (which == 0 || which == 1);

- if (ada_is_packed_array_type (arr_type))
- arr_type = decode_packed_array_type (arr_type);
+ if (ada_is_constrained_packed_array_type (arr_type))
+ arr_type = decode_constrained_packed_array_type (arr_type);

if (arr_type == NULL || !ada_is_simple_array_type (arr_type))
return (LONGEST) - which;
@@ -2539,8 +2601,8 @@ ada_array_bound (struct value *arr, int n, int which)
{
struct type *arr_type = value_type (arr);

- if (ada_is_packed_array_type (arr_type))
- return ada_array_bound (decode_packed_array (arr), n, which);
+ if (ada_is_constrained_packed_array_type (arr_type))
+ return ada_array_bound (decode_constrained_packed_array (arr), n, which);
else if (ada_is_simple_array_type (arr_type))
return ada_array_bound_from_type (arr_type, n, which);
else
@@ -2558,8 +2620,8 @@ ada_array_length (struct value *arr, int n)
{
struct type *arr_type = ada_check_typedef (value_type (arr));

- if (ada_is_packed_array_type (arr_type))
- return ada_array_length (decode_packed_array (arr), n);
+ if (ada_is_constrained_packed_array_type (arr_type))
+ return ada_array_length (decode_constrained_packed_array (arr), n);

if (ada_is_simple_array_type (arr_type))
return (ada_array_bound_from_type (arr_type, n, 1)
@@ -6589,7 +6651,7 @@ ada_prefer_type (struct type *type0, struct type *type1)
return 0;
else if (TYPE_NAME (type1) == NULL && TYPE_NAME (type0) != NULL)
return 1;
- else if (ada_is_packed_array_type (type0))
+ else if (ada_is_constrained_packed_array_type (type0))
return 1;
else if (ada_is_array_descriptor_type (type0)
&& !ada_is_array_descriptor_type (type1))
@@ -6659,7 +6721,7 @@ find_parallel_type_by_descriptive_type (struct type *type, const char *name)
older compilers, the descriptive type information is either absent or
irrelevant when it comes to packed arrays so the above lookup fails.
Fall back to using a parallel lookup by name in this case. */
- if (result == NULL && ada_is_packed_array_type (type))
+ if (result == NULL && ada_is_constrained_packed_array_type (type))
return ada_find_any_type (name);

return result;
@@ -7220,14 +7282,14 @@ to_fixed_array_type (struct type *type0, struct value *dval,
{
struct type *index_type_desc;
struct type *result;
- int packed_array_p;
+ int constrained_packed_array_p;

if (TYPE_FIXED_INSTANCE (type0))
return type0;

- packed_array_p = ada_is_packed_array_type (type0);
- if (packed_array_p)
- type0 = decode_packed_array_type (type0);
+ constrained_packed_array_p = ada_is_constrained_packed_array_type (type0);
+ if (constrained_packed_array_p)
+ type0 = decode_constrained_packed_array_type (type0);

index_type_desc = ada_find_parallel_type (type0, "___XA");
if (index_type_desc == NULL)
@@ -7249,7 +7311,7 @@ to_fixed_array_type (struct type *type0, struct value *dval,
/* Make sure we always create a new array type when dealing with
packed array types, since we're going to fix-up the array
type length and element bitsize a little further down. */
- if (elt_type0 == elt_type && !packed_array_p)
+ if (elt_type0 == elt_type && !constrained_packed_array_p)
result = type0;
else
result = create_array_type (alloc_type_copy (type0),
@@ -7292,7 +7354,7 @@ to_fixed_array_type (struct type *type0, struct value *dval,
error (_("array type with dynamic size is larger than varsize-limit"));
}

- if (packed_array_p)
+ if (constrained_packed_array_p)
{
/* So far, the resulting type has been created as if the original
type was a regular (non-packed) array type. As a result, the
@@ -8942,7 +9004,8 @@ ada_evaluate_subexp (struct type *expect_type, struct expression *exp,
goto nosideret;
}

- if (ada_is_packed_array_type (desc_base_type (value_type (argvec[0]))))
+ if (ada_is_constrained_packed_array_type
+ (desc_base_type (value_type (argvec[0]))))
argvec[0] = ada_coerce_to_simple_array (argvec[0]);
else if (TYPE_CODE (value_type (argvec[0])) == TYPE_CODE_ARRAY
&& TYPE_FIELD_BITSIZE (value_type (argvec[0]), 0) != 0)
@@ -9055,7 +9118,7 @@ ada_evaluate_subexp (struct type *expect_type, struct expression *exp,
TYPE_TARGET_TYPE (value_type (array)) =
ada_aligned_type (TYPE_TARGET_TYPE (value_type (array)));

- if (ada_is_packed_array_type (value_type (array)))
+ if (ada_is_constrained_packed_array_type (value_type (array)))
error (_("cannot slice a packed array"));

/* If this is a reference to an array or an array lvalue,
@@ -9220,7 +9283,7 @@ ada_evaluate_subexp (struct type *expect_type, struct expression *exp,
{
arg1 = ada_coerce_ref (arg1);

- if (ada_is_packed_array_type (value_type (arg1)))
+ if (ada_is_constrained_packed_array_type (value_type (arg1)))
arg1 = ada_coerce_to_simple_array (arg1);

type = ada_index_type (value_type (arg1), tem,
@@ -9275,8 +9338,8 @@ ada_evaluate_subexp (struct type *expect_type, struct expression *exp,
{
LONGEST low, high;

- if (ada_is_packed_array_type (type_arg))
- type_arg = decode_packed_array_type (type_arg);
+ if (ada_is_constrained_packed_array_type (type_arg))
+ type_arg = decode_constrained_packed_array_type (type_arg);

type = ada_index_type (type_arg, tem, ada_attribute_name (op));
if (type == NULL)
diff --git a/gdb/ada-lang.h b/gdb/ada-lang.h
index 8a465d4..cf4bebc 100644
--- a/gdb/ada-lang.h
+++ b/gdb/ada-lang.h
@@ -243,7 +243,7 @@ extern struct type *ada_parent_type (struct type *);

extern int ada_is_ignored_field (struct type *, int);

-extern int ada_is_packed_array_type (struct type *);
+extern int ada_is_constrained_packed_array_type (struct type *);

extern struct value *ada_value_primitive_packed_val (struct value *,
const gdb_byte *,
diff --git a/gdb/ada-typeprint.c b/gdb/ada-typeprint.c
index 9fabd68..fe902e2 100644
--- a/gdb/ada-typeprint.c
+++ b/gdb/ada-typeprint.c
@@ -352,7 +352,7 @@ print_array_type (struct type *type, struct ui_file *stream, int show,
int bitsize;
int n_indices;

- if (ada_is_packed_array_type (type))
+ if (ada_is_constrained_packed_array_type (type))
type = ada_coerce_to_simple_array_type (type);

bitsize = 0;
@@ -770,7 +770,7 @@ ada_print_type (struct type *type0, char *varstring, struct ui_file *stream,

if (ada_is_aligner_type (type))
ada_print_type (ada_aligned_type (type), "", stream, show, level);
- else if (ada_is_packed_array_type (type))
+ else if (ada_is_constrained_packed_array_type (type))
{
if (TYPE_CODE (type) == TYPE_CODE_PTR)
{
diff --git a/gdb/ada-valprint.c b/gdb/ada-valprint.c
index ec40a69..f9c182d 100644
--- a/gdb/ada-valprint.c
+++ b/gdb/ada-valprint.c
@@ -686,7 +686,8 @@ ada_val_print_1 (struct type *type, const gdb_byte *valaddr0,

type = ada_check_typedef (type);

- if (ada_is_array_descriptor_type (type) || ada_is_packed_array_type (type))
+ if (ada_is_array_descriptor_type (type)
+ || ada_is_constrained_packed_array_type (type))
{
int retn;
struct value *mark = value_mark ();
--
1.6.0.4