]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: ast: reconstruct: Add base for reconstructing and asserting different IDs
authorArthur Cohen <arthur.cohen@embecosm.com>
Tue, 20 May 2025 12:25:07 +0000 (14:25 +0200)
committerArthur Cohen <arthur.cohen@embecosm.com>
Tue, 5 Aug 2025 14:36:52 +0000 (16:36 +0200)
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>

gcc/rust/ast/rust-ast.h

index cd586c6aa7d3907df6ae8570ce3c50b5c482dc13..1cc10c90ee85f341a34c27aeab57887e9c1ab758 100644 (file)
@@ -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
 {