Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/GenPoly/FindFunction.cc

    rc6b4432 rc97b448  
    2020#include "AST/Pass.hpp"                 // for Pass
    2121#include "AST/Type.hpp"
     22#include "Common/PassVisitor.h"         // for PassVisitor
    2223#include "GenPoly/ErasableScopedMap.h"  // for ErasableScopedMap<>::iterator
    2324#include "GenPoly/GenPoly.h"            // for TyVarMap
    2425#include "ScrubTyVars.h"                // for ScrubTyVars
     26#include "SynTree/Declaration.h"        // for DeclarationWithType, TypeDecl
     27#include "SynTree/Mutator.h"            // for Mutator, mutateAll
     28#include "SynTree/Type.h"               // for FunctionType, Type, Type::For...
    2529
    2630namespace GenPoly {
     31        class FindFunction : public WithGuards, public WithVisitorRef<FindFunction>, public WithShortCircuiting {
     32          public:
     33                FindFunction( std::list< FunctionType const* > &functions, const TyVarMap &tyVars, bool replaceMode, FindFunctionPredicate predicate );
     34
     35                void premutate( FunctionType * functionType );
     36                Type * postmutate( FunctionType * functionType );
     37                void premutate( PointerType * pointerType );
     38          private:
     39                void handleForall( const Type::ForallList &forall );
     40
     41                std::list< FunctionType const * > & functions;
     42                TyVarMap tyVars;
     43                bool replaceMode;
     44                FindFunctionPredicate predicate;
     45        };
     46
     47        void findFunction( Type *type, std::list< FunctionType const * > &functions, const TyVarMap &tyVars, FindFunctionPredicate predicate ) {
     48                PassVisitor<FindFunction> finder( functions, tyVars, false, predicate );
     49                type->acceptMutator( finder );
     50        }
     51
     52        void findAndReplaceFunction( Type *&type, std::list< FunctionType const * > &functions, const TyVarMap &tyVars, FindFunctionPredicate predicate ) {
     53                PassVisitor<FindFunction> finder( functions, tyVars, true, predicate );
     54                type = type->acceptMutator( finder );
     55        }
     56
     57        FindFunction::FindFunction( std::list< FunctionType const * > &functions, const TyVarMap &tyVars, bool replaceMode, FindFunctionPredicate predicate )
     58                : functions( functions ), tyVars( tyVars ), replaceMode( replaceMode ), predicate( predicate ) {
     59        }
     60
     61        void FindFunction::handleForall( const Type::ForallList &forall ) {
     62                for ( const Declaration * td : forall ) {
     63                        TyVarMap::iterator var = tyVars.find( td->name );
     64                        if ( var != tyVars.end() ) {
     65                                tyVars.erase( var->first );
     66                        } // if
     67                } // for
     68        }
     69
     70        void FindFunction::premutate( FunctionType * functionType ) {
     71                visit_children = false;
     72                GuardScope( tyVars );
     73                handleForall( functionType->get_forall() );
     74                mutateAll( functionType->get_returnVals(), *visitor );
     75        }
     76
     77        Type * FindFunction::postmutate( FunctionType * functionType ) {
     78                Type *ret = functionType;
     79                if ( predicate( functionType, tyVars ) ) {
     80                        functions.push_back( functionType );
     81                        if ( replaceMode ) {
     82                                // replace type parameters in function type with void*
     83                                ret = ScrubTyVars::scrub( functionType->clone(), tyVars );
     84                        } // if
     85                } // if
     86                return ret;
     87        }
     88
     89        void FindFunction::premutate( PointerType * pointerType ) {
     90                GuardScope( tyVars );
     91                handleForall( pointerType->get_forall() );
     92        }
    2793
    2894namespace {
     
    88154void FindFunctionCore::previsit( ast::PointerType const * /*type*/ ) {
    89155        GuardScope( typeVars );
     156        //handleForall( type->forall );
    90157}
    91158
     
    97164        ast::Pass<FindFunctionCore> pass( functions, typeVars, predicate, false );
    98165        type->accept( pass );
     166        //(void)type;
     167        //(void)functions;
     168        //(void)typeVars;
     169        //(void)predicate;
    99170}
    100171
     
    104175        ast::Pass<FindFunctionCore> pass( functions, typeVars, predicate, true );
    105176        return type->accept( pass );
     177        //(void)functions;
     178        //(void)typeVars;
     179        //(void)predicate;
     180        //return type;
    106181}
    107182
Note: See TracChangeset for help on using the changeset viewer.