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 Dec 15 16:11:18 2015 |
---|
13 | // Update Count : 13 |
---|
14 | // |
---|
15 | |
---|
16 | #include "GenPoly.h" |
---|
17 | |
---|
18 | #include "SymTab/Mangler.h" |
---|
19 | #include "SynTree/Expression.h" |
---|
20 | #include "SynTree/Type.h" |
---|
21 | |
---|
22 | #include <iostream> |
---|
23 | using namespace std; |
---|
24 | |
---|
25 | namespace GenPoly { |
---|
26 | bool needsAdapter( FunctionType *adaptee, const TyVarMap &tyVars ) { |
---|
27 | if ( ! adaptee->get_returnVals().empty() && isPolyType( adaptee->get_returnVals().front()->get_type(), tyVars ) ) { |
---|
28 | return true; |
---|
29 | } // if |
---|
30 | for ( std::list< DeclarationWithType* >::const_iterator innerArg = adaptee->get_parameters().begin(); innerArg != adaptee->get_parameters().end(); ++innerArg ) { |
---|
31 | if ( isPolyType( (*innerArg)->get_type(), tyVars ) ) { |
---|
32 | return true; |
---|
33 | } // if |
---|
34 | } // for |
---|
35 | return false; |
---|
36 | } |
---|
37 | |
---|
38 | bool isPolyRet( FunctionType *function, std::string &name, const TyVarMap &otherTyVars ) { |
---|
39 | bool doTransform = false; |
---|
40 | if ( ! function->get_returnVals().empty() ) { |
---|
41 | if ( TypeInstType *typeInst = dynamic_cast< TypeInstType *>( function->get_returnVals().front()->get_type() ) ) { |
---|
42 | |
---|
43 | // figure out if the return type is specified by a type parameter |
---|
44 | for ( std::list< TypeDecl *>::const_iterator tyVar = function->get_forall().begin(); tyVar != function->get_forall().end(); ++tyVar ) { |
---|
45 | if ( (*tyVar)->get_name() == typeInst->get_name() ) { |
---|
46 | doTransform = true; |
---|
47 | name = typeInst->get_name(); |
---|
48 | break; |
---|
49 | } // if |
---|
50 | } // for |
---|
51 | if ( ! doTransform && otherTyVars.find( typeInst->get_name() ) != otherTyVars.end() ) { |
---|
52 | doTransform = true; |
---|
53 | } // if |
---|
54 | } // if |
---|
55 | } // if |
---|
56 | return doTransform; |
---|
57 | } |
---|
58 | |
---|
59 | bool isPolyRet( FunctionType *function, std::string &name ) { |
---|
60 | TyVarMap dummyTyVars; |
---|
61 | return isPolyRet( function, name, dummyTyVars ); |
---|
62 | } |
---|
63 | |
---|
64 | bool isPolyRet( FunctionType *function, const TyVarMap &otherTyVars ) { |
---|
65 | std::string dummyString; |
---|
66 | return isPolyRet( function, dummyString, otherTyVars ); |
---|
67 | } |
---|
68 | |
---|
69 | namespace { |
---|
70 | /// Checks a parameter list for polymorphic parameters |
---|
71 | bool hasPolyParams( std::list< Expression* >& params, const TyVarMap &tyVars, const TypeSubstitution *env ) { |
---|
72 | for ( std::list< Expression* >::iterator param = params.begin(); param != params.end(); ++param ) { |
---|
73 | TypeExpr *paramType = dynamic_cast< TypeExpr* >( *param ); |
---|
74 | assert(paramType && "Aggregate parameters should be type expressions"); |
---|
75 | if ( isPolyType( paramType->get_type(), tyVars, env ) ) return true; |
---|
76 | } |
---|
77 | return false; |
---|
78 | } |
---|
79 | } |
---|
80 | |
---|
81 | Type *isPolyType( Type *type, const TyVarMap &tyVars, const TypeSubstitution *env ) { |
---|
82 | if ( TypeInstType *typeInst = dynamic_cast< TypeInstType * >( type ) ) { |
---|
83 | if ( env ) { |
---|
84 | if ( Type *newType = env->lookup( typeInst->get_name() ) ) { |
---|
85 | return isPolyType( newType, tyVars, env ); |
---|
86 | } // if |
---|
87 | } // if |
---|
88 | if ( tyVars.find( typeInst->get_name() ) != tyVars.end() ) { |
---|
89 | return type; |
---|
90 | } |
---|
91 | } else if ( StructInstType *structType = dynamic_cast< StructInstType* >( type ) ) { |
---|
92 | if ( hasPolyParams( structType->get_parameters(), tyVars, env ) ) return type; |
---|
93 | } else if ( UnionInstType *unionType = dynamic_cast< UnionInstType* >( type ) ) { |
---|
94 | if ( hasPolyParams( unionType->get_parameters(), tyVars, env ) ) return type; |
---|
95 | } |
---|
96 | return 0; |
---|
97 | } |
---|
98 | |
---|
99 | Type *isPolyPtr( Type *type, const TyVarMap &tyVars, const TypeSubstitution *env ) { |
---|
100 | if ( PointerType *ptr = dynamic_cast< PointerType *>( type ) ) { |
---|
101 | return isPolyType( ptr->get_base(), tyVars, env ); |
---|
102 | } else if ( env ) { |
---|
103 | if ( TypeInstType *typeInst = dynamic_cast< TypeInstType *>( type ) ) { |
---|
104 | if ( Type *newType = env->lookup( typeInst->get_name() ) ) { |
---|
105 | return isPolyPtr( newType, tyVars, env ); |
---|
106 | } // if |
---|
107 | } // if |
---|
108 | } // if |
---|
109 | return 0; |
---|
110 | } |
---|
111 | |
---|
112 | void printTyVarMap( std::ostream &os, const TyVarMap &tyVarMap ) { |
---|
113 | for ( TyVarMap::const_iterator i = tyVarMap.begin(); i != tyVarMap.end(); ++i ) { |
---|
114 | os << i->first << " (" << i->second << ") "; |
---|
115 | } // for |
---|
116 | os << std::endl; |
---|
117 | } |
---|
118 | |
---|
119 | std::string sizeofName( Type *ty ) { |
---|
120 | return std::string( "_sizeof_" ) + SymTab::Mangler::mangle( ty, false, false ); |
---|
121 | } |
---|
122 | |
---|
123 | std::string alignofName( Type *ty ) { |
---|
124 | return std::string( "_alignof_" ) + SymTab::Mangler::mangle( ty, false, false ); |
---|
125 | } |
---|
126 | } // namespace GenPoly |
---|
127 | |
---|
128 | // Local Variables: // |
---|
129 | // tab-width: 4 // |
---|
130 | // mode: c++ // |
---|
131 | // compile-command: "make install" // |
---|
132 | // End: // |
---|