Changeset bd4f2e9 for src


Ignore:
Timestamp:
Nov 22, 2017, 1:32:55 PM (6 years ago)
Author:
Aaron Moss <a3moss@…>
Branches:
ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
Children:
c2c6177
Parents:
73a5cadb
Message:

Switch AltList? to std::vector from std::list

Location:
src
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • src/ResolvExpr/Alternative.cc

    r73a5cadb rbd4f2e9  
    1818#include <ostream>                       // for operator<<, ostream, basic_o...
    1919#include <string>                        // for operator<<, char_traits, string
     20#include <utility>                       // for move
    2021
    2122#include "Common/utility.h"              // for maybeClone
     
    8182                os << std::endl;
    8283        }
     84
     85        void splice( AltList& dst, AltList& src ) {
     86                dst.reserve( dst.size() + src.size() );
     87                for ( Alternative& alt : src ) {
     88                        dst.push_back( std::move(alt) );
     89                }
     90                src.clear();
     91        }
     92
     93        void spliceBegin( AltList& dst, AltList& src ) {
     94                splice( src, dst );
     95                dst.swap( src );
     96        }
     97
    8398} // namespace ResolvExpr
    8499
  • src/ResolvExpr/Alternative.h

    r73a5cadb rbd4f2e9  
    1717
    1818#include <iosfwd>             // for ostream
    19 #include <list>               // for list
     19#include <vector>             // for vector
    2020
    2121#include "Cost.h"             // for Cost
     
    2525
    2626namespace ResolvExpr {
    27         struct Alternative;
    28 
    29         typedef std::list< Alternative > AltList;
    30 
    3127        struct Alternative {
    3228                Alternative();
     
    4642                TypeEnvironment env;
    4743        };
     44
     45        typedef std::vector< Alternative > AltList;
     46
     47        /// Moves all elements from src to the end of dst
     48        void splice( AltList& dst, AltList& src );
     49
     50        /// Moves all elements from src to the beginning of dst
     51        void spliceBegin( AltList& dst, AltList& src );
    4852} // namespace ResolvExpr
    4953
  • src/ResolvExpr/AlternativeFinder.cc

    r73a5cadb rbd4f2e9  
    194194                                printAlts( alternatives, std::cerr );
    195195                        )
    196                         AltList::iterator oldBegin = alternatives.begin();
    197                         pruneAlternatives( alternatives.begin(), alternatives.end(), front_inserter( alternatives ) );
    198                         if ( failFast && alternatives.begin() == oldBegin ) {
     196                        AltList pruned;
     197                        pruneAlternatives( alternatives.begin(), alternatives.end(), back_inserter( pruned ) );
     198                        if ( failFast && pruned.empty() ) {
    199199                                std::ostringstream stream;
    200200                                AltList winners;
     
    206206                                throw SemanticError( stream.str() );
    207207                        }
    208                         alternatives.erase( oldBegin, alternatives.end() );
     208                        alternatives = move(pruned);
    209209                        PRINT(
    210210                                std::cerr << "there are " << oldsize << " alternatives before elimination" << std::endl;
     
    11191119
    11201120                // compute conversionsion costs
    1121                 for ( AltList::iterator withFunc = candidates.begin(); withFunc != candidates.end(); ++withFunc ) {
    1122                         Cost cvtCost = computeApplicationConversionCost( *withFunc, indexer );
     1121                for ( Alternative& withFunc : candidates ) {
     1122                        Cost cvtCost = computeApplicationConversionCost( withFunc, indexer );
    11231123
    11241124                        PRINT(
    1125                                 ApplicationExpr *appExpr = strict_dynamic_cast< ApplicationExpr* >( withFunc->expr );
     1125                                ApplicationExpr *appExpr = strict_dynamic_cast< ApplicationExpr* >( withFunc.expr );
    11261126                                PointerType *pointer = strict_dynamic_cast< PointerType* >( appExpr->get_function()->get_result() );
    11271127                                FunctionType *function = strict_dynamic_cast< FunctionType* >( pointer->get_base() );
     
    11321132                                printAll( appExpr->get_args(), std::cerr, 8 );
    11331133                                std::cerr << "bindings are:" << std::endl;
    1134                                 withFunc->env.print( std::cerr, 8 );
     1134                                withFunc.env.print( std::cerr, 8 );
    11351135                                std::cerr << "cost of conversion is:" << cvtCost << std::endl;
    11361136                        )
    11371137                        if ( cvtCost != Cost::infinity ) {
    1138                                 withFunc->cvtCost = cvtCost;
    1139                                 alternatives.push_back( *withFunc );
     1138                                withFunc.cvtCost = cvtCost;
     1139                                alternatives.push_back( withFunc );
    11401140                        } // if
    11411141                } // for
    11421142
    1143                 candidates.clear();
    1144                 candidates.splice( candidates.end(), alternatives );
     1143                candidates = move(alternatives);
    11451144
    11461145                // use a new list so that alternatives are not examined by addAnonConversions twice.
     
    11481147                findMinCost( candidates.begin(), candidates.end(), std::back_inserter( winners ) );
    11491148
    1150                 // function may return struct or union value, in which case we need to add alternatives for implicit
    1151                 // conversions to each of the anonymous members, must happen after findMinCost since anon conversions
    1152                 // are never the cheapest expression
     1149                // function may return struct or union value, in which case we need to add alternatives
     1150                // for implicitconversions to each of the anonymous members, must happen after findMinCost
     1151                // since anon conversions are never the cheapest expression
    11531152                for ( const Alternative & alt : winners ) {
    11541153                        addAnonConversions( alt );
    11551154                }
    1156                 alternatives.splice( alternatives.begin(), winners );
     1155                spliceBegin( alternatives, winners );
    11571156
    11581157                if ( alternatives.empty() && targetType && ! targetType->isVoid() ) {
     
    11781177                AlternativeFinder finder( indexer, env );
    11791178                finder.find( addressExpr->get_arg() );
    1180                 for ( std::list< Alternative >::iterator i = finder.alternatives.begin(); i != finder.alternatives.end(); ++i ) {
    1181                         if ( isLvalue( i->expr ) ) {
    1182                                 alternatives.push_back( Alternative( new AddressExpr( i->expr->clone() ), i->env, i->cost ) );
     1179                for ( Alternative& alt : finder.alternatives ) {
     1180                        if ( isLvalue( alt.expr ) ) {
     1181                                alternatives.push_back(
     1182                                        Alternative{ new AddressExpr( alt.expr->clone() ), alt.env, alt.cost } );
    11831183                        } // if
    11841184                } // for
     
    11861186
    11871187        void AlternativeFinder::visit( LabelAddressExpr * expr ) {
    1188                 alternatives.push_back( Alternative( expr->clone(), env, Cost::zero) );
     1188                alternatives.push_back( Alternative{ expr->clone(), env, Cost::zero } );
    11891189        }
    11901190
     
    12281228
    12291229                AltList candidates;
    1230                 for ( std::list< Alternative >::iterator i = finder.alternatives.begin(); i != finder.alternatives.end(); ++i ) {
     1230                for ( Alternative& alt : finder.alternatives ) {
    12311231                        AssertionSet needAssertions, haveAssertions;
    12321232                        OpenVarSet openVars;
     
    12361236                        // that are cast directly.  The candidate is invalid if it has fewer results than there are types to cast
    12371237                        // to.
    1238                         int discardedValues = i->expr->get_result()->size() - castExpr->get_result()->size();
     1238                        int discardedValues = alt.expr->get_result()->size() - castExpr->get_result()->size();
    12391239                        if ( discardedValues < 0 ) continue;
    12401240                        // xxx - may need to go into tuple types and extract relevant types and use unifyList. Note that currently, this does not
    12411241                        // allow casting a tuple to an atomic type (e.g. (int)([1, 2, 3]))
    12421242                        // unification run for side-effects
    1243                         unify( castExpr->get_result(), i->expr->get_result(), i->env, needAssertions, haveAssertions, openVars, indexer );
    1244                         Cost thisCost = castCost( i->expr->get_result(), castExpr->get_result(), indexer, i->env );
     1243                        unify( castExpr->get_result(), alt.expr->get_result(), alt.env, needAssertions,
     1244                                haveAssertions, openVars, indexer );
     1245                        Cost thisCost = castCost( alt.expr->get_result(), castExpr->get_result(), indexer,
     1246                                alt.env );
    12451247                        if ( thisCost != Cost::infinity ) {
    12461248                                // count one safe conversion for each value that is thrown away
    12471249                                thisCost.incSafe( discardedValues );
    1248                                 Alternative newAlt( restructureCast( i->expr->clone(), toType ), i->env, i->cost, thisCost );
    1249                                 inferParameters( needAssertions, haveAssertions, newAlt, openVars, back_inserter( candidates ) );
     1250                                Alternative newAlt( restructureCast( alt.expr->clone(), toType ), alt.env,
     1251                                        alt.cost, thisCost );
     1252                                inferParameters( needAssertions, haveAssertions, newAlt, openVars,
     1253                                        back_inserter( candidates ) );
    12501254                        } // if
    12511255                } // for
     
    15341538
    15351539        void AlternativeFinder::visit( UntypedTupleExpr *tupleExpr ) {
    1536                 std::list< AlternativeFinder > subExprAlternatives;
    1537                 findSubExprs( tupleExpr->get_exprs().begin(), tupleExpr->get_exprs().end(), back_inserter( subExprAlternatives ) );
    1538                 std::list< AltList > possibilities;
    1539                 combos( subExprAlternatives.begin(), subExprAlternatives.end(), back_inserter( possibilities ) );
    1540                 for ( std::list< AltList >::const_iterator i = possibilities.begin(); i != possibilities.end(); ++i ) {
     1540                std::vector< AlternativeFinder > subExprAlternatives;
     1541                findSubExprs( tupleExpr->get_exprs().begin(), tupleExpr->get_exprs().end(),
     1542                        back_inserter( subExprAlternatives ) );
     1543                std::vector< AltList > possibilities;
     1544                combos( subExprAlternatives.begin(), subExprAlternatives.end(),
     1545                        back_inserter( possibilities ) );
     1546                for ( const AltList& alts : possibilities ) {
    15411547                        std::list< Expression * > exprs;
    1542                         makeExprList( *i, exprs );
     1548                        makeExprList( alts, exprs );
    15431549
    15441550                        TypeEnvironment compositeEnv;
    1545                         simpleCombineEnvironments( i->begin(), i->end(), compositeEnv );
    1546                         alternatives.push_back( Alternative( new TupleExpr( exprs ) , compositeEnv, sumCost( *i ) ) );
     1551                        simpleCombineEnvironments( alts.begin(), alts.end(), compositeEnv );
     1552                        alternatives.push_back(
     1553                                Alternative{ new TupleExpr( exprs ), compositeEnv, sumCost( alts ) } );
    15471554                } // for
    15481555        }
  • src/ResolvExpr/Resolver.cc

    r73a5cadb rbd4f2e9  
    1818#include <memory>                        // for allocator, allocator_traits<...
    1919#include <tuple>                         // for get
     20#include <vector>
    2021
    2122#include "Alternative.h"                 // for Alternative, AltList
     
    411412
    412413                        // Find all alternatives for all arguments in canonical form
    413                         std::list< AlternativeFinder > argAlternatives;
     414                        std::vector< AlternativeFinder > argAlternatives;
    414415                        funcFinder.findSubExprs( clause.target.arguments.begin(), clause.target.arguments.end(), back_inserter( argAlternatives ) );
    415416
    416417                        // List all combinations of arguments
    417                         std::list< AltList > possibilities;
     418                        std::vector< AltList > possibilities;
    418419                        combos( argAlternatives.begin(), argAlternatives.end(), back_inserter( possibilities ) );
    419420
  • src/ResolvExpr/typeops.h

    r73a5cadb rbd4f2e9  
    1616#pragma once
    1717
     18#include <vector>
     19
    1820#include "SynTree/SynTree.h"
    1921#include "SynTree/Type.h"
     
    2830        void combos( InputIterator begin, InputIterator end, OutputIterator out ) {
    2931                typedef typename InputIterator::value_type SetType;
    30                 typedef typename std::list< typename SetType::value_type > ListType;
     32                typedef typename std::vector< typename SetType::value_type > ListType;
    3133
    3234                if ( begin == end )     {
     
    3840                begin++;
    3941
    40                 std::list< ListType > recursiveResult;
     42                std::vector< ListType > recursiveResult;
    4143                combos( begin, end, back_inserter( recursiveResult ) );
    4244
    43                 for ( typename std::list< ListType >::const_iterator i = recursiveResult.begin(); i != recursiveResult.end(); ++i ) {
    44                         for ( typename ListType::const_iterator j = current->begin(); j != current->end(); ++j ) {
    45                                 ListType result;
    46                                 std::back_insert_iterator< ListType > inserter = back_inserter( result );
    47                                 *inserter++ = *j;
    48                                 std::copy( i->begin(), i->end(), inserter );
    49                                 *out++ = result;
    50                         } // for
    51                 } // for
     45                for ( const auto& i : recursiveResult ) for ( const auto& j : *current ) {
     46                        ListType result;
     47                        std::back_insert_iterator< ListType > inserter = back_inserter( result );
     48                        *inserter++ = j;
     49                        std::copy( i.begin(), i.end(), inserter );
     50                        *out++ = result;
     51                }
    5252        }
    5353
  • src/Tuples/TupleAssignment.cc

    r73a5cadb rbd4f2e9  
    251251                // combine assignment environments into combined expression environment
    252252                simpleCombineEnvironments( current.begin(), current.end(), matcher->compositeEnv );
    253                 currentFinder.get_alternatives().push_front( ResolvExpr::Alternative(
     253                // xxx -- was push_front
     254                currentFinder.get_alternatives().push_back( ResolvExpr::Alternative(
    254255                        new TupleAssignExpr(solved_assigns, matcher->tmpDecls), matcher->compositeEnv,
    255256                        ResolvExpr::sumCost( current ) + matcher->baseCost ) );
Note: See TracChangeset for help on using the changeset viewer.