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