Changeset 62194cb
- Timestamp:
- Nov 23, 2017, 4:24:48 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:
- 178e4ec
- Parents:
- a8b27c6
- Location:
- src
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
src/ResolvExpr/Alternative.h
ra8b27c6 r62194cb 37 37 void print( std::ostream &os, Indenter indent = {} ) const; 38 38 39 /// Returns the stored expression, but released from management of this Alternative 40 Expression* release_expr() { 41 Expression* tmp = expr; 42 expr = nullptr; 43 return tmp; 44 } 45 39 46 Cost cost; 40 47 Cost cvtCost; -
src/ResolvExpr/AlternativeFinder.cc
ra8b27c6 r62194cb 676 676 auto nextArg = results[i].nextArg; 677 677 678 // use remainderof exploded tuple if present678 // use next element of exploded tuple if present 679 679 if ( results[i].hasExpl() ) { 680 680 const ExplodedActual& expl = results[i].getExpl( args ); 681 const Alternative& actual = expl.alts[results[i].nextExpl];682 681 683 TypeEnvironment env = results[i].env;684 OpenVarSet openVars = results[i].openVars;685 686 env.addActual( actual.env, openVars );687 688 682 unsigned nextExpl = results[i].nextExpl + 1; 689 if ( nextExpl == expl. alts.size() ) {683 if ( nextExpl == expl.exprs.size() ) { 690 684 nextExpl = 0; 691 685 } 692 686 693 687 results.emplace_back( 694 i, actual.expr, move(env), copy(results[i].need), 695 copy(results[i].have), move(openVars), nextArg, nTuples, 696 Cost::zero, nextExpl, results[i].explAlt ); 688 i, expl.exprs[results[i].nextExpl].get(), copy(results[i].env), 689 copy(results[i].need), copy(results[i].have), 690 copy(results[i].openVars), nextArg, nTuples, Cost::zero, nextExpl, 691 results[i].explAlt ); 697 692 698 693 continue; … … 758 753 759 754 // skip empty tuple arguments by (near-)cloning parent into next gen 760 if ( expl. alts.empty() ) {755 if ( expl.exprs.empty() ) { 761 756 results.emplace_back( 762 757 results[i], move(env), copy(results[i].need), … … 768 763 // add new result 769 764 results.emplace_back( 770 i, expl. alts.front().expr, move(env), copy(results[i].need),765 i, expl.exprs.front().get(), move(env), copy(results[i].need), 771 766 copy(results[i].have), move(openVars), nextArg + 1, 772 nTuples, expl.cost, expl. alts.size() == 1 ? 0 : 1, j );767 nTuples, expl.cost, expl.exprs.size() == 1 ? 0 : 1, j ); 773 768 } 774 769 } … … 794 789 if ( results[i].hasExpl() ) { 795 790 const ExplodedActual& expl = results[i].getExpl( args ); 796 const Alternative& actual = expl.alts[results[i].nextExpl];791 Expression* expr = expl.exprs[results[i].nextExpl].get(); 797 792 798 793 TypeEnvironment env = results[i].env; … … 800 795 OpenVarSet openVars = results[i].openVars; 801 796 802 env.addActual( actual.env, openVars ); 803 Type* actualType = actual.expr->get_result(); 797 Type* actualType = expr->get_result(); 804 798 805 799 PRINT( … … 813 807 if ( unify( formalType, actualType, env, need, have, openVars, indexer ) ) { 814 808 unsigned nextExpl = results[i].nextExpl + 1; 815 if ( nextExpl == expl. alts.size() ) {809 if ( nextExpl == expl.exprs.size() ) { 816 810 nextExpl = 0; 817 811 } 818 812 819 813 results.emplace_back( 820 i, actual.expr, move(env), move(need), move(have), move(openVars),821 n extArg, nTuples, Cost::zero, nextExpl, results[i].explAlt );814 i, expr, move(env), move(need), move(have), move(openVars), nextArg, 815 nTuples, Cost::zero, nextExpl, results[i].explAlt ); 822 816 } 823 817 … … 857 851 858 852 // skip empty tuple arguments by (near-)cloning parent into next gen 859 if ( expl. alts.empty() ) {853 if ( expl.exprs.empty() ) { 860 854 results.emplace_back( 861 855 results[i], move(env), move(need), move(have), move(openVars), … … 866 860 867 861 // consider only first exploded actual 868 const Alternative& actual = expl.alts.front();869 Type* actualType = actual.expr->get_result()->clone();862 Expression* expr = expl.exprs.front().get(); 863 Type* actualType = expr->get_result()->clone(); 870 864 871 865 PRINT( … … 881 875 // add new result 882 876 results.emplace_back( 883 i, actual.expr, move(env), move(need), move(have), move(openVars),884 n extArg + 1, nTuples, expl.cost, expl.alts.size() == 1 ? 0 : 1, j );877 i, expr, move(env), move(need), move(have), move(openVars), nextArg + 1, 878 nTuples, expl.cost, expl.exprs.size() == 1 ? 0 : 1, j ); 885 879 } 886 880 } … … 962 956 if ( results[i].hasExpl() ) { 963 957 const ExplodedActual& expl = results[i].getExpl( args ); 964 const Alternative& actual = expl.alts[results[i].nextExpl];965 958 966 TypeEnvironment env = results[i].env;967 OpenVarSet openVars = results[i].openVars;968 969 env.addActual( actual.env, openVars );970 971 959 unsigned nextExpl = results[i].nextExpl + 1; 972 if ( nextExpl == expl. alts.size() ) {960 if ( nextExpl == expl.exprs.size() ) { 973 961 nextExpl = 0; 974 962 } 975 963 976 964 results.emplace_back( 977 i, actual.expr, move(env), copy(results[i].need), 978 copy(results[i].have), move(openVars), nextArg, 0, 979 Cost::zero, nextExpl, results[i].explAlt ); 965 i, expl.exprs[results[i].nextExpl].get(), copy(results[i].env), 966 copy(results[i].need), copy(results[i].have), 967 copy(results[i].openVars), nextArg, 0, Cost::zero, nextExpl, 968 results[i].explAlt ); 980 969 981 970 continue; … … 1000 989 1001 990 // skip empty tuple arguments by (near-)cloning parent into next gen 1002 if ( expl. alts.empty() ) {991 if ( expl.exprs.empty() ) { 1003 992 results.emplace_back( 1004 993 results[i], move(env), copy(results[i].need), … … 1010 999 // add new result 1011 1000 results.emplace_back( 1012 i, expl. alts.front().expr, move(env), copy(results[i].need),1001 i, expl.exprs.front().get(), move(env), copy(results[i].need), 1013 1002 copy(results[i].have), move(openVars), nextArg + 1, 0, 1014 expl.cost, expl. alts.size() == 1 ? 0 : 1, j );1003 expl.cost, expl.exprs.size() == 1 ? 0 : 1, j ); 1015 1004 } 1016 1005 } -
src/ResolvExpr/ExplodedActual.cc
ra8b27c6 r62194cb 20 20 namespace ResolvExpr { 21 21 ExplodedActual::ExplodedActual( const Alternative& actual, const SymTab::Indexer& indexer ) 22 : env(actual.env), cost(actual.cost), alts() {23 Tuples::explode( actual, indexer, back_inserter( alts ));22 : env(actual.env), cost(actual.cost), exprs() { 23 Tuples::explode( actual, indexer, *this ); 24 24 } 25 25 } -
src/ResolvExpr/ExplodedActual.h
ra8b27c6 r62194cb 16 16 #pragma once 17 17 18 #include <memory> 19 #include <vector> 20 18 21 #include "Alternative.h" // for Alternative, AltList 19 22 #include "Cost.h" // for Cost … … 26 29 TypeEnvironment env; 27 30 Cost cost; 28 AltList alts; // TODO flatten this down to vector<unique_ptr<Expression>>31 std::vector< std::unique_ptr<Expression> > exprs; 29 32 30 ExplodedActual() : env(), cost(Cost::zero), alts() {}33 ExplodedActual() : env(), cost(Cost::zero), exprs() {} 31 34 32 ExplodedActual( const Alternative& actual, const SymTab::Indexer& indexer ); 35 ExplodedActual( const Alternative& actual, const SymTab::Indexer& indexer ); 33 36 }; 34 37 } -
src/Tuples/Explode.h
ra8b27c6 r62194cb 16 16 #pragma once 17 17 18 #include <iterator> // for back_inserter, back_insert_iterator 18 #include <iterator> // for back_inserter, back_insert_iterator 19 #include <utility> // for forward 19 20 20 #include "ResolvExpr/Alternative.h" // for Alternative, AltList 21 #include "SynTree/Expression.h" // for Expression, UniqueExpr, AddressExpr 22 #include "SynTree/Type.h" // for TupleType, Type 23 #include "Tuples.h" // for maybeImpure 21 #include "ResolvExpr/Alternative.h" // for Alternative, AltList 22 #include "ResolvExpr/ExplodedActual.h" // for ExplodedActual 23 #include "SynTree/Expression.h" // for Expression, UniqueExpr, AddressExpr 24 #include "SynTree/Type.h" // for TupleType, Type 25 #include "Tuples.h" // for maybeImpure 24 26 25 27 namespace SymTab { … … 39 41 } 40 42 43 /// Append alternative to an OutputIterator of Alternatives 44 template<typename OutputIterator> 45 void append( OutputIterator out, Expression* expr, const ResolvExpr::TypeEnvironment& env, 46 const ResolvExpr::Cost& cost, const ResolvExpr::Cost& cvtCost ) { 47 *out++ = ResolvExpr::Alternative{ expr, env, cost, cvtCost }; 48 } 49 50 /// Append alternative to an ExplodedActual 51 static inline void append( ResolvExpr::ExplodedActual& ea, Expression* expr, 52 const ResolvExpr::TypeEnvironment&, const ResolvExpr::Cost&, const ResolvExpr::Cost& ) { 53 ea.exprs.emplace_back( expr ); 54 /// xxx -- merge environment, cost? 55 } 56 41 57 /// helper function used by explode 42 template< typename OutputIterator > 43 void explodeUnique( Expression * expr, const ResolvExpr::Alternative & alt, const SymTab::Indexer & indexer, OutputIterator out, bool isTupleAssign ) { 58 template< typename Output > 59 void explodeUnique( Expression * expr, const ResolvExpr::Alternative & alt, 60 const SymTab::Indexer & indexer, Output&& out, bool isTupleAssign ) { 44 61 if ( isTupleAssign ) { 45 62 // tuple assignment needs CastExprs to be recursively exploded to easily get at all of the components 46 63 if ( CastExpr * castExpr = isReferenceCast( expr ) ) { 47 64 ResolvExpr::AltList alts; 48 explodeUnique( castExpr->get_arg(), alt, indexer, back_inserter( alts ), isTupleAssign ); 65 explodeUnique( 66 castExpr->get_arg(), alt, indexer, back_inserter( alts ), isTupleAssign ); 49 67 for ( ResolvExpr::Alternative & alt : alts ) { 50 68 // distribute reference cast over all components 51 a lt.expr = distributeReference( alt.expr );52 *out++ = alt;69 append( std::forward<Output>(out), distributeReference( alt.release_expr() ), 70 alt.env, alt.cost, alt.cvtCost ); 53 71 } 54 72 // in tuple assignment, still need to handle the other cases, but only if not already handled here (don't want to output too many alternatives) … … 61 79 // can open tuple expr and dump its exploded components 62 80 for ( Expression * expr : tupleExpr->get_exprs() ) { 63 explodeUnique( expr, alt, indexer, out, isTupleAssign );81 explodeUnique( expr, alt, indexer, std::forward<Output>(out), isTupleAssign ); 64 82 } 65 83 } else { … … 77 95 for ( unsigned int i = 0; i < tupleType->size(); i++ ) { 78 96 TupleIndexExpr * idx = new TupleIndexExpr( arg->clone(), i ); 79 explodeUnique( idx, alt, indexer, out, isTupleAssign );97 explodeUnique( idx, alt, indexer, std::forward<Output>(out), isTupleAssign ); 80 98 delete idx; 81 99 } … … 84 102 } else { 85 103 // atomic (non-tuple) type - output a clone of the expression in a new alternative 86 *out++ = ResolvExpr::Alternative(expr->clone(), alt.env, alt.cost, alt.cvtCost );104 append( std::forward<Output>(out), expr->clone(), alt.env, alt.cost, alt.cvtCost ); 87 105 } 88 106 } 89 107 90 108 /// expands a tuple-valued alternative into multiple alternatives, each with a non-tuple-type 91 template< typename OutputIterator > 92 void explode( const ResolvExpr::Alternative &alt, const SymTab::Indexer & indexer, OutputIterator out, bool isTupleAssign = false ) { 93 explodeUnique( alt.expr, alt, indexer, out, isTupleAssign ); 109 template< typename Output > 110 void explode( const ResolvExpr::Alternative &alt, const SymTab::Indexer & indexer, 111 Output&& out, bool isTupleAssign = false ) { 112 explodeUnique( alt.expr, alt, indexer, std::forward<Output>(out), isTupleAssign ); 94 113 } 95 114 96 115 // explode list of alternatives 97 template< typename AltIterator, typename OutputIterator > 98 void explode( AltIterator altBegin, AltIterator altEnd, const SymTab::Indexer & indexer, OutputIterator out, bool isTupleAssign = false ) { 116 template< typename AltIterator, typename Output > 117 void explode( AltIterator altBegin, AltIterator altEnd, const SymTab::Indexer & indexer, 118 Output&& out, bool isTupleAssign = false ) { 99 119 for ( ; altBegin != altEnd; ++altBegin ) { 100 explode( *altBegin, indexer, out, isTupleAssign );120 explode( *altBegin, indexer, std::forward<Output>(out), isTupleAssign ); 101 121 } 102 122 } 103 123 104 template< typename OutputIterator > 105 void explode( const ResolvExpr::AltList & alts, const SymTab::Indexer & indexer, OutputIterator out, bool isTupleAssign = false ) { 106 explode( alts.begin(), alts.end(), indexer, out, isTupleAssign ); 124 template< typename Output > 125 void explode( const ResolvExpr::AltList & alts, const SymTab::Indexer & indexer, Output&& out, 126 bool isTupleAssign = false ) { 127 explode( alts.begin(), alts.end(), indexer, std::forward<Output>(out), isTupleAssign ); 107 128 } 108 129 } // namespace Tuples
Note: See TracChangeset
for help on using the changeset viewer.