From: Pierre-Emmanuel Patry <[email protected]>

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]>
---
This change was merged into the gccrs repository and is posted here for
upstream visibility and potential drive-by review, as requested by GCC
release managers.
Each commit email contains a link to its details on github from where you can
find the Pull-Request and associated discussions.


Commit on github: 
https://github.com/Rust-GCC/gccrs/commit/b23f599e3fff1983c41fd221ec54cc2b3694f13d

The commit has NOT been mentioned in any issue.

The commit has been mentioned in the following pull-request(s):
 - https://github.com/Rust-GCC/gccrs/pull/4588

 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 25f5f255c..53f436a90 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 b35ff9db8..25667c2ea 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
-- 
2.54.0

Reply via email to