| 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 : Fri Dec 13 23:43:05 2019
|
|---|
| 13 | // Update Count : 46
|
|---|
| 14 | //
|
|---|
| 15 |
|
|---|
| 16 | #include "Unify.h"
|
|---|
| 17 |
|
|---|
| 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
|
|---|
| 25 | #include <vector>
|
|---|
| 26 |
|
|---|
| 27 | #include "AST/Copy.hpp"
|
|---|
| 28 | #include "AST/Decl.hpp"
|
|---|
| 29 | #include "AST/Node.hpp"
|
|---|
| 30 | #include "AST/Pass.hpp"
|
|---|
| 31 | #include "AST/Print.hpp"
|
|---|
| 32 | #include "AST/Type.hpp"
|
|---|
| 33 | #include "AST/TypeEnvironment.hpp"
|
|---|
| 34 | #include "Common/Eval.h" // for eval
|
|---|
| 35 | #include "CommonType.hpp" // for commonType
|
|---|
| 36 | #include "FindOpenVars.h" // for findOpenVars
|
|---|
| 37 | #include "SpecCost.hpp" // for SpecCost
|
|---|
| 38 | #include "Tuples/Tuples.h" // for isTtype
|
|---|
| 39 | #include "typeops.h" // for flatten, occurs
|
|---|
| 40 |
|
|---|
| 41 | namespace ast {
|
|---|
| 42 | class SymbolTable;
|
|---|
| 43 | }
|
|---|
| 44 |
|
|---|
| 45 | // #define DEBUG
|
|---|
| 46 |
|
|---|
| 47 | namespace ResolvExpr {
|
|---|
| 48 |
|
|---|
| 49 | bool 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 |
|
|---|
| 66 | bool 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 |
|
|---|
| 90 | namespace {
|
|---|
| 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;
|
|---|
| 104 | }
|
|---|
| 105 | }
|
|---|
| 106 | return typeInst;
|
|---|
| 107 | }
|
|---|
| 108 | };
|
|---|
| 109 | }
|
|---|
| 110 |
|
|---|
| 111 | std::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 );
|
|---|
| 131 | }
|
|---|
| 132 | }
|
|---|
| 133 | return dst;
|
|---|
| 134 | }
|
|---|
| 135 |
|
|---|
| 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
|
|---|
| 144 |
|
|---|
| 145 | static 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 );
|
|---|
| 148 |
|
|---|
| 149 | class 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;
|
|---|
| 156 | public:
|
|---|
| 157 | bool result;
|
|---|
| 158 |
|
|---|
| 159 | private:
|
|---|
| 160 |
|
|---|
| 161 | void tryMatchOnStaticValue( const ast::Expr * e1 ) {
|
|---|
| 162 | Evaluation r1 = eval(e1);
|
|---|
| 163 | Evaluation r2 = eval(e2);
|
|---|
| 164 |
|
|---|
| 165 | if ( ! r1.hasKnownValue ) return;
|
|---|
| 166 | if ( ! r2.hasKnownValue ) return;
|
|---|
| 167 |
|
|---|
| 168 | if (r1.knownValue != r2.knownValue) return;
|
|---|
| 169 |
|
|---|
| 170 | visit_children = false;
|
|---|
| 171 | result = true;
|
|---|
| 172 | }
|
|---|
| 173 |
|
|---|
| 174 | public:
|
|---|
| 175 |
|
|---|
| 176 | void previsit( const ast::Node * ) { assert(false); }
|
|---|
| 177 |
|
|---|
| 178 | void previsit( const ast::Expr * e1 ) {
|
|---|
| 179 | tryMatchOnStaticValue( e1 );
|
|---|
| 180 | visit_children = false;
|
|---|
| 181 | }
|
|---|
| 182 |
|
|---|
| 183 | void previsit( const ast::CastExpr * e1 ) {
|
|---|
| 184 | tryMatchOnStaticValue( e1 );
|
|---|
| 185 |
|
|---|
| 186 | if (result) {
|
|---|
| 187 | assert (visit_children == false);
|
|---|
| 188 | } else {
|
|---|
| 189 | assert (visit_children == true);
|
|---|
| 190 | visit_children = false;
|
|---|
| 191 |
|
|---|
| 192 | auto e2c = dynamic_cast< const ast::CastExpr * >( e2 );
|
|---|
| 193 | if ( ! e2c ) return;
|
|---|
| 194 |
|
|---|
| 195 | // inspect casts' target types
|
|---|
| 196 | if ( ! unifyExact(
|
|---|
| 197 | e1->result, e2c->result, tenv, need, have, open, widen ) ) return;
|
|---|
| 198 |
|
|---|
| 199 | // inspect casts' inner expressions
|
|---|
| 200 | result = unify( e1->arg, e2c->arg, tenv, need, have, open, widen );
|
|---|
| 201 | }
|
|---|
| 202 | }
|
|---|
| 203 |
|
|---|
| 204 | void previsit( const ast::VariableExpr * e1 ) {
|
|---|
| 205 | tryMatchOnStaticValue( e1 );
|
|---|
| 206 |
|
|---|
| 207 | if (result) {
|
|---|
| 208 | assert (visit_children == false);
|
|---|
| 209 | } else {
|
|---|
| 210 | assert (visit_children == true);
|
|---|
| 211 | visit_children = false;
|
|---|
| 212 |
|
|---|
| 213 | auto e2v = dynamic_cast< const ast::VariableExpr * >( e2 );
|
|---|
| 214 | if ( ! e2v ) return;
|
|---|
| 215 |
|
|---|
| 216 | assert(e1->var);
|
|---|
| 217 | assert(e2v->var);
|
|---|
| 218 |
|
|---|
| 219 | // conservative: variable exprs match if their declarations are represented by the same C++ AST object
|
|---|
| 220 | result = (e1->var == e2v->var);
|
|---|
| 221 | }
|
|---|
| 222 | }
|
|---|
| 223 |
|
|---|
| 224 | void previsit( const ast::SizeofExpr * e1 ) {
|
|---|
| 225 | tryMatchOnStaticValue( e1 );
|
|---|
| 226 |
|
|---|
| 227 | if (result) {
|
|---|
| 228 | assert (visit_children == false);
|
|---|
| 229 | } else {
|
|---|
| 230 | assert (visit_children == true);
|
|---|
| 231 | visit_children = false;
|
|---|
| 232 |
|
|---|
| 233 | auto e2so = dynamic_cast< const ast::SizeofExpr * >( e2 );
|
|---|
| 234 | if ( ! e2so ) return;
|
|---|
| 235 |
|
|---|
| 236 | assert((e1->type != nullptr) ^ (e1->expr != nullptr));
|
|---|
| 237 | assert((e2so->type != nullptr) ^ (e2so->expr != nullptr));
|
|---|
| 238 | if ( ! (e1->type && e2so->type) ) return;
|
|---|
| 239 |
|
|---|
| 240 | // expression unification calls type unification (mutual recursion)
|
|---|
| 241 | result = unifyExact( e1->type, e2so->type, tenv, need, have, open, widen );
|
|---|
| 242 | }
|
|---|
| 243 | }
|
|---|
| 244 |
|
|---|
| 245 | UnifyExpr( const ast::Expr * e2, ast::TypeEnvironment & env, ast::AssertionSet & need,
|
|---|
| 246 | ast::AssertionSet & have, const ast::OpenVarSet & open, WidenMode widen )
|
|---|
| 247 | : e2( e2 ), tenv(env), need(need), have(have), open(open), widen(widen), result(false) {}
|
|---|
| 248 | };
|
|---|
| 249 |
|
|---|
| 250 | static bool unify( const ast::Expr * e1, const ast::Expr * e2, ast::TypeEnvironment & env,
|
|---|
| 251 | ast::AssertionSet & need, ast::AssertionSet & have, const ast::OpenVarSet & open,
|
|---|
| 252 | WidenMode widen ) {
|
|---|
| 253 | assert( e1 && e2 );
|
|---|
| 254 | return ast::Pass<UnifyExpr>::read( e1, e2, env, need, have, open, widen );
|
|---|
| 255 | }
|
|---|
| 256 |
|
|---|
| 257 | class Unify final : public ast::WithShortCircuiting {
|
|---|
| 258 | const ast::Type * type2;
|
|---|
| 259 | ast::TypeEnvironment & tenv;
|
|---|
| 260 | ast::AssertionSet & need;
|
|---|
| 261 | ast::AssertionSet & have;
|
|---|
| 262 | const ast::OpenVarSet & open;
|
|---|
| 263 | WidenMode widen;
|
|---|
| 264 | public:
|
|---|
| 265 | static size_t traceId;
|
|---|
| 266 | bool result;
|
|---|
| 267 |
|
|---|
| 268 | Unify(
|
|---|
| 269 | const ast::Type * type2, ast::TypeEnvironment & env, ast::AssertionSet & need,
|
|---|
| 270 | ast::AssertionSet & have, const ast::OpenVarSet & open, WidenMode widen )
|
|---|
| 271 | : type2(type2), tenv(env), need(need), have(have), open(open), widen(widen),
|
|---|
| 272 | result(false) {}
|
|---|
| 273 |
|
|---|
| 274 | void previsit( const ast::Node * ) { visit_children = false; }
|
|---|
| 275 |
|
|---|
| 276 | void postvisit( const ast::VoidType * ) {
|
|---|
| 277 | result = dynamic_cast< const ast::VoidType * >( type2 );
|
|---|
| 278 | }
|
|---|
| 279 |
|
|---|
| 280 | void postvisit( const ast::BasicType * basic ) {
|
|---|
| 281 | if ( auto basic2 = dynamic_cast< const ast::BasicType * >( type2 ) ) {
|
|---|
| 282 | result = basic->kind == basic2->kind;
|
|---|
| 283 | }
|
|---|
| 284 | }
|
|---|
| 285 |
|
|---|
| 286 | void postvisit( const ast::PointerType * pointer ) {
|
|---|
| 287 | if ( auto pointer2 = dynamic_cast< const ast::PointerType * >( type2 ) ) {
|
|---|
| 288 | result = unifyExact(
|
|---|
| 289 | pointer->base, pointer2->base, tenv, need, have, open,
|
|---|
| 290 | noWiden());
|
|---|
| 291 | }
|
|---|
| 292 | }
|
|---|
| 293 |
|
|---|
| 294 | void postvisit( const ast::ArrayType * array ) {
|
|---|
| 295 | auto array2 = dynamic_cast< const ast::ArrayType * >( type2 );
|
|---|
| 296 | if ( ! array2 ) return;
|
|---|
| 297 |
|
|---|
| 298 | if ( array->isVarLen != array2->isVarLen ) return;
|
|---|
| 299 | if ( (array->dimension != nullptr) != (array2->dimension != nullptr) ) return;
|
|---|
| 300 |
|
|---|
| 301 | if ( array->dimension ) {
|
|---|
| 302 | assert( array2->dimension );
|
|---|
| 303 | // type unification calls expression unification (mutual recursion)
|
|---|
| 304 | if ( ! unify(array->dimension, array2->dimension,
|
|---|
| 305 | tenv, need, have, open, widen) ) return;
|
|---|
| 306 | }
|
|---|
| 307 |
|
|---|
| 308 | result = unifyExact(
|
|---|
| 309 | array->base, array2->base, tenv, need, have, open, noWiden());
|
|---|
| 310 | }
|
|---|
| 311 |
|
|---|
| 312 | void postvisit( const ast::ReferenceType * ref ) {
|
|---|
| 313 | if ( auto ref2 = dynamic_cast< const ast::ReferenceType * >( type2 ) ) {
|
|---|
| 314 | result = unifyExact(
|
|---|
| 315 | ref->base, ref2->base, tenv, need, have, open, noWiden());
|
|---|
| 316 | }
|
|---|
| 317 | }
|
|---|
| 318 |
|
|---|
| 319 | private:
|
|---|
| 320 |
|
|---|
| 321 | template< typename Iter >
|
|---|
| 322 | static bool unifyTypeList(
|
|---|
| 323 | Iter crnt1, Iter end1, Iter crnt2, Iter end2, ast::TypeEnvironment & env,
|
|---|
| 324 | ast::AssertionSet & need, ast::AssertionSet & have, const ast::OpenVarSet & open
|
|---|
| 325 | ) {
|
|---|
| 326 | while ( crnt1 != end1 && crnt2 != end2 ) {
|
|---|
| 327 | const ast::Type * t1 = *crnt1;
|
|---|
| 328 | const ast::Type * t2 = *crnt2;
|
|---|
| 329 | bool isTuple1 = Tuples::isTtype( t1 );
|
|---|
| 330 | bool isTuple2 = Tuples::isTtype( t2 );
|
|---|
| 331 |
|
|---|
| 332 | // assumes here that ttype *must* be last parameter
|
|---|
| 333 | if ( isTuple1 && ! isTuple2 ) {
|
|---|
| 334 | // combine remainder of list2, then unify
|
|---|
| 335 | return unifyExact(
|
|---|
| 336 | t1, tupleFromTypes( crnt2, end2 ), env, need, have, open,
|
|---|
| 337 | noWiden() );
|
|---|
| 338 | } else if ( ! isTuple1 && isTuple2 ) {
|
|---|
| 339 | // combine remainder of list1, then unify
|
|---|
| 340 | return unifyExact(
|
|---|
| 341 | tupleFromTypes( crnt1, end1 ), t2, env, need, have, open,
|
|---|
| 342 | noWiden() );
|
|---|
| 343 | }
|
|---|
| 344 |
|
|---|
| 345 | if ( ! unifyExact(
|
|---|
| 346 | t1, t2, env, need, have, open, noWiden() )
|
|---|
| 347 | ) return false;
|
|---|
| 348 |
|
|---|
| 349 | ++crnt1; ++crnt2;
|
|---|
| 350 | }
|
|---|
| 351 |
|
|---|
| 352 | // May get to the end of one argument list before the other. This is only okay if the
|
|---|
| 353 | // other is a ttype
|
|---|
| 354 | if ( crnt1 != end1 ) {
|
|---|
| 355 | // try unifying empty tuple with ttype
|
|---|
| 356 | const ast::Type * t1 = *crnt1;
|
|---|
| 357 | if ( ! Tuples::isTtype( t1 ) ) return false;
|
|---|
| 358 | return unifyExact(
|
|---|
| 359 | t1, tupleFromTypes( crnt2, end2 ), env, need, have, open,
|
|---|
| 360 | noWiden() );
|
|---|
| 361 | } else if ( crnt2 != end2 ) {
|
|---|
| 362 | // try unifying empty tuple with ttype
|
|---|
| 363 | const ast::Type * t2 = *crnt2;
|
|---|
| 364 | if ( ! Tuples::isTtype( t2 ) ) return false;
|
|---|
| 365 | return unifyExact(
|
|---|
| 366 | tupleFromTypes( crnt1, end1 ), t2, env, need, have, open,
|
|---|
| 367 | noWiden() );
|
|---|
| 368 | }
|
|---|
| 369 |
|
|---|
| 370 | return true;
|
|---|
| 371 | }
|
|---|
| 372 |
|
|---|
| 373 | static bool unifyTypeList(
|
|---|
| 374 | const std::vector< ast::ptr< ast::Type > > & list1,
|
|---|
| 375 | const std::vector< ast::ptr< ast::Type > > & list2,
|
|---|
| 376 | ast::TypeEnvironment & env, ast::AssertionSet & need, ast::AssertionSet & have,
|
|---|
| 377 | const ast::OpenVarSet & open
|
|---|
| 378 | ) {
|
|---|
| 379 | return unifyTypeList(
|
|---|
| 380 | list1.begin(), list1.end(), list2.begin(), list2.end(), env, need, have, open);
|
|---|
| 381 | }
|
|---|
| 382 |
|
|---|
| 383 | static void markAssertionSet( ast::AssertionSet & assns, const ast::VariableExpr * assn ) {
|
|---|
| 384 | auto i = assns.find( assn );
|
|---|
| 385 | if ( i != assns.end() ) {
|
|---|
| 386 | i->second.isUsed = true;
|
|---|
| 387 | }
|
|---|
| 388 | }
|
|---|
| 389 |
|
|---|
| 390 | /// mark all assertions in `type` used in both `assn1` and `assn2`
|
|---|
| 391 | static void markAssertions(
|
|---|
| 392 | ast::AssertionSet & assn1, ast::AssertionSet & assn2,
|
|---|
| 393 | const ast::FunctionType * type
|
|---|
| 394 | ) {
|
|---|
| 395 | for ( auto & assert : type->assertions ) {
|
|---|
| 396 | markAssertionSet( assn1, assert );
|
|---|
| 397 | markAssertionSet( assn2, assert );
|
|---|
| 398 | }
|
|---|
| 399 | }
|
|---|
| 400 |
|
|---|
| 401 | public:
|
|---|
| 402 | void postvisit( const ast::FunctionType * func ) {
|
|---|
| 403 | auto func2 = dynamic_cast< const ast::FunctionType * >( type2 );
|
|---|
| 404 | if ( ! func2 ) return;
|
|---|
| 405 |
|
|---|
| 406 | if ( func->isVarArgs != func2->isVarArgs ) return;
|
|---|
| 407 |
|
|---|
| 408 | // Flatten the parameter lists for both functions so that tuple structure does not
|
|---|
| 409 | // affect unification. Does not actually mutate function parameters.
|
|---|
| 410 | auto params = flattenList( func->params, tenv );
|
|---|
| 411 | auto params2 = flattenList( func2->params, tenv );
|
|---|
| 412 |
|
|---|
| 413 | // sizes don't have to match if ttypes are involved; need to be more precise w.r.t.
|
|---|
| 414 | // where the ttype is to prevent errors
|
|---|
| 415 | if (
|
|---|
| 416 | ( params.size() != params2.size() || func->returns.size() != func2->returns.size() )
|
|---|
| 417 | && ! func->isTtype()
|
|---|
| 418 | && ! func2->isTtype()
|
|---|
| 419 | ) return;
|
|---|
| 420 |
|
|---|
| 421 | if ( ! unifyTypeList( params, params2, tenv, need, have, open ) ) return;
|
|---|
| 422 | if ( ! unifyTypeList(
|
|---|
| 423 | func->returns, func2->returns, tenv, need, have, open ) ) return;
|
|---|
| 424 |
|
|---|
| 425 | markAssertions( have, need, func );
|
|---|
| 426 | markAssertions( have, need, func2 );
|
|---|
| 427 |
|
|---|
| 428 | result = true;
|
|---|
| 429 | }
|
|---|
| 430 |
|
|---|
| 431 | private:
|
|---|
| 432 | // Returns: other, cast as XInstType
|
|---|
| 433 | // Assigns this->result: whether types are compatible (up to generic parameters)
|
|---|
| 434 | template< typename XInstType >
|
|---|
| 435 | const XInstType * handleRefType( const XInstType * inst, const ast::Type * other ) {
|
|---|
| 436 | // check that the other type is compatible and named the same
|
|---|
| 437 | auto otherInst = dynamic_cast< const XInstType * >( other );
|
|---|
| 438 | if (otherInst && inst->name == otherInst->name)
|
|---|
| 439 | this->result = otherInst;
|
|---|
| 440 | return otherInst;
|
|---|
| 441 | }
|
|---|
| 442 |
|
|---|
| 443 | /// Creates a tuple type based on a list of TypeExpr
|
|---|
| 444 | template< typename Iter >
|
|---|
| 445 | static const ast::Type * tupleFromExprs(
|
|---|
| 446 | const ast::TypeExpr * param, Iter & crnt, Iter end, ast::CV::Qualifiers qs
|
|---|
| 447 | ) {
|
|---|
| 448 | std::vector< ast::ptr< ast::Type > > types;
|
|---|
| 449 | do {
|
|---|
| 450 | types.emplace_back( param->type );
|
|---|
| 451 |
|
|---|
| 452 | ++crnt;
|
|---|
| 453 | if ( crnt == end ) break;
|
|---|
| 454 | param = strict_dynamic_cast< const ast::TypeExpr * >( crnt->get() );
|
|---|
| 455 | } while(true);
|
|---|
| 456 |
|
|---|
| 457 | return new ast::TupleType{ std::move(types), qs };
|
|---|
| 458 | }
|
|---|
| 459 |
|
|---|
| 460 | template< typename XInstType >
|
|---|
| 461 | void handleGenericRefType( const XInstType * inst, const ast::Type * other ) {
|
|---|
| 462 | // check that other type is compatible and named the same
|
|---|
| 463 | const XInstType * otherInst = handleRefType( inst, other );
|
|---|
| 464 | if ( ! this->result ) return;
|
|---|
| 465 |
|
|---|
| 466 | // check that parameters of types unify, if any
|
|---|
| 467 | const std::vector< ast::ptr< ast::Expr > > & params = inst->params;
|
|---|
| 468 | const std::vector< ast::ptr< ast::Expr > > & params2 = otherInst->params;
|
|---|
| 469 |
|
|---|
| 470 | auto it = params.begin();
|
|---|
| 471 | auto jt = params2.begin();
|
|---|
| 472 | for ( ; it != params.end() && jt != params2.end(); ++it, ++jt ) {
|
|---|
| 473 | auto param = strict_dynamic_cast< const ast::TypeExpr * >( it->get() );
|
|---|
| 474 | auto param2 = strict_dynamic_cast< const ast::TypeExpr * >( jt->get() );
|
|---|
| 475 |
|
|---|
| 476 | ast::ptr< ast::Type > pty = param->type;
|
|---|
| 477 | ast::ptr< ast::Type > pty2 = param2->type;
|
|---|
| 478 |
|
|---|
| 479 | bool isTuple = Tuples::isTtype( pty );
|
|---|
| 480 | bool isTuple2 = Tuples::isTtype( pty2 );
|
|---|
| 481 |
|
|---|
| 482 | if ( isTuple && isTuple2 ) {
|
|---|
| 483 | ++it; ++jt; // skip ttype parameters before break
|
|---|
| 484 | } else if ( isTuple ) {
|
|---|
| 485 | // bundle remaining params into tuple
|
|---|
| 486 | pty2 = tupleFromExprs( param2, jt, params2.end(), pty->qualifiers );
|
|---|
| 487 | ++it; // skip ttype parameter for break
|
|---|
| 488 | } else if ( isTuple2 ) {
|
|---|
| 489 | // bundle remaining params into tuple
|
|---|
| 490 | pty = tupleFromExprs( param, it, params.end(), pty2->qualifiers );
|
|---|
| 491 | ++jt; // skip ttype parameter for break
|
|---|
| 492 | }
|
|---|
| 493 |
|
|---|
| 494 | if ( ! unifyExact(
|
|---|
| 495 | pty, pty2, tenv, need, have, open, noWiden() ) ) {
|
|---|
| 496 | result = false;
|
|---|
| 497 | return;
|
|---|
| 498 | }
|
|---|
| 499 |
|
|---|
| 500 | // ttype parameter should be last
|
|---|
| 501 | if ( isTuple || isTuple2 ) break;
|
|---|
| 502 | }
|
|---|
| 503 | result = it == params.end() && jt == params2.end();
|
|---|
| 504 | }
|
|---|
| 505 |
|
|---|
| 506 | public:
|
|---|
| 507 | void postvisit( const ast::StructInstType * aggrType ) {
|
|---|
| 508 | handleGenericRefType( aggrType, type2 );
|
|---|
| 509 | }
|
|---|
| 510 |
|
|---|
| 511 | void postvisit( const ast::UnionInstType * aggrType ) {
|
|---|
| 512 | handleGenericRefType( aggrType, type2 );
|
|---|
| 513 | }
|
|---|
| 514 |
|
|---|
| 515 | void postvisit( const ast::EnumInstType * aggrType ) {
|
|---|
| 516 | handleRefType( aggrType, type2 );
|
|---|
| 517 | }
|
|---|
| 518 |
|
|---|
| 519 | void postvisit( const ast::EnumPosType * posType ) {
|
|---|
| 520 | // Lazy approach for now
|
|---|
| 521 | auto otherPos = dynamic_cast< const ast::EnumPosType *>(type2);
|
|---|
| 522 | if ( otherPos ) {
|
|---|
| 523 | if ( otherPos->instance->base->name == posType->instance->base->name )
|
|---|
| 524 | result = otherPos;
|
|---|
| 525 | }
|
|---|
| 526 | }
|
|---|
| 527 |
|
|---|
| 528 | void postvisit( const ast::TraitInstType * aggrType ) {
|
|---|
| 529 | handleRefType( aggrType, type2 );
|
|---|
| 530 | }
|
|---|
| 531 |
|
|---|
| 532 | void postvisit( const ast::TypeInstType * typeInst ) {
|
|---|
| 533 | // assert( open.find( *typeInst ) == open.end() );
|
|---|
| 534 | auto otherInst = dynamic_cast< const ast::TypeInstType * >( type2 );
|
|---|
| 535 | if ( otherInst && typeInst->name == otherInst->name ) {
|
|---|
| 536 | this->result = otherInst;
|
|---|
| 537 | }
|
|---|
| 538 | }
|
|---|
| 539 |
|
|---|
| 540 | private:
|
|---|
| 541 | /// Creates a tuple type based on a list of Type
|
|---|
| 542 |
|
|---|
| 543 | static bool unifyList(
|
|---|
| 544 | const std::vector< ast::ptr< ast::Type > > & list1,
|
|---|
| 545 | const std::vector< ast::ptr< ast::Type > > & list2, ast::TypeEnvironment & env,
|
|---|
| 546 | ast::AssertionSet & need, ast::AssertionSet & have, const ast::OpenVarSet & open
|
|---|
| 547 | ) {
|
|---|
| 548 | auto crnt1 = list1.begin();
|
|---|
| 549 | auto crnt2 = list2.begin();
|
|---|
| 550 | while ( crnt1 != list1.end() && crnt2 != list2.end() ) {
|
|---|
| 551 | const ast::Type * t1 = *crnt1;
|
|---|
| 552 | const ast::Type * t2 = *crnt2;
|
|---|
| 553 | bool isTuple1 = Tuples::isTtype( t1 );
|
|---|
| 554 | bool isTuple2 = Tuples::isTtype( t2 );
|
|---|
| 555 |
|
|---|
| 556 | // assumes ttype must be last parameter
|
|---|
| 557 | if ( isTuple1 && ! isTuple2 ) {
|
|---|
| 558 | // combine entirety of list2, then unify
|
|---|
| 559 | return unifyExact(
|
|---|
| 560 | t1, tupleFromTypes( list2 ), env, need, have, open,
|
|---|
| 561 | noWiden() );
|
|---|
| 562 | } else if ( ! isTuple1 && isTuple2 ) {
|
|---|
| 563 | // combine entirety of list1, then unify
|
|---|
| 564 | return unifyExact(
|
|---|
| 565 | tupleFromTypes( list1 ), t2, env, need, have, open,
|
|---|
| 566 | noWiden() );
|
|---|
| 567 | }
|
|---|
| 568 |
|
|---|
| 569 | if ( ! unifyExact(
|
|---|
| 570 | t1, t2, env, need, have, open, noWiden() )
|
|---|
| 571 | ) return false;
|
|---|
| 572 |
|
|---|
| 573 | ++crnt1; ++crnt2;
|
|---|
| 574 | }
|
|---|
| 575 |
|
|---|
| 576 | if ( crnt1 != list1.end() ) {
|
|---|
| 577 | // try unifying empty tuple type with ttype
|
|---|
| 578 | const ast::Type * t1 = *crnt1;
|
|---|
| 579 | if ( ! Tuples::isTtype( t1 ) ) return false;
|
|---|
| 580 | // xxx - this doesn't generate an empty tuple, contrary to comment; both ported
|
|---|
| 581 | // from Rob's code
|
|---|
| 582 | return unifyExact(
|
|---|
| 583 | t1, tupleFromTypes( list2 ), env, need, have, open,
|
|---|
| 584 | noWiden() );
|
|---|
| 585 | } else if ( crnt2 != list2.end() ) {
|
|---|
| 586 | // try unifying empty tuple with ttype
|
|---|
| 587 | const ast::Type * t2 = *crnt2;
|
|---|
| 588 | if ( ! Tuples::isTtype( t2 ) ) return false;
|
|---|
| 589 | // xxx - this doesn't generate an empty tuple, contrary to comment; both ported
|
|---|
| 590 | // from Rob's code
|
|---|
| 591 | return unifyExact(
|
|---|
| 592 | tupleFromTypes( list1 ), t2, env, need, have, open,
|
|---|
| 593 | noWiden() );
|
|---|
| 594 | }
|
|---|
| 595 |
|
|---|
| 596 | return true;
|
|---|
| 597 | }
|
|---|
| 598 |
|
|---|
| 599 | public:
|
|---|
| 600 | void postvisit( const ast::TupleType * tuple ) {
|
|---|
| 601 | auto tuple2 = dynamic_cast< const ast::TupleType * >( type2 );
|
|---|
| 602 | if ( ! tuple2 ) return;
|
|---|
| 603 |
|
|---|
| 604 | ast::Pass<TtypeExpander> expander{ tenv };
|
|---|
| 605 |
|
|---|
| 606 | const ast::Type * flat = tuple->accept( expander );
|
|---|
| 607 | const ast::Type * flat2 = tuple2->accept( expander );
|
|---|
| 608 |
|
|---|
| 609 | auto types = flatten( flat );
|
|---|
| 610 | auto types2 = flatten( flat2 );
|
|---|
| 611 |
|
|---|
| 612 | result = unifyList( types, types2, tenv, need, have, open );
|
|---|
| 613 | }
|
|---|
| 614 |
|
|---|
| 615 | void postvisit( const ast::VarArgsType * ) {
|
|---|
| 616 | result = dynamic_cast< const ast::VarArgsType * >( type2 );
|
|---|
| 617 | }
|
|---|
| 618 |
|
|---|
| 619 | void postvisit( const ast::ZeroType * ) {
|
|---|
| 620 | result = dynamic_cast< const ast::ZeroType * >( type2 );
|
|---|
| 621 | }
|
|---|
| 622 |
|
|---|
| 623 | void postvisit( const ast::OneType * ) {
|
|---|
| 624 | result = dynamic_cast< const ast::OneType * >( type2 );
|
|---|
| 625 | }
|
|---|
| 626 | };
|
|---|
| 627 |
|
|---|
| 628 | // size_t Unify::traceId = Stats::Heap::new_stacktrace_id("Unify");
|
|---|
| 629 |
|
|---|
| 630 | bool unify(
|
|---|
| 631 | const ast::ptr<ast::Type> & type1, const ast::ptr<ast::Type> & type2,
|
|---|
| 632 | ast::TypeEnvironment & env, ast::AssertionSet & need, ast::AssertionSet & have,
|
|---|
| 633 | ast::OpenVarSet & open
|
|---|
| 634 | ) {
|
|---|
| 635 | ast::ptr<ast::Type> common;
|
|---|
| 636 | return unify( type1, type2, env, need, have, open, common );
|
|---|
| 637 | }
|
|---|
| 638 |
|
|---|
| 639 | bool unify(
|
|---|
| 640 | const ast::ptr<ast::Type> & type1, const ast::ptr<ast::Type> & type2,
|
|---|
| 641 | ast::TypeEnvironment & env, ast::AssertionSet & need, ast::AssertionSet & have,
|
|---|
| 642 | ast::OpenVarSet & open, ast::ptr<ast::Type> & common
|
|---|
| 643 | ) {
|
|---|
| 644 | ast::OpenVarSet closed;
|
|---|
| 645 | // findOpenVars( type1, open, closed, need, have, FirstClosed );
|
|---|
| 646 | findOpenVars( type2, open, closed, need, have, env, FirstOpen );
|
|---|
| 647 | return unifyInexact(
|
|---|
| 648 | type1, type2, env, need, have, open, WidenMode{ true, true }, common );
|
|---|
| 649 | }
|
|---|
| 650 |
|
|---|
| 651 | bool unifyExact(
|
|---|
| 652 | const ast::Type * type1, const ast::Type * type2, ast::TypeEnvironment & env,
|
|---|
| 653 | ast::AssertionSet & need, ast::AssertionSet & have, const ast::OpenVarSet & open,
|
|---|
| 654 | WidenMode widen
|
|---|
| 655 | ) {
|
|---|
| 656 | if ( type1->qualifiers != type2->qualifiers ) return false;
|
|---|
| 657 |
|
|---|
| 658 | auto var1 = dynamic_cast< const ast::TypeInstType * >( type1 );
|
|---|
| 659 | auto var2 = dynamic_cast< const ast::TypeInstType * >( type2 );
|
|---|
| 660 | bool isopen1 = var1 && env.lookup(*var1);
|
|---|
| 661 | bool isopen2 = var2 && env.lookup(*var2);
|
|---|
| 662 |
|
|---|
| 663 | if ( isopen1 && isopen2 ) {
|
|---|
| 664 | if ( var1->base->kind != var2->base->kind ) return false;
|
|---|
| 665 | return env.bindVarToVar(
|
|---|
| 666 | var1, var2, ast::TypeData{ var1->base->kind, var1->base->sized||var2->base->sized }, need, have,
|
|---|
| 667 | open, widen );
|
|---|
| 668 | } else if ( isopen1 ) {
|
|---|
| 669 | return env.bindVar( var1, type2, ast::TypeData{var1->base}, need, have, open, widen );
|
|---|
| 670 | } else if ( isopen2 ) {
|
|---|
| 671 | return env.bindVar( var2, type1, ast::TypeData{var2->base}, need, have, open, widen );
|
|---|
| 672 | } else {
|
|---|
| 673 | return ast::Pass<Unify>::read(
|
|---|
| 674 | type1, type2, env, need, have, open, widen );
|
|---|
| 675 | }
|
|---|
| 676 | }
|
|---|
| 677 |
|
|---|
| 678 | bool unifyInexact(
|
|---|
| 679 | const ast::ptr<ast::Type> & type1, const ast::ptr<ast::Type> & type2,
|
|---|
| 680 | ast::TypeEnvironment & env, ast::AssertionSet & need, ast::AssertionSet & have,
|
|---|
| 681 | const ast::OpenVarSet & open, WidenMode widen,
|
|---|
| 682 | ast::ptr<ast::Type> & common
|
|---|
| 683 | ) {
|
|---|
| 684 | ast::CV::Qualifiers q1 = type1->qualifiers, q2 = type2->qualifiers;
|
|---|
| 685 |
|
|---|
| 686 | // force t1 and t2 to be cloned if their qualifiers must be stripped, so that type1 and
|
|---|
| 687 | // type2 are left unchanged; calling convention forces type{1,2}->strong_ref >= 1
|
|---|
| 688 | ast::Type * t1 = shallowCopy(type1.get());
|
|---|
| 689 | ast::Type * t2 = shallowCopy(type2.get());
|
|---|
| 690 | t1->qualifiers = {};
|
|---|
| 691 | t2->qualifiers = {};
|
|---|
| 692 | ast::ptr< ast::Type > t1_(t1);
|
|---|
| 693 | ast::ptr< ast::Type > t2_(t2);
|
|---|
| 694 |
|
|---|
| 695 | if ( unifyExact( t1, t2, env, need, have, open, widen ) ) {
|
|---|
| 696 | // if exact unification on unqualified types, try to merge qualifiers
|
|---|
| 697 | if ( q1 == q2 || ( ( q1 > q2 || widen.first ) && ( q2 > q1 || widen.second ) ) ) {
|
|---|
| 698 | t1->qualifiers = q1 | q2;
|
|---|
| 699 | common = t1;
|
|---|
| 700 | return true;
|
|---|
| 701 | } else {
|
|---|
| 702 | return false;
|
|---|
| 703 | }
|
|---|
| 704 |
|
|---|
| 705 | } else if (( common = commonType( t1, t2, env, need, have, open, widen ))) {
|
|---|
| 706 | // no exact unification, but common type
|
|---|
| 707 | auto c = shallowCopy(common.get());
|
|---|
| 708 | c->qualifiers = q1 | q2;
|
|---|
| 709 | common = c;
|
|---|
| 710 | return true;
|
|---|
| 711 | } else {
|
|---|
| 712 | return false;
|
|---|
| 713 | }
|
|---|
| 714 | }
|
|---|
| 715 |
|
|---|
| 716 | ast::ptr<ast::Type> extractResultType( const ast::FunctionType * func ) {
|
|---|
| 717 | if ( func->returns.empty() ) return new ast::VoidType{};
|
|---|
| 718 | if ( func->returns.size() == 1 ) return func->returns[0];
|
|---|
| 719 |
|
|---|
| 720 | std::vector<ast::ptr<ast::Type>> tys;
|
|---|
| 721 | for ( const auto & decl : func->returns ) {
|
|---|
| 722 | tys.emplace_back( decl );
|
|---|
| 723 | }
|
|---|
| 724 | return new ast::TupleType{ std::move(tys) };
|
|---|
| 725 | }
|
|---|
| 726 | } // namespace ResolvExpr
|
|---|
| 727 |
|
|---|
| 728 | // Local Variables: //
|
|---|
| 729 | // tab-width: 4 //
|
|---|
| 730 | // mode: c++ //
|
|---|
| 731 | // compile-command: "make install" //
|
|---|
| 732 | // End: //
|
|---|