From: Arthur Cohen Date: Thu, 30 Jan 2025 13:56:37 +0000 (+0100) Subject: gccrs: ast-builder: Improve function generation. X-Git-Tag: basepoints/gcc-16~743 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=0897e2bcaf047cb52b65c151bd0cc7fe29b3279f;p=thirdparty%2Fgcc.git 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 --git a/gcc/rust/ast/rust-ast-builder.cc b/gcc/rust/ast/rust-ast-builder.cc index 369f5a44630..f59ff19d621 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 -Builder::block (std::unique_ptr &&stmt, +std::unique_ptr +Builder::block (tl::optional> &&stmt, std::unique_ptr &&tail_expr) const { auto stmts = std::vector> (); - 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 +std::unique_ptr Builder::block (std::vector> &&stmts, std::unique_ptr &&tail_expr) const { - return std::unique_ptr (new BlockExpr (std::move (stmts), - std::move (tail_expr), {}, {}, - LoopLabel::error (), loc, loc)); + return std::unique_ptr ( + new BlockExpr (std::move (stmts), std::move (tail_expr), {}, {}, + LoopLabel::error (), loc, loc)); } std::unique_ptr @@ -421,11 +423,9 @@ Builder::match_case (std::unique_ptr &&pattern, std::unique_ptr Builder::loop (std::vector> &&stmts) { - auto block = std::unique_ptr ( - new BlockExpr (std::move (stmts), nullptr, {}, {}, LoopLabel::error (), loc, - loc)); + auto block_expr = block (std::move (stmts), nullptr); - return std::unique_ptr (new LoopExpr (std::move (block), loc)); + return std::unique_ptr (new LoopExpr (std::move (block_expr), loc)); } std::unique_ptr diff --git a/gcc/rust/ast/rust-ast-builder.h b/gcc/rust/ast/rust-ast-builder.h index 99ab1610ce7..9fbcea182be 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 deref (std::unique_ptr &&of) const; /* Create a block with an optional tail expression */ - std::unique_ptr block (std::vector> &&stmts, - std::unique_ptr &&tail_expr - = nullptr) const; - std::unique_ptr block (std::unique_ptr &&stmt, - std::unique_ptr &&tail_expr - = nullptr) const; + std::unique_ptr block (std::vector> &&stmts, + std::unique_ptr &&tail_expr + = nullptr) const; + std::unique_ptr block (tl::optional> &&stmt, + std::unique_ptr &&tail_expr + = nullptr) const; /* Create an early return expression with an optional expression */ std::unique_ptr return_expr (std::unique_ptr &&to_return