https://gcc.gnu.org/g:0897e2bcaf047cb52b65c151bd0cc7fe29b3279f
commit r15-8810-g0897e2bcaf047cb52b65c151bd0cc7fe29b3279f Author: Arthur Cohen <arthur.co...@embecosm.com> Date: Thu Jan 30 14:56:37 2025 +0100 gccrs: ast-builder: Improve function generation. gcc/rust/ChangeLog: * ast/rust-ast-builder.cc (Builder::block): Change return type. (Builder::loop): Use new APIs. * ast/rust-ast-builder.h: Change return type of block functions. Diff: --- gcc/rust/ast/rust-ast-builder.cc | 22 +++++++++++----------- gcc/rust/ast/rust-ast-builder.h | 12 ++++++------ 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/gcc/rust/ast/rust-ast-builder.cc b/gcc/rust/ast/rust-ast-builder.cc index 369f5a44630b..f59ff19d6215 100644 --- a/gcc/rust/ast/rust-ast-builder.cc +++ b/gcc/rust/ast/rust-ast-builder.cc @@ -279,23 +279,25 @@ Builder::path_in_expression (LangItem::Kind lang_item) const return PathInExpression (lang_item, {}, loc); } -std::unique_ptr<Expr> -Builder::block (std::unique_ptr<Stmt> &&stmt, +std::unique_ptr<BlockExpr> +Builder::block (tl::optional<std::unique_ptr<Stmt>> &&stmt, std::unique_ptr<Expr> &&tail_expr) const { auto stmts = std::vector<std::unique_ptr<Stmt>> (); - stmts.emplace_back (std::move (stmt)); + + if (stmt) + stmts.emplace_back (std::move (*stmt)); return block (std::move (stmts), std::move (tail_expr)); } -std::unique_ptr<Expr> +std::unique_ptr<BlockExpr> Builder::block (std::vector<std::unique_ptr<Stmt>> &&stmts, std::unique_ptr<Expr> &&tail_expr) const { - return std::unique_ptr<Expr> (new BlockExpr (std::move (stmts), - std::move (tail_expr), {}, {}, - LoopLabel::error (), loc, loc)); + return std::unique_ptr<BlockExpr> ( + new BlockExpr (std::move (stmts), std::move (tail_expr), {}, {}, + LoopLabel::error (), loc, loc)); } std::unique_ptr<Expr> @@ -421,11 +423,9 @@ Builder::match_case (std::unique_ptr<Pattern> &&pattern, std::unique_ptr<Expr> Builder::loop (std::vector<std::unique_ptr<Stmt>> &&stmts) { - auto block = std::unique_ptr<BlockExpr> ( - new BlockExpr (std::move (stmts), nullptr, {}, {}, LoopLabel::error (), loc, - loc)); + auto block_expr = block (std::move (stmts), nullptr); - return std::unique_ptr<Expr> (new LoopExpr (std::move (block), loc)); + return std::unique_ptr<Expr> (new LoopExpr (std::move (block_expr), loc)); } std::unique_ptr<TypeParamBound> diff --git a/gcc/rust/ast/rust-ast-builder.h b/gcc/rust/ast/rust-ast-builder.h index 99ab1610ce7a..9fbcea182be6 100644 --- a/gcc/rust/ast/rust-ast-builder.h +++ b/gcc/rust/ast/rust-ast-builder.h @@ -82,12 +82,12 @@ public: std::unique_ptr<Expr> deref (std::unique_ptr<Expr> &&of) const; /* Create a block with an optional tail expression */ - std::unique_ptr<Expr> block (std::vector<std::unique_ptr<Stmt>> &&stmts, - std::unique_ptr<Expr> &&tail_expr - = nullptr) const; - std::unique_ptr<Expr> block (std::unique_ptr<Stmt> &&stmt, - std::unique_ptr<Expr> &&tail_expr - = nullptr) const; + std::unique_ptr<BlockExpr> block (std::vector<std::unique_ptr<Stmt>> &&stmts, + std::unique_ptr<Expr> &&tail_expr + = nullptr) const; + std::unique_ptr<BlockExpr> block (tl::optional<std::unique_ptr<Stmt>> &&stmt, + std::unique_ptr<Expr> &&tail_expr + = nullptr) const; /* Create an early return expression with an optional expression */ std::unique_ptr<Expr> return_expr (std::unique_ptr<Expr> &&to_return