[committed,046/103] gccrs: Add name resolution for closures

Message ID 20230221120230.596966-47-arthur.cohen@embecosm.com
State Unresolved
Headers
Series [committed,001/103] gccrs: Fix missing dead code analysis ICE on local enum definition |

Checks

Context Check Description
snail/gcc-patch-check warning Git am fail log

Commit Message

Arthur Cohen Feb. 21, 2023, 12:01 p.m. UTC
  From: Philip Herron <philip.herron@embecosm.com>

gcc/rust/ChangeLog:

	* resolve/rust-ast-resolve-expr.cc (ResolveExpr::visit): Visit closure properly when
	name resolving.
	(ResolveExpr::resolve_closure_param): Implement closure name resolving.
	* resolve/rust-ast-resolve-expr.h: Declare visitors for closure types.
---
 gcc/rust/resolve/rust-ast-resolve-expr.cc | 58 +++++++++++++++++++++++
 gcc/rust/resolve/rust-ast-resolve-expr.h  |  5 ++
 2 files changed, 63 insertions(+)
  

Patch

diff --git a/gcc/rust/resolve/rust-ast-resolve-expr.cc b/gcc/rust/resolve/rust-ast-resolve-expr.cc
index 40d4af09444..8ece5b63b6d 100644
--- a/gcc/rust/resolve/rust-ast-resolve-expr.cc
+++ b/gcc/rust/resolve/rust-ast-resolve-expr.cc
@@ -565,6 +565,64 @@  ResolveExpr::visit (AST::RangeFromToInclExpr &expr)
   ResolveExpr::go (expr.get_to_expr ().get (), prefix, canonical_prefix);
 }
 
+void
+ResolveExpr::visit (AST::ClosureExprInner &expr)
+{
+  NodeId scope_node_id = expr.get_node_id ();
+  resolver->get_name_scope ().push (scope_node_id);
+  resolver->get_type_scope ().push (scope_node_id);
+  resolver->get_label_scope ().push (scope_node_id);
+  resolver->push_new_name_rib (resolver->get_name_scope ().peek ());
+  resolver->push_new_type_rib (resolver->get_type_scope ().peek ());
+  resolver->push_new_label_rib (resolver->get_type_scope ().peek ());
+
+  for (auto &p : expr.get_params ())
+    {
+      resolve_closure_param (p);
+    }
+
+  ResolveExpr::go (expr.get_definition_expr ().get (), prefix,
+		   canonical_prefix);
+
+  resolver->get_name_scope ().pop ();
+  resolver->get_type_scope ().pop ();
+  resolver->get_label_scope ().pop ();
+}
+
+void
+ResolveExpr::visit (AST::ClosureExprInnerTyped &expr)
+{
+  NodeId scope_node_id = expr.get_node_id ();
+  resolver->get_name_scope ().push (scope_node_id);
+  resolver->get_type_scope ().push (scope_node_id);
+  resolver->get_label_scope ().push (scope_node_id);
+  resolver->push_new_name_rib (resolver->get_name_scope ().peek ());
+  resolver->push_new_type_rib (resolver->get_type_scope ().peek ());
+  resolver->push_new_label_rib (resolver->get_type_scope ().peek ());
+
+  for (auto &p : expr.get_params ())
+    {
+      resolve_closure_param (p);
+    }
+
+  ResolveType::go (expr.get_return_type ().get ());
+  ResolveExpr::go (expr.get_definition_block ().get (), prefix,
+		   canonical_prefix);
+
+  resolver->get_name_scope ().pop ();
+  resolver->get_type_scope ().pop ();
+  resolver->get_label_scope ().pop ();
+}
+
+void
+ResolveExpr::resolve_closure_param (AST::ClosureParam &param)
+{
+  PatternDeclaration::go (param.get_pattern ().get ());
+
+  if (param.has_type_given ())
+    ResolveType::go (param.get_type ().get ());
+}
+
 ResolveExpr::ResolveExpr (const CanonicalPath &prefix,
 			  const CanonicalPath &canonical_prefix)
   : ResolverBase (), prefix (prefix), canonical_prefix (canonical_prefix)
diff --git a/gcc/rust/resolve/rust-ast-resolve-expr.h b/gcc/rust/resolve/rust-ast-resolve-expr.h
index 60ae1720811..9e3e7750119 100644
--- a/gcc/rust/resolve/rust-ast-resolve-expr.h
+++ b/gcc/rust/resolve/rust-ast-resolve-expr.h
@@ -75,6 +75,11 @@  public:
   void visit (AST::RangeToExpr &expr) override;
   void visit (AST::RangeFullExpr &expr) override;
   void visit (AST::RangeFromToInclExpr &expr) override;
+  void visit (AST::ClosureExprInner &expr) override;
+  void visit (AST::ClosureExprInnerTyped &expr) override;
+
+protected:
+  void resolve_closure_param (AST::ClosureParam &param);
 
 private:
   ResolveExpr (const CanonicalPath &prefix,