[gcc r17-3069] gccrs: Simplify module and extern block emission

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

commit r17-3069-g30ec03e4caa718048bec6cd16330d8426a1dc5bb
Author: Pierre-Emmanuel Patry <[email protected]>
Date:   Mon Jun 29 13:46:23 2026 +0200

    gccrs: Simplify module and extern block emission
    
    We would have to call two functions, one of which could be forgotten,
    this would also duplicates the condition. This new interface makes
    things a bit simpler.
    
    gcc/rust/ChangeLog:
    
            * metadata/rust-export-metadata.cc (ExportContext::begin_extern_block):
            Delete function.
            (ExportContext::end_extern_block): Likewise.
            (ExportContext::begin_module): Likewise.
            (ExportContext::end_module): Likewise.
            (ExportContext::emit_extern_block): Merge both begin/end within one
            function that calls the visitor in between.
            (ExportContext::emit_module): Likewise.
            * metadata/rust-export-metadata.h: Update function prototypes.
    
    Signed-off-by: Pierre-Emmanuel Patry <[email protected]>

Diff:
---
 gcc/rust/metadata/rust-export-metadata.cc | 37 +++++++++++++------------------
 gcc/rust/metadata/rust-export-metadata.h  |  8 +++----
 2 files changed, 19 insertions(+), 26 deletions(-)

diff --git a/gcc/rust/metadata/rust-export-metadata.cc b/gcc/rust/metadata/rust-export-metadata.cc
index 3388d0e920da..6998319d8575 100644
--- a/gcc/rust/metadata/rust-export-metadata.cc
+++ b/gcc/rust/metadata/rust-export-metadata.cc
@@ -107,30 +107,25 @@ ExportContext::emit_function (AST::Function &fn)
 }
 
 void
-ExportContext::begin_extern_block (AST::ExternBlock &block)
+ExportContext::emit_extern_block (const AST::ExternBlock &block,
+				  std::function<void (void)> sub_visitor)
 {
   public_interface_buffer += "extern \"" + block.get_abi () + "\" {\n";
-}
-
-void
-ExportContext::end_extern_block ()
-{
+  sub_visitor ();
   public_interface_buffer += "}\n";
 }
 
 void
-ExportContext::begin_module (const AST::Module &module)
+ExportContext::emit_module (const AST::Module &module,
+			    std::function<void (void)> sub_visitor)
 {
   if (module.get_visibility ().is_public ())
-    public_interface_buffer
-      += "pub mod " + module.get_name ().as_string () + "{\n";
-}
-
-void
-ExportContext::end_module (const AST::Module &module)
-{
-  if (module.get_visibility ().is_public ())
-    public_interface_buffer += "}\n";
+    {
+      public_interface_buffer
+	+= "pub mod " + module.get_name ().as_string () + "{\n";
+      sub_visitor ();
+      public_interface_buffer += "}\n";
+    }
 }
 
 void
@@ -166,16 +161,14 @@ public:
   }
   void visit (AST::ExternBlock &block) override
   {
-    ctx.begin_extern_block (block);
-    AST::DefaultASTVisitor::visit (block);
-    ctx.end_extern_block ();
+    auto sub_visitor = [&] () { AST::DefaultASTVisitor::visit (block); };
+    ctx.emit_extern_block (block, sub_visitor);
   }
   void visit (AST::Trait &trait) override { ctx.emit_trait (trait); }
   void visit (AST::Module &module) override
   {
-    ctx.begin_module (module);
-    AST::DefaultASTVisitor::visit (module);
-    ctx.end_module (module);
+    auto sub_visitor = [&] () { AST::DefaultASTVisitor::visit (module); };
+    ctx.emit_module (module, sub_visitor);
   }
 
   void visit (AST::UseDeclaration &use_decl) override
diff --git a/gcc/rust/metadata/rust-export-metadata.h b/gcc/rust/metadata/rust-export-metadata.h
index 17dc20a7d5f2..b1e12f2647ff 100644
--- a/gcc/rust/metadata/rust-export-metadata.h
+++ b/gcc/rust/metadata/rust-export-metadata.h
@@ -42,11 +42,11 @@ public:
 
   void emit_trait (AST::Trait &trait);
   void emit_function (AST::Function &fn);
-  void begin_extern_block (AST::ExternBlock &block);
-  void end_extern_block ();
+  void emit_extern_block (const AST::ExternBlock &block,
+			  std::function<void (void)> sub_visitor);
   void emit_use_declaration (AST::UseDeclaration &use_decl);
-  void begin_module (const AST::Module &module);
-  void end_module (const AST::Module &module);
+  void emit_module (const AST::Module &,
+		    std::function<void (void)> sub_visitor);
 
   /**
    * 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.