#include "cir/cir.h"

namespace goose::cir
{
    bool Instruction::canBeExecuted() const
    {
        return visit( []< typename ET >( const ET& e )
        {
            if constexpr( is_same_v< ET, monostate > )
                return false;
            else
                return e.canBeExecuted();
        }, m_content );
    }

    bool Instruction::canBeEagerlyEvaluated() const
    {
        return visit( []< typename ET >( const ET& e )
        {
            if constexpr( is_same_v< ET, monostate > )
                return false;
            else
                return e.canBeEagerlyEvaluated();
        }, m_content );
    }

    bool Instruction::haveSideEffects() const
    {
        return visit( []< typename ET >( const ET& e )
        {
            if constexpr( is_same_v< ET, monostate > )
                return false;
            else
                return e.haveSideEffects();
        }, m_content );
    }

    ostream& operator<<( ostream& out, const Instruction& inst )
    {
        return visit( [&]< typename ET >( const ET& e ) -> ostream&
        {
            if constexpr( is_same_v< ET, monostate > )
                return out << "NIL";
            else if constexpr( is_same_v< ET, ptr< CFG > > )
                return out << "NESTED_CFG";
            else
                return out << e;
        }, inst.m_content );
    }

    ostream& operator<<( ostream& out, const Select& ins )
    {
        out << "SELECT(" << ins.m_memberIndex;
         return out << ')';
    }

    ostream& operator<<( ostream& out, const Load& ins )
    {
        out << "LOAD(";
         return out << ins.m_type << ')';
    }

    ostream& operator<<( ostream& out, const Store& ins )
    {
        return out << "STORE";
    }

    ostream& operator<<( ostream& out, const PHOverride& ins )
    {
        return out << "PHOVERRIDE(" << ins.m_name << ", " << ins.m_phVal << ')';
    }

    bool Select::operator<( const Select& rhs ) const
    {
        return m_memberIndex < rhs.m_memberIndex;
    }

    bool Load::operator<( const Load& rhs ) const
    {
        return m_type < rhs.m_type;
    }

    bool Store::operator<( const Store& rhs ) const
    {
        return m_type < rhs.m_type;
    }

    bool PHOverride::operator<( const PHOverride& rhs ) const
    {
        if( m_name != rhs.m_name )
            return m_name < rhs.m_name;
        return m_phVal < rhs.m_phVal;
    }
}
