From 128c042dcf36f28bbff612d30f1627a5c5b09a9a Mon Sep 17 00:00:00 2001 From: Pierre-Emmanuel Patry Date: Mon, 12 Jun 2023 16:36:51 +0200 Subject: [PATCH] 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 --- 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 2791a8ce8452..8ea2c8c5a8b6 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 &get_outer_attrs () const { return outer_attrs; } std::vector &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 6df9f7cb4e65..d79aeac440b6 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> &stmts) + AST::BlockExpr &expr, std::vector> &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 (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 630b60175945..e7e303b44b4f 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> &values); - void expand_inner_stmts (std::vector> &values); + void expand_inner_stmts (AST::BlockExpr &expr, + std::vector> &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 -- 2.47.2