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