LCOV - code coverage report
Current view: top level - gcc/rust/backend - rust-compile-pattern.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 94.6 % 945 894
Test Date: 2026-11-14 16:39:50 Functions: 100.0 % 27 27
Legend: Lines:     hit not hit

            Line data    Source code
       1              : // Copyright (C) 2020-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-compile-pattern.h"
      20              : #include "rust-compile-drop-builder.h"
      21              : #include "print-tree.h"
      22              : #include "rust-compile-drop.h"
      23              : #include "rust-compile-expr.h"
      24              : #include "rust-compile-resolve-path.h"
      25              : #include "rust-compile-type.h"
      26              : #include "rust-constexpr.h"
      27              : #include "rust-diagnostics.h"
      28              : #include "rust-hir-pattern-abstract.h"
      29              : #include "rust-hir-pattern.h"
      30              : #include "rust-hir-trait-reference.h"
      31              : #include "rust-hir-type-bounds.h"
      32              : #include "rust-lang-item.h"
      33              : #include "rust-system.h"
      34              : #include "rust-tyty.h"
      35              : #include "tree.h"
      36              : 
      37              : namespace Rust {
      38              : namespace Compile {
      39              : 
      40              : void
      41          825 : CompilePatternCheckExpr::visit (HIR::PathInExpression &pattern)
      42              : {
      43              :   // lookup the type
      44          825 :   TyTy::BaseType *lookup = nullptr;
      45          825 :   bool ok
      46          825 :     = ctx->get_tyctx ()->lookup_type (pattern.get_mappings ().get_hirid (),
      47              :                                       &lookup);
      48          825 :   rust_assert (ok);
      49              : 
      50          825 :   if (lookup->get_kind () != TyTy::TypeKind::ADT)
      51              :     {
      52            2 :       tree constant_expr = ResolvePathRef::Compile (pattern, ctx);
      53              : 
      54            2 :       check_expr
      55            2 :         = Backend::comparison_expression (ComparisonOperator::EQUAL,
      56              :                                           match_scrutinee_expr, constant_expr,
      57              :                                           pattern.get_locus ());
      58            4 :       return;
      59              :     }
      60              : 
      61          823 :   TyTy::ADTType *adt = static_cast<TyTy::ADTType *> (lookup);
      62              : 
      63              :   // if this isn't an enum, always succeed
      64          823 :   if (!adt->is_enum ())
      65              :     {
      66            2 :       check_expr = boolean_true_node;
      67            2 :       return;
      68              :     }
      69              : 
      70              :   // lookup the variant
      71          821 :   HirId variant_id;
      72          821 :   ok = ctx->get_tyctx ()->lookup_variant_definition (
      73          821 :     pattern.get_mappings ().get_hirid (), &variant_id);
      74          821 :   rust_assert (ok);
      75              : 
      76          821 :   TyTy::VariantDef *variant = nullptr;
      77          821 :   ok = adt->lookup_variant_by_id (variant_id, &variant);
      78          821 :   rust_assert (ok);
      79              : 
      80              :   // find discriminant field of scrutinee
      81          821 :   tree scrutinee_expr_qualifier_expr
      82          821 :     = Backend::struct_field_expression (match_scrutinee_expr, 0,
      83              :                                         pattern.get_locus ());
      84              : 
      85              :   // must be enum
      86          821 :   match_scrutinee_expr = scrutinee_expr_qualifier_expr;
      87              : 
      88          821 :   HIR::Expr &discrim_expr = variant->get_discriminant ();
      89          821 :   tree discrim_expr_node = CompileExpr::Compile (discrim_expr, ctx);
      90              : 
      91          821 :   check_expr
      92          821 :     = Backend::comparison_expression (ComparisonOperator::EQUAL,
      93              :                                       match_scrutinee_expr, discrim_expr_node,
      94              :                                       pattern.get_locus ());
      95              : }
      96              : 
      97              : void
      98          475 : CompilePatternCheckExpr::visit (HIR::LiteralPattern &pattern)
      99              : {
     100              :   // Compile the literal
     101          475 :   auto litexpr = std::make_unique<HIR::LiteralExpr> (
     102          950 :     HIR::LiteralExpr (pattern.get_mappings (), pattern.get_literal (),
     103          950 :                       pattern.get_locus (), std::vector<AST::Attribute> ()));
     104          475 :   if (pattern.get_has_minus ())
     105            8 :     litexpr->set_negative ();
     106              : 
     107              :   // Note: Floating point literals are currently accepted but will likely be
     108              :   // forbidden in LiteralPatterns in a future version of Rust.
     109              :   // See: https://github.com/rust-lang/rust/issues/41620
     110              :   // For now, we cannot compile them anyway as CASE_LABEL_EXPR does not support
     111              :   // floating point types.
     112          475 :   if (pattern.get_literal ().get_lit_type () == HIR::Literal::LitType::FLOAT)
     113              :     {
     114            0 :       rust_sorry_at (pattern.get_locus (), "floating-point literal in pattern");
     115              :     }
     116              : 
     117          475 :   tree lit = CompileExpr::Compile (*litexpr, ctx);
     118              : 
     119          475 :   check_expr = Backend::comparison_expression (ComparisonOperator::EQUAL,
     120              :                                                match_scrutinee_expr, lit,
     121          475 :                                                pattern.get_locus ());
     122          475 : }
     123              : 
     124              : static tree
     125           88 : compile_range_pattern_bound (HIR::RangePatternBound &bound,
     126              :                              Analysis::NodeMapping mappings, location_t locus,
     127              :                              Context *ctx)
     128              : {
     129           88 :   tree result = NULL_TREE;
     130           88 :   switch (bound.get_bound_type ())
     131              :     {
     132           67 :     case HIR::RangePatternBound::RangePatternBoundType::LITERAL:
     133           67 :       {
     134           67 :         auto &ref = static_cast<HIR::RangePatternBoundLiteral &> (bound);
     135              : 
     136          201 :         HIR::LiteralExpr litexpr (mappings, ref.get_literal (), locus,
     137           67 :                                   std::vector<AST::Attribute> ());
     138           67 :         if (ref.get_has_minus ())
     139           29 :           litexpr.set_negative ();
     140              : 
     141           67 :         result = CompileExpr::Compile (litexpr, ctx);
     142           67 :       }
     143           67 :       break;
     144              : 
     145           21 :     case HIR::RangePatternBound::RangePatternBoundType::PATH:
     146           21 :       {
     147           21 :         auto &ref = static_cast<HIR::RangePatternBoundPath &> (bound);
     148              : 
     149           21 :         result = ResolvePathRef::Compile (ref.get_path (), ctx);
     150              : 
     151              :         // If the path resolves to a const expression, fold it.
     152           21 :         result = fold_expr (result);
     153              :       }
     154           21 :       break;
     155              : 
     156            0 :     case HIR::RangePatternBound::RangePatternBoundType::QUALPATH:
     157            0 :       {
     158            0 :         auto &ref = static_cast<HIR::RangePatternBoundQualPath &> (bound);
     159              : 
     160            0 :         result = ResolvePathRef::Compile (ref.get_qualified_path (), ctx);
     161              : 
     162              :         // If the path resolves to a const expression, fold it.
     163            0 :         result = fold_expr (result);
     164              :       }
     165              :     }
     166              : 
     167           88 :   return result;
     168              : }
     169              : 
     170              : void
     171           44 : CompilePatternCheckExpr::visit (HIR::RangePattern &pattern)
     172              : {
     173           44 :   tree upper = compile_range_pattern_bound (pattern.get_upper_bound (),
     174           44 :                                             pattern.get_mappings (),
     175           44 :                                             pattern.get_locus (), ctx);
     176          132 :   tree lower = compile_range_pattern_bound (pattern.get_lower_bound (),
     177           44 :                                             pattern.get_mappings (),
     178           44 :                                             pattern.get_locus (), ctx);
     179              : 
     180           44 :   rust_assert (
     181              :     (TREE_CODE (upper) == REAL_CST && TREE_CODE (lower) == REAL_CST)
     182              :     || (TREE_CODE (upper) == INTEGER_CST && TREE_CODE (lower) == INTEGER_CST));
     183              : 
     184           44 :   bool error_E0579 = false;
     185           44 :   if (TREE_CODE (upper) == REAL_CST)
     186              :     {
     187            2 :       const REAL_VALUE_TYPE *upper_r = TREE_REAL_CST_PTR (upper);
     188            2 :       const REAL_VALUE_TYPE *lower_r = TREE_REAL_CST_PTR (lower);
     189            2 :       if (real_compare (GE_EXPR, lower_r, upper_r))
     190              :         error_E0579 = true;
     191              :     }
     192           42 :   else if (TREE_CODE (upper) == INTEGER_CST)
     193              :     {
     194           42 :       auto upper_wi = wi::to_wide (upper).to_shwi ();
     195           42 :       auto lower_wi = wi::to_wide (lower).to_shwi ();
     196           42 :       if (lower_wi >= upper_wi)
     197              :         error_E0579 = true;
     198              :     }
     199              : 
     200              :   if (error_E0579)
     201            2 :     rust_error_at (pattern.get_locus (), ErrorCode::E0579,
     202              :                    "lower range bound must be less than upper");
     203              : 
     204           44 :   ComparisonOperator upper_cmp = pattern.is_inclusive_range ()
     205           44 :                                    ? ComparisonOperator::LESS_OR_EQUAL
     206           21 :                                    : ComparisonOperator::LESS_THAN;
     207           44 :   tree check_lower
     208           44 :     = Backend::comparison_expression (ComparisonOperator::GREATER_OR_EQUAL,
     209              :                                       match_scrutinee_expr, lower,
     210           44 :                                       pattern.get_locus ());
     211           44 :   tree check_upper
     212           44 :     = Backend::comparison_expression (upper_cmp, match_scrutinee_expr, upper,
     213           44 :                                       pattern.get_locus ());
     214           44 :   check_expr = Backend::arithmetic_or_logical_expression (
     215              :     ArithmeticOrLogicalOperator::BITWISE_AND, check_lower, check_upper,
     216           44 :     pattern.get_locus ());
     217           44 : }
     218              : 
     219              : void
     220          163 : CompilePatternCheckExpr::visit (HIR::ReferencePattern &pattern)
     221              : {
     222          163 :   match_scrutinee_expr
     223          163 :     = indirect_expression (match_scrutinee_expr, pattern.get_locus ());
     224          163 :   pattern.get_referenced_pattern ().accept_vis (*this);
     225          163 : }
     226              : 
     227              : void
     228           51 : CompilePatternCheckExpr::visit (HIR::AltPattern &pattern)
     229              : {
     230           51 :   auto &alts = pattern.get_alts ();
     231              : 
     232           51 :   check_expr = CompilePatternCheckExpr::Compile (*alts.at (0),
     233              :                                                  match_scrutinee_expr, ctx);
     234           51 :   auto end = alts.end ();
     235          103 :   for (auto i = alts.begin () + 1; i != end; i++)
     236              :     {
     237           52 :       tree next_expr
     238           52 :         = CompilePatternCheckExpr::Compile (**i, match_scrutinee_expr, ctx);
     239           52 :       check_expr = Backend::arithmetic_or_logical_expression (
     240              :         ArithmeticOrLogicalOperator::BITWISE_OR, check_expr, next_expr,
     241           52 :         (*i)->get_locus ());
     242              :     }
     243           51 : }
     244              : 
     245              : void
     246          139 : CompilePatternCheckExpr::visit (HIR::StructPattern &pattern)
     247              : {
     248              :   // lookup the type
     249          139 :   TyTy::BaseType *lookup = nullptr;
     250          139 :   bool ok = ctx->get_tyctx ()->lookup_type (
     251          139 :     pattern.get_path ().get_mappings ().get_hirid (), &lookup);
     252          139 :   rust_assert (ok);
     253              : 
     254              :   // this might be an enum
     255          139 :   rust_assert (lookup->get_kind () == TyTy::TypeKind::ADT);
     256          139 :   TyTy::ADTType *adt = static_cast<TyTy::ADTType *> (lookup);
     257              : 
     258          139 :   rust_assert (adt->number_of_variants () > 0);
     259          139 :   TyTy::VariantDef *variant = nullptr;
     260          139 :   tree variant_accesser_expr = nullptr;
     261          139 :   if (adt->is_enum ())
     262              :     {
     263              :       // lookup the variant
     264          111 :       HirId variant_id;
     265          111 :       ok = ctx->get_tyctx ()->lookup_variant_definition (
     266          111 :         pattern.get_path ().get_mappings ().get_hirid (), &variant_id);
     267          111 :       rust_assert (ok);
     268              : 
     269          111 :       int variant_index = 0;
     270          111 :       ok = adt->lookup_variant_by_id (variant_id, &variant, &variant_index);
     271          111 :       rust_assert (ok);
     272              : 
     273              :       // find expected discriminant
     274              :       // // need to access qualifier the field, if we use QUAL_UNION_TYPE this
     275              :       // // would be DECL_QUALIFIER i think.
     276          111 :       HIR::Expr &discrim_expr = variant->get_discriminant ();
     277          111 :       tree discrim_expr_node = CompileExpr::Compile (discrim_expr, ctx);
     278              : 
     279              :       // find discriminant field of scrutinee
     280          111 :       tree scrutinee_expr_qualifier_expr
     281          111 :         = Backend::struct_field_expression (match_scrutinee_expr, 0,
     282          111 :                                             pattern.get_path ().get_locus ());
     283              : 
     284              :       // access variant data
     285          111 :       tree scrutinee_union_expr
     286          111 :         = Backend::struct_field_expression (match_scrutinee_expr, 1,
     287          111 :                                             pattern.get_path ().get_locus ());
     288          111 :       variant_accesser_expr
     289          111 :         = Backend::struct_field_expression (scrutinee_union_expr, variant_index,
     290          111 :                                             pattern.get_path ().get_locus ());
     291              : 
     292          111 :       check_expr
     293          111 :         = Backend::comparison_expression (ComparisonOperator::EQUAL,
     294              :                                           scrutinee_expr_qualifier_expr,
     295              :                                           discrim_expr_node,
     296          111 :                                           pattern.get_path ().get_locus ());
     297              : 
     298          111 :       match_scrutinee_expr = scrutinee_expr_qualifier_expr;
     299              :     }
     300              :   else
     301              :     {
     302           28 :       variant = adt->get_variants ().at (0);
     303           28 :       variant_accesser_expr = match_scrutinee_expr;
     304           28 :       check_expr = boolean_true_node;
     305              :     }
     306              : 
     307          139 :   auto &struct_pattern_elems = pattern.get_struct_pattern_elems ();
     308          377 :   for (auto &field : struct_pattern_elems.get_struct_pattern_fields ())
     309              :     {
     310          238 :       switch (field->get_item_type ())
     311              :         {
     312           18 :         case HIR::StructPatternField::ItemType::TUPLE_PAT:
     313           18 :           {
     314           18 :             HIR::StructPatternFieldTuplePat &tuple_pat
     315           18 :               = static_cast<HIR::StructPatternFieldTuplePat &> (*field.get ());
     316           18 :             size_t tuple_pat_index = tuple_pat.get_index ();
     317           18 :             tree field_expr
     318           18 :               = Backend::struct_field_expression (variant_accesser_expr,
     319              :                                                   tuple_pat_index,
     320              :                                                   tuple_pat.get_locus ());
     321           18 :             tree check_expr_sub = CompilePatternCheckExpr::Compile (
     322              :               tuple_pat.get_tuple_pattern (), field_expr, ctx);
     323           18 :             check_expr = Backend::arithmetic_or_logical_expression (
     324              :               ArithmeticOrLogicalOperator::BITWISE_AND, check_expr,
     325              :               check_expr_sub, tuple_pat.get_locus ());
     326              :           }
     327           18 :           break;
     328              : 
     329          130 :         case HIR::StructPatternField::ItemType::IDENT_PAT:
     330          130 :           {
     331          130 :             HIR::StructPatternFieldIdentPat &ident
     332          130 :               = static_cast<HIR::StructPatternFieldIdentPat &> (*field.get ());
     333              : 
     334          130 :             size_t offs = 0;
     335          130 :             ok = variant->lookup_field (ident.get_identifier ().as_string (),
     336              :                                         nullptr, &offs);
     337          130 :             rust_assert (ok);
     338              : 
     339          130 :             tree field_expr
     340          130 :               = Backend::struct_field_expression (variant_accesser_expr, offs,
     341              :                                                   ident.get_locus ());
     342              : 
     343          130 :             tree check_expr_sub
     344          130 :               = CompilePatternCheckExpr::Compile (ident.get_pattern (),
     345              :                                                   field_expr, ctx);
     346          130 :             check_expr = Backend::arithmetic_or_logical_expression (
     347              :               ArithmeticOrLogicalOperator::BITWISE_AND, check_expr,
     348          130 :               check_expr_sub, ident.get_pattern ().get_locus ());
     349              :           }
     350          130 :           break;
     351              : 
     352              :         case HIR::StructPatternField::ItemType::IDENT:
     353              :           {
     354              :             // ident pattern always matches - do nothing
     355              :           }
     356              :           break;
     357              :         }
     358              :     }
     359          139 : }
     360              : 
     361              : void
     362          895 : CompilePatternCheckExpr::visit (HIR::TupleStructPattern &pattern)
     363              : {
     364              :   // lookup the type
     365          895 :   TyTy::BaseType *lookup = nullptr;
     366          895 :   bool ok = ctx->get_tyctx ()->lookup_type (
     367          895 :     pattern.get_path ().get_mappings ().get_hirid (), &lookup);
     368          895 :   rust_assert (ok);
     369              : 
     370              :   // this might be an enum
     371          895 :   rust_assert (lookup->get_kind () == TyTy::TypeKind::ADT);
     372          895 :   TyTy::ADTType *adt = static_cast<TyTy::ADTType *> (lookup);
     373              : 
     374          895 :   int variant_index = 0;
     375          895 :   rust_assert (adt->number_of_variants () > 0);
     376          895 :   TyTy::VariantDef *variant = nullptr;
     377          895 :   if (adt->is_enum ())
     378              :     {
     379              :       // lookup the variant
     380          846 :       HirId variant_id;
     381          846 :       ok = ctx->get_tyctx ()->lookup_variant_definition (
     382          846 :         pattern.get_path ().get_mappings ().get_hirid (), &variant_id);
     383          846 :       rust_assert (ok);
     384              : 
     385          846 :       ok = adt->lookup_variant_by_id (variant_id, &variant, &variant_index);
     386          846 :       rust_assert (ok);
     387              : 
     388              :       // find expected discriminant
     389          846 :       HIR::Expr &discrim_expr = variant->get_discriminant ();
     390          846 :       tree discrim_expr_node = CompileExpr::Compile (discrim_expr, ctx);
     391              : 
     392              :       // find discriminant field of scrutinee
     393          846 :       tree scrutinee_expr_qualifier_expr
     394          846 :         = Backend::struct_field_expression (match_scrutinee_expr, 0,
     395          846 :                                             pattern.get_path ().get_locus ());
     396              : 
     397          846 :       check_expr
     398          846 :         = Backend::comparison_expression (ComparisonOperator::EQUAL,
     399              :                                           scrutinee_expr_qualifier_expr,
     400              :                                           discrim_expr_node,
     401          846 :                                           pattern.get_path ().get_locus ());
     402              :     }
     403              :   else
     404              :     {
     405           49 :       variant = adt->get_variants ().at (0);
     406           49 :       check_expr = boolean_true_node;
     407              :     }
     408              : 
     409          895 :   HIR::TupleStructItems &items = pattern.get_items ();
     410          895 :   switch (items.get_item_type ())
     411              :     {
     412          117 :     case HIR::TupleStructItems::HAS_REST:
     413          117 :       {
     414          117 :         HIR::TupleStructItemsHasRest &items_has_rest
     415              :           = static_cast<HIR::TupleStructItemsHasRest &> (items);
     416          117 :         size_t num_patterns = items_has_rest.get_lower_patterns ().size ()
     417          117 :                               + items_has_rest.get_upper_patterns ().size ();
     418              : 
     419          117 :         rust_assert (num_patterns <= variant->num_fields ());
     420              : 
     421          117 :         tree fields_expr = match_scrutinee_expr;
     422          117 :         if (adt->is_enum ())
     423              :           {
     424              :             // find payload union field of scrutinee
     425           81 :             tree payload_ref
     426           81 :               = Backend::struct_field_expression (match_scrutinee_expr, 1,
     427           81 :                                                   pattern.get_locus ());
     428              : 
     429           81 :             fields_expr
     430           81 :               = Backend::struct_field_expression (payload_ref, variant_index,
     431           81 :                                                   pattern.get_locus ());
     432              :           }
     433              : 
     434          117 :         size_t tuple_field_index = 0;
     435          169 :         for (auto &pattern : items_has_rest.get_lower_patterns ())
     436              :           {
     437           52 :             tree field_expr
     438           52 :               = Backend::struct_field_expression (fields_expr,
     439              :                                                   tuple_field_index++,
     440           52 :                                                   pattern->get_locus ());
     441           52 :             tree check_expr_sub
     442           52 :               = CompilePatternCheckExpr::Compile (*pattern, field_expr, ctx);
     443           52 :             check_expr = Backend::arithmetic_or_logical_expression (
     444              :               ArithmeticOrLogicalOperator::BITWISE_AND, check_expr,
     445           52 :               check_expr_sub, pattern->get_locus ());
     446              :           }
     447          117 :         tuple_field_index = variant->num_fields ()
     448          117 :                             - items_has_rest.get_upper_patterns ().size ();
     449          154 :         for (auto &pattern : items_has_rest.get_upper_patterns ())
     450              :           {
     451           37 :             tree field_expr
     452           37 :               = Backend::struct_field_expression (fields_expr,
     453              :                                                   tuple_field_index++,
     454           37 :                                                   pattern->get_locus ());
     455           37 :             tree check_expr_sub
     456           37 :               = CompilePatternCheckExpr::Compile (*pattern, field_expr, ctx);
     457           37 :             check_expr = Backend::arithmetic_or_logical_expression (
     458              :               ArithmeticOrLogicalOperator::BITWISE_AND, check_expr,
     459           37 :               check_expr_sub, pattern->get_locus ());
     460              :           }
     461              :       }
     462              :       break;
     463              : 
     464          778 :     case HIR::TupleStructItems::NO_REST:
     465          778 :       {
     466          778 :         HIR::TupleStructItemsNoRest &items_no_range
     467              :           = static_cast<HIR::TupleStructItemsNoRest &> (items);
     468              : 
     469          778 :         rust_assert (items_no_range.get_patterns ().size ()
     470              :                      == variant->num_fields ());
     471              : 
     472          778 :         if (adt->is_enum ())
     473              :           {
     474          765 :             size_t tuple_field_index = 0;
     475         1627 :             for (auto &pattern : items_no_range.get_patterns ())
     476              :               {
     477              :                 // find payload union field of scrutinee
     478          862 :                 tree payload_ref
     479          862 :                   = Backend::struct_field_expression (match_scrutinee_expr, 1,
     480          862 :                                                       pattern->get_locus ());
     481              : 
     482          862 :                 tree variant_ref
     483          862 :                   = Backend::struct_field_expression (payload_ref,
     484              :                                                       variant_index,
     485          862 :                                                       pattern->get_locus ());
     486              : 
     487          862 :                 tree field_expr
     488          862 :                   = Backend::struct_field_expression (variant_ref,
     489              :                                                       tuple_field_index++,
     490          862 :                                                       pattern->get_locus ());
     491              : 
     492          862 :                 tree check_expr_sub
     493          862 :                   = CompilePatternCheckExpr::Compile (*pattern, field_expr,
     494              :                                                       ctx);
     495          862 :                 check_expr = Backend::arithmetic_or_logical_expression (
     496              :                   ArithmeticOrLogicalOperator::BITWISE_AND, check_expr,
     497          862 :                   check_expr_sub, pattern->get_locus ());
     498              :               }
     499              :           }
     500              :         else
     501              :           {
     502              :             // For non-enum TupleStructPatterns
     503           13 :             size_t tuple_field_index = 0;
     504           34 :             for (auto &pattern : items_no_range.get_patterns ())
     505              :               {
     506           21 :                 tree field_expr
     507           21 :                   = Backend::struct_field_expression (match_scrutinee_expr,
     508              :                                                       tuple_field_index++,
     509           21 :                                                       pattern->get_locus ());
     510              : 
     511           21 :                 tree check_expr_sub
     512           21 :                   = CompilePatternCheckExpr::Compile (*pattern, field_expr,
     513              :                                                       ctx);
     514           21 :                 check_expr = Backend::arithmetic_or_logical_expression (
     515              :                   ArithmeticOrLogicalOperator::BITWISE_AND, check_expr,
     516           21 :                   check_expr_sub, pattern->get_locus ());
     517              :               }
     518              :           }
     519              :         break;
     520              :       }
     521              :     }
     522          895 : }
     523              : 
     524              : void
     525          161 : CompilePatternCheckExpr::visit (HIR::TuplePattern &pattern)
     526              : {
     527          161 :   check_expr = boolean_true_node;
     528              : 
     529          161 :   switch (pattern.get_items ().get_item_type ())
     530              :     {
     531           23 :     case HIR::TuplePatternItems::HAS_REST:
     532           23 :       {
     533           23 :         auto &items
     534           23 :           = static_cast<HIR::TuplePatternItemsHasRest &> (pattern.get_items ());
     535           23 :         size_t tuple_field_index = 0;
     536              : 
     537              :         // lookup the type to find out number of fields
     538           23 :         TyTy::BaseType *ty = nullptr;
     539           23 :         bool ok = ctx->get_tyctx ()->lookup_type (
     540           23 :           pattern.get_mappings ().get_hirid (), &ty);
     541           23 :         rust_assert (ok);
     542           23 :         rust_assert (ty->get_kind () == TyTy::TypeKind::TUPLE);
     543              : 
     544              :         // compile check expr for lower patterns
     545           46 :         for (auto &pat : items.get_lower_patterns ())
     546              :           {
     547           23 :             tree field_expr
     548           23 :               = Backend::struct_field_expression (match_scrutinee_expr,
     549              :                                                   tuple_field_index++,
     550           23 :                                                   pat->get_locus ());
     551              : 
     552           23 :             tree check_expr_sub
     553           23 :               = CompilePatternCheckExpr::Compile (*pat, field_expr, ctx);
     554           23 :             check_expr = Backend::arithmetic_or_logical_expression (
     555              :               ArithmeticOrLogicalOperator::BITWISE_AND, check_expr,
     556           23 :               check_expr_sub, pat->get_locus ());
     557              :           }
     558              : 
     559              :         // skip the fields that are not checked
     560           23 :         tuple_field_index = static_cast<TyTy::TupleType &> (*ty).num_fields ()
     561           23 :                             - items.get_upper_patterns ().size ();
     562              : 
     563              :         // compile check expr for upper patterns
     564           45 :         for (auto &pat : items.get_upper_patterns ())
     565              :           {
     566           22 :             tree field_expr
     567           22 :               = Backend::struct_field_expression (match_scrutinee_expr,
     568              :                                                   tuple_field_index++,
     569           22 :                                                   pat->get_locus ());
     570              : 
     571           22 :             tree check_expr_sub
     572           22 :               = CompilePatternCheckExpr::Compile (*pat, field_expr, ctx);
     573           22 :             check_expr = Backend::arithmetic_or_logical_expression (
     574              :               ArithmeticOrLogicalOperator::BITWISE_AND, check_expr,
     575           22 :               check_expr_sub, pat->get_locus ());
     576              :           }
     577              :       }
     578           23 :       break;
     579              : 
     580          138 :     case HIR::TuplePatternItems::NO_REST:
     581          138 :       {
     582          138 :         auto &items
     583          138 :           = static_cast<HIR::TuplePatternItemsNoRest &> (pattern.get_items ());
     584          138 :         size_t tuple_field_index = 0;
     585              : 
     586          415 :         for (auto &pat : items.get_patterns ())
     587              :           {
     588          277 :             tree field_expr
     589          277 :               = Backend::struct_field_expression (match_scrutinee_expr,
     590              :                                                   tuple_field_index++,
     591          277 :                                                   pat->get_locus ());
     592              : 
     593          277 :             tree check_expr_sub
     594          277 :               = CompilePatternCheckExpr::Compile (*pat, field_expr, ctx);
     595          277 :             check_expr = Backend::arithmetic_or_logical_expression (
     596              :               ArithmeticOrLogicalOperator::BITWISE_AND, check_expr,
     597          277 :               check_expr_sub, pat->get_locus ());
     598              :           }
     599              :       }
     600              :     }
     601          161 : }
     602              : 
     603              : void
     604          827 : CompilePatternCheckExpr::visit (HIR::IdentifierPattern &pattern)
     605              : {
     606          827 :   if (pattern.has_subpattern ())
     607              :     {
     608            9 :       check_expr = CompilePatternCheckExpr::Compile (pattern.get_subpattern (),
     609              :                                                      match_scrutinee_expr, ctx);
     610              :     }
     611              :   else
     612              :     {
     613          818 :       check_expr = boolean_true_node;
     614              :     }
     615          827 : }
     616              : 
     617              : void
     618           75 : CompilePatternCheckExpr::visit (HIR::SlicePattern &pattern)
     619              : {
     620           75 :   check_expr = boolean_true_node;
     621              : 
     622              :   // lookup the type
     623           75 :   TyTy::BaseType *lookup = nullptr;
     624           75 :   bool ok
     625           75 :     = ctx->get_tyctx ()->lookup_type (pattern.get_mappings ().get_hirid (),
     626              :                                       &lookup);
     627           75 :   rust_assert (ok);
     628              : 
     629              :   // pattern must either be ArrayType or SliceType, should be already confirmed
     630              :   // by type checking
     631           75 :   rust_assert (lookup->get_kind () == TyTy::TypeKind::ARRAY
     632              :                || lookup->get_kind () == TyTy::TypeKind::SLICE
     633              :                || lookup->get_kind () == TyTy::REF);
     634              : 
     635              :   // function ptr that points to either array_index_expression or
     636              :   // slice_index_expression depending on the scrutinee's type
     637           75 :   tree (*scrutinee_index_expr_func) (tree, tree, location_t) = nullptr;
     638              : 
     639           75 :   switch (lookup->get_kind ())
     640              :     {
     641              :     case TyTy::TypeKind::ARRAY:
     642              :       scrutinee_index_expr_func = Backend::array_index_expression;
     643              :       break;
     644            0 :     case TyTy::TypeKind::SLICE:
     645            0 :       rust_sorry_at (
     646              :         pattern.get_locus (),
     647              :         "SlicePattern matching against non-ref slices are not yet supported");
     648            0 :       break;
     649           39 :     case TyTy::TypeKind::REF:
     650           39 :       {
     651           39 :         rust_assert (RS_DST_FLAG_P (TREE_TYPE (match_scrutinee_expr)));
     652           39 :         scrutinee_index_expr_func = Backend::slice_index_expression;
     653           39 :         tree size_field
     654           39 :           = Backend::struct_field_expression (match_scrutinee_expr, 1,
     655           39 :                                               pattern.get_locus ());
     656              : 
     657              :         // for slices, generate a dynamic size comparison expression tree
     658              :         // because size checking is done at runtime.
     659           39 :         switch (pattern.get_items ().get_item_type ())
     660              :           {
     661           16 :           case HIR::SlicePatternItems::ItemType::NO_REST:
     662           16 :             {
     663           16 :               auto &items = static_cast<HIR::SlicePatternItemsNoRest &> (
     664           16 :                 pattern.get_items ());
     665           16 :               check_expr = Backend::comparison_expression (
     666              :                 ComparisonOperator::EQUAL, size_field,
     667           16 :                 build_int_cst (size_type_node, items.get_patterns ().size ()),
     668           16 :                 pattern.get_locus ());
     669              :             }
     670           16 :             break;
     671           23 :           case HIR::SlicePatternItems::ItemType::HAS_REST:
     672           23 :             {
     673           23 :               auto &items = static_cast<HIR::SlicePatternItemsHasRest &> (
     674           23 :                 pattern.get_items ());
     675           23 :               auto pattern_min_cap = items.get_lower_patterns ().size ()
     676           23 :                                      + items.get_upper_patterns ().size ();
     677           23 :               check_expr = Backend::comparison_expression (
     678              :                 ComparisonOperator::GREATER_OR_EQUAL, size_field,
     679           23 :                 build_int_cst (size_type_node, pattern_min_cap),
     680           23 :                 pattern.get_locus ());
     681              :             }
     682           23 :             break;
     683              :           }
     684              :       }
     685              :       break;
     686            0 :     default:
     687            0 :       rust_unreachable ();
     688              :     }
     689              : 
     690           39 :   rust_assert (scrutinee_index_expr_func != nullptr);
     691              : 
     692              :   // Generate tree to compare every element within array/slice
     693           75 :   size_t element_index = 0;
     694           75 :   switch (pattern.get_items ().get_item_type ())
     695              :     {
     696           31 :     case HIR::SlicePatternItems::ItemType::NO_REST:
     697           31 :       {
     698           31 :         auto &items
     699           31 :           = static_cast<HIR::SlicePatternItemsNoRest &> (pattern.get_items ());
     700           92 :         for (auto &pattern_member : items.get_patterns ())
     701              :           {
     702           61 :             tree index_tree
     703           61 :               = Backend::size_constant_expression (element_index++);
     704           61 :             tree element_expr
     705           61 :               = scrutinee_index_expr_func (match_scrutinee_expr, index_tree,
     706           61 :                                            pattern.get_locus ());
     707           61 :             tree check_expr_sub
     708           61 :               = CompilePatternCheckExpr::Compile (*pattern_member, element_expr,
     709              :                                                   ctx);
     710           61 :             check_expr = Backend::arithmetic_or_logical_expression (
     711              :               ArithmeticOrLogicalOperator::BITWISE_AND, check_expr,
     712           61 :               check_expr_sub, pattern.get_locus ());
     713              :           }
     714              :         break;
     715              :       }
     716           44 :     case HIR::SlicePatternItems::ItemType::HAS_REST:
     717           44 :       {
     718           44 :         auto &items
     719           44 :           = static_cast<HIR::SlicePatternItemsHasRest &> (pattern.get_items ());
     720           87 :         for (auto &pattern_member : items.get_lower_patterns ())
     721              :           {
     722           43 :             tree index_tree
     723           43 :               = Backend::size_constant_expression (element_index++);
     724           43 :             tree element_expr
     725           43 :               = scrutinee_index_expr_func (match_scrutinee_expr, index_tree,
     726           43 :                                            pattern.get_locus ());
     727           43 :             tree check_expr_sub
     728           43 :               = CompilePatternCheckExpr::Compile (*pattern_member, element_expr,
     729              :                                                   ctx);
     730           43 :             check_expr = Backend::arithmetic_or_logical_expression (
     731              :               ArithmeticOrLogicalOperator::BITWISE_AND, check_expr,
     732           43 :               check_expr_sub, pattern.get_locus ());
     733              :           }
     734              : 
     735              :         // handle codegen for upper patterns differently for both types
     736           44 :         switch (lookup->get_kind ())
     737              :           {
     738           21 :           case TyTy::TypeKind::ARRAY:
     739           21 :             {
     740              :               // for array type scrutinee, we can simply get the capacity as a
     741              :               // const and calculate how many elements to skip
     742           21 :               auto array_ty = static_cast<TyTy::ArrayType *> (lookup);
     743           21 :               auto capacity_ty = array_ty->get_capacity ();
     744              : 
     745           21 :               rust_assert (capacity_ty->get_kind () == TyTy::TypeKind::CONST);
     746           21 :               auto *capacity_const = capacity_ty->as_const_type ();
     747           21 :               rust_assert (capacity_const->const_kind ()
     748              :                            == TyTy::BaseConstType::ConstKind::Value);
     749           21 :               auto &capacity_value
     750              :                 = *static_cast<TyTy::ConstValueType *> (capacity_const);
     751           21 :               auto cap_tree = capacity_value.get_value ();
     752              : 
     753           21 :               rust_assert (!error_operand_p (cap_tree));
     754              : 
     755           21 :               size_t cap_wi = (size_t) wi::to_wide (cap_tree).to_uhwi ();
     756           21 :               element_index = cap_wi - items.get_upper_patterns ().size ();
     757           42 :               for (auto &pattern_member : items.get_upper_patterns ())
     758              :                 {
     759           21 :                   tree index_tree
     760           21 :                     = Backend::size_constant_expression (element_index++);
     761           21 :                   tree element_expr
     762           21 :                     = scrutinee_index_expr_func (match_scrutinee_expr,
     763              :                                                  index_tree,
     764           21 :                                                  pattern.get_locus ());
     765           21 :                   tree check_expr_sub
     766           21 :                     = CompilePatternCheckExpr::Compile (*pattern_member,
     767              :                                                         element_expr, ctx);
     768           21 :                   check_expr = Backend::arithmetic_or_logical_expression (
     769              :                     ArithmeticOrLogicalOperator::BITWISE_AND, check_expr,
     770           21 :                     check_expr_sub, pattern.get_locus ());
     771              :                 }
     772              :             }
     773              :             break;
     774           23 :           case TyTy::TypeKind::REF:
     775           23 :             {
     776              :               // for slice type scrutinee, size is dyanamic, so number of
     777              :               // elements to skip is calculated during runtime
     778           23 :               tree slice_size
     779           23 :                 = Backend::struct_field_expression (match_scrutinee_expr, 1,
     780           23 :                                                     pattern.get_locus ());
     781           23 :               tree upper_patterns_size = Backend::size_constant_expression (
     782           23 :                 items.get_upper_patterns ().size ());
     783           23 :               tree index_tree = Backend::arithmetic_or_logical_expression (
     784              :                 ArithmeticOrLogicalOperator::SUBTRACT, slice_size,
     785           23 :                 upper_patterns_size, pattern.get_locus ());
     786           45 :               for (auto &pattern_member : items.get_upper_patterns ())
     787              :                 {
     788           22 :                   tree element_expr
     789           22 :                     = scrutinee_index_expr_func (match_scrutinee_expr,
     790              :                                                  index_tree,
     791           22 :                                                  pattern.get_locus ());
     792           22 :                   tree check_expr_sub
     793           22 :                     = CompilePatternCheckExpr::Compile (*pattern_member,
     794              :                                                         element_expr, ctx);
     795           22 :                   check_expr = Backend::arithmetic_or_logical_expression (
     796              :                     ArithmeticOrLogicalOperator::BITWISE_AND, check_expr,
     797           22 :                     check_expr_sub, pattern.get_locus ());
     798           22 :                   index_tree = Backend::arithmetic_or_logical_expression (
     799              :                     ArithmeticOrLogicalOperator::ADD, index_tree,
     800              :                     Backend::size_constant_expression (1),
     801           22 :                     pattern.get_locus ());
     802              :                 }
     803              :             }
     804              :             break;
     805            0 :           default:
     806            0 :             rust_unreachable ();
     807              :           }
     808              :       }
     809              :       break;
     810              :     }
     811           75 : }
     812              : 
     813              : // setup the bindings
     814              : 
     815              : void
     816          884 : CompilePatternBindings::visit (HIR::TupleStructPattern &pattern)
     817              : {
     818              :   // lookup the type
     819          884 :   TyTy::BaseType *lookup = nullptr;
     820          884 :   bool ok = ctx->get_tyctx ()->lookup_type (
     821          884 :     pattern.get_path ().get_mappings ().get_hirid (), &lookup);
     822          884 :   rust_assert (ok);
     823              : 
     824              :   // this must be an enum
     825          884 :   rust_assert (lookup->get_kind () == TyTy::TypeKind::ADT);
     826          884 :   TyTy::ADTType *adt = static_cast<TyTy::ADTType *> (lookup);
     827          884 :   rust_assert (adt->number_of_variants () > 0);
     828              : 
     829          884 :   int variant_index = 0;
     830          884 :   TyTy::VariantDef *variant = adt->get_variants ().at (0);
     831          884 :   if (adt->is_enum ())
     832              :     {
     833          821 :       HirId variant_id = UNKNOWN_HIRID;
     834          821 :       bool ok = ctx->get_tyctx ()->lookup_variant_definition (
     835          821 :         pattern.get_path ().get_mappings ().get_hirid (), &variant_id);
     836          821 :       rust_assert (ok);
     837              : 
     838          821 :       ok = adt->lookup_variant_by_id (variant_id, &variant, &variant_index);
     839          821 :       rust_assert (ok);
     840              :     }
     841              : 
     842          884 :   rust_assert (variant->get_variant_type ()
     843              :                == TyTy::VariantDef::VariantType::TUPLE);
     844              : 
     845          884 :   HIR::TupleStructItems &items = pattern.get_items ();
     846          884 :   switch (items.get_item_type ())
     847              :     {
     848          119 :     case HIR::TupleStructItems::HAS_REST:
     849          119 :       {
     850          119 :         HIR::TupleStructItemsHasRest &items_has_rest
     851              :           = static_cast<HIR::TupleStructItemsHasRest &> (items);
     852          119 :         size_t num_patterns = items_has_rest.get_lower_patterns ().size ()
     853          119 :                               + items_has_rest.get_upper_patterns ().size ();
     854              : 
     855          119 :         rust_assert (num_patterns <= variant->num_fields ());
     856              : 
     857          119 :         tree fields_expr = match_scrutinee_expr;
     858          119 :         if (adt->is_enum ())
     859              :           {
     860              :             // find payload union field of scrutinee
     861           82 :             tree payload_ref
     862           82 :               = Backend::struct_field_expression (match_scrutinee_expr, 1,
     863           82 :                                                   pattern.get_locus ());
     864              : 
     865           82 :             fields_expr
     866           82 :               = Backend::struct_field_expression (payload_ref, variant_index,
     867           82 :                                                   pattern.get_locus ());
     868              :           }
     869              : 
     870          119 :         size_t tuple_field_index = 0;
     871          173 :         for (auto &pattern : items_has_rest.get_lower_patterns ())
     872              :           {
     873           54 :             tree binding
     874           54 :               = Backend::struct_field_expression (fields_expr,
     875              :                                                   tuple_field_index++,
     876           54 :                                                   pattern->get_locus ());
     877              : 
     878           54 :             CompilePatternBindings::Compile (*pattern, binding, ctx);
     879              :           }
     880              : 
     881          119 :         tuple_field_index = variant->num_fields ()
     882          119 :                             - items_has_rest.get_upper_patterns ().size ();
     883              : 
     884          158 :         for (auto &pattern : items_has_rest.get_upper_patterns ())
     885              :           {
     886           39 :             tree binding
     887           39 :               = Backend::struct_field_expression (fields_expr,
     888              :                                                   tuple_field_index++,
     889           39 :                                                   pattern->get_locus ());
     890              : 
     891           39 :             CompilePatternBindings::Compile (*pattern, binding, ctx);
     892              :           }
     893              :       }
     894              :       break;
     895              : 
     896          765 :     case HIR::TupleStructItems::NO_REST:
     897          765 :       {
     898          765 :         HIR::TupleStructItemsNoRest &items_no_rest
     899              :           = static_cast<HIR::TupleStructItemsNoRest &> (items);
     900          765 :         rust_assert (items_no_rest.get_patterns ().size ()
     901              :                      == variant->num_fields ());
     902              : 
     903          765 :         if (adt->is_enum ())
     904              :           {
     905          739 :             size_t tuple_field_index = 0;
     906         1576 :             for (auto &pattern : items_no_rest.get_patterns ())
     907              :               {
     908          837 :                 tree payload_accessor_union
     909          837 :                   = Backend::struct_field_expression (match_scrutinee_expr, 1,
     910          837 :                                                       pattern->get_locus ());
     911              : 
     912          837 :                 tree variant_accessor
     913          837 :                   = Backend::struct_field_expression (payload_accessor_union,
     914              :                                                       variant_index,
     915          837 :                                                       pattern->get_locus ());
     916              : 
     917          837 :                 tree binding
     918          837 :                   = Backend::struct_field_expression (variant_accessor,
     919              :                                                       tuple_field_index++,
     920          837 :                                                       pattern->get_locus ());
     921              : 
     922          837 :                 CompilePatternBindings::Compile (*pattern, binding, ctx);
     923              :               }
     924              :           }
     925              :         else
     926              :           {
     927           26 :             size_t tuple_field_index = 0;
     928           68 :             for (auto &pattern : items_no_rest.get_patterns ())
     929              :               {
     930           42 :                 tree binding
     931           42 :                   = Backend::struct_field_expression (match_scrutinee_expr,
     932              :                                                       tuple_field_index++,
     933           42 :                                                       pattern->get_locus ());
     934              : 
     935           42 :                 CompilePatternBindings::Compile (*pattern, binding, ctx);
     936              :               }
     937              :           }
     938              :       }
     939              :       break;
     940              :     }
     941          884 : }
     942              : 
     943              : tree
     944          228 : CompilePatternBindings::make_struct_access (TyTy::ADTType *adt,
     945              :                                             TyTy::VariantDef *variant,
     946              :                                             const Identifier &ident,
     947              :                                             int variant_index)
     948              : {
     949          228 :   size_t offs = 0;
     950          228 :   auto ok = variant->lookup_field (ident.as_string (), nullptr, &offs);
     951          228 :   rust_assert (ok);
     952              : 
     953          228 :   if (adt->is_enum ())
     954              :     {
     955          189 :       tree payload_accessor_union
     956          189 :         = Backend::struct_field_expression (match_scrutinee_expr, 1,
     957              :                                             ident.get_locus ());
     958              : 
     959          189 :       tree variant_accessor
     960          189 :         = Backend::struct_field_expression (payload_accessor_union,
     961              :                                             variant_index, ident.get_locus ());
     962              : 
     963          189 :       return Backend::struct_field_expression (variant_accessor, offs,
     964          189 :                                                ident.get_locus ());
     965              :     }
     966              :   else
     967              :     {
     968           39 :       tree variant_accessor = match_scrutinee_expr;
     969              : 
     970           39 :       return Backend::struct_field_expression (variant_accessor, offs,
     971           39 :                                                ident.get_locus ());
     972              :     }
     973              : }
     974              : 
     975              : void
     976           93 : CompilePatternBindings::handle_struct_pattern_ident (
     977              :   HIR::StructPatternField &pat, TyTy::ADTType *adt, TyTy::VariantDef *variant,
     978              :   int variant_index)
     979              : {
     980           93 :   HIR::StructPatternFieldIdent &ident
     981              :     = static_cast<HIR::StructPatternFieldIdent &> (pat);
     982              : 
     983           93 :   auto identifier = ident.get_identifier ();
     984           93 :   tree binding = make_struct_access (adt, variant, identifier, variant_index);
     985              : 
     986           93 :   ctx->insert_pattern_binding (ident.get_mappings ().get_hirid (), binding);
     987           93 : }
     988              : 
     989              : void
     990          135 : CompilePatternBindings::handle_struct_pattern_ident_pat (
     991              :   HIR::StructPatternField &pat, TyTy::ADTType *adt, TyTy::VariantDef *variant,
     992              :   int variant_index)
     993              : {
     994          135 :   auto &pattern = static_cast<HIR::StructPatternFieldIdentPat &> (pat);
     995              : 
     996          135 :   tree binding = make_struct_access (adt, variant, pattern.get_identifier (),
     997              :                                      variant_index);
     998          135 :   CompilePatternBindings::Compile (pattern.get_pattern (), binding, ctx);
     999          135 : }
    1000              : 
    1001              : void
    1002           20 : CompilePatternBindings::handle_struct_pattern_tuple_pat (
    1003              :   HIR::StructPatternField &pat, TyTy::ADTType *adt, TyTy::VariantDef *variant,
    1004              :   int variant_index)
    1005              : {
    1006           20 :   HIR::StructPatternFieldTuplePat &tuple_pat
    1007              :     = static_cast<HIR::StructPatternFieldTuplePat &> (pat);
    1008              : 
    1009           20 :   size_t tuple_pat_index = tuple_pat.get_index ();
    1010           20 :   tree binding;
    1011              : 
    1012           20 :   if (adt->is_enum ())
    1013              :     {
    1014            2 :       tree payload_accessor_union
    1015            2 :         = Backend::struct_field_expression (match_scrutinee_expr, 1,
    1016              :                                             pat.get_locus ());
    1017              : 
    1018            2 :       tree variant_accessor
    1019            2 :         = Backend::struct_field_expression (payload_accessor_union,
    1020              :                                             variant_index, pat.get_locus ());
    1021              : 
    1022            2 :       binding
    1023            2 :         = Backend::struct_field_expression (variant_accessor, tuple_pat_index,
    1024              :                                             pat.get_locus ());
    1025              :     }
    1026              :   else
    1027              :     {
    1028           18 :       tree variant_accessor = match_scrutinee_expr;
    1029              : 
    1030           18 :       binding
    1031           18 :         = Backend::struct_field_expression (variant_accessor, tuple_pat_index,
    1032              :                                             pat.get_locus ());
    1033              :     }
    1034              : 
    1035           20 :   CompilePatternBindings::Compile (tuple_pat.get_tuple_pattern (), binding,
    1036              :                                    ctx);
    1037           20 : }
    1038              : 
    1039              : void
    1040          145 : CompilePatternBindings::visit (HIR::StructPattern &pattern)
    1041              : {
    1042              :   // lookup the type
    1043          145 :   TyTy::BaseType *lookup = nullptr;
    1044          145 :   bool ok = ctx->get_tyctx ()->lookup_type (
    1045          145 :     pattern.get_path ().get_mappings ().get_hirid (), &lookup);
    1046          145 :   rust_assert (ok);
    1047              : 
    1048              :   // this must be an enum
    1049          145 :   rust_assert (lookup->get_kind () == TyTy::TypeKind::ADT);
    1050          145 :   TyTy::ADTType *adt = static_cast<TyTy::ADTType *> (lookup);
    1051          145 :   rust_assert (adt->number_of_variants () > 0);
    1052              : 
    1053          145 :   int variant_index = 0;
    1054          145 :   TyTy::VariantDef *variant = adt->get_variants ().at (0);
    1055          145 :   if (adt->is_enum ())
    1056              :     {
    1057          114 :       HirId variant_id = UNKNOWN_HIRID;
    1058          114 :       bool ok = ctx->get_tyctx ()->lookup_variant_definition (
    1059          114 :         pattern.get_path ().get_mappings ().get_hirid (), &variant_id);
    1060          114 :       rust_assert (ok);
    1061              : 
    1062          114 :       ok = adt->lookup_variant_by_id (variant_id, &variant, &variant_index);
    1063          114 :       rust_assert (ok);
    1064              :     }
    1065              : 
    1066          145 :   rust_assert (
    1067              :     variant->get_variant_type () == TyTy::VariantDef::VariantType::STRUCT
    1068              :     || variant->get_variant_type () == TyTy::VariantDef::VariantType::TUPLE);
    1069              : 
    1070          145 :   auto &struct_pattern_elems = pattern.get_struct_pattern_elems ();
    1071          393 :   for (auto &field : struct_pattern_elems.get_struct_pattern_fields ())
    1072              :     {
    1073          248 :       switch (field->get_item_type ())
    1074              :         {
    1075           20 :         case HIR::StructPatternField::ItemType::TUPLE_PAT:
    1076           20 :           handle_struct_pattern_tuple_pat (*field, adt, variant, variant_index);
    1077           20 :           break;
    1078          135 :         case HIR::StructPatternField::ItemType::IDENT_PAT:
    1079          135 :           handle_struct_pattern_ident_pat (*field, adt, variant, variant_index);
    1080          135 :           break;
    1081           93 :         case HIR::StructPatternField::ItemType::IDENT:
    1082           93 :           handle_struct_pattern_ident (*field, adt, variant, variant_index);
    1083           93 :           break;
    1084              :         }
    1085              :     }
    1086          145 : }
    1087              : 
    1088              : void
    1089          196 : CompilePatternBindings::visit (HIR::ReferencePattern &pattern)
    1090              : {
    1091          196 :   tree derefed
    1092          196 :     = indirect_expression (match_scrutinee_expr, pattern.get_locus ());
    1093              : 
    1094          196 :   CompilePatternBindings::Compile (pattern.get_referenced_pattern (), derefed,
    1095              :                                    ctx);
    1096          196 : }
    1097              : 
    1098              : void
    1099          937 : CompilePatternBindings::visit (HIR::IdentifierPattern &pattern)
    1100              : {
    1101          937 :   if (pattern.has_subpattern ())
    1102              :     {
    1103            9 :       CompilePatternBindings::Compile (pattern.get_subpattern (),
    1104              :                                        match_scrutinee_expr, ctx);
    1105              :     }
    1106              : 
    1107          937 :   if (!pattern.get_is_ref ())
    1108              :     {
    1109          936 :       ctx->insert_pattern_binding (pattern.get_mappings ().get_hirid (),
    1110              :                                    match_scrutinee_expr);
    1111          936 :       return;
    1112              :     }
    1113              : 
    1114            1 :   tree ref = address_expression (match_scrutinee_expr,
    1115            1 :                                  EXPR_LOCATION (match_scrutinee_expr));
    1116            1 :   ctx->insert_pattern_binding (pattern.get_mappings ().get_hirid (), ref);
    1117              : }
    1118              : 
    1119              : void
    1120          158 : CompilePatternBindings::visit (HIR::TuplePattern &pattern)
    1121              : {
    1122          158 :   rust_assert (pattern.has_tuple_pattern_items ());
    1123              : 
    1124              :   // lookup the type
    1125          158 :   TyTy::BaseType *ty = nullptr;
    1126          158 :   bool ok
    1127          158 :     = ctx->get_tyctx ()->lookup_type (pattern.get_mappings ().get_hirid (),
    1128              :                                       &ty);
    1129          158 :   rust_assert (ok);
    1130              : 
    1131          158 :   switch (pattern.get_items ().get_item_type ())
    1132              :     {
    1133           26 :     case HIR::TuplePatternItems::ItemType::HAS_REST:
    1134           26 :       {
    1135           26 :         size_t tuple_idx = 0;
    1136           26 :         auto &items
    1137           26 :           = static_cast<HIR::TuplePatternItemsHasRest &> (pattern.get_items ());
    1138              : 
    1139           26 :         auto &items_lower = items.get_lower_patterns ();
    1140           26 :         auto &items_upper = items.get_upper_patterns ();
    1141              : 
    1142           50 :         for (auto &sub : items_lower)
    1143              :           {
    1144           24 :             TyTy::BaseType *ty_sub = nullptr;
    1145           24 :             HirId sub_id = sub->get_mappings ().get_hirid ();
    1146           24 :             bool ok = ctx->get_tyctx ()->lookup_type (sub_id, &ty_sub);
    1147           24 :             rust_assert (ok);
    1148              : 
    1149           24 :             tree sub_init
    1150           24 :               = Backend::struct_field_expression (match_scrutinee_expr,
    1151           24 :                                                   tuple_idx, sub->get_locus ());
    1152              : 
    1153           24 :             CompilePatternBindings::Compile (*sub.get (), sub_init, ctx);
    1154           24 :             tuple_idx++;
    1155              :           }
    1156              : 
    1157           26 :         rust_assert (ty->get_kind () == TyTy::TypeKind::TUPLE);
    1158           26 :         tuple_idx = static_cast<TyTy::TupleType &> (*ty).num_fields ()
    1159           26 :                     - items_upper.size ();
    1160              : 
    1161           49 :         for (auto &sub : items_upper)
    1162              :           {
    1163           23 :             TyTy::BaseType *ty_sub = nullptr;
    1164           23 :             HirId sub_id = sub->get_mappings ().get_hirid ();
    1165           23 :             bool ok = ctx->get_tyctx ()->lookup_type (sub_id, &ty_sub);
    1166           23 :             rust_assert (ok);
    1167              : 
    1168           23 :             tree sub_init
    1169           23 :               = Backend::struct_field_expression (match_scrutinee_expr,
    1170           23 :                                                   tuple_idx, sub->get_locus ());
    1171           23 :             CompilePatternBindings::Compile (*sub.get (), sub_init, ctx);
    1172           23 :             tuple_idx++;
    1173              :           }
    1174              : 
    1175              :         return;
    1176              :       }
    1177          132 :     case HIR::TuplePatternItems::ItemType::NO_REST:
    1178          132 :       {
    1179          132 :         size_t tuple_idx = 0;
    1180          132 :         auto &items
    1181          132 :           = static_cast<HIR::TuplePatternItemsNoRest &> (pattern.get_items ());
    1182              : 
    1183          397 :         for (auto &sub : items.get_patterns ())
    1184              :           {
    1185          265 :             TyTy::BaseType *ty_sub = nullptr;
    1186          265 :             HirId sub_id = sub->get_mappings ().get_hirid ();
    1187          265 :             bool ok = ctx->get_tyctx ()->lookup_type (sub_id, &ty_sub);
    1188          265 :             rust_assert (ok);
    1189              : 
    1190          265 :             tree sub_init
    1191          265 :               = Backend::struct_field_expression (match_scrutinee_expr,
    1192          265 :                                                   tuple_idx, sub->get_locus ());
    1193          265 :             CompilePatternBindings::Compile (*sub.get (), sub_init, ctx);
    1194          265 :             tuple_idx++;
    1195              :           }
    1196              : 
    1197              :         return;
    1198              :       }
    1199            0 :     default:
    1200            0 :       {
    1201            0 :         rust_unreachable ();
    1202              :       }
    1203              :     }
    1204              : }
    1205              : 
    1206              : void
    1207           75 : CompilePatternBindings::visit (HIR::SlicePattern &pattern)
    1208              : {
    1209              :   // lookup the type
    1210           75 :   TyTy::BaseType *lookup = nullptr;
    1211           75 :   bool ok
    1212           75 :     = ctx->get_tyctx ()->lookup_type (pattern.get_mappings ().get_hirid (),
    1213              :                                       &lookup);
    1214           75 :   rust_assert (ok);
    1215              : 
    1216           75 :   rust_assert (lookup->get_kind () == TyTy::TypeKind::ARRAY
    1217              :                || lookup->get_kind () == TyTy::TypeKind::SLICE
    1218              :                || lookup->get_kind () == TyTy::REF);
    1219              : 
    1220              :   // function ptr that points to either array_index_expression or
    1221              :   // slice_index_expression depending on the scrutinee's type
    1222           75 :   tree (*scrutinee_index_expr_func) (tree, tree, location_t) = nullptr;
    1223              : 
    1224           75 :   switch (lookup->get_kind ())
    1225              :     {
    1226              :     case TyTy::TypeKind::ARRAY:
    1227              :       scrutinee_index_expr_func = Backend::array_index_expression;
    1228              :       break;
    1229            0 :     case TyTy::TypeKind::SLICE:
    1230            0 :       rust_sorry_at (pattern.get_locus (),
    1231              :                      "SlicePattern matching against non-ref slices are "
    1232              :                      "not yet supported");
    1233            0 :       break;
    1234              :     case TyTy::TypeKind::REF:
    1235              :       scrutinee_index_expr_func = Backend::slice_index_expression;
    1236              :       break;
    1237            0 :     default:
    1238            0 :       rust_unreachable ();
    1239              :     }
    1240              : 
    1241            0 :   rust_assert (scrutinee_index_expr_func != nullptr);
    1242              : 
    1243           75 :   size_t element_index = 0;
    1244              : 
    1245           75 :   switch (pattern.get_items ().get_item_type ())
    1246              :     {
    1247           31 :     case HIR::SlicePatternItems::ItemType::NO_REST:
    1248           31 :       {
    1249           31 :         auto &items
    1250           31 :           = static_cast<HIR::SlicePatternItemsNoRest &> (pattern.get_items ());
    1251           92 :         for (auto &pattern_member : items.get_patterns ())
    1252              :           {
    1253           61 :             tree index_tree
    1254           61 :               = Backend::size_constant_expression (element_index++);
    1255           61 :             tree element_expr
    1256           61 :               = scrutinee_index_expr_func (match_scrutinee_expr, index_tree,
    1257           61 :                                            pattern.get_locus ());
    1258           61 :             CompilePatternBindings::Compile (*pattern_member, element_expr,
    1259              :                                              ctx);
    1260              :           }
    1261              :       }
    1262              :       break;
    1263           44 :     case HIR::SlicePatternItems::ItemType::HAS_REST:
    1264           44 :       {
    1265           44 :         auto &items
    1266           44 :           = static_cast<HIR::SlicePatternItemsHasRest &> (pattern.get_items ());
    1267              : 
    1268              :         // TODO: support rest_bind (c in [a, b, c @ ..])
    1269           44 :         rust_assert (!items.has_rest_bind ());
    1270              : 
    1271           87 :         for (auto &pattern_member : items.get_lower_patterns ())
    1272              :           {
    1273           43 :             tree index_tree
    1274           43 :               = Backend::size_constant_expression (element_index++);
    1275           43 :             tree element_expr
    1276           43 :               = scrutinee_index_expr_func (match_scrutinee_expr, index_tree,
    1277           43 :                                            pattern.get_locus ());
    1278           43 :             CompilePatternBindings::Compile (*pattern_member, element_expr,
    1279              :                                              ctx);
    1280              :           }
    1281              : 
    1282              :         // handle codegen for upper patterns differently for both types
    1283           44 :         switch (lookup->get_kind ())
    1284              :           {
    1285           21 :           case TyTy::TypeKind::ARRAY:
    1286           21 :             {
    1287           21 :               auto array_ty = static_cast<TyTy::ArrayType *> (lookup);
    1288           21 :               auto capacity_ty = array_ty->get_capacity ();
    1289              : 
    1290           21 :               rust_assert (capacity_ty->get_kind () == TyTy::TypeKind::CONST);
    1291           21 :               auto *capacity_const = capacity_ty->as_const_type ();
    1292           21 :               rust_assert (capacity_const->const_kind ()
    1293              :                            == TyTy::BaseConstType::ConstKind::Value);
    1294           21 :               auto &capacity_value
    1295              :                 = *static_cast<TyTy::ConstValueType *> (capacity_const);
    1296           21 :               auto cap_tree = capacity_value.get_value ();
    1297              : 
    1298           21 :               rust_assert (!error_operand_p (cap_tree));
    1299              : 
    1300           21 :               size_t cap_wi = (size_t) wi::to_wide (cap_tree).to_uhwi ();
    1301           21 :               element_index = cap_wi - items.get_upper_patterns ().size ();
    1302           42 :               for (auto &pattern_member : items.get_upper_patterns ())
    1303              :                 {
    1304           21 :                   tree index_tree
    1305           21 :                     = Backend::size_constant_expression (element_index++);
    1306           21 :                   tree element_expr
    1307           21 :                     = scrutinee_index_expr_func (match_scrutinee_expr,
    1308              :                                                  index_tree,
    1309           21 :                                                  pattern.get_locus ());
    1310           21 :                   CompilePatternBindings::Compile (*pattern_member,
    1311              :                                                    element_expr, ctx);
    1312              :                 }
    1313              :             }
    1314              :             break;
    1315            0 :           case TyTy::TypeKind::SLICE:
    1316            0 :             rust_sorry_at (pattern.get_locus (),
    1317              :                            "SlicePattern matching against non-ref slices are "
    1318              :                            "not yet supported");
    1319            0 :             break;
    1320           23 :           case TyTy::TypeKind::REF:
    1321           23 :             {
    1322           23 :               tree slice_size
    1323           23 :                 = Backend::struct_field_expression (match_scrutinee_expr, 1,
    1324           23 :                                                     pattern.get_locus ());
    1325           23 :               tree upper_patterns_size = Backend::size_constant_expression (
    1326           23 :                 items.get_upper_patterns ().size ());
    1327           23 :               tree index_tree = Backend::arithmetic_or_logical_expression (
    1328              :                 ArithmeticOrLogicalOperator::SUBTRACT, slice_size,
    1329           23 :                 upper_patterns_size, pattern.get_locus ());
    1330           45 :               for (auto &pattern_member : items.get_upper_patterns ())
    1331              :                 {
    1332           22 :                   tree element_expr
    1333           22 :                     = scrutinee_index_expr_func (match_scrutinee_expr,
    1334              :                                                  index_tree,
    1335           22 :                                                  pattern.get_locus ());
    1336           22 :                   CompilePatternBindings::Compile (*pattern_member,
    1337              :                                                    element_expr, ctx);
    1338           22 :                   index_tree = Backend::arithmetic_or_logical_expression (
    1339              :                     ArithmeticOrLogicalOperator::ADD, index_tree,
    1340              :                     Backend::size_constant_expression (1),
    1341           22 :                     pattern.get_locus ());
    1342              :                 }
    1343              :             }
    1344              :             break;
    1345            0 :           default:
    1346            0 :             rust_unreachable ();
    1347              :           }
    1348              :       }
    1349              :       break;
    1350              :     }
    1351           75 : }
    1352              : 
    1353              : //
    1354              : 
    1355              : void
    1356        12624 : CompilePatternLet::visit (HIR::IdentifierPattern &pattern)
    1357              : {
    1358        12624 :   Bvariable *var = nullptr;
    1359        12624 :   rust_assert (
    1360              :     ctx->lookup_var_decl (pattern.get_mappings ().get_hirid (), &var));
    1361              : 
    1362        12624 :   if (pattern.get_is_ref ())
    1363              :     {
    1364            2 :       init_expr = address_expression (init_expr, EXPR_LOCATION (init_expr));
    1365              :     }
    1366              : 
    1367        12624 :   auto fnctx = ctx->peek_fn ();
    1368        12624 :   if (ty->is_unit ())
    1369              :     {
    1370          290 :       ctx->add_statement (init_expr);
    1371              : 
    1372          290 :       auto unit_type_init_expr = unit_expression (rval_locus);
    1373          290 :       auto s = Backend::init_statement (fnctx.fndecl, var, unit_type_init_expr);
    1374          290 :       ctx->add_statement (s);
    1375              :     }
    1376              :   else
    1377              :     {
    1378        12334 :       if (pattern.has_subpattern ())
    1379              :         {
    1380           16 :           CompilePatternLet::Compile (&pattern.get_subpattern (), init_expr, ty,
    1381              :                                       rval_locus, ctx);
    1382              :         }
    1383        12334 :       auto s = Backend::init_statement (fnctx.fndecl, var, init_expr);
    1384        12334 :       ctx->add_statement (s);
    1385              :     }
    1386              : 
    1387        12624 :   DropBuilder drop_builder (*ctx);
    1388        12624 :   tree set_drop_flag
    1389        12624 :     = drop_builder.drop_flag_assignment (pattern.get_mappings ().get_hirid (),
    1390        12624 :                                          true, pattern.get_locus ());
    1391        12624 :   if (set_drop_flag != nullptr)
    1392            1 :     ctx->add_statement (set_drop_flag);
    1393              : 
    1394        12624 :   TyTy::BaseType *drop_ty = ty;
    1395        12624 :   if (pattern.get_is_ref ())
    1396              :     {
    1397            2 :       auto ref_ty = ty->try_as<TyTy::ReferenceType> ();
    1398            0 :       rust_assert (ref_ty != nullptr);
    1399            2 :       drop_ty = ref_ty->get_base ();
    1400              :     }
    1401              : 
    1402        12624 :   if (!CompileDrop (ctx).type_has_drop_impl (drop_ty))
    1403        12568 :     return;
    1404              : 
    1405           56 :   if (!pattern.has_subpattern () && !pattern.get_is_ref ())
    1406              :     {
    1407           55 :       drop_builder.note_simple_drop_candidate (
    1408           55 :         pattern.get_mappings ().get_hirid (), pattern.get_locus ());
    1409              :     }
    1410              :   else
    1411            1 :     rust_sorry_at (pattern.get_locus (),
    1412              :                    "drop trait not supported for subpatterns and ref patterns");
    1413              : }
    1414              : 
    1415              : void
    1416          251 : CompilePatternLet::visit (HIR::WildcardPattern &pattern)
    1417              : {
    1418          251 :   tree init_stmt = NULL;
    1419          251 :   tree stmt_type = TyTyResolveCompile::compile (ctx, ty);
    1420              : 
    1421          251 :   Backend::temporary_variable (ctx->peek_fn ().fndecl, NULL_TREE, stmt_type,
    1422          251 :                                init_expr, false, pattern.get_locus (),
    1423              :                                &init_stmt);
    1424              : 
    1425          251 :   ctx->add_statement (init_stmt);
    1426          251 : }
    1427              : 
    1428              : void
    1429          373 : CompilePatternLet::visit (HIR::TuplePattern &pattern)
    1430              : {
    1431          373 :   rust_assert (pattern.has_tuple_pattern_items ());
    1432              : 
    1433          373 :   bool has_by_ref = false;
    1434          373 :   auto check_refs
    1435          378 :     = [] (const std::vector<std::unique_ptr<HIR::Pattern>> &patterns) {
    1436         1130 :         for (const auto &sub : patterns)
    1437              :           {
    1438          753 :             switch (sub->get_pattern_type ())
    1439              :               {
    1440          734 :               case HIR::Pattern::PatternType::IDENTIFIER:
    1441          734 :                 {
    1442          734 :                   auto id = static_cast<HIR::IdentifierPattern *> (sub.get ());
    1443          734 :                   if (id->get_is_ref ())
    1444              :                     return true;
    1445              :                   break;
    1446              :                 }
    1447              :               case HIR::Pattern::PatternType::REFERENCE:
    1448              :                 return true;
    1449              :               default:
    1450              :                 break;
    1451              :               }
    1452              :           }
    1453              :         return false;
    1454              :       };
    1455          373 :   switch (pattern.get_items ().get_item_type ())
    1456              :     {
    1457          368 :     case HIR::TuplePatternItems::ItemType::NO_REST:
    1458          368 :       {
    1459          368 :         auto &items
    1460          368 :           = static_cast<HIR::TuplePatternItemsNoRest &> (pattern.get_items ());
    1461          368 :         has_by_ref = check_refs (items.get_patterns ());
    1462          368 :         break;
    1463              :       }
    1464            5 :     case HIR::TuplePatternItems::ItemType::HAS_REST:
    1465            5 :       {
    1466            5 :         auto &items
    1467            5 :           = static_cast<HIR::TuplePatternItemsHasRest &> (pattern.get_items ());
    1468            5 :         has_by_ref = check_refs (items.get_lower_patterns ())
    1469            5 :                      || check_refs (items.get_upper_patterns ());
    1470              :         break;
    1471              :       }
    1472              :     default:
    1473              :       break;
    1474              :     }
    1475              : 
    1476          373 :   tree rhs_tuple_type = TYPE_MAIN_VARIANT (TREE_TYPE (init_expr));
    1477          373 :   tree init_stmt;
    1478          373 :   Bvariable *tmp_var
    1479          373 :     = Backend::temporary_variable (ctx->peek_fn ().fndecl, NULL_TREE,
    1480              :                                    rhs_tuple_type, init_expr, has_by_ref,
    1481          373 :                                    pattern.get_locus (), &init_stmt);
    1482          373 :   tree access_expr = Backend::var_expression (tmp_var, pattern.get_locus ());
    1483          373 :   ctx->add_statement (init_stmt);
    1484              : 
    1485          373 :   switch (pattern.get_items ().get_item_type ())
    1486              :     {
    1487            5 :     case HIR::TuplePatternItems::ItemType::HAS_REST:
    1488            5 :       {
    1489            5 :         size_t tuple_idx = 0;
    1490            5 :         auto &items
    1491            5 :           = static_cast<HIR::TuplePatternItemsHasRest &> (pattern.get_items ());
    1492              : 
    1493            5 :         auto &items_lower = items.get_lower_patterns ();
    1494            5 :         auto &items_upper = items.get_upper_patterns ();
    1495              : 
    1496            8 :         for (auto &sub : items_lower)
    1497              :           {
    1498            3 :             TyTy::BaseType *ty_sub = nullptr;
    1499            3 :             HirId sub_id = sub->get_mappings ().get_hirid ();
    1500            3 :             bool ok = ctx->get_tyctx ()->lookup_type (sub_id, &ty_sub);
    1501            3 :             rust_assert (ok);
    1502              : 
    1503            3 :             tree sub_init
    1504            3 :               = Backend::struct_field_expression (access_expr, tuple_idx,
    1505            3 :                                                   sub->get_locus ());
    1506            3 :             CompilePatternLet::Compile (sub.get (), sub_init, ty_sub,
    1507              :                                         rval_locus, ctx);
    1508            3 :             tuple_idx++;
    1509              :           }
    1510              : 
    1511            5 :         rust_assert (ty->get_kind () == TyTy::TypeKind::TUPLE);
    1512            5 :         tuple_idx = static_cast<TyTy::TupleType &> (*ty).num_fields ()
    1513            5 :                     - items_upper.size ();
    1514              : 
    1515            8 :         for (auto &sub : items_upper)
    1516              :           {
    1517            3 :             TyTy::BaseType *ty_sub = nullptr;
    1518            3 :             HirId sub_id = sub->get_mappings ().get_hirid ();
    1519            3 :             bool ok = ctx->get_tyctx ()->lookup_type (sub_id, &ty_sub);
    1520            3 :             rust_assert (ok);
    1521              : 
    1522            3 :             tree sub_init
    1523            3 :               = Backend::struct_field_expression (access_expr, tuple_idx,
    1524            3 :                                                   sub->get_locus ());
    1525            3 :             CompilePatternLet::Compile (sub.get (), sub_init, ty_sub,
    1526              :                                         rval_locus, ctx);
    1527            3 :             tuple_idx++;
    1528              :           }
    1529              : 
    1530              :         return;
    1531              :       }
    1532          368 :     case HIR::TuplePatternItems::ItemType::NO_REST:
    1533          368 :       {
    1534          368 :         size_t tuple_idx = 0;
    1535          368 :         auto &items
    1536          368 :           = static_cast<HIR::TuplePatternItemsNoRest &> (pattern.get_items ());
    1537              : 
    1538         1116 :         for (auto &sub : items.get_patterns ())
    1539              :           {
    1540          748 :             TyTy::BaseType *ty_sub = nullptr;
    1541          748 :             HirId sub_id = sub->get_mappings ().get_hirid ();
    1542          748 :             bool ok = ctx->get_tyctx ()->lookup_type (sub_id, &ty_sub);
    1543          748 :             rust_assert (ok);
    1544              : 
    1545          748 :             tree sub_init
    1546          748 :               = Backend::struct_field_expression (access_expr, tuple_idx,
    1547          748 :                                                   sub->get_locus ());
    1548          748 :             CompilePatternLet::Compile (sub.get (), sub_init, ty_sub,
    1549              :                                         rval_locus, ctx);
    1550          748 :             tuple_idx++;
    1551              :           }
    1552              : 
    1553              :         return;
    1554              :       }
    1555            0 :     default:
    1556            0 :       {
    1557            0 :         rust_unreachable ();
    1558              :       }
    1559              :     }
    1560              : }
    1561              : 
    1562              : void
    1563           32 : CompilePatternLet::visit (HIR::StructPattern &pattern)
    1564              : {
    1565              :   // lookup the type
    1566           32 :   TyTy::BaseType *lookup = nullptr;
    1567           32 :   bool ok = ctx->get_tyctx ()->lookup_type (
    1568           32 :     pattern.get_path ().get_mappings ().get_hirid (), &lookup);
    1569           32 :   rust_assert (ok);
    1570              : 
    1571           32 :   rust_assert (lookup->get_kind () == TyTy::TypeKind::ADT);
    1572           32 :   TyTy::ADTType *adt = static_cast<TyTy::ADTType *> (lookup);
    1573              : 
    1574              :   // only structs and single-variant enums are irrefutable, this check should
    1575              :   // already be handled by type check
    1576           32 :   rust_assert (adt->number_of_variants () == 1);
    1577              : 
    1578           32 :   int variant_index = 0;
    1579           32 :   TyTy::VariantDef *variant = nullptr;
    1580           32 :   if (adt->is_enum ())
    1581              :     {
    1582              :       // lookup the variant
    1583            0 :       HirId variant_id = UNKNOWN_HIRID;
    1584            0 :       bool ok = ctx->get_tyctx ()->lookup_variant_definition (
    1585            0 :         pattern.get_path ().get_mappings ().get_hirid (), &variant_id);
    1586            0 :       rust_assert (ok);
    1587              : 
    1588            0 :       ok = adt->lookup_variant_by_id (variant_id, &variant, &variant_index);
    1589            0 :       rust_assert (ok);
    1590              :     }
    1591              :   else
    1592              :     {
    1593           32 :       variant = adt->get_variants ().at (0);
    1594              :     }
    1595              : 
    1596           32 :   bool has_by_ref = false;
    1597           32 :   auto &struct_pattern_elems = pattern.get_struct_pattern_elems ();
    1598           65 :   for (auto &field : struct_pattern_elems.get_struct_pattern_fields ())
    1599              :     {
    1600           33 :       if (field->get_item_type () == HIR::StructPatternField::ItemType::IDENT)
    1601              :         {
    1602            8 :           HIR::StructPatternFieldIdent &ident
    1603            8 :             = static_cast<HIR::StructPatternFieldIdent &> (*field);
    1604            8 :           if (ident.get_has_ref ())
    1605           33 :             has_by_ref = true;
    1606              :         }
    1607              :     }
    1608              : 
    1609           32 :   tree rhs_type = TYPE_MAIN_VARIANT (TREE_TYPE (init_expr));
    1610           32 :   tree init_stmt;
    1611           32 :   Bvariable *tmp_var
    1612           32 :     = Backend::temporary_variable (ctx->peek_fn ().fndecl, NULL_TREE, rhs_type,
    1613           32 :                                    init_expr, has_by_ref, pattern.get_locus (),
    1614              :                                    &init_stmt);
    1615           32 :   ctx->add_statement (init_stmt);
    1616           32 :   tree access_expr = Backend::var_expression (tmp_var, pattern.get_locus ());
    1617              : 
    1618           57 :   auto make_ident_field_access = [&] (const Identifier &ident, location_t loc) {
    1619           25 :     size_t offs = 0;
    1620           25 :     bool ok = variant->lookup_field (ident.as_string (), nullptr, &offs);
    1621           25 :     rust_assert (ok);
    1622              : 
    1623           25 :     if (adt->is_enum ())
    1624              :       {
    1625            0 :         tree payload_accessor_union
    1626            0 :           = Backend::struct_field_expression (access_expr, 1, loc);
    1627            0 :         tree variant_accessor
    1628            0 :           = Backend::struct_field_expression (payload_accessor_union,
    1629            0 :                                               variant_index, loc);
    1630            0 :         return Backend::struct_field_expression (variant_accessor, offs, loc);
    1631              :       }
    1632              :     else
    1633              :       {
    1634           25 :         return Backend::struct_field_expression (access_expr, offs, loc);
    1635              :       }
    1636           32 :   };
    1637              : 
    1638           65 :   for (auto &field : struct_pattern_elems.get_struct_pattern_fields ())
    1639              :     {
    1640           33 :       switch (field->get_item_type ())
    1641              :         {
    1642            8 :         case HIR::StructPatternField::ItemType::TUPLE_PAT:
    1643            8 :           {
    1644            8 :             HIR::StructPatternFieldTuplePat &tuple_pat
    1645            8 :               = static_cast<HIR::StructPatternFieldTuplePat &> (*field);
    1646              : 
    1647            8 :             size_t tuple_pat_index = tuple_pat.get_index ();
    1648            8 :             tree field_expr = NULL_TREE;
    1649            8 :             if (adt->is_enum ())
    1650              :               {
    1651            0 :                 tree payload_accessor_union
    1652            0 :                   = Backend::struct_field_expression (access_expr, 1,
    1653              :                                                       tuple_pat.get_locus ());
    1654            0 :                 tree variant_accessor
    1655            0 :                   = Backend::struct_field_expression (payload_accessor_union,
    1656              :                                                       variant_index,
    1657              :                                                       tuple_pat.get_locus ());
    1658            0 :                 field_expr
    1659            0 :                   = Backend::struct_field_expression (variant_accessor,
    1660              :                                                       tuple_pat_index,
    1661              :                                                       tuple_pat.get_locus ());
    1662              :               }
    1663              :             else
    1664              :               {
    1665            8 :                 field_expr
    1666            8 :                   = Backend::struct_field_expression (access_expr,
    1667              :                                                       tuple_pat_index,
    1668              :                                                       tuple_pat.get_locus ());
    1669              :               }
    1670              : 
    1671            8 :             TyTy::BaseType *ty_sub = nullptr;
    1672            8 :             HirId sub_id
    1673            8 :               = tuple_pat.get_tuple_pattern ().get_mappings ().get_hirid ();
    1674            8 :             bool ok = ctx->get_tyctx ()->lookup_type (sub_id, &ty_sub);
    1675            8 :             rust_assert (ok);
    1676              : 
    1677            8 :             CompilePatternLet::Compile (&tuple_pat.get_tuple_pattern (),
    1678              :                                         field_expr, ty_sub, rval_locus, ctx);
    1679              :           }
    1680            8 :           break;
    1681           17 :         case HIR::StructPatternField::ItemType::IDENT_PAT:
    1682           17 :           {
    1683           17 :             HIR::StructPatternFieldIdentPat &ident_pat
    1684           17 :               = static_cast<HIR::StructPatternFieldIdentPat &> (*field);
    1685              : 
    1686           17 :             tree field_expr
    1687           17 :               = make_ident_field_access (ident_pat.get_identifier (),
    1688              :                                          ident_pat.get_locus ());
    1689              : 
    1690           17 :             TyTy::BaseType *ty_sub = nullptr;
    1691           17 :             HirId sub_id
    1692           17 :               = ident_pat.get_pattern ().get_mappings ().get_hirid ();
    1693           17 :             bool ok = ctx->get_tyctx ()->lookup_type (sub_id, &ty_sub);
    1694           17 :             rust_assert (ok);
    1695              : 
    1696           17 :             CompilePatternLet::Compile (&ident_pat.get_pattern (), field_expr,
    1697              :                                         ty_sub, rval_locus, ctx);
    1698              :           }
    1699           17 :           break;
    1700              : 
    1701            8 :         case HIR::StructPatternField::ItemType::IDENT:
    1702            8 :           {
    1703            8 :             HIR::StructPatternFieldIdent &ident
    1704            8 :               = static_cast<HIR::StructPatternFieldIdent &> (*field);
    1705              : 
    1706            8 :             tree field_expr = make_ident_field_access (ident.get_identifier (),
    1707              :                                                        ident.get_locus ());
    1708              : 
    1709            8 :             Bvariable *var = nullptr;
    1710            8 :             ok
    1711            8 :               = ctx->lookup_var_decl (ident.get_mappings ().get_hirid (), &var);
    1712            8 :             rust_assert (ok);
    1713              : 
    1714            8 :             if (ident.get_has_ref ())
    1715              :               {
    1716            0 :                 field_expr
    1717            0 :                   = address_expression (field_expr, EXPR_LOCATION (field_expr));
    1718              :               }
    1719              : 
    1720            8 :             auto fnctx = ctx->peek_fn ();
    1721            8 :             auto s = Backend::init_statement (fnctx.fndecl, var, field_expr);
    1722            8 :             ctx->add_statement (s);
    1723              :           }
    1724            8 :           break;
    1725              :         }
    1726              :     }
    1727           32 : }
    1728              : 
    1729              : } // namespace Compile
    1730              : } // namespace Rust
        

Generated by: LCOV version 2.5.0-beta

LCOV profile is generated on x86_64 machine using following configure options: configure --disable-bootstrap --enable-coverage=opt --enable-languages=c,c++,fortran,go,jit,lto,rust,m2 --enable-host-shared. GCC test suite is run with the built compiler.