source: src/ResolvExpr/Unify.cc@ 10dc6908

ADT arm-eh ast-experimental cleanup-dtors enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since 10dc6908 was f0ecf9b, checked in by Rob Schluntz <rschlunt@…>, 8 years ago

Remove TypeDecl::Any, as it is subsumed by Dtype+sized

  • Property mode set to 100644
File size: 32.9 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// Unify.cc --
8//
9// Author : Richard C. Bilson
10// Created On : Sun May 17 12:27:10 2015
11// Last Modified By : Peter A. Buhr
12// Last Modified On : Thu Mar 16 16:22:54 2017
13// Update Count : 42
14//
15
16#include <cassert> // for assertf, assert
17#include <iterator> // for back_insert_iterator, back_inserter
18#include <map> // for _Rb_tree_const_iterator, _Rb_tree_i...
19#include <memory> // for unique_ptr
20#include <set> // for set
21#include <string> // for string, operator==, operator!=, bas...
22#include <utility> // for pair
23
24#include "Common/PassVisitor.h" // for PassVisitor
25#include "FindOpenVars.h" // for findOpenVars
26#include "Parser/LinkageSpec.h" // for C
27#include "SynTree/Constant.h" // for Constant
28#include "SynTree/Declaration.h" // for TypeDecl, TypeDecl::Data, Declarati...
29#include "SynTree/Expression.h" // for TypeExpr, Expression, ConstantExpr
30#include "SynTree/Mutator.h" // for Mutator
31#include "SynTree/Type.h" // for Type, TypeInstType, FunctionType
32#include "SynTree/Visitor.h" // for Visitor
33#include "Tuples/Tuples.h" // for isTtype
34#include "TypeEnvironment.h" // for EqvClass, AssertionSet, OpenVarSet
35#include "Unify.h"
36#include "typeops.h" // for flatten, occurs, commonType
37
38namespace SymTab {
39class Indexer;
40} // namespace SymTab
41
42// #define DEBUG
43
44namespace ResolvExpr {
45
46 class Unify : public Visitor {
47 public:
48 Unify( Type *type2, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, WidenMode widenMode, const SymTab::Indexer &indexer );
49
50 bool get_result() const { return result; }
51 private:
52 virtual void visit(VoidType *voidType);
53 virtual void visit(BasicType *basicType);
54 virtual void visit(PointerType *pointerType);
55 virtual void visit(ArrayType *arrayType);
56 virtual void visit(ReferenceType *refType);
57 virtual void visit(FunctionType *functionType);
58 virtual void visit(StructInstType *aggregateUseType);
59 virtual void visit(UnionInstType *aggregateUseType);
60 virtual void visit(EnumInstType *aggregateUseType);
61 virtual void visit(TraitInstType *aggregateUseType);
62 virtual void visit(TypeInstType *aggregateUseType);
63 virtual void visit(TupleType *tupleType);
64 virtual void visit(VarArgsType *varArgsType);
65 virtual void visit(ZeroType *zeroType);
66 virtual void visit(OneType *oneType);
67
68 template< typename RefType > void handleRefType( RefType *inst, Type *other );
69 template< typename RefType > void handleGenericRefType( RefType *inst, Type *other );
70
71 bool result;
72 Type *type2; // inherited
73 TypeEnvironment &env;
74 AssertionSet &needAssertions;
75 AssertionSet &haveAssertions;
76 const OpenVarSet &openVars;
77 WidenMode widenMode;
78 const SymTab::Indexer &indexer;
79 };
80
81 /// Attempts an inexact unification of type1 and type2.
82 /// Returns false if no such unification; if the types can be unified, sets common (unless they unify exactly and have identical type qualifiers)
83 bool unifyInexact( Type *type1, Type *type2, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, WidenMode widenMode, const SymTab::Indexer &indexer, Type *&common );
84 bool unifyExact( Type *type1, Type *type2, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, WidenMode widenMode, const SymTab::Indexer &indexer );
85
86 bool typesCompatible( Type *first, Type *second, const SymTab::Indexer &indexer, const TypeEnvironment &env ) {
87 TypeEnvironment newEnv;
88 OpenVarSet openVars, closedVars; // added closedVars
89 AssertionSet needAssertions, haveAssertions;
90 Type *newFirst = first->clone(), *newSecond = second->clone();
91 env.apply( newFirst );
92 env.apply( newSecond );
93
94 // do we need to do this? Seems like we do, types should be able to be compatible if they
95 // have free variables that can unify
96 findOpenVars( newFirst, openVars, closedVars, needAssertions, haveAssertions, false );
97 findOpenVars( newSecond, openVars, closedVars, needAssertions, haveAssertions, true );
98
99 bool result = unifyExact( newFirst, newSecond, newEnv, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
100 delete newFirst;
101 delete newSecond;
102 return result;
103 }
104
105 bool typesCompatibleIgnoreQualifiers( Type *first, Type *second, const SymTab::Indexer &indexer, const TypeEnvironment &env ) {
106 TypeEnvironment newEnv;
107 OpenVarSet openVars;
108 AssertionSet needAssertions, haveAssertions;
109 Type *newFirst = first->clone(), *newSecond = second->clone();
110 env.apply( newFirst );
111 env.apply( newSecond );
112 newFirst->get_qualifiers() = Type::Qualifiers();
113 newSecond->get_qualifiers() = Type::Qualifiers();
114/// std::cerr << "first is ";
115/// first->print( std::cerr );
116/// std::cerr << std::endl << "second is ";
117/// second->print( std::cerr );
118/// std::cerr << std::endl << "newFirst is ";
119/// newFirst->print( std::cerr );
120/// std::cerr << std::endl << "newSecond is ";
121/// newSecond->print( std::cerr );
122/// std::cerr << std::endl;
123 bool result = unifyExact( newFirst, newSecond, newEnv, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
124 delete newFirst;
125 delete newSecond;
126 return result;
127 }
128
129 bool isFtype( Type *type ) {
130 if ( dynamic_cast< FunctionType* >( type ) ) {
131 return true;
132 } else if ( TypeInstType *typeInst = dynamic_cast< TypeInstType* >( type ) ) {
133 return typeInst->get_isFtype();
134 } // if
135 return false;
136 }
137
138 bool tyVarCompatible( const TypeDecl::Data & data, Type *type ) {
139 switch ( data.kind ) {
140 case TypeDecl::Dtype:
141 // to bind to an object type variable, the type must not be a function type.
142 // if the type variable is specified to be a complete type then the incoming
143 // type must also be complete
144 // xxx - should this also check that type is not a tuple type and that it's not a ttype?
145 return ! isFtype( type ) && (! data.isComplete || type->isComplete() );
146 case TypeDecl::Ftype:
147 return isFtype( type );
148 case TypeDecl::Ttype:
149 // ttype unifies with any tuple type
150 return dynamic_cast< TupleType * >( type ) || Tuples::isTtype( type );
151 } // switch
152 return false;
153 }
154
155 bool bindVar( TypeInstType *typeInst, Type *other, const TypeDecl::Data & data, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, WidenMode widenMode, const SymTab::Indexer &indexer ) {
156 // remove references from other, so that type variables can only bind to value types
157 other = other->stripReferences();
158 OpenVarSet::const_iterator tyvar = openVars.find( typeInst->get_name() );
159 assert( tyvar != openVars.end() );
160 if ( ! tyVarCompatible( tyvar->second, other ) ) {
161 return false;
162 } // if
163 if ( occurs( other, typeInst->get_name(), env ) ) {
164 return false;
165 } // if
166 EqvClass curClass;
167 if ( env.lookup( typeInst->get_name(), curClass ) ) {
168 if ( curClass.type ) {
169 Type *common = 0;
170 // attempt to unify equivalence class type (which has qualifiers stripped, so they must be restored) with the type to bind to
171 std::unique_ptr< Type > newType( curClass.type->clone() );
172 newType->get_qualifiers() = typeInst->get_qualifiers();
173 if ( unifyInexact( newType.get(), other, env, needAssertions, haveAssertions, openVars, widenMode & WidenMode( curClass.allowWidening, true ), indexer, common ) ) {
174 if ( common ) {
175 common->get_qualifiers() = Type::Qualifiers();
176 delete curClass.type;
177 curClass.type = common;
178 env.add( curClass );
179 } // if
180 return true;
181 } else {
182 return false;
183 } // if
184 } else {
185 curClass.type = other->clone();
186 curClass.type->get_qualifiers() = Type::Qualifiers();
187 curClass.allowWidening = widenMode.widenFirst && widenMode.widenSecond;
188 env.add( curClass );
189 } // if
190 } else {
191 EqvClass newClass;
192 newClass.vars.insert( typeInst->get_name() );
193 newClass.type = other->clone();
194 newClass.type->get_qualifiers() = Type::Qualifiers();
195 newClass.allowWidening = widenMode.widenFirst && widenMode.widenSecond;
196 newClass.data = data;
197 env.add( newClass );
198 } // if
199 return true;
200 }
201
202 bool bindVarToVar( TypeInstType *var1, TypeInstType *var2, const TypeDecl::Data & data, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, WidenMode widenMode, const SymTab::Indexer &indexer ) {
203 bool result = true;
204 EqvClass class1, class2;
205 bool hasClass1 = false, hasClass2 = false;
206 bool widen1 = false, widen2 = false;
207 Type *type1 = 0, *type2 = 0;
208
209 if ( env.lookup( var1->get_name(), class1 ) ) {
210 hasClass1 = true;
211 if ( class1.type ) {
212 if ( occurs( class1.type, var2->get_name(), env ) ) {
213 return false;
214 } // if
215 type1 = class1.type->clone();
216 } // if
217 widen1 = widenMode.widenFirst && class1.allowWidening;
218 } // if
219 if ( env.lookup( var2->get_name(), class2 ) ) {
220 hasClass2 = true;
221 if ( class2.type ) {
222 if ( occurs( class2.type, var1->get_name(), env ) ) {
223 return false;
224 } // if
225 type2 = class2.type->clone();
226 } // if
227 widen2 = widenMode.widenSecond && class2.allowWidening;
228 } // if
229
230 if ( type1 && type2 ) {
231// std::cerr << "has type1 && type2" << std::endl;
232 WidenMode newWidenMode ( widen1, widen2 );
233 Type *common = 0;
234 if ( unifyInexact( type1, type2, env, needAssertions, haveAssertions, openVars, newWidenMode, indexer, common ) ) {
235 class1.vars.insert( class2.vars.begin(), class2.vars.end() );
236 class1.allowWidening = widen1 && widen2;
237 if ( common ) {
238 common->get_qualifiers() = Type::Qualifiers();
239 delete class1.type;
240 class1.type = common;
241 } // if
242 env.add( class1 );
243 } else {
244 result = false;
245 } // if
246 } else if ( hasClass1 && hasClass2 ) {
247 if ( type1 ) {
248 class1.vars.insert( class2.vars.begin(), class2.vars.end() );
249 class1.allowWidening = widen1;
250 env.add( class1 );
251 } else {
252 class2.vars.insert( class1.vars.begin(), class1.vars.end() );
253 class2.allowWidening = widen2;
254 env.add( class2 );
255 } // if
256 } else if ( hasClass1 ) {
257 class1.vars.insert( var2->get_name() );
258 class1.allowWidening = widen1;
259 env.add( class1 );
260 } else if ( hasClass2 ) {
261 class2.vars.insert( var1->get_name() );
262 class2.allowWidening = widen2;
263 env.add( class2 );
264 } else {
265 EqvClass newClass;
266 newClass.vars.insert( var1->get_name() );
267 newClass.vars.insert( var2->get_name() );
268 newClass.allowWidening = widen1 && widen2;
269 newClass.data = data;
270 env.add( newClass );
271 } // if
272 delete type1;
273 delete type2;
274 return result;
275 }
276
277 bool unify( Type *type1, Type *type2, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, OpenVarSet &openVars, const SymTab::Indexer &indexer ) {
278 OpenVarSet closedVars;
279 findOpenVars( type1, openVars, closedVars, needAssertions, haveAssertions, false );
280 findOpenVars( type2, openVars, closedVars, needAssertions, haveAssertions, true );
281 Type *commonType = 0;
282 if ( unifyInexact( type1, type2, env, needAssertions, haveAssertions, openVars, WidenMode( true, true ), indexer, commonType ) ) {
283 if ( commonType ) {
284 delete commonType;
285 } // if
286 return true;
287 } else {
288 return false;
289 } // if
290 }
291
292 bool unify( Type *type1, Type *type2, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, OpenVarSet &openVars, const SymTab::Indexer &indexer, Type *&commonType ) {
293 OpenVarSet closedVars;
294 findOpenVars( type1, openVars, closedVars, needAssertions, haveAssertions, false );
295 findOpenVars( type2, openVars, closedVars, needAssertions, haveAssertions, true );
296 return unifyInexact( type1, type2, env, needAssertions, haveAssertions, openVars, WidenMode( true, true ), indexer, commonType );
297 }
298
299 bool unifyExact( Type *type1, Type *type2, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, WidenMode widenMode, const SymTab::Indexer &indexer ) {
300#ifdef DEBUG
301 TypeEnvironment debugEnv( env );
302#endif
303 if ( type1->get_qualifiers() != type2->get_qualifiers() ) {
304 return false;
305 }
306
307 bool result;
308 TypeInstType *var1 = dynamic_cast< TypeInstType* >( type1 );
309 TypeInstType *var2 = dynamic_cast< TypeInstType* >( type2 );
310 OpenVarSet::const_iterator entry1, entry2;
311 if ( var1 ) {
312 entry1 = openVars.find( var1->get_name() );
313 } // if
314 if ( var2 ) {
315 entry2 = openVars.find( var2->get_name() );
316 } // if
317 bool isopen1 = var1 && ( entry1 != openVars.end() );
318 bool isopen2 = var2 && ( entry2 != openVars.end() );
319
320 if ( isopen1 && isopen2 && entry1->second == entry2->second ) {
321 result = bindVarToVar( var1, var2, entry1->second, env, needAssertions, haveAssertions, openVars, widenMode, indexer );
322 } else if ( isopen1 ) {
323 result = bindVar( var1, type2, entry1->second, env, needAssertions, haveAssertions, openVars, widenMode, indexer );
324 } else if ( isopen2 ) {
325 result = bindVar( var2, type1, entry2->second, env, needAssertions, haveAssertions, openVars, widenMode, indexer );
326 } else {
327 Unify comparator( type2, env, needAssertions, haveAssertions, openVars, widenMode, indexer );
328 type1->accept( comparator );
329 result = comparator.get_result();
330 } // if
331#ifdef DEBUG
332 std::cerr << "============ unifyExact" << std::endl;
333 std::cerr << "type1 is ";
334 type1->print( std::cerr );
335 std::cerr << std::endl << "type2 is ";
336 type2->print( std::cerr );
337 std::cerr << std::endl << "openVars are ";
338 printOpenVarSet( openVars, std::cerr, 8 );
339 std::cerr << std::endl << "input env is " << std::endl;
340 debugEnv.print( std::cerr, 8 );
341 std::cerr << std::endl << "result env is " << std::endl;
342 env.print( std::cerr, 8 );
343 std::cerr << "result is " << result << std::endl;
344#endif
345 return result;
346 }
347
348 bool unifyExact( Type *type1, Type *type2, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, OpenVarSet &openVars, const SymTab::Indexer &indexer ) {
349 return unifyExact( type1, type2, env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
350 }
351
352 bool unifyInexact( Type *type1, Type *type2, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, WidenMode widenMode, const SymTab::Indexer &indexer, Type *&common ) {
353 Type::Qualifiers tq1 = type1->get_qualifiers(), tq2 = type2->get_qualifiers();
354 type1->get_qualifiers() = Type::Qualifiers();
355 type2->get_qualifiers() = Type::Qualifiers();
356 bool result;
357#ifdef DEBUG
358 std::cerr << "unifyInexact type 1 is ";
359 type1->print( std::cerr );
360 std::cerr << " type 2 is ";
361 type2->print( std::cerr );
362 std::cerr << std::endl;
363#endif
364 if ( ! unifyExact( type1, type2, env, needAssertions, haveAssertions, openVars, widenMode, indexer ) ) {
365#ifdef DEBUG
366 std::cerr << "unifyInexact: no exact unification found" << std::endl;
367#endif
368 if ( ( common = commonType( type1, type2, widenMode.widenFirst, widenMode.widenSecond, indexer, env, openVars ) ) ) {
369 common->get_qualifiers() = tq1 | tq2;
370#ifdef DEBUG
371 std::cerr << "unifyInexact: common type is ";
372 common->print( std::cerr );
373 std::cerr << std::endl;
374#endif
375 result = true;
376 } else {
377#ifdef DEBUG
378 std::cerr << "unifyInexact: no common type found" << std::endl;
379#endif
380 result = false;
381 } // if
382 } else {
383 if ( tq1 != tq2 ) {
384 if ( ( tq1 > tq2 || widenMode.widenFirst ) && ( tq2 > tq1 || widenMode.widenSecond ) ) {
385 common = type1->clone();
386 common->get_qualifiers() = tq1 | tq2;
387 result = true;
388 } else {
389 result = false;
390 } // if
391 } else {
392 common = type1->clone();
393 common->get_qualifiers() = tq1 | tq2;
394 result = true;
395 } // if
396 } // if
397 type1->get_qualifiers() = tq1;
398 type2->get_qualifiers() = tq2;
399 return result;
400 }
401
402 Unify::Unify( Type *type2, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, WidenMode widenMode, const SymTab::Indexer &indexer )
403 : result( false ), type2( type2 ), env( env ), needAssertions( needAssertions ), haveAssertions( haveAssertions ), openVars( openVars ), widenMode( widenMode ), indexer( indexer ) {
404 }
405
406 void Unify::visit( __attribute__((unused)) VoidType *voidType) {
407 result = dynamic_cast< VoidType* >( type2 );
408 }
409
410 void Unify::visit(BasicType *basicType) {
411 if ( BasicType *otherBasic = dynamic_cast< BasicType* >( type2 ) ) {
412 result = basicType->get_kind() == otherBasic->get_kind();
413 } // if
414 }
415
416 void markAssertionSet( AssertionSet &assertions, DeclarationWithType *assert ) {
417/// std::cerr << "assertion set is" << std::endl;
418/// printAssertionSet( assertions, std::cerr, 8 );
419/// std::cerr << "looking for ";
420/// assert->print( std::cerr );
421/// std::cerr << std::endl;
422 AssertionSet::iterator i = assertions.find( assert );
423 if ( i != assertions.end() ) {
424/// std::cerr << "found it!" << std::endl;
425 i->second.isUsed = true;
426 } // if
427 }
428
429 void markAssertions( AssertionSet &assertion1, AssertionSet &assertion2, Type *type ) {
430 for ( std::list< TypeDecl* >::const_iterator tyvar = type->get_forall().begin(); tyvar != type->get_forall().end(); ++tyvar ) {
431 for ( std::list< DeclarationWithType* >::const_iterator assert = (*tyvar)->get_assertions().begin(); assert != (*tyvar)->get_assertions().end(); ++assert ) {
432 markAssertionSet( assertion1, *assert );
433 markAssertionSet( assertion2, *assert );
434 } // for
435 } // for
436 }
437
438 void Unify::visit(PointerType *pointerType) {
439 if ( PointerType *otherPointer = dynamic_cast< PointerType* >( type2 ) ) {
440 result = unifyExact( pointerType->get_base(), otherPointer->get_base(), env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
441 markAssertions( haveAssertions, needAssertions, pointerType );
442 markAssertions( haveAssertions, needAssertions, otherPointer );
443 } // if
444 }
445
446 void Unify::visit(ReferenceType *refType) {
447 if ( ReferenceType *otherRef = dynamic_cast< ReferenceType* >( type2 ) ) {
448 result = unifyExact( refType->get_base(), otherRef->get_base(), env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
449 markAssertions( haveAssertions, needAssertions, refType );
450 markAssertions( haveAssertions, needAssertions, otherRef );
451 } // if
452 }
453
454 void Unify::visit(ArrayType *arrayType) {
455 ArrayType *otherArray = dynamic_cast< ArrayType* >( type2 );
456 // to unify, array types must both be VLA or both not VLA
457 // and must both have a dimension expression or not have a dimension
458 if ( otherArray && arrayType->get_isVarLen() == otherArray->get_isVarLen() ) {
459
460 if ( ! arrayType->get_isVarLen() && ! otherArray->get_isVarLen() &&
461 arrayType->get_dimension() != 0 && otherArray->get_dimension() != 0 ) {
462 ConstantExpr * ce1 = dynamic_cast< ConstantExpr * >( arrayType->get_dimension() );
463 ConstantExpr * ce2 = dynamic_cast< ConstantExpr * >( otherArray->get_dimension() );
464 // see C11 Reference Manual 6.7.6.2.6
465 // two array types with size specifiers that are integer constant expressions are
466 // compatible if both size specifiers have the same constant value
467 if ( ce1 && ce2 ) {
468 Constant * c1 = ce1->get_constant();
469 Constant * c2 = ce2->get_constant();
470
471 if ( c1->get_value() != c2->get_value() ) {
472 // does not unify if the dimension is different
473 return;
474 }
475 }
476 }
477
478 result = unifyExact( arrayType->get_base(), otherArray->get_base(), env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
479 } // if
480 }
481
482 template< typename Iterator, typename Func >
483 std::unique_ptr<Type> combineTypes( Iterator begin, Iterator end, Func & toType ) {
484 std::list< Type * > types;
485 for ( ; begin != end; ++begin ) {
486 // it's guaranteed that a ttype variable will be bound to a flat tuple, so ensure that this results in a flat tuple
487 flatten( toType( *begin ), back_inserter( types ) );
488 }
489 return std::unique_ptr<Type>( new TupleType( Type::Qualifiers(), types ) );
490 }
491
492 template< typename Iterator1, typename Iterator2 >
493 bool unifyDeclList( Iterator1 list1Begin, Iterator1 list1End, Iterator2 list2Begin, Iterator2 list2End, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, const SymTab::Indexer &indexer ) {
494 auto get_type = [](DeclarationWithType * dwt){ return dwt->get_type(); };
495 for ( ; list1Begin != list1End && list2Begin != list2End; ++list1Begin, ++list2Begin ) {
496 Type * t1 = (*list1Begin)->get_type();
497 Type * t2 = (*list2Begin)->get_type();
498 bool isTtype1 = Tuples::isTtype( t1 );
499 bool isTtype2 = Tuples::isTtype( t2 );
500 // xxx - assumes ttype must be last parameter
501 // xxx - there may be a nice way to refactor this, but be careful because the argument positioning might matter in some cases.
502 if ( isTtype1 && ! isTtype2 ) {
503 // combine all of the things in list2, then unify
504 return unifyExact( t1, combineTypes( list2Begin, list2End, get_type ).get(), env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
505 } else if ( isTtype2 && ! isTtype1 ) {
506 // combine all of the things in list1, then unify
507 return unifyExact( combineTypes( list1Begin, list1End, get_type ).get(), t2, env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
508 } else if ( ! unifyExact( t1, t2, env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer ) ) {
509 return false;
510 } // if
511 } // for
512 // may get to the end of one argument list before the end of the other. This is only okay when the other is a ttype
513 if ( list1Begin != list1End ) {
514 // try unifying empty tuple type with ttype
515 Type * t1 = (*list1Begin)->get_type();
516 if ( Tuples::isTtype( t1 ) ) {
517 return unifyExact( t1, combineTypes( list2Begin, list2End, get_type ).get(), env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
518 } else return false;
519 } else if ( list2Begin != list2End ) {
520 // try unifying empty tuple type with ttype
521 Type * t2 = (*list2Begin)->get_type();
522 if ( Tuples::isTtype( t2 ) ) {
523 return unifyExact( combineTypes( list1Begin, list1End, get_type ).get(), t2, env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
524 } else return false;
525 } else {
526 return true;
527 } // if
528 }
529
530 /// Finds ttypes and replaces them with their expansion, if known.
531 /// This needs to be done so that satisfying ttype assertions is easier.
532 /// If this isn't done then argument lists can have wildly different
533 /// size and structure, when they should be compatible.
534 struct TtypeExpander : public WithShortCircuiting {
535 TypeEnvironment & tenv;
536 TtypeExpander( TypeEnvironment & tenv ) : tenv( tenv ) {}
537 void premutate( TypeInstType * ) { visit_children = false; }
538 Type * postmutate( TypeInstType * typeInst ) {
539 EqvClass eqvClass;
540 if ( tenv.lookup( typeInst->get_name(), eqvClass ) ) {
541 if ( eqvClass.data.kind == TypeDecl::Ttype ) {
542 // expand ttype parameter into its actual type
543 if ( eqvClass.type ) {
544 delete typeInst;
545 return eqvClass.type->clone();
546 }
547 }
548 }
549 return typeInst;
550 }
551 };
552
553 /// flattens a list of declarations, so that each tuple type has a single declaration.
554 /// makes use of TtypeExpander to ensure ttypes are flat as well.
555 void flattenList( std::list< DeclarationWithType * > src, std::list< DeclarationWithType * > & dst, TypeEnvironment & env ) {
556 dst.clear();
557 for ( DeclarationWithType * dcl : src ) {
558 PassVisitor<TtypeExpander> expander( env );
559 dcl->acceptMutator( expander );
560 std::list< Type * > types;
561 flatten( dcl->get_type(), back_inserter( types ) );
562 for ( Type * t : types ) {
563 dst.push_back( new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::C, nullptr, t, nullptr ) );
564 }
565 delete dcl;
566 }
567 }
568
569 void Unify::visit(FunctionType *functionType) {
570 FunctionType *otherFunction = dynamic_cast< FunctionType* >( type2 );
571 if ( otherFunction && functionType->get_isVarArgs() == otherFunction->get_isVarArgs() ) {
572 // flatten the parameter lists for both functions so that tuple structure
573 // doesn't affect unification. Must be a clone so that the types don't change.
574 std::unique_ptr<FunctionType> flatFunc( functionType->clone() );
575 std::unique_ptr<FunctionType> flatOther( otherFunction->clone() );
576 flattenList( flatFunc->get_parameters(), flatFunc->get_parameters(), env );
577 flattenList( flatOther->get_parameters(), flatOther->get_parameters(), env );
578
579 // sizes don't have to match if ttypes are involved; need to be more precise wrt where the ttype is to prevent errors
580 if ( (flatFunc->get_parameters().size() == flatOther->get_parameters().size() && flatFunc->get_returnVals().size() == flatOther->get_returnVals().size()) || flatFunc->isTtype() || flatOther->isTtype() ) {
581 if ( unifyDeclList( flatFunc->get_parameters().begin(), flatFunc->get_parameters().end(), flatOther->get_parameters().begin(), flatOther->get_parameters().end(), env, needAssertions, haveAssertions, openVars, indexer ) ) {
582 if ( unifyDeclList( flatFunc->get_returnVals().begin(), flatFunc->get_returnVals().end(), flatOther->get_returnVals().begin(), flatOther->get_returnVals().end(), env, needAssertions, haveAssertions, openVars, indexer ) ) {
583
584 // the original types must be used in mark assertions, since pointer comparisons are used
585 markAssertions( haveAssertions, needAssertions, functionType );
586 markAssertions( haveAssertions, needAssertions, otherFunction );
587
588 result = true;
589 } // if
590 } // if
591 } // if
592 } // if
593 }
594
595 template< typename RefType >
596 void Unify::handleRefType( RefType *inst, Type *other ) {
597 // check that other type is compatible and named the same
598 RefType *otherStruct = dynamic_cast< RefType* >( other );
599 result = otherStruct && inst->get_name() == otherStruct->get_name();
600 }
601
602 template< typename RefType >
603 void Unify::handleGenericRefType( RefType *inst, Type *other ) {
604 // Check that other type is compatible and named the same
605 handleRefType( inst, other );
606 if ( ! result ) return;
607 // Check that parameters of types unify, if any
608 std::list< Expression* > params = inst->get_parameters();
609 std::list< Expression* > otherParams = ((RefType*)other)->get_parameters();
610
611 std::list< Expression* >::const_iterator it = params.begin(), jt = otherParams.begin();
612 for ( ; it != params.end() && jt != otherParams.end(); ++it, ++jt ) {
613 TypeExpr *param = dynamic_cast< TypeExpr* >(*it);
614 assertf(param, "Aggregate parameters should be type expressions");
615 TypeExpr *otherParam = dynamic_cast< TypeExpr* >(*jt);
616 assertf(otherParam, "Aggregate parameters should be type expressions");
617
618 Type* paramTy = param->get_type();
619 Type* otherParamTy = otherParam->get_type();
620
621 bool tupleParam = Tuples::isTtype( paramTy );
622 bool otherTupleParam = Tuples::isTtype( otherParamTy );
623
624 if ( tupleParam && otherTupleParam ) {
625 ++it; ++jt; // skip ttype parameters for break
626 } else if ( tupleParam ) {
627 // bundle other parameters into tuple to match
628 std::list< Type * > binderTypes;
629
630 do {
631 binderTypes.push_back( otherParam->get_type()->clone() );
632 ++jt;
633
634 if ( jt == otherParams.end() ) break;
635
636 otherParam = dynamic_cast< TypeExpr* >(*jt);
637 assertf(otherParam, "Aggregate parameters should be type expressions");
638 } while (true);
639
640 otherParamTy = new TupleType{ paramTy->get_qualifiers(), binderTypes };
641 ++it; // skip ttype parameter for break
642 } else if ( otherTupleParam ) {
643 // bundle parameters into tuple to match other
644 std::list< Type * > binderTypes;
645
646 do {
647 binderTypes.push_back( param->get_type()->clone() );
648 ++it;
649
650 if ( it == params.end() ) break;
651
652 param = dynamic_cast< TypeExpr* >(*it);
653 assertf(param, "Aggregate parameters should be type expressions");
654 } while (true);
655
656 paramTy = new TupleType{ otherParamTy->get_qualifiers(), binderTypes };
657 ++jt; // skip ttype parameter for break
658 }
659
660 if ( ! unifyExact( paramTy, otherParamTy, env, needAssertions, haveAssertions, openVars, WidenMode(false, false), indexer ) ) {
661 result = false;
662 return;
663 }
664
665 // ttype parameter should be last
666 if ( tupleParam || otherTupleParam ) break;
667 }
668 result = ( it == params.end() && jt == otherParams.end() );
669 }
670
671 void Unify::visit(StructInstType *structInst) {
672 handleGenericRefType( structInst, type2 );
673 }
674
675 void Unify::visit(UnionInstType *unionInst) {
676 handleGenericRefType( unionInst, type2 );
677 }
678
679 void Unify::visit(EnumInstType *enumInst) {
680 handleRefType( enumInst, type2 );
681 }
682
683 void Unify::visit(TraitInstType *contextInst) {
684 handleRefType( contextInst, type2 );
685 }
686
687 void Unify::visit(TypeInstType *typeInst) {
688 assert( openVars.find( typeInst->get_name() ) == openVars.end() );
689 TypeInstType *otherInst = dynamic_cast< TypeInstType* >( type2 );
690 if ( otherInst && typeInst->get_name() == otherInst->get_name() ) {
691 result = true;
692/// } else {
693/// NamedTypeDecl *nt = indexer.lookupType( typeInst->get_name() );
694/// if ( nt ) {
695/// TypeDecl *type = dynamic_cast< TypeDecl* >( nt );
696/// assert( type );
697/// if ( type->get_base() ) {
698/// result = unifyExact( type->get_base(), typeInst, env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
699/// }
700/// }
701 } // if
702 }
703
704 template< typename Iterator1, typename Iterator2 >
705 bool unifyList( Iterator1 list1Begin, Iterator1 list1End, Iterator2 list2Begin, Iterator2 list2End, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, const SymTab::Indexer &indexer ) {
706 auto get_type = [](Type * t) { return t; };
707 for ( ; list1Begin != list1End && list2Begin != list2End; ++list1Begin, ++list2Begin ) {
708 Type * t1 = *list1Begin;
709 Type * t2 = *list2Begin;
710 bool isTtype1 = Tuples::isTtype( t1 );
711 bool isTtype2 = Tuples::isTtype( t2 );
712 // xxx - assumes ttype must be last parameter
713 // xxx - there may be a nice way to refactor this, but be careful because the argument positioning might matter in some cases.
714 if ( isTtype1 && ! isTtype2 ) {
715 // combine all of the things in list2, then unify
716 return unifyExact( t1, combineTypes( list2Begin, list2End, get_type ).get(), env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
717 } else if ( isTtype2 && ! isTtype1 ) {
718 // combine all of the things in list1, then unify
719 return unifyExact( combineTypes( list1Begin, list1End, get_type ).get(), t2, env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
720 } else if ( ! unifyExact( t1, t2, env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer ) ) {
721 return false;
722 } // if
723
724 } // for
725 if ( list1Begin != list1End ) {
726 // try unifying empty tuple type with ttype
727 Type * t1 = *list1Begin;
728 if ( Tuples::isTtype( t1 ) ) {
729 return unifyExact( t1, combineTypes( list2Begin, list2End, get_type ).get(), env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
730 } else return false;
731 } else if ( list2Begin != list2End ) {
732 // try unifying empty tuple type with ttype
733 Type * t2 = *list2Begin;
734 if ( Tuples::isTtype( t2 ) ) {
735 return unifyExact( combineTypes( list1Begin, list1End, get_type ).get(), t2, env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
736 } else return false;
737 } else {
738 return true;
739 } // if
740 }
741
742 void Unify::visit(TupleType *tupleType) {
743 if ( TupleType *otherTuple = dynamic_cast< TupleType* >( type2 ) ) {
744 std::unique_ptr<TupleType> flat1( tupleType->clone() );
745 std::unique_ptr<TupleType> flat2( otherTuple->clone() );
746 std::list<Type *> types1, types2;
747
748 PassVisitor<TtypeExpander> expander( env );
749 flat1->acceptMutator( expander );
750 flat2->acceptMutator( expander );
751
752 flatten( flat1.get(), back_inserter( types1 ) );
753 flatten( flat2.get(), back_inserter( types2 ) );
754
755 result = unifyList( types1.begin(), types1.end(), types2.begin(), types2.end(), env, needAssertions, haveAssertions, openVars, indexer );
756 } // if
757 }
758
759 void Unify::visit( __attribute__((unused)) VarArgsType *varArgsType ) {
760 result = dynamic_cast< VarArgsType* >( type2 );
761 }
762
763 void Unify::visit( __attribute__((unused)) ZeroType *zeroType ) {
764 result = dynamic_cast< ZeroType* >( type2 );
765 }
766
767 void Unify::visit( __attribute__((unused)) OneType *oneType ) {
768 result = dynamic_cast< OneType* >( type2 );
769 }
770
771 // xxx - compute once and store in the FunctionType?
772 Type * extractResultType( FunctionType * function ) {
773 if ( function->get_returnVals().size() == 0 ) {
774 return new VoidType( Type::Qualifiers() );
775 } else if ( function->get_returnVals().size() == 1 ) {
776 return function->get_returnVals().front()->get_type()->clone();
777 } else {
778 std::list< Type * > types;
779 for ( DeclarationWithType * decl : function->get_returnVals() ) {
780 types.push_back( decl->get_type()->clone() );
781 } // for
782 return new TupleType( Type::Qualifiers(), types );
783 }
784 }
785} // namespace ResolvExpr
786
787// Local Variables: //
788// tab-width: 4 //
789// mode: c++ //
790// compile-command: "make install" //
791// End: //
Note: See TracBrowser for help on using the repository browser.