LCOV - code coverage report
Current view: top level - gcc/rust/resolve - rust-forever-stack.h (source / functions) Coverage Total Hit
Test: gcc.info Lines: 97.5 % 81 79
Test Date: 2026-08-22 16:33:35 Functions: 93.8 % 16 15
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              : #ifndef RUST_FOREVER_STACK_H
      20              : #define RUST_FOREVER_STACK_H
      21              : 
      22              : #include "rust-system.h"
      23              : #include "rust-rib.h"
      24              : #include "rust-ast.h"
      25              : #include "rust-path.h"
      26              : #include "optional.h"
      27              : #include "expected.h"
      28              : #include "rust-name-resolution.h"
      29              : #include "rust-unwrap-segment.h"
      30              : 
      31              : namespace Rust {
      32              : namespace Resolver2_0 {
      33              : 
      34              : /**
      35              : 
      36              : Let's look at our stack for resolving and traversing the following Rust code:
      37              : 
      38              : ```rust
      39              : mod foo {
      40              :     mod bar {
      41              :         fn outer() {
      42              :             fn inner() {}
      43              :         }
      44              : 
      45              :         fn another() {}
      46              :     }
      47              : }
      48              : ```
      49              : 
      50              : We start by creating the stack, which contains only one rib - the crate's. We
      51              : won't look in details on how different namespaces end up with different stacks,
      52              : and will only consider the "value" namespace for this example. Modules do not
      53              : get added to the value namespace, but functions do:
      54              : 
      55              : ```rust
      56              : let _ = foo;   // foo is a module, invalid Rust code
      57              : let _ = outer; // outer is a function, ok!
      58              : ```
      59              : 
      60              : So passing each module will create a new Rib, but not add that module's node to
      61              : the Rib.
      62              : 
      63              : The current cursor of the stack will be denoted with `-->`: an arrow pointing to
      64              : the current rib.
      65              : 
      66              : When we start the `TopLevel` pass on the crate we are compiling, we only see the
      67              : top rib, which is empty at first:
      68              : 
      69              :       ┌───────────────┐
      70              :       │               │
      71              :   --> │               │
      72              :       │               │
      73              :       └───────────────┘
      74              : 
      75              : We pass through our first module, and emplace another Rib: Another "scope" is
      76              : created, and it impacts name resolution rules.
      77              : 
      78              :       ┌───────────────┐
      79              :       │               │
      80              :       │               │
      81              :       │               │
      82              :       └───────┬───────┘
      83              :               │
      84              :           foo │
      85              :               │
      86              :               ▼
      87              :       ┌───────────────┐
      88              :       │               │
      89              :   --> │               │
      90              :       │               │
      91              :       └───────────────┘
      92              : 
      93              : Notice that we have moved the cursor to the newly-created Rib, and that we have
      94              : added a path between the two ribs - this is a `Link`. A link contains
      95              : information such as the scope's NodeId, as well as an optional path - present
      96              : only when the scope is named. This allows us to easily fetch AST nodes based on
      97              : their canonical path, or build a canonical path from a NodeId. It also makes it
      98              : really easy to do complex path name resolution, such as `super::super::<item>`.
      99              : As mentioned earlier, modules are not present in the value namespace, so our new
     100              : rib is also empty. Let's pass through the second module:
     101              : 
     102              :       ┌───────────────┐
     103              :       │               │
     104              :       │               │
     105              :       │               │
     106              :       └───────┬───────┘
     107              :               │
     108              :           foo │
     109              :               │
     110              :               ▼
     111              :       ┌───────────────┐
     112              :       │               │
     113              :       │               │
     114              :       │               │
     115              :       └───────┬───────┘
     116              :               │
     117              :           bar │
     118              :               │
     119              :               ▼
     120              :       ┌───────────────┐
     121              :       │               │
     122              :   --> │               │
     123              :       │               │
     124              :       └───────────────┘
     125              : 
     126              : Once again, the new rib is empty, and we have a link with a path. We now go
     127              : through each item in the `bar` module and visit them. The first item is a
     128              : function, `outer` - upon being visited, it adds itself to the current rib.
     129              : 
     130              :       ┌───────────────┐
     131              :       │               │
     132              :       │               │
     133              :       │               │
     134              :       └───────┬───────┘
     135              :               │
     136              :           foo │
     137              :               │
     138              :               ▼
     139              :       ┌───────────────┐
     140              :       │               │
     141              :       │               │
     142              :       │               │
     143              :       └───────┬───────┘
     144              :               │
     145              :           bar │
     146              :               │
     147              :               ▼
     148              :       ┌───────────────┐
     149              :       │outer          │
     150              :   --> │               │
     151              :       │               │
     152              :       └───────────────┘
     153              : 
     154              : We now visit `outer`'s definition. This creates a new Rib, as functions can have
     155              : arguments, whose declaration only lives for the function's scope.
     156              : 
     157              :       ┌───────────────┐
     158              :       │               │
     159              :       │               │
     160              :       │               │
     161              :       └───────┬───────┘
     162              :               │
     163              :           foo │
     164              :               │
     165              :               ▼
     166              :       ┌───────────────┐
     167              :       │               │
     168              :       │               │
     169              :       │               │
     170              :       └───────┬───────┘
     171              :               │
     172              :           bar │
     173              :               │
     174              :               ▼
     175              :       ┌───────────────┐
     176              :       │outer          │
     177              :       │               │
     178              :       │               │
     179              :       └───────┬───────┘
     180              :               │
     181              :        <anon> │
     182              :               │
     183              :               ▼
     184              :       ┌───────────────┐
     185              :       │               │
     186              :   --> │               │
     187              :       │               │
     188              :       └───────────────┘
     189              : 
     190              : This rib is anonymous (the link to it does not have a path), because we cannot
     191              : refer to a function's inner items from the outside:
     192              : 
     193              : ```rust
     194              : pub mod a {
     195              :     pub fn foo() {}
     196              : }
     197              : 
     198              : pub fn b() {
     199              :     pub fn foo() {}
     200              : }
     201              : 
     202              : fn main() {
     203              :     a::foo(); // ok
     204              :     b::foo(); // ko!
     205              : }
     206              : ```
     207              : 
     208              : We visit the function's block, which contain a single declaration, a function
     209              : named `inner`. It adds itself to the current rib.
     210              : 
     211              :       ┌───────────────┐
     212              :       │               │
     213              :       │               │
     214              :       │               │
     215              :       └───────┬───────┘
     216              :               │
     217              :           foo │
     218              :               │
     219              :               ▼
     220              :       ┌───────────────┐
     221              :       │               │
     222              :       │               │
     223              :       │               │
     224              :       └───────┬───────┘
     225              :               │
     226              :           bar │
     227              :               │
     228              :               ▼
     229              :       ┌───────────────┐
     230              :       │outer          │
     231              :       │               │
     232              :       │               │
     233              :       └───────┬───────┘
     234              :               │
     235              :        <anon> │
     236              :               │
     237              :               ▼
     238              :       ┌───────────────┐
     239              :       │inner          │
     240              :   --> │               │
     241              :       │               │
     242              :       └───────────────┘
     243              : 
     244              : We visit `inner`, which yields a rib but no other declaration.
     245              : 
     246              :       ┌───────────────┐
     247              :       │               │
     248              :       │               │
     249              :       │               │
     250              :       └───────┬───────┘
     251              :               │
     252              :           foo │
     253              :               │
     254              :               ▼
     255              :       ┌───────────────┐
     256              :       │               │
     257              :       │               │
     258              :       │               │
     259              :       └───────┬───────┘
     260              :               │
     261              :           bar │
     262              :               │
     263              :               ▼
     264              :       ┌───────────────┐
     265              :       │outer          │
     266              :       │               │
     267              :       │               │
     268              :       └───────┬───────┘
     269              :               │
     270              :        <anon> │
     271              :               │
     272              :               ▼
     273              :       ┌───────────────┐
     274              :       │inner          │
     275              :       │               │
     276              :       │               │
     277              :       └───────────────┘
     278              :               │
     279              :        <anon> │
     280              :               │
     281              :               ▼
     282              :       ┌───────────────┐
     283              :       │               │
     284              :   --> │               │
     285              :       │               │
     286              :       └───────────────┘
     287              : 
     288              : We are now at the end of the `inner` function, and we want to pop the current
     289              : scope. Instead of deleting the current rib, we simply move the cursor backwards.
     290              : This allows us to keep track of the existing information and access it in later
     291              : name resolution passes. We then finish visiting `outer`, then go back to our
     292              : `bar` module. This is what our stack looks like after this. Note how the only
     293              : difference is the cursor's location.
     294              : 
     295              :       ┌───────────────┐
     296              :       │               │
     297              :       │               │
     298              :       │               │
     299              :       └───────┬───────┘
     300              :               │
     301              :           foo │
     302              :               │
     303              :               ▼
     304              :       ┌───────────────┐
     305              :       │               │
     306              :       │               │
     307              :       │               │
     308              :       └───────┬───────┘
     309              :               │
     310              :           bar │
     311              :               │
     312              :               ▼
     313              :       ┌───────────────┐
     314              :       │outer          │
     315              :   --> │               │
     316              :       │               │
     317              :       └───────┬───────┘
     318              :               │
     319              :        <anon> │
     320              :               │
     321              :               ▼
     322              :       ┌───────────────┐
     323              :       │inner          │
     324              :       │               │
     325              :       │               │
     326              :       └───────────────┘
     327              :               │
     328              :        <anon> │
     329              :               │
     330              :               ▼
     331              :       ┌───────────────┐
     332              :       │               │
     333              :       │               │
     334              :       │               │
     335              :       └───────────────┘
     336              : 
     337              : We then visit the remaining `bar` items, which are composed of the `another`
     338              : function. It adds itself to the current rib. This function contains no
     339              : declarations, but it still creates a Rib upon being visited. We then finish our
     340              : visit of `bar`, which marks the end of our visit of `foo`, which marks the end
     341              : of our `TopLevel` name resolution pass.
     342              : 
     343              :       ┌───────────────┐
     344              :       │               │
     345              :   --> │               │
     346              :       │               │
     347              :       └───────┬───────┘
     348              :               │
     349              :           foo │
     350              :               │
     351              :               ▼
     352              :       ┌───────────────┐
     353              :       │               │
     354              :       │               │
     355              :       │               │
     356              :       └───────┬───────┘
     357              :               │
     358              :           bar │
     359              :               │
     360              :               ▼
     361              :       ┌───────────────┐
     362              :       │outer          │
     363              :       │another        │
     364              :       │               │
     365              :       └───────┬──┬────┘
     366              :               │  │       <anon>
     367              :        <anon> │  └────────────────────┐
     368              :               │                       │
     369              :               ▼                       ▼
     370              :       ┌───────────────┐       ┌───────────────┐
     371              :       │inner          │       │               │
     372              :       │               │       │               │
     373              :       │               │       │               │
     374              :       └───────┬───────┘       └───────────────┘
     375              :               │
     376              :        <anon> │
     377              :               │
     378              :               ▼
     379              :       ┌───────────────┐
     380              :       │               │
     381              :       │               │
     382              :       │               │
     383              :       └───────────────┘
     384              : 
     385              : We now have a stack with a lot of ribs, prime for the `Early` and `Late` name
     386              : resolution passes. We will revisit the ribs we created in these passes, and we
     387              : won't need to allocate or create new ones: because they will still be present in
     388              : the stack, we will simply move our cursor to these ribs. In this case, there is
     389              : nothing to do, since there are no uses of our definitions, as the Rust code we
     390              : are name-resolving is not really interesting. You'll also note that our
     391              : `TopLevel` pass did not resolve a whole lot: all it did was create new ribs, and
     392              : empty ones at that. The `Early` pass will not go further, since our code does
     393              : not contain any imports, macro definitions or macro invocations. You can look at
     394              : this pass's documentation for more details on this resolution process.
     395              : 
     396              : **/
     397              : 
     398              : enum class ResolutionMode
     399              : {
     400              :   Normal,
     401              :   FromRoot,
     402              :   FromExtern, // extern prelude
     403              : };
     404              : 
     405       591672 : class ResolutionPath
     406              : {
     407              : public:
     408              :   template <typename T>
     409       295836 :   ResolutionPath (const std::vector<T> &segments_in, NodeId node_id)
     410       295836 :     : node_id (node_id)
     411              :   {
     412       295836 :     segments.clear ();
     413       295836 :     segments.reserve (segments_in.size ());
     414       667421 :     for (auto &outer_seg : segments_in)
     415              :       {
     416       371585 :         if (auto lang_item = unwrap_segment_get_lang_item (outer_seg))
     417              :           {
     418         1208 :             rust_assert (!lang_prefix.has_value ());
     419         1208 :             lang_prefix = std::make_pair (lang_item.value (),
     420         1208 :                                           unwrap_segment_node_id (outer_seg));
     421         1208 :             continue;
     422              :           }
     423              : 
     424       370377 :         auto &seg = unwrap_type_segment (outer_seg);
     425              : 
     426       370377 :         Segment new_seg;
     427       370377 :         new_seg.name = seg.as_string ();
     428       370377 :         new_seg.node_id = unwrap_segment_node_id (outer_seg);
     429       370377 :         new_seg.locus = seg.get_locus ();
     430       370377 :         segments.push_back (std::move (new_seg));
     431              :       }
     432       295836 :   }
     433              : 
     434       295836 :   ResolutionPath () : node_id (UNKNOWN_NODEID) {}
     435              : 
     436      1481508 :   struct Segment
     437              :   {
     438              :     std::string name;
     439              :     NodeId node_id;
     440              :     location_t locus;
     441              : 
     442       142506 :     bool is_super_path_seg () const { return name.compare ("super") == 0; }
     443       269613 :     bool is_crate_path_seg () const { return name.compare ("crate") == 0; }
     444       238125 :     bool is_lower_self_seg () const { return name.compare ("self") == 0; }
     445              :     bool is_big_self_seg () const { return name.compare ("Self") == 0; }
     446              :   };
     447              : 
     448       329166 :   tl::optional<std::pair<LangItem::Kind, NodeId>> get_lang_prefix () const
     449              :   {
     450       329166 :     return lang_prefix;
     451              :   }
     452              : 
     453       329166 :   const std::vector<Segment> &get_segments () const { return segments; }
     454              : 
     455              :   NodeId get_node_id () const { return node_id; }
     456              : 
     457       329166 :   std::string as_string () const
     458              :   {
     459       329166 :     std::string ret;
     460       329166 :     if (lang_prefix)
     461         1208 :       ret = "#[lang]::";
     462       784915 :     for (auto &seg : segments)
     463       911498 :       ret += "::" + seg.name;
     464       329166 :     return ret;
     465              :   }
     466              : 
     467              : private:
     468              :   tl::optional<std::pair<LangItem::Kind, NodeId>> lang_prefix;
     469              :   std::vector<Segment> segments;
     470              :   NodeId node_id;
     471              : };
     472              : 
     473              : /**
     474              :  * Error enum for finding leaf definitions in the resolved_nodes map
     475              :  */
     476              : enum class LookupFinalizeError
     477              : {
     478              :   // Impossible - we did not find any definition corresponding to a Usage.
     479              :   // This is an internal compiler error
     480              :   NoDefinition,
     481              :   // There was a loop in the map, such as an import resolving to another
     482              :   // import which eventually resolved to the original import. Report the
     483              :   // error and stop the pipeline
     484              :   Loop,
     485              : };
     486              : 
     487              : class ForeverStackBase
     488              : {
     489              : public:
     490              :   /**
     491              :    * A link between two Nodes in our trie data structure. This class represents
     492              :    * the edges of the graph
     493              :    */
     494     97407456 :   class Link
     495              :   {
     496              :   public:
     497     30745908 :     Link (NodeId id, tl::optional<Identifier> path) : id (id), path (path) {}
     498              : 
     499    170709873 :     bool compare (const Link &other) const { return id < other.id; }
     500              : 
     501              :     NodeId id;
     502              :     tl::optional<Identifier> path;
     503              :   };
     504              : 
     505              :   /* Link comparison class, which we use in a Node's `children` map */
     506              :   class LinkCmp
     507              :   {
     508              :   public:
     509    170709873 :     bool operator() (const Link &lhs, const Link &rhs) const
     510              :     {
     511    170709873 :       return lhs.compare (rhs);
     512              :     }
     513              :   };
     514              : 
     515              :   class Node
     516              :   {
     517              :   public:
     518         9738 :     Node (Rib::Kind rib_kind, NodeId id)
     519         9738 :       : rib_values (rib_kind), rib_types (rib_kind), rib_labels (rib_kind),
     520         9738 :         rib_macros (rib_kind), id (id)
     521         9738 :     {}
     522     30731301 :     Node (Rib::Kind rib_kind, NodeId id, Node &parent)
     523     30731301 :       : rib_values (rib_kind), rib_types (rib_kind), rib_labels (rib_kind),
     524     30731301 :         rib_macros (rib_kind), id (id), parent (parent)
     525     30731301 :     {}
     526              : 
     527     73896113 :     const Rib &rib (Namespace n) const
     528              :     {
     529     73819680 :       switch (n)
     530              :         {
     531     24881330 :         case Namespace::Values:
     532     24881330 :           return rib_values;
     533     17833654 :         case Namespace::Types:
     534     17833654 :           return rib_types;
     535     15397259 :         case Namespace::Labels:
     536     15397259 :           return rib_labels;
     537     15783870 :         case Namespace::Macros:
     538     15783870 :           return rib_macros;
     539            0 :         default:
     540            0 :           rust_unreachable ();
     541              :         }
     542              :     }
     543              : 
     544     73819680 :     Rib &rib (Namespace n)
     545              :     {
     546     73765864 :       return const_cast<Rib &> (const_cast<const Node *> (this)->rib (n));
     547              :     }
     548              : 
     549              :     inline bool is_root () const;
     550              :     inline bool is_prelude () const;
     551              :     inline bool is_leaf () const;
     552              : 
     553              :     inline void insert_child (Link link, Node child);
     554              : 
     555              :     // these are the "values" of the node - the data it keeps.
     556              :     Rib rib_values;
     557              :     Rib rib_types;
     558              :     Rib rib_labels;
     559              :     Rib rib_macros;
     560              : 
     561              :     std::map<Link, Node, LinkCmp> children; // all the other nodes it links to
     562              : 
     563              :     NodeId id; // The node id of the Node's scope
     564              : 
     565              :     tl::optional<Node &> parent; // `None` only if the node is a root
     566              :   };
     567              : 
     568        19476 :   ForeverStackBase (Node &root, Node &lang_prelude, Node &extern_prelude)
     569        19476 :     : root (root), lang_prelude (lang_prelude), extern_prelude (extern_prelude)
     570              :   {}
     571              : 
     572              :   /* The forever stack's actual nodes */
     573              :   Node &root;
     574              : 
     575              :   /*
     576              :    * A special prelude node used currently for resolving language builtins
     577              :    * It has the root node as a parent, and acts as a "special case" for name
     578              :    * resolution
     579              :    */
     580              :   Node &lang_prelude;
     581              : 
     582              :   /*
     583              :    * The extern prelude, used for resolving external crates
     584              :    */
     585              :   Node &extern_prelude;
     586              : };
     587              : 
     588              : template <Namespace N> class ForeverStack : public ForeverStackBase
     589              : {
     590              : public:
     591        19476 :   ForeverStack (Node &root, Node &lang_prelude, Node &extern_prelude)
     592              :     : ForeverStackBase (root, lang_prelude, extern_prelude),
     593        19476 :       cursor_reference (root)
     594              :   {
     595        19476 :     rust_assert (root.is_root ());
     596        19476 :     rust_assert (root.is_leaf ());
     597              : 
     598              :     // TODO: Should we be using the forever stack root as the crate scope?
     599              :     // TODO: Is this how we should be getting the crate node id?
     600        19476 :     auto &mappings = Analysis::Mappings::get ();
     601        19476 :     root.id = *mappings.crate_num_to_nodeid (mappings.get_current_crate ());
     602        19476 :   }
     603              : 
     604              :   /**
     605              :    * Add a new Rib to the stack. If the Rib already exists, nothing is pushed
     606              :    * and the stack's cursor is simply moved to this existing Rib.
     607              :    *
     608              :    * @param rib The Rib to push
     609              :    * @param id The NodeId of the node for which the Rib was created. For
     610              :    *        example, if a Rib is created because a lexical scope is entered,
     611              :    *        then `id` is that `BlockExpr`'s NodeId.
     612              :    * @param path An optional path if the Rib was created due to a "named"
     613              :    *        lexical scope, like a module's.
     614              :    */
     615              :   void push (Rib::Kind rib_kind, NodeId id, tl::optional<Identifier> path = {});
     616              : 
     617              :   /**
     618              :    * Pop the innermost Rib from the stack
     619              :    */
     620              :   void pop ();
     621              : 
     622              :   /**
     623              :    * Insert a new definition in the innermost `Rib` in this stack
     624              :    *
     625              :    * @param name The name of the definition
     626              :    * @param id Its NodeId
     627              :    *
     628              :    * @return `DuplicateNameError` if that node was already present in the Rib,
     629              :    * the node's `NodeId` otherwise.
     630              :    *
     631              :    * @aborts if there are no `Rib`s inserted in the current map, this function
     632              :    *         aborts the program.
     633              :    */
     634              :   tl::expected<NodeId, DuplicateNameError> insert (Identifier name, NodeId id);
     635              : 
     636              :   tl::expected<NodeId, DuplicateNameError> insert_variant (Identifier name,
     637              :                                                            NodeId id);
     638              : 
     639              :   /**
     640              :    * Insert a new shadowable definition in the innermost `Rib` in this stack
     641              :    *
     642              :    * @param name The name of the definition
     643              :    * @param id Its NodeId
     644              :    *
     645              :    * @return `DuplicateNameError` if that node was already present in the Rib,
     646              :    * the node's `NodeId` otherwise.
     647              :    *
     648              :    * @aborts if there are no `Rib`s inserted in the current map, this function
     649              :    *         aborts the program.
     650              :    */
     651              :   tl::expected<NodeId, DuplicateNameError> insert_shadowable (Identifier name,
     652              :                                                               NodeId id);
     653              : 
     654              :   /**
     655              :    * Insert a new glob-originated definition in the innermost `Rib` in this
     656              :    * stack
     657              :    *
     658              :    * @param name The name of the definition
     659              :    * @param id Its NodeId
     660              :    *
     661              :    * @return `DuplicateNameError` if that node was already present in the Rib,
     662              :    * the node's `NodeId` otherwise.
     663              :    *
     664              :    * @aborts if there are no `Rib`s inserted in the current map, this function
     665              :    *         aborts the program.
     666              :    */
     667              :   tl::expected<NodeId, DuplicateNameError> insert_globbed (Identifier name,
     668              :                                                            NodeId id);
     669              : 
     670              :   /**
     671              :    * Insert a new definition at the root of this stack
     672              :    *
     673              :    * @param name The name of the definition
     674              :    * @param id Its NodeId
     675              :    *
     676              :    * @return `DuplicateNameError` if that node was already present in the Rib,
     677              :    * the node's `NodeId` otherwise.
     678              :    *
     679              :    * @aborts if there are no `Rib`s inserted in the current map, this function
     680              :    *         aborts the program.
     681              :    */
     682              :   tl::expected<NodeId, DuplicateNameError> insert_at_root (Identifier name,
     683              :                                                            NodeId id);
     684              : 
     685              :   /**
     686              :    * Insert an item within the lang prelude
     687              :    *
     688              :    * @param name The name of the definition
     689              :    * @param id Its NodeId
     690              :    */
     691              :   void insert_lang_prelude (Identifier name, NodeId id);
     692              : 
     693              :   /* Access the innermost `Rib` in this map */
     694              :   Rib &peek ();
     695              :   const Rib &peek () const;
     696              : 
     697              :   /**
     698              :    * Reverse iter on all ribs from the innermost one to the outermost one,
     699              :    * trying to find a name. This is the default algorithm.
     700              :    * This function gets specialized based on the Rib::Kind
     701              :    * this way, we ensure a proper resolution algorithm at the type level
     702              :    *
     703              :    * @param name Name of the identifier to locate in this scope or an outermore
     704              :    *        scope
     705              :    *
     706              :    * @return a valid option with the Definition if the identifier is present in
     707              :    * the current map, an empty one otherwise.
     708              :    */
     709              :   tl::optional<Rib::Definition> get (const Identifier &name);
     710              :   tl::optional<Rib::Definition> get_lang_prelude (const Identifier &name);
     711              :   tl::optional<Rib::Definition> get_lang_prelude (const std::string &name);
     712              :   tl::optional<Rib::Definition> get_from_prelude (NodeId prelude,
     713              :                                                   const Identifier &name);
     714              : 
     715              :   // FIXME: Documentation
     716              :   tl::optional<Rib &> to_rib (NodeId rib_id);
     717              :   tl::optional<const Rib &> to_rib (NodeId rib_id) const;
     718              : 
     719              :   std::string as_debug_string () const;
     720              : 
     721              :   /**
     722              :    * Used to check if a module is a descendant of another module
     723              :    * Intended for use in the privacy checker
     724              :    */
     725              :   bool is_module_descendant (NodeId parent, NodeId child) const;
     726              : 
     727              :   tl::optional<Rib::Definition> get (Node &start, const Identifier &name);
     728              : 
     729              :   /* Should we keep going upon seeing a Rib? */
     730              :   enum class KeepGoing
     731              :   {
     732              :     Yes,
     733              :     No,
     734              :   };
     735              : 
     736              :   /* Add a new Rib to the stack. This is an internal method */
     737              :   void push_inner (Rib::Kind rib, Link link);
     738              : 
     739              :   /* Reverse iterate on `Node`s from the cursor, in an outwards fashion */
     740              :   void reverse_iter (std::function<KeepGoing (Node &)> lambda);
     741              :   void reverse_iter (std::function<KeepGoing (const Node &)> lambda) const;
     742              : 
     743              :   /* Reverse iterate on `Node`s from a specified one, in an outwards fashion */
     744              :   void reverse_iter (Node &start, std::function<KeepGoing (Node &)> lambda);
     745              :   void reverse_iter (const Node &start,
     746              :                      std::function<KeepGoing (const Node &)> lambda) const;
     747              : 
     748              :   Node &cursor ();
     749              :   const Node &cursor () const;
     750              : 
     751              :   void update_cursor (Node &new_cursor);
     752              : 
     753              :   std::reference_wrapper<Node> cursor_reference;
     754              : 
     755              :   void stream_rib (std::stringstream &stream, const Rib &rib,
     756              :                    const std::string &next, const std::string &next_next) const;
     757              :   void stream_node (std::stringstream &stream, unsigned indentation,
     758              :                     const Node &node, unsigned depth = 0) const;
     759              : 
     760              :   /* Helper types and functions for `resolve_path` */
     761              : 
     762              :   using SegIterator =
     763              :     typename std::vector<ResolutionPath::Segment>::const_iterator;
     764              : 
     765              :   Node &find_closest_module (Node &starting_point);
     766              : 
     767              :   tl::optional<SegIterator>
     768              :   find_starting_point (const std::vector<ResolutionPath::Segment> &segments,
     769              :                        std::reference_wrapper<Node> &starting_point,
     770              :                        std::function<void (Usage, Definition, Namespace)>
     771              :                          insert_segment_resolution,
     772              :                        std::vector<Error> &collect_errors);
     773              : 
     774              :   /* Helper functions for forward resolution (to_canonical_path, to_rib...) */
     775              :   struct DfsResult
     776              :   {
     777              :     Node &first;
     778              :     std::string second;
     779              :   };
     780              :   struct ConstDfsResult
     781              :   {
     782              :     const Node &first;
     783              :     std::string second;
     784              :   };
     785              : 
     786              :   // FIXME: Documentation
     787              :   tl::optional<DfsResult> dfs (Node &starting_point, NodeId to_find);
     788              :   tl::optional<ConstDfsResult> dfs (const Node &starting_point,
     789              :                                     NodeId to_find) const;
     790              :   // FIXME: Documentation
     791              :   tl::optional<Rib &> dfs_rib (Node &starting_point, NodeId to_find);
     792              :   tl::optional<const Rib &> dfs_rib (const Node &starting_point,
     793              :                                      NodeId to_find) const;
     794              :   // FIXME: Documentation
     795              :   tl::optional<Node &> dfs_node (Node &starting_point, NodeId to_find);
     796              :   tl::optional<const Node &> dfs_node (const Node &starting_point,
     797              :                                        NodeId to_find) const;
     798              : 
     799              :   std::unordered_map<NodeId, Node &> dfs_cache;
     800              :   tl::optional<Node &> check_cache (NodeId to_find);
     801              :   void cache (NodeId found, Node &result);
     802              : 
     803       129207 :   bool forward_declared (NodeId definition, NodeId usage)
     804              :   {
     805       129207 :     if (peek ().kind != Rib::Kind::ForwardTypeParamBan)
     806              :       return false;
     807              : 
     808          401 :     const auto &definition_rib = dfs_rib (cursor (), definition);
     809              : 
     810          401 :     if (!definition_rib)
     811              :       return false;
     812              : 
     813              :     return (definition_rib
     814            1 :             && definition_rib.value ().kind == Rib::Kind::ForwardTypeParamBan);
     815              :   }
     816              : 
     817       772387 :   void map_usage (Usage usage, Definition definition)
     818              :   {
     819       772387 :     resolved_nodes.emplace (usage, definition);
     820              : 
     821              :     // auto inserted = resolved_nodes.emplace (usage, definition);
     822              : 
     823              :     // is that valid?
     824              :     // FIXME: Yikes
     825              :     // rust_assert (inserted.first->first.id == definition.id);
     826              :   }
     827              : 
     828      3259834 :   tl::optional<NodeId> lookup (NodeId usage) const
     829              :   {
     830      3259834 :     auto it = resolved_nodes.find (Usage (usage));
     831              : 
     832      3259834 :     if (it == resolved_nodes.end ())
     833        89216 :       return tl::nullopt;
     834              : 
     835      3170618 :     return it->second.id;
     836              :   }
     837              : 
     838              :   tl::expected<Definition, LookupFinalizeError>
     839              :   find_leaf_definition (const NodeId &key) const;
     840              : 
     841              :   // Flattening is not needed for now but should be used later?
     842              : #if 0
     843              :   /**
     844              :    * Look at NameResolutionContext::flatten - This is the inner working function
     845              :    * which works on one specific namespace, while NameResolutionContext::flatten
     846              :    * calls flatten for every namespace
     847              :    */
     848              :   void flatten ();
     849              : #endif
     850              : 
     851              :   /* Map of "usage" nodes which have been resolved to a "definition" node */
     852              :   std::map<Usage, Definition> resolved_nodes;
     853              : };
     854              : 
     855              : } // namespace Resolver2_0
     856              : } // namespace Rust
     857              : 
     858              : #include "rust-forever-stack.hxx"
     859              : 
     860              : #endif // !RUST_FOREVER_STACK_H
        

Generated by: LCOV version 2.4-beta

LCOV profile is generated on x86_64 machine using following configure options: configure --disable-bootstrap --enable-coverage=opt --enable-languages=c,c++,fortran,go,jit,lto,rust,m2 --enable-host-shared. GCC test suite is run with the built compiler.