LCOV - code coverage report
Current view: top level - gcc/rust/hir - rust-ast-lower-item.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 95.3 % 494 471
Test Date: 2026-11-14 16:39:50 Functions: 95.0 % 20 19
Legend: Lines:     hit not hit

            Line data    Source code
       1              : // Copyright (C) 2020-2026 Free Software Foundation, Inc.
       2              : 
       3              : // This file is part of GCC.
       4              : 
       5              : // GCC is free software; you can redistribute it and/or modify it under
       6              : // the terms of the GNU General Public License as published by the Free
       7              : // Software Foundation; either version 3, or (at your option) any later
       8              : // version.
       9              : 
      10              : // GCC is distributed in the hope that it will be useful, but WITHOUT ANY
      11              : // WARRANTY; without even the implied warranty of MERCHANTABILITY or
      12              : // FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
      13              : // for more details.
      14              : 
      15              : // You should have received a copy of the GNU General Public License
      16              : // along with GCC; see the file COPYING3.  If not see
      17              : // <http://www.gnu.org/licenses/>.
      18              : 
      19              : #include "rust-ast-lower-item.h"
      20              : #include "rust-diagnostics.h"
      21              : #include "rust-ast-lower.h"
      22              : #include "rust-ast-lower-base.h"
      23              : #include "rust-ast-lower-enumitem.h"
      24              : #include "rust-ast-lower-type.h"
      25              : #include "rust-ast-lower-implitem.h"
      26              : #include "rust-ast-lower-expr.h"
      27              : #include "rust-ast-lower-pattern.h"
      28              : #include "rust-ast-lower-block.h"
      29              : #include "rust-item.h"
      30              : 
      31              : namespace Rust {
      32              : namespace HIR {
      33              : 
      34              : HIR::Item *
      35       124885 : ASTLoweringItem::translate (AST::Item &item)
      36              : {
      37       124885 :   ASTLoweringItem resolver;
      38       124885 :   item.accept_vis (resolver);
      39              : 
      40       124877 :   if (resolver.translated != nullptr)
      41              :     {
      42       115615 :       auto id = resolver.translated->get_mappings ().get_hirid ();
      43       115615 :       auto defid = resolver.translated->get_mappings ().get_defid ();
      44       115615 :       auto locus = resolver.translated->get_locus ();
      45              : 
      46       115615 :       resolver.handle_outer_attributes (*resolver.translated);
      47       115615 :       resolver.mappings.insert_ast_item (&item);
      48       115615 :       resolver.mappings.hir.items.insert (resolver.translated);
      49       115615 :       resolver.mappings.insert_location (id, locus);
      50       115615 :       resolver.mappings.insert_defid_mapping (defid, resolver.translated);
      51              :     }
      52              : 
      53       124877 :   return resolver.translated;
      54       124877 : }
      55              : 
      56              : void
      57         3397 : ASTLoweringItem::visit (AST::Module &module)
      58              : {
      59         3397 :   auto crate_num = mappings.crate.get_current_crate ();
      60         6794 :   Analysis::NodeMapping mapping (crate_num, module.get_node_id (),
      61         3397 :                                  mappings.get_next_hir_id (crate_num),
      62         3397 :                                  mappings.get_next_localdef_id (crate_num));
      63              : 
      64              :   // should be lowered from module.get_vis()
      65         3397 :   HIR::Visibility vis = translate_visibility (module.get_visibility ());
      66              : 
      67         3397 :   auto items = std::vector<std::unique_ptr<Item>> ();
      68              : 
      69       100667 :   for (auto &item : module.get_items ())
      70              :     {
      71        97270 :       auto transitem = translate (*item);
      72              :       // The item may be null if it doesn't need to live in the HIR - for
      73              :       // example, macro rules definitions
      74        97270 :       if (transitem)
      75        89265 :         items.emplace_back (transitem);
      76              :     }
      77              : 
      78              :   // should be lowered/copied from module.get_in/outer_attrs()
      79         3397 :   AST::AttrVec inner_attrs = module.get_inner_attrs ();
      80         3397 :   AST::AttrVec outer_attrs = module.get_outer_attrs ();
      81              : 
      82         3397 :   translated
      83         3397 :     = new HIR::Module (mapping, module.get_name (), module.get_locus (),
      84              :                        std::move (items), std::move (vis),
      85         6794 :                        std::move (inner_attrs), std::move (outer_attrs));
      86         3397 :   mappings.hir.modules.insert (static_cast<Module *> (translated));
      87         3397 : }
      88              : 
      89              : void
      90          149 : ASTLoweringItem::visit (AST::TypeAlias &alias)
      91              : {
      92          149 :   std::vector<std::unique_ptr<HIR::WhereClauseItem>> where_clause_items;
      93          149 :   where_clause_items.reserve (alias.get_where_clause ().get_items ().size ());
      94              : 
      95          149 :   for (auto &item : alias.get_where_clause ().get_items ())
      96            0 :     where_clause_items.emplace_back (
      97            0 :       ASTLowerWhereClauseItem::translate (*item.get ()));
      98              : 
      99          149 :   HIR::WhereClause where_clause (std::move (where_clause_items));
     100          149 :   HIR::Visibility vis = translate_visibility (alias.get_visibility ());
     101              : 
     102          149 :   std::vector<std::unique_ptr<HIR::GenericParam>> generic_params;
     103          149 :   if (alias.has_generics ())
     104           10 :     generic_params = lower_generic_params (alias.get_generic_params ());
     105              : 
     106          149 :   HIR::Type *existing_type
     107          149 :     = ASTLoweringType::translate (alias.get_type_aliased ());
     108              : 
     109          149 :   auto crate_num = mappings.crate.get_current_crate ();
     110          149 :   Analysis::NodeMapping mapping (crate_num, alias.get_node_id (),
     111          149 :                                  mappings.get_next_hir_id (crate_num),
     112          149 :                                  mappings.get_next_localdef_id (crate_num));
     113              : 
     114          149 :   translated
     115          149 :     = new HIR::TypeAlias (mapping, alias.get_new_type_name (),
     116              :                           std::move (generic_params), std::move (where_clause),
     117          298 :                           std::unique_ptr<HIR::Type> (existing_type),
     118          149 :                           std::move (vis), alias.get_outer_attrs (),
     119          447 :                           alias.get_locus ());
     120          149 : }
     121              : 
     122              : void
     123            0 : ASTLoweringItem::visit (AST::TraitAlias &alias)
     124              : {
     125            0 :   rust_sorry_at (alias.get_locus (), "trait alias lowering");
     126            0 : }
     127              : 
     128              : void
     129         1903 : ASTLoweringItem::visit (AST::TupleStruct &struct_decl)
     130              : {
     131         1903 :   std::vector<std::unique_ptr<HIR::GenericParam>> generic_params;
     132         1903 :   if (struct_decl.has_generics ())
     133              :     {
     134          559 :       generic_params = lower_generic_params (struct_decl.get_generic_params ());
     135              :     }
     136              : 
     137         1903 :   std::vector<std::unique_ptr<HIR::WhereClauseItem>> where_clause_items;
     138         1903 :   where_clause_items.reserve (
     139         1903 :     struct_decl.get_where_clause ().get_items ().size ());
     140              : 
     141         1912 :   for (auto &item : struct_decl.get_where_clause ().get_items ())
     142            9 :     where_clause_items.emplace_back (
     143            9 :       ASTLowerWhereClauseItem::translate (*item.get ()));
     144              : 
     145         1903 :   HIR::WhereClause where_clause (std::move (where_clause_items));
     146         1903 :   HIR::Visibility vis = translate_visibility (struct_decl.get_visibility ());
     147              : 
     148         1903 :   std::vector<HIR::TupleField> fields;
     149         1903 :   fields.reserve (struct_decl.get_fields ().size ());
     150              : 
     151         7435 :   for (AST::TupleField &field : struct_decl.get_fields ())
     152              :     {
     153         5532 :       if (field.get_field_type ().is_marked_for_strip ())
     154            0 :         continue;
     155              : 
     156              :       // FIXME: How do we get the visibility from here?
     157         5532 :       HIR::Visibility vis = translate_visibility (field.get_visibility ());
     158         5532 :       HIR::Type *type = ASTLoweringType::translate (field.get_field_type ());
     159              : 
     160         5532 :       auto crate_num = mappings.crate.get_current_crate ();
     161         5532 :       Analysis::NodeMapping mapping (crate_num, field.get_node_id (),
     162         5532 :                                      mappings.get_next_hir_id (crate_num),
     163         5532 :                                      mappings.get_next_localdef_id (crate_num));
     164              : 
     165         5532 :       fields.emplace_back (mapping, std::unique_ptr<HIR::Type> (type), vis,
     166        11064 :                            field.get_locus (), field.get_outer_attrs ());
     167         5532 :     }
     168              : 
     169         1903 :   auto crate_num = mappings.crate.get_current_crate ();
     170         3806 :   Analysis::NodeMapping mapping (crate_num, struct_decl.get_node_id (),
     171         1903 :                                  mappings.get_next_hir_id (crate_num),
     172         1903 :                                  mappings.get_next_localdef_id (crate_num));
     173              : 
     174         3806 :   translated = new HIR::TupleStruct (mapping, std::move (fields),
     175         1903 :                                      struct_decl.get_identifier (),
     176              :                                      std::move (generic_params),
     177              :                                      std::move (where_clause), vis,
     178         1903 :                                      struct_decl.get_outer_attrs (),
     179         1903 :                                      struct_decl.get_locus ());
     180         1903 : }
     181              : 
     182              : void
     183         6663 : ASTLoweringItem::visit (AST::StructStruct &struct_decl)
     184              : {
     185         6663 :   std::vector<std::unique_ptr<HIR::GenericParam>> generic_params;
     186         6663 :   if (struct_decl.has_generics ())
     187              :     {
     188         2174 :       generic_params = lower_generic_params (struct_decl.get_generic_params ());
     189              :     }
     190              : 
     191         6663 :   std::vector<std::unique_ptr<HIR::WhereClauseItem>> where_clause_items;
     192         6663 :   where_clause_items.reserve (
     193         6663 :     struct_decl.get_where_clause ().get_items ().size ());
     194              : 
     195         6776 :   for (auto &item : struct_decl.get_where_clause ().get_items ())
     196          113 :     where_clause_items.emplace_back (
     197          113 :       ASTLowerWhereClauseItem::translate (*item.get ()));
     198              : 
     199         6663 :   HIR::WhereClause where_clause (std::move (where_clause_items));
     200              : 
     201         6663 :   HIR::Visibility vis = translate_visibility (struct_decl.get_visibility ());
     202              : 
     203         6663 :   bool is_unit = struct_decl.is_unit_struct ();
     204         6663 :   std::vector<HIR::StructField> fields;
     205        12129 :   for (AST::StructField &field : struct_decl.get_fields ())
     206              :     {
     207         5466 :       if (field.get_field_type ().is_marked_for_strip ())
     208            4 :         continue;
     209              : 
     210         5466 :       HIR::Visibility vis = translate_visibility (field.get_visibility ());
     211         5466 :       HIR::Type *type = ASTLoweringType::translate (field.get_field_type ());
     212              : 
     213         5466 :       auto crate_num = mappings.crate.get_current_crate ();
     214         5466 :       Analysis::NodeMapping mapping (crate_num, field.get_node_id (),
     215         5466 :                                      mappings.get_next_hir_id (crate_num),
     216         5466 :                                      mappings.get_next_localdef_id (crate_num));
     217              : 
     218         5466 :       HIR::StructField translated_field (mapping, field.get_field_name (),
     219        10932 :                                          std::unique_ptr<HIR::Type> (type), vis,
     220              :                                          field.get_locus (),
     221        10932 :                                          field.get_outer_attrs ());
     222              : 
     223         5466 :       if (struct_field_name_exists (fields, translated_field))
     224            4 :         continue;
     225              : 
     226         5462 :       fields.push_back (std::move (translated_field));
     227         5466 :     }
     228              : 
     229         6663 :   auto crate_num = mappings.crate.get_current_crate ();
     230        13326 :   Analysis::NodeMapping mapping (crate_num, struct_decl.get_node_id (),
     231         6663 :                                  mappings.get_next_hir_id (crate_num),
     232         6663 :                                  mappings.get_next_localdef_id (crate_num));
     233              : 
     234        13326 :   translated = new HIR::StructStruct (mapping, std::move (fields),
     235         6663 :                                       struct_decl.get_identifier (),
     236              :                                       std::move (generic_params),
     237              :                                       std::move (where_clause), is_unit, vis,
     238         6663 :                                       struct_decl.get_outer_attrs (),
     239         6663 :                                       struct_decl.get_locus ());
     240         6663 : }
     241              : 
     242              : void
     243          896 : ASTLoweringItem::visit (AST::Enum &enum_decl)
     244              : {
     245          896 :   std::vector<std::unique_ptr<HIR::GenericParam>> generic_params;
     246          896 :   if (enum_decl.has_generics ())
     247              :     {
     248          326 :       generic_params = lower_generic_params (enum_decl.get_generic_params ());
     249              :     }
     250              : 
     251          896 :   std::vector<std::unique_ptr<HIR::WhereClauseItem>> where_clause_items;
     252          896 :   where_clause_items.reserve (
     253          896 :     enum_decl.get_where_clause ().get_items ().size ());
     254              : 
     255          897 :   for (auto &item : enum_decl.get_where_clause ().get_items ())
     256            1 :     where_clause_items.emplace_back (
     257            1 :       ASTLowerWhereClauseItem::translate (*item.get ()));
     258              : 
     259          896 :   HIR::WhereClause where_clause (std::move (where_clause_items));
     260          896 :   HIR::Visibility vis = translate_visibility (enum_decl.get_visibility ());
     261              : 
     262              :   // bool is_unit = enum_decl.is_zero_variant ();
     263          896 :   std::vector<std::unique_ptr<HIR::EnumItem>> items;
     264          896 :   items.reserve (enum_decl.get_variants ().size ());
     265              : 
     266         3057 :   for (auto &variant : enum_decl.get_variants ())
     267              :     {
     268         2161 :       if (variant->is_marked_for_strip ())
     269            0 :         continue;
     270              : 
     271         2161 :       items.emplace_back (ASTLoweringEnumItem::translate (variant.get ()));
     272              :     }
     273              : 
     274          896 :   auto crate_num = mappings.crate.get_current_crate ();
     275         1792 :   Analysis::NodeMapping mapping (crate_num, enum_decl.get_node_id (),
     276          896 :                                  mappings.get_next_hir_id (crate_num),
     277          896 :                                  mappings.get_next_localdef_id (crate_num));
     278              : 
     279          896 :   HIR::Enum *hir_enum
     280          896 :     = new HIR::Enum (mapping, enum_decl.get_identifier (), vis,
     281              :                      std::move (generic_params), std::move (where_clause),
     282          896 :                      std::move (items), enum_decl.get_outer_attrs (),
     283         1792 :                      enum_decl.get_locus ());
     284          896 :   translated = hir_enum;
     285         3057 :   for (auto &variant : hir_enum->get_variants ())
     286              :     {
     287         2161 :       mappings.insert_hir_enumitem (hir_enum, variant.get ());
     288              :     }
     289          896 : }
     290              : 
     291              : void
     292          125 : ASTLoweringItem::visit (AST::Union &union_decl)
     293              : {
     294          125 :   std::vector<std::unique_ptr<HIR::GenericParam>> generic_params;
     295          125 :   if (union_decl.has_generics ())
     296           93 :     generic_params = lower_generic_params (union_decl.get_generic_params ());
     297              : 
     298          125 :   std::vector<std::unique_ptr<HIR::WhereClauseItem>> where_clause_items;
     299          125 :   where_clause_items.reserve (
     300          125 :     union_decl.get_where_clause ().get_items ().size ());
     301              : 
     302          125 :   for (auto &item : union_decl.get_where_clause ().get_items ())
     303            0 :     where_clause_items.emplace_back (
     304            0 :       ASTLowerWhereClauseItem::translate (*item.get ()));
     305              : 
     306          125 :   HIR::WhereClause where_clause (std::move (where_clause_items));
     307          125 :   HIR::Visibility vis = translate_visibility (union_decl.get_visibility ());
     308              : 
     309          125 :   std::vector<HIR::StructField> variants;
     310          480 :   for (AST::StructField &variant : union_decl.get_variants ())
     311              :     {
     312          357 :       if (variant.get_field_type ().is_marked_for_strip ())
     313            0 :         continue;
     314              : 
     315              :       // FIXME: Does visibility apply here?
     316          357 :       HIR::Visibility vis = translate_visibility (variant.get_visibility ());
     317          357 :       HIR::Type *type = ASTLoweringType::translate (variant.get_field_type ());
     318              : 
     319          357 :       auto crate_num = mappings.crate.get_current_crate ();
     320          357 :       Analysis::NodeMapping mapping (crate_num, variant.get_node_id (),
     321          357 :                                      mappings.get_next_hir_id (crate_num),
     322          357 :                                      mappings.get_next_localdef_id (crate_num));
     323              : 
     324          357 :       HIR::StructField translated_variant (mapping, variant.get_field_name (),
     325          714 :                                            std::unique_ptr<HIR::Type> (type),
     326              :                                            vis, variant.get_locus (),
     327          714 :                                            variant.get_outer_attrs ());
     328              : 
     329          357 :       if (struct_field_name_exists (variants, translated_variant))
     330              :         break;
     331              : 
     332          355 :       variants.push_back (std::move (translated_variant));
     333          357 :     }
     334              : 
     335          125 :   auto crate_num = mappings.crate.get_current_crate ();
     336          250 :   Analysis::NodeMapping mapping (crate_num, union_decl.get_node_id (),
     337          125 :                                  mappings.get_next_hir_id (crate_num),
     338          125 :                                  mappings.get_next_localdef_id (crate_num));
     339              : 
     340          125 :   translated
     341          125 :     = new HIR::Union (mapping, union_decl.get_identifier (), vis,
     342              :                       std::move (generic_params), std::move (where_clause),
     343          125 :                       std::move (variants), union_decl.get_outer_attrs (),
     344          250 :                       union_decl.get_locus ());
     345          125 : }
     346              : 
     347              : void
     348          356 : ASTLoweringItem::visit (AST::StaticItem &var)
     349              : {
     350          356 :   HIR::Visibility vis = translate_visibility (var.get_visibility ());
     351              : 
     352          356 :   HIR::Type *type = ASTLoweringType::translate (var.get_type (), true);
     353          356 :   HIR::Expr *expr = ASTLoweringExpr::translate (var.get_expr ());
     354              : 
     355          356 :   auto crate_num = mappings.crate.get_current_crate ();
     356          712 :   Analysis::NodeMapping mapping (crate_num, var.get_node_id (),
     357          356 :                                  mappings.get_next_hir_id (crate_num),
     358          356 :                                  mappings.get_next_localdef_id (crate_num));
     359              : 
     360          356 :   translated = new HIR::StaticItem (mapping, var.get_identifier (),
     361          356 :                                     var.is_mutable () ? Mutability::Mut
     362              :                                                       : Mutability::Imm,
     363          356 :                                     std::unique_ptr<HIR::Type> (type),
     364          712 :                                     std::unique_ptr<HIR::Expr> (expr), vis,
     365         1068 :                                     var.get_outer_attrs (), var.get_locus ());
     366          356 : }
     367              : 
     368              : void
     369         4881 : ASTLoweringItem::visit (AST::ConstantItem &constant)
     370              : {
     371         4881 :   HIR::Visibility vis = translate_visibility (constant.get_visibility ());
     372              : 
     373         4881 :   HIR::Type *type = ASTLoweringType::translate (constant.get_type (), true);
     374         4881 :   HIR::Expr *expr = nullptr;
     375         4881 :   if (constant.has_expr ())
     376         4880 :     expr = ASTLoweringExpr::translate (constant.get_expr ());
     377              : 
     378         4881 :   auto crate_num = mappings.crate.get_current_crate ();
     379         4881 :   Analysis::NodeMapping mapping (crate_num, constant.get_node_id (),
     380         4881 :                                  mappings.get_next_hir_id (crate_num),
     381         4881 :                                  mappings.get_next_localdef_id (crate_num));
     382              : 
     383         4881 :   translated = new HIR::ConstantItem (mapping, constant.get_identifier (), vis,
     384        14643 :                                       std::unique_ptr<HIR::Type> (type),
     385         9762 :                                       std::unique_ptr<HIR::Expr> (expr),
     386         4881 :                                       constant.get_outer_attrs (),
     387        14643 :                                       constant.get_locus ());
     388         4881 : }
     389              : 
     390              : void
     391        23902 : ASTLoweringItem::visit (AST::Function &function)
     392              : {
     393        23902 :   if (function.is_marked_for_strip ())
     394            0 :     return;
     395              : 
     396        23902 :   std::vector<std::unique_ptr<HIR::WhereClauseItem>> where_clause_items;
     397        23902 :   where_clause_items.reserve (
     398        23902 :     function.get_where_clause ().get_items ().size ());
     399              : 
     400        24311 :   for (auto &item : function.get_where_clause ().get_items ())
     401          409 :     where_clause_items.emplace_back (
     402          409 :       ASTLowerWhereClauseItem::translate (*item.get ()));
     403              : 
     404        23902 :   HIR::WhereClause where_clause (std::move (where_clause_items));
     405        23902 :   HIR::FunctionQualifiers qualifiers
     406        23902 :     = lower_qualifiers (function.get_qualifiers ());
     407        23902 :   HIR::Visibility vis = translate_visibility (function.get_visibility ());
     408              : 
     409              :   // need
     410        23902 :   std::vector<std::unique_ptr<HIR::GenericParam>> generic_params;
     411        23902 :   if (function.has_generics ())
     412              :     {
     413         2415 :       generic_params = lower_generic_params (function.get_generic_params ());
     414              :     }
     415        23902 :   Identifier function_name = function.get_function_name ();
     416        23902 :   location_t locus = function.get_locus ();
     417              : 
     418        23902 :   std::unique_ptr<HIR::Type> return_type
     419        23902 :     = function.has_return_type () ? std::unique_ptr<HIR::Type> (
     420              :         ASTLoweringType::translate (function.get_return_type (), false,
     421              :                                     ASTLoweringType::ImplTrait::Allow))
     422        23902 :                                   : nullptr;
     423              : 
     424        23902 :   std::vector<HIR::FunctionParam> function_params;
     425        23902 :   function_params.reserve (function.get_function_params ().size ());
     426              : 
     427        23902 :   auto crate_num = mappings.crate.get_current_crate ();
     428        66371 :   for (auto &p : function.get_function_params ())
     429              :     {
     430        42469 :       if (p->is_variadic ())
     431            2 :         continue;
     432        42469 :       if (p->is_self ())
     433              :         {
     434            0 :           rich_location r (line_table, p->get_locus ());
     435            0 :           r.add_range (function.get_locus ());
     436            0 :           rust_error_at (
     437              :             r, "%<self%> parameter is only allowed in associated functions");
     438              : 
     439              :           // rustc creates a synthetic regular fn-param here pointing to a
     440              :           // generic Self as far as i can see but that seems over the top for
     441              :           // now.
     442              :           //
     443              :           // see this example (invalid code):
     444              :           //
     445              :           // pub trait X {
     446              :           //   fn x() {
     447              :           //     fn f(&mut self) {}
     448              :           //     f();
     449              :           //   }
     450              :           // }
     451              :           //
     452              :           // without a synthetic param we wont get the number of args error as
     453              :           // well but i think this is fine for now.
     454              :           //
     455              :           // problem is what we make the param type to become...
     456              : 
     457            0 :           continue;
     458            0 :         }
     459              : 
     460        42469 :       auto &param = static_cast<AST::FunctionParam &> (*p);
     461              : 
     462        42469 :       auto translated_pattern = std::unique_ptr<HIR::Pattern> (
     463        42469 :         ASTLoweringPattern::translate (param.get_pattern ()));
     464              : 
     465        42469 :       switch (param.get_pattern ().get_pattern_kind ())
     466              :         {
     467            2 :         case AST::Pattern::Kind::Literal:
     468            2 :           rust_error_at (param.get_locus (),
     469              :                          "refutable pattern in function argument");
     470            2 :           continue;
     471        42467 :         default:
     472              :           // defer checking for when we have type information after lowering
     473        42467 :           break;
     474              :         }
     475              : 
     476        42467 :       auto translated_type = std::unique_ptr<HIR::Type> (
     477        42467 :         ASTLoweringType::translate (param.get_type ()));
     478              : 
     479        42467 :       auto crate_num = mappings.crate.get_current_crate ();
     480        42467 :       Analysis::NodeMapping mapping (crate_num, param.get_node_id (),
     481        42467 :                                      mappings.get_next_hir_id (crate_num),
     482        42467 :                                      UNKNOWN_LOCAL_DEFID);
     483              : 
     484        42467 :       function_params.emplace_back (mapping, std::move (translated_pattern),
     485              :                                     std::move (translated_type),
     486        42467 :                                     param.get_locus ());
     487        42469 :     }
     488              : 
     489        23902 :   bool terminated = false;
     490        23902 :   std::unique_ptr<HIR::BlockExpr> function_body
     491              :     = std::unique_ptr<HIR::BlockExpr> (
     492        23902 :       ASTLoweringBlock::translate (*function.get_definition ().value (),
     493        23902 :                                    &terminated));
     494              : 
     495        47804 :   Analysis::NodeMapping mapping (crate_num, function.get_node_id (),
     496        23902 :                                  mappings.get_next_hir_id (crate_num),
     497        23902 :                                  mappings.get_next_localdef_id (crate_num));
     498              : 
     499        23902 :   mappings.insert_location (function_body->get_mappings ().get_hirid (),
     500              :                             function.get_locus ());
     501              : 
     502        23902 :   Defaultness defaultness
     503        23902 :     = function.is_default () ? Defaultness::Default : Defaultness::Final;
     504              : 
     505        23902 :   auto fn
     506              :     = new HIR::Function (mapping, std::move (function_name),
     507              :                          std::move (qualifiers), std::move (generic_params),
     508              :                          std::move (function_params), std::move (return_type),
     509              :                          std::move (where_clause), std::move (function_body),
     510        23902 :                          std::move (vis), function.get_outer_attrs (),
     511        23902 :                          tl::nullopt, defaultness, locus);
     512              : 
     513              :   // add the mappings for the function params at the end
     514        66369 :   for (auto &param : fn->get_function_params ())
     515              :     {
     516        42467 :       mappings.hir.function_params.insert (&param);
     517        42467 :       mappings.insert_location (mapping.get_hirid (), param.get_locus ());
     518              :     }
     519              : 
     520        23902 :   translated = fn;
     521        23902 : }
     522              : 
     523              : void
     524         3547 : ASTLoweringItem::visit (AST::InherentImpl &impl_block)
     525              : {
     526         3547 :   std::vector<std::unique_ptr<HIR::WhereClauseItem>> where_clause_items;
     527         3547 :   where_clause_items.reserve (
     528         3547 :     impl_block.get_where_clause ().get_items ().size ());
     529              : 
     530         3571 :   for (auto &item : impl_block.get_where_clause ().get_items ())
     531           24 :     where_clause_items.emplace_back (
     532           24 :       ASTLowerWhereClauseItem::translate (*item));
     533              : 
     534         3547 :   HIR::WhereClause where_clause (std::move (where_clause_items));
     535         3547 :   HIR::Visibility vis = translate_visibility (impl_block.get_visibility ());
     536              : 
     537         3547 :   std::vector<std::unique_ptr<HIR::GenericParam>> generic_params;
     538         3547 :   if (impl_block.has_generics ())
     539              :     {
     540         1833 :       generic_params = lower_generic_params (impl_block.get_generic_params ());
     541              : 
     542         4650 :       for (auto &generic_param : generic_params)
     543              :         {
     544         2817 :           switch (generic_param->get_kind ())
     545              :             {
     546         2094 :             case HIR::GenericParam::GenericKind::TYPE:
     547         2094 :               {
     548         2094 :                 const HIR::TypeParam &t
     549         2094 :                   = static_cast<const HIR::TypeParam &> (*generic_param);
     550              : 
     551         2094 :                 if (t.has_type ())
     552              :                   {
     553            1 :                     rich_location rich_locus (line_table, t.get_locus ());
     554            1 :                     rich_locus.add_fixit_replace (
     555              :                       t.get_locus (),
     556              :                       "for more information, see issue #36887 "
     557              :                       "<https://github.com/rust-lang/rust/issues/36887>");
     558            1 :                     rust_error_at (rich_locus,
     559              :                                    "defaults for type parameters are only "
     560              :                                    "allowed in %<struct%>, %<enum%>, %<type%>, "
     561              :                                    "or %<trait%> definitions");
     562            1 :                   }
     563              :               }
     564              :               break;
     565              : 
     566              :             default:
     567              :               break;
     568              :             }
     569              :         }
     570              :     }
     571              : 
     572         3547 :   HIR::Type *impl_type = ASTLoweringType::translate (impl_block.get_type ());
     573              : 
     574         3547 :   auto crate_num = mappings.crate.get_current_crate ();
     575         7094 :   Analysis::NodeMapping mapping (crate_num, impl_block.get_node_id (),
     576         3547 :                                  mappings.get_next_hir_id (crate_num),
     577         3547 :                                  mappings.get_next_localdef_id (crate_num));
     578              : 
     579         7094 :   std::vector<std::unique_ptr<HIR::ImplItem>> impl_items;
     580         3547 :   std::vector<HirId> impl_item_ids;
     581        24868 :   for (auto &impl_item : impl_block.get_impl_items ())
     582              :     {
     583        21321 :       if (impl_item->is_marked_for_strip ())
     584            0 :         continue;
     585              : 
     586        21321 :       HIR::ImplItem *lowered
     587        21321 :         = ASTLowerImplItem::translate (*impl_item, mapping.get_hirid ());
     588        21321 :       rust_assert (lowered != nullptr);
     589        21321 :       impl_items.emplace_back (lowered);
     590        21321 :       impl_item_ids.push_back (lowered->get_impl_mappings ().get_hirid ());
     591              :     }
     592              : 
     593         3547 :   BoundPolarity polarity = BoundPolarity::RegularBound;
     594         3547 :   HIR::ImplBlock *hir_impl_block = new HIR::ImplBlock (
     595              :     mapping, std::move (impl_items), std::move (generic_params),
     596         3547 :     std::unique_ptr<HIR::Type> (impl_type), nullptr, where_clause, polarity,
     597         7094 :     vis, impl_block.get_inner_attrs (), impl_block.get_outer_attrs (),
     598         3547 :     impl_block.get_locus (), false);
     599         3547 :   translated = hir_impl_block;
     600              : 
     601         3547 :   mappings.insert_hir_impl_block (hir_impl_block);
     602        24868 :   for (auto &impl_item_id : impl_item_ids)
     603              :     {
     604        21321 :       mappings.insert_impl_item_mapping (impl_item_id, hir_impl_block);
     605              :     }
     606         3547 : }
     607              : 
     608              : void
     609         5708 : ASTLoweringItem::visit (AST::Trait &trait)
     610              : {
     611         5708 :   std::vector<std::unique_ptr<HIR::WhereClauseItem>> where_clause_items;
     612         5708 :   where_clause_items.reserve (trait.get_where_clause ().get_items ().size ());
     613              : 
     614         5715 :   for (auto &item : trait.get_where_clause ().get_items ())
     615            7 :     where_clause_items.emplace_back (
     616            7 :       ASTLowerWhereClauseItem::translate (*item.get ()));
     617              : 
     618         5708 :   HIR::WhereClause where_clause (std::move (where_clause_items));
     619              : 
     620         5708 :   HIR::Visibility vis = translate_visibility (trait.get_visibility ());
     621              : 
     622         5708 :   std::vector<std::unique_ptr<HIR::GenericParam>> generic_params;
     623         5708 :   if (trait.has_generics ())
     624              :     {
     625         1223 :       generic_params = lower_generic_params (trait.get_generic_params ());
     626              :     }
     627              : 
     628              :   // TODO: separate "Self" from normal generic parameters
     629              :   //       in HIR as well as in AST?
     630         5708 :   HIR::GenericParam *self_param
     631         5708 :     = ASTLowerGenericParam::translate (trait.get_implicit_self ());
     632         5708 :   generic_params.emplace (generic_params.begin (), self_param);
     633              : 
     634         5708 :   std::vector<std::unique_ptr<HIR::TypeParamBound>> type_param_bounds;
     635         5708 :   if (trait.has_type_param_bounds ())
     636              :     {
     637         2519 :       for (auto &bound : trait.get_type_param_bounds ())
     638         1370 :         type_param_bounds.emplace_back (lower_bound (*bound));
     639              :     }
     640              : 
     641         5708 :   auto trait_item_size = trait.get_trait_items ().size ();
     642              : 
     643         5708 :   std::vector<std::unique_ptr<HIR::TraitItem>> trait_items;
     644         5708 :   trait_items.reserve (trait_item_size);
     645         5708 :   std::vector<HirId> trait_item_ids;
     646         5708 :   trait_item_ids.reserve (trait_item_size);
     647              : 
     648        12664 :   for (auto &item : trait.get_trait_items ())
     649              :     {
     650         6956 :       if (item->is_marked_for_strip ())
     651            0 :         continue;
     652              : 
     653         6956 :       HIR::TraitItem *lowered = ASTLowerTraitItem::translate (*item);
     654         6956 :       trait_item_ids.push_back (lowered->get_mappings ().get_hirid ());
     655         6956 :       trait_items.emplace_back (lowered);
     656              :     }
     657              : 
     658         5708 :   auto crate_num = mappings.crate.get_current_crate ();
     659        11416 :   Analysis::NodeMapping mapping (crate_num, trait.get_node_id (),
     660         5708 :                                  mappings.get_next_hir_id (crate_num),
     661         5708 :                                  mappings.get_next_localdef_id (crate_num));
     662              : 
     663         5708 :   auto trait_unsafety
     664         5708 :     = trait.is_unsafe () ? Unsafety::Unsafe : Unsafety::Normal;
     665              : 
     666         5708 :   HIR::Trait *hir_trait
     667        11416 :     = new HIR::Trait (mapping, trait.get_identifier (), trait_unsafety,
     668              :                       std::move (generic_params), std::move (type_param_bounds),
     669              :                       where_clause, std::move (trait_items), vis,
     670         5708 :                       trait.get_outer_attrs (), trait.get_locus ());
     671              : 
     672         5708 :   if (trait.is_auto ())
     673           54 :     mappings.insert_auto_trait (hir_trait);
     674              : 
     675         5708 :   translated = hir_trait;
     676              : 
     677        12664 :   for (auto trait_item_id : trait_item_ids)
     678              :     {
     679         6956 :       mappings.insert_trait_item_mapping (trait_item_id, hir_trait);
     680              :     }
     681         5708 : }
     682              : 
     683              : void
     684        61932 : ASTLoweringItem::visit (AST::TraitImpl &impl_block)
     685              : {
     686        61932 :   bool unsafe = impl_block.is_unsafe ();
     687              : 
     688        61932 :   std::vector<std::unique_ptr<HIR::WhereClauseItem>> where_clause_items;
     689        61932 :   where_clause_items.reserve (
     690        61932 :     impl_block.get_where_clause ().get_items ().size ());
     691              : 
     692        65457 :   for (auto &item : impl_block.get_where_clause ().get_items ())
     693         3525 :     where_clause_items.emplace_back (
     694         3525 :       ASTLowerWhereClauseItem::translate (*item));
     695              : 
     696        61932 :   HIR::WhereClause where_clause (std::move (where_clause_items));
     697        61932 :   HIR::Visibility vis = translate_visibility (impl_block.get_visibility ());
     698              : 
     699        61932 :   std::vector<std::unique_ptr<HIR::GenericParam>> generic_params;
     700        61932 :   if (impl_block.has_generics ())
     701              :     {
     702        20072 :       generic_params = lower_generic_params (impl_block.get_generic_params ());
     703              : 
     704        75213 :       for (auto &generic_param : generic_params)
     705              :         {
     706        55141 :           switch (generic_param->get_kind ())
     707              :             {
     708        47129 :             case HIR::GenericParam::GenericKind::TYPE:
     709        47129 :               {
     710        47129 :                 const HIR::TypeParam &t
     711        47129 :                   = static_cast<const HIR::TypeParam &> (*generic_param);
     712              : 
     713        47129 :                 if (t.has_type ())
     714              :                   {
     715            0 :                     rich_location rich_locus (line_table, t.get_locus ());
     716            0 :                     rich_locus.add_fixit_replace (
     717              :                       t.get_locus (), "for more information, see issue #36887 "
     718              :                                       "<https://github.com/rust-lang/rust/"
     719              :                                       "issues/36887>");
     720            0 :                     rust_error_at (rich_locus,
     721              :                                    "defaults for type parameters are only "
     722              :                                    "allowed in %<struct%>, %<enum%>, %<type%>, "
     723              :                                    "or %<trait%> definitions");
     724            0 :                   }
     725              :               }
     726              :               break;
     727              : 
     728              :             default:
     729              :               break;
     730              :             }
     731              :         }
     732              :     }
     733              : 
     734        61932 :   HIR::Type *impl_type = ASTLoweringType::translate (impl_block.get_type ());
     735        61932 :   HIR::TypePath *trait_ref
     736        61932 :     = ASTLowerTypePath::translate (impl_block.get_trait_path ());
     737              : 
     738        61932 :   auto crate_num = mappings.crate.get_current_crate ();
     739       123864 :   Analysis::NodeMapping mapping (crate_num, impl_block.get_node_id (),
     740        61932 :                                  mappings.get_next_hir_id (crate_num),
     741        61932 :                                  mappings.get_next_localdef_id (crate_num));
     742              : 
     743        61932 :   auto impl_items_size = impl_block.get_impl_items ().size ();
     744              : 
     745       123864 :   std::vector<std::unique_ptr<HIR::ImplItem>> impl_items;
     746        61932 :   impl_items.reserve (impl_items_size);
     747        61932 :   std::vector<HirId> impl_item_ids;
     748        61932 :   impl_item_ids.reserve (impl_items_size);
     749              : 
     750       147460 :   for (auto &impl_item : impl_block.get_impl_items ())
     751              :     {
     752        85528 :       if (impl_item->is_marked_for_strip ())
     753            0 :         continue;
     754              : 
     755        85528 :       HIR::ImplItem *lowered
     756        85528 :         = ASTLowerImplItem::translate (*impl_item, mapping.get_hirid ());
     757        85528 :       rust_assert (lowered != nullptr);
     758        85528 :       impl_item_ids.push_back (lowered->get_impl_mappings ().get_hirid ());
     759        85528 :       impl_items.emplace_back (lowered);
     760              :     }
     761              : 
     762        61932 :   BoundPolarity polarity = impl_block.is_exclam ()
     763        61932 :                              ? BoundPolarity::NegativeBound
     764        61808 :                              : BoundPolarity::RegularBound;
     765        61932 :   HIR::ImplBlock *hir_impl_block = new HIR::ImplBlock (
     766              :     mapping, std::move (impl_items), std::move (generic_params),
     767       185796 :     std::unique_ptr<HIR::Type> (impl_type),
     768       123864 :     std::unique_ptr<HIR::TypePath> (trait_ref), where_clause, polarity, vis,
     769       123864 :     impl_block.get_inner_attrs (), impl_block.get_outer_attrs (),
     770       123864 :     impl_block.get_locus (), unsafe);
     771        61932 :   translated = hir_impl_block;
     772              : 
     773        61932 :   mappings.insert_hir_impl_block (hir_impl_block);
     774              : 
     775       147460 :   for (auto &impl_item_id : impl_item_ids)
     776              :     {
     777        85528 :       mappings.insert_impl_item_mapping (impl_item_id, hir_impl_block);
     778              :     }
     779        61932 : }
     780              : 
     781              : void
     782         2156 : ASTLoweringItem::visit (AST::ExternBlock &extern_block)
     783              : {
     784         2156 :   translated = lower_extern_block (extern_block);
     785         2156 : }
     786              : 
     787              : void
     788         2804 : ASTLoweringItem::visit (AST::MacroRulesDefinition &def)
     789              : {
     790         2804 :   lower_macro_definition (def);
     791         2804 : }
     792              : 
     793              : void
     794           54 : ASTLoweringItem::visit (AST::ExternCrate &extern_crate)
     795              : {
     796           54 :   if (extern_crate.references_self ())
     797              :     return;
     798              : 
     799           54 :   auto &mappings = Analysis::Mappings::get ();
     800           54 :   auto &crate_mapping = mappings.crate;
     801              : 
     802           54 :   CrateNum num
     803           54 :     = crate_mapping.lookup_crate_name (extern_crate.get_referenced_crate ())
     804           54 :         .value ();
     805           54 :   AST::Crate &crate = mappings.crate.get_ast_crate (num);
     806              : 
     807           54 :   auto saved_crate_num = mappings.crate.get_current_crate ();
     808           54 :   crate_mapping.set_current_crate (num);
     809           54 :   auto lowered = ASTLowering::Resolve (crate);
     810           54 :   mappings.insert_hir_crate (std::move (lowered));
     811           46 :   crate_mapping.set_current_crate (saved_crate_num);
     812           46 : }
     813              : 
     814              : HIR::SimplePath
     815         2600 : ASTLoweringSimplePath::translate (const AST::SimplePath &path)
     816              : {
     817         2600 :   ASTLoweringSimplePath resolver;
     818              : 
     819         2600 :   return resolver.lower (path);
     820         2600 : }
     821              : 
     822              : HIR::SimplePathSegment
     823         2668 : ASTLoweringSimplePath::lower (const AST::SimplePathSegment &segment)
     824              : {
     825         2668 :   auto crate_num = mappings.crate.get_current_crate ();
     826         2668 :   auto node_id = segment.get_node_id ();
     827              : 
     828         2668 :   auto mapping = Analysis::NodeMapping (crate_num, node_id,
     829         2668 :                                         mappings.get_next_hir_id (crate_num),
     830         2668 :                                         UNKNOWN_LOCAL_DEFID);
     831              : 
     832         2668 :   auto hir_seg = HIR::SimplePathSegment (mapping);
     833              : 
     834         2668 :   mappings.insert_node_to_hir (node_id, mapping.get_hirid ());
     835              :   // mappings.insert_simple_path_segment (crate_num, node_id, &segment);
     836              : 
     837         2668 :   return hir_seg;
     838              : }
     839              : 
     840              : HIR::SimplePath
     841         2600 : ASTLoweringSimplePath::lower (const AST::SimplePath &path)
     842              : {
     843         2600 :   auto segments = std::vector<HIR::SimplePathSegment> ();
     844         5268 :   for (auto &segment : path.get_segments ())
     845         2668 :     segments.emplace_back (lower (segment));
     846              : 
     847         2600 :   auto crate_num = mappings.crate.get_current_crate ();
     848         2600 :   auto node_id = path.get_node_id ();
     849              : 
     850         2600 :   auto mapping = Analysis::NodeMapping (crate_num, node_id,
     851         2600 :                                         mappings.get_next_hir_id (crate_num),
     852         2600 :                                         UNKNOWN_LOCAL_DEFID);
     853              : 
     854         2600 :   auto lowered
     855         2600 :     = HIR::SimplePath (std::move (segments), mapping, path.get_locus ());
     856              : 
     857         2600 :   mappings.insert_node_to_hir (node_id, mapping.get_hirid ());
     858              :   // mappings.insert_simple_path (crate_num, node_id, &path);
     859              : 
     860         2600 :   return lowered;
     861         2600 : }
     862              : 
     863              : } // namespace HIR
     864              : } // namespace Rust
        

Generated by: LCOV version 2.5.0-beta

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