[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 | //
|
---|
[01aeade] | 7 | // FindFunction.cc --
|
---|
[51587aa] | 8 | //
|
---|
| 9 | // Author : Richard C. Bilson
|
---|
| 10 | // Created On : Mon May 18 07:44:20 2015
|
---|
[01aeade] | 11 | // Last Modified By : Peter A. Buhr
|
---|
| 12 | // Last Modified On : Tue May 19 07:35:48 2015
|
---|
| 13 | // Update Count : 1
|
---|
[51587aa] | 14 | //
|
---|
[51b73452] | 15 |
|
---|
| 16 | #include "FindFunction.h"
|
---|
| 17 | #include "SynTree/Type.h"
|
---|
| 18 | #include "SynTree/Declaration.h"
|
---|
| 19 | #include "SynTree/Visitor.h"
|
---|
| 20 |
|
---|
| 21 | namespace GenPoly {
|
---|
[01aeade] | 22 | class FindFunction : public Mutator {
|
---|
| 23 | public:
|
---|
| 24 | FindFunction( std::list< FunctionType* > &functions, const TyVarMap &tyVars, bool replaceMode, FindFunctionPredicate predicate );
|
---|
[51b73452] | 25 |
|
---|
[01aeade] | 26 | virtual Type *mutate( FunctionType *functionType );
|
---|
| 27 | virtual Type *mutate( PointerType *pointerType );
|
---|
| 28 | private:
|
---|
| 29 | void handleForall( const std::list< TypeDecl* > &forall );
|
---|
[51b73452] | 30 |
|
---|
[01aeade] | 31 | std::list< FunctionType* > &functions;
|
---|
| 32 | TyVarMap tyVars;
|
---|
| 33 | bool replaceMode;
|
---|
| 34 | FindFunctionPredicate predicate;
|
---|
| 35 | };
|
---|
[51b73452] | 36 |
|
---|
[01aeade] | 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 | }
|
---|
[51b73452] | 41 |
|
---|
[01aeade] | 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 | }
|
---|
[51b73452] | 46 |
|
---|
[01aeade] | 47 | FindFunction::FindFunction( std::list< FunctionType* > &functions, const TyVarMap &tyVars, bool replaceMode, FindFunctionPredicate predicate )
|
---|
| 48 | : functions( functions ), tyVars( tyVars ), replaceMode( replaceMode ), predicate( predicate ) {
|
---|
| 49 | }
|
---|
[51b73452] | 50 |
|
---|
[01aeade] | 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 | }
|
---|
[51b73452] | 59 |
|
---|
[01aeade] | 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 | }
|
---|
[51b73452] | 74 |
|
---|
[01aeade] | 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 | }
|
---|
[51b73452] | 82 | } // namespace GenPoly
|
---|
[01aeade] | 83 |
|
---|
[51587aa] | 84 | // Local Variables: //
|
---|
| 85 | // tab-width: 4 //
|
---|
| 86 | // mode: c++ //
|
---|
| 87 | // compile-command: "make install" //
|
---|
| 88 | // End: //
|
---|