Goose  Artifact [5392146526]

Artifact 5392146526221f2d4fcef0d260f5b92c24d25eecd9f29eba0f391e1141b5d0b0:

  • File bs/builtins/types/func/invoke.cpp — part of check-in [88099a9cee] at 2019-03-15 19:18:16 on branch trunk — Overloading: added tests for, and fixed utrie unification. (user: achavasse size: 3002)

#include "builtins/builtins.h"

using namespace empathy::sema;

namespace empathy::builtins
{
    class FunctionInvocationRule : public InvocationRule
    {
        public:
            virtual optional< Value > resolveInvocation( const Context& c, const Value& callee, const Value& args ) const final
            {
                const auto& env = c.env();

                if( !IsBuiltinFunc( callee ) )
                    PerformLazyFuncParsing( env, callee );

                UnificationContext uc( env );

                optional< UnificationContext > bestUC;
                optional< Term > bestSol;
                bool ambiguous = false;
                auto&& [sig, rtype] = GetFuncSigAndRType( callee );

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

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

                    if( !bestSol || uc.score() > bestUC->score() )
                    {
                        bestUC = uc;
                        bestSol = s;
                        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 resolveInvocation( c, callee, *bestSol, *bestUC );
            }

            optional< Value > resolveInvocation( const Context& c, const Value& callee, const Term& uniSol, UnificationContext& uc ) const final
            {
                auto unifiedCallPat = Substitute( uniSol, uc );
                auto callDecomp = Decompose( unifiedCallPat,
                    Vec(
                        SubTerm(),  // args
                        SubTerm()   // return type
                    )
                );

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

            virtual Term getSignature( const Context& c, const Value& callee ) const final
            {
                return GetFuncSigAndRType( callee ).first;
            }
    };

    void SetupFunctionInvocationRule( Env& e )
    {
        e.invocationRuleSet()->addRule(
            ValueToIRExpr( Value( TSID( type ), TVEC( TSID( func ),
                ANYTERM( k ), ANYTERM( rt ), ANYTERM( p ), ANYTERM( sig ) ) ) ),
                make_shared< FunctionInvocationRule >() );
    }
}