1 | /* |
---|
2 | * This file is part of the Cforall project |
---|
3 | * |
---|
4 | * $Id: GenPoly.cc,v 1.4 2005/08/29 20:14:13 rcbilson Exp $ |
---|
5 | * |
---|
6 | */ |
---|
7 | |
---|
8 | #include "GenPoly.h" |
---|
9 | #include "SynTree/Type.h" |
---|
10 | |
---|
11 | #include <iostream> |
---|
12 | using namespace std; |
---|
13 | |
---|
14 | namespace GenPoly { |
---|
15 | |
---|
16 | // interface functions |
---|
17 | bool isPolyVal( Type *type, const TyVarMap &tyVars ) { |
---|
18 | return isPolyVal( type, tyVars, false ); |
---|
19 | } |
---|
20 | |
---|
21 | bool needsAdapter( FunctionType *adaptee, const TyVarMap &tyVars ) { |
---|
22 | return needsAdapter( adaptee, tyVars, false ); |
---|
23 | } |
---|
24 | |
---|
25 | bool |
---|
26 | isPolyVal( Type *type, const TyVarMap &tyVars, bool considerAllTyVars ) |
---|
27 | { |
---|
28 | if ( TypeInstType *typeInst = dynamic_cast< TypeInstType* >( type ) ) { |
---|
29 | if ( tyVars.find( typeInst->get_name() ) != tyVars.end() ) { |
---|
30 | return true; |
---|
31 | } |
---|
32 | return considerAllTyVars; |
---|
33 | } |
---|
34 | return false; |
---|
35 | } |
---|
36 | |
---|
37 | // A function needs an adapter if it returns a polymorphic value or if any of its |
---|
38 | // parameters have polymorphic type |
---|
39 | bool |
---|
40 | needsAdapter( FunctionType *adaptee, const TyVarMap &tyVars, bool considerAllTyVars ) |
---|
41 | { |
---|
42 | bool needsAdapter = false; |
---|
43 | if ( ! adaptee->get_returnVals().empty() && isPolyVal( adaptee->get_returnVals().front()->get_type(), tyVars, considerAllTyVars ) ) { |
---|
44 | needsAdapter = true; |
---|
45 | } |
---|
46 | for ( std::list< DeclarationWithType* >::const_iterator innerArg = adaptee->get_parameters().begin(); ! needsAdapter && innerArg != adaptee->get_parameters().end(); ++innerArg ) { |
---|
47 | if ( isPolyVal( (*innerArg)->get_type(), tyVars, considerAllTyVars ) ) { |
---|
48 | needsAdapter = true; |
---|
49 | } |
---|
50 | } |
---|
51 | |
---|
52 | return needsAdapter; |
---|
53 | } |
---|
54 | |
---|
55 | void |
---|
56 | printTyVarMap( std::ostream &os, const TyVarMap &tyVarMap ) |
---|
57 | { |
---|
58 | for ( TyVarMap::const_iterator i = tyVarMap.begin(); i != tyVarMap.end(); ++i ) { |
---|
59 | os << i->first << " (" << i->second << ") "; |
---|
60 | } |
---|
61 | os << std::endl; |
---|
62 | } |
---|
63 | |
---|
64 | } // namespace GenPoly |
---|