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 : Peter A. Buhr |
---|
12 | // Last Modified On : Wed Jul 10 16:10:37 2019 |
---|
13 | // Update Count : 2 |
---|
14 | // |
---|
15 | |
---|
16 | #include "ResolveAssertions.h" |
---|
17 | |
---|
18 | #include <algorithm> // for sort |
---|
19 | #include <cassert> // for assertf |
---|
20 | #include <list> // for list |
---|
21 | #include <memory> // for unique_ptr |
---|
22 | #include <sstream> // for ostringstream |
---|
23 | #include <string> // for string |
---|
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 |
---|
30 | #include "Common/Indenter.h" // for Indenter |
---|
31 | #include "Common/utility.h" // for sort_mins |
---|
32 | #include "GenPoly/GenPoly.h" // for getFunctionType |
---|
33 | #include "ResolvExpr/RenameVars.h" // for renameTyVars |
---|
34 | #include "SymTab/Indexer.h" // for Indexer |
---|
35 | #include "SymTab/Mangler.h" // for Mangler |
---|
36 | #include "SynTree/Expression.h" // for InferredParams |
---|
37 | #include "TypeEnvironment.h" // for TypeEnvironment, etc. |
---|
38 | #include "typeops.h" // for adjustExprType, specCost |
---|
39 | #include "Unify.h" // for unify |
---|
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 |
---|
50 | UniqueId resnSlot; ///< Slot for any recursive assertion IDs |
---|
51 | |
---|
52 | AssnCandidate( const SymTab::Indexer::IdData& cdata, Type* adjType, TypeEnvironment&& env, |
---|
53 | AssertionSet&& have, AssertionSet&& need, OpenVarSet&& openVars, UniqueId resnSlot ) |
---|
54 | : cdata(cdata), adjType(adjType), env(std::move(env)), have(std::move(have)), |
---|
55 | need(std::move(need)), openVars(std::move(openVars)), resnSlot(resnSlot) {} |
---|
56 | }; |
---|
57 | |
---|
58 | /// List of candidate assertion resolutions |
---|
59 | using CandidateList = std::vector<AssnCandidate>; |
---|
60 | |
---|
61 | /// Reference to single deferred item |
---|
62 | struct DeferRef { |
---|
63 | const DeclarationWithType* decl; |
---|
64 | const AssertionSetValue& info; |
---|
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 { |
---|
71 | const DeclarationWithType* decl; |
---|
72 | const AssertionSetValue& info; |
---|
73 | CandidateList matches; |
---|
74 | |
---|
75 | DeferItem( const DeclarationWithType* decl, const AssertionSetValue& info, CandidateList&& matches ) |
---|
76 | : decl(decl), info(info), matches(std::move(matches)) {} |
---|
77 | |
---|
78 | bool empty() const { return matches.empty(); } |
---|
79 | |
---|
80 | CandidateList::size_type size() const { return matches.size(); } |
---|
81 | |
---|
82 | DeferRef operator[] ( unsigned i ) const { return { decl, info, matches[i] }; } |
---|
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; |
---|
106 | Cost cost; |
---|
107 | |
---|
108 | OutType( const TypeEnvironment& env, const OpenVarSet& openVars, |
---|
109 | const std::vector<DeferRef>& assns ) |
---|
110 | : env(env), openVars(openVars), assns(assns), cost(Cost::infinity) {} |
---|
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 | |
---|
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 |
---|
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 ) { |
---|
157 | // compute conversion cost from satisfying decl to assertion |
---|
158 | k += computeConversionCost( |
---|
159 | assn.match.adjType, assn.decl->get_type(), indexer, x.env ); |
---|
160 | |
---|
161 | // mark vars+specialization cost on function-type assertions |
---|
162 | FunctionType* func = GenPoly::getFunctionType( assn.match.cdata.id->get_type() ); |
---|
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() ); |
---|
171 | } |
---|
172 | } |
---|
173 | |
---|
174 | // cache and return |
---|
175 | x.cost = k; |
---|
176 | return k; |
---|
177 | } |
---|
178 | |
---|
179 | /// compares elements by cost |
---|
180 | bool operator() ( Element& a, Element& b ) const { |
---|
181 | return get( a ) < get( b ); |
---|
182 | } |
---|
183 | }; |
---|
184 | |
---|
185 | /// Set of assertion resolutions, grouped by resolution ID |
---|
186 | using InferCache = std::unordered_map< UniqueId, InferredParams >; |
---|
187 | |
---|
188 | /// Lexicographically-ordered vector of costs |
---|
189 | using CostVec = std::vector< Cost >; |
---|
190 | |
---|
191 | int compare( const CostVec & a, const CostVec & b ) { |
---|
192 | unsigned i = 0; |
---|
193 | do { |
---|
194 | // lex-compare where shorter one is less |
---|
195 | if ( i == a.size() ) { |
---|
196 | return i == b.size() ? 0 : -1; |
---|
197 | } |
---|
198 | if ( i == b.size() /* && i < a.size() */ ) return 1; |
---|
199 | |
---|
200 | int c = a[i].compare( b[i] ); |
---|
201 | if ( c != 0 ) return c; |
---|
202 | } while ( ++i ); |
---|
203 | assert(!"unreachable"); |
---|
204 | } |
---|
205 | |
---|
206 | bool operator< ( const CostVec & a, const CostVec & b ) { return compare( a, b ) < 0; } |
---|
207 | |
---|
208 | /// Flag for state iteration |
---|
209 | enum IterateFlag { IterateState }; |
---|
210 | |
---|
211 | /// State needed to resolve a set of assertions |
---|
212 | struct ResnState { |
---|
213 | Alternative alt; ///< Alternative assertion is rooted on |
---|
214 | AssertionList need; ///< Assertions to find |
---|
215 | AssertionSet newNeed; ///< New assertions for current resolutions |
---|
216 | DeferList deferred; ///< Deferred matches |
---|
217 | InferCache inferred; ///< Cache of already-inferred parameters |
---|
218 | CostVec costs; ///< Costs of recursive assertion satisfaction for disambiguation |
---|
219 | SymTab::Indexer& indexer; ///< Name lookup (depends on previous assertions) |
---|
220 | |
---|
221 | /// Initial resolution state for an alternative |
---|
222 | ResnState( Alternative & a, SymTab::Indexer & indexer ) |
---|
223 | : alt(a), need(), newNeed(), deferred(), inferred(), costs{ Cost::zero }, indexer(indexer) { |
---|
224 | need.swap( a.need ); |
---|
225 | } |
---|
226 | |
---|
227 | /// Updated resolution state with new need-list |
---|
228 | ResnState( ResnState && o, IterateFlag ) |
---|
229 | : alt(std::move(o.alt)), need(o.newNeed.begin(), o.newNeed.end()), newNeed(), deferred(), |
---|
230 | inferred(std::move(o.inferred)), costs(o.costs), indexer(o.indexer) { |
---|
231 | costs.emplace_back( Cost::zero ); |
---|
232 | } |
---|
233 | }; |
---|
234 | |
---|
235 | /// Binds a single assertion, updating resolution state |
---|
236 | void bindAssertion( const DeclarationWithType * decl, AssertionSetValue info, Alternative & alt, |
---|
237 | AssnCandidate & match, InferCache & inferred ) { |
---|
238 | |
---|
239 | const DeclarationWithType * candidate = match.cdata.id; |
---|
240 | assertf( candidate->uniqueId, "Assertion candidate does not have a unique ID: %s", toString( candidate ).c_str() ); |
---|
241 | |
---|
242 | Expression * varExpr = match.cdata.combine( alt.cvtCost ); |
---|
243 | delete varExpr->result; |
---|
244 | varExpr->result = match.adjType->clone(); |
---|
245 | if ( match.resnSlot ) { varExpr->resnSlots.push_back( match.resnSlot ); } |
---|
246 | |
---|
247 | // place newly-inferred assertion in proper place in cache |
---|
248 | inferred[ info.resnSlot ][ decl->get_uniqueId() ] = ParamEntry{ |
---|
249 | candidate->uniqueId, candidate->clone(), match.adjType->clone(), decl->get_type()->clone(), |
---|
250 | varExpr }; |
---|
251 | } |
---|
252 | |
---|
253 | /// Adds a captured assertion to the symbol table |
---|
254 | void addToIndexer( AssertionSet & assertSet, SymTab::Indexer & indexer ) { |
---|
255 | for ( auto& i : assertSet ) { |
---|
256 | if ( i.second.isUsed ) { |
---|
257 | indexer.addId( i.first ); |
---|
258 | } |
---|
259 | } |
---|
260 | } |
---|
261 | |
---|
262 | // in AlternativeFinder.cc; unique ID for assertion resolutions |
---|
263 | extern UniqueId globalResnSlot; |
---|
264 | |
---|
265 | /// Resolve a single assertion, in context |
---|
266 | bool resolveAssertion( AssertionItem & assn, ResnState & resn ) { |
---|
267 | // skip unused assertions |
---|
268 | if ( ! assn.info.isUsed ) return true; |
---|
269 | |
---|
270 | // lookup candidates for this assertion |
---|
271 | std::list< SymTab::Indexer::IdData > candidates; |
---|
272 | resn.indexer.lookupId( assn.decl->name, candidates ); |
---|
273 | |
---|
274 | // find the candidates that unify with the desired type |
---|
275 | CandidateList matches; |
---|
276 | for ( const auto & cdata : candidates ) { |
---|
277 | const DeclarationWithType * candidate = cdata.id; |
---|
278 | |
---|
279 | // build independent unification context for candidate |
---|
280 | AssertionSet have, newNeed; |
---|
281 | TypeEnvironment newEnv{ resn.alt.env }; |
---|
282 | OpenVarSet newOpenVars{ resn.alt.openVars }; |
---|
283 | Type * adjType = candidate->get_type()->clone(); |
---|
284 | adjustExprType( adjType, newEnv, resn.indexer ); |
---|
285 | renameTyVars( adjType ); |
---|
286 | |
---|
287 | // keep unifying candidates |
---|
288 | if ( unify( assn.decl->get_type(), adjType, newEnv, newNeed, have, newOpenVars, |
---|
289 | resn.indexer ) ) { |
---|
290 | // set up binding slot for recursive assertions |
---|
291 | UniqueId crntResnSlot = 0; |
---|
292 | if ( ! newNeed.empty() ) { |
---|
293 | crntResnSlot = ++globalResnSlot; |
---|
294 | for ( auto& a : newNeed ) { |
---|
295 | a.second.resnSlot = crntResnSlot; |
---|
296 | } |
---|
297 | } |
---|
298 | |
---|
299 | matches.emplace_back( cdata, adjType, std::move(newEnv), std::move(have), |
---|
300 | std::move(newNeed), std::move(newOpenVars), crntResnSlot ); |
---|
301 | } else { |
---|
302 | delete adjType; |
---|
303 | } |
---|
304 | } |
---|
305 | |
---|
306 | // break if no suitable assertion |
---|
307 | if ( matches.empty() ) return false; |
---|
308 | |
---|
309 | // defer if too many suitable assertions |
---|
310 | if ( matches.size() > 1 ) { |
---|
311 | resn.deferred.emplace_back( assn.decl, assn.info, std::move(matches) ); |
---|
312 | return true; |
---|
313 | } |
---|
314 | |
---|
315 | // otherwise bind current match in ongoing scope |
---|
316 | AssnCandidate& match = matches.front(); |
---|
317 | addToIndexer( match.have, resn.indexer ); |
---|
318 | resn.newNeed.insert( match.need.begin(), match.need.end() ); |
---|
319 | resn.alt.env = std::move(match.env); |
---|
320 | resn.alt.openVars = std::move(match.openVars); |
---|
321 | |
---|
322 | bindAssertion( assn.decl, assn.info, resn.alt, match, resn.inferred ); |
---|
323 | return true; |
---|
324 | } |
---|
325 | |
---|
326 | /// Associates inferred parameters with an expression |
---|
327 | struct InferMatcher { |
---|
328 | InferCache& inferred; |
---|
329 | |
---|
330 | InferMatcher( InferCache& inferred ) : inferred( inferred ) {} |
---|
331 | |
---|
332 | Expression* postmutate( Expression* expr ) { |
---|
333 | // defer missing inferred parameters until they are hopefully found later |
---|
334 | std::vector<UniqueId> missingSlots; |
---|
335 | // place inferred parameters into resolution slots |
---|
336 | for ( UniqueId slot : expr->resnSlots ) { |
---|
337 | // fail if no matching parameters found |
---|
338 | auto it = inferred.find( slot ); |
---|
339 | if ( it == inferred.end() ) { |
---|
340 | missingSlots.push_back( slot ); |
---|
341 | continue; |
---|
342 | } |
---|
343 | InferredParams& inferParams = it->second; |
---|
344 | |
---|
345 | // place inferred parameters into proper place in expression |
---|
346 | for ( auto& entry : inferParams ) { |
---|
347 | // recurse on inferParams of resolved expressions |
---|
348 | entry.second.expr = postmutate( entry.second.expr ); |
---|
349 | // xxx - look at entry.second.inferParams? |
---|
350 | auto res = expr->inferParams.emplace( entry.first, entry.second ); |
---|
351 | assert(res.second); |
---|
352 | } |
---|
353 | } |
---|
354 | |
---|
355 | // clear resolution slots and return |
---|
356 | expr->resnSlots.swap( missingSlots ); |
---|
357 | return expr; |
---|
358 | } |
---|
359 | }; |
---|
360 | |
---|
361 | /// Map of alternative return types to recursive assertion satisfaction costs |
---|
362 | using PruneMap = std::unordered_map<std::string, CostVec>; |
---|
363 | |
---|
364 | /// Gets the pruning key for an alternative |
---|
365 | std::string pruneKey( const Alternative & alt ) { |
---|
366 | Type* resType = alt.expr->result->clone(); |
---|
367 | alt.env.apply( resType ); |
---|
368 | std::string resKey = SymTab::Mangler::mangleType( resType ); |
---|
369 | delete resType; |
---|
370 | return resKey; |
---|
371 | } |
---|
372 | |
---|
373 | /// Replace resnSlots with inferParams and add alternative to output list, if meets pruning |
---|
374 | /// threshold. |
---|
375 | void finalizeAssertions( ResnState& resn, PruneMap & pruneThresholds, AltList& out ) { |
---|
376 | // prune if cheaper alternative for same key has already been generated |
---|
377 | std::string resKey = pruneKey( resn.alt ); |
---|
378 | auto it = pruneThresholds.find( resKey ); |
---|
379 | if ( it != pruneThresholds.end() ) { |
---|
380 | if ( it->second < resn.costs ) return; |
---|
381 | } else { |
---|
382 | pruneThresholds.emplace_hint( it, resKey, resn.costs ); |
---|
383 | } |
---|
384 | |
---|
385 | // replace resolution slots with inferred params, add to output |
---|
386 | PassVisitor<InferMatcher> matcher{ resn.inferred }; |
---|
387 | resn.alt.expr = resn.alt.expr->acceptMutator( matcher ); |
---|
388 | out.emplace_back( resn.alt ); |
---|
389 | } |
---|
390 | |
---|
391 | /// Limit to depth of recursion of assertion satisfaction |
---|
392 | static const int recursionLimit = 4; |
---|
393 | /// Maximum number of simultaneously-deferred assertions to attempt concurrent satisfaction of |
---|
394 | static const int deferLimit = 10; |
---|
395 | |
---|
396 | void resolveAssertions( Alternative& alt, const SymTab::Indexer& indexer, AltList& out, std::list<std::string>& errors ) { |
---|
397 | // finish early if no assertions to resolve |
---|
398 | if ( alt.need.empty() ) { |
---|
399 | out.emplace_back( alt ); |
---|
400 | return; |
---|
401 | } |
---|
402 | |
---|
403 | // build list of possible resolutions |
---|
404 | using ResnList = std::vector<ResnState>; |
---|
405 | SymTab::Indexer root_indexer{ indexer }; |
---|
406 | ResnList resns{ ResnState{ alt, root_indexer } }; |
---|
407 | ResnList new_resns{}; |
---|
408 | |
---|
409 | // Pruning thresholds by result type of the output alternatives. |
---|
410 | // Alternatives *should* be generated in sorted order, so no need to retroactively prune |
---|
411 | PruneMap thresholds; |
---|
412 | |
---|
413 | // resolve assertions in breadth-first-order up to a limited number of levels deep |
---|
414 | for ( unsigned level = 0; level < recursionLimit; ++level ) { |
---|
415 | // scan over all mutually-compatible resolutions |
---|
416 | for ( auto& resn : resns ) { |
---|
417 | // stop this branch if already found a better option |
---|
418 | auto it = thresholds.find( pruneKey( resn.alt ) ); |
---|
419 | if ( it != thresholds.end() && it->second < resn.costs ) goto nextResn; |
---|
420 | |
---|
421 | // make initial pass at matching assertions |
---|
422 | for ( auto& assn : resn.need ) { |
---|
423 | // fail early if any assertion is not resolvable |
---|
424 | if ( ! resolveAssertion( assn, resn ) ) { |
---|
425 | Indenter tabs{ 3 }; |
---|
426 | std::ostringstream ss; |
---|
427 | ss << tabs << "Unsatisfiable alternative:\n"; |
---|
428 | resn.alt.print( ss, ++tabs ); |
---|
429 | ss << (tabs-1) << "Could not satisfy assertion:\n"; |
---|
430 | assn.decl->print( ss, tabs ); |
---|
431 | |
---|
432 | errors.emplace_back( ss.str() ); |
---|
433 | goto nextResn; |
---|
434 | } |
---|
435 | } |
---|
436 | |
---|
437 | if ( resn.deferred.empty() ) { |
---|
438 | // either add successful match or push back next state |
---|
439 | if ( resn.newNeed.empty() ) { |
---|
440 | finalizeAssertions( resn, thresholds, out ); |
---|
441 | } else { |
---|
442 | new_resns.emplace_back( std::move(resn), IterateState ); |
---|
443 | } |
---|
444 | } else if ( resn.deferred.size() > deferLimit ) { |
---|
445 | // too many deferred assertions to attempt mutual compatibility |
---|
446 | Indenter tabs{ 3 }; |
---|
447 | std::ostringstream ss; |
---|
448 | ss << tabs << "Unsatisfiable alternative:\n"; |
---|
449 | resn.alt.print( ss, ++tabs ); |
---|
450 | ss << (tabs-1) << "Too many non-unique satisfying assignments for " |
---|
451 | "assertions:\n"; |
---|
452 | for ( const auto& d : resn.deferred ) { |
---|
453 | d.decl->print( ss, tabs ); |
---|
454 | } |
---|
455 | |
---|
456 | errors.emplace_back( ss.str() ); |
---|
457 | goto nextResn; |
---|
458 | } else { |
---|
459 | // resolve deferred assertions by mutual compatibility |
---|
460 | std::vector<CandidateEnvMerger::OutType> compatible = filterCombos( |
---|
461 | resn.deferred, |
---|
462 | CandidateEnvMerger{ resn.alt.env, resn.alt.openVars, resn.indexer } ); |
---|
463 | // fail early if no mutually-compatible assertion satisfaction |
---|
464 | if ( compatible.empty() ) { |
---|
465 | Indenter tabs{ 3 }; |
---|
466 | std::ostringstream ss; |
---|
467 | ss << tabs << "Unsatisfiable alternative:\n"; |
---|
468 | resn.alt.print( ss, ++tabs ); |
---|
469 | ss << (tabs-1) << "No mutually-compatible satisfaction for assertions:\n"; |
---|
470 | for ( const auto& d : resn.deferred ) { |
---|
471 | d.decl->print( ss, tabs ); |
---|
472 | } |
---|
473 | |
---|
474 | errors.emplace_back( ss.str() ); |
---|
475 | goto nextResn; |
---|
476 | } |
---|
477 | // sort by cost for overall pruning |
---|
478 | CandidateCost coster{ resn.indexer }; |
---|
479 | std::sort( compatible.begin(), compatible.end(), coster ); |
---|
480 | |
---|
481 | for ( auto& compat : compatible ) { |
---|
482 | ResnState new_resn = resn; |
---|
483 | |
---|
484 | // add compatible assertions to new resolution state |
---|
485 | for ( DeferRef r : compat.assns ) { |
---|
486 | AssnCandidate match = r.match; |
---|
487 | addToIndexer( match.have, new_resn.indexer ); |
---|
488 | new_resn.newNeed.insert( match.need.begin(), match.need.end() ); |
---|
489 | |
---|
490 | bindAssertion( r.decl, r.info, new_resn.alt, match, new_resn.inferred ); |
---|
491 | } |
---|
492 | |
---|
493 | // set mutual environment into resolution state |
---|
494 | new_resn.alt.env = std::move(compat.env); |
---|
495 | new_resn.alt.openVars = std::move(compat.openVars); |
---|
496 | |
---|
497 | // mark cost of this path |
---|
498 | new_resn.costs.back() += compat.cost; |
---|
499 | |
---|
500 | // either add sucessful match or push back next state |
---|
501 | if ( new_resn.newNeed.empty() ) { |
---|
502 | finalizeAssertions( new_resn, thresholds, out ); |
---|
503 | } else { |
---|
504 | new_resns.emplace_back( std::move(new_resn), IterateState ); |
---|
505 | } |
---|
506 | } |
---|
507 | } |
---|
508 | nextResn:; } |
---|
509 | |
---|
510 | // finish or reset for next round |
---|
511 | if ( new_resns.empty() ) return; |
---|
512 | resns.swap( new_resns ); |
---|
513 | new_resns.clear(); |
---|
514 | } |
---|
515 | |
---|
516 | // exceeded recursion limit if reaches here |
---|
517 | if ( out.empty() ) { |
---|
518 | SemanticError( alt.expr->location, "Too many recursive assertions" ); |
---|
519 | } |
---|
520 | } |
---|
521 | } // namespace ResolvExpr |
---|
522 | |
---|
523 | // Local Variables: // |
---|
524 | // tab-width: 4 // |
---|
525 | // mode: c++ // |
---|
526 | // compile-command: "make install" // |
---|
527 | // End: // |
---|