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

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.