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