Changeset bd4f2e9
- Timestamp:
- Nov 22, 2017, 1:32:55 PM (7 years ago)
- 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
- Location:
- src
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
src/ResolvExpr/Alternative.cc
r73a5cadb rbd4f2e9 18 18 #include <ostream> // for operator<<, ostream, basic_o... 19 19 #include <string> // for operator<<, char_traits, string 20 #include <utility> // for move 20 21 21 22 #include "Common/utility.h" // for maybeClone … … 81 82 os << std::endl; 82 83 } 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 83 98 } // namespace ResolvExpr 84 99 -
src/ResolvExpr/Alternative.h
r73a5cadb rbd4f2e9 17 17 18 18 #include <iosfwd> // for ostream 19 #include < list> // for list19 #include <vector> // for vector 20 20 21 21 #include "Cost.h" // for Cost … … 25 25 26 26 namespace ResolvExpr { 27 struct Alternative;28 29 typedef std::list< Alternative > AltList;30 31 27 struct Alternative { 32 28 Alternative(); … … 46 42 TypeEnvironment env; 47 43 }; 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 ); 48 52 } // namespace ResolvExpr 49 53 -
src/ResolvExpr/AlternativeFinder.cc
r73a5cadb rbd4f2e9 194 194 printAlts( alternatives, std::cerr ); 195 195 ) 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() ) { 199 199 std::ostringstream stream; 200 200 AltList winners; … … 206 206 throw SemanticError( stream.str() ); 207 207 } 208 alternatives .erase( oldBegin, alternatives.end());208 alternatives = move(pruned); 209 209 PRINT( 210 210 std::cerr << "there are " << oldsize << " alternatives before elimination" << std::endl; … … 1119 1119 1120 1120 // compute conversionsion costs 1121 for ( Alt List::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 ); 1123 1123 1124 1124 PRINT( 1125 ApplicationExpr *appExpr = strict_dynamic_cast< ApplicationExpr* >( withFunc ->expr );1125 ApplicationExpr *appExpr = strict_dynamic_cast< ApplicationExpr* >( withFunc.expr ); 1126 1126 PointerType *pointer = strict_dynamic_cast< PointerType* >( appExpr->get_function()->get_result() ); 1127 1127 FunctionType *function = strict_dynamic_cast< FunctionType* >( pointer->get_base() ); … … 1132 1132 printAll( appExpr->get_args(), std::cerr, 8 ); 1133 1133 std::cerr << "bindings are:" << std::endl; 1134 withFunc ->env.print( std::cerr, 8 );1134 withFunc.env.print( std::cerr, 8 ); 1135 1135 std::cerr << "cost of conversion is:" << cvtCost << std::endl; 1136 1136 ) 1137 1137 if ( cvtCost != Cost::infinity ) { 1138 withFunc ->cvtCost = cvtCost;1139 alternatives.push_back( *withFunc );1138 withFunc.cvtCost = cvtCost; 1139 alternatives.push_back( withFunc ); 1140 1140 } // if 1141 1141 } // for 1142 1142 1143 candidates.clear(); 1144 candidates.splice( candidates.end(), alternatives ); 1143 candidates = move(alternatives); 1145 1144 1146 1145 // use a new list so that alternatives are not examined by addAnonConversions twice. … … 1148 1147 findMinCost( candidates.begin(), candidates.end(), std::back_inserter( winners ) ); 1149 1148 1150 // function may return struct or union value, in which case we need to add alternatives for implicit1151 // conversions to each of the anonymous members, must happen after findMinCost since anon conversions1152 // are never the cheapest expression1149 // 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 1153 1152 for ( const Alternative & alt : winners ) { 1154 1153 addAnonConversions( alt ); 1155 1154 } 1156 alternatives.splice( alternatives.begin(), winners );1155 spliceBegin( alternatives, winners ); 1157 1156 1158 1157 if ( alternatives.empty() && targetType && ! targetType->isVoid() ) { … … 1178 1177 AlternativeFinder finder( indexer, env ); 1179 1178 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 } ); 1183 1183 } // if 1184 1184 } // for … … 1186 1186 1187 1187 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 } ); 1189 1189 } 1190 1190 … … 1228 1228 1229 1229 AltList candidates; 1230 for ( std::list< Alternative >::iterator i = finder.alternatives.begin(); i != finder.alternatives.end(); ++i) {1230 for ( Alternative& alt : finder.alternatives ) { 1231 1231 AssertionSet needAssertions, haveAssertions; 1232 1232 OpenVarSet openVars; … … 1236 1236 // that are cast directly. The candidate is invalid if it has fewer results than there are types to cast 1237 1237 // 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(); 1239 1239 if ( discardedValues < 0 ) continue; 1240 1240 // xxx - may need to go into tuple types and extract relevant types and use unifyList. Note that currently, this does not 1241 1241 // allow casting a tuple to an atomic type (e.g. (int)([1, 2, 3])) 1242 1242 // 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 ); 1245 1247 if ( thisCost != Cost::infinity ) { 1246 1248 // count one safe conversion for each value that is thrown away 1247 1249 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 ) ); 1250 1254 } // if 1251 1255 } // for … … 1534 1538 1535 1539 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 ) { 1541 1547 std::list< Expression * > exprs; 1542 makeExprList( *i, exprs );1548 makeExprList( alts, exprs ); 1543 1549 1544 1550 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 ) } ); 1547 1554 } // for 1548 1555 } -
src/ResolvExpr/Resolver.cc
r73a5cadb rbd4f2e9 18 18 #include <memory> // for allocator, allocator_traits<... 19 19 #include <tuple> // for get 20 #include <vector> 20 21 21 22 #include "Alternative.h" // for Alternative, AltList … … 411 412 412 413 // Find all alternatives for all arguments in canonical form 413 std:: list< AlternativeFinder > argAlternatives;414 std::vector< AlternativeFinder > argAlternatives; 414 415 funcFinder.findSubExprs( clause.target.arguments.begin(), clause.target.arguments.end(), back_inserter( argAlternatives ) ); 415 416 416 417 // List all combinations of arguments 417 std:: list< AltList > possibilities;418 std::vector< AltList > possibilities; 418 419 combos( argAlternatives.begin(), argAlternatives.end(), back_inserter( possibilities ) ); 419 420 -
src/ResolvExpr/typeops.h
r73a5cadb rbd4f2e9 16 16 #pragma once 17 17 18 #include <vector> 19 18 20 #include "SynTree/SynTree.h" 19 21 #include "SynTree/Type.h" … … 28 30 void combos( InputIterator begin, InputIterator end, OutputIterator out ) { 29 31 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; 31 33 32 34 if ( begin == end ) { … … 38 40 begin++; 39 41 40 std:: list< ListType > recursiveResult;42 std::vector< ListType > recursiveResult; 41 43 combos( begin, end, back_inserter( recursiveResult ) ); 42 44 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 } 52 52 } 53 53 -
src/Tuples/TupleAssignment.cc
r73a5cadb rbd4f2e9 251 251 // combine assignment environments into combined expression environment 252 252 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( 254 255 new TupleAssignExpr(solved_assigns, matcher->tmpDecls), matcher->compositeEnv, 255 256 ResolvExpr::sumCost( current ) + matcher->baseCost ) );
Note: See TracChangeset
for help on using the changeset viewer.