]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: early: Resolve paths properly
authorArthur Cohen <arthur.cohen@embecosm.com>
Mon, 21 Aug 2023 14:52:48 +0000 (16:52 +0200)
committerArthur Cohen <arthur.cohen@embecosm.com>
Tue, 30 Jan 2024 11:36:46 +0000 (12:36 +0100)
gcc/rust/ChangeLog:

* resolve/rust-early-name-resolver-2.0.cc
(Early::insert_once): New function.
(Early::visit): Likewise.
* resolve/rust-early-name-resolver-2.0.h: Likewise.

gcc/rust/resolve/rust-early-name-resolver-2.0.cc
gcc/rust/resolve/rust-early-name-resolver-2.0.h

index 2245ba31772f79b8d018c806056c4a9fe6527a14..48bb4c68d26837fe33350cb5f3d3578406229bd2 100644 (file)
@@ -26,6 +26,33 @@ namespace Resolver2_0 {
 
 Early::Early (NameResolutionContext &ctx) : DefaultResolver (ctx) {}
 
+void
+Early::insert_once (AST::MacroInvocation &invocation, NodeId resolved)
+{
+  // TODO: Should we use `ctx.mark_resolved()`?
+  AST::MacroRulesDefinition *definition;
+  auto ok = ctx.mappings.lookup_macro_def (resolved, &definition);
+
+  rust_assert (ok);
+
+  AST::MacroRulesDefinition *existing;
+  auto exists = ctx.mappings.lookup_macro_invocation (invocation, &existing);
+
+  if (!exists)
+    ctx.mappings.insert_macro_invocation (invocation, definition);
+}
+
+void
+Early::insert_once (AST::MacroRulesDefinition &def)
+{
+  // TODO: Should we use `ctx.mark_resolved()`?
+  AST::MacroRulesDefinition *definition;
+  auto exists = ctx.mappings.lookup_macro_def (def.get_node_id (), &definition);
+
+  if (!exists)
+    ctx.mappings.insert_macro_def (&def);
+}
+
 void
 Early::go (AST::Crate &crate)
 {
@@ -89,6 +116,7 @@ Early::visit (AST::MacroRulesDefinition &def)
   DefaultResolver::visit (def);
 
   textual_scope.insert (def.get_rule_name ().as_string (), def.get_node_id ());
+  insert_once (def);
 }
 
 void
@@ -141,6 +169,8 @@ Early::visit (AST::MacroInvocation &invoc)
       return;
     }
 
+  insert_once (invoc, *definition);
+
   // now do we need to keep mappings or something? or insert "uses" into our
   // ForeverStack? can we do that? are mappings simpler?
   auto mappings = Analysis::Mappings::get ();
index dc27319647352f89de4b41f9305a6fae8fa8090b..46c4b936866e0d6897f04b986367140230e4a924 100644 (file)
@@ -60,6 +60,16 @@ public:
 private:
   void visit_attributes (std::vector<AST::Attribute> &attrs);
 
+  /**
+   * Insert a resolved macro invocation into the mappings once, meaning that we
+   * can call this function each time the early name resolution pass is underway
+   * and it will not trigger assertions for already resolved invocations.
+   */
+  // TODO: Rename
+  void insert_once (AST::MacroInvocation &invocation, NodeId resolved);
+  // TODO: Rename
+  void insert_once (AST::MacroRulesDefinition &definition);
+
   /**
    * Macros can either be resolved through textual scoping or regular path
    * scoping - which this class represents. Textual scoping works similarly to a