source: src/GenPoly/GenPoly.cc@ f5392c1

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new with_gc
Last change on this file since f5392c1 was 5a3ac84, checked in by Aaron Moss <a3moss@…>, 9 years ago

Fixed Box(T*) generic instantiation bug

  • Property mode set to 100644
File size: 17.3 KB
Line 
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#include "assert.h"
18
19#include "SynTree/Expression.h"
20#include "SynTree/Type.h"
21#include "ResolvExpr/typeops.h"
22
23#include <iostream>
24#include <iterator>
25#include <list>
26#include <typeindex>
27#include <typeinfo>
28#include <vector>
29using namespace std;
30
31namespace GenPoly {
32 namespace {
33 /// Checks a parameter list for polymorphic parameters; will substitute according to env if present
34 bool hasPolyParams( std::list< Expression* >& params, const TypeSubstitution *env ) {
35 for ( std::list< Expression* >::iterator param = params.begin(); param != params.end(); ++param ) {
36 TypeExpr *paramType = dynamic_cast< TypeExpr* >( *param );
37 assertf(paramType, "Aggregate parameters should be type expressions");
38 if ( isPolyType( paramType->get_type(), env ) ) return true;
39 }
40 return false;
41 }
42
43 /// Checks a parameter list for polymorphic parameters from tyVars; will substitute according to env if present
44 bool hasPolyParams( std::list< Expression* >& params, const TyVarMap &tyVars, const TypeSubstitution *env ) {
45 for ( std::list< Expression* >::iterator param = params.begin(); param != params.end(); ++param ) {
46 TypeExpr *paramType = dynamic_cast< TypeExpr* >( *param );
47 assertf(paramType, "Aggregate parameters should be type expressions");
48 if ( isPolyType( paramType->get_type(), tyVars, env ) ) return true;
49 }
50 return false;
51 }
52
53 /// Checks a parameter list for dynamic-layout parameters from tyVars; will substitute according to env if present
54 bool hasDynParams( std::list< Expression* >& params, const TyVarMap &tyVars, const TypeSubstitution *env ) {
55 for ( std::list< Expression* >::iterator param = params.begin(); param != params.end(); ++param ) {
56 TypeExpr *paramType = dynamic_cast< TypeExpr* >( *param );
57 assertf(paramType, "Aggregate parameters should be type expressions");
58 if ( isDynType( paramType->get_type(), tyVars, env ) ) return true;
59 }
60 return false;
61 }
62
63 /// Checks a parameter list for inclusion of polymorphic parameters; will substitute according to env if present
64 bool includesPolyParams( std::list< Expression* >& params, const TypeSubstitution *env ) {
65 for ( std::list< Expression* >::iterator param = params.begin(); param != params.end(); ++param ) {
66 TypeExpr *paramType = dynamic_cast< TypeExpr* >( *param );
67 assertf(paramType, "Aggregate parameters should be type expressions");
68 if ( includesPolyType( paramType->get_type(), env ) ) return true;
69 }
70 return false;
71 }
72
73 /// Checks a parameter list for inclusion of polymorphic parameters from tyVars; will substitute according to env if present
74 bool includesPolyParams( std::list< Expression* >& params, const TyVarMap &tyVars, const TypeSubstitution *env ) {
75 for ( std::list< Expression* >::iterator param = params.begin(); param != params.end(); ++param ) {
76 TypeExpr *paramType = dynamic_cast< TypeExpr* >( *param );
77 assertf(paramType, "Aggregate parameters should be type expressions");
78 if ( includesPolyType( paramType->get_type(), tyVars, env ) ) return true;
79 }
80 return false;
81 }
82 }
83
84 Type* replaceTypeInst( Type* type, const TypeSubstitution* env ) {
85 if ( ! env ) return type;
86 if ( TypeInstType *typeInst = dynamic_cast< TypeInstType* >( type ) ) {
87 Type *newType = env->lookup( typeInst->get_name() );
88 if ( newType ) return newType;
89 }
90 return type;
91 }
92
93 Type *isPolyType( Type *type, const TypeSubstitution *env ) {
94 type = replaceTypeInst( type, env );
95
96 if ( dynamic_cast< TypeInstType * >( type ) ) {
97 return type;
98 } else if ( StructInstType *structType = dynamic_cast< StructInstType* >( type ) ) {
99 if ( hasPolyParams( structType->get_parameters(), env ) ) return type;
100 } else if ( UnionInstType *unionType = dynamic_cast< UnionInstType* >( type ) ) {
101 if ( hasPolyParams( unionType->get_parameters(), env ) ) return type;
102 }
103 return 0;
104 }
105
106 Type *isPolyType( Type *type, const TyVarMap &tyVars, const TypeSubstitution *env ) {
107 type = replaceTypeInst( type, env );
108
109 if ( TypeInstType *typeInst = dynamic_cast< TypeInstType * >( type ) ) {
110 if ( tyVars.find( typeInst->get_name() ) != tyVars.end() ) {
111 return type;
112 }
113 } else if ( StructInstType *structType = dynamic_cast< StructInstType* >( type ) ) {
114 if ( hasPolyParams( structType->get_parameters(), tyVars, env ) ) return type;
115 } else if ( UnionInstType *unionType = dynamic_cast< UnionInstType* >( type ) ) {
116 if ( hasPolyParams( unionType->get_parameters(), tyVars, env ) ) return type;
117 }
118 return 0;
119 }
120
121 ReferenceToType *isDynType( Type *type, const TyVarMap &tyVars, const TypeSubstitution *env ) {
122 type = replaceTypeInst( type, env );
123
124 if ( TypeInstType *typeInst = dynamic_cast< TypeInstType * >( type ) ) {
125 auto var = tyVars.find( typeInst->get_name() );
126 if ( var != tyVars.end() && var->second.isComplete ) {
127 return typeInst;
128 }
129 } else if ( StructInstType *structType = dynamic_cast< StructInstType* >( type ) ) {
130 if ( hasDynParams( structType->get_parameters(), tyVars, env ) ) return structType;
131 } else if ( UnionInstType *unionType = dynamic_cast< UnionInstType* >( type ) ) {
132 if ( hasDynParams( unionType->get_parameters(), tyVars, env ) ) return unionType;
133 }
134 return 0;
135 }
136
137 ReferenceToType *isDynRet( FunctionType *function, const TyVarMap &forallTypes ) {
138 if ( function->get_returnVals().empty() ) return 0;
139
140 return (ReferenceToType*)isDynType( function->get_returnVals().front()->get_type(), forallTypes );
141 }
142
143 ReferenceToType *isDynRet( FunctionType *function ) {
144 if ( function->get_returnVals().empty() ) return 0;
145
146 TyVarMap forallTypes( TypeDecl::Data{} );
147 makeTyVarMap( function, forallTypes );
148 return (ReferenceToType*)isDynType( function->get_returnVals().front()->get_type(), forallTypes );
149 }
150
151 bool needsAdapter( FunctionType *adaptee, const TyVarMap &tyVars ) {
152// if ( ! adaptee->get_returnVals().empty() && isPolyType( adaptee->get_returnVals().front()->get_type(), tyVars ) ) {
153// return true;
154// } // if
155 if ( isDynRet( adaptee, tyVars ) ) return true;
156
157 for ( std::list< DeclarationWithType* >::const_iterator innerArg = adaptee->get_parameters().begin(); innerArg != adaptee->get_parameters().end(); ++innerArg ) {
158// if ( isPolyType( (*innerArg)->get_type(), tyVars ) ) {
159 if ( isDynType( (*innerArg)->get_type(), tyVars ) ) {
160 return true;
161 } // if
162 } // for
163 return false;
164 }
165
166 Type *isPolyPtr( Type *type, const TypeSubstitution *env ) {
167 type = replaceTypeInst( type, env );
168
169 if ( PointerType *ptr = dynamic_cast< PointerType *>( type ) ) {
170 return isPolyType( ptr->get_base(), env );
171 }
172 return 0;
173 }
174
175 Type *isPolyPtr( Type *type, const TyVarMap &tyVars, const TypeSubstitution *env ) {
176 type = replaceTypeInst( type, env );
177
178 if ( PointerType *ptr = dynamic_cast< PointerType *>( type ) ) {
179 return isPolyType( ptr->get_base(), tyVars, env );
180 }
181 return 0;
182 }
183
184 Type * hasPolyBase( Type *type, int *levels, const TypeSubstitution *env ) {
185 int dummy;
186 if ( ! levels ) { levels = &dummy; }
187 *levels = 0;
188
189 while ( true ) {
190 type = replaceTypeInst( type, env );
191
192 if ( PointerType *ptr = dynamic_cast< PointerType *>( type ) ) {
193 type = ptr->get_base();
194 ++(*levels);
195 } else break;
196 }
197
198 return isPolyType( type, env );
199 }
200
201 Type * hasPolyBase( Type *type, const TyVarMap &tyVars, int *levels, const TypeSubstitution *env ) {
202 int dummy;
203 if ( ! levels ) { levels = &dummy; }
204 *levels = 0;
205
206 while ( true ) {
207 type = replaceTypeInst( type, env );
208
209 if ( PointerType *ptr = dynamic_cast< PointerType *>( type ) ) {
210 type = ptr->get_base();
211 ++(*levels);
212 } else break;
213 }
214
215 return isPolyType( type, tyVars, env );
216 }
217
218 bool includesPolyType( Type *type, const TypeSubstitution *env ) {
219 type = replaceTypeInst( type, env );
220
221 if ( dynamic_cast< TypeInstType * >( type ) ) {
222 return true;
223 } else if ( PointerType *pointerType = dynamic_cast< PointerType* >( type ) ) {
224 if ( includesPolyType( pointerType->get_base(), env ) ) return true;
225 } else if ( StructInstType *structType = dynamic_cast< StructInstType* >( type ) ) {
226 if ( includesPolyParams( structType->get_parameters(), env ) ) return true;
227 } else if ( UnionInstType *unionType = dynamic_cast< UnionInstType* >( type ) ) {
228 if ( includesPolyParams( unionType->get_parameters(), env ) ) return true;
229 }
230 return false;
231 }
232
233 bool includesPolyType( Type *type, const TyVarMap &tyVars, const TypeSubstitution *env ) {
234 type = replaceTypeInst( type, env );
235
236 if ( TypeInstType *typeInstType = dynamic_cast< TypeInstType * >( type ) ) {
237 if ( tyVars.find( typeInstType->get_name() ) != tyVars.end() ) {
238 return true;
239 }
240 } else if ( PointerType *pointerType = dynamic_cast< PointerType* >( type ) ) {
241 if ( includesPolyType( pointerType->get_base(), tyVars, env ) ) return true;
242 } else if ( StructInstType *structType = dynamic_cast< StructInstType* >( type ) ) {
243 if ( includesPolyParams( structType->get_parameters(), tyVars, env ) ) return true;
244 } else if ( UnionInstType *unionType = dynamic_cast< UnionInstType* >( type ) ) {
245 if ( includesPolyParams( unionType->get_parameters(), tyVars, env ) ) return true;
246 }
247 return false;
248 }
249
250 FunctionType * getFunctionType( Type *ty ) {
251 PointerType *ptrType;
252 if ( ( ptrType = dynamic_cast< PointerType* >( ty ) ) ) {
253 return dynamic_cast< FunctionType* >( ptrType->get_base() ); // pointer if FunctionType, NULL otherwise
254 } else {
255 return dynamic_cast< FunctionType* >( ty ); // pointer if FunctionType, NULL otherwise
256 }
257 }
258
259 VariableExpr * getBaseVar( Expression *expr, int *levels ) {
260 int dummy;
261 if ( ! levels ) { levels = &dummy; }
262 *levels = 0;
263
264 while ( true ) {
265 if ( VariableExpr *varExpr = dynamic_cast< VariableExpr* >( expr ) ) {
266 return varExpr;
267 } else if ( MemberExpr *memberExpr = dynamic_cast< MemberExpr* >( expr ) ) {
268 expr = memberExpr->get_aggregate();
269 } else if ( AddressExpr *addressExpr = dynamic_cast< AddressExpr* >( expr ) ) {
270 expr = addressExpr->get_arg();
271 } else if ( UntypedExpr *untypedExpr = dynamic_cast< UntypedExpr* >( expr ) ) {
272 // look for compiler-inserted dereference operator
273 NameExpr *fn = dynamic_cast< NameExpr* >( untypedExpr->get_function() );
274 if ( ! fn || fn->get_name() != std::string("*?") ) return 0;
275 expr = *untypedExpr->begin_args();
276 } else if ( CommaExpr *commaExpr = dynamic_cast< CommaExpr* >( expr ) ) {
277 // copy constructors insert comma exprs, look at second argument which contains the variable
278 expr = commaExpr->get_arg2();
279 continue;
280 } else if ( ConditionalExpr * condExpr = dynamic_cast< ConditionalExpr * >( expr ) ) {
281 int lvl1;
282 int lvl2;
283 VariableExpr * var1 = getBaseVar( condExpr->get_arg2(), &lvl1 );
284 VariableExpr * var2 = getBaseVar( condExpr->get_arg3(), &lvl2 );
285 if ( lvl1 == lvl2 && var1 && var2 && var1->get_var() == var2->get_var() ) {
286 *levels = lvl1;
287 return var1;
288 }
289 break;
290 } else break;
291
292 ++(*levels);
293 }
294
295 return 0;
296 }
297
298 namespace {
299 /// Checks if is a pointer to D
300 template<typename D, typename B>
301 bool is( const B* p ) { return type_index{typeid(D)} == type_index{typeid(*p)}; }
302
303 /// Converts to a pointer to D without checking for safety
304 template<typename D, typename B>
305 inline D* as( B* p ) { return reinterpret_cast<D*>(p); }
306
307 /// Flattens a declaration list
308 template<typename Output>
309 void flattenList( list< DeclarationWithType* > src, Output out ) {
310 for ( DeclarationWithType* decl : src ) {
311 ResolvExpr::flatten( decl->get_type(), out );
312 }
313 }
314
315 /// Flattens a list of types
316 template<typename Output>
317 void flattenList( list< Type* > src, Output out ) {
318 for ( Type* ty : src ) {
319 ResolvExpr::flatten( ty, out );
320 }
321 }
322
323 /// Checks if two lists of parameters are equal up to polymorphic substitution.
324 bool paramListsPolyCompatible( const list< Expression* >& aparams, const list< Expression* >& bparams ) {
325 if ( aparams.size() != bparams.size() ) return false;
326
327 for ( list< Expression* >::const_iterator at = aparams.begin(), bt = bparams.begin();
328 at != aparams.end(); ++at, ++bt ) {
329 TypeExpr *aparam = dynamic_cast< TypeExpr* >(*at);
330 assertf(aparam, "Aggregate parameters should be type expressions");
331 TypeExpr *bparam = dynamic_cast< TypeExpr* >(*bt);
332 assertf(bparam, "Aggregate parameters should be type expressions");
333
334 // xxx - might need to let VoidType be a wildcard here too; could have some voids
335 // stuffed in for dtype-statics.
336 // if ( is<VoidType>( aparam->get_type() ) || is<VoidType>( bparam->get_type() ) ) continue;
337 if ( ! typesPolyCompatible( aparam->get_type(), bparam->get_type() ) ) return false;
338 }
339
340 return true;
341 }
342 }
343
344 bool typesPolyCompatible( Type *a, Type *b ) {
345 type_index aid{ typeid(*a) };
346 // polymorphic types always match
347 if ( aid == type_index{typeid(TypeInstType)} ) return true;
348
349 type_index bid{ typeid(*b) };
350 // polymorphic types always match
351 if ( bid == type_index{typeid(TypeInstType)} ) return true;
352
353 // can't match otherwise if different types
354 if ( aid != bid ) return false;
355
356 // recurse through type structure (conditions borrowed from Unify.cc)
357 if ( aid == type_index{typeid(BasicType)} ) {
358 return as<BasicType>(a)->get_kind() == as<BasicType>(b)->get_kind();
359 } else if ( aid == type_index{typeid(PointerType)} ) {
360 PointerType *ap = as<PointerType>(a), *bp = as<PointerType>(b);
361
362 // void pointers should match any other pointer type
363 return is<VoidType>( ap->get_base() ) || is<VoidType>( bp->get_base() )
364 || typesPolyCompatible( ap->get_base(), bp->get_base() );
365 } else if ( aid == type_index{typeid(ArrayType)} ) {
366 ArrayType *aa = as<ArrayType>(a), *ba = as<ArrayType>(b);
367
368 if ( aa->get_isVarLen() ) {
369 if ( ! ba->get_isVarLen() ) return false;
370 } else {
371 if ( ba->get_isVarLen() ) return false;
372
373 ConstantExpr *ad = dynamic_cast<ConstantExpr*>( aa->get_dimension() );
374 ConstantExpr *bd = dynamic_cast<ConstantExpr*>( ba->get_dimension() );
375 if ( ad && bd
376 && ad->get_constant()->get_value() != bd->get_constant()->get_value() )
377 return false;
378 }
379
380 return typesPolyCompatible( aa->get_base(), ba->get_base() );
381 } else if ( aid == type_index{typeid(FunctionType)} ) {
382 FunctionType *af = as<FunctionType>(a), *bf = as<FunctionType>(b);
383
384 vector<Type*> aparams, bparams;
385 flattenList( af->get_parameters(), back_inserter( aparams ) );
386 flattenList( bf->get_parameters(), back_inserter( bparams ) );
387 if ( aparams.size() != bparams.size() ) return false;
388
389 vector<Type*> areturns, breturns;
390 flattenList( af->get_returnVals(), back_inserter( areturns ) );
391 flattenList( bf->get_returnVals(), back_inserter( breturns ) );
392 if ( areturns.size() != breturns.size() ) return false;
393
394 for ( unsigned i = 0; i < aparams.size(); ++i ) {
395 if ( ! typesPolyCompatible( aparams[i], bparams[i] ) ) return false;
396 }
397 for ( unsigned i = 0; i < areturns.size(); ++i ) {
398 if ( ! typesPolyCompatible( areturns[i], breturns[i] ) ) return false;
399 }
400 return true;
401 } else if ( aid == type_index{typeid(StructInstType)} ) {
402 StructInstType *aa = as<StructInstType>(a), *ba = as<StructInstType>(b);
403
404 if ( aa->get_name() != ba->get_name() ) return false;
405 return paramListsPolyCompatible( aa->get_parameters(), ba->get_parameters() );
406 } else if ( aid == type_index{typeid(UnionInstType)} ) {
407 UnionInstType *aa = as<UnionInstType>(a), *ba = as<UnionInstType>(b);
408
409 if ( aa->get_name() != ba->get_name() ) return false;
410 return paramListsPolyCompatible( aa->get_parameters(), ba->get_parameters() );
411 } else if ( aid == type_index{typeid(EnumInstType)} ) {
412 return as<EnumInstType>(a)->get_name() == as<EnumInstType>(b)->get_name();
413 } else if ( aid == type_index{typeid(TraitInstType)} ) {
414 return as<TraitInstType>(a)->get_name() == as<TraitInstType>(b)->get_name();
415 } else if ( aid == type_index{typeid(TupleType)} ) {
416 TupleType *at = as<TupleType>(a), *bt = as<TupleType>(b);
417
418 vector<Type*> atypes, btypes;
419 flattenList( at->get_types(), back_inserter( atypes ) );
420 flattenList( bt->get_types(), back_inserter( btypes ) );
421 if ( atypes.size() != btypes.size() ) return false;
422
423 for ( unsigned i = 0; i < atypes.size(); ++i ) {
424 if ( ! typesPolyCompatible( atypes[i], btypes[i] ) ) return false;
425 }
426 return true;
427 } else return true; // VoidType, VarArgsType, ZeroType & OneType just need the same type
428 }
429
430 void addToTyVarMap( TypeDecl * tyVar, TyVarMap &tyVarMap ) {
431 tyVarMap[ tyVar->get_name() ] = TypeDecl::Data{ tyVar };
432 }
433
434 void makeTyVarMap( Type *type, TyVarMap &tyVarMap ) {
435 for ( Type::ForallList::const_iterator tyVar = type->get_forall().begin(); tyVar != type->get_forall().end(); ++tyVar ) {
436 assert( *tyVar );
437 addToTyVarMap( *tyVar, tyVarMap );
438 }
439 if ( PointerType *pointer = dynamic_cast< PointerType* >( type ) ) {
440 makeTyVarMap( pointer->get_base(), tyVarMap );
441 }
442 }
443
444 void printTyVarMap( std::ostream &os, const TyVarMap &tyVarMap ) {
445 for ( TyVarMap::const_iterator i = tyVarMap.begin(); i != tyVarMap.end(); ++i ) {
446 os << i->first << " (" << i->second << ") ";
447 } // for
448 os << std::endl;
449 }
450
451} // namespace GenPoly
452
453// Local Variables: //
454// tab-width: 4 //
455// mode: c++ //
456// compile-command: "make install" //
457// End: //
Note: See TracBrowser for help on using the repository browser.