Constify add_prefix_cmd
[deliverable/binutils-gdb.git] / gdb / value.c
index cd59f43f01f3eeb5c5fb8eb4455a6a3e921ed3ef..7d0966c8fe19dea8c99da8c2aaac2efa4f804c90 100644 (file)
@@ -1,6 +1,6 @@
 /* Low level packing and unpacking of values for GDB, the GNU Debugger.
 
-   Copyright (C) 1986-2016 Free Software Foundation, Inc.
+   Copyright (C) 1986-2017 Free Software Foundation, Inc.
 
    This file is part of GDB.
 
@@ -29,6 +29,7 @@
 #include "language.h"
 #include "demangle.h"
 #include "doublest.h"
+#include "floatformat.h"
 #include "regcache.h"
 #include "block.h"
 #include "dfp.h"
 #include "tracepoint.h"
 #include "cp-abi.h"
 #include "user-regs.h"
-
-/* Prototypes for exported functions.  */
-
-void _initialize_values (void);
+#include <algorithm>
+#include "completer.h"
 
 /* Definition of a user function.  */
 struct internal_function
@@ -84,8 +83,8 @@ ranges_overlap (LONGEST offset1, LONGEST len1,
 {
   ULONGEST h, l;
 
-  l = max (offset1, offset2);
-  h = min (offset1 + len1, offset2 + len2);
+  l = std::max (offset1, offset2);
+  h = std::min (offset1 + len1, offset2 + len2);
   return (l < h);
 }
 
@@ -204,17 +203,23 @@ struct value
   /* If the value has been released.  */
   unsigned int released : 1;
 
-  /* Register number if the value is from a register.  */
-  short regnum;
-
   /* Location of value (if lval).  */
   union
   {
-    /* If lval == lval_memory, this is the address in the inferior.
-       If lval == lval_register, this is the byte offset into the
-       registers structure.  */
+    /* If lval == lval_memory, this is the address in the inferior  */
     CORE_ADDR address;
 
+    /*If lval == lval_register, the value is from a register.  */
+    struct
+    {
+      /* Register number.  */
+      int regnum;
+      /* Frame ID of "next" frame to which a register value is relative.
+        If the register value is found relative to frame F, then the
+        frame id of F->next will be stored in next_frame_id.  */
+      struct frame_id next_frame_id;
+    } reg;
+
     /* Pointer to internal variable.  */
     struct internalvar *internalvar;
 
@@ -235,10 +240,8 @@ struct value
   } location;
 
   /* Describes offset of a value within lval of a structure in target
-     addressable memory units.  If lval == lval_memory, this is an offset to
-     the address.  If lval == lval_register, this is a further offset from
-     location.address within the registers structure.  Note also the member
-     embedded_offset below.  */
+     addressable memory units.  Note also the member embedded_offset
+     below.  */
   LONGEST offset;
 
   /* Only used for bitfields; number of bits contained in them.  */
@@ -261,10 +264,6 @@ struct value
      bitfields.  */
   struct value *parent;
 
-  /* Frame register value is relative to.  This will be described in
-     the lval enum above as "lval_register".  */
-  struct frame_id frame_id;
-
   /* Type of the value.  */
   struct type *type;
 
@@ -528,8 +527,8 @@ insert_into_bit_range_vector (VEC(range_s) **vectorp,
       if (ranges_overlap (bef->offset, bef->length, offset, length))
        {
          /* #1 */
-         ULONGEST l = min (bef->offset, offset);
-         ULONGEST h = max (bef->offset + bef->length, offset + length);
+         ULONGEST l = std::min (bef->offset, offset);
+         ULONGEST h = std::max (bef->offset + bef->length, offset + length);
 
          bef->offset = l;
          bef->length = h - l;
@@ -572,8 +571,8 @@ insert_into_bit_range_vector (VEC(range_s) **vectorp,
          {
            ULONGEST l, h;
 
-           l = min (t->offset, r->offset);
-           h = max (t->offset + t->length, r->offset + r->length);
+           l = std::min (t->offset, r->offset);
+           h = std::max (t->offset + t->length, r->offset + r->length);
 
            t->offset = l;
            t->length = h - l;
@@ -780,11 +779,11 @@ find_first_range_overlap_and_match (struct ranges_and_idx *rp1,
       /* Get the unavailable windows intersected by the incoming
         ranges.  The first and last ranges that overlap the argument
         range may be wider than said incoming arguments ranges.  */
-      l1 = max (offset1, r1->offset);
-      h1 = min (offset1 + length, r1->offset + r1->length);
+      l1 = std::max (offset1, r1->offset);
+      h1 = std::min (offset1 + length, r1->offset + r1->length);
 
-      l2 = max (offset2, r2->offset);
-      h2 = min (offset2 + length, offset2 + r2->length);
+      l2 = std::max (offset2, r2->offset);
+      h2 = std::min (offset2 + length, offset2 + r2->length);
 
       /* Make them relative to the respective start offsets, so we can
         compare them for equality.  */
@@ -887,8 +886,6 @@ value_contents_eq (const struct value *val1, LONGEST offset1,
 
 static void show_values (char *, int);
 
-static void show_convenience (char *, int);
-
 
 /* The value-history records all the values printed
    by print commands during this session.  Each chunk
@@ -942,11 +939,9 @@ allocate_value_lazy (struct type *type)
   val->enclosing_type = type;
   VALUE_LVAL (val) = not_lval;
   val->location.address = 0;
-  VALUE_FRAME_ID (val) = null_frame_id;
   val->offset = 0;
   val->bitpos = 0;
   val->bitsize = 0;
-  VALUE_REGNUM (val) = -1;
   val->lazy = 1;
   val->embedded_offset = 0;
   val->pointed_to_offset = 0;
@@ -1206,8 +1201,7 @@ value_actual_type (struct value *value, int resolve_simple_types,
     {
       /* If result's target type is TYPE_CODE_STRUCT, proceed to
         fetch its rtti type.  */
-      if ((TYPE_CODE (result) == TYPE_CODE_PTR
-          || TYPE_CODE (result) == TYPE_CODE_REF)
+      if ((TYPE_CODE (result) == TYPE_CODE_PTR || TYPE_IS_REFERENCE (result))
          && TYPE_CODE (check_typedef (TYPE_TARGET_TYPE (result)))
             == TYPE_CODE_STRUCT
          && !value_optimized_out (value))
@@ -1297,8 +1291,9 @@ ranges_copy_adjusted (VEC (range_s) **dst_range, int dst_bit_offset,
     {
       ULONGEST h, l;
 
-      l = max (r->offset, src_bit_offset);
-      h = min (r->offset + r->length, src_bit_offset + bit_length);
+      l = std::max (r->offset, (LONGEST) src_bit_offset);
+      h = std::min (r->offset + r->length,
+                   (LONGEST) src_bit_offset + bit_length);
 
       if (l < h)
        insert_into_bit_range_vector (dst_range,
@@ -1539,9 +1534,7 @@ value_lval_const (const struct value *value)
 CORE_ADDR
 value_address (const struct value *value)
 {
-  if (value->lval == lval_internalvar
-      || value->lval == lval_internalvar_component
-      || value->lval == lval_xcallable)
+  if (value->lval != lval_memory)
     return 0;
   if (value->parent != NULL)
     return value_address (value->parent) + value->offset;
@@ -1557,9 +1550,7 @@ value_address (const struct value *value)
 CORE_ADDR
 value_raw_address (const struct value *value)
 {
-  if (value->lval == lval_internalvar
-      || value->lval == lval_internalvar_component
-      || value->lval == lval_xcallable)
+  if (value->lval != lval_memory)
     return 0;
   return value->location.address;
 }
@@ -1567,9 +1558,7 @@ value_raw_address (const struct value *value)
 void
 set_value_address (struct value *value, CORE_ADDR addr)
 {
-  gdb_assert (value->lval != lval_internalvar
-             && value->lval != lval_internalvar_component
-             && value->lval != lval_xcallable);
+  gdb_assert (value->lval == lval_memory);
   value->location.address = addr;
 }
 
@@ -1580,15 +1569,17 @@ deprecated_value_internalvar_hack (struct value *value)
 }
 
 struct frame_id *
-deprecated_value_frame_id_hack (struct value *value)
+deprecated_value_next_frame_id_hack (struct value *value)
 {
-  return &value->frame_id;
+  gdb_assert (value->lval == lval_register);
+  return &value->location.reg.next_frame_id;
 }
 
-short *
+int *
 deprecated_value_regnum_hack (struct value *value)
 {
-  return &value->regnum;
+  gdb_assert (value->lval == lval_register);
+  return &value->location.reg.regnum;
 }
 
 int
@@ -1784,8 +1775,6 @@ value_copy (struct value *arg)
   val->offset = arg->offset;
   val->bitpos = arg->bitpos;
   val->bitsize = arg->bitsize;
-  VALUE_FRAME_ID (val) = VALUE_FRAME_ID (arg);
-  VALUE_REGNUM (val) = VALUE_REGNUM (arg);
   val->lazy = arg->lazy;
   val->embedded_offset = value_embedded_offset (arg);
   val->pointed_to_offset = arg->pointed_to_offset;
@@ -2111,9 +2100,7 @@ init_if_undefined_command (char* args, int from_tty)
   struct internalvar* intvar;
 
   /* Parse the expression - this is taken from set_command().  */
-  struct expression *expr = parse_expression (args);
-  register struct cleanup *old_chain =
-    make_cleanup (free_current_contents, &expr);
+  expression_up expr = parse_expression (args);
 
   /* Validate the expression.
      Was the expression an assignment?
@@ -2131,9 +2118,7 @@ init_if_undefined_command (char* args, int from_tty)
   /* Only evaluate the expression if the lvalue is void.
      This may still fail if the expresssion is invalid.  */
   if (intvar->kind == INTERNALVAR_VOID)
-    evaluate_expression (expr);
-
-  do_cleanups (old_chain);
+    evaluate_expression (expr.get ());
 }
 
 
@@ -2155,14 +2140,12 @@ lookup_only_internalvar (const char *name)
   return NULL;
 }
 
-/* Complete NAME by comparing it to the names of internal variables.
-   Returns a vector of newly allocated strings, or NULL if no matches
-   were found.  */
+/* Complete NAME by comparing it to the names of internal
+   variables.  */
 
-VEC (char_ptr) *
-complete_internalvar (const char *name)
+void
+complete_internalvar (completion_tracker &tracker, const char *name)
 {
-  VEC (char_ptr) *result = NULL;
   struct internalvar *var;
   int len;
 
@@ -2171,12 +2154,10 @@ complete_internalvar (const char *name)
   for (var = internalvars; var; var = var->next)
     if (strncmp (var->name, name, len) == 0)
       {
-       char *r = xstrdup (var->name);
+       gdb::unique_xmalloc_ptr<char> copy (xstrdup (var->name));
 
-       VEC_safe_push (char_ptr, result, r);
+       tracker.add_completion (std::move (copy));
       }
-
-  return result;
 }
 
 /* Create an internal variable with name NAME and with a void value.
@@ -2577,7 +2558,7 @@ call_internal_function (struct gdbarch *gdbarch,
    the implementation of the sub-command that is created when
    registering an internal function.  */
 static void
-function_command (char *command, int from_tty)
+function_command (const char *command, int from_tty)
 {
   /* Do nothing.  */
 }
@@ -2679,7 +2660,7 @@ preserve_values (struct objfile *objfile)
 }
 
 static void
-show_convenience (char *ignore, int from_tty)
+show_convenience (const char *ignore, int from_tty)
 {
   struct gdbarch *gdbarch = get_current_arch ();
   struct internalvar *var;
@@ -2893,7 +2874,7 @@ value_as_address (struct value *val)
      ABI-specific code is a more reasonable place to handle it.  */
 
   if (TYPE_CODE (value_type (val)) != TYPE_CODE_PTR
-      && TYPE_CODE (value_type (val)) != TYPE_CODE_REF
+      && !TYPE_IS_REFERENCE (value_type (val))
       && gdbarch_integer_to_address_p (gdbarch))
     return gdbarch_integer_to_address (gdbarch, value_type (val),
                                       value_contents (val));
@@ -2944,12 +2925,11 @@ unpack_long (struct type *type, const gdb_byte *valaddr)
       return (LONGEST) extract_typed_floating (valaddr, type);
 
     case TYPE_CODE_DECFLOAT:
-      /* libdecnumber has a function to convert from decimal to integer, but
-        it doesn't work when the decimal number has a fractional part.  */
-      return (LONGEST) decimal_to_doublest (valaddr, len, byte_order);
+      return decimal_to_longest (valaddr, len, byte_order);
 
     case TYPE_CODE_PTR:
     case TYPE_CODE_REF:
+    case TYPE_CODE_RVALUE_REF:
       /* Assume a CORE_ADDR can fit in a LONGEST (for now).  Not sure
          whether we want this to be true eventually.  */
       return extract_typed_address (valaddr, type);
@@ -3229,8 +3209,6 @@ value_primitive_field (struct value *arg1, LONGEST offset,
                   + value_embedded_offset (arg1));
     }
   set_value_component_location (v, arg1);
-  VALUE_REGNUM (v) = VALUE_REGNUM (arg1);
-  VALUE_FRAME_ID (v) = VALUE_FRAME_ID (arg1);
   return v;
 }
 
@@ -3276,6 +3254,7 @@ value_fn_field (struct value **arg1p, struct fn_field *f,
     }
 
   v = allocate_value (ftype);
+  VALUE_LVAL (v) = lval_memory;
   if (sym)
     {
       set_value_address (v, BLOCK_START (SYMBOL_BLOCK_VALUE (sym)));
@@ -3430,17 +3409,24 @@ unpack_value_bitfield (struct value *dest_val,
   enum bfd_endian byte_order;
   int src_bit_offset;
   int dst_bit_offset;
-  LONGEST num;
   struct type *field_type = value_type (dest_val);
 
-  /* First, unpack and sign extend the bitfield as if it was wholly
-     available.  Invalid/unavailable bits are read as zero, but that's
-     OK, as they'll end up marked below.  */
   byte_order = gdbarch_byte_order (get_type_arch (field_type));
-  num = unpack_bits_as_long (field_type, valaddr + embedded_offset,
-                            bitpos, bitsize);
-  store_signed_integer (value_contents_raw (dest_val),
-                       TYPE_LENGTH (field_type), byte_order, num);
+
+  /* First, unpack and sign extend the bitfield as if it was wholly
+     valid.  Optimized out/unavailable bits are read as zero, but
+     that's OK, as they'll end up marked below.  If the VAL is
+     wholly-invalid we may have skipped allocating its contents,
+     though.  See allocate_optimized_out_value.  */
+  if (valaddr != NULL)
+    {
+      LONGEST num;
+
+      num = unpack_bits_as_long (field_type, valaddr + embedded_offset,
+                                bitpos, bitsize);
+      store_signed_integer (value_contents_raw (dest_val),
+                           TYPE_LENGTH (field_type), byte_order, num);
+    }
 
   /* Now copy the optimized out / unavailability ranges to the right
      bits.  */
@@ -3550,6 +3536,7 @@ pack_long (gdb_byte *buf, struct type *type, LONGEST num)
       break;
 
     case TYPE_CODE_REF:
+    case TYPE_CODE_RVALUE_REF:
     case TYPE_CODE_PTR:
       store_typed_address (buf, type, (CORE_ADDR) num);
       break;
@@ -3586,6 +3573,7 @@ pack_unsigned_long (gdb_byte *buf, struct type *type, ULONGEST num)
       break;
 
     case TYPE_CODE_REF:
+    case TYPE_CODE_RVALUE_REF:
     case TYPE_CODE_PTR:
       store_typed_address (buf, type, (CORE_ADDR) num);
       break;
@@ -3655,8 +3643,8 @@ value_from_contents_and_address_unresolved (struct type *type,
     v = allocate_value_lazy (type);
   else
     v = value_from_contents (type, valaddr);
-  set_value_address (v, address);
   VALUE_LVAL (v) = lval_memory;
+  set_value_address (v, address);
   return v;
 }
 
@@ -3681,8 +3669,8 @@ value_from_contents_and_address (struct type *type,
   if (TYPE_DATA_LOCATION (resolved_type_no_typedef) != NULL
       && TYPE_DATA_LOCATION_KIND (resolved_type_no_typedef) == PROP_CONST)
     address = TYPE_DATA_LOCATION_ADDR (resolved_type_no_typedef);
-  set_value_address (v, address);
   VALUE_LVAL (v) = lval_memory;
+  set_value_address (v, address);
   return v;
 }
 
@@ -3788,12 +3776,35 @@ value_from_history_ref (const char *h, const char **endp)
   return access_value_history (index);
 }
 
+/* Get the component value (offset by OFFSET bytes) of a struct or
+   union WHOLE.  Component's type is TYPE.  */
+
+struct value *
+value_from_component (struct value *whole, struct type *type, LONGEST offset)
+{
+  struct value *v;
+
+  if (VALUE_LVAL (whole) == lval_memory && value_lazy (whole))
+    v = allocate_value_lazy (type);
+  else
+    {
+      v = allocate_value (type);
+      value_contents_copy (v, value_embedded_offset (v),
+                          whole, value_embedded_offset (whole) + offset,
+                          type_length_units (type));
+    }
+  v->offset = value_offset (whole) + offset + value_embedded_offset (whole);
+  set_value_component_location (v, whole);
+
+  return v;
+}
+
 struct value *
 coerce_ref_if_computed (const struct value *arg)
 {
   const struct lval_funcs *funcs;
 
-  if (TYPE_CODE (check_typedef (value_type (arg))) != TYPE_CODE_REF)
+  if (!TYPE_IS_REFERENCE (check_typedef (value_type (arg))))
     return NULL;
 
   if (value_lval_const (arg) != lval_computed)
@@ -3835,7 +3846,7 @@ coerce_ref (struct value *arg)
   if (retval)
     return retval;
 
-  if (TYPE_CODE (value_type_arg_tmp) != TYPE_CODE_REF)
+  if (!TYPE_IS_REFERENCE (value_type_arg_tmp))
     return arg;
 
   enc_type = check_typedef (value_enclosing_type (arg));
@@ -3969,7 +3980,7 @@ value_fetch_lazy (struct value *val)
     }
   else if (VALUE_LVAL (val) == lval_register)
     {
-      struct frame_info *frame;
+      struct frame_info *next_frame;
       int regnum;
       struct type *type = check_typedef (value_type (val));
       struct value *new_val = val, *mark = value_mark ();
@@ -3980,27 +3991,33 @@ value_fetch_lazy (struct value *val)
 
       while (VALUE_LVAL (new_val) == lval_register && value_lazy (new_val))
        {
-         struct frame_id frame_id = VALUE_FRAME_ID (new_val);
+         struct frame_id next_frame_id = VALUE_NEXT_FRAME_ID (new_val);
 
-         frame = frame_find_by_id (frame_id);
+         next_frame = frame_find_by_id (next_frame_id);
          regnum = VALUE_REGNUM (new_val);
 
-         gdb_assert (frame != NULL);
+         gdb_assert (next_frame != NULL);
 
          /* Convertible register routines are used for multi-register
             values and for interpretation in different types
             (e.g. float or int from a double register).  Lazy
             register values should have the register's natural type,
             so they do not apply.  */
-         gdb_assert (!gdbarch_convert_register_p (get_frame_arch (frame),
+         gdb_assert (!gdbarch_convert_register_p (get_frame_arch (next_frame),
                                                   regnum, type));
 
-         new_val = get_frame_register_value (frame, regnum);
+         /* FRAME was obtained, above, via VALUE_NEXT_FRAME_ID. 
+            Since a "->next" operation was performed when setting
+            this field, we do not need to perform a "next" operation
+            again when unwinding the register.  That's why
+            frame_unwind_register_value() is called here instead of
+            get_frame_register_value().  */
+         new_val = frame_unwind_register_value (next_frame, regnum);
 
          /* If we get another lazy lval_register value, it means the
-            register is found by reading it from the next frame.
-            get_frame_register_value should never return a value with
-            the frame id pointing to FRAME.  If it does, it means we
+            register is found by reading it from NEXT_FRAME's next frame.
+            frame_unwind_register_value should never return a value with
+            the frame id pointing to NEXT_FRAME.  If it does, it means we
             either have two consecutive frames with the same frame id
             in the frame chain, or some code is trying to unwind
             behind get_prev_frame's back (e.g., a frame unwind
@@ -4009,7 +4026,7 @@ value_fetch_lazy (struct value *val)
             in this situation.  */
          if (VALUE_LVAL (new_val) == lval_register
              && value_lazy (new_val)
-             && frame_id_eq (VALUE_FRAME_ID (new_val), frame_id))
+             && frame_id_eq (VALUE_NEXT_FRAME_ID (new_val), next_frame_id))
            internal_error (__FILE__, __LINE__,
                            _("infinite loop while fetching a register"));
        }
@@ -4029,6 +4046,9 @@ value_fetch_lazy (struct value *val)
       if (frame_debug)
        {
          struct gdbarch *gdbarch;
+         struct frame_info *frame;
+         /* VALUE_FRAME_ID is used here, instead of VALUE_NEXT_FRAME_ID,
+            so that the frame level will be shown correctly.  */
          frame = frame_find_by_id (VALUE_FRAME_ID (val));
          regnum = VALUE_REGNUM (val);
          gdbarch = get_frame_arch (frame);
This page took 0.032815 seconds and 4 git commands to generate.