[51b7345] | 1 | /* |
---|
| 2 | * This file is part of the Cforall project |
---|
| 3 | * |
---|
| 4 | * $Id: FindFunction.cc,v 1.5 2005/08/29 20:14:13 rcbilson Exp $ |
---|
| 5 | * |
---|
| 6 | */ |
---|
| 7 | |
---|
| 8 | #include "FindFunction.h" |
---|
| 9 | #include "SynTree/Type.h" |
---|
| 10 | #include "SynTree/Declaration.h" |
---|
| 11 | #include "SynTree/Visitor.h" |
---|
| 12 | |
---|
| 13 | namespace GenPoly { |
---|
| 14 | |
---|
| 15 | class FindFunction : public Mutator |
---|
| 16 | { |
---|
| 17 | public: |
---|
| 18 | FindFunction( std::list< FunctionType* > &functions, const TyVarMap &tyVars, bool replaceMode, FindFunctionPredicate predicate ); |
---|
| 19 | |
---|
| 20 | virtual Type *mutate( FunctionType *functionType ); |
---|
| 21 | virtual Type *mutate( PointerType *pointerType ); |
---|
| 22 | |
---|
| 23 | private: |
---|
| 24 | void handleForall( const std::list< TypeDecl* > &forall ); |
---|
| 25 | |
---|
| 26 | std::list< FunctionType* > &functions; |
---|
| 27 | TyVarMap tyVars; |
---|
| 28 | bool replaceMode; |
---|
| 29 | FindFunctionPredicate predicate; |
---|
| 30 | }; |
---|
| 31 | |
---|
| 32 | void |
---|
| 33 | findFunction( Type *type, std::list< FunctionType* > &functions, const TyVarMap &tyVars, FindFunctionPredicate predicate ) |
---|
| 34 | { |
---|
| 35 | FindFunction finder( functions, tyVars, false, predicate ); |
---|
| 36 | type->acceptMutator( finder ); |
---|
| 37 | } |
---|
| 38 | |
---|
| 39 | void |
---|
| 40 | findAndReplaceFunction( Type *&type, std::list< FunctionType* > &functions, const TyVarMap &tyVars, FindFunctionPredicate predicate ) |
---|
| 41 | { |
---|
| 42 | FindFunction finder( functions, tyVars, true, predicate ); |
---|
| 43 | type = type->acceptMutator( finder ); |
---|
| 44 | } |
---|
| 45 | |
---|
| 46 | FindFunction::FindFunction( std::list< FunctionType* > &functions, const TyVarMap &tyVars, bool replaceMode, FindFunctionPredicate predicate ) |
---|
| 47 | : functions( functions ), tyVars( tyVars ), replaceMode( replaceMode ), predicate( predicate ) |
---|
| 48 | { |
---|
| 49 | } |
---|
| 50 | |
---|
| 51 | void |
---|
| 52 | FindFunction::handleForall( const std::list< TypeDecl* > &forall ) |
---|
| 53 | { |
---|
| 54 | for( std::list< TypeDecl* >::const_iterator i = forall.begin(); i != forall.end(); ++i ) { |
---|
| 55 | TyVarMap::iterator var = tyVars.find( (*i)->get_name() ); |
---|
| 56 | if( var != tyVars.end() ) { |
---|
| 57 | tyVars.erase( var ); |
---|
| 58 | } |
---|
| 59 | } |
---|
| 60 | } |
---|
| 61 | |
---|
| 62 | Type* |
---|
| 63 | FindFunction::mutate( FunctionType *functionType ) |
---|
| 64 | { |
---|
| 65 | TyVarMap oldTyVars = tyVars; |
---|
| 66 | handleForall( functionType->get_forall() ); |
---|
| 67 | mutateAll( functionType->get_returnVals(), *this ); |
---|
| 68 | Type *ret = functionType; |
---|
| 69 | if( predicate( functionType, tyVars ) ) { |
---|
| 70 | functions.push_back( functionType ); |
---|
| 71 | if( replaceMode ) { |
---|
| 72 | ret = new FunctionType( Type::Qualifiers(), true ); |
---|
| 73 | } |
---|
| 74 | } |
---|
| 75 | tyVars = oldTyVars; |
---|
| 76 | return ret; |
---|
| 77 | } |
---|
| 78 | |
---|
| 79 | Type * |
---|
| 80 | FindFunction::mutate( PointerType *pointerType ) |
---|
| 81 | { |
---|
| 82 | TyVarMap oldTyVars = tyVars; |
---|
| 83 | handleForall( pointerType->get_forall() ); |
---|
| 84 | Type *ret = Mutator::mutate( pointerType ); |
---|
| 85 | tyVars = oldTyVars; |
---|
| 86 | return ret; |
---|
| 87 | } |
---|
| 88 | |
---|
| 89 | } // namespace GenPoly |
---|