From 73796c73263da16cedbefd5a43ab7e1887836115 Mon Sep 17 00:00:00 2001 From: Tom Tromey Date: Mon, 8 Mar 2021 07:27:57 -0700 Subject: [PATCH] Introduce ada_binop_addsub_operation This adds class ada_binop_addsub_operation, which implements the Ada + and - operators. gdb/ChangeLog 2021-03-08 Tom Tromey * ada-lang.c (ada_binop_addsub_operation::evaluate): New method. * ada-exp.h (class ada_binop_addsub_operation): New. --- gdb/ChangeLog | 5 +++++ gdb/ada-exp.h | 16 ++++++++++++++++ gdb/ada-lang.c | 40 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+) diff --git a/gdb/ChangeLog b/gdb/ChangeLog index af13b9af558..597513aea86 100644 --- a/gdb/ChangeLog +++ b/gdb/ChangeLog @@ -1,3 +1,8 @@ +2021-03-08 Tom Tromey + + * ada-lang.c (ada_binop_addsub_operation::evaluate): New method. + * ada-exp.h (class ada_binop_addsub_operation): New. + 2021-03-08 Tom Tromey * ada-lang.h (ada_find_operator_symbol, ada_resolve_funcall) diff --git a/gdb/ada-exp.h b/gdb/ada-exp.h index de69210bd2a..ad69d939489 100644 --- a/gdb/ada-exp.h +++ b/gdb/ada-exp.h @@ -134,6 +134,22 @@ public: { return UNOP_IN_RANGE; } }; +/* The Ada + and - operators. */ +class ada_binop_addsub_operation + : public tuple_holding_operation +{ +public: + + using tuple_holding_operation::tuple_holding_operation; + + value *evaluate (struct type *expect_type, + struct expression *exp, + enum noside noside) override; + + enum exp_opcode opcode () const override + { return std::get<0> (m_storage); } +}; + } /* namespace expr */ #endif /* ADA_EXP_H */ diff --git a/gdb/ada-lang.c b/gdb/ada-lang.c index 098a5a6858f..86ccc573348 100644 --- a/gdb/ada-lang.c +++ b/gdb/ada-lang.c @@ -10492,6 +10492,46 @@ ada_ternop_range_operation::evaluate (struct type *expect_type, return eval_ternop_in_range (expect_type, exp, noside, arg0, arg1, arg2); } +value * +ada_binop_addsub_operation::evaluate (struct type *expect_type, + struct expression *exp, + enum noside noside) +{ + value *arg1 = std::get<1> (m_storage)->evaluate_with_coercion (exp, noside); + value *arg2 = std::get<2> (m_storage)->evaluate_with_coercion (exp, noside); + + auto do_op = [=] (LONGEST x, LONGEST y) + { + if (std::get<0> (m_storage) == BINOP_ADD) + return x + y; + return x - y; + }; + + if (value_type (arg1)->code () == TYPE_CODE_PTR) + return (value_from_longest + (value_type (arg1), + do_op (value_as_long (arg1), value_as_long (arg2)))); + if (value_type (arg2)->code () == TYPE_CODE_PTR) + return (value_from_longest + (value_type (arg2), + do_op (value_as_long (arg1), value_as_long (arg2)))); + /* Preserve the original type for use by the range case below. + We cannot cast the result to a reference type, so if ARG1 is + a reference type, find its underlying type. */ + struct type *type = value_type (arg1); + while (type->code () == TYPE_CODE_REF) + type = TYPE_TARGET_TYPE (type); + binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2); + arg1 = value_binop (arg1, arg2, std::get<0> (m_storage)); + /* We need to special-case the result with a range. + This is done for the benefit of "ptype". gdb's Ada support + historically used the LHS to set the result type here, so + preserve this behavior. */ + if (type->code () == TYPE_CODE_RANGE) + arg1 = value_cast (type, arg1); + return arg1; +} + } /* Implement the evaluate_exp routine in the exp_descriptor structure -- 2.30.2