Goose  Artifact [154d65c7be]

Artifact 154d65c7bec3f604464e441231278607017243db8ddbf91390c0810190fab189:

  • File bs/builtins/types/template/build.cpp — part of check-in [0c3c9488af] at 2019-09-17 20:08:39 on branch trunk —
    • parser: add an utility function to retrieve a sub expression in its unparsed form.
    • tfunc type: store a pointer to a vector of unparsed tokens for the verification statements.
    (user: achavasse size: 3437)

#include "builtins/builtins.h"

namespace empathy::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 Term& domain, const Value& returnType, const Value& params )
    {
        auto v = make_shared< Vector >();
        v->reserve( TupleSize( params ) );

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

        return TFuncType( domain, ValueToIRExpr( returnType ), v, nullptr );
    }

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

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

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

        if( !success )
            return nullopt;

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

        return VEC( tft.domain(), v, *rtSig );
    }

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

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

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

        if( !success )
            return nullopt;

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

        return VEC( ftype->domain(), apv,*rtArgPat );
    }
}