[PATCH 105/203] Implement unary increment and decrement operations

Tom Tromey
2021-01-01T21:48:45Z

This implements the unary increment and decrement operations, "++" and
"--".

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

* expop.h (unop_incr_operation): New template.
(preinc_operation, predec_operation, postinc_operation)
(postdec_operation): New typedefs.
* eval.c (eval_op_preinc, eval_op_predec, eval_op_postinc)
(eval_op_postdec): No longer static.
---
gdb/ChangeLog | 8 ++++++++
gdb/eval.c | 8 ++++----
gdb/expop.h | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 62 insertions(+), 4 deletions(-)

diff --git a/gdb/eval.c b/gdb/eval.c
index 60e065cb0de..b7f816e28fe 100644
--- a/gdb/eval.c
+++ b/gdb/eval.c
@@ -1887,7 +1887,7 @@ eval_op_memval (struct type *expect_type, struct expression *exp,

/* A helper function for UNOP_PREINCREMENT. */

-static struct value *
+struct value *
eval_op_preinc (struct type *expect_type, struct expression *exp,
enum noside noside, enum exp_opcode op,
struct value *arg1)
@@ -1918,7 +1918,7 @@ eval_op_preinc (struct type *expect_type, struct expression *exp,

/* A helper function for UNOP_PREDECREMENT. */

-static struct value *
+struct value *
eval_op_predec (struct type *expect_type, struct expression *exp,
enum noside noside, enum exp_opcode op,
struct value *arg1)
@@ -1949,7 +1949,7 @@ eval_op_predec (struct type *expect_type, struct expression *exp,

/* A helper function for UNOP_POSTINCREMENT. */

-static struct value *
+struct value *
eval_op_postinc (struct type *expect_type, struct expression *exp,
enum noside noside, enum exp_opcode op,
struct value *arg1)
@@ -1983,7 +1983,7 @@ eval_op_postinc (struct type *expect_type, struct expression *exp,

/* A helper function for UNOP_POSTDECREMENT. */

-static struct value *
+struct value *
eval_op_postdec (struct type *expect_type, struct expression *exp,
enum noside noside, enum exp_opcode op,
struct value *arg1)
diff --git a/gdb/expop.h b/gdb/expop.h
index aae42f201bc..913988d7692 100644
--- a/gdb/expop.h
+++ b/gdb/expop.h
@@ -161,6 +161,26 @@ extern struct value *eval_op_lognot (struct type *expect_type,
enum noside noside,
enum exp_opcode op,
struct value *arg1);
+extern struct value *eval_op_preinc (struct type *expect_type,
+ struct expression *exp,
+ enum noside noside,
+ enum exp_opcode op,
+ struct value *arg1);
+extern struct value *eval_op_predec (struct type *expect_type,
+ struct expression *exp,
+ enum noside noside,
+ enum exp_opcode op,
+ struct value *arg1);
+extern struct value *eval_op_postinc (struct type *expect_type,
+ struct expression *exp,
+ enum noside noside,
+ enum exp_opcode op,
+ struct value *arg1);
+extern struct value *eval_op_postdec (struct type *expect_type,
+ struct expression *exp,
+ enum noside noside,
+ enum exp_opcode op,
+ struct value *arg1);

namespace expr
{
@@ -1305,6 +1325,36 @@ using unary_complement_operation
using unary_logical_not_operation
= usual_ax_unop_operation<UNOP_LOGICAL_NOT, eval_op_lognot>;

+/* Handle pre- and post- increment and -decrement. */
+template<enum exp_opcode OP, unary_ftype FUNC>
+class unop_incr_operation
+ : public tuple_holding_operation<operation_up>
+{
+public:
+
+ using tuple_holding_operation::tuple_holding_operation;
+
+ value *evaluate (struct type *expect_type,
+ struct expression *exp,
+ enum noside noside) override
+ {
+ value *val = std::get<0> (m_storage)->evaluate (expect_type, exp, noside);
+ return FUNC (expect_type, exp, noside, OP, val);
+ }
+
+ enum exp_opcode opcode () const override
+ { return OP; }
+};
+
+using preinc_operation
+ = unop_incr_operation<UNOP_PREINCREMENT, eval_op_preinc>;
+using predec_operation
+ = unop_incr_operation<UNOP_PREDECREMENT, eval_op_predec>;
+using postinc_operation
+ = unop_incr_operation<UNOP_POSTINCREMENT, eval_op_postinc>;
+using postdec_operation
+ = unop_incr_operation<UNOP_POSTDECREMENT, eval_op_postdec>;
+
} /* namespace expr */

#endif /* EXPOP_H */
--
2.26.2