[gcc r17-3060] gccrs: Emit modules within metadata export file

Arthur Cohen via Gcc-cvs <[email protected]>
Newsgroups gmane.comp.gcc.cvs
Message-ID <[email protected]>
https://gcc.gnu.org/g:53452c3eb2da9e3a846e20de8f3c37672232de5b

commit r17-3060-g53452c3eb2da9e3a846e20de8f3c37672232de5b
Author: Pierre-Emmanuel Patry <[email protected]>
Date:   Sun May 24 23:26:54 2026 +0200

    gccrs: Emit modules within metadata export file
    
    Modules were not emitted within metadata files, and could not be resolved
    upon crate import.
    
    gcc/rust/ChangeLog:
    
            * metadata/rust-export-metadata.cc (ExportContext::begin_module):
            Add a function to emit the module preamble.
            (ExportContext::end_module): Add function to emit public module brace
            suffix.
            * metadata/rust-export-metadata.h: Add function prototypes.
    
    Signed-off-by: Pierre-Emmanuel Patry <[email protected]>

Diff:
---
 gcc/rust/metadata/rust-export-metadata.cc | 30 +++++++++++++++++++++++++++++-
 gcc/rust/metadata/rust-export-metadata.h  |  2 ++
 2 files changed, 31 insertions(+), 1 deletion(-)

diff --git a/gcc/rust/metadata/rust-export-metadata.cc b/gcc/rust/metadata/rust-export-metadata.cc
index 25f5f255ccbc..53f436a90615 100644
--- a/gcc/rust/metadata/rust-export-metadata.cc
+++ b/gcc/rust/metadata/rust-export-metadata.cc
@@ -111,6 +111,20 @@ ExportContext::emit_function (const HIR::Function &fn)
   public_interface_buffer += oss.str ();
 }
 
+void
+ExportContext::begin_module (const HIR::Module &module)
+{
+  if (module.get_visibility ().is_public ())
+    public_interface_buffer
+      += "pub mod " + module.get_module_name ().as_string () + "{\n";
+}
+
+void
+ExportContext::end_module ()
+{
+  public_interface_buffer += "}\n";
+}
+
 void
 ExportContext::emit_macro (AST::MacroRulesDefinition &macro)
 {
@@ -135,7 +149,21 @@ class ExportVisItems : public HIR::HIRVisItemVisitor
 public:
   ExportVisItems (ExportContext &context) : ctx (context) {}
 
-  void visit (HIR::Module &) override {}
+  void visit (HIR::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 ();
+  }
   void visit (HIR::ExternCrate &) override {}
   void visit (HIR::UseDeclaration &) override {}
   void visit (HIR::TypeAlias &) override {}
diff --git a/gcc/rust/metadata/rust-export-metadata.h b/gcc/rust/metadata/rust-export-metadata.h
index b35ff9db8315..25667c2ea6bb 100644
--- a/gcc/rust/metadata/rust-export-metadata.h
+++ b/gcc/rust/metadata/rust-export-metadata.h
@@ -42,6 +42,8 @@ public:
 
   void emit_trait (const HIR::Trait &trait);
   void emit_function (const HIR::Function &fn);
+  void begin_module (const HIR::Module &module);
+  void end_module ();
 
   /**
    * Macros are a bit particular - they only live at the AST level, so we can
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.