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