From 224d6424bab021a0a8e16f07d52f8af5e153e91f Mon Sep 17 00:00:00 2001 From: Tom Tromey Date: Mon, 8 Mar 2021 07:27:57 -0700 Subject: Introduce subscript_operation This adds class subscript_operation, which implements BINOP_SUBSCRIPT. gdb/ChangeLog 2021-03-08 Tom Tromey * expop.h (class subscript_operation): New. * eval.c (eval_op_subscript): No longer static. --- gdb/ChangeLog | 5 +++++ gdb/eval.c | 35 ++++++++++++++++++++++++++++++++++- gdb/expop.h | 16 ++++++++++++++++ 3 files changed, 55 insertions(+), 1 deletion(-) (limited to 'gdb') diff --git a/gdb/ChangeLog b/gdb/ChangeLog index 0ed474f..b84ebfb 100644 --- a/gdb/ChangeLog +++ b/gdb/ChangeLog @@ -1,5 +1,10 @@ 2021-03-08 Tom Tromey + * expop.h (class subscript_operation): New. + * eval.c (eval_op_subscript): No longer static. + +2021-03-08 Tom Tromey + * expop.h (class binop_operation, class usual_ax_binop_operation): New. (exp_operation, intdiv_operation, mod_operation, mul_operation) diff --git a/gdb/eval.c b/gdb/eval.c index 6fde03d..056daa0 100644 --- a/gdb/eval.c +++ b/gdb/eval.c @@ -1558,7 +1558,7 @@ eval_op_binary (struct type *expect_type, struct expression *exp, /* A helper function for BINOP_SUBSCRIPT. */ -static struct value * +struct value * eval_op_subscript (struct type *expect_type, struct expression *exp, enum noside noside, enum exp_opcode op, struct value *arg1, struct value *arg2) @@ -3536,6 +3536,39 @@ var_msym_value_operation::evaluate_for_sizeof (struct expression *exp, return value_from_longest (size_type, TYPE_LENGTH (type)); } +value * +subscript_operation::evaluate_for_sizeof (struct expression *exp, + enum noside noside) +{ + if (noside == EVAL_NORMAL) + { + value *val = std::get<0> (m_storage)->evaluate (nullptr, exp, + EVAL_AVOID_SIDE_EFFECTS); + struct type *type = check_typedef (value_type (val)); + if (type->code () == TYPE_CODE_ARRAY) + { + type = check_typedef (TYPE_TARGET_TYPE (type)); + if (type->code () == TYPE_CODE_ARRAY) + { + type = type->index_type (); + /* Only re-evaluate the right hand side if the resulting type + is a variable length type. */ + if (type->bounds ()->flag_bound_evaluated) + { + val = evaluate (nullptr, exp, EVAL_NORMAL); + /* FIXME: This should be size_t. */ + struct type *size_type + = builtin_type (exp->gdbarch)->builtin_int; + return value_from_longest + (size_type, (LONGEST) TYPE_LENGTH (value_type (val))); + } + } + } + } + + return operation::evaluate_for_sizeof (exp, noside); +} + } /* Evaluate a subexpression of EXP, at index *POS, and return a value diff --git a/gdb/expop.h b/gdb/expop.h index 4624c2f..5c3b0af 100644 --- a/gdb/expop.h +++ b/gdb/expop.h @@ -100,6 +100,11 @@ extern struct value *eval_op_binary (struct type *expect_type, struct expression *exp, enum noside noside, enum exp_opcode op, struct value *arg1, struct value *arg2); +extern struct value *eval_op_subscript (struct type *expect_type, + struct expression *exp, + enum noside noside, enum exp_opcode op, + struct value *arg1, + struct value *arg2); namespace expr { @@ -1112,6 +1117,17 @@ using bitwise_ior_operation using bitwise_xor_operation = usual_ax_binop_operation; +class subscript_operation + : public usual_ax_binop_operation +{ +public: + using usual_ax_binop_operation::usual_ax_binop_operation; + + value *evaluate_for_sizeof (struct expression *exp, + enum noside noside) override; +}; + } /* namespace expr */ #endif /* EXPOP_H */ -- cgit v1.1