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