Goose  Artifact [016a571130]

Artifact 016a5711303e3449bca2e3e2f18dfbe54439ab18c25ee348d3d870df3ba0cd6b:

  • File bs/builtins/types/overloadset/helpers.cpp — part of check-in [35a5fd236f] at 2019-08-17 20:46:39 on branch trunk — Implemented compound assignment operators. (user: achavasse size: 1718)

#include "builtins/builtins.h"
#include "execute/execute.h"

namespace empathy::builtins
{
    ptr< OverloadSet > CreateOverloadSet( Env& env, const StringId& name )
    {
        auto identity = AppendToVectorTerm( RootIdentity(), TERM( name ) );
        auto pOvlSet = make_shared< OverloadSet >( identity );
        env.storeValue( identity, ANYTERM( _ ), ValueToIRExpr( ToValue( pOvlSet ) ) );
        return pOvlSet;
    }

    ptr< OverloadSet > GetOverloadSet( Env& env, const StringId& name )
    {
        auto identity = AppendToVectorTerm( RootIdentity(), TERM( name ) );

        Term result;

        switch( env.retrieveValue( identity, RootIdentity(), result ) )
        {
            case sema::Env::Status::Success:
                return *FromValue< ptr< OverloadSet > >( *ValueFromIRExpr( result ) );

            case sema::Env::Status::NoMatch:
                throw logic_error( format( "fatal: overload set {} not found", name ) );

            case sema::Env::Status::AmbiguousMatch:
                throw logic_error( format( "fatal: ambiguous match for overload set {}", name ) );
        }

        return nullptr;
    }

    Value InvokeOverloadSet( const Context& c, const ptr< OverloadSet >& pOvlSet, const Value& args )
    {
        assert( pOvlSet );

        Context localC( make_shared< Env >( *c.env() ), InjectDomainIntoIdentity( c.identity(), DomainCompileTime() ), GetValueType< uint32_t >() );

        auto val = ResolveInvocation( localC, GetOverloadSetInvocationRule(), ToValue( pOvlSet ), args );

        if( val.isConstant() || !llr::CanValueBeEagerlyEvaluated( val ) )
            return val;

        execute::VM vm;
        return execute::Evaluate( val, vm );
    }
}