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