]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: expand: Add tail expr expansion
authorPierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
Fri, 9 Jun 2023 15:13:41 +0000 (17:13 +0200)
committerArthur Cohen <arthur.cohen@embecosm.com>
Tue, 16 Jan 2024 17:46:29 +0000 (18:46 +0100)
Tail expression may contain attribute and thus should be expanded.

gcc/rust/ChangeLog:

* ast/rust-expr.h: Add a function to take tail expr as well
as a function to set a tail expression.
* expand/rust-expand-visitor.cc (expand_tail_expr): Add tail
expression expansion function.
(ExpandVisitor::visit): Add call to tail expr expansion in for
BlockExpr.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
gcc/rust/ast/rust-expr.h
gcc/rust/expand/rust-expand-visitor.cc

index adf9b684527e46aba4a66fff80be2c89c0b01c08..2791a8ce8452b6d6b570fbc381252f99862ab36e 100644 (file)
@@ -2487,6 +2487,17 @@ public:
     return expr;
   }
 
+  std::unique_ptr<Expr> take_tail_expr ()
+  {
+    rust_assert (has_tail_expr ());
+    return std::move (expr);
+  }
+
+  void set_tail_expr (std::unique_ptr<Expr> expr)
+  {
+    this->expr = std::move (expr);
+  }
+
   // Removes the tail expression from the block.
   void strip_tail_expr () { expr = nullptr; }
   // Normalizes a trailing statement without a semicolon to a tail expression.
index e9eefdb59f11d23c64bb1c9e29fc8fcfad23449c..6df9f7cb4e65ae4dcac3f1430ca4317f94197086 100644 (file)
@@ -149,9 +149,9 @@ expand_item_attribute (AST::Item &item, AST::SimplePath &name,
   return result;
 }
 
+template <typename T>
 static std::vector<std::unique_ptr<AST::Stmt>>
-expand_stmt_attribute (AST::Item &item, AST::SimplePath &name,
-                      MacroExpander &expander)
+expand_stmt_attribute (T &item, AST::SimplePath &name, MacroExpander &expander)
 {
   std::vector<std::unique_ptr<AST::Stmt>> result;
   auto frag = expander.expand_attribute_proc_macro (item, name);
@@ -172,6 +172,39 @@ expand_stmt_attribute (AST::Item &item, AST::SimplePath &name,
   return result;
 }
 
+void
+expand_tail_expr (AST::BlockExpr &item, MacroExpander &expander)
+{
+  if (item.has_tail_expr ())
+    {
+      auto tail = item.take_tail_expr ();
+      auto attrs = tail->get_outer_attrs ();
+      bool changed = false;
+      for (auto it = attrs.begin (); it != attrs.end ();)
+       {
+         auto current = *it;
+         if (is_builtin (current))
+           {
+             it++;
+           }
+         else
+           {
+             it = attrs.erase (it);
+             changed = true;
+             auto new_stmts
+               = expand_stmt_attribute (item, current.get_path (), expander);
+             auto &stmts = item.get_statements ();
+             std::move (new_stmts.begin (), new_stmts.end (),
+                        std::inserter (stmts, stmts.end ()));
+           }
+       }
+      if (changed)
+       item.strip_tail_expr ();
+      else
+       item.set_tail_expr (std::move (tail));
+    }
+}
+
 void
 ExpandVisitor::expand_inner_items (
   std::vector<std::unique_ptr<AST::Item>> &items)
@@ -799,6 +832,7 @@ ExpandVisitor::visit (AST::BlockExpr &expr)
 {
   expand_inner_stmts (expr.get_statements ());
 
+  expand_tail_expr (expr, expander);
   if (expr.has_tail_expr ())
     maybe_expand_expr (expr.get_tail_expr ());
 }