#include "builtins/builtins.h"
#include "parse/parse.h"
#include "precedence.h"
#include "builtins/helpers.h"
using namespace goose;
using namespace goose::eir;
using namespace goose::parse;
namespace goose::builtins
{
void SetupWhileStmt( Env& e )
{
auto handleWhile = []( Parser& p, uint32_t locationId, uint32_t prec )
{
auto& dm = DiagnosticsManager::GetInstance();
const auto& cb = p.context().codeBuilder();
if( p.isInParenExpr() || !cb )
{
dm.emitSyntaxErrorMessage( locationId, "the while statement is not allowed here.", 0 );
return false;
}
const auto& cfg = cb->cfg();
auto pPrecBB = cfg->currentBB();
if( !pPrecBB || pPrecBB->terminator() )
{
DiagnosticsManager::GetInstance().emitSyntaxErrorMessage(
locationId, "unreachable code.", 0 );
cfg->poison();
}
// Create a scope for the entire while, so that any var declared
// inside of the condition is alive and visible throughout the while.
Scope s( p );
auto np = p.makeNestedParser();
if( !np.parseExpression( precedence::IfStmt ) )
{
dm.emitSyntaxErrorMessage( locationId, "expected an expression following the while statement.", 0 );
return false;
}
auto condVal = np.popValue();
if( !condVal )
{
dm.emitSyntaxErrorMessage( locationId, "expected an expression following the while statement.", 0 );
return false;
}
const auto& context = p.context();
auto converted = ConvertValueToType( context, *condVal, GetValueType< bool >() );
if( holds_alternative< ValUnifyError >( converted ) )
{
switch( get< ValUnifyError >( converted ) )
{
// If the condition is invalid, bail out and mark the current
// diagnostics context as bust to avoid spamming cascading errors.
case ValUnifyError::NoSolution:
dm.emitSyntaxErrorMessage( condVal->locationId(), "the while condition can't be converted to a bool." );
break;
case ValUnifyError::Ambiguous:
dm.emitSyntaxErrorMessage( condVal->locationId(), "ambiguous while condition bool conversion." );
break;
}
return false;
}
if( get< Value >( converted ).isPoison() )
cb->poison();
// The condition may have emitted additional basic blocks, so get the current block again.
pPrecBB = cfg->currentBB();
auto pHeaderBB = cfg->createBB();
// Set the loop header's location to a span including the while keyword and the condition
pHeaderBB->setLocationId( Location::CreateSpanningLocation( locationId, condVal->locationId() ) );
CodeBuilder::BreakableScopeGuard bsg( cb );
CodeBuilder::ContinuableScopeGuard csg( cb );
auto pBodyBB = ParseSubStatement( p, precedence::IfStmt );
if( !pBodyBB )
{
dm.emitSyntaxErrorMessage( p.resolver()->currentLocation(), "expected a statement after the while condition.", 0 );
return false;
}
if( !pPrecBB )
return true;
// Retrieve the final block of the loop body, if any
if( auto pBodySuccBB = cfg->currentBB(); pBodySuccBB && !pBodySuccBB->terminator() )
{
// Jump from the end of the body BB back to the loop header
pBodySuccBB->setTerminator( cir::Branch( pHeaderBB ) );
}
// Jump unconditionally from the pred block to the loop header.
pPrecBB->setTerminator( cir::Branch( pHeaderBB ) );
auto pSuccBB = cfg->createBB();
auto breakLevel = cb->breakableScopeLevels();
auto continueLevel = cb->continuableScopeLevels();
// Go through all basic blocks, find all break and continue terminators
// for our scope level and replace them with branches respectively to
// the successor BB or to the loop header BB.
cfg->forEachBB( [&]( auto&& bb )
{
const auto& t = bb->terminator();
if( !t )
return;
if( const auto* pBreak = get_if< cir::Break >( &t->content() ) )
{
if( pBreak->level() == breakLevel )
bb->setTerminator( cir::Branch( pSuccBB ) );
return;
}
if( const auto* pCont = get_if< cir::Continue >( &t->content() ) )
{
if( pCont->level() == continueLevel )
bb->setTerminator( cir::Branch( pHeaderBB ) );
return;
}
} );
// Emit the conditional branch that will either run an iteration of the loop or exit to the succ bb.
pHeaderBB->setTerminator( cir::CondBranch(
get< Value >( converted ),
pBodyBB, pSuccBB ) );
cfg->setCurrentBB( pSuccBB );
return true;
};
RegisterRule( e, "while"_sid, Rule( handleWhile ) );
}
}