Implement Ada operator overloading
[deliverable/binutils-gdb.git] / gdb / valops.c
index 4df2538f16d836420f5fd6da5cc66c60f1ad2c51..fec821ad932c4d98d2a0e716a79439b32e48fbba 100644 (file)
@@ -1,6 +1,6 @@
 /* Perform non-arithmetic operations on values, for GDB.
 
-   Copyright (C) 1986-2020 Free Software Foundation, Inc.
+   Copyright (C) 1986-2021 Free Software Foundation, Inc.
 
    This file is part of GDB.
 
@@ -331,6 +331,75 @@ value_cast_pointers (struct type *type, struct value *arg2,
   return arg2;
 }
 
+/* See value.h.  */
+
+gdb_mpq
+value_to_gdb_mpq (struct value *value)
+{
+  struct type *type = check_typedef (value_type (value));
+
+  gdb_mpq result;
+  if (is_floating_type (type))
+    {
+      double d = target_float_to_host_double (value_contents (value),
+                                             type);
+      mpq_set_d (result.val, d);
+    }
+  else
+    {
+      gdb_assert (is_integral_type (type)
+                 || is_fixed_point_type (type));
+
+      gdb_mpz vz;
+      vz.read (gdb::make_array_view (value_contents (value),
+                                    TYPE_LENGTH (type)),
+              type_byte_order (type), type->is_unsigned ());
+      mpq_set_z (result.val, vz.val);
+
+      if (is_fixed_point_type (type))
+       mpq_mul (result.val, result.val,
+                type->fixed_point_scaling_factor ().val);
+    }
+
+  return result;
+}
+
+/* Assuming that TO_TYPE is a fixed point type, return a value
+   corresponding to the cast of FROM_VAL to that type.  */
+
+static struct value *
+value_cast_to_fixed_point (struct type *to_type, struct value *from_val)
+{
+  struct type *from_type = value_type (from_val);
+
+  if (from_type == to_type)
+    return from_val;
+
+  if (!is_floating_type (from_type)
+      && !is_integral_type (from_type)
+      && !is_fixed_point_type (from_type))
+    error (_("Invalid conversion from type %s to fixed point type %s"),
+          from_type->name (), to_type->name ());
+
+  gdb_mpq vq = value_to_gdb_mpq (from_val);
+
+  /* Divide that value by the scaling factor to obtain the unscaled
+     value, first in rational form, and then in integer form.  */
+
+  mpq_div (vq.val, vq.val, to_type->fixed_point_scaling_factor ().val);
+  gdb_mpz unscaled = vq.get_rounded ();
+
+  /* Finally, create the result value, and pack the unscaled value
+     in it.  */
+  struct value *result = allocate_value (to_type);
+  unscaled.write (gdb::make_array_view (value_contents_raw (result),
+                                       TYPE_LENGTH (to_type)),
+                 type_byte_order (to_type),
+                 to_type->is_unsigned ());
+
+  return result;
+}
+
 /* Cast value ARG2 to type TYPE and return as a value.
    More general than a C cast: accepts any two types of the same length,
    and if ARG2 is an lvalue it can be cast into anything at all.  */
@@ -349,6 +418,9 @@ value_cast (struct type *type, struct value *arg2)
   if (value_type (arg2) == type)
     return arg2;
 
+  if (is_fixed_point_type (type))
+    return value_cast_to_fixed_point (type, arg2);
+
   /* Check if we are casting struct reference to struct reference.  */
   if (TYPE_IS_REFERENCE (check_typedef (type)))
     {
@@ -394,7 +466,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)
@@ -439,7 +511,8 @@ value_cast (struct type *type, struct value *arg2)
 
   scalar = (code2 == TYPE_CODE_INT || code2 == TYPE_CODE_FLT
            || code2 == TYPE_CODE_DECFLOAT || code2 == TYPE_CODE_ENUM
-           || code2 == TYPE_CODE_RANGE);
+           || code2 == TYPE_CODE_RANGE
+           || is_fixed_point_type (type2));
 
   if ((code1 == TYPE_CODE_STRUCT || code1 == TYPE_CODE_UNION)
       && (code2 == TYPE_CODE_STRUCT || code2 == TYPE_CODE_UNION)
@@ -460,6 +533,20 @@ value_cast (struct type *type, struct value *arg2)
                                value_contents_raw (v), type);
          return v;
        }
+      else if (is_fixed_point_type (type2))
+       {
+         gdb_mpq fp_val;
+
+         fp_val.read_fixed_point
+           (gdb::make_array_view (value_contents (arg2), TYPE_LENGTH (type2)),
+            type_byte_order (type2), type2->is_unsigned (),
+            type2->fixed_point_scaling_factor ());
+
+         struct value *v = allocate_value (to_type);
+         target_float_from_host_double (value_contents_raw (v),
+                                        to_type, mpq_get_d (fp_val.val));
+         return v;
+       }
 
       /* The only option left is an integral type.  */
       if (type2->is_unsigned ())
@@ -503,7 +590,7 @@ value_cast (struct type *type, struct value *arg2)
         otherwise occur when dealing with a target having two byte
         pointers and four byte addresses.  */
 
-      int addr_bit = gdbarch_addr_bit (get_type_arch (type2));
+      int addr_bit = gdbarch_addr_bit (type2->arch ());
       LONGEST longest = value_as_long (arg2);
 
       if (addr_bit < sizeof (LONGEST) * HOST_CHAR_BIT)
@@ -1013,7 +1100,7 @@ value_assign (struct value *toval, struct value *fromval)
     {
     case lval_internalvar:
       set_internalvar (VALUE_INTERNALVAR (toval), fromval);
-      return value_of_internalvar (get_type_arch (type),
+      return value_of_internalvar (type->arch (),
                                   VALUE_INTERNALVAR (toval));
 
     case lval_internalvar_component:
@@ -1113,7 +1200,7 @@ value_assign (struct value *toval, struct value *fromval)
          {
            struct value *parent = value_parent (toval);
            LONGEST offset = value_offset (parent) + value_offset (toval);
-           int changed_len;
+           size_t changed_len;
            gdb_byte buffer[sizeof (LONGEST)];
            int optim, unavail;
 
@@ -1122,13 +1209,13 @@ value_assign (struct value *toval, struct value *fromval)
                           + HOST_CHAR_BIT - 1)
                          / HOST_CHAR_BIT;
 
-           if (changed_len > (int) sizeof (LONGEST))
+           if (changed_len > sizeof (LONGEST))
              error (_("Can't handle bitfields which "
                       "don't fit in a %d bit word."),
                     (int) sizeof (LONGEST) * HOST_CHAR_BIT);
 
            if (!get_frame_register_bytes (frame, value_reg, offset,
-                                          changed_len, buffer,
+                                          {buffer, changed_len},
                                           &optim, &unavail))
              {
                if (optim)
@@ -1143,7 +1230,7 @@ value_assign (struct value *toval, struct value *fromval)
                          value_bitpos (toval), value_bitsize (toval));
 
            put_frame_register_bytes (frame, value_reg, offset,
-                                     changed_len, buffer);
+                                     {buffer, changed_len});
          }
        else
          {
@@ -1159,10 +1246,12 @@ value_assign (struct value *toval, struct value *fromval)
              }
            else
              {
+               gdb::array_view<const gdb_byte> contents
+                 = gdb::make_array_view (value_contents (fromval),
+                                         TYPE_LENGTH (type));
                put_frame_register_bytes (frame, value_reg,
                                          value_offset (toval),
-                                         TYPE_LENGTH (type),
-                                         value_contents (fromval));
+                                         contents);
              }
          }
 
@@ -3874,7 +3963,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.026264 seconds and 4 git commands to generate.