[gcc r14-7395] gccrs: mappings: Keep exported macro IDs

Arthur Cohen cohenarthur@gcc.gnu.org
Tue Jan 16 17:31:02 GMT 2024


https://gcc.gnu.org/g:848bbbd7735e07fbbdacd3311127b56b0a2b2616

commit r14-7395-g848bbbd7735e07fbbdacd3311127b56b0a2b2616
Author: Arthur Cohen <arthur.cohen@embecosm.com>
Date:   Wed Mar 15 17:30:25 2023 +0100

    gccrs: mappings: Keep exported macro IDs
    
    gcc/rust/ChangeLog:
    
            * hir/rust-ast-lower-item.cc (ASTLoweringItem::visit): Add new visitor
            for AST::MacroRulesDefinition.
            * hir/rust-ast-lower-item.h: Declare above mentioned visitor.
            * metadata/rust-export-metadata.cc (ExportContext::emit_macro): New function.
            * metadata/rust-export-metadata.h: Declare it.
            (PublicInterface::gather_export_data): Go through each exported macro.
            * util/rust-hir-map.cc (Mappings::insert_exported_macro): New function.
            (Mappings::get_exported_macros): New function.
            * util/rust-hir-map.h: Add new mappings for exported macros.

Diff:
---
 gcc/rust/hir/rust-ast-lower-item.cc       |  8 ++++++++
 gcc/rust/hir/rust-ast-lower-item.h        |  1 +
 gcc/rust/metadata/rust-export-metadata.cc | 18 ++++++++++++++++++
 gcc/rust/metadata/rust-export-metadata.h  |  8 +++++++-
 gcc/rust/util/rust-hir-map.cc             | 12 ++++++++++++
 gcc/rust/util/rust-hir-map.h              |  4 ++++
 6 files changed, 50 insertions(+), 1 deletion(-)

diff --git a/gcc/rust/hir/rust-ast-lower-item.cc b/gcc/rust/hir/rust-ast-lower-item.cc
index da828079d2d..1230fa1ef6c 100644
--- a/gcc/rust/hir/rust-ast-lower-item.cc
+++ b/gcc/rust/hir/rust-ast-lower-item.cc
@@ -705,6 +705,14 @@ ASTLoweringItem::visit (AST::ExternBlock &extern_block)
   translated = lower_extern_block (extern_block);
 }
 
+void
+ASTLoweringItem::visit (AST::MacroRulesDefinition &def)
+{
+  for (const auto &attr : def.get_outer_attrs ())
+    if (attr.get_path ().as_string () == "macro_export")
+      mappings->insert_exported_macro (def);
+}
+
 HIR::SimplePath
 ASTLoweringSimplePath::translate (const AST::SimplePath &path)
 {
diff --git a/gcc/rust/hir/rust-ast-lower-item.h b/gcc/rust/hir/rust-ast-lower-item.h
index 300f7e15091..0b66084cf4d 100644
--- a/gcc/rust/hir/rust-ast-lower-item.h
+++ b/gcc/rust/hir/rust-ast-lower-item.h
@@ -44,6 +44,7 @@ public:
   void visit (AST::Trait &trait) override;
   void visit (AST::TraitImpl &impl_block) override;
   void visit (AST::ExternBlock &extern_block) override;
+  void visit (AST::MacroRulesDefinition &rules_def) override;
 
 private:
   ASTLoweringItem () : translated (nullptr) {}
diff --git a/gcc/rust/metadata/rust-export-metadata.cc b/gcc/rust/metadata/rust-export-metadata.cc
index 7b50a92586a..ea251f8f36a 100644
--- a/gcc/rust/metadata/rust-export-metadata.cc
+++ b/gcc/rust/metadata/rust-export-metadata.cc
@@ -144,6 +144,21 @@ ExportContext::emit_function (const HIR::Function &fn)
   public_interface_buffer += oss.str ();
 }
 
+void
+ExportContext::emit_macro (NodeId macro)
+{
+  std::stringstream oss;
+  AST::Dump dumper (oss);
+
+  AST::Item *item;
+  auto ok = mappings->lookup_ast_item (macro, &item);
+  rust_assert (ok);
+
+  dumper.go (*item);
+
+  public_interface_buffer += oss.str ();
+}
+
 const std::string &
 ExportContext::get_interface_buffer () const
 {
@@ -215,6 +230,9 @@ PublicInterface::gather_export_data ()
       if (is_crate_public (vis_item))
 	vis_item.accept_vis (visitor);
     }
+
+  for (const auto &macro : mappings.get_exported_macros ())
+    context.emit_macro (macro);
 }
 
 void
diff --git a/gcc/rust/metadata/rust-export-metadata.h b/gcc/rust/metadata/rust-export-metadata.h
index c57eb9596a2..d87f9bec133 100644
--- a/gcc/rust/metadata/rust-export-metadata.h
+++ b/gcc/rust/metadata/rust-export-metadata.h
@@ -41,9 +41,15 @@ public:
   const HIR::Module &pop_module_scope ();
 
   void emit_trait (const HIR::Trait &trait);
-
   void emit_function (const HIR::Function &fn);
 
+  /**
+   * Macros are a bit particular - they only live at the AST level, so we can
+   * directly refer to them using their NodeId. There's no need to keep an HIR
+   * node for them.
+   */
+  void emit_macro (NodeId macro);
+
   const std::string &get_interface_buffer () const;
 
 private:
diff --git a/gcc/rust/util/rust-hir-map.cc b/gcc/rust/util/rust-hir-map.cc
index 2a731fe9881..8d001fd8241 100644
--- a/gcc/rust/util/rust-hir-map.cc
+++ b/gcc/rust/util/rust-hir-map.cc
@@ -941,6 +941,18 @@ Mappings::lookup_macro_invocation (AST::MacroInvocation &invoc,
   return true;
 }
 
+void
+Mappings::insert_exported_macro (AST::MacroRulesDefinition &def)
+{
+  exportedMacros.emplace_back (def.get_node_id ());
+}
+
+std::vector<NodeId> &
+Mappings::get_exported_macros ()
+{
+  return exportedMacros;
+}
+
 void
 Mappings::insert_visibility (NodeId id, Privacy::ModuleVisibility visibility)
 {
diff --git a/gcc/rust/util/rust-hir-map.h b/gcc/rust/util/rust-hir-map.h
index 80d753cce36..21d1bc0622a 100644
--- a/gcc/rust/util/rust-hir-map.h
+++ b/gcc/rust/util/rust-hir-map.h
@@ -279,6 +279,9 @@ public:
   bool lookup_macro_invocation (AST::MacroInvocation &invoc,
 				AST::MacroRulesDefinition **def);
 
+  void insert_exported_macro (AST::MacroRulesDefinition &def);
+  std::vector<NodeId> &get_exported_macros ();
+
   void insert_visibility (NodeId id, Privacy::ModuleVisibility visibility);
   bool lookup_visibility (NodeId id, Privacy::ModuleVisibility &def);
 
@@ -350,6 +353,7 @@ private:
   // macros
   std::map<NodeId, AST::MacroRulesDefinition *> macroMappings;
   std::map<NodeId, AST::MacroRulesDefinition *> macroInvocations;
+  std::vector<NodeId> exportedMacros;
 
   // crate names
   std::map<CrateNum, std::string> crate_names;


More information about the Gcc-cvs mailing list