Goose  Artifact [cafbddd3cd]

Artifact cafbddd3cdaffb13e37739836cd54cb106d9ce1160c4a3538d7b78b1cbc47399:

  • File bs/builtins/types/template/build.cpp — part of check-in [7b9f645074] at 2021-01-21 21:05:48 on branch trunk — Removed the vector "typechecking rule", whose existence made no sense. Typechecking rules should operate only on values and pattern of values. Typechecking multiple values against multiple params is now done through a specific function instead. (user: achavasse size: 3504)

#include "builtins/builtins.h"

namespace goose::builtins
{
    optional< TDecl > BuildTDecl( const Context& c, const Term& typeTExpr, const StringId& name )
    {
        auto typeSig = BuildTemplateSignature( c, typeTExpr );
        if( !typeSig )
            return nullopt;

        return TDecl( *typeSig, name );
    }

    TFuncType BuildTFuncType( const Value& returnType, const Value& params )
    {
        auto v = make_shared< Vector >();
        v->reserve( TupleSize( params ) );

        ForEachInTuple( params, [&]( auto&& param )
        {
            v->append( ValueToEIR( param ) );
            return true;
        } );

        return TFuncType( ValueToEIR( returnType ), v,
            make_shared< vector< TermLoc > >(), make_shared< vector< TermLoc > >() );
    }

    optional< Term > BuildTFuncSignature( const Context& c, const TFuncType& tft )
    {
        auto v = make_shared< Vector >();
        v->reserve( VecSize( tft.params() ) + 1 );

        auto rtSig = BuildTemplateSignature( c, tft.returnType() );
        if( !rtSig )
        {
            DiagnosticsManager::GetInstance().emitErrorMessage( ValueFromEIR( tft.returnType() )->locationId(),
                "Invalid template return type or texpr." );
            return nullopt;
        }
        v->append( move( *rtSig ) );

        bool success = true;
        ForEachInVectorTerm( tft.params(), [&]( auto&& param )
        {
            auto teSig = BuildTemplateSignature( c, param );
            if( !teSig )
            {
                DiagnosticsManager::GetInstance().emitErrorMessage( ValueFromEIR( param )->locationId(),
                    "Invalid template parameter." );
                success = false;
                return false;
            }

            v->append( move( *teSig ) );
            return true;
        } );

        if( !success )
            return nullopt;

        return TERM( v );
    }

    Value BuildTFunc( const Context& c, const TFuncType& tft, const Term& identity, const Value& params, ptr< void > body )
    {
        auto sig = BuildTFuncSignature( c, tft );
        if( !sig )
            return PoisonValue();

        return ToValue( TFunc( tft, *sig, identity, body ) );
    }

    optional< Term > BuildArgPatternFromTFuncType( const Context& c, const Value& tfuncType )
    {
        const auto& ftype = FromValue< TFuncType >( tfuncType );
        assert( ftype );

        auto apv = make_shared< Vector >();
        apv->reserve( VecSize( ftype->params() ) + 1 );

        auto rtArgPat = BuildTemplateArgPattern( c, ftype->returnType() );
        if( !rtArgPat )
        {
            DiagnosticsManager::GetInstance().emitErrorMessage( ValueFromEIR( ftype->returnType() )->locationId(),
                "Invalid template return type or texpr." );
            return nullopt;
        }
        apv->append( move( *rtArgPat ) );

        bool success = true;
        ForEachInVectorTerm( ftype->params(), [&]( auto&& param )
        {
            auto teArgPat = BuildTemplateArgPattern( c, param );
            if( !teArgPat )
            {
                DiagnosticsManager::GetInstance().emitErrorMessage( ValueFromEIR( param )->locationId(),
                    "Invalid template parameter." );
                success = false;
                return false;
            }

            apv->append( move( *teArgPat ) );
            return true;
        } );

        if( !success )
            return nullopt;

        return TERM( apv );
    }
}