Goose  Artifact [3e52e5cba6]

Artifact 3e52e5cba67725ffeba980540c1c519a543b02f550ac60544592c262da7b2aa6:

  • File bs/builtins/types/runtime/array.cpp — part of check-in [51f288ba2a] at 2021-09-13 21:52:06 on branch trunk —
    • Implemented a generic wrapper for the simple native types that can be embedded in Terms
    • Added more specific overloads for the assignment operator that should work only on builtin types, so that library and user defined types will be able to choose whether they're copyable
    • The EIR representation for all builtin runtime types are now prefixed with "rt_type" to more easily write generic matching rules against them
    • EIR: Fixed long standing bugs in Enumerate and Decompose that surfaced because of the above
    • Miscellaneous code cleaning
    (user: achavasse size: 1702)

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

using namespace goose;
using namespace goose::builtins;

namespace goose::builtins
{
    void SetupRuntimeArrayType( Env& e )
    {
        RegisterBuiltinFunc< Eager< Value > ( Value, uint32_t ) >( e, "array"_sid,
            []( const Value& containedType, uint32_t count )
            {
                if( !GetLLVMType( containedType ) )
                {
                    DiagnosticsManager::GetInstance().emitErrorMessage( containedType.locationId(), "runtime arrays can only contain runtime types." );
                    return PoisonValue();
                }

                return ToValue( ArrayType( ValueToEIR( containedType ), count ) );
            } );
    }

    llvm::Type* GetLLVMType( const ArrayType& a )
    {
        return llvm::ArrayType::get( GetLLVMType( *EIRToValue( a.m_containedType ) ), a.m_count );
    }
}

namespace goose::eir
{
    Value Bridge< ArrayType >::ToValue( const ArrayType& a )
    {
        return Value( Type(), MkStdRTType( TSID( rt_type ),
            GetLLVMType( a ), TSID( array ),
            TERM( a.m_count ), a.m_containedType ) );
    }

    optional< ArrayType > Bridge< ArrayType >::FromValue( const Value& v )
    {
        auto result = Decompose( v.val(),
            Vec(
                Lit( "rt_type"_sid ),
                SubTerm(),
                Val< void* >(),
                Lit( "array"_sid ),
                Val< uint32_t >(),
                SubTerm()
            )
        );

        if( !result )
            return nullopt;

        auto&& [predicates, llvmType, count, containedType] = *result;
        return ArrayType( containedType, count );
    }
}