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