Goose  Artifact [3341ebaa32]

Artifact 3341ebaa3222dddc1f41c8bffd867311aada76cb2fb0809b1958a451ebcb4527:

  • File bs/builtins/types/template/rules/tfunctype.cpp — part of check-in [0db147f117] at 2024-09-15 20:24:31 on branch cir-ssa-refactor — Add clang format settings, reformat everything (user: achavasse size: 2895)

#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