From: Arthur Cohen <arthur.co...@embecosm.com> 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 | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/gcc/rust/ast/rust-ast.h b/gcc/rust/ast/rust-ast.h index cd586c6aa7d..1cc10c90ee8 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 { -- 2.49.0