From e82a5afcedd98a2ca9c1915bef2938c77e967959 Mon Sep 17 00:00:00 2001 From: Tom Tromey Date: Mon, 8 Mar 2021 07:27:57 -0700 Subject: [PATCH] Introduce var_value_operation This adds class var_value_operation, which implements OP_VAR_VALUE. gdb/ChangeLog 2021-03-08 Tom Tromey * expop.h (class var_value_operation): New. * eval.c (var_value_operation::evaluate) (var_value_operation::evaluate_for_address) (var_value_operation::evaluate_with_coercion) (var_value_operation::evaluate_for_sizeof) (var_value_operation::evaluate_for_cast): New methods. * ax-gdb.c (var_value_operation::do_generate_ax): New method. --- gdb/ChangeLog | 10 +++++ gdb/ax-gdb.c | 20 +++++++++ gdb/eval.c | 112 ++++++++++++++++++++++++++++++++++++++++++++++++++ gdb/expop.h | 37 +++++++++++++++++ 4 files changed, 179 insertions(+) diff --git a/gdb/ChangeLog b/gdb/ChangeLog index 053f9a919a1..4f6680ee230 100644 --- a/gdb/ChangeLog +++ b/gdb/ChangeLog @@ -1,3 +1,13 @@ +2021-03-08 Tom Tromey + + * expop.h (class var_value_operation): New. + * eval.c (var_value_operation::evaluate) + (var_value_operation::evaluate_for_address) + (var_value_operation::evaluate_with_coercion) + (var_value_operation::evaluate_for_sizeof) + (var_value_operation::evaluate_for_cast): New methods. + * ax-gdb.c (var_value_operation::do_generate_ax): New method. + 2021-03-08 Tom Tromey * expop.h (cxx_cast_ftype): New typedef. diff --git a/gdb/ax-gdb.c b/gdb/ax-gdb.c index e0970e87d68..bdf4a6dc39a 100644 --- a/gdb/ax-gdb.c +++ b/gdb/ax-gdb.c @@ -2673,6 +2673,26 @@ unop_cast_type_operation::do_generate_ax (struct expression *exp, std::get<1> (m_storage)->generate_ax (exp, ax, value, value_type (val)); } +void +var_value_operation::do_generate_ax (struct expression *exp, + struct agent_expr *ax, + struct axs_value *value, + struct type *cast_type) +{ + gen_var_ref (ax, value, std::get<0> (m_storage)); + + if (value->optimized_out) + error (_("`%s' has been optimized out, cannot use"), + std::get<0> (m_storage)->print_name ()); + + if (value->type->code () == TYPE_CODE_ERROR) + { + if (cast_type == nullptr) + error_unknown_type (std::get<0> (m_storage)->print_name ()); + value->type = cast_type; + } +} + } /* This handles the middle-to-right-side of code generation for binary diff --git a/gdb/eval.c b/gdb/eval.c index 4a7613565f1..9d3b5ae8762 100644 --- a/gdb/eval.c +++ b/gdb/eval.c @@ -686,6 +686,23 @@ evaluate_var_value (enum noside noside, const block *blk, symbol *var) return ret; } +namespace expr + +{ + +value * +var_value_operation::evaluate (struct type *expect_type, + struct expression *exp, + enum noside noside) +{ + symbol *var = std::get<0> (m_storage); + if (SYMBOL_TYPE (var)->code () == TYPE_CODE_ERROR) + error_unknown_type (var->print_name ()); + return evaluate_var_value (noside, std::get<1> (m_storage), var); +} + +} /* namespace expr */ + /* Helper for evaluating an OP_VAR_MSYM_VALUE. */ value * @@ -3417,6 +3434,54 @@ evaluate_subexp_with_coercion (struct expression *exp, } } +namespace expr +{ + +value * +var_value_operation::evaluate_for_address (struct expression *exp, + enum noside noside) +{ + symbol *var = std::get<0> (m_storage); + + /* C++: The "address" of a reference should yield the address + * of the object pointed to. Let value_addr() deal with it. */ + if (TYPE_IS_REFERENCE (SYMBOL_TYPE (var))) + return operation::evaluate_for_address (exp, noside); + + if (noside == EVAL_AVOID_SIDE_EFFECTS) + { + struct type *type = lookup_pointer_type (SYMBOL_TYPE (var)); + enum address_class sym_class = SYMBOL_CLASS (var); + + if (sym_class == LOC_CONST + || sym_class == LOC_CONST_BYTES + || sym_class == LOC_REGISTER) + error (_("Attempt to take address of register or constant.")); + + return value_zero (type, not_lval); + } + else + return address_of_variable (var, std::get<1> (m_storage)); +} + +value * +var_value_operation::evaluate_with_coercion (struct expression *exp, + enum noside noside) +{ + struct symbol *var = std::get<0> (m_storage); + struct type *type = check_typedef (SYMBOL_TYPE (var)); + if (type->code () == TYPE_CODE_ARRAY + && !type->is_vector () + && CAST_IS_CONVERSION (exp->language_defn)) + { + struct value *val = address_of_variable (var, std::get<1> (m_storage)); + return value_cast (lookup_pointer_type (TYPE_TARGET_TYPE (type)), val); + } + return evaluate (nullptr, exp, noside); +} + +} + /* Helper function for evaluating the size of a type. */ static value * @@ -3657,6 +3722,29 @@ unop_memval_type_operation::evaluate_for_sizeof (struct expression *exp, return evaluate_subexp_for_sizeof_base (exp, value_type (typeval)); } +value * +var_value_operation::evaluate_for_sizeof (struct expression *exp, + enum noside noside) +{ + struct type *type = SYMBOL_TYPE (std::get<0> (m_storage)); + if (is_dynamic_type (type)) + { + value *val = evaluate (nullptr, exp, EVAL_NORMAL); + type = value_type (val); + if (type->code () == TYPE_CODE_ARRAY) + { + /* FIXME: This should be size_t. */ + struct type *size_type = builtin_type (exp->gdbarch)->builtin_int; + 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); + } + } + return evaluate_subexp_for_sizeof_base (exp, type); +} + } /* Evaluate a subexpression of EXP, at index *POS, and return a value @@ -3744,6 +3832,30 @@ var_msym_value_operation::evaluate_for_cast (struct type *to_type, return val; } +value * +var_value_operation::evaluate_for_cast (struct type *to_type, + struct expression *exp, + enum noside noside) +{ + value *val = evaluate_var_value (noside, + std::get<1> (m_storage), + std::get<0> (m_storage)); + + if (noside == EVAL_SKIP) + return eval_skip_value (exp); + + val = value_cast (to_type, val); + + /* Don't allow e.g. '&(int)var_with_no_debug_info'. */ + if (VALUE_LVAL (val) == lval_memory) + { + if (value_lazy (val)) + value_fetch_lazy (val); + VALUE_LVAL (val) = not_lval; + } + return val; +} + } /* Parse a type expression in the string [P..P+LENGTH). */ diff --git a/gdb/expop.h b/gdb/expop.h index 3ab412e35a1..ef5b9bb197d 100644 --- a/gdb/expop.h +++ b/gdb/expop.h @@ -609,6 +609,43 @@ protected: override; }; +/* Compute the value of a variable. */ +class var_value_operation + : public maybe_constant_operation +{ +public: + + using maybe_constant_operation::maybe_constant_operation; + + value *evaluate (struct type *expect_type, + struct expression *exp, + enum noside noside) override; + + value *evaluate_with_coercion (struct expression *exp, + enum noside noside) override; + + value *evaluate_for_sizeof (struct expression *exp, enum noside noside) + override; + + value *evaluate_for_cast (struct type *expect_type, + struct expression *exp, + enum noside noside) override; + + value *evaluate_for_address (struct expression *exp, enum noside noside) + override; + + enum exp_opcode opcode () const override + { return OP_VAR_VALUE; } + +protected: + + void do_generate_ax (struct expression *exp, + struct agent_expr *ax, + struct axs_value *value, + struct type *cast_type) + override; +}; + class long_const_operation : public tuple_holding_operation { -- 2.30.2