public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc/devel/rust/master] Refactor how we track the current module scope id
@ 2022-06-14  8:03 Thomas Schwinge
  0 siblings, 0 replies; only message in thread
From: Thomas Schwinge @ 2022-06-14  8:03 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:fc3ef6c4b1fad0d88a65043df8102437416b1df3

commit fc3ef6c4b1fad0d88a65043df8102437416b1df3
Author: Philip Herron <philip.herron@embecosm.com>
Date:   Fri Jun 3 13:13:35 2022 +0100

    Refactor how we track the current module scope id
    
    This adds a module scope stack to the resolver class such that we don't
    have to continually update our constructors but are able to track it as
    part of this context class instead.
    
    Addresses #1227

Diff:
---
 gcc/rust/resolve/rust-ast-resolve-base.h     |  6 ++----
 gcc/rust/resolve/rust-ast-resolve-item.cc    |  6 ++++--
 gcc/rust/resolve/rust-ast-resolve-toplevel.h | 28 +++++++++++++++++++++-------
 gcc/rust/resolve/rust-ast-resolve.cc         | 13 +++++++++++--
 gcc/rust/resolve/rust-name-resolver.h        | 20 ++++++++++++++++++++
 5 files changed, 58 insertions(+), 15 deletions(-)

diff --git a/gcc/rust/resolve/rust-ast-resolve-base.h b/gcc/rust/resolve/rust-ast-resolve-base.h
index 9c1f0a10ac8..17d05c38cf2 100644
--- a/gcc/rust/resolve/rust-ast-resolve-base.h
+++ b/gcc/rust/resolve/rust-ast-resolve-base.h
@@ -199,10 +199,9 @@ public:
   void visit (AST::BareFunctionType &);
 
 protected:
-  ResolverBase (NodeId parent, NodeId current_module = UNKNOWN_NODEID)
+  ResolverBase (NodeId parent)
     : resolver (Resolver::get ()), mappings (Analysis::Mappings::get ()),
-      resolved_node (UNKNOWN_NODEID), parent (parent),
-      current_module (current_module), locus (Location ())
+      resolved_node (UNKNOWN_NODEID), parent (parent), locus (Location ())
   {}
 
   bool resolved () const { return resolved_node != UNKNOWN_NODEID; }
@@ -216,7 +215,6 @@ protected:
   Analysis::Mappings *mappings;
   NodeId resolved_node;
   NodeId parent;
-  NodeId current_module;
   Location locus;
 };
 
diff --git a/gcc/rust/resolve/rust-ast-resolve-item.cc b/gcc/rust/resolve/rust-ast-resolve-item.cc
index 603037e8d8f..198c9c0ca3d 100644
--- a/gcc/rust/resolve/rust-ast-resolve-item.cc
+++ b/gcc/rust/resolve/rust-ast-resolve-item.cc
@@ -253,12 +253,14 @@ ResolveItem::visit (AST::Module &module)
   // FIXME: Should we reinsert a child here? Any reason we ResolveTopLevel::go
   // in ResolveTopLevel::visit (AST::Module) as well as here?
   for (auto &item : module.get_items ())
-    ResolveTopLevel::go (item.get (), CanonicalPath::create_empty (), cpath,
-			 module.get_node_id ());
+    ResolveTopLevel::go (item.get (), CanonicalPath::create_empty (), cpath);
 
+  resolver->push_new_module_scope (module.get_node_id ());
   for (auto &item : module.get_items ())
     ResolveItem::go (item.get (), path, cpath);
 
+  resolver->pop_module_scope ();
+
   resolver->get_name_scope ().pop ();
   resolver->get_type_scope ().pop ();
   resolver->get_label_scope ().pop ();
diff --git a/gcc/rust/resolve/rust-ast-resolve-toplevel.h b/gcc/rust/resolve/rust-ast-resolve-toplevel.h
index 8d4e12b0616..7a1458e6143 100644
--- a/gcc/rust/resolve/rust-ast-resolve-toplevel.h
+++ b/gcc/rust/resolve/rust-ast-resolve-toplevel.h
@@ -34,13 +34,15 @@ class ResolveTopLevel : public ResolverBase
 
 public:
   static void go (AST::Item *item, const CanonicalPath &prefix,
-		  const CanonicalPath &canonical_prefix, NodeId current_module)
+		  const CanonicalPath &canonical_prefix)
   {
     if (item->is_marked_for_strip ())
       return;
 
-    ResolveTopLevel resolver (prefix, canonical_prefix, current_module);
+    ResolveTopLevel resolver (prefix, canonical_prefix);
     item->accept_vis (resolver);
+
+    NodeId current_module = resolver.resolver->peek_current_module_scope ();
     resolver.mappings->insert_child_item_to_parent_module_mapping (
       item->get_node_id (), current_module);
   };
@@ -64,11 +66,15 @@ public:
 				     Definition{module.get_node_id (),
 						module.get_node_id ()});
 
+    NodeId current_module = resolver->peek_current_module_scope ();
     mappings->insert_module_child_item (current_module, mod);
     mappings->insert_module_child (current_module, module.get_node_id ());
 
+    resolver->push_new_module_scope (module.get_node_id ());
     for (auto &item : module.get_items ())
-      ResolveTopLevel::go (item.get (), path, cpath, module.get_node_id ());
+      ResolveTopLevel::go (item.get (), path, cpath);
+
+    resolver->pop_module_scope ();
 
     mappings->insert_canonical_path (mappings->get_current_crate (),
 				     module.get_node_id (), cpath);
@@ -89,6 +95,7 @@ public:
 	rust_error_at (r, "redefined multiple times");
       });
 
+    NodeId current_module = resolver->peek_current_module_scope ();
     mappings->insert_module_child_item (current_module, talias);
     mappings->insert_canonical_path (mappings->get_current_crate (),
 				     alias.get_node_id (), cpath);
@@ -109,6 +116,7 @@ public:
 	rust_error_at (r, "redefined multiple times");
       });
 
+    NodeId current_module = resolver->peek_current_module_scope ();
     mappings->insert_module_child_item (current_module, decl);
     mappings->insert_canonical_path (mappings->get_current_crate (),
 				     struct_decl.get_node_id (), cpath);
@@ -130,8 +138,9 @@ public:
       });
 
     for (auto &variant : enum_decl.get_variants ())
-      ResolveTopLevel::go (variant.get (), path, cpath, current_module);
+      ResolveTopLevel::go (variant.get (), path, cpath);
 
+    NodeId current_module = resolver->peek_current_module_scope ();
     mappings->insert_module_child_item (current_module, decl);
     mappings->insert_canonical_path (mappings->get_current_crate (),
 				     enum_decl.get_node_id (), cpath);
@@ -152,7 +161,6 @@ public:
 	rust_error_at (r, "redefined multiple times");
       });
 
-    mappings->insert_module_child_item (current_module, decl);
     mappings->insert_canonical_path (mappings->get_current_crate (),
 				     item.get_node_id (), cpath);
   }
@@ -229,6 +237,7 @@ public:
 	rust_error_at (r, "redefined multiple times");
       });
 
+    NodeId current_module = resolver->peek_current_module_scope ();
     mappings->insert_module_child_item (current_module, decl);
     mappings->insert_canonical_path (mappings->get_current_crate (),
 				     struct_decl.get_node_id (), cpath);
@@ -249,6 +258,7 @@ public:
 	rust_error_at (r, "redefined multiple times");
       });
 
+    NodeId current_module = resolver->peek_current_module_scope ();
     mappings->insert_module_child_item (current_module, decl);
     mappings->insert_canonical_path (mappings->get_current_crate (),
 				     union_decl.get_node_id (), cpath);
@@ -273,6 +283,7 @@ public:
 						var.get_node_id ()});
     resolver->mark_decl_mutability (var.get_node_id (), var.is_mutable ());
 
+    NodeId current_module = resolver->peek_current_module_scope ();
     mappings->insert_module_child_item (current_module, decl);
     mappings->insert_canonical_path (mappings->get_current_crate (),
 				     var.get_node_id (), cpath);
@@ -295,6 +306,7 @@ public:
 				     Definition{constant.get_node_id (),
 						constant.get_node_id ()});
 
+    NodeId current_module = resolver->peek_current_module_scope ();
     mappings->insert_module_child_item (current_module, decl);
     mappings->insert_canonical_path (mappings->get_current_crate (),
 				     constant.get_node_id (), cpath);
@@ -325,6 +337,7 @@ public:
 					function.get_node_id ());
       }
 
+    NodeId current_module = resolver->peek_current_module_scope ();
     mappings->insert_module_child_item (current_module, decl);
     mappings->insert_canonical_path (mappings->get_current_crate (),
 				     function.get_node_id (), cpath);
@@ -403,6 +416,7 @@ public:
     for (auto &item : trait.get_trait_items ())
       ResolveTopLevelTraitItems::go (item.get (), path, cpath);
 
+    NodeId current_module = resolver->peek_current_module_scope ();
     mappings->insert_module_child_item (current_module, decl);
     mappings->insert_canonical_path (mappings->get_current_crate (),
 				     trait.get_node_id (), cpath);
@@ -418,8 +432,8 @@ public:
 
 private:
   ResolveTopLevel (const CanonicalPath &prefix,
-		   const CanonicalPath &canonical_prefix, NodeId current_module)
-    : ResolverBase (UNKNOWN_NODEID, current_module), prefix (prefix),
+		   const CanonicalPath &canonical_prefix)
+    : ResolverBase (UNKNOWN_NODEID), prefix (prefix),
       canonical_prefix (canonical_prefix)
   {}
 
diff --git a/gcc/rust/resolve/rust-ast-resolve.cc b/gcc/rust/resolve/rust-ast-resolve.cc
index 945ff28c195..8465162b565 100644
--- a/gcc/rust/resolve/rust-ast-resolve.cc
+++ b/gcc/rust/resolve/rust-ast-resolve.cc
@@ -81,20 +81,29 @@ NameResolution::go (AST::Crate &crate)
     = CanonicalPath::new_seg (scope_node_id, crate_name);
   crate_prefix.set_crate_num (cnum);
 
+  // setup the root scope
+  resolver->push_new_module_scope (scope_node_id);
+
   // first gather the top-level namespace names then we drill down so this
   // allows for resolving forward declarations since an impl block might have
   // a Self type Foo which is defined after the impl block for example.
   for (auto it = crate.items.begin (); it != crate.items.end (); it++)
     ResolveTopLevel::go (it->get (), CanonicalPath::create_empty (),
-			 crate_prefix, scope_node_id);
+			 crate_prefix);
 
   // FIXME remove this
   if (saw_errors ())
-    return;
+    {
+      resolver->pop_module_scope ();
+      return;
+    }
 
   // next we can drill down into the items and their scopes
   for (auto it = crate.items.begin (); it != crate.items.end (); it++)
     ResolveItem::go (it->get (), CanonicalPath::create_empty (), crate_prefix);
+
+  // done
+  resolver->pop_module_scope ();
 }
 
 // rust-ast-resolve-struct-expr-field.h
diff --git a/gcc/rust/resolve/rust-name-resolver.h b/gcc/rust/resolve/rust-name-resolver.h
index ab7cb5567e1..23a9d829b58 100644
--- a/gcc/rust/resolve/rust-name-resolver.h
+++ b/gcc/rust/resolve/rust-name-resolver.h
@@ -166,6 +166,23 @@ public:
   void set_unit_type_node_id (NodeId id) { unit_ty_node_id = id; }
   NodeId get_unit_type_node_id () { return unit_ty_node_id; }
 
+  void push_new_module_scope (NodeId module_id)
+  {
+    current_module_stack.push_back (module_id);
+  }
+
+  void pop_module_scope ()
+  {
+    rust_assert (!current_module_stack.empty ());
+    current_module_stack.pop_back ();
+  }
+
+  NodeId peek_current_module_scope () const
+  {
+    rust_assert (!current_module_stack.empty ());
+    return current_module_stack.back ();
+  }
+
 private:
   Resolver ();
 
@@ -211,6 +228,9 @@ private:
   std::map<NodeId, bool> decl_mutability;
   // map of resolved names and set of assignments to the decl
   std::map<NodeId, std::set<NodeId>> assignment_to_decl;
+
+  // keep track of the current module scope ids
+  std::vector<NodeId> current_module_stack;
 };
 
 } // namespace Resolver


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2022-06-14  8:03 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-06-14  8:03 [gcc/devel/rust/master] Refactor how we track the current module scope id Thomas Schwinge

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).