Changeset 1a59641
- Timestamp:
- Jul 27, 2018, 2:41:34 PM (5 years ago)
- Branches:
- new-env
- Children:
- 6fa409e
- Parents:
- 5a3e1f1
- Location:
- src/ResolvExpr
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
src/ResolvExpr/AlternativeFinder.cc
r5a3e1f1 r1a59641 687 687 /// as appropriate. 688 688 void bindAssertion( const DeclarationWithType* curDecl, const AssertionSetValue& assnInfo, 689 AssertionResnState& resn, AssertionPack&& match ) { 690 addToIndexer( match.have, resn.indexer ); 691 resn.newNeed.insert( match.need.begin(), match.need.end() ); 692 resn.openVars = std::move(match.openVars); 693 resn.alt.env = std::move(match.env); 689 Alternative& alt, AssertionPack& match ) { 694 690 695 691 DeclarationWithType* candidate = match.cdata.id; … … 702 698 } 703 699 704 Expression* varExpr = match.cdata.combine( resn.alt.cvtCost );700 Expression* varExpr = match.cdata.combine( alt.cvtCost ); 705 701 varExpr->result = match.adjType; 706 702 … … 708 704 // parameters to add the candidate o (i.e. the set of inferred parameters belonging 709 705 // to the entity which requested the assertion parameter) 710 InferredParams* inferParams = & resn.alt.expr->inferParams;706 InferredParams* inferParams = &alt.expr->inferParams; 711 707 for ( UniqueId id : assnInfo.idChain ) { 712 708 inferParams = (*inferParams)[ id ].inferParams.get(); … … 759 755 760 756 // otherwise bind current match in ongoing scope 761 bindAssertion( curDecl, assnInfo, resn, std::move(matches.front()) ); 757 AssertionPack& match = matches.front(); 758 addToIndexer( match.have, resn.indexer ); 759 resn.newNeed.insert( match.need.begin(), match.need.end() ); 760 resn.openVars = std::move(match.openVars); 761 resn.alt.env = std::move(match.env); 762 763 bindAssertion( curDecl, assnInfo, resn.alt, match ); 762 764 return true; 763 765 } … … 771 773 /// Stack of environments, to support backtracking 772 774 std::vector<TypeEnvironment> envs; 775 /// Open variables for environment combination 776 std::vector<OpenVarSet> varSets; 773 777 /// Indexer to use for environment merges 774 778 const SymTab::Indexer& indexer; 775 779 public: 776 /// Outputs a pair consisting of the merged environment and the list of interpretations 777 using OutType = std::pair<TypeEnvironment, std::vector<DeferElement>>; 778 779 InterpretationEnvMerger( const TypeEnvironment& env, const SymTab::Indexer& indexer ) 780 : crnt{}, envs{}, indexer{indexer} { envs.push_back( env ); } 780 /// The merged environment/open variables and the list of interpretations 781 struct OutType { 782 TypeEnvironment env; 783 OpenVarSet openVars; 784 std::vector<DeferElement> assns; 785 786 OutType( const TypeEnvironment& env, const OpenVarSet& openVars, 787 const std::vector<DeferElement>& assns ) 788 : env(env), openVars(openVars), assns(assns) {} 789 }; 790 791 InterpretationEnvMerger( const TypeEnvironment& env, const OpenVarSet& openVars, 792 const SymTab::Indexer& indexer ) : crnt{}, envs{}, varSets{}, indexer{indexer} { 793 envs.push_back( env ); 794 varSets.push_back( openVars ); 795 } 781 796 782 797 ComboResult append( DeferElement i ) { 783 798 TypeEnvironment env = envs.back(); 784 if ( ! env.combine( i.match.env, indexer ) ) return ComboResult::REJECT_THIS; 799 OpenVarSet openVars = varSets.back(); 800 mergeOpenVars( openVars, i.match.openVars ); 801 802 if ( ! env.combine( i.match.env, openVars, indexer ) ) 803 return ComboResult::REJECT_THIS; 804 785 805 crnt.push_back( i ); 786 806 envs.push_back( env ); 807 varSets.push_back( openVars ); 787 808 return ComboResult::ACCEPT; 788 809 } … … 791 812 crnt.pop_back(); 792 813 envs.pop_back(); 793 } 794 795 OutType finalize() { return { envs.back(), crnt }; } 814 varSets.pop_back(); 815 } 816 817 OutType finalize() { return { envs.back(), varSets.back(), crnt }; } 796 818 }; 797 819 } … … 833 855 834 856 std::vector<InterpretationEnvMerger::OutType> compatible = filterCombos( 835 resn.deferred, InterpretationEnvMerger{ resn.alt.env, resn.indexer }); 857 resn.deferred, 858 InterpretationEnvMerger{ resn.alt.env, resn.openVars, resn.indexer } ); 836 859 837 860 for ( auto& compat : compatible ) { … … 839 862 840 863 // add compatible assertions to new resolution state 841 for ( DeferElement el : compat.second ) { 842 bindAssertion( 843 el.curDecl, el.assnInfo, new_resn, AssertionPack{el.match} ); 864 for ( DeferElement el : compat.assns ) { 865 AssertionPack match = el.match; 866 addToIndexer( match.have, new_resn.indexer ); 867 resn.newNeed.insert( match.need.begin(), match.need.end() ); 868 869 bindAssertion( el.curDecl, el.assnInfo, new_resn.alt, match ); 844 870 } 845 871 846 872 // set mutual environment into resolution state 847 new_resn.alt.env = std::move(compat.first); 873 new_resn.alt.env = std::move(compat.env); 874 new_resn.openVars = std::move(compat.openVars); 848 875 849 876 // add successful match or push back next state -
src/ResolvExpr/TypeEnvironment.cc
r5a3e1f1 r1a59641 36 36 37 37 namespace ResolvExpr { 38 #if 038 #ifdef EXPENSIVE_ENV_VALIDATION 39 39 #define PRE_POST_VALIDATE auto dbg = ValidateGuard{this, __func__}; 40 40 #define PRE_POST_VALIDATE_NOM auto dbg = ValidateGuard{this}; … … 411 411 } 412 412 413 bool TypeEnvironment::combine( const TypeEnvironment& o, const SymTab::Indexer& indexer ) { 413 bool TypeEnvironment::combine( const TypeEnvironment& o, OpenVarSet& openVars, 414 const SymTab::Indexer& indexer ) { 414 415 // short-circuit for empty cases 415 416 if ( o.isEmpty() ) return true; … … 657 658 Type* common = nullptr; 658 659 AssertionSet need, have; 659 OpenVarSet openVars;660 660 if ( unifyInexact( ebound.type->clone(), nbound.type->clone(), *this, need, have, 661 661 openVars, WidenMode{ ebound.allowWidening, nbound.allowWidening }, -
src/ResolvExpr/TypeEnvironment.h
r5a3e1f1 r1a59641 78 78 typedef std::map< std::string, TypeDecl::Data > OpenVarSet; 79 79 80 /// merges one set of open vars into another 81 static inline void mergeOpenVars( OpenVarSet& dst, const OpenVarSet& src ) { 82 for ( const auto& entry : src ) { dst[ entry.first ] = entry.second; } 83 } 84 80 85 void printAssertionSet( const AssertionSet &, std::ostream &, int indent = 0 ); 81 86 void printOpenVarSet( const OpenVarSet &, std::ostream &, int indent = 0 ); … … 138 143 }; 139 144 145 //#define EXPENSIVE_ENV_VALIDATION 146 #ifdef EXPENSIVE_ENV_VALIDATION 140 147 class ValidateGuard; 148 #endif 141 149 142 150 class TypeEnvironment { … … 156 164 Bindings* bindings; 157 165 158 // for debugging 166 #ifdef EXPENSIVE_ENV_VALIDATION 167 /// for debugging 159 168 friend ValidateGuard; 160 169 const char* last_fn = "<none>"; 170 #endif 161 171 162 172 /// Merges the classes rooted at root1 and root2, returning a pair containing the root and … … 225 235 /// from the same initial environment. 226 236 /// Returns false if unsuccessful, but does NOT roll back partial changes 227 bool combine( const TypeEnvironment& o, const SymTab::Indexer& indexer ); 237 bool combine( const TypeEnvironment& o, OpenVarSet& openVars, 238 const SymTab::Indexer& indexer ); 228 239 229 240 void extractOpenVars( OpenVarSet &openVars ) const;
Note: See TracChangeset
for help on using the changeset viewer.