Debian Codegraph @

gcc/rust/hir/rust-ast-lower.cc

raw
1 // Copyright (C) 2020-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-ast-lower.h"
20 #include "rust-ast-lower-item.h"
21 #include "rust-ast-lower-stmt.h"
22 #include "rust-ast-lower-expr.h"
23 #include "rust-ast-lower-block.h"
24 #include "rust-ast-lower-type.h"
25 #include "rust-ast-lower-pattern.h"
26 #include "rust-ast-lower-struct-field-expr.h"
27 #include "rust-expr.h"
28 #include "rust-hir-expr.h"
29
30 namespace Rust {
31 namespace HIR {
32 using HIR::ClosureParam;
33
34 Visibility
35 translate_visibility (const AST::Visibility &vis)
36 {
37 // FIXME: How do we create a private visibility here? Is it always private if
38 // the AST vis is an error?
39 // FIXME: We need to add a `create_private()` static function to the
40 // AST::Visibility class and use it when the vis is empty in the parser...
41 if (vis.is_error ())
42 return Visibility::create_error ();
43
44 switch (vis.get_vis_type ())
45 {
46 case AST::Visibility::PUB:
47 return Visibility (Visibility::VisType::PUBLIC);
48 case AST::Visibility::PRIV:
49 case AST::Visibility::PUB_SELF:
50 return Visibility (Visibility::VisType::PRIVATE);
51 case AST::Visibility::PUB_CRATE:
52 case AST::Visibility::PUB_SUPER:
53 case AST::Visibility::PUB_IN_PATH:
54 return Visibility (Visibility::VisType::RESTRICTED,
55 ASTLoweringSimplePath::translate (vis.get_path ()),
56 vis.get_locus ());
57 break;
58 }
59
60 return Visibility::create_error ();
61 }
62
63 ASTLowering::ASTLowering (AST::Crate &astCrate) : astCrate (astCrate) {}
64
65 ASTLowering::~ASTLowering () {}
66
67 std::unique_ptr<HIR::Crate>
68 ASTLowering::Resolve (AST::Crate &astCrate)
69 {
70 ASTLowering resolver (astCrate);
71 return resolver.go ();
72 }
73
74 std::unique_ptr<HIR::Crate>
75 ASTLowering::go ()
76 {
77 std::vector<std::unique_ptr<HIR::Item>> items;
78
79 for (auto &item : astCrate.items)
80 {
81 auto translated = ASTLoweringItem::translate (*item);
82 if (translated != nullptr)
83 items.push_back (std::unique_ptr<HIR::Item> (translated));
84 }
85
86 auto &mappings = Analysis::Mappings::get ();
87 auto crate_num = mappings.get_current_crate ();
88 Analysis::NodeMapping mapping (crate_num, astCrate.get_node_id (),
89 mappings.get_next_hir_id (crate_num),
90 UNKNOWN_LOCAL_DEFID);
91
92 return std::unique_ptr<HIR::Crate> (
93 new HIR::Crate (std::move (items), astCrate.get_inner_attrs (), mapping));
94 }
95
96 // rust-ast-lower-block.h
97 void
98 ASTLoweringBlock::visit (AST::BlockExpr &expr)
99 {
100 tl::optional<HIR::LoopLabel> label;
101 if (expr.has_label ())
102 label = lower_loop_label (expr.get_label ());
103 else
104 label = tl::nullopt;
105
106 std::vector<std::unique_ptr<HIR::Stmt>> block_stmts;
107 bool block_did_terminate = false;
108
109 for (auto &s : expr.get_statements ())
110 {
111 // FIXME: We basically need to do that check for *every* single node in
112 // the AST. this isn't realistic and this should be turned into an
113 // optional, debug-visitor instead, which goes through the entire AST and
114 // checks if any of the nodes are macro invocations
115 if (s->get_stmt_kind () == AST::Stmt::Kind::MacroInvocation)
116 rust_fatal_error (
117 s->get_locus (),
118 "macro invocations should not get lowered to HIR - At "
119 "this point in "
120 "the pipeline, they should all have been expanded");
121
122 if (block_did_terminate)
123 rust_warning_at (s->get_locus (), 0, "unreachable statement");
124
125 bool terminated = false;
126 auto translated_stmt = ASTLoweringStmt::translate (s.get (), &terminated);
127 block_did_terminate |= terminated;
128
129 if (translated_stmt)
130 block_stmts.push_back (std::unique_ptr<HIR::Stmt> (translated_stmt));
131 }
132
133 if (expr.has_tail_expr () && block_did_terminate)
134 {
135 // warning unreachable tail expressions
136 rust_warning_at (expr.get_tail_expr ().get_locus (), 0,
137 "unreachable expression");
138 }
139
140 HIR::ExprWithoutBlock *tail_expr = nullptr;
141 if (expr.has_tail_expr ())
142 {
143 bool terminated = false;
144 tail_expr = (HIR::ExprWithoutBlock *)
145 ASTLoweringExpr::translate (expr.get_tail_expr (), &terminated);
146 block_did_terminate |= terminated;
147 }
148
149 bool tail_reachable = !block_did_terminate;
150 auto crate_num = mappings.get_current_crate ();
151 Analysis::NodeMapping mapping (crate_num, expr.get_node_id (),
152 mappings.get_next_hir_id (crate_num),
153 UNKNOWN_LOCAL_DEFID);
154 translated
155 = new HIR::BlockExpr (mapping, std::move (block_stmts),
156 std::unique_ptr<HIR::ExprWithoutBlock> (tail_expr),
157 tail_reachable, expr.get_inner_attrs (),
158 expr.get_outer_attrs (), label,
159 expr.get_start_locus (), expr.get_end_locus ());
160
161 terminated = block_did_terminate;
162 }
163
164 void
165 ASTLoweringIfBlock::visit (AST::IfExpr &expr)
166 {
167 bool ignored_terminated = false;
168 HIR::Expr *condition = ASTLoweringExpr::translate (expr.get_condition_expr (),
169 &ignored_terminated);
170 HIR::BlockExpr *block
171 = ASTLoweringBlock::translate (expr.get_if_block (), &ignored_terminated);
172
173 auto crate_num = mappings.get_current_crate ();
174 Analysis::NodeMapping mapping (crate_num, expr.get_node_id (),
175 mappings.get_next_hir_id (crate_num),
176 UNKNOWN_LOCAL_DEFID);
177
178 translated = new HIR::IfExpr (mapping, std::unique_ptr<HIR::Expr> (condition),
179 std::unique_ptr<HIR::BlockExpr> (block),
180 expr.get_locus ());
181 }
182
183 void
184 ASTLoweringIfBlock::visit (AST::IfExprConseqElse &expr)
185 {
186 HIR::Expr *condition
187 = ASTLoweringExpr::translate (expr.get_condition_expr ());
188
189 bool if_block_terminated = false;
190 bool else_block_termianted = false;
191
192 HIR::BlockExpr *if_block
193 = ASTLoweringBlock::translate (expr.get_if_block (), &if_block_terminated);
194 HIR::ExprWithBlock *else_block
195 = ASTLoweringExprWithBlock::translate (expr.get_else_block (),
196 &else_block_termianted);
197
198 terminated = if_block_terminated && else_block_termianted;
199
200 auto crate_num = mappings.get_current_crate ();
201 Analysis::NodeMapping mapping (crate_num, expr.get_node_id (),
202 mappings.get_next_hir_id (crate_num),
203 UNKNOWN_LOCAL_DEFID);
204
205 translated = new HIR::IfExprConseqElse (
206 mapping, std::unique_ptr<HIR::Expr> (condition),
207 std::unique_ptr<HIR::BlockExpr> (if_block),
208 std::unique_ptr<HIR::ExprWithBlock> (else_block), expr.get_locus ());
209 }
210
211 /**
212 * Lowers the common part "if let 'pattern' = 'expr' { 'if_block' }" of
213 * IfLetExpr[ConseqElse]:
214 * - 'expr' is lowered into *BRANCH_VALUE
215 * - 'pattern' + 'if_block' are lowered and resulting ARM pushed in MATCH_ARMS
216 * - 'KASE_ELSE_EXPR' is the lowered HIR to be used in the else part.
217 *
218 * Looks like:
219 *
220 * match (expr) {
221 * pattern => {if_block}
222 * _ => kase_else_expr
223 * }
224 *
225 */
226 void
227 ASTLoweringIfLetBlock::desugar_iflet (AST::IfLetExpr &expr,
228 HIR::Expr **branch_value,
229 HIR::Expr *kase_else_expr,
230 std::vector<HIR::MatchCase> &match_arms)
231 {
232 HIR::Expr *kase_expr;
233 std::vector<std::unique_ptr<HIR::Pattern>> match_arm_patterns;
234
235 *branch_value = ASTLoweringExpr::translate (expr.get_value_expr ());
236 kase_expr = ASTLoweringExpr::translate (expr.get_if_block ());
237
238 // (stable) if let only accepts a single pattern, but (unstable) if let chains
239 // need more than one pattern.
240 // We don't support if let chains, so only support a single pattern.
241 rust_assert (expr.get_patterns ().size () == 1);
242
243 for (auto &pattern : expr.get_patterns ())
244 {
245 HIR::Pattern *ptrn = ASTLoweringPattern::translate (*pattern);
246 match_arm_patterns.push_back (std::unique_ptr<HIR::Pattern> (ptrn));
247 }
248
249 // The match arm corresponding to the if let pattern when it matches.
250 HIR::MatchArm arm (std::move (match_arm_patterns), expr.get_locus (), nullptr,
251 {});
252
253 auto crate_num = mappings.get_current_crate ();
254 Analysis::NodeMapping mapping (crate_num, expr.get_node_id (),
255 mappings.get_next_hir_id (crate_num),
256 UNKNOWN_LOCAL_DEFID);
257
258 HIR::MatchCase kase (std::move (mapping), std::move (arm),
259 std::unique_ptr<HIR::Expr> (kase_expr));
260 match_arms.push_back (std::move (kase));
261
262 // The default match arm when the if let pattern does not match
263 std::vector<std::unique_ptr<HIR::Pattern>> match_arm_patterns_wildcard;
264 Analysis::NodeMapping mapping_default (crate_num, expr.get_node_id (),
265 mappings.get_next_hir_id (crate_num),
266 UNKNOWN_LOCAL_DEFID);
267
268 std::unique_ptr<HIR::WildcardPattern> wc
269 = std::unique_ptr<HIR::WildcardPattern> (
270 new HIR::WildcardPattern (mapping_default, expr.get_locus ()));
271
272 match_arm_patterns_wildcard.push_back (std::move (wc));
273
274 HIR::MatchArm arm_default (std::move (match_arm_patterns_wildcard),
275 expr.get_locus (), nullptr, {});
276
277 HIR::MatchCase kase_else (std::move (mapping_default),
278 std::move (arm_default),
279 std::unique_ptr<HIR::Expr> (kase_else_expr));
280 match_arms.push_back (std::move (kase_else));
281 }
282
283 void
284 ASTLoweringIfLetBlock::visit (AST::IfLetExpr &expr)
285 {
286 // Desugar:
287 // if let Some(y) = some_value {
288 // bar();
289 // }
290 //
291 // into:
292 //
293 // match some_value {
294 // Some(y) => {bar();},
295 // _ => ()
296 // }
297
298 HIR::Expr *branch_value;
299
300 std::vector<HIR::MatchCase> match_arms;
301 auto crate_num = mappings.get_current_crate ();
302 Analysis::NodeMapping mapping (crate_num, expr.get_node_id (),
303 mappings.get_next_hir_id (crate_num),
304 UNKNOWN_LOCAL_DEFID);
305
306 HIR::TupleExpr *unit
307 = new HIR::TupleExpr (mapping, {}, {}, {}, expr.get_locus ());
308
309 desugar_iflet (expr, &branch_value, unit, match_arms);
310
311 translated
312 = new HIR::MatchExpr (mapping, std::unique_ptr<HIR::Expr> (branch_value),
313 std::move (match_arms), {}, {}, expr.get_locus ());
314 }
315
316 void
317 ASTLoweringIfLetBlock::visit (AST::IfLetExprConseqElse &expr)
318 {
319 // desugar:
320 // if let Some(y) = some_value {
321 // bar();
322 // } else {
323 // baz();
324 // }
325 //
326 // into
327 // match some_value {
328 // Some(y) => {bar();},
329 // _ => {baz();}
330 // }
331 //
332
333 HIR::Expr *branch_value;
334 std::vector<HIR::MatchCase> match_arms;
335
336 HIR::Expr *kase_else_expr
337 = ASTLoweringExpr::translate (expr.get_else_block ());
338
339 desugar_iflet (expr, &branch_value, kase_else_expr, match_arms);
340
341 auto crate_num = mappings.get_current_crate ();
342 Analysis::NodeMapping mapping (crate_num, expr.get_node_id (),
343 mappings.get_next_hir_id (crate_num),
344 UNKNOWN_LOCAL_DEFID);
345
346 translated
347 = new HIR::MatchExpr (mapping, std::unique_ptr<HIR::Expr> (branch_value),
348 std::move (match_arms), {}, {}, expr.get_locus ());
349 }
350
351 // rust-ast-lower-struct-field-expr.h
352
353 void
354 ASTLowerStructExprField::visit (AST::StructExprFieldIdentifierValue &field)
355 {
356 HIR::Expr *value = ASTLoweringExpr::translate (field.get_value ());
357
358 auto crate_num = mappings.get_current_crate ();
359 Analysis::NodeMapping mapping (crate_num, field.get_node_id (),
360 mappings.get_next_hir_id (crate_num),
361 UNKNOWN_LOCAL_DEFID);
362
363 translated = new HIR::StructExprFieldIdentifierValue (
364 mapping, field.get_field_name (), std::unique_ptr<HIR::Expr> (value),
365 field.get_locus ());
366 }
367
368 void
369 ASTLowerStructExprField::visit (AST::StructExprFieldIndexValue &field)
370 {
371 HIR::Expr *value = ASTLoweringExpr::translate (field.get_value ());
372
373 auto crate_num = mappings.get_current_crate ();
374 Analysis::NodeMapping mapping (crate_num, field.get_node_id (),
375 mappings.get_next_hir_id (crate_num),
376 UNKNOWN_LOCAL_DEFID);
377
378 translated
379 = new HIR::StructExprFieldIndexValue (mapping, field.get_index (),
380 std::unique_ptr<HIR::Expr> (value),
381 field.get_locus ());
382 }
383
384 void
385 ASTLowerStructExprField::visit (AST::StructExprFieldIdentifier &field)
386 {
387 auto crate_num = mappings.get_current_crate ();
388 Analysis::NodeMapping mapping (crate_num, field.get_node_id (),
389 mappings.get_next_hir_id (crate_num),
390 UNKNOWN_LOCAL_DEFID);
391
392 translated
393 = new HIR::StructExprFieldIdentifier (mapping, field.get_field_name (),
394 field.get_locus ());
395 }
396
397 // rust-ast-lower-block.h
398
399 void
400 ASTLoweringExprWithBlock::visit (AST::WhileLoopExpr &expr)
401 {
402 HIR::BlockExpr *loop_block
403 = ASTLoweringBlock::translate (expr.get_loop_block (), &terminated);
404
405 tl::optional<HIR::LoopLabel> loop_label;
406 if (expr.has_loop_label ())
407 loop_label = lower_loop_label (expr.get_loop_label ());
408
409 HIR::Expr *loop_condition
410 = ASTLoweringExpr::translate (expr.get_predicate_expr (), &terminated);
411
412 auto crate_num = mappings.get_current_crate ();
413 Analysis::NodeMapping mapping (crate_num, expr.get_node_id (),
414 mappings.get_next_hir_id (crate_num),
415 UNKNOWN_LOCAL_DEFID);
416
417 translated
418 = new HIR::WhileLoopExpr (mapping,
419 std::unique_ptr<HIR::Expr> (loop_condition),
420 std::unique_ptr<HIR::BlockExpr> (loop_block),
421 expr.get_locus (), std::move (loop_label),
422 expr.get_outer_attrs ());
423 }
424
425 void
426 ASTLoweringExprWithBlock::visit (AST::ForLoopExpr &expr)
427 {
428 rust_unreachable ();
429 }
430
431 void
432 ASTLoweringExprWithBlock::visit (AST::MatchExpr &expr)
433 {
434 HIR::Expr *branch_value
435 = ASTLoweringExpr::translate (expr.get_scrutinee_expr ());
436
437 std::vector<HIR::MatchCase> match_arms;
438 for (auto &match_case : expr.get_match_cases ())
439 {
440 HIR::Expr *kase_expr
441 = ASTLoweringExpr::translate (match_case.get_expr ());
442
443 HIR::Expr *kase_guard_expr = nullptr;
444 if (match_case.get_arm ().has_match_arm_guard ())
445 {
446 kase_guard_expr = ASTLoweringExpr::translate (
447 match_case.get_arm ().get_guard_expr ());
448 }
449
450 std::vector<std::unique_ptr<HIR::Pattern>> match_arm_patterns;
451 for (auto &pattern : match_case.get_arm ().get_patterns ())
452 {
453 HIR::Pattern *ptrn = ASTLoweringPattern::translate (*pattern);
454 match_arm_patterns.push_back (std::unique_ptr<HIR::Pattern> (ptrn));
455 }
456
457 HIR::MatchArm arm (std::move (match_arm_patterns), expr.get_locus (),
458 std::unique_ptr<HIR::Expr> (kase_guard_expr),
459 match_case.get_arm ().get_outer_attrs ());
460
461 auto crate_num = mappings.get_current_crate ();
462 Analysis::NodeMapping mapping (crate_num, expr.get_node_id (),
463 mappings.get_next_hir_id (crate_num),
464 UNKNOWN_LOCAL_DEFID);
465
466 HIR::MatchCase kase (std::move (mapping), std::move (arm),
467 std::unique_ptr<HIR::Expr> (kase_expr));
468 match_arms.push_back (std::move (kase));
469 }
470
471 auto crate_num = mappings.get_current_crate ();
472 Analysis::NodeMapping mapping (crate_num, expr.get_node_id (),
473 mappings.get_next_hir_id (crate_num),
474 UNKNOWN_LOCAL_DEFID);
475
476 translated
477 = new HIR::MatchExpr (mapping, std::unique_ptr<HIR::Expr> (branch_value),
478 std::move (match_arms), expr.get_inner_attrs (),
479 expr.get_outer_attrs (), expr.get_locus ());
480 }
481
482 // rust-ast-lower-expr.h
483
484 void
485 ASTLowerPathInExpression::visit (AST::PathInExpression &expr)
486 {
487 auto crate_num = mappings.get_current_crate ();
488 Analysis::NodeMapping mapping (crate_num, expr.get_node_id (),
489 mappings.get_next_hir_id (crate_num),
490 UNKNOWN_LOCAL_DEFID);
491
492 if (expr.is_lang_item ())
493 {
494 translated = new HIR::PathInExpression (mapping, expr.get_lang_item (),
495 expr.get_locus (), false);
496 return;
497 }
498
499 std::vector<HIR::PathExprSegment> path_segments;
500 auto &segments = expr.get_segments ();
501 for (auto &s : segments)
502 {
503 path_segments.push_back (lower_path_expr_seg ((s)));
504
505 // insert the mappings for the segment
506 HIR::PathExprSegment *lowered_seg = &path_segments.back ();
507 mappings.insert_hir_path_expr_seg (lowered_seg);
508 }
509
510 translated = new HIR::PathInExpression (mapping, std::move (path_segments),
511 expr.get_locus (),
512 expr.opening_scope_resolution ());
513 }
514
515 HIR::QualifiedPathType
516 ASTLoweringBase::lower_qual_path_type (AST::QualifiedPathType &qualified_type)
517 {
518 HIR::Type *type = ASTLoweringType::translate (qualified_type.get_type ());
519 HIR::TypePath *trait
520 = qualified_type.has_as_clause ()
521 ? ASTLowerTypePath::translate (qualified_type.get_as_type_path ())
522 : nullptr;
523
524 auto crate_num = mappings.get_current_crate ();
525 Analysis::NodeMapping mapping (crate_num, qualified_type.get_node_id (),
526 mappings.get_next_hir_id (crate_num),
527 UNKNOWN_LOCAL_DEFID);
528
529 return HIR::QualifiedPathType (mapping, std::unique_ptr<HIR::Type> (type),
530 std::unique_ptr<HIR::TypePath> (trait),
531 qualified_type.get_locus ());
532 }
533
534 void
535 ASTLowerQualPathInExpression::visit (AST::QualifiedPathInExpression &expr)
536 {
537 HIR::QualifiedPathType qual_path_type
538 = lower_qual_path_type (expr.get_qualified_path_type ());
539
540 std::vector<HIR::PathExprSegment> path_segments;
541 auto &segments = expr.get_segments ();
542 for (auto &s : segments)
543 {
544 path_segments.push_back (lower_path_expr_seg ((s)));
545
546 // insert the mappings for the segment
547 HIR::PathExprSegment *lowered_seg = &path_segments.back ();
548 mappings.insert_hir_path_expr_seg (lowered_seg);
549 }
550
551 auto crate_num = mappings.get_current_crate ();
552 Analysis::NodeMapping mapping (crate_num, expr.get_node_id (),
553 mappings.get_next_hir_id (crate_num),
554 UNKNOWN_LOCAL_DEFID);
555
556 translated = new HIR::QualifiedPathInExpression (mapping, qual_path_type,
557 std::move (path_segments),
558 expr.get_locus (),
559 expr.get_outer_attrs ());
560 }
561
562 ClosureParam
563 ASTLoweringBase::lower_closure_param (AST::ClosureParam &param)
564 {
565 HIR::Pattern *param_pattern
566 = ASTLoweringPattern::translate (param.get_pattern ());
567
568 HIR::Type *param_type = param.has_type_given ()
569 ? ASTLoweringType::translate (param.get_type ())
570 : nullptr;
571
572 return HIR::ClosureParam (std::unique_ptr<HIR::Pattern> (param_pattern),
573 param.get_locus (),
574 param.has_type_given ()
575 ? std::unique_ptr<HIR::Type> (param_type)
576 : nullptr,
577 param.get_outer_attrs ());
578 }
579
580 } // namespace HIR
581 } // namespace Rust