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