LCOV - code coverage report
Current view: top level - gcc/rust/checks/errors/borrowck - rust-bir-builder.h (source / functions) Coverage Total Hit
Test: gcc.info Lines: 0.0 % 74 0
Test Date: 2024-09-28 13:20:55 Functions: 0.0 % 6 0
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: - 0 0

             Branch data     Line data    Source code
       1                 :             : // Copyright (C) 2020-2024 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_H
      20                 :             : #define RUST_BIR_BUILDER_H
      21                 :             : 
      22                 :             : #include "rust-bir-builder-internal.h"
      23                 :             : #include "rust-bir-builder-pattern.h"
      24                 :             : #include "rust-bir-builder-expr-stmt.h"
      25                 :             : 
      26                 :             : namespace Rust {
      27                 :             : namespace BIR {
      28                 :             : 
      29                 :             : /** Top-level builder, which compiles a HIR function into a BIR function. */
      30                 :           0 : class Builder final : public AbstractBuilder
      31                 :             : {
      32                 :             :   std::vector<std::pair<FreeRegion, FreeRegion>> universal_region_bounds;
      33                 :             : 
      34                 :             : public:
      35                 :           0 :   explicit Builder (BuilderContext &ctx) : AbstractBuilder (ctx) {}
      36                 :             : 
      37                 :           0 :   Function build (HIR::Function &function)
      38                 :             :   {
      39                 :           0 :     rust_debug ("BIR::Builder::build function={%s}",
      40                 :             :                 function.get_function_name ().as_string ().c_str ());
      41                 :             : 
      42                 :           0 :     auto fn_ty = lookup_type (function)->as<TyTy::FnType> ();
      43                 :             : 
      44                 :           0 :     handle_lifetime_params (fn_ty->get_num_lifetime_params ());
      45                 :           0 :     handle_lifetime_param_constraints (fn_ty->get_region_constraints ());
      46                 :             : 
      47                 :           0 :     handle_return (fn_ty);
      48                 :             : 
      49                 :           0 :     for (auto &param : function.get_function_params ())
      50                 :           0 :       handle_param (param);
      51                 :             : 
      52                 :           0 :     handle_body (*function.get_definition ());
      53                 :             : 
      54                 :           0 :     return Function{
      55                 :           0 :       std::move (ctx.place_db),
      56                 :           0 :       std::move (ctx.arguments),
      57                 :           0 :       std::move (ctx.basic_blocks),
      58                 :           0 :       std::move (ctx.fn_free_regions),
      59                 :           0 :       std::move (universal_region_bounds),
      60                 :           0 :       function.get_locus (),
      61                 :           0 :     };
      62                 :             :   }
      63                 :             : 
      64                 :             : private:
      65                 :             :   /** Instantiate `num_lifetime_params` free regions. */
      66                 :           0 :   void handle_lifetime_params (size_t num_lifetime_params)
      67                 :             :   {
      68                 :           0 :     std::vector<FreeRegion> function_free_regions;
      69                 :           0 :     for (size_t i = 0; i < num_lifetime_params; i++)
      70                 :             :       {
      71                 :           0 :         function_free_regions.push_back (ctx.place_db.get_next_free_region ());
      72                 :             :       }
      73                 :             : 
      74                 :           0 :     rust_debug ("\tctx.fn_free_region={%s}",
      75                 :             :                 ctx.fn_free_regions.to_string ().c_str ());
      76                 :           0 :     ctx.fn_free_regions.set_from (std::move (function_free_regions));
      77                 :           0 :   }
      78                 :             : 
      79                 :           0 :   void handle_lifetime_param_constraints (
      80                 :             :     const TyTy::RegionConstraints &region_constraints)
      81                 :             :   {
      82                 :           0 :     rust_debug ("\thandle_lifetime_param_constraints");
      83                 :             : 
      84                 :           0 :     for (auto bound : region_constraints.region_region)
      85                 :             :       {
      86                 :           0 :         rust_assert (bound.first.is_early_bound ());
      87                 :           0 :         rust_assert (bound.second.is_early_bound ());
      88                 :             : 
      89                 :           0 :         universal_region_bounds.emplace_back (
      90                 :           0 :           ctx.fn_free_regions[bound.first.get_index ()],
      91                 :           0 :           ctx.fn_free_regions[bound.second.get_index ()]);
      92                 :             : 
      93                 :           0 :         auto last_bound = universal_region_bounds.back ();
      94                 :           0 :         rust_debug ("\t\t %lu: %lu", (unsigned long) last_bound.first,
      95                 :             :                     (unsigned long) last_bound.second);
      96                 :             :       }
      97                 :             : 
      98                 :             :     // TODO: handle type_region constraints
      99                 :           0 :   }
     100                 :             : 
     101                 :           0 :   void handle_return (TyTy::FnType *fn_ty)
     102                 :             :   {
     103                 :           0 :     TyTy::BaseType *return_ty = fn_ty->get_return_type ();
     104                 :             : 
     105                 :           0 :     PlaceId return_place = ctx.place_db.add_temporary (return_ty);
     106                 :           0 :     rust_assert (return_place == RETURN_VALUE_PLACE);
     107                 :             : 
     108                 :             :     // Set return place to use functions regions, not the fresh ones.
     109                 :           0 :     ctx.place_db[return_place].regions
     110                 :           0 :       = bind_regions (Resolver::TypeCheckContext::get ()
     111                 :           0 :                         ->get_variance_analysis_ctx ()
     112                 :           0 :                         .query_type_regions (fn_ty->get_return_type ()),
     113                 :           0 :                       ctx.fn_free_regions);
     114                 :           0 :   }
     115                 :             : 
     116                 :           0 :   void handle_param (HIR::FunctionParam &param)
     117                 :             :   {
     118                 :           0 :     auto param_type = lookup_type (*param.get_param_name ());
     119                 :             : 
     120                 :           0 :     auto &pattern = param.get_param_name ();
     121                 :           0 :     if (pattern->get_pattern_type () == HIR::Pattern::IDENTIFIER
     122                 :           0 :         && !static_cast<HIR::IdentifierPattern &> (*pattern).get_is_ref ())
     123                 :             :       {
     124                 :             :         // Avoid useless temporary variable for parameter to look like MIR.
     125                 :           0 :         translated = declare_variable (pattern->get_mappings ());
     126                 :           0 :         ctx.arguments.push_back (translated);
     127                 :             :       }
     128                 :             :     else
     129                 :             :       {
     130                 :           0 :         translated = ctx.place_db.add_temporary (param_type);
     131                 :           0 :         ctx.arguments.push_back (translated);
     132                 :           0 :         PatternBindingBuilder (ctx, translated, tl::nullopt)
     133                 :           0 :           .go (*param.get_param_name ());
     134                 :             :       }
     135                 :             : 
     136                 :           0 :     rust_assert (param.get_type () != nullptr);
     137                 :             : 
     138                 :             :     // Set parameter place to use functions regions, not the fresh ones.
     139                 :           0 :     ctx.place_db[translated].regions
     140                 :           0 :       = bind_regions (Resolver::TypeCheckContext::get ()
     141                 :           0 :                         ->get_variance_analysis_ctx ()
     142                 :           0 :                         .query_type_regions (param_type),
     143                 :           0 :                       ctx.fn_free_regions);
     144                 :           0 :   }
     145                 :             : 
     146                 :           0 :   void handle_body (HIR::BlockExpr &body)
     147                 :             :   {
     148                 :           0 :     translated = ExprStmtBuilder (ctx).build (body, RETURN_VALUE_PLACE);
     149                 :           0 :     if (!ctx.get_current_bb ().is_terminated ())
     150                 :             :       {
     151                 :           0 :         if (ctx.place_db[RETURN_VALUE_PLACE].tyty->is_unit ())
     152                 :             :           {
     153                 :           0 :             push_assignment (RETURN_VALUE_PLACE,
     154                 :           0 :                              ctx.place_db.get_constant (
     155                 :           0 :                                ctx.place_db[RETURN_VALUE_PLACE].tyty));
     156                 :             :           }
     157                 :           0 :         ctx.get_current_bb ().statements.emplace_back (Statement::Kind::RETURN);
     158                 :             :       }
     159                 :           0 :   }
     160                 :             : };
     161                 :             : 
     162                 :             : } // namespace BIR
     163                 :             : } // namespace Rust
     164                 :             : 
     165                 :             : #endif // RUST_BIR_BUILDER_H
        

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.