LCOV - code coverage report
Current view: top level - gcc/rust/hir - rust-ast-lower.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 97.4 % 304 296
Test Date: 2025-06-21 16:26:05 Functions: 95.2 % 21 20
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: - 0 0

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

Generated by: LCOV version 2.1-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.