[PATCH 152/203] Introduce ada_binop_addsub_operation

Tom Tromey
2021-01-01T21:49:25Z

This adds class ada_binop_addsub_operation, which implements the Ada +
and - operators.

gdb/ChangeLog
2021-01-01 Tom Tromey <tom@tromey.com>

* 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 | 37 +++++++++++++++++++++++++++++++++++++
3 files changed, 58 insertions(+)

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 @@ class ada_unop_range_operation
{ return UNOP_IN_RANGE; }
};

+/* The Ada + and - operators. */
+class ada_binop_addsub_operation
+ : public tuple_holding_operation<enum exp_opcode, operation_up, operation_up>
+{
+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 50b82e6bcf9..0c36ec4cd2e 100644
--- a/gdb/ada-lang.c
+++ b/gdb/ada-lang.c
@@ -10637,6 +10637,43 @@ 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))));
+ if ((ada_is_gnat_encoded_fixed_point_type (value_type (arg1))
+ || ada_is_gnat_encoded_fixed_point_type (value_type (arg2)))
+ && value_type (arg1) != value_type (arg2))
+ error (_("Operands of fixed-point operation must have the same type"));
+ /* Do the work, and cast the result to the type of the first
+ argument. 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);
+ return value_cast (type, value_binop (arg1, arg2, std::get<0> (m_storage)));
+}
+
}

/* Implement the evaluate_exp routine in the exp_descriptor structure
--
2.26.2