source: src/ResolvExpr/Unify.cc@ 50f0ba9

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 50f0ba9 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
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...
[aeef2bd]19#include <memory> // for unique_ptr
[ea6332d]20#include <set> // for set
21#include <string> // for string, operator==, operator!=, bas...
22#include <utility> // for pair
[51b73452]23
[3096ec1]24#include "Common/PassVisitor.h" // for PassVisitor
[ea6332d]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
[51b73452]35#include "Unify.h"
[ea6332d]36#include "typeops.h" // for flatten, occurs, commonType
37
38namespace SymTab {
39class Indexer;
40} // namespace SymTab
[51b73452]41
[1cbca6e]42// #define DEBUG
[51b73452]43
44namespace ResolvExpr {
45
[a32b204]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 );
[41a2620]49
[a32b204]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);
[ce8c12f]56 virtual void visit(ReferenceType *refType);
[a32b204]57 virtual void visit(FunctionType *functionType);
58 virtual void visit(StructInstType *aggregateUseType);
59 virtual void visit(UnionInstType *aggregateUseType);
60 virtual void visit(EnumInstType *aggregateUseType);
[4040425]61 virtual void visit(TraitInstType *aggregateUseType);
[a32b204]62 virtual void visit(TypeInstType *aggregateUseType);
63 virtual void visit(TupleType *tupleType);
[44b7088]64 virtual void visit(VarArgsType *varArgsType);
[89e6ffc]65 virtual void visit(ZeroType *zeroType);
66 virtual void visit(OneType *oneType);
[a32b204]67
68 template< typename RefType > void handleRefType( RefType *inst, Type *other );
[02ec390]69 template< typename RefType > void handleGenericRefType( RefType *inst, Type *other );
[a32b204]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
[eb50842]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)
[a32b204]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 );
[41a2620]85
[a32b204]86 bool typesCompatible( Type *first, Type *second, const SymTab::Indexer &indexer, const TypeEnvironment &env ) {
87 TypeEnvironment newEnv;
[1cbca6e]88 OpenVarSet openVars, closedVars; // added closedVars
[a32b204]89 AssertionSet needAssertions, haveAssertions;
90 Type *newFirst = first->clone(), *newSecond = second->clone();
91 env.apply( newFirst );
92 env.apply( newSecond );
[1cbca6e]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
[a32b204]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();
[2c57025]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;
[a32b204]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
[d7dc824]129 bool isFtype( Type *type ) {
[a32b204]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
[d7dc824]138 bool tyVarCompatible( const TypeDecl::Data & data, Type *type ) {
[2c57025]139 switch ( data.kind ) {
[a32b204]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
[aeef2bd]171 std::unique_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 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() );
[41a2620]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 }
[1cbca6e]475 }
476 }
477
[a32b204]478 result = unifyExact( arrayType->get_base(), otherArray->get_base(), env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
479 } // if
480 }
481
[f3b0a07]482 template< typename Iterator, typename Func >
483 std::unique_ptr<Type> combineTypes( Iterator begin, Iterator end, Func & toType ) {
[53e3b4a]484 std::list< Type * > types;
485 for ( ; begin != end; ++begin ) {
[64eae56]486 // it's guaranteed that a ttype variable will be bound to a flat tuple, so ensure that this results in a flat tuple
[f3b0a07]487 flatten( toType( *begin ), back_inserter( types ) );
[53e3b4a]488 }
[6c3a988f]489 return std::unique_ptr<Type>( new TupleType( Type::Qualifiers(), types ) );
[53e3b4a]490 }
491
[a32b204]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 ) {
[f3b0a07]494 auto get_type = [](DeclarationWithType * dwt){ return dwt->get_type(); };
[a32b204]495 for ( ; list1Begin != list1End && list2Begin != list2End; ++list1Begin, ++list2Begin ) {
[53e3b4a]496 Type * t1 = (*list1Begin)->get_type();
497 Type * t2 = (*list2Begin)->get_type();
498 bool isTtype1 = Tuples::isTtype( t1 );
499 bool isTtype2 = Tuples::isTtype( t2 );
[6c3a988f]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.
[53e3b4a]502 if ( isTtype1 && ! isTtype2 ) {
503 // combine all of the things in list2, then unify
[f3b0a07]504 return unifyExact( t1, combineTypes( list2Begin, list2End, get_type ).get(), env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
[53e3b4a]505 } else if ( isTtype2 && ! isTtype1 ) {
506 // combine all of the things in list1, then unify
[f3b0a07]507 return unifyExact( combineTypes( list1Begin, list1End, get_type ).get(), t2, env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
[53e3b4a]508 } else if ( ! unifyExact( t1, t2, env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer ) ) {
[a32b204]509 return false;
510 } // if
511 } // for
[6c3a988f]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
[4c8621ac]513 if ( list1Begin != list1End ) {
514 // try unifying empty tuple type with ttype
515 Type * t1 = (*list1Begin)->get_type();
516 if ( Tuples::isTtype( t1 ) ) {
[f3b0a07]517 return unifyExact( t1, combineTypes( list2Begin, list2End, get_type ).get(), env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
[4c8621ac]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 ) ) {
[f3b0a07]523 return unifyExact( combineTypes( list1Begin, list1End, get_type ).get(), t2, env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
[4c8621ac]524 } else return false;
[a32b204]525 } else {
526 return true;
527 } // if
528 }
529
[6c3a988f]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.
[3096ec1]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 ) {
[6c3a988f]539 EqvClass eqvClass;
[3096ec1]540 if ( tenv.lookup( typeInst->get_name(), eqvClass ) ) {
[6c3a988f]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 ) {
[3096ec1]558 PassVisitor<TtypeExpander> expander( env );
[6c3a988f]559 dcl->acceptMutator( expander );
560 std::list< Type * > types;
561 flatten( dcl->get_type(), back_inserter( types ) );
562 for ( Type * t : types ) {
[68fe077a]563 dst.push_back( new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::C, nullptr, t, nullptr ) );
[6c3a988f]564 }
565 delete dcl;
566 }
567 }
568
[a32b204]569 void Unify::visit(FunctionType *functionType) {
570 FunctionType *otherFunction = dynamic_cast< FunctionType* >( type2 );
571 if ( otherFunction && functionType->get_isVarArgs() == otherFunction->get_isVarArgs() ) {
[6c3a988f]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
[53e3b4a]579 // sizes don't have to match if ttypes are involved; need to be more precise wrt where the ttype is to prevent errors
[6c3a988f]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 ) ) {
[1cbca6e]583
[6c3a988f]584 // the original types must be used in mark assertions, since pointer comparisons are used
[03da511]585 markAssertions( haveAssertions, needAssertions, functionType );
586 markAssertions( haveAssertions, needAssertions, otherFunction );
[41a2620]587
[03da511]588 result = true;
589 } // if
[a32b204]590 } // if
591 } // if
592 } // if
593 }
594
595 template< typename RefType >
[02ec390]596 void Unify::handleRefType( RefType *inst, Type *other ) {
597 // check that other type is compatible and named the same
[a32b204]598 RefType *otherStruct = dynamic_cast< RefType* >( other );
599 result = otherStruct && inst->get_name() == otherStruct->get_name();
[02ec390]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;
[f5234f3]607 // Check that parameters of types unify, if any
[02ec390]608 std::list< Expression* > params = inst->get_parameters();
[f5234f3]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);
[b2daebd4]614 assertf(param, "Aggregate parameters should be type expressions");
[f5234f3]615 TypeExpr *otherParam = dynamic_cast< TypeExpr* >(*jt);
[b2daebd4]616 assertf(otherParam, "Aggregate parameters should be type expressions");
[ce8c12f]617
[b2daebd4]618 Type* paramTy = param->get_type();
619 Type* otherParamTy = otherParam->get_type();
[f5234f3]620
[b2daebd4]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
[62423350]628 std::list< Type * > binderTypes;
[b2daebd4]629
630 do {
[62423350]631 binderTypes.push_back( otherParam->get_type()->clone() );
[b2daebd4]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
[62423350]640 otherParamTy = new TupleType{ paramTy->get_qualifiers(), binderTypes };
[b2daebd4]641 ++it; // skip ttype parameter for break
642 } else if ( otherTupleParam ) {
643 // bundle parameters into tuple to match other
[62423350]644 std::list< Type * > binderTypes;
[b2daebd4]645
646 do {
[62423350]647 binderTypes.push_back( param->get_type()->clone() );
[b2daebd4]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
[62423350]656 paramTy = new TupleType{ otherParamTy->get_qualifiers(), binderTypes };
[b2daebd4]657 ++jt; // skip ttype parameter for break
658 }
659
660 if ( ! unifyExact( paramTy, otherParamTy, env, needAssertions, haveAssertions, openVars, WidenMode(false, false), indexer ) ) {
[02ec390]661 result = false;
662 return;
663 }
[b2daebd4]664
665 // ttype parameter should be last
666 if ( tupleParam || otherTupleParam ) break;
[02ec390]667 }
[f5234f3]668 result = ( it == params.end() && jt == otherParams.end() );
[02ec390]669 }
[a32b204]670
671 void Unify::visit(StructInstType *structInst) {
[02ec390]672 handleGenericRefType( structInst, type2 );
[a32b204]673 }
674
675 void Unify::visit(UnionInstType *unionInst) {
[02ec390]676 handleGenericRefType( unionInst, type2 );
[a32b204]677 }
678
679 void Unify::visit(EnumInstType *enumInst) {
680 handleRefType( enumInst, type2 );
681 }
682
[4040425]683 void Unify::visit(TraitInstType *contextInst) {
[a32b204]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;
[51b73452]692/// } else {
693/// NamedTypeDecl *nt = indexer.lookupType( typeInst->get_name() );
[a32b204]694/// if ( nt ) {
[51b73452]695/// TypeDecl *type = dynamic_cast< TypeDecl* >( nt );
696/// assert( type );
[a32b204]697/// if ( type->get_base() ) {
[51b73452]698/// result = unifyExact( type->get_base(), typeInst, env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
699/// }
700/// }
[a32b204]701 } // if
702 }
703
704 template< typename Iterator1, typename Iterator2 >
[c77fd8b]705 bool unifyList( Iterator1 list1Begin, Iterator1 list1End, Iterator2 list2Begin, Iterator2 list2End, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, const SymTab::Indexer &indexer ) {
[f3b0a07]706 auto get_type = [](Type * t) { return t; };
[a32b204]707 for ( ; list1Begin != list1End && list2Begin != list2End; ++list1Begin, ++list2Begin ) {
[f3b0a07]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 ) ) {
[a32b204]721 return false;
[f3b0a07]722 } // if
723
[a32b204]724 } // for
[f3b0a07]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;
[a32b204]737 } else {
738 return true;
[f3b0a07]739 } // if
[a32b204]740 }
741
742 void Unify::visit(TupleType *tupleType) {
743 if ( TupleType *otherTuple = dynamic_cast< TupleType* >( type2 ) ) {
[f3b0a07]744 std::unique_ptr<TupleType> flat1( tupleType->clone() );
745 std::unique_ptr<TupleType> flat2( otherTuple->clone() );
746 std::list<Type *> types1, types2;
747
[3096ec1]748 PassVisitor<TtypeExpander> expander( env );
[f3b0a07]749 flat1->acceptMutator( expander );
750 flat2->acceptMutator( expander );
751
752 flatten( flat1.get(), back_inserter( types1 ) );
753 flatten( flat2.get(), back_inserter( types2 ) );
754
[c77fd8b]755 result = unifyList( types1.begin(), types1.end(), types2.begin(), types2.end(), env, needAssertions, haveAssertions, openVars, indexer );
[a32b204]756 } // if
757 }
[51b73452]758
[af397ef8]759 void Unify::visit( __attribute__((unused)) VarArgsType *varArgsType ) {
[44b7088]760 result = dynamic_cast< VarArgsType* >( type2 );
761 }
762
[af397ef8]763 void Unify::visit( __attribute__((unused)) ZeroType *zeroType ) {
[89e6ffc]764 result = dynamic_cast< ZeroType* >( type2 );
765 }
766
[af397ef8]767 void Unify::visit( __attribute__((unused)) OneType *oneType ) {
[89e6ffc]768 result = dynamic_cast< OneType* >( type2 );
769 }
770
[906e24d]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 {
[62423350]778 std::list< Type * > types;
[906e24d]779 for ( DeclarationWithType * decl : function->get_returnVals() ) {
[62423350]780 types.push_back( decl->get_type()->clone() );
[906e24d]781 } // for
[62423350]782 return new TupleType( Type::Qualifiers(), types );
[906e24d]783 }
784 }
[51b73452]785} // namespace ResolvExpr
[a32b204]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.