[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
|
---|
[c97b448] | 11 | // Last Modified By : Andrew Beach
|
---|
| 12 | // Last Modified On : Fri Oct 7 17:05:20 2022
|
---|
| 13 | // Update Count : 7
|
---|
[51587aa] | 14 | //
|
---|
[51b73452] | 15 |
|
---|
| 16 | #include "FindFunction.h"
|
---|
| 17 |
|
---|
[08fc48f] | 18 | #include <utility> // for pair
|
---|
| 19 |
|
---|
[c97b448] | 20 | #include "AST/Pass.hpp" // for Pass
|
---|
| 21 | #include "AST/Type.hpp"
|
---|
[08fc48f] | 22 | #include "GenPoly/ErasableScopedMap.h" // for ErasableScopedMap<>::iterator
|
---|
| 23 | #include "GenPoly/GenPoly.h" // for TyVarMap
|
---|
| 24 | #include "ScrubTyVars.h" // for ScrubTyVars
|
---|
[b4cd03b7] | 25 |
|
---|
[51b73452] | 26 | namespace GenPoly {
|
---|
[c97b448] | 27 |
|
---|
| 28 | namespace {
|
---|
| 29 |
|
---|
| 30 | struct FindFunctionCore :
|
---|
| 31 | public ast::WithGuards,
|
---|
| 32 | public ast::WithShortCircuiting,
|
---|
| 33 | public ast::WithVisitorRef<FindFunctionCore> {
|
---|
| 34 | FindFunctionCore(
|
---|
| 35 | std::vector<ast::ptr<ast::FunctionType>> & functions,
|
---|
| 36 | const TypeVarMap & typeVars, FindFunctionPred predicate,
|
---|
| 37 | bool replaceMode );
|
---|
| 38 |
|
---|
| 39 | void previsit( ast::FunctionType const * type );
|
---|
| 40 | ast::Type const * postvisit( ast::FunctionType const * type );
|
---|
| 41 | void previsit( ast::PointerType const * type );
|
---|
| 42 | private:
|
---|
| 43 | void handleForall( const ast::FunctionType::ForallList & forall );
|
---|
| 44 |
|
---|
| 45 | std::vector<ast::ptr<ast::FunctionType>> &functions;
|
---|
| 46 | TypeVarMap typeVars;
|
---|
| 47 | FindFunctionPred predicate;
|
---|
| 48 | bool replaceMode;
|
---|
| 49 | };
|
---|
| 50 |
|
---|
| 51 | FindFunctionCore::FindFunctionCore(
|
---|
| 52 | std::vector<ast::ptr<ast::FunctionType>> & functions,
|
---|
| 53 | const TypeVarMap &typeVars, FindFunctionPred predicate,
|
---|
| 54 | bool replaceMode ) :
|
---|
| 55 | functions( functions ), typeVars( typeVars ),
|
---|
| 56 | predicate( predicate ), replaceMode( replaceMode ) {}
|
---|
| 57 |
|
---|
| 58 | void FindFunctionCore::handleForall( const ast::FunctionType::ForallList & forall ) {
|
---|
| 59 | for ( const ast::ptr<ast::TypeInstType> & td : forall ) {
|
---|
| 60 | TypeVarMap::iterator var = typeVars.find( *td );
|
---|
| 61 | if ( var != typeVars.end() ) {
|
---|
| 62 | typeVars.erase( var->first );
|
---|
| 63 | } // if
|
---|
| 64 | } // for
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | void FindFunctionCore::previsit( ast::FunctionType const * type ) {
|
---|
| 68 | visit_children = false;
|
---|
| 69 | GuardScope( typeVars );
|
---|
| 70 | handleForall( type->forall );
|
---|
| 71 | //ast::accept_all( type->returns, *visitor );
|
---|
| 72 | // This might have to become ast::mutate_each with return.
|
---|
| 73 | ast::accept_each( type->returns, *visitor );
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | ast::Type const * FindFunctionCore::postvisit( ast::FunctionType const * type ) {
|
---|
| 77 | ast::Type const * ret = type;
|
---|
| 78 | if ( predicate( type, typeVars ) ) {
|
---|
| 79 | functions.push_back( type );
|
---|
| 80 | if ( replaceMode ) {
|
---|
| 81 | // replace type parameters in function type with void*
|
---|
| 82 | ret = scrubTypeVars( ast::deepCopy( type ), typeVars );
|
---|
| 83 | } // if
|
---|
| 84 | } // if
|
---|
| 85 | return ret;
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | void FindFunctionCore::previsit( ast::PointerType const * /*type*/ ) {
|
---|
| 89 | GuardScope( typeVars );
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | } // namespace
|
---|
| 93 |
|
---|
| 94 | void findFunction( const ast::Type * type,
|
---|
| 95 | std::vector<ast::ptr<ast::FunctionType>> & functions,
|
---|
| 96 | const TypeVarMap & typeVars, FindFunctionPred predicate ) {
|
---|
| 97 | ast::Pass<FindFunctionCore> pass( functions, typeVars, predicate, false );
|
---|
| 98 | type->accept( pass );
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 | const ast::Type * findAndReplaceFunction( const ast::Type * type,
|
---|
| 102 | std::vector<ast::ptr<ast::FunctionType>> & functions,
|
---|
| 103 | const TypeVarMap & typeVars, FindFunctionPred predicate ) {
|
---|
| 104 | ast::Pass<FindFunctionCore> pass( functions, typeVars, predicate, true );
|
---|
| 105 | return type->accept( pass );
|
---|
| 106 | }
|
---|
| 107 |
|
---|
[51b73452] | 108 | } // namespace GenPoly
|
---|
[01aeade] | 109 |
|
---|
[51587aa] | 110 | // Local Variables: //
|
---|
| 111 | // tab-width: 4 //
|
---|
| 112 | // mode: c++ //
|
---|
| 113 | // compile-command: "make install" //
|
---|
| 114 | // End: //
|
---|