Gate the displaying of non-debug sections in separate debuginfo files.
[deliverable/binutils-gdb.git] / gdb / eval.c
index 8d0c5747f3960d314ab6b3a84dc032be4c1c3723..7ba3ee59522fc4d627f85315eba77835547a6b5b 100644 (file)
@@ -1,6 +1,6 @@
 /* Evaluate expressions for GDB.
 
-   Copyright (C) 1986-2020 Free Software Foundation, Inc.
+   Copyright (C) 1986-2021 Free Software Foundation, Inc.
 
    This file is part of GDB.
 
@@ -57,30 +57,12 @@ static struct value *evaluate_struct_tuple (struct value *,
                                            struct expression *, int *,
                                            enum noside, int);
 
-static LONGEST init_array_element (struct value *, struct value *,
-                                  struct expression *, int *, enum noside,
-                                  LONGEST, LONGEST);
-
 struct value *
 evaluate_subexp (struct type *expect_type, struct expression *exp,
                 int *pos, enum noside noside)
 {
-  struct value *retval;
-
-  gdb::optional<enable_thread_stack_temporaries> stack_temporaries;
-  if (*pos == 0 && target_has_execution ()
-      && exp->language_defn->la_language == language_cplus
-      && !thread_stack_temporaries_enabled_p (inferior_thread ()))
-    stack_temporaries.emplace (inferior_thread ());
-
-  retval = (*exp->language_defn->expression_ops ()->evaluate_exp)
-    (expect_type, exp, pos, noside);
-
-  if (stack_temporaries.has_value ()
-      && value_in_thread_stack_temporaries (retval, inferior_thread ()))
-    retval = value_non_lval (retval);
-
-  return retval;
+  return ((*exp->language_defn->expression_ops ()->evaluate_exp)
+         (expect_type, exp, pos, noside));
 }
 \f
 /* Parse the string EXP as a C expression, evaluate it,
@@ -124,17 +106,34 @@ parse_to_comma_and_eval (const char **expp)
   return evaluate_expression (expr.get ());
 }
 \f
-/* Evaluate an expression in internal prefix form
-   such as is constructed by parse.y.
 
-   See expression.h for info on the format of an expression.  */
+/* See expression.h.  */
 
 struct value *
-evaluate_expression (struct expression *exp)
+expression::evaluate (struct type *expect_type, enum noside noside)
 {
-  int pc = 0;
+  gdb::optional<enable_thread_stack_temporaries> stack_temporaries;
+  if (target_has_execution ()
+      && language_defn->la_language == language_cplus
+      && !thread_stack_temporaries_enabled_p (inferior_thread ()))
+    stack_temporaries.emplace (inferior_thread ());
+
+  int pos = 0;
+  struct value *retval = evaluate_subexp (expect_type, this, &pos, noside);
+
+  if (stack_temporaries.has_value ()
+      && value_in_thread_stack_temporaries (retval, inferior_thread ()))
+    retval = value_non_lval (retval);
 
-  return evaluate_subexp (nullptr, exp, &pc, EVAL_NORMAL);
+  return retval;
+}
+
+/* See value.h.  */
+
+struct value *
+evaluate_expression (struct expression *exp, struct type *expect_type)
+{
+  return exp->evaluate (expect_type, EVAL_NORMAL);
 }
 
 /* Evaluate an expression, avoiding all memory references
@@ -143,9 +142,7 @@ evaluate_expression (struct expression *exp)
 struct value *
 evaluate_type (struct expression *exp)
 {
-  int pc = 0;
-
-  return evaluate_subexp (nullptr, exp, &pc, EVAL_AVOID_SIDE_EFFECTS);
+  return exp->evaluate (nullptr, EVAL_AVOID_SIDE_EFFECTS);
 }
 
 /* Evaluate a subexpression, avoiding all memory references and
@@ -183,7 +180,7 @@ void
 fetch_subexp_value (struct expression *exp, int *pc, struct value **valp,
                    struct value **resultp,
                    std::vector<value_ref_ptr> *val_chain,
-                   int preserve_errors)
+                   bool preserve_errors)
 {
   struct value *mark, *new_mark, *result;
 
@@ -337,39 +334,6 @@ evaluate_struct_tuple (struct value *struct_val,
   return struct_val;
 }
 
-/* Recursive helper function for setting elements of array tuples.
-   The target is ARRAY (which has bounds LOW_BOUND to HIGH_BOUND); the
-   element value is ELEMENT; EXP, POS and NOSIDE are as usual.
-   Evaluates index expressions and sets the specified element(s) of
-   ARRAY to ELEMENT.  Returns last index value.  */
-
-static LONGEST
-init_array_element (struct value *array, struct value *element,
-                   struct expression *exp, int *pos,
-                   enum noside noside, LONGEST low_bound, LONGEST high_bound)
-{
-  LONGEST index;
-  int element_size = TYPE_LENGTH (value_type (element));
-
-  if (exp->elts[*pos].opcode == BINOP_COMMA)
-    {
-      (*pos)++;
-      init_array_element (array, element, exp, pos, noside,
-                         low_bound, high_bound);
-      return init_array_element (array, element,
-                                exp, pos, noside, low_bound, high_bound);
-    }
-  else
-    {
-      index = value_as_long (evaluate_subexp (nullptr, exp, pos, noside));
-      if (index < low_bound || index > high_bound)
-       error (_("tuple index out of range"));
-      memcpy (value_contents_raw (array) + (index - low_bound) * element_size,
-             value_contents (element), element_size);
-    }
-  return index;
-}
-
 /* Promote value ARG1 as appropriate before performing a unary operation
    on this argument.
    If the result is not appropriate for any particular language then it
@@ -430,6 +394,9 @@ binop_promote (const struct language_defn *language, struct gdbarch *gdbarch,
          && !is_integral_type (type2)))
     return;
 
+  if (is_fixed_point_type (type1) || is_fixed_point_type (type2))
+        return;
+
   if (type1->code () == TYPE_CODE_DECFLOAT
       || type2->code () == TYPE_CODE_DECFLOAT)
     {
@@ -726,11 +693,12 @@ eval_skip_value (expression *exp)
 
 value *
 evaluate_subexp_do_call (expression *exp, enum noside noside,
-                        int nargs, value **argvec,
+                        value *callee,
+                        gdb::array_view<value *> argvec,
                         const char *function_name,
                         type *default_return_type)
 {
-  if (argvec[0] == NULL)
+  if (callee == NULL)
     error (_("Cannot evaluate function -- may be inlined"));
   if (noside == EVAL_AVOID_SIDE_EFFECTS)
     {
@@ -738,7 +706,7 @@ evaluate_subexp_do_call (expression *exp, enum noside noside,
         call an error.  This can happen if somebody tries to turn
         a variable into a function call.  */
 
-      type *ftype = value_type (argvec[0]);
+      type *ftype = value_type (callee);
 
       if (ftype->code () == TYPE_CODE_INTERNAL_FUNCTION)
        {
@@ -750,10 +718,7 @@ evaluate_subexp_do_call (expression *exp, enum noside noside,
        }
       else if (ftype->code () == TYPE_CODE_XMETHOD)
        {
-         type *return_type
-           = result_type_of_xmethod (argvec[0],
-                                     gdb::make_array_view (argvec + 1,
-                                                           nargs));
+         type *return_type = result_type_of_xmethod (callee, argvec);
 
          if (return_type == NULL)
            error (_("Xmethod is missing return type."));
@@ -764,7 +729,7 @@ evaluate_subexp_do_call (expression *exp, enum noside noside,
        {
          if (ftype->is_gnu_ifunc ())
            {
-             CORE_ADDR address = value_address (argvec[0]);
+             CORE_ADDR address = value_address (callee);
              type *resolved_type = find_gnu_ifunc_target_type (address);
 
              if (resolved_type != NULL)
@@ -785,16 +750,15 @@ evaluate_subexp_do_call (expression *exp, enum noside noside,
        error (_("Expression of type other than "
                 "\"Function returning ...\" used as function"));
     }
-  switch (value_type (argvec[0])->code ())
+  switch (value_type (callee)->code ())
     {
     case TYPE_CODE_INTERNAL_FUNCTION:
       return call_internal_function (exp->gdbarch, exp->language_defn,
-                                    argvec[0], nargs, argvec + 1);
+                                    callee, argvec.size (), argvec.data ());
     case TYPE_CODE_XMETHOD:
-      return call_xmethod (argvec[0], gdb::make_array_view (argvec + 1, nargs));
+      return call_xmethod (callee, argvec);
     default:
-      return call_function_by_hand (argvec[0], default_return_type,
-                                   gdb::make_array_view (argvec + 1, nargs));
+      return call_function_by_hand (callee, default_return_type, argvec);
     }
 }
 
@@ -1199,7 +1163,8 @@ evaluate_funcall (type *expect_type, expression *exp, int *pos,
       /* Nothing to be done; argvec already correctly set up.  */
     }
 
-  return evaluate_subexp_do_call (exp, noside, nargs, argvec,
+  return evaluate_subexp_do_call (exp, noside, argvec[0],
+                                 gdb::make_array_view (argvec + 1, nargs),
                                  var_func_name, expect_type);
 }
 
@@ -1420,7 +1385,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;
@@ -1430,30 +1395,17 @@ evaluate_subexp_standard (struct type *expect_type,
          for (tem = nargs; --nargs >= 0;)
            {
              struct value *element;
-             int index_pc = 0;
 
              element = evaluate_subexp (element_type, exp, pos, noside);
              if (value_type (element) != element_type)
                element = value_cast (element_type, element);
-             if (index_pc)
-               {
-                 int continue_pc = *pos;
-
-                 *pos = index_pc;
-                 index = init_array_element (array, element, exp, pos, noside,
-                                             low_bound, high_bound);
-                 *pos = continue_pc;
-               }
-             else
-               {
-                 if (index > high_bound)
-                   /* To avoid memory corruption.  */
-                   error (_("Too many array elements"));
-                 memcpy (value_contents_raw (array)
-                         + (index - low_bound) * element_size,
-                         value_contents (element),
-                         element_size);
-               }
+             if (index > high_bound)
+               /* To avoid memory corruption.  */
+               error (_("Too many array elements"));
+             memcpy (value_contents_raw (array)
+                     + (index - low_bound) * element_size,
+                     value_contents (element),
+                     element_size);
              index++;
            }
          return array;
@@ -1473,7 +1425,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++)
@@ -2139,12 +2091,11 @@ evaluate_subexp_standard (struct type *expect_type,
                  || op == BINOP_MOD)
              && value_logical_not (arg2))
            {
-             struct value *v_one, *retval;
+             struct value *v_one;
 
              v_one = value_one (value_type (arg2));
              binop_promote (exp->language_defn, exp->gdbarch, &arg1, &v_one);
-             retval = value_binop (arg1, v_one, op);
-             return retval;
+             return value_binop (arg1, v_one, op);
            }
          else
            {
@@ -2194,36 +2145,14 @@ evaluate_subexp_standard (struct type *expect_type,
       (*pos) += 2;
       nargs = longest_to_int (exp->elts[pc + 1].longconst);
       arg1 = evaluate_subexp_with_coercion (exp, pos, noside);
-      while (nargs-- > 0)
+      argvec = XALLOCAVEC (struct value *, nargs);
+      for (ix = 0; ix < nargs; ++ix)
+       argvec[ix] = evaluate_subexp_with_coercion (exp, pos, noside);
+      if (noside == EVAL_SKIP)
+       return arg1;
+      for (ix = 0; ix < nargs; ++ix)
        {
-         arg2 = evaluate_subexp_with_coercion (exp, pos, noside);
-         /* FIXME:  EVAL_SKIP handling may not be correct.  */
-         if (noside == EVAL_SKIP)
-           {
-             if (nargs > 0)
-               continue;
-             return eval_skip_value (exp);
-           }
-         /* FIXME:  EVAL_AVOID_SIDE_EFFECTS handling may not be correct.  */
-         if (noside == EVAL_AVOID_SIDE_EFFECTS)
-           {
-             /* If the user attempts to subscript something that has no target
-                type (like a plain int variable for example), then report this
-                as an error.  */
-
-             type = TYPE_TARGET_TYPE (check_typedef (value_type (arg1)));
-             if (type != NULL)
-               {
-                 arg1 = value_zero (type, VALUE_LVAL (arg1));
-                 noside = EVAL_SKIP;
-                 continue;
-               }
-             else
-               {
-                 error (_("cannot subscript something of type `%s'"),
-                        value_type (arg1)->name ());
-               }
-           }
+         arg2 = argvec[ix];
 
          if (binop_user_defined_p (op, arg1, arg2))
            {
@@ -2496,19 +2425,29 @@ evaluate_subexp_standard (struct type *expect_type,
       else if (noside == EVAL_AVOID_SIDE_EFFECTS)
        {
          type = check_typedef (value_type (arg1));
-         if (type->code () == TYPE_CODE_PTR
-             || TYPE_IS_REFERENCE (type)
-         /* In C you can dereference an array to get the 1st elt.  */
-             || type->code () == TYPE_CODE_ARRAY
-           )
-           return value_zero (TYPE_TARGET_TYPE (type),
-                              lval_memory);
-         else if (type->code () == TYPE_CODE_INT)
-           /* GDB allows dereferencing an int.  */
-           return value_zero (builtin_type (exp->gdbarch)->builtin_int,
-                              lval_memory);
-         else
-           error (_("Attempt to take contents of a non-pointer value."));
+
+         /* If the type pointed to is dynamic then in order to resolve the
+            dynamic properties we must actually dereference the pointer.
+            There is a risk that this dereference will have side-effects
+            in the inferior, but being able to print accurate type
+            information seems worth the risk. */
+         if ((type->code () != TYPE_CODE_PTR
+              && !TYPE_IS_REFERENCE (type))
+             || !is_dynamic_type (TYPE_TARGET_TYPE (type)))
+           {
+             if (type->code () == TYPE_CODE_PTR
+                 || TYPE_IS_REFERENCE (type)
+                 /* In C you can dereference an array to get the 1st elt.  */
+                 || type->code () == TYPE_CODE_ARRAY)
+               return value_zero (TYPE_TARGET_TYPE (type),
+                                  lval_memory);
+             else if (type->code () == TYPE_CODE_INT)
+               /* GDB allows dereferencing an int.  */
+               return value_zero (builtin_type (exp->gdbarch)->builtin_int,
+                                  lval_memory);
+             else
+               error (_("Attempt to take contents of a non-pointer value."));
+           }
        }
 
       /* Allow * on an integer so we can cast it to whatever we want.
@@ -2529,12 +2468,7 @@ evaluate_subexp_standard (struct type *expect_type,
          return eval_skip_value (exp);
        }
       else
-       {
-         struct value *retvalp = evaluate_subexp_for_address (exp, pos,
-                                                              noside);
-
-         return retvalp;
-       }
+       return evaluate_subexp_for_address (exp, pos, noside);
 
     case UNOP_SIZEOF:
       if (noside == EVAL_SKIP)
@@ -3018,10 +2952,14 @@ evaluate_subexp_for_sizeof (struct expression *exp, int *pos,
        {
          val = evaluate_subexp (nullptr, exp, pos, EVAL_NORMAL);
          type = value_type (val);
-         if (type->code () == TYPE_CODE_ARRAY
-             && is_dynamic_type (type->index_type ())
-             && type->bounds ()->high.kind () == PROP_UNDEFINED)
-           return allocate_optimized_out_value (size_type);
+         if (type->code () == TYPE_CODE_ARRAY)
+           {
+             if (type_not_allocated (type) || type_not_associated (type))
+               return value_zero (size_type, not_lval);
+             else if (is_dynamic_type (type->index_type ())
+                      && type->bounds ()->high.kind () == PROP_UNDEFINED)
+               return allocate_optimized_out_value (size_type);
+           }
        }
       else
        (*pos) += 4;
@@ -3149,7 +3087,7 @@ evaluate_subexp_for_cast (expression *exp, int *pos,
 /* Parse a type expression in the string [P..P+LENGTH).  */
 
 struct type *
-parse_and_eval_type (char *p, int length)
+parse_and_eval_type (const char *p, int length)
 {
   char *tmp = (char *) alloca (length + 4);
 
@@ -3159,7 +3097,7 @@ parse_and_eval_type (char *p, int length)
   tmp[length + 2] = '0';
   tmp[length + 3] = '\0';
   expression_up expr = parse_expression (tmp);
-  if (expr->elts[0].opcode != UNOP_CAST)
+  if (expr->first_opcode () != UNOP_CAST)
     error (_("Internal error in eval_type."));
   return expr->elts[1].type;
 }
This page took 0.043924 seconds and 4 git commands to generate.