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