Introduce unop_sizeof_operation
authorTom Tromey <tom@tromey.com>
Mon, 8 Mar 2021 14:27:57 +0000 (07:27 -0700)
committerTom Tromey <tom@tromey.com>
Mon, 8 Mar 2021 14:28:22 +0000 (07:28 -0700)
This adds class unop_sizeof_operation, which implements UNOP_SIZEOF.

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

* expop.h (class unop_sizeof_operation): New.
* ax-gdb.c (unop_sizeof_operation::do_generate_ax): New method.

gdb/ChangeLog
gdb/ax-gdb.c
gdb/expop.h

index 1998e486b7d41f22ea10101affccfca09ab5505f..cf5d7839f6a86a1fbacbcddd41c40d6ba6a4dabc 100644 (file)
@@ -1,3 +1,8 @@
+2021-03-08  Tom Tromey  <tom@tromey.com>
+
+       * expop.h (class unop_sizeof_operation): New.
+       * ax-gdb.c (unop_sizeof_operation::do_generate_ax): New method.
+
 2021-03-08  Tom Tromey  <tom@tromey.com>
 
        * expop.h (class unop_addr_operation): New.
index 64710887534440e640d9f39ade722ace92301e89..756f4d7969aa86f36b7528a73a4eed0ce91e894f 100644 (file)
@@ -2491,6 +2491,29 @@ comma_operation::do_generate_ax (struct expression *exp,
   /* It's the consumer's responsibility to trace the right operand.  */
 }
 
+void
+unop_sizeof_operation::do_generate_ax (struct expression *exp,
+                                      struct agent_expr *ax,
+                                      struct axs_value *value,
+                                      struct type *cast_type)
+{
+  /* We don't care about the value of the operand expression; we only
+     care about its type.  However, in the current arrangement, the
+     only way to find an expression's type is to generate code for it.
+     So we generate code for the operand, and then throw it away,
+     replacing it with code that simply pushes its size.  */
+  int start = ax->len;
+
+  std::get<0> (m_storage)->generate_ax (exp, ax, value);
+
+  /* Throw away the code we just generated.  */
+  ax->len = start;
+
+  ax_const_l (ax, TYPE_LENGTH (value->type));
+  value->kind = axs_rvalue;
+  value->type = builtin_type (ax->gdbarch)->builtin_int;
+}
+
 }
 
 /* This handles the middle-to-right-side of code generation for binary
index a09be969a087cde0281a530b4133941c18c7bc8b..8cb7281830bec8522fadb0dc1da00d0a15ce658f 100644 (file)
@@ -1582,6 +1582,35 @@ protected:
   }
 };
 
+/* Implement 'sizeof'.  */
+class unop_sizeof_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);
+    return std::get<0> (m_storage)->evaluate_for_sizeof (exp, noside);
+  }
+
+  enum exp_opcode opcode () const override
+  { return UNOP_SIZEOF; }
+
+protected:
+
+  void do_generate_ax (struct expression *exp,
+                      struct agent_expr *ax,
+                      struct axs_value *value,
+                      struct type *cast_type)
+    override;
+};
+
 } /* namespace expr */
 
 #endif /* EXPOP_H */