https://gcc.gnu.org/g:f7dd8c5f64305cbfcc3b191c239323f95b720483
commit r17-3061-gf7dd8c5f64305cbfcc3b191c239323f95b720483 Author: Pierre-Emmanuel Patry <[email protected]> Date: Fri May 29 15:24:29 2026 +0200 gccrs: Use AST for metadata export instead of HIR HIR does not contain the UseDeclaration but those are required to resolve some metadata. This commit change the whole metadata export to use the AST data instead. We could have lowered UseDeclaration to HIR instead akin to macro export but the AST dump accuracy is already used by multiple other systems such as proc macro expansion, using it deduplicates the dumper code used. gcc/rust/ChangeLog: * metadata/rust-export-metadata.cc (ExportContext::emit_trait): Use AST node instead. (ExportContext::emit_function): Likewise. (ExportContext::emit_use_declaration): likewise. (ExportContext::begin_module): Likewise. (ExportContext::end_module): Likewise. (class ExportVisItems): Use DefaultASTVisitor instead. (PublicInterface::gather_export_data): Lookup the AST crate and visit its items. * metadata/rust-export-metadata.h: Update function prototys with AST nodes. Signed-off-by: Pierre-Emmanuel Patry <[email protected]> Diff: --- gcc/rust/metadata/rust-export-metadata.cc | 104 ++++++++++++------------------ gcc/rust/metadata/rust-export-metadata.h | 9 +-- 2 files changed, 45 insertions(+), 68 deletions(-) diff --git a/gcc/rust/metadata/rust-export-metadata.cc b/gcc/rust/metadata/rust-export-metadata.cc index 53f436a90615..93574284c523 100644 --- a/gcc/rust/metadata/rust-export-metadata.cc +++ b/gcc/rust/metadata/rust-export-metadata.cc @@ -54,33 +54,32 @@ ExportContext::pop_module_scope () } void -ExportContext::emit_trait (const HIR::Trait &trait) +ExportContext::emit_trait (AST::Trait &trait) { - // lookup the AST node for this - AST::Item *item - = mappings.lookup_ast_item (trait.get_mappings ().get_nodeid ()).value (); - std::stringstream oss; AST::Dump dumper (oss); - dumper.go (*item); + dumper.process (trait); public_interface_buffer += oss.str (); } void -ExportContext::emit_function (const HIR::Function &fn) +ExportContext::emit_use_declaration (AST::UseDeclaration &use_decl) { - // lookup the AST node for this - AST::Item *item - = mappings.lookup_ast_item (fn.get_mappings ().get_nodeid ()).value (); + std::stringstream oss; + AST::Dump dumper (oss); + dumper.process (use_decl); + + public_interface_buffer += oss.str (); +} +void +ExportContext::emit_function (AST::Function &fn) +{ // is this a CFG macro or not - if (item->is_marked_for_strip ()) + if (fn.is_marked_for_strip ()) return; - // FIXME add assertion that item must be a vis_item; - AST::VisItem &vis_item = static_cast<AST::VisItem &> (*item); - // if its a generic function we need to output the full declaration // otherwise we can let people link against this @@ -88,23 +87,19 @@ ExportContext::emit_function (const HIR::Function &fn) AST::Dump dumper (oss); if (!fn.has_generics ()) { - // FIXME assert that this is actually an AST::Function - AST::Function &function = static_cast<AST::Function &> (vis_item); - std::vector<std::unique_ptr<AST::ExternalItem>> external_items; - external_items.emplace_back ( - static_cast<AST::ExternalItem *> (&function)); + external_items.emplace_back (fn.clone_external_item ()); AST::ExternBlock extern_block (get_string_from_abi (Rust::ABI::RUST), std::move (external_items), - vis_item.get_visibility (), {}, {}, + fn.get_visibility (), {}, {}, fn.get_locus ()); dumper.go (extern_block); } else { - dumper.go (*item); + dumper.process (fn); } // store the dump @@ -112,17 +107,18 @@ ExportContext::emit_function (const HIR::Function &fn) } void -ExportContext::begin_module (const HIR::Module &module) +ExportContext::begin_module (const AST::Module &module) { if (module.get_visibility ().is_public ()) public_interface_buffer - += "pub mod " + module.get_module_name ().as_string () + "{\n"; + += "pub mod " + module.get_name ().as_string () + "{\n"; } void -ExportContext::end_module () +ExportContext::end_module (const AST::Module &module) { - public_interface_buffer += "}\n"; + if (module.get_visibility ().is_public ()) + public_interface_buffer += "}\n"; } void @@ -144,43 +140,29 @@ ExportContext::get_interface_buffer () const // implicitly by using HIR nodes we know that these have passed CFG expansion // and they exist in the compilation unit -class ExportVisItems : public HIR::HIRVisItemVisitor +class ExportVisItems : public AST::DefaultASTVisitor { public: + using AST::DefaultASTVisitor::visit; ExportVisItems (ExportContext &context) : ctx (context) {} - void visit (HIR::Module &module) override + void go (AST::Crate &c) { visit (c); } + + void visit (AST::Function &function) override + { + ctx.emit_function (function); + } + void visit (AST::Trait &trait) override { ctx.emit_trait (trait); } + void visit (AST::Module &module) override { ctx.begin_module (module); - for (auto &item : module.get_items ()) - { - bool is_vis_item - = item->get_hir_kind () == HIR::Node::BaseKind::VIS_ITEM; - if (!is_vis_item) - continue; - - HIR::VisItem &vis_item = static_cast<HIR::VisItem &> (*item.get ()); - vis_item.accept_vis (*this); - } - ctx.end_module (); + AST::DefaultASTVisitor::visit (module); + ctx.end_module (module); } - void visit (HIR::ExternCrate &) override {} - void visit (HIR::UseDeclaration &) override {} - void visit (HIR::TypeAlias &) override {} - void visit (HIR::StructStruct &) override {} - void visit (HIR::TupleStruct &) override {} - void visit (HIR::Enum &) override {} - void visit (HIR::Union &) override {} - void visit (HIR::ConstantItem &) override {} - void visit (HIR::StaticItem &) override {} - void visit (HIR::ImplBlock &) override {} - void visit (HIR::ExternBlock &) override {} - - void visit (HIR::Trait &trait) override { ctx.emit_trait (trait); } - - void visit (HIR::Function &function) override + + void visit (AST::UseDeclaration &use_decl) override { - ctx.emit_function (function); + ctx.emit_use_declaration (use_decl); } private: @@ -211,16 +193,10 @@ void PublicInterface::gather_export_data () { ExportVisItems visitor (context); - for (auto &item : crate.get_items ()) - { - bool is_vis_item = item->get_hir_kind () == HIR::Node::BaseKind::VIS_ITEM; - if (!is_vis_item) - continue; - - HIR::VisItem &vis_item = static_cast<HIR::VisItem &> (*item.get ()); - if (is_crate_public (vis_item)) - vis_item.accept_vis (visitor); - } + auto crate_num + = mappings.lookup_crate_num (crate.get_mappings ().get_nodeid ()); + auto &ast_crate = mappings.get_ast_crate (crate_num.value ()); + visitor.go (ast_crate); for (auto ¯o : mappings.get_exported_macros ()) context.emit_macro (macro); diff --git a/gcc/rust/metadata/rust-export-metadata.h b/gcc/rust/metadata/rust-export-metadata.h index 25667c2ea6bb..399b92951757 100644 --- a/gcc/rust/metadata/rust-export-metadata.h +++ b/gcc/rust/metadata/rust-export-metadata.h @@ -40,10 +40,11 @@ public: const HIR::Module &pop_module_scope (); - void emit_trait (const HIR::Trait &trait); - void emit_function (const HIR::Function &fn); - void begin_module (const HIR::Module &module); - void end_module (); + void emit_trait (AST::Trait &trait); + void emit_function (AST::Function &fn); + void emit_use_declaration (AST::UseDeclaration &use_decl); + void begin_module (const AST::Module &module); + void end_module (const AST::Module &module); /** * Macros are a bit particular - they only live at the AST level, so we can
