Goose  Artifact [949a7c2d6f]

Artifact 949a7c2d6fe7fbb7a8e6702247437d556dd200367c80790f0689d510b853437b:

  • File bs/builtins/types/overloadset/helpers.cpp — part of check-in [4d4d76607a] at 2023-03-05 22:24:05 on branch trunk —
    • removed fmtlib, now use std::format
    • vastly improved the overload resolution debugging tools
    • prelude: started adding some extra error detection for small things that don't need to be builtin
    • prelude: started working on implementing #for for tuples, some stuff needs to be addressed still
    • fixed some type checking issue with wrapped args
    • fixed superfluous nested lifetime scope in brace blocks (which caused redundant calls to some extension points)
    (user: zlodo size: 3363)

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

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

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

        Term result;

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

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

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

        return nullptr;
    }

    ptr< OverloadSet > GetOrCreateOverloadSet( Env& env, StringId name )
    {
        auto identity = AppendToVectorTerm( RootG0Identity(), TERM( name ) );

        Term result;

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

            case sema::Env::Status::NoMatch:
            {
                auto pOvlSet = make_shared< OverloadSet >( identity );
                env.storeValue( identity, ANYTERM( _ ), ValueToEIR( ToValue( pOvlSet ) ) );
                return pOvlSet;
            }

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

        return nullptr;
    }

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

        Context localC( c.env(), c.identity(), GetValueType< uint32_t >() );
        localC.setBuilder( c.builder() );

        execute::VM vm;

        if( !args.isConstant() && cir::CanValueBeEagerlyEvaluated( args ) )
            args = execute::Evaluate( args, vm );
        if( args.isPoison() )
            return PoisonValue();

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

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

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

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

        execute::VM vm;

        if( !args.isConstant() && cir::CanValueBeEagerlyEvaluated( args ) )
            args = execute::Evaluate( args, vm );
        if( args.isPoison() )
            return PoisonValue();

        auto val = ResolveInvocation( c, GetOverloadSetInvocationRule(), ToValue( pOvlSet ).setLocationId( loc ), args );

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

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