source: src/ResolvExpr/Unify.cc@ 493a992

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 493a992 was 7a63486, checked in by Aaron Moss <a3moss@…>, 6 years ago

Allow merging between complete/incomplete type variables

  • Property mode set to 100644
File size: 28.3 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 : Aaron B. Moss
12// Last Modified On : Mon Jun 18 11:58:00 2018
13// Update Count : 43
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, move
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 struct Unify : public WithShortCircuiting {
47 Unify( Type *type2, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, WidenMode widenMode, const SymTab::Indexer &indexer );
48
49 bool get_result() const { return result; }
50
51 void previsit( BaseSyntaxNode * ) { visit_children = false; }
52
53 void postvisit( VoidType * voidType );
54 void postvisit( BasicType * basicType );
55 void postvisit( PointerType * pointerType );
56 void postvisit( ArrayType * arrayType );
57 void postvisit( ReferenceType * refType );
58 void postvisit( FunctionType * functionType );
59 void postvisit( StructInstType * aggregateUseType );
60 void postvisit( UnionInstType * aggregateUseType );
61 void postvisit( EnumInstType * aggregateUseType );
62 void postvisit( TraitInstType * aggregateUseType );
63 void postvisit( TypeInstType * aggregateUseType );
64 void postvisit( TupleType * tupleType );
65 void postvisit( VarArgsType * varArgsType );
66 void postvisit( ZeroType * zeroType );
67 void postvisit( OneType * oneType );
68
69 private:
70 template< typename RefType > void handleRefType( RefType *inst, Type *other );
71 template< typename RefType > void handleGenericRefType( RefType *inst, Type *other );
72
73 bool result;
74 Type *type2; // inherited
75 TypeEnvironment &env;
76 AssertionSet &needAssertions;
77 AssertionSet &haveAssertions;
78 const OpenVarSet &openVars;
79 WidenMode widenMode;
80 const SymTab::Indexer &indexer;
81 };
82
83 /// Attempts an inexact unification of type1 and type2.
84 /// Returns false if no such unification; if the types can be unified, sets common (unless they unify exactly and have identical type qualifiers)
85 bool unifyInexact( Type *type1, Type *type2, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, WidenMode widenMode, const SymTab::Indexer &indexer, Type *&common );
86 bool unifyExact( Type *type1, Type *type2, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, WidenMode widenMode, const SymTab::Indexer &indexer );
87
88 bool typesCompatible( Type *first, Type *second, const SymTab::Indexer &indexer, const TypeEnvironment &env ) {
89 TypeEnvironment newEnv;
90 OpenVarSet openVars, closedVars; // added closedVars
91 AssertionSet needAssertions, haveAssertions;
92 Type *newFirst = first->clone(), *newSecond = second->clone();
93 env.apply( newFirst );
94 env.apply( newSecond );
95
96 // do we need to do this? Seems like we do, types should be able to be compatible if they
97 // have free variables that can unify
98 findOpenVars( newFirst, openVars, closedVars, needAssertions, haveAssertions, false );
99 findOpenVars( newSecond, openVars, closedVars, needAssertions, haveAssertions, true );
100
101 bool result = unifyExact( newFirst, newSecond, newEnv, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
102 delete newFirst;
103 delete newSecond;
104 return result;
105 }
106
107 bool typesCompatibleIgnoreQualifiers( Type *first, Type *second, const SymTab::Indexer &indexer, const TypeEnvironment &env ) {
108 TypeEnvironment newEnv;
109 OpenVarSet openVars;
110 AssertionSet needAssertions, haveAssertions;
111 Type *newFirst = first->clone(), *newSecond = second->clone();
112 env.apply( newFirst );
113 env.apply( newSecond );
114 newFirst->get_qualifiers() = Type::Qualifiers();
115 newSecond->get_qualifiers() = Type::Qualifiers();
116/// std::cerr << "first is ";
117/// first->print( std::cerr );
118/// std::cerr << std::endl << "second is ";
119/// second->print( std::cerr );
120/// std::cerr << std::endl << "newFirst is ";
121/// newFirst->print( std::cerr );
122/// std::cerr << std::endl << "newSecond is ";
123/// newSecond->print( std::cerr );
124/// std::cerr << std::endl;
125 bool result = unifyExact( newFirst, newSecond, newEnv, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
126 delete newFirst;
127 delete newSecond;
128 return result;
129 }
130
131 bool unify( Type *type1, Type *type2, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, OpenVarSet &openVars, const SymTab::Indexer &indexer ) {
132 OpenVarSet closedVars;
133 findOpenVars( type1, openVars, closedVars, needAssertions, haveAssertions, false );
134 findOpenVars( type2, openVars, closedVars, needAssertions, haveAssertions, true );
135 Type *commonType = 0;
136 if ( unifyInexact( type1, type2, env, needAssertions, haveAssertions, openVars, WidenMode( true, true ), indexer, commonType ) ) {
137 if ( commonType ) {
138 delete commonType;
139 } // if
140 return true;
141 } else {
142 return false;
143 } // if
144 }
145
146 bool unify( Type *type1, Type *type2, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, OpenVarSet &openVars, const SymTab::Indexer &indexer, Type *&commonType ) {
147 OpenVarSet closedVars;
148 findOpenVars( type1, openVars, closedVars, needAssertions, haveAssertions, false );
149 findOpenVars( type2, openVars, closedVars, needAssertions, haveAssertions, true );
150 return unifyInexact( type1, type2, env, needAssertions, haveAssertions, openVars, WidenMode( true, true ), indexer, commonType );
151 }
152
153 bool unifyExact( Type *type1, Type *type2, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, WidenMode widenMode, const SymTab::Indexer &indexer ) {
154#ifdef DEBUG
155 TypeEnvironment debugEnv( env );
156#endif
157 if ( type1->get_qualifiers() != type2->get_qualifiers() ) {
158 return false;
159 }
160
161 bool result;
162 TypeInstType *var1 = dynamic_cast< TypeInstType* >( type1 );
163 TypeInstType *var2 = dynamic_cast< TypeInstType* >( type2 );
164 OpenVarSet::const_iterator entry1, entry2;
165 if ( var1 ) {
166 entry1 = openVars.find( var1->get_name() );
167 } // if
168 if ( var2 ) {
169 entry2 = openVars.find( var2->get_name() );
170 } // if
171 bool isopen1 = var1 && ( entry1 != openVars.end() );
172 bool isopen2 = var2 && ( entry2 != openVars.end() );
173
174 if ( isopen1 && isopen2 ) {
175 if ( entry1->second.kind != entry2->second.kind ) {
176 result = false;
177 } else {
178 result = env.bindVarToVar(
179 var1, var2, TypeDecl::Data{ entry1->second, entry2->second }, needAssertions,
180 haveAssertions, openVars, widenMode, indexer );
181 }
182 } else if ( isopen1 ) {
183 result = env.bindVar( var1, type2, entry1->second, needAssertions, haveAssertions, openVars, widenMode, indexer );
184 } else if ( isopen2 ) { // TODO: swap widenMode values in call, since type positions are flipped?
185 result = env.bindVar( var2, type1, entry2->second, needAssertions, haveAssertions, openVars, widenMode, indexer );
186 } else {
187 PassVisitor<Unify> comparator( type2, env, needAssertions, haveAssertions, openVars, widenMode, indexer );
188 type1->accept( comparator );
189 result = comparator.pass.get_result();
190 } // if
191#ifdef DEBUG
192 std::cerr << "============ unifyExact" << std::endl;
193 std::cerr << "type1 is ";
194 type1->print( std::cerr );
195 std::cerr << std::endl << "type2 is ";
196 type2->print( std::cerr );
197 std::cerr << std::endl << "openVars are ";
198 printOpenVarSet( openVars, std::cerr, 8 );
199 std::cerr << std::endl << "input env is " << std::endl;
200 debugEnv.print( std::cerr, 8 );
201 std::cerr << std::endl << "result env is " << std::endl;
202 env.print( std::cerr, 8 );
203 std::cerr << "result is " << result << std::endl;
204#endif
205 return result;
206 }
207
208 bool unifyExact( Type *type1, Type *type2, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, OpenVarSet &openVars, const SymTab::Indexer &indexer ) {
209 return unifyExact( type1, type2, env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
210 }
211
212 bool unifyInexact( Type *type1, Type *type2, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, WidenMode widenMode, const SymTab::Indexer &indexer, Type *&common ) {
213 Type::Qualifiers tq1 = type1->get_qualifiers(), tq2 = type2->get_qualifiers();
214 type1->get_qualifiers() = Type::Qualifiers();
215 type2->get_qualifiers() = Type::Qualifiers();
216 bool result;
217#ifdef DEBUG
218 std::cerr << "unifyInexact type 1 is ";
219 type1->print( std::cerr );
220 std::cerr << " type 2 is ";
221 type2->print( std::cerr );
222 std::cerr << std::endl;
223#endif
224 if ( ! unifyExact( type1, type2, env, needAssertions, haveAssertions, openVars, widenMode, indexer ) ) {
225#ifdef DEBUG
226 std::cerr << "unifyInexact: no exact unification found" << std::endl;
227#endif
228 if ( ( common = commonType( type1, type2, widenMode.widenFirst, widenMode.widenSecond, indexer, env, openVars ) ) ) {
229 common->get_qualifiers() = tq1 | tq2;
230#ifdef DEBUG
231 std::cerr << "unifyInexact: common type is ";
232 common->print( std::cerr );
233 std::cerr << std::endl;
234#endif
235 result = true;
236 } else {
237#ifdef DEBUG
238 std::cerr << "unifyInexact: no common type found" << std::endl;
239#endif
240 result = false;
241 } // if
242 } else {
243 if ( tq1 != tq2 ) {
244 if ( ( tq1 > tq2 || widenMode.widenFirst ) && ( tq2 > tq1 || widenMode.widenSecond ) ) {
245 common = type1->clone();
246 common->get_qualifiers() = tq1 | tq2;
247 result = true;
248 } else {
249 result = false;
250 } // if
251 } else {
252 common = type1->clone();
253 common->get_qualifiers() = tq1 | tq2;
254 result = true;
255 } // if
256 } // if
257 type1->get_qualifiers() = tq1;
258 type2->get_qualifiers() = tq2;
259 return result;
260 }
261
262 Unify::Unify( Type *type2, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, WidenMode widenMode, const SymTab::Indexer &indexer )
263 : result( false ), type2( type2 ), env( env ), needAssertions( needAssertions ), haveAssertions( haveAssertions ), openVars( openVars ), widenMode( widenMode ), indexer( indexer ) {
264 }
265
266 void Unify::postvisit( __attribute__((unused)) VoidType *voidType) {
267 result = dynamic_cast< VoidType* >( type2 );
268 }
269
270 void Unify::postvisit(BasicType *basicType) {
271 if ( BasicType *otherBasic = dynamic_cast< BasicType* >( type2 ) ) {
272 result = basicType->get_kind() == otherBasic->get_kind();
273 } // if
274 }
275
276 void markAssertionSet( AssertionSet &assertions, DeclarationWithType *assert ) {
277/// std::cerr << "assertion set is" << std::endl;
278/// printAssertionSet( assertions, std::cerr, 8 );
279/// std::cerr << "looking for ";
280/// assert->print( std::cerr );
281/// std::cerr << std::endl;
282 AssertionSet::iterator i = assertions.find( assert );
283 if ( i != assertions.end() ) {
284/// std::cerr << "found it!" << std::endl;
285 i->second.isUsed = true;
286 } // if
287 }
288
289 void markAssertions( AssertionSet &assertion1, AssertionSet &assertion2, Type *type ) {
290 for ( std::list< TypeDecl* >::const_iterator tyvar = type->get_forall().begin(); tyvar != type->get_forall().end(); ++tyvar ) {
291 for ( std::list< DeclarationWithType* >::const_iterator assert = (*tyvar)->get_assertions().begin(); assert != (*tyvar)->get_assertions().end(); ++assert ) {
292 markAssertionSet( assertion1, *assert );
293 markAssertionSet( assertion2, *assert );
294 } // for
295 } // for
296 }
297
298 void Unify::postvisit(PointerType *pointerType) {
299 if ( PointerType *otherPointer = dynamic_cast< PointerType* >( type2 ) ) {
300 result = unifyExact( pointerType->get_base(), otherPointer->get_base(), env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
301 markAssertions( haveAssertions, needAssertions, pointerType );
302 markAssertions( haveAssertions, needAssertions, otherPointer );
303 } // if
304 }
305
306 void Unify::postvisit(ReferenceType *refType) {
307 if ( ReferenceType *otherRef = dynamic_cast< ReferenceType* >( type2 ) ) {
308 result = unifyExact( refType->get_base(), otherRef->get_base(), env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
309 markAssertions( haveAssertions, needAssertions, refType );
310 markAssertions( haveAssertions, needAssertions, otherRef );
311 } // if
312 }
313
314 void Unify::postvisit(ArrayType *arrayType) {
315 ArrayType *otherArray = dynamic_cast< ArrayType* >( type2 );
316 // to unify, array types must both be VLA or both not VLA
317 // and must both have a dimension expression or not have a dimension
318 if ( otherArray && arrayType->get_isVarLen() == otherArray->get_isVarLen() ) {
319
320 if ( ! arrayType->get_isVarLen() && ! otherArray->get_isVarLen() &&
321 arrayType->get_dimension() != 0 && otherArray->get_dimension() != 0 ) {
322 ConstantExpr * ce1 = dynamic_cast< ConstantExpr * >( arrayType->get_dimension() );
323 ConstantExpr * ce2 = dynamic_cast< ConstantExpr * >( otherArray->get_dimension() );
324 // see C11 Reference Manual 6.7.6.2.6
325 // two array types with size specifiers that are integer constant expressions are
326 // compatible if both size specifiers have the same constant value
327 if ( ce1 && ce2 ) {
328 Constant * c1 = ce1->get_constant();
329 Constant * c2 = ce2->get_constant();
330
331 if ( c1->get_value() != c2->get_value() ) {
332 // does not unify if the dimension is different
333 return;
334 }
335 }
336 }
337
338 result = unifyExact( arrayType->get_base(), otherArray->get_base(), env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
339 } // if
340 }
341
342 template< typename Iterator, typename Func >
343 std::unique_ptr<Type> combineTypes( Iterator begin, Iterator end, Func & toType ) {
344 std::list< Type * > types;
345 for ( ; begin != end; ++begin ) {
346 // it's guaranteed that a ttype variable will be bound to a flat tuple, so ensure that this results in a flat tuple
347 flatten( toType( *begin ), back_inserter( types ) );
348 }
349 return std::unique_ptr<Type>( new TupleType( Type::Qualifiers(), types ) );
350 }
351
352 template< typename Iterator1, typename Iterator2 >
353 bool unifyDeclList( Iterator1 list1Begin, Iterator1 list1End, Iterator2 list2Begin, Iterator2 list2End, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, const SymTab::Indexer &indexer ) {
354 auto get_type = [](DeclarationWithType * dwt){ return dwt->get_type(); };
355 for ( ; list1Begin != list1End && list2Begin != list2End; ++list1Begin, ++list2Begin ) {
356 Type * t1 = (*list1Begin)->get_type();
357 Type * t2 = (*list2Begin)->get_type();
358 bool isTtype1 = Tuples::isTtype( t1 );
359 bool isTtype2 = Tuples::isTtype( t2 );
360 // xxx - assumes ttype must be last parameter
361 // xxx - there may be a nice way to refactor this, but be careful because the argument positioning might matter in some cases.
362 if ( isTtype1 && ! isTtype2 ) {
363 // combine all of the things in list2, then unify
364 return unifyExact( t1, combineTypes( list2Begin, list2End, get_type ).get(), env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
365 } else if ( isTtype2 && ! isTtype1 ) {
366 // combine all of the things in list1, then unify
367 return unifyExact( combineTypes( list1Begin, list1End, get_type ).get(), t2, env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
368 } else if ( ! unifyExact( t1, t2, env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer ) ) {
369 return false;
370 } // if
371 } // for
372 // 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
373 if ( list1Begin != list1End ) {
374 // try unifying empty tuple type with ttype
375 Type * t1 = (*list1Begin)->get_type();
376 if ( Tuples::isTtype( t1 ) ) {
377 return unifyExact( t1, combineTypes( list2Begin, list2End, get_type ).get(), env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
378 } else return false;
379 } else if ( list2Begin != list2End ) {
380 // try unifying empty tuple type with ttype
381 Type * t2 = (*list2Begin)->get_type();
382 if ( Tuples::isTtype( t2 ) ) {
383 return unifyExact( combineTypes( list1Begin, list1End, get_type ).get(), t2, env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
384 } else return false;
385 } else {
386 return true;
387 } // if
388 }
389
390 /// Finds ttypes and replaces them with their expansion, if known.
391 /// This needs to be done so that satisfying ttype assertions is easier.
392 /// If this isn't done then argument lists can have wildly different
393 /// size and structure, when they should be compatible.
394 struct TtypeExpander : public WithShortCircuiting {
395 TypeEnvironment & tenv;
396 TtypeExpander( TypeEnvironment & tenv ) : tenv( tenv ) {}
397 void premutate( TypeInstType * ) { visit_children = false; }
398 Type * postmutate( TypeInstType * typeInst ) {
399 if ( const EqvClass *eqvClass = tenv.lookup( typeInst->get_name() ) ) {
400 // expand ttype parameter into its actual type
401 if ( eqvClass->data.kind == TypeDecl::Ttype && eqvClass->type ) {
402 delete typeInst;
403 return eqvClass->type->clone();
404 }
405 }
406 return typeInst;
407 }
408 };
409
410 /// flattens a list of declarations, so that each tuple type has a single declaration.
411 /// makes use of TtypeExpander to ensure ttypes are flat as well.
412 void flattenList( std::list< DeclarationWithType * > src, std::list< DeclarationWithType * > & dst, TypeEnvironment & env ) {
413 dst.clear();
414 for ( DeclarationWithType * dcl : src ) {
415 PassVisitor<TtypeExpander> expander( env );
416 dcl->acceptMutator( expander );
417 std::list< Type * > types;
418 flatten( dcl->get_type(), back_inserter( types ) );
419 for ( Type * t : types ) {
420 // outermost const, volatile, _Atomic qualifiers in parameters should not play a role in the unification of function types, since they do not determine whether a function is callable.
421 // Note: MUST consider at least mutex qualifier, since functions can be overloaded on outermost mutex and a mutex function has different requirements than a non-mutex function.
422 t->get_qualifiers() -= Type::Qualifiers(Type::Const | Type::Volatile | Type::Atomic);
423
424 dst.push_back( new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::C, nullptr, t, nullptr ) );
425 }
426 delete dcl;
427 }
428 }
429
430 void Unify::postvisit(FunctionType *functionType) {
431 FunctionType *otherFunction = dynamic_cast< FunctionType* >( type2 );
432 if ( otherFunction && functionType->get_isVarArgs() == otherFunction->get_isVarArgs() ) {
433 // flatten the parameter lists for both functions so that tuple structure
434 // doesn't affect unification. Must be a clone so that the types don't change.
435 std::unique_ptr<FunctionType> flatFunc( functionType->clone() );
436 std::unique_ptr<FunctionType> flatOther( otherFunction->clone() );
437 flattenList( flatFunc->get_parameters(), flatFunc->get_parameters(), env );
438 flattenList( flatOther->get_parameters(), flatOther->get_parameters(), env );
439
440 // sizes don't have to match if ttypes are involved; need to be more precise wrt where the ttype is to prevent errors
441 if ( (flatFunc->parameters.size() == flatOther->parameters.size() && flatFunc->returnVals.size() == flatOther->returnVals.size()) || flatFunc->isTtype() || flatOther->isTtype() ) {
442 if ( unifyDeclList( flatFunc->parameters.begin(), flatFunc->parameters.end(), flatOther->parameters.begin(), flatOther->parameters.end(), env, needAssertions, haveAssertions, openVars, indexer ) ) {
443 if ( unifyDeclList( flatFunc->returnVals.begin(), flatFunc->returnVals.end(), flatOther->returnVals.begin(), flatOther->returnVals.end(), env, needAssertions, haveAssertions, openVars, indexer ) ) {
444
445 // the original types must be used in mark assertions, since pointer comparisons are used
446 markAssertions( haveAssertions, needAssertions, functionType );
447 markAssertions( haveAssertions, needAssertions, otherFunction );
448
449 result = true;
450 } // if
451 } // if
452 } // if
453 } // if
454 }
455
456 template< typename RefType >
457 void Unify::handleRefType( RefType *inst, Type *other ) {
458 // check that other type is compatible and named the same
459 RefType *otherStruct = dynamic_cast< RefType* >( other );
460 result = otherStruct && inst->name == otherStruct->name;
461 }
462
463 template< typename RefType >
464 void Unify::handleGenericRefType( RefType *inst, Type *other ) {
465 // Check that other type is compatible and named the same
466 handleRefType( inst, other );
467 if ( ! result ) return;
468 // Check that parameters of types unify, if any
469 std::list< Expression* > params = inst->parameters;
470 std::list< Expression* > otherParams = ((RefType*)other)->parameters;
471
472 std::list< Expression* >::const_iterator it = params.begin(), jt = otherParams.begin();
473 for ( ; it != params.end() && jt != otherParams.end(); ++it, ++jt ) {
474 TypeExpr *param = dynamic_cast< TypeExpr* >(*it);
475 assertf(param, "Aggregate parameters should be type expressions");
476 TypeExpr *otherParam = dynamic_cast< TypeExpr* >(*jt);
477 assertf(otherParam, "Aggregate parameters should be type expressions");
478
479 Type* paramTy = param->get_type();
480 Type* otherParamTy = otherParam->get_type();
481
482 bool tupleParam = Tuples::isTtype( paramTy );
483 bool otherTupleParam = Tuples::isTtype( otherParamTy );
484
485 if ( tupleParam && otherTupleParam ) {
486 ++it; ++jt; // skip ttype parameters for break
487 } else if ( tupleParam ) {
488 // bundle other parameters into tuple to match
489 std::list< Type * > binderTypes;
490
491 do {
492 binderTypes.push_back( otherParam->get_type()->clone() );
493 ++jt;
494
495 if ( jt == otherParams.end() ) break;
496
497 otherParam = dynamic_cast< TypeExpr* >(*jt);
498 assertf(otherParam, "Aggregate parameters should be type expressions");
499 } while (true);
500
501 otherParamTy = new TupleType{ paramTy->get_qualifiers(), binderTypes };
502 ++it; // skip ttype parameter for break
503 } else if ( otherTupleParam ) {
504 // bundle parameters into tuple to match other
505 std::list< Type * > binderTypes;
506
507 do {
508 binderTypes.push_back( param->get_type()->clone() );
509 ++it;
510
511 if ( it == params.end() ) break;
512
513 param = dynamic_cast< TypeExpr* >(*it);
514 assertf(param, "Aggregate parameters should be type expressions");
515 } while (true);
516
517 paramTy = new TupleType{ otherParamTy->get_qualifiers(), binderTypes };
518 ++jt; // skip ttype parameter for break
519 }
520
521 if ( ! unifyExact( paramTy, otherParamTy, env, needAssertions, haveAssertions, openVars, WidenMode(false, false), indexer ) ) {
522 result = false;
523 return;
524 }
525
526 // ttype parameter should be last
527 if ( tupleParam || otherTupleParam ) break;
528 }
529 result = ( it == params.end() && jt == otherParams.end() );
530 }
531
532 void Unify::postvisit(StructInstType *structInst) {
533 handleGenericRefType( structInst, type2 );
534 }
535
536 void Unify::postvisit(UnionInstType *unionInst) {
537 handleGenericRefType( unionInst, type2 );
538 }
539
540 void Unify::postvisit(EnumInstType *enumInst) {
541 handleRefType( enumInst, type2 );
542 }
543
544 void Unify::postvisit(TraitInstType *contextInst) {
545 handleRefType( contextInst, type2 );
546 }
547
548 void Unify::postvisit(TypeInstType *typeInst) {
549 assert( openVars.find( typeInst->get_name() ) == openVars.end() );
550 TypeInstType *otherInst = dynamic_cast< TypeInstType* >( type2 );
551 if ( otherInst && typeInst->get_name() == otherInst->get_name() ) {
552 result = true;
553/// } else {
554/// NamedTypeDecl *nt = indexer.lookupType( typeInst->get_name() );
555/// if ( nt ) {
556/// TypeDecl *type = dynamic_cast< TypeDecl* >( nt );
557/// assert( type );
558/// if ( type->get_base() ) {
559/// result = unifyExact( type->get_base(), typeInst, env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
560/// }
561/// }
562 } // if
563 }
564
565 template< typename Iterator1, typename Iterator2 >
566 bool unifyList( Iterator1 list1Begin, Iterator1 list1End, Iterator2 list2Begin, Iterator2 list2End, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, const SymTab::Indexer &indexer ) {
567 auto get_type = [](Type * t) { return t; };
568 for ( ; list1Begin != list1End && list2Begin != list2End; ++list1Begin, ++list2Begin ) {
569 Type * t1 = *list1Begin;
570 Type * t2 = *list2Begin;
571 bool isTtype1 = Tuples::isTtype( t1 );
572 bool isTtype2 = Tuples::isTtype( t2 );
573 // xxx - assumes ttype must be last parameter
574 // xxx - there may be a nice way to refactor this, but be careful because the argument positioning might matter in some cases.
575 if ( isTtype1 && ! isTtype2 ) {
576 // combine all of the things in list2, then unify
577 return unifyExact( t1, combineTypes( list2Begin, list2End, get_type ).get(), env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
578 } else if ( isTtype2 && ! isTtype1 ) {
579 // combine all of the things in list1, then unify
580 return unifyExact( combineTypes( list1Begin, list1End, get_type ).get(), t2, env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
581 } else if ( ! unifyExact( t1, t2, env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer ) ) {
582 return false;
583 } // if
584
585 } // for
586 if ( list1Begin != list1End ) {
587 // try unifying empty tuple type with ttype
588 Type * t1 = *list1Begin;
589 if ( Tuples::isTtype( t1 ) ) {
590 return unifyExact( t1, combineTypes( list2Begin, list2End, get_type ).get(), env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
591 } else return false;
592 } else if ( list2Begin != list2End ) {
593 // try unifying empty tuple type with ttype
594 Type * t2 = *list2Begin;
595 if ( Tuples::isTtype( t2 ) ) {
596 return unifyExact( combineTypes( list1Begin, list1End, get_type ).get(), t2, env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
597 } else return false;
598 } else {
599 return true;
600 } // if
601 }
602
603 void Unify::postvisit(TupleType *tupleType) {
604 if ( TupleType *otherTuple = dynamic_cast< TupleType* >( type2 ) ) {
605 std::unique_ptr<TupleType> flat1( tupleType->clone() );
606 std::unique_ptr<TupleType> flat2( otherTuple->clone() );
607 std::list<Type *> types1, types2;
608
609 PassVisitor<TtypeExpander> expander( env );
610 flat1->acceptMutator( expander );
611 flat2->acceptMutator( expander );
612
613 flatten( flat1.get(), back_inserter( types1 ) );
614 flatten( flat2.get(), back_inserter( types2 ) );
615
616 result = unifyList( types1.begin(), types1.end(), types2.begin(), types2.end(), env, needAssertions, haveAssertions, openVars, indexer );
617 } // if
618 }
619
620 void Unify::postvisit( __attribute__((unused)) VarArgsType *varArgsType ) {
621 result = dynamic_cast< VarArgsType* >( type2 );
622 }
623
624 void Unify::postvisit( __attribute__((unused)) ZeroType *zeroType ) {
625 result = dynamic_cast< ZeroType* >( type2 );
626 }
627
628 void Unify::postvisit( __attribute__((unused)) OneType *oneType ) {
629 result = dynamic_cast< OneType* >( type2 );
630 }
631
632 // xxx - compute once and store in the FunctionType?
633 Type * extractResultType( FunctionType * function ) {
634 if ( function->get_returnVals().size() == 0 ) {
635 return new VoidType( Type::Qualifiers() );
636 } else if ( function->get_returnVals().size() == 1 ) {
637 return function->get_returnVals().front()->get_type()->clone();
638 } else {
639 std::list< Type * > types;
640 for ( DeclarationWithType * decl : function->get_returnVals() ) {
641 types.push_back( decl->get_type()->clone() );
642 } // for
643 return new TupleType( Type::Qualifiers(), types );
644 }
645 }
646} // namespace ResolvExpr
647
648// Local Variables: //
649// tab-width: 4 //
650// mode: c++ //
651// compile-command: "make install" //
652// End: //
Note: See TracBrowser for help on using the repository browser.