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 <cassert> // for assertf
|
---|
19 | #include <list> // for list
|
---|
20 | #include <utility> // for move
|
---|
21 | #include <vector> // for vector
|
---|
22 |
|
---|
23 | #include "Alternative.h" // for Alternative, AssertionItem
|
---|
24 | #include "Common/FilterCombos.h" // for filterCombos
|
---|
25 | #include "SymTab/Indexer.h" // for Indexer
|
---|
26 | #include "TypeEnvironment.h" // for TypeEnvironment, etc.
|
---|
27 | #include "typeops.h" // for adjustExprType
|
---|
28 | #include "Unify.h" // for unify
|
---|
29 |
|
---|
30 | namespace ResolvExpr {
|
---|
31 | /// Unified assertion candidate
|
---|
32 | struct AssnCandidate {
|
---|
33 | SymTab::Indexer::IdData cdata; ///< Satisfying declaration
|
---|
34 | Type* adjType; ///< Satisfying type
|
---|
35 | TypeEnvironment env; ///< Post-unification environment
|
---|
36 | AssertionSet have; ///< Post-unification have-set
|
---|
37 | AssertionSet need; ///< Post-unification need-set
|
---|
38 | OpenVarSet openVars; ///< Post-unification open-var set
|
---|
39 |
|
---|
40 | AssnCandidate( const SymTab::Indexer::IdData& cdata, Type* adjType, TypeEnvironment&& env,
|
---|
41 | AssertionSet&& have, AssertionSet&& need, OpenVarSet&& openVars )
|
---|
42 | : cdata(cdata), adjType(adjType), env(std::move(env)), have(std::move(have)),
|
---|
43 | need(std::move(need)), openVars(std::move(openVars)) {}
|
---|
44 | };
|
---|
45 |
|
---|
46 | /// List of candidate assertion resolutions
|
---|
47 | using CandidateList = std::vector<AssnCandidate>;
|
---|
48 |
|
---|
49 | /// Reference to single deferred item
|
---|
50 | struct DeferRef {
|
---|
51 | const DeclarationWithType* decl;
|
---|
52 | const AssertionSetValue& info;
|
---|
53 | const AssnCandidate& match;
|
---|
54 | };
|
---|
55 |
|
---|
56 | /// Wrapper for the deferred items from a single assertion resolution.
|
---|
57 | /// Acts like indexed list of DeferRef
|
---|
58 | struct DeferItem {
|
---|
59 | DeclarationWithType* decl;
|
---|
60 | AssertionSetValue info;
|
---|
61 | CandidateList matches;
|
---|
62 |
|
---|
63 | DeferItem( DeclarationWithType* decl, const AssertionSetValue& info,
|
---|
64 | CandidateList&& matches )
|
---|
65 | : decl(decl), info(info), matches(std::move(matches)) {}
|
---|
66 |
|
---|
67 | bool empty() const { return matches.empty(); }
|
---|
68 |
|
---|
69 | CandidateList::size_type size() const { return matches.size(); }
|
---|
70 |
|
---|
71 | DeferRef operator[] ( unsigned i ) const { return { decl, info, matches[i] }; }
|
---|
72 | };
|
---|
73 |
|
---|
74 | /// List of deferred resolution items
|
---|
75 | using DeferList = std::vector<DeferItem>;
|
---|
76 |
|
---|
77 | /// Combo iterator that combines candidates into an output list, merging their environments.
|
---|
78 | /// Rejects an appended candidate if the environments cannot be merged.
|
---|
79 | class CandidateEnvMerger {
|
---|
80 | /// Current list of merged candidates
|
---|
81 | std::vector<DeferRef> crnt;
|
---|
82 | /// Stack of environments to support backtracking
|
---|
83 | std::vector<TypeEnvironment> envs;
|
---|
84 | /// Stack of open variables to support backtracking
|
---|
85 | std::vector<OpenVarSet> varSets;
|
---|
86 | /// Indexer to use for merges
|
---|
87 | const SymTab::Indexer& indexer;
|
---|
88 |
|
---|
89 | public:
|
---|
90 | /// The merged environment/open variables and the list of candidates
|
---|
91 | struct OutType {
|
---|
92 | TypeEnvironment env;
|
---|
93 | OpenVarSet openVars;
|
---|
94 | std::vector<DeferRef> assns;
|
---|
95 |
|
---|
96 | OutType( const TypeEnvironment& env, const OpenVarSet& openVars,
|
---|
97 | const std::vector<DeferRef>& assns )
|
---|
98 | : env(env), openVars(openVars), assns(assns) {}
|
---|
99 | };
|
---|
100 |
|
---|
101 | CandidateEnvMerger( const TypeEnvironment& env, const OpenVarSet& openVars,
|
---|
102 | const SymTab::Indexer& indexer )
|
---|
103 | : crnt(), envs{ env }, varSets{ openVars }, indexer(indexer) {}
|
---|
104 |
|
---|
105 | bool append( DeferRef i ) {
|
---|
106 | TypeEnvironment env = envs.back();
|
---|
107 | OpenVarSet openVars = varSets.back();
|
---|
108 | mergeOpenVars( openVars, i.match.openVars );
|
---|
109 |
|
---|
110 | if ( ! env.combine( i.match.env, openVars, indexer ) ) return false;
|
---|
111 |
|
---|
112 | crnt.emplace_back( i );
|
---|
113 | envs.emplace_back( env );
|
---|
114 | varSets.emplace_back( openVars );
|
---|
115 | return true;
|
---|
116 | }
|
---|
117 |
|
---|
118 | void backtrack() {
|
---|
119 | crnt.pop_back();
|
---|
120 | envs.pop_back();
|
---|
121 | varSets.pop_back();
|
---|
122 | }
|
---|
123 |
|
---|
124 | OutType finalize() { return { envs.back(), varSets.back(), crnt }; }
|
---|
125 | };
|
---|
126 |
|
---|
127 | /// Flag for state iteration
|
---|
128 | enum IterateFlag { IterateState };
|
---|
129 |
|
---|
130 | /// State needed to resolve a set of assertions
|
---|
131 | struct ResnState {
|
---|
132 | Alternative alt; ///< Alternative assertion is rooted on
|
---|
133 | std::vector<AssertionItem> need; ///< Assertions to find
|
---|
134 | AssertionSet newNeed; ///< New assertions for current resolutions
|
---|
135 | DeferList deferred; ///< Deferred matches
|
---|
136 | SymTab::Indexer& indexer; ///< Name lookup (depends on previous assertions)
|
---|
137 |
|
---|
138 | /// Initial resolution state for an alternative
|
---|
139 | ResnState( Alternative& a, SymTab::Indexer& indexer )
|
---|
140 | : alt(a), need(), newNeed(), deferred(), indexer(indexer) {
|
---|
141 | need.swap( a.need );
|
---|
142 | }
|
---|
143 |
|
---|
144 | /// Updated resolution state with new need-list
|
---|
145 | ResnState( ResnState&& o, IterateFlag )
|
---|
146 | : alt(std::move(o.alt)), need(o.newNeed.begin(), o.newNeed.end()), newNeed(), deferred(),
|
---|
147 | indexer(o.indexer) {}
|
---|
148 | };
|
---|
149 |
|
---|
150 | /// Binds a single assertion, updating resolution state
|
---|
151 | void bindAssertion( const DeclarationWithType* decl, AssertionSetValue info, Alternative& alt,
|
---|
152 | AssnCandidate& match ) {
|
---|
153 |
|
---|
154 | DeclarationWithType* candidate = match.cdata.id;
|
---|
155 | assertf( candidate->get_uniqueId(), "Assertion candidate does not have a unique ID: %s", toString( candidate ).c_str() );
|
---|
156 |
|
---|
157 | // everything with an empty idChain was pulled in by the current assertion.
|
---|
158 | // add current assertion's idChain + current assertion's ID so that the correct
|
---|
159 | // inferParameters can be found.
|
---|
160 | for ( auto& a : match.need ) {
|
---|
161 | if ( a.second.idChain.empty() ) {
|
---|
162 | a.second.idChain = info.idChain;
|
---|
163 | a.second.idChain.push_back( decl->get_uniqueId() );
|
---|
164 | }
|
---|
165 | }
|
---|
166 |
|
---|
167 | Expression* varExpr = match.cdata.combine( alt.cvtCost );
|
---|
168 | delete varExpr->result;
|
---|
169 | varExpr->result = match.adjType->clone();
|
---|
170 |
|
---|
171 | // follow the current assertion's ID chain to find the correct set of inferred parameters
|
---|
172 | // to add the candidate o (i.e. the set of inferred parameters belonging to the entity
|
---|
173 | // which requested the assertion parameter)
|
---|
174 | InferredParams* inferParams = &alt.expr->inferParams;
|
---|
175 | for ( UniqueId id : info.idChain ) {
|
---|
176 | inferParams = (*inferParams)[ id ].inferParams.get();
|
---|
177 | }
|
---|
178 |
|
---|
179 | (*inferParams)[ decl->get_uniqueId() ] = ParamEntry{
|
---|
180 | candidate->get_uniqueId(), match.adjType, decl->get_type()->clone(), varExpr };
|
---|
181 | }
|
---|
182 |
|
---|
183 | /// Adds a captured assertion to the symbol table
|
---|
184 | void addToIndexer( AssertionSet &assertSet, SymTab::Indexer &indexer ) {
|
---|
185 | for ( AssertionSet::iterator i = assertSet.begin(); i != assertSet.end(); ++i ) {
|
---|
186 | if ( i->second.isUsed ) {
|
---|
187 | indexer.addId( i->first );
|
---|
188 | }
|
---|
189 | }
|
---|
190 | }
|
---|
191 |
|
---|
192 | /// Resolve a single assertion, in context
|
---|
193 | bool resolveAssertion( AssertionItem& assn, ResnState& resn ) {
|
---|
194 | // skip unused assertions
|
---|
195 | if ( ! assn.info.isUsed ) return true;
|
---|
196 |
|
---|
197 | // lookup candidates for this assertion
|
---|
198 | std::list< SymTab::Indexer::IdData > candidates;
|
---|
199 | resn.indexer.lookupId( assn.decl->name, candidates );
|
---|
200 |
|
---|
201 | // find the candidates that unify with the desired type
|
---|
202 | CandidateList matches;
|
---|
203 | for ( const auto& cdata : candidates ) {
|
---|
204 | DeclarationWithType* candidate = cdata.id;
|
---|
205 |
|
---|
206 | // build independent unification context for candidate
|
---|
207 | AssertionSet have, newNeed;
|
---|
208 | TypeEnvironment newEnv{ resn.alt.env };
|
---|
209 | OpenVarSet newOpenVars{ resn.alt.openVars };
|
---|
210 | Type* adjType = candidate->get_type()->clone();
|
---|
211 | adjustExprType( adjType, newEnv, resn.indexer );
|
---|
212 |
|
---|
213 | // keep unifying candidates
|
---|
214 | if ( unify( assn.decl->get_type(), adjType, newEnv, newNeed, have, newOpenVars,
|
---|
215 | resn.indexer ) ) {
|
---|
216 | matches.emplace_back( cdata, adjType, std::move(newEnv), std::move(have),
|
---|
217 | std::move(newNeed), std::move(newOpenVars) );
|
---|
218 | } else {
|
---|
219 | delete adjType;
|
---|
220 | }
|
---|
221 | }
|
---|
222 |
|
---|
223 | // break if no suitable assertion
|
---|
224 | if ( matches.empty() ) return false;
|
---|
225 |
|
---|
226 | // defer if too many suitable assertions
|
---|
227 | if ( matches.size() > 1 ) {
|
---|
228 | resn.deferred.emplace_back( assn.decl, assn.info, std::move(matches) );
|
---|
229 | return true;
|
---|
230 | }
|
---|
231 |
|
---|
232 | // otherwise bind current match in ongoing scope
|
---|
233 | AssnCandidate& match = matches.front();
|
---|
234 | addToIndexer( match.have, resn.indexer );
|
---|
235 | resn.newNeed.insert( match.need.begin(), match.need.end() );
|
---|
236 | resn.alt.env = std::move(match.env);
|
---|
237 | resn.alt.openVars = std::move(match.openVars);
|
---|
238 |
|
---|
239 | bindAssertion( assn.decl, assn.info, resn.alt, match );
|
---|
240 | return true;
|
---|
241 | }
|
---|
242 |
|
---|
243 | ///< Limit to depth of recursion of assertion satisfaction
|
---|
244 | static const int recursionLimit = /* 10 */ 4;
|
---|
245 |
|
---|
246 | void resolveAssertions( Alternative& alt, const SymTab::Indexer& indexer, AltList& out ) {
|
---|
247 | // finish early if no assertions to resolve
|
---|
248 | if ( alt.need.empty() ) {
|
---|
249 | out.emplace_back( alt );
|
---|
250 | return;
|
---|
251 | }
|
---|
252 |
|
---|
253 | // build list of possible resolutions
|
---|
254 | using ResnList = std::vector<ResnState>;
|
---|
255 | SymTab::Indexer root_indexer{ indexer };
|
---|
256 | ResnList resns{ ResnState{ alt, root_indexer } };
|
---|
257 | ResnList new_resns{};
|
---|
258 |
|
---|
259 | // resolve assertions in breadth-first-order up to a limited number of levels deep
|
---|
260 | for ( unsigned level = 0; level < recursionLimit; ++level ) {
|
---|
261 | // scan over all mutually-compatible resolutions
|
---|
262 | for ( auto& resn : resns ) {
|
---|
263 | // make initial pass at matching assertions
|
---|
264 | for ( auto& assn : resn.need ) {
|
---|
265 | // fail early if any assertion is not resolvable
|
---|
266 | if ( ! resolveAssertion( assn, resn ) ) goto nextResn;
|
---|
267 | }
|
---|
268 |
|
---|
269 | if ( resn.deferred.empty() ) {
|
---|
270 | // either add successful match or push back next state
|
---|
271 | if ( resn.newNeed.empty() ) {
|
---|
272 | out.emplace_back( resn.alt );
|
---|
273 | } else {
|
---|
274 | new_resns.emplace_back( std::move(resn), IterateState );
|
---|
275 | }
|
---|
276 | } else {
|
---|
277 | // resolve deferred assertions by mutual compatibility
|
---|
278 | std::vector<CandidateEnvMerger::OutType> compatible = filterCombos(
|
---|
279 | resn.deferred,
|
---|
280 | CandidateEnvMerger{ resn.alt.env, resn.alt.openVars, resn.indexer } );
|
---|
281 |
|
---|
282 | for ( auto& compat : compatible ) {
|
---|
283 | ResnState new_resn = resn;
|
---|
284 |
|
---|
285 | // add compatible assertions to new resolution state
|
---|
286 | for ( DeferRef r : compat.assns ) {
|
---|
287 | AssnCandidate match = r.match;
|
---|
288 | addToIndexer( match.have, new_resn.indexer );
|
---|
289 | new_resn.newNeed.insert( match.need.begin(), match.need.end() );
|
---|
290 |
|
---|
291 | bindAssertion( r.decl, r.info, new_resn.alt, match );
|
---|
292 | }
|
---|
293 |
|
---|
294 | // set mutual environment into resolution state
|
---|
295 | new_resn.alt.env = std::move(compat.env);
|
---|
296 | new_resn.alt.openVars = std::move(compat.openVars);
|
---|
297 |
|
---|
298 | // either add sucessful match or push back next state
|
---|
299 | if ( new_resn.newNeed.empty() ) {
|
---|
300 | out.emplace_back( new_resn.alt );
|
---|
301 | } else {
|
---|
302 | new_resns.emplace_back( std::move(new_resn), IterateState );
|
---|
303 | }
|
---|
304 | }
|
---|
305 | }
|
---|
306 | nextResn:; }
|
---|
307 |
|
---|
308 | // finish or reset for next round
|
---|
309 | if ( new_resns.empty() ) return;
|
---|
310 | resns.swap( new_resns );
|
---|
311 | new_resns.clear();
|
---|
312 | }
|
---|
313 |
|
---|
314 | // exceeded recursion limit if reaches here
|
---|
315 | if ( out.empty() ) {
|
---|
316 | SemanticError( alt.expr->location, "Too many recursive assertions" );
|
---|
317 | }
|
---|
318 | }
|
---|
319 | } // namespace ResolvExpr
|
---|
320 |
|
---|
321 | // Local Variables: //
|
---|
322 | // tab-width: 4 //
|
---|
323 | // mode: c++ //
|
---|
324 | // compile-command: "make install" //
|
---|
325 | // End: //
|
---|