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