| 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 : Peter A. Buhr
 | 
|---|
| 12 | // Last Modified On : Tue May 19 07:35:48 2015
 | 
|---|
| 13 | // Update Count     : 1
 | 
|---|
| 14 | //
 | 
|---|
| 15 | 
 | 
|---|
| 16 | #include "FindFunction.h"
 | 
|---|
| 17 | #include "SynTree/Type.h"
 | 
|---|
| 18 | #include "SynTree/Declaration.h"
 | 
|---|
| 19 | #include "SynTree/Visitor.h"
 | 
|---|
| 20 | 
 | 
|---|
| 21 | namespace GenPoly {
 | 
|---|
| 22 |         class FindFunction : public Mutator {
 | 
|---|
| 23 |           public:
 | 
|---|
| 24 |                 FindFunction( std::list< FunctionType* > &functions, const TyVarMap &tyVars, bool replaceMode, FindFunctionPredicate predicate );
 | 
|---|
| 25 |   
 | 
|---|
| 26 |                 virtual Type *mutate( FunctionType *functionType );
 | 
|---|
| 27 |                 virtual Type *mutate( PointerType *pointerType );
 | 
|---|
| 28 |           private:
 | 
|---|
| 29 |                 void handleForall( const std::list< TypeDecl* > &forall );
 | 
|---|
| 30 | 
 | 
|---|
| 31 |                 std::list< FunctionType* > &functions;
 | 
|---|
| 32 |                 TyVarMap tyVars;
 | 
|---|
| 33 |                 bool replaceMode;
 | 
|---|
| 34 |                 FindFunctionPredicate predicate;
 | 
|---|
| 35 |         };
 | 
|---|
| 36 | 
 | 
|---|
| 37 |         void findFunction( Type *type, std::list< FunctionType* > &functions, const TyVarMap &tyVars, FindFunctionPredicate predicate ) {
 | 
|---|
| 38 |                 FindFunction finder( functions, tyVars, false, predicate );
 | 
|---|
| 39 |                 type->acceptMutator( finder );
 | 
|---|
| 40 |         }
 | 
|---|
| 41 | 
 | 
|---|
| 42 |         void findAndReplaceFunction( Type *&type, std::list< FunctionType* > &functions, const TyVarMap &tyVars, FindFunctionPredicate predicate ) {
 | 
|---|
| 43 |                 FindFunction finder( functions, tyVars, true, predicate );
 | 
|---|
| 44 |                 type = type->acceptMutator( finder );
 | 
|---|
| 45 |         }
 | 
|---|
| 46 | 
 | 
|---|
| 47 |         FindFunction::FindFunction( std::list< FunctionType* > &functions, const TyVarMap &tyVars, bool replaceMode, FindFunctionPredicate predicate )
 | 
|---|
| 48 |                 : functions( functions ), tyVars( tyVars ), replaceMode( replaceMode ), predicate( predicate ) {
 | 
|---|
| 49 |         }
 | 
|---|
| 50 | 
 | 
|---|
| 51 |         void FindFunction::handleForall( const std::list< TypeDecl* > &forall ) {
 | 
|---|
| 52 |                 for ( std::list< TypeDecl* >::const_iterator i = forall.begin(); i != forall.end(); ++i ) {
 | 
|---|
| 53 |                         TyVarMap::iterator var = tyVars.find( (*i)->get_name() );
 | 
|---|
| 54 |                         if ( var != tyVars.end() ) {
 | 
|---|
| 55 |                                 tyVars.erase( var );
 | 
|---|
| 56 |                         } // if
 | 
|---|
| 57 |                 } // for
 | 
|---|
| 58 |         }
 | 
|---|
| 59 | 
 | 
|---|
| 60 |         Type * FindFunction::mutate( FunctionType *functionType ) {
 | 
|---|
| 61 |                 TyVarMap oldTyVars = tyVars;
 | 
|---|
| 62 |                 handleForall( functionType->get_forall() );
 | 
|---|
| 63 |                 mutateAll( functionType->get_returnVals(), *this );
 | 
|---|
| 64 |                 Type *ret = functionType;
 | 
|---|
| 65 |                 if ( predicate( functionType, tyVars ) ) {
 | 
|---|
| 66 |                         functions.push_back( functionType );
 | 
|---|
| 67 |                         if ( replaceMode ) {
 | 
|---|
| 68 |                                 ret = new FunctionType( Type::Qualifiers(), true );
 | 
|---|
| 69 |                         } // if
 | 
|---|
| 70 |                 } // if
 | 
|---|
| 71 |                 tyVars = oldTyVars;
 | 
|---|
| 72 |                 return ret;
 | 
|---|
| 73 |         }
 | 
|---|
| 74 | 
 | 
|---|
| 75 |         Type * FindFunction::mutate( PointerType *pointerType ) {
 | 
|---|
| 76 |                 TyVarMap oldTyVars = tyVars;
 | 
|---|
| 77 |                 handleForall( pointerType->get_forall() );
 | 
|---|
| 78 |                 Type *ret = Mutator::mutate( pointerType );
 | 
|---|
| 79 |                 tyVars = oldTyVars;
 | 
|---|
| 80 |                 return ret;
 | 
|---|
| 81 |         }
 | 
|---|
| 82 | } // namespace GenPoly
 | 
|---|
| 83 | 
 | 
|---|
| 84 | // Local Variables: //
 | 
|---|
| 85 | // tab-width: 4 //
 | 
|---|
| 86 | // mode: c++ //
 | 
|---|
| 87 | // compile-command: "make install" //
 | 
|---|
| 88 | // End: //
 | 
|---|