Goose  Artifact [f25bc8ac41]

Artifact f25bc8ac412faa7f8a68e69e5fd406a117e053a19cb17df712b6152559f0f09d:

  • File bs/builtins/types/func/invoke.cpp — part of check-in [ef1e94f44d] at 2019-06-29 11:40:08 on branch trunk — Added a domain specifier in function and template function types. (user: achavasse size: 3488)

#include "builtins/builtins.h"

using namespace empathy::sema;

namespace empathy::builtins
{
    class FunctionInvocationRule : public InvocationRule
    {
        public:
            optional< Value > resolveInvocation( const Context& c, const Value& callee, const Value& args ) const final
            {
                UnificationContext uc( c );

                optional< UnificationContext > bestUC;
                optional< Term > bestSol;
                bool ambiguous = false;
                auto sig = GetFuncSig( callee );

                auto callPat = TVEC( args.val(), MkHole( "_"_sid ) );

                for( auto&& [s, uc] : Unify( sig, callPat, uc ) )
                {
                    if( uc.numUnknownValues() )
                        continue;

                    auto ssol = Substitute( s, uc );
                    if( !ssol )
                        continue;

                    if( !bestSol || uc.score() > bestUC->score() )
                    {
                        bestUC = uc;
                        bestSol = ssol;
                        ambiguous = false;
                        continue;
                    }

                    if( uc.score() < bestUC->score() )
                        continue;

                    ambiguous = true;
                }

                if( ambiguous )
                {
                    // TODO error mgmt
                    cout << "ambiguous function call.\n";
                    return nullopt;
                }

                if( !bestSol )
                {
                    // TODO error mgmt
                    cout << "function arguments mismatch.\n";
                    return nullopt;
                }

                return invoke( c, callee, *bestSol, *bestUC );
            }

            optional< Value > invoke( const Context& c, const Value& callee, const Term& unifiedCallPat, UnificationContext& uc ) const final
            {
                if( !IsBuiltinFunc( callee ) && !CompileFunc( c, callee ) )
                    return nullopt;

                auto ppCallPat = Postprocess( unifiedCallPat, uc );
                if( !ppCallPat )
                    return nullopt;

                auto callDecomp = Decompose( *ppCallPat,
                    Vec(
                        SubTerm(),  // args
                        SubTerm()   // return type
                    )
                );

                auto&& [unifiedArgs, unifiedRType] = *callDecomp;
                return Value( unifiedRType, make_shared< llr::Instruction >( llr::Call( callee, unifiedArgs ) ) );
            }

            optional< Term > getSignature( const Value& callee ) const final
            {
                return GetFuncSig( callee );
            }

            optional< Value > prepareFunc( const Context& c, const Value& callee, const Term& unifiedCallPat, UnificationContext& uc ) const final
            {
                if( !IsBuiltinFunc( callee ) && !CompileFunc( c, callee ) )
                    return nullopt;

                return callee;
            }
    };

    void SetupFunctionInvocationRule( Env& e )
    {
        e.invocationRuleSet()->addRule(
            ValueToIRExpr( ValuePattern( ANYTERM( _ ),
                ValueToIRExpr( Value( TypeType(), TVEC( TSID( func ),
                ANYTERM( _ ), ANYTERM( _ ), ANYTERM( _ ), ANYTERM( _ ) ) ) ),
                ANYTERM( _ ) ) ),
            make_shared< FunctionInvocationRule >() );
    }
}