LCOV - code coverage report
Current view: top level - gcc/rust/resolve - rust-toplevel-name-resolver-2.0.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 84.0 % 262 220
Test Date: 2025-07-26 09:32:30 Functions: 93.5 % 62 58
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-toplevel-name-resolver-2.0.h"
      20                 :             : #include "input.h"
      21                 :             : #include "optional.h"
      22                 :             : #include "rust-ast-full.h"
      23                 :             : #include "rust-hir-map.h"
      24                 :             : #include "rust-attribute-values.h"
      25                 :             : 
      26                 :             : namespace Rust {
      27                 :             : namespace Resolver2_0 {
      28                 :             : 
      29                 :        3041 : TopLevel::TopLevel (NameResolutionContext &resolver)
      30                 :        3041 :   : DefaultResolver (resolver), dirty (false)
      31                 :        3041 : {}
      32                 :             : 
      33                 :             : template <typename T>
      34                 :             : void
      35                 :         435 : TopLevel::insert_enum_variant_or_error_out (const Identifier &identifier,
      36                 :             :                                             const T &node)
      37                 :             : {
      38                 :         435 :   insert_enum_variant_or_error_out (identifier, node.get_locus (),
      39                 :             :                                     node.get_node_id ());
      40                 :         435 : }
      41                 :             : 
      42                 :             : void
      43                 :       17399 : TopLevel::check_multiple_insertion_error (
      44                 :             :   tl::expected<NodeId, DuplicateNameError> result, const Identifier &identifier,
      45                 :             :   const location_t &locus, const NodeId node_id)
      46                 :             : {
      47                 :       17399 :   if (result)
      48                 :        7229 :     dirty = true;
      49                 :       10170 :   else if (result.error ().existing != node_id)
      50                 :             :     {
      51                 :          18 :       rich_location rich_loc (line_table, locus);
      52                 :          18 :       rich_loc.add_range (node_locations[result.error ().existing]);
      53                 :             : 
      54                 :          18 :       rust_error_at (rich_loc, ErrorCode::E0428, "%qs defined multiple times",
      55                 :          18 :                      identifier.as_string ().c_str ());
      56                 :          18 :     }
      57                 :       17399 : }
      58                 :             : void
      59                 :         435 : TopLevel::insert_enum_variant_or_error_out (const Identifier &identifier,
      60                 :             :                                             const location_t &locus,
      61                 :             :                                             const NodeId node_id)
      62                 :             : {
      63                 :             :   // keep track of each node's location to provide useful errors
      64                 :         435 :   node_locations.emplace (node_id, locus);
      65                 :             : 
      66                 :         435 :   auto result = ctx.insert_variant (identifier, node_id);
      67                 :         670 :   check_multiple_insertion_error (result, identifier, locus, node_id);
      68                 :         435 : }
      69                 :             : 
      70                 :             : template <typename T>
      71                 :             : void
      72                 :       16701 : TopLevel::insert_or_error_out (const Identifier &identifier, const T &node,
      73                 :             :                                Namespace ns)
      74                 :             : {
      75                 :       16701 :   insert_or_error_out (identifier, node.get_locus (), node.get_node_id (), ns);
      76                 :       16701 : }
      77                 :             : 
      78                 :             : void
      79                 :       16964 : TopLevel::insert_or_error_out (const Identifier &identifier,
      80                 :             :                                const location_t &locus, const NodeId &node_id,
      81                 :             :                                Namespace ns)
      82                 :             : {
      83                 :             :   // keep track of each node's location to provide useful errors
      84                 :       16964 :   node_locations.emplace (node_id, locus);
      85                 :             : 
      86                 :       16964 :   auto result = ctx.insert (identifier, node_id, ns);
      87                 :       26899 :   check_multiple_insertion_error (result, identifier, locus, node_id);
      88                 :       16964 : }
      89                 :             : 
      90                 :             : void
      91                 :        3041 : TopLevel::go (AST::Crate &crate)
      92                 :             : {
      93                 :             :   // we do not include builtin types in the top-level definition collector, as
      94                 :             :   // they are not used until `Late`. furthermore, we run this visitor multiple
      95                 :             :   // times in a row in a fixed-point fashion, so it would make the code
      96                 :             :   // responsible for this ugly and perfom a lot of error checking.
      97                 :             : 
      98                 :       12280 :   for (auto &item : crate.items)
      99                 :        9239 :     item->accept_vis (*this);
     100                 :        3041 : }
     101                 :             : 
     102                 :             : void
     103                 :         620 : TopLevel::visit (AST::Module &module)
     104                 :             : {
     105                 :         620 :   insert_or_error_out (module.get_name (), module, Namespace::Types);
     106                 :             : 
     107                 :             :   // Parse the module's items if they haven't been expanded and the file
     108                 :             :   // should be parsed (i.e isn't hidden behind an untrue or impossible cfg
     109                 :             :   // directive
     110                 :             :   // TODO: make sure this is right
     111                 :             :   // TODO: avoid loading items if cfg attributes are present?
     112                 :             :   //       might not be needed if this runs after early resolution?
     113                 :             :   // This was copied from the old early resolver method
     114                 :             :   // 'accumulate_escaped_macros'
     115                 :         620 :   if (module.get_kind () == AST::Module::UNLOADED)
     116                 :             :     {
     117                 :          14 :       module.load_items ();
     118                 :             : 
     119                 :             :       // If the module was previously unloaded, then we don't want to visit it
     120                 :             :       // this time around as the CfgStrip hasn't run on its inner items yet.
     121                 :             :       // Skip it for now, mark the visitor as dirty and try again
     122                 :             : 
     123                 :          14 :       dirty = true;
     124                 :             : 
     125                 :          14 :       return;
     126                 :             :     }
     127                 :             : 
     128                 :         606 :   DefaultResolver::visit (module);
     129                 :             : 
     130                 :         606 :   if (Analysis::Mappings::get ().lookup_ast_module (module.get_node_id ())
     131                 :         606 :       == tl::nullopt)
     132                 :         271 :     Analysis::Mappings::get ().insert_ast_module (&module);
     133                 :             : }
     134                 :             : 
     135                 :             : void
     136                 :        1312 : TopLevel::visit (AST::Trait &trait)
     137                 :             : {
     138                 :        1312 :   insert_or_error_out (trait.get_identifier (), trait, Namespace::Types);
     139                 :             : 
     140                 :        1312 :   DefaultResolver::visit (trait);
     141                 :        1312 : }
     142                 :             : 
     143                 :             : void
     144                 :         388 : TopLevel::visit (AST::InherentImpl &impl)
     145                 :             : {
     146                 :         776 :   auto inner_fn = [this, &impl] () {
     147                 :         776 :     insert_or_error_out (Identifier ("Self", impl.get_type ().get_locus ()),
     148                 :         388 :                          impl.get_type (), Namespace::Types);
     149                 :             : 
     150                 :             :     // We do want to visit with the default visitor instead of default resolver
     151                 :             :     // because we don't want to insert the scope twice.
     152                 :         388 :     AST::DefaultASTVisitor::visit (impl);
     153                 :         776 :   };
     154                 :             : 
     155                 :         388 :   ctx.scoped (Rib::Kind::TraitOrImpl, impl.get_node_id (), inner_fn);
     156                 :         388 : }
     157                 :             : 
     158                 :             : void
     159                 :        1480 : TopLevel::visit (AST::TraitImpl &impl)
     160                 :             : {
     161                 :        2960 :   auto inner_fn = [this, &impl] () {
     162                 :        2960 :     insert_or_error_out (Identifier ("Self", impl.get_type ().get_locus ()),
     163                 :        1480 :                          impl.get_type (), Namespace::Types);
     164                 :             : 
     165                 :             :     // We do want to visit using the default visitor instead of default resolver
     166                 :             :     // because we don't want to insert the scope twice.
     167                 :        1480 :     AST::DefaultASTVisitor::visit (impl);
     168                 :        2960 :   };
     169                 :             : 
     170                 :        1480 :   ctx.scoped (Rib::Kind::TraitOrImpl, impl.get_node_id (), inner_fn);
     171                 :        1480 : }
     172                 :             : 
     173                 :             : void
     174                 :         257 : TopLevel::visit (AST::TraitItemType &trait_item)
     175                 :             : {
     176                 :         771 :   insert_or_error_out (trait_item.get_identifier ().as_string (), trait_item,
     177                 :             :                        Namespace::Types);
     178                 :             : 
     179                 :         257 :   DefaultResolver::visit (trait_item);
     180                 :         257 : }
     181                 :             : 
     182                 :             : template <typename PROC_MACRO>
     183                 :             : static void
     184                 :           0 : insert_macros (std::vector<PROC_MACRO> &macros, NameResolutionContext &ctx)
     185                 :             : {
     186                 :           0 :   for (auto &macro : macros)
     187                 :             :     {
     188                 :           0 :       auto res = ctx.macros.insert (macro.get_name (), macro.get_node_id ());
     189                 :             : 
     190                 :           0 :       if (!res && res.error ().existing != macro.get_node_id ())
     191                 :             :         {
     192                 :           0 :           rust_error_at (UNKNOWN_LOCATION, ErrorCode::E0428,
     193                 :             :                          "macro %qs defined multiple times",
     194                 :           0 :                          macro.get_name ().c_str ());
     195                 :             :         }
     196                 :             :     }
     197                 :           0 : }
     198                 :             : 
     199                 :             : void
     200                 :           3 : TopLevel::visit (AST::ExternCrate &crate)
     201                 :             : {
     202                 :           3 :   auto &mappings = Analysis::Mappings::get ();
     203                 :           3 :   auto num_opt = mappings.lookup_crate_name (crate.get_referenced_crate ());
     204                 :             : 
     205                 :           3 :   if (!num_opt)
     206                 :             :     {
     207                 :           3 :       rust_error_at (crate.get_locus (), "unknown crate %qs",
     208                 :           3 :                      crate.get_referenced_crate ().c_str ());
     209                 :           3 :       return;
     210                 :             :     }
     211                 :             : 
     212                 :           0 :   CrateNum num = *num_opt;
     213                 :             : 
     214                 :           0 :   auto attribute_macros = mappings.lookup_attribute_proc_macros (num);
     215                 :             : 
     216                 :           0 :   auto bang_macros = mappings.lookup_bang_proc_macros (num);
     217                 :             : 
     218                 :           0 :   auto derive_macros = mappings.lookup_derive_proc_macros (num);
     219                 :             : 
     220                 :           0 :   auto sub_visitor = [&] () {
     221                 :             :     // TODO: Find a way to keep this part clean without the double dispatch.
     222                 :           0 :     if (derive_macros.has_value ())
     223                 :             :       {
     224                 :           0 :         insert_macros (derive_macros.value (), ctx);
     225                 :           0 :         for (auto &macro : derive_macros.value ())
     226                 :           0 :           mappings.insert_derive_proc_macro_def (macro);
     227                 :             :       }
     228                 :           0 :     if (attribute_macros.has_value ())
     229                 :             :       {
     230                 :           0 :         insert_macros (attribute_macros.value (), ctx);
     231                 :           0 :         for (auto &macro : attribute_macros.value ())
     232                 :           0 :           mappings.insert_attribute_proc_macro_def (macro);
     233                 :             :       }
     234                 :           0 :     if (bang_macros.has_value ())
     235                 :             :       {
     236                 :           0 :         insert_macros (bang_macros.value (), ctx);
     237                 :           0 :         for (auto &macro : bang_macros.value ())
     238                 :           0 :           mappings.insert_bang_proc_macro_def (macro);
     239                 :             :       }
     240                 :           0 :   };
     241                 :             : 
     242                 :           0 :   if (crate.has_as_clause ())
     243                 :           0 :     ctx.scoped (Rib::Kind::Module, crate.get_node_id (), sub_visitor,
     244                 :           0 :                 crate.get_as_clause ());
     245                 :             :   else
     246                 :           0 :     ctx.scoped (Rib::Kind::Module, crate.get_node_id (), sub_visitor,
     247                 :           0 :                 crate.get_referenced_crate ());
     248                 :             : }
     249                 :             : 
     250                 :             : static bool
     251                 :        1545 : is_macro_export (AST::MacroRulesDefinition &def)
     252                 :             : {
     253                 :        1622 :   for (const auto &attr : def.get_outer_attrs ())
     254                 :          87 :     if (attr.get_path ().as_string () == Values::Attributes::MACRO_EXPORT)
     255                 :        1545 :       return true;
     256                 :             : 
     257                 :             :   return false;
     258                 :             : }
     259                 :             : 
     260                 :             : void
     261                 :        1545 : TopLevel::visit (AST::MacroRulesDefinition &macro)
     262                 :             : {
     263                 :             :   // we do not insert macros in the current rib as that needs to be done in the
     264                 :             :   // textual scope of the Early pass. we only insert them in the root of the
     265                 :             :   // crate if they are marked with #[macro_export]. The execption to this is
     266                 :             :   // macros 2.0, which get resolved and inserted like regular items.
     267                 :             : 
     268                 :        1545 :   if (is_macro_export (macro))
     269                 :             :     {
     270                 :          30 :       auto res = ctx.macros.insert_at_root (macro.get_rule_name (),
     271                 :          10 :                                             macro.get_node_id ());
     272                 :          10 :       if (!res && res.error ().existing != macro.get_node_id ())
     273                 :             :         {
     274                 :             :           // TODO: Factor this
     275                 :           0 :           rich_location rich_loc (line_table, macro.get_locus ());
     276                 :           0 :           rich_loc.add_range (node_locations[res.error ().existing]);
     277                 :             : 
     278                 :           0 :           rust_error_at (rich_loc, ErrorCode::E0428,
     279                 :             :                          "macro %qs defined multiple times",
     280                 :           0 :                          macro.get_rule_name ().as_string ().c_str ());
     281                 :           0 :         }
     282                 :          10 :     }
     283                 :             : 
     284                 :        1545 :   if (macro.get_kind () == AST::MacroRulesDefinition::MacroKind::DeclMacro)
     285                 :          39 :     insert_or_error_out (macro.get_rule_name (), macro, Namespace::Macros);
     286                 :             : 
     287                 :        1545 :   auto &mappings = Analysis::Mappings::get ();
     288                 :        1545 :   if (mappings.lookup_macro_def (macro.get_node_id ()))
     289                 :             :     return;
     290                 :             : 
     291                 :         218 :   mappings.insert_macro_def (&macro);
     292                 :             : }
     293                 :             : 
     294                 :             : void
     295                 :        6875 : TopLevel::visit (AST::Function &function)
     296                 :             : {
     297                 :        6875 :   insert_or_error_out (function.get_function_name (), function,
     298                 :             :                        Namespace::Values);
     299                 :             : 
     300                 :        6875 :   DefaultResolver::visit (function);
     301                 :        6875 : }
     302                 :             : 
     303                 :             : void
     304                 :          36 : TopLevel::visit (AST::StaticItem &static_item)
     305                 :             : {
     306                 :          36 :   insert_or_error_out (static_item.get_identifier (), static_item,
     307                 :             :                        Namespace::Values);
     308                 :             : 
     309                 :          36 :   DefaultResolver::visit (static_item);
     310                 :          36 : }
     311                 :             : 
     312                 :             : void
     313                 :           2 : TopLevel::visit (AST::ExternalStaticItem &static_item)
     314                 :             : {
     315                 :           6 :   insert_or_error_out (static_item.get_identifier ().as_string (), static_item,
     316                 :             :                        Namespace::Values);
     317                 :             : 
     318                 :           2 :   DefaultResolver::visit (static_item);
     319                 :           2 : }
     320                 :             : 
     321                 :             : void
     322                 :         664 : TopLevel::visit (AST::StructStruct &struct_item)
     323                 :             : {
     324                 :        1328 :   auto generic_vis = [this, &struct_item] () {
     325                 :         855 :     for (auto &g : struct_item.get_generic_params ())
     326                 :             :       {
     327                 :         191 :         g->accept_vis (*this);
     328                 :             :       }
     329                 :        1328 :   };
     330                 :             : 
     331                 :         664 :   ctx.scoped (Rib::Kind::Item, struct_item.get_node_id (), generic_vis);
     332                 :             : 
     333                 :         664 :   insert_or_error_out (struct_item.get_struct_name (), struct_item,
     334                 :             :                        Namespace::Types);
     335                 :             : 
     336                 :             :   // Do we need to insert the constructor in the value namespace as well?
     337                 :             : 
     338                 :             :   // Do we need to do anything if the struct is a unit struct?
     339                 :         664 :   if (struct_item.is_unit_struct ())
     340                 :         333 :     insert_or_error_out (struct_item.get_struct_name (), struct_item,
     341                 :             :                          Namespace::Values);
     342                 :         664 : }
     343                 :             : 
     344                 :             : void
     345                 :        2803 : TopLevel::visit (AST::TypeParam &type_param)
     346                 :             : {
     347                 :        2803 :   insert_or_error_out (type_param.get_type_representation (), type_param,
     348                 :             :                        Namespace::Types);
     349                 :             : 
     350                 :        2803 :   DefaultResolver::visit (type_param);
     351                 :        2803 : }
     352                 :             : 
     353                 :             : void
     354                 :          58 : TopLevel::visit (AST::ConstGenericParam &const_param)
     355                 :             : {
     356                 :          58 :   insert_or_error_out (const_param.get_name (), const_param, Namespace::Values);
     357                 :             : 
     358                 :          58 :   DefaultResolver::visit (const_param);
     359                 :          58 : }
     360                 :             : 
     361                 :             : void
     362                 :         340 : TopLevel::visit (AST::TupleStruct &tuple_struct)
     363                 :             : {
     364                 :         340 :   insert_or_error_out (tuple_struct.get_struct_name (), tuple_struct,
     365                 :             :                        Namespace::Types);
     366                 :             : 
     367                 :         340 :   insert_or_error_out (tuple_struct.get_struct_name (), tuple_struct,
     368                 :             :                        Namespace::Values);
     369                 :             : 
     370                 :         340 :   DefaultResolver::visit (tuple_struct);
     371                 :         340 : }
     372                 :             : 
     373                 :             : void
     374                 :         180 : TopLevel::visit (AST::EnumItem &variant)
     375                 :             : {
     376                 :         180 :   insert_enum_variant_or_error_out (variant.get_identifier (), variant);
     377                 :         180 : }
     378                 :             : 
     379                 :             : void
     380                 :         209 : TopLevel::visit (AST::EnumItemTuple &variant)
     381                 :             : {
     382                 :         209 :   insert_enum_variant_or_error_out (variant.get_identifier (), variant);
     383                 :         209 : }
     384                 :             : 
     385                 :             : void
     386                 :          46 : TopLevel::visit (AST::EnumItemStruct &variant)
     387                 :             : {
     388                 :          46 :   insert_enum_variant_or_error_out (variant.get_identifier (), variant);
     389                 :          46 : }
     390                 :             : 
     391                 :             : void
     392                 :          35 : TopLevel::visit (AST::EnumItemDiscriminant &variant)
     393                 :             : {
     394                 :          35 :   insert_or_error_out (variant.get_identifier (), variant, Namespace::Types);
     395                 :          35 : }
     396                 :             : 
     397                 :             : void
     398                 :         231 : TopLevel::visit (AST::Enum &enum_item)
     399                 :             : {
     400                 :         231 :   insert_or_error_out (enum_item.get_identifier (), enum_item,
     401                 :             :                        Namespace::Types);
     402                 :             : 
     403                 :         231 :   DefaultResolver::visit (enum_item);
     404                 :         231 : }
     405                 :             : 
     406                 :             : void
     407                 :          37 : TopLevel::visit (AST::Union &union_item)
     408                 :             : {
     409                 :          37 :   insert_or_error_out (union_item.get_identifier (), union_item,
     410                 :             :                        Namespace::Types);
     411                 :             : 
     412                 :          37 :   DefaultResolver::visit (union_item);
     413                 :          37 : }
     414                 :             : 
     415                 :             : void
     416                 :         221 : TopLevel::visit (AST::ConstantItem &const_item)
     417                 :             : {
     418                 :         663 :   insert_or_error_out (const_item.get_identifier (), const_item,
     419                 :             :                        Namespace::Values);
     420                 :             : 
     421                 :         221 :   DefaultResolver::visit (const_item);
     422                 :         221 : }
     423                 :             : 
     424                 :             : void
     425                 :         630 : TopLevel::visit (AST::TypeAlias &type_item)
     426                 :             : {
     427                 :         630 :   insert_or_error_out (type_item.get_new_type_name (), type_item,
     428                 :             :                        Namespace::Types);
     429                 :             : 
     430                 :         630 :   DefaultResolver::visit (type_item);
     431                 :         630 : }
     432                 :             : 
     433                 :             : static void
     434                 :             : flatten_rebind (
     435                 :             :   const AST::UseTreeRebind &glob,
     436                 :             :   std::vector<std::pair<AST::SimplePath, AST::UseTreeRebind>> &rebind_paths);
     437                 :             : 
     438                 :             : static void
     439                 :             : flatten_list (
     440                 :             :   const AST::UseTreeList &glob, std::vector<AST::SimplePath> &paths,
     441                 :             :   std::vector<AST::SimplePath> &glob_paths,
     442                 :             :   std::vector<std::pair<AST::SimplePath, AST::UseTreeRebind>> &rebind_paths,
     443                 :             :   NameResolutionContext &ctx);
     444                 :             : static void
     445                 :             : flatten_glob (const AST::UseTreeGlob &glob,
     446                 :             :               std::vector<AST::SimplePath> &glob_paths,
     447                 :             :               NameResolutionContext &ctx);
     448                 :             : 
     449                 :             : static void
     450                 :         335 : flatten (
     451                 :             :   const AST::UseTree *tree, std::vector<AST::SimplePath> &paths,
     452                 :             :   std::vector<AST::SimplePath> &glob_paths,
     453                 :             :   std::vector<std::pair<AST::SimplePath, AST::UseTreeRebind>> &rebind_paths,
     454                 :             :   NameResolutionContext &ctx)
     455                 :             : {
     456                 :         335 :   switch (tree->get_kind ())
     457                 :             :     {
     458                 :         258 :       case AST::UseTree::Rebind: {
     459                 :         258 :         auto rebind = static_cast<const AST::UseTreeRebind *> (tree);
     460                 :         258 :         flatten_rebind (*rebind, rebind_paths);
     461                 :         258 :         break;
     462                 :             :       }
     463                 :          55 :       case AST::UseTree::List: {
     464                 :          55 :         auto list = static_cast<const AST::UseTreeList *> (tree);
     465                 :          55 :         flatten_list (*list, paths, glob_paths, rebind_paths, ctx);
     466                 :          55 :         break;
     467                 :             :       }
     468                 :          22 :       case AST::UseTree::Glob: {
     469                 :          22 :         auto glob = static_cast<const AST::UseTreeGlob *> (tree);
     470                 :          22 :         flatten_glob (*glob, glob_paths, ctx);
     471                 :          22 :         break;
     472                 :             :       }
     473                 :             :       break;
     474                 :             :     }
     475                 :         335 : }
     476                 :             : 
     477                 :             : static void
     478                 :         258 : flatten_rebind (
     479                 :             :   const AST::UseTreeRebind &rebind,
     480                 :             :   std::vector<std::pair<AST::SimplePath, AST::UseTreeRebind>> &rebind_paths)
     481                 :             : {
     482                 :         258 :   rebind_paths.emplace_back (rebind.get_path (), rebind);
     483                 :         258 : }
     484                 :             : 
     485                 :             : /** Prefix a list of subpath
     486                 :             :  * @param prefix A prefix for all subpath
     487                 :             :  * @param subs List of subpath to prefix
     488                 :             :  * @param size List where results should be stored
     489                 :             :  */
     490                 :             : static void
     491                 :         296 : prefix_subpaths (AST::SimplePath prefix, std::vector<AST::SimplePath> subs,
     492                 :             :                  std::vector<AST::SimplePath> &results)
     493                 :             : {
     494                 :         296 :   for (auto &sub : subs)
     495                 :             :     {
     496                 :           0 :       auto new_path = prefix;
     497                 :           0 :       std::copy (sub.get_segments ().begin (), sub.get_segments ().end (),
     498                 :             :                  std::back_inserter (new_path.get_segments ()));
     499                 :           0 :       results.emplace_back (new_path);
     500                 :           0 :     }
     501                 :         296 : }
     502                 :             : 
     503                 :             : static void
     504                 :         148 : prefix_rebinds (
     505                 :             :   AST::SimplePath prefix,
     506                 :             :   std::vector<std::pair<AST::SimplePath, AST::UseTreeRebind>> subs,
     507                 :             :   std::vector<std::pair<AST::SimplePath, AST::UseTreeRebind>> &results)
     508                 :             : {
     509                 :         296 :   for (auto &sub : subs)
     510                 :             :     {
     511                 :         148 :       auto new_path = prefix;
     512                 :         148 :       std::copy (sub.first.get_segments ().begin (),
     513                 :         148 :                  sub.first.get_segments ().end (),
     514                 :             :                  std::back_inserter (new_path.get_segments ()));
     515                 :         296 :       results.emplace_back (std::make_pair (new_path, sub.second));
     516                 :         148 :     }
     517                 :         148 : }
     518                 :             : 
     519                 :             : static void
     520                 :          55 : flatten_list (
     521                 :             :   const AST::UseTreeList &list, std::vector<AST::SimplePath> &paths,
     522                 :             :   std::vector<AST::SimplePath> &glob_paths,
     523                 :             :   std::vector<std::pair<AST::SimplePath, AST::UseTreeRebind>> &rebind_paths,
     524                 :             :   NameResolutionContext &ctx)
     525                 :             : {
     526                 :          55 :   auto prefix = AST::SimplePath::create_empty ();
     527                 :          55 :   if (list.has_path ())
     528                 :          55 :     prefix = list.get_path ();
     529                 :             : 
     530                 :         203 :   for (const auto &tree : list.get_trees ())
     531                 :             :     {
     532                 :         148 :       auto sub_paths = std::vector<AST::SimplePath> ();
     533                 :         148 :       auto sub_globs = std::vector<AST::SimplePath> ();
     534                 :         148 :       auto sub_rebinds
     535                 :         148 :         = std::vector<std::pair<AST::SimplePath, AST::UseTreeRebind>> ();
     536                 :         148 :       flatten (tree.get (), sub_paths, sub_globs, sub_rebinds, ctx);
     537                 :             : 
     538                 :         148 :       prefix_subpaths (prefix, sub_paths, paths);
     539                 :         148 :       prefix_subpaths (prefix, sub_globs, glob_paths);
     540                 :         148 :       prefix_rebinds (prefix, sub_rebinds, rebind_paths);
     541                 :         148 :     }
     542                 :          55 : }
     543                 :             : 
     544                 :             : static void
     545                 :          22 : flatten_glob (const AST::UseTreeGlob &glob, std::vector<AST::SimplePath> &paths,
     546                 :             :               NameResolutionContext &ctx)
     547                 :             : {
     548                 :          22 :   if (glob.has_path ())
     549                 :          22 :     paths.emplace_back (glob.get_path ());
     550                 :             :   else
     551                 :           0 :     paths.emplace_back (AST::SimplePath ({}, false, glob.get_locus ()));
     552                 :          22 : }
     553                 :             : 
     554                 :             : void
     555                 :         187 : TopLevel::visit (AST::UseDeclaration &use)
     556                 :             : {
     557                 :         187 :   auto paths = std::vector<AST::SimplePath> ();
     558                 :         187 :   auto glob_path = std::vector<AST::SimplePath> ();
     559                 :         187 :   auto rebind_path
     560                 :         187 :     = std::vector<std::pair<AST::SimplePath, AST::UseTreeRebind>> ();
     561                 :             : 
     562                 :         187 :   auto &values_rib = ctx.values.peek ();
     563                 :         187 :   auto &types_rib = ctx.types.peek ();
     564                 :         187 :   auto &macros_rib = ctx.macros.peek ();
     565                 :             : 
     566                 :             :   // FIXME: How do we handle `use foo::{self}` imports? Some beforehand cleanup?
     567                 :             :   // How do we handle module imports in general? Should they get added to all
     568                 :             :   // namespaces?
     569                 :             : 
     570                 :         187 :   const auto &tree = use.get_tree ();
     571                 :         187 :   flatten (tree.get (), paths, glob_path, rebind_path, this->ctx);
     572                 :             : 
     573                 :         187 :   auto imports = std::vector<ImportKind> ();
     574                 :             : 
     575                 :         187 :   for (auto &&path : paths)
     576                 :           0 :     imports.emplace_back (
     577                 :           0 :       ImportKind::Simple (std::move (path), values_rib, types_rib, macros_rib));
     578                 :             : 
     579                 :         209 :   for (auto &&glob : glob_path)
     580                 :          22 :     imports.emplace_back (
     581                 :          22 :       ImportKind::Glob (std::move (glob), values_rib, types_rib, macros_rib));
     582                 :             : 
     583                 :         445 :   for (auto &&rebind : rebind_path)
     584                 :         258 :     imports.emplace_back (
     585                 :         516 :       ImportKind::Rebind (std::move (rebind.first), std::move (rebind.second),
     586                 :             :                           values_rib, types_rib, macros_rib));
     587                 :             : 
     588                 :         187 :   imports_to_resolve.insert ({use.get_node_id (), std::move (imports)});
     589                 :         187 : }
     590                 :             : 
     591                 :             : } // namespace Resolver2_0
     592                 :             : } // 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.