source: src/ResolvExpr/Unify.cc@ c519942

ADT arm-eh ast-experimental cleanup-dtors enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since c519942 was 4139e3d, checked in by Thierry Delisle <tdelisle@…>, 6 years ago

Missed one file'

  • Property mode set to 100644
File size: 28.7 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 : Aaron B. Moss
12// Last Modified On : Mon Jun 18 11:58:00 2018
13// Update Count : 43
14//
15
16#include <cassert> // for assertf, assert
17#include <iterator> // for back_insert_iterator, back_inserter
18#include <map> // for _Rb_tree_const_iterator, _Rb_tree_i...
19#include <memory> // for unique_ptr
20#include <set> // for set
21#include <string> // for string, operator==, operator!=, bas...
22#include <utility> // for pair, move
23#include <vector>
24
25#include "AST/Node.hpp"
26#include "AST/Type.hpp"
27#include "Common/PassVisitor.h" // for PassVisitor
28#include "FindOpenVars.h" // for findOpenVars
29#include "Parser/LinkageSpec.h" // for C
30#include "SynTree/Constant.h" // for Constant
31#include "SynTree/Declaration.h" // for TypeDecl, TypeDecl::Data, Declarati...
32#include "SynTree/Expression.h" // for TypeExpr, Expression, ConstantExpr
33#include "SynTree/Mutator.h" // for Mutator
34#include "SynTree/Type.h" // for Type, TypeInstType, FunctionType
35#include "SynTree/Visitor.h" // for Visitor
36#include "Tuples/Tuples.h" // for isTtype
37#include "TypeEnvironment.h" // for EqvClass, AssertionSet, OpenVarSet
38#include "Unify.h"
39#include "typeops.h" // for flatten, occurs, commonType
40
41namespace SymTab {
42class Indexer;
43} // namespace SymTab
44
45// #define DEBUG
46
47namespace ResolvExpr {
48
49 struct Unify : public WithShortCircuiting {
50 Unify( Type *type2, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, WidenMode widenMode, const SymTab::Indexer &indexer );
51
52 bool get_result() const { return result; }
53
54 void previsit( BaseSyntaxNode * ) { visit_children = false; }
55
56 void postvisit( VoidType * voidType );
57 void postvisit( BasicType * basicType );
58 void postvisit( PointerType * pointerType );
59 void postvisit( ArrayType * arrayType );
60 void postvisit( ReferenceType * refType );
61 void postvisit( FunctionType * functionType );
62 void postvisit( StructInstType * aggregateUseType );
63 void postvisit( UnionInstType * aggregateUseType );
64 void postvisit( EnumInstType * aggregateUseType );
65 void postvisit( TraitInstType * aggregateUseType );
66 void postvisit( TypeInstType * aggregateUseType );
67 void postvisit( TupleType * tupleType );
68 void postvisit( VarArgsType * varArgsType );
69 void postvisit( ZeroType * zeroType );
70 void postvisit( OneType * oneType );
71
72 private:
73 template< typename RefType > void handleRefType( RefType *inst, Type *other );
74 template< typename RefType > void handleGenericRefType( RefType *inst, Type *other );
75
76 bool result;
77 Type *type2; // inherited
78 TypeEnvironment &env;
79 AssertionSet &needAssertions;
80 AssertionSet &haveAssertions;
81 const OpenVarSet &openVars;
82 WidenMode widenMode;
83 const SymTab::Indexer &indexer;
84 };
85
86 /// Attempts an inexact unification of type1 and type2.
87 /// Returns false if no such unification; if the types can be unified, sets common (unless they unify exactly and have identical type qualifiers)
88 bool unifyInexact( Type *type1, Type *type2, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, WidenMode widenMode, const SymTab::Indexer &indexer, Type *&common );
89 bool unifyExact( Type *type1, Type *type2, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, WidenMode widenMode, const SymTab::Indexer &indexer );
90
91 bool typesCompatible( Type *first, Type *second, const SymTab::Indexer &indexer, const TypeEnvironment &env ) {
92 TypeEnvironment newEnv;
93 OpenVarSet openVars, closedVars; // added closedVars
94 AssertionSet needAssertions, haveAssertions;
95 Type *newFirst = first->clone(), *newSecond = second->clone();
96 env.apply( newFirst );
97 env.apply( newSecond );
98
99 // do we need to do this? Seems like we do, types should be able to be compatible if they
100 // have free variables that can unify
101 findOpenVars( newFirst, openVars, closedVars, needAssertions, haveAssertions, false );
102 findOpenVars( newSecond, openVars, closedVars, needAssertions, haveAssertions, true );
103
104 bool result = unifyExact( newFirst, newSecond, newEnv, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
105 delete newFirst;
106 delete newSecond;
107 return result;
108 }
109
110 bool typesCompatibleIgnoreQualifiers( Type *first, Type *second, const SymTab::Indexer &indexer, const TypeEnvironment &env ) {
111 TypeEnvironment newEnv;
112 OpenVarSet openVars;
113 AssertionSet needAssertions, haveAssertions;
114 Type *newFirst = first->clone(), *newSecond = second->clone();
115 env.apply( newFirst );
116 env.apply( newSecond );
117 newFirst->get_qualifiers() = Type::Qualifiers();
118 newSecond->get_qualifiers() = Type::Qualifiers();
119/// std::cerr << "first is ";
120/// first->print( std::cerr );
121/// std::cerr << std::endl << "second is ";
122/// second->print( std::cerr );
123/// std::cerr << std::endl << "newFirst is ";
124/// newFirst->print( std::cerr );
125/// std::cerr << std::endl << "newSecond is ";
126/// newSecond->print( std::cerr );
127/// std::cerr << std::endl;
128 bool result = unifyExact( newFirst, newSecond, newEnv, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
129 delete newFirst;
130 delete newSecond;
131 return result;
132 }
133
134 bool unify( Type *type1, Type *type2, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, OpenVarSet &openVars, const SymTab::Indexer &indexer ) {
135 OpenVarSet closedVars;
136 findOpenVars( type1, openVars, closedVars, needAssertions, haveAssertions, false );
137 findOpenVars( type2, openVars, closedVars, needAssertions, haveAssertions, true );
138 Type *commonType = 0;
139 if ( unifyInexact( type1, type2, env, needAssertions, haveAssertions, openVars, WidenMode( true, true ), indexer, commonType ) ) {
140 if ( commonType ) {
141 delete commonType;
142 } // if
143 return true;
144 } else {
145 return false;
146 } // if
147 }
148
149 bool unify( Type *type1, Type *type2, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, OpenVarSet &openVars, const SymTab::Indexer &indexer, Type *&commonType ) {
150 OpenVarSet closedVars;
151 findOpenVars( type1, openVars, closedVars, needAssertions, haveAssertions, false );
152 findOpenVars( type2, openVars, closedVars, needAssertions, haveAssertions, true );
153 return unifyInexact( type1, type2, env, needAssertions, haveAssertions, openVars, WidenMode( true, true ), indexer, commonType );
154 }
155
156 bool unifyExact( Type *type1, Type *type2, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, WidenMode widenMode, const SymTab::Indexer &indexer ) {
157#ifdef DEBUG
158 TypeEnvironment debugEnv( env );
159#endif
160 if ( type1->get_qualifiers() != type2->get_qualifiers() ) {
161 return false;
162 }
163
164 bool result;
165 TypeInstType *var1 = dynamic_cast< TypeInstType* >( type1 );
166 TypeInstType *var2 = dynamic_cast< TypeInstType* >( type2 );
167 OpenVarSet::const_iterator entry1, entry2;
168 if ( var1 ) {
169 entry1 = openVars.find( var1->get_name() );
170 } // if
171 if ( var2 ) {
172 entry2 = openVars.find( var2->get_name() );
173 } // if
174 bool isopen1 = var1 && ( entry1 != openVars.end() );
175 bool isopen2 = var2 && ( entry2 != openVars.end() );
176
177 if ( isopen1 && isopen2 ) {
178 if ( entry1->second.kind != entry2->second.kind ) {
179 result = false;
180 } else {
181 result = env.bindVarToVar(
182 var1, var2, TypeDecl::Data{ entry1->second, entry2->second }, needAssertions,
183 haveAssertions, openVars, widenMode, indexer );
184 }
185 } else if ( isopen1 ) {
186 result = env.bindVar( var1, type2, entry1->second, needAssertions, haveAssertions, openVars, widenMode, indexer );
187 } else if ( isopen2 ) { // TODO: swap widenMode values in call, since type positions are flipped?
188 result = env.bindVar( var2, type1, entry2->second, needAssertions, haveAssertions, openVars, widenMode, indexer );
189 } else {
190 PassVisitor<Unify> comparator( type2, env, needAssertions, haveAssertions, openVars, widenMode, indexer );
191 type1->accept( comparator );
192 result = comparator.pass.get_result();
193 } // if
194#ifdef DEBUG
195 std::cerr << "============ unifyExact" << std::endl;
196 std::cerr << "type1 is ";
197 type1->print( std::cerr );
198 std::cerr << std::endl << "type2 is ";
199 type2->print( std::cerr );
200 std::cerr << std::endl << "openVars are ";
201 printOpenVarSet( openVars, std::cerr, 8 );
202 std::cerr << std::endl << "input env is " << std::endl;
203 debugEnv.print( std::cerr, 8 );
204 std::cerr << std::endl << "result env is " << std::endl;
205 env.print( std::cerr, 8 );
206 std::cerr << "result is " << result << std::endl;
207#endif
208 return result;
209 }
210
211 bool unifyExact( Type *type1, Type *type2, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, OpenVarSet &openVars, const SymTab::Indexer &indexer ) {
212 return unifyExact( type1, type2, env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
213 }
214
215 bool unifyInexact( Type *type1, Type *type2, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, WidenMode widenMode, const SymTab::Indexer &indexer, Type *&common ) {
216 Type::Qualifiers tq1 = type1->get_qualifiers(), tq2 = type2->get_qualifiers();
217 type1->get_qualifiers() = Type::Qualifiers();
218 type2->get_qualifiers() = Type::Qualifiers();
219 bool result;
220#ifdef DEBUG
221 std::cerr << "unifyInexact type 1 is ";
222 type1->print( std::cerr );
223 std::cerr << " type 2 is ";
224 type2->print( std::cerr );
225 std::cerr << std::endl;
226#endif
227 if ( ! unifyExact( type1, type2, env, needAssertions, haveAssertions, openVars, widenMode, indexer ) ) {
228#ifdef DEBUG
229 std::cerr << "unifyInexact: no exact unification found" << std::endl;
230#endif
231 if ( ( common = commonType( type1, type2, widenMode.widenFirst, widenMode.widenSecond, indexer, env, openVars ) ) ) {
232 common->get_qualifiers() = tq1 | tq2;
233#ifdef DEBUG
234 std::cerr << "unifyInexact: common type is ";
235 common->print( std::cerr );
236 std::cerr << std::endl;
237#endif
238 result = true;
239 } else {
240#ifdef DEBUG
241 std::cerr << "unifyInexact: no common type found" << std::endl;
242#endif
243 result = false;
244 } // if
245 } else {
246 if ( tq1 != tq2 ) {
247 if ( ( tq1 > tq2 || widenMode.widenFirst ) && ( tq2 > tq1 || widenMode.widenSecond ) ) {
248 common = type1->clone();
249 common->get_qualifiers() = tq1 | tq2;
250 result = true;
251 } else {
252 result = false;
253 } // if
254 } else {
255 common = type1->clone();
256 common->get_qualifiers() = tq1 | tq2;
257 result = true;
258 } // if
259 } // if
260 type1->get_qualifiers() = tq1;
261 type2->get_qualifiers() = tq2;
262 return result;
263 }
264
265 Unify::Unify( Type *type2, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, WidenMode widenMode, const SymTab::Indexer &indexer )
266 : result( false ), type2( type2 ), env( env ), needAssertions( needAssertions ), haveAssertions( haveAssertions ), openVars( openVars ), widenMode( widenMode ), indexer( indexer ) {
267 }
268
269 void Unify::postvisit( __attribute__((unused)) VoidType *voidType) {
270 result = dynamic_cast< VoidType* >( type2 );
271 }
272
273 void Unify::postvisit(BasicType *basicType) {
274 if ( BasicType *otherBasic = dynamic_cast< BasicType* >( type2 ) ) {
275 result = basicType->get_kind() == otherBasic->get_kind();
276 } // if
277 }
278
279 void markAssertionSet( AssertionSet &assertions, DeclarationWithType *assert ) {
280/// std::cerr << "assertion set is" << std::endl;
281/// printAssertionSet( assertions, std::cerr, 8 );
282/// std::cerr << "looking for ";
283/// assert->print( std::cerr );
284/// std::cerr << std::endl;
285 AssertionSet::iterator i = assertions.find( assert );
286 if ( i != assertions.end() ) {
287/// std::cerr << "found it!" << std::endl;
288 i->second.isUsed = true;
289 } // if
290 }
291
292 void markAssertions( AssertionSet &assertion1, AssertionSet &assertion2, Type *type ) {
293 for ( std::list< TypeDecl* >::const_iterator tyvar = type->get_forall().begin(); tyvar != type->get_forall().end(); ++tyvar ) {
294 for ( std::list< DeclarationWithType* >::const_iterator assert = (*tyvar)->get_assertions().begin(); assert != (*tyvar)->get_assertions().end(); ++assert ) {
295 markAssertionSet( assertion1, *assert );
296 markAssertionSet( assertion2, *assert );
297 } // for
298 } // for
299 }
300
301 void Unify::postvisit(PointerType *pointerType) {
302 if ( PointerType *otherPointer = dynamic_cast< PointerType* >( type2 ) ) {
303 result = unifyExact( pointerType->get_base(), otherPointer->get_base(), env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
304 markAssertions( haveAssertions, needAssertions, pointerType );
305 markAssertions( haveAssertions, needAssertions, otherPointer );
306 } // if
307 }
308
309 void Unify::postvisit(ReferenceType *refType) {
310 if ( ReferenceType *otherRef = dynamic_cast< ReferenceType* >( type2 ) ) {
311 result = unifyExact( refType->get_base(), otherRef->get_base(), env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
312 markAssertions( haveAssertions, needAssertions, refType );
313 markAssertions( haveAssertions, needAssertions, otherRef );
314 } // if
315 }
316
317 void Unify::postvisit(ArrayType *arrayType) {
318 ArrayType *otherArray = dynamic_cast< ArrayType* >( type2 );
319 // to unify, array types must both be VLA or both not VLA
320 // and must both have a dimension expression or not have a dimension
321 if ( otherArray && arrayType->get_isVarLen() == otherArray->get_isVarLen() ) {
322
323 if ( ! arrayType->get_isVarLen() && ! otherArray->get_isVarLen() &&
324 arrayType->get_dimension() != 0 && otherArray->get_dimension() != 0 ) {
325 ConstantExpr * ce1 = dynamic_cast< ConstantExpr * >( arrayType->get_dimension() );
326 ConstantExpr * ce2 = dynamic_cast< ConstantExpr * >( otherArray->get_dimension() );
327 // see C11 Reference Manual 6.7.6.2.6
328 // two array types with size specifiers that are integer constant expressions are
329 // compatible if both size specifiers have the same constant value
330 if ( ce1 && ce2 ) {
331 Constant * c1 = ce1->get_constant();
332 Constant * c2 = ce2->get_constant();
333
334 if ( c1->get_value() != c2->get_value() ) {
335 // does not unify if the dimension is different
336 return;
337 }
338 }
339 }
340
341 result = unifyExact( arrayType->get_base(), otherArray->get_base(), env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
342 } // if
343 }
344
345 template< typename Iterator, typename Func >
346 std::unique_ptr<Type> combineTypes( Iterator begin, Iterator end, Func & toType ) {
347 std::list< Type * > types;
348 for ( ; begin != end; ++begin ) {
349 // it's guaranteed that a ttype variable will be bound to a flat tuple, so ensure that this results in a flat tuple
350 flatten( toType( *begin ), back_inserter( types ) );
351 }
352 return std::unique_ptr<Type>( new TupleType( Type::Qualifiers(), types ) );
353 }
354
355 template< typename Iterator1, typename Iterator2 >
356 bool unifyDeclList( Iterator1 list1Begin, Iterator1 list1End, Iterator2 list2Begin, Iterator2 list2End, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, const SymTab::Indexer &indexer ) {
357 auto get_type = [](DeclarationWithType * dwt){ return dwt->get_type(); };
358 for ( ; list1Begin != list1End && list2Begin != list2End; ++list1Begin, ++list2Begin ) {
359 Type * t1 = (*list1Begin)->get_type();
360 Type * t2 = (*list2Begin)->get_type();
361 bool isTtype1 = Tuples::isTtype( t1 );
362 bool isTtype2 = Tuples::isTtype( t2 );
363 // xxx - assumes ttype must be last parameter
364 // xxx - there may be a nice way to refactor this, but be careful because the argument positioning might matter in some cases.
365 if ( isTtype1 && ! isTtype2 ) {
366 // combine all of the things in list2, then unify
367 return unifyExact( t1, combineTypes( list2Begin, list2End, get_type ).get(), env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
368 } else if ( isTtype2 && ! isTtype1 ) {
369 // combine all of the things in list1, then unify
370 return unifyExact( combineTypes( list1Begin, list1End, get_type ).get(), t2, env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
371 } else if ( ! unifyExact( t1, t2, env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer ) ) {
372 return false;
373 } // if
374 } // for
375 // 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
376 if ( list1Begin != list1End ) {
377 // try unifying empty tuple type with ttype
378 Type * t1 = (*list1Begin)->get_type();
379 if ( Tuples::isTtype( t1 ) ) {
380 return unifyExact( t1, combineTypes( list2Begin, list2End, get_type ).get(), env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
381 } else return false;
382 } else if ( list2Begin != list2End ) {
383 // try unifying empty tuple type with ttype
384 Type * t2 = (*list2Begin)->get_type();
385 if ( Tuples::isTtype( t2 ) ) {
386 return unifyExact( combineTypes( list1Begin, list1End, get_type ).get(), t2, env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
387 } else return false;
388 } else {
389 return true;
390 } // if
391 }
392
393 /// Finds ttypes and replaces them with their expansion, if known.
394 /// This needs to be done so that satisfying ttype assertions is easier.
395 /// If this isn't done then argument lists can have wildly different
396 /// size and structure, when they should be compatible.
397 struct TtypeExpander : public WithShortCircuiting {
398 TypeEnvironment & tenv;
399 TtypeExpander( TypeEnvironment & tenv ) : tenv( tenv ) {}
400 void premutate( TypeInstType * ) { visit_children = false; }
401 Type * postmutate( TypeInstType * typeInst ) {
402 if ( const EqvClass *eqvClass = tenv.lookup( typeInst->get_name() ) ) {
403 // expand ttype parameter into its actual type
404 if ( eqvClass->data.kind == TypeDecl::Ttype && eqvClass->type ) {
405 delete typeInst;
406 return eqvClass->type->clone();
407 }
408 }
409 return typeInst;
410 }
411 };
412
413 /// flattens a list of declarations, so that each tuple type has a single declaration.
414 /// makes use of TtypeExpander to ensure ttypes are flat as well.
415 void flattenList( std::list< DeclarationWithType * > src, std::list< DeclarationWithType * > & dst, TypeEnvironment & env ) {
416 dst.clear();
417 for ( DeclarationWithType * dcl : src ) {
418 PassVisitor<TtypeExpander> expander( env );
419 dcl->acceptMutator( expander );
420 std::list< Type * > types;
421 flatten( dcl->get_type(), back_inserter( types ) );
422 for ( Type * t : types ) {
423 // outermost const, volatile, _Atomic qualifiers in parameters should not play a role in the unification of function types, since they do not determine whether a function is callable.
424 // Note: MUST consider at least mutex qualifier, since functions can be overloaded on outermost mutex and a mutex function has different requirements than a non-mutex function.
425 t->get_qualifiers() -= Type::Qualifiers(Type::Const | Type::Volatile | Type::Atomic);
426
427 dst.push_back( new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::C, nullptr, t, nullptr ) );
428 }
429 delete dcl;
430 }
431 }
432
433 void Unify::postvisit(FunctionType *functionType) {
434 FunctionType *otherFunction = dynamic_cast< FunctionType* >( type2 );
435 if ( otherFunction && functionType->get_isVarArgs() == otherFunction->get_isVarArgs() ) {
436 // flatten the parameter lists for both functions so that tuple structure
437 // doesn't affect unification. Must be a clone so that the types don't change.
438 std::unique_ptr<FunctionType> flatFunc( functionType->clone() );
439 std::unique_ptr<FunctionType> flatOther( otherFunction->clone() );
440 flattenList( flatFunc->get_parameters(), flatFunc->get_parameters(), env );
441 flattenList( flatOther->get_parameters(), flatOther->get_parameters(), env );
442
443 // sizes don't have to match if ttypes are involved; need to be more precise wrt where the ttype is to prevent errors
444 if ( (flatFunc->parameters.size() == flatOther->parameters.size() && flatFunc->returnVals.size() == flatOther->returnVals.size()) || flatFunc->isTtype() || flatOther->isTtype() ) {
445 if ( unifyDeclList( flatFunc->parameters.begin(), flatFunc->parameters.end(), flatOther->parameters.begin(), flatOther->parameters.end(), env, needAssertions, haveAssertions, openVars, indexer ) ) {
446 if ( unifyDeclList( flatFunc->returnVals.begin(), flatFunc->returnVals.end(), flatOther->returnVals.begin(), flatOther->returnVals.end(), env, needAssertions, haveAssertions, openVars, indexer ) ) {
447
448 // the original types must be used in mark assertions, since pointer comparisons are used
449 markAssertions( haveAssertions, needAssertions, functionType );
450 markAssertions( haveAssertions, needAssertions, otherFunction );
451
452 result = true;
453 } // if
454 } // if
455 } // if
456 } // if
457 }
458
459 template< typename RefType >
460 void Unify::handleRefType( RefType *inst, Type *other ) {
461 // check that other type is compatible and named the same
462 RefType *otherStruct = dynamic_cast< RefType* >( other );
463 result = otherStruct && inst->name == otherStruct->name;
464 }
465
466 template< typename RefType >
467 void Unify::handleGenericRefType( RefType *inst, Type *other ) {
468 // Check that other type is compatible and named the same
469 handleRefType( inst, other );
470 if ( ! result ) return;
471 // Check that parameters of types unify, if any
472 std::list< Expression* > params = inst->parameters;
473 std::list< Expression* > otherParams = ((RefType*)other)->parameters;
474
475 std::list< Expression* >::const_iterator it = params.begin(), jt = otherParams.begin();
476 for ( ; it != params.end() && jt != otherParams.end(); ++it, ++jt ) {
477 TypeExpr *param = dynamic_cast< TypeExpr* >(*it);
478 assertf(param, "Aggregate parameters should be type expressions");
479 TypeExpr *otherParam = dynamic_cast< TypeExpr* >(*jt);
480 assertf(otherParam, "Aggregate parameters should be type expressions");
481
482 Type* paramTy = param->get_type();
483 Type* otherParamTy = otherParam->get_type();
484
485 bool tupleParam = Tuples::isTtype( paramTy );
486 bool otherTupleParam = Tuples::isTtype( otherParamTy );
487
488 if ( tupleParam && otherTupleParam ) {
489 ++it; ++jt; // skip ttype parameters for break
490 } else if ( tupleParam ) {
491 // bundle other parameters into tuple to match
492 std::list< Type * > binderTypes;
493
494 do {
495 binderTypes.push_back( otherParam->get_type()->clone() );
496 ++jt;
497
498 if ( jt == otherParams.end() ) break;
499
500 otherParam = dynamic_cast< TypeExpr* >(*jt);
501 assertf(otherParam, "Aggregate parameters should be type expressions");
502 } while (true);
503
504 otherParamTy = new TupleType{ paramTy->get_qualifiers(), binderTypes };
505 ++it; // skip ttype parameter for break
506 } else if ( otherTupleParam ) {
507 // bundle parameters into tuple to match other
508 std::list< Type * > binderTypes;
509
510 do {
511 binderTypes.push_back( param->get_type()->clone() );
512 ++it;
513
514 if ( it == params.end() ) break;
515
516 param = dynamic_cast< TypeExpr* >(*it);
517 assertf(param, "Aggregate parameters should be type expressions");
518 } while (true);
519
520 paramTy = new TupleType{ otherParamTy->get_qualifiers(), binderTypes };
521 ++jt; // skip ttype parameter for break
522 }
523
524 if ( ! unifyExact( paramTy, otherParamTy, env, needAssertions, haveAssertions, openVars, WidenMode(false, false), indexer ) ) {
525 result = false;
526 return;
527 }
528
529 // ttype parameter should be last
530 if ( tupleParam || otherTupleParam ) break;
531 }
532 result = ( it == params.end() && jt == otherParams.end() );
533 }
534
535 void Unify::postvisit(StructInstType *structInst) {
536 handleGenericRefType( structInst, type2 );
537 }
538
539 void Unify::postvisit(UnionInstType *unionInst) {
540 handleGenericRefType( unionInst, type2 );
541 }
542
543 void Unify::postvisit(EnumInstType *enumInst) {
544 handleRefType( enumInst, type2 );
545 }
546
547 void Unify::postvisit(TraitInstType *contextInst) {
548 handleRefType( contextInst, type2 );
549 }
550
551 void Unify::postvisit(TypeInstType *typeInst) {
552 assert( openVars.find( typeInst->get_name() ) == openVars.end() );
553 TypeInstType *otherInst = dynamic_cast< TypeInstType* >( type2 );
554 if ( otherInst && typeInst->get_name() == otherInst->get_name() ) {
555 result = true;
556/// } else {
557/// NamedTypeDecl *nt = indexer.lookupType( typeInst->get_name() );
558/// if ( nt ) {
559/// TypeDecl *type = dynamic_cast< TypeDecl* >( nt );
560/// assert( type );
561/// if ( type->get_base() ) {
562/// result = unifyExact( type->get_base(), typeInst, env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
563/// }
564/// }
565 } // if
566 }
567
568 template< typename Iterator1, typename Iterator2 >
569 bool unifyList( Iterator1 list1Begin, Iterator1 list1End, Iterator2 list2Begin, Iterator2 list2End, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, const SymTab::Indexer &indexer ) {
570 auto get_type = [](Type * t) { return t; };
571 for ( ; list1Begin != list1End && list2Begin != list2End; ++list1Begin, ++list2Begin ) {
572 Type * t1 = *list1Begin;
573 Type * t2 = *list2Begin;
574 bool isTtype1 = Tuples::isTtype( t1 );
575 bool isTtype2 = Tuples::isTtype( t2 );
576 // xxx - assumes ttype must be last parameter
577 // xxx - there may be a nice way to refactor this, but be careful because the argument positioning might matter in some cases.
578 if ( isTtype1 && ! isTtype2 ) {
579 // combine all of the things in list2, then unify
580 return unifyExact( t1, combineTypes( list2Begin, list2End, get_type ).get(), env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
581 } else if ( isTtype2 && ! isTtype1 ) {
582 // combine all of the things in list1, then unify
583 return unifyExact( combineTypes( list1Begin, list1End, get_type ).get(), t2, env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
584 } else if ( ! unifyExact( t1, t2, env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer ) ) {
585 return false;
586 } // if
587
588 } // for
589 if ( list1Begin != list1End ) {
590 // try unifying empty tuple type with ttype
591 Type * t1 = *list1Begin;
592 if ( Tuples::isTtype( t1 ) ) {
593 return unifyExact( t1, combineTypes( list2Begin, list2End, get_type ).get(), env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
594 } else return false;
595 } else if ( list2Begin != list2End ) {
596 // try unifying empty tuple type with ttype
597 Type * t2 = *list2Begin;
598 if ( Tuples::isTtype( t2 ) ) {
599 return unifyExact( combineTypes( list1Begin, list1End, get_type ).get(), t2, env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
600 } else return false;
601 } else {
602 return true;
603 } // if
604 }
605
606 void Unify::postvisit(TupleType *tupleType) {
607 if ( TupleType *otherTuple = dynamic_cast< TupleType* >( type2 ) ) {
608 std::unique_ptr<TupleType> flat1( tupleType->clone() );
609 std::unique_ptr<TupleType> flat2( otherTuple->clone() );
610 std::list<Type *> types1, types2;
611
612 PassVisitor<TtypeExpander> expander( env );
613 flat1->acceptMutator( expander );
614 flat2->acceptMutator( expander );
615
616 flatten( flat1.get(), back_inserter( types1 ) );
617 flatten( flat2.get(), back_inserter( types2 ) );
618
619 result = unifyList( types1.begin(), types1.end(), types2.begin(), types2.end(), env, needAssertions, haveAssertions, openVars, indexer );
620 } // if
621 }
622
623 void Unify::postvisit( __attribute__((unused)) VarArgsType *varArgsType ) {
624 result = dynamic_cast< VarArgsType* >( type2 );
625 }
626
627 void Unify::postvisit( __attribute__((unused)) ZeroType *zeroType ) {
628 result = dynamic_cast< ZeroType* >( type2 );
629 }
630
631 void Unify::postvisit( __attribute__((unused)) OneType *oneType ) {
632 result = dynamic_cast< OneType* >( type2 );
633 }
634
635 Type * extractResultType( FunctionType * function ) {
636 if ( function->get_returnVals().size() == 0 ) {
637 return new VoidType( Type::Qualifiers() );
638 } else if ( function->get_returnVals().size() == 1 ) {
639 return function->get_returnVals().front()->get_type()->clone();
640 } else {
641 std::list< Type * > types;
642 for ( DeclarationWithType * decl : function->get_returnVals() ) {
643 types.push_back( decl->get_type()->clone() );
644 } // for
645 return new TupleType( Type::Qualifiers(), types );
646 }
647 }
648
649 ast::ptr<ast::Type> extractResultType( const ast::FunctionType * func ) {
650 if ( func->returns.empty() ) return new ast::VoidType{};
651 if ( func->returns.size() == 1 ) return func->returns[0]->get_type();
652
653 std::vector<ast::ptr<ast::Type>> tys;
654 for ( const ast::DeclWithType * decl : func->returns ) {
655 tys.emplace_back( decl->get_type() );
656 }
657 return new ast::TupleType{ std::move(tys) };
658 }
659} // namespace ResolvExpr
660
661// Local Variables: //
662// tab-width: 4 //
663// mode: c++ //
664// compile-command: "make install" //
665// End: //
Note: See TracBrowser for help on using the repository browser.