#include "builtins/builtins.h"
#include "parse/parse.h"
using namespace goose::parse;
using namespace goose::llr;
namespace goose::builtins
{
void SetupDropValue( Env& e )
{
// Default implementation of DropValue().
// Constant values are discarded, and the llr of computed values
// is appended to the current BB of the current parser.
RegisterBuiltinFunc< Intrinsic< void ( Value ) > >( e, e.extDropValue(),
[]( const Context& c, const Value& v )
{
if( v.isConstant() || !c.codeBuilder() )
return;
// Reference use a load instruction to store their address,
// so don't emit them. There is never any point in emitting
// a load whose result isn't used anyway.
if( holds_alternative< Load >( v.llr()->content() ) )
return;
auto bb = c.codeBuilder()->cfg()->currentBB();
bb->emplace_back( move( *v.llr() ) );
} );
using AnyDeclType = CustomPattern< Decl, Decl::Pattern >;
// DropValue for Decls: declare a local variable with default initialization.
// TODO: if the invocation to InitializeValue fails, we should have a way to
// replace the generic "function arguments mismatch" error message with something
// more specific such as "can't default-initialize a variable of type XXX"
RegisterBuiltinFunc< Intrinsic< void ( AnyDeclType ) > >( e, e.extDropValue(),
[]( const Context& c, const Value& v )
{
if( !c.codeBuilder() )
{
DiagnosticsManager::GetInstance().emitSyntaxErrorMessage( 0, "variable declarations are not allowed here." );
return PoisonValue();
}
auto decl = *FromValue< Decl >( v );
return DeclareLocalVar( c, decl.type(), decl.name(), nullopt );
} );
}
}