Goose  Artifact [ecb6da27f3]

Artifact ecb6da27f3ff20dee96221e484f8b666d0a64bdb2d5995f75f896a7351aa4c5d:

  • File bs/builtins/types/types.cpp — part of check-in [1ad61a2717] at 2021-11-11 20:05:58 on branch trunk — Refactored the code builder: it is now carried around as a Value, and accessed through a bunch of extension points, so we can have different builders (and even user defined ones) later to make classes etc. (user: zlodo size: 3467)

#include "builtins/builtins.h"

namespace goose::builtins
{
    void SetupBuiltinTypes( Env& e )
    {
        SetupDefaultTypeExtPoints( e );

        SetupPredicatesTypeChecking( e );

        SetupBasicTypes( e );
        SetupBasicTypesPrettyPrinting();

        SetupTupleTypeChecking( e );

        SetupFunctionInvocationRule( e );
        SetupFunctionTypeChecking( e );
        SetupFunctionLowering( e );

        SetupTemplateRules( e );
        SetupTemplateFunctionInvocationRule( e );
        SetupTemplateFunctionTypeChecking( e );
        SetupTDeclTypeChecking( e );
        SetupTemplatePrettyPrinting();

        SetupOverloadSetInvocationRule( e );
        SetupOverloadSetTypeChecking( e );

        SetupConstrainedFuncInvocationRule( e );
        SetupConstrainedFuncTypeChecking( e );

        SetupLocalVarDropValue( e );
        SetupLocalVarTypeChecking( e );
        SetupLocalVarInvocationRule( e );

        SetupReferenceTypeChecking( e );
        SetupRefTypeParsingRule( e );
        SetupReferenceInitialize( e );
        SetupReferenceLowering( e );

        SetupRuntimeTypes( e );

        SetupInitialize( e );
        SetupDestroyValue( e );
        SetupDropValue( e );
        SetupLower( e );
        SetupConvert( e );

        SetupTupleTypeExtPoints( e );
        SetupTupleInitialize( e );
        SetupTupleDestroyValue( e );
        SetupTupleDropValue( e );
        SetupTupleLowering( e );

        SetupRuntimeBasicTypesInitialize( e );
    }

    void SetupDefaultTypeExtPoints( Env& e )
    {
        RegisterBuiltinFunc< Intrinsic< bool ( Value ) > >( e, e.extIsType(),
            []( const Context& c, const Value& v )
            {
                return ToValue( false );
            } );

        RegisterBuiltinFunc< Intrinsic< bool ( CustomPattern< Value, PatternType > ) > >( e, e.extIsType(),
            []( const Context& c, const Value& v )
            {
                return ToValue( true );
            } );

        RegisterBuiltinFunc< Intrinsic< Value ( CustomPattern< Value, PatternType > ) > >( e, e.extToType(),
            []( const Context& c, const Value& v )
            {
                return v;
            } );
    }

    bool IsType( const Context& c, const Value& v )
    {
        if( v.isType() )
            return true;

        auto result = InvokeOverloadSet( c,
            c.env()->extIsType(),
            MakeTuple( v ) );

        if( result.isPoison() )
        {
            PoisonBuilder( c );
            return false;
        }

        auto boolRes = FromValue< bool >( result );
        if( !boolRes )
        {
            DiagnosticsManager::GetInstance().emitErrorMessage( v.locationId(),
                "the IsType extension point returned a non boolean value." );

            PoisonBuilder( c );
            return false;
        }

        return *boolRes;
    }

    Value ToType( const Context& c, const Value& v )
    {
        auto result = InvokeOverloadSet( c,
            c.env()->extToType(),
            MakeTuple( v ) );

        if( result.isPoison() )
        {
            PoisonBuilder( c );
            return PoisonType();
        }

        if( !result.isType() )
        {
            DiagnosticsManager::GetInstance().emitErrorMessage( v.locationId(),
                "the ToType extension point returned a non type value." );

            PoisonBuilder( c );
            return PoisonType();
        }

        return result;
    }
}