#include "builtins/builtins.h"
using namespace goose;
using namespace goose::eir;
using namespace goose::cir;
namespace goose::builtins
{
void SetupReferenceTypeChecking( Env& e )
{
auto localVarPattern = GetValueType< LocalVar >( ANYTERM( _ ) );
auto refTypePattern = ValueToEIR(
Value( GetValueType< ReferenceType >(), TVEC( TSID( reference ), ANYTERM( _ ), ANYTERM( _ ) ) ) );
auto refTypePatternConstant = ValueToEIR(
Value( GetValueType< ReferenceType >(), TVEC( TSID( reference ), TSID( const ), ANYTERM( _ ) ) ) );
auto refTypePatternMutable = ValueToEIR(
Value( GetValueType< ReferenceType >(), TVEC( TSID( reference ), TSID( mut ), ANYTERM( _ ) ) ) );
auto refTypePatternTemporary = ValueToEIR(
Value( GetValueType< ReferenceType >(), TVEC( TSID( reference ), TSID( temp ), ANYTERM( _ ) ) ) );
// Reference type checking rule.
e.typeCheckingRuleSet()->addTypeCheckingRule( TCRINFOS,
ParamPat( refTypePattern ),
ValueToEIR( ValuePattern(
ANYTERM( _ ),
refTypePattern,
ANYTERM( _ ) ) ),
[]( const Term& lhs, const Term& rhs, const TypeCheckingContext& tcc ) -> TCGen
{
auto lRefType = *FromValue< ReferenceType >( *ValueFromEIR( ValuePatternFromIRExpr( lhs )->type() ) );
auto rhsVal = *ValuePatternFromIRExpr( rhs );
auto rRefType = *FromValue< ReferenceType >( *ValueFromEIR( rhsVal.type() ) );
// Unify the behaviors
for( auto&& [b, tcc] : Unify( lRefType.behavior(), rRefType.behavior(), tcc ) )
{
// Unify the types
for( auto&& [t, tcc] : Unify( lRefType.type(), rRefType.type(), tcc ) )
{
co_yield { ValueToEIR( ValuePattern( rhsVal.sort(), ValueToEIR( ToValue( ReferenceType( t, b ) ) ),
rhsVal.val() ) ), tcc };
}
}
}
);
// mut -> const reference unification rule.
e.typeCheckingRuleSet()->addUnificationRule( TCRINFOS,
TSID( const ),
TSID( mut ),
[]( const Term& lhs, const Term& rhs, const TypeCheckingContext& tcc ) -> TCGen
{
co_yield { TSID( const ), tcc };
}
);
// temp -> const reference unification rule.
e.typeCheckingRuleSet()->addUnificationRule( TCRINFOS,
TSID( const ),
TSID( temp ),
[]( const Term& lhs, const Term& rhs, const TypeCheckingContext& tcc ) -> TCGen
{
co_yield { TSID( const ), tcc };
}
);
// temp -> mut reference unification rule.
e.typeCheckingRuleSet()->addUnificationRule( TCRINFOS,
TSID( mut ),
TSID( temp ),
[]( const Term& lhs, const Term& rhs, const TypeCheckingContext& tcc ) -> TCGen
{
co_yield { TSID( mut ), tcc };
}
);
// Reference type checking against a param (implicit dereferencing):
// Unify the referenced value with the param.
e.typeCheckingRuleSet()->addTypeCheckingRule( TCRINFOS,
ANYTERM( _ ),
ValueToEIR( ValuePattern(
ANYTERM( _ ),
refTypePattern,
ANYTERM( _ ) ) ),
[]( const Term& lhs, const Term& rhs, const TypeCheckingContext& tcc ) -> TCGen
{
auto refval = *ValueFromEIR( rhs );
G_VAL_ASSERT( refval, !refval.isConstant() );
auto refType = FromValue< ReferenceType >( *ValueFromEIR( refval.type() ) );
if( !refType )
co_return;
auto content = ValueToEIR( BuildComputedValue( refType->type(),
cir::Load( refval.cir(), refType->type() ) )
.setLocationId( refval.locationId() ) );
// TypeCheck the param with the ref's content
co_yield TypeCheck( lhs, content, tcc );
} );
// LocalVar type checking against a param (implicit referencing):
// Build a mutable ref value, or just unwrap the locVar if it
// already contains a ref
e.typeCheckingRuleSet()->addTypeCheckingRule( TCRINFOS,
ValueToEIR( ValuePattern(
ANYTERM( _ ),
ANYTERM( _ ),
ANYTERM( _ ) ) ),
ValueToEIR( ValuePattern(
ANYTERM( _ ),
localVarPattern,
ANYTERM( _ ) ) ),
[]( const Term& lhs, const Term& rhs, const TypeCheckingContext& tcc ) -> TCGen
{
auto ltype = ValuePatternFromIRExpr( lhs )->type();
auto lvval = *ValueFromEIR( rhs );
auto locvar = FromValue< LocalVar >( lvval );
if( !locvar )
co_return;
auto ref = ValueToEIR( ToValue( BuildLocalVarMutRef( *locvar ) )
.setLocationId( lvval.locationId() ) );
co_yield TypeCheck( lhs, ref, tcc );
} );
// Implicit referencing of non-variables: build a tempref
e.typeCheckingRuleSet()->addTypeCheckingRule( TCRINFOS,
ValueToEIR( ValuePattern(
ANYTERM( _ ),
refTypePattern,
ANYTERM( _ ) ) ),
ValueToEIR( ValuePattern(
ANYTERM( _ ),
ANYTERM( _ ),
ANYTERM( _ ) ) ),
[refTypePattern]( const Term& lhs, const Term& rhs, const TypeCheckingContext& tcc ) -> TCGen
{
if( !tcc.context().codeBuilder() )
co_return;
if( !tcc.context().codeBuilder()->cfg() )
co_return;
auto lRefType = *FromValue< ReferenceType >( *ValueFromEIR( ValuePatternFromIRExpr( lhs )->type() ) );
auto lhsPat = ValueToEIR( ValuePattern( TSID( param ), lRefType.type(), HOLE( "_"_sid ) ) );
auto tempIndex = tcc.context().codeBuilder()->cfg()->getNewTemporaryIndex();
for( auto&& [s,tcc] : TypeCheck( lhsPat, rhs, tcc ) )
{
auto valPat = *ValuePatternFromIRExpr( s );
ReferenceType rt( valPat.type(), TSID( temp ) );
auto refPat = ValueToEIR( ValuePattern( HOLE( "_"_sid ), ValueToEIR( ToValue( rt ) ), HOLE( "_"_sid ) ) );
// TypeCheck the param with the ref
for( auto&& [s,tcc] : TypeCheck( lhs, refPat, tcc ) )
{
assert( tcc.complexity() >= GetComplexity( ParamPat( refTypePattern ) ) );
tcc.subComplexity( GetComplexity( ParamPat( refTypePattern ) ) );
auto wrapped = WrapWithPostprocFunc( VEC( s, rhs ),
[tempIndex]( const Term& t, TypeCheckingContext tcc ) -> optional< Term >
{
auto result = Decompose( t,
Vec(
SubTerm(),
SubTerm()
)
);
assert( result );
auto&& [ref, rhs] = *result;
auto rhsVal = *ValueFromEIR( rhs );
auto refPat = *ValuePatternFromIRExpr( ref );
return ValueToEIR( BuildComputedValue( refPat.type(), TempAddr( tempIndex, rhsVal ) ) );
} );
co_yield { move( wrapped ), tcc };
}
}
} );
}
}