- File:
-
- 1 edited
-
src/ResolvExpr/ResolveAssertions.cc (modified) (12 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/ResolvExpr/ResolveAssertions.cc
r493a992 r4d2d45f9 35 35 #include "SynTree/Expression.h" // for InferredParams 36 36 #include "TypeEnvironment.h" // for TypeEnvironment, etc. 37 #include "typeops.h" // for adjustExprType , specCost37 #include "typeops.h" // for adjustExprType 38 38 #include "Unify.h" // for unify 39 39 … … 58 58 using CandidateList = std::vector<AssnCandidate>; 59 59 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 60 79 /// Reference to single deferred item 61 80 struct DeferRef { 62 const DeclarationWithType* decl; 63 const AssertionSetValue& info; 81 const AssnCacheItem& item; 64 82 const AssnCandidate& match; 65 83 }; … … 68 86 /// Acts like indexed list of DeferRef 69 87 struct DeferItem { 70 const DeclarationWithType* decl; 71 const AssertionSetValue& info; 72 CandidateList matches; 73 74 DeferItem( DeclarationWithType* decl, const AssertionSetValue& info, CandidateList&& matches ) 75 : decl(decl), info(info), matches(std::move(matches)) {} 76 77 bool empty() const { return matches.empty(); } 78 79 CandidateList::size_type size() const { return matches.size(); } 80 81 DeferRef operator[] ( unsigned i ) const { return { decl, info, matches[i] }; } 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) {} 92 93 bool empty() const { return cache->at(key).matches.empty(); } 94 95 CandidateList::size_type size() const { return cache->at(key).matches.size(); } 96 97 DeferRef operator[] ( unsigned i ) const { 98 const AssnCacheItem& item = cache->at(key); 99 return { item, item.matches[i] }; 100 } 101 102 const DeclarationWithType* get_decl() const { return cache->at(key).deferIds[0].decl; } 103 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; } 82 109 }; 83 110 … … 154 181 for ( const auto& assn : x.assns ) { 155 182 k += computeConversionCost( 156 assn.match.adjType, assn.decl->get_type(), indexer, x.env ); 157 158 // mark vars+specialization cost on function-type assertions 159 PointerType* ptr = dynamic_cast< PointerType* >( assn.decl->get_type() ); 160 if ( ! ptr ) continue; 161 FunctionType* func = dynamic_cast< FunctionType* >( ptr->base ); 162 if ( ! func ) continue; 163 164 for ( DeclarationWithType* formal : func->parameters ) { 165 k.decSpec( specCost( formal->get_type() ) ); 166 } 167 k.incVar( func->forall.size() ); 168 for ( TypeDecl* td : func->forall ) { 169 k.decSpec( td->assertions.size() ); 170 } 183 assn.match.adjType, assn.item.deferIds[0].decl->get_type(), indexer, 184 x.env ); 171 185 } 172 186 it = cache.emplace_hint( it, &x, k ); … … 239 253 240 254 /// Resolve a single assertion, in context 241 bool resolveAssertion( AssertionItem& assn, ResnState& resn ) {255 bool resolveAssertion( AssertionItem& assn, ResnState& resn, AssnCache& cache ) { 242 256 // skip unused assertions 243 257 if ( ! assn.info.isUsed ) return true; 244 258 245 // lookup candidates for this assertion 246 std::list< SymTab::Indexer::IdData > candidates; 247 resn.indexer.lookupId( assn.decl->name, candidates ); 248 249 // find the candidates that unify with the desired type 250 CandidateList matches; 251 for ( const auto& cdata : candidates ) { 252 DeclarationWithType* candidate = cdata.id; 253 254 // build independent unification context for candidate 255 AssertionSet have, newNeed; 256 TypeEnvironment newEnv{ resn.alt.env }; 257 OpenVarSet newOpenVars{ resn.alt.openVars }; 258 Type* adjType = candidate->get_type()->clone(); 259 adjustExprType( adjType, newEnv, resn.indexer ); 260 renameTyVars( adjType ); 261 262 // keep unifying candidates 263 if ( unify( assn.decl->get_type(), adjType, newEnv, newNeed, have, newOpenVars, 264 resn.indexer ) ) { 265 // set up binding slot for recursive assertions 266 UniqueId crntResnSlot = 0; 267 if ( ! newNeed.empty() ) { 268 crntResnSlot = ++globalResnSlot; 269 for ( auto& a : newNeed ) { 270 a.second.resnSlot = crntResnSlot; 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 } 271 292 } 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;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; 298 } 278 299 } 279 } 300 301 it = cache.emplace_hint( it, assnKey, AssnCacheItem{ std::move(matches) } ); 302 } 303 304 CandidateList& matches = it->second.matches; 280 305 281 306 // break if no suitable assertion … … 284 309 // defer if too many suitable assertions 285 310 if ( matches.size() > 1 ) { 286 resn.deferred.emplace_back( assn.decl, assn.info, std::move(matches) ); 311 it->second.deferIds.emplace_back( assn.decl, assn.info ); 312 resn.deferred.emplace_back( cache, assnKey ); 287 313 return true; 288 314 } … … 292 318 addToIndexer( match.have, resn.indexer ); 293 319 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);320 resn.alt.env = match.env; 321 resn.alt.openVars = match.openVars; 296 322 297 323 bindAssertion( assn.decl, assn.info, resn.alt, match, resn.inferred ); … … 354 380 ResnList resns{ ResnState{ alt, root_indexer } }; 355 381 ResnList new_resns{}; 382 AssnCache assnCache; 356 383 357 384 // resolve assertions in breadth-first-order up to a limited number of levels deep … … 362 389 for ( auto& assn : resn.need ) { 363 390 // fail early if any assertion is not resolvable 364 if ( ! resolveAssertion( assn, resn ) ) {391 if ( ! resolveAssertion( assn, resn, assnCache ) ) { 365 392 Indenter tabs{ Indenter::tabsize, 3 }; 366 393 std::ostringstream ss; … … 383 410 } 384 411 } else { 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() ); 385 416 // resolve deferred assertions by mutual compatibility 386 417 std::vector<CandidateEnvMerger::OutType> compatible = filterCombos( … … 396 427 ++tabs; 397 428 for ( const auto& d : resn.deferred ) { 398 d. decl->print( ss, tabs );429 d.get_decl()->print( ss, tabs ); 399 430 } 400 431 … … 427 458 new_resn.newNeed.insert( match.need.begin(), match.need.end() ); 428 459 429 bindAssertion( r.decl, r.info, new_resn.alt, match, new_resn.inferred ); 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 } 430 465 } 431 466
Note:
See TracChangeset
for help on using the changeset viewer.