/* Evaluate expressions for GDB.
- Copyright (C) 1986-2013 Free Software Foundation, Inc.
+ Copyright (C) 1986-2014 Free Software Foundation, Inc.
This file is part of GDB.
along with this program. If not, see <http://www.gnu.org/licenses/>. */
#include "defs.h"
-#include "gdb_string.h"
+#include <string.h>
#include "symtab.h"
#include "gdbtypes.h"
#include "value.h"
#include "valprint.h"
#include "gdb_obstack.h"
#include "objfiles.h"
-#include "python/python.h"
#include "gdb_assert.h"
/* Prototypes for local functions. */
-static struct value *evaluate_subexp_for_sizeof (struct expression *, int *);
+static struct value *evaluate_subexp_for_sizeof (struct expression *, int *,
+ enum noside);
static struct value *evaluate_subexp_for_address (struct expression *,
int *, enum noside);
/* Like parse_and_eval_address, but treats the value of the expression
as an integer, not an address, returns a LONGEST, not a CORE_ADDR. */
LONGEST
-parse_and_eval_long (char *exp)
+parse_and_eval_long (const char *exp)
{
struct expression *expr = parse_expression (exp);
LONGEST retval;
in *VAL_CHAIN. RESULTP and VAL_CHAIN may be NULL if the caller does
not need them.
- If a memory error occurs while evaluating the expression, *RESULTP will
- be set to NULL. *RESULTP may be a lazy value, if the result could
- not be read from memory. It is used to determine whether a value
- is user-specified (we should watch the whole value) or intermediate
+ If PRESERVE_ERRORS is true, then exceptions are passed through.
+ Otherwise, if PRESERVE_ERRORS is false, then if a memory error
+ occurs while evaluating the expression, *RESULTP will be set to
+ NULL. *RESULTP may be a lazy value, if the result could not be
+ read from memory. It is used to determine whether a value is
+ user-specified (we should watch the whole value) or intermediate
(we should watch only the bit used to locate the final value).
If the final value, or any intermediate value, could not be read
void
fetch_subexp_value (struct expression *exp, int *pc, struct value **valp,
- struct value **resultp, struct value **val_chain)
+ struct value **resultp, struct value **val_chain,
+ int preserve_errors)
{
struct value *mark, *new_mark, *result;
volatile struct gdb_exception ex;
}
if (ex.reason < 0)
{
- /* Ignore memory errors, we want watchpoints pointing at
+ /* Ignore memory errors if we want watchpoints pointing at
inaccessible memory to still be created; otherwise, throw the
error to some higher catcher. */
switch (ex.error)
{
case MEMORY_ERROR:
- break;
+ if (!preserve_errors)
+ break;
default:
throw_exception (ex);
break;
static struct type *
make_params (int num_types, struct type **param_types)
{
- struct type *type = XZALLOC (struct type);
- TYPE_MAIN_TYPE (type) = XZALLOC (struct main_type);
+ struct type *type = XCNEW (struct type);
+ TYPE_MAIN_TYPE (type) = XCNEW (struct main_type);
TYPE_LENGTH (type) = 1;
TYPE_CODE (type) = TYPE_CODE_METHOD;
TYPE_VPTR_FIELDNO (type) = -1;
if (noside == EVAL_AVOID_SIDE_EFFECTS)
return value_zero (SYMBOL_TYPE (sym), not_lval);
- if (SYMBOL_CLASS (sym) != LOC_COMPUTED
+ if (SYMBOL_COMPUTED_OPS (sym) == NULL
|| SYMBOL_COMPUTED_OPS (sym)->read_variable_at_entry == NULL)
error (_("Symbol \"%s\" does not have any specific entry value"),
SYMBOL_PRINT_NAME (sym));
if (value_as_long (target) == 0)
return value_from_longest (long_type, 0);
- if (lookup_minimal_symbol ("objc_msg_lookup", 0, 0))
+ if (lookup_minimal_symbol ("objc_msg_lookup", 0, 0).minsym)
gnu_runtime = 1;
/* Find the method dispatch (Apple runtime) or method lookup
op = exp->elts[*pos].opcode;
nargs = longest_to_int (exp->elts[pc + 1].longconst);
/* Allocate arg vector, including space for the function to be
- called in argvec[0] and a terminating NULL. */
+ called in argvec[0], a potential `this', and a terminating NULL. */
argvec = (struct value **)
alloca (sizeof (struct value *) * (nargs + 3));
if (op == STRUCTOP_MEMBER || op == STRUCTOP_MPTR)
name, TYPE_TAG_NAME (type));
tem = 1;
+ /* arg2 is left as NULL on purpose. */
}
else
{
|| TYPE_CODE (type) == TYPE_CODE_UNION);
function_name = name;
+ /* We need a properly typed value for method lookup. For
+ static methods arg2 is otherwise unused. */
arg2 = value_zero (type, lval_memory);
++nargs;
tem = 2;
}
}
- /* Evaluate arguments. */
+ /* Evaluate arguments (if not already done, e.g., namespace::func()
+ and overload-resolution is off). */
for (; tem <= nargs; tem++)
{
/* Ensure that array expressions are coerced into pointer
/* Signal end of arglist. */
argvec[tem] = 0;
+
if (op == OP_ADL_FUNC)
{
struct symbol *symp;
int static_memfuncp;
char *tstr;
- /* Method invocation : stuff "this" as first parameter. */
+ /* Method invocation: stuff "this" as first parameter.
+ If the method turns out to be static we undo this below. */
argvec[1] = arg2;
if (op != OP_SCOPE)
argvec[1] = arg2; /* the ``this'' pointer */
}
+ /* Take out `this' if needed. */
if (static_memfuncp)
{
argvec[1] = argvec[0];
error (_("Expression of type other than "
"\"Function returning ...\" used as function"));
}
- if (TYPE_CODE (value_type (argvec[0])) == TYPE_CODE_INTERNAL_FUNCTION)
- return call_internal_function (exp->gdbarch, exp->language_defn,
- argvec[0], nargs, argvec + 1);
-
- return call_function_by_hand (argvec[0], nargs, argvec + 1);
+ switch (TYPE_CODE (value_type (argvec[0])))
+ {
+ case TYPE_CODE_INTERNAL_FUNCTION:
+ return call_internal_function (exp->gdbarch, exp->language_defn,
+ argvec[0], nargs, argvec + 1);
+ case TYPE_CODE_XMETHOD:
+ return call_xmethod (argvec[0], nargs, argvec + 1);
+ default:
+ return call_function_by_hand (argvec[0], nargs, argvec + 1);
+ }
/* pai: FIXME save value from call_function_by_hand, then adjust
pc by adjust_fn_pc if +ve. */
arg1 = evaluate_subexp (NULL_TYPE, exp, pos, noside);
if (noside == EVAL_SKIP)
goto nosideret;
+ arg3 = value_struct_elt (&arg1, NULL, &exp->elts[pc + 2].string,
+ NULL, "structure");
if (noside == EVAL_AVOID_SIDE_EFFECTS)
- return value_zero (lookup_struct_elt_type (value_type (arg1),
- &exp->elts[pc + 2].string,
- 0),
- lval_memory);
- else
- {
- struct value *temp = arg1;
-
- return value_struct_elt (&temp, NULL, &exp->elts[pc + 2].string,
- NULL, "structure");
- }
+ arg3 = value_zero (value_type (arg3), not_lval);
+ return arg3;
case STRUCTOP_PTR:
tem = longest_to_int (exp->elts[pc + 1].longconst);
}
}
+ arg3 = value_struct_elt (&arg1, NULL, &exp->elts[pc + 2].string,
+ NULL, "structure pointer");
if (noside == EVAL_AVOID_SIDE_EFFECTS)
- return value_zero (lookup_struct_elt_type (value_type (arg1),
- &exp->elts[pc + 2].string,
- 0),
- lval_memory);
- else
- {
- struct value *temp = arg1;
-
- return value_struct_elt (&temp, NULL, &exp->elts[pc + 2].string,
- NULL, "structure pointer");
- }
+ arg3 = value_zero (value_type (arg3), not_lval);
+ return arg3;
case STRUCTOP_MEMBER:
case STRUCTOP_MPTR:
evaluate_subexp (NULL_TYPE, exp, pos, EVAL_SKIP);
goto nosideret;
}
- return evaluate_subexp_for_sizeof (exp, pos);
+ return evaluate_subexp_for_sizeof (exp, pos, noside);
case UNOP_CAST:
(*pos) += 2;
else
error (_("Attempt to use a type as an expression"));
+ case OP_TYPEID:
+ {
+ struct value *result;
+ enum exp_opcode sub_op = exp->elts[*pos].opcode;
+
+ if (sub_op == OP_TYPE || sub_op == OP_DECLTYPE || sub_op == OP_TYPEOF)
+ result = evaluate_subexp (NULL_TYPE, exp, pos,
+ EVAL_AVOID_SIDE_EFFECTS);
+ else
+ result = evaluate_subexp (NULL_TYPE, exp, pos, noside);
+
+ if (noside != EVAL_NORMAL)
+ return allocate_value (cplus_typeid_type (exp->gdbarch));
+
+ return cplus_typeid (result);
+ }
+
default:
/* Removing this case and compiling with gcc -Wall reveals that
a lot of cases are hitting this case. Some of these should
/* Evaluate a subexpression of EXP, at index *POS,
and return a value for the size of that subexpression.
- Advance *POS over the subexpression. */
+ Advance *POS over the subexpression. If NOSIDE is EVAL_NORMAL
+ we allow side-effects on the operand if its type is a variable
+ length array. */
static struct value *
-evaluate_subexp_for_sizeof (struct expression *exp, int *pos)
+evaluate_subexp_for_sizeof (struct expression *exp, int *pos,
+ enum noside noside)
{
/* FIXME: This should be size_t. */
struct type *size_type = builtin_type (exp->gdbarch)->builtin_int;
&& TYPE_CODE (type) != TYPE_CODE_REF
&& TYPE_CODE (type) != TYPE_CODE_ARRAY)
error (_("Attempt to take contents of a non-pointer value."));
- type = check_typedef (TYPE_TARGET_TYPE (type));
+ type = TYPE_TARGET_TYPE (type);
+ if (is_dynamic_type (type))
+ type = value_type (value_ind (val));
return value_from_longest (size_type, (LONGEST) TYPE_LENGTH (type));
case UNOP_MEMVAL:
(*pos) += 3;
- type = check_typedef (exp->elts[pc + 1].type);
- return value_from_longest (size_type, (LONGEST) TYPE_LENGTH (type));
+ type = exp->elts[pc + 1].type;
+ break;
case UNOP_MEMVAL_TYPE:
(*pos) += 1;
val = evaluate_subexp (NULL, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
- type = check_typedef (value_type (val));
- return value_from_longest (size_type, (LONGEST) TYPE_LENGTH (type));
+ type = value_type (val);
+ break;
case OP_VAR_VALUE:
- (*pos) += 4;
- type = check_typedef (SYMBOL_TYPE (exp->elts[pc + 2].symbol));
- return
- value_from_longest (size_type, (LONGEST) TYPE_LENGTH (type));
+ type = SYMBOL_TYPE (exp->elts[pc + 2].symbol);
+ if (is_dynamic_type (type))
+ {
+ val = evaluate_subexp (NULL_TYPE, exp, pos, EVAL_NORMAL);
+ type = value_type (val);
+ }
+ else
+ (*pos) += 4;
+ break;
+
+ /* Deal with the special case if NOSIDE is EVAL_NORMAL and the resulting
+ type of the subscript is a variable length array type. In this case we
+ must re-evaluate the right hand side of the subcription to allow
+ side-effects. */
+ case BINOP_SUBSCRIPT:
+ if (noside == EVAL_NORMAL)
+ {
+ int pc = (*pos) + 1;
+
+ val = evaluate_subexp (NULL_TYPE, exp, &pc, EVAL_AVOID_SIDE_EFFECTS);
+ type = check_typedef (value_type (val));
+ if (TYPE_CODE (type) == TYPE_CODE_ARRAY)
+ {
+ type = check_typedef (TYPE_TARGET_TYPE (type));
+ if (TYPE_CODE (type) == TYPE_CODE_ARRAY)
+ {
+ type = TYPE_INDEX_TYPE (type);
+ /* Only re-evaluate the right hand side if the resulting type
+ is a variable length type. */
+ if (TYPE_RANGE_DATA (type)->flag_bound_evaluated)
+ {
+ val = evaluate_subexp (NULL_TYPE, exp, pos, EVAL_NORMAL);
+ return value_from_longest
+ (size_type, (LONGEST) TYPE_LENGTH (value_type (val)));
+ }
+ }
+ }
+ }
+
+ /* Fall through. */
default:
val = evaluate_subexp (NULL_TYPE, exp, pos, EVAL_AVOID_SIDE_EFFECTS);
- return value_from_longest (size_type,
- (LONGEST) TYPE_LENGTH (value_type (val)));
+ type = value_type (val);
+ break;
}
+
+ /* $5.3.3/2 of the C++ Standard (n3290 draft) says of sizeof:
+ "When applied to a reference or a reference type, the result is
+ the size of the referenced type." */
+ CHECK_TYPEDEF (type);
+ if (exp->language_defn->la_language == language_cplus
+ && TYPE_CODE (type) == TYPE_CODE_REF)
+ type = check_typedef (TYPE_TARGET_TYPE (type));
+ return value_from_longest (size_type, (LONGEST) TYPE_LENGTH (type));
}
/* Parse a type expression in the string [P..P+LENGTH). */