| [51587aa] | 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 | //
 | 
|---|
| [b4cd03b7] | 7 | // FindFunction.cc --
 | 
|---|
| [51587aa] | 8 | //
 | 
|---|
 | 9 | // Author           : Richard C. Bilson
 | 
|---|
 | 10 | // Created On       : Mon May 18 07:44:20 2015
 | 
|---|
| [b4cd03b7] | 11 | // Last Modified By : Rob Schluntz
 | 
|---|
 | 12 | // Last Modified On : Fri Feb 05 12:22:20 2016
 | 
|---|
 | 13 | // Update Count     : 6
 | 
|---|
| [51587aa] | 14 | //
 | 
|---|
| [51b73452] | 15 | 
 | 
|---|
 | 16 | #include "FindFunction.h"
 | 
|---|
 | 17 | 
 | 
|---|
| [08fc48f] | 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...
 | 
|---|
| [b4cd03b7] | 27 | 
 | 
|---|
| [51b73452] | 28 | namespace GenPoly {
 | 
|---|
| [01aeade] | 29 |         class FindFunction : public Mutator {
 | 
|---|
 | 30 |           public:
 | 
|---|
 | 31 |                 FindFunction( std::list< FunctionType* > &functions, const TyVarMap &tyVars, bool replaceMode, FindFunctionPredicate predicate );
 | 
|---|
| [b4cd03b7] | 32 | 
 | 
|---|
| [01aeade] | 33 |                 virtual Type *mutate( FunctionType *functionType );
 | 
|---|
 | 34 |                 virtual Type *mutate( PointerType *pointerType );
 | 
|---|
 | 35 |           private:
 | 
|---|
| [8c49c0e] | 36 |                 void handleForall( const Type::ForallList &forall );
 | 
|---|
| [51b73452] | 37 | 
 | 
|---|
| [01aeade] | 38 |                 std::list< FunctionType* > &functions;
 | 
|---|
 | 39 |                 TyVarMap tyVars;
 | 
|---|
 | 40 |                 bool replaceMode;
 | 
|---|
 | 41 |                 FindFunctionPredicate predicate;
 | 
|---|
 | 42 |         };
 | 
|---|
| [51b73452] | 43 | 
 | 
|---|
| [01aeade] | 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 |         }
 | 
|---|
| [51b73452] | 48 | 
 | 
|---|
| [01aeade] | 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 |         }
 | 
|---|
| [51b73452] | 53 | 
 | 
|---|
| [01aeade] | 54 |         FindFunction::FindFunction( std::list< FunctionType* > &functions, const TyVarMap &tyVars, bool replaceMode, FindFunctionPredicate predicate )
 | 
|---|
 | 55 |                 : functions( functions ), tyVars( tyVars ), replaceMode( replaceMode ), predicate( predicate ) {
 | 
|---|
 | 56 |         }
 | 
|---|
| [51b73452] | 57 | 
 | 
|---|
| [8c49c0e] | 58 |         void FindFunction::handleForall( const Type::ForallList &forall ) {
 | 
|---|
 | 59 |                 for ( Type::ForallList::const_iterator i = forall.begin(); i != forall.end(); ++i ) {
 | 
|---|
| [01aeade] | 60 |                         TyVarMap::iterator var = tyVars.find( (*i)->get_name() );
 | 
|---|
 | 61 |                         if ( var != tyVars.end() ) {
 | 
|---|
| [bfae637] | 62 |                                 tyVars.erase( var->first );
 | 
|---|
| [01aeade] | 63 |                         } // if
 | 
|---|
 | 64 |                 } // for
 | 
|---|
 | 65 |         }
 | 
|---|
| [51b73452] | 66 | 
 | 
|---|
| [01aeade] | 67 |         Type * FindFunction::mutate( FunctionType *functionType ) {
 | 
|---|
| [6f49cdf] | 68 |                 tyVars.beginScope();
 | 
|---|
| [01aeade] | 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 ) {
 | 
|---|
| [b4cd03b7] | 75 |                                 // replace type parameters in function type with void*
 | 
|---|
 | 76 |                                 ret = ScrubTyVars::scrub( functionType->clone(), tyVars );
 | 
|---|
| [01aeade] | 77 |                         } // if
 | 
|---|
 | 78 |                 } // if
 | 
|---|
| [6f49cdf] | 79 |                 tyVars.endScope();
 | 
|---|
| [01aeade] | 80 |                 return ret;
 | 
|---|
 | 81 |         }
 | 
|---|
| [51b73452] | 82 | 
 | 
|---|
| [01aeade] | 83 |         Type * FindFunction::mutate( PointerType *pointerType ) {
 | 
|---|
| [6f49cdf] | 84 |                 tyVars.beginScope();
 | 
|---|
| [01aeade] | 85 |                 handleForall( pointerType->get_forall() );
 | 
|---|
 | 86 |                 Type *ret = Mutator::mutate( pointerType );
 | 
|---|
| [6f49cdf] | 87 |                 tyVars.endScope();
 | 
|---|
| [01aeade] | 88 |                 return ret;
 | 
|---|
 | 89 |         }
 | 
|---|
| [51b73452] | 90 | } // namespace GenPoly
 | 
|---|
| [01aeade] | 91 | 
 | 
|---|
| [51587aa] | 92 | // Local Variables: //
 | 
|---|
 | 93 | // tab-width: 4 //
 | 
|---|
 | 94 | // mode: c++ //
 | 
|---|
 | 95 | // compile-command: "make install" //
 | 
|---|
 | 96 | // End: //
 | 
|---|