| 1 | // | 
|---|
| 2 | // Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo | 
|---|
| 3 | // | 
|---|
| 4 | // The contents of this file are covered under the licence agreement in the | 
|---|
| 5 | // file "LICENCE" distributed with Cforall. | 
|---|
| 6 | // | 
|---|
| 7 | // FindFunction.cc -- | 
|---|
| 8 | // | 
|---|
| 9 | // Author           : Richard C. Bilson | 
|---|
| 10 | // Created On       : Mon May 18 07:44:20 2015 | 
|---|
| 11 | // Last Modified By : Rob Schluntz | 
|---|
| 12 | // Last Modified On : Fri Feb 05 12:22:20 2016 | 
|---|
| 13 | // Update Count     : 6 | 
|---|
| 14 | // | 
|---|
| 15 |  | 
|---|
| 16 | #include "FindFunction.h" | 
|---|
| 17 |  | 
|---|
| 18 | #include <utility>                      // for pair | 
|---|
| 19 |  | 
|---|
| 20 | #include "Common/SemanticError.h"       // for SemanticError | 
|---|
| 21 | #include "GenPoly/ErasableScopedMap.h"  // for ErasableScopedMap<>::iterator | 
|---|
| 22 | #include "GenPoly/GenPoly.h"            // for TyVarMap | 
|---|
| 23 | #include "ScrubTyVars.h"                // for ScrubTyVars | 
|---|
| 24 | #include "SynTree/Declaration.h"        // for DeclarationWithType, TypeDecl | 
|---|
| 25 | #include "SynTree/Mutator.h"            // for Mutator, mutateAll | 
|---|
| 26 | #include "SynTree/Type.h"               // for FunctionType, Type, Type::For... | 
|---|
| 27 |  | 
|---|
| 28 | namespace GenPoly { | 
|---|
| 29 | class FindFunction : public Mutator { | 
|---|
| 30 | public: | 
|---|
| 31 | FindFunction( std::list< FunctionType* > &functions, const TyVarMap &tyVars, bool replaceMode, FindFunctionPredicate predicate ); | 
|---|
| 32 |  | 
|---|
| 33 | virtual Type *mutate( FunctionType *functionType ); | 
|---|
| 34 | virtual Type *mutate( PointerType *pointerType ); | 
|---|
| 35 | private: | 
|---|
| 36 | void handleForall( const Type::ForallList &forall ); | 
|---|
| 37 |  | 
|---|
| 38 | std::list< FunctionType* > &functions; | 
|---|
| 39 | TyVarMap tyVars; | 
|---|
| 40 | bool replaceMode; | 
|---|
| 41 | FindFunctionPredicate predicate; | 
|---|
| 42 | }; | 
|---|
| 43 |  | 
|---|
| 44 | void findFunction( Type *type, std::list< FunctionType* > &functions, const TyVarMap &tyVars, FindFunctionPredicate predicate ) { | 
|---|
| 45 | FindFunction finder( functions, tyVars, false, predicate ); | 
|---|
| 46 | type->acceptMutator( finder ); | 
|---|
| 47 | } | 
|---|
| 48 |  | 
|---|
| 49 | void findAndReplaceFunction( Type *&type, std::list< FunctionType* > &functions, const TyVarMap &tyVars, FindFunctionPredicate predicate ) { | 
|---|
| 50 | FindFunction finder( functions, tyVars, true, predicate ); | 
|---|
| 51 | type = type->acceptMutator( finder ); | 
|---|
| 52 | } | 
|---|
| 53 |  | 
|---|
| 54 | FindFunction::FindFunction( std::list< FunctionType* > &functions, const TyVarMap &tyVars, bool replaceMode, FindFunctionPredicate predicate ) | 
|---|
| 55 | : functions( functions ), tyVars( tyVars ), replaceMode( replaceMode ), predicate( predicate ) { | 
|---|
| 56 | } | 
|---|
| 57 |  | 
|---|
| 58 | void FindFunction::handleForall( const Type::ForallList &forall ) { | 
|---|
| 59 | for ( Type::ForallList::const_iterator i = forall.begin(); i != forall.end(); ++i ) { | 
|---|
| 60 | TyVarMap::iterator var = tyVars.find( (*i)->get_name() ); | 
|---|
| 61 | if ( var != tyVars.end() ) { | 
|---|
| 62 | tyVars.erase( var->first ); | 
|---|
| 63 | } // if | 
|---|
| 64 | } // for | 
|---|
| 65 | } | 
|---|
| 66 |  | 
|---|
| 67 | Type * FindFunction::mutate( FunctionType *functionType ) { | 
|---|
| 68 | tyVars.beginScope(); | 
|---|
| 69 | handleForall( functionType->get_forall() ); | 
|---|
| 70 | mutateAll( functionType->get_returnVals(), *this ); | 
|---|
| 71 | Type *ret = functionType; | 
|---|
| 72 | if ( predicate( functionType, tyVars ) ) { | 
|---|
| 73 | functions.push_back( functionType ); | 
|---|
| 74 | if ( replaceMode ) { | 
|---|
| 75 | // replace type parameters in function type with void* | 
|---|
| 76 | ret = ScrubTyVars::scrub( functionType->clone(), tyVars ); | 
|---|
| 77 | } // if | 
|---|
| 78 | } // if | 
|---|
| 79 | tyVars.endScope(); | 
|---|
| 80 | return ret; | 
|---|
| 81 | } | 
|---|
| 82 |  | 
|---|
| 83 | Type * FindFunction::mutate( PointerType *pointerType ) { | 
|---|
| 84 | tyVars.beginScope(); | 
|---|
| 85 | handleForall( pointerType->get_forall() ); | 
|---|
| 86 | Type *ret = Mutator::mutate( pointerType ); | 
|---|
| 87 | tyVars.endScope(); | 
|---|
| 88 | return ret; | 
|---|
| 89 | } | 
|---|
| 90 | } // namespace GenPoly | 
|---|
| 91 |  | 
|---|
| 92 | // Local Variables: // | 
|---|
| 93 | // tab-width: 4 // | 
|---|
| 94 | // mode: c++ // | 
|---|
| 95 | // compile-command: "make install" // | 
|---|
| 96 | // End: // | 
|---|