Goose  Artifact [5446b39bc5]

Artifact 5446b39bc54095261d861a4c8abbb6326510bb10600bc74ac36bf1ae7ad4f27e:

  • File bs/builtins/types/func/invoke.cpp — part of check-in [12c9e92618] at 2019-02-24 23:18:18 on branch trunk — Templates: implemented the template function invocation rule. Some pieces are still missing before it can work. (user: achavasse size: 3125)

#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] = getSignatureAndRType( env, callee );

                for( auto&& [s, uc] : Unify( sig, args.val(), uc ) )
                {
                    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;
                }

                auto unifiedArgs = Substitute( *bestSol, *bestUC );
                return Value( rtype, make_shared< llr::Element >( llr::Call( callee, unifiedArgs ) ) );
            }

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

        private:
            pair< Term, Term > getSignatureAndRType( const ptr< Env >& env, const Value& callee ) const
            {
                // Extract the signature from the type
                auto valType = ValueFromIRExpr( callee.type() );
                assert( valType );

                auto decomp = Decompose( valType->val(),
                    Vec(
                        Lit( "func"_sid ),
                        SubTerm(),  // kind
                        SubTerm(),  // return type
                        SubTerm(),  // param types
                        SubTerm()   // signature
                    )
                );

                assert( decomp );
                auto&& [kind, rtype, ptypes, sig] = *decomp;
                return { sig, rtype };
            }
    };

    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 >() );
    }
}