gdb: make get_discrete_bounds return bool
authorSimon Marchi <simon.marchi@efficios.com>
Wed, 9 Dec 2020 18:51:57 +0000 (13:51 -0500)
committerSimon Marchi <simon.marchi@polymtl.ca>
Wed, 9 Dec 2020 18:51:57 +0000 (13:51 -0500)
get_discrete_bounds currently has three possible return values (see its
current doc for details).  It appears that for all callers, it would be
sufficient to have a boolean "worked" / "didn't work" return value.

Change the return type of get_discrete_bounds to bool and adjust all
callers.  Doing so simplifies the following patch.

gdb/ChangeLog:

* gdbtypes.h (get_discrete_bounds): Return bool, adjust all
callers.
* gdbtypes.c (get_discrete_bounds): Return bool.

Change-Id: Ie51feee23c75f0cd7939742604282d745db59172

14 files changed:
gdb/ChangeLog
gdb/ada-lang.c
gdb/ada-valprint.c
gdb/c-lang.c
gdb/eval.c
gdb/f-array-walker.h
gdb/f-lang.c
gdb/gdbtypes.c
gdb/gdbtypes.h
gdb/m2-typeprint.c
gdb/m2-valprint.c
gdb/p-valprint.c
gdb/valarith.c
gdb/valops.c

index 468bbca3d02559fdb320e2511c1b66bd0d000ecf..e7f083ca58f5c8a4ec43ea469e60cf08a6f8d8bc 100644 (file)
@@ -1,3 +1,9 @@
+2020-12-09  Simon Marchi  <simon.marchi@efficios.com>
+
+       * gdbtypes.h (get_discrete_bounds): Return bool, adjust all
+       callers.
+       * gdbtypes.c (get_discrete_bounds): Return bool.
+
 2020-12-09  Simon Marchi  <simon.marchi@efficios.com>
 
        * ada-lang.c (ada_value_slice_from_ptr): Adjust.
index 5813ecded4472610c8b07e8a9e11bab1c18cb5f8..5e2f1ece9654513bdc2218578c79f674d77c8048 100644 (file)
@@ -2112,7 +2112,7 @@ constrained_packed_array_type (struct type *type, long *elt_bits)
 
   if ((check_typedef (index_type)->code () == TYPE_CODE_RANGE
        && is_dynamic_type (check_typedef (index_type)))
-      || get_discrete_bounds (index_type, &low_bound, &high_bound) < 0)
+      || !get_discrete_bounds (index_type, &low_bound, &high_bound))
     low_bound = high_bound = 0;
   if (high_bound < low_bound)
     *elt_bits = TYPE_LENGTH (new_type) = 0;
@@ -2182,7 +2182,7 @@ recursively_update_array_bitsize (struct type *type)
   gdb_assert (type->code () == TYPE_CODE_ARRAY);
 
   LONGEST low, high;
-  if (get_discrete_bounds (type->index_type (), &low, &high) < 0
+  if (!get_discrete_bounds (type->index_type (), &low, &high)
       || low > high)
     return 0;
   LONGEST our_len = high - low + 1;
@@ -2299,7 +2299,7 @@ value_subscript_packed (struct value *arr, int arity, struct value **ind)
          LONGEST lowerbound, upperbound;
          LONGEST idx;
 
-         if (get_discrete_bounds (range_type, &lowerbound, &upperbound) < 0)
+         if (!get_discrete_bounds (range_type, &lowerbound, &upperbound))
            {
              lim_warning (_("don't know bounds of array"));
              lowerbound = upperbound = 0;
index 6ddb584d11c18f2bc5ac3929eabd22719563384c..7d17f410c9268362872169144e7b4e84ed6db8eb 100644 (file)
@@ -136,7 +136,7 @@ val_print_packed_array_elements (struct type *type, const gdb_byte *valaddr,
   {
     LONGEST high;
 
-    if (get_discrete_bounds (index_type, &low, &high) < 0)
+    if (!get_discrete_bounds (index_type, &low, &high))
       len = 1;
     else if (low > high)
       {
index 2a17e007fd0db71d69e8e8d2e8854aa92a5ba357..c2db47e11e251963f81b83cb327ef02ce211aed9 100644 (file)
@@ -698,8 +698,8 @@ evaluate_subexp_c (struct type *expect_type, struct expression *exp,
                LONGEST low_bound, high_bound;
                int element_size = TYPE_LENGTH (type);
 
-               if (get_discrete_bounds (expect_type->index_type (),
-                                        &low_bound, &high_bound) < 0)
+               if (!get_discrete_bounds (expect_type->index_type (),
+                                         &low_bound, &high_bound))
                  {
                    low_bound = 0;
                    high_bound = (TYPE_LENGTH (expect_type) / element_size) - 1;
index d02813eec523c8c52c32773ebb423526a2b6c745..5eb6ce1a2638ae98fba114a5cfb25492f17446aa 100644 (file)
@@ -1386,7 +1386,7 @@ evaluate_subexp_standard (struct type *expect_type,
          int element_size = TYPE_LENGTH (check_typedef (element_type));
          LONGEST low_bound, high_bound, index;
 
-         if (get_discrete_bounds (range_type, &low_bound, &high_bound) < 0)
+         if (!get_discrete_bounds (range_type, &low_bound, &high_bound))
            {
              low_bound = 0;
              high_bound = (TYPE_LENGTH (type) / element_size) - 1;
@@ -1426,7 +1426,7 @@ evaluate_subexp_standard (struct type *expect_type,
                 || check_type->code () == TYPE_CODE_TYPEDEF)
            check_type = TYPE_TARGET_TYPE (check_type);
 
-         if (get_discrete_bounds (element_type, &low_bound, &high_bound) < 0)
+         if (!get_discrete_bounds (element_type, &low_bound, &high_bound))
            error (_("(power)set type with unknown size"));
          memset (valaddr, '\0', TYPE_LENGTH (type));
          for (tem = 0; tem < nargs; tem++)
index 417f9f079805bd9b50e1be7409f3d34df9bf6234..c202df6c65fbacdf780a5e2c9ad1e330aaa0cda3 100644 (file)
@@ -42,7 +42,7 @@ public:
 
     /* Get the range, and extract the bounds.  */
     struct type *range_type = type->index_type ();
-    if (get_discrete_bounds (range_type, &m_lowerbound, &m_upperbound) < 0)
+    if (!get_discrete_bounds (range_type, &m_lowerbound, &m_upperbound))
       error ("unable to read array bounds");
 
     /* Figure out the stride for this array.  */
@@ -198,7 +198,7 @@ private:
     /* Extract the range, and get lower and upper bounds.  */
     struct type *range_type = check_typedef (type)->index_type ();
     LONGEST lowerbound, upperbound;
-    if (get_discrete_bounds (range_type, &lowerbound, &upperbound) < 0)
+    if (!get_discrete_bounds (range_type, &lowerbound, &upperbound))
       error ("failed to get range bounds");
 
     /* CALC is used to calculate the offsets for each element in this
index 6771758bacb2765a53a52a4322b723a7a31eedaf..bbece501799378a1897748a9c40c2847dfc702ae 100644 (file)
@@ -1404,7 +1404,7 @@ fortran_adjust_dynamic_array_base_address_hack (struct type *type,
       tmp_type = check_typedef (tmp_type);
       struct type *range_type = tmp_type->index_type ();
       LONGEST lowerbound, upperbound, stride;
-      if (get_discrete_bounds (range_type, &lowerbound, &upperbound) < 0)
+      if (!get_discrete_bounds (range_type, &lowerbound, &upperbound))
        error ("failed to get range bounds");
 
       /* Figure out the stride for this dimension.  */
index 0c91edd48a54a14c391f8dccd3b357b472601c9c..f1b19b58aab4f805067f80417b8190e2d677f2cc 100644 (file)
@@ -1036,15 +1036,9 @@ has_static_range (const struct range_bounds *bounds)
          && bounds->stride.kind () == PROP_CONST);
 }
 
+/* See gdbtypes.h.  */
 
-/* Set *LOWP and *HIGHP to the lower and upper bounds of discrete type
-   TYPE.
-
-   Return 1 if type is a range type with two defined, constant bounds.
-   Else, return 0 if it is discrete (and bounds will fit in LONGEST).
-   Else, return -1.  */
-
-int
+bool
 get_discrete_bounds (struct type *type, LONGEST *lowp, LONGEST *highp)
 {
   type = check_typedef (type);
@@ -1055,7 +1049,7 @@ get_discrete_bounds (struct type *type, LONGEST *lowp, LONGEST *highp)
         constant bounds.  */
       if (type->bounds ()->low.kind () != PROP_CONST
          || type->bounds ()->high.kind () != PROP_CONST)
-       return -1;
+       return false;
 
       *lowp = type->bounds ()->low.const_val ();
       *highp = type->bounds ()->high.const_val ();
@@ -1065,20 +1059,17 @@ get_discrete_bounds (struct type *type, LONGEST *lowp, LONGEST *highp)
          gdb::optional<LONGEST> low_pos
            = discrete_position (TYPE_TARGET_TYPE (type), *lowp);
 
-         if (!low_pos.has_value ())
-           return 0;
-
-         *lowp = *low_pos;
+         if (low_pos.has_value ())
+           *lowp = *low_pos;
 
          gdb::optional<LONGEST> high_pos
            = discrete_position (TYPE_TARGET_TYPE (type), *highp);
 
-         if (!high_pos.has_value ())
-           return 0;
-
-         *highp = *high_pos;
+         if (high_pos.has_value ())
+           *highp = *high_pos;
        }
-      return 1;
+      return true;
+
     case TYPE_CODE_ENUM:
       if (type->num_fields () > 0)
        {
@@ -1104,19 +1095,22 @@ get_discrete_bounds (struct type *type, LONGEST *lowp, LONGEST *highp)
          *lowp = 0;
          *highp = -1;
        }
-      return 0;
+      return true;
+
     case TYPE_CODE_BOOL:
       *lowp = 0;
       *highp = 1;
-      return 0;
+      return true;
+
     case TYPE_CODE_INT:
       if (TYPE_LENGTH (type) > sizeof (LONGEST))       /* Too big */
-       return -1;
+       return false;
+
       if (!type->is_unsigned ())
        {
          *lowp = -(1 << (TYPE_LENGTH (type) * TARGET_CHAR_BIT - 1));
          *highp = -*lowp - 1;
-         return 0;
+         return true;
        }
       /* fall through */
     case TYPE_CODE_CHAR:
@@ -1126,9 +1120,10 @@ get_discrete_bounds (struct type *type, LONGEST *lowp, LONGEST *highp)
         if TYPE_LENGTH (type) == sizeof (LONGEST).  */
       *highp = 1 << (TYPE_LENGTH (type) * TARGET_CHAR_BIT - 1);
       *highp = (*highp - 1) | *highp;
-      return 0;
+      return true;
+
     default:
-      return -1;
+      return false;
     }
 }
 
@@ -1140,13 +1135,11 @@ get_array_bounds (struct type *type, LONGEST *low_bound, LONGEST *high_bound)
   struct type *index = type->index_type ();
   LONGEST low = 0;
   LONGEST high = 0;
-  int res;
 
   if (index == NULL)
     return false;
 
-  res = get_discrete_bounds (index, &low, &high);
-  if (res == -1)
+  if (!get_discrete_bounds (index, &low, &high))
     return false;
 
   if (low_bound)
@@ -1223,8 +1216,9 @@ update_static_array_size (struct type *type)
       if (stride == 0)
        stride = range_type->bit_stride ();
 
-      if (get_discrete_bounds (range_type, &low_bound, &high_bound) < 0)
+      if (!get_discrete_bounds (range_type, &low_bound, &high_bound))
        low_bound = high_bound = 0;
+
       element_type = check_typedef (TYPE_TARGET_TYPE (type));
       /* Be careful when setting the array length.  Ada arrays can be
         empty arrays with the high_bound being smaller than the low_bound.
@@ -1420,8 +1414,9 @@ create_set_type (struct type *result_type, struct type *domain_type)
     {
       LONGEST low_bound, high_bound, bit_length;
 
-      if (get_discrete_bounds (domain_type, &low_bound, &high_bound) < 0)
+      if (!get_discrete_bounds (domain_type, &low_bound, &high_bound))
        low_bound = high_bound = 0;
+
       bit_length = high_bound - low_bound + 1;
       TYPE_LENGTH (result_type)
        = (bit_length + TARGET_CHAR_BIT - 1) / TARGET_CHAR_BIT;
index e30485652702e3847ea73f96281a1b1411447915..19d07405d63d7a82fcad04437256401be4a38b3a 100644 (file)
@@ -2466,7 +2466,13 @@ extern struct type *lookup_template_type (const char *, struct type *,
 
 extern int get_vptr_fieldno (struct type *, struct type **);
 
-extern int get_discrete_bounds (struct type *, LONGEST *, LONGEST *);
+/* Set *LOWP and *HIGHP to the lower and upper bounds of discrete type
+   TYPE.
+
+   Return true if the two bounds are available, false otherwise.  */
+
+extern bool get_discrete_bounds (struct type *type, LONGEST *lowp,
+                                LONGEST *highp);
 
 /* 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.
index 8fbcdf4c603cc806b7df7d67713900ba7d61b72a..20a3d131476adfbdccada9751df83c3271c3ce53 100644 (file)
@@ -372,7 +372,7 @@ m2_is_long_set (struct type *type)
                            This should be integrated into gdbtypes.c
                            inside get_discrete_bounds.  */
 
-static int
+static bool
 m2_get_discrete_bounds (struct type *type, LONGEST *lowp, LONGEST *highp)
 {
   type = check_typedef (type);
@@ -419,7 +419,7 @@ m2_is_long_set_of_type (struct type *type, struct type **of_type)
       l1 = type->field (i).type ()->bounds ()->low.const_val ();
       h1 = type->field (len - 1).type ()->bounds ()->high.const_val ();
       *of_type = target;
-      if (m2_get_discrete_bounds (target, &l2, &h2) >= 0)
+      if (m2_get_discrete_bounds (target, &l2, &h2))
        return (l1 == l2 && h1 == h2);
       error (_("long_set failed to find discrete bounds for its subtype"));
       return 0;
index 96dc18119cf2898eda67f03c26c57cdb51e88549..542fa492d068e077e0f348373e3b3825b32544b3 100644 (file)
@@ -97,7 +97,7 @@ m2_print_long_set (struct type *type, const gdb_byte *valaddr,
 
   target = TYPE_TARGET_TYPE (range);
 
-  if (get_discrete_bounds (range, &field_low, &field_high) >= 0)
+  if (get_discrete_bounds (range, &field_low, &field_high))
     {
       for (i = low_bound; i <= high_bound; i++)
        {
@@ -137,7 +137,7 @@ m2_print_long_set (struct type *type, const gdb_byte *valaddr,
              if (field == len)
                break;
              range = type->field (field).type ()->index_type ();
-             if (get_discrete_bounds (range, &field_low, &field_high) < 0)
+             if (!get_discrete_bounds (range, &field_low, &field_high))
                break;
              target = TYPE_TARGET_TYPE (range);
            }
@@ -399,7 +399,7 @@ m2_language::value_print_inner (struct value *val, struct ui_file *stream,
 
          fputs_filtered ("{", stream);
 
-         i = get_discrete_bounds (range, &low_bound, &high_bound);
+         i = get_discrete_bounds (range, &low_bound, &high_bound) ? 0 : -1;
        maybe_bad_bstring:
          if (i < 0)
            {
index 428b2efc656629472d2ea96731b7e5fc2771939e..8f785b71ea43333308702fd45530736a1c3b6b91 100644 (file)
@@ -343,7 +343,8 @@ pascal_value_print_inner (struct value *val, struct ui_file *stream,
 
          fputs_filtered ("[", stream);
 
-         int bound_info = get_discrete_bounds (range, &low_bound, &high_bound);
+         int bound_info = (get_discrete_bounds (range, &low_bound, &high_bound)
+                           ? 0 : -1);
          if (low_bound == 0 && high_bound == -1 && TYPE_LENGTH (type) > 0)
            {
              /* If we know the size of the set type, we can figure out the
index 5b2bf1889ed9ef108113f7e0f0ec72cdf55af6de..077bcb41dd822928533c090f0b4981f1f647af35 100644 (file)
@@ -1949,7 +1949,7 @@ value_bit_index (struct type *type, const gdb_byte *valaddr, int index)
   unsigned rel_index;
   struct type *range = type->index_type ();
 
-  if (get_discrete_bounds (range, &low_bound, &high_bound) < 0)
+  if (!get_discrete_bounds (range, &low_bound, &high_bound))
     return -2;
   if (index < low_bound || index > high_bound)
     return -1;
index 4d0e002b20d618c0e146d54c70e570aa942c6447..09e97d2d3b9797f8b587aea7f12ac653d26f2028 100644 (file)
@@ -453,7 +453,7 @@ value_cast (struct type *type, struct value *arg2)
          int val_length = TYPE_LENGTH (type2);
          LONGEST low_bound, high_bound, new_length;
 
-         if (get_discrete_bounds (range_type, &low_bound, &high_bound) < 0)
+         if (!get_discrete_bounds (range_type, &low_bound, &high_bound))
            low_bound = 0, high_bound = 0;
          new_length = val_length / element_length;
          if (val_length % element_length != 0)
@@ -3948,7 +3948,7 @@ value_slice (struct value *array, int lowbound, int length)
     error (_("array not associated"));
 
   range_type = array_type->index_type ();
-  if (get_discrete_bounds (range_type, &lowerbound, &upperbound) < 0)
+  if (!get_discrete_bounds (range_type, &lowerbound, &upperbound))
     error (_("slice from bad array or bitstring"));
 
   if (lowbound < lowerbound || length < 0
This page took 0.04758 seconds and 4 git commands to generate.