Goose  Artifact [3ea19ac6e5]

Artifact 3ea19ac6e569c9a5a6a01b4a8f7c20360c6126958d050ce0984c65c78f87dd99:

  • File bs/sema/template.cpp — part of check-in [1c4c7dde4c] at 2019-02-24 15:37:01 on branch trunk — Templates: implemented the template rules for Decl, TDecl, TNamedDecl and TVar. (user: achavasse size: 1148)

#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, tpl );
    }
}