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