[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
|
---|
[e56cfdb0] | 12 | // Last Modified On : Thu Nov 19 17:23:44 2015
|
---|
| 13 | // Update Count : 10
|
---|
[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 {
|
---|
[e56cfdb0] | 23 | // A function needs an adapter if it returns a polymorphic value or if any of its
|
---|
| 24 | // parameters have polymorphic type
|
---|
| 25 | bool needsAdapter( FunctionType *adaptee, const TyVarMap &tyVars ) {
|
---|
| 26 | if ( ! adaptee->get_returnVals().empty() && isPolyVal( adaptee->get_returnVals().front()->get_type(), tyVars ) ) {
|
---|
| 27 | return true;
|
---|
| 28 | } // if
|
---|
| 29 | for ( std::list< DeclarationWithType* >::const_iterator innerArg = adaptee->get_parameters().begin(); innerArg != adaptee->get_parameters().end(); ++innerArg ) {
|
---|
| 30 | if ( isPolyVal( (*innerArg)->get_type(), tyVars ) ) {
|
---|
| 31 | return true;
|
---|
| 32 | } // if
|
---|
| 33 | } // for
|
---|
| 34 | return false;
|
---|
| 35 | }
|
---|
| 36 |
|
---|
| 37 | bool isPolyRet( FunctionType *function, std::string &name, const TyVarMap &otherTyVars ) {
|
---|
| 38 | bool doTransform = false;
|
---|
| 39 | if ( ! function->get_returnVals().empty() ) {
|
---|
| 40 | if ( TypeInstType *typeInst = dynamic_cast< TypeInstType *>( function->get_returnVals().front()->get_type() ) ) {
|
---|
| 41 |
|
---|
| 42 | // figure out if the return type is specified by a type parameter
|
---|
| 43 | for ( std::list< TypeDecl *>::const_iterator tyVar = function->get_forall().begin(); tyVar != function->get_forall().end(); ++tyVar ) {
|
---|
| 44 | if ( (*tyVar)->get_name() == typeInst->get_name() ) {
|
---|
| 45 | doTransform = true;
|
---|
| 46 | name = typeInst->get_name();
|
---|
| 47 | break;
|
---|
| 48 | } // if
|
---|
| 49 | } // for
|
---|
| 50 | if ( ! doTransform && otherTyVars.find( typeInst->get_name() ) != otherTyVars.end() ) {
|
---|
| 51 | doTransform = true;
|
---|
| 52 | } // if
|
---|
| 53 | } // if
|
---|
| 54 | } // if
|
---|
| 55 | return doTransform;
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | bool isPolyRet( FunctionType *function, std::string &name ) {
|
---|
| 59 | TyVarMap dummyTyVars;
|
---|
| 60 | return isPolyRet( function, name, dummyTyVars );
|
---|
[01aeade] | 61 | }
|
---|
[51b73452] | 62 |
|
---|
[e56cfdb0] | 63 | bool isPolyRet( FunctionType *function, const TyVarMap &otherTyVars ) {
|
---|
| 64 | std::string dummyString;
|
---|
| 65 | return isPolyRet( function, dummyString, otherTyVars );
|
---|
[01aeade] | 66 | }
|
---|
[b1a6d6b] | 67 |
|
---|
[e56cfdb0] | 68 | bool isPolyVal( Type *type, const TyVarMap &tyVars ) {
|
---|
| 69 | if ( TypeInstType *typeInst = dynamic_cast< TypeInstType * >( type ) ) {
|
---|
[01aeade] | 70 | if ( tyVars.find( typeInst->get_name() ) != tyVars.end() ) {
|
---|
| 71 | return true;
|
---|
| 72 | } // if
|
---|
| 73 | } // if
|
---|
| 74 | return false;
|
---|
| 75 | }
|
---|
[b1a6d6b] | 76 |
|
---|
[01aeade] | 77 | void printTyVarMap( std::ostream &os, const TyVarMap &tyVarMap ) {
|
---|
| 78 | for ( TyVarMap::const_iterator i = tyVarMap.begin(); i != tyVarMap.end(); ++i ) {
|
---|
| 79 | os << i->first << " (" << i->second << ") ";
|
---|
| 80 | } // for
|
---|
| 81 | os << std::endl;
|
---|
| 82 | }
|
---|
[51b73452] | 83 | } // namespace GenPoly
|
---|
[01aeade] | 84 |
|
---|
[51587aa] | 85 | // Local Variables: //
|
---|
| 86 | // tab-width: 4 //
|
---|
| 87 | // mode: c++ //
|
---|
| 88 | // compile-command: "make install" //
|
---|
| 89 | // End: //
|
---|