source: src/GenPoly/GenPoly.cc@ a8615fd1

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 no_list persistent-indexer pthread-emulation qualifiedEnum
Last change on this file since a8615fd1 was 682dcae, checked in by Rob Schluntz <rschlunt@…>, 7 years ago

Add a case for ReferenceType in typesPolyCompatible

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