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 : Sun May 17 13:08:48 2015
|
---|
13 | // Update Count : 6
|
---|
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 "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(ContextInstType *aggregateUseType);
|
---|
59 | virtual void visit(TypeInstType *aggregateUseType);
|
---|
60 | virtual void visit(TupleType *tupleType);
|
---|
61 |
|
---|
62 | template< typename RefType > void handleRefType( RefType *inst, Type *other );
|
---|
63 |
|
---|
64 | bool result;
|
---|
65 | Type *type2; // inherited
|
---|
66 | TypeEnvironment &env;
|
---|
67 | AssertionSet &needAssertions;
|
---|
68 | AssertionSet &haveAssertions;
|
---|
69 | const OpenVarSet &openVars;
|
---|
70 | WidenMode widenMode;
|
---|
71 | Type *commonType;
|
---|
72 | const SymTab::Indexer &indexer;
|
---|
73 | };
|
---|
74 |
|
---|
75 | bool unifyInexact( Type *type1, Type *type2, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, WidenMode widenMode, const SymTab::Indexer &indexer, Type *&common );
|
---|
76 | bool unifyExact( Type *type1, Type *type2, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, WidenMode widenMode, const SymTab::Indexer &indexer );
|
---|
77 |
|
---|
78 | bool typesCompatible( Type *first, Type *second, const SymTab::Indexer &indexer, const TypeEnvironment &env ) {
|
---|
79 | TypeEnvironment newEnv;
|
---|
80 | OpenVarSet openVars;
|
---|
81 | AssertionSet needAssertions, haveAssertions;
|
---|
82 | Type *newFirst = first->clone(), *newSecond = second->clone();
|
---|
83 | env.apply( newFirst );
|
---|
84 | env.apply( newSecond );
|
---|
85 | bool result = unifyExact( newFirst, newSecond, newEnv, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
|
---|
86 | delete newFirst;
|
---|
87 | delete newSecond;
|
---|
88 | return result;
|
---|
89 | }
|
---|
90 |
|
---|
91 | bool typesCompatibleIgnoreQualifiers( Type *first, Type *second, const SymTab::Indexer &indexer, const TypeEnvironment &env ) {
|
---|
92 | TypeEnvironment newEnv;
|
---|
93 | OpenVarSet openVars;
|
---|
94 | AssertionSet needAssertions, haveAssertions;
|
---|
95 | Type *newFirst = first->clone(), *newSecond = second->clone();
|
---|
96 | env.apply( newFirst );
|
---|
97 | env.apply( newSecond );
|
---|
98 | newFirst->get_qualifiers() = Type::Qualifiers();
|
---|
99 | newSecond->get_qualifiers() = Type::Qualifiers();
|
---|
100 | /// std::cout << "first is ";
|
---|
101 | /// first->print( std::cout );
|
---|
102 | /// std::cout << std::endl << "second is ";
|
---|
103 | /// second->print( std::cout );
|
---|
104 | /// std::cout << std::endl << "newFirst is ";
|
---|
105 | /// newFirst->print( std::cout );
|
---|
106 | /// std::cout << std::endl << "newSecond is ";
|
---|
107 | /// newSecond->print( std::cout );
|
---|
108 | /// std::cout << std::endl;
|
---|
109 | bool result = unifyExact( newFirst, newSecond, newEnv, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
|
---|
110 | delete newFirst;
|
---|
111 | delete newSecond;
|
---|
112 | return result;
|
---|
113 | }
|
---|
114 |
|
---|
115 | bool isFtype( Type *type, const SymTab::Indexer &indexer ) {
|
---|
116 | if ( dynamic_cast< FunctionType* >( type ) ) {
|
---|
117 | return true;
|
---|
118 | } else if ( TypeInstType *typeInst = dynamic_cast< TypeInstType* >( type ) ) {
|
---|
119 | return typeInst->get_isFtype();
|
---|
120 | } // if
|
---|
121 | return false;
|
---|
122 | }
|
---|
123 |
|
---|
124 | bool tyVarCompatible( TypeDecl::Kind kind, Type *type, const SymTab::Indexer &indexer ) {
|
---|
125 | switch ( kind ) {
|
---|
126 | case TypeDecl::Any:
|
---|
127 | case TypeDecl::Dtype:
|
---|
128 | return ! isFtype( type, indexer );
|
---|
129 |
|
---|
130 | case TypeDecl::Ftype:
|
---|
131 | return isFtype( type, indexer );
|
---|
132 | } // switch
|
---|
133 | assert( false );
|
---|
134 | return false;
|
---|
135 | }
|
---|
136 |
|
---|
137 | bool bindVar( TypeInstType *typeInst, Type *other, TypeDecl::Kind kind, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, WidenMode widenMode, const SymTab::Indexer &indexer ) {
|
---|
138 | OpenVarSet::const_iterator tyvar = openVars.find( typeInst->get_name() );
|
---|
139 | assert( tyvar != openVars.end() );
|
---|
140 | if ( ! tyVarCompatible( tyvar->second, other, indexer ) ) {
|
---|
141 | return false;
|
---|
142 | } // if
|
---|
143 | if ( occurs( other, typeInst->get_name(), env ) ) {
|
---|
144 | return false;
|
---|
145 | } // if
|
---|
146 | EqvClass curClass;
|
---|
147 | if ( env.lookup( typeInst->get_name(), curClass ) ) {
|
---|
148 | if ( curClass.type ) {
|
---|
149 | Type *common = 0;
|
---|
150 | std::auto_ptr< Type > newType( curClass.type->clone() );
|
---|
151 | if ( unifyInexact( newType.get(), other, env, needAssertions, haveAssertions, openVars, widenMode & WidenMode( curClass.allowWidening, true ), indexer, common ) ) {
|
---|
152 | if ( common ) {
|
---|
153 | common->get_qualifiers() = Type::Qualifiers();
|
---|
154 | delete curClass.type;
|
---|
155 | curClass.type = common;
|
---|
156 | env.add( curClass );
|
---|
157 | } // if
|
---|
158 | return true;
|
---|
159 | } else {
|
---|
160 | return false;
|
---|
161 | } // if
|
---|
162 | } else {
|
---|
163 | curClass.type = other->clone();
|
---|
164 | curClass.type->get_qualifiers() = Type::Qualifiers();
|
---|
165 | curClass.allowWidening = widenMode.widenFirst && widenMode.widenSecond;
|
---|
166 | env.add( curClass );
|
---|
167 | } // if
|
---|
168 | } else {
|
---|
169 | EqvClass newClass;
|
---|
170 | newClass.vars.insert( typeInst->get_name() );
|
---|
171 | newClass.type = other->clone();
|
---|
172 | newClass.type->get_qualifiers() = Type::Qualifiers();
|
---|
173 | newClass.allowWidening = widenMode.widenFirst && widenMode.widenSecond;
|
---|
174 | newClass.kind = kind;
|
---|
175 | env.add( newClass );
|
---|
176 | } // if
|
---|
177 | return true;
|
---|
178 | }
|
---|
179 |
|
---|
180 | bool bindVarToVar( TypeInstType *var1, TypeInstType *var2, TypeDecl::Kind kind, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, WidenMode widenMode, const SymTab::Indexer &indexer ) {
|
---|
181 | bool result = true;
|
---|
182 | EqvClass class1, class2;
|
---|
183 | bool hasClass1 = false, hasClass2 = false;
|
---|
184 | bool widen1 = false, widen2 = false;
|
---|
185 | Type *type1 = 0, *type2 = 0;
|
---|
186 |
|
---|
187 | if ( env.lookup( var1->get_name(), class1 ) ) {
|
---|
188 | hasClass1 = true;
|
---|
189 | if ( class1.type ) {
|
---|
190 | if ( occurs( class1.type, var2->get_name(), env ) ) {
|
---|
191 | return false;
|
---|
192 | } // if
|
---|
193 | type1 = class1.type->clone();
|
---|
194 | } // if
|
---|
195 | widen1 = widenMode.widenFirst && class1.allowWidening;
|
---|
196 | } // if
|
---|
197 | if ( env.lookup( var2->get_name(), class2 ) ) {
|
---|
198 | hasClass2 = true;
|
---|
199 | if ( class2.type ) {
|
---|
200 | if ( occurs( class2.type, var1->get_name(), env ) ) {
|
---|
201 | return false;
|
---|
202 | } // if
|
---|
203 | type2 = class2.type->clone();
|
---|
204 | } // if
|
---|
205 | widen2 = widenMode.widenSecond && class2.allowWidening;
|
---|
206 | } // if
|
---|
207 |
|
---|
208 | if ( type1 && type2 ) {
|
---|
209 | // std::cout << "has type1 && type2" << std::endl;
|
---|
210 | WidenMode newWidenMode ( widen1, widen2 );
|
---|
211 | Type *common = 0;
|
---|
212 | if ( unifyInexact( type1, type2, env, needAssertions, haveAssertions, openVars, newWidenMode, indexer, common ) ) {
|
---|
213 | class1.vars.insert( class2.vars.begin(), class2.vars.end() );
|
---|
214 | class1.allowWidening = widen1 && widen2;
|
---|
215 | if ( common ) {
|
---|
216 | common->get_qualifiers() = Type::Qualifiers();
|
---|
217 | delete class1.type;
|
---|
218 | class1.type = common;
|
---|
219 | } // if
|
---|
220 | env.add( class1 );
|
---|
221 | } else {
|
---|
222 | result = false;
|
---|
223 | } // if
|
---|
224 | } else if ( hasClass1 && hasClass2 ) {
|
---|
225 | if ( type1 ) {
|
---|
226 | class1.vars.insert( class2.vars.begin(), class2.vars.end() );
|
---|
227 | class1.allowWidening = widen1;
|
---|
228 | env.add( class1 );
|
---|
229 | } else {
|
---|
230 | class2.vars.insert( class1.vars.begin(), class1.vars.end() );
|
---|
231 | class2.allowWidening = widen2;
|
---|
232 | env.add( class2 );
|
---|
233 | } // if
|
---|
234 | } else if ( hasClass1 ) {
|
---|
235 | class1.vars.insert( var2->get_name() );
|
---|
236 | class1.allowWidening = widen1;
|
---|
237 | env.add( class1 );
|
---|
238 | } else if ( hasClass2 ) {
|
---|
239 | class2.vars.insert( var1->get_name() );
|
---|
240 | class2.allowWidening = widen2;
|
---|
241 | env.add( class2 );
|
---|
242 | } else {
|
---|
243 | EqvClass newClass;
|
---|
244 | newClass.vars.insert( var1->get_name() );
|
---|
245 | newClass.vars.insert( var2->get_name() );
|
---|
246 | newClass.allowWidening = widen1 && widen2;
|
---|
247 | newClass.kind = kind;
|
---|
248 | env.add( newClass );
|
---|
249 | } // if
|
---|
250 | delete type1;
|
---|
251 | delete type2;
|
---|
252 | return result;
|
---|
253 | }
|
---|
254 |
|
---|
255 | bool unify( Type *type1, Type *type2, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, OpenVarSet &openVars, const SymTab::Indexer &indexer ) {
|
---|
256 | OpenVarSet closedVars;
|
---|
257 | findOpenVars( type1, openVars, closedVars, needAssertions, haveAssertions, false );
|
---|
258 | findOpenVars( type2, openVars, closedVars, needAssertions, haveAssertions, true );
|
---|
259 | Type *commonType = 0;
|
---|
260 | if ( unifyInexact( type1, type2, env, needAssertions, haveAssertions, openVars, WidenMode( true, true ), indexer, commonType ) ) {
|
---|
261 | if ( commonType ) {
|
---|
262 | delete commonType;
|
---|
263 | } // if
|
---|
264 | return true;
|
---|
265 | } else {
|
---|
266 | return false;
|
---|
267 | } // if
|
---|
268 | }
|
---|
269 |
|
---|
270 | bool unify( Type *type1, Type *type2, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, OpenVarSet &openVars, const SymTab::Indexer &indexer, Type *&commonType ) {
|
---|
271 | OpenVarSet closedVars;
|
---|
272 | findOpenVars( type1, openVars, closedVars, needAssertions, haveAssertions, false );
|
---|
273 | findOpenVars( type2, openVars, closedVars, needAssertions, haveAssertions, true );
|
---|
274 | return unifyInexact( type1, type2, env, needAssertions, haveAssertions, openVars, WidenMode( true, true ), indexer, commonType );
|
---|
275 | }
|
---|
276 |
|
---|
277 | bool unifyExact( Type *type1, Type *type2, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, WidenMode widenMode, const SymTab::Indexer &indexer ) {
|
---|
278 | #ifdef DEBUG
|
---|
279 | TypeEnvironment debugEnv( env );
|
---|
280 | #endif
|
---|
281 | bool result;
|
---|
282 | TypeInstType *var1 = dynamic_cast< TypeInstType* >( type1 );
|
---|
283 | TypeInstType *var2 = dynamic_cast< TypeInstType* >( type2 );
|
---|
284 | OpenVarSet::const_iterator entry1, entry2;
|
---|
285 | if ( var1 ) {
|
---|
286 | entry1 = openVars.find( var1->get_name() );
|
---|
287 | } // if
|
---|
288 | if ( var2 ) {
|
---|
289 | entry2 = openVars.find( var2->get_name() );
|
---|
290 | } // if
|
---|
291 | bool isopen1 = var1 && ( entry1 != openVars.end() );
|
---|
292 | bool isopen2 = var2 && ( entry2 != openVars.end() );
|
---|
293 | if ( type1->get_qualifiers() != type2->get_qualifiers() ) {
|
---|
294 | return false;
|
---|
295 | } else if ( isopen1 && isopen2 && entry1->second == entry2->second ) {
|
---|
296 | result = bindVarToVar( var1, var2, entry1->second, env, needAssertions, haveAssertions, openVars, widenMode, indexer );
|
---|
297 | } else if ( isopen1 ) {
|
---|
298 | result = bindVar( var1, type2, entry1->second, env, needAssertions, haveAssertions, openVars, widenMode, indexer );
|
---|
299 | } else if ( isopen2 ) {
|
---|
300 | result = bindVar( var2, type1, entry2->second, env, needAssertions, haveAssertions, openVars, widenMode, indexer );
|
---|
301 | } else {
|
---|
302 | Unify comparator( type2, env, needAssertions, haveAssertions, openVars, widenMode, indexer );
|
---|
303 | type1->accept( comparator );
|
---|
304 | result = comparator.get_result();
|
---|
305 | } // if
|
---|
306 | #ifdef DEBUG
|
---|
307 | std::cout << "============ unifyExact" << std::endl;
|
---|
308 | std::cout << "type1 is ";
|
---|
309 | type1->print( std::cout );
|
---|
310 | std::cout << std::endl << "type2 is ";
|
---|
311 | type2->print( std::cout );
|
---|
312 | std::cout << std::endl << "openVars are ";
|
---|
313 | printOpenVarSet( openVars, std::cout, 8 );
|
---|
314 | std::cout << std::endl << "input env is " << std::endl;
|
---|
315 | debugEnv.print( std::cout, 8 );
|
---|
316 | std::cout << std::endl << "result env is " << std::endl;
|
---|
317 | env.print( std::cout, 8 );
|
---|
318 | std::cout << "result is " << result << std::endl;
|
---|
319 | #endif
|
---|
320 | return result;
|
---|
321 | }
|
---|
322 |
|
---|
323 | bool unifyExact( Type *type1, Type *type2, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, OpenVarSet &openVars, const SymTab::Indexer &indexer ) {
|
---|
324 | return unifyExact( type1, type2, env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
|
---|
325 | }
|
---|
326 |
|
---|
327 | bool unifyInexact( Type *type1, Type *type2, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, WidenMode widenMode, const SymTab::Indexer &indexer, Type *&common ) {
|
---|
328 | Type::Qualifiers tq1 = type1->get_qualifiers(), tq2 = type2->get_qualifiers();
|
---|
329 | type1->get_qualifiers() = Type::Qualifiers();
|
---|
330 | type2->get_qualifiers() = Type::Qualifiers();
|
---|
331 | bool result;
|
---|
332 | #ifdef DEBUG
|
---|
333 | std::cout << "unifyInexact type 1 is ";
|
---|
334 | type1->print( std::cout );
|
---|
335 | std::cout << "type 2 is ";
|
---|
336 | type2->print( std::cout );
|
---|
337 | std::cout << std::endl;
|
---|
338 | #endif
|
---|
339 | if ( ! unifyExact( type1, type2, env, needAssertions, haveAssertions, openVars, widenMode, indexer ) ) {
|
---|
340 | #ifdef DEBUG
|
---|
341 | std::cout << "unifyInexact: no exact unification found" << std::endl;
|
---|
342 | #endif
|
---|
343 | if ( ( common = commonType( type1, type2, widenMode.widenFirst, widenMode.widenSecond, indexer, env, openVars ) ) ) {
|
---|
344 | common->get_qualifiers() = tq1 + tq2;
|
---|
345 | #ifdef DEBUG
|
---|
346 | std::cout << "unifyInexact: common type is ";
|
---|
347 | common->print( std::cout );
|
---|
348 | std::cout << std::endl;
|
---|
349 | #endif
|
---|
350 | result = true;
|
---|
351 | } else {
|
---|
352 | #ifdef DEBUG
|
---|
353 | std::cout << "unifyInexact: no common type found" << std::endl;
|
---|
354 | #endif
|
---|
355 | result = false;
|
---|
356 | } // if
|
---|
357 | } else {
|
---|
358 | if ( tq1 != tq2 ) {
|
---|
359 | if ( ( tq1 > tq2 || widenMode.widenFirst ) && ( tq2 > tq1 || widenMode.widenSecond ) ) {
|
---|
360 | common = type1->clone();
|
---|
361 | common->get_qualifiers() = tq1 + tq2;
|
---|
362 | result = true;
|
---|
363 | } else {
|
---|
364 | result = false;
|
---|
365 | } // if
|
---|
366 | } else {
|
---|
367 | result = true;
|
---|
368 | } // if
|
---|
369 | } // if
|
---|
370 | type1->get_qualifiers() = tq1;
|
---|
371 | type2->get_qualifiers() = tq2;
|
---|
372 | return result;
|
---|
373 | }
|
---|
374 |
|
---|
375 | Unify::Unify( Type *type2, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, WidenMode widenMode, const SymTab::Indexer &indexer )
|
---|
376 | : result( false ), type2( type2 ), env( env ), needAssertions( needAssertions ), haveAssertions( haveAssertions ), openVars( openVars ), widenMode( widenMode ), indexer( indexer ) {
|
---|
377 | }
|
---|
378 |
|
---|
379 | void Unify::visit(VoidType *voidType) {
|
---|
380 | result = dynamic_cast< VoidType* >( type2 );
|
---|
381 | }
|
---|
382 |
|
---|
383 | void Unify::visit(BasicType *basicType) {
|
---|
384 | if ( BasicType *otherBasic = dynamic_cast< BasicType* >( type2 ) ) {
|
---|
385 | result = basicType->get_kind() == otherBasic->get_kind();
|
---|
386 | } // if
|
---|
387 | }
|
---|
388 |
|
---|
389 | void markAssertionSet( AssertionSet &assertions, DeclarationWithType *assert ) {
|
---|
390 | /// std::cout << "assertion set is" << std::endl;
|
---|
391 | /// printAssertionSet( assertions, std::cout, 8 );
|
---|
392 | /// std::cout << "looking for ";
|
---|
393 | /// assert->print( std::cout );
|
---|
394 | /// std::cout << std::endl;
|
---|
395 | AssertionSet::iterator i = assertions.find( assert );
|
---|
396 | if ( i != assertions.end() ) {
|
---|
397 | /// std::cout << "found it!" << std::endl;
|
---|
398 | i->second = true;
|
---|
399 | } // if
|
---|
400 | }
|
---|
401 |
|
---|
402 | void markAssertions( AssertionSet &assertion1, AssertionSet &assertion2, Type *type ) {
|
---|
403 | for ( std::list< TypeDecl* >::const_iterator tyvar = type->get_forall().begin(); tyvar != type->get_forall().end(); ++tyvar ) {
|
---|
404 | for ( std::list< DeclarationWithType* >::const_iterator assert = (*tyvar)->get_assertions().begin(); assert != (*tyvar)->get_assertions().end(); ++assert ) {
|
---|
405 | markAssertionSet( assertion1, *assert );
|
---|
406 | markAssertionSet( assertion2, *assert );
|
---|
407 | } // for
|
---|
408 | } // for
|
---|
409 | }
|
---|
410 |
|
---|
411 | void Unify::visit(PointerType *pointerType) {
|
---|
412 | if ( PointerType *otherPointer = dynamic_cast< PointerType* >( type2 ) ) {
|
---|
413 | result = unifyExact( pointerType->get_base(), otherPointer->get_base(), env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
|
---|
414 | markAssertions( haveAssertions, needAssertions, pointerType );
|
---|
415 | markAssertions( haveAssertions, needAssertions, otherPointer );
|
---|
416 | } // if
|
---|
417 | }
|
---|
418 |
|
---|
419 | void Unify::visit(ArrayType *arrayType) {
|
---|
420 | // XXX -- compare array dimension
|
---|
421 | ArrayType *otherArray = dynamic_cast< ArrayType* >( type2 );
|
---|
422 | if ( otherArray && arrayType->get_isVarLen() == otherArray->get_isVarLen() ) {
|
---|
423 | result = unifyExact( arrayType->get_base(), otherArray->get_base(), env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
|
---|
424 | } // if
|
---|
425 | }
|
---|
426 |
|
---|
427 | template< typename Iterator1, typename Iterator2 >
|
---|
428 | bool unifyDeclList( Iterator1 list1Begin, Iterator1 list1End, Iterator2 list2Begin, Iterator2 list2End, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, const SymTab::Indexer &indexer ) {
|
---|
429 | for ( ; list1Begin != list1End && list2Begin != list2End; ++list1Begin, ++list2Begin ) {
|
---|
430 | if ( ! unifyExact( (*list1Begin)->get_type(), (*list2Begin)->get_type(), env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer ) ) {
|
---|
431 | return false;
|
---|
432 | } // if
|
---|
433 | } // for
|
---|
434 | if ( list1Begin != list1End || list2Begin != list2End ) {
|
---|
435 | return false;
|
---|
436 | } else {
|
---|
437 | return true;
|
---|
438 | } // if
|
---|
439 | }
|
---|
440 |
|
---|
441 | void Unify::visit(FunctionType *functionType) {
|
---|
442 | FunctionType *otherFunction = dynamic_cast< FunctionType* >( type2 );
|
---|
443 | if ( otherFunction && functionType->get_isVarArgs() == otherFunction->get_isVarArgs() ) {
|
---|
444 |
|
---|
445 | if ( unifyDeclList( functionType->get_parameters().begin(), functionType->get_parameters().end(), otherFunction->get_parameters().begin(), otherFunction->get_parameters().end(), env, needAssertions, haveAssertions, openVars, indexer ) ) {
|
---|
446 |
|
---|
447 | if ( unifyDeclList( functionType->get_returnVals().begin(), functionType->get_returnVals().end(), otherFunction->get_returnVals().begin(), otherFunction->get_returnVals().end(), env, needAssertions, haveAssertions, openVars, indexer ) ) {
|
---|
448 |
|
---|
449 | markAssertions( haveAssertions, needAssertions, functionType );
|
---|
450 | markAssertions( haveAssertions, needAssertions, otherFunction );
|
---|
451 |
|
---|
452 | result = true;
|
---|
453 | } // if
|
---|
454 | } // if
|
---|
455 | } // if
|
---|
456 | }
|
---|
457 |
|
---|
458 | template< typename RefType >
|
---|
459 | void Unify::handleRefType( RefType *inst, Type *other ) {
|
---|
460 | RefType *otherStruct = dynamic_cast< RefType* >( other );
|
---|
461 | result = otherStruct && inst->get_name() == otherStruct->get_name();
|
---|
462 | }
|
---|
463 |
|
---|
464 | void Unify::visit(StructInstType *structInst) {
|
---|
465 | handleRefType( structInst, type2 );
|
---|
466 | }
|
---|
467 |
|
---|
468 | void Unify::visit(UnionInstType *unionInst) {
|
---|
469 | handleRefType( unionInst, type2 );
|
---|
470 | }
|
---|
471 |
|
---|
472 | void Unify::visit(EnumInstType *enumInst) {
|
---|
473 | handleRefType( enumInst, type2 );
|
---|
474 | }
|
---|
475 |
|
---|
476 | void Unify::visit(ContextInstType *contextInst) {
|
---|
477 | handleRefType( contextInst, type2 );
|
---|
478 | }
|
---|
479 |
|
---|
480 | void Unify::visit(TypeInstType *typeInst) {
|
---|
481 | assert( openVars.find( typeInst->get_name() ) == openVars.end() );
|
---|
482 | TypeInstType *otherInst = dynamic_cast< TypeInstType* >( type2 );
|
---|
483 | if ( otherInst && typeInst->get_name() == otherInst->get_name() ) {
|
---|
484 | result = true;
|
---|
485 | /// } else {
|
---|
486 | /// NamedTypeDecl *nt = indexer.lookupType( typeInst->get_name() );
|
---|
487 | /// if ( nt ) {
|
---|
488 | /// TypeDecl *type = dynamic_cast< TypeDecl* >( nt );
|
---|
489 | /// assert( type );
|
---|
490 | /// if ( type->get_base() ) {
|
---|
491 | /// result = unifyExact( type->get_base(), typeInst, env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
|
---|
492 | /// }
|
---|
493 | /// }
|
---|
494 | } // if
|
---|
495 | }
|
---|
496 |
|
---|
497 | template< typename Iterator1, typename Iterator2 >
|
---|
498 | 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 ) {
|
---|
499 | for ( ; list1Begin != list1End && list2Begin != list2End; ++list1Begin, ++list2Begin ) {
|
---|
500 | Type *commonType = 0;
|
---|
501 | if ( ! unifyInexact( *list1Begin, *list2Begin, env, needAssertions, haveAssertions, openVars, widenMode, indexer, commonType ) ) {
|
---|
502 | return false;
|
---|
503 | }
|
---|
504 | delete commonType;
|
---|
505 | } // for
|
---|
506 | if ( list1Begin != list1End || list2Begin != list2End ) {
|
---|
507 | return false;
|
---|
508 | } else {
|
---|
509 | return true;
|
---|
510 | } //if
|
---|
511 | }
|
---|
512 |
|
---|
513 | void Unify::visit(TupleType *tupleType) {
|
---|
514 | if ( TupleType *otherTuple = dynamic_cast< TupleType* >( type2 ) ) {
|
---|
515 | result = unifyList( tupleType->get_types().begin(), tupleType->get_types().end(), otherTuple->get_types().begin(), otherTuple->get_types().end(), env, needAssertions, haveAssertions, openVars, widenMode, indexer );
|
---|
516 | } // if
|
---|
517 | }
|
---|
518 |
|
---|
519 | } // namespace ResolvExpr
|
---|
520 |
|
---|
521 | // Local Variables: //
|
---|
522 | // tab-width: 4 //
|
---|
523 | // mode: c++ //
|
---|
524 | // compile-command: "make install" //
|
---|
525 | // End: //
|
---|