[109/203] Introduce decltype_operation

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

Commit Message

Tom Tromey Jan. 1, 2021, 9:45 p.m.
This adds class decltype_operation, which implements OP_DECLTYPE.

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

	* expop.h (class decltype_operation): New.
---
 gdb/ChangeLog |  4 ++++
 gdb/expop.h   | 47 +++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 51 insertions(+)

-- 
2.26.2

Patch

diff --git a/gdb/expop.h b/gdb/expop.h
index 502ad71d067..ba107f10ba0 100644
--- a/gdb/expop.h
+++ b/gdb/expop.h
@@ -1459,6 +1459,53 @@  class typeof_operation
   { return OP_TYPEOF; }
 };
 
+/* Implement 'decltype'.  */
+class decltype_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)
+      {
+	value *result
+	  = std::get<0> (m_storage)->evaluate (nullptr, exp,
+					       EVAL_AVOID_SIDE_EFFECTS);
+	enum exp_opcode sub_op = std::get<0> (m_storage)->opcode ();
+	if (sub_op == BINOP_SUBSCRIPT
+	    || sub_op == STRUCTOP_MEMBER
+	    || sub_op == STRUCTOP_MPTR
+	    || sub_op == UNOP_IND
+	    || sub_op == STRUCTOP_STRUCT
+	    || sub_op == STRUCTOP_PTR
+	    || sub_op == OP_SCOPE)
+	  {
+	    struct type *type = value_type (result);
+
+	    if (!TYPE_IS_REFERENCE (type))
+	      {
+		type = lookup_lvalue_reference_type (type);
+		result = allocate_value (type);
+	      }
+	  }
+
+	return result;
+      }
+    else
+      error (_("Attempt to use a type as an expression"));
+  }
+
+  enum exp_opcode opcode () const override
+  { return OP_DECLTYPE; }
+};
+
 } /* namespace expr */
 
 #endif /* EXPOP_H */