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: 94.7 % 283 268
Test Date: 2026-11-14 16:39:50 Functions: 100.0 % 64 64
Legend: Lines:     hit not hit

            Line data    Source code
       1              : // Copyright (C) 2020-2026 Free Software Foundation, Inc.
       2              : 
       3              : // This file is part of GCC.
       4              : 
       5              : // GCC is free software; you can redistribute it and/or modify it under
       6              : // the terms of the GNU General Public License as published by the Free
       7              : // Software Foundation; either version 3, or (at your option) any later
       8              : // version.
       9              : 
      10              : // GCC is distributed in the hope that it will be useful, but WITHOUT ANY
      11              : // WARRANTY; without even the implied warranty of MERCHANTABILITY or
      12              : // FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
      13              : // for more details.
      14              : 
      15              : // You should have received a copy of the GNU General Public License
      16              : // along with GCC; see the file COPYING3.  If not see
      17              : // <http://www.gnu.org/licenses/>.
      18              : 
      19              : #include "rust-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        17507 : TopLevel::TopLevel (NameResolutionContext &resolver)
      30        17507 :   : DefaultResolver (resolver), dirty (false)
      31        17507 : {}
      32              : 
      33              : template <typename T>
      34              : void
      35        30784 : TopLevel::insert_enum_variant_or_error_out (const Identifier &identifier,
      36              :                                             const T &node, bool is_also_value)
      37              : {
      38        30784 :   insert_enum_variant_or_error_out (identifier, node.get_locus (),
      39        30784 :                                     node.get_node_id (), is_also_value);
      40        30784 : }
      41              : 
      42              : void
      43      9132594 : 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      9132594 :   if (result)
      48       332427 :     dirty = true;
      49      8800167 :   else if (result.error ().existing != node_id)
      50              :     {
      51           39 :       rich_location rich_loc (line_table, locus);
      52           39 :       rich_loc.add_range (node_locations[result.error ().existing]);
      53           39 :       auto &mappings = Analysis::Mappings::get ();
      54           39 :       ErrorCode code;
      55           39 :       if (mappings.is_extern_crate (node_id)
      56           39 :           && mappings.is_extern_crate (result.error ().existing))
      57              :         code = ErrorCode::E0259;
      58           31 :       else if (mappings.is_extern_crate (node_id)
      59           31 :                || mappings.is_extern_crate (result.error ().existing))
      60              :         code = ErrorCode::E0260;
      61              :       else
      62              :         code = ErrorCode::E0428;
      63              : 
      64           39 :       rust_error_at (rich_loc, code, "%qs defined multiple times",
      65           39 :                      identifier.as_string ().c_str ());
      66           39 :     }
      67      9132594 : }
      68              : void
      69        30784 : TopLevel::insert_enum_variant_or_error_out (const Identifier &identifier,
      70              :                                             const location_t &locus,
      71              :                                             const NodeId node_id,
      72              :                                             bool is_also_value)
      73              : {
      74              :   // keep track of each node's location to provide useful errors
      75        30784 :   node_locations.emplace (node_id, locus);
      76              : 
      77        30784 :   auto result = ctx.insert_variant (identifier, node_id, is_also_value);
      78        59364 :   check_multiple_insertion_error (result, identifier, locus, node_id);
      79        30784 : }
      80              : 
      81              : template <typename T>
      82              : void
      83      9047172 : TopLevel::insert_or_error_out (const Identifier &identifier, const T &node,
      84              :                                Namespace ns)
      85              : {
      86      9047172 :   insert_or_error_out (identifier, node.get_locus (), node.get_node_id (), ns);
      87      9047172 : }
      88              : 
      89              : void
      90      9101810 : TopLevel::insert_or_error_out (const Identifier &identifier,
      91              :                                const location_t &locus, const NodeId &node_id,
      92              :                                Namespace ns)
      93              : {
      94              :   // keep track of each node's location to provide useful errors
      95      9101810 :   node_locations.emplace (node_id, locus);
      96              : 
      97      9101810 :   auto result = ctx.insert (identifier, node_id, ns);
      98     17873397 :   check_multiple_insertion_error (result, identifier, locus, node_id);
      99      9101810 : }
     100              : 
     101              : void
     102        17507 : TopLevel::go (AST::Crate &crate)
     103              : {
     104              :   // we do not include builtin types in the top-level definition collector, as
     105              :   // they are not used until `Late`. furthermore, we run this visitor multiple
     106              :   // times in a row in a fixed-point fashion, so it would make the code
     107              :   // responsible for this ugly and perfom a lot of error checking.
     108              : 
     109        17507 :   visit (crate);
     110              : 
     111        17507 :   if (Analysis::Mappings::get ().lookup_glob_container (crate.get_node_id ())
     112        17507 :       == tl::nullopt)
     113         5334 :     Analysis::Mappings::get ().insert_glob_container (crate.get_node_id (),
     114              :                                                       &crate);
     115        17507 : }
     116              : 
     117              : void
     118        73831 : TopLevel::visit (AST::Module &module)
     119              : {
     120        73831 :   if (Analysis::Mappings::get ().lookup_glob_container (module.get_node_id ())
     121        73831 :       == tl::nullopt)
     122         3527 :     Analysis::Mappings::get ().insert_glob_container (module.get_node_id (),
     123              :                                                       &module);
     124              : 
     125        73831 :   insert_or_error_out (module.get_name (), module, Namespace::Types);
     126              : 
     127        73831 :   Analysis::Mappings::get ().insert_module_id (module.get_node_id ());
     128              : 
     129        73831 :   DefaultResolver::visit (module);
     130        73831 : }
     131              : 
     132              : void
     133        53858 : TopLevel::visit (AST::Trait &trait)
     134              : {
     135        53858 :   insert_or_error_out (trait.get_identifier (), trait, Namespace::Types);
     136              : 
     137        53858 :   DefaultResolver::visit (trait);
     138        53858 : }
     139              : 
     140              : void
     141          230 : TopLevel::visit (AST::ExternCrate &crate)
     142              : {
     143          230 :   auto &name = crate.has_as_clause () ? crate.get_as_clause ()
     144          230 :                                       : crate.get_referenced_crate ();
     145          230 :   auto &mappings = Analysis::Mappings::get ();
     146          230 :   mappings.insert_extern_crate_id (crate.get_node_id ());
     147          690 :   insert_or_error_out (name, crate, Namespace::Types);
     148              : 
     149          230 :   DefaultResolver::visit (crate);
     150              : 
     151          230 :   if (crate.get_referenced_crate () != Values::Keywords::SELF)
     152              :     {
     153          227 :       if (auto extern_crate_num
     154          227 :           = mappings.crate.lookup_crate_name (crate.get_referenced_crate ()))
     155              :         {
     156          224 :           auto &extern_crate
     157          224 :             = mappings.crate.get_ast_crate (extern_crate_num.value ());
     158          224 :           if (!mappings.lookup_glob_container (extern_crate.get_node_id ()))
     159            0 :             mappings.insert_glob_container (extern_crate.get_node_id (),
     160              :                                             &extern_crate);
     161              :           // Insert the crate within the extern prelude only after the scope has
     162              :           // been created
     163          672 :           ctx.insert_crate_in_prelude (name, extern_crate.get_node_id ());
     164              :         }
     165              :     }
     166          230 : }
     167              : 
     168              : void
     169      1947440 : TopLevel::maybe_insert_big_self (AST::Impl &impl)
     170              : {
     171      3894880 :   insert_or_error_out (Identifier ("Self", impl.get_type ().get_locus ()),
     172      1947440 :                        impl.get_type (), Namespace::Types);
     173      1947440 : }
     174              : 
     175              : void
     176        13803 : TopLevel::visit (AST::TraitItemType &trait_item)
     177              : {
     178        41409 :   insert_or_error_out (trait_item.get_identifier ().as_string (), trait_item,
     179              :                        Namespace::Types);
     180              : 
     181        13803 :   DefaultResolver::visit (trait_item);
     182        13803 : }
     183              : 
     184              : template <typename PROC_MACRO>
     185              : static void
     186          672 : insert_macros (std::vector<PROC_MACRO> &macros, NameResolutionContext &ctx)
     187              : {
     188          672 :   for (auto &macro : macros)
     189              :     {
     190            0 :       auto res = ctx.macros.insert (macro.get_name (), macro.get_node_id ());
     191              : 
     192            0 :       if (!res && res.error ().existing != macro.get_node_id ())
     193              :         {
     194            0 :           rust_error_at (UNKNOWN_LOCATION, ErrorCode::E0428,
     195              :                          "macro %qs defined multiple times",
     196            0 :                          macro.get_name ().c_str ());
     197              :         }
     198              :     }
     199          672 : }
     200              : 
     201              : void
     202          227 : TopLevel::visit_extern_crate (AST::ExternCrate &extern_crate, AST::Crate &crate,
     203              :                               CrateNum num)
     204              : {
     205          227 :   auto &mappings = Analysis::Mappings::get ();
     206              : 
     207          227 :   auto attribute_macros = mappings.crate.attributes.lookup (num);
     208              : 
     209          227 :   auto bang_macros = mappings.crate.bangs.lookup (num);
     210              : 
     211          227 :   auto derive_macros = mappings.crate.derives.lookup (num);
     212              : 
     213              :   // TODO: Find a way to keep this part clean without the double dispatch.
     214          227 :   if (derive_macros.has_value ())
     215              :     {
     216          224 :       insert_macros (derive_macros.value (), ctx);
     217          224 :       for (auto &macro : derive_macros.value ())
     218            0 :         mappings.pmacro.definitions.derives.insert (macro.get_node_id (),
     219              :                                                     macro);
     220              :     }
     221          227 :   if (attribute_macros.has_value ())
     222              :     {
     223          224 :       insert_macros (attribute_macros.value (), ctx);
     224          224 :       for (auto &macro : attribute_macros.value ())
     225            0 :         mappings.pmacro.definitions.attributes.insert (macro.get_node_id (),
     226              :                                                        macro);
     227              :     }
     228          227 :   if (bang_macros.has_value ())
     229              :     {
     230          224 :       insert_macros (bang_macros.value (), ctx);
     231          224 :       for (auto &macro : bang_macros.value ())
     232            0 :         mappings.pmacro.definitions.bangs.insert (macro.get_node_id (), macro);
     233              :     }
     234              : 
     235              :   // We do *NOT* visit the crate because loaded crates are resolved
     236              :   // independently.
     237          227 : }
     238              : 
     239              : static bool
     240       212228 : is_macro_export (AST::MacroRulesDefinition &def)
     241              : {
     242       557022 :   for (const auto &attr : def.get_outer_attrs ())
     243       354952 :     if (attr.get_path ().as_string () == Values::Attributes::MACRO_EXPORT)
     244       212228 :       return true;
     245              : 
     246              :   return false;
     247              : }
     248              : 
     249              : void
     250       212228 : TopLevel::visit (AST::MacroRulesDefinition &macro)
     251              : {
     252              :   // we do not insert macros in the current rib as that needs to be done in the
     253              :   // textual scope of the Early pass. we only insert them in the root of the
     254              :   // crate if they are marked with #[macro_export]. The execption to this is
     255              :   // macros 2.0, which get resolved and inserted like regular items.
     256              : 
     257       212228 :   if (is_macro_export (macro))
     258              :     {
     259        30474 :       auto res = ctx.macros.insert_at_root (macro.get_rule_name (),
     260        10158 :                                             macro.get_node_id ());
     261        10158 :       if (!res && res.error ().existing != macro.get_node_id ())
     262              :         {
     263              :           // TODO: Factor this
     264            0 :           rich_location rich_loc (line_table, macro.get_locus ());
     265            0 :           rich_loc.add_range (node_locations[res.error ().existing]);
     266              : 
     267            0 :           rust_error_at (rich_loc, ErrorCode::E0428,
     268              :                          "macro %qs defined multiple times",
     269            0 :                          macro.get_rule_name ().as_string ().c_str ());
     270            0 :         }
     271        10158 :     }
     272              : 
     273       212228 :   if (macro.get_kind () == AST::MacroRulesDefinition::MacroKind::DeclMacro)
     274         5670 :     insert_or_error_out (macro.get_rule_name (), macro, Namespace::Macros);
     275              : 
     276       212228 :   auto &mappings = Analysis::Mappings::get ();
     277       212228 :   if (mappings.lookup_macro_def (macro.get_node_id ()))
     278              :     return;
     279              : 
     280         6957 :   mappings.insert_macro_def (&macro);
     281              : }
     282              : 
     283              : void
     284      3488500 : TopLevel::visit (AST::Function &function)
     285              : {
     286      3488500 :   insert_or_error_out (function.get_function_name (), function,
     287              :                        Namespace::Values);
     288              : 
     289      3488500 :   Analysis::Mappings::get ().add_function_node (function.get_node_id ());
     290              : 
     291      3488500 :   DefaultResolver::visit (function);
     292      3488500 : }
     293              : 
     294              : void
     295        10362 : TopLevel::visit (AST::StaticItem &static_item)
     296              : {
     297        10362 :   insert_or_error_out (static_item.get_identifier (), static_item,
     298              :                        Namespace::Values);
     299              : 
     300        10362 :   DefaultResolver::visit (static_item);
     301        10362 : }
     302              : 
     303              : void
     304            3 : TopLevel::visit (AST::ExternalStaticItem &static_item)
     305              : {
     306            9 :   insert_or_error_out (static_item.get_identifier ().as_string (), static_item,
     307              :                        Namespace::Values);
     308              : 
     309            3 :   DefaultResolver::visit (static_item);
     310            3 : }
     311              : 
     312              : void
     313       171185 : TopLevel::visit (AST::StructStruct &struct_item)
     314              : {
     315       171185 :   DefaultResolver::visit (struct_item);
     316              : 
     317       171185 :   insert_or_error_out (struct_item.get_struct_name (), struct_item,
     318              :                        Namespace::Types);
     319              : 
     320              :   // Do we need to insert the constructor in the value namespace as well?
     321              : 
     322              :   // Do we need to do anything if the struct is a unit struct?
     323       171185 :   if (struct_item.is_unit_struct ())
     324       105316 :     insert_or_error_out (struct_item.get_struct_name (), struct_item,
     325              :                          Namespace::Values);
     326       171185 : }
     327              : 
     328              : void
     329      2157933 : TopLevel::visit (AST::TypeParam &type_param)
     330              : {
     331      2157933 :   insert_or_error_out (type_param.get_type_representation (), type_param,
     332              :                        Namespace::Types);
     333              : 
     334      2157933 :   DefaultResolver::visit (type_param);
     335      2157933 : }
     336              : 
     337              : void
     338        21997 : TopLevel::visit (AST::ConstGenericParam &const_param)
     339              : {
     340        21997 :   insert_or_error_out (const_param.get_name (), const_param, Namespace::Values);
     341              : 
     342        21997 :   DefaultResolver::visit (const_param);
     343        21997 : }
     344              : 
     345              : void
     346        32214 : TopLevel::visit (AST::TupleStruct &tuple_struct)
     347              : {
     348        32214 :   insert_or_error_out (tuple_struct.get_struct_name (), tuple_struct,
     349              :                        Namespace::Types);
     350              : 
     351        32214 :   insert_or_error_out (tuple_struct.get_struct_name (), tuple_struct,
     352              :                        Namespace::Values);
     353              : 
     354        32214 :   DefaultResolver::visit (tuple_struct);
     355        32214 : }
     356              : 
     357              : void
     358        18800 : TopLevel::visit (AST::EnumItem &variant)
     359              : {
     360        18800 :   insert_enum_variant_or_error_out (variant.get_identifier (), variant, true);
     361              : 
     362        18800 :   DefaultResolver::visit (variant);
     363        18800 : }
     364              : 
     365              : void
     366         9767 : TopLevel::visit (AST::EnumItemTuple &variant)
     367              : {
     368         9767 :   insert_enum_variant_or_error_out (variant.get_identifier (), variant, true);
     369              : 
     370         9767 :   DefaultResolver::visit (variant);
     371         9767 : }
     372              : 
     373              : void
     374          337 : TopLevel::visit (AST::EnumItemStruct &variant)
     375              : {
     376          337 :   insert_enum_variant_or_error_out (variant.get_identifier (), variant, false);
     377              : 
     378          337 :   DefaultResolver::visit (variant);
     379          337 : }
     380              : 
     381              : void
     382         1880 : TopLevel::visit (AST::EnumItemDiscriminant &variant)
     383              : {
     384         1880 :   insert_enum_variant_or_error_out (variant.get_identifier (), variant, true);
     385              : 
     386         1880 :   DefaultResolver::visit (variant);
     387         1880 : }
     388              : 
     389              : void
     390        10979 : TopLevel::visit (AST::Enum &enum_item)
     391              : {
     392        10979 :   insert_or_error_out (enum_item.get_identifier (), enum_item,
     393              :                        Namespace::Types);
     394              : 
     395        10979 :   DefaultResolver::visit (enum_item);
     396              : 
     397              :   // Since enums can be containers for imports, we need to insert them like we
     398              :   // do for modules
     399        21958 :   if (Analysis::Mappings::get ().lookup_glob_container (
     400        10979 :         enum_item.get_node_id ())
     401        10979 :       == tl::nullopt)
     402          921 :     Analysis::Mappings::get ().insert_glob_container (enum_item.get_node_id (),
     403              :                                                       &enum_item);
     404        10979 : }
     405              : 
     406              : void
     407          926 : TopLevel::visit (AST::Union &union_item)
     408              : {
     409          926 :   insert_or_error_out (union_item.get_identifier (), union_item,
     410              :                        Namespace::Types);
     411              : 
     412          926 :   DefaultResolver::visit (union_item);
     413          926 : }
     414              : 
     415              : void
     416       203031 : TopLevel::visit (AST::ConstantItem &const_item)
     417              : {
     418       203031 :   if (const_item.get_identifier ().as_string () != Values::Keywords::UNDERSCORE)
     419       203010 :     insert_or_error_out (const_item.get_identifier (), const_item,
     420              :                          Namespace::Values);
     421              : 
     422       203031 :   DefaultResolver::visit (const_item);
     423       203031 : }
     424              : 
     425              : void
     426       717395 : TopLevel::visit (AST::TypeAlias &type_item)
     427              : {
     428       717395 :   insert_or_error_out (type_item.get_new_type_name (), type_item,
     429              :                        Namespace::Types);
     430              : 
     431       717395 :   DefaultResolver::visit (type_item);
     432       717395 : }
     433              : 
     434              : void
     435            6 : TopLevel::visit (AST::TraitAlias &trait_item)
     436              : {
     437            6 :   insert_or_error_out (trait_item.get_alias_name (), trait_item,
     438              :                        Namespace::Types);
     439              : 
     440            6 :   DefaultResolver::visit (trait_item);
     441            6 : }
     442              : 
     443              : void
     444          300 : TopLevel::visit (AST::ExternalTypeItem &type_item)
     445              : {
     446          300 :   insert_or_error_out (type_item.get_identifier (), type_item,
     447              :                        Namespace::Types);
     448              : 
     449          300 :   DefaultResolver::visit (type_item);
     450          300 : }
     451              : 
     452              : static void flatten_rebind (
     453              :   const AST::UseTreeRebind &glob,
     454              :   std::vector<std::pair<AST::SimplePath, AST::UseTreeRebind>> &rebind_paths);
     455              : 
     456              : static void flatten_list (
     457              :   const AST::UseTreeList &glob, std::vector<AST::SimplePath> &paths,
     458              :   std::vector<AST::SimplePath> &glob_paths,
     459              :   std::vector<std::pair<AST::SimplePath, AST::UseTreeRebind>> &rebind_paths,
     460              :   NameResolutionContext &ctx);
     461              : static void flatten_glob (const AST::UseTreeGlob &glob,
     462              :                           std::vector<AST::SimplePath> &glob_paths,
     463              :                           NameResolutionContext &ctx);
     464              : 
     465              : static void
     466       408642 : flatten (
     467              :   const AST::UseTree *tree, std::vector<AST::SimplePath> &paths,
     468              :   std::vector<AST::SimplePath> &glob_paths,
     469              :   std::vector<std::pair<AST::SimplePath, AST::UseTreeRebind>> &rebind_paths,
     470              :   NameResolutionContext &ctx)
     471              : {
     472       408642 :   switch (tree->get_kind ())
     473              :     {
     474       310605 :     case AST::UseTree::Rebind:
     475       310605 :       {
     476       310605 :         auto rebind = static_cast<const AST::UseTreeRebind *> (tree);
     477       310605 :         flatten_rebind (*rebind, rebind_paths);
     478       310605 :         break;
     479              :       }
     480        66603 :     case AST::UseTree::List:
     481        66603 :       {
     482        66603 :         auto list = static_cast<const AST::UseTreeList *> (tree);
     483        66603 :         flatten_list (*list, paths, glob_paths, rebind_paths, ctx);
     484        66603 :         break;
     485              :       }
     486        31434 :     case AST::UseTree::Glob:
     487        31434 :       {
     488        31434 :         auto glob = static_cast<const AST::UseTreeGlob *> (tree);
     489        31434 :         flatten_glob (*glob, glob_paths, ctx);
     490        31434 :         break;
     491              :       }
     492              :     }
     493       408642 : }
     494              : 
     495              : static void
     496       310605 : flatten_rebind (
     497              :   const AST::UseTreeRebind &rebind,
     498              :   std::vector<std::pair<AST::SimplePath, AST::UseTreeRebind>> &rebind_paths)
     499              : {
     500       310605 :   rebind_paths.emplace_back (rebind.get_path (), rebind);
     501       310605 : }
     502              : 
     503              : /** Prefix a list of subpath
     504              :  * @param prefix A prefix for all subpath
     505              :  * @param subs List of subpath to prefix
     506              :  * @param size List where results should be stored
     507              :  */
     508              : static void
     509       417100 : prefix_subpaths (AST::SimplePath prefix, std::vector<AST::SimplePath> subs,
     510              :                  std::vector<AST::SimplePath> &results)
     511              : {
     512       441061 :   for (auto &sub : subs)
     513              :     {
     514        23961 :       auto new_path = prefix;
     515        23961 :       std::copy (sub.get_segments ().begin (), sub.get_segments ().end (),
     516              :                  std::back_inserter (new_path.get_segments ()));
     517        23961 :       results.emplace_back (new_path);
     518        23961 :     }
     519       417100 : }
     520              : 
     521              : static void
     522       208550 : prefix_rebinds (
     523              :   AST::SimplePath prefix,
     524              :   std::vector<std::pair<AST::SimplePath, AST::UseTreeRebind>> subs,
     525              :   std::vector<std::pair<AST::SimplePath, AST::UseTreeRebind>> &results)
     526              : {
     527       403795 :   for (auto &sub : subs)
     528              :     {
     529       195245 :       auto new_path = prefix;
     530       195245 :       std::copy (sub.first.get_segments ().begin (),
     531       195245 :                  sub.first.get_segments ().end (),
     532              :                  std::back_inserter (new_path.get_segments ()));
     533       390490 :       results.emplace_back (std::make_pair (new_path, sub.second));
     534       195245 :     }
     535       208550 : }
     536              : 
     537              : static void
     538        66603 : flatten_list (
     539              :   const AST::UseTreeList &list, std::vector<AST::SimplePath> &paths,
     540              :   std::vector<AST::SimplePath> &glob_paths,
     541              :   std::vector<std::pair<AST::SimplePath, AST::UseTreeRebind>> &rebind_paths,
     542              :   NameResolutionContext &ctx)
     543              : {
     544        66603 :   auto prefix = AST::SimplePath::create_empty ();
     545        66603 :   if (list.has_path ())
     546        66599 :     prefix = list.get_path ();
     547              : 
     548       275153 :   for (const auto &tree : list.get_trees ())
     549              :     {
     550       208550 :       auto sub_paths = std::vector<AST::SimplePath> ();
     551       208550 :       auto sub_globs = std::vector<AST::SimplePath> ();
     552       208550 :       auto sub_rebinds
     553              :         = std::vector<std::pair<AST::SimplePath, AST::UseTreeRebind>> ();
     554       208550 :       flatten (tree.get (), sub_paths, sub_globs, sub_rebinds, ctx);
     555              : 
     556       208550 :       prefix_subpaths (prefix, sub_paths, paths);
     557       208550 :       prefix_subpaths (prefix, sub_globs, glob_paths);
     558       208550 :       prefix_rebinds (prefix, sub_rebinds, rebind_paths);
     559       208550 :     }
     560        66603 : }
     561              : 
     562              : static void
     563        31434 : flatten_glob (const AST::UseTreeGlob &glob, std::vector<AST::SimplePath> &paths,
     564              :               NameResolutionContext &ctx)
     565              : {
     566        31434 :   if (glob.has_path ())
     567        31417 :     paths.emplace_back (glob.get_path ());
     568              :   else
     569           17 :     paths.emplace_back (AST::SimplePath (
     570           17 :       {}, glob.get_glob_type () == AST::UseTreeGlob::PathType::GLOBAL,
     571           34 :       glob.get_locus ()));
     572        31434 : }
     573              : 
     574              : static bool
     575        31434 : has_prelude_import (const std::vector<AST::Attribute> &attributes)
     576              : {
     577        32609 :   for (const auto &attr : attributes)
     578         1466 :     if (attr.get_path ().as_string () == "prelude_import")
     579        31434 :       return true;
     580              : 
     581              :   return false;
     582              : }
     583              : 
     584              : void
     585       200092 : TopLevel::visit (AST::UseDeclaration &use)
     586              : {
     587       200092 :   auto paths = std::vector<AST::SimplePath> ();
     588       200092 :   auto glob_path = std::vector<AST::SimplePath> ();
     589       200092 :   auto rebind_path
     590              :     = std::vector<std::pair<AST::SimplePath, AST::UseTreeRebind>> ();
     591              : 
     592       200092 :   auto &values_rib = ctx.values.peek ();
     593       200092 :   auto &types_rib = ctx.types.peek ();
     594       200092 :   auto &macros_rib = ctx.macros.peek ();
     595              : 
     596              :   // FIXME: How do we handle `use foo::{self}` imports? Some beforehand cleanup?
     597              :   // How do we handle module imports in general? Should they get added to all
     598              :   // namespaces?
     599              : 
     600       200092 :   const auto &tree = use.get_tree ();
     601       200092 :   flatten (tree.get (), paths, glob_path, rebind_path, this->ctx);
     602              : 
     603       200092 :   auto imports = std::vector<ImportKind> ();
     604              : 
     605       200092 :   for (auto &&path : paths)
     606            0 :     imports.emplace_back (
     607            0 :       ImportKind::Simple (std::move (path), values_rib, types_rib, macros_rib));
     608              : 
     609       231526 :   for (auto &&glob : glob_path)
     610        62868 :     imports.emplace_back (
     611        31434 :       ImportKind::Glob (std::move (glob), values_rib, types_rib, macros_rib,
     612        31434 :                         has_prelude_import (use.get_outer_attrs ())));
     613              : 
     614       510697 :   for (auto &&rebind : rebind_path)
     615       310605 :     imports.emplace_back (
     616       621210 :       ImportKind::Rebind (std::move (rebind.first), std::move (rebind.second),
     617              :                           values_rib, types_rib, macros_rib));
     618              : 
     619       200092 :   imports_to_resolve.insert ({use.get_node_id (), std::move (imports)});
     620       200092 : }
     621              : 
     622              : } // namespace Resolver2_0
     623              : } // namespace Rust
        

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