Debian Codegraph @

gcc/rust/backend/rust-compile-pattern.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-compile-pattern.h"
20 #include "rust-compile-expr.h"
21 #include "rust-compile-resolve-path.h"
22 #include "rust-constexpr.h"
23 #include "rust-compile-type.h"
24 #include "print-tree.h"
25
26 namespace Rust {
27 namespace Compile {
28
29 void
30 CompilePatternCheckExpr::visit (HIR::PathInExpression &pattern)
31 {
32 // lookup the type
33 TyTy::BaseType *lookup = nullptr;
34 bool ok
35 = ctx->get_tyctx ()->lookup_type (pattern.get_mappings ().get_hirid (),
36 &lookup);
37 rust_assert (ok);
38
39 // must be an ADT (?)
40 rust_assert (lookup->get_kind () == TyTy::TypeKind::ADT);
41 TyTy::ADTType *adt = static_cast<TyTy::ADTType *> (lookup);
42
43 // if this isn't an enum, always succeed
44 if (!adt->is_enum ())
45 {
46 check_expr = boolean_true_node;
47 return;
48 }
49
50 // lookup the variant
51 HirId variant_id;
52 ok = ctx->get_tyctx ()->lookup_variant_definition (
53 pattern.get_mappings ().get_hirid (), &variant_id);
54 rust_assert (ok);
55
56 TyTy::VariantDef *variant = nullptr;
57 ok = adt->lookup_variant_by_id (variant_id, &variant);
58 rust_assert (ok);
59
60 // find discriminant field of scrutinee
61 tree scrutinee_expr_qualifier_expr
62 = Backend::struct_field_expression (match_scrutinee_expr, 0,
63 pattern.get_locus ());
64
65 // must be enum
66 match_scrutinee_expr = scrutinee_expr_qualifier_expr;
67
68 HIR::Expr &discrim_expr = variant->get_discriminant ();
69 tree discrim_expr_node = CompileExpr::Compile (discrim_expr, ctx);
70
71 check_expr
72 = Backend::comparison_expression (ComparisonOperator::EQUAL,
73 match_scrutinee_expr, discrim_expr_node,
74 pattern.get_locus ());
75 }
76
77 void
78 CompilePatternCheckExpr::visit (HIR::LiteralPattern &pattern)
79 {
80 // Compile the literal
81 auto litexpr = std::make_unique<HIR::LiteralExpr> (
82 HIR::LiteralExpr (pattern.get_mappings (), pattern.get_literal (),
83 pattern.get_locus (), std::vector<AST::Attribute> ()));
84
85 // Note: Floating point literals are currently accepted but will likely be
86 // forbidden in LiteralPatterns in a future version of Rust.
87 // See: https://github.com/rust-lang/rust/issues/41620
88 // For now, we cannot compile them anyway as CASE_LABEL_EXPR does not support
89 // floating point types.
90 if (pattern.get_literal ().get_lit_type () == HIR::Literal::LitType::FLOAT)
91 {
92 rust_sorry_at (pattern.get_locus (), "floating-point literal in pattern");
93 }
94
95 tree lit = CompileExpr::Compile (*litexpr, ctx);
96
97 check_expr = Backend::comparison_expression (ComparisonOperator::EQUAL,
98 match_scrutinee_expr, lit,
99 pattern.get_locus ());
100 }
101
102 static tree
103 compile_range_pattern_bound (HIR::RangePatternBound &bound,
104 Analysis::NodeMapping mappings, location_t locus,
105 Context *ctx)
106 {
107 tree result = NULL_TREE;
108 switch (bound.get_bound_type ())
109 {
110 case HIR::RangePatternBound::RangePatternBoundType::LITERAL: {
111 auto &ref = static_cast<HIR::RangePatternBoundLiteral &> (bound);
112
113 HIR::LiteralExpr litexpr (mappings, ref.get_literal (), locus,
114 std::vector<AST::Attribute> ());
115
116 result = CompileExpr::Compile (litexpr, ctx);
117 }
118 break;
119
120 case HIR::RangePatternBound::RangePatternBoundType::PATH: {
121 auto &ref = static_cast<HIR::RangePatternBoundPath &> (bound);
122
123 result = ResolvePathRef::Compile (ref.get_path (), ctx);
124
125 // If the path resolves to a const expression, fold it.
126 result = fold_expr (result);
127 }
128 break;
129
130 case HIR::RangePatternBound::RangePatternBoundType::QUALPATH: {
131 auto &ref = static_cast<HIR::RangePatternBoundQualPath &> (bound);
132
133 result = ResolvePathRef::Compile (ref.get_qualified_path (), ctx);
134
135 // If the path resolves to a const expression, fold it.
136 result = fold_expr (result);
137 }
138 }
139
140 return result;
141 }
142
143 void
144 CompilePatternCheckExpr::visit (HIR::RangePattern &pattern)
145 {
146 tree upper = compile_range_pattern_bound (pattern.get_upper_bound (),
147 pattern.get_mappings (),
148 pattern.get_locus (), ctx);
149 tree lower = compile_range_pattern_bound (pattern.get_lower_bound (),
150 pattern.get_mappings (),
151 pattern.get_locus (), ctx);
152
153 tree check_lower
154 = Backend::comparison_expression (ComparisonOperator::GREATER_OR_EQUAL,
155 match_scrutinee_expr, lower,
156 pattern.get_locus ());
157 tree check_upper
158 = Backend::comparison_expression (ComparisonOperator::LESS_OR_EQUAL,
159 match_scrutinee_expr, upper,
160 pattern.get_locus ());
161 check_expr = Backend::arithmetic_or_logical_expression (
162 ArithmeticOrLogicalOperator::BITWISE_AND, check_lower, check_upper,
163 pattern.get_locus ());
164 }
165
166 void
167 CompilePatternCheckExpr::visit (HIR::ReferencePattern &pattern)
168 {
169 match_scrutinee_expr
170 = indirect_expression (match_scrutinee_expr, pattern.get_locus ());
171 pattern.get_referenced_pattern ().accept_vis (*this);
172 }
173
174 void
175 CompilePatternCheckExpr::visit (HIR::AltPattern &pattern)
176 {
177 auto &alts = pattern.get_alts ();
178
179 check_expr = CompilePatternCheckExpr::Compile (*alts.at (0),
180 match_scrutinee_expr, ctx);
181 auto end = alts.end ();
182 for (auto i = alts.begin () + 1; i != end; i++)
183 {
184 tree next_expr
185 = CompilePatternCheckExpr::Compile (**i, match_scrutinee_expr, ctx);
186 check_expr = Backend::arithmetic_or_logical_expression (
187 ArithmeticOrLogicalOperator::BITWISE_OR, check_expr, next_expr,
188 (*i)->get_locus ());
189 }
190 }
191
192 void
193 CompilePatternCheckExpr::visit (HIR::StructPattern &pattern)
194 {
195 // lookup the type
196 TyTy::BaseType *lookup = nullptr;
197 bool ok = ctx->get_tyctx ()->lookup_type (
198 pattern.get_path ().get_mappings ().get_hirid (), &lookup);
199 rust_assert (ok);
200
201 // this might be an enum
202 rust_assert (lookup->get_kind () == TyTy::TypeKind::ADT);
203 TyTy::ADTType *adt = static_cast<TyTy::ADTType *> (lookup);
204
205 rust_assert (adt->number_of_variants () > 0);
206 TyTy::VariantDef *variant = nullptr;
207 if (adt->is_enum ())
208 {
209 // lookup the variant
210 HirId variant_id;
211 ok = ctx->get_tyctx ()->lookup_variant_definition (
212 pattern.get_path ().get_mappings ().get_hirid (), &variant_id);
213 rust_assert (ok);
214
215 int variant_index = 0;
216 ok = adt->lookup_variant_by_id (variant_id, &variant, &variant_index);
217 rust_assert (ok);
218
219 // find expected discriminant
220 // // need to access qualifier the field, if we use QUAL_UNION_TYPE this
221 // // would be DECL_QUALIFIER i think. For now this will just access the
222 // // first record field and its respective qualifier because it will
223 // // always be set because this is all a big special union
224 HIR::Expr &discrim_expr = variant->get_discriminant ();
225 tree discrim_expr_node = CompileExpr::Compile (discrim_expr, ctx);
226
227 // find discriminant field of scrutinee
228 tree scrutinee_expr_qualifier_expr
229 = Backend::struct_field_expression (match_scrutinee_expr, 0,
230 pattern.get_path ().get_locus ());
231
232 check_expr
233 = Backend::comparison_expression (ComparisonOperator::EQUAL,
234 scrutinee_expr_qualifier_expr,
235 discrim_expr_node,
236 pattern.get_path ().get_locus ());
237
238 match_scrutinee_expr = scrutinee_expr_qualifier_expr;
239 }
240 else
241 {
242 variant = adt->get_variants ().at (0);
243 check_expr = boolean_true_node;
244 }
245
246 auto &struct_pattern_elems = pattern.get_struct_pattern_elems ();
247 for (auto &field : struct_pattern_elems.get_struct_pattern_fields ())
248 {
249 switch (field->get_item_type ())
250 {
251 case HIR::StructPatternField::ItemType::TUPLE_PAT: {
252 // TODO
253 rust_unreachable ();
254 }
255 break;
256
257 case HIR::StructPatternField::ItemType::IDENT_PAT: {
258 HIR::StructPatternFieldIdentPat &ident
259 = static_cast<HIR::StructPatternFieldIdentPat &> (*field.get ());
260
261 size_t offs = 0;
262 ok = variant->lookup_field (ident.get_identifier ().as_string (),
263 nullptr, &offs);
264 rust_assert (ok);
265
266 // we may be offsetting by + 1 here since the first field in the
267 // record is always the discriminator
268 offs += adt->is_enum ();
269 tree field_expr
270 = Backend::struct_field_expression (match_scrutinee_expr, offs,
271 ident.get_locus ());
272
273 tree check_expr_sub
274 = CompilePatternCheckExpr::Compile (ident.get_pattern (),
275 field_expr, ctx);
276 check_expr = Backend::arithmetic_or_logical_expression (
277 ArithmeticOrLogicalOperator::BITWISE_AND, check_expr,
278 check_expr_sub, ident.get_pattern ().get_locus ());
279 }
280 break;
281
282 case HIR::StructPatternField::ItemType::IDENT: {
283 // ident pattern always matches - do nothing
284 }
285 break;
286 }
287 }
288 }
289
290 void
291 CompilePatternCheckExpr::visit (HIR::TupleStructPattern &pattern)
292 {
293 // lookup the type
294 TyTy::BaseType *lookup = nullptr;
295 bool ok = ctx->get_tyctx ()->lookup_type (
296 pattern.get_path ().get_mappings ().get_hirid (), &lookup);
297 rust_assert (ok);
298
299 // this might be an enum
300 rust_assert (lookup->get_kind () == TyTy::TypeKind::ADT);
301 TyTy::ADTType *adt = static_cast<TyTy::ADTType *> (lookup);
302
303 int variant_index = 0;
304 rust_assert (adt->number_of_variants () > 0);
305 TyTy::VariantDef *variant = nullptr;
306 if (adt->is_enum ())
307 {
308 // lookup the variant
309 HirId variant_id;
310 ok = ctx->get_tyctx ()->lookup_variant_definition (
311 pattern.get_path ().get_mappings ().get_hirid (), &variant_id);
312 rust_assert (ok);
313
314 ok = adt->lookup_variant_by_id (variant_id, &variant, &variant_index);
315 rust_assert (ok);
316
317 // find expected discriminant
318 HIR::Expr &discrim_expr = variant->get_discriminant ();
319 tree discrim_expr_node = CompileExpr::Compile (discrim_expr, ctx);
320
321 // find discriminant field of scrutinee
322 tree scrutinee_expr_qualifier_expr
323 = Backend::struct_field_expression (match_scrutinee_expr, 0,
324 pattern.get_path ().get_locus ());
325
326 check_expr
327 = Backend::comparison_expression (ComparisonOperator::EQUAL,
328 scrutinee_expr_qualifier_expr,
329 discrim_expr_node,
330 pattern.get_path ().get_locus ());
331 }
332 else
333 {
334 variant = adt->get_variants ().at (0);
335 check_expr = boolean_true_node;
336 }
337
338 HIR::TupleStructItems &items = pattern.get_items ();
339 switch (items.get_item_type ())
340 {
341 case HIR::TupleStructItems::RANGED: {
342 // TODO
343 rust_unreachable ();
344 }
345 break;
346
347 case HIR::TupleStructItems::MULTIPLE: {
348 HIR::TupleStructItemsNoRange &items_no_range
349 = static_cast<HIR::TupleStructItemsNoRange &> (items);
350
351 rust_assert (items_no_range.get_patterns ().size ()
352 == variant->num_fields ());
353
354 size_t tuple_field_index = 0;
355 for (auto &pattern : items_no_range.get_patterns ())
356 {
357 // find payload union field of scrutinee
358 tree payload_ref
359 = Backend::struct_field_expression (match_scrutinee_expr, 1,
360 pattern->get_locus ());
361
362 tree variant_ref
363 = Backend::struct_field_expression (payload_ref, variant_index,
364 pattern->get_locus ());
365
366 tree field_expr
367 = Backend::struct_field_expression (variant_ref,
368 tuple_field_index++,
369 pattern->get_locus ());
370
371 tree check_expr_sub
372 = CompilePatternCheckExpr::Compile (*pattern, field_expr, ctx);
373 check_expr = Backend::arithmetic_or_logical_expression (
374 ArithmeticOrLogicalOperator::BITWISE_AND, check_expr,
375 check_expr_sub, pattern->get_locus ());
376 }
377 }
378 break;
379 }
380 }
381
382 void
383 CompilePatternCheckExpr::visit (HIR::TuplePattern &pattern)
384 {
385 check_expr = boolean_true_node;
386
387 switch (pattern.get_items ().get_item_type ())
388 {
389 case HIR::TuplePatternItems::RANGED: {
390 // TODO
391 gcc_unreachable ();
392 }
393 break;
394
395 case HIR::TuplePatternItems::MULTIPLE: {
396 auto &items = static_cast<HIR::TuplePatternItemsMultiple &> (
397 pattern.get_items ());
398 size_t tuple_field_index = 0;
399
400 for (auto &pat : items.get_patterns ())
401 {
402 tree field_expr
403 = Backend::struct_field_expression (match_scrutinee_expr,
404 tuple_field_index++,
405 pat->get_locus ());
406
407 tree check_expr_sub
408 = CompilePatternCheckExpr::Compile (*pat, field_expr, ctx);
409 check_expr = Backend::arithmetic_or_logical_expression (
410 ArithmeticOrLogicalOperator::BITWISE_AND, check_expr,
411 check_expr_sub, pat->get_locus ());
412 }
413 }
414 }
415 }
416
417 // setup the bindings
418
419 void
420 CompilePatternBindings::visit (HIR::TupleStructPattern &pattern)
421 {
422 // lookup the type
423 TyTy::BaseType *lookup = nullptr;
424 bool ok = ctx->get_tyctx ()->lookup_type (
425 pattern.get_path ().get_mappings ().get_hirid (), &lookup);
426 rust_assert (ok);
427
428 // this must be an enum
429 rust_assert (lookup->get_kind () == TyTy::TypeKind::ADT);
430 TyTy::ADTType *adt = static_cast<TyTy::ADTType *> (lookup);
431 rust_assert (adt->number_of_variants () > 0);
432
433 int variant_index = 0;
434 TyTy::VariantDef *variant = adt->get_variants ().at (0);
435 if (adt->is_enum ())
436 {
437 HirId variant_id = UNKNOWN_HIRID;
438 bool ok = ctx->get_tyctx ()->lookup_variant_definition (
439 pattern.get_path ().get_mappings ().get_hirid (), &variant_id);
440 rust_assert (ok);
441
442 ok = adt->lookup_variant_by_id (variant_id, &variant, &variant_index);
443 rust_assert (ok);
444 }
445
446 rust_assert (variant->get_variant_type ()
447 == TyTy::VariantDef::VariantType::TUPLE);
448
449 HIR::TupleStructItems &items = pattern.get_items ();
450 switch (items.get_item_type ())
451 {
452 case HIR::TupleStructItems::RANGED: {
453 // TODO
454 rust_unreachable ();
455 }
456 break;
457
458 case HIR::TupleStructItems::MULTIPLE: {
459 HIR::TupleStructItemsNoRange &items_no_range
460 = static_cast<HIR::TupleStructItemsNoRange &> (items);
461
462 rust_assert (items_no_range.get_patterns ().size ()
463 == variant->num_fields ());
464
465 if (adt->is_enum ())
466 {
467 size_t tuple_field_index = 0;
468 for (auto &pattern : items_no_range.get_patterns ())
469 {
470 tree payload_accessor_union
471 = Backend::struct_field_expression (match_scrutinee_expr, 1,
472 pattern->get_locus ());
473
474 tree variant_accessor
475 = Backend::struct_field_expression (payload_accessor_union,
476 variant_index,
477 pattern->get_locus ());
478
479 tree binding
480 = Backend::struct_field_expression (variant_accessor,
481 tuple_field_index++,
482 pattern->get_locus ());
483
484 CompilePatternBindings::Compile (*pattern, binding, ctx);
485 }
486 }
487 else
488 {
489 size_t tuple_field_index = 0;
490 for (auto &pattern : items_no_range.get_patterns ())
491 {
492 tree variant_accessor = match_scrutinee_expr;
493
494 tree binding
495 = Backend::struct_field_expression (variant_accessor,
496 tuple_field_index++,
497 pattern->get_locus ());
498
499 CompilePatternBindings::Compile (*pattern, binding, ctx);
500 }
501 }
502 }
503 break;
504 }
505 }
506
507 void
508 CompilePatternBindings::visit (HIR::StructPattern &pattern)
509 {
510 // lookup the type
511 TyTy::BaseType *lookup = nullptr;
512 bool ok = ctx->get_tyctx ()->lookup_type (
513 pattern.get_path ().get_mappings ().get_hirid (), &lookup);
514 rust_assert (ok);
515
516 // this must be an enum
517 rust_assert (lookup->get_kind () == TyTy::TypeKind::ADT);
518 TyTy::ADTType *adt = static_cast<TyTy::ADTType *> (lookup);
519 rust_assert (adt->number_of_variants () > 0);
520
521 int variant_index = 0;
522 TyTy::VariantDef *variant = adt->get_variants ().at (0);
523 if (adt->is_enum ())
524 {
525 HirId variant_id = UNKNOWN_HIRID;
526 bool ok = ctx->get_tyctx ()->lookup_variant_definition (
527 pattern.get_path ().get_mappings ().get_hirid (), &variant_id);
528 rust_assert (ok);
529
530 ok = adt->lookup_variant_by_id (variant_id, &variant, &variant_index);
531 rust_assert (ok);
532 }
533
534 rust_assert (variant->get_variant_type ()
535 == TyTy::VariantDef::VariantType::STRUCT);
536
537 auto &struct_pattern_elems = pattern.get_struct_pattern_elems ();
538 for (auto &field : struct_pattern_elems.get_struct_pattern_fields ())
539 {
540 switch (field->get_item_type ())
541 {
542 case HIR::StructPatternField::ItemType::TUPLE_PAT: {
543 // TODO
544 rust_unreachable ();
545 }
546 break;
547
548 case HIR::StructPatternField::ItemType::IDENT_PAT: {
549 // TODO
550 rust_unreachable ();
551 }
552 break;
553
554 case HIR::StructPatternField::ItemType::IDENT: {
555 HIR::StructPatternFieldIdent &ident
556 = static_cast<HIR::StructPatternFieldIdent &> (*field.get ());
557
558 size_t offs = 0;
559 ok = variant->lookup_field (ident.get_identifier ().as_string (),
560 nullptr, &offs);
561 rust_assert (ok);
562
563 tree binding = error_mark_node;
564 if (adt->is_enum ())
565 {
566 tree payload_accessor_union
567 = Backend::struct_field_expression (match_scrutinee_expr, 1,
568 ident.get_locus ());
569
570 tree variant_accessor
571 = Backend::struct_field_expression (payload_accessor_union,
572 variant_index,
573 ident.get_locus ());
574
575 binding
576 = Backend::struct_field_expression (variant_accessor, offs,
577 ident.get_locus ());
578 }
579 else
580 {
581 tree variant_accessor = match_scrutinee_expr;
582 binding
583 = Backend::struct_field_expression (variant_accessor, offs,
584 ident.get_locus ());
585 }
586
587 ctx->insert_pattern_binding (ident.get_mappings ().get_hirid (),
588 binding);
589 }
590 break;
591 }
592 }
593 }
594
595 void
596 CompilePatternBindings::visit (HIR::ReferencePattern &pattern)
597 {
598 tree derefed
599 = indirect_expression (match_scrutinee_expr, pattern.get_locus ());
600
601 CompilePatternBindings::Compile (pattern.get_referenced_pattern (), derefed,
602 ctx);
603 }
604
605 void
606 CompilePatternBindings::visit (HIR::IdentifierPattern &pattern)
607 {
608 if (!pattern.get_is_ref ())
609 {
610 ctx->insert_pattern_binding (pattern.get_mappings ().get_hirid (),
611 match_scrutinee_expr);
612 return;
613 }
614
615 tree ref = address_expression (match_scrutinee_expr,
616 EXPR_LOCATION (match_scrutinee_expr));
617 ctx->insert_pattern_binding (pattern.get_mappings ().get_hirid (), ref);
618 }
619
620 void
621 CompilePatternBindings::visit (HIR::TuplePattern &pattern)
622 {
623 rust_assert (pattern.has_tuple_pattern_items ());
624
625 // lookup the type
626 TyTy::BaseType *ty = nullptr;
627 bool ok
628 = ctx->get_tyctx ()->lookup_type (pattern.get_mappings ().get_hirid (),
629 &ty);
630 rust_assert (ok);
631
632 switch (pattern.get_items ().get_item_type ())
633 {
634 case HIR::TuplePatternItems::ItemType::RANGED: {
635 size_t tuple_idx = 0;
636 auto &items
637 = static_cast<HIR::TuplePatternItemsRanged &> (pattern.get_items ());
638
639 auto &items_lower = items.get_lower_patterns ();
640 auto &items_upper = items.get_upper_patterns ();
641
642 for (auto &sub : items_lower)
643 {
644 TyTy::BaseType *ty_sub = nullptr;
645 HirId sub_id = sub->get_mappings ().get_hirid ();
646 bool ok = ctx->get_tyctx ()->lookup_type (sub_id, &ty_sub);
647 rust_assert (ok);
648
649 tree sub_init
650 = Backend::struct_field_expression (match_scrutinee_expr,
651 tuple_idx, sub->get_locus ());
652
653 CompilePatternBindings::Compile (*sub.get (), sub_init, ctx);
654 tuple_idx++;
655 }
656
657 rust_assert (ty->get_kind () == TyTy::TypeKind::TUPLE);
658 tuple_idx = static_cast<TyTy::TupleType &> (*ty).num_fields ()
659 - items_upper.size ();
660
661 for (auto &sub : items_upper)
662 {
663 TyTy::BaseType *ty_sub = nullptr;
664 HirId sub_id = sub->get_mappings ().get_hirid ();
665 bool ok = ctx->get_tyctx ()->lookup_type (sub_id, &ty_sub);
666 rust_assert (ok);
667
668 tree sub_init
669 = Backend::struct_field_expression (match_scrutinee_expr,
670 tuple_idx, sub->get_locus ());
671 CompilePatternBindings::Compile (*sub.get (), sub_init, ctx);
672 tuple_idx++;
673 }
674
675 return;
676 }
677 case HIR::TuplePatternItems::ItemType::MULTIPLE: {
678 size_t tuple_idx = 0;
679 auto &items = static_cast<HIR::TuplePatternItemsMultiple &> (
680 pattern.get_items ());
681
682 for (auto &sub : items.get_patterns ())
683 {
684 TyTy::BaseType *ty_sub = nullptr;
685 HirId sub_id = sub->get_mappings ().get_hirid ();
686 bool ok = ctx->get_tyctx ()->lookup_type (sub_id, &ty_sub);
687 rust_assert (ok);
688
689 tree sub_init
690 = Backend::struct_field_expression (match_scrutinee_expr,
691 tuple_idx, sub->get_locus ());
692 CompilePatternBindings::Compile (*sub.get (), sub_init, ctx);
693 tuple_idx++;
694 }
695
696 return;
697 }
698 default: {
699 rust_unreachable ();
700 }
701 }
702 }
703
704 //
705
706 void
707 CompilePatternLet::visit (HIR::IdentifierPattern &pattern)
708 {
709 Bvariable *var = nullptr;
710 rust_assert (
711 ctx->lookup_var_decl (pattern.get_mappings ().get_hirid (), &var));
712
713 auto fnctx = ctx->peek_fn ();
714 if (ty->is_unit ())
715 {
716 ctx->add_statement (init_expr);
717
718 auto unit_type_init_expr = unit_expression (rval_locus);
719 auto s = Backend::init_statement (fnctx.fndecl, var, unit_type_init_expr);
720 ctx->add_statement (s);
721 }
722 else
723 {
724 auto s = Backend::init_statement (fnctx.fndecl, var, init_expr);
725 ctx->add_statement (s);
726 }
727 }
728
729 void
730 CompilePatternLet::visit (HIR::WildcardPattern &pattern)
731 {
732 tree init_stmt = NULL;
733 tree stmt_type = TyTyResolveCompile::compile (ctx, ty);
734
735 Backend::temporary_variable (ctx->peek_fn ().fndecl, NULL_TREE, stmt_type,
736 init_expr, false, pattern.get_locus (),
737 &init_stmt);
738
739 ctx->add_statement (init_stmt);
740 }
741
742 void
743 CompilePatternLet::visit (HIR::TuplePattern &pattern)
744 {
745 rust_assert (pattern.has_tuple_pattern_items ());
746
747 tree tuple_type = TyTyResolveCompile::compile (ctx, ty);
748 tree init_stmt;
749 Bvariable *tmp_var
750 = Backend::temporary_variable (ctx->peek_fn ().fndecl, NULL_TREE,
751 tuple_type, init_expr, false,
752 pattern.get_locus (), &init_stmt);
753 tree access_expr = Backend::var_expression (tmp_var, pattern.get_locus ());
754 ctx->add_statement (init_stmt);
755
756 switch (pattern.get_items ().get_item_type ())
757 {
758 case HIR::TuplePatternItems::ItemType::RANGED: {
759 size_t tuple_idx = 0;
760 auto &items
761 = static_cast<HIR::TuplePatternItemsRanged &> (pattern.get_items ());
762
763 auto &items_lower = items.get_lower_patterns ();
764 auto &items_upper = items.get_upper_patterns ();
765
766 for (auto &sub : items_lower)
767 {
768 TyTy::BaseType *ty_sub = nullptr;
769 HirId sub_id = sub->get_mappings ().get_hirid ();
770 bool ok = ctx->get_tyctx ()->lookup_type (sub_id, &ty_sub);
771 rust_assert (ok);
772
773 tree sub_init
774 = Backend::struct_field_expression (access_expr, tuple_idx,
775 sub->get_locus ());
776 CompilePatternLet::Compile (sub.get (), sub_init, ty_sub,
777 rval_locus, ctx);
778 tuple_idx++;
779 }
780
781 rust_assert (ty->get_kind () == TyTy::TypeKind::TUPLE);
782 tuple_idx = static_cast<TyTy::TupleType &> (*ty).num_fields ()
783 - items_upper.size ();
784
785 for (auto &sub : items_upper)
786 {
787 TyTy::BaseType *ty_sub = nullptr;
788 HirId sub_id = sub->get_mappings ().get_hirid ();
789 bool ok = ctx->get_tyctx ()->lookup_type (sub_id, &ty_sub);
790 rust_assert (ok);
791
792 tree sub_init
793 = Backend::struct_field_expression (access_expr, tuple_idx,
794 sub->get_locus ());
795 CompilePatternLet::Compile (sub.get (), sub_init, ty_sub,
796 rval_locus, ctx);
797 tuple_idx++;
798 }
799
800 return;
801 }
802 case HIR::TuplePatternItems::ItemType::MULTIPLE: {
803 size_t tuple_idx = 0;
804 auto &items = static_cast<HIR::TuplePatternItemsMultiple &> (
805 pattern.get_items ());
806
807 for (auto &sub : items.get_patterns ())
808 {
809 TyTy::BaseType *ty_sub = nullptr;
810 HirId sub_id = sub->get_mappings ().get_hirid ();
811 bool ok = ctx->get_tyctx ()->lookup_type (sub_id, &ty_sub);
812 rust_assert (ok);
813
814 tree sub_init
815 = Backend::struct_field_expression (access_expr, tuple_idx,
816 sub->get_locus ());
817 CompilePatternLet::Compile (sub.get (), sub_init, ty_sub,
818 rval_locus, ctx);
819 tuple_idx++;
820 }
821
822 return;
823 }
824 default: {
825 rust_unreachable ();
826 }
827 }
828 }
829
830 } // namespace Compile
831 } // namespace Rust