LCOV - code coverage report
Current view: top level - gcc/rust/checks/errors/borrowck - rust-bir-builder-expr-stmt.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 39.7 % 370 147
Test Date: 2025-06-28 16:12:38 Functions: 28.0 % 50 14
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
      17                 :             : // not see
      18                 :             : // <http://www.gnu.org/licenses/>.
      19                 :             : 
      20                 :             : #include "rust-bir-builder-expr-stmt.h"
      21                 :             : #include "rust-bir-builder-lazyboolexpr.h"
      22                 :             : #include "rust-bir-builder-pattern.h"
      23                 :             : #include "rust-bir-builder-struct.h"
      24                 :             : #include "rust-hir-expr.h"
      25                 :             : 
      26                 :             : namespace Rust {
      27                 :             : namespace BIR {
      28                 :             : 
      29                 :             : using LoopAndLabelCtx = BuilderContext::LoopAndLabelCtx;
      30                 :             : 
      31                 :             : BuilderContext::LoopAndLabelCtx &
      32                 :           0 : ExprStmtBuilder::setup_loop (HIR::BaseLoopExpr &expr)
      33                 :             : {
      34                 :           0 :   NodeId label
      35                 :           0 :     = (expr.has_loop_label ())
      36                 :           0 :         ? expr.get_loop_label ().get_lifetime ().get_mappings ().get_nodeid ()
      37                 :           0 :         : UNKNOWN_NODEID;
      38                 :           0 :   PlaceId label_var = take_or_create_return_place (lookup_type (expr));
      39                 :             : 
      40                 :           0 :   BasicBlockId continue_bb = new_bb ();
      41                 :           0 :   push_goto (continue_bb);
      42                 :           0 :   ctx.current_bb = continue_bb;
      43                 :             :   // falseUnwind
      44                 :           0 :   start_new_consecutive_bb ();
      45                 :             : 
      46                 :           0 :   BasicBlockId break_bb = new_bb ();
      47                 :             :   // We are still outside the loop block;
      48                 :           0 :   ScopeId continue_scope
      49                 :           0 :     = ctx.place_db.get_current_scope_id ().next_scope_id ();
      50                 :           0 :   ctx.loop_and_label_stack.emplace_back (true, label, label_var, break_bb,
      51                 :             :                                          continue_bb, continue_scope);
      52                 :             : 
      53                 :           0 :   return ctx.loop_and_label_stack.back ();
      54                 :             : }
      55                 :             : 
      56                 :             : BuilderContext::LoopAndLabelCtx &
      57                 :           0 : ExprStmtBuilder::get_label_ctx (HIR::Lifetime &label)
      58                 :             : {
      59                 :           0 :   NodeId label_id = resolve_label (label);
      60                 :           0 :   auto lookup = std::find_if (ctx.loop_and_label_stack.rbegin (),
      61                 :           0 :                               ctx.loop_and_label_stack.rend (),
      62                 :           0 :                               [label_id] (LoopAndLabelCtx &info) {
      63                 :           0 :                                 return info.label == label_id;
      64                 :           0 :                               });
      65                 :           0 :   rust_assert (lookup != ctx.loop_and_label_stack.rend ());
      66                 :           0 :   return *lookup;
      67                 :             : }
      68                 :             : 
      69                 :             : LoopAndLabelCtx &
      70                 :           0 : ExprStmtBuilder::get_unnamed_loop_ctx ()
      71                 :             : {
      72                 :           0 :   auto lookup
      73                 :           0 :     = std::find_if (ctx.loop_and_label_stack.rbegin (),
      74                 :           0 :                     ctx.loop_and_label_stack.rend (),
      75                 :           0 :                     [] (LoopAndLabelCtx &info) { return info.is_loop; });
      76                 :           0 :   rust_assert (lookup != ctx.loop_and_label_stack.rend ());
      77                 :           0 :   return *lookup;
      78                 :             : }
      79                 :             : 
      80                 :             : void
      81                 :           0 : ExprStmtBuilder::visit (HIR::ClosureExpr &expr)
      82                 :             : {
      83                 :           0 :   auto closure_ty = lookup_type (expr)->as<TyTy::ClosureType> ();
      84                 :           0 :   std::vector<PlaceId> captures;
      85                 :           0 :   std::vector<location_t> capture_locations;
      86                 :           0 :   for (auto &capture : closure_ty->get_captures ())
      87                 :             :     {
      88                 :           0 :       captures.push_back (ctx.place_db.lookup_variable (capture));
      89                 :           0 :       auto location = Analysis::Mappings::get ()
      90                 :           0 :                         .lookup_ast_item (capture)
      91                 :           0 :                         .value ()
      92                 :           0 :                         ->get_locus ();
      93                 :           0 :       capture_locations.push_back (location);
      94                 :             :     }
      95                 :           0 :   move_all (captures, capture_locations);
      96                 :             : 
      97                 :             :   // Note: Not a coercion site for captures.
      98                 :           0 :   return_expr (new InitializerExpr (std::move (captures)), lookup_type (expr),
      99                 :             :                expr.get_locus ());
     100                 :           0 : }
     101                 :             : 
     102                 :             : void
     103                 :           6 : ExprStmtBuilder::visit (HIR::StructExprStructFields &fields)
     104                 :             : {
     105                 :           6 :   auto *p_adt_type = lookup_type (fields)->as<TyTy::ADTType> ();
     106                 :           6 :   auto struct_ty = p_adt_type->get_variants ().at (0);
     107                 :           6 :   auto init_values = StructBuilder (ctx, struct_ty).build (fields);
     108                 :             :   // collect fields locations
     109                 :           6 :   std::vector<location_t> field_locations;
     110                 :          12 :   for (auto &field : fields.get_fields ())
     111                 :             :     {
     112                 :           6 :       field_locations.push_back (field->get_locus ());
     113                 :             :     }
     114                 :           6 :   move_all (init_values, field_locations);
     115                 :           6 :   return_expr (new InitializerExpr (std::move (init_values)),
     116                 :             :                lookup_type (fields), fields.get_locus ());
     117                 :           6 : }
     118                 :             : 
     119                 :             : void
     120                 :           0 : ExprStmtBuilder::visit (HIR::StructExprStruct &expr)
     121                 :             : {
     122                 :             :   // There is no way to modify empty struct, which makes them constant.
     123                 :           0 :   return_place (ctx.place_db.get_constant (lookup_type (expr)),
     124                 :             :                 expr.get_locus ());
     125                 :           0 : }
     126                 :             : 
     127                 :             : void
     128                 :          34 : ExprStmtBuilder::visit (HIR::LiteralExpr &expr)
     129                 :             : {
     130                 :             :   // Different literal values of the same type are not distinguished in BIR.
     131                 :          34 :   return_place (ctx.place_db.get_constant (lookup_type (expr)),
     132                 :             :                 expr.get_locus ());
     133                 :          34 : }
     134                 :             : 
     135                 :             : void
     136                 :          42 : ExprStmtBuilder::visit (HIR::BorrowExpr &expr)
     137                 :             : {
     138                 :          42 :   auto operand = visit_expr (expr.get_expr ());
     139                 :          42 :   if (ctx.place_db[operand].is_constant ())
     140                 :             :     {
     141                 :             :       // Cannot borrow a constant, must create a temporary copy.
     142                 :           1 :       push_tmp_assignment (operand, expr.get_locus ());
     143                 :           1 :       operand = translated;
     144                 :             :     }
     145                 :             : 
     146                 :             :   // BorrowExpr cannot be annotated with lifetime.
     147                 :          42 :   return_borrowed (operand, lookup_type (expr), expr.get_locus ());
     148                 :          42 : }
     149                 :             : 
     150                 :             : void
     151                 :          10 : ExprStmtBuilder::visit (HIR::DereferenceExpr &expr)
     152                 :             : {
     153                 :          10 :   auto operand = visit_expr (expr.get_expr ());
     154                 :          10 :   return_place (ctx.place_db.lookup_or_add_path (Place::DEREF,
     155                 :             :                                                  lookup_type (expr), operand),
     156                 :             :                 expr.get_locus ());
     157                 :          10 : }
     158                 :             : 
     159                 :             : void
     160                 :           0 : ExprStmtBuilder::visit (HIR::ErrorPropagationExpr &expr)
     161                 :             : {
     162                 :             :   // TODO: desugar in AST->HIR
     163                 :           0 :   rust_sorry_at (expr.get_locus (), "error propagation is not supported");
     164                 :           0 : }
     165                 :             : 
     166                 :             : void
     167                 :           0 : ExprStmtBuilder::visit (HIR::NegationExpr &expr)
     168                 :             : {
     169                 :           0 :   PlaceId operand = visit_expr (expr.get_expr ());
     170                 :           0 :   return_expr (new Operator<1> (
     171                 :           0 :                  {move_place (operand, expr.get_expr ().get_locus ())}),
     172                 :             :                lookup_type (expr), expr.get_locus ());
     173                 :           0 : }
     174                 :             : 
     175                 :             : void
     176                 :           0 : ExprStmtBuilder::visit (HIR::ArithmeticOrLogicalExpr &expr)
     177                 :             : {
     178                 :           0 :   PlaceId lhs = visit_expr (expr.get_lhs ());
     179                 :           0 :   PlaceId rhs = visit_expr (expr.get_rhs ());
     180                 :           0 :   return_expr (new Operator<2> (
     181                 :           0 :                  {move_place (lhs, expr.get_lhs ().get_locus ()),
     182                 :           0 :                   move_place (rhs, expr.get_rhs ().get_locus ())}),
     183                 :             :                lookup_type (expr), expr.get_locus ());
     184                 :           0 : }
     185                 :             : 
     186                 :             : void
     187                 :           0 : ExprStmtBuilder::visit (HIR::ComparisonExpr &expr)
     188                 :             : {
     189                 :           0 :   PlaceId lhs = visit_expr (expr.get_lhs ());
     190                 :           0 :   PlaceId rhs = visit_expr (expr.get_rhs ());
     191                 :           0 :   return_expr (new Operator<2> (
     192                 :           0 :                  {move_place (lhs, expr.get_lhs ().get_locus ()),
     193                 :           0 :                   move_place (rhs, expr.get_rhs ().get_locus ())}),
     194                 :             :                lookup_type (expr), expr.get_locus ());
     195                 :           0 : }
     196                 :             : 
     197                 :             : void
     198                 :           0 : ExprStmtBuilder::visit (HIR::LazyBooleanExpr &expr)
     199                 :             : {
     200                 :           0 :   return_place (LazyBooleanExprBuilder (ctx, take_or_create_return_place (
     201                 :           0 :                                                lookup_type (expr)))
     202                 :             :                   .build (expr),
     203                 :             :                 expr.get_locus ());
     204                 :           0 : }
     205                 :             : 
     206                 :             : void
     207                 :           0 : ExprStmtBuilder::visit (HIR::TypeCastExpr &expr)
     208                 :             : {
     209                 :           0 :   auto operand = visit_expr (expr.get_expr ());
     210                 :           0 :   return_expr (new Operator<1> ({operand}), lookup_type (expr),
     211                 :             :                expr.get_locus ());
     212                 :           0 : }
     213                 :             : 
     214                 :             : void
     215                 :           5 : ExprStmtBuilder::visit (HIR::AssignmentExpr &expr)
     216                 :             : {
     217                 :           5 :   auto lhs = visit_expr (expr.get_lhs ());
     218                 :           5 :   auto rhs = visit_expr (expr.get_rhs ());
     219                 :           5 :   push_assignment (lhs, rhs, expr.get_locus ());
     220                 :           5 :   translated = INVALID_PLACE;
     221                 :           5 : }
     222                 :             : 
     223                 :             : void
     224                 :           0 : ExprStmtBuilder::visit (HIR::CompoundAssignmentExpr &expr)
     225                 :             : {
     226                 :           0 :   auto lhs = visit_expr (expr.get_lhs ());
     227                 :           0 :   auto rhs = visit_expr (expr.get_rhs ());
     228                 :           0 :   push_assignment (lhs, new Operator<2> ({lhs, rhs}), expr.get_locus ());
     229                 :           0 : }
     230                 :             : 
     231                 :             : void
     232                 :           0 : ExprStmtBuilder::visit (HIR::GroupedExpr &expr)
     233                 :             : {
     234                 :           0 :   return_place (visit_expr (expr.get_expr_in_parens ()), expr.get_locus ());
     235                 :           0 : }
     236                 :             : 
     237                 :             : void
     238                 :           0 : ExprStmtBuilder::visit (HIR::ArrayExpr &expr)
     239                 :             : {
     240                 :           0 :   auto &elems = expr.get_internal_elements ();
     241                 :           0 :   switch (elems.get_array_expr_type ())
     242                 :             :     {
     243                 :           0 :       case HIR::ArrayElems::VALUES: {
     244                 :           0 :         auto &elem_vals = (static_cast<HIR::ArrayElemsValues &> (elems));
     245                 :           0 :         auto init_values = visit_list (elem_vals.get_values ());
     246                 :             :         // collect locations
     247                 :           0 :         std::vector<location_t> value_locations;
     248                 :           0 :         for (auto &value : elem_vals.get_values ())
     249                 :             :           {
     250                 :           0 :             value_locations.push_back (value->get_locus ());
     251                 :             :           }
     252                 :           0 :         move_all (init_values, value_locations);
     253                 :           0 :         return_expr (new InitializerExpr (std::move (init_values)),
     254                 :             :                      lookup_type (expr), expr.get_locus ());
     255                 :           0 :         break;
     256                 :           0 :       }
     257                 :           0 :       case HIR::ArrayElems::COPIED: {
     258                 :           0 :         auto &elem_copied = (static_cast<HIR::ArrayElemsCopied &> (elems));
     259                 :           0 :         auto init = visit_expr (elem_copied.get_elem_to_copy ());
     260                 :           0 :         return_expr (new InitializerExpr ({init}), lookup_type (expr),
     261                 :             :                      expr.get_locus ());
     262                 :           0 :         break;
     263                 :             :       }
     264                 :             :     }
     265                 :           0 : }
     266                 :             : 
     267                 :             : void
     268                 :           0 : ExprStmtBuilder::visit (HIR::ArrayIndexExpr &expr)
     269                 :             : {
     270                 :           0 :   auto lhs = visit_expr (expr.get_array_expr ());
     271                 :           0 :   auto rhs = visit_expr (expr.get_index_expr ());
     272                 :             :   // The index is not tracked in BIR.
     273                 :           0 :   std::ignore = rhs;
     274                 :           0 :   return_place (ctx.place_db.lookup_or_add_path (Place::INDEX,
     275                 :             :                                                  lookup_type (expr), lhs),
     276                 :             :                 expr.get_locus ());
     277                 :           0 : }
     278                 :             : 
     279                 :             : void
     280                 :           0 : ExprStmtBuilder::visit (HIR::TupleExpr &expr)
     281                 :             : {
     282                 :           0 :   std::vector<PlaceId> init_values = visit_list (expr.get_tuple_elems ());
     283                 :           0 :   return_expr (new InitializerExpr (std::move (init_values)),
     284                 :             :                lookup_type (expr), expr.get_locus ());
     285                 :           0 : }
     286                 :             : 
     287                 :             : void
     288                 :           0 : ExprStmtBuilder::visit (HIR::TupleIndexExpr &expr)
     289                 :             : {
     290                 :           0 :   auto tuple = visit_expr (expr.get_tuple_expr ());
     291                 :           0 :   return_place (ctx.place_db.lookup_or_add_path (Place::FIELD,
     292                 :             :                                                  lookup_type (expr), tuple,
     293                 :           0 :                                                  expr.get_tuple_index ()),
     294                 :             :                 expr.get_locus ());
     295                 :           0 : }
     296                 :             : 
     297                 :             : void
     298                 :          11 : ExprStmtBuilder::visit (HIR::CallExpr &expr)
     299                 :             : {
     300                 :          11 :   PlaceId fn = visit_expr (expr.get_fnexpr ());
     301                 :          11 :   std::vector<PlaceId> arguments = visit_list (expr.get_arguments ());
     302                 :             : 
     303                 :          11 :   const auto fn_type
     304                 :          11 :     = ctx.place_db[fn].tyty->as<const TyTy::CallableTypeInterface> ();
     305                 :             : 
     306                 :          22 :   for (size_t i = 0; i < fn_type->get_num_params (); ++i)
     307                 :             :     {
     308                 :          11 :       coercion_site (arguments[i], fn_type->get_param_type_at (i));
     309                 :             :     }
     310                 :             : 
     311                 :             :   // collect parameter locations
     312                 :          11 :   std::vector<location_t> parameter_locations;
     313                 :          22 :   for (auto &parameter : expr.get_arguments ())
     314                 :             :     {
     315                 :          11 :       parameter_locations.push_back (parameter->get_locus ());
     316                 :             :     }
     317                 :          11 :   move_all (arguments, parameter_locations);
     318                 :             : 
     319                 :          11 :   return_expr (new CallExpr (fn, std::move (arguments)), lookup_type (expr),
     320                 :             :                expr.get_locus (), true);
     321                 :          11 : }
     322                 :             : 
     323                 :             : void
     324                 :           0 : ExprStmtBuilder::visit (HIR::InlineAsm &expr)
     325                 :           0 : {}
     326                 :             : 
     327                 :             : void
     328                 :           0 : ExprStmtBuilder::visit (HIR::LlvmInlineAsm &expr)
     329                 :           0 : {}
     330                 :             : 
     331                 :             : void
     332                 :           0 : ExprStmtBuilder::visit (HIR::MethodCallExpr &expr)
     333                 :           0 : {}
     334                 :             : 
     335                 :             : void
     336                 :           5 : ExprStmtBuilder::visit (HIR::FieldAccessExpr &expr)
     337                 :             : {
     338                 :           5 :   auto receiver = visit_expr (expr.get_receiver_expr ());
     339                 :           5 :   auto type = autoderef (receiver);
     340                 :           5 :   rust_assert (type->get_kind () == TyTy::ADT);
     341                 :           5 :   auto adt = type->as<TyTy::ADTType> ();
     342                 :           5 :   rust_assert (!adt->is_enum ());
     343                 :           5 :   rust_assert (adt->number_of_variants () == 1);
     344                 :           5 :   auto struct_ty = adt->get_variants ().at (0);
     345                 :             : 
     346                 :           5 :   TyTy::StructFieldType *field_ty = nullptr;
     347                 :           5 :   size_t field_index = 0;
     348                 :           5 :   bool ok = struct_ty->lookup_field (expr.get_field_name ().as_string (),
     349                 :             :                                      &field_ty, &field_index);
     350                 :           5 :   rust_assert (ok);
     351                 :             : 
     352                 :           5 :   return_place (ctx.place_db.lookup_or_add_path (Place::FIELD,
     353                 :             :                                                  field_ty->get_field_type (),
     354                 :             :                                                  receiver, field_index),
     355                 :             :                 expr.get_locus ());
     356                 :           5 : }
     357                 :             : 
     358                 :             : void
     359                 :          46 : ExprStmtBuilder::visit (HIR::BlockExpr &block)
     360                 :             : {
     361                 :          46 :   push_new_scope ();
     362                 :             : 
     363                 :          46 :   if (block.has_label ())
     364                 :             :     {
     365                 :           0 :       NodeId label
     366                 :           0 :         = block.get_label ().get_lifetime ().get_mappings ().get_nodeid ();
     367                 :           0 :       PlaceId label_var = take_or_create_return_place (lookup_type (block));
     368                 :           0 :       ctx.loop_and_label_stack.emplace_back (
     369                 :           0 :         false, label, label_var, new_bb (), INVALID_BB,
     370                 :           0 :         ctx.place_db.get_current_scope_id ());
     371                 :             :     }
     372                 :             : 
     373                 :             :   // Eliminates dead code after break, continue, return.
     374                 :          46 :   bool unreachable = false;
     375                 :         164 :   for (auto &stmt : block.get_statements ())
     376                 :             :     {
     377                 :         120 :       stmt->accept_vis (*this);
     378                 :         120 :       if (ctx.get_current_bb ().is_terminated ())
     379                 :             :         {
     380                 :             :           unreachable = true;
     381                 :             :           break;
     382                 :             :         }
     383                 :             :     }
     384                 :             : 
     385                 :          46 :   if (block.has_label ())
     386                 :             :     {
     387                 :           0 :       auto block_ctx = ctx.loop_and_label_stack.back ();
     388                 :           0 :       if (block.has_expr () && !unreachable)
     389                 :             :         {
     390                 :           0 :           push_assignment (block_ctx.label_var,
     391                 :             :                            visit_expr (block.get_final_expr ()),
     392                 :             :                            block.get_start_locus ());
     393                 :             :         }
     394                 :           0 :       if (!ctx.get_current_bb ().is_terminated ())
     395                 :             :         {
     396                 :           0 :           push_goto (block_ctx.break_bb);
     397                 :             :         }
     398                 :           0 :       ctx.current_bb = block_ctx.break_bb;
     399                 :           0 :       ctx.loop_and_label_stack.pop_back ();
     400                 :             : 
     401                 :           0 :       return_place (block_ctx.label_var, block.get_start_locus ());
     402                 :             :     }
     403                 :          46 :   else if (block.has_expr () && !unreachable)
     404                 :             :     {
     405                 :          14 :       return_place (visit_expr (block.get_final_expr (),
     406                 :             :                                 take_or_create_return_place (
     407                 :             :                                   lookup_type (block.get_final_expr ()))),
     408                 :             :                     block.get_start_locus ());
     409                 :             :     }
     410                 :             : 
     411                 :          46 :   if (!unreachable)
     412                 :          44 :     pop_scope ();
     413                 :             :   else
     414                 :           2 :     ctx.place_db.pop_scope ();
     415                 :          46 : }
     416                 :             : 
     417                 :             : void
     418                 :           0 : ExprStmtBuilder::visit (HIR::ContinueExpr &cont)
     419                 :             : {
     420                 :           0 :   LoopAndLabelCtx info = cont.has_label () ? get_label_ctx (cont.get_label ())
     421                 :           0 :                                            : get_unnamed_loop_ctx ();
     422                 :           0 :   start_new_consecutive_bb ();
     423                 :           0 :   unwind_until (info.continue_scope);
     424                 :           0 :   push_goto (info.continue_bb);
     425                 :             :   // No code allowed after continue. Handled in BlockExpr.
     426                 :           0 : }
     427                 :             : 
     428                 :             : void
     429                 :           0 : ExprStmtBuilder::visit (HIR::BreakExpr &brk)
     430                 :             : {
     431                 :           0 :   LoopAndLabelCtx info = brk.has_label () ? get_label_ctx (brk.get_label ())
     432                 :           0 :                                           : get_unnamed_loop_ctx ();
     433                 :           0 :   if (brk.has_break_expr ())
     434                 :           0 :     push_assignment (info.label_var, visit_expr (brk.get_expr ()),
     435                 :             :                      brk.get_locus ());
     436                 :             : 
     437                 :           0 :   start_new_consecutive_bb ();
     438                 :           0 :   unwind_until (ctx.place_db.get_scope (info.continue_scope).parent);
     439                 :           0 :   push_goto (info.break_bb);
     440                 :             :   // No code allowed after continue. Handled in BlockExpr.
     441                 :           0 : }
     442                 :             : 
     443                 :             : void
     444                 :           0 : ExprStmtBuilder::visit (HIR::RangeFromToExpr &range)
     445                 :             : {
     446                 :           0 :   auto from = visit_expr (range.get_from_expr ());
     447                 :           0 :   auto to = visit_expr (range.get_to_expr ());
     448                 :           0 :   return_expr (new InitializerExpr ({from, to}), lookup_type (range),
     449                 :             :                range.get_locus ());
     450                 :           0 : }
     451                 :             : 
     452                 :             : void
     453                 :           0 : ExprStmtBuilder::visit (HIR::RangeFromExpr &expr)
     454                 :             : {
     455                 :           0 :   auto from = visit_expr (expr.get_from_expr ());
     456                 :           0 :   return_expr (new InitializerExpr ({from}), lookup_type (expr),
     457                 :             :                expr.get_locus ());
     458                 :           0 : }
     459                 :             : 
     460                 :             : void
     461                 :           0 : ExprStmtBuilder::visit (HIR::RangeToExpr &expr)
     462                 :             : {
     463                 :           0 :   auto to = visit_expr (expr.get_to_expr ());
     464                 :           0 :   return_expr (new InitializerExpr ({to}), lookup_type (expr),
     465                 :             :                expr.get_locus ());
     466                 :           0 : }
     467                 :             : 
     468                 :             : void
     469                 :           0 : ExprStmtBuilder::visit (HIR::RangeFullExpr &expr)
     470                 :             : {
     471                 :           0 :   return_expr (new InitializerExpr ({}), lookup_type (expr), expr.get_locus ());
     472                 :           0 : }
     473                 :             : 
     474                 :             : void
     475                 :           0 : ExprStmtBuilder::visit (HIR::RangeFromToInclExpr &expr)
     476                 :             : {
     477                 :           0 :   auto from = visit_expr (expr.get_from_expr ());
     478                 :           0 :   auto to = visit_expr (expr.get_to_expr ());
     479                 :           0 :   return_expr (new InitializerExpr ({from, to}), lookup_type (expr),
     480                 :             :                expr.get_locus ());
     481                 :           0 : }
     482                 :             : 
     483                 :             : void
     484                 :           0 : ExprStmtBuilder::visit (HIR::RangeToInclExpr &expr)
     485                 :             : {
     486                 :           0 :   auto to = visit_expr (expr.get_to_expr ());
     487                 :           0 :   return_expr (new InitializerExpr ({to}), lookup_type (expr),
     488                 :             :                expr.get_locus ());
     489                 :           0 : }
     490                 :             : 
     491                 :             : void
     492                 :           2 : ExprStmtBuilder::visit (HIR::ReturnExpr &ret)
     493                 :             : {
     494                 :           2 :   if (ret.has_return_expr ())
     495                 :             :     {
     496                 :           2 :       push_assignment (RETURN_VALUE_PLACE,
     497                 :             :                        move_place (visit_expr (ret.get_expr ()),
     498                 :           2 :                                    ret.get_expr ().get_locus ()),
     499                 :           2 :                        ret.get_expr ().get_locus ());
     500                 :             :     }
     501                 :           2 :   unwind_until (ROOT_SCOPE);
     502                 :           2 :   push_return (ret.get_locus ());
     503                 :           2 :   translated = INVALID_PLACE;
     504                 :           2 : }
     505                 :             : 
     506                 :             : void
     507                 :           0 : ExprStmtBuilder::visit (HIR::UnsafeBlockExpr &expr)
     508                 :             : {
     509                 :           0 :   rust_sorry_at (expr.get_locus (), "unsafe blocks are not supported");
     510                 :           0 : }
     511                 :             : 
     512                 :             : void
     513                 :           0 : ExprStmtBuilder::visit (HIR::LoopExpr &expr)
     514                 :             : {
     515                 :           0 :   auto loop = setup_loop (expr);
     516                 :             : 
     517                 :           0 :   std::ignore = visit_expr (expr.get_loop_block ());
     518                 :           0 :   if (!ctx.get_current_bb ().is_terminated ())
     519                 :           0 :     push_goto (loop.continue_bb);
     520                 :             : 
     521                 :           0 :   ctx.current_bb = loop.break_bb;
     522                 :           0 : }
     523                 :             : 
     524                 :             : void
     525                 :           0 : ExprStmtBuilder::visit (HIR::WhileLoopExpr &expr)
     526                 :             : {
     527                 :           0 :   auto loop = setup_loop (expr);
     528                 :             : 
     529                 :           0 :   auto cond_val = visit_expr (expr.get_predicate_expr ());
     530                 :           0 :   auto body_bb = new_bb ();
     531                 :           0 :   push_switch (cond_val, expr.get_locus (), {body_bb, loop.break_bb});
     532                 :             : 
     533                 :           0 :   ctx.current_bb = body_bb;
     534                 :           0 :   std::ignore = visit_expr (expr.get_loop_block ());
     535                 :           0 :   push_goto (loop.continue_bb);
     536                 :             : 
     537                 :           0 :   ctx.current_bb = loop.break_bb;
     538                 :           0 : }
     539                 :             : 
     540                 :             : void
     541                 :           0 : ExprStmtBuilder::visit (HIR::WhileLetLoopExpr &expr)
     542                 :             : {
     543                 :             :   // TODO: Desugar in AST->HIR
     544                 :           0 :   rust_sorry_at (expr.get_locus (), "while let loops are not yet supported");
     545                 :           0 : }
     546                 :             : 
     547                 :             : void
     548                 :           4 : ExprStmtBuilder::visit (HIR::IfExpr &expr)
     549                 :             : {
     550                 :             :   // If without else cannot return a non-unit value (see [E0317]).
     551                 :             : 
     552                 :           4 :   if (expr.get_if_block ().statements.empty ())
     553                 :           4 :     return;
     554                 :             : 
     555                 :           4 :   push_switch (visit_expr (expr.get_if_condition ()), expr.get_locus ());
     556                 :           4 :   BasicBlockId if_block = ctx.current_bb;
     557                 :             : 
     558                 :           4 :   ctx.current_bb = new_bb ();
     559                 :           4 :   BasicBlockId then_start_block = ctx.current_bb;
     560                 :           4 :   std::ignore = visit_expr (expr.get_if_block ());
     561                 :           4 :   if (!ctx.get_current_bb ().is_terminated ())
     562                 :           4 :     push_goto (INVALID_BB); // Resolved later.
     563                 :           4 :   BasicBlockId then_end_block = ctx.current_bb;
     564                 :             : 
     565                 :           4 :   ctx.current_bb = new_bb ();
     566                 :           4 :   BasicBlockId final_block = ctx.current_bb;
     567                 :           4 :   return_unit (expr);
     568                 :             : 
     569                 :             :   // Jumps are added at the end to match rustc MIR order for easier comparison.
     570                 :           4 :   add_jump (if_block, then_start_block);
     571                 :           4 :   add_jump (if_block, final_block);
     572                 :             : 
     573                 :           4 :   auto &then_end_bb = ctx.basic_blocks[then_end_block];
     574                 :           4 :   if (then_end_bb.is_goto_terminated () && then_end_bb.successors.empty ())
     575                 :           4 :     add_jump (then_end_block, final_block);
     576                 :             : }
     577                 :             : 
     578                 :             : void
     579                 :           3 : ExprStmtBuilder::visit (HIR::IfExprConseqElse &expr)
     580                 :             : {
     581                 :           3 :   push_switch (move_place (visit_expr (expr.get_if_condition ()),
     582                 :           3 :                            expr.get_if_condition ().get_locus ()),
     583                 :             :                expr.get_locus ());
     584                 :           3 :   BasicBlockId if_end_bb = ctx.current_bb;
     585                 :             : 
     586                 :           3 :   PlaceId result = take_or_create_return_place (lookup_type (expr));
     587                 :             : 
     588                 :           3 :   ctx.current_bb = new_bb ();
     589                 :           3 :   BasicBlockId then_start_bb = ctx.current_bb;
     590                 :           3 :   std::ignore = visit_expr (expr.get_if_block (), result);
     591                 :           3 :   if (!ctx.get_current_bb ().is_terminated ())
     592                 :           2 :     push_goto (INVALID_BB); // Resolved later.
     593                 :           3 :   BasicBlockId then_end_bb = ctx.current_bb;
     594                 :             : 
     595                 :           3 :   ctx.current_bb = new_bb ();
     596                 :           3 :   BasicBlockId else_start_bb = ctx.current_bb;
     597                 :           3 :   std::ignore = visit_expr (expr.get_else_block (), result);
     598                 :           3 :   if (!ctx.get_current_bb ().is_terminated ())
     599                 :           2 :     push_goto (INVALID_BB); // Resolved later.
     600                 :           3 :   BasicBlockId else_end_bb = ctx.current_bb;
     601                 :             : 
     602                 :           3 :   ctx.current_bb = new_bb ();
     603                 :           3 :   BasicBlockId final_start_bb = ctx.current_bb;
     604                 :           3 :   return_place (result, expr.get_locus ());
     605                 :             : 
     606                 :             :   // Jumps are added at the end to match rustc MIR order for easier comparison.
     607                 :           3 :   add_jump (if_end_bb, then_start_bb);
     608                 :           3 :   add_jump (if_end_bb, else_start_bb);
     609                 :             : 
     610                 :           3 :   auto &then_bb = ctx.basic_blocks[then_end_bb];
     611                 :           3 :   if (then_bb.is_goto_terminated () && then_bb.successors.empty ())
     612                 :           2 :     add_jump (then_end_bb, final_start_bb);
     613                 :             : 
     614                 :           3 :   auto &else_bb = ctx.basic_blocks[else_end_bb];
     615                 :           3 :   if (else_bb.is_goto_terminated () && else_bb.successors.empty ())
     616                 :           2 :     add_jump (else_end_bb, final_start_bb);
     617                 :           3 : }
     618                 :             : 
     619                 :             : void
     620                 :           0 : ExprStmtBuilder::visit (HIR::MatchExpr &expr)
     621                 :             : {
     622                 :           0 :   rust_sorry_at (expr.get_locus (), "match expressions are not supported");
     623                 :             :   //  // TODO
     624                 :             :   //  expr.get_scrutinee_expr ()->accept_vis (*this);
     625                 :             :   //  PlaceId scrutinee = translated;
     626                 :             :   //
     627                 :             :   //  BasicBlockId final_bb = new_bb ();
     628                 :             :   //
     629                 :             :   //  BasicBlockId next_case_bb = new_bb ();
     630                 :             :   //  for (auto &match_case : expr.get_match_cases ())
     631                 :             :   //    {
     632                 :             :   //      BasicBlockId body_bb = new_bb ();
     633                 :             :   //
     634                 :             :   //      BasicBlockId next_pattern_bb = new_bb ();
     635                 :             :   //      for (auto &pat : match_case.get_arm ().get_patterns ())
     636                 :             :   //    {
     637                 :             :   //      compile_pattern_validation (*pat, scrutinee);
     638                 :             :   //      push_switch (translated);
     639                 :             :   //      add_jump_to (next_pattern_bb);
     640                 :             :   //      start_new_subsequent_bb ();
     641                 :             :   //      compile_pattern_bindings (*pat, scrutinee);
     642                 :             :   //      add_jump_to (body_bb);
     643                 :             :   //
     644                 :             :   //      ctx.current_bb = next_pattern_bb;
     645                 :             :   //      next_pattern_bb = new_bb ();
     646                 :             :   //    }
     647                 :             :   //      ctx.current_bb = next_pattern_bb;
     648                 :             :   //      // No pattern matched, go to the next case.
     649                 :             :   //      add_jump_to (next_case_bb);
     650                 :             :   //
     651                 :             :   //      ctx.current_bb = body_bb;
     652                 :             :   //      match_case.get_expr ()->accept_vis (*this);
     653                 :             :   //      add_jump_to (final_bb);
     654                 :             :   //
     655                 :             :   //      ctx.current_bb = next_case_bb;
     656                 :             :   //      next_case_bb = new_bb ();
     657                 :             :   //    }
     658                 :             :   //  add_jump_to (final_bb);
     659                 :             :   //
     660                 :             :   //  ctx.current_bb = final_bb;
     661                 :           0 : }
     662                 :             : 
     663                 :             : void
     664                 :           0 : ExprStmtBuilder::visit (HIR::AwaitExpr &expr)
     665                 :             : {
     666                 :           0 :   rust_sorry_at (expr.get_locus (), "await expressions are not supported");
     667                 :           0 : }
     668                 :             : 
     669                 :             : void
     670                 :           0 : ExprStmtBuilder::visit (HIR::AsyncBlockExpr &expr)
     671                 :             : {
     672                 :           0 :   rust_sorry_at (expr.get_locus (), "async blocks are not supported");
     673                 :           0 : }
     674                 :             : 
     675                 :             : void
     676                 :           0 : ExprStmtBuilder::visit (HIR::QualifiedPathInExpression &expr)
     677                 :             : {
     678                 :             :   // Note: Type is only stored for the expr, not the segment.
     679                 :           0 :   PlaceId result = resolve_variable_or_fn (expr, lookup_type (expr));
     680                 :           0 :   return_place (result, expr.get_locus ());
     681                 :           0 : }
     682                 :             : 
     683                 :             : void
     684                 :         108 : ExprStmtBuilder::visit (HIR::PathInExpression &expr)
     685                 :             : {
     686                 :             :   // Note: Type is only stored for the expr, not the segment.
     687                 :         108 :   PlaceId result = resolve_variable_or_fn (expr, lookup_type (expr));
     688                 :         108 :   return_place (result, expr.get_locus ());
     689                 :         108 : }
     690                 :             : 
     691                 :             : void
     692                 :         103 : ExprStmtBuilder::visit (HIR::LetStmt &stmt)
     693                 :             : {
     694                 :         103 :   tl::optional<PlaceId> init;
     695                 :         103 :   tl::optional<TyTy::BaseType *> type_annotation;
     696                 :             : 
     697                 :         103 :   if (stmt.has_type ())
     698                 :           0 :     type_annotation = lookup_type (stmt.get_type ());
     699                 :             : 
     700                 :         103 :   if (stmt.get_pattern ().get_pattern_type () == HIR::Pattern::IDENTIFIER)
     701                 :             :     {
     702                 :             :       // Only if a pattern is just an identifier, no destructuring is needed.
     703                 :             :       // Hoverer PatternBindingBuilder cannot change existing temporary
     704                 :             :       // (init expr is evaluated before pattern binding) into a
     705                 :             :       // variable, so it would emit extra assignment.
     706                 :         103 :       auto var = declare_variable (stmt.get_pattern ().get_mappings ());
     707                 :         103 :       if (stmt.has_type ())
     708                 :           0 :         push_user_type_ascription (var, lookup_type (stmt.get_type ()));
     709                 :             : 
     710                 :         103 :       if (stmt.has_init_expr ())
     711                 :         103 :         std::ignore = visit_expr (stmt.get_init_expr (), var);
     712                 :             :     }
     713                 :             :   else
     714                 :             :     {
     715                 :           0 :       if (stmt.has_init_expr ())
     716                 :           0 :         init = visit_expr (stmt.get_init_expr ());
     717                 :             : 
     718                 :           0 :       PatternBindingBuilder (ctx, init, type_annotation)
     719                 :           0 :         .go (stmt.get_pattern ());
     720                 :             :     }
     721                 :         103 : }
     722                 :             : 
     723                 :             : void
     724                 :          11 : ExprStmtBuilder::visit (HIR::ExprStmt &stmt)
     725                 :             : {
     726                 :          11 :   PlaceId result = visit_expr (stmt.get_expr ());
     727                 :             :   // We must read the value for current liveness and we must not store it into
     728                 :             :   // the same place.
     729                 :          11 :   if (result != INVALID_PLACE)
     730                 :           4 :     push_tmp_assignment (result, stmt.get_locus ());
     731                 :          11 : }
     732                 :             : } // namespace BIR
     733                 :             : } // 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.