|
| 1 | +// Copyright (C) 2025 Free Software Foundation, Inc. |
| 2 | + |
| 3 | +// This file is part of GCC. |
| 4 | + |
| 5 | +// GCC is free software; you can redistribute it and/or modify it under |
| 6 | +// the terms of the GNU General Public License as published by the Free |
| 7 | +// Software Foundation; either version 3, or (at your option) any later |
| 8 | +// version. |
| 9 | + |
| 10 | +// GCC is distributed in the hope that it will be useful, but WITHOUT ANY |
| 11 | +// WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 12 | +// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 13 | +// for more details. |
| 14 | + |
| 15 | +// You should have received a copy of the GNU General Public License |
| 16 | +// along with GCC; see the file COPYING3. If not see |
| 17 | +// <http://www.gnu.org/licenses/>. |
| 18 | + |
| 19 | +#include "rust-desugar-while-let.h" |
| 20 | +#include "rust-ast.h" |
| 21 | +#include "rust-hir-map.h" |
| 22 | +#include "rust-path.h" |
| 23 | +#include "rust-pattern.h" |
| 24 | +#include "rust-stmt.h" |
| 25 | +#include "rust-expr.h" |
| 26 | +#include "rust-ast-builder.h" |
| 27 | + |
| 28 | +namespace Rust { |
| 29 | +namespace AST { |
| 30 | + |
| 31 | +DesugarWhileLet::DesugarWhileLet () {} |
| 32 | + |
| 33 | +MatchCase |
| 34 | +DesugarWhileLet::DesugarCtx::make_break_arm () |
| 35 | +{ |
| 36 | + auto arm = builder.match_arm (builder.wildcard ()); |
| 37 | + |
| 38 | + auto break_expr |
| 39 | + = std::unique_ptr<Expr> (new BreakExpr (tl::nullopt, nullptr, {}, loc)); |
| 40 | + |
| 41 | + return MatchCase (std::move (arm), std::move (break_expr)); |
| 42 | +} |
| 43 | + |
| 44 | +MatchCase |
| 45 | +DesugarWhileLet::DesugarCtx::make_continue_arm ( |
| 46 | + std::unique_ptr<Pattern> &&pattern, std::unique_ptr<BlockExpr> &&body) |
| 47 | +{ |
| 48 | + auto arm = builder.match_arm (std::move (pattern)); |
| 49 | + |
| 50 | + return MatchCase (std::move (arm), std::move (body)); |
| 51 | +} |
| 52 | + |
| 53 | +std::unique_ptr<Expr> |
| 54 | +DesugarWhileLet::desugar (WhileLetLoopExpr &expr) |
| 55 | +{ |
| 56 | + rust_assert (expr.get_patterns ().size () == 1); |
| 57 | + |
| 58 | + auto pattern = expr.get_patterns ()[0]->clone_pattern (); |
| 59 | + auto body = expr.get_loop_block ().clone_block_expr (); |
| 60 | + auto scrutinee = expr.get_scrutinee_expr ().clone_expr (); |
| 61 | + |
| 62 | + auto ctx = DesugarCtx (expr.get_locus ()); |
| 63 | + |
| 64 | + // _ => break, |
| 65 | + auto break_arm = ctx.make_break_arm (); |
| 66 | + |
| 67 | + // <pattern> => <body>, |
| 68 | + auto continue_arm |
| 69 | + = ctx.make_continue_arm (std::move (pattern), std::move (body)); |
| 70 | + |
| 71 | + // match <scrutinee> { |
| 72 | + // <continue_arm> |
| 73 | + // <break_arm> |
| 74 | + // } |
| 75 | + auto match_expr |
| 76 | + = ctx.builder.match (std::move (scrutinee), |
| 77 | + {std::move (continue_arm), std::move (break_arm)}); |
| 78 | + |
| 79 | + auto loop_stmts = std::vector<std::unique_ptr<Stmt>> (); |
| 80 | + loop_stmts.emplace_back (ctx.builder.statementify (std::move (match_expr))); |
| 81 | + |
| 82 | + // loop { |
| 83 | + // <match_expr> |
| 84 | + // } |
| 85 | + return ctx.builder.loop (std::move (loop_stmts)); |
| 86 | +} |
| 87 | + |
| 88 | +void |
| 89 | +DesugarWhileLet::go (std::unique_ptr<Expr> &ptr) |
| 90 | +{ |
| 91 | + rust_assert (ptr->get_expr_kind () == Expr::Kind::Loop); |
| 92 | + |
| 93 | + auto &loop = static_cast<BaseLoopExpr &> (*ptr); |
| 94 | + |
| 95 | + rust_assert (loop.get_loop_kind () == BaseLoopExpr::Kind::WhileLet); |
| 96 | + |
| 97 | + auto &while_let = static_cast<WhileLetLoopExpr &> (loop); |
| 98 | + auto desugared = DesugarWhileLet ().desugar (while_let); |
| 99 | + |
| 100 | + ptr = std::move (desugared); |
| 101 | +} |
| 102 | + |
| 103 | +} // namespace AST |
| 104 | +} // namespace Rust |
0 commit comments