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-12-05 16:10:02 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        17651 : TopLevel::TopLevel (NameResolutionContext &resolver)
      30        17651 :   : DefaultResolver (resolver), dirty (false)
      31        17651 : {}
      32              : 
      33              : template <typename T>
      34              : void
      35        43600 : TopLevel::insert_enum_variant_or_error_out (const Identifier &identifier,
      36              :                                             const T &node, bool is_also_value)
      37              : {
      38        43600 :   insert_enum_variant_or_error_out (identifier, node.get_locus (),
      39        43600 :                                     node.get_node_id (), is_also_value);
      40        43600 : }
      41              : 
      42              : void
      43     13586966 : 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     13586966 :   if (result)
      48       465795 :     dirty = true;
      49     13121171 :   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     13586966 : }
      68              : void
      69        43600 : 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        43600 :   node_locations.emplace (node_id, locus);
      76              : 
      77        43600 :   auto result = ctx.insert_variant (identifier, node_id, is_also_value);
      78        84640 :   check_multiple_insertion_error (result, identifier, locus, node_id);
      79        43600 : }
      80              : 
      81              : template <typename T>
      82              : void
      83     13462700 : TopLevel::insert_or_error_out (const Identifier &identifier, const T &node,
      84              :                                Namespace ns)
      85              : {
      86     13462700 :   insert_or_error_out (identifier, node.get_locus (), node.get_node_id (), ns);
      87     13462700 : }
      88              : 
      89              : void
      90     13543366 : 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     13543366 :   node_locations.emplace (node_id, locus);
      96              : 
      97     13543366 :   auto result = ctx.insert (identifier, node_id, ns);
      98     26623497 :   check_multiple_insertion_error (result, identifier, locus, node_id);
      99     13543366 : }
     100              : 
     101              : void
     102        17651 : 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        17651 :   visit (crate);
     110              : 
     111        17651 :   if (Analysis::Mappings::get ().lookup_glob_container (crate.get_node_id ())
     112        17651 :       == tl::nullopt)
     113         5338 :     Analysis::Mappings::get ().insert_glob_container (crate.get_node_id (),
     114              :                                                       &crate);
     115        17651 : }
     116              : 
     117              : void
     118       107815 : TopLevel::visit (AST::Module &module)
     119              : {
     120       107815 :   if (Analysis::Mappings::get ().lookup_glob_container (module.get_node_id ())
     121       107815 :       == tl::nullopt)
     122         4471 :     Analysis::Mappings::get ().insert_glob_container (module.get_node_id (),
     123              :                                                       &module);
     124              : 
     125       107815 :   insert_or_error_out (module.get_name (), module, Namespace::Types);
     126              : 
     127       107815 :   Analysis::Mappings::get ().insert_module_id (module.get_node_id ());
     128              : 
     129       107815 :   DefaultResolver::visit (module);
     130       107815 : }
     131              : 
     132              : void
     133        72866 : TopLevel::visit (AST::Trait &trait)
     134              : {
     135        72866 :   insert_or_error_out (trait.get_identifier (), trait, Namespace::Types);
     136              : 
     137        72866 :   DefaultResolver::visit (trait);
     138        72866 : }
     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      2904972 : TopLevel::maybe_insert_big_self (AST::Impl &impl)
     170              : {
     171      5809944 :   insert_or_error_out (Identifier ("Self", impl.get_type ().get_locus ()),
     172      2904972 :                        impl.get_type (), Namespace::Types);
     173      2904972 : }
     174              : 
     175              : void
     176        19131 : TopLevel::visit (AST::TraitItemType &trait_item)
     177              : {
     178        57393 :   insert_or_error_out (trait_item.get_identifier ().as_string (), trait_item,
     179              :                        Namespace::Types);
     180              : 
     181        19131 :   DefaultResolver::visit (trait_item);
     182        19131 : }
     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       315452 : is_macro_export (AST::MacroRulesDefinition &def)
     241              : {
     242       832182 :   for (const auto &attr : def.get_outer_attrs ())
     243       531928 :     if (attr.get_path ().as_string () == Values::Attributes::MACRO_EXPORT)
     244       315452 :       return true;
     245              : 
     246              :   return false;
     247              : }
     248              : 
     249              : void
     250       315452 : 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       315452 :   if (is_macro_export (macro))
     258              :     {
     259        45594 :       auto res = ctx.macros.insert_at_root (macro.get_rule_name (),
     260        15198 :                                             macro.get_node_id ());
     261        15198 :       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        15198 :     }
     272              : 
     273       315452 :   if (macro.get_kind () == AST::MacroRulesDefinition::MacroKind::DeclMacro)
     274         8406 :     insert_or_error_out (macro.get_rule_name (), macro, Namespace::Macros);
     275              : 
     276       315452 :   auto &mappings = Analysis::Mappings::get ();
     277       315452 :   if (mappings.lookup_macro_def (macro.get_node_id ()))
     278              :     return;
     279              : 
     280         9825 :   mappings.insert_macro_def (&macro);
     281              : }
     282              : 
     283              : void
     284      5189688 : TopLevel::visit (AST::Function &function)
     285              : {
     286      5189688 :   insert_or_error_out (function.get_function_name (), function,
     287              :                        Namespace::Values);
     288              : 
     289      5189688 :   Analysis::Mappings::get ().add_function_node (function.get_node_id ());
     290              : 
     291      5189688 :   DefaultResolver::visit (function);
     292      5189688 : }
     293              : 
     294              : void
     295        15402 : TopLevel::visit (AST::StaticItem &static_item)
     296              : {
     297        15402 :   insert_or_error_out (static_item.get_identifier (), static_item,
     298              :                        Namespace::Values);
     299              : 
     300        15402 :   DefaultResolver::visit (static_item);
     301        15402 : }
     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       253245 : TopLevel::visit (AST::StructStruct &struct_item)
     314              : {
     315       253245 :   DefaultResolver::visit (struct_item);
     316              : 
     317       253245 :   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       253245 :   if (struct_item.is_unit_struct ())
     324       156752 :     insert_or_error_out (struct_item.get_struct_name (), struct_item,
     325              :                          Namespace::Values);
     326       253245 : }
     327              : 
     328              : void
     329      3214805 : TopLevel::visit (AST::TypeParam &type_param)
     330              : {
     331      3214805 :   insert_or_error_out (type_param.get_type_representation (), type_param,
     332              :                        Namespace::Types);
     333              : 
     334      3214805 :   DefaultResolver::visit (type_param);
     335      3214805 : }
     336              : 
     337              : void
     338        32777 : TopLevel::visit (AST::ConstGenericParam &const_param)
     339              : {
     340        32777 :   insert_or_error_out (const_param.get_name (), const_param, Namespace::Values);
     341              : 
     342        32777 :   DefaultResolver::visit (const_param);
     343        32777 : }
     344              : 
     345              : void
     346        46554 : TopLevel::visit (AST::TupleStruct &tuple_struct)
     347              : {
     348        46554 :   insert_or_error_out (tuple_struct.get_struct_name (), tuple_struct,
     349              :                        Namespace::Types);
     350              : 
     351        46554 :   insert_or_error_out (tuple_struct.get_struct_name (), tuple_struct,
     352              :                        Namespace::Values);
     353              : 
     354        46554 :   DefaultResolver::visit (tuple_struct);
     355        46554 : }
     356              : 
     357              : void
     358        27296 : TopLevel::visit (AST::EnumItem &variant)
     359              : {
     360        27296 :   insert_enum_variant_or_error_out (variant.get_identifier (), variant, true);
     361              : 
     362        27296 :   DefaultResolver::visit (variant);
     363        27296 : }
     364              : 
     365              : void
     366        13655 : TopLevel::visit (AST::EnumItemTuple &variant)
     367              : {
     368        13655 :   insert_enum_variant_or_error_out (variant.get_identifier (), variant, true);
     369              : 
     370        13655 :   DefaultResolver::visit (variant);
     371        13655 : }
     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         2312 : TopLevel::visit (AST::EnumItemDiscriminant &variant)
     383              : {
     384         2312 :   insert_enum_variant_or_error_out (variant.get_identifier (), variant, true);
     385              : 
     386         2312 :   DefaultResolver::visit (variant);
     387         2312 : }
     388              : 
     389              : void
     390        15299 : TopLevel::visit (AST::Enum &enum_item)
     391              : {
     392        15299 :   insert_or_error_out (enum_item.get_identifier (), enum_item,
     393              :                        Namespace::Types);
     394              : 
     395        15299 :   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        30598 :   if (Analysis::Mappings::get ().lookup_glob_container (
     400        15299 :         enum_item.get_node_id ())
     401        15299 :       == tl::nullopt)
     402         1041 :     Analysis::Mappings::get ().insert_glob_container (enum_item.get_node_id (),
     403              :                                                       &enum_item);
     404        15299 : }
     405              : 
     406              : void
     407         1214 : TopLevel::visit (AST::Union &union_item)
     408              : {
     409         1214 :   insert_or_error_out (union_item.get_identifier (), union_item,
     410              :                        Namespace::Types);
     411              : 
     412         1214 :   DefaultResolver::visit (union_item);
     413         1214 : }
     414              : 
     415              : void
     416       303535 : TopLevel::visit (AST::ConstantItem &const_item)
     417              : {
     418       303535 :   if (const_item.get_identifier ().as_string () != Values::Keywords::UNDERSCORE)
     419       303514 :     insert_or_error_out (const_item.get_identifier (), const_item,
     420              :                          Namespace::Values);
     421              : 
     422       303535 :   DefaultResolver::visit (const_item);
     423       303535 : }
     424              : 
     425              : void
     426      1073023 : TopLevel::visit (AST::TypeAlias &type_item)
     427              : {
     428      1073023 :   insert_or_error_out (type_item.get_new_type_name (), type_item,
     429              :                        Namespace::Types);
     430              : 
     431      1073023 :   DefaultResolver::visit (type_item);
     432      1073023 : }
     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          444 : TopLevel::visit (AST::ExternalTypeItem &type_item)
     445              : {
     446          444 :   insert_or_error_out (type_item.get_identifier (), type_item,
     447              :                        Namespace::Types);
     448              : 
     449          444 :   DefaultResolver::visit (type_item);
     450          444 : }
     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       607706 : 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       607706 :   switch (tree->get_kind ())
     473              :     {
     474       461573 :     case AST::UseTree::Rebind:
     475       461573 :       {
     476       461573 :         auto rebind = static_cast<const AST::UseTreeRebind *> (tree);
     477       461573 :         flatten_rebind (*rebind, rebind_paths);
     478       461573 :         break;
     479              :       }
     480        99147 :     case AST::UseTree::List:
     481        99147 :       {
     482        99147 :         auto list = static_cast<const AST::UseTreeList *> (tree);
     483        99147 :         flatten_list (*list, paths, glob_paths, rebind_paths, ctx);
     484        99147 :         break;
     485              :       }
     486        46986 :     case AST::UseTree::Glob:
     487        46986 :       {
     488        46986 :         auto glob = static_cast<const AST::UseTreeGlob *> (tree);
     489        46986 :         flatten_glob (*glob, glob_paths, ctx);
     490        46986 :         break;
     491              :       }
     492              :     }
     493       607706 : }
     494              : 
     495              : static void
     496       461573 : flatten_rebind (
     497              :   const AST::UseTreeRebind &rebind,
     498              :   std::vector<std::pair<AST::SimplePath, AST::UseTreeRebind>> &rebind_paths)
     499              : {
     500       461573 :   rebind_paths.emplace_back (rebind.get_path (), rebind);
     501       461573 : }
     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       620716 : prefix_subpaths (AST::SimplePath prefix, std::vector<AST::SimplePath> subs,
     510              :                  std::vector<AST::SimplePath> &results)
     511              : {
     512       656629 :   for (auto &sub : subs)
     513              :     {
     514        35913 :       auto new_path = prefix;
     515        35913 :       std::copy (sub.get_segments ().begin (), sub.get_segments ().end (),
     516              :                  std::back_inserter (new_path.get_segments ()));
     517        35913 :       results.emplace_back (new_path);
     518        35913 :     }
     519       620716 : }
     520              : 
     521              : static void
     522       310358 : 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       600787 :   for (auto &sub : subs)
     528              :     {
     529       290429 :       auto new_path = prefix;
     530       290429 :       std::copy (sub.first.get_segments ().begin (),
     531       290429 :                  sub.first.get_segments ().end (),
     532              :                  std::back_inserter (new_path.get_segments ()));
     533       580858 :       results.emplace_back (std::make_pair (new_path, sub.second));
     534       290429 :     }
     535       310358 : }
     536              : 
     537              : static void
     538        99147 : 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        99147 :   auto prefix = AST::SimplePath::create_empty ();
     545        99147 :   if (list.has_path ())
     546        99143 :     prefix = list.get_path ();
     547              : 
     548       409505 :   for (const auto &tree : list.get_trees ())
     549              :     {
     550       310358 :       auto sub_paths = std::vector<AST::SimplePath> ();
     551       310358 :       auto sub_globs = std::vector<AST::SimplePath> ();
     552       310358 :       auto sub_rebinds
     553              :         = std::vector<std::pair<AST::SimplePath, AST::UseTreeRebind>> ();
     554       310358 :       flatten (tree.get (), sub_paths, sub_globs, sub_rebinds, ctx);
     555              : 
     556       310358 :       prefix_subpaths (prefix, sub_paths, paths);
     557       310358 :       prefix_subpaths (prefix, sub_globs, glob_paths);
     558       310358 :       prefix_rebinds (prefix, sub_rebinds, rebind_paths);
     559       310358 :     }
     560        99147 : }
     561              : 
     562              : static void
     563        46986 : flatten_glob (const AST::UseTreeGlob &glob, std::vector<AST::SimplePath> &paths,
     564              :               NameResolutionContext &ctx)
     565              : {
     566        46986 :   if (glob.has_path ())
     567        46969 :     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        46986 : }
     573              : 
     574              : static bool
     575        46986 : has_prelude_import (const std::vector<AST::Attribute> &attributes)
     576              : {
     577        48737 :   for (const auto &attr : attributes)
     578         2186 :     if (attr.get_path ().as_string () == "prelude_import")
     579        46986 :       return true;
     580              : 
     581              :   return false;
     582              : }
     583              : 
     584              : void
     585       297348 : TopLevel::visit (AST::UseDeclaration &use)
     586              : {
     587       297348 :   auto paths = std::vector<AST::SimplePath> ();
     588       297348 :   auto glob_path = std::vector<AST::SimplePath> ();
     589       297348 :   auto rebind_path
     590              :     = std::vector<std::pair<AST::SimplePath, AST::UseTreeRebind>> ();
     591              : 
     592       297348 :   auto &values_rib = ctx.values.peek ();
     593       297348 :   auto &types_rib = ctx.types.peek ();
     594       297348 :   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       297348 :   const auto &tree = use.get_tree ();
     601       297348 :   flatten (tree.get (), paths, glob_path, rebind_path, this->ctx);
     602              : 
     603       297348 :   auto imports = std::vector<ImportKind> ();
     604              : 
     605       297348 :   for (auto &&path : paths)
     606            0 :     imports.emplace_back (
     607            0 :       ImportKind::Simple (std::move (path), values_rib, types_rib, macros_rib));
     608              : 
     609       344334 :   for (auto &&glob : glob_path)
     610        93972 :     imports.emplace_back (
     611        46986 :       ImportKind::Glob (std::move (glob), values_rib, types_rib, macros_rib,
     612        46986 :                         has_prelude_import (use.get_outer_attrs ())));
     613              : 
     614       758921 :   for (auto &&rebind : rebind_path)
     615       461573 :     imports.emplace_back (
     616       923146 :       ImportKind::Rebind (std::move (rebind.first), std::move (rebind.second),
     617              :                           values_rib, types_rib, macros_rib));
     618              : 
     619       297348 :   imports_to_resolve.insert ({use.get_node_id (), std::move (imports)});
     620       297348 : }
     621              : 
     622              : } // namespace Resolver2_0
     623              : } // namespace Rust
        

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