source: src/GenPoly/GenPoly.cc@ fc72845d

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 fc72845d was 9b18044, checked in by Rob Schluntz <rschlunt@…>, 8 years ago

Handle TraitDecl in Box Pass2

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