source: src/ResolvExpr/Unify.cc@ 74e5a3aa

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 74e5a3aa was a2a77af, checked in by Aaron Moss <a3moss@…>, 9 years ago

Fix void* to dtype* bindings

  • Property mode set to 100644
File size: 24.0 KB
Line 
1//
2// Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6//
7// Unify.cc --
8//
9// Author : Richard C. Bilson
10// Created On : Sun May 17 12:27:10 2015
11// Last Modified By : Peter A. Buhr
12// Last Modified On : Wed Mar 2 17:37:05 2016
13// Update Count : 37
14//
15
16#include <set>
17#include <memory>
18
19#include "Unify.h"
20#include "TypeEnvironment.h"
21#include "typeops.h"
22#include "FindOpenVars.h"
23#include "SynTree/Visitor.h"
24#include "SynTree/Type.h"
25#include "SynTree/Declaration.h"
26#include "SymTab/Indexer.h"
27#include "Common/utility.h"
28
29
30// #define DEBUG
31
32namespace ResolvExpr {
33
34 class Unify : public Visitor {
35 public:
36 Unify( Type *type2, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, WidenMode widenMode, const SymTab::Indexer &indexer );
37
38 bool get_result() const { return result; }
39 private:
40 virtual void visit(VoidType *voidType);
41 virtual void visit(BasicType *basicType);
42 virtual void visit(PointerType *pointerType);
43 virtual void visit(ArrayType *arrayType);
44 virtual void visit(FunctionType *functionType);
45 virtual void visit(StructInstType *aggregateUseType);
46 virtual void visit(UnionInstType *aggregateUseType);
47 virtual void visit(EnumInstType *aggregateUseType);
48 virtual void visit(TraitInstType *aggregateUseType);
49 virtual void visit(TypeInstType *aggregateUseType);
50 virtual void visit(TupleType *tupleType);
51 virtual void visit(VarArgsType *varArgsType);
52 virtual void visit(ZeroType *zeroType);
53 virtual void visit(OneType *oneType);
54
55 template< typename RefType > void handleRefType( RefType *inst, Type *other );
56 template< typename RefType > void handleGenericRefType( RefType *inst, Type *other );
57
58 bool result;
59 Type *type2; // inherited
60 TypeEnvironment &env;
61 AssertionSet &needAssertions;
62 AssertionSet &haveAssertions;
63 const OpenVarSet &openVars;
64 WidenMode widenMode;
65 const SymTab::Indexer &indexer;
66 };
67
68 /// Attempts an inexact unification of type1 and type2.
69 /// Returns false if no such unification; if the types can be unified, sets common (unless they unify exactly and have identical type qualifiers)
70 bool unifyInexact( Type *type1, Type *type2, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, WidenMode widenMode, const SymTab::Indexer &indexer, Type *&common );
71 bool unifyExact( Type *type1, Type *type2, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, WidenMode widenMode, const SymTab::Indexer &indexer );
72
73 bool typesCompatible( Type *first, Type *second, const SymTab::Indexer &indexer, const TypeEnvironment &env ) {
74 TypeEnvironment newEnv;
75 OpenVarSet openVars, closedVars; // added closedVars
76 AssertionSet needAssertions, haveAssertions;
77 Type *newFirst = first->clone(), *newSecond = second->clone();
78 env.apply( newFirst );
79 env.apply( newSecond );
80
81 // do we need to do this? Seems like we do, types should be able to be compatible if they
82 // have free variables that can unify
83 findOpenVars( newFirst, openVars, closedVars, needAssertions, haveAssertions, false );
84 findOpenVars( newSecond, openVars, closedVars, needAssertions, haveAssertions, true );
85
86 bool result = unifyExact( newFirst, newSecond, newEnv, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
87 delete newFirst;
88 delete newSecond;
89 return result;
90 }
91
92 bool typesCompatibleIgnoreQualifiers( Type *first, Type *second, const SymTab::Indexer &indexer, const TypeEnvironment &env ) {
93 TypeEnvironment newEnv;
94 OpenVarSet openVars;
95 AssertionSet needAssertions, haveAssertions;
96 Type *newFirst = first->clone(), *newSecond = second->clone();
97 env.apply( newFirst );
98 env.apply( newSecond );
99 newFirst->get_qualifiers() = Type::Qualifiers();
100 newSecond->get_qualifiers() = Type::Qualifiers();
101/// std::cout << "first is ";
102/// first->print( std::cout );
103/// std::cout << std::endl << "second is ";
104/// second->print( std::cout );
105/// std::cout << std::endl << "newFirst is ";
106/// newFirst->print( std::cout );
107/// std::cout << std::endl << "newSecond is ";
108/// newSecond->print( std::cout );
109/// std::cout << std::endl;
110 bool result = unifyExact( newFirst, newSecond, newEnv, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
111 delete newFirst;
112 delete newSecond;
113 return result;
114 }
115
116 bool isFtype( Type *type, const SymTab::Indexer &indexer ) {
117 if ( dynamic_cast< FunctionType* >( type ) ) {
118 return true;
119 } else if ( TypeInstType *typeInst = dynamic_cast< TypeInstType* >( type ) ) {
120 return typeInst->get_isFtype();
121 } // if
122 return false;
123 }
124
125 bool tyVarCompatible( TypeDecl::Kind kind, Type *type, const SymTab::Indexer &indexer ) {
126 switch ( kind ) {
127 case TypeDecl::Any:
128 case TypeDecl::Dtype:
129 return ! isFtype( type, indexer );
130
131 case TypeDecl::Ftype:
132 return isFtype( type, indexer );
133 } // switch
134 assert( false );
135 return false;
136 }
137
138 bool bindVar( TypeInstType *typeInst, Type *other, TypeDecl::Kind kind, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, WidenMode widenMode, const SymTab::Indexer &indexer ) {
139 OpenVarSet::const_iterator tyvar = openVars.find( typeInst->get_name() );
140 assert( tyvar != openVars.end() );
141 if ( ! tyVarCompatible( tyvar->second, other, indexer ) ) {
142 return false;
143 } // if
144 if ( occurs( other, typeInst->get_name(), env ) ) {
145 return false;
146 } // if
147 EqvClass curClass;
148 if ( env.lookup( typeInst->get_name(), curClass ) ) {
149 if ( curClass.type ) {
150 Type *common = 0;
151 // attempt to unify equivalence class type (which has qualifiers stripped, so they must be restored) with the type to bind to
152 std::auto_ptr< Type > newType( curClass.type->clone() );
153 newType->get_qualifiers() = typeInst->get_qualifiers();
154 if ( unifyInexact( newType.get(), other, env, needAssertions, haveAssertions, openVars, widenMode & WidenMode( curClass.allowWidening, true ), indexer, common ) ) {
155 if ( common ) {
156 common->get_qualifiers() = Type::Qualifiers();
157 delete curClass.type;
158 curClass.type = common;
159 env.add( curClass );
160 } // if
161 return true;
162 } else {
163 return false;
164 } // if
165 } else {
166 curClass.type = other->clone();
167 curClass.type->get_qualifiers() = Type::Qualifiers();
168 curClass.allowWidening = widenMode.widenFirst && widenMode.widenSecond;
169 env.add( curClass );
170 } // if
171 } else {
172 EqvClass newClass;
173 newClass.vars.insert( typeInst->get_name() );
174 newClass.type = other->clone();
175 newClass.type->get_qualifiers() = Type::Qualifiers();
176 newClass.allowWidening = widenMode.widenFirst && widenMode.widenSecond;
177 newClass.kind = kind;
178 env.add( newClass );
179 } // if
180 return true;
181 }
182
183 bool bindVarToVar( TypeInstType *var1, TypeInstType *var2, TypeDecl::Kind kind, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, WidenMode widenMode, const SymTab::Indexer &indexer ) {
184 bool result = true;
185 EqvClass class1, class2;
186 bool hasClass1 = false, hasClass2 = false;
187 bool widen1 = false, widen2 = false;
188 Type *type1 = 0, *type2 = 0;
189
190 if ( env.lookup( var1->get_name(), class1 ) ) {
191 hasClass1 = true;
192 if ( class1.type ) {
193 if ( occurs( class1.type, var2->get_name(), env ) ) {
194 return false;
195 } // if
196 type1 = class1.type->clone();
197 } // if
198 widen1 = widenMode.widenFirst && class1.allowWidening;
199 } // if
200 if ( env.lookup( var2->get_name(), class2 ) ) {
201 hasClass2 = true;
202 if ( class2.type ) {
203 if ( occurs( class2.type, var1->get_name(), env ) ) {
204 return false;
205 } // if
206 type2 = class2.type->clone();
207 } // if
208 widen2 = widenMode.widenSecond && class2.allowWidening;
209 } // if
210
211 if ( type1 && type2 ) {
212// std::cout << "has type1 && type2" << std::endl;
213 WidenMode newWidenMode ( widen1, widen2 );
214 Type *common = 0;
215 if ( unifyInexact( type1, type2, env, needAssertions, haveAssertions, openVars, newWidenMode, indexer, common ) ) {
216 class1.vars.insert( class2.vars.begin(), class2.vars.end() );
217 class1.allowWidening = widen1 && widen2;
218 if ( common ) {
219 common->get_qualifiers() = Type::Qualifiers();
220 delete class1.type;
221 class1.type = common;
222 } // if
223 env.add( class1 );
224 } else {
225 result = false;
226 } // if
227 } else if ( hasClass1 && hasClass2 ) {
228 if ( type1 ) {
229 class1.vars.insert( class2.vars.begin(), class2.vars.end() );
230 class1.allowWidening = widen1;
231 env.add( class1 );
232 } else {
233 class2.vars.insert( class1.vars.begin(), class1.vars.end() );
234 class2.allowWidening = widen2;
235 env.add( class2 );
236 } // if
237 } else if ( hasClass1 ) {
238 class1.vars.insert( var2->get_name() );
239 class1.allowWidening = widen1;
240 env.add( class1 );
241 } else if ( hasClass2 ) {
242 class2.vars.insert( var1->get_name() );
243 class2.allowWidening = widen2;
244 env.add( class2 );
245 } else {
246 EqvClass newClass;
247 newClass.vars.insert( var1->get_name() );
248 newClass.vars.insert( var2->get_name() );
249 newClass.allowWidening = widen1 && widen2;
250 newClass.kind = kind;
251 env.add( newClass );
252 } // if
253 delete type1;
254 delete type2;
255 return result;
256 }
257
258 bool unify( Type *type1, Type *type2, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, OpenVarSet &openVars, const SymTab::Indexer &indexer ) {
259 OpenVarSet closedVars;
260 findOpenVars( type1, openVars, closedVars, needAssertions, haveAssertions, false );
261 findOpenVars( type2, openVars, closedVars, needAssertions, haveAssertions, true );
262 Type *commonType = 0;
263 if ( unifyInexact( type1, type2, env, needAssertions, haveAssertions, openVars, WidenMode( true, true ), indexer, commonType ) ) {
264 if ( commonType ) {
265 delete commonType;
266 } // if
267 return true;
268 } else {
269 return false;
270 } // if
271 }
272
273 bool unify( Type *type1, Type *type2, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, OpenVarSet &openVars, const SymTab::Indexer &indexer, Type *&commonType ) {
274 OpenVarSet closedVars;
275 findOpenVars( type1, openVars, closedVars, needAssertions, haveAssertions, false );
276 findOpenVars( type2, openVars, closedVars, needAssertions, haveAssertions, true );
277 return unifyInexact( type1, type2, env, needAssertions, haveAssertions, openVars, WidenMode( true, true ), indexer, commonType );
278 }
279
280 bool unifyExact( Type *type1, Type *type2, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, WidenMode widenMode, const SymTab::Indexer &indexer ) {
281#ifdef DEBUG
282 TypeEnvironment debugEnv( env );
283#endif
284 if ( type1->get_qualifiers() != type2->get_qualifiers() ) {
285 return false;
286 }
287
288 bool result;
289 TypeInstType *var1 = dynamic_cast< TypeInstType* >( type1 );
290 TypeInstType *var2 = dynamic_cast< TypeInstType* >( type2 );
291 OpenVarSet::const_iterator entry1, entry2;
292 if ( var1 ) {
293 entry1 = openVars.find( var1->get_name() );
294 } // if
295 if ( var2 ) {
296 entry2 = openVars.find( var2->get_name() );
297 } // if
298 bool isopen1 = var1 && ( entry1 != openVars.end() );
299 bool isopen2 = var2 && ( entry2 != openVars.end() );
300
301 if ( isopen1 && isopen2 && entry1->second == entry2->second ) {
302 result = bindVarToVar( var1, var2, entry1->second, env, needAssertions, haveAssertions, openVars, widenMode, indexer );
303 } else if ( isopen1 ) {
304 result = bindVar( var1, type2, entry1->second, env, needAssertions, haveAssertions, openVars, widenMode, indexer );
305 } else if ( isopen2 ) {
306 result = bindVar( var2, type1, entry2->second, env, needAssertions, haveAssertions, openVars, widenMode, indexer );
307 } else {
308 Unify comparator( type2, env, needAssertions, haveAssertions, openVars, widenMode, indexer );
309 type1->accept( comparator );
310 result = comparator.get_result();
311 } // if
312#ifdef DEBUG
313 std::cout << "============ unifyExact" << std::endl;
314 std::cout << "type1 is ";
315 type1->print( std::cout );
316 std::cout << std::endl << "type2 is ";
317 type2->print( std::cout );
318 std::cout << std::endl << "openVars are ";
319 printOpenVarSet( openVars, std::cout, 8 );
320 std::cout << std::endl << "input env is " << std::endl;
321 debugEnv.print( std::cout, 8 );
322 std::cout << std::endl << "result env is " << std::endl;
323 env.print( std::cout, 8 );
324 std::cout << "result is " << result << std::endl;
325#endif
326 return result;
327 }
328
329 bool unifyExact( Type *type1, Type *type2, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, OpenVarSet &openVars, const SymTab::Indexer &indexer ) {
330 return unifyExact( type1, type2, env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
331 }
332
333 bool unifyInexact( Type *type1, Type *type2, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, WidenMode widenMode, const SymTab::Indexer &indexer, Type *&common ) {
334 Type::Qualifiers tq1 = type1->get_qualifiers(), tq2 = type2->get_qualifiers();
335 type1->get_qualifiers() = Type::Qualifiers();
336 type2->get_qualifiers() = Type::Qualifiers();
337 bool result;
338#ifdef DEBUG
339 std::cout << "unifyInexact type 1 is ";
340 type1->print( std::cout );
341 std::cout << "type 2 is ";
342 type2->print( std::cout );
343 std::cout << std::endl;
344#endif
345 if ( ! unifyExact( type1, type2, env, needAssertions, haveAssertions, openVars, widenMode, indexer ) ) {
346#ifdef DEBUG
347 std::cout << "unifyInexact: no exact unification found" << std::endl;
348#endif
349 if ( ( common = commonType( type1, type2, widenMode.widenFirst, widenMode.widenSecond, indexer, env, openVars ) ) ) {
350 common->get_qualifiers() = tq1 + tq2;
351#ifdef DEBUG
352 std::cout << "unifyInexact: common type is ";
353 common->print( std::cout );
354 std::cout << std::endl;
355#endif
356 result = true;
357 } else {
358#ifdef DEBUG
359 std::cout << "unifyInexact: no common type found" << std::endl;
360#endif
361 result = false;
362 } // if
363 } else {
364 if ( tq1 != tq2 ) {
365 if ( ( tq1 > tq2 || widenMode.widenFirst ) && ( tq2 > tq1 || widenMode.widenSecond ) ) {
366 common = type1->clone();
367 common->get_qualifiers() = tq1 + tq2;
368 result = true;
369 } else {
370 result = false;
371 } // if
372 } else {
373 result = true;
374 } // if
375 } // if
376 type1->get_qualifiers() = tq1;
377 type2->get_qualifiers() = tq2;
378 return result;
379 }
380
381 Unify::Unify( Type *type2, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, WidenMode widenMode, const SymTab::Indexer &indexer )
382 : result( false ), type2( type2 ), env( env ), needAssertions( needAssertions ), haveAssertions( haveAssertions ), openVars( openVars ), widenMode( widenMode ), indexer( indexer ) {
383 }
384
385 void Unify::visit(VoidType *voidType) {
386 result = dynamic_cast< VoidType* >( type2 );
387 }
388
389 void Unify::visit(BasicType *basicType) {
390 if ( BasicType *otherBasic = dynamic_cast< BasicType* >( type2 ) ) {
391 result = basicType->get_kind() == otherBasic->get_kind();
392 } // if
393 }
394
395 void markAssertionSet( AssertionSet &assertions, DeclarationWithType *assert ) {
396/// std::cout << "assertion set is" << std::endl;
397/// printAssertionSet( assertions, std::cout, 8 );
398/// std::cout << "looking for ";
399/// assert->print( std::cout );
400/// std::cout << std::endl;
401 AssertionSet::iterator i = assertions.find( assert );
402 if ( i != assertions.end() ) {
403/// std::cout << "found it!" << std::endl;
404 i->second = true;
405 } // if
406 }
407
408 void markAssertions( AssertionSet &assertion1, AssertionSet &assertion2, Type *type ) {
409 for ( std::list< TypeDecl* >::const_iterator tyvar = type->get_forall().begin(); tyvar != type->get_forall().end(); ++tyvar ) {
410 for ( std::list< DeclarationWithType* >::const_iterator assert = (*tyvar)->get_assertions().begin(); assert != (*tyvar)->get_assertions().end(); ++assert ) {
411 markAssertionSet( assertion1, *assert );
412 markAssertionSet( assertion2, *assert );
413 } // for
414 } // for
415 }
416
417 void Unify::visit(PointerType *pointerType) {
418 if ( PointerType *otherPointer = dynamic_cast< PointerType* >( type2 ) ) {
419 result = unifyExact( pointerType->get_base(), otherPointer->get_base(), env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
420 markAssertions( haveAssertions, needAssertions, pointerType );
421 markAssertions( haveAssertions, needAssertions, otherPointer );
422 } // if
423 }
424
425 void Unify::visit(ArrayType *arrayType) {
426 ArrayType *otherArray = dynamic_cast< ArrayType* >( type2 );
427 // to unify, array types must both be VLA or both not VLA
428 // and must both have a dimension expression or not have a dimension
429 if ( otherArray && arrayType->get_isVarLen() == otherArray->get_isVarLen() ) {
430
431 // not positive this is correct in all cases, but it's needed for typedefs
432 if ( arrayType->get_isVarLen() || otherArray->get_isVarLen() ) {
433 return;
434 }
435
436 if ( ! arrayType->get_isVarLen() && ! otherArray->get_isVarLen() &&
437 arrayType->get_dimension() != 0 && otherArray->get_dimension() != 0 ) {
438 ConstantExpr * ce1 = dynamic_cast< ConstantExpr * >( arrayType->get_dimension() );
439 ConstantExpr * ce2 = dynamic_cast< ConstantExpr * >( otherArray->get_dimension() );
440 // see C11 Reference Manual 6.7.6.2.6
441 // two array types with size specifiers that are integer constant expressions are
442 // compatible if both size specifiers have the same constant value
443 if ( ce1 && ce2 ) {
444 Constant * c1 = ce1->get_constant();
445 Constant * c2 = ce2->get_constant();
446
447 if ( c1->get_value() != c2->get_value() ) {
448 // does not unify if the dimension is different
449 return;
450 }
451 }
452 }
453
454 result = unifyExact( arrayType->get_base(), otherArray->get_base(), env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
455 } // if
456 }
457
458 template< typename Iterator1, typename Iterator2 >
459 bool unifyDeclList( Iterator1 list1Begin, Iterator1 list1End, Iterator2 list2Begin, Iterator2 list2End, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, const SymTab::Indexer &indexer ) {
460 for ( ; list1Begin != list1End && list2Begin != list2End; ++list1Begin, ++list2Begin ) {
461 // Type * commonType;
462 // if ( ! unifyInexact( (*list1Begin)->get_type(), (*list2Begin)->get_type(), env, needAssertions, haveAssertions, openVars, WidenMode( true, true ), indexer, commonType ) ) {
463 if ( ! unifyExact( (*list1Begin)->get_type(), (*list2Begin)->get_type(), env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer ) ) {
464 return false;
465 } // if
466 } // for
467 if ( list1Begin != list1End || list2Begin != list2End ) {
468 return false;
469 } else {
470 return true;
471 } // if
472 }
473
474 void Unify::visit(FunctionType *functionType) {
475 FunctionType *otherFunction = dynamic_cast< FunctionType* >( type2 );
476 if ( otherFunction && functionType->get_isVarArgs() == otherFunction->get_isVarArgs() ) {
477 if ( functionType->get_parameters().size() == otherFunction->get_parameters().size() && functionType->get_returnVals().size() == otherFunction->get_returnVals().size() ) {
478 if ( unifyDeclList( functionType->get_parameters().begin(), functionType->get_parameters().end(), otherFunction->get_parameters().begin(), otherFunction->get_parameters().end(), env, needAssertions, haveAssertions, openVars, indexer ) ) {
479 if ( unifyDeclList( functionType->get_returnVals().begin(), functionType->get_returnVals().end(), otherFunction->get_returnVals().begin(), otherFunction->get_returnVals().end(), env, needAssertions, haveAssertions, openVars, indexer ) ) {
480
481 markAssertions( haveAssertions, needAssertions, functionType );
482 markAssertions( haveAssertions, needAssertions, otherFunction );
483
484 result = true;
485 } // if
486 } // if
487 } // if
488 } // if
489 }
490
491 template< typename RefType >
492 void Unify::handleRefType( RefType *inst, Type *other ) {
493 // check that other type is compatible and named the same
494 RefType *otherStruct = dynamic_cast< RefType* >( other );
495 result = otherStruct && inst->get_name() == otherStruct->get_name();
496 }
497
498 template< typename RefType >
499 void Unify::handleGenericRefType( RefType *inst, Type *other ) {
500 // Check that other type is compatible and named the same
501 handleRefType( inst, other );
502 if ( ! result ) return;
503 // Check that parameters of types unify, if any
504 std::list< Expression* > params = inst->get_parameters();
505 std::list< Expression* > otherParams = ((RefType*)other)->get_parameters();
506
507 std::list< Expression* >::const_iterator it = params.begin(), jt = otherParams.begin();
508 for ( ; it != params.end() && jt != otherParams.end(); ++it, ++jt ) {
509 TypeExpr *param = dynamic_cast< TypeExpr* >(*it);
510 assert(param && "Aggregate parameters should be type expressions");
511 TypeExpr *otherParam = dynamic_cast< TypeExpr* >(*jt);
512 assert(otherParam && "Aggregate parameters should be type expressions");
513
514 if ( ! unifyExact( param->get_type(), otherParam->get_type(), env, needAssertions, haveAssertions, openVars, WidenMode(false, false), indexer ) ) {
515 result = false;
516 return;
517 }
518 }
519 result = ( it == params.end() && jt == otherParams.end() );
520 }
521
522 void Unify::visit(StructInstType *structInst) {
523 handleGenericRefType( structInst, type2 );
524 }
525
526 void Unify::visit(UnionInstType *unionInst) {
527 handleGenericRefType( unionInst, type2 );
528 }
529
530 void Unify::visit(EnumInstType *enumInst) {
531 handleRefType( enumInst, type2 );
532 }
533
534 void Unify::visit(TraitInstType *contextInst) {
535 handleRefType( contextInst, type2 );
536 }
537
538 void Unify::visit(TypeInstType *typeInst) {
539 assert( openVars.find( typeInst->get_name() ) == openVars.end() );
540 TypeInstType *otherInst = dynamic_cast< TypeInstType* >( type2 );
541 if ( otherInst && typeInst->get_name() == otherInst->get_name() ) {
542 result = true;
543/// } else {
544/// NamedTypeDecl *nt = indexer.lookupType( typeInst->get_name() );
545/// if ( nt ) {
546/// TypeDecl *type = dynamic_cast< TypeDecl* >( nt );
547/// assert( type );
548/// if ( type->get_base() ) {
549/// result = unifyExact( type->get_base(), typeInst, env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
550/// }
551/// }
552 } // if
553 }
554
555 template< typename Iterator1, typename Iterator2 >
556 bool unifyList( Iterator1 list1Begin, Iterator1 list1End, Iterator2 list2Begin, Iterator2 list2End, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, WidenMode widenMode, const SymTab::Indexer &indexer ) {
557 for ( ; list1Begin != list1End && list2Begin != list2End; ++list1Begin, ++list2Begin ) {
558 Type *commonType = 0;
559 if ( ! unifyInexact( *list1Begin, *list2Begin, env, needAssertions, haveAssertions, openVars, widenMode, indexer, commonType ) ) {
560 return false;
561 }
562 delete commonType;
563 } // for
564 if ( list1Begin != list1End || list2Begin != list2End ) {
565 return false;
566 } else {
567 return true;
568 } //if
569 }
570
571 void Unify::visit(TupleType *tupleType) {
572 if ( TupleType *otherTuple = dynamic_cast< TupleType* >( type2 ) ) {
573 result = unifyList( tupleType->get_types().begin(), tupleType->get_types().end(), otherTuple->get_types().begin(), otherTuple->get_types().end(), env, needAssertions, haveAssertions, openVars, widenMode, indexer );
574 } // if
575 }
576
577 void Unify::visit(VarArgsType *varArgsType) {
578 result = dynamic_cast< VarArgsType* >( type2 );
579 }
580
581 void Unify::visit(ZeroType *zeroType) {
582 result = dynamic_cast< ZeroType* >( type2 );
583 }
584
585 void Unify::visit(OneType *oneType) {
586 result = dynamic_cast< OneType* >( type2 );
587 }
588
589 // xxx - compute once and store in the FunctionType?
590 Type * extractResultType( FunctionType * function ) {
591 if ( function->get_returnVals().size() == 0 ) {
592 return new VoidType( Type::Qualifiers() );
593 } else if ( function->get_returnVals().size() == 1 ) {
594 return function->get_returnVals().front()->get_type()->clone();
595 } else {
596 TupleType * tupleType = new TupleType( Type::Qualifiers() );
597 for ( DeclarationWithType * decl : function->get_returnVals() ) {
598 tupleType->get_types().push_back( decl->get_type()->clone() );
599 } // for
600 return tupleType;
601 }
602 }
603} // namespace ResolvExpr
604
605// Local Variables: //
606// tab-width: 4 //
607// mode: c++ //
608// compile-command: "make install" //
609// End: //
Note: See TracBrowser for help on using the repository browser.