[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 | // GenPoly.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:37:46 2015
|
---|
| 13 | // Update Count : 1
|
---|
[51587aa] | 14 | //
|
---|
[51b73452] | 15 |
|
---|
| 16 | #include "GenPoly.h"
|
---|
| 17 | #include "SynTree/Type.h"
|
---|
| 18 |
|
---|
[b1a6d6b] | 19 | #include <iostream>
|
---|
| 20 | using namespace std;
|
---|
[51b73452] | 21 |
|
---|
| 22 | namespace GenPoly {
|
---|
[01aeade] | 23 | // interface functions
|
---|
| 24 | bool isPolyVal( Type *type, const TyVarMap &tyVars ) {
|
---|
| 25 | return isPolyVal( type, tyVars, false );
|
---|
| 26 | }
|
---|
[51b73452] | 27 |
|
---|
[01aeade] | 28 | bool needsAdapter( FunctionType *adaptee, const TyVarMap &tyVars ) {
|
---|
| 29 | return needsAdapter( adaptee, tyVars, false );
|
---|
| 30 | }
|
---|
[b1a6d6b] | 31 |
|
---|
[01aeade] | 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 | }
|
---|
[b1a6d6b] | 41 |
|
---|
[01aeade] | 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 | }
|
---|
[51b73452] | 56 |
|
---|
[01aeade] | 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 | }
|
---|
[51b73452] | 63 | } // namespace GenPoly
|
---|
[01aeade] | 64 |
|
---|
[51587aa] | 65 | // Local Variables: //
|
---|
| 66 | // tab-width: 4 //
|
---|
| 67 | // mode: c++ //
|
---|
| 68 | // compile-command: "make install" //
|
---|
| 69 | // End: //
|
---|