Goose  Artifact [c589dd1e02]

Artifact c589dd1e0253f6747ebef5d077ea0c42f3f192fb633c6218902e1cdbb1c8f629:

  • File bs/builtins/types/template/tvar.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: 1548)

#include "builtins/builtins.h"

using namespace empathy::builtins;

namespace empathy::builtins
{
    bool IsTExpr( const optional< Value >& te )
    {
        return te && IsTExpr( *te );
    }

    bool IsTExpr( const Value& te )
    {
        if( te.isConstant() )
        {
            const auto* ppVec = get_if< pvec >( &te.val() );
            if( ppVec && !( *ppVec )->empty()
                &&( **ppVec )[0] == TSID( texpr ) )
            {
                return true;
            }
        }

        auto typeVal = ValueFromIRExpr( te.type() );
        if( !typeVal )
            return false;

        const auto* ppVec = get_if< pvec >( &typeVal->val() );
        if( !ppVec )
            return false;

        if( ( *ppVec )->empty() )
            return false;

        return ( **ppVec )[0] == TSID( texpr );
    }

    bool IsTVar( const Value& tv )
    {
        return tv.type() == GetValueType< TVar >();
    }
}

namespace empathy::ir
{
    const Term& Bridge< TVar >::Type()
    {
        static auto type = ValueToIRExpr( Value( TypeType(), VEC( TSID( texpr ), TSID( tvar ) ) ) );
        return type;
    }

    Value Bridge< TVar >::ToValue( TVar&& td )
    {
        return Value( Type(), TERM( td.name() ) );
    }

    optional< TVar > Bridge< TVar >::FromValue( const Value& v )
    {
        if( !IsTVar( v ) )
            return nullopt;

        auto result = Decompose( v.val(),
            Val< StringId >()
        );

        if( !result )
            return nullopt;

        return TVar( *result );
    }
}