#include "builtins/builtins.h"
namespace goose::builtins
{
class TFuncTypeTemplateRule : public TemplateRule
{
bool prepare( TemplateContext& c, const Term& val ) const final
{
auto tft = FromValue< TFuncType >( *EIRToValue( val ) );
assert( tft );
bool success = true;
ForEachInVectorTerm( tft->params(),
[&]( auto&& param )
{
if( !PrepareTemplate( c, param ) )
{
success = false;
return false;
}
return true;
} );
if( !success )
return false;
return PrepareTemplate( c, tft->returnType() );
}
optional< Term > buildSignature( const TemplateContext& c, const Term& val ) const final
{
auto tfuncType = FromValue< TFuncType >( *EIRToValue( val ) );
auto sig = BuildTFuncSignature( c.semaContext(), *tfuncType );
if( !sig )
return nullopt;
return TFuncTypeSigPattern( val, move( *sig ) );
}
Generator< Value > buildParamDecls(
const Context& c, const Term& param, TermGen& args ) const final
{
co_yield *EIRToValue( *args.consume() );
}
void setup( const Context& c, TypeCheckingContext& tcc, const Term& val ) const final
{
auto tft = FromValue< TFuncType >( *EIRToValue( val ) );
assert( tft );
ForEachInVectorTerm( tft->params(),
[&]( auto&& param )
{
TemplateSetup( c, tcc, param );
return true;
} );
TemplateSetup( c, tcc, tft->returnType() );
}
uint64_t hashTypePredicates(
const Context& c, const TypeCheckingContext& tcc, const Term& val ) const final
{
auto tft = FromValue< TFuncType >( *EIRToValue( val ) );
assert( tft );
const auto& vec = *get< pvec >( tft->params() );
auto g = ContainerTypePredicatesHashGenerator( c, tcc, vec.terms() );
auto paramsHash = llvm::hash_combine_range( g.begin(), g.end() );
return llvm::hash_combine(
paramsHash, TemplateHashTypePredicates( c, tcc, tft->returnType() ) );
}
optional< Term > buildArgPattern( const Context& c, const Term& val ) const final
{
return buildSignature( c, val );
}
bool isPackExpr( const Context& c, const Term& val ) const final { return false; }
};
void SetupTFuncTypeTemplateRule( Env& e )
{
e.templateRuleSet()->addRule( TFuncTypePattern(), make_shared< TFuncTypeTemplateRule >() );
}
} // namespace goose::builtins