Changes in src/ResolvExpr/Unify.cc [8d7bef2:0873d22e]
- File:
-
- 1 edited
-
src/ResolvExpr/Unify.cc (modified) (21 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/ResolvExpr/Unify.cc
r8d7bef2 r0873d22e 17 17 #include <iterator> // for back_insert_iterator, back_inserter 18 18 #include <map> // for _Rb_tree_const_iterator, _Rb_tree_i... 19 #include <memory> // for unique_ptr 19 20 #include <set> // for set 20 21 #include <string> // for string, operator==, operator!=, bas... … … 98 99 findOpenVars( newSecond, openVars, closedVars, needAssertions, haveAssertions, true ); 99 100 100 return unifyExact( newFirst, newSecond, newEnv, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer ); 101 bool result = unifyExact( newFirst, newSecond, newEnv, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer ); 102 delete newFirst; 103 delete newSecond; 104 return result; 101 105 } 102 106 … … 119 123 /// newSecond->print( std::cerr ); 120 124 /// std::cerr << std::endl; 121 return unifyExact( newFirst, newSecond, newEnv, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer ); 125 bool result = unifyExact( newFirst, newSecond, newEnv, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer ); 126 delete newFirst; 127 delete newSecond; 128 return result; 122 129 } 123 130 … … 164 171 Type *common = 0; 165 172 // attempt to unify equivalence class type (which has qualifiers stripped, so they must be restored) with the type to bind to 166 Type* newType = curClass.type->clone();173 std::unique_ptr< Type > newType( curClass.type->clone() ); 167 174 newType->get_qualifiers() = typeInst->get_qualifiers(); 168 if ( unifyInexact( newType , other, env, needAssertions, haveAssertions, openVars, widenMode & WidenMode( curClass.allowWidening, true ), indexer, common ) ) {175 if ( unifyInexact( newType.get(), other, env, needAssertions, haveAssertions, openVars, widenMode & WidenMode( curClass.allowWidening, true ), indexer, common ) ) { 169 176 if ( common ) { 170 177 common->get_qualifiers() = Type::Qualifiers(); 178 delete curClass.type; 171 179 curClass.type = common; 172 180 env.add( curClass ); … … 231 239 if ( common ) { 232 240 common->get_qualifiers() = Type::Qualifiers(); 241 delete class1.type; 233 242 class1.type = common; 234 243 } // if … … 263 272 env.add( newClass ); 264 273 } // if 274 delete type1; 275 delete type2; 265 276 return result; 266 277 } … … 271 282 findOpenVars( type2, openVars, closedVars, needAssertions, haveAssertions, true ); 272 283 Type *commonType = 0; 273 return unifyInexact( type1, type2, env, needAssertions, haveAssertions, openVars, WidenMode( true, true ), indexer, commonType ); 284 if ( unifyInexact( type1, type2, env, needAssertions, haveAssertions, openVars, WidenMode( true, true ), indexer, commonType ) ) { 285 if ( commonType ) { 286 delete commonType; 287 } // if 288 return true; 289 } else { 290 return false; 291 } // if 274 292 } 275 293 … … 306 324 } else if ( isopen1 ) { 307 325 result = bindVar( var1, type2, entry1->second, env, needAssertions, haveAssertions, openVars, widenMode, indexer ); 308 } else if ( isopen2 ) { 326 } else if ( isopen2 ) { // TODO: swap widenMode values in call, since type positions are flipped? 309 327 result = bindVar( var2, type1, entry2->second, env, needAssertions, haveAssertions, openVars, widenMode, indexer ); 310 328 } else { … … 465 483 466 484 template< typename Iterator, typename Func > 467 Type*combineTypes( Iterator begin, Iterator end, Func & toType ) {485 std::unique_ptr<Type> combineTypes( Iterator begin, Iterator end, Func & toType ) { 468 486 std::list< Type * > types; 469 487 for ( ; begin != end; ++begin ) { … … 471 489 flatten( toType( *begin ), back_inserter( types ) ); 472 490 } 473 return new TupleType{ Type::Qualifiers(), types };491 return std::unique_ptr<Type>( new TupleType( Type::Qualifiers(), types ) ); 474 492 } 475 493 … … 486 504 if ( isTtype1 && ! isTtype2 ) { 487 505 // combine all of the things in list2, then unify 488 return unifyExact( t1, combineTypes( list2Begin, list2End, get_type ) , env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );506 return unifyExact( t1, combineTypes( list2Begin, list2End, get_type ).get(), env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer ); 489 507 } else if ( isTtype2 && ! isTtype1 ) { 490 508 // combine all of the things in list1, then unify 491 return unifyExact( combineTypes( list1Begin, list1End, get_type ) , t2, env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );509 return unifyExact( combineTypes( list1Begin, list1End, get_type ).get(), t2, env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer ); 492 510 } else if ( ! unifyExact( t1, t2, env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer ) ) { 493 511 return false; … … 499 517 Type * t1 = (*list1Begin)->get_type(); 500 518 if ( Tuples::isTtype( t1 ) ) { 501 return unifyExact( t1, combineTypes( list2Begin, list2End, get_type ) , env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );519 return unifyExact( t1, combineTypes( list2Begin, list2End, get_type ).get(), env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer ); 502 520 } else return false; 503 521 } else if ( list2Begin != list2End ) { … … 505 523 Type * t2 = (*list2Begin)->get_type(); 506 524 if ( Tuples::isTtype( t2 ) ) { 507 return unifyExact( combineTypes( list1Begin, list1End, get_type ) , t2, env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );525 return unifyExact( combineTypes( list1Begin, list1End, get_type ).get(), t2, env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer ); 508 526 } else return false; 509 527 } else { … … 526 544 // expand ttype parameter into its actual type 527 545 if ( eqvClass.type ) { 546 delete typeInst; 528 547 return eqvClass.type->clone(); 529 548 } … … 550 569 dst.push_back( new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::C, nullptr, t, nullptr ) ); 551 570 } 571 delete dcl; 552 572 } 553 573 } … … 558 578 // flatten the parameter lists for both functions so that tuple structure 559 579 // doesn't affect unification. Must be a clone so that the types don't change. 560 FunctionType* flatFunc = functionType->clone();561 FunctionType* flatOther = otherFunction->clone();580 std::unique_ptr<FunctionType> flatFunc( functionType->clone() ); 581 std::unique_ptr<FunctionType> flatOther( otherFunction->clone() ); 562 582 flattenList( flatFunc->get_parameters(), flatFunc->get_parameters(), env ); 563 583 flattenList( flatOther->get_parameters(), flatOther->get_parameters(), env ); … … 700 720 if ( isTtype1 && ! isTtype2 ) { 701 721 // combine all of the things in list2, then unify 702 return unifyExact( t1, combineTypes( list2Begin, list2End, get_type ) , env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );722 return unifyExact( t1, combineTypes( list2Begin, list2End, get_type ).get(), env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer ); 703 723 } else if ( isTtype2 && ! isTtype1 ) { 704 724 // combine all of the things in list1, then unify 705 return unifyExact( combineTypes( list1Begin, list1End, get_type ) , t2, env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );725 return unifyExact( combineTypes( list1Begin, list1End, get_type ).get(), t2, env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer ); 706 726 } else if ( ! unifyExact( t1, t2, env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer ) ) { 707 727 return false; … … 713 733 Type * t1 = *list1Begin; 714 734 if ( Tuples::isTtype( t1 ) ) { 715 return unifyExact( t1, combineTypes( list2Begin, list2End, get_type ) , env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );735 return unifyExact( t1, combineTypes( list2Begin, list2End, get_type ).get(), env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer ); 716 736 } else return false; 717 737 } else if ( list2Begin != list2End ) { … … 719 739 Type * t2 = *list2Begin; 720 740 if ( Tuples::isTtype( t2 ) ) { 721 return unifyExact( combineTypes( list1Begin, list1End, get_type ) , t2, env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer );741 return unifyExact( combineTypes( list1Begin, list1End, get_type ).get(), t2, env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer ); 722 742 } else return false; 723 743 } else { … … 728 748 void Unify::postvisit(TupleType *tupleType) { 729 749 if ( TupleType *otherTuple = dynamic_cast< TupleType* >( type2 ) ) { 730 TupleType* flat1 = tupleType->clone();731 TupleType* flat2 = otherTuple->clone();750 std::unique_ptr<TupleType> flat1( tupleType->clone() ); 751 std::unique_ptr<TupleType> flat2( otherTuple->clone() ); 732 752 std::list<Type *> types1, types2; 733 753 … … 736 756 flat2->acceptMutator( expander ); 737 757 738 flatten( flat1 , back_inserter( types1 ) );739 flatten( flat2 , back_inserter( types2 ) );758 flatten( flat1.get(), back_inserter( types1 ) ); 759 flatten( flat2.get(), back_inserter( types2 ) ); 740 760 741 761 result = unifyList( types1.begin(), types1.end(), types2.begin(), types2.end(), env, needAssertions, haveAssertions, openVars, indexer );
Note:
See TracChangeset
for help on using the changeset viewer.