[169/203] Implement Ada resolution

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

Commit Message

Tom Tromey Jan. 1, 2021, 9:46 p.m.
Ada has a parser post-pass that implements "resolution".  This process
replaces some opcodes with function calls.  For example, a "+"
operation might be replaced with a call to the appropriate overloaded
function.

This differs from the approach taken for the same problem in C++.
However, in this series I chose not to try to make changes outside of
rewrite the expression data structure.  So, resolution remains.

The new approach to resolution is to introduce an interface class,
that some concrete operations implement.  Then, the Ada code will use
this to resolve the expression tree.  Because new-style expressions
are built as ordinary objects, and don't require rewriting the data
structure in place, in the new code this processing will be done in
the parser.  By the end of the series, some special cases in this area
that exist only for Ada will be removed.

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

	* ada-lang.c (ada_var_value_operation::resolve)
	(ada_funcall_operation::resolve): New methods.
	* ada-exp.h (struct ada_resolvable): New.
	(class ada_var_value_operation): Derive from ada_resolvable.
	<get_block, resolve>: New methods.
	(class ada_funcall_operation): Derive from ada_resolvable.
	<resolve>: New method.
---
 gdb/ChangeLog  | 10 ++++++++
 gdb/ada-exp.h  | 38 ++++++++++++++++++++++++++++--
 gdb/ada-lang.c | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 109 insertions(+), 2 deletions(-)

-- 
2.26.2

Comments

Joel Brobecker Jan. 3, 2021, 7:57 a.m. | #1
Hi Tom,

Just a tiny question below...

On Fri, Jan 01, 2021 at 02:46:49PM -0700, Tom Tromey wrote:
> Ada has a parser post-pass that implements "resolution".  This process

> replaces some opcodes with function calls.  For example, a "+"

> operation might be replaced with a call to the appropriate overloaded

> function.

> 

> This differs from the approach taken for the same problem in C++.

> However, in this series I chose not to try to make changes outside of

> rewrite the expression data structure.  So, resolution remains.

> 

> The new approach to resolution is to introduce an interface class,

> that some concrete operations implement.  Then, the Ada code will use

> this to resolve the expression tree.  Because new-style expressions

> are built as ordinary objects, and don't require rewriting the data

> structure in place, in the new code this processing will be done in

> the parser.  By the end of the series, some special cases in this area

> that exist only for Ada will be removed.

> 

> gdb/ChangeLog

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

> 

> 	* ada-lang.c (ada_var_value_operation::resolve)

> 	(ada_funcall_operation::resolve): New methods.

> 	* ada-exp.h (struct ada_resolvable): New.

> 	(class ada_var_value_operation): Derive from ada_resolvable.

> 	<get_block, resolve>: New methods.

> 	(class ada_funcall_operation): Derive from ada_resolvable.

> 	<resolve>: New method.

> ---

>  gdb/ChangeLog  | 10 ++++++++

>  gdb/ada-exp.h  | 38 ++++++++++++++++++++++++++++--

>  gdb/ada-lang.c | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++

>  3 files changed, 109 insertions(+), 2 deletions(-)

> 

> diff --git a/gdb/ada-exp.h b/gdb/ada-exp.h

> index 287ed5cc62c..2c5696cab76 100644

> --- a/gdb/ada-exp.h

> +++ b/gdb/ada-exp.h

> @@ -304,9 +304,27 @@ class ada_unop_atr_operation

>    { return std::get<1> (m_storage); }

>  };

>  

> +/* The base class for Ada type resolution.  Ada operations that want

> +   to participate in resolution implement this interface.  */

> +struct ada_resolvable

> +{

> +  /* Resolve this object.  EXP is the expression being resolved.

> +     DEPROCEDURE_P is true if de-proceduring is desired.


What does it mean "de-proceduring is desired"?

> +     PARSE_COMPLETION and TRACKER are passed in from the parser

> +     context.  CONTEXT_TYPE is the expected type of the expression, or

> +     nullptr if none is known.  This method should return true if the

> +     operation should be replaced by a function call with this object

> +     as the callee.  */


-- 
Joel
Tom Tromey Feb. 13, 2021, 7:49 p.m. | #2
>> +  /* Resolve this object.  EXP is the expression being resolved.

>> +     DEPROCEDURE_P is true if de-proceduring is desired.


Joel> What does it mean "de-proceduring is desired"?

I've updated the comment.  My understanding is that this refers to
transforming a reference to a symbol into a function call, if the symbol
names a zero-argument function.

Tom

Patch

diff --git a/gdb/ada-exp.h b/gdb/ada-exp.h
index 287ed5cc62c..2c5696cab76 100644
--- a/gdb/ada-exp.h
+++ b/gdb/ada-exp.h
@@ -304,9 +304,27 @@  class ada_unop_atr_operation
   { return std::get<1> (m_storage); }
 };
 
+/* The base class for Ada type resolution.  Ada operations that want
+   to participate in resolution implement this interface.  */
+struct ada_resolvable
+{
+  /* Resolve this object.  EXP is the expression being resolved.
+     DEPROCEDURE_P is true if de-proceduring is desired.
+     PARSE_COMPLETION and TRACKER are passed in from the parser
+     context.  CONTEXT_TYPE is the expected type of the expression, or
+     nullptr if none is known.  This method should return true if the
+     operation should be replaced by a function call with this object
+     as the callee.  */
+  virtual bool resolve (struct expression *exp,
+			bool deprocedure_p,
+			bool parse_completion,
+			innermost_block_tracker *tracker,
+			struct type *context_type) = 0;
+};
+
 /* Variant of var_value_operation for Ada.  */
 class ada_var_value_operation
-  : public var_value_operation
+  : public var_value_operation, public ada_resolvable
 {
 public:
 
@@ -323,6 +341,15 @@  class ada_var_value_operation
   symbol *get_symbol () const
   { return std::get<0> (m_storage); }
 
+  const block *get_block () const
+  { return std::get<1> (m_storage); }
+
+  bool resolve (struct expression *exp,
+		bool deprocedure_p,
+		bool parse_completion,
+		innermost_block_tracker *tracker,
+		struct type *context_type) override;
+
 protected:
 
   using operation::do_generate_ax;
@@ -392,7 +419,8 @@  class ada_structop_operation
 
 /* Function calls for Ada.  */
 class ada_funcall_operation
-  : public tuple_holding_operation<operation_up, std::vector<operation_up>>
+  : public tuple_holding_operation<operation_up, std::vector<operation_up>>,
+    public ada_resolvable
 {
 public:
 
@@ -402,6 +430,12 @@  class ada_funcall_operation
 		   struct expression *exp,
 		   enum noside noside) override;
 
+  bool resolve (struct expression *exp,
+		bool deprocedure_p,
+		bool parse_completion,
+		innermost_block_tracker *tracker,
+		struct type *context_type) override;
+
   enum exp_opcode opcode () const override
   { return OP_FUNCALL; }
 };
diff --git a/gdb/ada-lang.c b/gdb/ada-lang.c
index 18f250b5a34..fa323331047 100644
--- a/gdb/ada-lang.c
+++ b/gdb/ada-lang.c
@@ -10836,6 +10836,31 @@  ada_var_value_operation::evaluate (struct type *expect_type,
   return ada_to_fixed_value (arg1);
 }
 
+bool
+ada_var_value_operation::resolve (struct expression *exp,
+				  bool deprocedure_p,
+				  bool parse_completion,
+				  innermost_block_tracker *tracker,
+				  struct type *context_type)
+{
+  symbol *sym = std::get<0> (m_storage);
+  if (SYMBOL_DOMAIN (sym) == UNDEF_DOMAIN)
+    {
+      block_symbol resolved
+	= ada_resolve_variable (sym, std::get<1> (m_storage),
+				context_type, parse_completion,
+				deprocedure_p, tracker);
+      std::get<0> (m_storage) = resolved.symbol;
+      std::get<1> (m_storage) = resolved.block;
+    }
+
+  if (deprocedure_p
+      && SYMBOL_TYPE (std::get<0> (m_storage))->code () == TYPE_CODE_FUNC)
+    return true;
+
+  return false;
+}
+
 value *
 ada_atr_val_operation::evaluate (struct type *expect_type,
 				 struct expression *exp,
@@ -11114,6 +11139,44 @@  ada_funcall_operation::evaluate (struct type *expect_type,
     }
 }
 
+bool
+ada_funcall_operation::resolve (struct expression *exp,
+				bool deprocedure_p,
+				bool parse_completion,
+				innermost_block_tracker *tracker,
+				struct type *context_type)
+{
+  operation_up &callee_op = std::get<0> (m_storage);
+
+  ada_var_value_operation *avv
+    = dynamic_cast<ada_var_value_operation *> (callee_op.get ());
+  if (avv == nullptr)
+    return false;
+
+  symbol *sym = avv->get_symbol ();
+  if (SYMBOL_DOMAIN (sym) != UNDEF_DOMAIN)
+    return false;
+
+  const std::vector<operation_up> &args_up = std::get<1> (m_storage);
+  int nargs = args_up.size ();
+  std::vector<value *> argvec (nargs);
+
+  for (int i = 0; i < args_up.size (); ++i)
+    argvec[i] = args_up[i]->evaluate (nullptr, exp, EVAL_AVOID_SIDE_EFFECTS);
+
+  const block *block = avv->get_block ();
+  block_symbol resolved
+    = ada_resolve_funcall (sym, block,
+			   context_type, parse_completion,
+			   nargs, argvec.data (),
+			   tracker);
+
+  std::get<0> (m_storage)
+    = make_operation<ada_var_value_operation> (resolved.symbol,
+					       resolved.block);
+  return false;
+}
+
 }
 
 /* Implement the evaluate_exp routine in the exp_descriptor structure