#include "builtins/builtins.h"

using namespace empathy::builtins;

namespace empathy::ir
{
    Term Bridge< LocalVar >::Type( const Term& type )
    {
        return ValueToIRExpr( Value( TypeType(), TVEC( TSID( local_var ), type ) ) );
    }

    Value Bridge< LocalVar >::ToValue( const LocalVar& d )
    {
        return Value( Type( d.type() ), TVEC( TERM( d.cfgId() ), TERM( d.index() ) ) );
    }

    optional< LocalVar > Bridge< LocalVar >::FromValue( const Value& v )
    {
        auto typeVal = ValueFromIRExpr( v.type() );
        auto result = Decompose( typeVal->val(),
            Vec(
                Lit( "local_var"_sid ),
                SubTerm()
            )
        );

        if( !result )
            return nullopt;

        auto valResult = Decompose( v.val(),
            Vec(
                Val< uint64_t >(),
                Val< uint64_t >()
            )
        );

        if( !valResult )
            return nullopt;

        auto&& [type] = *result;
        auto&& [cfgId,index] = *valResult;

        return LocalVar( move( type ), cfgId, index );
    }
}
