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: 94.7 % 490 464
Test Date: 2026-09-19 16:22:48 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        26246 : ASTLoweringItem::translate (AST::Item &item)
      36              : {
      37        26246 :   ASTLoweringItem resolver;
      38        26246 :   item.accept_vis (resolver);
      39              : 
      40        26238 :   if (resolver.translated != nullptr)
      41              :     {
      42        24515 :       auto id = resolver.translated->get_mappings ().get_hirid ();
      43        24515 :       auto defid = resolver.translated->get_mappings ().get_defid ();
      44        24515 :       auto locus = resolver.translated->get_locus ();
      45              : 
      46        24515 :       resolver.handle_outer_attributes (*resolver.translated);
      47        24515 :       resolver.mappings.insert_ast_item (&item);
      48        24515 :       resolver.mappings.insert_hir_item (resolver.translated);
      49        24515 :       resolver.mappings.insert_location (id, locus);
      50        24515 :       resolver.mappings.insert_defid_mapping (defid, resolver.translated);
      51              :     }
      52              : 
      53        26238 :   return resolver.translated;
      54        26238 : }
      55              : 
      56              : void
      57         1257 : ASTLoweringItem::visit (AST::Module &module)
      58              : {
      59         1257 :   auto crate_num = mappings.get_current_crate ();
      60         2514 :   Analysis::NodeMapping mapping (crate_num, module.get_node_id (),
      61         1257 :                                  mappings.get_next_hir_id (crate_num),
      62         1257 :                                  mappings.get_next_localdef_id (crate_num));
      63              : 
      64              :   // should be lowered from module.get_vis()
      65         1257 :   HIR::Visibility vis = translate_visibility (module.get_visibility ());
      66              : 
      67         1257 :   auto items = std::vector<std::unique_ptr<Item>> ();
      68              : 
      69         5947 :   for (auto &item : module.get_items ())
      70              :     {
      71         4690 :       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         4690 :       if (transitem)
      75         4186 :         items.emplace_back (transitem);
      76              :     }
      77              : 
      78              :   // should be lowered/copied from module.get_in/outer_attrs()
      79         1257 :   AST::AttrVec inner_attrs = module.get_inner_attrs ();
      80         1257 :   AST::AttrVec outer_attrs = module.get_outer_attrs ();
      81              : 
      82         1257 :   translated
      83         1257 :     = new HIR::Module (mapping, module.get_name (), module.get_locus (),
      84              :                        std::move (items), std::move (vis),
      85         2514 :                        std::move (inner_attrs), std::move (outer_attrs));
      86         1257 :   mappings.insert_module (static_cast<Module *> (translated));
      87         1257 : }
      88              : 
      89              : void
      90           71 : ASTLoweringItem::visit (AST::TypeAlias &alias)
      91              : {
      92           71 :   std::vector<std::unique_ptr<HIR::WhereClauseItem>> where_clause_items;
      93           71 :   where_clause_items.reserve (alias.get_where_clause ().get_items ().size ());
      94              : 
      95           71 :   for (auto &item : alias.get_where_clause ().get_items ())
      96            0 :     where_clause_items.emplace_back (
      97            0 :       ASTLowerWhereClauseItem::translate (*item.get ()));
      98              : 
      99           71 :   HIR::WhereClause where_clause (std::move (where_clause_items));
     100           71 :   HIR::Visibility vis = translate_visibility (alias.get_visibility ());
     101              : 
     102           71 :   std::vector<std::unique_ptr<HIR::GenericParam>> generic_params;
     103           71 :   if (alias.has_generics ())
     104            9 :     generic_params = lower_generic_params (alias.get_generic_params ());
     105              : 
     106           71 :   HIR::Type *existing_type
     107           71 :     = ASTLoweringType::translate (alias.get_type_aliased ());
     108              : 
     109           71 :   auto crate_num = mappings.get_current_crate ();
     110           71 :   Analysis::NodeMapping mapping (crate_num, alias.get_node_id (),
     111           71 :                                  mappings.get_next_hir_id (crate_num),
     112           71 :                                  mappings.get_next_localdef_id (crate_num));
     113              : 
     114           71 :   translated
     115           71 :     = new HIR::TypeAlias (mapping, alias.get_new_type_name (),
     116              :                           std::move (generic_params), std::move (where_clause),
     117          142 :                           std::unique_ptr<HIR::Type> (existing_type),
     118           71 :                           std::move (vis), alias.get_outer_attrs (),
     119          213 :                           alias.get_locus ());
     120           71 : }
     121              : 
     122              : void
     123         1014 : ASTLoweringItem::visit (AST::TupleStruct &struct_decl)
     124              : {
     125         1014 :   std::vector<std::unique_ptr<HIR::GenericParam>> generic_params;
     126         1014 :   if (struct_decl.has_generics ())
     127              :     {
     128          309 :       generic_params = lower_generic_params (struct_decl.get_generic_params ());
     129              :     }
     130              : 
     131         1014 :   std::vector<std::unique_ptr<HIR::WhereClauseItem>> where_clause_items;
     132         1014 :   where_clause_items.reserve (
     133         1014 :     struct_decl.get_where_clause ().get_items ().size ());
     134              : 
     135         1014 :   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         1014 :   HIR::WhereClause where_clause (std::move (where_clause_items));
     140         1014 :   HIR::Visibility vis = translate_visibility (struct_decl.get_visibility ());
     141              : 
     142         1014 :   std::vector<HIR::TupleField> fields;
     143         1014 :   fields.reserve (struct_decl.get_fields ().size ());
     144              : 
     145         2811 :   for (AST::TupleField &field : struct_decl.get_fields ())
     146              :     {
     147         1797 :       if (field.get_field_type ().is_marked_for_strip ())
     148            0 :         continue;
     149              : 
     150              :       // FIXME: How do we get the visibility from here?
     151         1797 :       HIR::Visibility vis = translate_visibility (field.get_visibility ());
     152         1797 :       HIR::Type *type = ASTLoweringType::translate (field.get_field_type ());
     153              : 
     154         1797 :       auto crate_num = mappings.get_current_crate ();
     155         1797 :       Analysis::NodeMapping mapping (crate_num, field.get_node_id (),
     156         1797 :                                      mappings.get_next_hir_id (crate_num),
     157         1797 :                                      mappings.get_next_localdef_id (crate_num));
     158              : 
     159         1797 :       fields.emplace_back (mapping, std::unique_ptr<HIR::Type> (type), vis,
     160         3594 :                            field.get_locus (), field.get_outer_attrs ());
     161         1797 :     }
     162              : 
     163         1014 :   auto crate_num = mappings.get_current_crate ();
     164         2028 :   Analysis::NodeMapping mapping (crate_num, struct_decl.get_node_id (),
     165         1014 :                                  mappings.get_next_hir_id (crate_num),
     166         1014 :                                  mappings.get_next_localdef_id (crate_num));
     167              : 
     168         2028 :   translated = new HIR::TupleStruct (mapping, std::move (fields),
     169         1014 :                                      struct_decl.get_identifier (),
     170              :                                      std::move (generic_params),
     171              :                                      std::move (where_clause), vis,
     172         1014 :                                      struct_decl.get_outer_attrs (),
     173         1014 :                                      struct_decl.get_locus ());
     174         1014 : }
     175              : 
     176              : void
     177         1708 : ASTLoweringItem::visit (AST::StructStruct &struct_decl)
     178              : {
     179         1708 :   std::vector<std::unique_ptr<HIR::GenericParam>> generic_params;
     180         1708 :   if (struct_decl.has_generics ())
     181              :     {
     182          531 :       generic_params = lower_generic_params (struct_decl.get_generic_params ());
     183              :     }
     184              : 
     185         1708 :   std::vector<std::unique_ptr<HIR::WhereClauseItem>> where_clause_items;
     186         1708 :   where_clause_items.reserve (
     187         1708 :     struct_decl.get_where_clause ().get_items ().size ());
     188              : 
     189         1714 :   for (auto &item : struct_decl.get_where_clause ().get_items ())
     190            6 :     where_clause_items.emplace_back (
     191            6 :       ASTLowerWhereClauseItem::translate (*item.get ()));
     192              : 
     193         1708 :   HIR::WhereClause where_clause (std::move (where_clause_items));
     194              : 
     195         1708 :   HIR::Visibility vis = translate_visibility (struct_decl.get_visibility ());
     196              : 
     197         1708 :   bool is_unit = struct_decl.is_unit_struct ();
     198         1708 :   std::vector<HIR::StructField> fields;
     199         3702 :   for (AST::StructField &field : struct_decl.get_fields ())
     200              :     {
     201         1994 :       if (field.get_field_type ().is_marked_for_strip ())
     202            4 :         continue;
     203              : 
     204         1994 :       HIR::Visibility vis = translate_visibility (field.get_visibility ());
     205         1994 :       HIR::Type *type = ASTLoweringType::translate (field.get_field_type ());
     206              : 
     207         1994 :       auto crate_num = mappings.get_current_crate ();
     208         1994 :       Analysis::NodeMapping mapping (crate_num, field.get_node_id (),
     209         1994 :                                      mappings.get_next_hir_id (crate_num),
     210         1994 :                                      mappings.get_next_localdef_id (crate_num));
     211              : 
     212         1994 :       HIR::StructField translated_field (mapping, field.get_field_name (),
     213         3988 :                                          std::unique_ptr<HIR::Type> (type), vis,
     214              :                                          field.get_locus (),
     215         3988 :                                          field.get_outer_attrs ());
     216              : 
     217         1994 :       if (struct_field_name_exists (fields, translated_field))
     218            4 :         continue;
     219              : 
     220         1990 :       fields.push_back (std::move (translated_field));
     221         1994 :     }
     222              : 
     223         1708 :   auto crate_num = mappings.get_current_crate ();
     224         3416 :   Analysis::NodeMapping mapping (crate_num, struct_decl.get_node_id (),
     225         1708 :                                  mappings.get_next_hir_id (crate_num),
     226         1708 :                                  mappings.get_next_localdef_id (crate_num));
     227              : 
     228         3416 :   translated = new HIR::StructStruct (mapping, std::move (fields),
     229         1708 :                                       struct_decl.get_identifier (),
     230              :                                       std::move (generic_params),
     231              :                                       std::move (where_clause), is_unit, vis,
     232         1708 :                                       struct_decl.get_outer_attrs (),
     233         1708 :                                       struct_decl.get_locus ());
     234         1708 : }
     235              : 
     236              : void
     237          567 : ASTLoweringItem::visit (AST::Enum &enum_decl)
     238              : {
     239          567 :   std::vector<std::unique_ptr<HIR::GenericParam>> generic_params;
     240          567 :   if (enum_decl.has_generics ())
     241              :     {
     242          245 :       generic_params = lower_generic_params (enum_decl.get_generic_params ());
     243              :     }
     244              : 
     245          567 :   std::vector<std::unique_ptr<HIR::WhereClauseItem>> where_clause_items;
     246          567 :   where_clause_items.reserve (
     247          567 :     enum_decl.get_where_clause ().get_items ().size ());
     248              : 
     249          567 :   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          567 :   HIR::WhereClause where_clause (std::move (where_clause_items));
     254          567 :   HIR::Visibility vis = translate_visibility (enum_decl.get_visibility ());
     255              : 
     256              :   // bool is_unit = enum_decl.is_zero_variant ();
     257          567 :   std::vector<std::unique_ptr<HIR::EnumItem>> items;
     258          567 :   items.reserve (enum_decl.get_variants ().size ());
     259              : 
     260         1878 :   for (auto &variant : enum_decl.get_variants ())
     261              :     {
     262         1311 :       if (variant->is_marked_for_strip ())
     263            0 :         continue;
     264              : 
     265         1311 :       items.emplace_back (ASTLoweringEnumItem::translate (variant.get ()));
     266              :     }
     267              : 
     268          567 :   auto crate_num = mappings.get_current_crate ();
     269         1134 :   Analysis::NodeMapping mapping (crate_num, enum_decl.get_node_id (),
     270          567 :                                  mappings.get_next_hir_id (crate_num),
     271          567 :                                  mappings.get_next_localdef_id (crate_num));
     272              : 
     273          567 :   HIR::Enum *hir_enum
     274          567 :     = new HIR::Enum (mapping, enum_decl.get_identifier (), vis,
     275              :                      std::move (generic_params), std::move (where_clause),
     276          567 :                      std::move (items), enum_decl.get_outer_attrs (),
     277         1134 :                      enum_decl.get_locus ());
     278          567 :   translated = hir_enum;
     279         1878 :   for (auto &variant : hir_enum->get_variants ())
     280              :     {
     281         1311 :       mappings.insert_hir_enumitem (hir_enum, variant.get ());
     282              :     }
     283          567 : }
     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           57 : ASTLoweringItem::visit (AST::StaticItem &var)
     343              : {
     344           57 :   HIR::Visibility vis = translate_visibility (var.get_visibility ());
     345              : 
     346           57 :   HIR::Type *type = ASTLoweringType::translate (var.get_type (), true);
     347           57 :   HIR::Expr *expr = ASTLoweringExpr::translate (var.get_expr ());
     348              : 
     349           57 :   auto crate_num = mappings.get_current_crate ();
     350          114 :   Analysis::NodeMapping mapping (crate_num, var.get_node_id (),
     351           57 :                                  mappings.get_next_hir_id (crate_num),
     352           57 :                                  mappings.get_next_localdef_id (crate_num));
     353              : 
     354           57 :   translated = new HIR::StaticItem (mapping, var.get_identifier (),
     355           57 :                                     var.is_mutable () ? Mutability::Mut
     356              :                                                       : Mutability::Imm,
     357           57 :                                     std::unique_ptr<HIR::Type> (type),
     358          114 :                                     std::unique_ptr<HIR::Expr> (expr), vis,
     359          171 :                                     var.get_outer_attrs (), var.get_locus ());
     360           57 : }
     361              : 
     362              : void
     363          487 : ASTLoweringItem::visit (AST::ConstantItem &constant)
     364              : {
     365          487 :   HIR::Visibility vis = translate_visibility (constant.get_visibility ());
     366              : 
     367          487 :   HIR::Type *type = ASTLoweringType::translate (constant.get_type (), true);
     368          487 :   HIR::Expr *expr = nullptr;
     369          487 :   if (constant.has_expr ())
     370          486 :     expr = ASTLoweringExpr::translate (constant.get_expr ());
     371              : 
     372          487 :   auto crate_num = mappings.get_current_crate ();
     373          487 :   Analysis::NodeMapping mapping (crate_num, constant.get_node_id (),
     374          487 :                                  mappings.get_next_hir_id (crate_num),
     375          487 :                                  mappings.get_next_localdef_id (crate_num));
     376              : 
     377          487 :   translated = new HIR::ConstantItem (mapping, constant.get_identifier (), vis,
     378         1461 :                                       std::unique_ptr<HIR::Type> (type),
     379          974 :                                       std::unique_ptr<HIR::Expr> (expr),
     380          487 :                                       constant.get_outer_attrs (),
     381         1461 :                                       constant.get_locus ());
     382          487 : }
     383              : 
     384              : void
     385         6964 : ASTLoweringItem::visit (AST::Function &function)
     386              : {
     387         6964 :   if (function.is_marked_for_strip ())
     388            0 :     return;
     389              : 
     390         6964 :   std::vector<std::unique_ptr<HIR::WhereClauseItem>> where_clause_items;
     391         6964 :   where_clause_items.reserve (
     392         6964 :     function.get_where_clause ().get_items ().size ());
     393              : 
     394         7023 :   for (auto &item : function.get_where_clause ().get_items ())
     395           59 :     where_clause_items.emplace_back (
     396           59 :       ASTLowerWhereClauseItem::translate (*item.get ()));
     397              : 
     398         6964 :   HIR::WhereClause where_clause (std::move (where_clause_items));
     399         6964 :   HIR::FunctionQualifiers qualifiers
     400         6964 :     = lower_qualifiers (function.get_qualifiers ());
     401         6964 :   HIR::Visibility vis = translate_visibility (function.get_visibility ());
     402              : 
     403              :   // need
     404         6964 :   std::vector<std::unique_ptr<HIR::GenericParam>> generic_params;
     405         6964 :   if (function.has_generics ())
     406              :     {
     407          683 :       generic_params = lower_generic_params (function.get_generic_params ());
     408              :     }
     409         6964 :   Identifier function_name = function.get_function_name ();
     410         6964 :   location_t locus = function.get_locus ();
     411              : 
     412         6964 :   std::unique_ptr<HIR::Type> return_type
     413         6964 :     = function.has_return_type () ? std::unique_ptr<HIR::Type> (
     414              :         ASTLoweringType::translate (function.get_return_type (), false,
     415              :                                     ASTLoweringType::ImplTrait::Allow))
     416         6964 :                                   : nullptr;
     417              : 
     418         6964 :   std::vector<HIR::FunctionParam> function_params;
     419         6964 :   function_params.reserve (function.get_function_params ().size ());
     420              : 
     421         6964 :   auto crate_num = mappings.get_current_crate ();
     422         9150 :   for (auto &p : function.get_function_params ())
     423              :     {
     424         2186 :       if (p->is_variadic ())
     425            2 :         continue;
     426         2186 :       if (p->is_self ())
     427              :         {
     428            0 :           rich_location r (line_table, p->get_locus ());
     429            0 :           r.add_range (function.get_locus ());
     430            0 :           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            0 :           continue;
     452            0 :         }
     453              : 
     454         2186 :       auto &param = static_cast<AST::FunctionParam &> (*p);
     455              : 
     456         2186 :       auto translated_pattern = std::unique_ptr<HIR::Pattern> (
     457         2186 :         ASTLoweringPattern::translate (param.get_pattern ()));
     458              : 
     459         2186 :       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         2184 :         default:
     466              :           // defer checking for when we have type information after lowering
     467         2184 :           break;
     468              :         }
     469              : 
     470         2184 :       auto translated_type = std::unique_ptr<HIR::Type> (
     471         2184 :         ASTLoweringType::translate (param.get_type ()));
     472              : 
     473         2184 :       auto crate_num = mappings.get_current_crate ();
     474         2184 :       Analysis::NodeMapping mapping (crate_num, param.get_node_id (),
     475         2184 :                                      mappings.get_next_hir_id (crate_num),
     476         2184 :                                      UNKNOWN_LOCAL_DEFID);
     477              : 
     478         2184 :       function_params.emplace_back (mapping, std::move (translated_pattern),
     479              :                                     std::move (translated_type),
     480         2184 :                                     param.get_locus ());
     481         2186 :     }
     482              : 
     483         6964 :   bool terminated = false;
     484         6964 :   std::unique_ptr<HIR::BlockExpr> function_body
     485              :     = std::unique_ptr<HIR::BlockExpr> (
     486         6964 :       ASTLoweringBlock::translate (*function.get_definition ().value (),
     487         6964 :                                    &terminated));
     488              : 
     489        13928 :   Analysis::NodeMapping mapping (crate_num, function.get_node_id (),
     490         6964 :                                  mappings.get_next_hir_id (crate_num),
     491         6964 :                                  mappings.get_next_localdef_id (crate_num));
     492              : 
     493         6964 :   mappings.insert_location (function_body->get_mappings ().get_hirid (),
     494              :                             function.get_locus ());
     495              : 
     496         6964 :   Defaultness defaultness
     497         6964 :     = function.is_default () ? Defaultness::Default : Defaultness::Final;
     498              : 
     499         6964 :   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         6964 :                          std::move (vis), function.get_outer_attrs (),
     505         6964 :                          tl::nullopt, defaultness, locus);
     506              : 
     507              :   // add the mappings for the function params at the end
     508         9148 :   for (auto &param : fn->get_function_params ())
     509              :     {
     510         2184 :       mappings.insert_hir_param (&param);
     511         2184 :       mappings.insert_location (mapping.get_hirid (), param.get_locus ());
     512              :     }
     513              : 
     514         6964 :   translated = fn;
     515         6964 : }
     516              : 
     517              : void
     518         1047 : ASTLoweringItem::visit (AST::InherentImpl &impl_block)
     519              : {
     520         1047 :   std::vector<std::unique_ptr<HIR::WhereClauseItem>> where_clause_items;
     521         1047 :   where_clause_items.reserve (
     522         1047 :     impl_block.get_where_clause ().get_items ().size ());
     523              : 
     524         1047 :   for (auto &item : impl_block.get_where_clause ().get_items ())
     525            0 :     where_clause_items.emplace_back (
     526            0 :       ASTLowerWhereClauseItem::translate (*item));
     527              : 
     528         1047 :   HIR::WhereClause where_clause (std::move (where_clause_items));
     529         1047 :   HIR::Visibility vis = translate_visibility (impl_block.get_visibility ());
     530              : 
     531         1047 :   std::vector<std::unique_ptr<HIR::GenericParam>> generic_params;
     532         1047 :   if (impl_block.has_generics ())
     533              :     {
     534          359 :       generic_params = lower_generic_params (impl_block.get_generic_params ());
     535              : 
     536          719 :       for (auto &generic_param : generic_params)
     537              :         {
     538          360 :           switch (generic_param->get_kind ())
     539              :             {
     540          332 :             case HIR::GenericParam::GenericKind::TYPE:
     541          332 :               {
     542          332 :                 const HIR::TypeParam &t
     543          332 :                   = static_cast<const HIR::TypeParam &> (*generic_param);
     544              : 
     545          332 :                 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         1047 :   HIR::Type *impl_type = ASTLoweringType::translate (impl_block.get_type ());
     567              : 
     568         1047 :   auto crate_num = mappings.get_current_crate ();
     569         2094 :   Analysis::NodeMapping mapping (crate_num, impl_block.get_node_id (),
     570         1047 :                                  mappings.get_next_hir_id (crate_num),
     571         1047 :                                  mappings.get_next_localdef_id (crate_num));
     572              : 
     573         2094 :   std::vector<std::unique_ptr<HIR::ImplItem>> impl_items;
     574         1047 :   std::vector<HirId> impl_item_ids;
     575         4261 :   for (auto &impl_item : impl_block.get_impl_items ())
     576              :     {
     577         3214 :       if (impl_item->is_marked_for_strip ())
     578            0 :         continue;
     579              : 
     580         3214 :       HIR::ImplItem *lowered
     581         3214 :         = ASTLowerImplItem::translate (*impl_item, mapping.get_hirid ());
     582         3214 :       rust_assert (lowered != nullptr);
     583         3214 :       impl_items.emplace_back (lowered);
     584         3214 :       impl_item_ids.push_back (lowered->get_impl_mappings ().get_hirid ());
     585              :     }
     586              : 
     587         1047 :   BoundPolarity polarity = BoundPolarity::RegularBound;
     588         1047 :   HIR::ImplBlock *hir_impl_block = new HIR::ImplBlock (
     589              :     mapping, std::move (impl_items), std::move (generic_params),
     590         1047 :     std::unique_ptr<HIR::Type> (impl_type), nullptr, where_clause, polarity,
     591         2094 :     vis, impl_block.get_inner_attrs (), impl_block.get_outer_attrs (),
     592         1047 :     impl_block.get_locus (), false);
     593         1047 :   translated = hir_impl_block;
     594              : 
     595         1047 :   mappings.insert_hir_impl_block (hir_impl_block);
     596         4261 :   for (auto &impl_item_id : impl_item_ids)
     597              :     {
     598         3214 :       mappings.insert_impl_item_mapping (impl_item_id, hir_impl_block);
     599              :     }
     600         1047 : }
     601              : 
     602              : void
     603         4190 : ASTLoweringItem::visit (AST::Trait &trait)
     604              : {
     605         4190 :   std::vector<std::unique_ptr<HIR::WhereClauseItem>> where_clause_items;
     606         4190 :   where_clause_items.reserve (trait.get_where_clause ().get_items ().size ());
     607              : 
     608         4197 :   for (auto &item : trait.get_where_clause ().get_items ())
     609            7 :     where_clause_items.emplace_back (
     610            7 :       ASTLowerWhereClauseItem::translate (*item.get ()));
     611              : 
     612         4190 :   HIR::WhereClause where_clause (std::move (where_clause_items));
     613              : 
     614         4190 :   HIR::Visibility vis = translate_visibility (trait.get_visibility ());
     615              : 
     616         4190 :   std::vector<std::unique_ptr<HIR::GenericParam>> generic_params;
     617         4190 :   if (trait.has_generics ())
     618              :     {
     619          701 :       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         4190 :   HIR::GenericParam *self_param
     625         4190 :     = ASTLowerGenericParam::translate (trait.get_implicit_self ());
     626         4190 :   generic_params.emplace (generic_params.begin (), self_param);
     627              : 
     628         4190 :   std::vector<std::unique_ptr<HIR::TypeParamBound>> type_param_bounds;
     629         4190 :   if (trait.has_type_param_bounds ())
     630              :     {
     631         1216 :       for (auto &bound : trait.get_type_param_bounds ())
     632          645 :         type_param_bounds.emplace_back (lower_bound (*bound));
     633              :     }
     634              : 
     635         4190 :   auto trait_item_size = trait.get_trait_items ().size ();
     636              : 
     637         4190 :   std::vector<std::unique_ptr<HIR::TraitItem>> trait_items;
     638         4190 :   trait_items.reserve (trait_item_size);
     639         4190 :   std::vector<HirId> trait_item_ids;
     640         4190 :   trait_item_ids.reserve (trait_item_size);
     641              : 
     642         7804 :   for (auto &item : trait.get_trait_items ())
     643              :     {
     644         3614 :       if (item->is_marked_for_strip ())
     645            0 :         continue;
     646              : 
     647         3614 :       HIR::TraitItem *lowered = ASTLowerTraitItem::translate (*item);
     648         3614 :       trait_item_ids.push_back (lowered->get_mappings ().get_hirid ());
     649         3614 :       trait_items.emplace_back (lowered);
     650              :     }
     651              : 
     652         4190 :   auto crate_num = mappings.get_current_crate ();
     653         8380 :   Analysis::NodeMapping mapping (crate_num, trait.get_node_id (),
     654         4190 :                                  mappings.get_next_hir_id (crate_num),
     655         4190 :                                  mappings.get_next_localdef_id (crate_num));
     656              : 
     657         4190 :   auto trait_unsafety
     658         4190 :     = trait.is_unsafe () ? Unsafety::Unsafe : Unsafety::Normal;
     659              : 
     660         4190 :   HIR::Trait *hir_trait
     661         8380 :     = 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         4190 :                       trait.get_outer_attrs (), trait.get_locus ());
     665              : 
     666         4190 :   if (trait.is_auto ())
     667           15 :     mappings.insert_auto_trait (hir_trait);
     668              : 
     669         4190 :   translated = hir_trait;
     670              : 
     671         7804 :   for (auto trait_item_id : trait_item_ids)
     672              :     {
     673         3614 :       mappings.insert_trait_item_mapping (trait_item_id, hir_trait);
     674              :     }
     675         4190 : }
     676              : 
     677              : void
     678         5348 : ASTLoweringItem::visit (AST::TraitImpl &impl_block)
     679              : {
     680         5348 :   bool unsafe = impl_block.is_unsafe ();
     681              : 
     682         5348 :   std::vector<std::unique_ptr<HIR::WhereClauseItem>> where_clause_items;
     683         5348 :   where_clause_items.reserve (
     684         5348 :     impl_block.get_where_clause ().get_items ().size ());
     685              : 
     686         5449 :   for (auto &item : impl_block.get_where_clause ().get_items ())
     687          101 :     where_clause_items.emplace_back (
     688          101 :       ASTLowerWhereClauseItem::translate (*item));
     689              : 
     690         5348 :   HIR::WhereClause where_clause (std::move (where_clause_items));
     691         5348 :   HIR::Visibility vis = translate_visibility (impl_block.get_visibility ());
     692              : 
     693         5348 :   std::vector<std::unique_ptr<HIR::GenericParam>> generic_params;
     694         5348 :   if (impl_block.has_generics ())
     695              :     {
     696          714 :       generic_params = lower_generic_params (impl_block.get_generic_params ());
     697              : 
     698         1560 :       for (auto &generic_param : generic_params)
     699              :         {
     700          846 :           switch (generic_param->get_kind ())
     701              :             {
     702          764 :             case HIR::GenericParam::GenericKind::TYPE:
     703          764 :               {
     704          764 :                 const HIR::TypeParam &t
     705          764 :                   = static_cast<const HIR::TypeParam &> (*generic_param);
     706              : 
     707          764 :                 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         5348 :   HIR::Type *impl_type = ASTLoweringType::translate (impl_block.get_type ());
     729         5348 :   HIR::TypePath *trait_ref
     730         5348 :     = ASTLowerTypePath::translate (impl_block.get_trait_path ());
     731              : 
     732         5348 :   auto crate_num = mappings.get_current_crate ();
     733        10696 :   Analysis::NodeMapping mapping (crate_num, impl_block.get_node_id (),
     734         5348 :                                  mappings.get_next_hir_id (crate_num),
     735         5348 :                                  mappings.get_next_localdef_id (crate_num));
     736              : 
     737         5348 :   auto impl_items_size = impl_block.get_impl_items ().size ();
     738              : 
     739        10696 :   std::vector<std::unique_ptr<HIR::ImplItem>> impl_items;
     740         5348 :   impl_items.reserve (impl_items_size);
     741         5348 :   std::vector<HirId> impl_item_ids;
     742         5348 :   impl_item_ids.reserve (impl_items_size);
     743              : 
     744        11523 :   for (auto &impl_item : impl_block.get_impl_items ())
     745              :     {
     746         6175 :       if (impl_item->is_marked_for_strip ())
     747            0 :         continue;
     748              : 
     749         6175 :       HIR::ImplItem *lowered
     750         6175 :         = ASTLowerImplItem::translate (*impl_item, mapping.get_hirid ());
     751         6175 :       rust_assert (lowered != nullptr);
     752         6175 :       impl_item_ids.push_back (lowered->get_impl_mappings ().get_hirid ());
     753         6175 :       impl_items.emplace_back (lowered);
     754              :     }
     755              : 
     756         5348 :   BoundPolarity polarity = impl_block.is_exclam ()
     757         5348 :                              ? BoundPolarity::NegativeBound
     758         5341 :                              : BoundPolarity::RegularBound;
     759         5348 :   HIR::ImplBlock *hir_impl_block = new HIR::ImplBlock (
     760              :     mapping, std::move (impl_items), std::move (generic_params),
     761        16044 :     std::unique_ptr<HIR::Type> (impl_type),
     762        10696 :     std::unique_ptr<HIR::TypePath> (trait_ref), where_clause, polarity, vis,
     763        10696 :     impl_block.get_inner_attrs (), impl_block.get_outer_attrs (),
     764        10696 :     impl_block.get_locus (), unsafe);
     765         5348 :   translated = hir_impl_block;
     766              : 
     767         5348 :   mappings.insert_hir_impl_block (hir_impl_block);
     768              : 
     769        11523 :   for (auto &impl_item_id : impl_item_ids)
     770              :     {
     771         6175 :       mappings.insert_impl_item_mapping (impl_item_id, hir_impl_block);
     772              :     }
     773         5348 : }
     774              : 
     775              : void
     776         1698 : ASTLoweringItem::visit (AST::ExternBlock &extern_block)
     777              : {
     778         1698 :   translated = lower_extern_block (extern_block);
     779         1698 : }
     780              : 
     781              : void
     782          989 : ASTLoweringItem::visit (AST::MacroRulesDefinition &def)
     783              : {
     784          989 :   lower_macro_definition (def);
     785          989 : }
     786              : 
     787              : void
     788           40 : ASTLoweringItem::visit (AST::ExternCrate &extern_crate)
     789              : {
     790           40 :   if (extern_crate.references_self ())
     791              :     return;
     792              : 
     793           40 :   auto &mappings = Analysis::Mappings::get ();
     794           40 :   CrateNum num
     795           40 :     = mappings.lookup_crate_name (extern_crate.get_referenced_crate ())
     796           40 :         .value ();
     797           40 :   AST::Crate &crate = mappings.get_ast_crate (num);
     798              : 
     799           40 :   auto saved_crate_num = mappings.get_current_crate ();
     800           40 :   mappings.set_current_crate (num);
     801           40 :   auto lowered = ASTLowering::Resolve (crate);
     802           40 :   mappings.insert_hir_crate (std::move (lowered));
     803           32 :   mappings.set_current_crate (saved_crate_num);
     804           32 : }
     805              : 
     806              : HIR::SimplePath
     807           64 : ASTLoweringSimplePath::translate (const AST::SimplePath &path)
     808              : {
     809           64 :   ASTLoweringSimplePath resolver;
     810              : 
     811           64 :   return resolver.lower (path);
     812           64 : }
     813              : 
     814              : HIR::SimplePathSegment
     815           76 : ASTLoweringSimplePath::lower (const AST::SimplePathSegment &segment)
     816              : {
     817           76 :   auto crate_num = mappings.get_current_crate ();
     818           76 :   auto node_id = segment.get_node_id ();
     819              : 
     820           76 :   auto mapping = Analysis::NodeMapping (crate_num, node_id,
     821           76 :                                         mappings.get_next_hir_id (crate_num),
     822           76 :                                         UNKNOWN_LOCAL_DEFID);
     823              : 
     824           76 :   auto hir_seg = HIR::SimplePathSegment (mapping);
     825              : 
     826           76 :   mappings.insert_node_to_hir (node_id, mapping.get_hirid ());
     827              :   // mappings.insert_simple_path_segment (crate_num, node_id, &segment);
     828              : 
     829           76 :   return hir_seg;
     830              : }
     831              : 
     832              : HIR::SimplePath
     833           64 : ASTLoweringSimplePath::lower (const AST::SimplePath &path)
     834              : {
     835           64 :   auto segments = std::vector<HIR::SimplePathSegment> ();
     836          140 :   for (auto &segment : path.get_segments ())
     837           76 :     segments.emplace_back (lower (segment));
     838              : 
     839           64 :   auto crate_num = mappings.get_current_crate ();
     840           64 :   auto node_id = path.get_node_id ();
     841              : 
     842           64 :   auto mapping = Analysis::NodeMapping (crate_num, node_id,
     843           64 :                                         mappings.get_next_hir_id (crate_num),
     844           64 :                                         UNKNOWN_LOCAL_DEFID);
     845              : 
     846           64 :   auto lowered
     847           64 :     = HIR::SimplePath (std::move (segments), mapping, path.get_locus ());
     848              : 
     849           64 :   mappings.insert_node_to_hir (node_id, mapping.get_hirid ());
     850              :   // mappings.insert_simple_path (crate_num, node_id, &path);
     851              : 
     852           64 :   return lowered;
     853           64 : }
     854              : 
     855              : } // namespace HIR
     856              : } // 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.