[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 | // |
---|
| 7 | // Unify.cc -- |
---|
| 8 | // |
---|
| 9 | // Author : Richard C. Bilson |
---|
| 10 | // Created On : Sun May 17 12:27:10 2015 |
---|
[1cbca6e] | 11 | // Last Modified By : Rob Schluntz |
---|
| 12 | // Last Modified On : Wed Sep 02 14:43:22 2015 |
---|
| 13 | // Update Count : 36 |
---|
[a32b204] | 14 | // |
---|
[51b7345] | 15 | |
---|
| 16 | #include <set> |
---|
| 17 | #include <memory> |
---|
| 18 | |
---|
| 19 | #include "Unify.h" |
---|
| 20 | #include "TypeEnvironment.h" |
---|
| 21 | #include "typeops.h" |
---|
| 22 | #include "FindOpenVars.h" |
---|
| 23 | #include "SynTree/Visitor.h" |
---|
| 24 | #include "SynTree/Type.h" |
---|
| 25 | #include "SynTree/Declaration.h" |
---|
| 26 | #include "SymTab/Indexer.h" |
---|
| 27 | #include "utility.h" |
---|
| 28 | |
---|
| 29 | |
---|
[1cbca6e] | 30 | // #define DEBUG |
---|
[51b7345] | 31 | |
---|
| 32 | namespace ResolvExpr { |
---|
[a32b204] | 33 | struct WidenMode { |
---|
| 34 | WidenMode( bool widenFirst, bool widenSecond ): widenFirst( widenFirst ), widenSecond( widenSecond ) {} |
---|
| 35 | WidenMode &operator|=( const WidenMode &other ) { widenFirst |= other.widenFirst; widenSecond |= other.widenSecond; return *this; } |
---|
| 36 | WidenMode &operator&=( const WidenMode &other ) { widenFirst &= other.widenFirst; widenSecond &= other.widenSecond; return *this; } |
---|
| 37 | WidenMode operator|( const WidenMode &other ) { WidenMode newWM( *this ); newWM |= other; return newWM; } |
---|
| 38 | WidenMode operator&( const WidenMode &other ) { WidenMode newWM( *this ); newWM &= other; return newWM; } |
---|
| 39 | operator bool() { return widenFirst && widenSecond; } |
---|
[51b7345] | 40 | |
---|
[a32b204] | 41 | bool widenFirst : 1, widenSecond : 1; |
---|
| 42 | }; |
---|
[51b7345] | 43 | |
---|
[a32b204] | 44 | class Unify : public Visitor { |
---|
| 45 | public: |
---|
| 46 | Unify( Type *type2, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, WidenMode widenMode, const SymTab::Indexer &indexer ); |
---|
[51b7345] | 47 | |
---|
[a32b204] | 48 | bool get_result() const { return result; } |
---|
| 49 | private: |
---|
| 50 | virtual void visit(VoidType *voidType); |
---|
| 51 | virtual void visit(BasicType *basicType); |
---|
| 52 | virtual void visit(PointerType *pointerType); |
---|
| 53 | virtual void visit(ArrayType *arrayType); |
---|
| 54 | virtual void visit(FunctionType *functionType); |
---|
| 55 | virtual void visit(StructInstType *aggregateUseType); |
---|
| 56 | virtual void visit(UnionInstType *aggregateUseType); |
---|
| 57 | virtual void visit(EnumInstType *aggregateUseType); |
---|
| 58 | virtual void visit(ContextInstType *aggregateUseType); |
---|
| 59 | virtual void visit(TypeInstType *aggregateUseType); |
---|
| 60 | virtual void visit(TupleType *tupleType); |
---|
| 61 | |
---|
| 62 | template< typename RefType > void handleRefType( RefType *inst, Type *other ); |
---|
| 63 | |
---|
| 64 | bool result; |
---|
| 65 | Type *type2; // inherited |
---|
| 66 | TypeEnvironment &env; |
---|
| 67 | AssertionSet &needAssertions; |
---|
| 68 | AssertionSet &haveAssertions; |
---|
| 69 | const OpenVarSet &openVars; |
---|
| 70 | WidenMode widenMode; |
---|
| 71 | Type *commonType; |
---|
| 72 | const SymTab::Indexer &indexer; |
---|
| 73 | }; |
---|
| 74 | |
---|
[eb50842] | 75 | /// Attempts an inexact unification of type1 and type2. |
---|
| 76 | /// Returns false if no such unification; if the types can be unified, sets common (unless they unify exactly and have identical type qualifiers) |
---|
[a32b204] | 77 | bool unifyInexact( Type *type1, Type *type2, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, WidenMode widenMode, const SymTab::Indexer &indexer, Type *&common ); |
---|
| 78 | bool unifyExact( Type *type1, Type *type2, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, WidenMode widenMode, const SymTab::Indexer &indexer ); |
---|
[51b7345] | 79 | |
---|
[a32b204] | 80 | bool typesCompatible( Type *first, Type *second, const SymTab::Indexer &indexer, const TypeEnvironment &env ) { |
---|
| 81 | TypeEnvironment newEnv; |
---|
[1cbca6e] | 82 | OpenVarSet openVars, closedVars; // added closedVars |
---|
[a32b204] | 83 | AssertionSet needAssertions, haveAssertions; |
---|
| 84 | Type *newFirst = first->clone(), *newSecond = second->clone(); |
---|
| 85 | env.apply( newFirst ); |
---|
| 86 | env.apply( newSecond ); |
---|
[1cbca6e] | 87 | |
---|
| 88 | // do we need to do this? Seems like we do, types should be able to be compatible if they |
---|
| 89 | // have free variables that can unify |
---|
| 90 | findOpenVars( newFirst, openVars, closedVars, needAssertions, haveAssertions, false ); |
---|
| 91 | findOpenVars( newSecond, openVars, closedVars, needAssertions, haveAssertions, true ); |
---|
| 92 | |
---|
[a32b204] | 93 | bool result = unifyExact( newFirst, newSecond, newEnv, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer ); |
---|
| 94 | delete newFirst; |
---|
| 95 | delete newSecond; |
---|
| 96 | return result; |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | bool typesCompatibleIgnoreQualifiers( Type *first, Type *second, const SymTab::Indexer &indexer, const TypeEnvironment &env ) { |
---|
| 100 | TypeEnvironment newEnv; |
---|
| 101 | OpenVarSet openVars; |
---|
| 102 | AssertionSet needAssertions, haveAssertions; |
---|
| 103 | Type *newFirst = first->clone(), *newSecond = second->clone(); |
---|
| 104 | env.apply( newFirst ); |
---|
| 105 | env.apply( newSecond ); |
---|
| 106 | newFirst->get_qualifiers() = Type::Qualifiers(); |
---|
| 107 | newSecond->get_qualifiers() = Type::Qualifiers(); |
---|
[51b7345] | 108 | /// std::cout << "first is "; |
---|
| 109 | /// first->print( std::cout ); |
---|
| 110 | /// std::cout << std::endl << "second is "; |
---|
| 111 | /// second->print( std::cout ); |
---|
| 112 | /// std::cout << std::endl << "newFirst is "; |
---|
| 113 | /// newFirst->print( std::cout ); |
---|
| 114 | /// std::cout << std::endl << "newSecond is "; |
---|
| 115 | /// newSecond->print( std::cout ); |
---|
| 116 | /// std::cout << std::endl; |
---|
[a32b204] | 117 | bool result = unifyExact( newFirst, newSecond, newEnv, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer ); |
---|
| 118 | delete newFirst; |
---|
| 119 | delete newSecond; |
---|
| 120 | return result; |
---|
| 121 | } |
---|
| 122 | |
---|
| 123 | bool isFtype( Type *type, const SymTab::Indexer &indexer ) { |
---|
| 124 | if ( dynamic_cast< FunctionType* >( type ) ) { |
---|
| 125 | return true; |
---|
| 126 | } else if ( TypeInstType *typeInst = dynamic_cast< TypeInstType* >( type ) ) { |
---|
| 127 | return typeInst->get_isFtype(); |
---|
| 128 | } // if |
---|
| 129 | return false; |
---|
| 130 | } |
---|
| 131 | |
---|
| 132 | bool tyVarCompatible( TypeDecl::Kind kind, Type *type, const SymTab::Indexer &indexer ) { |
---|
| 133 | switch ( kind ) { |
---|
| 134 | case TypeDecl::Any: |
---|
| 135 | case TypeDecl::Dtype: |
---|
| 136 | return ! isFtype( type, indexer ); |
---|
[51b7345] | 137 | |
---|
[a32b204] | 138 | case TypeDecl::Ftype: |
---|
| 139 | return isFtype( type, indexer ); |
---|
| 140 | } // switch |
---|
| 141 | assert( false ); |
---|
| 142 | return false; |
---|
| 143 | } |
---|
| 144 | |
---|
| 145 | bool bindVar( TypeInstType *typeInst, Type *other, TypeDecl::Kind kind, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, WidenMode widenMode, const SymTab::Indexer &indexer ) { |
---|
| 146 | OpenVarSet::const_iterator tyvar = openVars.find( typeInst->get_name() ); |
---|
| 147 | assert( tyvar != openVars.end() ); |
---|
| 148 | if ( ! tyVarCompatible( tyvar->second, other, indexer ) ) { |
---|
| 149 | return false; |
---|
| 150 | } // if |
---|
| 151 | if ( occurs( other, typeInst->get_name(), env ) ) { |
---|
| 152 | return false; |
---|
| 153 | } // if |
---|
| 154 | EqvClass curClass; |
---|
| 155 | if ( env.lookup( typeInst->get_name(), curClass ) ) { |
---|
| 156 | if ( curClass.type ) { |
---|
| 157 | Type *common = 0; |
---|
[eb50842] | 158 | // attempt to unify equivalence class type (which has qualifiers stripped, so they must be restored) with the type to bind to |
---|
[a32b204] | 159 | std::auto_ptr< Type > newType( curClass.type->clone() ); |
---|
[721f17a] | 160 | newType->get_qualifiers() = typeInst->get_qualifiers(); |
---|
[a32b204] | 161 | if ( unifyInexact( newType.get(), other, env, needAssertions, haveAssertions, openVars, widenMode & WidenMode( curClass.allowWidening, true ), indexer, common ) ) { |
---|
| 162 | if ( common ) { |
---|
| 163 | common->get_qualifiers() = Type::Qualifiers(); |
---|
| 164 | delete curClass.type; |
---|
| 165 | curClass.type = common; |
---|
| 166 | env.add( curClass ); |
---|
| 167 | } // if |
---|
| 168 | return true; |
---|
| 169 | } else { |
---|
| 170 | return false; |
---|
| 171 | } // if |
---|
| 172 | } else { |
---|
| 173 | curClass.type = other->clone(); |
---|
| 174 | curClass.type->get_qualifiers() = Type::Qualifiers(); |
---|
| 175 | curClass.allowWidening = widenMode.widenFirst && widenMode.widenSecond; |
---|
| 176 | env.add( curClass ); |
---|
| 177 | } // if |
---|
| 178 | } else { |
---|
| 179 | EqvClass newClass; |
---|
| 180 | newClass.vars.insert( typeInst->get_name() ); |
---|
| 181 | newClass.type = other->clone(); |
---|
| 182 | newClass.type->get_qualifiers() = Type::Qualifiers(); |
---|
| 183 | newClass.allowWidening = widenMode.widenFirst && widenMode.widenSecond; |
---|
| 184 | newClass.kind = kind; |
---|
| 185 | env.add( newClass ); |
---|
| 186 | } // if |
---|
| 187 | return true; |
---|
| 188 | } |
---|
| 189 | |
---|
| 190 | bool bindVarToVar( TypeInstType *var1, TypeInstType *var2, TypeDecl::Kind kind, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, WidenMode widenMode, const SymTab::Indexer &indexer ) { |
---|
| 191 | bool result = true; |
---|
| 192 | EqvClass class1, class2; |
---|
| 193 | bool hasClass1 = false, hasClass2 = false; |
---|
| 194 | bool widen1 = false, widen2 = false; |
---|
| 195 | Type *type1 = 0, *type2 = 0; |
---|
[51b7345] | 196 | |
---|
[a32b204] | 197 | if ( env.lookup( var1->get_name(), class1 ) ) { |
---|
| 198 | hasClass1 = true; |
---|
| 199 | if ( class1.type ) { |
---|
| 200 | if ( occurs( class1.type, var2->get_name(), env ) ) { |
---|
| 201 | return false; |
---|
| 202 | } // if |
---|
| 203 | type1 = class1.type->clone(); |
---|
| 204 | } // if |
---|
| 205 | widen1 = widenMode.widenFirst && class1.allowWidening; |
---|
| 206 | } // if |
---|
| 207 | if ( env.lookup( var2->get_name(), class2 ) ) { |
---|
| 208 | hasClass2 = true; |
---|
| 209 | if ( class2.type ) { |
---|
| 210 | if ( occurs( class2.type, var1->get_name(), env ) ) { |
---|
| 211 | return false; |
---|
| 212 | } // if |
---|
| 213 | type2 = class2.type->clone(); |
---|
| 214 | } // if |
---|
| 215 | widen2 = widenMode.widenSecond && class2.allowWidening; |
---|
| 216 | } // if |
---|
[51b7345] | 217 | |
---|
[a32b204] | 218 | if ( type1 && type2 ) { |
---|
[51b7345] | 219 | // std::cout << "has type1 && type2" << std::endl; |
---|
[a32b204] | 220 | WidenMode newWidenMode ( widen1, widen2 ); |
---|
| 221 | Type *common = 0; |
---|
| 222 | if ( unifyInexact( type1, type2, env, needAssertions, haveAssertions, openVars, newWidenMode, indexer, common ) ) { |
---|
| 223 | class1.vars.insert( class2.vars.begin(), class2.vars.end() ); |
---|
| 224 | class1.allowWidening = widen1 && widen2; |
---|
| 225 | if ( common ) { |
---|
| 226 | common->get_qualifiers() = Type::Qualifiers(); |
---|
| 227 | delete class1.type; |
---|
| 228 | class1.type = common; |
---|
| 229 | } // if |
---|
| 230 | env.add( class1 ); |
---|
| 231 | } else { |
---|
| 232 | result = false; |
---|
| 233 | } // if |
---|
| 234 | } else if ( hasClass1 && hasClass2 ) { |
---|
| 235 | if ( type1 ) { |
---|
| 236 | class1.vars.insert( class2.vars.begin(), class2.vars.end() ); |
---|
| 237 | class1.allowWidening = widen1; |
---|
| 238 | env.add( class1 ); |
---|
| 239 | } else { |
---|
| 240 | class2.vars.insert( class1.vars.begin(), class1.vars.end() ); |
---|
| 241 | class2.allowWidening = widen2; |
---|
| 242 | env.add( class2 ); |
---|
| 243 | } // if |
---|
| 244 | } else if ( hasClass1 ) { |
---|
| 245 | class1.vars.insert( var2->get_name() ); |
---|
| 246 | class1.allowWidening = widen1; |
---|
| 247 | env.add( class1 ); |
---|
| 248 | } else if ( hasClass2 ) { |
---|
| 249 | class2.vars.insert( var1->get_name() ); |
---|
| 250 | class2.allowWidening = widen2; |
---|
| 251 | env.add( class2 ); |
---|
| 252 | } else { |
---|
| 253 | EqvClass newClass; |
---|
| 254 | newClass.vars.insert( var1->get_name() ); |
---|
| 255 | newClass.vars.insert( var2->get_name() ); |
---|
| 256 | newClass.allowWidening = widen1 && widen2; |
---|
| 257 | newClass.kind = kind; |
---|
| 258 | env.add( newClass ); |
---|
| 259 | } // if |
---|
| 260 | delete type1; |
---|
| 261 | delete type2; |
---|
| 262 | return result; |
---|
| 263 | } |
---|
| 264 | |
---|
| 265 | bool unify( Type *type1, Type *type2, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, OpenVarSet &openVars, const SymTab::Indexer &indexer ) { |
---|
| 266 | OpenVarSet closedVars; |
---|
| 267 | findOpenVars( type1, openVars, closedVars, needAssertions, haveAssertions, false ); |
---|
| 268 | findOpenVars( type2, openVars, closedVars, needAssertions, haveAssertions, true ); |
---|
| 269 | Type *commonType = 0; |
---|
| 270 | if ( unifyInexact( type1, type2, env, needAssertions, haveAssertions, openVars, WidenMode( true, true ), indexer, commonType ) ) { |
---|
| 271 | if ( commonType ) { |
---|
| 272 | delete commonType; |
---|
| 273 | } // if |
---|
| 274 | return true; |
---|
| 275 | } else { |
---|
| 276 | return false; |
---|
| 277 | } // if |
---|
| 278 | } |
---|
| 279 | |
---|
| 280 | bool unify( Type *type1, Type *type2, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, OpenVarSet &openVars, const SymTab::Indexer &indexer, Type *&commonType ) { |
---|
| 281 | OpenVarSet closedVars; |
---|
| 282 | findOpenVars( type1, openVars, closedVars, needAssertions, haveAssertions, false ); |
---|
| 283 | findOpenVars( type2, openVars, closedVars, needAssertions, haveAssertions, true ); |
---|
| 284 | return unifyInexact( type1, type2, env, needAssertions, haveAssertions, openVars, WidenMode( true, true ), indexer, commonType ); |
---|
| 285 | } |
---|
| 286 | |
---|
| 287 | bool unifyExact( Type *type1, Type *type2, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, WidenMode widenMode, const SymTab::Indexer &indexer ) { |
---|
[51b7345] | 288 | #ifdef DEBUG |
---|
[a32b204] | 289 | TypeEnvironment debugEnv( env ); |
---|
[51b7345] | 290 | #endif |
---|
[eb50842] | 291 | if ( type1->get_qualifiers() != type2->get_qualifiers() ) { |
---|
| 292 | return false; |
---|
| 293 | } |
---|
| 294 | |
---|
[a32b204] | 295 | bool result; |
---|
| 296 | TypeInstType *var1 = dynamic_cast< TypeInstType* >( type1 ); |
---|
| 297 | TypeInstType *var2 = dynamic_cast< TypeInstType* >( type2 ); |
---|
| 298 | OpenVarSet::const_iterator entry1, entry2; |
---|
| 299 | if ( var1 ) { |
---|
| 300 | entry1 = openVars.find( var1->get_name() ); |
---|
| 301 | } // if |
---|
| 302 | if ( var2 ) { |
---|
| 303 | entry2 = openVars.find( var2->get_name() ); |
---|
| 304 | } // if |
---|
| 305 | bool isopen1 = var1 && ( entry1 != openVars.end() ); |
---|
| 306 | bool isopen2 = var2 && ( entry2 != openVars.end() ); |
---|
[eb50842] | 307 | |
---|
| 308 | if ( isopen1 && isopen2 && entry1->second == entry2->second ) { |
---|
[a32b204] | 309 | result = bindVarToVar( var1, var2, entry1->second, env, needAssertions, haveAssertions, openVars, widenMode, indexer ); |
---|
| 310 | } else if ( isopen1 ) { |
---|
| 311 | result = bindVar( var1, type2, entry1->second, env, needAssertions, haveAssertions, openVars, widenMode, indexer ); |
---|
| 312 | } else if ( isopen2 ) { |
---|
| 313 | result = bindVar( var2, type1, entry2->second, env, needAssertions, haveAssertions, openVars, widenMode, indexer ); |
---|
| 314 | } else { |
---|
| 315 | Unify comparator( type2, env, needAssertions, haveAssertions, openVars, widenMode, indexer ); |
---|
| 316 | type1->accept( comparator ); |
---|
| 317 | result = comparator.get_result(); |
---|
| 318 | } // if |
---|
[51b7345] | 319 | #ifdef DEBUG |
---|
[a32b204] | 320 | std::cout << "============ unifyExact" << std::endl; |
---|
| 321 | std::cout << "type1 is "; |
---|
| 322 | type1->print( std::cout ); |
---|
| 323 | std::cout << std::endl << "type2 is "; |
---|
| 324 | type2->print( std::cout ); |
---|
| 325 | std::cout << std::endl << "openVars are "; |
---|
| 326 | printOpenVarSet( openVars, std::cout, 8 ); |
---|
| 327 | std::cout << std::endl << "input env is " << std::endl; |
---|
| 328 | debugEnv.print( std::cout, 8 ); |
---|
| 329 | std::cout << std::endl << "result env is " << std::endl; |
---|
| 330 | env.print( std::cout, 8 ); |
---|
| 331 | std::cout << "result is " << result << std::endl; |
---|
[51b7345] | 332 | #endif |
---|
[a32b204] | 333 | return result; |
---|
| 334 | } |
---|
| 335 | |
---|
| 336 | bool unifyExact( Type *type1, Type *type2, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, OpenVarSet &openVars, const SymTab::Indexer &indexer ) { |
---|
| 337 | return unifyExact( type1, type2, env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer ); |
---|
| 338 | } |
---|
| 339 | |
---|
| 340 | bool unifyInexact( Type *type1, Type *type2, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, WidenMode widenMode, const SymTab::Indexer &indexer, Type *&common ) { |
---|
| 341 | Type::Qualifiers tq1 = type1->get_qualifiers(), tq2 = type2->get_qualifiers(); |
---|
| 342 | type1->get_qualifiers() = Type::Qualifiers(); |
---|
| 343 | type2->get_qualifiers() = Type::Qualifiers(); |
---|
| 344 | bool result; |
---|
[51b7345] | 345 | #ifdef DEBUG |
---|
[a32b204] | 346 | std::cout << "unifyInexact type 1 is "; |
---|
| 347 | type1->print( std::cout ); |
---|
| 348 | std::cout << "type 2 is "; |
---|
| 349 | type2->print( std::cout ); |
---|
| 350 | std::cout << std::endl; |
---|
[51b7345] | 351 | #endif |
---|
[a32b204] | 352 | if ( ! unifyExact( type1, type2, env, needAssertions, haveAssertions, openVars, widenMode, indexer ) ) { |
---|
[51b7345] | 353 | #ifdef DEBUG |
---|
[a32b204] | 354 | std::cout << "unifyInexact: no exact unification found" << std::endl; |
---|
[51b7345] | 355 | #endif |
---|
[a32b204] | 356 | if ( ( common = commonType( type1, type2, widenMode.widenFirst, widenMode.widenSecond, indexer, env, openVars ) ) ) { |
---|
| 357 | common->get_qualifiers() = tq1 + tq2; |
---|
[51b7345] | 358 | #ifdef DEBUG |
---|
[a32b204] | 359 | std::cout << "unifyInexact: common type is "; |
---|
| 360 | common->print( std::cout ); |
---|
| 361 | std::cout << std::endl; |
---|
[51b7345] | 362 | #endif |
---|
[a32b204] | 363 | result = true; |
---|
| 364 | } else { |
---|
[51b7345] | 365 | #ifdef DEBUG |
---|
[a32b204] | 366 | std::cout << "unifyInexact: no common type found" << std::endl; |
---|
[51b7345] | 367 | #endif |
---|
[a32b204] | 368 | result = false; |
---|
| 369 | } // if |
---|
| 370 | } else { |
---|
| 371 | if ( tq1 != tq2 ) { |
---|
| 372 | if ( ( tq1 > tq2 || widenMode.widenFirst ) && ( tq2 > tq1 || widenMode.widenSecond ) ) { |
---|
| 373 | common = type1->clone(); |
---|
| 374 | common->get_qualifiers() = tq1 + tq2; |
---|
| 375 | result = true; |
---|
| 376 | } else { |
---|
| 377 | result = false; |
---|
| 378 | } // if |
---|
| 379 | } else { |
---|
| 380 | result = true; |
---|
| 381 | } // if |
---|
| 382 | } // if |
---|
| 383 | type1->get_qualifiers() = tq1; |
---|
| 384 | type2->get_qualifiers() = tq2; |
---|
| 385 | return result; |
---|
| 386 | } |
---|
| 387 | |
---|
| 388 | Unify::Unify( Type *type2, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, WidenMode widenMode, const SymTab::Indexer &indexer ) |
---|
| 389 | : result( false ), type2( type2 ), env( env ), needAssertions( needAssertions ), haveAssertions( haveAssertions ), openVars( openVars ), widenMode( widenMode ), indexer( indexer ) { |
---|
| 390 | } |
---|
| 391 | |
---|
| 392 | void Unify::visit(VoidType *voidType) { |
---|
| 393 | result = dynamic_cast< VoidType* >( type2 ); |
---|
| 394 | } |
---|
| 395 | |
---|
| 396 | void Unify::visit(BasicType *basicType) { |
---|
| 397 | if ( BasicType *otherBasic = dynamic_cast< BasicType* >( type2 ) ) { |
---|
| 398 | result = basicType->get_kind() == otherBasic->get_kind(); |
---|
| 399 | } // if |
---|
| 400 | } |
---|
| 401 | |
---|
| 402 | void markAssertionSet( AssertionSet &assertions, DeclarationWithType *assert ) { |
---|
[51b7345] | 403 | /// std::cout << "assertion set is" << std::endl; |
---|
| 404 | /// printAssertionSet( assertions, std::cout, 8 ); |
---|
| 405 | /// std::cout << "looking for "; |
---|
| 406 | /// assert->print( std::cout ); |
---|
| 407 | /// std::cout << std::endl; |
---|
[a32b204] | 408 | AssertionSet::iterator i = assertions.find( assert ); |
---|
| 409 | if ( i != assertions.end() ) { |
---|
[51b7345] | 410 | /// std::cout << "found it!" << std::endl; |
---|
[a32b204] | 411 | i->second = true; |
---|
| 412 | } // if |
---|
| 413 | } |
---|
| 414 | |
---|
| 415 | void markAssertions( AssertionSet &assertion1, AssertionSet &assertion2, Type *type ) { |
---|
| 416 | for ( std::list< TypeDecl* >::const_iterator tyvar = type->get_forall().begin(); tyvar != type->get_forall().end(); ++tyvar ) { |
---|
| 417 | for ( std::list< DeclarationWithType* >::const_iterator assert = (*tyvar)->get_assertions().begin(); assert != (*tyvar)->get_assertions().end(); ++assert ) { |
---|
| 418 | markAssertionSet( assertion1, *assert ); |
---|
| 419 | markAssertionSet( assertion2, *assert ); |
---|
| 420 | } // for |
---|
| 421 | } // for |
---|
| 422 | } |
---|
| 423 | |
---|
| 424 | void Unify::visit(PointerType *pointerType) { |
---|
| 425 | if ( PointerType *otherPointer = dynamic_cast< PointerType* >( type2 ) ) { |
---|
| 426 | result = unifyExact( pointerType->get_base(), otherPointer->get_base(), env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer ); |
---|
| 427 | markAssertions( haveAssertions, needAssertions, pointerType ); |
---|
| 428 | markAssertions( haveAssertions, needAssertions, otherPointer ); |
---|
| 429 | } // if |
---|
| 430 | } |
---|
| 431 | |
---|
| 432 | void Unify::visit(ArrayType *arrayType) { |
---|
| 433 | ArrayType *otherArray = dynamic_cast< ArrayType* >( type2 ); |
---|
[1cbca6e] | 434 | // to unify, array types must both be VLA or both not VLA |
---|
| 435 | // and must both have a dimension expression or not have a dimension |
---|
| 436 | if ( otherArray && arrayType->get_isVarLen() == otherArray->get_isVarLen() |
---|
| 437 | && ((arrayType->get_dimension() != 0 && otherArray->get_dimension() != 0) |
---|
| 438 | || (arrayType->get_dimension() == 0 && otherArray->get_dimension() == 0))) { |
---|
| 439 | |
---|
| 440 | // not positive this is correct in all cases, but it's needed for typedefs |
---|
| 441 | if ( arrayType->get_isVarLen() || otherArray->get_isVarLen() ) { |
---|
| 442 | return; |
---|
| 443 | } |
---|
| 444 | |
---|
| 445 | if ( ! arrayType->get_isVarLen() && ! otherArray->get_isVarLen() && |
---|
| 446 | arrayType->get_dimension() != 0 && otherArray->get_dimension() != 0 ) { |
---|
| 447 | ConstantExpr * ce1 = dynamic_cast< ConstantExpr * >( arrayType->get_dimension() ); |
---|
| 448 | ConstantExpr * ce2 = dynamic_cast< ConstantExpr * >( otherArray->get_dimension() ); |
---|
| 449 | assert(ce1 && ce2); |
---|
| 450 | |
---|
| 451 | Constant * c1 = ce1->get_constant(); |
---|
| 452 | Constant * c2 = ce2->get_constant(); |
---|
| 453 | |
---|
| 454 | if ( c1->get_value() != c2->get_value() ) { |
---|
| 455 | // does not unify if the dimension is different |
---|
| 456 | return; |
---|
| 457 | } |
---|
| 458 | } |
---|
| 459 | |
---|
[a32b204] | 460 | result = unifyExact( arrayType->get_base(), otherArray->get_base(), env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer ); |
---|
| 461 | } // if |
---|
| 462 | } |
---|
| 463 | |
---|
| 464 | template< typename Iterator1, typename Iterator2 > |
---|
| 465 | bool unifyDeclList( Iterator1 list1Begin, Iterator1 list1End, Iterator2 list2Begin, Iterator2 list2End, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, const SymTab::Indexer &indexer ) { |
---|
| 466 | for ( ; list1Begin != list1End && list2Begin != list2End; ++list1Begin, ++list2Begin ) { |
---|
[1cbca6e] | 467 | // Type * commonType; |
---|
| 468 | // if ( ! unifyInexact( (*list1Begin)->get_type(), (*list2Begin)->get_type(), env, needAssertions, haveAssertions, openVars, WidenMode( true, true ), indexer, commonType ) ) { |
---|
[a32b204] | 469 | if ( ! unifyExact( (*list1Begin)->get_type(), (*list2Begin)->get_type(), env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer ) ) { |
---|
| 470 | return false; |
---|
| 471 | } // if |
---|
| 472 | } // for |
---|
| 473 | if ( list1Begin != list1End || list2Begin != list2End ) { |
---|
| 474 | return false; |
---|
| 475 | } else { |
---|
| 476 | return true; |
---|
| 477 | } // if |
---|
| 478 | } |
---|
| 479 | |
---|
| 480 | void Unify::visit(FunctionType *functionType) { |
---|
| 481 | FunctionType *otherFunction = dynamic_cast< FunctionType* >( type2 ); |
---|
| 482 | if ( otherFunction && functionType->get_isVarArgs() == otherFunction->get_isVarArgs() ) { |
---|
[1cbca6e] | 483 | |
---|
[a32b204] | 484 | if ( unifyDeclList( functionType->get_parameters().begin(), functionType->get_parameters().end(), otherFunction->get_parameters().begin(), otherFunction->get_parameters().end(), env, needAssertions, haveAssertions, openVars, indexer ) ) { |
---|
| 485 | |
---|
| 486 | if ( unifyDeclList( functionType->get_returnVals().begin(), functionType->get_returnVals().end(), otherFunction->get_returnVals().begin(), otherFunction->get_returnVals().end(), env, needAssertions, haveAssertions, openVars, indexer ) ) { |
---|
| 487 | |
---|
| 488 | markAssertions( haveAssertions, needAssertions, functionType ); |
---|
| 489 | markAssertions( haveAssertions, needAssertions, otherFunction ); |
---|
| 490 | |
---|
| 491 | result = true; |
---|
| 492 | } // if |
---|
| 493 | } // if |
---|
| 494 | } // if |
---|
| 495 | } |
---|
| 496 | |
---|
| 497 | template< typename RefType > |
---|
| 498 | void Unify::handleRefType( RefType *inst, Type *other ) { |
---|
| 499 | RefType *otherStruct = dynamic_cast< RefType* >( other ); |
---|
| 500 | result = otherStruct && inst->get_name() == otherStruct->get_name(); |
---|
| 501 | } |
---|
| 502 | |
---|
| 503 | void Unify::visit(StructInstType *structInst) { |
---|
| 504 | handleRefType( structInst, type2 ); |
---|
| 505 | } |
---|
| 506 | |
---|
| 507 | void Unify::visit(UnionInstType *unionInst) { |
---|
| 508 | handleRefType( unionInst, type2 ); |
---|
| 509 | } |
---|
| 510 | |
---|
| 511 | void Unify::visit(EnumInstType *enumInst) { |
---|
| 512 | handleRefType( enumInst, type2 ); |
---|
| 513 | } |
---|
| 514 | |
---|
| 515 | void Unify::visit(ContextInstType *contextInst) { |
---|
| 516 | handleRefType( contextInst, type2 ); |
---|
| 517 | } |
---|
| 518 | |
---|
| 519 | void Unify::visit(TypeInstType *typeInst) { |
---|
| 520 | assert( openVars.find( typeInst->get_name() ) == openVars.end() ); |
---|
| 521 | TypeInstType *otherInst = dynamic_cast< TypeInstType* >( type2 ); |
---|
| 522 | if ( otherInst && typeInst->get_name() == otherInst->get_name() ) { |
---|
| 523 | result = true; |
---|
[51b7345] | 524 | /// } else { |
---|
| 525 | /// NamedTypeDecl *nt = indexer.lookupType( typeInst->get_name() ); |
---|
[a32b204] | 526 | /// if ( nt ) { |
---|
[51b7345] | 527 | /// TypeDecl *type = dynamic_cast< TypeDecl* >( nt ); |
---|
| 528 | /// assert( type ); |
---|
[a32b204] | 529 | /// if ( type->get_base() ) { |
---|
[51b7345] | 530 | /// result = unifyExact( type->get_base(), typeInst, env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer ); |
---|
| 531 | /// } |
---|
| 532 | /// } |
---|
[a32b204] | 533 | } // if |
---|
| 534 | } |
---|
| 535 | |
---|
| 536 | template< typename Iterator1, typename Iterator2 > |
---|
| 537 | bool unifyList( Iterator1 list1Begin, Iterator1 list1End, Iterator2 list2Begin, Iterator2 list2End, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, WidenMode widenMode, const SymTab::Indexer &indexer ) { |
---|
| 538 | for ( ; list1Begin != list1End && list2Begin != list2End; ++list1Begin, ++list2Begin ) { |
---|
| 539 | Type *commonType = 0; |
---|
| 540 | if ( ! unifyInexact( *list1Begin, *list2Begin, env, needAssertions, haveAssertions, openVars, widenMode, indexer, commonType ) ) { |
---|
| 541 | return false; |
---|
| 542 | } |
---|
| 543 | delete commonType; |
---|
| 544 | } // for |
---|
| 545 | if ( list1Begin != list1End || list2Begin != list2End ) { |
---|
| 546 | return false; |
---|
| 547 | } else { |
---|
| 548 | return true; |
---|
| 549 | } //if |
---|
| 550 | } |
---|
| 551 | |
---|
| 552 | void Unify::visit(TupleType *tupleType) { |
---|
| 553 | if ( TupleType *otherTuple = dynamic_cast< TupleType* >( type2 ) ) { |
---|
| 554 | result = unifyList( tupleType->get_types().begin(), tupleType->get_types().end(), otherTuple->get_types().begin(), otherTuple->get_types().end(), env, needAssertions, haveAssertions, openVars, widenMode, indexer ); |
---|
| 555 | } // if |
---|
| 556 | } |
---|
[51b7345] | 557 | |
---|
| 558 | } // namespace ResolvExpr |
---|
[a32b204] | 559 | |
---|
| 560 | // Local Variables: // |
---|
| 561 | // tab-width: 4 // |
---|
| 562 | // mode: c++ // |
---|
| 563 | // compile-command: "make install" // |
---|
| 564 | // End: // |
---|