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