From: Arthur Cohen Date: Wed, 15 Mar 2023 16:30:25 +0000 (+0100) Subject: gccrs: mappings: Keep exported macro IDs X-Git-Tag: basepoints/gcc-15~2744 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=848bbbd7735e07fbbdacd3311127b56b0a2b2616;p=thirdparty%2Fgcc.git 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 --git a/gcc/rust/hir/rust-ast-lower-item.cc b/gcc/rust/hir/rust-ast-lower-item.cc index da828079d2dc..1230fa1ef6c2 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 300f7e15091b..0b66084cf4da 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 7b50a92586a1..ea251f8f36a0 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 ¯o : 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 c57eb9596a23..d87f9bec1332 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 2a731fe98818..8d001fd8241f 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 & +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 80d753cce363..21d1bc0622a7 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 &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 macroMappings; std::map macroInvocations; + std::vector exportedMacros; // crate names std::map crate_names;