| 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/PassVisitor.h"         // for PassVisitor | 
|---|
| 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 WithGuards, public WithVisitorRef<FindFunction>, public WithShortCircuiting { | 
|---|
| 30 | public: | 
|---|
| 31 | FindFunction( std::list< FunctionType* > &functions, const TyVarMap &tyVars, bool replaceMode, FindFunctionPredicate predicate ); | 
|---|
| 32 |  | 
|---|
| 33 | void premutate( FunctionType * functionType ); | 
|---|
| 34 | Type * postmutate( FunctionType * functionType ); | 
|---|
| 35 | void premutate( PointerType * pointerType ); | 
|---|
| 36 | private: | 
|---|
| 37 | void handleForall( const Type::ForallList &forall ); | 
|---|
| 38 |  | 
|---|
| 39 | std::list< FunctionType* > &functions; | 
|---|
| 40 | TyVarMap tyVars; | 
|---|
| 41 | bool replaceMode; | 
|---|
| 42 | FindFunctionPredicate predicate; | 
|---|
| 43 | }; | 
|---|
| 44 |  | 
|---|
| 45 | void findFunction( Type *type, std::list< FunctionType* > &functions, const TyVarMap &tyVars, FindFunctionPredicate predicate ) { | 
|---|
| 46 | PassVisitor<FindFunction> finder( functions, tyVars, false, predicate ); | 
|---|
| 47 | type->acceptMutator( finder ); | 
|---|
| 48 | } | 
|---|
| 49 |  | 
|---|
| 50 | void findAndReplaceFunction( Type *&type, std::list< FunctionType* > &functions, const TyVarMap &tyVars, FindFunctionPredicate predicate ) { | 
|---|
| 51 | PassVisitor<FindFunction> finder( functions, tyVars, true, predicate ); | 
|---|
| 52 | type = type->acceptMutator( finder ); | 
|---|
| 53 | } | 
|---|
| 54 |  | 
|---|
| 55 | FindFunction::FindFunction( std::list< FunctionType* > &functions, const TyVarMap &tyVars, bool replaceMode, FindFunctionPredicate predicate ) | 
|---|
| 56 | : functions( functions ), tyVars( tyVars ), replaceMode( replaceMode ), predicate( predicate ) { | 
|---|
| 57 | } | 
|---|
| 58 |  | 
|---|
| 59 | void FindFunction::handleForall( const Type::ForallList &forall ) { | 
|---|
| 60 | for ( const Declaration * td : forall ) { | 
|---|
| 61 | TyVarMap::iterator var = tyVars.find( td->name ); | 
|---|
| 62 | if ( var != tyVars.end() ) { | 
|---|
| 63 | tyVars.erase( var->first ); | 
|---|
| 64 | } // if | 
|---|
| 65 | } // for | 
|---|
| 66 | } | 
|---|
| 67 |  | 
|---|
| 68 | void FindFunction::premutate( FunctionType * functionType ) { | 
|---|
| 69 | visit_children = false; | 
|---|
| 70 | GuardScope( tyVars ); | 
|---|
| 71 | handleForall( functionType->get_forall() ); | 
|---|
| 72 | mutateAll( functionType->get_returnVals(), *visitor ); | 
|---|
| 73 | } | 
|---|
| 74 |  | 
|---|
| 75 | Type * FindFunction::postmutate( FunctionType * functionType ) { | 
|---|
| 76 | Type *ret = functionType; | 
|---|
| 77 | if ( predicate( functionType, tyVars ) ) { | 
|---|
| 78 | functions.push_back( functionType ); | 
|---|
| 79 | if ( replaceMode ) { | 
|---|
| 80 | // replace type parameters in function type with void* | 
|---|
| 81 | ret = ScrubTyVars::scrub( functionType->clone(), tyVars ); | 
|---|
| 82 | } // if | 
|---|
| 83 | } // if | 
|---|
| 84 | return ret; | 
|---|
| 85 | } | 
|---|
| 86 |  | 
|---|
| 87 | void FindFunction::premutate( PointerType * pointerType ) { | 
|---|
| 88 | GuardScope( tyVars ); | 
|---|
| 89 | handleForall( pointerType->get_forall() ); | 
|---|
| 90 | } | 
|---|
| 91 | } // namespace GenPoly | 
|---|
| 92 |  | 
|---|
| 93 | // Local Variables: // | 
|---|
| 94 | // tab-width: 4 // | 
|---|
| 95 | // mode: c++ // | 
|---|
| 96 | // compile-command: "make install" // | 
|---|
| 97 | // End: // | 
|---|