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.2 % 467 440
Test Date: 2025-08-30 13:27:53 Functions: 100.0 % 19 19
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: - 0 0

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

Generated by: LCOV version 2.1-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.