[COMMITTED 02/77] gccrs: Emit modules within metadata export file

[email protected]
Newsgroups gmane.comp.gcc.rust,gmane.comp.gcc.patches
Message-ID <[email protected]>
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]>
---
 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 25f5f255ccb..53f436a9061 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 b35ff9db831..25667c2ea6b 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.50.1
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.