| 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
|
|---|
| 20 | #include <unordered_map> // for unordered_map
|
|---|
| 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
|
|---|
| 26 | #include "Common/utility.h" // for sort_mins
|
|---|
| 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 |
|
|---|
| 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 |
|
|---|
| 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 | Expression* varExpr = match.cdata.combine( alt.cvtCost );
|
|---|
| 192 | delete varExpr->result;
|
|---|
| 193 | varExpr->result = match.adjType->clone();
|
|---|
| 194 |
|
|---|
| 195 | // follow the current assertion's ID chain to find the correct set of inferred parameters
|
|---|
| 196 | // to add the candidate o (i.e. the set of inferred parameters belonging to the entity
|
|---|
| 197 | // which requested the assertion parameter)
|
|---|
| 198 | InferredParams* inferParams = &alt.expr->inferParams;
|
|---|
| 199 | for ( UniqueId id : info.idChain ) {
|
|---|
| 200 | inferParams = (*inferParams)[ id ].inferParams.get();
|
|---|
| 201 | }
|
|---|
| 202 |
|
|---|
| 203 | (*inferParams)[ decl->get_uniqueId() ] = ParamEntry{
|
|---|
| 204 | candidate->get_uniqueId(), match.adjType, decl->get_type()->clone(), varExpr };
|
|---|
| 205 | }
|
|---|
| 206 |
|
|---|
| 207 | /// Adds a captured assertion to the symbol table
|
|---|
| 208 | void addToIndexer( AssertionSet &assertSet, SymTab::Indexer &indexer ) {
|
|---|
| 209 | for ( AssertionSet::iterator i = assertSet.begin(); i != assertSet.end(); ++i ) {
|
|---|
| 210 | if ( i->second.isUsed ) {
|
|---|
| 211 | indexer.addId( i->first );
|
|---|
| 212 | }
|
|---|
| 213 | }
|
|---|
| 214 | }
|
|---|
| 215 |
|
|---|
| 216 | /// Resolve a single assertion, in context
|
|---|
| 217 | bool resolveAssertion( AssertionItem& assn, ResnState& resn ) {
|
|---|
| 218 | // skip unused assertions
|
|---|
| 219 | if ( ! assn.info.isUsed ) return true;
|
|---|
| 220 |
|
|---|
| 221 | // lookup candidates for this assertion
|
|---|
| 222 | std::list< SymTab::Indexer::IdData > candidates;
|
|---|
| 223 | resn.indexer.lookupId( assn.decl->name, candidates );
|
|---|
| 224 |
|
|---|
| 225 | // find the candidates that unify with the desired type
|
|---|
| 226 | CandidateList matches;
|
|---|
| 227 | for ( const auto& cdata : candidates ) {
|
|---|
| 228 | DeclarationWithType* candidate = cdata.id;
|
|---|
| 229 |
|
|---|
| 230 | // build independent unification context for candidate
|
|---|
| 231 | AssertionSet have, newNeed;
|
|---|
| 232 | TypeEnvironment newEnv{ resn.alt.env };
|
|---|
| 233 | OpenVarSet newOpenVars{ resn.alt.openVars };
|
|---|
| 234 | Type* adjType = candidate->get_type()->clone();
|
|---|
| 235 | adjustExprType( adjType, newEnv, resn.indexer );
|
|---|
| 236 |
|
|---|
| 237 | // keep unifying candidates
|
|---|
| 238 | if ( unify( assn.decl->get_type(), adjType, newEnv, newNeed, have, newOpenVars,
|
|---|
| 239 | resn.indexer ) ) {
|
|---|
| 240 | // set up idChain on new assertions
|
|---|
| 241 | for ( auto& a : newNeed ) {
|
|---|
| 242 | a.second.idChain = assn.info.idChain;
|
|---|
| 243 | a.second.idChain.push_back( assn.decl->get_uniqueId() );
|
|---|
| 244 | }
|
|---|
| 245 |
|
|---|
| 246 | matches.emplace_back( cdata, adjType, std::move(newEnv), std::move(have),
|
|---|
| 247 | std::move(newNeed), std::move(newOpenVars) );
|
|---|
| 248 | } else {
|
|---|
| 249 | delete adjType;
|
|---|
| 250 | }
|
|---|
| 251 | }
|
|---|
| 252 |
|
|---|
| 253 | // break if no suitable assertion
|
|---|
| 254 | if ( matches.empty() ) return false;
|
|---|
| 255 |
|
|---|
| 256 | // defer if too many suitable assertions
|
|---|
| 257 | if ( matches.size() > 1 ) {
|
|---|
| 258 | resn.deferred.emplace_back( assn.decl, assn.info, std::move(matches) );
|
|---|
| 259 | return true;
|
|---|
| 260 | }
|
|---|
| 261 |
|
|---|
| 262 | // otherwise bind current match in ongoing scope
|
|---|
| 263 | AssnCandidate& match = matches.front();
|
|---|
| 264 | addToIndexer( match.have, resn.indexer );
|
|---|
| 265 | resn.newNeed.insert( match.need.begin(), match.need.end() );
|
|---|
| 266 | resn.alt.env = std::move(match.env);
|
|---|
| 267 | resn.alt.openVars = std::move(match.openVars);
|
|---|
| 268 |
|
|---|
| 269 | bindAssertion( assn.decl, assn.info, resn.alt, match );
|
|---|
| 270 | return true;
|
|---|
| 271 | }
|
|---|
| 272 |
|
|---|
| 273 | ///< Limit to depth of recursion of assertion satisfaction
|
|---|
| 274 | static const int recursionLimit = /* 10 */ 4;
|
|---|
| 275 |
|
|---|
| 276 | void resolveAssertions( Alternative& alt, const SymTab::Indexer& indexer, AltList& out ) {
|
|---|
| 277 | // finish early if no assertions to resolve
|
|---|
| 278 | if ( alt.need.empty() ) {
|
|---|
| 279 | out.emplace_back( alt );
|
|---|
| 280 | return;
|
|---|
| 281 | }
|
|---|
| 282 |
|
|---|
| 283 | // build list of possible resolutions
|
|---|
| 284 | using ResnList = std::vector<ResnState>;
|
|---|
| 285 | SymTab::Indexer root_indexer{ indexer };
|
|---|
| 286 | ResnList resns{ ResnState{ alt, root_indexer } };
|
|---|
| 287 | ResnList new_resns{};
|
|---|
| 288 |
|
|---|
| 289 | // resolve assertions in breadth-first-order up to a limited number of levels deep
|
|---|
| 290 | for ( unsigned level = 0; level < recursionLimit; ++level ) {
|
|---|
| 291 | // scan over all mutually-compatible resolutions
|
|---|
| 292 | for ( auto& resn : resns ) {
|
|---|
| 293 | // make initial pass at matching assertions
|
|---|
| 294 | for ( auto& assn : resn.need ) {
|
|---|
| 295 | // fail early if any assertion is not resolvable
|
|---|
| 296 | if ( ! resolveAssertion( assn, resn ) ) goto nextResn;
|
|---|
| 297 | }
|
|---|
| 298 |
|
|---|
| 299 | if ( resn.deferred.empty() ) {
|
|---|
| 300 | // either add successful match or push back next state
|
|---|
| 301 | if ( resn.newNeed.empty() ) {
|
|---|
| 302 | out.emplace_back( resn.alt );
|
|---|
| 303 | } else {
|
|---|
| 304 | new_resns.emplace_back( std::move(resn), IterateState );
|
|---|
| 305 | }
|
|---|
| 306 | } else {
|
|---|
| 307 | // resolve deferred assertions by mutual compatibility and sort by cost
|
|---|
| 308 | std::vector<CandidateEnvMerger::OutType> compatible = filterCombos(
|
|---|
| 309 | resn.deferred,
|
|---|
| 310 | CandidateEnvMerger{ resn.alt.env, resn.alt.openVars, resn.indexer } );
|
|---|
| 311 | auto lmin = sort_mins( compatible.begin(), compatible.end(),
|
|---|
| 312 | CandidateCost{resn.indexer} );
|
|---|
| 313 |
|
|---|
| 314 | for ( auto it = compatible.begin(); it != lmin ; ++it ) {
|
|---|
| 315 | auto& compat = *it;
|
|---|
| 316 | ResnState new_resn = resn;
|
|---|
| 317 |
|
|---|
| 318 | // add compatible assertions to new resolution state
|
|---|
| 319 | for ( DeferRef r : compat.assns ) {
|
|---|
| 320 | AssnCandidate match = r.match;
|
|---|
| 321 | addToIndexer( match.have, new_resn.indexer );
|
|---|
| 322 | new_resn.newNeed.insert( match.need.begin(), match.need.end() );
|
|---|
| 323 |
|
|---|
| 324 | bindAssertion( r.decl, r.info, new_resn.alt, match );
|
|---|
| 325 | }
|
|---|
| 326 |
|
|---|
| 327 | // set mutual environment into resolution state
|
|---|
| 328 | new_resn.alt.env = std::move(compat.env);
|
|---|
| 329 | new_resn.alt.openVars = std::move(compat.openVars);
|
|---|
| 330 |
|
|---|
| 331 | // either add sucessful match or push back next state
|
|---|
| 332 | if ( new_resn.newNeed.empty() ) {
|
|---|
| 333 | out.emplace_back( new_resn.alt );
|
|---|
| 334 | } else {
|
|---|
| 335 | new_resns.emplace_back( std::move(new_resn), IterateState );
|
|---|
| 336 | }
|
|---|
| 337 | }
|
|---|
| 338 | }
|
|---|
| 339 | nextResn:; }
|
|---|
| 340 |
|
|---|
| 341 | // finish or reset for next round
|
|---|
| 342 | if ( new_resns.empty() ) return;
|
|---|
| 343 | resns.swap( new_resns );
|
|---|
| 344 | new_resns.clear();
|
|---|
| 345 | }
|
|---|
| 346 |
|
|---|
| 347 | // exceeded recursion limit if reaches here
|
|---|
| 348 | if ( out.empty() ) {
|
|---|
| 349 | SemanticError( alt.expr->location, "Too many recursive assertions" );
|
|---|
| 350 | }
|
|---|
| 351 | }
|
|---|
| 352 | } // namespace ResolvExpr
|
|---|
| 353 |
|
|---|
| 354 | // Local Variables: //
|
|---|
| 355 | // tab-width: 4 //
|
|---|
| 356 | // mode: c++ //
|
|---|
| 357 | // compile-command: "make install" //
|
|---|
| 358 | // End: //
|
|---|