[v3,170/206] Introduce ada_structop_operation

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

Commit Message

Tom Tromey Feb. 20, 2021, 8:15 p.m.
This adds class ada_structop_operation, which implements
STRUCTOP_STRUCT for Ada.

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

	* ada-lang.c (ada_structop_operation::evaluate): New method.
	* ada-exp.h (class ada_structop_operation): New.
---
 gdb/ChangeLog  |  5 +++++
 gdb/ada-exp.h  | 16 ++++++++++++++++
 gdb/ada-lang.c | 42 ++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 63 insertions(+)

-- 
2.26.2

Patch

diff --git a/gdb/ada-exp.h b/gdb/ada-exp.h
index c87036e2d19..34b9c1d166d 100644
--- a/gdb/ada-exp.h
+++ b/gdb/ada-exp.h
@@ -371,6 +371,22 @@  class ada_unop_ind_operation
 		   enum noside noside) override;
 };
 
+/* Implement STRUCTOP_STRUCT for Ada.  */
+class ada_structop_operation
+  : public structop_base_operation
+{
+public:
+
+  using structop_base_operation::structop_base_operation;
+
+  value *evaluate (struct type *expect_type,
+		   struct expression *exp,
+		   enum noside noside) override;
+
+  enum exp_opcode opcode () const override
+  { return STRUCTOP_STRUCT; }
+};
+
 } /* namespace expr */
 
 #endif /* ADA_EXP_H */
diff --git a/gdb/ada-lang.c b/gdb/ada-lang.c
index 77adf878ed3..c9888f2494e 100644
--- a/gdb/ada-lang.c
+++ b/gdb/ada-lang.c
@@ -10959,6 +10959,48 @@  ada_unop_ind_operation::evaluate (struct type *expect_type,
     return ada_value_ind (arg1);
 }
 
+value *
+ada_structop_operation::evaluate (struct type *expect_type,
+				  struct expression *exp,
+				  enum noside noside)
+{
+  value *arg1 = std::get<0> (m_storage)->evaluate (nullptr, exp, noside);
+  const char *str = std::get<1> (m_storage).c_str ();
+  if (noside == EVAL_AVOID_SIDE_EFFECTS)
+    {
+      struct type *type;
+      struct type *type1 = value_type (arg1);
+
+      if (ada_is_tagged_type (type1, 1))
+	{
+	  type = ada_lookup_struct_elt_type (type1, str, 1, 1);
+
+	  /* If the field is not found, check if it exists in the
+	     extension of this object's type. This means that we
+	     need to evaluate completely the expression.  */
+
+	  if (type == NULL)
+	    {
+	      arg1 = std::get<0> (m_storage)->evaluate (nullptr, exp,
+							EVAL_NORMAL);
+	      arg1 = ada_value_struct_elt (arg1, str, 0);
+	      arg1 = unwrap_value (arg1);
+	      type = value_type (ada_to_fixed_value (arg1));
+	    }
+	}
+      else
+	type = ada_lookup_struct_elt_type (type1, str, 1, 0);
+
+      return value_zero (ada_aligned_type (type), lval_memory);
+    }
+  else
+    {
+      arg1 = ada_value_struct_elt (arg1, str, 0);
+      arg1 = unwrap_value (arg1);
+      return ada_to_fixed_value (arg1);
+    }
+}
+
 }
 
 /* Implement the evaluate_exp routine in the exp_descriptor structure