| 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 | #include "SynTree/Type.h"
 | 
|---|
| 18 | #include "SynTree/Declaration.h"
 | 
|---|
| 19 | #include "SynTree/Visitor.h"
 | 
|---|
| 20 | 
 | 
|---|
| 21 | #include "ScrubTyVars.h"
 | 
|---|
| 22 | 
 | 
|---|
| 23 | namespace GenPoly {
 | 
|---|
| 24 |         class FindFunction : public Mutator {
 | 
|---|
| 25 |           public:
 | 
|---|
| 26 |                 FindFunction( std::list< FunctionType* > &functions, const TyVarMap &tyVars, bool replaceMode, FindFunctionPredicate predicate );
 | 
|---|
| 27 | 
 | 
|---|
| 28 |                 virtual Type *mutate( FunctionType *functionType );
 | 
|---|
| 29 |                 virtual Type *mutate( PointerType *pointerType );
 | 
|---|
| 30 |           private:
 | 
|---|
| 31 |                 void handleForall( const std::list< TypeDecl* > &forall );
 | 
|---|
| 32 | 
 | 
|---|
| 33 |                 std::list< FunctionType* > &functions;
 | 
|---|
| 34 |                 TyVarMap tyVars;
 | 
|---|
| 35 |                 bool replaceMode;
 | 
|---|
| 36 |                 FindFunctionPredicate predicate;
 | 
|---|
| 37 |         };
 | 
|---|
| 38 | 
 | 
|---|
| 39 |         void findFunction( Type *type, std::list< FunctionType* > &functions, const TyVarMap &tyVars, FindFunctionPredicate predicate ) {
 | 
|---|
| 40 |                 FindFunction finder( functions, tyVars, false, predicate );
 | 
|---|
| 41 |                 type->acceptMutator( finder );
 | 
|---|
| 42 |         }
 | 
|---|
| 43 | 
 | 
|---|
| 44 |         void findAndReplaceFunction( Type *&type, std::list< FunctionType* > &functions, const TyVarMap &tyVars, FindFunctionPredicate predicate ) {
 | 
|---|
| 45 |                 FindFunction finder( functions, tyVars, true, predicate );
 | 
|---|
| 46 |                 type = type->acceptMutator( finder );
 | 
|---|
| 47 |         }
 | 
|---|
| 48 | 
 | 
|---|
| 49 |         FindFunction::FindFunction( std::list< FunctionType* > &functions, const TyVarMap &tyVars, bool replaceMode, FindFunctionPredicate predicate )
 | 
|---|
| 50 |                 : functions( functions ), tyVars( tyVars ), replaceMode( replaceMode ), predicate( predicate ) {
 | 
|---|
| 51 |         }
 | 
|---|
| 52 | 
 | 
|---|
| 53 |         void FindFunction::handleForall( const std::list< TypeDecl* > &forall ) {
 | 
|---|
| 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->first );
 | 
|---|
| 58 |                         } // if
 | 
|---|
| 59 |                 } // for
 | 
|---|
| 60 |         }
 | 
|---|
| 61 | 
 | 
|---|
| 62 |         Type * FindFunction::mutate( FunctionType *functionType ) {
 | 
|---|
| 63 |                 tyVars.beginScope();
 | 
|---|
| 64 |                 handleForall( functionType->get_forall() );
 | 
|---|
| 65 |                 mutateAll( functionType->get_returnVals(), *this );
 | 
|---|
| 66 |                 Type *ret = functionType;
 | 
|---|
| 67 |                 if ( predicate( functionType, tyVars ) ) {
 | 
|---|
| 68 |                         functions.push_back( functionType );
 | 
|---|
| 69 |                         if ( replaceMode ) {
 | 
|---|
| 70 |                                 // replace type parameters in function type with void*
 | 
|---|
| 71 |                                 ret = ScrubTyVars::scrub( functionType->clone(), tyVars );
 | 
|---|
| 72 |                         } // if
 | 
|---|
| 73 |                 } // if
 | 
|---|
| 74 |                 tyVars.endScope();
 | 
|---|
| 75 |                 return ret;
 | 
|---|
| 76 |         }
 | 
|---|
| 77 | 
 | 
|---|
| 78 |         Type * FindFunction::mutate( PointerType *pointerType ) {
 | 
|---|
| 79 |                 tyVars.beginScope();
 | 
|---|
| 80 |                 handleForall( pointerType->get_forall() );
 | 
|---|
| 81 |                 Type *ret = Mutator::mutate( pointerType );
 | 
|---|
| 82 |                 tyVars.endScope();
 | 
|---|
| 83 |                 return ret;
 | 
|---|
| 84 |         }
 | 
|---|
| 85 | } // namespace GenPoly
 | 
|---|
| 86 | 
 | 
|---|
| 87 | // Local Variables: //
 | 
|---|
| 88 | // tab-width: 4 //
 | 
|---|
| 89 | // mode: c++ //
 | 
|---|
| 90 | // compile-command: "make install" //
 | 
|---|
| 91 | // End: //
 | 
|---|