source: src/GenPoly/GenPoly.cc@ 418882af

ADT ast-experimental
Last change on this file since 418882af was 7b5694d, checked in by Andrew Beach <ajbeach@…>, 3 years ago

Header Clean-up: Moved more things from typeops to Unify.

  • Property mode set to 100644
File size: 31.6 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 : Andrew Beach
12// Last Modified On : Mon Oct 24 15:19:00 2022
13// Update Count : 17
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 "AST/Type.hpp"
27#include "GenPoly/ErasableScopedMap.h" // for ErasableScopedMap<>::const_it...
28#include "ResolvExpr/typeops.h" // for flatten
29#include "SynTree/Constant.h" // for Constant
30#include "SynTree/Expression.h" // for Expression, TypeExpr, Constan...
31#include "SynTree/Type.h" // for Type, StructInstType, UnionIn...
32#include "SynTree/TypeSubstitution.h" // for TypeSubstitution
33
34using namespace std;
35
36namespace GenPoly {
37 namespace {
38 /// Checks a parameter list for polymorphic parameters; will substitute according to env if present
39 bool hasPolyParams( std::list< Expression* >& params, const TypeSubstitution *env ) {
40 for ( std::list< Expression* >::iterator param = params.begin(); param != params.end(); ++param ) {
41 TypeExpr *paramType = dynamic_cast< TypeExpr* >( *param );
42 assertf(paramType, "Aggregate parameters should be type expressions");
43 if ( isPolyType( paramType->get_type(), env ) ) return true;
44 }
45 return false;
46 }
47
48 bool hasPolyParams( const std::vector<ast::ptr<ast::Expr>> & params, const ast::TypeSubstitution * env) {
49 for (auto &param : params) {
50 auto paramType = param.strict_as<ast::TypeExpr>();
51 if (isPolyType(paramType->type, env)) return true;
52 }
53 return false;
54 }
55
56 /// Checks a parameter list for polymorphic parameters from tyVars; will substitute according to env if present
57 bool hasPolyParams( std::list< Expression* >& params, const TyVarMap &tyVars, const TypeSubstitution *env ) {
58 for ( std::list< Expression* >::iterator param = params.begin(); param != params.end(); ++param ) {
59 TypeExpr *paramType = dynamic_cast< TypeExpr* >( *param );
60 assertf(paramType, "Aggregate parameters should be type expressions");
61 if ( isPolyType( paramType->get_type(), tyVars, env ) ) return true;
62 }
63 return false;
64 }
65
66 /// Checks a parameter list for dynamic-layout parameters from tyVars; will substitute according to env if present
67 bool hasDynParams( std::list< Expression* >& params, const TyVarMap &tyVars, const TypeSubstitution *env ) {
68 for ( std::list< Expression* >::iterator param = params.begin(); param != params.end(); ++param ) {
69 TypeExpr *paramType = dynamic_cast< TypeExpr* >( *param );
70 assertf(paramType, "Aggregate parameters should be type expressions");
71 if ( isDynType( paramType->get_type(), tyVars, env ) ) return true;
72 }
73 return false;
74 }
75
76 bool hasDynParams(
77 const std::vector<ast::ptr<ast::Expr>> & params,
78 const TypeVarMap & typeVars,
79 const ast::TypeSubstitution * subst ) {
80 for ( ast::ptr<ast::Expr> const & paramExpr : params ) {
81 auto param = paramExpr.as<ast::TypeExpr>();
82 assertf( param, "Aggregate parameters should be type expressions." );
83 if ( isDynType( param->type.get(), typeVars, subst ) ) {
84 return true;
85 }
86 }
87 return false;
88 }
89
90 /// Checks a parameter list for inclusion of polymorphic parameters; will substitute according to env if present
91 bool includesPolyParams( std::list< Expression* >& params, const TypeSubstitution *env ) {
92 for ( std::list< Expression* >::iterator param = params.begin(); param != params.end(); ++param ) {
93 TypeExpr *paramType = dynamic_cast< TypeExpr* >( *param );
94 assertf(paramType, "Aggregate parameters should be type expressions");
95 if ( includesPolyType( paramType->get_type(), env ) ) return true;
96 }
97 return false;
98 }
99
100 /// Checks a parameter list for inclusion of polymorphic parameters from tyVars; will substitute according to env if present
101 bool includesPolyParams( std::list< Expression* >& params, const TyVarMap &tyVars, const TypeSubstitution *env ) {
102 for ( std::list< Expression* >::iterator param = params.begin(); param != params.end(); ++param ) {
103 TypeExpr *paramType = dynamic_cast< TypeExpr* >( *param );
104 assertf(paramType, "Aggregate parameters should be type expressions");
105 if ( includesPolyType( paramType->get_type(), tyVars, env ) ) return true;
106 }
107 return false;
108 }
109 }
110
111 Type* replaceTypeInst( Type* type, const TypeSubstitution* env ) {
112 if ( ! env ) return type;
113 if ( TypeInstType *typeInst = dynamic_cast< TypeInstType* >( type ) ) {
114 Type *newType = env->lookup( typeInst->get_name() );
115 if ( newType ) return newType;
116 }
117 return type;
118 }
119
120 const Type* replaceTypeInst( const Type* type, const TypeSubstitution* env ) {
121 if ( ! env ) return type;
122 if ( auto typeInst = dynamic_cast< const TypeInstType* >( type ) ) {
123 Type *newType = env->lookup( typeInst->get_name() );
124 if ( newType ) return newType;
125 }
126 return type;
127 }
128
129 const ast::Type * replaceTypeInst(const ast::Type * type, const ast::TypeSubstitution * env) {
130 if (!env) return type;
131 if ( auto typeInst = dynamic_cast<const ast::TypeInstType*>(type) ) {
132 auto newType = env->lookup(typeInst);
133 if (newType) return newType;
134 }
135 return type;
136 }
137
138 Type *isPolyType( Type *type, const TypeSubstitution *env ) {
139 type = replaceTypeInst( type, env );
140
141 if ( dynamic_cast< TypeInstType * >( type ) ) {
142 return type;
143 } else if ( ArrayType * arrayType = dynamic_cast< ArrayType * >( type ) ) {
144 return isPolyType( arrayType->base, env );
145 } else if ( StructInstType *structType = dynamic_cast< StructInstType* >( type ) ) {
146 if ( hasPolyParams( structType->get_parameters(), env ) ) return type;
147 } else if ( UnionInstType *unionType = dynamic_cast< UnionInstType* >( type ) ) {
148 if ( hasPolyParams( unionType->get_parameters(), env ) ) return type;
149 }
150 return 0;
151 }
152
153 const ast::Type * isPolyType(const ast::Type * type, const ast::TypeSubstitution * env) {
154 type = replaceTypeInst( type, env );
155
156 if ( dynamic_cast< const ast::TypeInstType * >( type ) ) {
157 return type;
158 } else if ( auto arrayType = dynamic_cast< const ast::ArrayType * >( type ) ) {
159 return isPolyType( arrayType->base, env );
160 } else if ( auto structType = dynamic_cast< const ast::StructInstType* >( type ) ) {
161 if ( hasPolyParams( structType->params, env ) ) return type;
162 } else if ( auto unionType = dynamic_cast< const ast::UnionInstType* >( type ) ) {
163 if ( hasPolyParams( unionType->params, env ) ) return type;
164 }
165 return 0;
166 }
167
168 Type *isPolyType( Type *type, const TyVarMap &tyVars, const TypeSubstitution *env ) {
169 type = replaceTypeInst( type, env );
170
171 if ( TypeInstType *typeInst = dynamic_cast< TypeInstType * >( type ) ) {
172 if ( tyVars.find( typeInst->get_name() ) != tyVars.end() ) {
173 return type;
174 }
175 } else if ( ArrayType * arrayType = dynamic_cast< ArrayType * >( type ) ) {
176 return isPolyType( arrayType->base, tyVars, env );
177 } else if ( StructInstType *structType = dynamic_cast< StructInstType* >( type ) ) {
178 if ( hasPolyParams( structType->get_parameters(), tyVars, env ) ) return type;
179 } else if ( UnionInstType *unionType = dynamic_cast< UnionInstType* >( type ) ) {
180 if ( hasPolyParams( unionType->get_parameters(), tyVars, env ) ) return type;
181 }
182 return 0;
183 }
184
185 const ast::Type * isPolyType(const ast::Type * type, const TyVarMap & tyVars, const ast::TypeSubstitution * env) {
186 type = replaceTypeInst( type, env );
187
188 if ( auto typeInst = dynamic_cast< const ast::TypeInstType * >( type ) ) {
189 return tyVars.find(typeInst->typeString()) != tyVars.end() ? type : nullptr;
190 } else if ( auto arrayType = dynamic_cast< const ast::ArrayType * >( type ) ) {
191 return isPolyType( arrayType->base, env );
192 } else if ( auto structType = dynamic_cast< const ast::StructInstType* >( type ) ) {
193 if ( hasPolyParams( structType->params, env ) ) return type;
194 } else if ( auto unionType = dynamic_cast< const ast::UnionInstType* >( type ) ) {
195 if ( hasPolyParams( unionType->params, env ) ) return type;
196 }
197 return nullptr;
198 }
199
200const ast::Type * isPolyType( const ast::Type * type,
201 const TypeVarMap & typeVars, const ast::TypeSubstitution * subst ) {
202 type = replaceTypeInst( type, subst );
203
204 if ( auto inst = dynamic_cast< const ast::TypeInstType * >( type ) ) {
205 if ( typeVars.find( *inst ) != typeVars.end() ) return type;
206 } else if ( auto array = dynamic_cast< const ast::ArrayType * >( type ) ) {
207 return isPolyType( array->base, subst );
208 } else if ( auto sue = dynamic_cast< const ast::StructInstType * >( type ) ) {
209 if ( hasPolyParams( sue->params, subst ) ) return type;
210 } else if ( auto sue = dynamic_cast< const ast::UnionInstType * >( type ) ) {
211 if ( hasPolyParams( sue->params, subst ) ) return type;
212 }
213 return nullptr;
214}
215
216 ReferenceToType *isDynType( Type *type, const TyVarMap &tyVars, const TypeSubstitution *env ) {
217 type = replaceTypeInst( type, env );
218
219 if ( TypeInstType *typeInst = dynamic_cast< TypeInstType * >( type ) ) {
220 auto var = tyVars.find( typeInst->get_name() );
221 if ( var != tyVars.end() && var->second.isComplete ) {
222 return typeInst;
223 }
224 } else if ( StructInstType *structType = dynamic_cast< StructInstType* >( type ) ) {
225 if ( hasDynParams( structType->get_parameters(), tyVars, env ) ) return structType;
226 } else if ( UnionInstType *unionType = dynamic_cast< UnionInstType* >( type ) ) {
227 if ( hasDynParams( unionType->get_parameters(), tyVars, env ) ) return unionType;
228 }
229 return 0;
230 }
231
232const ast::BaseInstType * isDynType(
233 const ast::Type * type, const TypeVarMap & typeVars,
234 const ast::TypeSubstitution * subst ) {
235 type = replaceTypeInst( type, subst );
236
237 if ( auto inst = dynamic_cast<ast::TypeInstType const *>( type ) ) {
238 auto var = typeVars.find( *inst );
239 if ( var != typeVars.end() && var->second.isComplete ) {
240 return inst;
241 }
242 } else if ( auto inst = dynamic_cast<ast::StructInstType const *>( type ) ) {
243 if ( hasDynParams( inst->params, typeVars, subst ) ) {
244 return inst;
245 }
246 } else if ( auto inst = dynamic_cast<ast::UnionInstType const *>( type ) ) {
247 if ( hasDynParams( inst->params, typeVars, subst ) ) {
248 return inst;
249 }
250 }
251 return nullptr;
252}
253
254 ReferenceToType *isDynRet( FunctionType *function, const TyVarMap &forallTypes ) {
255 if ( function->get_returnVals().empty() ) return 0;
256
257 return (ReferenceToType*)isDynType( function->get_returnVals().front()->get_type(), forallTypes );
258 }
259
260const ast::BaseInstType *isDynRet(
261 const ast::FunctionType * type, const TypeVarMap & typeVars ) {
262 if ( type->returns.empty() ) return nullptr;
263
264 return isDynType( type->returns.front(), typeVars );
265}
266
267 ReferenceToType *isDynRet( FunctionType *function ) {
268 if ( function->get_returnVals().empty() ) return 0;
269
270 TyVarMap forallTypes( TypeDecl::Data{} );
271 makeTyVarMap( function, forallTypes );
272 return (ReferenceToType*)isDynType( function->get_returnVals().front()->get_type(), forallTypes );
273 }
274
275 bool needsAdapter( FunctionType *adaptee, const TyVarMap &tyVars ) {
276// if ( ! adaptee->get_returnVals().empty() && isPolyType( adaptee->get_returnVals().front()->get_type(), tyVars ) ) {
277// return true;
278// } // if
279 if ( isDynRet( adaptee, tyVars ) ) return true;
280
281 for ( std::list< DeclarationWithType* >::const_iterator innerArg = adaptee->get_parameters().begin(); innerArg != adaptee->get_parameters().end(); ++innerArg ) {
282// if ( isPolyType( (*innerArg)->get_type(), tyVars ) ) {
283 if ( isDynType( (*innerArg)->get_type(), tyVars ) ) {
284 return true;
285 } // if
286 } // for
287 return false;
288 }
289
290bool needsAdapter(
291 ast::FunctionType const * adaptee, const TypeVarMap & typeVars ) {
292 if ( isDynRet( adaptee, typeVars ) ) return true;
293
294 for ( auto param : adaptee->params ) {
295 if ( isDynType( param, typeVars ) ) {
296 return true;
297 }
298 }
299 return false;
300}
301
302 Type *isPolyPtr( Type *type, const TypeSubstitution *env ) {
303 type = replaceTypeInst( type, env );
304
305 if ( PointerType *ptr = dynamic_cast< PointerType *>( type ) ) {
306 return isPolyType( ptr->get_base(), env );
307 }
308 return 0;
309 }
310
311 Type *isPolyPtr( Type *type, const TyVarMap &tyVars, const TypeSubstitution *env ) {
312 type = replaceTypeInst( type, env );
313
314 if ( PointerType *ptr = dynamic_cast< PointerType *>( type ) ) {
315 return isPolyType( ptr->get_base(), tyVars, env );
316 }
317 return 0;
318 }
319
320 Type * hasPolyBase( Type *type, int *levels, const TypeSubstitution *env ) {
321 int dummy;
322 if ( ! levels ) { levels = &dummy; }
323 *levels = 0;
324
325 while ( true ) {
326 type = replaceTypeInst( type, env );
327
328 if ( PointerType *ptr = dynamic_cast< PointerType *>( type ) ) {
329 type = ptr->get_base();
330 ++(*levels);
331 } else break;
332 }
333
334 return isPolyType( type, env );
335 }
336
337 Type * hasPolyBase( Type *type, const TyVarMap &tyVars, int *levels, const TypeSubstitution *env ) {
338 int dummy;
339 if ( ! levels ) { levels = &dummy; }
340 *levels = 0;
341
342 while ( true ) {
343 type = replaceTypeInst( type, env );
344
345 if ( PointerType *ptr = dynamic_cast< PointerType *>( type ) ) {
346 type = ptr->get_base();
347 ++(*levels);
348 } else break;
349 }
350
351 return isPolyType( type, tyVars, env );
352 }
353
354ast::Type const * hasPolyBase(
355 ast::Type const * type, const TypeVarMap & typeVars,
356 int * levels, const ast::TypeSubstitution * subst ) {
357 int level_count = 0;
358
359 while ( true ) {
360 type = replaceTypeInst( type, subst );
361
362 if ( auto ptr = dynamic_cast<ast::PointerType const *>( type ) ) {
363 type = ptr->base;
364 ++level_count;
365 } else {
366 break;
367 }
368 }
369
370 if ( nullptr != levels ) { *levels = level_count; }
371 return isPolyType( type, typeVars, subst );
372}
373
374 bool includesPolyType( Type *type, const TypeSubstitution *env ) {
375 type = replaceTypeInst( type, env );
376
377 if ( dynamic_cast< TypeInstType * >( type ) ) {
378 return true;
379 } else if ( PointerType *pointerType = dynamic_cast< PointerType* >( type ) ) {
380 if ( includesPolyType( pointerType->get_base(), env ) ) return true;
381 } else if ( StructInstType *structType = dynamic_cast< StructInstType* >( type ) ) {
382 if ( includesPolyParams( structType->get_parameters(), env ) ) return true;
383 } else if ( UnionInstType *unionType = dynamic_cast< UnionInstType* >( type ) ) {
384 if ( includesPolyParams( unionType->get_parameters(), env ) ) return true;
385 }
386 return false;
387 }
388
389 bool includesPolyType( Type *type, const TyVarMap &tyVars, const TypeSubstitution *env ) {
390 type = replaceTypeInst( type, env );
391
392 if ( TypeInstType *typeInstType = dynamic_cast< TypeInstType * >( type ) ) {
393 if ( tyVars.find( typeInstType->get_name() ) != tyVars.end() ) {
394 return true;
395 }
396 } else if ( PointerType *pointerType = dynamic_cast< PointerType* >( type ) ) {
397 if ( includesPolyType( pointerType->get_base(), tyVars, env ) ) return true;
398 } else if ( StructInstType *structType = dynamic_cast< StructInstType* >( type ) ) {
399 if ( includesPolyParams( structType->get_parameters(), tyVars, env ) ) return true;
400 } else if ( UnionInstType *unionType = dynamic_cast< UnionInstType* >( type ) ) {
401 if ( includesPolyParams( unionType->get_parameters(), tyVars, env ) ) return true;
402 }
403 return false;
404 }
405
406 FunctionType * getFunctionType( Type *ty ) {
407 PointerType *ptrType;
408 if ( ( ptrType = dynamic_cast< PointerType* >( ty ) ) ) {
409 return dynamic_cast< FunctionType* >( ptrType->get_base() ); // pointer if FunctionType, NULL otherwise
410 } else {
411 return dynamic_cast< FunctionType* >( ty ); // pointer if FunctionType, NULL otherwise
412 }
413 }
414
415 const ast::FunctionType * getFunctionType( const ast::Type * ty ) {
416 if ( auto pty = dynamic_cast< const ast::PointerType * >( ty ) ) {
417 return pty->base.as< ast::FunctionType >();
418 } else {
419 return dynamic_cast< const ast::FunctionType * >( ty );
420 }
421 }
422
423 VariableExpr * getBaseVar( Expression *expr, int *levels ) {
424 int dummy;
425 if ( ! levels ) { levels = &dummy; }
426 *levels = 0;
427
428 while ( true ) {
429 if ( VariableExpr *varExpr = dynamic_cast< VariableExpr* >( expr ) ) {
430 return varExpr;
431 } else if ( MemberExpr *memberExpr = dynamic_cast< MemberExpr* >( expr ) ) {
432 expr = memberExpr->get_aggregate();
433 } else if ( AddressExpr *addressExpr = dynamic_cast< AddressExpr* >( expr ) ) {
434 expr = addressExpr->get_arg();
435 } else if ( UntypedExpr *untypedExpr = dynamic_cast< UntypedExpr* >( expr ) ) {
436 // look for compiler-inserted dereference operator
437 NameExpr *fn = dynamic_cast< NameExpr* >( untypedExpr->get_function() );
438 if ( ! fn || fn->get_name() != std::string("*?") ) return 0;
439 expr = *untypedExpr->begin_args();
440 } else if ( CommaExpr *commaExpr = dynamic_cast< CommaExpr* >( expr ) ) {
441 // copy constructors insert comma exprs, look at second argument which contains the variable
442 expr = commaExpr->get_arg2();
443 continue;
444 } else if ( ConditionalExpr * condExpr = dynamic_cast< ConditionalExpr * >( expr ) ) {
445 int lvl1;
446 int lvl2;
447 VariableExpr * var1 = getBaseVar( condExpr->get_arg2(), &lvl1 );
448 VariableExpr * var2 = getBaseVar( condExpr->get_arg3(), &lvl2 );
449 if ( lvl1 == lvl2 && var1 && var2 && var1->get_var() == var2->get_var() ) {
450 *levels = lvl1;
451 return var1;
452 }
453 break;
454 } else break;
455
456 ++(*levels);
457 }
458
459 return 0;
460 }
461
462 namespace {
463 /// Checks if is a pointer to D
464 template<typename D, typename B>
465 bool is( const B* p ) { return type_index{typeid(D)} == type_index{typeid(*p)}; }
466
467 /// Converts to a pointer to D without checking for safety
468 template<typename D, typename B>
469 inline D* as( B* p ) { return reinterpret_cast<D*>(p); }
470
471 template<typename D, typename B>
472 inline D const * as( B const * p ) {
473 return reinterpret_cast<D const *>( p );
474 }
475
476 /// Flattens a declaration list
477 template<typename Output>
478 void flattenList( list< DeclarationWithType* > src, Output out ) {
479 for ( DeclarationWithType* decl : src ) {
480 ResolvExpr::flatten( decl->get_type(), out );
481 }
482 }
483
484 /// Flattens a list of types
485 template<typename Output>
486 void flattenList( list< Type* > src, Output out ) {
487 for ( Type* ty : src ) {
488 ResolvExpr::flatten( ty, out );
489 }
490 }
491
492 /// Flattens a list of types.
493 // There is another flattenList in Unify.
494 void flattenList( vector<ast::ptr<ast::Type>> const & src,
495 vector<ast::ptr<ast::Type>> & out ) {
496 for ( auto const & type : src ) {
497 ResolvExpr::flatten( type, out );
498 }
499 }
500
501 /// Checks if two lists of parameters are equal up to polymorphic substitution.
502 bool paramListsPolyCompatible( const list< Expression* >& aparams, const list< Expression* >& bparams ) {
503 if ( aparams.size() != bparams.size() ) return false;
504
505 for ( list< Expression* >::const_iterator at = aparams.begin(), bt = bparams.begin();
506 at != aparams.end(); ++at, ++bt ) {
507 TypeExpr *aparam = dynamic_cast< TypeExpr* >(*at);
508 assertf(aparam, "Aggregate parameters should be type expressions");
509 TypeExpr *bparam = dynamic_cast< TypeExpr* >(*bt);
510 assertf(bparam, "Aggregate parameters should be type expressions");
511
512 // xxx - might need to let VoidType be a wildcard here too; could have some voids
513 // stuffed in for dtype-statics.
514 // if ( is<VoidType>( aparam->get_type() ) || is<VoidType>( bparam->get_type() ) ) continue;
515 if ( ! typesPolyCompatible( aparam->get_type(), bparam->get_type() ) ) return false;
516 }
517
518 return true;
519 }
520
521 bool paramListsPolyCompatible(
522 std::vector<ast::ptr<ast::Expr>> const & lparams,
523 std::vector<ast::ptr<ast::Expr>> const & rparams ) {
524 if ( lparams.size() != rparams.size() ) {
525 return false;
526 }
527
528 for ( auto lparam = lparams.begin(), rparam = rparams.begin() ;
529 lparam != lparams.end() ; ++lparam, ++rparam ) {
530 ast::TypeExpr const * lexpr = lparam->as<ast::TypeExpr>();
531 assertf( lexpr, "Aggregate parameters should be type expressions" );
532 ast::TypeExpr const * rexpr = rparam->as<ast::TypeExpr>();
533 assertf( rexpr, "Aggregate parameters should be type expressions" );
534
535 // xxx - might need to let VoidType be a wildcard here too; could have some voids
536 // stuffed in for dtype-statics.
537 // if ( is<VoidType>( lexpr->type() ) || is<VoidType>( bparam->get_type() ) ) continue;
538 if ( !typesPolyCompatible( lexpr->type, rexpr->type ) ) {
539 return false;
540 }
541 }
542
543 return true;
544 }
545 }
546
547 bool typesPolyCompatible( Type *a, Type *b ) {
548 type_index aid{ typeid(*a) };
549 // polymorphic types always match
550 if ( aid == type_index{typeid(TypeInstType)} ) return true;
551
552 type_index bid{ typeid(*b) };
553 // polymorphic types always match
554 if ( bid == type_index{typeid(TypeInstType)} ) return true;
555
556 // can't match otherwise if different types
557 if ( aid != bid ) return false;
558
559 // recurse through type structure (conditions borrowed from Unify.cc)
560 if ( aid == type_index{typeid(BasicType)} ) {
561 return as<BasicType>(a)->get_kind() == as<BasicType>(b)->get_kind();
562 } else if ( aid == type_index{typeid(PointerType)} ) {
563 PointerType *ap = as<PointerType>(a), *bp = as<PointerType>(b);
564
565 // void pointers should match any other pointer type
566 return is<VoidType>( ap->get_base() ) || is<VoidType>( bp->get_base() )
567 || typesPolyCompatible( ap->get_base(), bp->get_base() );
568 } else if ( aid == type_index{typeid(ReferenceType)} ) {
569 ReferenceType *ap = as<ReferenceType>(a), *bp = as<ReferenceType>(b);
570 return is<VoidType>( ap->get_base() ) || is<VoidType>( bp->get_base() )
571 || typesPolyCompatible( ap->get_base(), bp->get_base() );
572 } else if ( aid == type_index{typeid(ArrayType)} ) {
573 ArrayType *aa = as<ArrayType>(a), *ba = as<ArrayType>(b);
574
575 if ( aa->get_isVarLen() ) {
576 if ( ! ba->get_isVarLen() ) return false;
577 } else {
578 if ( ba->get_isVarLen() ) return false;
579
580 ConstantExpr *ad = dynamic_cast<ConstantExpr*>( aa->get_dimension() );
581 ConstantExpr *bd = dynamic_cast<ConstantExpr*>( ba->get_dimension() );
582 if ( ad && bd
583 && ad->get_constant()->get_value() != bd->get_constant()->get_value() )
584 return false;
585 }
586
587 return typesPolyCompatible( aa->get_base(), ba->get_base() );
588 } else if ( aid == type_index{typeid(FunctionType)} ) {
589 FunctionType *af = as<FunctionType>(a), *bf = as<FunctionType>(b);
590
591 vector<Type*> aparams, bparams;
592 flattenList( af->get_parameters(), back_inserter( aparams ) );
593 flattenList( bf->get_parameters(), back_inserter( bparams ) );
594 if ( aparams.size() != bparams.size() ) return false;
595
596 vector<Type*> areturns, breturns;
597 flattenList( af->get_returnVals(), back_inserter( areturns ) );
598 flattenList( bf->get_returnVals(), back_inserter( breturns ) );
599 if ( areturns.size() != breturns.size() ) return false;
600
601 for ( unsigned i = 0; i < aparams.size(); ++i ) {
602 if ( ! typesPolyCompatible( aparams[i], bparams[i] ) ) return false;
603 }
604 for ( unsigned i = 0; i < areturns.size(); ++i ) {
605 if ( ! typesPolyCompatible( areturns[i], breturns[i] ) ) return false;
606 }
607 return true;
608 } else if ( aid == type_index{typeid(StructInstType)} ) {
609 StructInstType *aa = as<StructInstType>(a), *ba = as<StructInstType>(b);
610
611 if ( aa->get_name() != ba->get_name() ) return false;
612 return paramListsPolyCompatible( aa->get_parameters(), ba->get_parameters() );
613 } else if ( aid == type_index{typeid(UnionInstType)} ) {
614 UnionInstType *aa = as<UnionInstType>(a), *ba = as<UnionInstType>(b);
615
616 if ( aa->get_name() != ba->get_name() ) return false;
617 return paramListsPolyCompatible( aa->get_parameters(), ba->get_parameters() );
618 } else if ( aid == type_index{typeid(EnumInstType)} ) {
619 return as<EnumInstType>(a)->get_name() == as<EnumInstType>(b)->get_name();
620 } else if ( aid == type_index{typeid(TraitInstType)} ) {
621 return as<TraitInstType>(a)->get_name() == as<TraitInstType>(b)->get_name();
622 } else if ( aid == type_index{typeid(TupleType)} ) {
623 TupleType *at = as<TupleType>(a), *bt = as<TupleType>(b);
624
625 vector<Type*> atypes, btypes;
626 flattenList( at->get_types(), back_inserter( atypes ) );
627 flattenList( bt->get_types(), back_inserter( btypes ) );
628 if ( atypes.size() != btypes.size() ) return false;
629
630 for ( unsigned i = 0; i < atypes.size(); ++i ) {
631 if ( ! typesPolyCompatible( atypes[i], btypes[i] ) ) return false;
632 }
633 return true;
634 } else return true; // VoidType, VarArgsType, ZeroType & OneType just need the same type
635 }
636
637bool typesPolyCompatible( ast::Type const * lhs, ast::Type const * rhs ) {
638 type_index const lid = typeid(*lhs);
639
640 // Polymorphic types always match:
641 if ( type_index(typeid(ast::TypeInstType)) == lid ) return true;
642
643 type_index const rid = typeid(*rhs);
644 if ( type_index(typeid(ast::TypeInstType)) == rid ) return true;
645
646 // All other types only match if they are the same type:
647 if ( lid != rid ) return false;
648
649 // So remaining types can be examined case by case.
650 // Recurse through type structure (conditions borrowed from Unify.cc).
651
652 if ( type_index(typeid(ast::BasicType)) == lid ) {
653 return as<ast::BasicType>(lhs)->kind == as<ast::BasicType>(rhs)->kind;
654 } else if ( type_index(typeid(ast::PointerType)) == lid ) {
655 ast::PointerType const * l = as<ast::PointerType>(lhs);
656 ast::PointerType const * r = as<ast::PointerType>(rhs);
657
658 // void pointers should match any other pointer type.
659 return is<ast::VoidType>( l->base.get() )
660 || is<ast::VoidType>( r->base.get() )
661 || typesPolyCompatible( l->base.get(), r->base.get() );
662 } else if ( type_index(typeid(ast::ReferenceType)) == lid ) {
663 ast::ReferenceType const * l = as<ast::ReferenceType>(lhs);
664 ast::ReferenceType const * r = as<ast::ReferenceType>(rhs);
665
666 // void references should match any other reference type.
667 return is<ast::VoidType>( l->base.get() )
668 || is<ast::VoidType>( r->base.get() )
669 || typesPolyCompatible( l->base.get(), r->base.get() );
670 } else if ( type_index(typeid(ast::ArrayType)) == lid ) {
671 ast::ArrayType const * l = as<ast::ArrayType>(lhs);
672 ast::ArrayType const * r = as<ast::ArrayType>(rhs);
673
674 if ( l->isVarLen ) {
675 if ( !r->isVarLen ) return false;
676 } else {
677 if ( r->isVarLen ) return false;
678
679 auto lc = l->dimension.as<ast::ConstantExpr>();
680 auto rc = r->dimension.as<ast::ConstantExpr>();
681 if ( lc && rc && lc->intValue() != rc->intValue() ) {
682 return false;
683 }
684 }
685
686 return typesPolyCompatible( l->base.get(), r->base.get() );
687 } else if ( type_index(typeid(ast::FunctionType)) == lid ) {
688 ast::FunctionType const * l = as<ast::FunctionType>(lhs);
689 ast::FunctionType const * r = as<ast::FunctionType>(rhs);
690
691 std::vector<ast::ptr<ast::Type>> lparams, rparams;
692 flattenList( l->params, lparams );
693 flattenList( r->params, rparams );
694 if ( lparams.size() != rparams.size() ) return false;
695 for ( unsigned i = 0; i < lparams.size(); ++i ) {
696 if ( !typesPolyCompatible( lparams[i], rparams[i] ) ) return false;
697 }
698
699 std::vector<ast::ptr<ast::Type>> lrets, rrets;
700 flattenList( l->returns, lrets );
701 flattenList( r->returns, rrets );
702 if ( lrets.size() != rrets.size() ) return false;
703 for ( unsigned i = 0; i < lrets.size(); ++i ) {
704 if ( !typesPolyCompatible( lrets[i], rrets[i] ) ) return false;
705 }
706 return true;
707 } else if ( type_index(typeid(ast::StructInstType)) == lid ) {
708 ast::StructInstType const * l = as<ast::StructInstType>(lhs);
709 ast::StructInstType const * r = as<ast::StructInstType>(rhs);
710
711 if ( l->name != r->name ) return false;
712 return paramListsPolyCompatible( l->params, r->params );
713 } else if ( type_index(typeid(ast::UnionInstType)) == lid ) {
714 ast::UnionInstType const * l = as<ast::UnionInstType>(lhs);
715 ast::UnionInstType const * r = as<ast::UnionInstType>(rhs);
716
717 if ( l->name != r->name ) return false;
718 return paramListsPolyCompatible( l->params, r->params );
719 } else if ( type_index(typeid(ast::EnumInstType)) == lid ) {
720 ast::EnumInstType const * l = as<ast::EnumInstType>(lhs);
721 ast::EnumInstType const * r = as<ast::EnumInstType>(rhs);
722
723 return l->name == r->name;
724 } else if ( type_index(typeid(ast::TraitInstType)) == lid ) {
725 ast::TraitInstType const * l = as<ast::TraitInstType>(lhs);
726 ast::TraitInstType const * r = as<ast::TraitInstType>(rhs);
727
728 return l->name == r->name;
729 } else if ( type_index(typeid(ast::TupleType)) == lid ) {
730 ast::TupleType const * l = as<ast::TupleType>(lhs);
731 ast::TupleType const * r = as<ast::TupleType>(rhs);
732
733 std::vector<ast::ptr<ast::Type>> ltypes, rtypes;
734 flattenList( l->types, ( ltypes ) );
735 flattenList( r->types, ( rtypes ) );
736 if ( ltypes.size() != rtypes.size() ) return false;
737
738 for ( unsigned i = 0 ; i < ltypes.size() ; ++i ) {
739 if ( !typesPolyCompatible( ltypes[i], rtypes[i] ) ) return false;
740 }
741 return true;
742 // The remaining types (VoidType, VarArgsType, ZeroType & OneType)
743 // have no variation so will always be equal.
744 } else {
745 return true;
746 }
747}
748
749 bool needsBoxing( Type * param, Type * arg, const TyVarMap &exprTyVars, const TypeSubstitution * env ) {
750 // is parameter is not polymorphic, don't need to box
751 if ( ! isPolyType( param, exprTyVars ) ) return false;
752 Type * newType = arg->clone();
753 if ( env ) env->apply( newType );
754 std::unique_ptr<Type> manager( newType );
755 // if the argument's type is polymorphic, we don't need to box again!
756 return ! isPolyType( newType );
757 }
758
759bool needsBoxing( const ast::Type * param, const ast::Type * arg,
760 const TypeVarMap & typeVars, const ast::TypeSubstitution * subst ) {
761 // Don't need to box if the parameter is not polymorphic.
762 if ( !isPolyType( param, typeVars ) ) return false;
763
764 ast::ptr<ast::Type> newType = arg;
765 if ( subst ) {
766 int count = subst->apply( newType );
767 (void)count;
768 }
769 // Only need to box if the argument is not also polymorphic.
770 return !isPolyType( newType );
771}
772
773 bool needsBoxing( Type * param, Type * arg, ApplicationExpr * appExpr, const TypeSubstitution * env ) {
774 FunctionType * function = getFunctionType( appExpr->function->result );
775 assertf( function, "ApplicationExpr has non-function type: %s", toString( appExpr->function->result ).c_str() );
776 TyVarMap exprTyVars( TypeDecl::Data{} );
777 makeTyVarMap( function, exprTyVars );
778 return needsBoxing( param, arg, exprTyVars, env );
779 }
780
781bool needsBoxing(
782 const ast::Type * param, const ast::Type * arg,
783 const ast::ApplicationExpr * expr,
784 const ast::TypeSubstitution * subst ) {
785 const ast::FunctionType * function = getFunctionType( expr->func->result );
786 assertf( function, "ApplicationExpr has non-function type: %s", toString( expr->func->result ).c_str() );
787 TypeVarMap exprTyVars = { ast::TypeData() };
788 makeTypeVarMap( function, exprTyVars );
789 return needsBoxing( param, arg, exprTyVars, subst );
790}
791
792 void addToTyVarMap( TypeDecl * tyVar, TyVarMap &tyVarMap ) {
793 tyVarMap.insert( tyVar->name, TypeDecl::Data{ tyVar } );
794 }
795
796void addToTypeVarMap( const ast::TypeInstType * type, TypeVarMap & typeVars ) {
797 typeVars.insert( *type, ast::TypeData( type->base ) );
798}
799
800 void makeTyVarMap( Type *type, TyVarMap &tyVarMap ) {
801 for ( Type::ForallList::const_iterator tyVar = type->get_forall().begin(); tyVar != type->get_forall().end(); ++tyVar ) {
802 assert( *tyVar );
803 addToTyVarMap( *tyVar, tyVarMap );
804 }
805 if ( PointerType *pointer = dynamic_cast< PointerType* >( type ) ) {
806 makeTyVarMap( pointer->get_base(), tyVarMap );
807 }
808 }
809
810void makeTypeVarMap( const ast::Type * type, TypeVarMap & typeVars ) {
811 if ( auto func = dynamic_cast<ast::FunctionType const *>( type ) ) {
812 for ( auto & typeVar : func->forall ) {
813 assert( typeVar );
814 addToTypeVarMap( typeVar, typeVars );
815 }
816 }
817 if ( auto pointer = dynamic_cast<ast::PointerType const *>( type ) ) {
818 makeTypeVarMap( pointer->base, typeVars );
819 }
820}
821
822 void printTyVarMap( std::ostream &os, const TyVarMap &tyVarMap ) {
823 for ( TyVarMap::const_iterator i = tyVarMap.begin(); i != tyVarMap.end(); ++i ) {
824 os << i->first << " (" << i->second << ") ";
825 } // for
826 os << std::endl;
827 }
828
829} // namespace GenPoly
830
831// Local Variables: //
832// tab-width: 4 //
833// mode: c++ //
834// compile-command: "make install" //
835// End: //
Note: See TracBrowser for help on using the repository browser.