[132/203] Introduce rust_subscript_operation

Message ID 20210101214723.1784144-133-tom@tromey.com
State Superseded
Headers show
Series
  • Refactor expressions
Related show

Commit Message

Tom Tromey Jan. 1, 2021, 9:46 p.m.
This adds class rust_subscript_operation, which implements
BINOP_SUBSCRIPT for Rust.

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

	* rust-lang.c (rust_subscript): No longer static.
	* rust-exp.h (class rust_subscript_operation): New.
---
 gdb/ChangeLog   |  5 +++++
 gdb/rust-exp.h  | 57 +++++++++++++++++++++++++++++++++++++++++++++++++
 gdb/rust-lang.c |  2 +-
 3 files changed, 63 insertions(+), 1 deletion(-)

-- 
2.26.2

Patch

diff --git a/gdb/rust-exp.h b/gdb/rust-exp.h
index d16f921ca00..7571009ea39 100644
--- a/gdb/rust-exp.h
+++ b/gdb/rust-exp.h
@@ -38,6 +38,10 @@  extern struct value *eval_op_rust_ind (struct type *expect_type,
 				       enum noside noside,
 				       enum exp_opcode opcode,
 				       struct value *value);
+extern struct value *rust_subscript (struct type *expect_type,
+				     struct expression *exp,
+				     enum noside noside, bool for_addr,
+				     struct value *lhs, struct value *rhs);
 
 namespace expr
 {
@@ -67,6 +71,59 @@  class rust_unop_ind_operation
   }
 };
 
+/* Subscript operator for Rust.  */
+class rust_subscript_operation
+  : public tuple_holding_operation<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
+  {
+    value *arg1 = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
+    value *arg2 = std::get<1> (m_storage)->evaluate (nullptr, exp, noside);
+    return rust_subscript (expect_type, exp, noside, false, arg1, arg2);
+  }
+
+  value *slice (struct type *expect_type,
+		struct expression *exp,
+		enum noside noside)
+  {
+    value *arg1 = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
+    value *arg2 = std::get<1> (m_storage)->evaluate (nullptr, exp, noside);
+    return rust_subscript (expect_type, exp, noside, true, arg1, arg2);
+  }
+
+  enum exp_opcode opcode () const override
+  { return BINOP_SUBSCRIPT; }
+};
+
+class rust_unop_addr_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
+  {
+    operation *oper = std::get<0> (m_storage).get ();
+    rust_subscript_operation *sub_op
+      = dynamic_cast<rust_subscript_operation *> (oper);
+    if (sub_op != nullptr)
+      return sub_op->slice (expect_type, exp, noside);
+    return oper->evaluate_for_address (exp, noside);
+  }
+
+  enum exp_opcode opcode () const override
+  { return UNOP_ADDR; }
+};
+
 } /* namespace expr */
 
 #endif /* RUST_EXP_H */
diff --git a/gdb/rust-lang.c b/gdb/rust-lang.c
index 2c3f72ed404..0035a58adc4 100644
--- a/gdb/rust-lang.c
+++ b/gdb/rust-lang.c
@@ -1168,7 +1168,7 @@  rust_compute_range (struct type *type, struct value *range,
 
 /* A helper for rust_evaluate_subexp that handles BINOP_SUBSCRIPT.  */
 
-static struct value *
+struct value *
 rust_subscript (struct type *expect_type, struct expression *exp,
 		enum noside noside, bool for_addr,
 		struct value *lhs, struct value *rhs)