Goose  Artifact [caa76f94f1]

Artifact caa76f94f1370ac6bd8cd781939c1deceb2d94be936a94a1eb826dc060944224:

  • File bs/builtins/types/template/build.cpp — part of check-in [45078c42df] at 2021-10-27 20:55:12 on branch trunk — Parser: when building a function, if its parent identity depends on type predicates captured by template vars, build it as a template function instead. (user: zlodo size: 3580)

#include "builtins/builtins.h"

namespace goose::builtins
{
    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 > >() );
    }

    TFuncType FuncTypeToTFuncType( const FuncType& ft )
    {
        auto pVerifInf = ft.verifInfos();
        return TFuncType(
            ft.returnType(),
            ft.params(),
            pVerifInf->preCondToks(),
            pVerifInf->postCondToks(),
            ft.intrinsic() );
    }

    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( EIRToValue( 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( EIRToValue( 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& parentIdentity, const Term& identity, const Value& params, ptr< void > body )
    {
        auto sig = BuildTFuncSignature( c, tft );
        if( !sig )
            return PoisonValue();

        return ToValue( TFunc( tft, *sig, parentIdentity, 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( EIRToValue( 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( EIRToValue( param )->locationId(),
                    "invalid template parameter." );
                success = false;
                return false;
            }

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

        if( !success )
            return nullopt;

        return TERM( apv );
    }
}