Line data Source code
1 : // Copyright (C) 2025-2026 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 3 : DesugarWhileLet::DesugarWhileLet () {}
32 :
33 : MatchCase
34 3 : DesugarWhileLet::DesugarCtx::make_break_arm ()
35 : {
36 3 : auto arm = builder.match_arm (builder.wildcard ());
37 :
38 3 : auto break_expr
39 3 : = std::unique_ptr<Expr> (new BreakExpr (tl::nullopt, tl::nullopt, {}, loc));
40 :
41 3 : return MatchCase (std::move (arm), std::move (break_expr));
42 3 : }
43 :
44 : MatchCase
45 3 : DesugarWhileLet::DesugarCtx::make_continue_arm (
46 : std::unique_ptr<Pattern> &&pattern, std::unique_ptr<BlockExpr> &&body)
47 : {
48 3 : auto arm = builder.match_arm (std::move (pattern));
49 :
50 3 : return MatchCase (std::move (arm), std::move (body));
51 3 : }
52 :
53 : std::unique_ptr<Expr>
54 3 : DesugarWhileLet::desugar (WhileLetLoopExpr &expr)
55 : {
56 3 : rust_assert (expr.get_pattern () != nullptr);
57 :
58 3 : auto pattern = expr.get_pattern ()->clone_pattern ();
59 3 : auto body = expr.get_loop_block ().clone_block_expr ();
60 3 : auto scrutinee = expr.get_scrutinee_expr ().clone_expr ();
61 :
62 3 : auto ctx = DesugarCtx (expr.get_locus ());
63 :
64 : // _ => break,
65 3 : auto break_arm = ctx.make_break_arm ();
66 :
67 : // <pattern> => <body>,
68 3 : auto continue_arm
69 3 : = ctx.make_continue_arm (std::move (pattern), std::move (body));
70 :
71 : // match <scrutinee> {
72 : // <continue_arm>
73 : // <break_arm>
74 : // }
75 3 : auto match_expr
76 12 : = ctx.builder.match (std::move (scrutinee),
77 3 : {std::move (continue_arm), std::move (break_arm)});
78 :
79 3 : auto loop_stmts = std::vector<std::unique_ptr<Stmt>> ();
80 3 : loop_stmts.emplace_back (ctx.builder.statementify (std::move (match_expr)));
81 :
82 : // loop {
83 : // <match_expr>
84 : // }
85 3 : return ctx.builder.loop (std::move (loop_stmts));
86 9 : }
87 :
88 : void
89 3 : DesugarWhileLet::go (std::unique_ptr<Expr> &ptr)
90 : {
91 3 : rust_assert (ptr->get_expr_kind () == Expr::Kind::Loop);
92 :
93 3 : auto &loop = static_cast<BaseLoopExpr &> (*ptr);
94 :
95 3 : rust_assert (loop.get_loop_kind () == BaseLoopExpr::Kind::WhileLet);
96 :
97 3 : auto &while_let = static_cast<WhileLetLoopExpr &> (loop);
98 3 : auto desugared = DesugarWhileLet ().desugar (while_let);
99 :
100 3 : ptr = std::move (desugared);
101 3 : }
102 :
103 : } // namespace AST
104 : } // namespace Rust
|