source: src/ResolvExpr/Unify.cpp@ fbb5bdd

Last change on this file since fbb5bdd was 81e768d, checked in by Michael Brooks <mlbrooks@…>, 10 months ago

Fix #276; add support for c-array parameters using dependent lengths.

Without this fix, declarations like

void f( int m, int n, float[m][n] );

would either

  • generate bad C code, with unmangled variable names appearing in the function definition, or
  • refuse to resolve a valid-c call of such a function.

tests/array-collections/c-dependent: add direct tests of such cases
tests/tuplearray: activate and expand cases which were blocked on #276
tests/array: activate case fm5y, which was blocked on #276; [noise] adjust source line numbers in .expect
tests/typedefRedef: expand coverage of "error, an array detail is different" cases; [noise] adjust source line numbers in .expect
tests/functions: [noise] adjust .expect to have resolved array sizes (extra casts) in the diffed code dump

The fix is:

  • (ResolvExpr/ResolveTypeof, ResolvExpr/Resolver) Resolve the dimension expressions, where they were missed.
  • (ResolvExpr/Resolver) Prevent dimension expressions that are bound to other parameters from escaping in the function's type, to where they are out of scope. In the f example above, redact the type shown to callers from void (*)(int, int, float[m][n]) to void (*)(int, int, float[][*]).
  • (ResolvExpr/Unify) Relax the matching rules for such a type, when used at a call site, letting the patameters wildcard type match with the concrete type in scope at the caller's side.
  • (Validate/ReplaceTypedef) Apply the former, stricter matching rules to the one place where they are still needed: detecting inconsistent typedefs.
  • Property mode set to 100644
File size: 22.6 KB
RevLine 
[a32b204]1//
2// Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6//
[c92bdcc]7// Unify.cpp --
[a32b204]8//
9// Author : Richard C. Bilson
10// Created On : Sun May 17 12:27:10 2015
[07de76b]11// Last Modified By : Peter A. Buhr
12// Last Modified On : Fri Dec 13 23:43:05 2019
13// Update Count : 46
[a32b204]14//
[51b73452]15
[c92bdcc]16#include "Unify.hpp"
[f474e91]17
[d76c588]18#include <cassert> // for assertf, assert
19#include <iterator> // for back_insert_iterator, back_inserter
20#include <map> // for _Rb_tree_const_iterator, _Rb_tree_i...
21#include <memory> // for unique_ptr
22#include <set> // for set
23#include <string> // for string, operator==, operator!=, bas...
24#include <utility> // for pair, move
[54e41b3]25#include <vector>
[51b73452]26
[2890212]27#include "AST/Copy.hpp"
[f474e91]28#include "AST/Decl.hpp"
[54e41b3]29#include "AST/Node.hpp"
[f474e91]30#include "AST/Pass.hpp"
[2890212]31#include "AST/Print.hpp"
[54e41b3]32#include "AST/Type.hpp"
[d76c588]33#include "AST/TypeEnvironment.hpp"
[c92bdcc]34#include "Common/Eval.hpp" // for eval
[5bf3976]35#include "CommonType.hpp" // for commonType
[c92bdcc]36#include "FindOpenVars.hpp" // for findOpenVars
[5bf3976]37#include "SpecCost.hpp" // for SpecCost
[c92bdcc]38#include "Tuples/Tuples.hpp" // for isTtype
39#include "Typeops.hpp" // for flatten, occurs
[ea6332d]40
[f474e91]41namespace ast {
42 class SymbolTable;
43}
44
[1cbca6e]45// #define DEBUG
[51b73452]46
47namespace ResolvExpr {
48
[13de4478]49bool typesCompatible(
50 const ast::Type * first, const ast::Type * second,
51 const ast::TypeEnvironment & env ) {
52 ast::TypeEnvironment newEnv;
53 ast::OpenVarSet open, closed;
54 ast::AssertionSet need, have;
55
56 ast::ptr<ast::Type> newFirst( first ), newSecond( second );
57 env.apply( newFirst );
58 env.apply( newSecond );
59
60 // findOpenVars( newFirst, open, closed, need, have, FirstClosed );
61 findOpenVars( newSecond, open, closed, need, have, newEnv, FirstOpen );
62
63 return unifyExact(newFirst, newSecond, newEnv, need, have, open, noWiden() );
64}
65
66bool typesCompatibleIgnoreQualifiers(
67 const ast::Type * first, const ast::Type * second,
68 const ast::TypeEnvironment & env ) {
69 ast::TypeEnvironment newEnv;
70 ast::OpenVarSet open;
71 ast::AssertionSet need, have;
72
73 ast::Type * newFirst = shallowCopy( first );
74 ast::Type * newSecond = shallowCopy( second );
75
76 newFirst ->qualifiers = {};
77 newSecond->qualifiers = {};
78 ast::ptr< ast::Type > t1_(newFirst );
79 ast::ptr< ast::Type > t2_(newSecond);
80
81 ast::ptr< ast::Type > subFirst = env.apply(newFirst).node;
82 ast::ptr< ast::Type > subSecond = env.apply(newSecond).node;
83
84 return unifyExact(
85 subFirst,
86 subSecond,
87 newEnv, need, have, open, noWiden() );
88}
89
90namespace {
91 /// Replaces ttype variables with their bound types.
92 /// If this isn't done when satifying ttype assertions, then argument lists can have
93 /// different size and structure when they should be compatible.
94 struct TtypeExpander : public ast::WithShortCircuiting, public ast::PureVisitor {
95 ast::TypeEnvironment & tenv;
96
97 TtypeExpander( ast::TypeEnvironment & env ) : tenv( env ) {}
98
99 const ast::Type * postvisit( const ast::TypeInstType * typeInst ) {
100 if ( const ast::EqvClass * clz = tenv.lookup( *typeInst ) ) {
101 // expand ttype parameter into its actual type
102 if ( clz->data.kind == ast::TypeDecl::Ttype && clz->bound ) {
103 return clz->bound;
[ef1da0e2]104 }
105 }
[13de4478]106 return typeInst;
107 }
108 };
109}
[0bd46fd]110
[13de4478]111std::vector< ast::ptr< ast::Type > > flattenList(
112 const std::vector< ast::ptr< ast::Type > > & src, ast::TypeEnvironment & env
113) {
114 std::vector< ast::ptr< ast::Type > > dst;
115 dst.reserve( src.size() );
116 for ( const auto & d : src ) {
117 ast::Pass<TtypeExpander> expander( env );
118 // TtypeExpander pass is impure (may mutate nodes in place)
119 // need to make nodes shared to prevent accidental mutation
120 ast::ptr<ast::Type> dc = d->accept(expander);
121 auto types = flatten( dc );
122 for ( ast::ptr< ast::Type > & t : types ) {
123 // outermost const, volatile, _Atomic qualifiers in parameters should not play
124 // a role in the unification of function types, since they do not determine
125 // whether a function is callable.
126 // NOTE: **must** consider at least mutex qualifier, since functions can be
127 // overloaded on outermost mutex and a mutex function has different
128 // requirements than a non-mutex function
129 remove_qualifiers( t, ast::CV::Const | ast::CV::Volatile | ast::CV::Atomic );
130 dst.emplace_back( t );
[ef1da0e2]131 }
132 }
[13de4478]133 return dst;
134}
[ef1da0e2]135
[13de4478]136// Unification of Expressions
137//
138// Boolean outcome (obvious): Are they basically spelled the same?
139// Side effect of binding variables (subtle): if `sizeof(int)` ===_expr `sizeof(T)` then `int` ===_ty `T`
140//
141// Context: if `float[VAREXPR1]` ===_ty `float[VAREXPR2]` then `VAREXPR1` ===_expr `VAREXPR2`
142// where the VAREXPR are meant as notational metavariables representing the fact that unification always
143// sees distinct ast::VariableExpr objects at these positions
[f02f546]144
[13de4478]145static bool unify( const ast::Expr * e1, const ast::Expr * e2, ast::TypeEnvironment & env,
146 ast::AssertionSet & need, ast::AssertionSet & have, const ast::OpenVarSet & open,
147 WidenMode widen );
[f02f546]148
[13de4478]149class UnifyExpr final : public ast::WithShortCircuiting {
150 const ast::Expr * e2;
151 ast::TypeEnvironment & tenv;
152 ast::AssertionSet & need;
153 ast::AssertionSet & have;
154 const ast::OpenVarSet & open;
155 WidenMode widen;
156public:
157 bool result;
[f02f546]158
[13de4478]159private:
[f02f546]160
[13de4478]161 void tryMatchOnStaticValue( const ast::Expr * e1 ) {
162 Evaluation r1 = eval(e1);
163 Evaluation r2 = eval(e2);
[f02f546]164
[13de4478]165 if ( !r1.hasKnownValue ) return;
166 if ( !r2.hasKnownValue ) return;
[f02f546]167
[13de4478]168 if ( r1.knownValue != r2.knownValue ) return;
[f02f546]169
[13de4478]170 visit_children = false;
171 result = true;
172 }
[f02f546]173
[13de4478]174public:
[f02f546]175
[13de4478]176 void previsit( const ast::Node * ) { assert(false); }
[f02f546]177
[13de4478]178 void previsit( const ast::Expr * e1 ) {
179 tryMatchOnStaticValue( e1 );
180 visit_children = false;
181 }
[f02f546]182
[13de4478]183 void previsit( const ast::CastExpr * e1 ) {
184 tryMatchOnStaticValue( e1 );
[f02f546]185
[13de4478]186 if ( result ) {
187 assert( visit_children == false );
188 } else {
189 assert( visit_children == true );
190 visit_children = false;
[f02f546]191
[13de4478]192 auto e2c = dynamic_cast< const ast::CastExpr * >( e2 );
193 if ( !e2c ) return;
[f02f546]194
[13de4478]195 // inspect casts' target types
196 if ( !unifyExact(
197 e1->result, e2c->result, tenv, need, have, open, widen ) ) return;
[f02f546]198
[13de4478]199 // inspect casts' inner expressions
200 result = unify( e1->arg, e2c->arg, tenv, need, have, open, widen );
[f02f546]201 }
[13de4478]202 }
[f02f546]203
[13de4478]204 void previsit( const ast::VariableExpr * e1 ) {
205 tryMatchOnStaticValue( e1 );
[f02f546]206
[13de4478]207 if ( result ) {
208 assert( visit_children == false );
209 } else {
210 assert( visit_children == true );
211 visit_children = false;
[f02f546]212
[13de4478]213 auto e2v = dynamic_cast< const ast::VariableExpr * >( e2 );
214 if ( !e2v ) return;
[f02f546]215
[13de4478]216 assert(e1->var);
217 assert(e2v->var);
[f02f546]218
[13de4478]219 // conservative: variable exprs match if their declarations are represented by the same C++ AST object
220 result = (e1->var == e2v->var);
[f02f546]221 }
[13de4478]222 }
[f02f546]223
[13de4478]224 void previsit( const ast::SizeofExpr * e1 ) {
225 tryMatchOnStaticValue( e1 );
[f02f546]226
[13de4478]227 if ( result ) {
228 assert( visit_children == false );
229 } else {
230 assert( visit_children == true );
231 visit_children = false;
[f02f546]232
[13de4478]233 auto e2so = dynamic_cast< const ast::SizeofExpr * >( e2 );
234 if ( !e2so ) return;
[f02f546]235
[13de4478]236 // expression unification calls type unification (mutual recursion)
237 result = unifyExact( e1->type, e2so->type, tenv, need, have, open, widen );
[f02f546]238 }
[13de4478]239 }
[f02f546]240
[13de4478]241 UnifyExpr( const ast::Expr * e2, ast::TypeEnvironment & env, ast::AssertionSet & need,
242 ast::AssertionSet & have, const ast::OpenVarSet & open, WidenMode widen )
243 : e2( e2 ), tenv(env), need(need), have(have), open(open), widen(widen), result(false) {}
244};
[f02f546]245
[13de4478]246static bool unify( const ast::Expr * e1, const ast::Expr * e2, ast::TypeEnvironment & env,
247 ast::AssertionSet & need, ast::AssertionSet & have, const ast::OpenVarSet & open,
248 WidenMode widen ) {
249 assert( e1 && e2 );
250 return ast::Pass<UnifyExpr>::read( e1, e2, env, need, have, open, widen );
251}
252
253class Unify final : public ast::WithShortCircuiting {
254 const ast::Type * type2;
255 ast::TypeEnvironment & tenv;
256 ast::AssertionSet & need;
257 ast::AssertionSet & have;
258 const ast::OpenVarSet & open;
259 WidenMode widen;
260public:
261 static size_t traceId;
262 bool result;
263
264 Unify(
265 const ast::Type * type2, ast::TypeEnvironment & env, ast::AssertionSet & need,
266 ast::AssertionSet & have, const ast::OpenVarSet & open, WidenMode widen )
267 : type2(type2), tenv(env), need(need), have(have), open(open), widen(widen),
268 result(false) {}
269
270 void previsit( const ast::Node * ) { visit_children = false; }
271
[822332e]272 void postvisit( const ast::VoidType * ) {
273 result = dynamic_cast< const ast::VoidType * >( type2 );
[f02f546]274 }
275
[13de4478]276 void postvisit( const ast::BasicType * basic ) {
277 if ( auto basic2 = dynamic_cast< const ast::BasicType * >( type2 ) ) {
278 result = basic->kind == basic2->kind;
[f474e91]279 }
[13de4478]280 }
[f474e91]281
[13de4478]282 void postvisit( const ast::PointerType * pointer ) {
283 if ( auto pointer2 = dynamic_cast< const ast::PointerType * >( type2 ) ) {
284 result = unifyExact(
285 pointer->base, pointer2->base, tenv, need, have, open,
286 noWiden());
[f474e91]287 }
[13de4478]288 }
[f474e91]289
[13de4478]290 void postvisit( const ast::ArrayType * array ) {
291 auto array2 = dynamic_cast< const ast::ArrayType * >( type2 );
292 if ( !array2 ) return;
[f474e91]293
[81e768d]294 // Permit cases where one side has a dimension or isVarLen,
295 // while the other side is the opposite.
296 // Acheves a wildcard-iterpretation semantics, where lack of
297 // dimension (`float a[]` or `float a[25][*]`) means
298 // "anything here is fine."
299 // Sole known case where a verbatim-match semantics is intended
300 // is typedef redefinition, for which extra checking is added
301 // in src/Validate/ReplaceTypedef.cpp.
302
303 if ( array->dimension && array2->dimension ) {
[13de4478]304 assert( array2->dimension );
305 // type unification calls expression unification (mutual recursion)
306 if ( !unify(array->dimension, array2->dimension,
307 tenv, need, have, open, widen) ) return;
308 }
[f474e91]309
[13de4478]310 result = unifyExact(
[acb33f15]311 array->base, array2->base, tenv, need, have, open, noWiden());
[13de4478]312 }
[f474e91]313
[13de4478]314 void postvisit( const ast::ReferenceType * ref ) {
315 if ( auto ref2 = dynamic_cast< const ast::ReferenceType * >( type2 ) ) {
[7870799]316 result = unifyExact(
[13de4478]317 ref->base, ref2->base, tenv, need, have, open, noWiden());
[f474e91]318 }
[13de4478]319 }
[f474e91]320
[13de4478]321private:
[f474e91]322
[13de4478]323 template< typename Iter >
324 static bool unifyTypeList(
325 Iter crnt1, Iter end1, Iter crnt2, Iter end2, ast::TypeEnvironment & env,
326 ast::AssertionSet & need, ast::AssertionSet & have, const ast::OpenVarSet & open
327 ) {
328 while ( crnt1 != end1 && crnt2 != end2 ) {
329 const ast::Type * t1 = *crnt1;
330 const ast::Type * t2 = *crnt2;
331 bool isTuple1 = Tuples::isTtype( t1 );
332 bool isTuple2 = Tuples::isTtype( t2 );
333
334 // assumes here that ttype *must* be last parameter
335 if ( isTuple1 && !isTuple2 ) {
336 // combine remainder of list2, then unify
[7870799]337 return unifyExact(
[954c954]338 t1, tupleFromTypes( crnt2, end2 ), env, need, have, open,
[251ce80]339 noWiden() );
[13de4478]340 } else if ( !isTuple1 && isTuple2 ) {
341 // combine remainder of list1, then unify
[7870799]342 return unifyExact(
[954c954]343 tupleFromTypes( crnt1, end1 ), t2, env, need, have, open,
[251ce80]344 noWiden() );
[f474e91]345 }
346
[13de4478]347 if ( !unifyExact(
348 t1, t2, env, need, have, open, noWiden() )
349 ) return false;
350
351 ++crnt1; ++crnt2;
[f474e91]352 }
353
[13de4478]354 // May get to the end of one argument list before the other. This is only okay if the
355 // other is a ttype
356 if ( crnt1 != end1 ) {
357 // try unifying empty tuple with ttype
358 const ast::Type * t1 = *crnt1;
359 if ( !Tuples::isTtype( t1 ) ) return false;
360 return unifyExact(
361 t1, tupleFromTypes( crnt2, end2 ), env, need, have, open,
362 noWiden() );
363 } else if ( crnt2 != end2 ) {
364 // try unifying empty tuple with ttype
365 const ast::Type * t2 = *crnt2;
366 if ( !Tuples::isTtype( t2 ) ) return false;
367 return unifyExact(
368 tupleFromTypes( crnt1, end1 ), t2, env, need, have, open,
369 noWiden() );
[f474e91]370 }
371
[13de4478]372 return true;
373 }
374
375 static bool unifyTypeList(
376 const std::vector< ast::ptr< ast::Type > > & list1,
377 const std::vector< ast::ptr< ast::Type > > & list2,
378 ast::TypeEnvironment & env, ast::AssertionSet & need, ast::AssertionSet & have,
379 const ast::OpenVarSet & open
380 ) {
381 return unifyTypeList(
382 list1.begin(), list1.end(), list2.begin(), list2.end(), env, need, have, open);
383 }
384
385 static void markAssertionSet( ast::AssertionSet & assns, const ast::VariableExpr * assn ) {
386 auto i = assns.find( assn );
387 if ( i != assns.end() ) {
388 i->second.isUsed = true;
[f474e91]389 }
[13de4478]390 }
[f474e91]391
[13de4478]392 /// mark all assertions in `type` used in both `assn1` and `assn2`
393 static void markAssertions(
394 ast::AssertionSet & assn1, ast::AssertionSet & assn2,
395 const ast::FunctionType * type
396 ) {
397 for ( auto & assert : type->assertions ) {
398 markAssertionSet( assn1, assert );
399 markAssertionSet( assn2, assert );
[f474e91]400 }
[13de4478]401 }
[f474e91]402
[13de4478]403public:
404 void postvisit( const ast::FunctionType * func ) {
405 auto func2 = dynamic_cast< const ast::FunctionType * >( type2 );
406 if ( !func2 ) return;
[f474e91]407
[13de4478]408 if ( func->isVarArgs != func2->isVarArgs ) return;
[7870799]409
[13de4478]410 // Flatten the parameter lists for both functions so that tuple structure does not
411 // affect unification. Does not actually mutate function parameters.
412 auto params = flattenList( func->params, tenv );
413 auto params2 = flattenList( func2->params, tenv );
[f474e91]414
[13de4478]415 // sizes don't have to match if ttypes are involved; need to be more precise w.r.t.
416 // where the ttype is to prevent errors
417 if (
418 ( params.size() != params2.size() || func->returns.size() != func2->returns.size() )
419 && !func->isTtype()
420 && !func2->isTtype()
421 ) return;
[f474e91]422
[13de4478]423 if ( !unifyTypeList( params, params2, tenv, need, have, open ) ) return;
424 if ( !unifyTypeList(
425 func->returns, func2->returns, tenv, need, have, open ) ) return;
[7870799]426
[13de4478]427 markAssertions( have, need, func );
428 markAssertions( have, need, func2 );
[f474e91]429
[13de4478]430 result = true;
431 }
[7870799]432
[13de4478]433private:
434 // Returns: other, cast as XInstType
435 // Assigns this->result: whether types are compatible (up to generic parameters)
436 template< typename XInstType >
437 const XInstType * handleRefType( const XInstType * inst, const ast::Type * other ) {
438 // check that the other type is compatible and named the same
439 auto otherInst = dynamic_cast< const XInstType * >( other );
440 if ( otherInst && inst->name == otherInst->name ) {
441 this->result = otherInst;
442 }
443 return otherInst;
444 }
[f474e91]445
[13de4478]446 /// Creates a tuple type based on a list of TypeExpr
447 template< typename Iter >
448 static const ast::Type * tupleFromExprs(
449 const ast::TypeExpr * param, Iter & crnt, Iter end, ast::CV::Qualifiers qs
450 ) {
451 std::vector< ast::ptr< ast::Type > > types;
452 do {
453 types.emplace_back( param->type );
[f474e91]454
[13de4478]455 ++crnt;
456 if ( crnt == end ) break;
457 param = strict_dynamic_cast< const ast::TypeExpr * >( crnt->get() );
458 } while(true);
[f474e91]459
[13de4478]460 return new ast::TupleType( std::move(types), qs );
461 }
[f474e91]462
[13de4478]463 template< typename XInstType >
464 void handleGenericRefType( const XInstType * inst, const ast::Type * other ) {
465 // check that other type is compatible and named the same
466 const XInstType * otherInst = handleRefType( inst, other );
467 if ( !this->result ) return;
468
469 // check that parameters of types unify, if any
470 const std::vector< ast::ptr< ast::Expr > > & params = inst->params;
471 const std::vector< ast::ptr< ast::Expr > > & params2 = otherInst->params;
472
473 auto it = params.begin();
474 auto jt = params2.begin();
475 for ( ; it != params.end() && jt != params2.end(); ++it, ++jt ) {
476 auto param = strict_dynamic_cast< const ast::TypeExpr * >( it->get() );
477 auto param2 = strict_dynamic_cast< const ast::TypeExpr * >( jt->get() );
478
479 ast::ptr< ast::Type > pty = param->type;
480 ast::ptr< ast::Type > pty2 = param2->type;
481
482 bool isTuple = Tuples::isTtype( pty );
483 bool isTuple2 = Tuples::isTtype( pty2 );
484
485 if ( isTuple && isTuple2 ) {
486 ++it; ++jt; // skip ttype parameters before break
487 } else if ( isTuple ) {
488 // bundle remaining params into tuple
489 pty2 = tupleFromExprs( param2, jt, params2.end(), pty->qualifiers );
490 ++it; // skip ttype parameter for break
491 } else if ( isTuple2 ) {
492 // bundle remaining params into tuple
493 pty = tupleFromExprs( param, it, params.end(), pty2->qualifiers );
494 ++jt; // skip ttype parameter for break
[f474e91]495 }
496
[13de4478]497 if ( !unifyExact(
498 pty, pty2, tenv, need, have, open, noWiden() ) ) {
499 result = false;
500 return;
501 }
[f474e91]502
[13de4478]503 // ttype parameter should be last
504 if ( isTuple || isTuple2 ) break;
[f474e91]505 }
[13de4478]506 result = it == params.end() && jt == params2.end();
507 }
[f474e91]508
[13de4478]509public:
510 void postvisit( const ast::StructInstType * aggrType ) {
511 handleGenericRefType( aggrType, type2 );
512 }
[f474e91]513
[13de4478]514 void postvisit( const ast::UnionInstType * aggrType ) {
515 handleGenericRefType( aggrType, type2 );
516 }
[0522ebe]517
[13de4478]518 void postvisit( const ast::EnumInstType * aggrType ) {
519 handleRefType( aggrType, type2 );
520 }
[f474e91]521
[13de4478]522 void postvisit( const ast::TraitInstType * aggrType ) {
523 handleRefType( aggrType, type2 );
524 }
[f474e91]525
[13de4478]526 void postvisit( const ast::TypeInstType * typeInst ) {
527 // assert( open.find( *typeInst ) == open.end() );
528 auto otherInst = dynamic_cast< const ast::TypeInstType * >( type2 );
529 if ( otherInst && typeInst->name == otherInst->name ) {
530 this->result = otherInst;
531 }
532 }
[f474e91]533
[13de4478]534private:
535 /// Creates a tuple type based on a list of Type
536 static bool unifyList(
537 const std::vector< ast::ptr< ast::Type > > & list1,
538 const std::vector< ast::ptr< ast::Type > > & list2, ast::TypeEnvironment & env,
539 ast::AssertionSet & need, ast::AssertionSet & have, const ast::OpenVarSet & open
540 ) {
541 auto crnt1 = list1.begin();
542 auto crnt2 = list2.begin();
543 while ( crnt1 != list1.end() && crnt2 != list2.end() ) {
544 const ast::Type * t1 = *crnt1;
545 const ast::Type * t2 = *crnt2;
546 bool isTuple1 = Tuples::isTtype( t1 );
547 bool isTuple2 = Tuples::isTtype( t2 );
548
549 // assumes ttype must be last parameter
550 if ( isTuple1 && !isTuple2 ) {
551 // combine entirety of list2, then unify
[7870799]552 return unifyExact(
[13de4478]553 t1, tupleFromTypes( list2 ), env, need, have, open,
554 noWiden() );
555 } else if ( !isTuple1 && isTuple2 ) {
556 // combine entirety of list1, then unify
[f474e91]557 return unifyExact(
[13de4478]558 tupleFromTypes( list1 ), t2, env, need, have, open,
559 noWiden() );
[f474e91]560 }
561
[13de4478]562 if ( !unifyExact(
563 t1, t2, env, need, have, open, noWiden() )
564 ) return false;
[f474e91]565
[13de4478]566 ++crnt1; ++crnt2;
567 }
[ef9988b]568
[13de4478]569 if ( crnt1 != list1.end() ) {
570 // try unifying empty tuple type with ttype
571 const ast::Type * t1 = *crnt1;
572 if ( !Tuples::isTtype( t1 ) ) return false;
573 // xxx - this doesn't generate an empty tuple, contrary to comment; both ported
574 // from Rob's code
575 return unifyExact(
576 t1, tupleFromTypes( list2 ), env, need, have, open,
577 noWiden() );
578 } else if ( crnt2 != list2.end() ) {
579 // try unifying empty tuple with ttype
580 const ast::Type * t2 = *crnt2;
581 if ( !Tuples::isTtype( t2 ) ) return false;
582 // xxx - this doesn't generate an empty tuple, contrary to comment; both ported
583 // from Rob's code
584 return unifyExact(
585 tupleFromTypes( list1 ), t2, env, need, have, open,
586 noWiden() );
587 }
[f474e91]588
[13de4478]589 return true;
590 }
[f474e91]591
[13de4478]592public:
593 void postvisit( const ast::TupleType * tuple ) {
594 auto tuple2 = dynamic_cast< const ast::TupleType * >( type2 );
595 if ( ! tuple2 ) return;
[f474e91]596
[13de4478]597 ast::Pass<TtypeExpander> expander{ tenv };
[f474e91]598
[13de4478]599 const ast::Type * flat = tuple->accept( expander );
600 const ast::Type * flat2 = tuple2->accept( expander );
[f474e91]601
[13de4478]602 auto types = flatten( flat );
603 auto types2 = flatten( flat2 );
[f474e91]604
[acb33f15]605 result = unifyList( types, types2, tenv, need, have, open );
[13de4478]606 }
[0bd3faf]607
[822332e]608 void postvisit( const ast::VarArgsType * ) {
[acb33f15]609 result = dynamic_cast< const ast::VarArgsType * >( type2 );
[2773ab8]610 }
611
[822332e]612 void postvisit( const ast::ZeroType * ) {
[acb33f15]613 result = dynamic_cast< const ast::ZeroType * >( type2 );
[ee574a2]614 }
615
[822332e]616 void postvisit( const ast::OneType * ) {
[acb33f15]617 result = dynamic_cast< const ast::OneType * >( type2 );
[f474e91]618 }
[13de4478]619};
[f474e91]620
[13de4478]621// size_t Unify::traceId = Stats::Heap::new_stacktrace_id("Unify");
622
623bool unify(
624 const ast::ptr<ast::Type> & type1, const ast::ptr<ast::Type> & type2,
625 ast::TypeEnvironment & env, ast::AssertionSet & need, ast::AssertionSet & have,
626 ast::OpenVarSet & open
627) {
628 ast::ptr<ast::Type> common;
629 return unify( type1, type2, env, need, have, open, common );
630}
631
632bool unify(
633 const ast::ptr<ast::Type> & type1, const ast::ptr<ast::Type> & type2,
634 ast::TypeEnvironment & env, ast::AssertionSet & need, ast::AssertionSet & have,
635 ast::OpenVarSet & open, ast::ptr<ast::Type> & common
636) {
637 ast::OpenVarSet closed;
638 // findOpenVars( type1, open, closed, need, have, FirstClosed );
639 findOpenVars( type2, open, closed, need, have, env, FirstOpen );
640 return unifyInexact(
641 type1, type2, env, need, have, open, WidenMode{ true, true }, common );
642}
643
644bool unifyExact(
645 const ast::Type * type1, const ast::Type * type2, ast::TypeEnvironment & env,
646 ast::AssertionSet & need, ast::AssertionSet & have, const ast::OpenVarSet & open,
647 WidenMode widen
648) {
649 if ( type1->qualifiers != type2->qualifiers ) return false;
650
651 auto var1 = dynamic_cast< const ast::TypeInstType * >( type1 );
652 auto var2 = dynamic_cast< const ast::TypeInstType * >( type2 );
653 bool isopen1 = var1 && env.lookup(*var1);
654 bool isopen2 = var2 && env.lookup(*var2);
655
656 if ( isopen1 && isopen2 ) {
657 if ( var1->base->kind != var2->base->kind ) return false;
658 return env.bindVarToVar(
659 var1, var2, ast::TypeData{ var1->base->kind, var1->base->sized||var2->base->sized }, need, have,
660 open, widen );
661 } else if ( isopen1 ) {
662 return env.bindVar( var1, type2, ast::TypeData{var1->base}, need, have, open, widen );
663 } else if ( isopen2 ) {
664 return env.bindVar( var2, type1, ast::TypeData{var2->base}, need, have, open, widen );
665 } else {
666 return ast::Pass<Unify>::read(
667 type1, type2, env, need, have, open, widen );
668 }
669}
[f474e91]670
[13de4478]671bool unifyInexact(
672 const ast::ptr<ast::Type> & type1, const ast::ptr<ast::Type> & type2,
673 ast::TypeEnvironment & env, ast::AssertionSet & need, ast::AssertionSet & have,
674 const ast::OpenVarSet & open, WidenMode widen,
675 ast::ptr<ast::Type> & common
676) {
677 ast::CV::Qualifiers q1 = type1->qualifiers, q2 = type2->qualifiers;
678
679 // force t1 and t2 to be cloned if their qualifiers must be stripped, so that type1 and
680 // type2 are left unchanged; calling convention forces type{1,2}->strong_ref >= 1
681 ast::Type * t1 = shallowCopy(type1.get());
682 ast::Type * t2 = shallowCopy(type2.get());
683 t1->qualifiers = {};
684 t2->qualifiers = {};
685 ast::ptr< ast::Type > t1_(t1);
686 ast::ptr< ast::Type > t2_(t2);
687
688 if ( unifyExact( t1, t2, env, need, have, open, widen ) ) {
689 // if exact unification on unqualified types, try to merge qualifiers
690 if ( q1 == q2 || ( ( q1 > q2 || widen.first ) && ( q2 > q1 || widen.second ) ) ) {
691 t1->qualifiers = q1 | q2;
692 common = t1;
[f474e91]693 return true;
694 } else {
695 return false;
696 }
[13de4478]697 } else if (( common = commonType( t1, t2, env, need, have, open, widen ))) {
698 // no exact unification, but common type
699 auto c = shallowCopy(common.get());
700 c->qualifiers = q1 | q2;
701 common = c;
702 return true;
703 } else {
704 return false;
[f474e91]705 }
[13de4478]706}
[f474e91]707
[13de4478]708ast::ptr<ast::Type> extractResultType( const ast::FunctionType * func ) {
709 if ( func->returns.empty() ) return new ast::VoidType();
710 if ( func->returns.size() == 1 ) return func->returns[0];
[4139e3d]711
[13de4478]712 std::vector<ast::ptr<ast::Type>> tys;
713 for ( const auto & decl : func->returns ) {
714 tys.emplace_back( decl );
[54e41b3]715 }
[13de4478]716 return new ast::TupleType( std::move(tys) );
717}
718
[51b73452]719} // namespace ResolvExpr
[a32b204]720
721// Local Variables: //
722// tab-width: 4 //
723// mode: c++ //
724// compile-command: "make install" //
725// End: //
Note: See TracBrowser for help on using the repository browser.