+/* Assuming TYPE is a simple, non-empty array type, compute its upper
+ and lower bound. Save the low bound into LOW_BOUND if not NULL.
+ Save the high bound into HIGH_BOUND if not NULL.
+
+ Return 1 if the operation was successful. Return zero otherwise,
+ in which case the values of LOW_BOUND and HIGH_BOUNDS are unmodified.
+
+ Computing the array upper and lower bounds is pretty easy, but this
+ function does some additional verifications before returning them.
+ If something incorrect is detected, it is better to return a status
+ rather than throwing an error, making it easier for the caller to
+ implement an error-recovery plan. For instance, it may decide to
+ warn the user that the bounds were not found and then use some
+ default values instead. */
+
+int
+get_array_bounds (struct type *type, long *low_bound, long *high_bound)
+{
+ struct type *index = TYPE_INDEX_TYPE (type);
+ long low = 0;
+ long high = 0;
+
+ if (index == NULL)
+ return 0;
+
+ if (TYPE_CODE (index) == TYPE_CODE_RANGE)
+ {
+ low = TYPE_LOW_BOUND (index);
+ high = TYPE_HIGH_BOUND (index);
+ }
+ else if (TYPE_CODE (index) == TYPE_CODE_ENUM)
+ {
+ const int n_enums = TYPE_NFIELDS (index);
+
+ low = TYPE_FIELD_BITPOS (index, 0);
+ high = TYPE_FIELD_BITPOS (index, n_enums - 1);
+ }
+ else
+ return 0;
+
+ /* Abort if the lower bound is greater than the higher bound, except
+ when low = high + 1. This is a very common idiom used in Ada when
+ defining empty ranges (for instance "range 1 .. 0"). */
+ if (low > high + 1)
+ return 0;
+
+ if (low_bound)
+ *low_bound = low;
+
+ if (high_bound)
+ *high_bound = high;
+
+ return 1;
+}
+
+/* Print on STREAM using the given OPTIONS the index for the element
+ at INDEX of an array whose index type is INDEX_TYPE. */
+
+void
+maybe_print_array_index (struct type *index_type, LONGEST index,
+ struct ui_file *stream,
+ const struct value_print_options *options)
+{
+ struct value *index_value;
+
+ if (!options->print_array_indexes)
+ return;
+
+ index_value = value_from_longest (index_type, index);
+
+ LA_PRINT_ARRAY_INDEX (index_value, stream, options);
+}
+