- Timestamp:
- Oct 17, 2018, 4:16:18 PM (6 years ago)
- Branches:
- ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, no_list, persistent-indexer, pthread-emulation, qualifiedEnum
- Children:
- fbecee5
- Parents:
- da48183
- Location:
- src
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
src/ResolvExpr/Alternative.cc
rda48183 r2c187378 20 20 #include <utility> // for move 21 21 22 #include "Common/utility.h" // for maybeClone22 #include "Common/utility.h" // for cloneAll 23 23 #include "ResolvExpr/Cost.h" // for Cost, Cost::zero, operator<< 24 24 #include "ResolvExpr/TypeEnvironment.h" // for TypeEnvironment … … 35 35 Alternative::Alternative( const Alternative &o, Expression *expr, const Cost &cost ) 36 36 : cost( cost ), cvtCost( Cost::zero ), expr( expr ), env( o.env ), openVars( o.openVars ), 37 need( o.need ) {}37 need() { cloneAll( o.need, need ); } 38 38 39 39 Alternative::Alternative( Expression *expr, const TypeEnvironment &env, 40 const OpenVarSet& openVars, const AssertionList& need, const Cost& cost )40 const OpenVarSet& openVars, const AssertionList& oneed, const Cost& cost ) 41 41 : cost( cost ), cvtCost( Cost::zero ), expr( expr ), env( env ), openVars( openVars ), 42 need( need ) {}42 need() { cloneAll( oneed, need ); } 43 43 44 44 Alternative::Alternative( Expression *expr, const TypeEnvironment &env, 45 const OpenVarSet& openVars, const AssertionList& need, const Cost& cost,45 const OpenVarSet& openVars, const AssertionList& oneed, const Cost& cost, 46 46 const Cost &cvtCost ) 47 47 : cost( cost ), cvtCost( cvtCost ), expr( expr ), env( env ), openVars( openVars ), 48 need( need ) {} 48 need() { cloneAll( oneed, need ); } 49 50 Alternative::Alternative( Expression *expr, const TypeEnvironment &env, 51 const OpenVarSet &openVars, const AssertionSet &oneed, const Cost &cost) 52 : cost( cost ), cvtCost( Cost::zero ), expr( expr ), env( env ), openVars( openVars ), 53 need() { cloneAll( oneed, need ); } 54 55 Alternative::Alternative( Expression *expr, const TypeEnvironment &env, 56 const OpenVarSet &openVars, const AssertionSet &oneed, const Cost &cost, 57 const Cost& cvtCost ) 58 : cost( cost ), cvtCost( cvtCost ), expr( expr ), env( env ), openVars( openVars ), 59 need() { cloneAll( oneed, need ); } 60 61 Alternative::Alternative( Expression *expr, TypeEnvironment &&env, OpenVarSet &&openVars, 62 AssertionSet &&needSet, const Cost &cost ) 63 : cost( cost ), cvtCost( Cost::zero ), expr( expr ), env( std::move(env) ), 64 openVars( std::move(openVars) ), need( needSet.begin(), needSet.end() ) {} 65 66 Alternative::Alternative( Expression *expr, TypeEnvironment &&env, OpenVarSet &&openVars, 67 AssertionSet &&needSet, const Cost &cost, const Cost &cvtCost ) 68 : cost( cost ), cvtCost( cvtCost ), expr( expr ), env( std::move(env) ), 69 openVars( std::move(openVars) ), need( needSet.begin(), needSet.end() ) {} 49 70 50 71 Alternative::Alternative( const Alternative &other ) 51 72 : cost( other.cost ), cvtCost( other.cvtCost ), expr( maybeClone( other.expr ) ), 52 env( other.env ), openVars( other.openVars ), need( other.need ) {}73 env( other.env ), openVars( other.openVars ), need() { cloneAll( other.need, need ); } 53 74 54 75 Alternative &Alternative::operator=( const Alternative &other ) { … … 60 81 env = other.env; 61 82 openVars = other.openVars; 62 need = other.need; 83 need.clear(); 84 cloneAll( other.need, need ); 63 85 return *this; 64 86 } … … 67 89 : cost( other.cost ), cvtCost( other.cvtCost ), expr( other.expr ), 68 90 env( std::move( other.env ) ), openVars( std::move( other.openVars ) ), 69 need( std::move( other.need ) ) { 70 other.expr = nullptr; 71 } 91 need( std::move( other.need ) ) { other.expr = nullptr; } 72 92 73 93 Alternative & Alternative::operator=( Alternative && other ) { -
src/ResolvExpr/Alternative.h
rda48183 r2c187378 22 22 #include "TypeEnvironment.h" // for TypeEnvironment, AssertionSetValue 23 23 24 #include "Common/utility.h" // for maybeClone 25 24 26 class Expression; 25 27 … … 35 37 AssertionItem( const AssertionSet::value_type& e ) : decl(e.first), info(e.second) {} 36 38 operator AssertionSet::value_type () const { return { decl, info }; } 39 40 // to support cloneAll 41 AssertionItem clone() const { return { maybeClone(decl), info }; } 37 42 }; 38 43 /// A list of unresolved assertions 39 44 using AssertionList = std::vector<AssertionItem>; 45 46 /// Clones an assertion list into an assertion set 47 static inline void cloneAll( const AssertionList& src, AssertionSet& dst ) { 48 for ( const AssertionItem& item : src ) { 49 dst.emplace( maybeClone(item.decl), item.info ); 50 } 51 } 52 53 /// Clones an assertion set into an assertion list 54 static inline void cloneAll( const AssertionSet& src, AssertionList& dst ) { 55 dst.reserve( dst.size() + src.size() ); 56 for ( const auto& entry : src ) { 57 dst.emplace_back( maybeClone(entry.first), entry.second ); 58 } 59 } 60 61 /// Clones an assertion list into an assertion list 62 static inline void cloneAll( const AssertionList& src, AssertionList& dst ) { 63 dst.reserve( dst.size() + src.size() ); 64 for ( const AssertionItem& item : src ) { 65 dst.emplace_back( maybeClone(item.decl), item.info ); 66 } 67 } 40 68 41 69 /// One option for resolution of an expression … … 48 76 Alternative( Expression *expr, const TypeEnvironment &env, const OpenVarSet& openVars, 49 77 const AssertionList& need, const Cost &cost, const Cost &cvtCost ); 78 Alternative( Expression *expr, const TypeEnvironment &env, const OpenVarSet &openVars, 79 const AssertionSet &need, const Cost &cost); 80 Alternative( Expression *expr, const TypeEnvironment &env, const OpenVarSet &openVars, 81 const AssertionSet &need, const Cost &cost, const Cost& cvtCost ); 82 Alternative( Expression *expr, TypeEnvironment &&env, OpenVarSet &&openVars, 83 AssertionSet &&need, const Cost &cost ); 84 Alternative( Expression *expr, TypeEnvironment &&env, OpenVarSet &&openVars, 85 AssertionSet &&need, const Cost &cost, const Cost &cvtCost ); 50 86 Alternative( const Alternative &other ); 51 87 Alternative &operator=( const Alternative &other ); -
src/ResolvExpr/AlternativeFinder.cc
rda48183 r2c187378 962 962 } 963 963 // build and validate new alternative 964 Alternative newAlt{ 965 appExpr, result.env, result.openVars, 966 AssertionList( result.need.begin(), result.need.end() ), cost }; 964 Alternative newAlt{ appExpr, result.env, result.openVars, result.need, cost }; 967 965 PRINT( 968 966 std::cerr << "instantiate function success: " << appExpr << std::endl; … … 1330 1328 Alternative newAlt{ 1331 1329 restructureCast( alt.expr->clone(), toType, castExpr->isGenerated ), 1332 alt.env, openVars, 1333 AssertionList( needAssertions.begin(), needAssertions.end() ), alt.cost, 1334 thisCost }; 1330 alt.env, openVars, needAssertions, alt.cost, thisCost }; 1335 1331 inferParameters( needAssertions, haveAssertions, newAlt, openVars, 1336 1332 back_inserter( candidates ) ); … … 1524 1520 Expression * newExpr = data.combine( cost ); 1525 1521 alternatives.push_back( Alternative{ 1526 new AttrExpr{ newExpr, argType->clone() }, env, OpenVarSet{}, AssertionList{},1527 Cost::zero, cost } );1522 new AttrExpr{ newExpr, argType->clone() }, env, OpenVarSet{}, 1523 AssertionList{}, Cost::zero, cost } ); 1528 1524 for ( DeclarationWithType * retVal : function->returnVals ) { 1529 1525 alternatives.back().expr->result = retVal->get_type()->clone(); … … 1584 1580 OpenVarSet openVars{ first.openVars }; 1585 1581 mergeOpenVars( openVars, second.openVars ); 1586 AssertionList need{ first.need }; 1587 need.insert( need.end(), second.need.begin(), second.need.end() ); 1582 AssertionSet need; 1583 cloneAll( first.need, need ); 1584 cloneAll( second.need, need ); 1588 1585 1589 1586 LogicalExpr *newExpr = new LogicalExpr{ 1590 1587 first.expr->clone(), second.expr->clone(), logicalExpr->get_isAnd() }; 1591 1588 alternatives.push_back( Alternative{ 1592 newExpr, compositeEnv, openVars, need, first.cost + second.cost } ); 1589 newExpr, std::move(compositeEnv), std::move(openVars), 1590 AssertionList( need.begin(), need.end() ), first.cost + second.cost } ); 1593 1591 } 1594 1592 } … … 1617 1615 mergeOpenVars( openVars, second.openVars ); 1618 1616 mergeOpenVars( openVars, third.openVars ); 1619 AssertionSet needAssertions( first.need.begin(), first.need.end() ); 1620 needAssertions.insert( second.need.begin(), second.need.end() ); 1621 needAssertions.insert( third.need.begin(), third.need.end() ); 1622 AssertionSet haveAssertions; 1617 AssertionSet need; 1618 cloneAll( first.need, need ); 1619 cloneAll( second.need, need ); 1620 cloneAll( third.need, need ); 1621 AssertionSet have; 1623 1622 1624 1623 // unify true and false types, then infer parameters to produce new alternatives 1625 1624 Type* commonType = nullptr; 1626 1625 if ( unify( second.expr->result, third.expr->result, compositeEnv, 1627 need Assertions, haveAssertions, openVars, indexer, commonType ) ) {1626 need, have, openVars, indexer, commonType ) ) { 1628 1627 ConditionalExpr *newExpr = new ConditionalExpr{ 1629 1628 first.expr->clone(), second.expr->clone(), third.expr->clone() }; … … 1637 1636 // output alternative 1638 1637 Alternative newAlt{ 1639 newExpr, compositeEnv, openVars,1640 AssertionList( need Assertions.begin(), needAssertions.end() ), cost };1641 inferParameters( need Assertions, haveAssertions, newAlt, openVars, back_inserter( alternatives ) );1638 newExpr, std::move(compositeEnv), std::move(openVars), 1639 AssertionList( need.begin(), need.end() ), cost }; 1640 inferParameters( need, have, newAlt, openVars, back_inserter( alternatives ) ); 1642 1641 } // if 1643 1642 } // for … … 1672 1671 OpenVarSet openVars{ first.openVars }; 1673 1672 mergeOpenVars( openVars, second.openVars ); 1674 AssertionSet needAssertions( first.need.begin(), first.need.end() ); 1675 needAssertions.insert( second.need.begin(), second.need.end() ); 1676 AssertionSet haveAssertions; 1673 AssertionSet need; 1674 cloneAll( first.need, need ); 1675 cloneAll( second.need, need ); 1676 AssertionSet have; 1677 1677 1678 1678 Type* commonType = nullptr; 1679 if ( unify( first.expr->result, second.expr->result, compositeEnv, need Assertions,1680 haveAssertions,openVars, indexer, commonType ) ) {1679 if ( unify( first.expr->result, second.expr->result, compositeEnv, need, have, 1680 openVars, indexer, commonType ) ) { 1681 1681 RangeExpr * newExpr = 1682 1682 new RangeExpr{ first.expr->clone(), second.expr->clone() }; 1683 1683 newExpr->result = commonType ? commonType : first.expr->result->clone(); 1684 1684 Alternative newAlt{ 1685 newExpr, compositeEnv, openVars, 1686 AssertionList( needAssertions.begin(), needAssertions.end() ), 1687 first.cost + second.cost }; 1688 inferParameters( needAssertions, haveAssertions, newAlt, openVars, back_inserter( alternatives ) ); 1685 newExpr, std::move(compositeEnv), std::move(openVars), 1686 AssertionList( need.begin(), need.end() ), first.cost + second.cost }; 1687 inferParameters( need, have, newAlt, openVars, back_inserter( alternatives ) ); 1689 1688 } // if 1690 1689 } // for … … 1709 1708 compositeEnv.simpleCombine( alt.env ); 1710 1709 mergeOpenVars( openVars, alt.openVars ); 1711 need.insert( alt.need.begin(), alt.need.end());1710 cloneAll( alt.need, need ); 1712 1711 } 1713 1712 1714 1713 alternatives.push_back( Alternative{ 1715 new TupleExpr{ exprs }, compositeEnv, openVars,1714 new TupleExpr{ exprs }, std::move(compositeEnv), std::move(openVars), 1716 1715 AssertionList( need.begin(), need.end() ), sumCost( alts ) } ); 1717 1716 } // for … … 1781 1780 for ( Alternative & alt : finder.get_alternatives() ) { 1782 1781 TypeEnvironment newEnv( alt.env ); 1783 AssertionSet needAssertions( alt.need.begin(), alt.need.end() ); 1784 AssertionSet haveAssertions; 1782 AssertionSet need; 1783 cloneAll( alt.need, need ); 1784 AssertionSet have; 1785 1785 OpenVarSet openVars( alt.openVars ); 1786 1786 // xxx - find things in env that don't have a "representative type" and claim … … 1801 1801 1802 1802 // unification run for side-effects 1803 unify( toType, alt.expr->result, newEnv, needAssertions, haveAssertions, openVars, 1804 indexer ); 1803 unify( toType, alt.expr->result, newEnv, need, have, openVars, indexer ); 1805 1804 // xxx - do some inspecting on this line... why isn't result bound to initAlt.type? 1806 1805 … … 1812 1811 new InitExpr{ 1813 1812 restructureCast( alt.expr->clone(), toType, true ), initAlt.designation->clone() }, 1814 newEnv, openVars, 1815 AssertionList( needAssertions.begin(), needAssertions.end() ), 1816 alt.cost, thisCost }; 1817 inferParameters( needAssertions, haveAssertions, newAlt, openVars, back_inserter( candidates ) ); 1813 std::move(newEnv), std::move(openVars), 1814 AssertionList( need.begin(), need.end() ), alt.cost, thisCost }; 1815 inferParameters( need, have, newAlt, openVars, back_inserter( candidates ) ); 1818 1816 } 1819 1817 } -
src/ResolvExpr/ResolveAssertions.cc
rda48183 r2c187378 166 166 167 167 Expression* varExpr = match.cdata.combine( alt.cvtCost ); 168 varExpr->result = match.adjType; 168 delete varExpr->result; 169 varExpr->result = match.adjType->clone(); 169 170 170 171 // follow the current assertion's ID chain to find the correct set of inferred parameters … … 177 178 178 179 (*inferParams)[ decl->get_uniqueId() ] = ParamEntry{ 179 candidate->get_uniqueId(), match.adjType, decl->get_type() , varExpr };180 candidate->get_uniqueId(), match.adjType, decl->get_type()->clone(), varExpr }; 180 181 } 181 182 … … 215 216 matches.emplace_back( cdata, adjType, std::move(newEnv), std::move(have), 216 217 std::move(newNeed), std::move(newOpenVars) ); 218 } else { 219 delete adjType; 217 220 } 218 221 } -
src/Tuples/TupleAssignment.cc
rda48183 r2c187378 72 72 compositeEnv.simpleCombine( alt.env ); 73 73 ResolvExpr::mergeOpenVars( openVars, alt.openVars ); 74 need.insert( alt.need.begin(), alt.need.end());74 cloneAll( alt.need, need ); 75 75 } 76 76
Note: See TracChangeset
for help on using the changeset viewer.