source: src/GenPoly/GenPoly.cc@ 5e3f1e1

ADT ast-experimental
Last change on this file since 5e3f1e1 was e9b5043, checked in by Andrew Beach <ajbeach@…>, 3 years ago

Added contains to some of our containers. Also changed some code to use the new method.

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