Goose  Artifact [b9fbcb88bd]

Artifact b9fbcb88bdb91caaece87037dabb6e769ef9e1cc24c0fd84caa4d0ab64e2ff7d:

  • File bs/sema/template.cpp — part of check-in [114cdaf51b] at 2019-02-22 21:43:18 on branch trunk — Templates: implemented BuildTFuncType() and BuildTFunc(). (user: achavasse size: 1154)

#include "sema.h"

namespace empathy::sema
{
    ptr< TemplateRule > GetTemplateRuleSet( const Context& c, const Value& tpl )
    {
        const auto& rules = c.env()->templateRuleSet()->rules();

        MatchSolution bestSol;
        ptr< TemplateRule > pBestRule;
        bool ambiguous = false;

        for( auto&& [s, rule] : Match( tpl.type(), rules ) )
        {
            if( !pBestRule || s > bestSol )
            {
                bestSol = s;
                pBestRule = rule;
                ambiguous = false;
                continue;
            }

            if( s < bestSol )
                continue;

            ambiguous = true;
        }

        // Let's not really worry about how to properly report this for now.
        if( ambiguous )
            throw runtime_error( "ambiguous template rule" );

        return pBestRule;
    }

    optional< Term > BuildTemplateSignature( const Context& c, const Value& tpl )
    {
        const auto pTemplateRuleSet = GetTemplateRuleSet( c, tpl );
        if( !pTemplateRuleSet )
            return nullopt;

        return pTemplateRuleSet->buildSignature( c.env(), tpl );
    }
}