Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/ResolvExpr/Unify.cc

    r8d7bef2 r0873d22e  
    1717#include <iterator>               // for back_insert_iterator, back_inserter
    1818#include <map>                    // for _Rb_tree_const_iterator, _Rb_tree_i...
     19#include <memory>                 // for unique_ptr
    1920#include <set>                    // for set
    2021#include <string>                 // for string, operator==, operator!=, bas...
     
    9899                findOpenVars( newSecond, openVars, closedVars, needAssertions, haveAssertions, true );
    99100
    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;
    101105        }
    102106
     
    119123///   newSecond->print( std::cerr );
    120124///   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;
    122129        }
    123130
     
    164171                                Type *common = 0;
    165172                                // 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() );
    167174                                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 ) ) {
    169176                                        if ( common ) {
    170177                                                common->get_qualifiers() = Type::Qualifiers();
     178                                                delete curClass.type;
    171179                                                curClass.type = common;
    172180                                                env.add( curClass );
     
    231239                                if ( common ) {
    232240                                        common->get_qualifiers() = Type::Qualifiers();
     241                                        delete class1.type;
    233242                                        class1.type = common;
    234243                                } // if
     
    263272                        env.add( newClass );
    264273                } // if
     274                delete type1;
     275                delete type2;
    265276                return result;
    266277        }
     
    271282                findOpenVars( type2, openVars, closedVars, needAssertions, haveAssertions, true );
    272283                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
    274292        }
    275293
     
    306324                } else if ( isopen1 ) {
    307325                        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?
    309327                        result = bindVar( var2, type1, entry2->second, env, needAssertions, haveAssertions, openVars, widenMode, indexer );
    310328                } else {
     
    465483
    466484        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 ) {
    468486                std::list< Type * > types;
    469487                for ( ; begin != end; ++begin ) {
     
    471489                        flatten( toType( *begin ), back_inserter( types ) );
    472490                }
    473                 return new TupleType{ Type::Qualifiers(), types };
     491                return std::unique_ptr<Type>( new TupleType( Type::Qualifiers(), types ) );
    474492        }
    475493
     
    486504                        if ( isTtype1 && ! isTtype2 ) {
    487505                                // 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 );
    489507                        } else if ( isTtype2 && ! isTtype1 ) {
    490508                                // 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 );
    492510                        } else if ( ! unifyExact( t1, t2, env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer ) ) {
    493511                                return false;
     
    499517                        Type * t1 = (*list1Begin)->get_type();
    500518                        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 );
    502520                        } else return false;
    503521                } else if ( list2Begin != list2End ) {
     
    505523                        Type * t2 = (*list2Begin)->get_type();
    506524                        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 );
    508526                        } else return false;
    509527                } else {
     
    526544                                        // expand ttype parameter into its actual type
    527545                                        if ( eqvClass.type ) {
     546                                                delete typeInst;
    528547                                                return eqvClass.type->clone();
    529548                                        }
     
    550569                                dst.push_back( new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::C, nullptr, t, nullptr ) );
    551570                        }
     571                        delete dcl;
    552572                }
    553573        }
     
    558578                        // flatten the parameter lists for both functions so that tuple structure
    559579                        // 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() );
    562582                        flattenList( flatFunc->get_parameters(), flatFunc->get_parameters(), env );
    563583                        flattenList( flatOther->get_parameters(), flatOther->get_parameters(), env );
     
    700720                        if ( isTtype1 && ! isTtype2 ) {
    701721                                // 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 );
    703723                        } else if ( isTtype2 && ! isTtype1 ) {
    704724                                // 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 );
    706726                        } else if ( ! unifyExact( t1, t2, env, needAssertions, haveAssertions, openVars, WidenMode( false, false ), indexer ) ) {
    707727                                return false;
     
    713733                        Type * t1 = *list1Begin;
    714734                        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 );
    716736                        } else return false;
    717737                } else if ( list2Begin != list2End ) {
     
    719739                        Type * t2 = *list2Begin;
    720740                        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 );
    722742                        } else return false;
    723743                } else {
     
    728748        void Unify::postvisit(TupleType *tupleType) {
    729749                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() );
    732752                        std::list<Type *> types1, types2;
    733753
     
    736756                        flat2->acceptMutator( expander );
    737757
    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 ) );
    740760
    741761                        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.