| 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 | // GenPoly.cc -- | 
|---|
| 8 | // | 
|---|
| 9 | // Author           : Richard C. Bilson | 
|---|
| 10 | // Created On       : Mon May 18 07:44:20 2015 | 
|---|
| 11 | // Last Modified By : Peter A. Buhr | 
|---|
| 12 | // Last Modified On : Tue May 19 07:37:46 2015 | 
|---|
| 13 | // Update Count     : 1 | 
|---|
| 14 | // | 
|---|
| 15 |  | 
|---|
| 16 | #include "GenPoly.h" | 
|---|
| 17 | #include "SynTree/Type.h" | 
|---|
| 18 |  | 
|---|
| 19 | #include <iostream> | 
|---|
| 20 | using namespace std; | 
|---|
| 21 |  | 
|---|
| 22 | namespace GenPoly { | 
|---|
| 23 | // interface functions | 
|---|
| 24 | bool isPolyVal( Type *type, const TyVarMap &tyVars ) { | 
|---|
| 25 | return isPolyVal( type, tyVars, false ); | 
|---|
| 26 | } | 
|---|
| 27 |  | 
|---|
| 28 | bool needsAdapter( FunctionType *adaptee, const TyVarMap &tyVars ) { | 
|---|
| 29 | return needsAdapter( adaptee, tyVars, false ); | 
|---|
| 30 | } | 
|---|
| 31 |  | 
|---|
| 32 | bool isPolyVal( Type *type, const TyVarMap &tyVars, bool considerAllTyVars ) { | 
|---|
| 33 | if ( TypeInstType *typeInst = dynamic_cast< TypeInstType* >( type ) ) { | 
|---|
| 34 | if ( tyVars.find( typeInst->get_name() ) != tyVars.end() ) { | 
|---|
| 35 | return true; | 
|---|
| 36 | } // if | 
|---|
| 37 | return considerAllTyVars; | 
|---|
| 38 | } // if | 
|---|
| 39 | return false; | 
|---|
| 40 | } | 
|---|
| 41 |  | 
|---|
| 42 | // A function needs an adapter if it returns a polymorphic value or if any of its | 
|---|
| 43 | // parameters have polymorphic type | 
|---|
| 44 | bool needsAdapter( FunctionType *adaptee, const TyVarMap &tyVars, bool considerAllTyVars ) { | 
|---|
| 45 | bool needsAdapter = false; | 
|---|
| 46 | if ( ! adaptee->get_returnVals().empty() && isPolyVal( adaptee->get_returnVals().front()->get_type(), tyVars, considerAllTyVars ) ) { | 
|---|
| 47 | needsAdapter = true; | 
|---|
| 48 | } // if | 
|---|
| 49 | for ( std::list< DeclarationWithType* >::const_iterator innerArg = adaptee->get_parameters().begin(); ! needsAdapter && innerArg != adaptee->get_parameters().end(); ++innerArg ) { | 
|---|
| 50 | if ( isPolyVal( (*innerArg)->get_type(), tyVars, considerAllTyVars ) ) { | 
|---|
| 51 | needsAdapter = true; | 
|---|
| 52 | } // if | 
|---|
| 53 | } // for | 
|---|
| 54 | return needsAdapter; | 
|---|
| 55 | } | 
|---|
| 56 |  | 
|---|
| 57 | void printTyVarMap( std::ostream &os, const TyVarMap &tyVarMap ) { | 
|---|
| 58 | for ( TyVarMap::const_iterator i = tyVarMap.begin(); i != tyVarMap.end(); ++i ) { | 
|---|
| 59 | os << i->first << " (" << i->second << ") "; | 
|---|
| 60 | } // for | 
|---|
| 61 | os << std::endl; | 
|---|
| 62 | } | 
|---|
| 63 | } // namespace GenPoly | 
|---|
| 64 |  | 
|---|
| 65 | // Local Variables: // | 
|---|
| 66 | // tab-width: 4 // | 
|---|
| 67 | // mode: c++ // | 
|---|
| 68 | // compile-command: "make install" // | 
|---|
| 69 | // End: // | 
|---|