Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/ResolvExpr/AlternativeFinder.cc

    r8e18b8e ra8706fc  
    2121#include <list>                    // for _List_iterator, list, _List_const_...
    2222#include <map>                     // for _Rb_tree_iterator, map, _Rb_tree_c...
    23 #include <memory>                  // for allocator_traits<>::value_type
     23#include <memory>                  // for allocator_traits<>::value_type, unique_ptr
    2424#include <utility>                 // for pair
    2525#include <vector>                  // for vector
     
    3535#include "ResolveTypeof.h"         // for resolveTypeof
    3636#include "Resolver.h"              // for resolveStmtExpr
    37 #include "Common/GC.h"             // for new_static_root
    3837#include "SymTab/Indexer.h"        // for Indexer
    3938#include "SymTab/Mangler.h"        // for Mangler
     
    102101                void addAnonConversions( const Alternative & alt );
    103102                /// Adds alternatives for member expressions, given the aggregate, conversion cost for that aggregate, and name of the member
    104                 template< typename StructOrUnionType > void addAggMembers( StructOrUnionType *aggInst, Expression *expr, const Cost &newCost, const TypeEnvironment & env, const std::string & name );
     103                template< typename StructOrUnionType > void addAggMembers( StructOrUnionType *aggInst, Expression *expr, const Cost &newCost, const TypeEnvironment & env, Expression * member );
    105104                /// Adds alternatives for member expressions where the left side has tuple type
    106105                void addTupleMembers( TupleType * tupleType, Expression *expr, const Cost &newCost, const TypeEnvironment & env, Expression * member );
     
    166165                                        candidate->env.apply( newType );
    167166                                        mangleName = SymTab::Mangler::mangle( newType );
     167                                        delete newType;
    168168                                }
    169169                                std::map< std::string, PruneStruct >::iterator mapPlace = selected.find( mangleName );
     
    297297                // adds anonymous member interpretations whenever an aggregate value type is seen.
    298298                // it's okay for the aggregate expression to have reference type -- cast it to the base type to treat the aggregate as the referenced value
    299                 Expression* aggrExpr = alt.expr->clone();
     299                std::unique_ptr<Expression> aggrExpr( alt.expr->clone() );
    300300                alt.env.apply( aggrExpr->get_result() );
    301301                Type * aggrType = aggrExpr->get_result();
    302302                if ( dynamic_cast< ReferenceType * >( aggrType ) ) {
    303303                        aggrType = aggrType->stripReferences();
    304                         aggrExpr = new CastExpr{ aggrExpr, aggrType->clone() };
     304                        aggrExpr.reset( new CastExpr( aggrExpr.release(), aggrType->clone() ) );
    305305                }
    306306
    307307                if ( StructInstType *structInst = dynamic_cast< StructInstType* >( aggrExpr->get_result() ) ) {
    308                         addAggMembers( structInst, aggrExpr, alt.cost+Cost::safe, alt.env, "" );
     308                        NameExpr nameExpr( "" );
     309                        addAggMembers( structInst, aggrExpr.get(), alt.cost+Cost::safe, alt.env, &nameExpr );
    309310                } else if ( UnionInstType *unionInst = dynamic_cast< UnionInstType* >( aggrExpr->get_result() ) ) {
    310                         addAggMembers( unionInst, aggrExpr, alt.cost+Cost::safe, alt.env, "" );
     311                        NameExpr nameExpr( "" );
     312                        addAggMembers( unionInst, aggrExpr.get(), alt.cost+Cost::safe, alt.env, &nameExpr );
    311313                } // if
    312314        }
    313315
    314316        template< typename StructOrUnionType >
    315         void AlternativeFinder::Finder::addAggMembers( StructOrUnionType *aggInst, Expression *expr, const Cost &newCost, const TypeEnvironment & env, const std::string & name ) {
     317        void AlternativeFinder::Finder::addAggMembers( StructOrUnionType *aggInst, Expression *expr, const Cost &newCost, const TypeEnvironment & env, Expression * member ) {
     318                // by this point, member must be a name expr
     319                NameExpr * nameExpr = dynamic_cast< NameExpr * >( member );
     320                if ( ! nameExpr ) return;
     321                const std::string & name = nameExpr->get_name();
    316322                std::list< Declaration* > members;
    317323                aggInst->lookup( name, members );
     
    338344                        if ( ss >> val && ! (ss >> tmp) ) {
    339345                                if ( val >= 0 && (unsigned int)val < tupleType->size() ) {
    340                                         alternatives.push_back( Alternative( new TupleIndexExpr( expr, val ), env, newCost ) );
     346                                        alternatives.push_back( Alternative( new TupleIndexExpr( expr->clone(), val ), env, newCost ) );
    341347                                } // if
    342348                        } // if
     
    347353                                int val;
    348354                                ss >> val;
    349                                 alternatives.push_back( Alternative( new TupleIndexExpr( expr, val ), env, newCost ) );
     355                                alternatives.push_back( Alternative( new TupleIndexExpr( expr->clone(), val ), env, newCost ) );
    350356                        }
    351357                } // if
     
    353359
    354360        void AlternativeFinder::Finder::postvisit( ApplicationExpr *applicationExpr ) {
    355                 alternatives.push_back( Alternative( applicationExpr, env, Cost::zero ) );
     361                alternatives.push_back( Alternative( applicationExpr->clone(), env, Cost::zero ) );
    356362        }
    357363
     
    462468        }
    463469
     470        // /// Map of declaration uniqueIds (intended to be the assertions in an AssertionSet) to their parents and the number of times they've been included
     471        //typedef std::unordered_map< UniqueId, std::unordered_map< UniqueId, unsigned > > AssertionParentSet;
     472
    464473        static const int recursionLimit = /*10*/ 4;  ///< Limit to depth of recursion satisfaction
     474        //static const unsigned recursionParentLimit = 1;  ///< Limit to the number of times an assertion can recursively use itself
    465475
    466476        void addToIndexer( AssertionSet &assertSet, SymTab::Indexer &indexer ) {
     
    473483
    474484        template< typename ForwardIterator, typename OutputIterator >
    475         void inferRecursive( ForwardIterator begin, ForwardIterator end, const Alternative &newAlt, OpenVarSet &openVars, const SymTab::Indexer &decls, const AssertionSet &newNeed, int level, const SymTab::Indexer &indexer, OutputIterator out ) {
     485        void inferRecursive( ForwardIterator begin, ForwardIterator end, const Alternative &newAlt, OpenVarSet &openVars, const SymTab::Indexer &decls, const AssertionSet &newNeed, /*const AssertionParentSet &needParents,*/
     486                                                 int level, const SymTab::Indexer &indexer, OutputIterator out ) {
    476487                if ( begin == end ) {
    477488                        if ( newNeed.empty() ) {
     
    491502                                        printAssertionSet( newNeed, std::cerr, 8 );
    492503                                )
    493                                 inferRecursive( newNeed.begin(), newNeed.end(), newAlt, openVars, decls, newerNeed, level+1, indexer, out );
     504                                inferRecursive( newNeed.begin(), newNeed.end(), newAlt, openVars, decls, newerNeed, /*needParents,*/ level+1, indexer, out );
    494505                                return;
    495506                        }
     
    498509                ForwardIterator cur = begin++;
    499510                if ( ! cur->second.isUsed ) {
    500                         inferRecursive( begin, end, newAlt, openVars, decls, newNeed, level, indexer, out );
     511                        inferRecursive( begin, end, newAlt, openVars, decls, newNeed, /*needParents,*/ level, indexer, out );
    501512                        return; // xxx - should this continue? previously this wasn't here, and it looks like it should be
    502513                }
     
    551562                                }
    552563
     564                                //AssertionParentSet newNeedParents( needParents );
     565                                // skip repeatingly-self-recursive assertion satisfaction
     566                                // DOESN'T WORK: grandchild nodes conflict with their cousins
     567                                //if ( newNeedParents[ curDecl->get_uniqueId() ][ candDecl->get_uniqueId() ]++ > recursionParentLimit ) continue;
     568
    553569                                Expression *varExpr = data.combine( newerAlt.cvtCost );
     570                                delete varExpr->get_result();
    554571                                varExpr->set_result( adjType->clone() );
    555572                                PRINT(
     
    565582                                        inferParameters = (*inferParameters)[ id ].inferParams.get();
    566583                                }
    567                                
    568                                 (*inferParameters)[ curDecl->get_uniqueId() ] = ParamEntry( candidate->get_uniqueId(), adjType, curDecl->get_type(), varExpr );
    569                                 inferRecursive( begin, end, newerAlt, newOpenVars, newDecls, newerNeed, level, indexer, out );
     584                                // XXX: this is a memory leak, but adjType can't be deleted because it might contain assertions
     585                                (*inferParameters)[ curDecl->get_uniqueId() ] = ParamEntry( candidate->get_uniqueId(), adjType->clone(), curDecl->get_type()->clone(), varExpr );
     586                                inferRecursive( begin, end, newerAlt, newOpenVars, newDecls, newerNeed, /*newNeedParents,*/ level, indexer, out );
     587                        } else {
     588                                delete adjType;
    570589                        }
    571590                }
     
    587606                addToIndexer( have, decls );
    588607                AssertionSet newNeed;
     608                //AssertionParentSet needParents;
    589609                PRINT(
    590610                        std::cerr << "env is: " << std::endl;
     
    593613                )
    594614
    595                 inferRecursive( need.begin(), need.end(), newAlt, openVars, decls, newNeed, 0, indexer, out );
     615                inferRecursive( need.begin(), need.end(), newAlt, openVars, decls, newNeed, /*needParents,*/ 0, indexer, out );
    596616//      PRINT(
    597617//          std::cerr << "declaration 14 is ";
     
    614634        struct ArgPack {
    615635                std::size_t parent;                ///< Index of parent pack
    616                 Expression* expr;                  ///< The argument stored here
     636                std::unique_ptr<Expression> expr;  ///< The argument stored here
    617637                Cost cost;                         ///< The cost of this argument
    618638                TypeEnvironment env;               ///< Environment for this pack
     
    626646
    627647                ArgPack()
    628                         : parent(0), expr(nullptr), cost(Cost::zero), env(), need(), have(), openVars(),
    629                           nextArg(0), tupleStart(0), nextExpl(0), explAlt(0) {}
     648                        : parent(0), expr(), cost(Cost::zero), env(), need(), have(), openVars(), nextArg(0),
     649                          tupleStart(0), nextExpl(0), explAlt(0) {}
    630650
    631651                ArgPack(const TypeEnvironment& env, const AssertionSet& need, const AssertionSet& have,
    632652                                const OpenVarSet& openVars)
    633                         : parent(0), expr(nullptr), cost(Cost::zero), env(env), need(need), have(have),
     653                        : parent(0), expr(), cost(Cost::zero), env(env), need(need), have(have),
    634654                          openVars(openVars), nextArg(0), tupleStart(0), nextExpl(0), explAlt(0) {}
    635655
     
    638658                                unsigned tupleStart = 0, Cost cost = Cost::zero, unsigned nextExpl = 0,
    639659                                unsigned explAlt = 0 )
    640                         : parent(parent), expr(expr), cost(cost), env(move(env)), need(move(need)),
     660                        : parent(parent), expr(expr->clone()), cost(cost), env(move(env)), need(move(need)),
    641661                          have(move(have)), openVars(move(openVars)), nextArg(nextArg), tupleStart(tupleStart),
    642662                          nextExpl(nextExpl), explAlt(explAlt) {}
     
    644664                ArgPack(const ArgPack& o, TypeEnvironment&& env, AssertionSet&& need, AssertionSet&& have,
    645665                                OpenVarSet&& openVars, unsigned nextArg, Cost added )
    646                         : parent(o.parent), expr(o.expr), cost(o.cost + added), env(move(env)),
    647                           need(move(need)), have(move(have)), openVars(move(openVars)), nextArg(nextArg),
    648                           tupleStart(o.tupleStart), nextExpl(0), explAlt(0) {}
     666                        : parent(o.parent), expr(o.expr ? o.expr->clone() : nullptr), cost(o.cost + added),
     667                          env(move(env)), need(move(need)), have(move(have)), openVars(move(openVars)),
     668                          nextArg(nextArg), tupleStart(o.tupleStart), nextExpl(0), explAlt(0) {}
    649669
    650670                /// true iff this pack is in the middle of an exploded argument
     
    661681                        std::list<Expression*> exprs;
    662682                        const ArgPack* pack = this;
    663                         if ( expr ) { exprs.push_front( expr ); }
     683                        if ( expr ) { exprs.push_front( expr.release() ); }
    664684                        while ( pack->tupleStart == 0 ) {
    665685                                pack = &packs[pack->parent];
    666                                 exprs.push_front( pack->expr );
     686                                exprs.push_front( pack->expr->clone() );
    667687                                cost += pack->cost;
    668688                        }
    669689                        // reset pack to appropriate tuple
    670                         expr = new TupleExpr{ exprs };
     690                        expr.reset( new TupleExpr( exprs ) );
    671691                        tupleStart = pack->tupleStart - 1;
    672692                        parent = pack->parent;
     
    721741
    722742                                                results.emplace_back(
    723                                                         i, expl.exprs[results[i].nextExpl], copy(results[i].env),
     743                                                        i, expl.exprs[results[i].nextExpl].get(), copy(results[i].env),
    724744                                                        copy(results[i].need), copy(results[i].have),
    725745                                                        copy(results[i].openVars), nextArg, nTuples, Cost::zero, nextExpl,
     
    742762                                                        newResult.parent = i;
    743763                                                        std::list<Expression*> emptyList;
    744                                                         newResult.expr = new TupleExpr{ emptyList };
     764                                                        newResult.expr.reset( new TupleExpr( emptyList ) );
    745765                                                        argType = newResult.expr->get_result();
    746766                                                } else {
     
    749769                                                        newResult.cost = results[i].cost;
    750770                                                        newResult.tupleStart = results[i].tupleStart;
    751                                                         newResult.expr = results[i].expr;
     771                                                        newResult.expr.reset( results[i].expr->clone() );
    752772                                                        argType = newResult.expr->get_result();
    753773
     
    799819                                                // add new result
    800820                                                results.emplace_back(
    801                                                         i, expl.exprs.front(), move(env), copy(results[i].need),
     821                                                        i, expl.exprs.front().get(), move(env), copy(results[i].need),
    802822                                                        copy(results[i].have), move(openVars), nextArg + 1,
    803823                                                        nTuples, expl.cost, expl.exprs.size() == 1 ? 0 : 1, j );
     
    825845                        if ( results[i].hasExpl() ) {
    826846                                const ExplodedActual& expl = results[i].getExpl( args );
    827                                 Expression* expr = expl.exprs[results[i].nextExpl];
     847                                Expression* expr = expl.exprs[results[i].nextExpl].get();
    828848
    829849                                TypeEnvironment env = results[i].env;
     
    896916
    897917                                // consider only first exploded actual
    898                                 Expression* expr = expl.exprs.front();
     918                                Expression* expr = expl.exprs.front().get();
    899919                                Type* actualType = expr->result->clone();
    900920
     
    924944
    925945        template<typename OutputIterator>
    926         void AlternativeFinder::Finder::validateFunctionAlternative( const Alternative &func,
    927                         ArgPack& result, const std::vector<ArgPack>& results, OutputIterator out ) {
    928                 ApplicationExpr *appExpr = new ApplicationExpr( func.expr );
     946        void AlternativeFinder::Finder::validateFunctionAlternative( const Alternative &func, ArgPack& result,
     947                        const std::vector<ArgPack>& results, OutputIterator out ) {
     948                ApplicationExpr *appExpr = new ApplicationExpr( func.expr->clone() );
    929949                // sum cost and accumulate actuals
    930950                std::list<Expression*>& args = appExpr->args;
     
    932952                const ArgPack* pack = &result;
    933953                while ( pack->expr ) {
    934                         args.push_front( pack->expr );
     954                        args.push_front( pack->expr->clone() );
    935955                        cost += pack->cost;
    936956                        pack = &results[pack->parent];
     
    9991019
    10001020                                                results.emplace_back(
    1001                                                         i, expl.exprs[results[i].nextExpl], copy(results[i].env),
     1021                                                        i, expl.exprs[results[i].nextExpl].get(), copy(results[i].env),
    10021022                                                        copy(results[i].need), copy(results[i].have),
    10031023                                                        copy(results[i].openVars), nextArg, 0, Cost::zero, nextExpl,
     
    10351055                                                // add new result
    10361056                                                results.emplace_back(
    1037                                                         i, expl.exprs.front(), move(env), copy(results[i].need),
     1057                                                        i, expl.exprs.front().get(), move(env), copy(results[i].need),
    10381058                                                        copy(results[i].have), move(openVars), nextArg + 1, 0,
    10391059                                                        expl.cost, expl.exprs.size() == 1 ? 0 : 1, j );
     
    10691089
    10701090                // find function operators
    1071                 static auto *opExpr = new_static_root<NameExpr>( "?()" );
     1091                static NameExpr *opExpr = new NameExpr( "?()" );
    10721092                AlternativeFinder funcOpFinder( indexer, env );
    10731093                // it's ok if there aren't any defined function ops
    1074                 funcOpFinder.maybeFind( opExpr );
     1094                funcOpFinder.maybeFind( opExpr);
    10751095                PRINT(
    10761096                        std::cerr << "known function ops:" << std::endl;
     
    11011121                                )
    11021122                                // check if the type is pointer to function
    1103                                 if ( PointerType *pointer = dynamic_cast< PointerType* >( func->expr->get_result()->stripReferences() ) ) {
    1104                                         if ( FunctionType *function = dynamic_cast< FunctionType* >( pointer->get_base() ) ) {
     1123                                if ( PointerType *pointer = dynamic_cast< PointerType* >( func->expr->result->stripReferences() ) ) {
     1124                                        if ( FunctionType *function = dynamic_cast< FunctionType* >( pointer->base ) ) {
    11051125                                                Alternative newFunc( *func );
    11061126                                                referenceToRvalueConversion( newFunc.expr, newFunc.cost );
     
    11081128                                                        std::back_inserter( candidates ) );
    11091129                                        }
    1110                                 } else if ( TypeInstType *typeInst = dynamic_cast< TypeInstType* >( func->expr->get_result()->stripReferences() ) ) { // handle ftype (e.g. *? on function pointer)
    1111                                         if ( const EqvClass *eqvClass = func->env.lookup( typeInst->get_name() ) ) {
    1112                                                 if ( FunctionType *function = dynamic_cast< FunctionType* >( eqvClass->type ) ) {
     1130                                } else if ( TypeInstType *typeInst = dynamic_cast< TypeInstType* >( func->expr->result->stripReferences() ) ) { // handle ftype (e.g. *? on function pointer)
     1131                                        EqvClass eqvClass;
     1132                                        if ( func->env.lookup( typeInst->name, eqvClass ) && eqvClass.type ) {
     1133                                                if ( FunctionType *function = dynamic_cast< FunctionType* >( eqvClass.type ) ) {
    11131134                                                        Alternative newFunc( *func );
    11141135                                                        referenceToRvalueConversion( newFunc.expr, newFunc.cost );
     
    11381159                                        // check if type is a pointer to function
    11391160                                        if ( PointerType* pointer = dynamic_cast<PointerType*>(
    1140                                                         funcOp->expr->get_result()->stripReferences() ) ) {
     1161                                                        funcOp->expr->result->stripReferences() ) ) {
    11411162                                                if ( FunctionType* function =
    1142                                                                 dynamic_cast<FunctionType*>( pointer->get_base() ) ) {
     1163                                                                dynamic_cast<FunctionType*>( pointer->base ) ) {
    11431164                                                        Alternative newFunc( *funcOp );
    11441165                                                        referenceToRvalueConversion( newFunc.expr, newFunc.cost );
     
    11621183                        PRINT(
    11631184                                ApplicationExpr *appExpr = strict_dynamic_cast< ApplicationExpr* >( withFunc.expr );
    1164                                 PointerType *pointer = strict_dynamic_cast< PointerType* >( appExpr->get_function()->get_result() );
    1165                                 FunctionType *function = strict_dynamic_cast< FunctionType* >( pointer->get_base() );
    1166                                 std::cerr << "Case +++++++++++++ " << appExpr->get_function() << std::endl;
     1185                                PointerType *pointer = strict_dynamic_cast< PointerType* >( appExpr->function->result );
     1186                                FunctionType *function = strict_dynamic_cast< FunctionType* >( pointer->base );
     1187                                std::cerr << "Case +++++++++++++ " << appExpr->function << std::endl;
    11671188                                std::cerr << "formals are:" << std::endl;
    1168                                 printAll( function->get_parameters(), std::cerr, 8 );
     1189                                printAll( function->parameters, std::cerr, 8 );
    11691190                                std::cerr << "actuals are:" << std::endl;
    1170                                 printAll( appExpr->get_args(), std::cerr, 8 );
     1191                                printAll( appExpr->args, std::cerr, 8 );
    11711192                                std::cerr << "bindings are:" << std::endl;
    11721193                                withFunc.env.print( std::cerr, 8 );
     
    12091230        bool isLvalue( Expression *expr ) {
    12101231                // xxx - recurse into tuples?
    1211                 return expr->result && ( expr->get_result()->get_lvalue() || dynamic_cast< ReferenceType * >( expr->get_result() ) );
     1232                return expr->result && ( expr->result->get_lvalue() || dynamic_cast< ReferenceType * >( expr->result ) );
    12121233        }
    12131234
     
    12181239                        if ( isLvalue( alt.expr ) ) {
    12191240                                alternatives.push_back(
    1220                                         Alternative{ new AddressExpr( alt.expr ), alt.env, alt.cost } );
     1241                                        Alternative{ new AddressExpr( alt.expr->clone() ), alt.env, alt.cost } );
    12211242                        } // if
    12221243                } // for
     
    12241245
    12251246        void AlternativeFinder::Finder::postvisit( LabelAddressExpr * expr ) {
    1226                 alternatives.push_back( Alternative{ expr, env, Cost::zero } );
     1247                alternatives.push_back( Alternative{ expr->clone(), env, Cost::zero } );
    12271248        }
    12281249
     
    12441265                                componentExprs.push_back( restructureCast( idx, toType->getComponent( i ), isGenerated ) );
    12451266                        }
     1267                        delete argExpr;
    12461268                        assert( componentExprs.size() > 0 );
    12471269                        // produce the tuple of casts
     
    13191341                for ( Alternative & alt : finder.alternatives ) {
    13201342                        alternatives.push_back( Alternative(
    1321                                 new VirtualCastExpr( alt.expr, castExpr->get_result()->clone() ),
     1343                                new VirtualCastExpr( alt.expr->clone(), castExpr->get_result()->clone() ),
    13221344                                alt.env, alt.cost ) );
    1323                 }
    1324         }
    1325 
    1326         namespace {
    1327                 /// Gets name from untyped member expression (member must be NameExpr)
    1328                 const std::string& get_member_name( UntypedMemberExpr *memberExpr ) {
    1329                         NameExpr * nameExpr = dynamic_cast< NameExpr * >( memberExpr->get_member() );
    1330                         assert( nameExpr );
    1331                         return nameExpr->get_name();
    13321345                }
    13331346        }
     
    13411354                        Expression * aggrExpr = agg->expr->clone();
    13421355                        referenceToRvalueConversion( aggrExpr, cost );
     1356                        std::unique_ptr<Expression> guard( aggrExpr );
    13431357
    13441358                        // find member of the given type
    13451359                        if ( StructInstType *structInst = dynamic_cast< StructInstType* >( aggrExpr->get_result() ) ) {
    1346                                 addAggMembers( structInst, aggrExpr, cost, agg->env, get_member_name(memberExpr) );
     1360                                addAggMembers( structInst, aggrExpr, cost, agg->env, memberExpr->get_member() );
    13471361                        } else if ( UnionInstType *unionInst = dynamic_cast< UnionInstType* >( aggrExpr->get_result() ) ) {
    1348                                 addAggMembers( unionInst, aggrExpr, cost, agg->env, get_member_name(memberExpr) );
     1362                                addAggMembers( unionInst, aggrExpr, cost, agg->env, memberExpr->get_member() );
    13491363                        } else if ( TupleType * tupleType = dynamic_cast< TupleType * >( aggrExpr->get_result() ) ) {
    13501364                                addTupleMembers( tupleType, aggrExpr, cost, agg->env, memberExpr->get_member() );
     
    13541368
    13551369        void AlternativeFinder::Finder::postvisit( MemberExpr *memberExpr ) {
    1356                 alternatives.push_back( Alternative( memberExpr, env, Cost::zero ) );
     1370                alternatives.push_back( Alternative( memberExpr->clone(), env, Cost::zero ) );
    13571371        }
    13581372
     
    13851399
    13861400        void AlternativeFinder::Finder::postvisit( ConstantExpr *constantExpr ) {
    1387                 alternatives.push_back( Alternative( constantExpr, env, Cost::zero ) );
     1401                alternatives.push_back( Alternative( constantExpr->clone(), env, Cost::zero ) );
    13881402        }
    13891403
     
    14051419                        Alternative &choice = winners.front();
    14061420                        referenceToRvalueConversion( choice.expr, choice.cost );
    1407                         alternatives.push_back( Alternative( new SizeofExpr( choice.expr ), choice.env, Cost::zero ) );
     1421                        alternatives.push_back( Alternative( new SizeofExpr( choice.expr->clone() ), choice.env, Cost::zero ) );
    14081422                } // if
    14091423        }
     
    14261440                        Alternative &choice = winners.front();
    14271441                        referenceToRvalueConversion( choice.expr, choice.cost );
    1428                         alternatives.push_back( Alternative( new AlignofExpr( choice.expr ), choice.env, Cost::zero ) );
     1442                        alternatives.push_back( Alternative( new AlignofExpr( choice.expr->clone() ), choice.env, Cost::zero ) );
    14291443                } // if
    14301444        }
     
    14551469
    14561470        void AlternativeFinder::Finder::postvisit( OffsetofExpr *offsetofExpr ) {
    1457                 alternatives.push_back( Alternative( offsetofExpr, env, Cost::zero ) );
     1471                alternatives.push_back( Alternative( offsetofExpr->clone(), env, Cost::zero ) );
    14581472        }
    14591473
    14601474        void AlternativeFinder::Finder::postvisit( OffsetPackExpr *offsetPackExpr ) {
    1461                 alternatives.push_back( Alternative( offsetPackExpr, env, Cost::zero ) );
     1475                alternatives.push_back( Alternative( offsetPackExpr->clone(), env, Cost::zero ) );
    14621476        }
    14631477
     
    15371551                                compositeEnv.simpleCombine( second.env );
    15381552
    1539                                 LogicalExpr *newExpr = new LogicalExpr( first.expr, second.expr, logicalExpr->get_isAnd() );
     1553                                LogicalExpr *newExpr = new LogicalExpr( first.expr->clone(), second.expr->clone(), logicalExpr->get_isAnd() );
    15401554                                alternatives.push_back( Alternative( newExpr, compositeEnv, first.cost + second.cost ) );
    15411555                        }
     
    15701584                                        Type* commonType = nullptr;
    15711585                                        if ( unify( second.expr->result, third.expr->result, newAlt.env, needAssertions, haveAssertions, openVars, indexer, commonType ) ) {
    1572                                                 ConditionalExpr *newExpr = new ConditionalExpr( first.expr, second.expr, third.expr );
     1586                                                ConditionalExpr *newExpr = new ConditionalExpr( first.expr->clone(), second.expr->clone(), third.expr->clone() );
    15731587                                                newExpr->result = commonType ? commonType : second.expr->result->clone();
    15741588                                                // convert both options to the conditional result type
     
    15891603                secondFinder.findWithAdjustment( commaExpr->get_arg2() );
    15901604                for ( const Alternative & alt : secondFinder.alternatives ) {
    1591                         alternatives.push_back( Alternative( new CommaExpr( newFirstArg, alt.expr ), alt.env, alt.cost ) );
     1605                        alternatives.push_back( Alternative( new CommaExpr( newFirstArg->clone(), alt.expr->clone() ), alt.env, alt.cost ) );
    15921606                } // for
     1607                delete newFirstArg;
    15931608        }
    15941609
     
    16111626                                Type* commonType = nullptr;
    16121627                                if ( unify( first.expr->result, second.expr->result, newAlt.env, needAssertions, haveAssertions, openVars, indexer, commonType ) ) {
    1613                                         RangeExpr * newExpr = new RangeExpr( first.expr, second.expr );
     1628                                        RangeExpr * newExpr = new RangeExpr( first.expr->clone(), second.expr->clone() );
    16141629                                        newExpr->result = commonType ? commonType : first.expr->result->clone();
    16151630                                        newAlt.expr = newExpr;
     
    16391654
    16401655        void AlternativeFinder::Finder::postvisit( TupleExpr *tupleExpr ) {
    1641                 alternatives.push_back( Alternative( tupleExpr, env, Cost::zero ) );
     1656                alternatives.push_back( Alternative( tupleExpr->clone(), env, Cost::zero ) );
    16421657        }
    16431658
    16441659        void AlternativeFinder::Finder::postvisit( ImplicitCopyCtorExpr * impCpCtorExpr ) {
    1645                 alternatives.push_back( Alternative( impCpCtorExpr, env, Cost::zero ) );
     1660                alternatives.push_back( Alternative( impCpCtorExpr->clone(), env, Cost::zero ) );
    16461661        }
    16471662
     
    16521667                finder.findWithoutPrune( ctorExpr->get_callExpr() );
    16531668                for ( Alternative & alt : finder.alternatives ) {
    1654                         alternatives.push_back( Alternative( new ConstructorExpr( alt.expr ), alt.env, alt.cost ) );
     1669                        alternatives.push_back( Alternative( new ConstructorExpr( alt.expr->clone() ), alt.env, alt.cost ) );
    16551670                }
    16561671        }
    16571672
    16581673        void AlternativeFinder::Finder::postvisit( TupleIndexExpr *tupleExpr ) {
    1659                 alternatives.push_back( Alternative( tupleExpr, env, Cost::zero ) );
     1674                alternatives.push_back( Alternative( tupleExpr->clone(), env, Cost::zero ) );
    16601675        }
    16611676
    16621677        void AlternativeFinder::Finder::postvisit( TupleAssignExpr *tupleAssignExpr ) {
    1663                 alternatives.push_back( Alternative( tupleAssignExpr, env, Cost::zero ) );
     1678                alternatives.push_back( Alternative( tupleAssignExpr->clone(), env, Cost::zero ) );
    16641679        }
    16651680
     
    16691684                for ( Alternative & alt : finder.alternatives ) {
    16701685                        // ensure that the id is passed on to the UniqueExpr alternative so that the expressions are "linked"
    1671                         UniqueExpr * newUnqExpr = new UniqueExpr( alt.expr, unqExpr->get_id() );
     1686                        UniqueExpr * newUnqExpr = new UniqueExpr( alt.expr->clone(), unqExpr->get_id() );
    16721687                        alternatives.push_back( Alternative( newUnqExpr, alt.env, alt.cost ) );
    16731688                }
     
    17201735                                        // count one safe conversion for each value that is thrown away
    17211736                                        thisCost.incSafe( discardedValues );
    1722                                         Alternative newAlt( new InitExpr( restructureCast( alt.expr, toType, true ), initAlt.designation ), newEnv, alt.cost, thisCost );
     1737                                        Alternative newAlt( new InitExpr( restructureCast( alt.expr->clone(), toType, true ), initAlt.designation->clone() ), newEnv, alt.cost, thisCost );
    17231738                                        inferParameters( needAssertions, haveAssertions, newAlt, openVars, back_inserter( candidates ) );
    17241739                                }
Note: See TracChangeset for help on using the changeset viewer.