[v3,126/206] Introduce ada_wrapped_operation

Message ID 20210220201609.838264-127-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 ada_wrapped_operation, which is used to wrap some
generic operations with a bit of Ada-specific handling.  This
corresponds to the old "default" case in ada_evaluate_subexp.

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

	* ada-lang.c (ada_wrapped_operation::evaluate): New method.
	* ada-exp.h: New file.
---
 gdb/ChangeLog  |  5 +++++
 gdb/ada-exp.h  | 47 +++++++++++++++++++++++++++++++++++++++++++++++
 gdb/ada-lang.c | 29 +++++++++++++++++++++++++++++
 3 files changed, 81 insertions(+)
 create mode 100644 gdb/ada-exp.h

-- 
2.26.2

Patch

diff --git a/gdb/ada-exp.h b/gdb/ada-exp.h
new file mode 100644
index 00000000000..3f780dc9ff4
--- /dev/null
+++ b/gdb/ada-exp.h
@@ -0,0 +1,47 @@ 
+/* Definitions for Ada expressions
+
+   Copyright (C) 2020 Free Software Foundation, Inc.
+
+   This file is part of GDB.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+#ifndef ADA_EXP_H
+#define ADA_EXP_H
+
+#include "expop.h"
+
+namespace expr
+{
+
+/* In Ada, some generic operations must be wrapped with a handler that
+   handles some Ada-specific type conversions.  */
+class ada_wrapped_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;
+
+  enum exp_opcode opcode () const override
+  { return std::get<0> (m_storage)->opcode (); }
+};
+
+} /* namespace expr */
+
+#endif /* ADA_EXP_H */
diff --git a/gdb/ada-lang.c b/gdb/ada-lang.c
index ae1adca1d5e..80b1fa1a4d6 100644
--- a/gdb/ada-lang.c
+++ b/gdb/ada-lang.c
@@ -57,6 +57,7 @@ 
 #include "gdbsupport/function-view.h"
 #include "gdbsupport/byte-vector.h"
 #include <algorithm>
+#include "ada-exp.h"
 
 /* Define whether or not the C operator '/' truncates towards zero for
    differently signed operands (truncation direction is undefined in C).
@@ -10562,6 +10563,34 @@  ada_binop_exp (struct type *expect_type,
     }
 }
 
+namespace expr
+{
+
+value *
+ada_wrapped_operation::evaluate (struct type *expect_type,
+				 struct expression *exp,
+				 enum noside noside)
+{
+  value *result = std::get<0> (m_storage)->evaluate (expect_type, exp, noside);
+  if (noside == EVAL_NORMAL)
+    result = unwrap_value (result);
+
+  /* If evaluating an OP_FLOAT and an EXPECT_TYPE was provided,
+     then we need to perform the conversion manually, because
+     evaluate_subexp_standard doesn't do it.  This conversion is
+     necessary in Ada because the different kinds of float/fixed
+     types in Ada have different representations.
+
+     Similarly, we need to perform the conversion from OP_LONG
+     ourselves.  */
+  if ((opcode () == OP_FLOAT || opcode () == OP_LONG) && expect_type != NULL)
+    result = ada_value_cast (expect_type, result);
+
+  return result;
+}
+
+}
+
 /* Implement the evaluate_exp routine in the exp_descriptor structure
    for the Ada language.  */