Goose  Artifact [1714d5156e]

Artifact 1714d5156ef771ddcf8b701e30ab66a7c9094a031e2bfe2251674410d0b049d0:

  • File bs/g0api/extensibility/typewrappers.cpp — part of check-in [8dd49b0d89] at 2021-09-16 21:31:22 on branch trunk — g0 api: started implementing the CIR api (user: achavasse size: 4133)

#include "g0api/g0api.h"
#include "eir/eir.h"
#include "builtins/helpers.h"

using namespace goose;
using namespace goose::g0api;

namespace
{
    template< typename T >
    void SetupWrapperForType( Env& e, StringId name, const ptr< OverloadSet >& pEquals, const ptr< OverloadSet >& pNotEquals )
    {
        DefineConstant( e, name, GetValueType< TypeWrapper< T > >() );

        using reftype = CustomPattern< Value, ReferenceType::PatternMutableOfType< TypeWrapper< T > > >;
        auto type = ValueToEIR( ToValue( builtins::ReferenceType( GetValueType< TypeWrapper< T > >(), TSID( mut ) ) ) );
        RegisterBuiltinFunc< Intrinsic< Value ( reftype ) > >( e, e.extInitialize(),
            []( auto&& c, const Value& r )
            {
                // Do nothing: the types we're wrapping are default initialized anyway.
                // The only purpose of this Initialize overload is to let goose know that
                // the type has a default initialization.
                return Value( GetValueType< void >(), 0U );
            } );

        using constreftype = CustomPattern<
            TypeWrapper< T >, ReferenceType::PatternConstOfType< TypeWrapper< T > > >;
        RegisterBuiltinFunc< bool ( constreftype, constreftype ) >( e, pEquals,
            []( const TypeWrapper< T >& lhs, const TypeWrapper< T >& rhs )
            {
                return lhs.get() == rhs.get();
            } );

        RegisterBuiltinFunc< bool ( constreftype, constreftype ) >( e, pNotEquals,
            []( const TypeWrapper< T >& lhs, const TypeWrapper< T >& rhs )
            {
                return lhs.get() != rhs.get();
            } );

        RegisterBuiltinFunc< string ( TypeWrapper< T > ) >( e, "ToString"_sid,
            []( const TypeWrapper< T >& t )
            {
                stringstream sstr;
                sstr << t.get();
                return sstr.str();
            } );
    }
}

namespace goose::g0api
{
    void SetupTypeWrappers( Env& e )
    {
        auto pEquals = GetOverloadSet( e, "operator_equals"_sid );
        auto pNotEquals = GetOverloadSet( e, "operator_not_equals"_sid );

        ////////////////////////////
        // EIR types
        ////////////////////////////

        // We need to be able to create empty terms to be filled later by GetVecTerm
        DefineConstant( e, "Term"_sid, GetValueType< TermWrapper >() );

        // Equality operator for term wrapper
        RegisterBuiltinFunc< bool ( TermWrapper, TermWrapper ) >( e, pEquals,
            []( const TermWrapper& lhs, const TermWrapper& rhs )
            {
                return lhs.get() == rhs.get();
            } );

        RegisterBuiltinFunc< bool ( TermWrapper, TermWrapper ) >( e, pNotEquals,
            []( const TermWrapper& lhs, const TermWrapper& rhs )
            {
                return lhs.get() != rhs.get();
            } );

        SetupWrapperForType< LocationId >( e, "LocationId"_sid, pEquals, pNotEquals );
        SetupWrapperForType< StringId >( e, "StringId"_sid, pEquals, pNotEquals );
        SetupWrapperForType< Hole >( e, "Hole"_sid, pEquals, pNotEquals );
        SetupWrapperForType< AnyTerm >( e, "AnyTerm"_sid, pEquals, pNotEquals );
        SetupWrapperForType< VecOfLength >( e, "VecOfLength"_sid, pEquals, pNotEquals );
        SetupWrapperForType< pvec >( e, "Vec"_sid, pEquals, pNotEquals );
        SetupWrapperForType< APSInt >( e, "FixedInt"_sid, pEquals, pNotEquals );

        DefineConstant( e, "Value"_sid, GetValueType< ValueWrapper >() );

        // Equality operator for value wrapper
        RegisterBuiltinFunc< bool ( ValueWrapper, ValueWrapper ) >( e, pEquals,
            []( const ValueWrapper& lhs, const ValueWrapper& rhs )
            {
                return lhs.get() == rhs.get();
            } );

        RegisterBuiltinFunc< bool ( ValueWrapper, ValueWrapper ) >( e, pNotEquals,
            []( const ValueWrapper& lhs, const ValueWrapper& rhs )
            {
                return lhs.get() != rhs.get();
            } );

        ////////////////////////////
        // CIR types
        ////////////////////////////
    }
}