source: src/GenPoly/GenPoly.cpp@ 16d862d

Last change on this file since 16d862d was 16d862d, checked in by Peter A. Buhr <pabuhr@…>, 2 days ago

formatting

  • Property mode set to 100644
File size: 16.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.cpp -- General GenPoly utilities.
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 : Sun Jun 28 21:49:48 2026
13// Update Count : 18
14//
15
16#include "GenPoly.hpp"
17
18#include <cassert> // for assertf, assert
19#include <iostream> // for operator<<, ostream, basic_...
20#include <iterator> // for back_insert_iterator, back_...
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 "Common/Eval.hpp" // for eval
30#include "GenPoly/ErasableScopedMap.hpp" // for ErasableScopedMap<>::const_...
31#include "ResolvExpr/Typeops.hpp" // for flatten
32
33using namespace std;
34
35namespace GenPoly {
36
37namespace {
38 /// Checks a parameter list for polymorphic parameters; will substitute according to env if present.
39 bool hasPolyParams( const std::vector<ast::ptr<ast::Expr>> & params, const ast::TypeSubstitution * env ) {
40 for ( auto & param : params ) {
41 auto paramType = param.as<ast::TypeExpr>();
42 assertf( paramType, "Aggregate parameters should be type expressions" );
43 if ( isPolyType( paramType->type, env ) ) return true;
44 }
45 return false;
46 }
47
48 /// Checks a parameter list for polymorphic parameters from typeVars; will substitute according to env if present.
49 bool hasPolyParams( const std::vector<ast::ptr<ast::Expr>> & params, const TypeVarMap & typeVars, const ast::TypeSubstitution * env ) {
50 for ( auto & param : params ) {
51 auto paramType = param.as<ast::TypeExpr>();
52 assertf( paramType, "Aggregate parameters should be type expressions" );
53 if ( isPolyType( paramType->type, typeVars, env ) ) return true;
54 }
55 return false;
56 }
57
58 /// Checks a parameter list for dynamic-layout parameters from tyVars; will substitute according to env if present.
59 bool hasDynParams(
60 const std::vector<ast::ptr<ast::Expr>> & params,
61 const TypeVarMap & typeVars,
62 const ast::TypeSubstitution * subst ) {
63 for ( ast::ptr<ast::Expr> const & paramExpr : params ) {
64 auto param = paramExpr.as<ast::TypeExpr>();
65 assertf( param, "Aggregate parameters should be type expressions." );
66 if ( isDynType( param->type.get(), typeVars, subst ) ) {
67 return true;
68 }
69 }
70 return false;
71 }
72} // namespace
73
74const ast::Type * replaceTypeInst( const ast::Type * type, const ast::TypeSubstitution * env ) {
75 if ( !env ) return type;
76 if ( auto typeInst = dynamic_cast<const ast::TypeInstType*>( type ) ) {
77 if ( auto newType = env->lookup( typeInst ) ) return newType;
78 }
79 return type;
80}
81
82const ast::Type * isPolyType( const ast::Type * type, const ast::TypeSubstitution * subst ) {
83 type = replaceTypeInst( type, subst );
84
85 if ( dynamic_cast< const ast::TypeInstType * >( type ) ) {
86 // This case is where the two variants of isPolyType differ.
87 return type;
88 } else if ( auto arrayType = dynamic_cast< const ast::ArrayType * >( type ) ) {
89 return isPolyType( arrayType->base, subst );
90 } else if ( auto structType = dynamic_cast< const ast::StructInstType* >( type ) ) {
91 if ( hasPolyParams( structType->params, subst ) ) return type;
92 } else if ( auto unionType = dynamic_cast< const ast::UnionInstType* >( type ) ) {
93 if ( hasPolyParams( unionType->params, subst ) ) return type;
94 }
95 return nullptr;
96}
97
98const ast::Type * isPolyType( const ast::Type * type,
99 const TypeVarMap & typeVars, const ast::TypeSubstitution * subst ) {
100 type = replaceTypeInst( type, subst );
101
102 if ( auto inst = dynamic_cast< const ast::TypeInstType * >( type ) ) {
103 if ( typeVars.contains( *inst ) ) return type;
104 } else if ( auto array = dynamic_cast< const ast::ArrayType * >( type ) ) {
105 return isPolyType( array->base, typeVars, subst );
106 } else if ( auto sue = dynamic_cast< const ast::StructInstType * >( type ) ) {
107 if ( hasPolyParams( sue->params, typeVars, subst ) ) return type;
108 } else if ( auto sue = dynamic_cast< const ast::UnionInstType * >( type ) ) {
109 if ( hasPolyParams( sue->params, typeVars, subst ) ) return type;
110 }
111 return nullptr;
112}
113
114const ast::BaseInstType * isDynType(
115 const ast::Type * type, const TypeVarMap & typeVars,
116 const ast::TypeSubstitution * subst ) {
117 type = replaceTypeInst( type, subst );
118
119 if ( auto inst = dynamic_cast<ast::TypeInstType const *>( type ) ) {
120 auto var = typeVars.find( *inst );
121 if ( var != typeVars.end() && var->second.isComplete ) {
122 return inst;
123 }
124 } else if ( auto inst = dynamic_cast<ast::StructInstType const *>( type ) ) {
125 if ( hasDynParams( inst->params, typeVars, subst ) ) return inst;
126 } else if ( auto inst = dynamic_cast<ast::UnionInstType const *>( type ) ) {
127 if ( hasDynParams( inst->params, typeVars, subst ) ) return inst;
128 }
129 return nullptr;
130}
131
132const ast::BaseInstType *isDynRet(
133 const ast::FunctionType * type, const TypeVarMap & typeVars ) {
134 if ( type->returns.empty() ) return nullptr;
135
136 return isDynType( type->returns.front(), typeVars );
137}
138
139const ast::BaseInstType *isDynRet( const ast::FunctionType * func ) {
140 if ( func->returns.empty() ) return nullptr;
141
142 TypeVarMap forallTypes;
143 makeTypeVarMap( func, forallTypes );
144 return isDynType( func->returns.front(), forallTypes );
145}
146
147bool needsAdapter(
148 ast::FunctionType const * adaptee, const TypeVarMap & typeVars ) {
149 if ( isDynRet( adaptee, typeVars ) ) return true;
150
151 for ( auto param : adaptee->params ) {
152 if ( isDynType( param, typeVars ) ) {
153 return true;
154 }
155 }
156 return false;
157}
158
159const ast::Type * isPolyPtr(
160 const ast::Type * type, const TypeVarMap & typeVars,
161 const ast::TypeSubstitution * typeSubs ) {
162 type = replaceTypeInst( type, typeSubs );
163
164 if ( auto * ptr = dynamic_cast<ast::PointerType const *>( type ) ) {
165 return isPolyType( ptr->base, typeVars, typeSubs );
166 }
167 return nullptr;
168}
169
170ast::Type const * hasPolyBase(
171 ast::Type const * type, const TypeVarMap & typeVars,
172 int * levels, const ast::TypeSubstitution * subst ) {
173 int level_count = 0;
174
175 while ( true ) {
176 type = replaceTypeInst( type, subst );
177
178 if ( auto ptr = dynamic_cast<ast::PointerType const *>( type ) ) {
179 type = ptr->base;
180 ++level_count;
181 } else {
182 break;
183 }
184 }
185
186 if ( nullptr != levels ) { *levels = level_count; }
187 return isPolyType( type, typeVars, subst );
188}
189
190const ast::FunctionType * getFunctionType( const ast::Type * ty ) {
191 if ( auto pty = dynamic_cast< const ast::PointerType * >( ty ) ) {
192 return pty->base.as< ast::FunctionType >();
193 } else {
194 return dynamic_cast< const ast::FunctionType * >( ty );
195 }
196}
197
198namespace {
199 /// Checks if is a pointer to D
200 template<typename D, typename B>
201 bool is( const B* p ) { return type_index{typeid(D)} == type_index{typeid(*p)}; }
202
203 /// Converts to a pointer to D without checking for safety
204 template<typename D, typename B>
205 inline D* as( B* p ) { return reinterpret_cast<D*>(p); }
206
207 template<typename D, typename B>
208 inline D const * as( B const * p ) {
209 return reinterpret_cast<D const *>( p );
210 }
211
212 /// Flattens a list of types.
213 void flattenList( vector<ast::ptr<ast::Type>> const & src,
214 vector<ast::ptr<ast::Type>> & out ) {
215 for ( auto const & type : src ) {
216 ResolvExpr::flatten( type, out );
217 }
218 }
219
220 bool paramListsPolyCompatible(
221 std::vector<ast::ptr<ast::Expr>> const & lparams,
222 std::vector<ast::ptr<ast::Expr>> const & rparams ) {
223 if ( lparams.size() != rparams.size() ) {
224 return false;
225 }
226
227 for ( auto lparam = lparams.begin(), rparam = rparams.begin() ;
228 lparam != lparams.end() ; ++lparam, ++rparam ) {
229 ast::TypeExpr const * lexpr = lparam->as<ast::TypeExpr>();
230 assertf( lexpr, "Aggregate parameters should be type expressions" );
231 ast::TypeExpr const * rexpr = rparam->as<ast::TypeExpr>();
232 assertf( rexpr, "Aggregate parameters should be type expressions" );
233
234 // xxx - might need to let VoidType be a wildcard here too; could have some voids
235 // stuffed in for dtype-statics.
236 // if ( is<VoidType>( lexpr->type() ) || is<VoidType>( bparam->get_type() ) ) continue;
237 if ( !typesPolyCompatible( lexpr->type, rexpr->type ) ) {
238 return false;
239 }
240 }
241
242 return true;
243 }
244} // namespace
245
246// This function, and its helpers following, have logic duplicated from
247// unification. The difference in context is that unification applies where
248// the types "must" match, while this variation applies to arbitrary type
249// pairs, when an optimization could apply if they happen to match. This
250// variation does not bind type variables. The helper functions support
251// the case for matching ArrayType.
252bool typesPolyCompatible( ast::Type const * lhs, ast::Type const * rhs );
253
254static bool exprsPolyCompatibleByStaticValue( const ast::Expr * e1, const ast::Expr * e2 ) {
255 Evaluation r1 = eval(e1);
256 Evaluation r2 = eval(e2);
257
258 if ( !r1.hasKnownValue ) return false;
259 if ( !r2.hasKnownValue ) return false;
260
261 if ( r1.knownValue != r2.knownValue ) return false;
262
263 return true;
264}
265
266static bool exprsPolyCompatible( ast::Expr const * lhs,
267 ast::Expr const * rhs ) {
268 type_index const lid = typeid(*lhs);
269 type_index const rid = typeid(*rhs);
270 if ( lid != rid ) return false;
271
272 if ( exprsPolyCompatibleByStaticValue( lhs, rhs ) ) return true;
273
274 if ( type_index(typeid(ast::CastExpr)) == lid ) {
275 ast::CastExpr const * l = as<ast::CastExpr>(lhs);
276 ast::CastExpr const * r = as<ast::CastExpr>(rhs);
277
278 // inspect casts' target types
279 if ( !typesPolyCompatible(
280 l->result, r->result ) ) return false;
281
282 // inspect casts' inner expressions
283 return exprsPolyCompatible( l->arg, r->arg );
284
285 } else if ( type_index(typeid(ast::VariableExpr)) == lid ) {
286 ast::VariableExpr const * l = as<ast::VariableExpr>(lhs);
287 ast::VariableExpr const * r = as<ast::VariableExpr>(rhs);
288
289 assert(l->var);
290 assert(r->var);
291
292 // conservative: variable exprs match if their declarations are
293 // represented by the same C++ AST object
294 return (l->var == r->var);
295
296 } else if ( type_index(typeid(ast::SizeofExpr)) == lid ) {
297 ast::SizeofExpr const * l = as<ast::SizeofExpr>(lhs);
298 ast::SizeofExpr const * r = as<ast::SizeofExpr>(rhs);
299
300 assert( l->type );
301 assert( r->type );
302
303 // mutual recursion with type poly compatibility
304 return typesPolyCompatible( l->type, r->type );
305
306 } else {
307 // All other forms compare on static value only, done earlier
308 return false;
309 }
310}
311
312bool typesPolyCompatible( ast::Type const * lhs, ast::Type const * rhs ) {
313 type_index const lid = typeid(*lhs);
314
315 // Polymorphic types always match:
316 if ( type_index(typeid(ast::TypeInstType)) == lid ) return true;
317
318 type_index const rid = typeid(*rhs);
319 if ( type_index(typeid(ast::TypeInstType)) == rid ) return true;
320
321 // All other types only match if they are the same type:
322 if ( lid != rid ) return false;
323
324 // So remaining types can be examined case by case.
325 // Recurse through type structure (conditions duplicated from Unify.cpp).
326
327 if ( type_index(typeid(ast::BasicType)) == lid ) {
328 return as<ast::BasicType>(lhs)->kind == as<ast::BasicType>(rhs)->kind;
329 } else if ( type_index(typeid(ast::PointerType)) == lid ) {
330 ast::PointerType const * l = as<ast::PointerType>(lhs);
331 ast::PointerType const * r = as<ast::PointerType>(rhs);
332
333 // void pointers should match any other pointer type.
334 return is<ast::VoidType>( l->base.get() )
335 || is<ast::VoidType>( r->base.get() )
336 || typesPolyCompatible( l->base.get(), r->base.get() );
337 } else if ( type_index(typeid(ast::ReferenceType)) == lid ) {
338 ast::ReferenceType const * l = as<ast::ReferenceType>(lhs);
339 ast::ReferenceType const * r = as<ast::ReferenceType>(rhs);
340
341 // void references should match any other reference type.
342 return is<ast::VoidType>( l->base.get() )
343 || is<ast::VoidType>( r->base.get() )
344 || typesPolyCompatible( l->base.get(), r->base.get() );
345 } else if ( type_index(typeid(ast::ArrayType)) == lid ) {
346 ast::ArrayType const * l = as<ast::ArrayType>(lhs);
347 ast::ArrayType const * r = as<ast::ArrayType>(rhs);
348
349 if ( l->isVarLen != r->isVarLen ) return false;
350 if ( (l->dimension != nullptr) != (r->dimension != nullptr) )
351 return false;
352
353 if ( l->dimension ) {
354 assert( r->dimension );
355 // mutual recursion with expression poly compatibility
356 if ( !exprsPolyCompatible(l->dimension, r->dimension) )
357 return false;
358 }
359
360 return typesPolyCompatible( l->base.get(), r->base.get() );
361 } else if ( type_index(typeid(ast::FunctionType)) == lid ) {
362 ast::FunctionType const * l = as<ast::FunctionType>(lhs);
363 ast::FunctionType const * r = as<ast::FunctionType>(rhs);
364
365 std::vector<ast::ptr<ast::Type>> lparams, rparams;
366 flattenList( l->params, lparams );
367 flattenList( r->params, rparams );
368 if ( lparams.size() != rparams.size() ) return false;
369 for ( unsigned i = 0; i < lparams.size(); ++i ) {
370 if ( !typesPolyCompatible( lparams[i], rparams[i] ) ) return false;
371 }
372
373 std::vector<ast::ptr<ast::Type>> lrets, rrets;
374 flattenList( l->returns, lrets );
375 flattenList( r->returns, rrets );
376 if ( lrets.size() != rrets.size() ) return false;
377 for ( unsigned i = 0; i < lrets.size(); ++i ) {
378 if ( !typesPolyCompatible( lrets[i], rrets[i] ) ) return false;
379 }
380 return true;
381 } else if ( type_index(typeid(ast::StructInstType)) == lid ) {
382 ast::StructInstType const * l = as<ast::StructInstType>(lhs);
383 ast::StructInstType const * r = as<ast::StructInstType>(rhs);
384
385 if ( l->name != r->name ) return false;
386 return paramListsPolyCompatible( l->params, r->params );
387 } else if ( type_index(typeid(ast::UnionInstType)) == lid ) {
388 ast::UnionInstType const * l = as<ast::UnionInstType>(lhs);
389 ast::UnionInstType const * r = as<ast::UnionInstType>(rhs);
390
391 if ( l->name != r->name ) return false;
392 return paramListsPolyCompatible( l->params, r->params );
393 } else if ( type_index(typeid(ast::EnumInstType)) == lid ) {
394 ast::EnumInstType const * l = as<ast::EnumInstType>(lhs);
395 ast::EnumInstType const * r = as<ast::EnumInstType>(rhs);
396
397 return l->name == r->name;
398 } else if ( type_index(typeid(ast::TraitInstType)) == lid ) {
399 ast::TraitInstType const * l = as<ast::TraitInstType>(lhs);
400 ast::TraitInstType const * r = as<ast::TraitInstType>(rhs);
401
402 return l->name == r->name;
403 } else if ( type_index(typeid(ast::TupleType)) == lid ) {
404 ast::TupleType const * l = as<ast::TupleType>(lhs);
405 ast::TupleType const * r = as<ast::TupleType>(rhs);
406
407 std::vector<ast::ptr<ast::Type>> ltypes, rtypes;
408 flattenList( l->types, ( ltypes ) );
409 flattenList( r->types, ( rtypes ) );
410 if ( ltypes.size() != rtypes.size() ) return false;
411
412 for ( unsigned i = 0 ; i < ltypes.size() ; ++i ) {
413 if ( !typesPolyCompatible( ltypes[i], rtypes[i] ) ) return false;
414 }
415 return true;
416 // The remaining types (VoidType, VarArgsType, ZeroType & OneType)
417 // have no variation so will always be equal.
418 } else {
419 return true;
420 }
421}
422
423bool needsBoxing( const ast::Type * param, const ast::Type * arg,
424 const TypeVarMap & typeVars, const ast::TypeSubstitution * subst ) {
425 // Don't need to box if the parameter is not polymorphic.
426 if ( !isPolyType( param, typeVars ) ) return false;
427
428 ast::ptr<ast::Type> newType = arg;
429 if ( subst ) {
430 int count = subst->apply( newType );
431 (void)count;
432 }
433 // Only need to box if the argument is not also polymorphic.
434 return !isPolyType( newType );
435}
436
437bool needsBoxing(
438 const ast::Type * param, const ast::Type * arg,
439 const ast::ApplicationExpr * expr,
440 const ast::TypeSubstitution * subst ) {
441 const ast::FunctionType * function = getFunctionType( expr->func->result );
442 assertf( function, "ApplicationExpr has non-function type: %s", toCString( expr->func->result ) );
443 TypeVarMap exprTyVars;
444 makeTypeVarMap( function, exprTyVars );
445 return needsBoxing( param, arg, exprTyVars, subst );
446}
447
448void addToTypeVarMap( const ast::TypeDecl * decl, TypeVarMap & typeVars ) {
449 typeVars.insert( ast::TypeEnvKey( decl, 0, 0 ), ast::TypeData( decl ) );
450}
451
452void addToTypeVarMap( const ast::TypeInstType * type, TypeVarMap & typeVars ) {
453 typeVars.insert( ast::TypeEnvKey( *type ), ast::TypeData( type->base ) );
454}
455
456void makeTypeVarMap( const ast::Type * type, TypeVarMap & typeVars ) {
457 if ( auto func = dynamic_cast<ast::FunctionType const *>( type ) ) {
458 for ( auto & typeVar : func->forall ) {
459 assert( typeVar );
460 addToTypeVarMap( typeVar, typeVars );
461 }
462 }
463 if ( auto pointer = dynamic_cast<ast::PointerType const *>( type ) ) {
464 makeTypeVarMap( pointer->base, typeVars );
465 }
466}
467
468void makeTypeVarMap( const ast::FunctionDecl * decl, TypeVarMap & typeVars ) {
469 for ( auto & typeDecl : decl->type_params ) {
470 addToTypeVarMap( typeDecl, typeVars );
471 }
472}
473
474} // namespace GenPoly
475
476// Local Variables: //
477// tab-width: 4 //
478// mode: c++ //
479// compile-command: "make install" //
480// End: //
Note: See TracBrowser for help on using the repository browser.