gdb: make discrete_position return optional
authorSimon Marchi <simon.marchi@efficios.com>
Wed, 9 Dec 2020 18:51:45 +0000 (13:51 -0500)
committerSimon Marchi <simon.marchi@polymtl.ca>
Wed, 9 Dec 2020 18:51:45 +0000 (13:51 -0500)
Instead of returning a boolean status and returning the value through a
pointer, return an optional that does both jobs.  This helps in the
following patches, and I think it is an improvement in general.

gdb/ChangeLog:

* ada-lang.c (ada_value_slice_from_ptr): Adjust.
(ada_value_slice): Adjust.
(pos_atr): Adjust.
* gdbtypes.c (get_discrete_bounds): Adjust.
(discrete_position): Return optional.
* gdbtypes.h (discrete_position): Return optional.

Change-Id: I758dbd8858b296ee472ed39ec35db1dbd624a5ae

gdb/ChangeLog
gdb/ada-lang.c
gdb/gdbtypes.c
gdb/gdbtypes.h

index 0634f780ba1cfdbbac40d2823a24cae50ff134c0..468bbca3d02559fdb320e2511c1b66bd0d000ecf 100644 (file)
@@ -1,3 +1,12 @@
+2020-12-09  Simon Marchi  <simon.marchi@efficios.com>
+
+       * ada-lang.c (ada_value_slice_from_ptr): Adjust.
+       (ada_value_slice): Adjust.
+       (pos_atr): Adjust.
+       * gdbtypes.c (get_discrete_bounds): Adjust.
+       (discrete_position): Return optional.
+       * gdbtypes.h (discrete_position): Return optional.
+
 2020-12-07  Tom Tromey  <tromey@adacore.com>
 
        * maint.c (_initialize_maint_cmds): Use expression command
index 8a1d9df5411fe5be54876f7016aabd108472ebc6..5813ecded4472610c8b07e8a9e11bab1c18cb5f8 100644 (file)
@@ -2815,11 +2815,13 @@ ada_value_slice_from_ptr (struct value *array_ptr, struct type *type,
                               type0->dyn_prop (DYN_PROP_BYTE_STRIDE),
                               TYPE_FIELD_BITSIZE (type0, 0));
   int base_low =  ada_discrete_type_low_bound (type0->index_type ());
-  LONGEST base_low_pos, low_pos;
+  gdb::optional<LONGEST> base_low_pos, low_pos;
   CORE_ADDR base;
 
-  if (!discrete_position (base_index_type, low, &low_pos)
-      || !discrete_position (base_index_type, base_low, &base_low_pos))
+  low_pos = discrete_position (base_index_type, low);
+  base_low_pos = discrete_position (base_index_type, base_low);
+
+  if (!low_pos.has_value () || !base_low_pos.has_value ())
     {
       warning (_("unable to get positions in slice, use bounds instead"));
       low_pos = low;
@@ -2830,7 +2832,7 @@ ada_value_slice_from_ptr (struct value *array_ptr, struct type *type,
   if (stride == 0)
     stride = TYPE_LENGTH (TYPE_TARGET_TYPE (type0));
 
-  base = value_as_address (array_ptr) + (low_pos - base_low_pos) * stride;
+  base = value_as_address (array_ptr) + (*low_pos - *base_low_pos) * stride;
   return value_at_lazy (slice_type, base);
 }
 
@@ -2846,10 +2848,13 @@ ada_value_slice (struct value *array, int low, int high)
                              (NULL, TYPE_TARGET_TYPE (type), index_type,
                               type->dyn_prop (DYN_PROP_BYTE_STRIDE),
                               TYPE_FIELD_BITSIZE (type, 0));
-  LONGEST low_pos, high_pos;
+  gdb::optional<LONGEST> low_pos, high_pos;
+
 
-  if (!discrete_position (base_index_type, low, &low_pos)
-      || !discrete_position (base_index_type, high, &high_pos))
+  low_pos = discrete_position (base_index_type, low);
+  high_pos = discrete_position (base_index_type, high);
+
+  if (!low_pos.has_value () || !high_pos.has_value ())
     {
       warning (_("unable to get positions in slice, use bounds instead"));
       low_pos = low;
@@ -2857,7 +2862,7 @@ ada_value_slice (struct value *array, int low, int high)
     }
 
   return value_cast (slice_type,
-                    value_slice (array, low, high_pos - low_pos + 1));
+                    value_slice (array, low, *high_pos - *low_pos + 1));
 }
 
 /* If type is a record type in the form of a standard GNAT array
@@ -8926,15 +8931,15 @@ pos_atr (struct value *arg)
 {
   struct value *val = coerce_ref (arg);
   struct type *type = value_type (val);
-  LONGEST result;
 
   if (!discrete_type_p (type))
     error (_("'POS only defined on discrete types"));
 
-  if (!discrete_position (type, value_as_long (val), &result))
+  gdb::optional<LONGEST> result = discrete_position (type, value_as_long (val));
+  if (!result.has_value ())
     error (_("enumeration value is invalid: can't find 'POS"));
 
-  return result;
+  return *result;
 }
 
 static struct value *
index 4eaefa5ee652f7bc75b007a321fde71f595abda7..0c91edd48a54a14c391f8dccd3b357b472601c9c 100644 (file)
@@ -1062,9 +1062,21 @@ get_discrete_bounds (struct type *type, LONGEST *lowp, LONGEST *highp)
 
       if (TYPE_TARGET_TYPE (type)->code () == TYPE_CODE_ENUM)
        {
-         if (!discrete_position (TYPE_TARGET_TYPE (type), *lowp, lowp)
-             || ! discrete_position (TYPE_TARGET_TYPE (type), *highp, highp))
+         gdb::optional<LONGEST> low_pos
+           = discrete_position (TYPE_TARGET_TYPE (type), *lowp);
+
+         if (!low_pos.has_value ())
+           return 0;
+
+         *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;
        }
       return 1;
     case TYPE_CODE_ENUM:
@@ -1160,8 +1172,8 @@ get_array_bounds (struct type *type, LONGEST *low_bound, LONGEST *high_bound)
    in which case the value of POS is unmodified.
 */
 
-int
-discrete_position (struct type *type, LONGEST val, LONGEST *pos)
+gdb::optional<LONGEST>
+discrete_position (struct type *type, LONGEST val)
 {
   if (type->code () == TYPE_CODE_RANGE)
     type = TYPE_TARGET_TYPE (type);
@@ -1173,19 +1185,14 @@ discrete_position (struct type *type, LONGEST val, LONGEST *pos)
       for (i = 0; i < type->num_fields (); i += 1)
        {
          if (val == TYPE_FIELD_ENUMVAL (type, i))
-           {
-             *pos = i;
-             return 1;
-           }
+           return i;
        }
+
       /* Invalid enumeration value.  */
-      return 0;
+      return {};
     }
   else
-    {
-      *pos = val;
-      return 1;
-    }
+    return val;
 }
 
 /* If the array TYPE has static bounds calculate and update its
index eecd874cfae1a668e8743f13fbddaa94a3651573..e30485652702e3847ea73f96281a1b1411447915 100644 (file)
@@ -46,6 +46,7 @@
 
 #include "hashtab.h"
 #include "gdbsupport/array-view.h"
+#include "gdbsupport/gdb_optional.h"
 #include "gdbsupport/offset-type.h"
 #include "gdbsupport/enum-flags.h"
 #include "gdbsupport/underlying.h"
@@ -2477,7 +2478,8 @@ extern int get_discrete_bounds (struct type *, LONGEST *, LONGEST *);
 extern bool get_array_bounds (struct type *type, LONGEST *low_bound,
                              LONGEST *high_bound);
 
-extern int discrete_position (struct type *type, LONGEST val, LONGEST *pos);
+extern gdb::optional<LONGEST> discrete_position (struct type *type,
+                                                LONGEST val);
 
 extern int class_types_same_p (const struct type *, const struct type *);
 
This page took 0.040308 seconds and 4 git commands to generate.