[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 | // |
---|
[41a2620] | 7 | // Unify.cc -- |
---|
[a32b204] | 8 | // |
---|
| 9 | // Author : Richard C. Bilson |
---|
| 10 | // Created On : Sun May 17 12:27:10 2015 |
---|
[4040425] | 11 | // Last Modified By : Peter A. Buhr |
---|
[6f95000] | 12 | // Last Modified On : Thu Mar 16 16:22:54 2017 |
---|
| 13 | // Update Count : 42 |
---|
[a32b204] | 14 | // |
---|
[51b7345] | 15 | |
---|
[ea6332d] | 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, auto_ptr |
---|
| 20 | #include <set> // for set |
---|
| 21 | #include <string> // for string, operator==, operator!=, bas... |
---|
| 22 | #include <utility> // for pair |
---|
[51b7345] | 23 | |
---|
[ea6332d] | 24 | #include "FindOpenVars.h" // for findOpenVars |
---|
| 25 | #include "Parser/LinkageSpec.h" // for C |
---|
| 26 | #include "SynTree/Constant.h" // for Constant |
---|
| 27 | #include "SynTree/Declaration.h" // for TypeDecl, TypeDecl::Data, Declarati... |
---|
| 28 | #include "SynTree/Expression.h" // for TypeExpr, Expression, ConstantExpr |
---|
| 29 | #include "SynTree/Mutator.h" // for Mutator |
---|
| 30 | #include "SynTree/Type.h" // for Type, TypeInstType, FunctionType |
---|
| 31 | #include "SynTree/Visitor.h" // for Visitor |
---|
| 32 | #include "Tuples/Tuples.h" // for isTtype |
---|
| 33 | #include "TypeEnvironment.h" // for EqvClass, AssertionSet, OpenVarSet |
---|
[51b7345] | 34 | #include "Unify.h" |
---|
[ea6332d] | 35 | #include "typeops.h" // for flatten, occurs, commonType |
---|
| 36 | |
---|
| 37 | namespace SymTab { |
---|
| 38 | class Indexer; |
---|
| 39 | } // namespace SymTab |
---|
[51b7345] | 40 | |
---|
[1cbca6e] | 41 | // #define DEBUG |
---|
[51b7345] | 42 | |
---|
| 43 | namespace ResolvExpr { |
---|
| 44 | |
---|
[a32b204] | 45 | class Unify : public Visitor { |
---|
| 46 | public: |
---|
| 47 | Unify( Type *type2, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, WidenMode widenMode, const SymTab::Indexer &indexer ); |
---|
[41a2620] | 48 | |
---|
[a32b204] | 49 | bool get_result() const { return result; } |
---|
| 50 | private: |
---|
| 51 | virtual void visit(VoidType *voidType); |
---|
| 52 | virtual void visit(BasicType *basicType); |
---|
| 53 | virtual void visit(PointerType *pointerType); |
---|
| 54 | virtual void visit(ArrayType *arrayType); |
---|
| 55 | virtual void visit(FunctionType *functionType); |
---|
| 56 | virtual void visit(StructInstType *aggregateUseType); |
---|
| 57 | virtual void visit(UnionInstType *aggregateUseType); |
---|
| 58 | virtual void visit(EnumInstType *aggregateUseType); |
---|
[4040425] | 59 | virtual void visit(TraitInstType *aggregateUseType); |
---|
[a32b204] | 60 | virtual void visit(TypeInstType *aggregateUseType); |
---|
| 61 | virtual void visit(TupleType *tupleType); |
---|
[44b7088] | 62 | virtual void visit(VarArgsType *varArgsType); |
---|
[89e6ffc] | 63 | virtual void visit(ZeroType *zeroType); |
---|
| 64 | virtual void visit(OneType *oneType); |
---|
[a32b204] | 65 | |
---|
| 66 | template< typename RefType > void handleRefType( RefType *inst, Type *other ); |
---|
[02ec390] | 67 | template< typename RefType > void handleGenericRefType( RefType *inst, Type *other ); |
---|
[a32b204] | 68 | |
---|
| 69 | bool result; |
---|
| 70 | Type *type2; // inherited |
---|
| 71 | TypeEnvironment &env; |
---|
| 72 | AssertionSet &needAssertions; |
---|
| 73 | AssertionSet &haveAssertions; |
---|
| 74 | const OpenVarSet &openVars; |
---|
| 75 | WidenMode widenMode; |
---|
| 76 | const SymTab::Indexer &indexer; |
---|
| 77 | }; |
---|
| 78 | |
---|
[eb50842] | 79 | /// Attempts an inexact unification of type1 and type2. |
---|
| 80 | /// Returns false if no such unification; if the types can be unified, sets common (unless they unify exactly and have identical type qualifiers) |
---|
[a32b204] | 81 | bool unifyInexact( Type *type1, Type *type2, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, WidenMode widenMode, const SymTab::Indexer &indexer, Type *&common ); |
---|
| 82 | bool unifyExact( Type *type1, Type *type2, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, WidenMode widenMode, const SymTab::Indexer &indexer ); |
---|
[41a2620] | 83 | |
---|
[a32b204] | 84 | bool typesCompatible( Type *first, Type *second, const SymTab::Indexer &indexer, const TypeEnvironment &env ) { |
---|
| 85 | TypeEnvironment newEnv; |
---|
[1cbca6e] | 86 | OpenVarSet openVars, closedVars; // added closedVars |
---|
[a32b204] | 87 | AssertionSet needAssertions, haveAssertions; |
---|
| 88 | Type *newFirst = first->clone(), *newSecond = second->clone(); |
---|
| 89 | env.apply( newFirst ); |
---|
| 90 | env.apply( newSecond ); |
---|
[1cbca6e] | 91 | |
---|
| 92 | // do we need to do this? Seems like we do, types should be able to be compatible if they |
---|
| 93 | // have free variables that can unify |
---|
| 94 | findOpenVars( newFirst, openVars, closedVars, needAssertions, haveAssertions, false ); |
---|
| 95 | findOpenVars( newSecond, openVars, closedVars, needAssertions, haveAssertions, true ); |
---|
| 96 | |
---|
[a32b204] | 97 | bool result = unifyExact( newFirst, newSecond, newEnv, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer ); |
---|
| 98 | delete newFirst; |
---|
| 99 | delete newSecond; |
---|
| 100 | return result; |
---|
| 101 | } |
---|
| 102 | |
---|
| 103 | bool typesCompatibleIgnoreQualifiers( Type *first, Type *second, const SymTab::Indexer &indexer, const TypeEnvironment &env ) { |
---|
| 104 | TypeEnvironment newEnv; |
---|
| 105 | OpenVarSet openVars; |
---|
| 106 | AssertionSet needAssertions, haveAssertions; |
---|
| 107 | Type *newFirst = first->clone(), *newSecond = second->clone(); |
---|
| 108 | env.apply( newFirst ); |
---|
| 109 | env.apply( newSecond ); |
---|
| 110 | newFirst->get_qualifiers() = Type::Qualifiers(); |
---|
| 111 | newSecond->get_qualifiers() = Type::Qualifiers(); |
---|
[2c57025] | 112 | /// std::cerr << "first is "; |
---|
| 113 | /// first->print( std::cerr ); |
---|
| 114 | /// std::cerr << std::endl << "second is "; |
---|
| 115 | /// second->print( std::cerr ); |
---|
| 116 | /// std::cerr << std::endl << "newFirst is "; |
---|
| 117 | /// newFirst->print( std::cerr ); |
---|
| 118 | /// std::cerr << std::endl << "newSecond is "; |
---|
| 119 | /// newSecond->print( std::cerr ); |
---|
| 120 | /// std::cerr << std::endl; |
---|
[a32b204] | 121 | bool result = unifyExact( newFirst, newSecond, newEnv, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer ); |
---|
| 122 | delete newFirst; |
---|
| 123 | delete newSecond; |
---|
| 124 | return result; |
---|
| 125 | } |
---|
| 126 | |
---|
[d7dc824] | 127 | bool isFtype( Type *type ) { |
---|
[a32b204] | 128 | if ( dynamic_cast< FunctionType* >( type ) ) { |
---|
| 129 | return true; |
---|
| 130 | } else if ( TypeInstType *typeInst = dynamic_cast< TypeInstType* >( type ) ) { |
---|
| 131 | return typeInst->get_isFtype(); |
---|
| 132 | } // if |
---|
| 133 | return false; |
---|
| 134 | } |
---|
| 135 | |
---|
[d7dc824] | 136 | bool tyVarCompatible( const TypeDecl::Data & data, Type *type ) { |
---|
[2c57025] | 137 | switch ( data.kind ) { |
---|
[a32b204] | 138 | case TypeDecl::Any: |
---|
| 139 | case TypeDecl::Dtype: |
---|
[2c57025] | 140 | // to bind to an object type variable, the type must not be a function type. |
---|
| 141 | // if the type variable is specified to be a complete type then the incoming |
---|
| 142 | // type must also be complete |
---|
[6c3a988f] | 143 | // xxx - should this also check that type is not a tuple type and that it's not a ttype? |
---|
[d7dc824] | 144 | return ! isFtype( type ) && (! data.isComplete || type->isComplete() ); |
---|
[a32b204] | 145 | case TypeDecl::Ftype: |
---|
[d7dc824] | 146 | return isFtype( type ); |
---|
[b2daebd4] | 147 | case TypeDecl::Ttype: |
---|
[6c3a988f] | 148 | // ttype unifies with any tuple type |
---|
[f3b0a07] | 149 | return dynamic_cast< TupleType * >( type ) || Tuples::isTtype( type ); |
---|
[a32b204] | 150 | } // switch |
---|
| 151 | return false; |
---|
| 152 | } |
---|
| 153 | |
---|
[2c57025] | 154 | bool bindVar( TypeInstType *typeInst, Type *other, const TypeDecl::Data & data, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, WidenMode widenMode, const SymTab::Indexer &indexer ) { |
---|
[a32b204] | 155 | OpenVarSet::const_iterator tyvar = openVars.find( typeInst->get_name() ); |
---|
| 156 | assert( tyvar != openVars.end() ); |
---|
[d7dc824] | 157 | if ( ! tyVarCompatible( tyvar->second, other ) ) { |
---|
[a32b204] | 158 | return false; |
---|
| 159 | } // if |
---|
| 160 | if ( occurs( other, typeInst->get_name(), env ) ) { |
---|
| 161 | return false; |
---|
| 162 | } // if |
---|
| 163 | EqvClass curClass; |
---|
| 164 | if ( env.lookup( typeInst->get_name(), curClass ) ) { |
---|
| 165 | if ( curClass.type ) { |
---|
| 166 | Type *common = 0; |
---|
[eb50842] | 167 | // attempt to unify equivalence class type (which has qualifiers stripped, so they must be restored) with the type to bind to |
---|
[a32b204] | 168 | std::auto_ptr< Type > newType( curClass.type->clone() ); |
---|
[721f17a] | 169 | newType->get_qualifiers() = typeInst->get_qualifiers(); |
---|
[a32b204] | 170 | if ( unifyInexact( newType.get(), other, env, needAssertions, haveAssertions, openVars, widenMode & WidenMode( curClass.allowWidening, true ), indexer, common ) ) { |
---|
| 171 | if ( common ) { |
---|
| 172 | common->get_qualifiers() = Type::Qualifiers(); |
---|
| 173 | delete curClass.type; |
---|
| 174 | curClass.type = common; |
---|
| 175 | env.add( curClass ); |
---|
| 176 | } // if |
---|
| 177 | return true; |
---|
| 178 | } else { |
---|
| 179 | return false; |
---|
| 180 | } // if |
---|
| 181 | } else { |
---|
| 182 | curClass.type = other->clone(); |
---|
| 183 | curClass.type->get_qualifiers() = Type::Qualifiers(); |
---|
| 184 | curClass.allowWidening = widenMode.widenFirst && widenMode.widenSecond; |
---|
| 185 | env.add( curClass ); |
---|
| 186 | } // if |
---|
| 187 | } else { |
---|
| 188 | EqvClass newClass; |
---|
| 189 | newClass.vars.insert( typeInst->get_name() ); |
---|
| 190 | newClass.type = other->clone(); |
---|
| 191 | newClass.type->get_qualifiers() = Type::Qualifiers(); |
---|
| 192 | newClass.allowWidening = widenMode.widenFirst && widenMode.widenSecond; |
---|
[2c57025] | 193 | newClass.data = data; |
---|
[a32b204] | 194 | env.add( newClass ); |
---|
| 195 | } // if |
---|
| 196 | return true; |
---|
| 197 | } |
---|
| 198 | |
---|
[2c57025] | 199 | bool bindVarToVar( TypeInstType *var1, TypeInstType *var2, const TypeDecl::Data & data, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, WidenMode widenMode, const SymTab::Indexer &indexer ) { |
---|
[a32b204] | 200 | bool result = true; |
---|
| 201 | EqvClass class1, class2; |
---|
| 202 | bool hasClass1 = false, hasClass2 = false; |
---|
| 203 | bool widen1 = false, widen2 = false; |
---|
| 204 | Type *type1 = 0, *type2 = 0; |
---|
[41a2620] | 205 | |
---|
[a32b204] | 206 | if ( env.lookup( var1->get_name(), class1 ) ) { |
---|
| 207 | hasClass1 = true; |
---|
| 208 | if ( class1.type ) { |
---|
| 209 | if ( occurs( class1.type, var2->get_name(), env ) ) { |
---|
| 210 | return false; |
---|
| 211 | } // if |
---|
| 212 | type1 = class1.type->clone(); |
---|
| 213 | } // if |
---|
| 214 | widen1 = widenMode.widenFirst && class1.allowWidening; |
---|
| 215 | } // if |
---|
| 216 | if ( env.lookup( var2->get_name(), class2 ) ) { |
---|
| 217 | hasClass2 = true; |
---|
| 218 | if ( class2.type ) { |
---|
| 219 | if ( occurs( class2.type, var1->get_name(), env ) ) { |
---|
| 220 | return false; |
---|
| 221 | } // if |
---|
| 222 | type2 = class2.type->clone(); |
---|
| 223 | } // if |
---|
| 224 | widen2 = widenMode.widenSecond && class2.allowWidening; |
---|
| 225 | } // if |
---|
[41a2620] | 226 | |
---|
[a32b204] | 227 | if ( type1 && type2 ) { |
---|
[2c57025] | 228 | // std::cerr << "has type1 && type2" << std::endl; |
---|
[a32b204] | 229 | WidenMode newWidenMode ( widen1, widen2 ); |
---|
| 230 | Type *common = 0; |
---|
| 231 | if ( unifyInexact( type1, type2, env, needAssertions, haveAssertions, openVars, newWidenMode, indexer, common ) ) { |
---|
| 232 | class1.vars.insert( class2.vars.begin(), class2.vars.end() ); |
---|
| 233 | class1.allowWidening = widen1 && widen2; |
---|
| 234 | if ( common ) { |
---|
| 235 | common->get_qualifiers() = Type::Qualifiers(); |
---|
| 236 | delete class1.type; |
---|
| 237 | class1.type = common; |
---|
| 238 | } // if |
---|
| 239 | env.add( class1 ); |
---|
| 240 | } else { |
---|
| 241 | result = false; |
---|
| 242 | } // if |
---|
| 243 | } else if ( hasClass1 && hasClass2 ) { |
---|
| 244 | if ( type1 ) { |
---|
| 245 | class1.vars.insert( class2.vars.begin(), class2.vars.end() ); |
---|
| 246 | class1.allowWidening = widen1; |
---|
| 247 | env.add( class1 ); |
---|
| 248 | } else { |
---|
| 249 | class2.vars.insert( class1.vars.begin(), class1.vars.end() ); |
---|
| 250 | class2.allowWidening = widen2; |
---|
| 251 | env.add( class2 ); |
---|
| 252 | } // if |
---|
| 253 | } else if ( hasClass1 ) { |
---|
| 254 | class1.vars.insert( var2->get_name() ); |
---|
| 255 | class1.allowWidening = widen1; |
---|
| 256 | env.add( class1 ); |
---|
| 257 | } else if ( hasClass2 ) { |
---|
| 258 | class2.vars.insert( var1->get_name() ); |
---|
| 259 | class2.allowWidening = widen2; |
---|
| 260 | env.add( class2 ); |
---|
| 261 | } else { |
---|
| 262 | EqvClass newClass; |
---|
| 263 | newClass.vars.insert( var1->get_name() ); |
---|
| 264 | newClass.vars.insert( var2->get_name() ); |
---|
| 265 | newClass.allowWidening = widen1 && widen2; |
---|
[2c57025] | 266 | newClass.data = data; |
---|
[a32b204] | 267 | env.add( newClass ); |
---|
| 268 | } // if |
---|
| 269 | delete type1; |
---|
| 270 | delete type2; |
---|
| 271 | return result; |
---|
| 272 | } |
---|
| 273 | |
---|
| 274 | bool unify( Type *type1, Type *type2, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, OpenVarSet &openVars, const SymTab::Indexer &indexer ) { |
---|
| 275 | OpenVarSet closedVars; |
---|
| 276 | findOpenVars( type1, openVars, closedVars, needAssertions, haveAssertions, false ); |
---|
| 277 | findOpenVars( type2, openVars, closedVars, needAssertions, haveAssertions, true ); |
---|
| 278 | Type *commonType = 0; |
---|
| 279 | if ( unifyInexact( type1, type2, env, needAssertions, haveAssertions, openVars, WidenMode( true, true ), indexer, commonType ) ) { |
---|
| 280 | if ( commonType ) { |
---|
| 281 | delete commonType; |
---|
| 282 | } // if |
---|
| 283 | return true; |
---|
| 284 | } else { |
---|
| 285 | return false; |
---|
| 286 | } // if |
---|
| 287 | } |
---|
| 288 | |
---|
| 289 | bool unify( Type *type1, Type *type2, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, OpenVarSet &openVars, const SymTab::Indexer &indexer, Type *&commonType ) { |
---|
| 290 | OpenVarSet closedVars; |
---|
| 291 | findOpenVars( type1, openVars, closedVars, needAssertions, haveAssertions, false ); |
---|
| 292 | findOpenVars( type2, openVars, closedVars, needAssertions, haveAssertions, true ); |
---|
| 293 | return unifyInexact( type1, type2, env, needAssertions, haveAssertions, openVars, WidenMode( true, true ), indexer, commonType ); |
---|
| 294 | } |
---|
| 295 | |
---|
| 296 | bool unifyExact( Type *type1, Type *type2, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, WidenMode widenMode, const SymTab::Indexer &indexer ) { |
---|
[51b7345] | 297 | #ifdef DEBUG |
---|
[a32b204] | 298 | TypeEnvironment debugEnv( env ); |
---|
[51b7345] | 299 | #endif |
---|
[eb50842] | 300 | if ( type1->get_qualifiers() != type2->get_qualifiers() ) { |
---|
| 301 | return false; |
---|
| 302 | } |
---|
| 303 | |
---|
[a32b204] | 304 | bool result; |
---|
| 305 | TypeInstType *var1 = dynamic_cast< TypeInstType* >( type1 ); |
---|
| 306 | TypeInstType *var2 = dynamic_cast< TypeInstType* >( type2 ); |
---|
| 307 | OpenVarSet::const_iterator entry1, entry2; |
---|
| 308 | if ( var1 ) { |
---|
| 309 | entry1 = openVars.find( var1->get_name() ); |
---|
| 310 | } // if |
---|
| 311 | if ( var2 ) { |
---|
| 312 | entry2 = openVars.find( var2->get_name() ); |
---|
| 313 | } // if |
---|
| 314 | bool isopen1 = var1 && ( entry1 != openVars.end() ); |
---|
| 315 | bool isopen2 = var2 && ( entry2 != openVars.end() ); |
---|
[eb50842] | 316 | |
---|
| 317 | if ( isopen1 && isopen2 && entry1->second == entry2->second ) { |
---|
[a32b204] | 318 | result = bindVarToVar( var1, var2, entry1->second, env, needAssertions, haveAssertions, openVars, widenMode, indexer ); |
---|
| 319 | } else if ( isopen1 ) { |
---|
| 320 | result = bindVar( var1, type2, entry1->second, env, needAssertions, haveAssertions, openVars, widenMode, indexer ); |
---|
| 321 | } else if ( isopen2 ) { |
---|
| 322 | result = bindVar( var2, type1, entry2->second, env, needAssertions, haveAssertions, openVars, widenMode, indexer ); |
---|
| 323 | } else { |
---|
| 324 | Unify comparator( type2, env, needAssertions, haveAssertions, openVars, widenMode, indexer ); |
---|
| 325 | type1->accept( comparator ); |
---|
| 326 | result = comparator.get_result(); |
---|
| 327 | } // if |
---|
[51b7345] | 328 | #ifdef DEBUG |
---|
[2c57025] | 329 | std::cerr << "============ unifyExact" << std::endl; |
---|
| 330 | std::cerr << "type1 is "; |
---|
| 331 | type1->print( std::cerr ); |
---|
| 332 | std::cerr << std::endl << "type2 is "; |
---|
| 333 | type2->print( std::cerr ); |
---|
| 334 | std::cerr << std::endl << "openVars are "; |
---|
| 335 | printOpenVarSet( openVars, std::cerr, 8 ); |
---|
| 336 | std::cerr << std::endl << "input env is " << std::endl; |
---|
| 337 | debugEnv.print( std::cerr, 8 ); |
---|
| 338 | std::cerr << std::endl << "result env is " << std::endl; |
---|
| 339 | env.print( std::cerr, 8 ); |
---|
| 340 | std::cerr << "result is " << result << std::endl; |
---|
[51b7345] | 341 | #endif |
---|
[a32b204] | 342 | return result; |
---|
| 343 | } |
---|
| 344 | |
---|
| 345 | bool unifyExact( Type *type1, Type *type2, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, OpenVarSet &openVars, const SymTab::Indexer &indexer ) { |
---|
| 346 | return unifyExact( type1, type2, env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer ); |
---|
| 347 | } |
---|
| 348 | |
---|
| 349 | bool unifyInexact( Type *type1, Type *type2, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, WidenMode widenMode, const SymTab::Indexer &indexer, Type *&common ) { |
---|
| 350 | Type::Qualifiers tq1 = type1->get_qualifiers(), tq2 = type2->get_qualifiers(); |
---|
| 351 | type1->get_qualifiers() = Type::Qualifiers(); |
---|
| 352 | type2->get_qualifiers() = Type::Qualifiers(); |
---|
| 353 | bool result; |
---|
[51b7345] | 354 | #ifdef DEBUG |
---|
[2c57025] | 355 | std::cerr << "unifyInexact type 1 is "; |
---|
| 356 | type1->print( std::cerr ); |
---|
[0b150ec] | 357 | std::cerr << " type 2 is "; |
---|
[2c57025] | 358 | type2->print( std::cerr ); |
---|
| 359 | std::cerr << std::endl; |
---|
[51b7345] | 360 | #endif |
---|
[a32b204] | 361 | if ( ! unifyExact( type1, type2, env, needAssertions, haveAssertions, openVars, widenMode, indexer ) ) { |
---|
[51b7345] | 362 | #ifdef DEBUG |
---|
[2c57025] | 363 | std::cerr << "unifyInexact: no exact unification found" << std::endl; |
---|
[51b7345] | 364 | #endif |
---|
[a32b204] | 365 | if ( ( common = commonType( type1, type2, widenMode.widenFirst, widenMode.widenSecond, indexer, env, openVars ) ) ) { |
---|
[6f95000] | 366 | common->get_qualifiers() = tq1 | tq2; |
---|
[51b7345] | 367 | #ifdef DEBUG |
---|
[2c57025] | 368 | std::cerr << "unifyInexact: common type is "; |
---|
| 369 | common->print( std::cerr ); |
---|
| 370 | std::cerr << std::endl; |
---|
[51b7345] | 371 | #endif |
---|
[a32b204] | 372 | result = true; |
---|
| 373 | } else { |
---|
[51b7345] | 374 | #ifdef DEBUG |
---|
[2c57025] | 375 | std::cerr << "unifyInexact: no common type found" << std::endl; |
---|
[51b7345] | 376 | #endif |
---|
[a32b204] | 377 | result = false; |
---|
| 378 | } // if |
---|
| 379 | } else { |
---|
| 380 | if ( tq1 != tq2 ) { |
---|
| 381 | if ( ( tq1 > tq2 || widenMode.widenFirst ) && ( tq2 > tq1 || widenMode.widenSecond ) ) { |
---|
| 382 | common = type1->clone(); |
---|
[6f95000] | 383 | common->get_qualifiers() = tq1 | tq2; |
---|
[a32b204] | 384 | result = true; |
---|
| 385 | } else { |
---|
| 386 | result = false; |
---|
| 387 | } // if |
---|
| 388 | } else { |
---|
| 389 | result = true; |
---|
| 390 | } // if |
---|
| 391 | } // if |
---|
| 392 | type1->get_qualifiers() = tq1; |
---|
| 393 | type2->get_qualifiers() = tq2; |
---|
| 394 | return result; |
---|
| 395 | } |
---|
| 396 | |
---|
| 397 | Unify::Unify( Type *type2, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, WidenMode widenMode, const SymTab::Indexer &indexer ) |
---|
| 398 | : result( false ), type2( type2 ), env( env ), needAssertions( needAssertions ), haveAssertions( haveAssertions ), openVars( openVars ), widenMode( widenMode ), indexer( indexer ) { |
---|
| 399 | } |
---|
| 400 | |
---|
[af397ef8] | 401 | void Unify::visit( __attribute__((unused)) VoidType *voidType) { |
---|
[a32b204] | 402 | result = dynamic_cast< VoidType* >( type2 ); |
---|
| 403 | } |
---|
| 404 | |
---|
| 405 | void Unify::visit(BasicType *basicType) { |
---|
| 406 | if ( BasicType *otherBasic = dynamic_cast< BasicType* >( type2 ) ) { |
---|
| 407 | result = basicType->get_kind() == otherBasic->get_kind(); |
---|
| 408 | } // if |
---|
| 409 | } |
---|
| 410 | |
---|
| 411 | void markAssertionSet( AssertionSet &assertions, DeclarationWithType *assert ) { |
---|
[2c57025] | 412 | /// std::cerr << "assertion set is" << std::endl; |
---|
| 413 | /// printAssertionSet( assertions, std::cerr, 8 ); |
---|
| 414 | /// std::cerr << "looking for "; |
---|
| 415 | /// assert->print( std::cerr ); |
---|
| 416 | /// std::cerr << std::endl; |
---|
[a32b204] | 417 | AssertionSet::iterator i = assertions.find( assert ); |
---|
| 418 | if ( i != assertions.end() ) { |
---|
[2c57025] | 419 | /// std::cerr << "found it!" << std::endl; |
---|
[6c3a988f] | 420 | i->second.isUsed = true; |
---|
[a32b204] | 421 | } // if |
---|
| 422 | } |
---|
| 423 | |
---|
| 424 | void markAssertions( AssertionSet &assertion1, AssertionSet &assertion2, Type *type ) { |
---|
[aefcc3b] | 425 | for ( std::list< TypeDecl* >::const_iterator tyvar = type->get_forall().begin(); tyvar != type->get_forall().end(); ++tyvar ) { |
---|
[a32b204] | 426 | for ( std::list< DeclarationWithType* >::const_iterator assert = (*tyvar)->get_assertions().begin(); assert != (*tyvar)->get_assertions().end(); ++assert ) { |
---|
| 427 | markAssertionSet( assertion1, *assert ); |
---|
| 428 | markAssertionSet( assertion2, *assert ); |
---|
| 429 | } // for |
---|
| 430 | } // for |
---|
| 431 | } |
---|
| 432 | |
---|
| 433 | void Unify::visit(PointerType *pointerType) { |
---|
| 434 | if ( PointerType *otherPointer = dynamic_cast< PointerType* >( type2 ) ) { |
---|
| 435 | result = unifyExact( pointerType->get_base(), otherPointer->get_base(), env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer ); |
---|
| 436 | markAssertions( haveAssertions, needAssertions, pointerType ); |
---|
| 437 | markAssertions( haveAssertions, needAssertions, otherPointer ); |
---|
| 438 | } // if |
---|
| 439 | } |
---|
| 440 | |
---|
| 441 | void Unify::visit(ArrayType *arrayType) { |
---|
| 442 | ArrayType *otherArray = dynamic_cast< ArrayType* >( type2 ); |
---|
[1cbca6e] | 443 | // to unify, array types must both be VLA or both not VLA |
---|
| 444 | // and must both have a dimension expression or not have a dimension |
---|
[41a2620] | 445 | if ( otherArray && arrayType->get_isVarLen() == otherArray->get_isVarLen() ) { |
---|
[1cbca6e] | 446 | |
---|
| 447 | // not positive this is correct in all cases, but it's needed for typedefs |
---|
| 448 | if ( arrayType->get_isVarLen() || otherArray->get_isVarLen() ) { |
---|
| 449 | return; |
---|
| 450 | } |
---|
| 451 | |
---|
| 452 | if ( ! arrayType->get_isVarLen() && ! otherArray->get_isVarLen() && |
---|
| 453 | arrayType->get_dimension() != 0 && otherArray->get_dimension() != 0 ) { |
---|
| 454 | ConstantExpr * ce1 = dynamic_cast< ConstantExpr * >( arrayType->get_dimension() ); |
---|
| 455 | ConstantExpr * ce2 = dynamic_cast< ConstantExpr * >( otherArray->get_dimension() ); |
---|
[41a2620] | 456 | // see C11 Reference Manual 6.7.6.2.6 |
---|
| 457 | // two array types with size specifiers that are integer constant expressions are |
---|
| 458 | // compatible if both size specifiers have the same constant value |
---|
| 459 | if ( ce1 && ce2 ) { |
---|
| 460 | Constant * c1 = ce1->get_constant(); |
---|
| 461 | Constant * c2 = ce2->get_constant(); |
---|
| 462 | |
---|
| 463 | if ( c1->get_value() != c2->get_value() ) { |
---|
| 464 | // does not unify if the dimension is different |
---|
| 465 | return; |
---|
| 466 | } |
---|
[1cbca6e] | 467 | } |
---|
| 468 | } |
---|
| 469 | |
---|
[a32b204] | 470 | result = unifyExact( arrayType->get_base(), otherArray->get_base(), env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer ); |
---|
| 471 | } // if |
---|
| 472 | } |
---|
| 473 | |
---|
[f3b0a07] | 474 | template< typename Iterator, typename Func > |
---|
| 475 | std::unique_ptr<Type> combineTypes( Iterator begin, Iterator end, Func & toType ) { |
---|
[53e3b4a] | 476 | std::list< Type * > types; |
---|
| 477 | for ( ; begin != end; ++begin ) { |
---|
[64eae56] | 478 | // it's guaranteed that a ttype variable will be bound to a flat tuple, so ensure that this results in a flat tuple |
---|
[f3b0a07] | 479 | flatten( toType( *begin ), back_inserter( types ) ); |
---|
[53e3b4a] | 480 | } |
---|
[6c3a988f] | 481 | return std::unique_ptr<Type>( new TupleType( Type::Qualifiers(), types ) ); |
---|
[53e3b4a] | 482 | } |
---|
| 483 | |
---|
[a32b204] | 484 | template< typename Iterator1, typename Iterator2 > |
---|
| 485 | bool unifyDeclList( Iterator1 list1Begin, Iterator1 list1End, Iterator2 list2Begin, Iterator2 list2End, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, const SymTab::Indexer &indexer ) { |
---|
[f3b0a07] | 486 | auto get_type = [](DeclarationWithType * dwt){ return dwt->get_type(); }; |
---|
[a32b204] | 487 | for ( ; list1Begin != list1End && list2Begin != list2End; ++list1Begin, ++list2Begin ) { |
---|
[53e3b4a] | 488 | Type * t1 = (*list1Begin)->get_type(); |
---|
| 489 | Type * t2 = (*list2Begin)->get_type(); |
---|
| 490 | bool isTtype1 = Tuples::isTtype( t1 ); |
---|
| 491 | bool isTtype2 = Tuples::isTtype( t2 ); |
---|
[6c3a988f] | 492 | // xxx - assumes ttype must be last parameter |
---|
| 493 | // xxx - there may be a nice way to refactor this, but be careful because the argument positioning might matter in some cases. |
---|
[53e3b4a] | 494 | if ( isTtype1 && ! isTtype2 ) { |
---|
| 495 | // combine all of the things in list2, then unify |
---|
[f3b0a07] | 496 | return unifyExact( t1, combineTypes( list2Begin, list2End, get_type ).get(), env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer ); |
---|
[53e3b4a] | 497 | } else if ( isTtype2 && ! isTtype1 ) { |
---|
| 498 | // combine all of the things in list1, then unify |
---|
[f3b0a07] | 499 | return unifyExact( combineTypes( list1Begin, list1End, get_type ).get(), t2, env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer ); |
---|
[53e3b4a] | 500 | } else if ( ! unifyExact( t1, t2, env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer ) ) { |
---|
[a32b204] | 501 | return false; |
---|
| 502 | } // if |
---|
| 503 | } // for |
---|
[6c3a988f] | 504 | // 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 |
---|
[4c8621ac] | 505 | if ( list1Begin != list1End ) { |
---|
| 506 | // try unifying empty tuple type with ttype |
---|
| 507 | Type * t1 = (*list1Begin)->get_type(); |
---|
| 508 | if ( Tuples::isTtype( t1 ) ) { |
---|
[f3b0a07] | 509 | return unifyExact( t1, combineTypes( list2Begin, list2End, get_type ).get(), env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer ); |
---|
[4c8621ac] | 510 | } else return false; |
---|
| 511 | } else if ( list2Begin != list2End ) { |
---|
| 512 | // try unifying empty tuple type with ttype |
---|
| 513 | Type * t2 = (*list2Begin)->get_type(); |
---|
| 514 | if ( Tuples::isTtype( t2 ) ) { |
---|
[f3b0a07] | 515 | return unifyExact( combineTypes( list1Begin, list1End, get_type ).get(), t2, env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer ); |
---|
[4c8621ac] | 516 | } else return false; |
---|
[a32b204] | 517 | } else { |
---|
| 518 | return true; |
---|
| 519 | } // if |
---|
| 520 | } |
---|
| 521 | |
---|
[6c3a988f] | 522 | /// Finds ttypes and replaces them with their expansion, if known. |
---|
| 523 | /// This needs to be done so that satisfying ttype assertions is easier. |
---|
| 524 | /// If this isn't done then argument lists can have wildly different |
---|
| 525 | /// size and structure, when they should be compatible. |
---|
| 526 | struct TtypeExpander : public Mutator { |
---|
| 527 | TypeEnvironment & env; |
---|
| 528 | TtypeExpander( TypeEnvironment & env ) : env( env ) {} |
---|
| 529 | Type * mutate( TypeInstType * typeInst ) { |
---|
| 530 | EqvClass eqvClass; |
---|
| 531 | if ( env.lookup( typeInst->get_name(), eqvClass ) ) { |
---|
| 532 | if ( eqvClass.data.kind == TypeDecl::Ttype ) { |
---|
| 533 | // expand ttype parameter into its actual type |
---|
| 534 | if ( eqvClass.type ) { |
---|
| 535 | delete typeInst; |
---|
| 536 | return eqvClass.type->clone(); |
---|
| 537 | } |
---|
| 538 | } |
---|
| 539 | } |
---|
| 540 | return typeInst; |
---|
| 541 | } |
---|
| 542 | }; |
---|
| 543 | |
---|
| 544 | /// flattens a list of declarations, so that each tuple type has a single declaration. |
---|
| 545 | /// makes use of TtypeExpander to ensure ttypes are flat as well. |
---|
| 546 | void flattenList( std::list< DeclarationWithType * > src, std::list< DeclarationWithType * > & dst, TypeEnvironment & env ) { |
---|
| 547 | dst.clear(); |
---|
| 548 | for ( DeclarationWithType * dcl : src ) { |
---|
| 549 | TtypeExpander expander( env ); |
---|
| 550 | dcl->acceptMutator( expander ); |
---|
| 551 | std::list< Type * > types; |
---|
| 552 | flatten( dcl->get_type(), back_inserter( types ) ); |
---|
| 553 | for ( Type * t : types ) { |
---|
[68fe077a] | 554 | dst.push_back( new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::C, nullptr, t, nullptr ) ); |
---|
[6c3a988f] | 555 | } |
---|
| 556 | delete dcl; |
---|
| 557 | } |
---|
| 558 | } |
---|
| 559 | |
---|
[a32b204] | 560 | void Unify::visit(FunctionType *functionType) { |
---|
| 561 | FunctionType *otherFunction = dynamic_cast< FunctionType* >( type2 ); |
---|
| 562 | if ( otherFunction && functionType->get_isVarArgs() == otherFunction->get_isVarArgs() ) { |
---|
[6c3a988f] | 563 | // flatten the parameter lists for both functions so that tuple structure |
---|
| 564 | // doesn't affect unification. Must be a clone so that the types don't change. |
---|
| 565 | std::unique_ptr<FunctionType> flatFunc( functionType->clone() ); |
---|
| 566 | std::unique_ptr<FunctionType> flatOther( otherFunction->clone() ); |
---|
| 567 | flattenList( flatFunc->get_parameters(), flatFunc->get_parameters(), env ); |
---|
| 568 | flattenList( flatOther->get_parameters(), flatOther->get_parameters(), env ); |
---|
| 569 | |
---|
[53e3b4a] | 570 | // sizes don't have to match if ttypes are involved; need to be more precise wrt where the ttype is to prevent errors |
---|
[6c3a988f] | 571 | if ( (flatFunc->get_parameters().size() == flatOther->get_parameters().size() && flatFunc->get_returnVals().size() == flatOther->get_returnVals().size()) || flatFunc->isTtype() || flatOther->isTtype() ) { |
---|
| 572 | if ( unifyDeclList( flatFunc->get_parameters().begin(), flatFunc->get_parameters().end(), flatOther->get_parameters().begin(), flatOther->get_parameters().end(), env, needAssertions, haveAssertions, openVars, indexer ) ) { |
---|
| 573 | if ( unifyDeclList( flatFunc->get_returnVals().begin(), flatFunc->get_returnVals().end(), flatOther->get_returnVals().begin(), flatOther->get_returnVals().end(), env, needAssertions, haveAssertions, openVars, indexer ) ) { |
---|
[1cbca6e] | 574 | |
---|
[6c3a988f] | 575 | // the original types must be used in mark assertions, since pointer comparisons are used |
---|
[03da511] | 576 | markAssertions( haveAssertions, needAssertions, functionType ); |
---|
| 577 | markAssertions( haveAssertions, needAssertions, otherFunction ); |
---|
[41a2620] | 578 | |
---|
[03da511] | 579 | result = true; |
---|
| 580 | } // if |
---|
[a32b204] | 581 | } // if |
---|
| 582 | } // if |
---|
| 583 | } // if |
---|
| 584 | } |
---|
| 585 | |
---|
| 586 | template< typename RefType > |
---|
[02ec390] | 587 | void Unify::handleRefType( RefType *inst, Type *other ) { |
---|
| 588 | // check that other type is compatible and named the same |
---|
[a32b204] | 589 | RefType *otherStruct = dynamic_cast< RefType* >( other ); |
---|
| 590 | result = otherStruct && inst->get_name() == otherStruct->get_name(); |
---|
[02ec390] | 591 | } |
---|
| 592 | |
---|
| 593 | template< typename RefType > |
---|
| 594 | void Unify::handleGenericRefType( RefType *inst, Type *other ) { |
---|
| 595 | // Check that other type is compatible and named the same |
---|
| 596 | handleRefType( inst, other ); |
---|
| 597 | if ( ! result ) return; |
---|
[f5234f3] | 598 | // Check that parameters of types unify, if any |
---|
[02ec390] | 599 | std::list< Expression* > params = inst->get_parameters(); |
---|
[f5234f3] | 600 | std::list< Expression* > otherParams = ((RefType*)other)->get_parameters(); |
---|
| 601 | |
---|
| 602 | std::list< Expression* >::const_iterator it = params.begin(), jt = otherParams.begin(); |
---|
| 603 | for ( ; it != params.end() && jt != otherParams.end(); ++it, ++jt ) { |
---|
| 604 | TypeExpr *param = dynamic_cast< TypeExpr* >(*it); |
---|
[b2daebd4] | 605 | assertf(param, "Aggregate parameters should be type expressions"); |
---|
[f5234f3] | 606 | TypeExpr *otherParam = dynamic_cast< TypeExpr* >(*jt); |
---|
[b2daebd4] | 607 | assertf(otherParam, "Aggregate parameters should be type expressions"); |
---|
[0b150ec] | 608 | |
---|
[b2daebd4] | 609 | Type* paramTy = param->get_type(); |
---|
| 610 | Type* otherParamTy = otherParam->get_type(); |
---|
[f5234f3] | 611 | |
---|
[b2daebd4] | 612 | bool tupleParam = Tuples::isTtype( paramTy ); |
---|
| 613 | bool otherTupleParam = Tuples::isTtype( otherParamTy ); |
---|
| 614 | |
---|
| 615 | if ( tupleParam && otherTupleParam ) { |
---|
| 616 | ++it; ++jt; // skip ttype parameters for break |
---|
| 617 | } else if ( tupleParam ) { |
---|
| 618 | // bundle other parameters into tuple to match |
---|
[62423350] | 619 | std::list< Type * > binderTypes; |
---|
[b2daebd4] | 620 | |
---|
| 621 | do { |
---|
[62423350] | 622 | binderTypes.push_back( otherParam->get_type()->clone() ); |
---|
[b2daebd4] | 623 | ++jt; |
---|
| 624 | |
---|
| 625 | if ( jt == otherParams.end() ) break; |
---|
| 626 | |
---|
| 627 | otherParam = dynamic_cast< TypeExpr* >(*jt); |
---|
| 628 | assertf(otherParam, "Aggregate parameters should be type expressions"); |
---|
| 629 | } while (true); |
---|
| 630 | |
---|
[62423350] | 631 | otherParamTy = new TupleType{ paramTy->get_qualifiers(), binderTypes }; |
---|
[b2daebd4] | 632 | ++it; // skip ttype parameter for break |
---|
| 633 | } else if ( otherTupleParam ) { |
---|
| 634 | // bundle parameters into tuple to match other |
---|
[62423350] | 635 | std::list< Type * > binderTypes; |
---|
[b2daebd4] | 636 | |
---|
| 637 | do { |
---|
[62423350] | 638 | binderTypes.push_back( param->get_type()->clone() ); |
---|
[b2daebd4] | 639 | ++it; |
---|
| 640 | |
---|
| 641 | if ( it == params.end() ) break; |
---|
| 642 | |
---|
| 643 | param = dynamic_cast< TypeExpr* >(*it); |
---|
| 644 | assertf(param, "Aggregate parameters should be type expressions"); |
---|
| 645 | } while (true); |
---|
| 646 | |
---|
[62423350] | 647 | paramTy = new TupleType{ otherParamTy->get_qualifiers(), binderTypes }; |
---|
[b2daebd4] | 648 | ++jt; // skip ttype parameter for break |
---|
| 649 | } |
---|
| 650 | |
---|
| 651 | if ( ! unifyExact( paramTy, otherParamTy, env, needAssertions, haveAssertions, openVars, WidenMode(false, false), indexer ) ) { |
---|
[02ec390] | 652 | result = false; |
---|
| 653 | return; |
---|
| 654 | } |
---|
[b2daebd4] | 655 | |
---|
| 656 | // ttype parameter should be last |
---|
| 657 | if ( tupleParam || otherTupleParam ) break; |
---|
[02ec390] | 658 | } |
---|
[f5234f3] | 659 | result = ( it == params.end() && jt == otherParams.end() ); |
---|
[02ec390] | 660 | } |
---|
[a32b204] | 661 | |
---|
| 662 | void Unify::visit(StructInstType *structInst) { |
---|
[02ec390] | 663 | handleGenericRefType( structInst, type2 ); |
---|
[a32b204] | 664 | } |
---|
| 665 | |
---|
| 666 | void Unify::visit(UnionInstType *unionInst) { |
---|
[02ec390] | 667 | handleGenericRefType( unionInst, type2 ); |
---|
[a32b204] | 668 | } |
---|
| 669 | |
---|
| 670 | void Unify::visit(EnumInstType *enumInst) { |
---|
| 671 | handleRefType( enumInst, type2 ); |
---|
| 672 | } |
---|
| 673 | |
---|
[4040425] | 674 | void Unify::visit(TraitInstType *contextInst) { |
---|
[a32b204] | 675 | handleRefType( contextInst, type2 ); |
---|
| 676 | } |
---|
| 677 | |
---|
| 678 | void Unify::visit(TypeInstType *typeInst) { |
---|
| 679 | assert( openVars.find( typeInst->get_name() ) == openVars.end() ); |
---|
| 680 | TypeInstType *otherInst = dynamic_cast< TypeInstType* >( type2 ); |
---|
| 681 | if ( otherInst && typeInst->get_name() == otherInst->get_name() ) { |
---|
| 682 | result = true; |
---|
[51b7345] | 683 | /// } else { |
---|
| 684 | /// NamedTypeDecl *nt = indexer.lookupType( typeInst->get_name() ); |
---|
[a32b204] | 685 | /// if ( nt ) { |
---|
[51b7345] | 686 | /// TypeDecl *type = dynamic_cast< TypeDecl* >( nt ); |
---|
| 687 | /// assert( type ); |
---|
[a32b204] | 688 | /// if ( type->get_base() ) { |
---|
[51b7345] | 689 | /// result = unifyExact( type->get_base(), typeInst, env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer ); |
---|
| 690 | /// } |
---|
| 691 | /// } |
---|
[a32b204] | 692 | } // if |
---|
| 693 | } |
---|
| 694 | |
---|
| 695 | template< typename Iterator1, typename Iterator2 > |
---|
[c77fd8b] | 696 | bool unifyList( Iterator1 list1Begin, Iterator1 list1End, Iterator2 list2Begin, Iterator2 list2End, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, const SymTab::Indexer &indexer ) { |
---|
[f3b0a07] | 697 | auto get_type = [](Type * t) { return t; }; |
---|
[a32b204] | 698 | for ( ; list1Begin != list1End && list2Begin != list2End; ++list1Begin, ++list2Begin ) { |
---|
[f3b0a07] | 699 | Type * t1 = *list1Begin; |
---|
| 700 | Type * t2 = *list2Begin; |
---|
| 701 | bool isTtype1 = Tuples::isTtype( t1 ); |
---|
| 702 | bool isTtype2 = Tuples::isTtype( t2 ); |
---|
| 703 | // xxx - assumes ttype must be last parameter |
---|
| 704 | // xxx - there may be a nice way to refactor this, but be careful because the argument positioning might matter in some cases. |
---|
| 705 | if ( isTtype1 && ! isTtype2 ) { |
---|
| 706 | // combine all of the things in list2, then unify |
---|
| 707 | return unifyExact( t1, combineTypes( list2Begin, list2End, get_type ).get(), env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer ); |
---|
| 708 | } else if ( isTtype2 && ! isTtype1 ) { |
---|
| 709 | // combine all of the things in list1, then unify |
---|
| 710 | return unifyExact( combineTypes( list1Begin, list1End, get_type ).get(), t2, env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer ); |
---|
| 711 | } else if ( ! unifyExact( t1, t2, env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer ) ) { |
---|
[a32b204] | 712 | return false; |
---|
[f3b0a07] | 713 | } // if |
---|
| 714 | |
---|
[a32b204] | 715 | } // for |
---|
[f3b0a07] | 716 | if ( list1Begin != list1End ) { |
---|
| 717 | // try unifying empty tuple type with ttype |
---|
| 718 | Type * t1 = *list1Begin; |
---|
| 719 | if ( Tuples::isTtype( t1 ) ) { |
---|
| 720 | return unifyExact( t1, combineTypes( list2Begin, list2End, get_type ).get(), env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer ); |
---|
| 721 | } else return false; |
---|
| 722 | } else if ( list2Begin != list2End ) { |
---|
| 723 | // try unifying empty tuple type with ttype |
---|
| 724 | Type * t2 = *list2Begin; |
---|
| 725 | if ( Tuples::isTtype( t2 ) ) { |
---|
| 726 | return unifyExact( combineTypes( list1Begin, list1End, get_type ).get(), t2, env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer ); |
---|
| 727 | } else return false; |
---|
[a32b204] | 728 | } else { |
---|
| 729 | return true; |
---|
[f3b0a07] | 730 | } // if |
---|
[a32b204] | 731 | } |
---|
| 732 | |
---|
| 733 | void Unify::visit(TupleType *tupleType) { |
---|
| 734 | if ( TupleType *otherTuple = dynamic_cast< TupleType* >( type2 ) ) { |
---|
[f3b0a07] | 735 | std::unique_ptr<TupleType> flat1( tupleType->clone() ); |
---|
| 736 | std::unique_ptr<TupleType> flat2( otherTuple->clone() ); |
---|
| 737 | std::list<Type *> types1, types2; |
---|
| 738 | |
---|
| 739 | TtypeExpander expander( env ); |
---|
| 740 | flat1->acceptMutator( expander ); |
---|
| 741 | flat2->acceptMutator( expander ); |
---|
| 742 | |
---|
| 743 | flatten( flat1.get(), back_inserter( types1 ) ); |
---|
| 744 | flatten( flat2.get(), back_inserter( types2 ) ); |
---|
| 745 | |
---|
[c77fd8b] | 746 | result = unifyList( types1.begin(), types1.end(), types2.begin(), types2.end(), env, needAssertions, haveAssertions, openVars, indexer ); |
---|
[a32b204] | 747 | } // if |
---|
| 748 | } |
---|
[51b7345] | 749 | |
---|
[af397ef8] | 750 | void Unify::visit( __attribute__((unused)) VarArgsType *varArgsType ) { |
---|
[44b7088] | 751 | result = dynamic_cast< VarArgsType* >( type2 ); |
---|
| 752 | } |
---|
| 753 | |
---|
[af397ef8] | 754 | void Unify::visit( __attribute__((unused)) ZeroType *zeroType ) { |
---|
[89e6ffc] | 755 | result = dynamic_cast< ZeroType* >( type2 ); |
---|
| 756 | } |
---|
| 757 | |
---|
[af397ef8] | 758 | void Unify::visit( __attribute__((unused)) OneType *oneType ) { |
---|
[89e6ffc] | 759 | result = dynamic_cast< OneType* >( type2 ); |
---|
| 760 | } |
---|
| 761 | |
---|
[906e24d] | 762 | // xxx - compute once and store in the FunctionType? |
---|
| 763 | Type * extractResultType( FunctionType * function ) { |
---|
| 764 | if ( function->get_returnVals().size() == 0 ) { |
---|
| 765 | return new VoidType( Type::Qualifiers() ); |
---|
| 766 | } else if ( function->get_returnVals().size() == 1 ) { |
---|
| 767 | return function->get_returnVals().front()->get_type()->clone(); |
---|
| 768 | } else { |
---|
[62423350] | 769 | std::list< Type * > types; |
---|
[906e24d] | 770 | for ( DeclarationWithType * decl : function->get_returnVals() ) { |
---|
[62423350] | 771 | types.push_back( decl->get_type()->clone() ); |
---|
[906e24d] | 772 | } // for |
---|
[62423350] | 773 | return new TupleType( Type::Qualifiers(), types ); |
---|
[906e24d] | 774 | } |
---|
| 775 | } |
---|
[51b7345] | 776 | } // namespace ResolvExpr |
---|
[a32b204] | 777 | |
---|
| 778 | // Local Variables: // |
---|
| 779 | // tab-width: 4 // |
---|
| 780 | // mode: c++ // |
---|
| 781 | // compile-command: "make install" // |
---|
| 782 | // End: // |
---|