Goose  Artifact [f13d8c7b5c]

Artifact f13d8c7b5c23b48c935f6f6081e5da86f3f707f2551a8abda37073a9e92a810c:

  • File bs/builtins/types/template/build.cpp — part of check-in [27fc719d74] at 2019-08-16 14:48:20 on branch trunk — Added a new type of template expression: TVec, along with a helper function to make it possible for parametric types to be constructed either normally or as a template expression when passed template parameters. Only used by LocalVar for now, parametric runtime types require some refactoring. (user: achavasse size: 3593)

#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 )
    {
        immer::vector< Term > v;
        auto vt = v.transient();

        ForEachInTuple( params, [&]( auto&& param )
        {
            vt.push_back( ValueToIRExpr( param ) );
            return true;
        } );

        return TFuncType( domain, ValueToIRExpr( returnType ), TERM( make_shared< Vector >( vt.persistent() ) ) );
    }

    optional< Term > BuildTFuncSignature( const Context& c, const TFuncType& tft )
    {
        immer::vector< Term > v;
        auto vt = v.transient();

        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;
            }

            vt.push_back( 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(),
            TERM( make_shared< Vector >( vt.persistent() ) ),
            *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 );

        immer::vector< Term > apv;
        auto apvt = apv.transient();

        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;
            }

            apvt.push_back( 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(),
            TERM( make_shared< Vector >( apvt.persistent() ) ),
            *rtArgPat
        );
    }
}