[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 ) {
|
---|
[a32b204] | 145 | OpenVarSet::const_iterator tyvar = openVars.find( typeInst->get_name() );
|
---|
| 146 | assert( tyvar != openVars.end() );
|
---|
[d7dc824] | 147 | if ( ! tyVarCompatible( tyvar->second, other ) ) {
|
---|
[a32b204] | 148 | return false;
|
---|
| 149 | } // if
|
---|
| 150 | if ( occurs( other, typeInst->get_name(), env ) ) {
|
---|
| 151 | return false;
|
---|
| 152 | } // if
|
---|
| 153 | EqvClass curClass;
|
---|
| 154 | if ( env.lookup( typeInst->get_name(), curClass ) ) {
|
---|
| 155 | if ( curClass.type ) {
|
---|
| 156 | Type *common = 0;
|
---|
[eb50842] | 157 | // attempt to unify equivalence class type (which has qualifiers stripped, so they must be restored) with the type to bind to
|
---|
[a32b204] | 158 | std::auto_ptr< Type > newType( curClass.type->clone() );
|
---|
[721f17a] | 159 | newType->get_qualifiers() = typeInst->get_qualifiers();
|
---|
[a32b204] | 160 | if ( unifyInexact( newType.get(), other, env, needAssertions, haveAssertions, openVars, widenMode & WidenMode( curClass.allowWidening, true ), indexer, common ) ) {
|
---|
| 161 | if ( common ) {
|
---|
| 162 | common->get_qualifiers() = Type::Qualifiers();
|
---|
| 163 | delete curClass.type;
|
---|
| 164 | curClass.type = common;
|
---|
| 165 | env.add( curClass );
|
---|
| 166 | } // if
|
---|
| 167 | return true;
|
---|
| 168 | } else {
|
---|
| 169 | return false;
|
---|
| 170 | } // if
|
---|
| 171 | } else {
|
---|
| 172 | curClass.type = other->clone();
|
---|
| 173 | curClass.type->get_qualifiers() = Type::Qualifiers();
|
---|
| 174 | curClass.allowWidening = widenMode.widenFirst && widenMode.widenSecond;
|
---|
| 175 | env.add( curClass );
|
---|
| 176 | } // if
|
---|
| 177 | } else {
|
---|
| 178 | EqvClass newClass;
|
---|
| 179 | newClass.vars.insert( typeInst->get_name() );
|
---|
| 180 | newClass.type = other->clone();
|
---|
| 181 | newClass.type->get_qualifiers() = Type::Qualifiers();
|
---|
| 182 | newClass.allowWidening = widenMode.widenFirst && widenMode.widenSecond;
|
---|
[2c57025] | 183 | newClass.data = data;
|
---|
[a32b204] | 184 | env.add( newClass );
|
---|
| 185 | } // if
|
---|
| 186 | return true;
|
---|
| 187 | }
|
---|
| 188 |
|
---|
[2c57025] | 189 | 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] | 190 | bool result = true;
|
---|
| 191 | EqvClass class1, class2;
|
---|
| 192 | bool hasClass1 = false, hasClass2 = false;
|
---|
| 193 | bool widen1 = false, widen2 = false;
|
---|
| 194 | Type *type1 = 0, *type2 = 0;
|
---|
[41a2620] | 195 |
|
---|
[a32b204] | 196 | if ( env.lookup( var1->get_name(), class1 ) ) {
|
---|
| 197 | hasClass1 = true;
|
---|
| 198 | if ( class1.type ) {
|
---|
| 199 | if ( occurs( class1.type, var2->get_name(), env ) ) {
|
---|
| 200 | return false;
|
---|
| 201 | } // if
|
---|
| 202 | type1 = class1.type->clone();
|
---|
| 203 | } // if
|
---|
| 204 | widen1 = widenMode.widenFirst && class1.allowWidening;
|
---|
| 205 | } // if
|
---|
| 206 | if ( env.lookup( var2->get_name(), class2 ) ) {
|
---|
| 207 | hasClass2 = true;
|
---|
| 208 | if ( class2.type ) {
|
---|
| 209 | if ( occurs( class2.type, var1->get_name(), env ) ) {
|
---|
| 210 | return false;
|
---|
| 211 | } // if
|
---|
| 212 | type2 = class2.type->clone();
|
---|
| 213 | } // if
|
---|
| 214 | widen2 = widenMode.widenSecond && class2.allowWidening;
|
---|
| 215 | } // if
|
---|
[41a2620] | 216 |
|
---|
[a32b204] | 217 | if ( type1 && type2 ) {
|
---|
[2c57025] | 218 | // std::cerr << "has type1 && type2" << std::endl;
|
---|
[a32b204] | 219 | WidenMode newWidenMode ( widen1, widen2 );
|
---|
| 220 | Type *common = 0;
|
---|
| 221 | if ( unifyInexact( type1, type2, env, needAssertions, haveAssertions, openVars, newWidenMode, indexer, common ) ) {
|
---|
| 222 | class1.vars.insert( class2.vars.begin(), class2.vars.end() );
|
---|
| 223 | class1.allowWidening = widen1 && widen2;
|
---|
| 224 | if ( common ) {
|
---|
| 225 | common->get_qualifiers() = Type::Qualifiers();
|
---|
| 226 | delete class1.type;
|
---|
| 227 | class1.type = common;
|
---|
| 228 | } // if
|
---|
| 229 | env.add( class1 );
|
---|
| 230 | } else {
|
---|
| 231 | result = false;
|
---|
| 232 | } // if
|
---|
| 233 | } else if ( hasClass1 && hasClass2 ) {
|
---|
| 234 | if ( type1 ) {
|
---|
| 235 | class1.vars.insert( class2.vars.begin(), class2.vars.end() );
|
---|
| 236 | class1.allowWidening = widen1;
|
---|
| 237 | env.add( class1 );
|
---|
| 238 | } else {
|
---|
| 239 | class2.vars.insert( class1.vars.begin(), class1.vars.end() );
|
---|
| 240 | class2.allowWidening = widen2;
|
---|
| 241 | env.add( class2 );
|
---|
| 242 | } // if
|
---|
| 243 | } else if ( hasClass1 ) {
|
---|
| 244 | class1.vars.insert( var2->get_name() );
|
---|
| 245 | class1.allowWidening = widen1;
|
---|
| 246 | env.add( class1 );
|
---|
| 247 | } else if ( hasClass2 ) {
|
---|
| 248 | class2.vars.insert( var1->get_name() );
|
---|
| 249 | class2.allowWidening = widen2;
|
---|
| 250 | env.add( class2 );
|
---|
| 251 | } else {
|
---|
| 252 | EqvClass newClass;
|
---|
| 253 | newClass.vars.insert( var1->get_name() );
|
---|
| 254 | newClass.vars.insert( var2->get_name() );
|
---|
| 255 | newClass.allowWidening = widen1 && widen2;
|
---|
[2c57025] | 256 | newClass.data = data;
|
---|
[a32b204] | 257 | env.add( newClass );
|
---|
| 258 | } // if
|
---|
| 259 | delete type1;
|
---|
| 260 | delete type2;
|
---|
| 261 | return result;
|
---|
| 262 | }
|
---|
| 263 |
|
---|
| 264 | bool unify( Type *type1, Type *type2, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, OpenVarSet &openVars, const SymTab::Indexer &indexer ) {
|
---|
| 265 | OpenVarSet closedVars;
|
---|
| 266 | findOpenVars( type1, openVars, closedVars, needAssertions, haveAssertions, false );
|
---|
| 267 | findOpenVars( type2, openVars, closedVars, needAssertions, haveAssertions, true );
|
---|
| 268 | Type *commonType = 0;
|
---|
| 269 | if ( unifyInexact( type1, type2, env, needAssertions, haveAssertions, openVars, WidenMode( true, true ), indexer, commonType ) ) {
|
---|
| 270 | if ( commonType ) {
|
---|
| 271 | delete commonType;
|
---|
| 272 | } // if
|
---|
| 273 | return true;
|
---|
| 274 | } else {
|
---|
| 275 | return false;
|
---|
| 276 | } // if
|
---|
| 277 | }
|
---|
| 278 |
|
---|
| 279 | bool unify( Type *type1, Type *type2, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, OpenVarSet &openVars, const SymTab::Indexer &indexer, Type *&commonType ) {
|
---|
| 280 | OpenVarSet closedVars;
|
---|
| 281 | findOpenVars( type1, openVars, closedVars, needAssertions, haveAssertions, false );
|
---|
| 282 | findOpenVars( type2, openVars, closedVars, needAssertions, haveAssertions, true );
|
---|
| 283 | return unifyInexact( type1, type2, env, needAssertions, haveAssertions, openVars, WidenMode( true, true ), indexer, commonType );
|
---|
| 284 | }
|
---|
| 285 |
|
---|
| 286 | bool unifyExact( Type *type1, Type *type2, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, WidenMode widenMode, const SymTab::Indexer &indexer ) {
|
---|
[51b73452] | 287 | #ifdef DEBUG
|
---|
[a32b204] | 288 | TypeEnvironment debugEnv( env );
|
---|
[51b73452] | 289 | #endif
|
---|
[eb50842] | 290 | if ( type1->get_qualifiers() != type2->get_qualifiers() ) {
|
---|
| 291 | return false;
|
---|
| 292 | }
|
---|
| 293 |
|
---|
[a32b204] | 294 | bool result;
|
---|
| 295 | TypeInstType *var1 = dynamic_cast< TypeInstType* >( type1 );
|
---|
| 296 | TypeInstType *var2 = dynamic_cast< TypeInstType* >( type2 );
|
---|
| 297 | OpenVarSet::const_iterator entry1, entry2;
|
---|
| 298 | if ( var1 ) {
|
---|
| 299 | entry1 = openVars.find( var1->get_name() );
|
---|
| 300 | } // if
|
---|
| 301 | if ( var2 ) {
|
---|
| 302 | entry2 = openVars.find( var2->get_name() );
|
---|
| 303 | } // if
|
---|
| 304 | bool isopen1 = var1 && ( entry1 != openVars.end() );
|
---|
| 305 | bool isopen2 = var2 && ( entry2 != openVars.end() );
|
---|
[eb50842] | 306 |
|
---|
| 307 | if ( isopen1 && isopen2 && entry1->second == entry2->second ) {
|
---|
[a32b204] | 308 | result = bindVarToVar( var1, var2, entry1->second, env, needAssertions, haveAssertions, openVars, widenMode, indexer );
|
---|
| 309 | } else if ( isopen1 ) {
|
---|
| 310 | result = bindVar( var1, type2, entry1->second, env, needAssertions, haveAssertions, openVars, widenMode, indexer );
|
---|
| 311 | } else if ( isopen2 ) {
|
---|
| 312 | result = bindVar( var2, type1, entry2->second, env, needAssertions, haveAssertions, openVars, widenMode, indexer );
|
---|
| 313 | } else {
|
---|
| 314 | Unify comparator( type2, env, needAssertions, haveAssertions, openVars, widenMode, indexer );
|
---|
| 315 | type1->accept( comparator );
|
---|
| 316 | result = comparator.get_result();
|
---|
| 317 | } // if
|
---|
[51b73452] | 318 | #ifdef DEBUG
|
---|
[2c57025] | 319 | std::cerr << "============ unifyExact" << std::endl;
|
---|
| 320 | std::cerr << "type1 is ";
|
---|
| 321 | type1->print( std::cerr );
|
---|
| 322 | std::cerr << std::endl << "type2 is ";
|
---|
| 323 | type2->print( std::cerr );
|
---|
| 324 | std::cerr << std::endl << "openVars are ";
|
---|
| 325 | printOpenVarSet( openVars, std::cerr, 8 );
|
---|
| 326 | std::cerr << std::endl << "input env is " << std::endl;
|
---|
| 327 | debugEnv.print( std::cerr, 8 );
|
---|
| 328 | std::cerr << std::endl << "result env is " << std::endl;
|
---|
| 329 | env.print( std::cerr, 8 );
|
---|
| 330 | std::cerr << "result is " << result << std::endl;
|
---|
[51b73452] | 331 | #endif
|
---|
[a32b204] | 332 | return result;
|
---|
| 333 | }
|
---|
| 334 |
|
---|
| 335 | bool unifyExact( Type *type1, Type *type2, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, OpenVarSet &openVars, const SymTab::Indexer &indexer ) {
|
---|
| 336 | return unifyExact( type1, type2, env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
|
---|
| 337 | }
|
---|
| 338 |
|
---|
| 339 | bool unifyInexact( Type *type1, Type *type2, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, WidenMode widenMode, const SymTab::Indexer &indexer, Type *&common ) {
|
---|
| 340 | Type::Qualifiers tq1 = type1->get_qualifiers(), tq2 = type2->get_qualifiers();
|
---|
| 341 | type1->get_qualifiers() = Type::Qualifiers();
|
---|
| 342 | type2->get_qualifiers() = Type::Qualifiers();
|
---|
| 343 | bool result;
|
---|
[51b73452] | 344 | #ifdef DEBUG
|
---|
[2c57025] | 345 | std::cerr << "unifyInexact type 1 is ";
|
---|
| 346 | type1->print( std::cerr );
|
---|
[0b150ec] | 347 | std::cerr << " type 2 is ";
|
---|
[2c57025] | 348 | type2->print( std::cerr );
|
---|
| 349 | std::cerr << std::endl;
|
---|
[51b73452] | 350 | #endif
|
---|
[a32b204] | 351 | if ( ! unifyExact( type1, type2, env, needAssertions, haveAssertions, openVars, widenMode, indexer ) ) {
|
---|
[51b73452] | 352 | #ifdef DEBUG
|
---|
[2c57025] | 353 | std::cerr << "unifyInexact: no exact unification found" << std::endl;
|
---|
[51b73452] | 354 | #endif
|
---|
[a32b204] | 355 | if ( ( common = commonType( type1, type2, widenMode.widenFirst, widenMode.widenSecond, indexer, env, openVars ) ) ) {
|
---|
[6f95000] | 356 | common->get_qualifiers() = tq1 | tq2;
|
---|
[51b73452] | 357 | #ifdef DEBUG
|
---|
[2c57025] | 358 | std::cerr << "unifyInexact: common type is ";
|
---|
| 359 | common->print( std::cerr );
|
---|
| 360 | std::cerr << std::endl;
|
---|
[51b73452] | 361 | #endif
|
---|
[a32b204] | 362 | result = true;
|
---|
| 363 | } else {
|
---|
[51b73452] | 364 | #ifdef DEBUG
|
---|
[2c57025] | 365 | std::cerr << "unifyInexact: no common type found" << std::endl;
|
---|
[51b73452] | 366 | #endif
|
---|
[a32b204] | 367 | result = false;
|
---|
| 368 | } // if
|
---|
| 369 | } else {
|
---|
| 370 | if ( tq1 != tq2 ) {
|
---|
| 371 | if ( ( tq1 > tq2 || widenMode.widenFirst ) && ( tq2 > tq1 || widenMode.widenSecond ) ) {
|
---|
| 372 | common = type1->clone();
|
---|
[6f95000] | 373 | common->get_qualifiers() = tq1 | tq2;
|
---|
[a32b204] | 374 | result = true;
|
---|
| 375 | } else {
|
---|
| 376 | result = false;
|
---|
| 377 | } // if
|
---|
| 378 | } else {
|
---|
| 379 | result = true;
|
---|
| 380 | } // if
|
---|
| 381 | } // if
|
---|
| 382 | type1->get_qualifiers() = tq1;
|
---|
| 383 | type2->get_qualifiers() = tq2;
|
---|
| 384 | return result;
|
---|
| 385 | }
|
---|
| 386 |
|
---|
| 387 | Unify::Unify( Type *type2, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, WidenMode widenMode, const SymTab::Indexer &indexer )
|
---|
| 388 | : result( false ), type2( type2 ), env( env ), needAssertions( needAssertions ), haveAssertions( haveAssertions ), openVars( openVars ), widenMode( widenMode ), indexer( indexer ) {
|
---|
| 389 | }
|
---|
| 390 |
|
---|
[af397ef8] | 391 | void Unify::visit( __attribute__((unused)) VoidType *voidType) {
|
---|
[a32b204] | 392 | result = dynamic_cast< VoidType* >( type2 );
|
---|
| 393 | }
|
---|
| 394 |
|
---|
| 395 | void Unify::visit(BasicType *basicType) {
|
---|
| 396 | if ( BasicType *otherBasic = dynamic_cast< BasicType* >( type2 ) ) {
|
---|
| 397 | result = basicType->get_kind() == otherBasic->get_kind();
|
---|
| 398 | } // if
|
---|
| 399 | }
|
---|
| 400 |
|
---|
| 401 | void markAssertionSet( AssertionSet &assertions, DeclarationWithType *assert ) {
|
---|
[2c57025] | 402 | /// std::cerr << "assertion set is" << std::endl;
|
---|
| 403 | /// printAssertionSet( assertions, std::cerr, 8 );
|
---|
| 404 | /// std::cerr << "looking for ";
|
---|
| 405 | /// assert->print( std::cerr );
|
---|
| 406 | /// std::cerr << std::endl;
|
---|
[a32b204] | 407 | AssertionSet::iterator i = assertions.find( assert );
|
---|
| 408 | if ( i != assertions.end() ) {
|
---|
[2c57025] | 409 | /// std::cerr << "found it!" << std::endl;
|
---|
[6c3a988f] | 410 | i->second.isUsed = true;
|
---|
[a32b204] | 411 | } // if
|
---|
| 412 | }
|
---|
| 413 |
|
---|
| 414 | void markAssertions( AssertionSet &assertion1, AssertionSet &assertion2, Type *type ) {
|
---|
[aefcc3b] | 415 | for ( std::list< TypeDecl* >::const_iterator tyvar = type->get_forall().begin(); tyvar != type->get_forall().end(); ++tyvar ) {
|
---|
[a32b204] | 416 | for ( std::list< DeclarationWithType* >::const_iterator assert = (*tyvar)->get_assertions().begin(); assert != (*tyvar)->get_assertions().end(); ++assert ) {
|
---|
| 417 | markAssertionSet( assertion1, *assert );
|
---|
| 418 | markAssertionSet( assertion2, *assert );
|
---|
| 419 | } // for
|
---|
| 420 | } // for
|
---|
| 421 | }
|
---|
| 422 |
|
---|
| 423 | void Unify::visit(PointerType *pointerType) {
|
---|
| 424 | if ( PointerType *otherPointer = dynamic_cast< PointerType* >( type2 ) ) {
|
---|
| 425 | result = unifyExact( pointerType->get_base(), otherPointer->get_base(), env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
|
---|
| 426 | markAssertions( haveAssertions, needAssertions, pointerType );
|
---|
| 427 | markAssertions( haveAssertions, needAssertions, otherPointer );
|
---|
| 428 | } // if
|
---|
| 429 | }
|
---|
| 430 |
|
---|
[ce8c12f] | 431 | void Unify::visit(ReferenceType *refType) {
|
---|
| 432 | if ( ReferenceType *otherRef = dynamic_cast< ReferenceType* >( type2 ) ) {
|
---|
| 433 | result = unifyExact( refType->get_base(), otherRef->get_base(), env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
|
---|
| 434 | markAssertions( haveAssertions, needAssertions, refType );
|
---|
| 435 | markAssertions( haveAssertions, needAssertions, otherRef );
|
---|
| 436 | } // if
|
---|
| 437 | }
|
---|
| 438 |
|
---|
[a32b204] | 439 | void Unify::visit(ArrayType *arrayType) {
|
---|
| 440 | ArrayType *otherArray = dynamic_cast< ArrayType* >( type2 );
|
---|
[1cbca6e] | 441 | // to unify, array types must both be VLA or both not VLA
|
---|
| 442 | // and must both have a dimension expression or not have a dimension
|
---|
[41a2620] | 443 | if ( otherArray && arrayType->get_isVarLen() == otherArray->get_isVarLen() ) {
|
---|
[1cbca6e] | 444 |
|
---|
| 445 | // not positive this is correct in all cases, but it's needed for typedefs
|
---|
| 446 | if ( arrayType->get_isVarLen() || otherArray->get_isVarLen() ) {
|
---|
| 447 | return;
|
---|
| 448 | }
|
---|
| 449 |
|
---|
| 450 | if ( ! arrayType->get_isVarLen() && ! otherArray->get_isVarLen() &&
|
---|
| 451 | arrayType->get_dimension() != 0 && otherArray->get_dimension() != 0 ) {
|
---|
| 452 | ConstantExpr * ce1 = dynamic_cast< ConstantExpr * >( arrayType->get_dimension() );
|
---|
| 453 | ConstantExpr * ce2 = dynamic_cast< ConstantExpr * >( otherArray->get_dimension() );
|
---|
[41a2620] | 454 | // see C11 Reference Manual 6.7.6.2.6
|
---|
| 455 | // two array types with size specifiers that are integer constant expressions are
|
---|
| 456 | // compatible if both size specifiers have the same constant value
|
---|
| 457 | if ( ce1 && ce2 ) {
|
---|
| 458 | Constant * c1 = ce1->get_constant();
|
---|
| 459 | Constant * c2 = ce2->get_constant();
|
---|
| 460 |
|
---|
| 461 | if ( c1->get_value() != c2->get_value() ) {
|
---|
| 462 | // does not unify if the dimension is different
|
---|
| 463 | return;
|
---|
| 464 | }
|
---|
[1cbca6e] | 465 | }
|
---|
| 466 | }
|
---|
| 467 |
|
---|
[a32b204] | 468 | result = unifyExact( arrayType->get_base(), otherArray->get_base(), env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
|
---|
| 469 | } // if
|
---|
| 470 | }
|
---|
| 471 |
|
---|
[f3b0a07] | 472 | template< typename Iterator, typename Func >
|
---|
| 473 | std::unique_ptr<Type> combineTypes( Iterator begin, Iterator end, Func & toType ) {
|
---|
[53e3b4a] | 474 | std::list< Type * > types;
|
---|
| 475 | for ( ; begin != end; ++begin ) {
|
---|
[64eae56] | 476 | // it's guaranteed that a ttype variable will be bound to a flat tuple, so ensure that this results in a flat tuple
|
---|
[f3b0a07] | 477 | flatten( toType( *begin ), back_inserter( types ) );
|
---|
[53e3b4a] | 478 | }
|
---|
[6c3a988f] | 479 | return std::unique_ptr<Type>( new TupleType( Type::Qualifiers(), types ) );
|
---|
[53e3b4a] | 480 | }
|
---|
| 481 |
|
---|
[a32b204] | 482 | template< typename Iterator1, typename Iterator2 >
|
---|
| 483 | bool unifyDeclList( Iterator1 list1Begin, Iterator1 list1End, Iterator2 list2Begin, Iterator2 list2End, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, const SymTab::Indexer &indexer ) {
|
---|
[f3b0a07] | 484 | auto get_type = [](DeclarationWithType * dwt){ return dwt->get_type(); };
|
---|
[a32b204] | 485 | for ( ; list1Begin != list1End && list2Begin != list2End; ++list1Begin, ++list2Begin ) {
|
---|
[53e3b4a] | 486 | Type * t1 = (*list1Begin)->get_type();
|
---|
| 487 | Type * t2 = (*list2Begin)->get_type();
|
---|
| 488 | bool isTtype1 = Tuples::isTtype( t1 );
|
---|
| 489 | bool isTtype2 = Tuples::isTtype( t2 );
|
---|
[6c3a988f] | 490 | // xxx - assumes ttype must be last parameter
|
---|
| 491 | // xxx - there may be a nice way to refactor this, but be careful because the argument positioning might matter in some cases.
|
---|
[53e3b4a] | 492 | if ( isTtype1 && ! isTtype2 ) {
|
---|
| 493 | // combine all of the things in list2, then unify
|
---|
[f3b0a07] | 494 | return unifyExact( t1, combineTypes( list2Begin, list2End, get_type ).get(), env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
|
---|
[53e3b4a] | 495 | } else if ( isTtype2 && ! isTtype1 ) {
|
---|
| 496 | // combine all of the things in list1, then unify
|
---|
[f3b0a07] | 497 | return unifyExact( combineTypes( list1Begin, list1End, get_type ).get(), t2, env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
|
---|
[53e3b4a] | 498 | } else if ( ! unifyExact( t1, t2, env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer ) ) {
|
---|
[a32b204] | 499 | return false;
|
---|
| 500 | } // if
|
---|
| 501 | } // for
|
---|
[6c3a988f] | 502 | // 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] | 503 | if ( list1Begin != list1End ) {
|
---|
| 504 | // try unifying empty tuple type with ttype
|
---|
| 505 | Type * t1 = (*list1Begin)->get_type();
|
---|
| 506 | if ( Tuples::isTtype( t1 ) ) {
|
---|
[f3b0a07] | 507 | return unifyExact( t1, combineTypes( list2Begin, list2End, get_type ).get(), env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
|
---|
[4c8621ac] | 508 | } else return false;
|
---|
| 509 | } else if ( list2Begin != list2End ) {
|
---|
| 510 | // try unifying empty tuple type with ttype
|
---|
| 511 | Type * t2 = (*list2Begin)->get_type();
|
---|
| 512 | if ( Tuples::isTtype( t2 ) ) {
|
---|
[f3b0a07] | 513 | return unifyExact( combineTypes( list1Begin, list1End, get_type ).get(), t2, env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
|
---|
[4c8621ac] | 514 | } else return false;
|
---|
[a32b204] | 515 | } else {
|
---|
| 516 | return true;
|
---|
| 517 | } // if
|
---|
| 518 | }
|
---|
| 519 |
|
---|
[6c3a988f] | 520 | /// Finds ttypes and replaces them with their expansion, if known.
|
---|
| 521 | /// This needs to be done so that satisfying ttype assertions is easier.
|
---|
| 522 | /// If this isn't done then argument lists can have wildly different
|
---|
| 523 | /// size and structure, when they should be compatible.
|
---|
| 524 | struct TtypeExpander : public Mutator {
|
---|
| 525 | TypeEnvironment & env;
|
---|
| 526 | TtypeExpander( TypeEnvironment & env ) : env( env ) {}
|
---|
| 527 | Type * mutate( TypeInstType * typeInst ) {
|
---|
| 528 | EqvClass eqvClass;
|
---|
| 529 | if ( env.lookup( typeInst->get_name(), eqvClass ) ) {
|
---|
| 530 | if ( eqvClass.data.kind == TypeDecl::Ttype ) {
|
---|
| 531 | // expand ttype parameter into its actual type
|
---|
| 532 | if ( eqvClass.type ) {
|
---|
| 533 | delete typeInst;
|
---|
| 534 | return eqvClass.type->clone();
|
---|
| 535 | }
|
---|
| 536 | }
|
---|
| 537 | }
|
---|
| 538 | return typeInst;
|
---|
| 539 | }
|
---|
| 540 | };
|
---|
| 541 |
|
---|
| 542 | /// flattens a list of declarations, so that each tuple type has a single declaration.
|
---|
| 543 | /// makes use of TtypeExpander to ensure ttypes are flat as well.
|
---|
| 544 | void flattenList( std::list< DeclarationWithType * > src, std::list< DeclarationWithType * > & dst, TypeEnvironment & env ) {
|
---|
| 545 | dst.clear();
|
---|
| 546 | for ( DeclarationWithType * dcl : src ) {
|
---|
| 547 | TtypeExpander expander( env );
|
---|
| 548 | dcl->acceptMutator( expander );
|
---|
| 549 | std::list< Type * > types;
|
---|
| 550 | flatten( dcl->get_type(), back_inserter( types ) );
|
---|
| 551 | for ( Type * t : types ) {
|
---|
[68fe077a] | 552 | dst.push_back( new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::C, nullptr, t, nullptr ) );
|
---|
[6c3a988f] | 553 | }
|
---|
| 554 | delete dcl;
|
---|
| 555 | }
|
---|
| 556 | }
|
---|
| 557 |
|
---|
[a32b204] | 558 | void Unify::visit(FunctionType *functionType) {
|
---|
| 559 | FunctionType *otherFunction = dynamic_cast< FunctionType* >( type2 );
|
---|
| 560 | if ( otherFunction && functionType->get_isVarArgs() == otherFunction->get_isVarArgs() ) {
|
---|
[6c3a988f] | 561 | // flatten the parameter lists for both functions so that tuple structure
|
---|
| 562 | // doesn't affect unification. Must be a clone so that the types don't change.
|
---|
| 563 | std::unique_ptr<FunctionType> flatFunc( functionType->clone() );
|
---|
| 564 | std::unique_ptr<FunctionType> flatOther( otherFunction->clone() );
|
---|
| 565 | flattenList( flatFunc->get_parameters(), flatFunc->get_parameters(), env );
|
---|
| 566 | flattenList( flatOther->get_parameters(), flatOther->get_parameters(), env );
|
---|
| 567 |
|
---|
[53e3b4a] | 568 | // sizes don't have to match if ttypes are involved; need to be more precise wrt where the ttype is to prevent errors
|
---|
[6c3a988f] | 569 | if ( (flatFunc->get_parameters().size() == flatOther->get_parameters().size() && flatFunc->get_returnVals().size() == flatOther->get_returnVals().size()) || flatFunc->isTtype() || flatOther->isTtype() ) {
|
---|
| 570 | if ( unifyDeclList( flatFunc->get_parameters().begin(), flatFunc->get_parameters().end(), flatOther->get_parameters().begin(), flatOther->get_parameters().end(), env, needAssertions, haveAssertions, openVars, indexer ) ) {
|
---|
| 571 | if ( unifyDeclList( flatFunc->get_returnVals().begin(), flatFunc->get_returnVals().end(), flatOther->get_returnVals().begin(), flatOther->get_returnVals().end(), env, needAssertions, haveAssertions, openVars, indexer ) ) {
|
---|
[1cbca6e] | 572 |
|
---|
[6c3a988f] | 573 | // the original types must be used in mark assertions, since pointer comparisons are used
|
---|
[03da511] | 574 | markAssertions( haveAssertions, needAssertions, functionType );
|
---|
| 575 | markAssertions( haveAssertions, needAssertions, otherFunction );
|
---|
[41a2620] | 576 |
|
---|
[03da511] | 577 | result = true;
|
---|
| 578 | } // if
|
---|
[a32b204] | 579 | } // if
|
---|
| 580 | } // if
|
---|
| 581 | } // if
|
---|
| 582 | }
|
---|
| 583 |
|
---|
| 584 | template< typename RefType >
|
---|
[02ec390] | 585 | void Unify::handleRefType( RefType *inst, Type *other ) {
|
---|
| 586 | // check that other type is compatible and named the same
|
---|
[a32b204] | 587 | RefType *otherStruct = dynamic_cast< RefType* >( other );
|
---|
| 588 | result = otherStruct && inst->get_name() == otherStruct->get_name();
|
---|
[02ec390] | 589 | }
|
---|
| 590 |
|
---|
| 591 | template< typename RefType >
|
---|
| 592 | void Unify::handleGenericRefType( RefType *inst, Type *other ) {
|
---|
| 593 | // Check that other type is compatible and named the same
|
---|
| 594 | handleRefType( inst, other );
|
---|
| 595 | if ( ! result ) return;
|
---|
[f5234f3] | 596 | // Check that parameters of types unify, if any
|
---|
[02ec390] | 597 | std::list< Expression* > params = inst->get_parameters();
|
---|
[f5234f3] | 598 | std::list< Expression* > otherParams = ((RefType*)other)->get_parameters();
|
---|
| 599 |
|
---|
| 600 | std::list< Expression* >::const_iterator it = params.begin(), jt = otherParams.begin();
|
---|
| 601 | for ( ; it != params.end() && jt != otherParams.end(); ++it, ++jt ) {
|
---|
| 602 | TypeExpr *param = dynamic_cast< TypeExpr* >(*it);
|
---|
[b2daebd4] | 603 | assertf(param, "Aggregate parameters should be type expressions");
|
---|
[f5234f3] | 604 | TypeExpr *otherParam = dynamic_cast< TypeExpr* >(*jt);
|
---|
[b2daebd4] | 605 | assertf(otherParam, "Aggregate parameters should be type expressions");
|
---|
[ce8c12f] | 606 |
|
---|
[b2daebd4] | 607 | Type* paramTy = param->get_type();
|
---|
| 608 | Type* otherParamTy = otherParam->get_type();
|
---|
[f5234f3] | 609 |
|
---|
[b2daebd4] | 610 | bool tupleParam = Tuples::isTtype( paramTy );
|
---|
| 611 | bool otherTupleParam = Tuples::isTtype( otherParamTy );
|
---|
| 612 |
|
---|
| 613 | if ( tupleParam && otherTupleParam ) {
|
---|
| 614 | ++it; ++jt; // skip ttype parameters for break
|
---|
| 615 | } else if ( tupleParam ) {
|
---|
| 616 | // bundle other parameters into tuple to match
|
---|
[62423350] | 617 | std::list< Type * > binderTypes;
|
---|
[b2daebd4] | 618 |
|
---|
| 619 | do {
|
---|
[62423350] | 620 | binderTypes.push_back( otherParam->get_type()->clone() );
|
---|
[b2daebd4] | 621 | ++jt;
|
---|
| 622 |
|
---|
| 623 | if ( jt == otherParams.end() ) break;
|
---|
| 624 |
|
---|
| 625 | otherParam = dynamic_cast< TypeExpr* >(*jt);
|
---|
| 626 | assertf(otherParam, "Aggregate parameters should be type expressions");
|
---|
| 627 | } while (true);
|
---|
| 628 |
|
---|
[62423350] | 629 | otherParamTy = new TupleType{ paramTy->get_qualifiers(), binderTypes };
|
---|
[b2daebd4] | 630 | ++it; // skip ttype parameter for break
|
---|
| 631 | } else if ( otherTupleParam ) {
|
---|
| 632 | // bundle parameters into tuple to match other
|
---|
[62423350] | 633 | std::list< Type * > binderTypes;
|
---|
[b2daebd4] | 634 |
|
---|
| 635 | do {
|
---|
[62423350] | 636 | binderTypes.push_back( param->get_type()->clone() );
|
---|
[b2daebd4] | 637 | ++it;
|
---|
| 638 |
|
---|
| 639 | if ( it == params.end() ) break;
|
---|
| 640 |
|
---|
| 641 | param = dynamic_cast< TypeExpr* >(*it);
|
---|
| 642 | assertf(param, "Aggregate parameters should be type expressions");
|
---|
| 643 | } while (true);
|
---|
| 644 |
|
---|
[62423350] | 645 | paramTy = new TupleType{ otherParamTy->get_qualifiers(), binderTypes };
|
---|
[b2daebd4] | 646 | ++jt; // skip ttype parameter for break
|
---|
| 647 | }
|
---|
| 648 |
|
---|
| 649 | if ( ! unifyExact( paramTy, otherParamTy, env, needAssertions, haveAssertions, openVars, WidenMode(false, false), indexer ) ) {
|
---|
[02ec390] | 650 | result = false;
|
---|
| 651 | return;
|
---|
| 652 | }
|
---|
[b2daebd4] | 653 |
|
---|
| 654 | // ttype parameter should be last
|
---|
| 655 | if ( tupleParam || otherTupleParam ) break;
|
---|
[02ec390] | 656 | }
|
---|
[f5234f3] | 657 | result = ( it == params.end() && jt == otherParams.end() );
|
---|
[02ec390] | 658 | }
|
---|
[a32b204] | 659 |
|
---|
| 660 | void Unify::visit(StructInstType *structInst) {
|
---|
[02ec390] | 661 | handleGenericRefType( structInst, type2 );
|
---|
[a32b204] | 662 | }
|
---|
| 663 |
|
---|
| 664 | void Unify::visit(UnionInstType *unionInst) {
|
---|
[02ec390] | 665 | handleGenericRefType( unionInst, type2 );
|
---|
[a32b204] | 666 | }
|
---|
| 667 |
|
---|
| 668 | void Unify::visit(EnumInstType *enumInst) {
|
---|
| 669 | handleRefType( enumInst, type2 );
|
---|
| 670 | }
|
---|
| 671 |
|
---|
[4040425] | 672 | void Unify::visit(TraitInstType *contextInst) {
|
---|
[a32b204] | 673 | handleRefType( contextInst, type2 );
|
---|
| 674 | }
|
---|
| 675 |
|
---|
| 676 | void Unify::visit(TypeInstType *typeInst) {
|
---|
| 677 | assert( openVars.find( typeInst->get_name() ) == openVars.end() );
|
---|
| 678 | TypeInstType *otherInst = dynamic_cast< TypeInstType* >( type2 );
|
---|
| 679 | if ( otherInst && typeInst->get_name() == otherInst->get_name() ) {
|
---|
| 680 | result = true;
|
---|
[51b73452] | 681 | /// } else {
|
---|
| 682 | /// NamedTypeDecl *nt = indexer.lookupType( typeInst->get_name() );
|
---|
[a32b204] | 683 | /// if ( nt ) {
|
---|
[51b73452] | 684 | /// TypeDecl *type = dynamic_cast< TypeDecl* >( nt );
|
---|
| 685 | /// assert( type );
|
---|
[a32b204] | 686 | /// if ( type->get_base() ) {
|
---|
[51b73452] | 687 | /// result = unifyExact( type->get_base(), typeInst, env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
|
---|
| 688 | /// }
|
---|
| 689 | /// }
|
---|
[a32b204] | 690 | } // if
|
---|
| 691 | }
|
---|
| 692 |
|
---|
| 693 | template< typename Iterator1, typename Iterator2 >
|
---|
[c77fd8b] | 694 | bool unifyList( Iterator1 list1Begin, Iterator1 list1End, Iterator2 list2Begin, Iterator2 list2End, TypeEnvironment &env, AssertionSet &needAssertions, AssertionSet &haveAssertions, const OpenVarSet &openVars, const SymTab::Indexer &indexer ) {
|
---|
[f3b0a07] | 695 | auto get_type = [](Type * t) { return t; };
|
---|
[a32b204] | 696 | for ( ; list1Begin != list1End && list2Begin != list2End; ++list1Begin, ++list2Begin ) {
|
---|
[f3b0a07] | 697 | Type * t1 = *list1Begin;
|
---|
| 698 | Type * t2 = *list2Begin;
|
---|
| 699 | bool isTtype1 = Tuples::isTtype( t1 );
|
---|
| 700 | bool isTtype2 = Tuples::isTtype( t2 );
|
---|
| 701 | // xxx - assumes ttype must be last parameter
|
---|
| 702 | // xxx - there may be a nice way to refactor this, but be careful because the argument positioning might matter in some cases.
|
---|
| 703 | if ( isTtype1 && ! isTtype2 ) {
|
---|
| 704 | // combine all of the things in list2, then unify
|
---|
| 705 | return unifyExact( t1, combineTypes( list2Begin, list2End, get_type ).get(), env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
|
---|
| 706 | } else if ( isTtype2 && ! isTtype1 ) {
|
---|
| 707 | // combine all of the things in list1, then unify
|
---|
| 708 | return unifyExact( combineTypes( list1Begin, list1End, get_type ).get(), t2, env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
|
---|
| 709 | } else if ( ! unifyExact( t1, t2, env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer ) ) {
|
---|
[a32b204] | 710 | return false;
|
---|
[f3b0a07] | 711 | } // if
|
---|
| 712 |
|
---|
[a32b204] | 713 | } // for
|
---|
[f3b0a07] | 714 | if ( list1Begin != list1End ) {
|
---|
| 715 | // try unifying empty tuple type with ttype
|
---|
| 716 | Type * t1 = *list1Begin;
|
---|
| 717 | if ( Tuples::isTtype( t1 ) ) {
|
---|
| 718 | return unifyExact( t1, combineTypes( list2Begin, list2End, get_type ).get(), env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
|
---|
| 719 | } else return false;
|
---|
| 720 | } else if ( list2Begin != list2End ) {
|
---|
| 721 | // try unifying empty tuple type with ttype
|
---|
| 722 | Type * t2 = *list2Begin;
|
---|
| 723 | if ( Tuples::isTtype( t2 ) ) {
|
---|
| 724 | return unifyExact( combineTypes( list1Begin, list1End, get_type ).get(), t2, env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );
|
---|
| 725 | } else return false;
|
---|
[a32b204] | 726 | } else {
|
---|
| 727 | return true;
|
---|
[f3b0a07] | 728 | } // if
|
---|
[a32b204] | 729 | }
|
---|
| 730 |
|
---|
| 731 | void Unify::visit(TupleType *tupleType) {
|
---|
| 732 | if ( TupleType *otherTuple = dynamic_cast< TupleType* >( type2 ) ) {
|
---|
[f3b0a07] | 733 | std::unique_ptr<TupleType> flat1( tupleType->clone() );
|
---|
| 734 | std::unique_ptr<TupleType> flat2( otherTuple->clone() );
|
---|
| 735 | std::list<Type *> types1, types2;
|
---|
| 736 |
|
---|
| 737 | TtypeExpander expander( env );
|
---|
| 738 | flat1->acceptMutator( expander );
|
---|
| 739 | flat2->acceptMutator( expander );
|
---|
| 740 |
|
---|
| 741 | flatten( flat1.get(), back_inserter( types1 ) );
|
---|
| 742 | flatten( flat2.get(), back_inserter( types2 ) );
|
---|
| 743 |
|
---|
[c77fd8b] | 744 | result = unifyList( types1.begin(), types1.end(), types2.begin(), types2.end(), env, needAssertions, haveAssertions, openVars, indexer );
|
---|
[a32b204] | 745 | } // if
|
---|
| 746 | }
|
---|
[51b73452] | 747 |
|
---|
[af397ef8] | 748 | void Unify::visit( __attribute__((unused)) VarArgsType *varArgsType ) {
|
---|
[44b7088] | 749 | result = dynamic_cast< VarArgsType* >( type2 );
|
---|
| 750 | }
|
---|
| 751 |
|
---|
[af397ef8] | 752 | void Unify::visit( __attribute__((unused)) ZeroType *zeroType ) {
|
---|
[89e6ffc] | 753 | result = dynamic_cast< ZeroType* >( type2 );
|
---|
| 754 | }
|
---|
| 755 |
|
---|
[af397ef8] | 756 | void Unify::visit( __attribute__((unused)) OneType *oneType ) {
|
---|
[89e6ffc] | 757 | result = dynamic_cast< OneType* >( type2 );
|
---|
| 758 | }
|
---|
| 759 |
|
---|
[906e24d] | 760 | // xxx - compute once and store in the FunctionType?
|
---|
| 761 | Type * extractResultType( FunctionType * function ) {
|
---|
| 762 | if ( function->get_returnVals().size() == 0 ) {
|
---|
| 763 | return new VoidType( Type::Qualifiers() );
|
---|
| 764 | } else if ( function->get_returnVals().size() == 1 ) {
|
---|
| 765 | return function->get_returnVals().front()->get_type()->clone();
|
---|
| 766 | } else {
|
---|
[62423350] | 767 | std::list< Type * > types;
|
---|
[906e24d] | 768 | for ( DeclarationWithType * decl : function->get_returnVals() ) {
|
---|
[62423350] | 769 | types.push_back( decl->get_type()->clone() );
|
---|
[906e24d] | 770 | } // for
|
---|
[62423350] | 771 | return new TupleType( Type::Qualifiers(), types );
|
---|
[906e24d] | 772 | }
|
---|
| 773 | }
|
---|
[51b73452] | 774 | } // namespace ResolvExpr
|
---|
[a32b204] | 775 |
|
---|
| 776 | // Local Variables: //
|
---|
| 777 | // tab-width: 4 //
|
---|
| 778 | // mode: c++ //
|
---|
| 779 | // compile-command: "make install" //
|
---|
| 780 | // End: //
|
---|