[6d6e829] | 1 | // |
---|
| 2 | // Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo |
---|
| 3 | // |
---|
| 4 | // The contents of this file are covered under the licence agreement in the |
---|
| 5 | // file "LICENCE" distributed with Cforall. |
---|
| 6 | // |
---|
| 7 | // ResolveAssertions.cc -- |
---|
| 8 | // |
---|
| 9 | // Author : Aaron B. Moss |
---|
| 10 | // Created On : Fri Oct 05 13:46:00 2018 |
---|
| 11 | // Last Modified By : Aaron B. Moss |
---|
| 12 | // Last Modified On : Fri Oct 05 13:46:00 2018 |
---|
| 13 | // Update Count : 1 |
---|
| 14 | // |
---|
| 15 | |
---|
| 16 | #include "ResolveAssertions.h" |
---|
| 17 | |
---|
[b3bd772] | 18 | #include <algorithm> // for sort |
---|
[83ab931] | 19 | #include <cassert> // for assertf |
---|
| 20 | #include <list> // for list |
---|
[b3bd772] | 21 | #include <memory> // for unique_ptr |
---|
[4d2d45f9] | 22 | #include <sstream> // for ostringstream |
---|
| 23 | #include <string> // for string |
---|
[83ab931] | 24 | #include <unordered_map> // for unordered_map, unordered_multimap |
---|
| 25 | #include <utility> // for move |
---|
| 26 | #include <vector> // for vector |
---|
| 27 | |
---|
| 28 | #include "Alternative.h" // for Alternative, AssertionItem, AssertionList |
---|
| 29 | #include "Common/FilterCombos.h" // for filterCombos |
---|
[4d2d45f9] | 30 | #include "Common/Indenter.h" // for Indenter |
---|
[83ab931] | 31 | #include "Common/utility.h" // for sort_mins |
---|
[d76c588] | 32 | #include "GenPoly/GenPoly.h" // for getFunctionType |
---|
[83ab931] | 33 | #include "ResolvExpr/RenameVars.h" // for renameTyVars |
---|
| 34 | #include "SymTab/Indexer.h" // for Indexer |
---|
[b3bd772] | 35 | #include "SymTab/Mangler.h" // for Mangler |
---|
[83ab931] | 36 | #include "SynTree/Expression.h" // for InferredParams |
---|
| 37 | #include "TypeEnvironment.h" // for TypeEnvironment, etc. |
---|
[052cd71] | 38 | #include "typeops.h" // for adjustExprType, specCost |
---|
[83ab931] | 39 | #include "Unify.h" // for unify |
---|
[6d6e829] | 40 | |
---|
| 41 | namespace ResolvExpr { |
---|
| 42 | /// Unified assertion candidate |
---|
| 43 | struct AssnCandidate { |
---|
| 44 | SymTab::Indexer::IdData cdata; ///< Satisfying declaration |
---|
| 45 | Type* adjType; ///< Satisfying type |
---|
| 46 | TypeEnvironment env; ///< Post-unification environment |
---|
| 47 | AssertionSet have; ///< Post-unification have-set |
---|
| 48 | AssertionSet need; ///< Post-unification need-set |
---|
| 49 | OpenVarSet openVars; ///< Post-unification open-var set |
---|
[0b00df0] | 50 | UniqueId resnSlot; ///< Slot for any recursive assertion IDs |
---|
[6d6e829] | 51 | |
---|
| 52 | AssnCandidate( const SymTab::Indexer::IdData& cdata, Type* adjType, TypeEnvironment&& env, |
---|
[0b00df0] | 53 | AssertionSet&& have, AssertionSet&& need, OpenVarSet&& openVars, UniqueId resnSlot ) |
---|
[6d6e829] | 54 | : cdata(cdata), adjType(adjType), env(std::move(env)), have(std::move(have)), |
---|
[0b00df0] | 55 | need(std::move(need)), openVars(std::move(openVars)), resnSlot(resnSlot) {} |
---|
[6d6e829] | 56 | }; |
---|
| 57 | |
---|
| 58 | /// List of candidate assertion resolutions |
---|
| 59 | using CandidateList = std::vector<AssnCandidate>; |
---|
| 60 | |
---|
| 61 | /// Reference to single deferred item |
---|
| 62 | struct DeferRef { |
---|
[052cd71] | 63 | const DeclarationWithType* decl; |
---|
| 64 | const AssertionSetValue& info; |
---|
[6d6e829] | 65 | const AssnCandidate& match; |
---|
| 66 | }; |
---|
| 67 | |
---|
| 68 | /// Wrapper for the deferred items from a single assertion resolution. |
---|
| 69 | /// Acts like indexed list of DeferRef |
---|
| 70 | struct DeferItem { |
---|
[052cd71] | 71 | const DeclarationWithType* decl; |
---|
| 72 | const AssertionSetValue& info; |
---|
| 73 | CandidateList matches; |
---|
[6d6e829] | 74 | |
---|
[052cd71] | 75 | DeferItem( DeclarationWithType* decl, const AssertionSetValue& info, CandidateList&& matches ) |
---|
| 76 | : decl(decl), info(info), matches(std::move(matches)) {} |
---|
[6d6e829] | 77 | |
---|
[052cd71] | 78 | bool empty() const { return matches.empty(); } |
---|
[6d6e829] | 79 | |
---|
[052cd71] | 80 | CandidateList::size_type size() const { return matches.size(); } |
---|
[4d2d45f9] | 81 | |
---|
[052cd71] | 82 | DeferRef operator[] ( unsigned i ) const { return { decl, info, matches[i] }; } |
---|
[6d6e829] | 83 | }; |
---|
| 84 | |
---|
| 85 | /// List of deferred resolution items |
---|
| 86 | using DeferList = std::vector<DeferItem>; |
---|
| 87 | |
---|
| 88 | /// Combo iterator that combines candidates into an output list, merging their environments. |
---|
| 89 | /// Rejects an appended candidate if the environments cannot be merged. |
---|
| 90 | class CandidateEnvMerger { |
---|
| 91 | /// Current list of merged candidates |
---|
| 92 | std::vector<DeferRef> crnt; |
---|
| 93 | /// Stack of environments to support backtracking |
---|
| 94 | std::vector<TypeEnvironment> envs; |
---|
| 95 | /// Stack of open variables to support backtracking |
---|
| 96 | std::vector<OpenVarSet> varSets; |
---|
| 97 | /// Indexer to use for merges |
---|
| 98 | const SymTab::Indexer& indexer; |
---|
| 99 | |
---|
| 100 | public: |
---|
| 101 | /// The merged environment/open variables and the list of candidates |
---|
| 102 | struct OutType { |
---|
| 103 | TypeEnvironment env; |
---|
| 104 | OpenVarSet openVars; |
---|
| 105 | std::vector<DeferRef> assns; |
---|
[c519942] | 106 | Cost cost; |
---|
[6d6e829] | 107 | |
---|
| 108 | OutType( const TypeEnvironment& env, const OpenVarSet& openVars, |
---|
| 109 | const std::vector<DeferRef>& assns ) |
---|
[c519942] | 110 | : env(env), openVars(openVars), assns(assns), cost(Cost::infinity) {} |
---|
[6d6e829] | 111 | }; |
---|
| 112 | |
---|
| 113 | CandidateEnvMerger( const TypeEnvironment& env, const OpenVarSet& openVars, |
---|
| 114 | const SymTab::Indexer& indexer ) |
---|
| 115 | : crnt(), envs{ env }, varSets{ openVars }, indexer(indexer) {} |
---|
| 116 | |
---|
| 117 | bool append( DeferRef i ) { |
---|
| 118 | TypeEnvironment env = envs.back(); |
---|
| 119 | OpenVarSet openVars = varSets.back(); |
---|
| 120 | mergeOpenVars( openVars, i.match.openVars ); |
---|
| 121 | |
---|
| 122 | if ( ! env.combine( i.match.env, openVars, indexer ) ) return false; |
---|
| 123 | |
---|
| 124 | crnt.emplace_back( i ); |
---|
| 125 | envs.emplace_back( env ); |
---|
| 126 | varSets.emplace_back( openVars ); |
---|
| 127 | return true; |
---|
| 128 | } |
---|
| 129 | |
---|
| 130 | void backtrack() { |
---|
| 131 | crnt.pop_back(); |
---|
| 132 | envs.pop_back(); |
---|
| 133 | varSets.pop_back(); |
---|
| 134 | } |
---|
| 135 | |
---|
| 136 | OutType finalize() { return { envs.back(), varSets.back(), crnt }; } |
---|
| 137 | }; |
---|
| 138 | |
---|
[fbecee5] | 139 | /// Comparator for CandidateEnvMerger outputs that sums their costs and caches the stored |
---|
| 140 | /// sums |
---|
| 141 | struct CandidateCost { |
---|
| 142 | using Element = CandidateEnvMerger::OutType; |
---|
| 143 | private: |
---|
| 144 | const SymTab::Indexer& indexer; ///< Indexer for costing |
---|
| 145 | |
---|
| 146 | public: |
---|
| 147 | CandidateCost( const SymTab::Indexer& indexer ) : indexer(indexer) {} |
---|
| 148 | |
---|
| 149 | /// reports the cost of an element |
---|
[c519942] | 150 | Cost get( Element& x ) const { |
---|
| 151 | // check cached cost |
---|
| 152 | if ( x.cost != Cost::infinity ) return x.cost; |
---|
| 153 | |
---|
| 154 | // generate cost |
---|
| 155 | Cost k = Cost::zero; |
---|
| 156 | for ( const auto& assn : x.assns ) { |
---|
[d76c588] | 157 | // compute conversion cost from satisfying decl to assertion |
---|
[c519942] | 158 | k += computeConversionCost( |
---|
| 159 | assn.match.adjType, assn.decl->get_type(), indexer, x.env ); |
---|
| 160 | |
---|
| 161 | // mark vars+specialization cost on function-type assertions |
---|
[d76c588] | 162 | FunctionType* func = GenPoly::getFunctionType( assn.match.cdata.id->get_type() ); |
---|
[c519942] | 163 | if ( ! func ) continue; |
---|
| 164 | |
---|
| 165 | for ( DeclarationWithType* formal : func->parameters ) { |
---|
| 166 | k.decSpec( specCost( formal->get_type() ) ); |
---|
| 167 | } |
---|
| 168 | k.incVar( func->forall.size() ); |
---|
| 169 | for ( TypeDecl* td : func->forall ) { |
---|
| 170 | k.decSpec( td->assertions.size() ); |
---|
[fbecee5] | 171 | } |
---|
| 172 | } |
---|
[c519942] | 173 | |
---|
| 174 | // cache and return |
---|
| 175 | x.cost = k; |
---|
| 176 | return k; |
---|
[fbecee5] | 177 | } |
---|
| 178 | |
---|
| 179 | /// compares elements by cost |
---|
[c519942] | 180 | bool operator() ( Element& a, Element& b ) const { |
---|
[fbecee5] | 181 | return get( a ) < get( b ); |
---|
| 182 | } |
---|
| 183 | }; |
---|
| 184 | |
---|
[0b00df0] | 185 | /// Set of assertion resolutions, grouped by resolution ID |
---|
| 186 | using InferCache = std::unordered_map< UniqueId, InferredParams >; |
---|
| 187 | |
---|
[6d6e829] | 188 | /// Flag for state iteration |
---|
| 189 | enum IterateFlag { IterateState }; |
---|
| 190 | |
---|
| 191 | /// State needed to resolve a set of assertions |
---|
| 192 | struct ResnState { |
---|
[0b00df0] | 193 | Alternative alt; ///< Alternative assertion is rooted on |
---|
| 194 | AssertionList need; ///< Assertions to find |
---|
| 195 | AssertionSet newNeed; ///< New assertions for current resolutions |
---|
| 196 | DeferList deferred; ///< Deferred matches |
---|
| 197 | InferCache inferred; ///< Cache of already-inferred parameters |
---|
| 198 | SymTab::Indexer& indexer; ///< Name lookup (depends on previous assertions) |
---|
[6d6e829] | 199 | |
---|
| 200 | /// Initial resolution state for an alternative |
---|
| 201 | ResnState( Alternative& a, SymTab::Indexer& indexer ) |
---|
[0b00df0] | 202 | : alt(a), need(), newNeed(), deferred(), inferred(), indexer(indexer) { |
---|
[6d6e829] | 203 | need.swap( a.need ); |
---|
| 204 | } |
---|
| 205 | |
---|
| 206 | /// Updated resolution state with new need-list |
---|
| 207 | ResnState( ResnState&& o, IterateFlag ) |
---|
| 208 | : alt(std::move(o.alt)), need(o.newNeed.begin(), o.newNeed.end()), newNeed(), deferred(), |
---|
[0b00df0] | 209 | inferred(std::move(o.inferred)), indexer(o.indexer) {} |
---|
[6d6e829] | 210 | }; |
---|
| 211 | |
---|
| 212 | /// Binds a single assertion, updating resolution state |
---|
| 213 | void bindAssertion( const DeclarationWithType* decl, AssertionSetValue info, Alternative& alt, |
---|
[0b00df0] | 214 | AssnCandidate& match, InferCache& inferred ) { |
---|
[6d6e829] | 215 | |
---|
| 216 | DeclarationWithType* candidate = match.cdata.id; |
---|
| 217 | assertf( candidate->get_uniqueId(), "Assertion candidate does not have a unique ID: %s", toString( candidate ).c_str() ); |
---|
| 218 | |
---|
| 219 | Expression* varExpr = match.cdata.combine( alt.cvtCost ); |
---|
[2c187378] | 220 | delete varExpr->result; |
---|
| 221 | varExpr->result = match.adjType->clone(); |
---|
[0b00df0] | 222 | if ( match.resnSlot ) { varExpr->resnSlots.push_back( match.resnSlot ); } |
---|
[6d6e829] | 223 | |
---|
[0b00df0] | 224 | // place newly-inferred assertion in proper place in cache |
---|
| 225 | inferred[ info.resnSlot ][ decl->get_uniqueId() ] = ParamEntry{ |
---|
[9aaacc27] | 226 | candidate->get_uniqueId(), match.adjType->clone(), decl->get_type()->clone(), |
---|
| 227 | varExpr }; |
---|
[6d6e829] | 228 | } |
---|
| 229 | |
---|
| 230 | /// Adds a captured assertion to the symbol table |
---|
| 231 | void addToIndexer( AssertionSet &assertSet, SymTab::Indexer &indexer ) { |
---|
[e1f7eef] | 232 | for ( auto& i : assertSet ) { |
---|
| 233 | if ( i.second.isUsed ) { |
---|
| 234 | indexer.addId( i.first ); |
---|
[6d6e829] | 235 | } |
---|
| 236 | } |
---|
| 237 | } |
---|
| 238 | |
---|
[0b00df0] | 239 | // in AlternativeFinder.cc; unique ID for assertion resolutions |
---|
| 240 | extern UniqueId globalResnSlot; |
---|
| 241 | |
---|
[6d6e829] | 242 | /// Resolve a single assertion, in context |
---|
[052cd71] | 243 | bool resolveAssertion( AssertionItem& assn, ResnState& resn ) { |
---|
[6d6e829] | 244 | // skip unused assertions |
---|
| 245 | if ( ! assn.info.isUsed ) return true; |
---|
| 246 | |
---|
[052cd71] | 247 | // lookup candidates for this assertion |
---|
| 248 | std::list< SymTab::Indexer::IdData > candidates; |
---|
| 249 | resn.indexer.lookupId( assn.decl->name, candidates ); |
---|
| 250 | |
---|
| 251 | // find the candidates that unify with the desired type |
---|
| 252 | CandidateList matches; |
---|
| 253 | for ( const auto& cdata : candidates ) { |
---|
| 254 | DeclarationWithType* candidate = cdata.id; |
---|
| 255 | |
---|
| 256 | // build independent unification context for candidate |
---|
| 257 | AssertionSet have, newNeed; |
---|
| 258 | TypeEnvironment newEnv{ resn.alt.env }; |
---|
| 259 | OpenVarSet newOpenVars{ resn.alt.openVars }; |
---|
| 260 | Type* adjType = candidate->get_type()->clone(); |
---|
| 261 | adjustExprType( adjType, newEnv, resn.indexer ); |
---|
| 262 | renameTyVars( adjType ); |
---|
| 263 | |
---|
| 264 | // keep unifying candidates |
---|
| 265 | if ( unify( assn.decl->get_type(), adjType, newEnv, newNeed, have, newOpenVars, |
---|
| 266 | resn.indexer ) ) { |
---|
| 267 | // set up binding slot for recursive assertions |
---|
| 268 | UniqueId crntResnSlot = 0; |
---|
| 269 | if ( ! newNeed.empty() ) { |
---|
| 270 | crntResnSlot = ++globalResnSlot; |
---|
| 271 | for ( auto& a : newNeed ) { |
---|
| 272 | a.second.resnSlot = crntResnSlot; |
---|
[0b00df0] | 273 | } |
---|
[2fd9f24] | 274 | } |
---|
[e1f7eef] | 275 | |
---|
[052cd71] | 276 | matches.emplace_back( cdata, adjType, std::move(newEnv), std::move(have), |
---|
| 277 | std::move(newNeed), std::move(newOpenVars), crntResnSlot ); |
---|
| 278 | } else { |
---|
| 279 | delete adjType; |
---|
| 280 | } |
---|
[6d6e829] | 281 | } |
---|
| 282 | |
---|
| 283 | // break if no suitable assertion |
---|
| 284 | if ( matches.empty() ) return false; |
---|
| 285 | |
---|
| 286 | // defer if too many suitable assertions |
---|
| 287 | if ( matches.size() > 1 ) { |
---|
[052cd71] | 288 | resn.deferred.emplace_back( assn.decl, assn.info, std::move(matches) ); |
---|
[6d6e829] | 289 | return true; |
---|
| 290 | } |
---|
| 291 | |
---|
| 292 | // otherwise bind current match in ongoing scope |
---|
| 293 | AssnCandidate& match = matches.front(); |
---|
| 294 | addToIndexer( match.have, resn.indexer ); |
---|
| 295 | resn.newNeed.insert( match.need.begin(), match.need.end() ); |
---|
[052cd71] | 296 | resn.alt.env = std::move(match.env); |
---|
| 297 | resn.alt.openVars = std::move(match.openVars); |
---|
[6d6e829] | 298 | |
---|
[0b00df0] | 299 | bindAssertion( assn.decl, assn.info, resn.alt, match, resn.inferred ); |
---|
[6d6e829] | 300 | return true; |
---|
| 301 | } |
---|
| 302 | |
---|
[0b00df0] | 303 | /// Associates inferred parameters with an expression |
---|
| 304 | struct InferMatcher { |
---|
| 305 | InferCache& inferred; |
---|
| 306 | |
---|
| 307 | InferMatcher( InferCache& inferred ) : inferred( inferred ) {} |
---|
| 308 | |
---|
| 309 | Expression* postmutate( Expression* expr ) { |
---|
[40290497] | 310 | // defer missing inferred parameters until they are hopefully found later |
---|
| 311 | std::vector<UniqueId> missingSlots; |
---|
[0b00df0] | 312 | // place inferred parameters into resolution slots |
---|
| 313 | for ( UniqueId slot : expr->resnSlots ) { |
---|
| 314 | // fail if no matching parameters found |
---|
[40290497] | 315 | auto it = inferred.find( slot ); |
---|
| 316 | if ( it == inferred.end() ) { |
---|
| 317 | missingSlots.push_back( slot ); |
---|
| 318 | continue; |
---|
| 319 | } |
---|
| 320 | InferredParams& inferParams = it->second; |
---|
[0b00df0] | 321 | |
---|
| 322 | // place inferred parameters into proper place in expression |
---|
| 323 | for ( auto& entry : inferParams ) { |
---|
| 324 | // recurse on inferParams of resolved expressions |
---|
| 325 | entry.second.expr = postmutate( entry.second.expr ); |
---|
| 326 | // xxx - look at entry.second.inferParams? |
---|
| 327 | expr->inferParams[ entry.first ] = entry.second; |
---|
| 328 | } |
---|
| 329 | } |
---|
| 330 | |
---|
| 331 | // clear resolution slots and return |
---|
[40290497] | 332 | expr->resnSlots.swap( missingSlots ); |
---|
[0b00df0] | 333 | return expr; |
---|
| 334 | } |
---|
| 335 | }; |
---|
| 336 | |
---|
| 337 | void finalizeAssertions( Alternative& alt, InferCache& inferred, AltList& out ) { |
---|
| 338 | PassVisitor<InferMatcher> matcher{ inferred }; |
---|
| 339 | alt.expr = alt.expr->acceptMutator( matcher ); |
---|
| 340 | out.emplace_back( alt ); |
---|
| 341 | } |
---|
| 342 | |
---|
| 343 | /// Limit to depth of recursion of assertion satisfaction |
---|
[6d6e829] | 344 | static const int recursionLimit = /* 10 */ 4; |
---|
| 345 | |
---|
[4d2d45f9] | 346 | void resolveAssertions( Alternative& alt, const SymTab::Indexer& indexer, AltList& out, std::list<std::string>& errors ) { |
---|
[6d6e829] | 347 | // finish early if no assertions to resolve |
---|
| 348 | if ( alt.need.empty() ) { |
---|
| 349 | out.emplace_back( alt ); |
---|
| 350 | return; |
---|
| 351 | } |
---|
| 352 | |
---|
| 353 | // build list of possible resolutions |
---|
| 354 | using ResnList = std::vector<ResnState>; |
---|
| 355 | SymTab::Indexer root_indexer{ indexer }; |
---|
| 356 | ResnList resns{ ResnState{ alt, root_indexer } }; |
---|
| 357 | ResnList new_resns{}; |
---|
| 358 | |
---|
| 359 | // resolve assertions in breadth-first-order up to a limited number of levels deep |
---|
| 360 | for ( unsigned level = 0; level < recursionLimit; ++level ) { |
---|
| 361 | // scan over all mutually-compatible resolutions |
---|
| 362 | for ( auto& resn : resns ) { |
---|
| 363 | // make initial pass at matching assertions |
---|
| 364 | for ( auto& assn : resn.need ) { |
---|
| 365 | // fail early if any assertion is not resolvable |
---|
[052cd71] | 366 | if ( ! resolveAssertion( assn, resn ) ) { |
---|
[4d2d45f9] | 367 | Indenter tabs{ Indenter::tabsize, 3 }; |
---|
| 368 | std::ostringstream ss; |
---|
| 369 | ss << tabs << "Unsatisfiable alternative:\n"; |
---|
| 370 | resn.alt.print( ss, ++tabs ); |
---|
| 371 | ss << --tabs << "Could not satisfy assertion:\n"; |
---|
| 372 | assn.decl->print( ss, ++tabs ); |
---|
| 373 | |
---|
| 374 | errors.emplace_back( ss.str() ); |
---|
| 375 | goto nextResn; |
---|
| 376 | } |
---|
[6d6e829] | 377 | } |
---|
| 378 | |
---|
| 379 | if ( resn.deferred.empty() ) { |
---|
| 380 | // either add successful match or push back next state |
---|
| 381 | if ( resn.newNeed.empty() ) { |
---|
[0b00df0] | 382 | finalizeAssertions( resn.alt, resn.inferred, out ); |
---|
[6d6e829] | 383 | } else { |
---|
| 384 | new_resns.emplace_back( std::move(resn), IterateState ); |
---|
| 385 | } |
---|
| 386 | } else { |
---|
[b3bd772] | 387 | // resolve deferred assertions by mutual compatibility |
---|
[6d6e829] | 388 | std::vector<CandidateEnvMerger::OutType> compatible = filterCombos( |
---|
| 389 | resn.deferred, |
---|
| 390 | CandidateEnvMerger{ resn.alt.env, resn.alt.openVars, resn.indexer } ); |
---|
[4d2d45f9] | 391 | // fail early if no mutually-compatible assertion satisfaction |
---|
| 392 | if ( compatible.empty() ) { |
---|
| 393 | Indenter tabs{ Indenter::tabsize, 3 }; |
---|
| 394 | std::ostringstream ss; |
---|
| 395 | ss << tabs << "Unsatisfiable alternative:\n"; |
---|
| 396 | resn.alt.print( ss, ++tabs ); |
---|
| 397 | ss << --tabs << "No mutually-compatible satisfaction for assertions:\n"; |
---|
| 398 | ++tabs; |
---|
| 399 | for ( const auto& d : resn.deferred ) { |
---|
[052cd71] | 400 | d.decl->print( ss, tabs ); |
---|
[4d2d45f9] | 401 | } |
---|
| 402 | |
---|
| 403 | errors.emplace_back( ss.str() ); |
---|
| 404 | goto nextResn; |
---|
| 405 | } |
---|
[b3bd772] | 406 | // sort by cost |
---|
| 407 | CandidateCost coster{ resn.indexer }; |
---|
| 408 | std::sort( compatible.begin(), compatible.end(), coster ); |
---|
| 409 | |
---|
| 410 | // keep map of detected options |
---|
| 411 | std::unordered_map<std::string, Cost> found; |
---|
| 412 | for ( auto& compat : compatible ) { |
---|
| 413 | // filter by environment-adjusted result type, keep only cheapest option |
---|
| 414 | Type* resType = alt.expr->result->clone(); |
---|
| 415 | compat.env.apply( resType ); |
---|
| 416 | // skip if cheaper alternative already processed with same result type |
---|
| 417 | Cost resCost = coster.get( compat ); |
---|
| 418 | auto it = found.emplace( SymTab::Mangler::mangleType( resType ), resCost ); |
---|
[aeb8f70] | 419 | delete resType; |
---|
[b3bd772] | 420 | if ( it.second == false && it.first->second < resCost ) continue; |
---|
| 421 | |
---|
| 422 | // proceed with resolution step |
---|
[6d6e829] | 423 | ResnState new_resn = resn; |
---|
| 424 | |
---|
| 425 | // add compatible assertions to new resolution state |
---|
| 426 | for ( DeferRef r : compat.assns ) { |
---|
| 427 | AssnCandidate match = r.match; |
---|
| 428 | addToIndexer( match.have, new_resn.indexer ); |
---|
| 429 | new_resn.newNeed.insert( match.need.begin(), match.need.end() ); |
---|
| 430 | |
---|
[052cd71] | 431 | bindAssertion( r.decl, r.info, new_resn.alt, match, new_resn.inferred ); |
---|
[6d6e829] | 432 | } |
---|
| 433 | |
---|
| 434 | // set mutual environment into resolution state |
---|
| 435 | new_resn.alt.env = std::move(compat.env); |
---|
| 436 | new_resn.alt.openVars = std::move(compat.openVars); |
---|
| 437 | |
---|
| 438 | // either add sucessful match or push back next state |
---|
| 439 | if ( new_resn.newNeed.empty() ) { |
---|
[0b00df0] | 440 | finalizeAssertions( new_resn.alt, new_resn.inferred, out ); |
---|
[6d6e829] | 441 | } else { |
---|
| 442 | new_resns.emplace_back( std::move(new_resn), IterateState ); |
---|
| 443 | } |
---|
| 444 | } |
---|
| 445 | } |
---|
| 446 | nextResn:; } |
---|
| 447 | |
---|
| 448 | // finish or reset for next round |
---|
| 449 | if ( new_resns.empty() ) return; |
---|
| 450 | resns.swap( new_resns ); |
---|
| 451 | new_resns.clear(); |
---|
| 452 | } |
---|
| 453 | |
---|
| 454 | // exceeded recursion limit if reaches here |
---|
| 455 | if ( out.empty() ) { |
---|
| 456 | SemanticError( alt.expr->location, "Too many recursive assertions" ); |
---|
| 457 | } |
---|
| 458 | } |
---|
| 459 | } // namespace ResolvExpr |
---|
| 460 | |
---|
| 461 | // Local Variables: // |
---|
| 462 | // tab-width: 4 // |
---|
| 463 | // mode: c++ // |
---|
| 464 | // compile-command: "make install" // |
---|
| 465 | // End: // |
---|