| 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-for-loops.h" |
| 20 |
#include "rust-ast-visitor.h" |
| 21 |
#include "rust-ast.h" |
| 22 |
#include "rust-hir-map.h" |
| 23 |
#include "rust-path.h" |
| 24 |
#include "rust-pattern.h" |
| 25 |
#include "rust-stmt.h" |
| 26 |
#include "rust-expr.h" |
| 27 |
#include "rust-ast-builder.h" |
| 28 |
|
| 29 |
namespace Rust { |
| 30 |
namespace AST { |
| 31 |
|
| 32 |
DesugarForLoops::DesugarForLoops () {} |
| 33 |
|
| 34 |
void |
| 35 |
DesugarForLoops::go (AST::Crate &crate) |
| 36 |
{ |
| 37 |
DefaultASTVisitor::visit (crate); |
| 38 |
} |
| 39 |
|
| 40 |
static void |
| 41 |
replace_for_loop (std::unique_ptr<Expr> &for_loop, |
| 42 |
std::unique_ptr<Expr> &&expanded) |
| 43 |
{ |
| 44 |
for_loop = std::move (expanded); |
| 45 |
} |
| 46 |
|
| 47 |
MatchArm |
| 48 |
DesugarForLoops::DesugarCtx::make_match_arm (std::unique_ptr<Pattern> &&path) |
| 49 |
{ |
| 50 |
auto patterns = std::vector<std::unique_ptr<Pattern>> (); |
| 51 |
patterns.emplace_back (std::move (path)); |
| 52 |
|
| 53 |
return MatchArm (std::move (patterns), loc); |
| 54 |
} |
| 55 |
|
| 56 |
MatchCase |
| 57 |
DesugarForLoops::DesugarCtx::make_break_arm () |
| 58 |
{ |
| 59 |
auto arm = make_match_arm (std::unique_ptr<Pattern> (new PathInExpression ( |
| 60 |
builder.path_in_expression (LangItem::Kind::OPTION_NONE)))); |
| 61 |
|
| 62 |
auto break_expr |
| 63 |
= std::unique_ptr<Expr> (new BreakExpr (tl::nullopt, nullptr, {}, loc)); |
| 64 |
|
| 65 |
return MatchCase (std::move (arm), std::move (break_expr)); |
| 66 |
} |
| 67 |
|
| 68 |
MatchCase |
| 69 |
DesugarForLoops::DesugarCtx::make_continue_arm () |
| 70 |
{ |
| 71 |
auto val = builder.identifier_pattern (DesugarCtx::continue_pattern_id); |
| 72 |
|
| 73 |
auto patterns = std::vector<std::unique_ptr<Pattern>> (); |
| 74 |
patterns.emplace_back (std::move (val)); |
| 75 |
|
| 76 |
auto pattern_item = std::unique_ptr<TupleStructItems> ( |
| 77 |
new TupleStructItemsNoRange (std::move (patterns))); |
| 78 |
auto pattern = std::unique_ptr<Pattern> (new TupleStructPattern ( |
| 79 |
builder.path_in_expression (LangItem::Kind::OPTION_SOME), |
| 80 |
std::move (pattern_item))); |
| 81 |
|
| 82 |
auto val_arm = make_match_arm (std::move (pattern)); |
| 83 |
|
| 84 |
auto next = builder.identifier (DesugarCtx::next_value_id); |
| 85 |
|
| 86 |
auto assignment = std::unique_ptr<Expr> ( |
| 87 |
new AssignmentExpr (std::move (next), |
| 88 |
builder.identifier (DesugarCtx::continue_pattern_id), |
| 89 |
{}, loc)); |
| 90 |
|
| 91 |
return MatchCase (std::move (val_arm), std::move (assignment)); |
| 92 |
} |
| 93 |
|
| 94 |
std::unique_ptr<Stmt> |
| 95 |
DesugarForLoops::DesugarCtx::statementify (std::unique_ptr<Expr> &&expr) |
| 96 |
{ |
| 97 |
return std::unique_ptr<Stmt> (new ExprStmt (std::move (expr), loc, true)); |
| 98 |
} |
| 99 |
|
| 100 |
std::unique_ptr<Expr> |
| 101 |
DesugarForLoops::desugar (AST::ForLoopExpr &expr) |
| 102 |
{ |
| 103 |
auto ctx = DesugarCtx (expr.get_locus ()); |
| 104 |
|
| 105 |
auto into_iter = std::make_unique<PathInExpression> ( |
| 106 |
ctx.builder.path_in_expression (LangItem::Kind::INTOITER_INTOITER)); |
| 107 |
auto next = std::make_unique<PathInExpression> ( |
| 108 |
ctx.builder.path_in_expression (LangItem::Kind::ITERATOR_NEXT)); |
| 109 |
|
| 110 |
// IntoIterator::into_iter(<head>) |
| 111 |
auto into_iter_call |
| 112 |
= ctx.builder.call (std::move (into_iter), |
| 113 |
expr.get_iterator_expr ().clone_expr ()); |
| 114 |
|
| 115 |
// Iterator::next(iter) |
| 116 |
auto next_call = ctx.builder.call ( |
| 117 |
std::move (next), |
| 118 |
ctx.builder.ref (ctx.builder.identifier (DesugarCtx::iter_id), true)); |
| 119 |
|
| 120 |
// None => break, |
| 121 |
auto break_arm = ctx.make_break_arm (); |
| 122 |
// Some(val) => __next = val; }, |
| 123 |
auto continue_arm = ctx.make_continue_arm (); |
| 124 |
|
| 125 |
// match <next_call> { |
| 126 |
// <continue_arm> |
| 127 |
// <break_arm> |
| 128 |
// } |
| 129 |
auto match_next |
| 130 |
= ctx.builder.match (std::move (next_call), |
| 131 |
{std::move (continue_arm), std::move (break_arm)}); |
| 132 |
|
| 133 |
// let mut __next; |
| 134 |
auto let_next = ctx.builder.let ( |
| 135 |
ctx.builder.identifier_pattern (DesugarCtx::next_value_id, true)); |
| 136 |
// let <pattern> = __next; |
| 137 |
auto let_pat |
| 138 |
= ctx.builder.let (expr.get_pattern ().clone_pattern (), nullptr, |
| 139 |
ctx.builder.identifier (DesugarCtx::next_value_id)); |
| 140 |
|
| 141 |
auto loop_stmts = std::vector<std::unique_ptr<Stmt>> (); |
| 142 |
loop_stmts.emplace_back (std::move (let_next)); |
| 143 |
loop_stmts.emplace_back (ctx.statementify (std::move (match_next))); |
| 144 |
loop_stmts.emplace_back (std::move (let_pat)); |
| 145 |
loop_stmts.emplace_back ( |
| 146 |
ctx.statementify (expr.get_loop_block ().clone_expr ())); |
| 147 |
|
| 148 |
// loop { |
| 149 |
// <let_next>; |
| 150 |
// <match_next>; |
| 151 |
// <let_pat>; |
| 152 |
// |
| 153 |
// <body>; |
| 154 |
// } |
| 155 |
auto loop = ctx.builder.loop (std::move (loop_stmts)); |
| 156 |
|
| 157 |
auto mut_iter_pattern |
| 158 |
= ctx.builder.identifier_pattern (DesugarCtx::iter_id, true); |
| 159 |
auto match_iter |
| 160 |
= ctx.builder.match (std::move (into_iter_call), |
| 161 |
{ctx.builder.match_case (std::move (mut_iter_pattern), |
| 162 |
std::move (loop))}); |
| 163 |
|
| 164 |
auto let_result |
| 165 |
= ctx.builder.let (ctx.builder.identifier_pattern (DesugarCtx::result_id), |
| 166 |
nullptr, std::move (match_iter)); |
| 167 |
auto result_return = ctx.builder.identifier (DesugarCtx::result_id); |
| 168 |
|
| 169 |
return ctx.builder.block (std::move (let_result), std::move (result_return)); |
| 170 |
} |
| 171 |
|
| 172 |
void |
| 173 |
DesugarForLoops::maybe_desugar_expr (std::unique_ptr<Expr> &expr) |
| 174 |
{ |
| 175 |
if (expr->get_expr_kind () == AST::Expr::Kind::Loop) |
| 176 |
{ |
| 177 |
auto &loop = static_cast<AST::BaseLoopExpr &> (*expr); |
| 178 |
|
| 179 |
if (loop.get_loop_kind () == AST::BaseLoopExpr::Kind::For) |
| 180 |
{ |
| 181 |
auto &for_loop = static_cast<AST::ForLoopExpr &> (loop); |
| 182 |
|
| 183 |
auto desugared = desugar (for_loop); |
| 184 |
|
| 185 |
replace_for_loop (expr, std::move (desugared)); |
| 186 |
} |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
void |
| 191 |
DesugarForLoops::visit (AST::BlockExpr &block) |
| 192 |
{ |
| 193 |
for (auto &stmt : block.get_statements ()) |
| 194 |
if (stmt->get_stmt_kind () == AST::Stmt::Kind::Expr) |
| 195 |
maybe_desugar_expr (static_cast<AST::ExprStmt &> (*stmt).get_expr_ptr ()); |
| 196 |
|
| 197 |
if (block.has_tail_expr ()) |
| 198 |
maybe_desugar_expr (block.get_tail_expr_ptr ()); |
| 199 |
|
| 200 |
DefaultASTVisitor::visit (block); |
| 201 |
} |
| 202 |
|
| 203 |
} // namespace AST |
| 204 |
} // namespace Rust |