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