Introduce ada_binop_addsub_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:32 +0000 (07:28 -0700)
This adds class ada_binop_addsub_operation, which implements the Ada +
and - operators.

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

* ada-lang.c (ada_binop_addsub_operation::evaluate): New method.
* ada-exp.h (class ada_binop_addsub_operation): New.

gdb/ChangeLog
gdb/ada-exp.h
gdb/ada-lang.c

index af13b9af5584c7344c4daa111058ce86301820e1..597513aea86ade2ad43094e098284e54483d83ea 100644 (file)
@@ -1,3 +1,8 @@
+2021-03-08  Tom Tromey  <tom@tromey.com>
+
+       * ada-lang.c (ada_binop_addsub_operation::evaluate): New method.
+       * ada-exp.h (class ada_binop_addsub_operation): New.
+
 2021-03-08  Tom Tromey  <tom@tromey.com>
 
        * ada-lang.h (ada_find_operator_symbol, ada_resolve_funcall)
index de69210bd2a9e918928e9d7784368f942e031bc7..ad69d93948928c367069117177542c9cc5e0310d 100644 (file)
@@ -134,6 +134,22 @@ public:
   { return UNOP_IN_RANGE; }
 };
 
+/* The Ada + and - operators.  */
+class ada_binop_addsub_operation
+  : public tuple_holding_operation<enum exp_opcode, 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;
+
+  enum exp_opcode opcode () const override
+  { return std::get<0> (m_storage); }
+};
+
 } /* namespace expr */
 
 #endif /* ADA_EXP_H */
index 098a5a6858f71cb99a9c3caa3dc237ccf6da436b..86ccc57334892ba399d9c558f332c3a95876357d 100644 (file)
@@ -10492,6 +10492,46 @@ ada_ternop_range_operation::evaluate (struct type *expect_type,
   return eval_ternop_in_range (expect_type, exp, noside, arg0, arg1, arg2);
 }
 
+value *
+ada_binop_addsub_operation::evaluate (struct type *expect_type,
+                                     struct expression *exp,
+                                     enum noside noside)
+{
+  value *arg1 = std::get<1> (m_storage)->evaluate_with_coercion (exp, noside);
+  value *arg2 = std::get<2> (m_storage)->evaluate_with_coercion (exp, noside);
+
+  auto do_op = [=] (LONGEST x, LONGEST y)
+    {
+      if (std::get<0> (m_storage) == BINOP_ADD)
+       return x + y;
+      return x - y;
+    };
+
+  if (value_type (arg1)->code () == TYPE_CODE_PTR)
+    return (value_from_longest
+           (value_type (arg1),
+            do_op (value_as_long (arg1), value_as_long (arg2))));
+  if (value_type (arg2)->code () == TYPE_CODE_PTR)
+    return (value_from_longest
+           (value_type (arg2),
+            do_op (value_as_long (arg1), value_as_long (arg2))));
+  /* Preserve the original type for use by the range case below.
+     We cannot cast the result to a reference type, so if ARG1 is
+     a reference type, find its underlying type.  */
+  struct type *type = value_type (arg1);
+  while (type->code () == TYPE_CODE_REF)
+    type = TYPE_TARGET_TYPE (type);
+  binop_promote (exp->language_defn, exp->gdbarch, &arg1, &arg2);
+  arg1 = value_binop (arg1, arg2, std::get<0> (m_storage));
+  /* We need to special-case the result with a range.
+     This is done for the benefit of "ptype".  gdb's Ada support
+     historically used the LHS to set the result type here, so
+     preserve this behavior.  */
+  if (type->code () == TYPE_CODE_RANGE)
+    arg1 = value_cast (type, arg1);
+  return arg1;
+}
+
 }
 
 /* Implement the evaluate_exp routine in the exp_descriptor structure
This page took 0.036172 seconds and 4 git commands to generate.