diff mbox series

[027/125] gccrs: Make DefaultResolver visit more of the AST

Message ID 20240801145809.366388-29-arthur.cohen@embecosm.com
State New
Headers show
Series [001/125] Rust: Make 'tree'-level 'MAIN_NAME_P' work | expand

Commit Message

Arthur Cohen Aug. 1, 2024, 2:56 p.m. UTC
From: Owen Avery <powerboat9.gamer@gmail.com>

gcc/rust/ChangeLog:

	* resolve/rust-default-resolver.cc
	(DefaultResolver::visit): Visit inner AST nodes of ClosureExprInner,
	ClosureExprInnerTyped, IfExpr, IfExprConseqElse, MatchExpr,
	PathInExpression, EnumItemTuple, EnumItemStruct, and
	EnumItemDiscriminant.
	* ast/rust-item.h
	(EnumItemDiscriminant::has_expr): New function.

Signed-off-by: Owen Avery <powerboat9.gamer@gmail.com>
---
 gcc/rust/ast/rust-item.h                  |   2 +
 gcc/rust/resolve/rust-default-resolver.cc | 105 ++++++++++++++++++----
 2 files changed, 91 insertions(+), 16 deletions(-)
diff mbox series

Patch

diff --git a/gcc/rust/ast/rust-item.h b/gcc/rust/ast/rust-item.h
index 0911719b716..44963ba386e 100644
--- a/gcc/rust/ast/rust-item.h
+++ b/gcc/rust/ast/rust-item.h
@@ -2081,6 +2081,8 @@  public:
 
   void accept_vis (ASTVisitor &vis) override;
 
+  bool has_expr () { return expression != nullptr; }
+
   // TODO: is this better? Or is a "vis_block" better?
   std::unique_ptr<Expr> &get_expr ()
   {
diff --git a/gcc/rust/resolve/rust-default-resolver.cc b/gcc/rust/resolve/rust-default-resolver.cc
index 9f7fda4adaa..28f04a10839 100644
--- a/gcc/rust/resolve/rust-default-resolver.cc
+++ b/gcc/rust/resolve/rust-default-resolver.cc
@@ -177,12 +177,43 @@  DefaultResolver::visit (AST::StructExprFieldIndexValue &)
 {}
 
 void
-DefaultResolver::visit (AST::ClosureExprInner &)
-{}
+DefaultResolver::visit (AST::ClosureExprInner &expr)
+{
+  if (expr.is_marked_for_strip ())
+    return;
+
+  for (auto &param : expr.get_params ())
+    {
+      if (param.is_error ())
+	continue;
+
+      param.get_pattern ()->accept_vis (*this);
+      if (param.has_type_given ())
+	param.get_type ()->accept_vis (*this);
+    }
+
+  expr.get_definition_expr ()->accept_vis (*this);
+}
 
 void
-DefaultResolver::visit (AST::ClosureExprInnerTyped &)
-{}
+DefaultResolver::visit (AST::ClosureExprInnerTyped &expr)
+{
+  if (expr.is_marked_for_strip ())
+    return;
+
+  for (auto &param : expr.get_params ())
+    {
+      if (param.is_error ())
+	continue;
+
+      param.get_pattern ()->accept_vis (*this);
+      if (param.has_type_given ())
+	param.get_type ()->accept_vis (*this);
+    }
+
+  expr.get_definition_block ()->accept_vis (*this);
+  expr.get_return_type ()->accept_vis (*this);
+}
 
 void
 DefaultResolver::visit (AST::ContinueExpr &expr)
@@ -230,11 +261,18 @@  DefaultResolver::visit (AST::WhileLetLoopExpr &expr)
 
 void
 DefaultResolver::visit (AST::IfExpr &expr)
-{}
+{
+  expr.get_condition_expr ()->accept_vis (*this);
+  expr.get_if_block ()->accept_vis (*this);
+}
 
 void
-DefaultResolver::visit (AST::IfExprConseqElse &)
-{}
+DefaultResolver::visit (AST::IfExprConseqElse &expr)
+{
+  expr.get_condition_expr ()->accept_vis (*this);
+  expr.get_if_block ()->accept_vis (*this);
+  expr.get_else_block ()->accept_vis (*this);
+}
 
 void
 DefaultResolver::visit (AST::IfLetExpr &expr)
@@ -246,7 +284,20 @@  DefaultResolver::visit (AST::IfLetExprConseqElse &)
 
 void
 DefaultResolver::visit (AST::MatchExpr &expr)
-{}
+{
+  if (expr.is_marked_for_strip ())
+    return;
+
+  expr.get_scrutinee_expr ()->accept_vis (*this);
+  for (auto &arm : expr.get_match_cases ())
+    {
+      arm.get_expr ()->accept_vis (*this);
+      for (auto &pat : arm.get_arm ().get_patterns ())
+	pat->accept_vis (*this);
+      if (arm.get_arm ().has_match_arm_guard ())
+	arm.get_arm ().get_guard_expr ()->accept_vis (*this);
+    }
+}
 
 void
 DefaultResolver::visit (AST::AwaitExpr &expr)
@@ -277,8 +328,21 @@  DefaultResolver::visit (AST::ConstGenericParam &)
 {}
 
 void
-DefaultResolver::visit (AST::PathInExpression &)
-{}
+DefaultResolver::visit (AST::PathInExpression &expr)
+{
+  for (auto &seg : expr.get_segments ())
+    if (seg.has_generic_args ())
+      {
+	auto &args = seg.get_generic_args ();
+	for (auto &arg : args.get_generic_args ())
+	  arg.accept_vis (*this);
+	for (auto &arg : args.get_binding_args ())
+	  if (!arg.is_error ())
+	    arg.get_type ()->accept_vis (*this);
+	for (auto &arg : args.get_lifetime_args ())
+	  arg.accept_vis (*this);
+      }
+}
 
 void
 DefaultResolver::visit (AST::TypePathSegmentGeneric &)
@@ -373,16 +437,25 @@  DefaultResolver::visit (AST::EnumItem &)
 {}
 
 void
-DefaultResolver::visit (AST::EnumItemTuple &)
-{}
+DefaultResolver::visit (AST::EnumItemTuple &item)
+{
+  for (auto &field : item.get_tuple_fields ())
+    field.get_field_type ()->accept_vis (*this);
+}
 
 void
-DefaultResolver::visit (AST::EnumItemStruct &)
-{}
+DefaultResolver::visit (AST::EnumItemStruct &item)
+{
+  for (auto &field : item.get_struct_fields ())
+    field.get_field_type ()->accept_vis (*this);
+}
 
 void
-DefaultResolver::visit (AST::EnumItemDiscriminant &)
-{}
+DefaultResolver::visit (AST::EnumItemDiscriminant &item)
+{
+  if (item.has_expr ())
+    item.get_expr ()->accept_vis (*this);
+}
 
 void
 DefaultResolver::visit (AST::ConstantItem &item)