[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 |
---|
[7d01cf44] | 11 | // Last Modified By : Andrew Beach |
---|
| 12 | // Last Modified On : Thu Aug 8 16:47:00 2019 |
---|
| 13 | // Update Count : 3 |
---|
[6d6e829] | 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 | |
---|
[aaeacf4] | 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)), |
---|
[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 | |
---|
[85dac33] | 75 | DeferItem( const DeclarationWithType* decl, const AssertionSetValue& info, CandidateList&& matches ) |
---|
[052cd71] | 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 | |
---|
[aaeacf4] | 88 | /// Combo iterator that combines candidates into an output list, merging their environments. |
---|
[6d6e829] | 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; |
---|
[aaeacf4] | 99 | |
---|
[6d6e829] | 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 | |
---|
[aaeacf4] | 108 | OutType( const TypeEnvironment& env, const OpenVarSet& openVars, |
---|
[6d6e829] | 109 | const std::vector<DeferRef>& assns ) |
---|
[c519942] | 110 | : env(env), openVars(openVars), assns(assns), cost(Cost::infinity) {} |
---|
[6d6e829] | 111 | }; |
---|
| 112 | |
---|
[aaeacf4] | 113 | CandidateEnvMerger( const TypeEnvironment& env, const OpenVarSet& openVars, |
---|
[6d6e829] | 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 | |
---|
[aaeacf4] | 139 | /// Comparator for CandidateEnvMerger outputs that sums their costs and caches the stored |
---|
[fbecee5] | 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 |
---|
[aaeacf4] | 158 | k += computeConversionCost( |
---|
[7d01cf44] | 159 | assn.match.adjType, assn.decl->get_type(), false, indexer, x.env ); |
---|
[aaeacf4] | 160 | |
---|
[c519942] | 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; |
---|
[aaeacf4] | 164 | |
---|
[c519942] | 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 | } |
---|
[aaeacf4] | 178 | |
---|
[fbecee5] | 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 | |
---|
[b408364] | 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; |
---|
[6f096d2] | 199 | |
---|
[b408364] | 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 | |
---|
[6d6e829] | 208 | /// Flag for state iteration |
---|
| 209 | enum IterateFlag { IterateState }; |
---|
| 210 | |
---|
| 211 | /// State needed to resolve a set of assertions |
---|
| 212 | struct ResnState { |
---|
[0b00df0] | 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 |
---|
[b408364] | 218 | CostVec costs; ///< Costs of recursive assertion satisfaction for disambiguation |
---|
[0b00df0] | 219 | SymTab::Indexer& indexer; ///< Name lookup (depends on previous assertions) |
---|
[6d6e829] | 220 | |
---|
| 221 | /// Initial resolution state for an alternative |
---|
[6f096d2] | 222 | ResnState( Alternative & a, SymTab::Indexer & indexer ) |
---|
[b408364] | 223 | : alt(a), need(), newNeed(), deferred(), inferred(), costs{ Cost::zero }, indexer(indexer) { |
---|
[6d6e829] | 224 | need.swap( a.need ); |
---|
| 225 | } |
---|
| 226 | |
---|
| 227 | /// Updated resolution state with new need-list |
---|
[6f096d2] | 228 | ResnState( ResnState && o, IterateFlag ) |
---|
[aaeacf4] | 229 | : alt(std::move(o.alt)), need(o.newNeed.begin(), o.newNeed.end()), newNeed(), deferred(), |
---|
[b408364] | 230 | inferred(std::move(o.inferred)), costs(o.costs), indexer(o.indexer) { |
---|
| 231 | costs.emplace_back( Cost::zero ); |
---|
| 232 | } |
---|
[6d6e829] | 233 | }; |
---|
| 234 | |
---|
| 235 | /// Binds a single assertion, updating resolution state |
---|
[6f096d2] | 236 | void bindAssertion( const DeclarationWithType * decl, AssertionSetValue info, Alternative & alt, |
---|
| 237 | AssnCandidate & match, InferCache & inferred ) { |
---|
[aaeacf4] | 238 | |
---|
[6f096d2] | 239 | const DeclarationWithType * candidate = match.cdata.id; |
---|
| 240 | assertf( candidate->uniqueId, "Assertion candidate does not have a unique ID: %s", toString( candidate ).c_str() ); |
---|
[6d6e829] | 241 | |
---|
[6f096d2] | 242 | Expression * varExpr = match.cdata.combine( alt.cvtCost ); |
---|
[2c187378] | 243 | delete varExpr->result; |
---|
| 244 | varExpr->result = match.adjType->clone(); |
---|
[0b00df0] | 245 | if ( match.resnSlot ) { varExpr->resnSlots.push_back( match.resnSlot ); } |
---|
[6d6e829] | 246 | |
---|
[0b00df0] | 247 | // place newly-inferred assertion in proper place in cache |
---|
| 248 | inferred[ info.resnSlot ][ decl->get_uniqueId() ] = ParamEntry{ |
---|
[6f096d2] | 249 | candidate->uniqueId, candidate->clone(), match.adjType->clone(), decl->get_type()->clone(), |
---|
[9aaacc27] | 250 | varExpr }; |
---|
[6d6e829] | 251 | } |
---|
| 252 | |
---|
| 253 | /// Adds a captured assertion to the symbol table |
---|
[6f096d2] | 254 | void addToIndexer( AssertionSet & assertSet, SymTab::Indexer & indexer ) { |
---|
[e1f7eef] | 255 | for ( auto& i : assertSet ) { |
---|
| 256 | if ( i.second.isUsed ) { |
---|
| 257 | indexer.addId( i.first ); |
---|
[6d6e829] | 258 | } |
---|
| 259 | } |
---|
| 260 | } |
---|
| 261 | |
---|
[0b00df0] | 262 | // in AlternativeFinder.cc; unique ID for assertion resolutions |
---|
| 263 | extern UniqueId globalResnSlot; |
---|
| 264 | |
---|
[6d6e829] | 265 | /// Resolve a single assertion, in context |
---|
[6f096d2] | 266 | bool resolveAssertion( AssertionItem & assn, ResnState & resn ) { |
---|
[6d6e829] | 267 | // skip unused assertions |
---|
| 268 | if ( ! assn.info.isUsed ) return true; |
---|
| 269 | |
---|
[052cd71] | 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; |
---|
[6f096d2] | 276 | for ( const auto & cdata : candidates ) { |
---|
| 277 | const DeclarationWithType * candidate = cdata.id; |
---|
[052cd71] | 278 | |
---|
[2fb35df] | 279 | // ignore deleted candidates. |
---|
| 280 | // NOTE: this behavior is different from main resolver. |
---|
| 281 | // further investigations might be needed to determine |
---|
| 282 | // if we should implement the same rule here |
---|
| 283 | // (i.e. error if unique best match is deleted) |
---|
| 284 | if (candidate->isDeleted) continue; |
---|
| 285 | |
---|
| 286 | // build independent unification context. for candidate |
---|
[052cd71] | 287 | AssertionSet have, newNeed; |
---|
| 288 | TypeEnvironment newEnv{ resn.alt.env }; |
---|
| 289 | OpenVarSet newOpenVars{ resn.alt.openVars }; |
---|
[6f096d2] | 290 | Type * adjType = candidate->get_type()->clone(); |
---|
[052cd71] | 291 | adjustExprType( adjType, newEnv, resn.indexer ); |
---|
| 292 | renameTyVars( adjType ); |
---|
| 293 | |
---|
| 294 | // keep unifying candidates |
---|
[aaeacf4] | 295 | if ( unify( assn.decl->get_type(), adjType, newEnv, newNeed, have, newOpenVars, |
---|
[052cd71] | 296 | resn.indexer ) ) { |
---|
| 297 | // set up binding slot for recursive assertions |
---|
| 298 | UniqueId crntResnSlot = 0; |
---|
| 299 | if ( ! newNeed.empty() ) { |
---|
| 300 | crntResnSlot = ++globalResnSlot; |
---|
| 301 | for ( auto& a : newNeed ) { |
---|
| 302 | a.second.resnSlot = crntResnSlot; |
---|
[0b00df0] | 303 | } |
---|
[2fd9f24] | 304 | } |
---|
[e1f7eef] | 305 | |
---|
[aaeacf4] | 306 | matches.emplace_back( cdata, adjType, std::move(newEnv), std::move(have), |
---|
[052cd71] | 307 | std::move(newNeed), std::move(newOpenVars), crntResnSlot ); |
---|
| 308 | } else { |
---|
| 309 | delete adjType; |
---|
| 310 | } |
---|
[6d6e829] | 311 | } |
---|
| 312 | |
---|
| 313 | // break if no suitable assertion |
---|
| 314 | if ( matches.empty() ) return false; |
---|
| 315 | |
---|
| 316 | // defer if too many suitable assertions |
---|
| 317 | if ( matches.size() > 1 ) { |
---|
[052cd71] | 318 | resn.deferred.emplace_back( assn.decl, assn.info, std::move(matches) ); |
---|
[6d6e829] | 319 | return true; |
---|
| 320 | } |
---|
| 321 | |
---|
| 322 | // otherwise bind current match in ongoing scope |
---|
| 323 | AssnCandidate& match = matches.front(); |
---|
| 324 | addToIndexer( match.have, resn.indexer ); |
---|
| 325 | resn.newNeed.insert( match.need.begin(), match.need.end() ); |
---|
[052cd71] | 326 | resn.alt.env = std::move(match.env); |
---|
| 327 | resn.alt.openVars = std::move(match.openVars); |
---|
[6d6e829] | 328 | |
---|
[0b00df0] | 329 | bindAssertion( assn.decl, assn.info, resn.alt, match, resn.inferred ); |
---|
[6d6e829] | 330 | return true; |
---|
| 331 | } |
---|
| 332 | |
---|
[0b00df0] | 333 | /// Associates inferred parameters with an expression |
---|
| 334 | struct InferMatcher { |
---|
| 335 | InferCache& inferred; |
---|
| 336 | |
---|
| 337 | InferMatcher( InferCache& inferred ) : inferred( inferred ) {} |
---|
| 338 | |
---|
| 339 | Expression* postmutate( Expression* expr ) { |
---|
[40290497] | 340 | // defer missing inferred parameters until they are hopefully found later |
---|
| 341 | std::vector<UniqueId> missingSlots; |
---|
[0b00df0] | 342 | // place inferred parameters into resolution slots |
---|
| 343 | for ( UniqueId slot : expr->resnSlots ) { |
---|
| 344 | // fail if no matching parameters found |
---|
[40290497] | 345 | auto it = inferred.find( slot ); |
---|
| 346 | if ( it == inferred.end() ) { |
---|
| 347 | missingSlots.push_back( slot ); |
---|
| 348 | continue; |
---|
| 349 | } |
---|
| 350 | InferredParams& inferParams = it->second; |
---|
[aaeacf4] | 351 | |
---|
[0b00df0] | 352 | // place inferred parameters into proper place in expression |
---|
| 353 | for ( auto& entry : inferParams ) { |
---|
| 354 | // recurse on inferParams of resolved expressions |
---|
| 355 | entry.second.expr = postmutate( entry.second.expr ); |
---|
| 356 | // xxx - look at entry.second.inferParams? |
---|
[462a7c7] | 357 | auto res = expr->inferParams.emplace( entry.first, entry.second ); |
---|
| 358 | assert(res.second); |
---|
[0b00df0] | 359 | } |
---|
| 360 | } |
---|
| 361 | |
---|
| 362 | // clear resolution slots and return |
---|
[40290497] | 363 | expr->resnSlots.swap( missingSlots ); |
---|
[0b00df0] | 364 | return expr; |
---|
| 365 | } |
---|
| 366 | }; |
---|
| 367 | |
---|
[b408364] | 368 | /// Map of alternative return types to recursive assertion satisfaction costs |
---|
| 369 | using PruneMap = std::unordered_map<std::string, CostVec>; |
---|
| 370 | |
---|
| 371 | /// Gets the pruning key for an alternative |
---|
| 372 | std::string pruneKey( const Alternative & alt ) { |
---|
| 373 | Type* resType = alt.expr->result->clone(); |
---|
| 374 | alt.env.apply( resType ); |
---|
| 375 | std::string resKey = SymTab::Mangler::mangleType( resType ); |
---|
| 376 | delete resType; |
---|
[1d760934] | 377 | return resKey; |
---|
[b408364] | 378 | } |
---|
[6f096d2] | 379 | |
---|
| 380 | /// Replace resnSlots with inferParams and add alternative to output list, if meets pruning |
---|
[b408364] | 381 | /// threshold. |
---|
| 382 | void finalizeAssertions( ResnState& resn, PruneMap & pruneThresholds, AltList& out ) { |
---|
| 383 | // prune if cheaper alternative for same key has already been generated |
---|
| 384 | std::string resKey = pruneKey( resn.alt ); |
---|
| 385 | auto it = pruneThresholds.find( resKey ); |
---|
| 386 | if ( it != pruneThresholds.end() ) { |
---|
| 387 | if ( it->second < resn.costs ) return; |
---|
| 388 | } else { |
---|
| 389 | pruneThresholds.emplace_hint( it, resKey, resn.costs ); |
---|
| 390 | } |
---|
| 391 | |
---|
| 392 | // replace resolution slots with inferred params, add to output |
---|
| 393 | PassVisitor<InferMatcher> matcher{ resn.inferred }; |
---|
| 394 | resn.alt.expr = resn.alt.expr->acceptMutator( matcher ); |
---|
| 395 | out.emplace_back( resn.alt ); |
---|
[0b00df0] | 396 | } |
---|
| 397 | |
---|
| 398 | /// Limit to depth of recursion of assertion satisfaction |
---|
[aca6a54c] | 399 | static const int recursionLimit = 7; |
---|
[d3b2c32a] | 400 | /// Maximum number of simultaneously-deferred assertions to attempt concurrent satisfaction of |
---|
| 401 | static const int deferLimit = 10; |
---|
[6d6e829] | 402 | |
---|
[4d2d45f9] | 403 | void resolveAssertions( Alternative& alt, const SymTab::Indexer& indexer, AltList& out, std::list<std::string>& errors ) { |
---|
[6d6e829] | 404 | // finish early if no assertions to resolve |
---|
| 405 | if ( alt.need.empty() ) { |
---|
| 406 | out.emplace_back( alt ); |
---|
| 407 | return; |
---|
| 408 | } |
---|
| 409 | |
---|
| 410 | // build list of possible resolutions |
---|
| 411 | using ResnList = std::vector<ResnState>; |
---|
| 412 | SymTab::Indexer root_indexer{ indexer }; |
---|
| 413 | ResnList resns{ ResnState{ alt, root_indexer } }; |
---|
| 414 | ResnList new_resns{}; |
---|
[6f096d2] | 415 | |
---|
[b408364] | 416 | // Pruning thresholds by result type of the output alternatives. |
---|
| 417 | // Alternatives *should* be generated in sorted order, so no need to retroactively prune |
---|
| 418 | PruneMap thresholds; |
---|
[6d6e829] | 419 | |
---|
| 420 | // resolve assertions in breadth-first-order up to a limited number of levels deep |
---|
| 421 | for ( unsigned level = 0; level < recursionLimit; ++level ) { |
---|
| 422 | // scan over all mutually-compatible resolutions |
---|
| 423 | for ( auto& resn : resns ) { |
---|
[b408364] | 424 | // stop this branch if already found a better option |
---|
| 425 | auto it = thresholds.find( pruneKey( resn.alt ) ); |
---|
| 426 | if ( it != thresholds.end() && it->second < resn.costs ) goto nextResn; |
---|
| 427 | |
---|
[6d6e829] | 428 | // make initial pass at matching assertions |
---|
| 429 | for ( auto& assn : resn.need ) { |
---|
| 430 | // fail early if any assertion is not resolvable |
---|
[052cd71] | 431 | if ( ! resolveAssertion( assn, resn ) ) { |
---|
[99d4584] | 432 | Indenter tabs{ 3 }; |
---|
[4d2d45f9] | 433 | std::ostringstream ss; |
---|
| 434 | ss << tabs << "Unsatisfiable alternative:\n"; |
---|
| 435 | resn.alt.print( ss, ++tabs ); |
---|
[d3b2c32a] | 436 | ss << (tabs-1) << "Could not satisfy assertion:\n"; |
---|
| 437 | assn.decl->print( ss, tabs ); |
---|
[aaeacf4] | 438 | |
---|
[4d2d45f9] | 439 | errors.emplace_back( ss.str() ); |
---|
| 440 | goto nextResn; |
---|
| 441 | } |
---|
[6d6e829] | 442 | } |
---|
| 443 | |
---|
| 444 | if ( resn.deferred.empty() ) { |
---|
| 445 | // either add successful match or push back next state |
---|
| 446 | if ( resn.newNeed.empty() ) { |
---|
[b408364] | 447 | finalizeAssertions( resn, thresholds, out ); |
---|
[6d6e829] | 448 | } else { |
---|
| 449 | new_resns.emplace_back( std::move(resn), IterateState ); |
---|
| 450 | } |
---|
[d3b2c32a] | 451 | } else if ( resn.deferred.size() > deferLimit ) { |
---|
| 452 | // too many deferred assertions to attempt mutual compatibility |
---|
| 453 | Indenter tabs{ 3 }; |
---|
| 454 | std::ostringstream ss; |
---|
| 455 | ss << tabs << "Unsatisfiable alternative:\n"; |
---|
| 456 | resn.alt.print( ss, ++tabs ); |
---|
| 457 | ss << (tabs-1) << "Too many non-unique satisfying assignments for " |
---|
| 458 | "assertions:\n"; |
---|
| 459 | for ( const auto& d : resn.deferred ) { |
---|
| 460 | d.decl->print( ss, tabs ); |
---|
| 461 | } |
---|
| 462 | |
---|
| 463 | errors.emplace_back( ss.str() ); |
---|
| 464 | goto nextResn; |
---|
[6d6e829] | 465 | } else { |
---|
[b3bd772] | 466 | // resolve deferred assertions by mutual compatibility |
---|
[6d6e829] | 467 | std::vector<CandidateEnvMerger::OutType> compatible = filterCombos( |
---|
[aaeacf4] | 468 | resn.deferred, |
---|
[6d6e829] | 469 | CandidateEnvMerger{ resn.alt.env, resn.alt.openVars, resn.indexer } ); |
---|
[4d2d45f9] | 470 | // fail early if no mutually-compatible assertion satisfaction |
---|
| 471 | if ( compatible.empty() ) { |
---|
[99d4584] | 472 | Indenter tabs{ 3 }; |
---|
[4d2d45f9] | 473 | std::ostringstream ss; |
---|
| 474 | ss << tabs << "Unsatisfiable alternative:\n"; |
---|
| 475 | resn.alt.print( ss, ++tabs ); |
---|
[d3b2c32a] | 476 | ss << (tabs-1) << "No mutually-compatible satisfaction for assertions:\n"; |
---|
[4d2d45f9] | 477 | for ( const auto& d : resn.deferred ) { |
---|
[052cd71] | 478 | d.decl->print( ss, tabs ); |
---|
[4d2d45f9] | 479 | } |
---|
| 480 | |
---|
| 481 | errors.emplace_back( ss.str() ); |
---|
| 482 | goto nextResn; |
---|
| 483 | } |
---|
[b408364] | 484 | // sort by cost for overall pruning |
---|
[b3bd772] | 485 | CandidateCost coster{ resn.indexer }; |
---|
| 486 | std::sort( compatible.begin(), compatible.end(), coster ); |
---|
| 487 | |
---|
| 488 | for ( auto& compat : compatible ) { |
---|
[6d6e829] | 489 | ResnState new_resn = resn; |
---|
| 490 | |
---|
| 491 | // add compatible assertions to new resolution state |
---|
| 492 | for ( DeferRef r : compat.assns ) { |
---|
| 493 | AssnCandidate match = r.match; |
---|
| 494 | addToIndexer( match.have, new_resn.indexer ); |
---|
| 495 | new_resn.newNeed.insert( match.need.begin(), match.need.end() ); |
---|
| 496 | |
---|
[052cd71] | 497 | bindAssertion( r.decl, r.info, new_resn.alt, match, new_resn.inferred ); |
---|
[6d6e829] | 498 | } |
---|
| 499 | |
---|
| 500 | // set mutual environment into resolution state |
---|
| 501 | new_resn.alt.env = std::move(compat.env); |
---|
| 502 | new_resn.alt.openVars = std::move(compat.openVars); |
---|
| 503 | |
---|
[b408364] | 504 | // mark cost of this path |
---|
| 505 | new_resn.costs.back() += compat.cost; |
---|
| 506 | |
---|
[6d6e829] | 507 | // either add sucessful match or push back next state |
---|
| 508 | if ( new_resn.newNeed.empty() ) { |
---|
[b408364] | 509 | finalizeAssertions( new_resn, thresholds, out ); |
---|
[6d6e829] | 510 | } else { |
---|
| 511 | new_resns.emplace_back( std::move(new_resn), IterateState ); |
---|
| 512 | } |
---|
| 513 | } |
---|
| 514 | } |
---|
| 515 | nextResn:; } |
---|
| 516 | |
---|
| 517 | // finish or reset for next round |
---|
| 518 | if ( new_resns.empty() ) return; |
---|
| 519 | resns.swap( new_resns ); |
---|
| 520 | new_resns.clear(); |
---|
| 521 | } |
---|
[aaeacf4] | 522 | |
---|
[6d6e829] | 523 | // exceeded recursion limit if reaches here |
---|
| 524 | if ( out.empty() ) { |
---|
| 525 | SemanticError( alt.expr->location, "Too many recursive assertions" ); |
---|
| 526 | } |
---|
| 527 | } |
---|
| 528 | } // namespace ResolvExpr |
---|
| 529 | |
---|
| 530 | // Local Variables: // |
---|
| 531 | // tab-width: 4 // |
---|
| 532 | // mode: c++ // |
---|
| 533 | // compile-command: "make install" // |
---|
| 534 | // End: // |
---|