public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r14-7688] gccrs: expand: Convert last statement to tail expr
@ 2024-01-16 17:53 Arthur Cohen
  0 siblings, 0 replies; only message in thread
From: Arthur Cohen @ 2024-01-16 17:53 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:128c042dcf36f28bbff612d30f1627a5c5b09a9a

commit r14-7688-g128c042dcf36f28bbff612d30f1627a5c5b09a9a
Author: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
Date:   Mon Jun 12 16:36:51 2023 +0200

    gccrs: expand: Convert last statement to tail expr
    
    Some inner statements may be expanded to statements as well as a tail
    expression, which should then be propagated to the parent tail
    expression field.
    
    gcc/rust/ChangeLog:
    
            * expand/rust-expand-visitor.cc (ExpandVisitor::visit): Change
            call to expand_inner_stmts.
            (expand_tail_expr): Change argument name and try to expand the
            last statement as a tail expression once all statements have
            been expanded.
            * expand/rust-expand-visitor.h: Change prototype to accept
            parent class.
            * ast/rust-expr.h: Add try_convert_last_stmt function prototype.
    
    Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>

Diff:
---
 gcc/rust/ast/rust-expr.h               |  2 ++
 gcc/rust/expand/rust-expand-visitor.cc | 22 +++++++++++++---------
 gcc/rust/expand/rust-expand-visitor.h  |  3 ++-
 3 files changed, 17 insertions(+), 10 deletions(-)

diff --git a/gcc/rust/ast/rust-expr.h b/gcc/rust/ast/rust-expr.h
index 2791a8ce845..8ea2c8c5a8b 100644
--- a/gcc/rust/ast/rust-expr.h
+++ b/gcc/rust/ast/rust-expr.h
@@ -2503,6 +2503,8 @@ public:
   // Normalizes a trailing statement without a semicolon to a tail expression.
   void normalize_tail_expr ();
 
+  void try_convert_last_stmt ();
+
   const std::vector<Attribute> &get_outer_attrs () const { return outer_attrs; }
   std::vector<Attribute> &get_outer_attrs () override { return outer_attrs; }
 
diff --git a/gcc/rust/expand/rust-expand-visitor.cc b/gcc/rust/expand/rust-expand-visitor.cc
index 6df9f7cb4e6..d79aeac440b 100644
--- a/gcc/rust/expand/rust-expand-visitor.cc
+++ b/gcc/rust/expand/rust-expand-visitor.cc
@@ -173,11 +173,11 @@ expand_stmt_attribute (T &item, AST::SimplePath &name, MacroExpander &expander)
 }
 
 void
-expand_tail_expr (AST::BlockExpr &item, MacroExpander &expander)
+expand_tail_expr (AST::BlockExpr &block_expr, MacroExpander &expander)
 {
-  if (item.has_tail_expr ())
+  if (block_expr.has_tail_expr ())
     {
-      auto tail = item.take_tail_expr ();
+      auto tail = block_expr.take_tail_expr ();
       auto attrs = tail->get_outer_attrs ();
       bool changed = false;
       for (auto it = attrs.begin (); it != attrs.end ();)
@@ -192,16 +192,17 @@ expand_tail_expr (AST::BlockExpr &item, MacroExpander &expander)
 	      it = attrs.erase (it);
 	      changed = true;
 	      auto new_stmts
-		= expand_stmt_attribute (item, current.get_path (), expander);
-	      auto &stmts = item.get_statements ();
+		= expand_stmt_attribute (block_expr, current.get_path (),
+					 expander);
+	      auto &stmts = block_expr.get_statements ();
 	      std::move (new_stmts.begin (), new_stmts.end (),
 			 std::inserter (stmts, stmts.end ()));
 	    }
 	}
       if (changed)
-	item.strip_tail_expr ();
+	block_expr.normalize_tail_expr ();
       else
-	item.set_tail_expr (std::move (tail));
+	block_expr.set_tail_expr (std::move (tail));
     }
 }
 
@@ -286,7 +287,7 @@ ExpandVisitor::expand_inner_items (
 
 void
 ExpandVisitor::expand_inner_stmts (
-  std::vector<std::unique_ptr<AST::Stmt>> &stmts)
+  AST::BlockExpr &expr, std::vector<std::unique_ptr<AST::Stmt>> &stmts)
 {
   expander.push_context (MacroExpander::ContextType::STMT);
 
@@ -362,6 +363,9 @@ ExpandVisitor::expand_inner_stmts (
 	}
     }
 
+  if (!expr.has_tail_expr ())
+    expr.normalize_tail_expr ();
+
   std::function<std::unique_ptr<AST::Stmt> (AST::SingleASTNode)> extractor
     = [] (AST::SingleASTNode node) { return node.take_stmt (); };
 
@@ -830,7 +834,7 @@ ExpandVisitor::visit (AST::ClosureExprInner &expr)
 void
 ExpandVisitor::visit (AST::BlockExpr &expr)
 {
-  expand_inner_stmts (expr.get_statements ());
+  expand_inner_stmts (expr, expr.get_statements ());
 
   expand_tail_expr (expr, expander);
   if (expr.has_tail_expr ())
diff --git a/gcc/rust/expand/rust-expand-visitor.h b/gcc/rust/expand/rust-expand-visitor.h
index 630b6017594..e7e303b44b4 100644
--- a/gcc/rust/expand/rust-expand-visitor.h
+++ b/gcc/rust/expand/rust-expand-visitor.h
@@ -158,7 +158,8 @@ public:
    * @param values Vector of values to mutate in-place and append into
    */
   void expand_inner_items (std::vector<std::unique_ptr<AST::Item>> &values);
-  void expand_inner_stmts (std::vector<std::unique_ptr<AST::Stmt>> &values);
+  void expand_inner_stmts (AST::BlockExpr &expr,
+			   std::vector<std::unique_ptr<AST::Stmt>> &values);
 
   // TODO: See if possible to make more specialization for Impl items, Block
   // stmts etc? This could allow us to remove expand_macro_children or at least

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2024-01-16 17:53 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-01-16 17:53 [gcc r14-7688] gccrs: expand: Convert last statement to tail expr Arthur Cohen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).