[v3,109/206] Introduce typeof_operation

Message ID 20210220201609.838264-110-tom@tromey.com
State New
Headers show
Series
  • Refactor expressions
Related show

Commit Message

Tom Tromey Feb. 20, 2021, 8:14 p.m.
This adds class typeof_operation, which implements OP_TYPEOF.

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

	* expop.h (class typeof_operation): New.
---
 gdb/ChangeLog |  4 ++++
 gdb/expop.h   | 25 +++++++++++++++++++++++++
 2 files changed, 29 insertions(+)

-- 
2.26.2

Patch

diff --git a/gdb/expop.h b/gdb/expop.h
index 8b7bfbe3af6..da7435918e2 100644
--- a/gdb/expop.h
+++ b/gdb/expop.h
@@ -1445,6 +1445,31 @@  class type_operation
   { return true; }
 };
 
+/* Implement the "typeof" operation.  */
+class typeof_operation
+  : public maybe_constant_operation<operation_up>
+{
+public:
+
+  using maybe_constant_operation::maybe_constant_operation;
+
+  value *evaluate (struct type *expect_type,
+		   struct expression *exp,
+		   enum noside noside) override
+  {
+    if (noside == EVAL_SKIP)
+      return eval_skip_value (exp);
+    else if (noside == EVAL_AVOID_SIDE_EFFECTS)
+      return std::get<0> (m_storage)->evaluate (nullptr, exp,
+						EVAL_AVOID_SIDE_EFFECTS);
+    else
+      error (_("Attempt to use a type as an expression"));
+  }
+
+  enum exp_opcode opcode () const override
+  { return OP_TYPEOF; }
+};
+
 } /* namespace expr */
 
 #endif /* EXPOP_H */