]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: mappings: Keep exported macro IDs
authorArthur Cohen <arthur.cohen@embecosm.com>
Wed, 15 Mar 2023 16:30:25 +0000 (17:30 +0100)
committerArthur Cohen <arthur.cohen@embecosm.com>
Tue, 16 Jan 2024 17:28:38 +0000 (18:28 +0100)
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.

gcc/rust/hir/rust-ast-lower-item.cc
gcc/rust/hir/rust-ast-lower-item.h
gcc/rust/metadata/rust-export-metadata.cc
gcc/rust/metadata/rust-export-metadata.h
gcc/rust/util/rust-hir-map.cc
gcc/rust/util/rust-hir-map.h

index da828079d2dc7e0e1da17b26712a53a6c7d9e469..1230fa1ef6c2a2f8444e97607021067691e6ad8c 100644 (file)
@@ -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)
 {
index 300f7e15091b7402b43d3b55fc2f31bf32a41bdd..0b66084cf4dac5c561ad7510b732e27c9c27fc1e 100644 (file)
@@ -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) {}
index 7b50a92586a142091d4908db12fc6aa702bbaa8c..ea251f8f36a0cfc9c6a02d80c14c58f180c7ae94 100644 (file)
@@ -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
index c57eb9596a23d83b10b2f516591f2a9506ef6e6f..d87f9bec13329201e4315c3f653bea49161a38d4 100644 (file)
@@ -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:
index 2a731fe98818d2efd6ba19e0cd0adbcb85e72ff6..8d001fd8241f755d9988f4e41ee6119180a0de56 100644 (file)
@@ -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)
 {
index 80d753cce3638f23d012a6e2aa93b02b12a744eb..21d1bc0622a733de698adb698498da90cb0ebbb3 100644 (file)
@@ -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;