source: src/ResolvExpr/Unify.cc@ 78a0b88

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new with_gc
Last change on this file since 78a0b88 was 8135d4c, checked in by Rob Schluntz <rschlunt@…>, 8 years ago

Merge branch 'master' into references

  • Property mode set to 100644
File size: 32.9 KB
RevLine 
[a32b204]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//
[41a2620]7// Unify.cc --
[a32b204]8//
9// Author : Richard C. Bilson
10// Created On : Sun May 17 12:27:10 2015
[4040425]11// Last Modified By : Peter A. Buhr
[6f95000]12// Last Modified On : Thu Mar 16 16:22:54 2017
13// Update Count : 42
[a32b204]14//
[51b73452]15
[ea6332d]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, auto_ptr
20#include <set> // for set
21#include <string> // for string, operator==, operator!=, bas...
22#include <utility> // for pair
[51b73452]23
[ea6332d]24#include "FindOpenVars.h" // for findOpenVars
25#include "Parser/LinkageSpec.h" // for C
26#include "SynTree/Constant.h" // for Constant
27#include "SynTree/Declaration.h" // for TypeDecl, TypeDecl::Data, Declarati...
28#include "SynTree/Expression.h" // for TypeExpr, Expression, ConstantExpr
29#include "SynTree/Mutator.h" // for Mutator
30#include "SynTree/Type.h" // for Type, TypeInstType, FunctionType
31#include "SynTree/Visitor.h" // for Visitor
32#include "Tuples/Tuples.h" // for isTtype
33#include "TypeEnvironment.h" // for EqvClass, AssertionSet, OpenVarSet
[51b73452]34#include "Unify.h"
[ea6332d]35#include "typeops.h" // for flatten, occurs, commonType
36
37namespace SymTab {
38class Indexer;
39} // namespace SymTab
[51b73452]40
[1cbca6e]41// #define DEBUG
[51b73452]42
43namespace ResolvExpr {
44
[a32b204]45 class Unify : public Visitor {
46 public:
47 Unify( Type *type2, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, WidenMode widenMode, const SymTab::Indexer &indexer );
[41a2620]48
[a32b204]49 bool get_result() const { return result; }
50 private:
51 virtual void visit(VoidType *voidType);
52 virtual void visit(BasicType *basicType);
53 virtual void visit(PointerType *pointerType);
54 virtual void visit(ArrayType *arrayType);
[ce8c12f]55 virtual void visit(ReferenceType *refType);
[a32b204]56 virtual void visit(FunctionType *functionType);
57 virtual void visit(StructInstType *aggregateUseType);
58 virtual void visit(UnionInstType *aggregateUseType);
59 virtual void visit(EnumInstType *aggregateUseType);
[4040425]60 virtual void visit(TraitInstType *aggregateUseType);
[a32b204]61 virtual void visit(TypeInstType *aggregateUseType);
62 virtual void visit(TupleType *tupleType);
[44b7088]63 virtual void visit(VarArgsType *varArgsType);
[89e6ffc]64 virtual void visit(ZeroType *zeroType);
65 virtual void visit(OneType *oneType);
[a32b204]66
67 template< typename RefType > void handleRefType( RefType *inst, Type *other );
[02ec390]68 template< typename RefType > void handleGenericRefType( RefType *inst, Type *other );
[a32b204]69
70 bool result;
71 Type *type2; // inherited
72 TypeEnvironment &env;
73 AssertionSet &needAssertions;
74 AssertionSet &haveAssertions;
75 const OpenVarSet &openVars;
76 WidenMode widenMode;
77 const SymTab::Indexer &indexer;
78 };
79
[eb50842]80 /// Attempts an inexact unification of type1 and type2.
81 /// Returns false if no such unification; if the types can be unified, sets common (unless they unify exactly and have identical type qualifiers)
[a32b204]82 bool unifyInexact( Type *type1, Type *type2, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, WidenMode widenMode, const SymTab::Indexer &indexer, Type *&common );
83 bool unifyExact( Type *type1, Type *type2, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, WidenMode widenMode, const SymTab::Indexer &indexer );
[41a2620]84
[a32b204]85 bool typesCompatible( Type *first, Type *second, const SymTab::Indexer &indexer, const TypeEnvironment &env ) {
86 TypeEnvironment newEnv;
[1cbca6e]87 OpenVarSet openVars, closedVars; // added closedVars
[a32b204]88 AssertionSet needAssertions, haveAssertions;
89 Type *newFirst = first->clone(), *newSecond = second->clone();
90 env.apply( newFirst );
91 env.apply( newSecond );
[1cbca6e]92
93 // do we need to do this? Seems like we do, types should be able to be compatible if they
94 // have free variables that can unify
95 findOpenVars( newFirst, openVars, closedVars, needAssertions, haveAssertions, false );
96 findOpenVars( newSecond, openVars, closedVars, needAssertions, haveAssertions, true );
97
[a32b204]98 bool result = unifyExact( newFirst, newSecond, newEnv, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
99 delete newFirst;
100 delete newSecond;
101 return result;
102 }
103
104 bool typesCompatibleIgnoreQualifiers( Type *first, Type *second, const SymTab::Indexer &indexer, const TypeEnvironment &env ) {
105 TypeEnvironment newEnv;
106 OpenVarSet openVars;
107 AssertionSet needAssertions, haveAssertions;
108 Type *newFirst = first->clone(), *newSecond = second->clone();
109 env.apply( newFirst );
110 env.apply( newSecond );
111 newFirst->get_qualifiers() = Type::Qualifiers();
112 newSecond->get_qualifiers() = Type::Qualifiers();
[2c57025]113/// std::cerr << "first is ";
114/// first->print( std::cerr );
115/// std::cerr << std::endl << "second is ";
116/// second->print( std::cerr );
117/// std::cerr << std::endl << "newFirst is ";
118/// newFirst->print( std::cerr );
119/// std::cerr << std::endl << "newSecond is ";
120/// newSecond->print( std::cerr );
121/// std::cerr << std::endl;
[a32b204]122 bool result = unifyExact( newFirst, newSecond, newEnv, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
123 delete newFirst;
124 delete newSecond;
125 return result;
126 }
127
[d7dc824]128 bool isFtype( Type *type ) {
[a32b204]129 if ( dynamic_cast< FunctionType* >( type ) ) {
130 return true;
131 } else if ( TypeInstType *typeInst = dynamic_cast< TypeInstType* >( type ) ) {
132 return typeInst->get_isFtype();
133 } // if
134 return false;
135 }
136
[d7dc824]137 bool tyVarCompatible( const TypeDecl::Data & data, Type *type ) {
[2c57025]138 switch ( data.kind ) {
[a32b204]139 case TypeDecl::Any:
140 case TypeDecl::Dtype:
[2c57025]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
[6c3a988f]144 // xxx - should this also check that type is not a tuple type and that it's not a ttype?
[d7dc824]145 return ! isFtype( type ) && (! data.isComplete || type->isComplete() );
[a32b204]146 case TypeDecl::Ftype:
[d7dc824]147 return isFtype( type );
[b2daebd4]148 case TypeDecl::Ttype:
[6c3a988f]149 // ttype unifies with any tuple type
[f3b0a07]150 return dynamic_cast< TupleType * >( type ) || Tuples::isTtype( type );
[a32b204]151 } // switch
152 return false;
153 }
154
[2c57025]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 ) {
[36a5a77]156 // remove references from other, so that type variables can only bind to value types
157 other = other->stripReferences();
[a32b204]158 OpenVarSet::const_iterator tyvar = openVars.find( typeInst->get_name() );
159 assert( tyvar != openVars.end() );
[d7dc824]160 if ( ! tyVarCompatible( tyvar->second, other ) ) {
[a32b204]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;
[eb50842]170 // attempt to unify equivalence class type (which has qualifiers stripped, so they must be restored) with the type to bind to
[a32b204]171 std::auto_ptr< Type > newType( curClass.type->clone() );
[721f17a]172 newType->get_qualifiers() = typeInst->get_qualifiers();
[a32b204]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;
[2c57025]196 newClass.data = data;
[a32b204]197 env.add( newClass );
198 } // if
199 return true;
200 }
201
[2c57025]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 ) {
[a32b204]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;
[41a2620]208
[a32b204]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
[41a2620]229
[a32b204]230 if ( type1 && type2 ) {
[2c57025]231// std::cerr << "has type1 && type2" << std::endl;
[a32b204]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;
[2c57025]269 newClass.data = data;
[a32b204]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 ) {
[51b73452]300#ifdef DEBUG
[a32b204]301 TypeEnvironment debugEnv( env );
[51b73452]302#endif
[eb50842]303 if ( type1->get_qualifiers() != type2->get_qualifiers() ) {
304 return false;
305 }
306
[a32b204]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() );
[eb50842]319
320 if ( isopen1 && isopen2 && entry1->second == entry2->second ) {
[a32b204]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
[51b73452]331#ifdef DEBUG
[2c57025]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;
[51b73452]344#endif
[a32b204]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;
[51b73452]357#ifdef DEBUG
[2c57025]358 std::cerr << "unifyInexact type 1 is ";
359 type1->print( std::cerr );
[0b150ec]360 std::cerr << " type 2 is ";
[2c57025]361 type2->print( std::cerr );
362 std::cerr << std::endl;
[51b73452]363#endif
[a32b204]364 if ( ! unifyExact( type1, type2, env, needAssertions, haveAssertions, openVars, widenMode, indexer ) ) {
[51b73452]365#ifdef DEBUG
[2c57025]366 std::cerr << "unifyInexact: no exact unification found" << std::endl;
[51b73452]367#endif
[a32b204]368 if ( ( common = commonType( type1, type2, widenMode.widenFirst, widenMode.widenSecond, indexer, env, openVars ) ) ) {
[6f95000]369 common->get_qualifiers() = tq1 | tq2;
[51b73452]370#ifdef DEBUG
[2c57025]371 std::cerr << "unifyInexact: common type is ";
372 common->print( std::cerr );
373 std::cerr << std::endl;
[51b73452]374#endif
[a32b204]375 result = true;
376 } else {
[51b73452]377#ifdef DEBUG
[2c57025]378 std::cerr << "unifyInexact: no common type found" << std::endl;
[51b73452]379#endif
[a32b204]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();
[6f95000]386 common->get_qualifiers() = tq1 | tq2;
[a32b204]387 result = true;
388 } else {
389 result = false;
390 } // if
391 } else {
[e6cee92]392 common = type1->clone();
393 common->get_qualifiers() = tq1 | tq2;
[a32b204]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
[af397ef8]406 void Unify::visit( __attribute__((unused)) VoidType *voidType) {
[a32b204]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 ) {
[2c57025]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;
[a32b204]422 AssertionSet::iterator i = assertions.find( assert );
423 if ( i != assertions.end() ) {
[2c57025]424/// std::cerr << "found it!" << std::endl;
[6c3a988f]425 i->second.isUsed = true;
[a32b204]426 } // if
427 }
428
429 void markAssertions( AssertionSet &assertion1, AssertionSet &assertion2, Type *type ) {
[aefcc3b]430 for ( std::list< TypeDecl* >::const_iterator tyvar = type->get_forall().begin(); tyvar != type->get_forall().end(); ++tyvar ) {
[a32b204]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
[ce8c12f]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
[a32b204]454 void Unify::visit(ArrayType *arrayType) {
455 ArrayType *otherArray = dynamic_cast< ArrayType* >( type2 );
[1cbca6e]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
[41a2620]458 if ( otherArray && arrayType->get_isVarLen() == otherArray->get_isVarLen() ) {
[1cbca6e]459
460 // not positive this is correct in all cases, but it's needed for typedefs
461 if ( arrayType->get_isVarLen() || otherArray->get_isVarLen() ) {
462 return;
463 }
464
465 if ( ! arrayType->get_isVarLen() && ! otherArray->get_isVarLen() &&
466 arrayType->get_dimension() != 0 && otherArray->get_dimension() != 0 ) {
467 ConstantExpr * ce1 = dynamic_cast< ConstantExpr * >( arrayType->get_dimension() );
468 ConstantExpr * ce2 = dynamic_cast< ConstantExpr * >( otherArray->get_dimension() );
[41a2620]469 // see C11 Reference Manual 6.7.6.2.6
470 // two array types with size specifiers that are integer constant expressions are
471 // compatible if both size specifiers have the same constant value
472 if ( ce1 && ce2 ) {
473 Constant * c1 = ce1->get_constant();
474 Constant * c2 = ce2->get_constant();
475
476 if ( c1->get_value() != c2->get_value() ) {
477 // does not unify if the dimension is different
478 return;
479 }
[1cbca6e]480 }
481 }
482
[a32b204]483 result = unifyExact( arrayType->get_base(), otherArray->get_base(), env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
484 } // if
485 }
486
[f3b0a07]487 template< typename Iterator, typename Func >
488 std::unique_ptr<Type> combineTypes( Iterator begin, Iterator end, Func & toType ) {
[53e3b4a]489 std::list< Type * > types;
490 for ( ; begin != end; ++begin ) {
[64eae56]491 // it's guaranteed that a ttype variable will be bound to a flat tuple, so ensure that this results in a flat tuple
[f3b0a07]492 flatten( toType( *begin ), back_inserter( types ) );
[53e3b4a]493 }
[6c3a988f]494 return std::unique_ptr<Type>( new TupleType( Type::Qualifiers(), types ) );
[53e3b4a]495 }
496
[a32b204]497 template< typename Iterator1, typename Iterator2 >
498 bool unifyDeclList( Iterator1 list1Begin, Iterator1 list1End, Iterator2 list2Begin, Iterator2 list2End, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, const SymTab::Indexer &indexer ) {
[f3b0a07]499 auto get_type = [](DeclarationWithType * dwt){ return dwt->get_type(); };
[a32b204]500 for ( ; list1Begin != list1End && list2Begin != list2End; ++list1Begin, ++list2Begin ) {
[53e3b4a]501 Type * t1 = (*list1Begin)->get_type();
502 Type * t2 = (*list2Begin)->get_type();
503 bool isTtype1 = Tuples::isTtype( t1 );
504 bool isTtype2 = Tuples::isTtype( t2 );
[6c3a988f]505 // xxx - assumes ttype must be last parameter
506 // xxx - there may be a nice way to refactor this, but be careful because the argument positioning might matter in some cases.
[53e3b4a]507 if ( isTtype1 && ! isTtype2 ) {
508 // combine all of the things in list2, then unify
[f3b0a07]509 return unifyExact( t1, combineTypes( list2Begin, list2End, get_type ).get(), env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
[53e3b4a]510 } else if ( isTtype2 && ! isTtype1 ) {
511 // combine all of the things in list1, then unify
[f3b0a07]512 return unifyExact( combineTypes( list1Begin, list1End, get_type ).get(), t2, env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
[53e3b4a]513 } else if ( ! unifyExact( t1, t2, env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer ) ) {
[a32b204]514 return false;
515 } // if
516 } // for
[6c3a988f]517 // 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
[4c8621ac]518 if ( list1Begin != list1End ) {
519 // try unifying empty tuple type with ttype
520 Type * t1 = (*list1Begin)->get_type();
521 if ( Tuples::isTtype( t1 ) ) {
[f3b0a07]522 return unifyExact( t1, combineTypes( list2Begin, list2End, get_type ).get(), env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
[4c8621ac]523 } else return false;
524 } else if ( list2Begin != list2End ) {
525 // try unifying empty tuple type with ttype
526 Type * t2 = (*list2Begin)->get_type();
527 if ( Tuples::isTtype( t2 ) ) {
[f3b0a07]528 return unifyExact( combineTypes( list1Begin, list1End, get_type ).get(), t2, env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
[4c8621ac]529 } else return false;
[a32b204]530 } else {
531 return true;
532 } // if
533 }
534
[6c3a988f]535 /// Finds ttypes and replaces them with their expansion, if known.
536 /// This needs to be done so that satisfying ttype assertions is easier.
537 /// If this isn't done then argument lists can have wildly different
538 /// size and structure, when they should be compatible.
539 struct TtypeExpander : public Mutator {
540 TypeEnvironment & env;
541 TtypeExpander( TypeEnvironment & env ) : env( env ) {}
542 Type * mutate( TypeInstType * typeInst ) {
543 EqvClass eqvClass;
544 if ( env.lookup( typeInst->get_name(), eqvClass ) ) {
545 if ( eqvClass.data.kind == TypeDecl::Ttype ) {
546 // expand ttype parameter into its actual type
547 if ( eqvClass.type ) {
548 delete typeInst;
549 return eqvClass.type->clone();
550 }
551 }
552 }
553 return typeInst;
554 }
555 };
556
557 /// flattens a list of declarations, so that each tuple type has a single declaration.
558 /// makes use of TtypeExpander to ensure ttypes are flat as well.
559 void flattenList( std::list< DeclarationWithType * > src, std::list< DeclarationWithType * > & dst, TypeEnvironment & env ) {
560 dst.clear();
561 for ( DeclarationWithType * dcl : src ) {
562 TtypeExpander expander( env );
563 dcl->acceptMutator( expander );
564 std::list< Type * > types;
565 flatten( dcl->get_type(), back_inserter( types ) );
566 for ( Type * t : types ) {
[68fe077a]567 dst.push_back( new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::C, nullptr, t, nullptr ) );
[6c3a988f]568 }
569 delete dcl;
570 }
571 }
572
[a32b204]573 void Unify::visit(FunctionType *functionType) {
574 FunctionType *otherFunction = dynamic_cast< FunctionType* >( type2 );
575 if ( otherFunction && functionType->get_isVarArgs() == otherFunction->get_isVarArgs() ) {
[6c3a988f]576 // flatten the parameter lists for both functions so that tuple structure
577 // doesn't affect unification. Must be a clone so that the types don't change.
578 std::unique_ptr<FunctionType> flatFunc( functionType->clone() );
579 std::unique_ptr<FunctionType> flatOther( otherFunction->clone() );
580 flattenList( flatFunc->get_parameters(), flatFunc->get_parameters(), env );
581 flattenList( flatOther->get_parameters(), flatOther->get_parameters(), env );
582
[53e3b4a]583 // sizes don't have to match if ttypes are involved; need to be more precise wrt where the ttype is to prevent errors
[6c3a988f]584 if ( (flatFunc->get_parameters().size() == flatOther->get_parameters().size() && flatFunc->get_returnVals().size() == flatOther->get_returnVals().size()) || flatFunc->isTtype() || flatOther->isTtype() ) {
585 if ( unifyDeclList( flatFunc->get_parameters().begin(), flatFunc->get_parameters().end(), flatOther->get_parameters().begin(), flatOther->get_parameters().end(), env, needAssertions, haveAssertions, openVars, indexer ) ) {
586 if ( unifyDeclList( flatFunc->get_returnVals().begin(), flatFunc->get_returnVals().end(), flatOther->get_returnVals().begin(), flatOther->get_returnVals().end(), env, needAssertions, haveAssertions, openVars, indexer ) ) {
[1cbca6e]587
[6c3a988f]588 // the original types must be used in mark assertions, since pointer comparisons are used
[03da511]589 markAssertions( haveAssertions, needAssertions, functionType );
590 markAssertions( haveAssertions, needAssertions, otherFunction );
[41a2620]591
[03da511]592 result = true;
593 } // if
[a32b204]594 } // if
595 } // if
596 } // if
597 }
598
599 template< typename RefType >
[02ec390]600 void Unify::handleRefType( RefType *inst, Type *other ) {
601 // check that other type is compatible and named the same
[a32b204]602 RefType *otherStruct = dynamic_cast< RefType* >( other );
603 result = otherStruct && inst->get_name() == otherStruct->get_name();
[02ec390]604 }
605
606 template< typename RefType >
607 void Unify::handleGenericRefType( RefType *inst, Type *other ) {
608 // Check that other type is compatible and named the same
609 handleRefType( inst, other );
610 if ( ! result ) return;
[f5234f3]611 // Check that parameters of types unify, if any
[02ec390]612 std::list< Expression* > params = inst->get_parameters();
[f5234f3]613 std::list< Expression* > otherParams = ((RefType*)other)->get_parameters();
614
615 std::list< Expression* >::const_iterator it = params.begin(), jt = otherParams.begin();
616 for ( ; it != params.end() && jt != otherParams.end(); ++it, ++jt ) {
617 TypeExpr *param = dynamic_cast< TypeExpr* >(*it);
[b2daebd4]618 assertf(param, "Aggregate parameters should be type expressions");
[f5234f3]619 TypeExpr *otherParam = dynamic_cast< TypeExpr* >(*jt);
[b2daebd4]620 assertf(otherParam, "Aggregate parameters should be type expressions");
[ce8c12f]621
[b2daebd4]622 Type* paramTy = param->get_type();
623 Type* otherParamTy = otherParam->get_type();
[f5234f3]624
[b2daebd4]625 bool tupleParam = Tuples::isTtype( paramTy );
626 bool otherTupleParam = Tuples::isTtype( otherParamTy );
627
628 if ( tupleParam && otherTupleParam ) {
629 ++it; ++jt; // skip ttype parameters for break
630 } else if ( tupleParam ) {
631 // bundle other parameters into tuple to match
[62423350]632 std::list< Type * > binderTypes;
[b2daebd4]633
634 do {
[62423350]635 binderTypes.push_back( otherParam->get_type()->clone() );
[b2daebd4]636 ++jt;
637
638 if ( jt == otherParams.end() ) break;
639
640 otherParam = dynamic_cast< TypeExpr* >(*jt);
641 assertf(otherParam, "Aggregate parameters should be type expressions");
642 } while (true);
643
[62423350]644 otherParamTy = new TupleType{ paramTy->get_qualifiers(), binderTypes };
[b2daebd4]645 ++it; // skip ttype parameter for break
646 } else if ( otherTupleParam ) {
647 // bundle parameters into tuple to match other
[62423350]648 std::list< Type * > binderTypes;
[b2daebd4]649
650 do {
[62423350]651 binderTypes.push_back( param->get_type()->clone() );
[b2daebd4]652 ++it;
653
654 if ( it == params.end() ) break;
655
656 param = dynamic_cast< TypeExpr* >(*it);
657 assertf(param, "Aggregate parameters should be type expressions");
658 } while (true);
659
[62423350]660 paramTy = new TupleType{ otherParamTy->get_qualifiers(), binderTypes };
[b2daebd4]661 ++jt; // skip ttype parameter for break
662 }
663
664 if ( ! unifyExact( paramTy, otherParamTy, env, needAssertions, haveAssertions, openVars, WidenMode(false, false), indexer ) ) {
[02ec390]665 result = false;
666 return;
667 }
[b2daebd4]668
669 // ttype parameter should be last
670 if ( tupleParam || otherTupleParam ) break;
[02ec390]671 }
[f5234f3]672 result = ( it == params.end() && jt == otherParams.end() );
[02ec390]673 }
[a32b204]674
675 void Unify::visit(StructInstType *structInst) {
[02ec390]676 handleGenericRefType( structInst, type2 );
[a32b204]677 }
678
679 void Unify::visit(UnionInstType *unionInst) {
[02ec390]680 handleGenericRefType( unionInst, type2 );
[a32b204]681 }
682
683 void Unify::visit(EnumInstType *enumInst) {
684 handleRefType( enumInst, type2 );
685 }
686
[4040425]687 void Unify::visit(TraitInstType *contextInst) {
[a32b204]688 handleRefType( contextInst, type2 );
689 }
690
691 void Unify::visit(TypeInstType *typeInst) {
692 assert( openVars.find( typeInst->get_name() ) == openVars.end() );
693 TypeInstType *otherInst = dynamic_cast< TypeInstType* >( type2 );
694 if ( otherInst && typeInst->get_name() == otherInst->get_name() ) {
695 result = true;
[51b73452]696/// } else {
697/// NamedTypeDecl *nt = indexer.lookupType( typeInst->get_name() );
[a32b204]698/// if ( nt ) {
[51b73452]699/// TypeDecl *type = dynamic_cast< TypeDecl* >( nt );
700/// assert( type );
[a32b204]701/// if ( type->get_base() ) {
[51b73452]702/// result = unifyExact( type->get_base(), typeInst, env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
703/// }
704/// }
[a32b204]705 } // if
706 }
707
708 template< typename Iterator1, typename Iterator2 >
[c77fd8b]709 bool unifyList( Iterator1 list1Begin, Iterator1 list1End, Iterator2 list2Begin, Iterator2 list2End, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, const SymTab::Indexer &indexer ) {
[f3b0a07]710 auto get_type = [](Type * t) { return t; };
[a32b204]711 for ( ; list1Begin != list1End && list2Begin != list2End; ++list1Begin, ++list2Begin ) {
[f3b0a07]712 Type * t1 = *list1Begin;
713 Type * t2 = *list2Begin;
714 bool isTtype1 = Tuples::isTtype( t1 );
715 bool isTtype2 = Tuples::isTtype( t2 );
716 // xxx - assumes ttype must be last parameter
717 // xxx - there may be a nice way to refactor this, but be careful because the argument positioning might matter in some cases.
718 if ( isTtype1 && ! isTtype2 ) {
719 // combine all of the things in list2, then unify
720 return unifyExact( t1, combineTypes( list2Begin, list2End, get_type ).get(), env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
721 } else if ( isTtype2 && ! isTtype1 ) {
722 // combine all of the things in list1, then unify
723 return unifyExact( combineTypes( list1Begin, list1End, get_type ).get(), t2, env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
724 } else if ( ! unifyExact( t1, t2, env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer ) ) {
[a32b204]725 return false;
[f3b0a07]726 } // if
727
[a32b204]728 } // for
[f3b0a07]729 if ( list1Begin != list1End ) {
730 // try unifying empty tuple type with ttype
731 Type * t1 = *list1Begin;
732 if ( Tuples::isTtype( t1 ) ) {
733 return unifyExact( t1, combineTypes( list2Begin, list2End, get_type ).get(), env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
734 } else return false;
735 } else if ( list2Begin != list2End ) {
736 // try unifying empty tuple type with ttype
737 Type * t2 = *list2Begin;
738 if ( Tuples::isTtype( t2 ) ) {
739 return unifyExact( combineTypes( list1Begin, list1End, get_type ).get(), t2, env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
740 } else return false;
[a32b204]741 } else {
742 return true;
[f3b0a07]743 } // if
[a32b204]744 }
745
746 void Unify::visit(TupleType *tupleType) {
747 if ( TupleType *otherTuple = dynamic_cast< TupleType* >( type2 ) ) {
[f3b0a07]748 std::unique_ptr<TupleType> flat1( tupleType->clone() );
749 std::unique_ptr<TupleType> flat2( otherTuple->clone() );
750 std::list<Type *> types1, types2;
751
752 TtypeExpander expander( env );
753 flat1->acceptMutator( expander );
754 flat2->acceptMutator( expander );
755
756 flatten( flat1.get(), back_inserter( types1 ) );
757 flatten( flat2.get(), back_inserter( types2 ) );
758
[c77fd8b]759 result = unifyList( types1.begin(), types1.end(), types2.begin(), types2.end(), env, needAssertions, haveAssertions, openVars, indexer );
[a32b204]760 } // if
761 }
[51b73452]762
[af397ef8]763 void Unify::visit( __attribute__((unused)) VarArgsType *varArgsType ) {
[44b7088]764 result = dynamic_cast< VarArgsType* >( type2 );
765 }
766
[af397ef8]767 void Unify::visit( __attribute__((unused)) ZeroType *zeroType ) {
[89e6ffc]768 result = dynamic_cast< ZeroType* >( type2 );
769 }
770
[af397ef8]771 void Unify::visit( __attribute__((unused)) OneType *oneType ) {
[89e6ffc]772 result = dynamic_cast< OneType* >( type2 );
773 }
774
[906e24d]775 // xxx - compute once and store in the FunctionType?
776 Type * extractResultType( FunctionType * function ) {
777 if ( function->get_returnVals().size() == 0 ) {
778 return new VoidType( Type::Qualifiers() );
779 } else if ( function->get_returnVals().size() == 1 ) {
780 return function->get_returnVals().front()->get_type()->clone();
781 } else {
[62423350]782 std::list< Type * > types;
[906e24d]783 for ( DeclarationWithType * decl : function->get_returnVals() ) {
[62423350]784 types.push_back( decl->get_type()->clone() );
[906e24d]785 } // for
[62423350]786 return new TupleType( Type::Qualifiers(), types );
[906e24d]787 }
788 }
[51b73452]789} // namespace ResolvExpr
[a32b204]790
791// Local Variables: //
792// tab-width: 4 //
793// mode: c++ //
794// compile-command: "make install" //
795// End: //
Note: See TracBrowser for help on using the repository browser.