LCOV - code coverage report
Current view: top level - gcc/rust/resolve - rust-early-name-resolver-2.0.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 75.0 % 116 87
Test Date: 2024-04-27 14:03:13 Functions: 93.3 % 15 14
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: - 0 0

             Branch data     Line data    Source code
       1                 :             : // Copyright (C) 2020-2024 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-early-name-resolver-2.0.h"
      20                 :             : #include "rust-ast-full.h"
      21                 :             : #include "rust-toplevel-name-resolver-2.0.h"
      22                 :             : #include "rust-attributes.h"
      23                 :             : 
      24                 :             : namespace Rust {
      25                 :             : namespace Resolver2_0 {
      26                 :             : 
      27                 :          21 : Early::Early (NameResolutionContext &ctx) : DefaultResolver (ctx) {}
      28                 :             : 
      29                 :             : void
      30                 :          11 : Early::insert_once (AST::MacroInvocation &invocation, NodeId resolved)
      31                 :             : {
      32                 :             :   // TODO: Should we use `ctx.mark_resolved()`?
      33                 :          11 :   AST::MacroRulesDefinition *definition;
      34                 :          11 :   auto ok = ctx.mappings.lookup_macro_def (resolved, &definition);
      35                 :             : 
      36                 :          11 :   rust_assert (ok);
      37                 :             : 
      38                 :          11 :   AST::MacroRulesDefinition *existing;
      39                 :          11 :   auto exists = ctx.mappings.lookup_macro_invocation (invocation, &existing);
      40                 :             : 
      41                 :          11 :   if (!exists)
      42                 :          11 :     ctx.mappings.insert_macro_invocation (invocation, definition);
      43                 :          11 : }
      44                 :             : 
      45                 :             : void
      46                 :          14 : Early::insert_once (AST::MacroRulesDefinition &def)
      47                 :             : {
      48                 :             :   // TODO: Should we use `ctx.mark_resolved()`?
      49                 :          14 :   AST::MacroRulesDefinition *definition;
      50                 :          14 :   auto exists = ctx.mappings.lookup_macro_def (def.get_node_id (), &definition);
      51                 :             : 
      52                 :          14 :   if (!exists)
      53                 :           0 :     ctx.mappings.insert_macro_def (&def);
      54                 :          14 : }
      55                 :             : 
      56                 :             : void
      57                 :          21 : Early::go (AST::Crate &crate)
      58                 :             : {
      59                 :             :   // First we go through TopLevel resolution to get all our declared items
      60                 :          21 :   auto toplevel = TopLevel (ctx);
      61                 :          21 :   toplevel.go (crate);
      62                 :             : 
      63                 :          21 :   textual_scope.push ();
      64                 :             : 
      65                 :             :   // Then we proceed to the proper "early" name resolution: Import and macro
      66                 :             :   // name resolution
      67                 :          74 :   for (auto &item : crate.items)
      68                 :          53 :     item->accept_vis (*this);
      69                 :             : 
      70                 :          21 :   textual_scope.pop ();
      71                 :          21 : }
      72                 :             : 
      73                 :             : void
      74                 :          91 : Early::TextualScope::push ()
      75                 :             : {
      76                 :             :   // push a new empty scope
      77                 :          91 :   scopes.emplace_back ();
      78                 :          91 : }
      79                 :             : 
      80                 :             : void
      81                 :          91 : Early::TextualScope::pop ()
      82                 :             : {
      83                 :          91 :   rust_assert (!scopes.empty ());
      84                 :             : 
      85                 :          91 :   scopes.pop_back ();
      86                 :          91 : }
      87                 :             : 
      88                 :             : void
      89                 :          14 : Early::TextualScope::insert (std::string name, NodeId id)
      90                 :             : {
      91                 :          14 :   rust_assert (!scopes.empty ());
      92                 :             : 
      93                 :             :   // we can ignore the return value as we always want the latest defined macro
      94                 :             :   // to shadow a previous one - so if two macros have the same name and get
      95                 :             :   // inserted with the same key, it's not a bug
      96                 :          14 :   scopes.back ().insert ({name, id});
      97                 :          14 : }
      98                 :             : 
      99                 :             : tl::optional<NodeId>
     100                 :           5 : Early::TextualScope::get (const std::string &name)
     101                 :             : {
     102                 :           9 :   for (auto iterator = scopes.rbegin (); iterator != scopes.rend (); iterator++)
     103                 :             :     {
     104                 :           7 :       auto scope = *iterator;
     105                 :           7 :       auto found = scope.find (name);
     106                 :           7 :       if (found != scope.end ())
     107                 :           3 :         return found->second;
     108                 :           7 :     }
     109                 :             : 
     110                 :           2 :   return tl::nullopt;
     111                 :             : }
     112                 :             : 
     113                 :             : void
     114                 :          14 : Early::visit (AST::MacroRulesDefinition &def)
     115                 :             : {
     116                 :          14 :   DefaultResolver::visit (def);
     117                 :             : 
     118                 :          14 :   textual_scope.insert (def.get_rule_name ().as_string (), def.get_node_id ());
     119                 :          14 :   insert_once (def);
     120                 :          14 : }
     121                 :             : 
     122                 :             : void
     123                 :          39 : Early::visit (AST::BlockExpr &block)
     124                 :             : {
     125                 :          39 :   textual_scope.push ();
     126                 :             : 
     127                 :          39 :   DefaultResolver::visit (block);
     128                 :             : 
     129                 :          39 :   textual_scope.pop ();
     130                 :          39 : }
     131                 :             : 
     132                 :             : void
     133                 :          31 : Early::visit (AST::Module &module)
     134                 :             : {
     135                 :          31 :   textual_scope.push ();
     136                 :             : 
     137                 :          31 :   DefaultResolver::visit (module);
     138                 :             : 
     139                 :          31 :   textual_scope.pop ();
     140                 :          31 : }
     141                 :             : 
     142                 :             : void
     143                 :          17 : Early::visit (AST::MacroInvocation &invoc)
     144                 :             : {
     145                 :          17 :   auto path = invoc.get_invoc_data ().get_path ();
     146                 :             : 
     147                 :             :   // When a macro is invoked by an unqualified identifier (not part of a
     148                 :             :   // multi-part path), it is first looked up in textual scoping. If this does
     149                 :             :   // not yield any results, then it is looked up in path-based scoping. If the
     150                 :             :   // macro's name is qualified with a path, then it is only looked up in
     151                 :             :   // path-based scoping.
     152                 :             : 
     153                 :             :   // https://doc.rust-lang.org/reference/macros-by-example.html#path-based-scope
     154                 :             : 
     155                 :          17 :   tl::optional<NodeId> definition = tl::nullopt;
     156                 :          17 :   if (path.get_segments ().size () == 1)
     157                 :           5 :     definition = textual_scope.get (path.get_final_segment ().as_string ());
     158                 :             : 
     159                 :             :   // we won't have changed `definition` from `nullopt` if there are more
     160                 :             :   // than one segments in our path
     161                 :          17 :   if (!definition.has_value ())
     162                 :          14 :     definition = ctx.macros.resolve_path (path.get_segments ());
     163                 :             : 
     164                 :             :   // if the definition still does not have a value, then it's an error
     165                 :          17 :   if (!definition.has_value ())
     166                 :             :     {
     167                 :           6 :       collect_error (Error (invoc.get_locus (), ErrorCode::E0433,
     168                 :             :                             "could not resolve macro invocation"));
     169                 :           6 :       return;
     170                 :             :     }
     171                 :             : 
     172                 :          11 :   insert_once (invoc, *definition);
     173                 :             : 
     174                 :             :   // now do we need to keep mappings or something? or insert "uses" into our
     175                 :             :   // ForeverStack? can we do that? are mappings simpler?
     176                 :          11 :   auto mappings = Analysis::Mappings::get ();
     177                 :          11 :   AST::MacroRulesDefinition *rules_def = nullptr;
     178                 :          11 :   if (!mappings->lookup_macro_def (definition.value (), &rules_def))
     179                 :             :     {
     180                 :             :       // Macro definition not found, maybe it is not expanded yet.
     181                 :             :       return;
     182                 :             :     }
     183                 :             : 
     184                 :          11 :   AST::MacroRulesDefinition *tmp_def = nullptr;
     185                 :          11 :   if (mappings->lookup_macro_invocation (invoc, &tmp_def))
     186                 :             :     return;
     187                 :             : 
     188                 :           0 :   mappings->insert_macro_invocation (invoc, rules_def);
     189                 :          17 : }
     190                 :             : 
     191                 :             : void
     192                 :          39 : Early::visit_attributes (std::vector<AST::Attribute> &attrs)
     193                 :             : {
     194                 :          39 :   auto mappings = Analysis::Mappings::get ();
     195                 :             : 
     196                 :          41 :   for (auto &attr : attrs)
     197                 :             :     {
     198                 :           2 :       auto name = attr.get_path ().get_segments ().at (0).get_segment_name ();
     199                 :             : 
     200                 :           2 :       if (attr.is_derive ())
     201                 :             :         {
     202                 :           0 :           auto traits = attr.get_traits_to_derive ();
     203                 :           0 :           for (auto &trait : traits)
     204                 :             :             {
     205                 :           0 :               auto definition
     206                 :           0 :                 = ctx.macros.resolve_path (trait.get ().get_segments ());
     207                 :           0 :               if (!definition.has_value ())
     208                 :             :                 {
     209                 :             :                   // FIXME: Change to proper error message
     210                 :           0 :                   rust_error_at (trait.get ().get_locus (),
     211                 :             :                                  "could not resolve trait");
     212                 :           0 :                   continue;
     213                 :             :                 }
     214                 :             : 
     215                 :           0 :               auto pm_def
     216                 :           0 :                 = mappings->lookup_derive_proc_macro_def (definition.value ());
     217                 :             : 
     218                 :           0 :               rust_assert (pm_def.has_value ());
     219                 :             : 
     220                 :           0 :               mappings->insert_derive_proc_macro_invocation (trait,
     221                 :           0 :                                                              pm_def.value ());
     222                 :             :             }
     223                 :           0 :         }
     224                 :           4 :       else if (Analysis::BuiltinAttributeMappings::get ()
     225                 :           2 :                  ->lookup_builtin (name)
     226                 :           2 :                  .is_error ()) // Do not resolve builtins
     227                 :             :         {
     228                 :           0 :           auto definition
     229                 :           0 :             = ctx.macros.resolve_path (attr.get_path ().get_segments ());
     230                 :           0 :           if (!definition.has_value ())
     231                 :             :             {
     232                 :             :               // FIXME: Change to proper error message
     233                 :           0 :               rust_error_at (attr.get_locus (),
     234                 :             :                              "could not resolve attribute macro invocation");
     235                 :           0 :               return;
     236                 :             :             }
     237                 :           0 :           auto pm_def
     238                 :           0 :             = mappings->lookup_attribute_proc_macro_def (definition.value ());
     239                 :             : 
     240                 :           0 :           rust_assert (pm_def.has_value ());
     241                 :             : 
     242                 :           0 :           mappings->insert_attribute_proc_macro_invocation (attr.get_path (),
     243                 :           0 :                                                             pm_def.value ());
     244                 :             :         }
     245                 :           2 :     }
     246                 :             : }
     247                 :             : 
     248                 :             : void
     249                 :          39 : Early::visit (AST::Function &fn)
     250                 :             : {
     251                 :          39 :   visit_attributes (fn.get_outer_attrs ());
     252                 :          39 :   DefaultResolver::visit (fn);
     253                 :          39 : }
     254                 :             : 
     255                 :             : void
     256                 :           0 : Early::visit (AST::StructStruct &s)
     257                 :             : {
     258                 :           0 :   visit_attributes (s.get_outer_attrs ());
     259                 :           0 :   DefaultResolver::visit (s);
     260                 :           0 : }
     261                 :             : 
     262                 :             : } // namespace Resolver2_0
     263                 :             : } // namespace Rust
        

Generated by: LCOV version 2.1-beta

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