LCOV - code coverage report
Current view: top level - gcc/rust/checks/errors/borrowck - rust-bir-builder-internal.h (source / functions) Coverage Total Hit
Test: gcc.info Lines: 90.0 % 250 225
Test Date: 2026-09-12 16:25:28 Functions: 65.3 % 75 49
Legend: Lines:     hit not hit

            Line data    Source code
       1              : // Copyright (C) 2020-2026 Free Software Foundation, Inc.
       2              : 
       3              : // This file is part of GCC.
       4              : 
       5              : // GCC is free software; you can redistribute it and/or modify it under
       6              : // the terms of the GNU General Public License as published by the Free
       7              : // Software Foundation; either version 3, or (at your option) any later
       8              : // version.
       9              : 
      10              : // GCC is distributed in the hope that it will be useful, but WITHOUT ANY
      11              : // WARRANTY; without even the implied warranty of MERCHANTABILITY or
      12              : // FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
      13              : // for more details.
      14              : 
      15              : // You should have received a copy of the GNU General Public License
      16              : // along with GCC; see the file COPYING3.  If not see
      17              : // <http://www.gnu.org/licenses/>.
      18              : 
      19              : #ifndef RUST_BIR_BUILDER_INTERNAL_H
      20              : #define RUST_BIR_BUILDER_INTERNAL_H
      21              : 
      22              : #include "rust-bir-place.h"
      23              : #include "rust-hir-expr.h"
      24              : #include "rust-hir-item.h"
      25              : #include "rust-hir-type-check.h"
      26              : #include "rust-hir-visitor.h"
      27              : #include "rust-bir.h"
      28              : #include "rust-bir-free-region.h"
      29              : #include "rust-finalized-name-resolution-context.h"
      30              : #include "options.h"
      31              : #include "rust-rib.h"
      32              : 
      33              : namespace Rust {
      34              : 
      35              : namespace TyTy {
      36              : 
      37              : using Variance = VarianceAnalysis::Variance;
      38              : 
      39              : class RenumberCtx
      40              : {
      41              :   Polonius::Origin next_region = 0;
      42              : 
      43              : public:
      44              :   Polonius::Origin get_next_region () { return next_region++; }
      45              : };
      46              : 
      47              : } // namespace TyTy
      48              : 
      49              : namespace BIR {
      50              : 
      51              : /** Holds the context of BIR building so that it can be shared/passed between
      52              :  * different builders. */
      53              : struct BuilderContext
      54              : {
      55              :   struct LoopAndLabelCtx
      56              :   {
      57              :     bool is_loop;      // Loop or labelled block
      58              :     NodeId label;      // UNKNOWN_NODEID if no label (loop)
      59              :     PlaceId label_var; // For break with value.
      60              :     BasicBlockId break_bb;
      61              :     BasicBlockId continue_bb; // Only valid for loops
      62              :     ScopeId continue_scope;
      63              :     // Break scope is the parent of the `continue_scope`.
      64              : 
      65            0 :     LoopAndLabelCtx (bool is_loop = false, NodeId label = UNKNOWN_NODEID,
      66              :                      PlaceId label_var = INVALID_PLACE,
      67              :                      BasicBlockId break_bb = INVALID_BB,
      68              :                      BasicBlockId continue_bb = INVALID_BB,
      69              :                      ScopeId continue_scope = INVALID_SCOPE)
      70            0 :       : is_loop (is_loop), label (label), label_var (label_var),
      71            0 :         break_bb (break_bb), continue_bb (continue_bb),
      72            0 :         continue_scope (continue_scope)
      73              :     {}
      74              :   };
      75              : 
      76              :   // External context.
      77              :   Resolver::TypeCheckContext &tyctx;
      78              :   const Resolver2_0::FinalizedNameResolutionContext &resolver;
      79              : 
      80              :   // BIR output
      81              :   BasicBlocks basic_blocks;
      82              :   BasicBlockId current_bb = ENTRY_BASIC_BLOCK;
      83              : 
      84              :   /**
      85              :    * Allocation and lookup of places (variables, temporaries, paths, and
      86              :    * constants)
      87              :    */
      88              :   PlaceDB place_db;
      89              :   RegionBinder region_binder{place_db.expose_next_free_region ()};
      90              : 
      91              :   // Used for cleaner dump.
      92              :   std::vector<PlaceId> arguments;
      93              :   /**
      94              :    * Since labels can be used to return values, we need to reserve a place for
      95              :    * them. This map associates labels with their respective places.
      96              :    */
      97              :   std::unordered_map<NodeId, PlaceId> label_place_map;
      98              : 
      99              :   /** Context for current situation (loop, label, etc.) */
     100              :   std::vector<LoopAndLabelCtx> loop_and_label_stack;
     101              : 
     102              :   FreeRegions fn_free_regions{{}};
     103              : 
     104              : public:
     105           47 :   BuilderContext ()
     106           47 :     : tyctx (*Resolver::TypeCheckContext::get ()),
     107           47 :       resolver (Resolver2_0::FinalizedNameResolutionContext::get ())
     108              :   {
     109           47 :     basic_blocks.emplace_back (); // StartBB
     110           47 :   }
     111              : 
     112          195 :   BasicBlock &get_current_bb () { return basic_blocks[current_bb]; }
     113              : 
     114              :   const LoopAndLabelCtx &lookup_label (NodeId label)
     115              :   {
     116              :     auto label_match = [label] (const LoopAndLabelCtx &info) {
     117              :       return info.label != UNKNOWN_NODEID && info.label == label;
     118              :     };
     119              : 
     120              :     auto found = std::find_if (loop_and_label_stack.rbegin (),
     121              :                                loop_and_label_stack.rend (), label_match);
     122              :     rust_assert (found != loop_and_label_stack.rend ());
     123              :     return *found;
     124              :   }
     125              : };
     126              : 
     127              : /** Common infrastructure for building BIR from HIR. */
     128              : class AbstractBuilder
     129              : {
     130              : protected:
     131              :   BuilderContext &ctx;
     132              : 
     133              :   /**
     134              :    * This emulates the return value of the visitor, to be able to use the
     135              :    * current visitor infrastructure, where the return value is forced to be
     136              :    * void.
     137              :    */
     138              :   PlaceId translated = INVALID_PLACE;
     139              : 
     140              : protected:
     141           60 :   explicit AbstractBuilder (BuilderContext &ctx) : ctx (ctx) {}
     142              : 
     143          115 :   PlaceId declare_variable (const Analysis::NodeMapping &node,
     144              :                             bool user_type_annotation = false)
     145              :   {
     146          115 :     return declare_variable (node, lookup_type (node.get_hirid ()),
     147          115 :                              user_type_annotation);
     148              :   }
     149              : 
     150          115 :   PlaceId declare_variable (const Analysis::NodeMapping &node,
     151              :                             TyTy::BaseType *ty,
     152              :                             bool user_type_annotation = false)
     153              :   {
     154          115 :     const NodeId nodeid = node.get_nodeid ();
     155              : 
     156              :     // In debug mode, check that the variable is not already declared.
     157          115 :     rust_assert (ctx.place_db.lookup_variable (nodeid) == INVALID_PLACE);
     158              : 
     159          115 :     auto place_id = ctx.place_db.add_variable (nodeid, ty);
     160              : 
     161          115 :     if (ctx.place_db.get_current_scope_id () != INVALID_SCOPE)
     162          115 :       push_storage_live (place_id);
     163              : 
     164          115 :     if (user_type_annotation)
     165            0 :       push_user_type_ascription (place_id, ty);
     166              : 
     167          115 :     return place_id;
     168              :   }
     169              : 
     170           21 :   PlaceId declare_argument (const Analysis::NodeMapping &node,
     171              :                             TyTy::BaseType *ty)
     172              :   {
     173           21 :     const NodeId nodeid = node.get_nodeid ();
     174              : 
     175              :     // In debug mode, check that the argument is not already declared.
     176           21 :     rust_assert (ctx.place_db.lookup_variable (nodeid) == INVALID_PLACE);
     177              : 
     178           21 :     return ctx.place_db.add_variable (nodeid, ty);
     179              :   }
     180              : 
     181           57 :   void push_new_scope () { ctx.place_db.push_new_scope (); }
     182              : 
     183          192 :   void push_drop (PlaceId place)
     184              :   {
     185          192 :     ctx.get_current_bb ().statements.push_back (Statement::make_drop (place));
     186          192 :   }
     187              : 
     188           49 :   void push_function_argument_drops ()
     189              :   {
     190           49 :     std::for_each (ctx.arguments.rbegin (), ctx.arguments.rend (),
     191           25 :                    [&] (PlaceId argument) { push_drop (argument); });
     192           49 :   }
     193              : 
     194           55 :   void pop_scope ()
     195              :   {
     196           55 :     auto &scope = ctx.place_db.get_current_scope ();
     197           55 :     if (ctx.place_db.get_current_scope_id () != INVALID_SCOPE)
     198              :       {
     199           55 :         std::for_each (scope.locals.rbegin (), scope.locals.rend (),
     200          161 :                        [&] (PlaceId place) {
     201          161 :                          push_drop (place);
     202          161 :                          push_storage_dead (place);
     203          161 :                        });
     204              :       }
     205           55 :     ctx.place_db.pop_scope ();
     206           55 :   }
     207              : 
     208              :   bool intersection_empty (std::vector<PlaceId> &a, std::vector<PlaceId> &b)
     209              :   {
     210              :     for (auto &place : a)
     211              :       {
     212              :         if (std::find (b.begin (), b.end (), place) != b.end ())
     213              :           return false;
     214              :       }
     215              :     return true;
     216              :   }
     217              : 
     218            2 :   void unwind_until (ScopeId final_scope)
     219              :   {
     220            2 :     auto current_scope_id = ctx.place_db.get_current_scope_id ();
     221            6 :     while (current_scope_id != final_scope)
     222              :       {
     223            4 :         auto &scope = ctx.place_db.get_scope (current_scope_id);
     224              : 
     225              :         // TODO: Perform stable toposort based on `borrowed_by`.
     226              : 
     227            4 :         std::for_each (scope.locals.rbegin (), scope.locals.rend (),
     228            6 :                        [&] (PlaceId place) {
     229            6 :                          push_drop (place);
     230            6 :                          push_storage_dead (place);
     231            6 :                        });
     232            4 :         current_scope_id = scope.parent;
     233              :       }
     234            2 :   }
     235              : 
     236           68 :   FreeRegions bind_regions (std::vector<TyTy::Region> regions,
     237              :                             FreeRegions parent_free_regions)
     238              :   {
     239           68 :     FreeRegions free_regions;
     240           89 :     for (auto &region : regions)
     241              :       {
     242           21 :         if (region.is_early_bound ())
     243              :           {
     244           18 :             free_regions.push_back (parent_free_regions[region.get_index ()]);
     245              :           }
     246            3 :         else if (region.is_static ())
     247              :           {
     248            1 :             free_regions.push_back (STATIC_FREE_REGION);
     249              :           }
     250            2 :         else if (region.is_anonymous ())
     251              :           {
     252            2 :             free_regions.push_back (ctx.place_db.get_next_free_region ());
     253              :           }
     254            0 :         else if (region.is_named ())
     255              :           {
     256            0 :             rust_unreachable (); // FIXME
     257              :           }
     258              :         else
     259              :           {
     260            0 :             rust_sorry_at (UNKNOWN_LOCATION, "Unimplemented");
     261            0 :             rust_unreachable ();
     262              :           }
     263              :       }
     264           68 :     return free_regions;
     265              :   }
     266              : 
     267              : protected: // Helpers to add BIR statements
     268          218 :   void push_assignment (PlaceId lhs, AbstractExpr *rhs, location_t location)
     269              :   {
     270          218 :     ctx.get_current_bb ().statements.push_back (
     271          218 :       Statement::make_assignment (lhs, rhs, location));
     272          218 :     translated = lhs;
     273          218 :   }
     274              : 
     275          115 :   void push_assignment (PlaceId lhs, PlaceId rhs, location_t location)
     276              :   {
     277          115 :     push_assignment (lhs, new Assignment (rhs), location);
     278          115 :   }
     279              : 
     280           48 :   void push_tmp_assignment (AbstractExpr *rhs, TyTy::BaseType *tyty,
     281              :                             location_t location)
     282              :   {
     283           48 :     PlaceId tmp = ctx.place_db.add_temporary (tyty);
     284           48 :     push_storage_live (tmp);
     285           48 :     push_assignment (tmp, rhs, location);
     286           48 :   }
     287              : 
     288           19 :   void push_tmp_assignment (PlaceId rhs, location_t location)
     289              :   {
     290           19 :     push_tmp_assignment (new Assignment (rhs), ctx.place_db[rhs].tyty,
     291              :                          location);
     292           19 :   }
     293              : 
     294            7 :   void push_switch (PlaceId switch_val, location_t location,
     295              :                     std::initializer_list<BasicBlockId> destinations = {})
     296              :   {
     297            7 :     auto copy = move_place (switch_val, location);
     298            7 :     ctx.get_current_bb ().statements.push_back (Statement::make_switch (copy));
     299            7 :     ctx.get_current_bb ().successors.insert (
     300            7 :       ctx.get_current_bb ().successors.end (), destinations);
     301            7 :   }
     302              : 
     303            8 :   void push_goto (BasicBlockId bb)
     304              :   {
     305            8 :     ctx.get_current_bb ().statements.push_back (Statement::make_goto ());
     306            8 :     if (bb != INVALID_BB) // INVALID_BB means the goto will be resolved later.
     307            0 :       ctx.get_current_bb ().successors.push_back (bb);
     308            8 :   }
     309              : 
     310          163 :   void push_storage_live (PlaceId place)
     311              :   {
     312          163 :     ctx.get_current_bb ().statements.push_back (
     313          163 :       Statement::make_storage_live (place));
     314          163 :   }
     315              : 
     316          167 :   void push_storage_dead (PlaceId place)
     317              :   {
     318          167 :     ctx.get_current_bb ().statements.push_back (
     319          167 :       Statement::make_storage_dead (place));
     320          167 :   }
     321              : 
     322            0 :   void push_user_type_ascription (PlaceId place, TyTy::BaseType *ty)
     323              :   {
     324            0 :     ctx.get_current_bb ().statements.push_back (
     325            0 :       Statement::make_user_type_ascription (place, ty));
     326            0 :   }
     327              : 
     328          105 :   void push_fake_read (PlaceId place)
     329              :   {
     330          105 :     ctx.get_current_bb ().statements.push_back (
     331          105 :       Statement::make_fake_read (place));
     332          105 :   }
     333              : 
     334           49 :   void push_return (location_t location)
     335              :   {
     336           49 :     push_function_argument_drops ();
     337              : 
     338           49 :     ctx.get_current_bb ().statements.push_back (
     339           49 :       Statement::make_return (location));
     340           49 :   }
     341              : 
     342           13 :   PlaceId borrow_place (PlaceId place_id, TyTy::BaseType *ty,
     343              :                         location_t location)
     344              :   {
     345           13 :     auto mutability = ty->as<const TyTy::ReferenceType> ()->mutability ();
     346           13 :     auto loan = ctx.place_db.add_loan ({mutability, place_id, location});
     347           13 :     push_tmp_assignment (
     348              :       new BorrowExpr (place_id, loan,
     349           13 :                       ctx.place_db.get_next_free_region ().value),
     350              :       ty, location);
     351           13 :     return translated;
     352              :   }
     353              : 
     354           38 :   PlaceId move_place (PlaceId arg, location_t location)
     355              :   {
     356           38 :     auto &place = ctx.place_db[arg];
     357              : 
     358           38 :     if (place.is_constant ())
     359           11 :       return arg;
     360              : 
     361           27 :     if (place.tyty->is<TyTy::ReferenceType> ())
     362           13 :       return reborrow_place (arg, location);
     363              : 
     364           14 :     if (place.is_rvalue ())
     365            4 :       return arg;
     366              : 
     367           10 :     push_tmp_assignment (arg, location);
     368           10 :     return translated;
     369              :   }
     370              : 
     371           13 :   PlaceId reborrow_place (PlaceId arg, location_t location)
     372              :   {
     373           13 :     auto ty = ctx.place_db[arg].tyty->as<TyTy::ReferenceType> ();
     374           13 :     return borrow_place (ctx.place_db.lookup_or_add_path (Place::DEREF,
     375              :                                                           ty->get_base (), arg),
     376           13 :                          ty, location);
     377              :   }
     378              : 
     379              :   template <typename T>
     380           29 :   void move_all (T &args, std::vector<location_t> locations)
     381              :   {
     382           29 :     rust_assert (args.size () == locations.size ());
     383           29 :     std::transform (args.begin (), args.end (), locations.begin (),
     384           26 :                     args.begin (), [this] (PlaceId arg, location_t location) {
     385           26 :                       return move_place (arg, location);
     386              :                     });
     387           29 :   }
     388              : 
     389              : protected: // CFG helpers
     390           33 :   BasicBlockId new_bb ()
     391              :   {
     392           33 :     ctx.basic_blocks.emplace_back ();
     393           33 :     return {ctx.basic_blocks.size () - 1};
     394              :   }
     395              : 
     396           16 :   BasicBlockId start_new_consecutive_bb ()
     397              :   {
     398           16 :     BasicBlockId bb = new_bb ();
     399           16 :     if (!ctx.get_current_bb ().is_terminated ())
     400              :       {
     401            0 :         push_goto (bb);
     402              :       }
     403              :     else
     404              :       {
     405           16 :         add_jump_to (bb);
     406              :       }
     407           16 :     ctx.current_bb = bb;
     408           16 :     return bb;
     409              :   }
     410              : 
     411           38 :   void add_jump (BasicBlockId from, BasicBlockId to)
     412              :   {
     413           15 :     ctx.basic_blocks[from].successors.emplace_back (to);
     414              :   }
     415              : 
     416           16 :   void add_jump_to (BasicBlockId bb) { add_jump (ctx.current_bb, bb); }
     417              : 
     418              : protected: // HIR resolution helpers
     419              :   template <typename T>
     420          349 :   WARN_UNUSED_RESULT TyTy::BaseType *lookup_type (T &hir_node) const
     421              :   {
     422          349 :     return lookup_type (hir_node.get_mappings ().get_hirid ());
     423              :   }
     424              : 
     425          464 :   WARN_UNUSED_RESULT TyTy::BaseType *lookup_type (HirId hirid) const
     426              :   {
     427          464 :     TyTy::BaseType *type = nullptr;
     428          464 :     bool ok = ctx.tyctx.lookup_type (hirid, &type);
     429          464 :     rust_assert (ok);
     430          464 :     rust_assert (type != nullptr);
     431          464 :     return type;
     432              :   }
     433              : 
     434            0 :   template <typename T> NodeId resolve_label (T &expr)
     435              :   {
     436            0 :     auto res = ctx.resolver.lookup (expr.get_mappings ().get_nodeid (),
     437              :                                     Resolver2_0::Namespace::Labels);
     438            0 :     rust_assert (res.has_value ());
     439            0 :     return res.value ();
     440              :   }
     441              : 
     442            2 :   template <typename T> PlaceId resolve_variable (T &variable)
     443              :   {
     444            2 :     auto res = ctx.resolver.lookup (variable.get_mappings ().get_nodeid (),
     445              :                                     Resolver2_0::Namespace::Values);
     446            2 :     rust_assert (res.has_value ());
     447            2 :     return ctx.place_db.lookup_variable (res.value ());
     448              :   }
     449              : 
     450              :   template <typename T>
     451          119 :   PlaceId resolve_variable_or_fn (T &variable, TyTy::BaseType *ty)
     452              :   {
     453          119 :     ty = (ty) ? ty : lookup_type (variable);
     454              : 
     455              :     // Unlike variables,
     456              :     // functions do not have to be declared in PlaceDB before use.
     457          119 :     if (ty->is<TyTy::FnType> ())
     458           16 :       return ctx.place_db.get_constant (ty);
     459              : 
     460          103 :     auto res = ctx.resolver.lookup (variable.get_mappings ().get_nodeid (),
     461              :                                     Resolver2_0::Namespace::Values);
     462          103 :     rust_assert (res.has_value ());
     463          103 :     return ctx.place_db.lookup_or_add_variable (res.value (), ty);
     464              :   }
     465              : 
     466              : protected: // Implicit conversions.
     467              :   /**
     468              :    * Performs implicit coercions on the `translated` place defined for a
     469              :    * coercion site.
     470              :    *
     471              :    * Reference: https://doc.rust-lang.org/reference/type-coercions.html
     472              :    *
     473              :    * The only coercion relevant to BIR is the autoderef. All other coercions
     474              :    * will be taken in account because the type is extracted from each node and
     475              :    * not derived from operations in HIR/BIR. The borrowck does not care about
     476              :    * type transitions. Lifetimes are not coerced, rather new are created with
     477              :    * defined bounds to the original ones.
     478              :    */
     479           26 :   void coercion_site (PlaceId &place, TyTy::BaseType *expected_ty)
     480              :   {
     481           78 :     auto count_ref_levels = [] (TyTy::BaseType *ty) {
     482           52 :       size_t count = 0;
     483           76 :       while (auto r = ty->try_as<TyTy::ReferenceType> ())
     484              :         {
     485           24 :           ty = r->get_base ();
     486           24 :           count++;
     487           24 :         }
     488           52 :       return count;
     489              :     };
     490              : 
     491           26 :     auto actual_ty = ctx.place_db[place].tyty;
     492              : 
     493           26 :     auto deref_count
     494           26 :       = count_ref_levels (actual_ty) - count_ref_levels (expected_ty);
     495              : 
     496           26 :     for (size_t i = 0; i < deref_count; ++i)
     497              :       {
     498            0 :         actual_ty = actual_ty->as<TyTy::ReferenceType> ()->get_base ();
     499            0 :         place
     500            0 :           = ctx.place_db.lookup_or_add_path (Place::DEREF, actual_ty, place);
     501              :       }
     502           26 :   }
     503              : 
     504              :   /** Dereferences the `translated` place until it is at most one reference
     505              :    * and return the base type. */
     506            5 :   TyTy::BaseType *autoderef (PlaceId &place)
     507              :   {
     508            5 :     auto ty = ctx.place_db[place].tyty;
     509            6 :     while (auto ref_ty = ty->try_as<TyTy::ReferenceType> ())
     510              :       {
     511            1 :         ty = ref_ty->get_base ();
     512            1 :         place = ctx.place_db.lookup_or_add_path (Place::DEREF, ty, place);
     513            1 :       }
     514            5 :     return ty;
     515              :   }
     516              : 
     517              :   void autoref ()
     518              :   {
     519              :     if (ctx.place_db[translated].tyty->get_kind () != TyTy::REF)
     520              :       {
     521              :         // FIXME: not sure how to fetch correct location for this
     522              :         // this function is unused yet, so can ignore for now
     523              :         auto ty = ctx.place_db[translated].tyty;
     524              :         translated
     525              :           = borrow_place (translated,
     526              :                           new TyTy::ReferenceType (ty->get_ref (),
     527              :                                                    TyTy::TyVar (ty->get_ref ()),
     528              :                                                    Mutability::Imm),
     529              :                           UNKNOWN_LOCATION);
     530              :       }
     531              :   }
     532              : };
     533              : 
     534              : class AbstractExprBuilder : public AbstractBuilder,
     535              :                             public HIR::HIRExpressionVisitor
     536              : {
     537              : protected:
     538              :   /**
     539              :    * Optional place for the result of the evaluated expression.
     540              :    * Valid if value is not `INVALID_PLACE`.
     541              :    * Used when return place must be created by caller (return for if-else).
     542              :    */
     543              :   PlaceId expr_return_place = INVALID_PLACE;
     544              : 
     545              : protected:
     546           58 :   explicit AbstractExprBuilder (BuilderContext &ctx,
     547              :                                 PlaceId expr_return_place = INVALID_PLACE)
     548           58 :     : AbstractBuilder (ctx), expr_return_place (expr_return_place)
     549              :   {}
     550              : 
     551              :   /**
     552              :    * Wrapper that provides return value based API inside a visitor which has
     553              :    * to use global state to pass the data around.
     554              :    * @param dst_place Place to assign the produced value to, optionally
     555              :    * allocated by the caller.
     556              :    * */
     557          320 :   PlaceId visit_expr (HIR::Expr &expr, PlaceId dst_place = INVALID_PLACE)
     558              :   {
     559              :     // Save to support proper recursion.
     560          320 :     auto saved = expr_return_place;
     561          320 :     expr_return_place = dst_place;
     562          320 :     translated = INVALID_PLACE;
     563          315 :     expr.accept_vis (*this);
     564          320 :     expr_return_place = saved;
     565          320 :     auto result = translated;
     566          320 :     translated = INVALID_PLACE;
     567          315 :     return result;
     568              :   }
     569              : 
     570              :   /**
     571              :    * Create a return value of a subexpression, which produces an expression.
     572              :    * Use `return_place` for subexpression that only produce a place (look it
     573              :    * up) to avoid needless assignments.
     574              :    *
     575              :    * @param can_panic mark that expression can panic to insert jump to
     576              :    * cleanup.
     577              :    */
     578           71 :   void return_expr (AbstractExpr *expr, TyTy::BaseType *ty, location_t location,
     579              :                     bool can_panic = false)
     580              :   {
     581           71 :     if (expr_return_place != INVALID_PLACE)
     582              :       {
     583           55 :         push_assignment (expr_return_place, expr, location);
     584              :       }
     585              :     else
     586              :       {
     587           16 :         push_tmp_assignment (expr, ty, location);
     588              :       }
     589              : 
     590           71 :     if (can_panic)
     591              :       {
     592           16 :         start_new_consecutive_bb ();
     593              :       }
     594              : 
     595           71 :     if (ty->is<TyTy::ReferenceType> ()
     596           71 :         || ctx.place_db[translated].is_constant ())
     597              :       {
     598           43 :         push_fake_read (translated);
     599              :       }
     600           71 :   }
     601              : 
     602              :   /** Mark place to be a result of processed subexpression. */
     603          198 :   void return_place (PlaceId place, location_t location, bool can_panic = false)
     604              :   {
     605          198 :     if (expr_return_place != INVALID_PLACE)
     606              :       {
     607              :         // Return place is already allocated, no need to defer assignment.
     608           72 :         push_assignment (expr_return_place, place, location);
     609              :       }
     610              :     else
     611              :       {
     612          126 :         translated = place;
     613              :       }
     614              : 
     615          198 :     if (can_panic)
     616              :       {
     617            0 :         start_new_consecutive_bb ();
     618              :       }
     619              : 
     620          198 :     if (ctx.place_db[place].is_constant ())
     621              :       {
     622           62 :         push_fake_read (translated);
     623              :       }
     624          198 :   }
     625              : 
     626              :   /** Explicitly return a unit value. Expression produces no value. */
     627            4 :   void return_unit (HIR::Expr &expr)
     628              :   {
     629            4 :     translated = ctx.place_db.get_constant (lookup_type (expr));
     630            4 :   }
     631              : 
     632           42 :   PlaceId return_borrowed (PlaceId place_id, TyTy::BaseType *ty,
     633              :                            location_t location)
     634              :   {
     635              :     // TODO: deduplicate with borrow_place
     636           42 :     auto loan = ctx.place_db.add_loan (
     637           42 :       {ty->as<const TyTy::ReferenceType> ()->mutability (), place_id,
     638              :        location});
     639           42 :     return_expr (new BorrowExpr (place_id, loan,
     640           42 :                                  ctx.place_db.get_next_free_region ().value),
     641              :                  ty, location);
     642           42 :     return translated;
     643              :   }
     644              : 
     645           20 :   PlaceId take_or_create_return_place (TyTy::BaseType *type)
     646              :   {
     647           20 :     PlaceId result = INVALID_PLACE;
     648           20 :     if (expr_return_place != INVALID_PLACE)
     649              :       {
     650           20 :         result = expr_return_place;
     651           20 :         expr_return_place = INVALID_PLACE;
     652              :       }
     653              :     else
     654              :       {
     655            0 :         result = ctx.place_db.add_temporary (type);
     656            0 :         push_storage_live (result);
     657              :       }
     658           20 :     return result;
     659              :   }
     660              : };
     661              : 
     662              : /**
     663              :  * Helper to convert a pointer to an optional. Maps nullptr to nullopt.
     664              :  * Optionals are mainly used here to provide monadic operations (map) over
     665              :  * possibly null pointers.
     666              :  */
     667              : template <typename T>
     668              : tl::optional<T>
     669              : optional_from_ptr (T ptr)
     670              : {
     671              :   if (ptr != nullptr)
     672              :     return {ptr};
     673              :   else
     674              :     return tl::nullopt;
     675              : }
     676              : 
     677              : } // namespace BIR
     678              : } // namespace Rust
     679              : 
     680              : #endif // RUST_BIR_BUILDER_INTERNAL_H
        

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