From 848bbbd7735e07fbbdacd3311127b56b0a2b2616 Mon Sep 17 00:00:00 2001 From: Arthur Cohen Date: Wed, 15 Mar 2023 17:30:25 +0100 Subject: 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. --- 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(-) (limited to 'gcc') diff --git a/gcc/rust/hir/rust-ast-lower-item.cc b/gcc/rust/hir/rust-ast-lower-item.cc index da82807..1230fa1 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 300f7e15..0b66084 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 7b50a92..ea251f8 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 c57eb95..d87f9be 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 2a731fe..8d001fd 100644 --- a/gcc/rust/util/rust-hir-map.cc +++ b/gcc/rust/util/rust-hir-map.cc @@ -942,6 +942,18 @@ Mappings::lookup_macro_invocation (AST::MacroInvocation &invoc, } 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) { visibility_map.insert ({id, visibility}); diff --git a/gcc/rust/util/rust-hir-map.h b/gcc/rust/util/rust-hir-map.h index 80d753c..21d1bc0 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; -- cgit v1.1