Goose  Artifact [bb76a7e996]

Artifact bb76a7e9968d05ef0ddd5a0101bb74b9bcccaf84e3927c09c81e45067d9b3a0f:

  • File bs/builtins/types/runtime/array.cpp — part of check-in [aee388d9c0] at 2019-08-09 19:54:22 on branch trunk — Cleanup: got rid of the half-assed location and poisoning systems in ir::Terms. (user: achavasse size: 1606)

#include "builtins/builtins.h"

using namespace empathy;
using namespace empathy::builtins;

namespace empathy::builtins
{
    void SetupRuntimeArrayType( Env& e )
    {
        RegisterBuiltinFunc< Eager< Value > ( Value, uint32_t ) >( e, "array"_sid,
            []( const Value& containedType, uint32_t count )
            {
                if( !GetLLVMType( containedType ) )
                {
                    // TODO come up with some lightweight builtin option type
                    // for the builtin apis, because this is a very bullshit
                    // way to handle errors
                    return ToValue( "error"s );
                }

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

namespace empathy::ir
{
    Value Bridge< ArrayType >::ToValue( const ArrayType& a )
    {
        return Value( Type(), TVEC( TSID( rt_type ),
            TERM( llvm::ArrayType::get( GetLLVMType( *ValueFromIRExpr( a.m_containedType ) ), a.m_count ) ),
            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 ),
                Val< void* >(),
                Lit( "array"_sid ),
                Val< uint32_t >(),
                SubTerm()
            )
        );

        if( !result )
            return nullopt;

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