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 : Wed Jun 29 21:45:53 2016 |
---|
13 | // Update Count : 14 |
---|
14 | // |
---|
15 | |
---|
16 | #include "GenPoly.h" |
---|
17 | |
---|
18 | #include "SynTree/Expression.h" |
---|
19 | #include "SynTree/Type.h" |
---|
20 | |
---|
21 | #include <iostream> |
---|
22 | using namespace std; |
---|
23 | |
---|
24 | namespace GenPoly { |
---|
25 | bool needsAdapter( FunctionType *adaptee, const TyVarMap &tyVars ) { |
---|
26 | if ( ! adaptee->get_returnVals().empty() && isPolyType( 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 ( isPolyType( (*innerArg)->get_type(), tyVars ) ) { |
---|
31 | return true; |
---|
32 | } // if |
---|
33 | } // for |
---|
34 | return false; |
---|
35 | } |
---|
36 | |
---|
37 | ReferenceToType *isPolyRet( FunctionType *function ) { |
---|
38 | if ( ! function->get_returnVals().empty() ) { |
---|
39 | TyVarMap forallTypes( (TypeDecl::Kind)-1 ); |
---|
40 | makeTyVarMap( function, forallTypes ); |
---|
41 | return (ReferenceToType*)isPolyType( function->get_returnVals().front()->get_type(), forallTypes ); |
---|
42 | } // if |
---|
43 | return 0; |
---|
44 | } |
---|
45 | |
---|
46 | namespace { |
---|
47 | /// Checks a parameter list for polymorphic parameters; will substitute according to env if present |
---|
48 | bool hasPolyParams( std::list< Expression* >& params, const TypeSubstitution *env ) { |
---|
49 | for ( std::list< Expression* >::iterator param = params.begin(); param != params.end(); ++param ) { |
---|
50 | TypeExpr *paramType = dynamic_cast< TypeExpr* >( *param ); |
---|
51 | assert(paramType && "Aggregate parameters should be type expressions"); |
---|
52 | if ( isPolyType( paramType->get_type(), env ) ) return true; |
---|
53 | } |
---|
54 | return false; |
---|
55 | } |
---|
56 | |
---|
57 | /// Checks a parameter list for polymorphic parameters from tyVars; will substitute according to env if present |
---|
58 | bool hasPolyParams( std::list< Expression* >& params, const TyVarMap &tyVars, const TypeSubstitution *env ) { |
---|
59 | for ( std::list< Expression* >::iterator param = params.begin(); param != params.end(); ++param ) { |
---|
60 | TypeExpr *paramType = dynamic_cast< TypeExpr* >( *param ); |
---|
61 | assert(paramType && "Aggregate parameters should be type expressions"); |
---|
62 | if ( isPolyType( paramType->get_type(), tyVars, env ) ) return true; |
---|
63 | } |
---|
64 | return false; |
---|
65 | } |
---|
66 | } |
---|
67 | |
---|
68 | Type* replaceTypeInst( Type* type, const TypeSubstitution* env ) { |
---|
69 | if ( ! env ) return type; |
---|
70 | if ( TypeInstType *typeInst = dynamic_cast< TypeInstType* >( type ) ) { |
---|
71 | Type *newType = env->lookup( typeInst->get_name() ); |
---|
72 | if ( newType ) return newType; |
---|
73 | } |
---|
74 | return type; |
---|
75 | } |
---|
76 | |
---|
77 | Type *isPolyType( Type *type, const TypeSubstitution *env ) { |
---|
78 | type = replaceTypeInst( type, env ); |
---|
79 | |
---|
80 | if ( dynamic_cast< TypeInstType * >( type ) ) { |
---|
81 | return type; |
---|
82 | } else if ( StructInstType *structType = dynamic_cast< StructInstType* >( type ) ) { |
---|
83 | if ( hasPolyParams( structType->get_parameters(), env ) ) return type; |
---|
84 | } else if ( UnionInstType *unionType = dynamic_cast< UnionInstType* >( type ) ) { |
---|
85 | if ( hasPolyParams( unionType->get_parameters(), env ) ) return type; |
---|
86 | } |
---|
87 | return 0; |
---|
88 | } |
---|
89 | |
---|
90 | Type *isPolyType( Type *type, const TyVarMap &tyVars, const TypeSubstitution *env ) { |
---|
91 | type = replaceTypeInst( type, env ); |
---|
92 | |
---|
93 | if ( TypeInstType *typeInst = dynamic_cast< TypeInstType * >( type ) ) { |
---|
94 | if ( tyVars.find( typeInst->get_name() ) != tyVars.end() ) { |
---|
95 | return type; |
---|
96 | } |
---|
97 | } else if ( StructInstType *structType = dynamic_cast< StructInstType* >( type ) ) { |
---|
98 | if ( hasPolyParams( structType->get_parameters(), tyVars, env ) ) return type; |
---|
99 | } else if ( UnionInstType *unionType = dynamic_cast< UnionInstType* >( type ) ) { |
---|
100 | if ( hasPolyParams( unionType->get_parameters(), tyVars, env ) ) return type; |
---|
101 | } |
---|
102 | return 0; |
---|
103 | } |
---|
104 | |
---|
105 | Type *isPolyPtr( Type *type, const TypeSubstitution *env ) { |
---|
106 | type = replaceTypeInst( type, env ); |
---|
107 | |
---|
108 | if ( PointerType *ptr = dynamic_cast< PointerType *>( type ) ) { |
---|
109 | return isPolyType( ptr->get_base(), env ); |
---|
110 | } |
---|
111 | return 0; |
---|
112 | } |
---|
113 | |
---|
114 | Type *isPolyPtr( Type *type, const TyVarMap &tyVars, const TypeSubstitution *env ) { |
---|
115 | type = replaceTypeInst( type, env ); |
---|
116 | |
---|
117 | if ( PointerType *ptr = dynamic_cast< PointerType *>( type ) ) { |
---|
118 | return isPolyType( ptr->get_base(), tyVars, env ); |
---|
119 | } |
---|
120 | return 0; |
---|
121 | } |
---|
122 | |
---|
123 | Type * hasPolyBase( Type *type, int *levels, const TypeSubstitution *env ) { |
---|
124 | int dummy; |
---|
125 | if ( ! levels ) { levels = &dummy; } |
---|
126 | *levels = 0; |
---|
127 | |
---|
128 | while ( true ) { |
---|
129 | type = replaceTypeInst( type, env ); |
---|
130 | |
---|
131 | if ( PointerType *ptr = dynamic_cast< PointerType *>( type ) ) { |
---|
132 | type = ptr->get_base(); |
---|
133 | ++(*levels); |
---|
134 | } else break; |
---|
135 | } |
---|
136 | |
---|
137 | return isPolyType( type, env ); |
---|
138 | } |
---|
139 | |
---|
140 | Type * hasPolyBase( Type *type, const TyVarMap &tyVars, int *levels, const TypeSubstitution *env ) { |
---|
141 | int dummy; |
---|
142 | if ( ! levels ) { levels = &dummy; } |
---|
143 | *levels = 0; |
---|
144 | |
---|
145 | while ( true ) { |
---|
146 | type = replaceTypeInst( type, env ); |
---|
147 | |
---|
148 | if ( PointerType *ptr = dynamic_cast< PointerType *>( type ) ) { |
---|
149 | type = ptr->get_base(); |
---|
150 | ++(*levels); |
---|
151 | } else break; |
---|
152 | } |
---|
153 | |
---|
154 | return isPolyType( type, tyVars, env ); |
---|
155 | } |
---|
156 | |
---|
157 | FunctionType * getFunctionType( Type *ty ) { |
---|
158 | PointerType *ptrType; |
---|
159 | if ( ( ptrType = dynamic_cast< PointerType* >( ty ) ) ) { |
---|
160 | return dynamic_cast< FunctionType* >( ptrType->get_base() ); // pointer if FunctionType, NULL otherwise |
---|
161 | } else { |
---|
162 | return dynamic_cast< FunctionType* >( ty ); // pointer if FunctionType, NULL otherwise |
---|
163 | } |
---|
164 | } |
---|
165 | |
---|
166 | VariableExpr * getBaseVar( Expression *expr, int *levels ) { |
---|
167 | int dummy; |
---|
168 | if ( ! levels ) { levels = &dummy; } |
---|
169 | *levels = 0; |
---|
170 | |
---|
171 | while ( true ) { |
---|
172 | if ( VariableExpr *varExpr = dynamic_cast< VariableExpr* >( expr ) ) { |
---|
173 | return varExpr; |
---|
174 | } else if ( MemberExpr *memberExpr = dynamic_cast< MemberExpr* >( expr ) ) { |
---|
175 | expr = memberExpr->get_aggregate(); |
---|
176 | } else if ( AddressExpr *addressExpr = dynamic_cast< AddressExpr* >( expr ) ) { |
---|
177 | expr = addressExpr->get_arg(); |
---|
178 | } else if ( UntypedExpr *untypedExpr = dynamic_cast< UntypedExpr* >( expr ) ) { |
---|
179 | // look for compiler-inserted dereference operator |
---|
180 | NameExpr *fn = dynamic_cast< NameExpr* >( untypedExpr->get_function() ); |
---|
181 | if ( ! fn || fn->get_name() != std::string("*?") ) return 0; |
---|
182 | expr = *untypedExpr->begin_args(); |
---|
183 | } else if ( CommaExpr *commaExpr = dynamic_cast< CommaExpr* >( expr ) ) { |
---|
184 | // copy constructors insert comma exprs, look at second argument which contains the variable |
---|
185 | expr = commaExpr->get_arg2(); |
---|
186 | continue; |
---|
187 | } else break; |
---|
188 | |
---|
189 | ++(*levels); |
---|
190 | } |
---|
191 | |
---|
192 | return 0; |
---|
193 | } |
---|
194 | |
---|
195 | void makeTyVarMap( Type *type, TyVarMap &tyVarMap ) { |
---|
196 | for ( std::list< TypeDecl* >::const_iterator tyVar = type->get_forall().begin(); tyVar != type->get_forall().end(); ++tyVar ) { |
---|
197 | assert( *tyVar ); |
---|
198 | tyVarMap[ (*tyVar)->get_name() ] = (*tyVar)->get_kind(); |
---|
199 | } |
---|
200 | if ( PointerType *pointer = dynamic_cast< PointerType* >( type ) ) { |
---|
201 | makeTyVarMap( pointer->get_base(), tyVarMap ); |
---|
202 | } |
---|
203 | } |
---|
204 | |
---|
205 | void printTyVarMap( std::ostream &os, const TyVarMap &tyVarMap ) { |
---|
206 | for ( TyVarMap::const_iterator i = tyVarMap.begin(); i != tyVarMap.end(); ++i ) { |
---|
207 | os << i->first << " (" << i->second << ") "; |
---|
208 | } // for |
---|
209 | os << std::endl; |
---|
210 | } |
---|
211 | |
---|
212 | } // namespace GenPoly |
---|
213 | |
---|
214 | // Local Variables: // |
---|
215 | // tab-width: 4 // |
---|
216 | // mode: c++ // |
---|
217 | // compile-command: "make install" // |
---|
218 | // End: // |
---|