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

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]>
---
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/7debe62624f996f8f94ff6d75e2ac45f9b632c98

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 | 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 3388d0e92..6998319d8 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 17dc20a7d..b1e12f264 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
-- 
2.54.0

Reply via email to