summaryrefslogtreecommitdiff
path: root/gdb/valprint.c
diff options
context:
space:
mode:
authorJerome Guitton <guitton@adacore.com>2015-03-27 14:45:08 +0100
committerJoel Brobecker <brobecker@adacore.com>2015-05-15 14:03:46 -0700
commitaa7151351ed16c5a4eb1334c9a1af1b06dbb7a99 (patch)
tree44c933eb16bda42b110800a9e3cce8ce744a5174 /gdb/valprint.c
parent931e5bc3e19d1e279fc28c5cf5571f812c79b8d3 (diff)
Array indexed by non-contiguous enumeration types
In Ada, index types of arrays can be enumeration types, and enumeration types can be non-contiguous. In which case the address of elements is not given by the value of the index, but by its position in the enumeration type. In other words, in this example: type Color is (Blue, Red); for Color use (Blue => 8, Red => 12, Green => 16); type A is array (Color) of Integer; type B is array (1 .. 3) of Integer; Arrays of type A and B will have the same layout in memory, even if the enumeration Color has a hole in its set of integer value. Since recently support for such a feature was in ada-lang.c, where the array was casted to a regular continuous index range. We were losing the information of index type. And this was not quite working for subranges in variable-length fields; their bounds are expressed using the integer value of the bounds, not its position in the enumeration, and there was some confusion all over ada-lang.c as to whether we had the position or the integer value was used for indexes. The idea behind this patch is to clean this up by keeping the real representation of these array index types and bounds when representing the value, and only use the position when accessing the elements or computing the length. This first patch fixes the printing of such an array. To the best of my knowledge, this feature only exists in Ada so it should only affect this language. gdb/ChangeLog: Jerome Guitton <guitton@adacore.com>: * ada-lang.c (ada_value_ptr_subscript): Use enum position of index to get element instead of enum value. (ada_value_slice_from_ptr, ada_value_slice): Use enum position of index to compute length, but enum values to compute bounds. (ada_array_length): Use enum position of index instead of enum value. (pos_atr): Move position computation to... (ada_evaluate_subexp): Use enum values to compute bounds. * gdbtypes.c (discrete_position): ...this new function. * gdbtypes.h (discrete_position): New function declaration. * valprint.c (val_print_array_elements): Call discrete_position to handle array indexed by non-contiguous enumeration types. gdb/testsuite/ChangeLog: * gdb.ada/arr_enum_with_gap: New testcase.
Diffstat (limited to 'gdb/valprint.c')
-rw-r--r--gdb/valprint.c29
1 files changed, 24 insertions, 5 deletions
diff --git a/gdb/valprint.c b/gdb/valprint.c
index 9a70b2f208..294c6a86e1 100644
--- a/gdb/valprint.c
+++ b/gdb/valprint.c
@@ -1626,7 +1626,7 @@ val_print_array_elements (struct type *type,
{
unsigned int things_printed = 0;
unsigned len;
- struct type *elttype, *index_type;
+ struct type *elttype, *index_type, *base_index_type;
unsigned eltlen;
/* Position of the array element we are examining to see
whether it is repeated. */
@@ -1634,6 +1634,7 @@ val_print_array_elements (struct type *type,
/* Number of repetitions we have detected so far. */
unsigned int reps;
LONGEST low_bound, high_bound;
+ LONGEST low_pos, high_pos;
elttype = TYPE_TARGET_TYPE (type);
eltlen = TYPE_LENGTH (check_typedef (elttype));
@@ -1641,15 +1642,33 @@ val_print_array_elements (struct type *type,
if (get_array_bounds (type, &low_bound, &high_bound))
{
- /* The array length should normally be HIGH_BOUND - LOW_BOUND + 1.
+ if (TYPE_CODE (index_type) == TYPE_CODE_RANGE)
+ base_index_type = TYPE_TARGET_TYPE (index_type);
+ else
+ base_index_type = index_type;
+
+ /* Non-contiguous enumerations types can by used as index types
+ in some languages (e.g. Ada). In this case, the array length
+ shall be computed from the positions of the first and last
+ literal in the enumeration type, and not from the values
+ of these literals. */
+ if (!discrete_position (base_index_type, low_bound, &low_pos)
+ || !discrete_position (base_index_type, high_bound, &high_pos))
+ {
+ warning (_("unable to get positions in array, use bounds instead"));
+ low_pos = low_bound;
+ high_pos = high_bound;
+ }
+
+ /* The array length should normally be HIGH_POS - LOW_POS + 1.
But we have to be a little extra careful, because some languages
- such as Ada allow LOW_BOUND to be greater than HIGH_BOUND for
+ such as Ada allow LOW_POS to be greater than HIGH_POS for
empty arrays. In that situation, the array length is just zero,
not negative! */
- if (low_bound > high_bound)
+ if (low_pos > high_pos)
len = 0;
else
- len = high_bound - low_bound + 1;
+ len = high_pos - low_pos + 1;
}
else
{