public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r14-7948] gccrs: Resolve nested macro definition
@ 2024-01-16 18:09 Arthur Cohen
  0 siblings, 0 replies; only message in thread
From: Arthur Cohen @ 2024-01-16 18:09 UTC (permalink / raw)
  To: gcc-cvs

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

commit r14-7948-gdfef82f273690130660acc4bf989d372c33e7528
Author: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
Date:   Tue Aug 8 12:16:44 2023 +0200

    gccrs: Resolve nested macro definition
    
    We need to collect the early resolver's macros error to emit them at a
    later stage after further expansion in order to retrieve macros defined
    by other macro invocations.
    Register some mappings for macro invocations and macro definitions.
    
    gcc/rust/ChangeLog:
    
            * resolve/rust-early-name-resolver-2.0.cc (Early::visit):
            Collect error instead of emitting it. Also add invocation
            registration.
            * resolve/rust-early-name-resolver-2.0.h (std::function<void):
            Add type definition for collection.
            * resolve/rust-toplevel-name-resolver-2.0.cc (TopLevel::visit):
            Register macro rule definition in mappings.
            * rust-session-manager.cc (Session::expansion): Add macro
            resolve error collection.
    
    Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>

Diff:
---
 gcc/rust/resolve/rust-early-name-resolver-2.0.cc    | 19 +++++++++++++++++--
 gcc/rust/resolve/rust-early-name-resolver-2.0.h     | 10 ++++++++++
 gcc/rust/resolve/rust-toplevel-name-resolver-2.0.cc |  7 +++++++
 gcc/rust/rust-session-manager.cc                    | 11 ++++++++++-
 4 files changed, 44 insertions(+), 3 deletions(-)

diff --git a/gcc/rust/resolve/rust-early-name-resolver-2.0.cc b/gcc/rust/resolve/rust-early-name-resolver-2.0.cc
index df72d6e27c3..a201bc4a78b 100644
--- a/gcc/rust/resolve/rust-early-name-resolver-2.0.cc
+++ b/gcc/rust/resolve/rust-early-name-resolver-2.0.cc
@@ -135,13 +135,28 @@ Early::visit (AST::MacroInvocation &invoc)
   // if the definition still does not have a value, then it's an error
   if (!definition.has_value ())
     {
-      rust_error_at (invoc.get_locus (), ErrorCode::E0433,
-		     "could not resolve macro invocation");
+      collect_error ([&] () {
+	rust_error_at (invoc.get_locus (), ErrorCode::E0433,
+		       "could not resolve macro invocation");
+      });
       return;
     }
 
   // 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 ();
+  AST::MacroRulesDefinition *rules_def = nullptr;
+  if (!mappings->lookup_macro_def (definition.value (), &rules_def))
+    {
+      // Macro definition not found, maybe it is not expanded yet.
+      return;
+    }
+
+  AST::MacroRulesDefinition *tmp_def = nullptr;
+  if (mappings->lookup_macro_invocation (invoc, &tmp_def))
+    return;
+
+  mappings->insert_macro_invocation (invoc, rules_def);
 }
 
 void
diff --git a/gcc/rust/resolve/rust-early-name-resolver-2.0.h b/gcc/rust/resolve/rust-early-name-resolver-2.0.h
index fe83cf4493b..67785d0b604 100644
--- a/gcc/rust/resolve/rust-early-name-resolver-2.0.h
+++ b/gcc/rust/resolve/rust-early-name-resolver-2.0.h
@@ -28,6 +28,8 @@
 namespace Rust {
 namespace Resolver2_0 {
 
+using ResolveError = std::function<void ()>;
+
 class Early : public DefaultResolver
 {
   using DefaultResolver::visit;
@@ -37,6 +39,11 @@ public:
 
   void go (AST::Crate &crate);
 
+  const std::vector<ResolveError> &get_macro_resolve_errors () const
+  {
+    return macro_resolve_errors;
+  }
+
   // we need to handle definitions for textual scoping
   void visit (AST::MacroRulesDefinition &) override;
 
@@ -76,6 +83,9 @@ private:
   };
 
   TextualScope textual_scope;
+  std::vector<ResolveError> macro_resolve_errors;
+
+  void collect_error (ResolveError e) { macro_resolve_errors.push_back (e); }
 };
 
 } // namespace Resolver2_0
diff --git a/gcc/rust/resolve/rust-toplevel-name-resolver-2.0.cc b/gcc/rust/resolve/rust-toplevel-name-resolver-2.0.cc
index c28d9227003..e6c48d5d0ed 100644
--- a/gcc/rust/resolve/rust-toplevel-name-resolver-2.0.cc
+++ b/gcc/rust/resolve/rust-toplevel-name-resolver-2.0.cc
@@ -107,6 +107,13 @@ TopLevel::visit (AST::MacroRulesDefinition &macro)
 
   if (macro.get_kind () == AST::MacroRulesDefinition::MacroKind::DeclMacro)
     insert_or_error_out (macro.get_rule_name (), macro, Namespace::Macros);
+
+  auto mappings = Analysis::Mappings::get ();
+  AST::MacroRulesDefinition *tmp = nullptr;
+  if (mappings->lookup_macro_def (macro.get_node_id (), &tmp))
+    return;
+
+  mappings->insert_macro_def (&macro);
 }
 
 void
diff --git a/gcc/rust/rust-session-manager.cc b/gcc/rust/rust-session-manager.cc
index 1c5d72906d8..8911e7d89ae 100644
--- a/gcc/rust/rust-session-manager.cc
+++ b/gcc/rust/rust-session-manager.cc
@@ -874,6 +874,7 @@ Session::expansion (AST::Crate &crate)
   /* expand by calling cxtctxt object's monotonic_expander's expand_crate
    * method. */
   MacroExpander expander (crate, cfg, *this);
+  std::vector<Resolver2_0::ResolveError> macro_errors;
 
   while (!fixed_point_reached && iterations < cfg.recursion_limit)
     {
@@ -882,7 +883,11 @@ Session::expansion (AST::Crate &crate)
       auto ctx = Resolver2_0::NameResolutionContext ();
 
       if (flag_name_resolution_2_0)
-	Resolver2_0::Early (ctx).go (crate);
+	{
+	  Resolver2_0::Early early (ctx);
+	  early.go (crate);
+	  macro_errors = early.get_macro_resolve_errors ();
+	}
       else
 	Resolver::EarlyNameResolver ().go (crate);
 
@@ -896,6 +901,10 @@ Session::expansion (AST::Crate &crate)
 	break;
     }
 
+  // Fixed point reached: Emit unresolved macros error
+  for (auto &error : macro_errors)
+    error ();
+
   if (iterations == cfg.recursion_limit)
     {
       auto &last_invoc = expander.get_last_invocation ();

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

only message in thread, other threads:[~2024-01-16 18:09 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-01-16 18:09 [gcc r14-7948] gccrs: Resolve nested macro definition Arthur Cohen

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).