Branch data Line data Source code
1 : : // Copyright (C) 2020-2024 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 : : #ifndef RUST_AST_LOWER_BLOCK
20 : : #define RUST_AST_LOWER_BLOCK
21 : :
22 : : #include "rust-diagnostics.h"
23 : : #include "rust-ast-lower-base.h"
24 : :
25 : : namespace Rust {
26 : : namespace HIR {
27 : :
28 : 15263 : class ASTLoweringBlock : public ASTLoweringBase
29 : : {
30 : : using Rust::HIR::ASTLoweringBase::visit;
31 : :
32 : : public:
33 : 12784 : static HIR::BlockExpr *translate (AST::BlockExpr *expr, bool *terminated)
34 : : {
35 : 25568 : ASTLoweringBlock resolver;
36 : 12784 : expr->normalize_tail_expr ();
37 : 12784 : expr->accept_vis (resolver);
38 : 12784 : if (resolver.translated != nullptr)
39 : : {
40 : 12784 : resolver.mappings->insert_hir_expr (resolver.translated);
41 : : }
42 : :
43 : 12784 : *terminated = resolver.terminated;
44 : 12784 : return resolver.translated;
45 : 12784 : }
46 : :
47 : 2479 : static HIR::UnsafeBlockExpr *translate (AST::UnsafeBlockExpr *expr,
48 : : bool *terminated)
49 : : {
50 : 4958 : ASTLoweringBlock resolver;
51 : :
52 : 2479 : HIR::BlockExpr *block
53 : 2479 : = ASTLoweringBlock::translate (expr->get_block_expr ().get (),
54 : : terminated);
55 : 2479 : auto crate_num = resolver.mappings->get_current_crate ();
56 : 4958 : Analysis::NodeMapping mapping (crate_num, expr->get_node_id (),
57 : : resolver.mappings->get_next_hir_id (
58 : : crate_num),
59 : 2479 : UNKNOWN_LOCAL_DEFID);
60 : :
61 : 2479 : HIR::UnsafeBlockExpr *translated
62 : : = new HIR::UnsafeBlockExpr (mapping,
63 : 2479 : std::unique_ptr<HIR::BlockExpr> (block),
64 : 2479 : expr->get_outer_attrs (), expr->get_locus ());
65 : :
66 : 2479 : resolver.mappings->insert_hir_expr (translated);
67 : :
68 : 2479 : return translated;
69 : 2479 : }
70 : :
71 : : void visit (AST::BlockExpr &expr) override;
72 : :
73 : : private:
74 : 15263 : ASTLoweringBlock ()
75 : 15263 : : ASTLoweringBase (), translated (nullptr), terminated (false)
76 : : {}
77 : :
78 : : HIR::BlockExpr *translated;
79 : : bool terminated;
80 : : };
81 : :
82 : : class ASTLoweringIfBlock : public ASTLoweringBase
83 : : {
84 : : using Rust::HIR::ASTLoweringBase::visit;
85 : :
86 : : public:
87 : 647 : static HIR::IfExpr *translate (AST::IfExpr *expr, bool *terminated)
88 : : {
89 : 1294 : ASTLoweringIfBlock resolver;
90 : 647 : expr->accept_vis (resolver);
91 : 647 : if (resolver.translated != nullptr)
92 : : {
93 : 647 : resolver.mappings->insert_hir_expr (resolver.translated);
94 : : }
95 : 647 : *terminated = resolver.terminated;
96 : 647 : return resolver.translated;
97 : 647 : }
98 : :
99 : 647 : ~ASTLoweringIfBlock () {}
100 : :
101 : : void visit (AST::IfExpr &expr) override;
102 : :
103 : : void visit (AST::IfExprConseqElse &expr) override;
104 : :
105 : : private:
106 : 647 : ASTLoweringIfBlock ()
107 : 647 : : ASTLoweringBase (), translated (nullptr), terminated (false)
108 : : {}
109 : :
110 : : HIR::IfExpr *translated;
111 : : bool terminated;
112 : : };
113 : :
114 : : class ASTLoweringIfLetBlock : public ASTLoweringBase
115 : : {
116 : : using Rust::HIR::ASTLoweringBase::visit;
117 : :
118 : : public:
119 : 2 : static HIR::IfLetExpr *translate (AST::IfLetExpr *expr)
120 : : {
121 : 4 : ASTLoweringIfLetBlock resolver;
122 : 2 : expr->accept_vis (resolver);
123 : 2 : if (resolver.translated != nullptr)
124 : : {
125 : 2 : resolver.mappings->insert_hir_expr (resolver.translated);
126 : : }
127 : 2 : return resolver.translated;
128 : 2 : }
129 : :
130 : 2 : ~ASTLoweringIfLetBlock () {}
131 : :
132 : : void visit (AST::IfLetExpr &expr) override;
133 : :
134 : : void visit (AST::IfLetExprConseqElse &expr) override;
135 : :
136 : : private:
137 : 2 : ASTLoweringIfLetBlock () : ASTLoweringBase (), translated (nullptr) {}
138 : :
139 : : HIR::IfLetExpr *translated;
140 : : };
141 : :
142 : : class ASTLoweringExprWithBlock : public ASTLoweringBase
143 : : {
144 : : using Rust::HIR::ASTLoweringBase::visit;
145 : :
146 : : public:
147 : 667 : static HIR::ExprWithBlock *translate (AST::ExprWithBlock *expr,
148 : : bool *terminated)
149 : : {
150 : 1334 : ASTLoweringExprWithBlock resolver;
151 : 667 : expr->accept_vis (resolver);
152 : 667 : if (resolver.translated != nullptr)
153 : : {
154 : 667 : resolver.mappings->insert_hir_expr (resolver.translated);
155 : : }
156 : :
157 : 667 : *terminated = resolver.terminated;
158 : 667 : return resolver.translated;
159 : 667 : }
160 : :
161 : 667 : ~ASTLoweringExprWithBlock () {}
162 : :
163 : 9 : void visit (AST::IfExpr &expr) override
164 : : {
165 : 9 : translated = ASTLoweringIfBlock::translate (&expr, &terminated);
166 : 9 : }
167 : :
168 : 30 : void visit (AST::IfExprConseqElse &expr) override
169 : : {
170 : 30 : translated = ASTLoweringIfBlock::translate (&expr, &terminated);
171 : 30 : }
172 : :
173 : 0 : void visit (AST::IfLetExpr &expr) override
174 : : {
175 : 0 : translated = ASTLoweringIfLetBlock::translate (&expr);
176 : 0 : }
177 : :
178 : 1 : void visit (AST::IfLetExprConseqElse &expr) override
179 : : {
180 : 1 : translated = ASTLoweringIfLetBlock::translate (&expr);
181 : 1 : }
182 : :
183 : 309 : void visit (AST::BlockExpr &expr) override
184 : : {
185 : 309 : translated = ASTLoweringBlock::translate (&expr, &terminated);
186 : 309 : }
187 : :
188 : 0 : void visit (AST::UnsafeBlockExpr &expr) override
189 : : {
190 : 0 : translated = ASTLoweringBlock::translate (&expr, &terminated);
191 : 0 : }
192 : :
193 : 88 : void visit (AST::LoopExpr &expr) override
194 : : {
195 : 88 : HIR::BlockExpr *loop_block
196 : 88 : = ASTLoweringBlock::translate (expr.get_loop_block ().get (),
197 : : &terminated);
198 : :
199 : 88 : HIR::LoopLabel loop_label = lower_loop_label (expr.get_loop_label ());
200 : :
201 : 88 : auto crate_num = mappings->get_current_crate ();
202 : 176 : Analysis::NodeMapping mapping (crate_num, expr.get_node_id (),
203 : 88 : mappings->get_next_hir_id (crate_num),
204 : 88 : UNKNOWN_LOCAL_DEFID);
205 : :
206 : 88 : translated
207 : 88 : = new HIR::LoopExpr (mapping,
208 : 88 : std::unique_ptr<HIR::BlockExpr> (loop_block),
209 : : expr.get_locus (), std::move (loop_label),
210 : 176 : expr.get_outer_attrs ());
211 : 88 : }
212 : :
213 : : void visit (AST::WhileLoopExpr &expr) override;
214 : :
215 : : void visit (AST::ForLoopExpr &expr) override;
216 : :
217 : : void visit (AST::MatchExpr &expr) override;
218 : :
219 : : private:
220 : 667 : ASTLoweringExprWithBlock ()
221 : 667 : : ASTLoweringBase (), translated (nullptr), terminated (false)
222 : : {}
223 : :
224 : : HIR::ExprWithBlock *translated;
225 : : bool terminated;
226 : : };
227 : :
228 : : } // namespace HIR
229 : : } // namespace Rust
230 : :
231 : : #endif // RUST_AST_LOWER_BLOCK
|