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