From: Pierre-Emmanuel Patry <[email protected]>

Put macro invocations and definitions in their own container and reuse
the Mapping infrastructure.

gcc/rust/ChangeLog:

        * expand/rust-macro-expand.h (struct MacroExpander): Update call name.
        * resolve/rust-early-name-resolver-2.0.cc 
(Early::visit_derive_attribute):
        Likewise.
        (Early::visit_non_builtin_attribute): Likewise.
        * resolve/rust-toplevel-name-resolver-2.0.cc 
(TopLevel::visit_extern_crate):
        Likewise.
        * util/rust-hir-map.cc (ProcMacroMappings::insert_derive_def): Remove.
        (ProcMacroMappings::insert_bang_def): Remove.
        (ProcMacroMappings::insert_attribute_def): Remove.
        (ProcMacroMappings::lookup_derive_def): Remove.
        (ProcMacroMappings::lookup_bang_def): Remove.
        (ProcMacroMappings::lookup_attribute_def): Remove.
        (ProcMacroMappings::insert_derive_invocation): Remove.
        (ProcMacroMappings::lookup_derive_invocation): Remove.
        (ProcMacroMappings::insert_bang_invocation): Remove.
        (ProcMacroMappings::lookup_bang_invocation): Remove.
        (ProcMacroMappings::insert_attribute_invocation): Remove.
        (ProcMacroMappings::lookup_attribute_invocation): Remove.
        * util/rust-hir-map.h (class ProcMacroMappings): Convert to struct.
        (struct ProcMacroDefinitionMappings): Add definition mappings.
        (struct ProcMacroInvocationsMappings): Add invocation mappings.
        (struct ProcMacroMappings): Use two new separate fields for definitions
        and invocations.

Signed-off-by: Pierre-Emmanuel Patry <[email protected]>
---
This change was merged into the gccrs repository and is posted here for
upstream visibility and potential drive-by review, as requested by GCC
release managers.
Each commit email contains a link to its details on github from where you can
find the Pull-Request and associated discussions.


Commit on github: 
https://github.com/Rust-GCC/gccrs/commit/320fdf21bfccc24d0f64f03c7e5c8aedc16edab2

The commit has NOT been mentioned in any issue.

The commit has been mentioned in the following pull-request(s):
 - https://github.com/Rust-GCC/gccrs/pull/4878

 gcc/rust/expand/rust-macro-expand.h           |   6 +-
 .../resolve/rust-early-name-resolver-2.0.cc   |  15 +--
 .../rust-toplevel-name-resolver-2.0.cc        |   8 +-
 gcc/rust/util/rust-hir-map.cc                 | 117 ------------------
 gcc/rust/util/rust-hir-map.h                  |  47 +++----
 5 files changed, 36 insertions(+), 157 deletions(-)

diff --git a/gcc/rust/expand/rust-macro-expand.h 
b/gcc/rust/expand/rust-macro-expand.h
index ae52bf79c..fcab75a20 100644
--- a/gcc/rust/expand/rust-macro-expand.h
+++ b/gcc/rust/expand/rust-macro-expand.h
@@ -412,7 +412,7 @@ struct MacroExpander
   AST::Fragment expand_derive_proc_macro (T &item, AST::SimplePath &path)
   {
     tl::optional<CustomDeriveProcMacro &> macro
-      = mappings.pmacro.lookup_derive_invocation (path);
+      = mappings.pmacro.invocations.derives.lookup (path.get_node_id ());
     if (!macro.has_value ())
       {
        rust_error_at (path.get_locus (), "macro not found");
@@ -435,7 +435,7 @@ struct MacroExpander
                                        AST::MacroInvocation &invocation)
   {
     tl::optional<BangProcMacro &> macro
-      = mappings.pmacro.lookup_bang_invocation (invocation);
+      = mappings.pmacro.invocations.bangs.lookup (invocation.get_node_id ());
     if (!macro.has_value ())
       {
        rust_error_at (invocation.get_locus (), "macro not found");
@@ -457,7 +457,7 @@ struct MacroExpander
   AST::Fragment expand_attribute_proc_macro (T &item, AST::SimplePath &path)
   {
     tl::optional<AttributeProcMacro &> macro
-      = mappings.pmacro.lookup_attribute_invocation (path);
+      = mappings.pmacro.invocations.attributes.lookup (path.get_node_id ());
     if (!macro.has_value ())
       {
        rust_error_at (path.get_locus (), "macro not found");
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 a0a5eec87..8b3220d49 100644
--- a/gcc/rust/resolve/rust-early-name-resolver-2.0.cc
+++ b/gcc/rust/resolve/rust-early-name-resolver-2.0.cc
@@ -402,11 +402,12 @@ Early::visit_derive_attribute (AST::Attribute &attr,
          continue;
        }
 
-      auto pm_def
-       = mappings.pmacro.lookup_derive_def (ns_def->definition.get_node_id ());
+      auto pm_def = mappings.pmacro.definitions.derives.lookup (
+       ns_def->definition.get_node_id ());
 
       if (pm_def.has_value ())
-       mappings.pmacro.insert_derive_invocation (trait, pm_def.value ());
+       mappings.pmacro.invocations.derives.insert (trait.get ().get_node_id (),
+                                                   pm_def.value ());
     }
 }
 
@@ -424,14 +425,14 @@ Early::visit_non_builtin_attribute (AST::Attribute &attr,
                            name.c_str ()));
       return;
     }
-  auto pm_def
-    = mappings.pmacro.lookup_attribute_def (ns_def->definition.get_node_id ());
+  auto pm_def = mappings.pmacro.definitions.attributes.lookup (
+    ns_def->definition.get_node_id ());
 
   if (!pm_def.has_value ())
     return;
 
-  mappings.pmacro.insert_attribute_invocation (attr.get_path (),
-                                              pm_def.value ());
+  mappings.pmacro.invocations.attributes.insert (
+    attr.get_path ().get_node_id (), pm_def.value ());
 }
 
 void
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 d92e5e2a4..fa81639b6 100644
--- a/gcc/rust/resolve/rust-toplevel-name-resolver-2.0.cc
+++ b/gcc/rust/resolve/rust-toplevel-name-resolver-2.0.cc
@@ -198,19 +198,21 @@ TopLevel::visit_extern_crate (AST::ExternCrate 
&extern_crate, AST::Crate &crate,
     {
       insert_macros (derive_macros.value (), ctx);
       for (auto &macro : derive_macros.value ())
-       mappings.pmacro.insert_derive_def (macro);
+       mappings.pmacro.definitions.derives.insert (macro.get_node_id (),
+                                                   macro);
     }
   if (attribute_macros.has_value ())
     {
       insert_macros (attribute_macros.value (), ctx);
       for (auto &macro : attribute_macros.value ())
-       mappings.pmacro.insert_attribute_def (macro);
+       mappings.pmacro.definitions.attributes.insert (macro.get_node_id (),
+                                                      macro);
     }
   if (bang_macros.has_value ())
     {
       insert_macros (bang_macros.value (), ctx);
       for (auto &macro : bang_macros.value ())
-       mappings.pmacro.insert_bang_def (macro);
+       mappings.pmacro.definitions.bangs.insert (macro.get_node_id (), macro);
     }
 
   // We do *NOT* visit the crate because loaded crates are resolved
diff --git a/gcc/rust/util/rust-hir-map.cc b/gcc/rust/util/rust-hir-map.cc
index 59c8bed8f..8876311b7 100644
--- a/gcc/rust/util/rust-hir-map.cc
+++ b/gcc/rust/util/rust-hir-map.cc
@@ -855,123 +855,6 @@ Mappings::get_exported_macros ()
   return exportedMacros;
 }
 
-void
-ProcMacroMappings::insert_derive_def (CustomDeriveProcMacro macro)
-{
-  auto it = procmacroDeriveMappings.find (macro.get_node_id ());
-  rust_assert (it == procmacroDeriveMappings.end ());
-
-  procmacroDeriveMappings[macro.get_node_id ()] = macro;
-}
-
-void
-ProcMacroMappings::insert_bang_def (BangProcMacro macro)
-{
-  auto it = procmacroBangMappings.find (macro.get_node_id ());
-  rust_assert (it == procmacroBangMappings.end ());
-
-  procmacroBangMappings[macro.get_node_id ()] = macro;
-}
-
-void
-ProcMacroMappings::insert_attribute_def (AttributeProcMacro macro)
-{
-  auto it = procmacroAttributeMappings.find (macro.get_node_id ());
-  rust_assert (it == procmacroAttributeMappings.end ());
-
-  procmacroAttributeMappings[macro.get_node_id ()] = macro;
-}
-
-tl::optional<CustomDeriveProcMacro &>
-ProcMacroMappings::lookup_derive_def (NodeId id)
-{
-  auto it = procmacroDeriveMappings.find (id);
-  if (it == procmacroDeriveMappings.end ())
-    return tl::nullopt;
-
-  return it->second;
-}
-
-tl::optional<BangProcMacro &>
-ProcMacroMappings::lookup_bang_def (NodeId id)
-{
-  auto it = procmacroBangMappings.find (id);
-  if (it == procmacroBangMappings.end ())
-    return tl::nullopt;
-
-  return it->second;
-}
-
-tl::optional<AttributeProcMacro &>
-ProcMacroMappings::lookup_attribute_def (NodeId id)
-{
-  auto it = procmacroAttributeMappings.find (id);
-  if (it == procmacroAttributeMappings.end ())
-    return tl::nullopt;
-
-  return it->second;
-}
-
-void
-ProcMacroMappings::insert_derive_invocation (AST::SimplePath &invoc,
-                                            CustomDeriveProcMacro def)
-{
-  auto it = procmacroDeriveInvocations.find (invoc.get_node_id ());
-  rust_assert (it == procmacroDeriveInvocations.end ());
-
-  procmacroDeriveInvocations[invoc.get_node_id ()] = def;
-}
-
-tl::optional<CustomDeriveProcMacro &>
-ProcMacroMappings::lookup_derive_invocation (AST::SimplePath &invoc)
-{
-  auto it = procmacroDeriveInvocations.find (invoc.get_node_id ());
-  if (it == procmacroDeriveInvocations.end ())
-    return tl::nullopt;
-
-  return it->second;
-}
-
-void
-ProcMacroMappings::insert_bang_invocation (AST::MacroInvocation &invoc,
-                                          BangProcMacro def)
-{
-  auto it = procmacroBangInvocations.find (invoc.get_node_id ());
-  rust_assert (it == procmacroBangInvocations.end ());
-
-  procmacroBangInvocations[invoc.get_node_id ()] = def;
-}
-
-tl::optional<BangProcMacro &>
-ProcMacroMappings::lookup_bang_invocation (AST::MacroInvocation &invoc)
-{
-  auto it = procmacroBangInvocations.find (invoc.get_node_id ());
-  if (it == procmacroBangInvocations.end ())
-    return tl::nullopt;
-
-  return it->second;
-}
-
-void
-ProcMacroMappings::insert_attribute_invocation (AST::SimplePath &invoc,
-                                               AttributeProcMacro def)
-{
-  auto it = procmacroAttributeInvocations.find (invoc.get_node_id ());
-  rust_assert (it == procmacroAttributeInvocations.end ());
-
-  procmacroAttributeInvocations[invoc.get_node_id ()] = def;
-}
-
-tl::optional<AttributeProcMacro &>
-ProcMacroMappings::lookup_attribute_invocation (AST::SimplePath &invoc)
-{
-  auto it = procmacroAttributeInvocations.find (invoc.get_node_id ());
-  if (it == procmacroAttributeInvocations.end ())
-    return tl::nullopt;
-
-  return it->second;
-}
-
 void
 Mappings::insert_module_child (NodeId module, NodeId child)
 {
diff --git a/gcc/rust/util/rust-hir-map.h b/gcc/rust/util/rust-hir-map.h
index e22a067dd..4816b648a 100644
--- a/gcc/rust/util/rust-hir-map.h
+++ b/gcc/rust/util/rust-hir-map.h
@@ -162,35 +162,28 @@ public:
     crate_names;
 };
 
-class ProcMacroMappings
+struct ProcMacroDefinitionMappings
 {
-  std::map<NodeId, CustomDeriveProcMacro> procmacroDeriveMappings;
-  std::map<NodeId, BangProcMacro> procmacroBangMappings;
-  std::map<NodeId, AttributeProcMacro> procmacroAttributeMappings;
-  std::map<NodeId, CustomDeriveProcMacro> procmacroDeriveInvocations;
-  std::map<NodeId, BangProcMacro> procmacroBangInvocations;
-  std::map<NodeId, AttributeProcMacro> procmacroAttributeInvocations;
+  Mapping<NodeId, CustomDeriveProcMacro, InsertionPolicy::FORBID_DUPLICATES>
+    derives;
+  Mapping<NodeId, BangProcMacro, InsertionPolicy::FORBID_DUPLICATES> bangs;
+  Mapping<NodeId, AttributeProcMacro, InsertionPolicy::FORBID_DUPLICATES>
+    attributes;
+};
 
-public:
-  void insert_derive_def (CustomDeriveProcMacro macro);
-  void insert_bang_def (BangProcMacro macro);
-  void insert_attribute_def (AttributeProcMacro macro);
-
-  tl::optional<CustomDeriveProcMacro &> lookup_derive_def (NodeId id);
-  tl::optional<BangProcMacro &> lookup_bang_def (NodeId id);
-  tl::optional<AttributeProcMacro &> lookup_attribute_def (NodeId id);
-
-  tl::optional<CustomDeriveProcMacro &>
-  lookup_derive_invocation (AST::SimplePath &invoc);
-  tl::optional<BangProcMacro &>
-  lookup_bang_invocation (AST::MacroInvocation &invoc_id);
-  tl::optional<AttributeProcMacro &>
-  lookup_attribute_invocation (AST::SimplePath &invoc);
-  void insert_derive_invocation (AST::SimplePath &invoc,
-                                CustomDeriveProcMacro def);
-  void insert_bang_invocation (AST::MacroInvocation &invoc, BangProcMacro def);
-  void insert_attribute_invocation (AST::SimplePath &invoc,
-                                   AttributeProcMacro def);
+struct ProcMacroInvocationsMappings
+{
+  Mapping<NodeId, CustomDeriveProcMacro, InsertionPolicy::FORBID_DUPLICATES>
+    derives;
+  Mapping<NodeId, BangProcMacro, InsertionPolicy::FORBID_DUPLICATES> bangs;
+  Mapping<NodeId, AttributeProcMacro, InsertionPolicy::FORBID_DUPLICATES>
+    attributes;
+};
+
+struct ProcMacroMappings
+{
+  ProcMacroDefinitionMappings definitions;
+  ProcMacroInvocationsMappings invocations;
 };
 
 enum class InsertLocation
-- 
2.55.0

Reply via email to