
class Node;

/** Interface for all AST nodes. */
class Concept : public hilti::util::type_erasure::ConceptBase {
public:
    Concept() = default;
    Concept(const Concept&) = default;
    Concept(Concept&&) = default;
    virtual ~Concept() = default;
    /** Returns any properties associated with the the node. */
    virtual hilti::node::Properties properties() const = 0;
    /** Returns the node's childrens. */
    virtual const std::vector<hilti::Node>& children() const = 0;
    /** Returns the node's childrens. */
    virtual std::vector<hilti::Node>& children()  = 0;
    /** Returns the node's meta data. */
    virtual const Meta& meta() const = 0;
    /** Sets the node's meta data. */
    virtual void setMeta(Meta m)  = 0;
    /** Returns true if AST walking is not to visit the nodes children. */
    virtual bool pruneWalk() const = 0;
    Concept& operator=(const Concept&) = default;
    Concept& operator=(Concept&&) = default;
    virtual Node _clone() const = 0;
    virtual hilti::IntrusivePtr<Concept> _clone_ptr() const = 0;
private:
    
};

template<typename T>
using ModelBase = hilti::util::type_erasure::ModelBase<T, Concept>;

template<class T>
class Model : public ModelBase<T> {
public:
    using ModelBase<T>::ModelBase;
    using ModelBase<T>::data;

    hilti::node::Properties properties() const final;
    const std::vector<hilti::Node>& children() const final;
    std::vector<hilti::Node>& children()  final;
    const Meta& meta() const final;
    void setMeta(Meta m)  final;
    bool pruneWalk() const final;
    Node _clone() const final;
    hilti::IntrusivePtr<Concept> _clone_ptr() const final;
};

using ErasedBase = hilti::util::type_erasure::ErasedBase<trait::isNode, Concept, Model>;

class Node : public ErasedBase {
public:
    using ErasedBase::ErasedBase;

    Node() noexcept = default;
    Node(Node&&) noexcept = default;
    Node& operator=(Node&&) = default;
    ~Node() override = default;
    Node(const Node& other) {
        if ( auto x = other._clone_ptr() )
            ErasedBase::operator=(x);
        else
            ErasedBase::operator=(other.data());
    }

    Node& operator=(const Node& other) {
        if ( this != &other ) {
            if ( auto x = other._clone_ptr() )
                ErasedBase::operator=(x);
            else
                ErasedBase::operator=(other.data());
        }

        return *this;
    }

    hilti::node::Properties properties() const;
    const std::vector<hilti::Node>& children() const;
    std::vector<hilti::Node>& children() ;
    const Meta& meta() const;
    void setMeta(Meta m) ;
    bool pruneWalk() const;
    Node _clone() const;

    hilti::IntrusivePtr<Concept> _clone_ptr() const;

protected:
    
};

template<typename T>
hilti::node::Properties Model<T>::properties() const { return data().properties(); }

template<typename T>
const std::vector<hilti::Node>& Model<T>::children() const { return data().children(); }

template<typename T>
std::vector<hilti::Node>& Model<T>::children()  { return data().children(); }

template<typename T>
const Meta& Model<T>::meta() const { return data().meta(); }

template<typename T>
void Model<T>::setMeta(Meta m)  { return data().setMeta(m); }

template<typename T>
bool Model<T>::pruneWalk() const { return data().pruneWalk(); }

template<typename T>
Node Model<T>::_clone() const { return T(data()); }

template<typename T>
hilti::IntrusivePtr<Concept> Model<T>::_clone_ptr() const {
    if constexpr ( std::is_base_of<hilti::util::type_erasure::trait::Singleton, T>::value )
        return nullptr;
    else
        return hilti::make_intrusive<Model>(T(data()));
}

inline hilti::node::Properties Node::properties() const { return data()->properties(); }

inline const std::vector<hilti::Node>& Node::children() const { return data()->children(); }

inline std::vector<hilti::Node>& Node::children()  { return data()->children(); }

inline const Meta& Node::meta() const { return data()->meta(); }

inline void Node::setMeta(Meta m)  { return data()->setMeta(std::move(m)); }

inline bool Node::pruneWalk() const { return data()->pruneWalk(); }

inline Node Node::_clone() const { return data() ? data()->_clone() : Node(); }

inline hilti::IntrusivePtr<Concept> Node::_clone_ptr() const {
    if ( data() )
        return data()->_clone_ptr();
    else
        return nullptr;
}



