| 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 | // | 
|---|
| 7 | // FindFunction.cpp -- Find function types in a larger type. | 
|---|
| 8 | // | 
|---|
| 9 | // Author           : Richard C. Bilson | 
|---|
| 10 | // Created On       : Mon May 18 07:44:20 2015 | 
|---|
| 11 | // Last Modified By : Andrew Beach | 
|---|
| 12 | // Last Modified On : Fri Oct  7 17:05:20 2022 | 
|---|
| 13 | // Update Count     : 7 | 
|---|
| 14 | // | 
|---|
| 15 |  | 
|---|
| 16 | #include "FindFunction.hpp" | 
|---|
| 17 |  | 
|---|
| 18 | #include <utility>                        // for pair | 
|---|
| 19 |  | 
|---|
| 20 | #include "AST/Pass.hpp"                   // for Pass | 
|---|
| 21 | #include "AST/Type.hpp" | 
|---|
| 22 | #include "GenPoly/ErasableScopedMap.hpp"  // for ErasableScopedMap<>::iterator | 
|---|
| 23 | #include "GenPoly/GenPoly.hpp"            // for TyVarMap | 
|---|
| 24 | #include "ScrubTypeVars.hpp"              // for scrubTypeVars | 
|---|
| 25 |  | 
|---|
| 26 | namespace GenPoly { | 
|---|
| 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_each( type->returns, *visitor ); | 
|---|
| 72 | } | 
|---|
| 73 |  | 
|---|
| 74 | ast::Type const * FindFunctionCore::postvisit( ast::FunctionType const * type ) { | 
|---|
| 75 | ast::Type const * ret = type; | 
|---|
| 76 | if ( predicate( type, typeVars ) ) { | 
|---|
| 77 | functions.push_back( type ); | 
|---|
| 78 | if ( replaceMode ) { | 
|---|
| 79 | // Replace type parameters in function type with void *. | 
|---|
| 80 | ret = scrubTypeVars( ast::deepCopy( type ), typeVars ); | 
|---|
| 81 | } // if | 
|---|
| 82 | } // if | 
|---|
| 83 | return ret; | 
|---|
| 84 | } | 
|---|
| 85 |  | 
|---|
| 86 | void FindFunctionCore::previsit( ast::PointerType const * /*type*/ ) { | 
|---|
| 87 | GuardScope( typeVars ); | 
|---|
| 88 | } | 
|---|
| 89 |  | 
|---|
| 90 | } // namespace | 
|---|
| 91 |  | 
|---|
| 92 | void findFunction( const ast::Type * type, | 
|---|
| 93 | std::vector<ast::ptr<ast::FunctionType>> & functions, | 
|---|
| 94 | const TypeVarMap & typeVars, FindFunctionPred predicate ) { | 
|---|
| 95 | ast::Pass<FindFunctionCore> pass( functions, typeVars, predicate, false ); | 
|---|
| 96 | type->accept( pass ); | 
|---|
| 97 | } | 
|---|
| 98 |  | 
|---|
| 99 | const ast::Type * findAndReplaceFunction( const ast::Type * type, | 
|---|
| 100 | std::vector<ast::ptr<ast::FunctionType>> & functions, | 
|---|
| 101 | const TypeVarMap & typeVars, FindFunctionPred predicate ) { | 
|---|
| 102 | ast::Pass<FindFunctionCore> pass( functions, typeVars, predicate, true ); | 
|---|
| 103 | return type->accept( pass ); | 
|---|
| 104 | } | 
|---|
| 105 |  | 
|---|
| 106 | } // namespace GenPoly | 
|---|
| 107 |  | 
|---|
| 108 | // Local Variables: // | 
|---|
| 109 | // tab-width: 4 // | 
|---|
| 110 | // mode: c++ // | 
|---|
| 111 | // compile-command: "make install" // | 
|---|
| 112 | // End: // | 
|---|