gccrs icon indicating copy to clipboard operation
gccrs copied to clipboard

AST: Add `reconstruct_type()` method

Open CohenArthur opened this issue 7 months ago • 3 comments

  • ast: reconstruct: Add base for reconstructing and asserting different IDs

  • ast: Add reconstruct_type() method

  • ast: builder: Remove ASTTypeBuilder

    This is the first step of adding the reconstruct_*() methods as discussed on Zulip. The goal is to have something similar to clone_*(), but each time reconstructing the node and assigning it a different NodeID - and modifying clone_*() where necessary to ensure the node ID stays the same. Subsequently, this also deletes the ASTTypeBuilder visitor as it is replaced by simple calls to .reconstruct_type() instead.

CohenArthur avatar May 20 '25 13:05 CohenArthur

TLDR, it looks like you can override functions with functions that return a different type, if you're not overriding a virtual function

https://godbolt.org/z/TW1rjbMba

#include <memory>

class A
{
    public:
    std::unique_ptr<A> clone()
    {
        return std::make_unique<A> ();
    }
};

class B: public A
{
    public:
    std::unique_ptr<B> clone()
    {
        return std::make_unique<B> ();
    }
};

int main() {
    A w;
    std::unique_ptr<A> x = w.clone ();
    B y;
    std::unique_ptr<A> z = y.clone ();
}

So clone_* methods shouldn't need different names, especially since std::unique_ptr has a constructor that can do upcasting

powerboat9 avatar May 20 '25 18:05 powerboat9

The "^^^" comment was supposed to be a reply to https://github.com/Rust-GCC/gccrs/pull/3799#discussion_r2098520527

powerboat9 avatar May 23 '25 15:05 powerboat9

We could use deducing-this to great effect here, but that's a C++23 thing (GCC 14)

powerboat9 avatar Jun 07 '25 21:06 powerboat9

Looks like auto-merge didn't work

powerboat9 avatar Jun 20 '25 17:06 powerboat9