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