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