From: Arthur Cohen Date: Tue, 20 May 2025 12:25:07 +0000 (+0200) Subject: gccrs: ast: reconstruct: Add base for reconstructing and asserting different IDs X-Git-Url: http://git.ipfire.org/gitweb/gitweb.cgi?a=commitdiff_plain;h=1c0b98dedc14deae83b7887c75415a7ccd295f64;p=thirdparty%2Fgcc.git 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. (reconstruct_vec): Likewise, but for vectors of unique_ptr --- 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 +std::unique_ptr +reconstruct (const T *instance, F method) +{ + auto *reconstructed = (instance->*method) (); + + rust_assert (reconstructed->get_node_id () != instance->get_node_id ()); + + return std::unique_ptr (reconstructed); +} + +/** + * Reconstruct multiple items in a vector + */ +template +std::vector> +reconstruct_vec (const std::vector> &to_reconstruct, + F method) +{ + std::vector> 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 {