https://gcc.gnu.org/g:1c0b98dedc14deae83b7887c75415a7ccd295f64

commit r16-2906-g1c0b98dedc14deae83b7887c75415a7ccd295f64
Author: Arthur Cohen <arthur.co...@embecosm.com>
Date:   Tue May 20 14:25:07 2025 +0200

    gccrs: ast: reconstruct: Add base for reconstructing and asserting 
different IDs
    
    gcc/rust/ChangeLog:
    
            * ast/rust-ast.h (reconstruct): New function for calling the 
`reconstruct_*_impl` method
            and asserting that the new NodeId is different, and then wrap it in 
a unique_ptr<T>.
            (reconstruct_vec): Likewise, but for vectors of unique_ptr<T>

Diff:
---
 gcc/rust/ast/rust-ast.h | 36 ++++++++++++++++++++++++++++++++++++
 1 file changed, 36 insertions(+)

diff --git a/gcc/rust/ast/rust-ast.h b/gcc/rust/ast/rust-ast.h
index cd586c6aa7d3..1cc10c90ee85 100644
--- a/gcc/rust/ast/rust-ast.h
+++ b/gcc/rust/ast/rust-ast.h
@@ -83,6 +83,42 @@ public:
   virtual void accept_vis (ASTVisitor &vis) = 0;
 };
 
+/**
+ * Base function for reconstructing and asserting that the new NodeId is
+ * different from the old NodeId. It then wraps the given pointer into a unique
+ * pointer and returns it.
+ */
+template <typename T, typename F>
+std::unique_ptr<T>
+reconstruct (const T *instance, F method)
+{
+  auto *reconstructed = (instance->*method) ();
+
+  rust_assert (reconstructed->get_node_id () != instance->get_node_id ());
+
+  return std::unique_ptr<T> (reconstructed);
+}
+
+/**
+ * Reconstruct multiple items in a vector
+ */
+template <typename T, typename F>
+std::vector<std::unique_ptr<T>>
+reconstruct_vec (const std::vector<std::unique_ptr<T>> &to_reconstruct,
+                F method)
+{
+  std::vector<std::unique_ptr<T>> reconstructed;
+
+  for (const auto &elt : to_reconstruct)
+    {
+      auto new_elt = (elt.get ()->*method) ();
+
+      reconstructed.emplace_back (std::move (new_elt));
+    }
+
+  return reconstructed;
+}
+
 // Delimiter types - used in macros and whatever.
 enum DelimType
 {

Reply via email to