]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: expand: Convert last statement to tail expr
authorPierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
Mon, 12 Jun 2023 14:36:51 +0000 (16:36 +0200)
committerArthur Cohen <arthur.cohen@embecosm.com>
Tue, 16 Jan 2024 17:46:29 +0000 (18:46 +0100)
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>
gcc/rust/ast/rust-expr.h
gcc/rust/expand/rust-expand-visitor.cc
gcc/rust/expand/rust-expand-visitor.h

index 2791a8ce8452b6d6b570fbc381252f99862ab36e..8ea2c8c5a8b6dcf3b5059194fabaee0839fdd21f 100644 (file)
@@ -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; }
 
index 6df9f7cb4e65ae4dcac3f1430ca4317f94197086..d79aeac440b63d1f40e974c13716ed7ec88d4ad2 100644 (file)
@@ -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 ())
index 630b601759450b927bb19fc58389fe20a665b333..e7e303b44b4f29f32ac9c5bf8d226c7284975ea3 100644 (file)
@@ -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