[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 | //
|
---|
[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"
|
---|
[d3b7937] | 27 | #include "Common/utility.h"
|
---|
[53e3b4a] | 28 | #include "Tuples/Tuples.h"
|
---|
[51b73452] | 29 |
|
---|
[1cbca6e] | 30 | // #define DEBUG
|
---|
[51b73452] | 31 |
|
---|
| 32 | namespace ResolvExpr {
|
---|
| 33 |
|
---|
[a32b204] | 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 );
|
---|
[41a2620] | 37 |
|
---|
[a32b204] | 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);
|
---|
[ce8c12f] | 44 | virtual void visit(ReferenceType *refType);
|
---|
[a32b204] | 45 | virtual void visit(FunctionType *functionType);
|
---|
| 46 | virtual void visit(StructInstType *aggregateUseType);
|
---|
| 47 | virtual void visit(UnionInstType *aggregateUseType);
|
---|
| 48 | virtual void visit(EnumInstType *aggregateUseType);
|
---|
[4040425] | 49 | virtual void visit(TraitInstType *aggregateUseType);
|
---|
[a32b204] | 50 | virtual void visit(TypeInstType *aggregateUseType);
|
---|
| 51 | virtual void visit(TupleType *tupleType);
|
---|
[44b7088] | 52 | virtual void visit(VarArgsType *varArgsType);
|
---|
[89e6ffc] | 53 | virtual void visit(ZeroType *zeroType);
|
---|
| 54 | virtual void visit(OneType *oneType);
|
---|
[a32b204] | 55 |
|
---|
| 56 | template< typename RefType > void handleRefType( RefType *inst, Type *other );
|
---|
[02ec390] | 57 | template< typename RefType > void handleGenericRefType( RefType *inst, Type *other );
|
---|
[a32b204] | 58 |
|
---|
| 59 | bool result;
|
---|
| 60 | Type *type2; // inherited
|
---|
| 61 | TypeEnvironment &env;
|
---|
| 62 | AssertionSet &needAssertions;
|
---|
| 63 | AssertionSet &haveAssertions;
|
---|
| 64 | const OpenVarSet &openVars;
|
---|
| 65 | WidenMode widenMode;
|
---|
| 66 | const SymTab::Indexer &indexer;
|
---|
| 67 | };
|
---|
| 68 |
|
---|
[eb50842] | 69 | /// Attempts an inexact unification of type1 and type2.
|
---|
| 70 | /// Returns false if no such unification; if the types can be unified, sets common (unless they unify exactly and have identical type qualifiers)
|
---|
[a32b204] | 71 | bool unifyInexact( Type *type1, Type *type2, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, WidenMode widenMode, const SymTab::Indexer &indexer, Type *&common );
|
---|
| 72 | bool unifyExact( Type *type1, Type *type2, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, WidenMode widenMode, const SymTab::Indexer &indexer );
|
---|
[41a2620] | 73 |
|
---|
[a32b204] | 74 | bool typesCompatible( Type *first, Type *second, const SymTab::Indexer &indexer, const TypeEnvironment &env ) {
|
---|
| 75 | TypeEnvironment newEnv;
|
---|
[1cbca6e] | 76 | OpenVarSet openVars, closedVars; // added closedVars
|
---|
[a32b204] | 77 | AssertionSet needAssertions, haveAssertions;
|
---|
| 78 | Type *newFirst = first->clone(), *newSecond = second->clone();
|
---|
| 79 | env.apply( newFirst );
|
---|
| 80 | env.apply( newSecond );
|
---|
[1cbca6e] | 81 |
|
---|
| 82 | // do we need to do this? Seems like we do, types should be able to be compatible if they
|
---|
| 83 | // have free variables that can unify
|
---|
| 84 | findOpenVars( newFirst, openVars, closedVars, needAssertions, haveAssertions, false );
|
---|
| 85 | findOpenVars( newSecond, openVars, closedVars, needAssertions, haveAssertions, true );
|
---|
| 86 |
|
---|
[a32b204] | 87 | bool result = unifyExact( newFirst, newSecond, newEnv, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
|
---|
| 88 | delete newFirst;
|
---|
| 89 | delete newSecond;
|
---|
| 90 | return result;
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | bool typesCompatibleIgnoreQualifiers( Type *first, Type *second, const SymTab::Indexer &indexer, const TypeEnvironment &env ) {
|
---|
| 94 | TypeEnvironment newEnv;
|
---|
| 95 | OpenVarSet openVars;
|
---|
| 96 | AssertionSet needAssertions, haveAssertions;
|
---|
| 97 | Type *newFirst = first->clone(), *newSecond = second->clone();
|
---|
| 98 | env.apply( newFirst );
|
---|
| 99 | env.apply( newSecond );
|
---|
| 100 | newFirst->get_qualifiers() = Type::Qualifiers();
|
---|
| 101 | newSecond->get_qualifiers() = Type::Qualifiers();
|
---|
[2c57025] | 102 | /// std::cerr << "first is ";
|
---|
| 103 | /// first->print( std::cerr );
|
---|
| 104 | /// std::cerr << std::endl << "second is ";
|
---|
| 105 | /// second->print( std::cerr );
|
---|
| 106 | /// std::cerr << std::endl << "newFirst is ";
|
---|
| 107 | /// newFirst->print( std::cerr );
|
---|
| 108 | /// std::cerr << std::endl << "newSecond is ";
|
---|
| 109 | /// newSecond->print( std::cerr );
|
---|
| 110 | /// std::cerr << std::endl;
|
---|
[a32b204] | 111 | bool result = unifyExact( newFirst, newSecond, newEnv, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
|
---|
| 112 | delete newFirst;
|
---|
| 113 | delete newSecond;
|
---|
| 114 | return result;
|
---|
| 115 | }
|
---|
| 116 |
|
---|
[d7dc824] | 117 | bool isFtype( Type *type ) {
|
---|
[a32b204] | 118 | if ( dynamic_cast< FunctionType* >( type ) ) {
|
---|
| 119 | return true;
|
---|
| 120 | } else if ( TypeInstType *typeInst = dynamic_cast< TypeInstType* >( type ) ) {
|
---|
| 121 | return typeInst->get_isFtype();
|
---|
| 122 | } // if
|
---|
| 123 | return false;
|
---|
| 124 | }
|
---|
| 125 |
|
---|
[d7dc824] | 126 | bool tyVarCompatible( const TypeDecl::Data & data, Type *type ) {
|
---|
[2c57025] | 127 | switch ( data.kind ) {
|
---|
[a32b204] | 128 | case TypeDecl::Any:
|
---|
| 129 | case TypeDecl::Dtype:
|
---|
[2c57025] | 130 | // to bind to an object type variable, the type must not be a function type.
|
---|
| 131 | // if the type variable is specified to be a complete type then the incoming
|
---|
| 132 | // type must also be complete
|
---|
[6c3a988f] | 133 | // xxx - should this also check that type is not a tuple type and that it's not a ttype?
|
---|
[d7dc824] | 134 | return ! isFtype( type ) && (! data.isComplete || type->isComplete() );
|
---|
[a32b204] | 135 | case TypeDecl::Ftype:
|
---|
[d7dc824] | 136 | return isFtype( type );
|
---|
[b2daebd4] | 137 | case TypeDecl::Ttype:
|
---|
[6c3a988f] | 138 | // ttype unifies with any tuple type
|
---|
[f3b0a07] | 139 | return dynamic_cast< TupleType * >( type ) || Tuples::isTtype( type );
|
---|
[a32b204] | 140 | } // switch
|
---|
| 141 | return false;
|
---|
| 142 | }
|
---|
| 143 |
|
---|
[2c57025] | 144 | 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] | 145 | // remove references from other, so that type variables can only bind to value types
|
---|
| 146 | other = other->stripReferences();
|
---|
[a32b204] | 147 | OpenVarSet::const_iterator tyvar = openVars.find( typeInst->get_name() );
|
---|
| 148 | assert( tyvar != openVars.end() );
|
---|
[d7dc824] | 149 | if ( ! tyVarCompatible( tyvar->second, other ) ) {
|
---|
[a32b204] | 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;
|
---|
[2c57025] | 185 | newClass.data = data;
|
---|
[a32b204] | 186 | env.add( newClass );
|
---|
| 187 | } // if
|
---|
| 188 | return true;
|
---|
| 189 | }
|
---|
| 190 |
|
---|
[2c57025] | 191 | 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] | 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;
|
---|
[41a2620] | 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
|
---|
[41a2620] | 218 |
|
---|
[a32b204] | 219 | if ( type1 && type2 ) {
|
---|
[2c57025] | 220 | // std::cerr << "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;
|
---|
[2c57025] | 258 | newClass.data = data;
|
---|
[a32b204] | 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
|
---|
[2c57025] | 321 | std::cerr << "============ unifyExact" << std::endl;
|
---|
| 322 | std::cerr << "type1 is ";
|
---|
| 323 | type1->print( std::cerr );
|
---|
| 324 | std::cerr << std::endl << "type2 is ";
|
---|
| 325 | type2->print( std::cerr );
|
---|
| 326 | std::cerr << std::endl << "openVars are ";
|
---|
| 327 | printOpenVarSet( openVars, std::cerr, 8 );
|
---|
| 328 | std::cerr << std::endl << "input env is " << std::endl;
|
---|
| 329 | debugEnv.print( std::cerr, 8 );
|
---|
| 330 | std::cerr << std::endl << "result env is " << std::endl;
|
---|
| 331 | env.print( std::cerr, 8 );
|
---|
| 332 | std::cerr << "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
|
---|
[2c57025] | 347 | std::cerr << "unifyInexact type 1 is ";
|
---|
| 348 | type1->print( std::cerr );
|
---|
[0b150ec] | 349 | std::cerr << " type 2 is ";
|
---|
[2c57025] | 350 | type2->print( std::cerr );
|
---|
| 351 | std::cerr << std::endl;
|
---|
[51b73452] | 352 | #endif
|
---|
[a32b204] | 353 | if ( ! unifyExact( type1, type2, env, needAssertions, haveAssertions, openVars, widenMode, indexer ) ) {
|
---|
[51b73452] | 354 | #ifdef DEBUG
|
---|
[2c57025] | 355 | std::cerr << "unifyInexact: no exact unification found" << std::endl;
|
---|
[51b73452] | 356 | #endif
|
---|
[a32b204] | 357 | if ( ( common = commonType( type1, type2, widenMode.widenFirst, widenMode.widenSecond, indexer, env, openVars ) ) ) {
|
---|
[6f95000] | 358 | common->get_qualifiers() = tq1 | tq2;
|
---|
[51b73452] | 359 | #ifdef DEBUG
|
---|
[2c57025] | 360 | std::cerr << "unifyInexact: common type is ";
|
---|
| 361 | common->print( std::cerr );
|
---|
| 362 | std::cerr << std::endl;
|
---|
[51b73452] | 363 | #endif
|
---|
[a32b204] | 364 | result = true;
|
---|
| 365 | } else {
|
---|
[51b73452] | 366 | #ifdef DEBUG
|
---|
[2c57025] | 367 | std::cerr << "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();
|
---|
[6f95000] | 375 | common->get_qualifiers() = tq1 | tq2;
|
---|
[a32b204] | 376 | result = true;
|
---|
| 377 | } else {
|
---|
| 378 | result = false;
|
---|
| 379 | } // if
|
---|
| 380 | } else {
|
---|
[e6cee92] | 381 | common = type1->clone();
|
---|
| 382 | common->get_qualifiers() = tq1 | tq2;
|
---|
[a32b204] | 383 | result = true;
|
---|
| 384 | } // if
|
---|
| 385 | } // if
|
---|
| 386 | type1->get_qualifiers() = tq1;
|
---|
| 387 | type2->get_qualifiers() = tq2;
|
---|
| 388 | return result;
|
---|
| 389 | }
|
---|
| 390 |
|
---|
| 391 | Unify::Unify( Type *type2, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, WidenMode widenMode, const SymTab::Indexer &indexer )
|
---|
| 392 | : result( false ), type2( type2 ), env( env ), needAssertions( needAssertions ), haveAssertions( haveAssertions ), openVars( openVars ), widenMode( widenMode ), indexer( indexer ) {
|
---|
| 393 | }
|
---|
| 394 |
|
---|
[af397ef8] | 395 | void Unify::visit( __attribute__((unused)) VoidType *voidType) {
|
---|
[a32b204] | 396 | result = dynamic_cast< VoidType* >( type2 );
|
---|
| 397 | }
|
---|
| 398 |
|
---|
| 399 | void Unify::visit(BasicType *basicType) {
|
---|
| 400 | if ( BasicType *otherBasic = dynamic_cast< BasicType* >( type2 ) ) {
|
---|
| 401 | result = basicType->get_kind() == otherBasic->get_kind();
|
---|
| 402 | } // if
|
---|
| 403 | }
|
---|
| 404 |
|
---|
| 405 | void markAssertionSet( AssertionSet &assertions, DeclarationWithType *assert ) {
|
---|
[2c57025] | 406 | /// std::cerr << "assertion set is" << std::endl;
|
---|
| 407 | /// printAssertionSet( assertions, std::cerr, 8 );
|
---|
| 408 | /// std::cerr << "looking for ";
|
---|
| 409 | /// assert->print( std::cerr );
|
---|
| 410 | /// std::cerr << std::endl;
|
---|
[a32b204] | 411 | AssertionSet::iterator i = assertions.find( assert );
|
---|
| 412 | if ( i != assertions.end() ) {
|
---|
[2c57025] | 413 | /// std::cerr << "found it!" << std::endl;
|
---|
[6c3a988f] | 414 | i->second.isUsed = true;
|
---|
[a32b204] | 415 | } // if
|
---|
| 416 | }
|
---|
| 417 |
|
---|
| 418 | void markAssertions( AssertionSet &assertion1, AssertionSet &assertion2, Type *type ) {
|
---|
[aefcc3b] | 419 | for ( std::list< TypeDecl* >::const_iterator tyvar = type->get_forall().begin(); tyvar != type->get_forall().end(); ++tyvar ) {
|
---|
[a32b204] | 420 | for ( std::list< DeclarationWithType* >::const_iterator assert = (*tyvar)->get_assertions().begin(); assert != (*tyvar)->get_assertions().end(); ++assert ) {
|
---|
| 421 | markAssertionSet( assertion1, *assert );
|
---|
| 422 | markAssertionSet( assertion2, *assert );
|
---|
| 423 | } // for
|
---|
| 424 | } // for
|
---|
| 425 | }
|
---|
| 426 |
|
---|
| 427 | void Unify::visit(PointerType *pointerType) {
|
---|
| 428 | if ( PointerType *otherPointer = dynamic_cast< PointerType* >( type2 ) ) {
|
---|
| 429 | result = unifyExact( pointerType->get_base(), otherPointer->get_base(), env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
|
---|
| 430 | markAssertions( haveAssertions, needAssertions, pointerType );
|
---|
| 431 | markAssertions( haveAssertions, needAssertions, otherPointer );
|
---|
| 432 | } // if
|
---|
| 433 | }
|
---|
| 434 |
|
---|
[ce8c12f] | 435 | void Unify::visit(ReferenceType *refType) {
|
---|
| 436 | if ( ReferenceType *otherRef = dynamic_cast< ReferenceType* >( type2 ) ) {
|
---|
| 437 | result = unifyExact( refType->get_base(), otherRef->get_base(), env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
|
---|
| 438 | markAssertions( haveAssertions, needAssertions, refType );
|
---|
| 439 | markAssertions( haveAssertions, needAssertions, otherRef );
|
---|
| 440 | } // if
|
---|
| 441 | }
|
---|
| 442 |
|
---|
[a32b204] | 443 | void Unify::visit(ArrayType *arrayType) {
|
---|
| 444 | ArrayType *otherArray = dynamic_cast< ArrayType* >( type2 );
|
---|
[1cbca6e] | 445 | // to unify, array types must both be VLA or both not VLA
|
---|
| 446 | // and must both have a dimension expression or not have a dimension
|
---|
[41a2620] | 447 | if ( otherArray && arrayType->get_isVarLen() == otherArray->get_isVarLen() ) {
|
---|
[1cbca6e] | 448 |
|
---|
| 449 | // not positive this is correct in all cases, but it's needed for typedefs
|
---|
| 450 | if ( arrayType->get_isVarLen() || otherArray->get_isVarLen() ) {
|
---|
| 451 | return;
|
---|
| 452 | }
|
---|
| 453 |
|
---|
| 454 | if ( ! arrayType->get_isVarLen() && ! otherArray->get_isVarLen() &&
|
---|
| 455 | arrayType->get_dimension() != 0 && otherArray->get_dimension() != 0 ) {
|
---|
| 456 | ConstantExpr * ce1 = dynamic_cast< ConstantExpr * >( arrayType->get_dimension() );
|
---|
| 457 | ConstantExpr * ce2 = dynamic_cast< ConstantExpr * >( otherArray->get_dimension() );
|
---|
[41a2620] | 458 | // see C11 Reference Manual 6.7.6.2.6
|
---|
| 459 | // two array types with size specifiers that are integer constant expressions are
|
---|
| 460 | // compatible if both size specifiers have the same constant value
|
---|
| 461 | if ( ce1 && ce2 ) {
|
---|
| 462 | Constant * c1 = ce1->get_constant();
|
---|
| 463 | Constant * c2 = ce2->get_constant();
|
---|
| 464 |
|
---|
| 465 | if ( c1->get_value() != c2->get_value() ) {
|
---|
| 466 | // does not unify if the dimension is different
|
---|
| 467 | return;
|
---|
| 468 | }
|
---|
[1cbca6e] | 469 | }
|
---|
| 470 | }
|
---|
| 471 |
|
---|
[a32b204] | 472 | result = unifyExact( arrayType->get_base(), otherArray->get_base(), env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
|
---|
| 473 | } // if
|
---|
| 474 | }
|
---|
| 475 |
|
---|
[f3b0a07] | 476 | template< typename Iterator, typename Func >
|
---|
| 477 | std::unique_ptr<Type> combineTypes( Iterator begin, Iterator end, Func & toType ) {
|
---|
[53e3b4a] | 478 | std::list< Type * > types;
|
---|
| 479 | for ( ; begin != end; ++begin ) {
|
---|
[64eae56] | 480 | // it's guaranteed that a ttype variable will be bound to a flat tuple, so ensure that this results in a flat tuple
|
---|
[f3b0a07] | 481 | flatten( toType( *begin ), back_inserter( types ) );
|
---|
[53e3b4a] | 482 | }
|
---|
[6c3a988f] | 483 | return std::unique_ptr<Type>( new TupleType( Type::Qualifiers(), types ) );
|
---|
[53e3b4a] | 484 | }
|
---|
| 485 |
|
---|
[a32b204] | 486 | template< typename Iterator1, typename Iterator2 >
|
---|
| 487 | bool unifyDeclList( Iterator1 list1Begin, Iterator1 list1End, Iterator2 list2Begin, Iterator2 list2End, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, const SymTab::Indexer &indexer ) {
|
---|
[f3b0a07] | 488 | auto get_type = [](DeclarationWithType * dwt){ return dwt->get_type(); };
|
---|
[a32b204] | 489 | for ( ; list1Begin != list1End && list2Begin != list2End; ++list1Begin, ++list2Begin ) {
|
---|
[53e3b4a] | 490 | Type * t1 = (*list1Begin)->get_type();
|
---|
| 491 | Type * t2 = (*list2Begin)->get_type();
|
---|
| 492 | bool isTtype1 = Tuples::isTtype( t1 );
|
---|
| 493 | bool isTtype2 = Tuples::isTtype( t2 );
|
---|
[6c3a988f] | 494 | // xxx - assumes ttype must be last parameter
|
---|
| 495 | // xxx - there may be a nice way to refactor this, but be careful because the argument positioning might matter in some cases.
|
---|
[53e3b4a] | 496 | if ( isTtype1 && ! isTtype2 ) {
|
---|
| 497 | // combine all of the things in list2, then unify
|
---|
[f3b0a07] | 498 | return unifyExact( t1, combineTypes( list2Begin, list2End, get_type ).get(), env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
|
---|
[53e3b4a] | 499 | } else if ( isTtype2 && ! isTtype1 ) {
|
---|
| 500 | // combine all of the things in list1, then unify
|
---|
[f3b0a07] | 501 | return unifyExact( combineTypes( list1Begin, list1End, get_type ).get(), t2, env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
|
---|
[53e3b4a] | 502 | } else if ( ! unifyExact( t1, t2, env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer ) ) {
|
---|
[a32b204] | 503 | return false;
|
---|
| 504 | } // if
|
---|
| 505 | } // for
|
---|
[6c3a988f] | 506 | // 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] | 507 | if ( list1Begin != list1End ) {
|
---|
| 508 | // try unifying empty tuple type with ttype
|
---|
| 509 | Type * t1 = (*list1Begin)->get_type();
|
---|
| 510 | if ( Tuples::isTtype( t1 ) ) {
|
---|
[f3b0a07] | 511 | return unifyExact( t1, combineTypes( list2Begin, list2End, get_type ).get(), env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
|
---|
[4c8621ac] | 512 | } else return false;
|
---|
| 513 | } else if ( list2Begin != list2End ) {
|
---|
| 514 | // try unifying empty tuple type with ttype
|
---|
| 515 | Type * t2 = (*list2Begin)->get_type();
|
---|
| 516 | if ( Tuples::isTtype( t2 ) ) {
|
---|
[f3b0a07] | 517 | return unifyExact( combineTypes( list1Begin, list1End, get_type ).get(), t2, env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
|
---|
[4c8621ac] | 518 | } else return false;
|
---|
[a32b204] | 519 | } else {
|
---|
| 520 | return true;
|
---|
| 521 | } // if
|
---|
| 522 | }
|
---|
| 523 |
|
---|
[6c3a988f] | 524 | /// Finds ttypes and replaces them with their expansion, if known.
|
---|
| 525 | /// This needs to be done so that satisfying ttype assertions is easier.
|
---|
| 526 | /// If this isn't done then argument lists can have wildly different
|
---|
| 527 | /// size and structure, when they should be compatible.
|
---|
| 528 | struct TtypeExpander : public Mutator {
|
---|
| 529 | TypeEnvironment & env;
|
---|
| 530 | TtypeExpander( TypeEnvironment & env ) : env( env ) {}
|
---|
| 531 | Type * mutate( TypeInstType * typeInst ) {
|
---|
| 532 | EqvClass eqvClass;
|
---|
| 533 | if ( env.lookup( typeInst->get_name(), eqvClass ) ) {
|
---|
| 534 | if ( eqvClass.data.kind == TypeDecl::Ttype ) {
|
---|
| 535 | // expand ttype parameter into its actual type
|
---|
| 536 | if ( eqvClass.type ) {
|
---|
| 537 | delete typeInst;
|
---|
| 538 | return eqvClass.type->clone();
|
---|
| 539 | }
|
---|
| 540 | }
|
---|
| 541 | }
|
---|
| 542 | return typeInst;
|
---|
| 543 | }
|
---|
| 544 | };
|
---|
| 545 |
|
---|
| 546 | /// flattens a list of declarations, so that each tuple type has a single declaration.
|
---|
| 547 | /// makes use of TtypeExpander to ensure ttypes are flat as well.
|
---|
| 548 | void flattenList( std::list< DeclarationWithType * > src, std::list< DeclarationWithType * > & dst, TypeEnvironment & env ) {
|
---|
| 549 | dst.clear();
|
---|
| 550 | for ( DeclarationWithType * dcl : src ) {
|
---|
| 551 | TtypeExpander expander( env );
|
---|
| 552 | dcl->acceptMutator( expander );
|
---|
| 553 | std::list< Type * > types;
|
---|
| 554 | flatten( dcl->get_type(), back_inserter( types ) );
|
---|
| 555 | for ( Type * t : types ) {
|
---|
[68fe077a] | 556 | dst.push_back( new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::C, nullptr, t, nullptr ) );
|
---|
[6c3a988f] | 557 | }
|
---|
| 558 | delete dcl;
|
---|
| 559 | }
|
---|
| 560 | }
|
---|
| 561 |
|
---|
[a32b204] | 562 | void Unify::visit(FunctionType *functionType) {
|
---|
| 563 | FunctionType *otherFunction = dynamic_cast< FunctionType* >( type2 );
|
---|
| 564 | if ( otherFunction && functionType->get_isVarArgs() == otherFunction->get_isVarArgs() ) {
|
---|
[6c3a988f] | 565 | // flatten the parameter lists for both functions so that tuple structure
|
---|
| 566 | // doesn't affect unification. Must be a clone so that the types don't change.
|
---|
| 567 | std::unique_ptr<FunctionType> flatFunc( functionType->clone() );
|
---|
| 568 | std::unique_ptr<FunctionType> flatOther( otherFunction->clone() );
|
---|
| 569 | flattenList( flatFunc->get_parameters(), flatFunc->get_parameters(), env );
|
---|
| 570 | flattenList( flatOther->get_parameters(), flatOther->get_parameters(), env );
|
---|
| 571 |
|
---|
[53e3b4a] | 572 | // sizes don't have to match if ttypes are involved; need to be more precise wrt where the ttype is to prevent errors
|
---|
[6c3a988f] | 573 | if ( (flatFunc->get_parameters().size() == flatOther->get_parameters().size() && flatFunc->get_returnVals().size() == flatOther->get_returnVals().size()) || flatFunc->isTtype() || flatOther->isTtype() ) {
|
---|
| 574 | if ( unifyDeclList( flatFunc->get_parameters().begin(), flatFunc->get_parameters().end(), flatOther->get_parameters().begin(), flatOther->get_parameters().end(), env, needAssertions, haveAssertions, openVars, indexer ) ) {
|
---|
| 575 | if ( unifyDeclList( flatFunc->get_returnVals().begin(), flatFunc->get_returnVals().end(), flatOther->get_returnVals().begin(), flatOther->get_returnVals().end(), env, needAssertions, haveAssertions, openVars, indexer ) ) {
|
---|
[1cbca6e] | 576 |
|
---|
[6c3a988f] | 577 | // the original types must be used in mark assertions, since pointer comparisons are used
|
---|
[03da511] | 578 | markAssertions( haveAssertions, needAssertions, functionType );
|
---|
| 579 | markAssertions( haveAssertions, needAssertions, otherFunction );
|
---|
[41a2620] | 580 |
|
---|
[03da511] | 581 | result = true;
|
---|
| 582 | } // if
|
---|
[a32b204] | 583 | } // if
|
---|
| 584 | } // if
|
---|
| 585 | } // if
|
---|
| 586 | }
|
---|
| 587 |
|
---|
| 588 | template< typename RefType >
|
---|
[02ec390] | 589 | void Unify::handleRefType( RefType *inst, Type *other ) {
|
---|
| 590 | // check that other type is compatible and named the same
|
---|
[a32b204] | 591 | RefType *otherStruct = dynamic_cast< RefType* >( other );
|
---|
| 592 | result = otherStruct && inst->get_name() == otherStruct->get_name();
|
---|
[02ec390] | 593 | }
|
---|
| 594 |
|
---|
| 595 | template< typename RefType >
|
---|
| 596 | void Unify::handleGenericRefType( RefType *inst, Type *other ) {
|
---|
| 597 | // Check that other type is compatible and named the same
|
---|
| 598 | handleRefType( inst, other );
|
---|
| 599 | if ( ! result ) return;
|
---|
[f5234f3] | 600 | // Check that parameters of types unify, if any
|
---|
[02ec390] | 601 | std::list< Expression* > params = inst->get_parameters();
|
---|
[f5234f3] | 602 | std::list< Expression* > otherParams = ((RefType*)other)->get_parameters();
|
---|
| 603 |
|
---|
| 604 | std::list< Expression* >::const_iterator it = params.begin(), jt = otherParams.begin();
|
---|
| 605 | for ( ; it != params.end() && jt != otherParams.end(); ++it, ++jt ) {
|
---|
| 606 | TypeExpr *param = dynamic_cast< TypeExpr* >(*it);
|
---|
[b2daebd4] | 607 | assertf(param, "Aggregate parameters should be type expressions");
|
---|
[f5234f3] | 608 | TypeExpr *otherParam = dynamic_cast< TypeExpr* >(*jt);
|
---|
[b2daebd4] | 609 | assertf(otherParam, "Aggregate parameters should be type expressions");
|
---|
[ce8c12f] | 610 |
|
---|
[b2daebd4] | 611 | Type* paramTy = param->get_type();
|
---|
| 612 | Type* otherParamTy = otherParam->get_type();
|
---|
[f5234f3] | 613 |
|
---|
[b2daebd4] | 614 | bool tupleParam = Tuples::isTtype( paramTy );
|
---|
| 615 | bool otherTupleParam = Tuples::isTtype( otherParamTy );
|
---|
| 616 |
|
---|
| 617 | if ( tupleParam && otherTupleParam ) {
|
---|
| 618 | ++it; ++jt; // skip ttype parameters for break
|
---|
| 619 | } else if ( tupleParam ) {
|
---|
| 620 | // bundle other parameters into tuple to match
|
---|
[62423350] | 621 | std::list< Type * > binderTypes;
|
---|
[b2daebd4] | 622 |
|
---|
| 623 | do {
|
---|
[62423350] | 624 | binderTypes.push_back( otherParam->get_type()->clone() );
|
---|
[b2daebd4] | 625 | ++jt;
|
---|
| 626 |
|
---|
| 627 | if ( jt == otherParams.end() ) break;
|
---|
| 628 |
|
---|
| 629 | otherParam = dynamic_cast< TypeExpr* >(*jt);
|
---|
| 630 | assertf(otherParam, "Aggregate parameters should be type expressions");
|
---|
| 631 | } while (true);
|
---|
| 632 |
|
---|
[62423350] | 633 | otherParamTy = new TupleType{ paramTy->get_qualifiers(), binderTypes };
|
---|
[b2daebd4] | 634 | ++it; // skip ttype parameter for break
|
---|
| 635 | } else if ( otherTupleParam ) {
|
---|
| 636 | // bundle parameters into tuple to match other
|
---|
[62423350] | 637 | std::list< Type * > binderTypes;
|
---|
[b2daebd4] | 638 |
|
---|
| 639 | do {
|
---|
[62423350] | 640 | binderTypes.push_back( param->get_type()->clone() );
|
---|
[b2daebd4] | 641 | ++it;
|
---|
| 642 |
|
---|
| 643 | if ( it == params.end() ) break;
|
---|
| 644 |
|
---|
| 645 | param = dynamic_cast< TypeExpr* >(*it);
|
---|
| 646 | assertf(param, "Aggregate parameters should be type expressions");
|
---|
| 647 | } while (true);
|
---|
| 648 |
|
---|
[62423350] | 649 | paramTy = new TupleType{ otherParamTy->get_qualifiers(), binderTypes };
|
---|
[b2daebd4] | 650 | ++jt; // skip ttype parameter for break
|
---|
| 651 | }
|
---|
| 652 |
|
---|
| 653 | if ( ! unifyExact( paramTy, otherParamTy, env, needAssertions, haveAssertions, openVars, WidenMode(false, false), indexer ) ) {
|
---|
[02ec390] | 654 | result = false;
|
---|
| 655 | return;
|
---|
| 656 | }
|
---|
[b2daebd4] | 657 |
|
---|
| 658 | // ttype parameter should be last
|
---|
| 659 | if ( tupleParam || otherTupleParam ) break;
|
---|
[02ec390] | 660 | }
|
---|
[f5234f3] | 661 | result = ( it == params.end() && jt == otherParams.end() );
|
---|
[02ec390] | 662 | }
|
---|
[a32b204] | 663 |
|
---|
| 664 | void Unify::visit(StructInstType *structInst) {
|
---|
[02ec390] | 665 | handleGenericRefType( structInst, type2 );
|
---|
[a32b204] | 666 | }
|
---|
| 667 |
|
---|
| 668 | void Unify::visit(UnionInstType *unionInst) {
|
---|
[02ec390] | 669 | handleGenericRefType( unionInst, type2 );
|
---|
[a32b204] | 670 | }
|
---|
| 671 |
|
---|
| 672 | void Unify::visit(EnumInstType *enumInst) {
|
---|
| 673 | handleRefType( enumInst, type2 );
|
---|
| 674 | }
|
---|
| 675 |
|
---|
[4040425] | 676 | void Unify::visit(TraitInstType *contextInst) {
|
---|
[a32b204] | 677 | handleRefType( contextInst, type2 );
|
---|
| 678 | }
|
---|
| 679 |
|
---|
| 680 | void Unify::visit(TypeInstType *typeInst) {
|
---|
| 681 | assert( openVars.find( typeInst->get_name() ) == openVars.end() );
|
---|
| 682 | TypeInstType *otherInst = dynamic_cast< TypeInstType* >( type2 );
|
---|
| 683 | if ( otherInst && typeInst->get_name() == otherInst->get_name() ) {
|
---|
| 684 | result = true;
|
---|
[51b73452] | 685 | /// } else {
|
---|
| 686 | /// NamedTypeDecl *nt = indexer.lookupType( typeInst->get_name() );
|
---|
[a32b204] | 687 | /// if ( nt ) {
|
---|
[51b73452] | 688 | /// TypeDecl *type = dynamic_cast< TypeDecl* >( nt );
|
---|
| 689 | /// assert( type );
|
---|
[a32b204] | 690 | /// if ( type->get_base() ) {
|
---|
[51b73452] | 691 | /// result = unifyExact( type->get_base(), typeInst, env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
|
---|
| 692 | /// }
|
---|
| 693 | /// }
|
---|
[a32b204] | 694 | } // if
|
---|
| 695 | }
|
---|
| 696 |
|
---|
| 697 | template< typename Iterator1, typename Iterator2 >
|
---|
[c77fd8b] | 698 | bool unifyList( Iterator1 list1Begin, Iterator1 list1End, Iterator2 list2Begin, Iterator2 list2End, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, const SymTab::Indexer &indexer ) {
|
---|
[f3b0a07] | 699 | auto get_type = [](Type * t) { return t; };
|
---|
[a32b204] | 700 | for ( ; list1Begin != list1End && list2Begin != list2End; ++list1Begin, ++list2Begin ) {
|
---|
[f3b0a07] | 701 | Type * t1 = *list1Begin;
|
---|
| 702 | Type * t2 = *list2Begin;
|
---|
| 703 | bool isTtype1 = Tuples::isTtype( t1 );
|
---|
| 704 | bool isTtype2 = Tuples::isTtype( t2 );
|
---|
| 705 | // xxx - assumes ttype must be last parameter
|
---|
| 706 | // xxx - there may be a nice way to refactor this, but be careful because the argument positioning might matter in some cases.
|
---|
| 707 | if ( isTtype1 && ! isTtype2 ) {
|
---|
| 708 | // combine all of the things in list2, then unify
|
---|
| 709 | return unifyExact( t1, combineTypes( list2Begin, list2End, get_type ).get(), env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
|
---|
| 710 | } else if ( isTtype2 && ! isTtype1 ) {
|
---|
| 711 | // combine all of the things in list1, then unify
|
---|
| 712 | return unifyExact( combineTypes( list1Begin, list1End, get_type ).get(), t2, env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
|
---|
| 713 | } else if ( ! unifyExact( t1, t2, env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer ) ) {
|
---|
[a32b204] | 714 | return false;
|
---|
[f3b0a07] | 715 | } // if
|
---|
| 716 |
|
---|
[a32b204] | 717 | } // for
|
---|
[f3b0a07] | 718 | if ( list1Begin != list1End ) {
|
---|
| 719 | // try unifying empty tuple type with ttype
|
---|
| 720 | Type * t1 = *list1Begin;
|
---|
| 721 | if ( Tuples::isTtype( t1 ) ) {
|
---|
| 722 | return unifyExact( t1, combineTypes( list2Begin, list2End, get_type ).get(), env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
|
---|
| 723 | } else return false;
|
---|
| 724 | } else if ( list2Begin != list2End ) {
|
---|
| 725 | // try unifying empty tuple type with ttype
|
---|
| 726 | Type * t2 = *list2Begin;
|
---|
| 727 | if ( Tuples::isTtype( t2 ) ) {
|
---|
| 728 | return unifyExact( combineTypes( list1Begin, list1End, get_type ).get(), t2, env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
|
---|
| 729 | } else return false;
|
---|
[a32b204] | 730 | } else {
|
---|
| 731 | return true;
|
---|
[f3b0a07] | 732 | } // if
|
---|
[a32b204] | 733 | }
|
---|
| 734 |
|
---|
| 735 | void Unify::visit(TupleType *tupleType) {
|
---|
| 736 | if ( TupleType *otherTuple = dynamic_cast< TupleType* >( type2 ) ) {
|
---|
[f3b0a07] | 737 | std::unique_ptr<TupleType> flat1( tupleType->clone() );
|
---|
| 738 | std::unique_ptr<TupleType> flat2( otherTuple->clone() );
|
---|
| 739 | std::list<Type *> types1, types2;
|
---|
| 740 |
|
---|
| 741 | TtypeExpander expander( env );
|
---|
| 742 | flat1->acceptMutator( expander );
|
---|
| 743 | flat2->acceptMutator( expander );
|
---|
| 744 |
|
---|
| 745 | flatten( flat1.get(), back_inserter( types1 ) );
|
---|
| 746 | flatten( flat2.get(), back_inserter( types2 ) );
|
---|
| 747 |
|
---|
[c77fd8b] | 748 | result = unifyList( types1.begin(), types1.end(), types2.begin(), types2.end(), env, needAssertions, haveAssertions, openVars, indexer );
|
---|
[a32b204] | 749 | } // if
|
---|
| 750 | }
|
---|
[51b73452] | 751 |
|
---|
[af397ef8] | 752 | void Unify::visit( __attribute__((unused)) VarArgsType *varArgsType ) {
|
---|
[44b7088] | 753 | result = dynamic_cast< VarArgsType* >( type2 );
|
---|
| 754 | }
|
---|
| 755 |
|
---|
[af397ef8] | 756 | void Unify::visit( __attribute__((unused)) ZeroType *zeroType ) {
|
---|
[89e6ffc] | 757 | result = dynamic_cast< ZeroType* >( type2 );
|
---|
| 758 | }
|
---|
| 759 |
|
---|
[af397ef8] | 760 | void Unify::visit( __attribute__((unused)) OneType *oneType ) {
|
---|
[89e6ffc] | 761 | result = dynamic_cast< OneType* >( type2 );
|
---|
| 762 | }
|
---|
| 763 |
|
---|
[906e24d] | 764 | // xxx - compute once and store in the FunctionType?
|
---|
| 765 | Type * extractResultType( FunctionType * function ) {
|
---|
| 766 | if ( function->get_returnVals().size() == 0 ) {
|
---|
| 767 | return new VoidType( Type::Qualifiers() );
|
---|
| 768 | } else if ( function->get_returnVals().size() == 1 ) {
|
---|
| 769 | return function->get_returnVals().front()->get_type()->clone();
|
---|
| 770 | } else {
|
---|
[62423350] | 771 | std::list< Type * > types;
|
---|
[906e24d] | 772 | for ( DeclarationWithType * decl : function->get_returnVals() ) {
|
---|
[62423350] | 773 | types.push_back( decl->get_type()->clone() );
|
---|
[906e24d] | 774 | } // for
|
---|
[62423350] | 775 | return new TupleType( Type::Qualifiers(), types );
|
---|
[906e24d] | 776 | }
|
---|
| 777 | }
|
---|
[51b73452] | 778 | } // namespace ResolvExpr
|
---|
[a32b204] | 779 |
|
---|
| 780 | // Local Variables: //
|
---|
| 781 | // tab-width: 4 //
|
---|
| 782 | // mode: c++ //
|
---|
| 783 | // compile-command: "make install" //
|
---|
| 784 | // End: //
|
---|