[396037d] | 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 | // SatisfyAssertions.cpp -- |
---|
| 8 | // |
---|
| 9 | // Author : Aaron B. Moss |
---|
| 10 | // Created On : Mon Jun 10 17:45:00 2019 |
---|
[cf32116] | 11 | // Last Modified By : Andrew Beach |
---|
| 12 | // Last Modified On : Tue Oct 1 13:56:00 2019 |
---|
| 13 | // Update Count : 2 |
---|
[396037d] | 14 | // |
---|
| 15 | |
---|
| 16 | #include "SatisfyAssertions.hpp" |
---|
| 17 | |
---|
[b69233ac] | 18 | #include <algorithm> |
---|
[396037d] | 19 | #include <cassert> |
---|
[b69233ac] | 20 | #include <sstream> |
---|
| 21 | #include <string> |
---|
| 22 | #include <unordered_map> |
---|
| 23 | #include <vector> |
---|
| 24 | |
---|
| 25 | #include "Candidate.hpp" |
---|
| 26 | #include "CandidateFinder.hpp" |
---|
| 27 | #include "Cost.h" |
---|
| 28 | #include "RenameVars.h" |
---|
| 29 | #include "typeops.h" |
---|
| 30 | #include "Unify.h" |
---|
| 31 | #include "AST/Decl.hpp" |
---|
| 32 | #include "AST/Expr.hpp" |
---|
| 33 | #include "AST/Node.hpp" |
---|
| 34 | #include "AST/Pass.hpp" |
---|
| 35 | #include "AST/Print.hpp" |
---|
| 36 | #include "AST/SymbolTable.hpp" |
---|
| 37 | #include "AST/TypeEnvironment.hpp" |
---|
[ef1da0e2] | 38 | #include "FindOpenVars.h" |
---|
[b69233ac] | 39 | #include "Common/FilterCombos.h" |
---|
| 40 | #include "Common/Indenter.h" |
---|
| 41 | #include "GenPoly/GenPoly.h" |
---|
| 42 | #include "SymTab/Mangler.h" |
---|
[396037d] | 43 | |
---|
| 44 | namespace ResolvExpr { |
---|
| 45 | |
---|
[b69233ac] | 46 | // in CandidateFinder.cpp; unique ID for assertion satisfaction |
---|
| 47 | extern UniqueId globalResnSlot; |
---|
| 48 | |
---|
| 49 | namespace { |
---|
| 50 | /// Post-unification assertion satisfaction candidate |
---|
| 51 | struct AssnCandidate { |
---|
| 52 | ast::SymbolTable::IdData cdata; ///< Satisfying declaration |
---|
| 53 | ast::ptr< ast::Type > adjType; ///< Satisfying type |
---|
| 54 | ast::TypeEnvironment env; ///< Post-unification environment |
---|
| 55 | ast::AssertionSet have; ///< Post-unification have-set |
---|
| 56 | ast::AssertionSet need; ///< Post-unification need-set |
---|
| 57 | ast::OpenVarSet open; ///< Post-unification open-var-set |
---|
| 58 | ast::UniqueId resnSlot; ///< Slot for any recursive assertion IDs |
---|
| 59 | |
---|
[aca6a54c] | 60 | AssnCandidate( |
---|
| 61 | const ast::SymbolTable::IdData c, const ast::Type * at, ast::TypeEnvironment && e, |
---|
[b69233ac] | 62 | ast::AssertionSet && h, ast::AssertionSet && n, ast::OpenVarSet && o, ast::UniqueId rs ) |
---|
[aca6a54c] | 63 | : cdata( c ), adjType( at ), env( std::move( e ) ), have( std::move( h ) ), |
---|
[b69233ac] | 64 | need( std::move( n ) ), open( std::move( o ) ), resnSlot( rs ) {} |
---|
| 65 | }; |
---|
| 66 | |
---|
| 67 | /// List of assertion satisfaction candidates |
---|
| 68 | using AssnCandidateList = std::vector< AssnCandidate >; |
---|
| 69 | |
---|
| 70 | /// Reference to a single deferred item |
---|
| 71 | struct DeferRef { |
---|
[3e5dd913] | 72 | const ast::VariableExpr * expr; |
---|
[b69233ac] | 73 | const ast::AssertionSetValue & info; |
---|
| 74 | const AssnCandidate & match; |
---|
| 75 | }; |
---|
[aca6a54c] | 76 | |
---|
| 77 | /// Wrapper for the deferred items from a single assertion satisfaction. |
---|
[b69233ac] | 78 | /// Acts like an indexed list of DeferRef |
---|
| 79 | struct DeferItem { |
---|
[3e5dd913] | 80 | const ast::VariableExpr * expr; |
---|
[b69233ac] | 81 | const ast::AssertionSetValue & info; |
---|
| 82 | AssnCandidateList matches; |
---|
| 83 | |
---|
[aca6a54c] | 84 | DeferItem( |
---|
[3e5dd913] | 85 | const ast::VariableExpr * d, const ast::AssertionSetValue & i, AssnCandidateList && ms ) |
---|
| 86 | : expr( d ), info( i ), matches( std::move( ms ) ) {} |
---|
[b69233ac] | 87 | |
---|
| 88 | bool empty() const { return matches.empty(); } |
---|
| 89 | |
---|
| 90 | AssnCandidateList::size_type size() const { return matches.size(); } |
---|
| 91 | |
---|
[3e5dd913] | 92 | DeferRef operator[] ( unsigned i ) const { return { expr, info, matches[i] }; } |
---|
[b69233ac] | 93 | }; |
---|
| 94 | |
---|
| 95 | /// List of deferred satisfaction items |
---|
| 96 | using DeferList = std::vector< DeferItem >; |
---|
| 97 | |
---|
| 98 | /// Set of assertion satisfactions, grouped by resolution ID |
---|
| 99 | using InferCache = std::unordered_map< ast::UniqueId, ast::InferredParams >; |
---|
| 100 | |
---|
| 101 | /// Lexicographically-ordered vector of costs. |
---|
| 102 | /// Lexicographic order comes from default operator< on std::vector. |
---|
| 103 | using CostVec = std::vector< Cost >; |
---|
| 104 | |
---|
| 105 | /// Flag for state iteration |
---|
| 106 | enum IterateFlag { IterateState }; |
---|
| 107 | |
---|
| 108 | /// Intermediate state for satisfying a set of assertions |
---|
| 109 | struct SatState { |
---|
| 110 | CandidateRef cand; ///< Candidate assertion is rooted on |
---|
| 111 | ast::AssertionList need; ///< Assertions to find |
---|
| 112 | ast::AssertionSet newNeed; ///< Recursive assertions from current satisfied assertions |
---|
| 113 | DeferList deferred; ///< Deferred matches |
---|
| 114 | InferCache inferred; ///< Cache of already-inferred assertions |
---|
| 115 | CostVec costs; ///< Disambiguating costs of recursive assertion satisfaction |
---|
| 116 | ast::SymbolTable symtab; ///< Name lookup (depends on previous assertions) |
---|
| 117 | |
---|
| 118 | /// Initial satisfaction state for a candidate |
---|
| 119 | SatState( CandidateRef & c, const ast::SymbolTable & syms ) |
---|
[aca6a54c] | 120 | : cand( c ), need(), newNeed(), deferred(), inferred(), costs{ Cost::zero }, |
---|
[b69233ac] | 121 | symtab( syms ) { need.swap( c->need ); } |
---|
[aca6a54c] | 122 | |
---|
[b69233ac] | 123 | /// Update satisfaction state for next step after previous state |
---|
| 124 | SatState( SatState && o, IterateFlag ) |
---|
[aca6a54c] | 125 | : cand( std::move( o.cand ) ), need( o.newNeed.begin(), o.newNeed.end() ), newNeed(), |
---|
| 126 | deferred(), inferred( std::move( o.inferred ) ), costs( std::move( o.costs ) ), |
---|
[b69233ac] | 127 | symtab( o.symtab ) { costs.emplace_back( Cost::zero ); } |
---|
[aca6a54c] | 128 | |
---|
[b69233ac] | 129 | /// Field-wise next step constructor |
---|
| 130 | SatState( |
---|
[aca6a54c] | 131 | CandidateRef && c, ast::AssertionSet && nn, InferCache && i, CostVec && cs, |
---|
[b69233ac] | 132 | ast::SymbolTable && syms ) |
---|
[aca6a54c] | 133 | : cand( std::move( c ) ), need( nn.begin(), nn.end() ), newNeed(), deferred(), |
---|
[b69233ac] | 134 | inferred( std::move( i ) ), costs( std::move( cs ) ), symtab( std::move( syms ) ) |
---|
| 135 | { costs.emplace_back( Cost::zero ); } |
---|
| 136 | }; |
---|
| 137 | |
---|
| 138 | /// Adds a captured assertion to the symbol table |
---|
| 139 | void addToSymbolTable( const ast::AssertionSet & have, ast::SymbolTable & symtab ) { |
---|
| 140 | for ( auto & i : have ) { |
---|
[3e5dd913] | 141 | if ( i.second.isUsed ) { symtab.addId( i.first->var ); } |
---|
[b69233ac] | 142 | } |
---|
| 143 | } |
---|
| 144 | |
---|
| 145 | /// Binds a single assertion, updating satisfaction state |
---|
[aca6a54c] | 146 | void bindAssertion( |
---|
[eef8dfb] | 147 | const ast::VariableExpr * expr, const ast::AssertionSetValue & info, CandidateRef & cand, |
---|
[b69233ac] | 148 | AssnCandidate & match, InferCache & inferred |
---|
| 149 | ) { |
---|
| 150 | const ast::DeclWithType * candidate = match.cdata.id; |
---|
[aca6a54c] | 151 | assertf( candidate->uniqueId, |
---|
[b69233ac] | 152 | "Assertion candidate does not have a unique ID: %s", toString( candidate ).c_str() ); |
---|
[aca6a54c] | 153 | |
---|
[b69233ac] | 154 | ast::Expr * varExpr = match.cdata.combine( cand->expr->location, cand->cvtCost ); |
---|
| 155 | varExpr->result = match.adjType; |
---|
| 156 | if ( match.resnSlot ) { varExpr->inferred.resnSlots().emplace_back( match.resnSlot ); } |
---|
| 157 | |
---|
| 158 | // place newly-inferred assertion in proper location in cache |
---|
[3e5dd913] | 159 | inferred[ info.resnSlot ][ expr->var->uniqueId ] = ast::ParamEntry{ |
---|
| 160 | candidate->uniqueId, candidate, match.adjType, expr->result, varExpr }; |
---|
[b69233ac] | 161 | } |
---|
| 162 | |
---|
| 163 | /// Satisfy a single assertion |
---|
[ef1da0e2] | 164 | bool satisfyAssertion( ast::AssertionList::value_type & assn, SatState & sat, bool allowConversion = false, bool skipUnbound = false) { |
---|
[b69233ac] | 165 | // skip unused assertions |
---|
| 166 | if ( ! assn.second.isUsed ) return true; |
---|
| 167 | |
---|
| 168 | // find candidates that unify with the desired type |
---|
| 169 | AssnCandidateList matches; |
---|
[e5c3811] | 170 | |
---|
| 171 | std::vector<ast::SymbolTable::IdData> candidates; |
---|
[3e5dd913] | 172 | auto kind = ast::SymbolTable::getSpecialFunctionKind(assn.first->var->name); |
---|
[e5c3811] | 173 | if (kind != ast::SymbolTable::SpecialFunctionKind::NUMBER_OF_KINDS) { |
---|
| 174 | // prefilter special decls by argument type, if already known |
---|
[3e5dd913] | 175 | ast::ptr<ast::Type> thisArgType = assn.first->result.strict_as<ast::PointerType>()->base |
---|
[e5c3811] | 176 | .strict_as<ast::FunctionType>()->params[0] |
---|
| 177 | .strict_as<ast::ReferenceType>()->base; |
---|
| 178 | sat.cand->env.apply(thisArgType); |
---|
| 179 | |
---|
| 180 | std::string otypeKey = ""; |
---|
| 181 | if (thisArgType.as<ast::PointerType>()) otypeKey = Mangle::Encoding::pointer; |
---|
| 182 | else if (!isUnboundType(thisArgType)) otypeKey = Mangle::mangle(thisArgType, Mangle::Type | Mangle::NoGenericParams); |
---|
[ef1da0e2] | 183 | else if (skipUnbound) return false; |
---|
[e5c3811] | 184 | |
---|
| 185 | candidates = sat.symtab.specialLookupId(kind, otypeKey); |
---|
| 186 | } |
---|
| 187 | else { |
---|
[3e5dd913] | 188 | candidates = sat.symtab.lookupId(assn.first->var->name); |
---|
[e5c3811] | 189 | } |
---|
| 190 | for ( const ast::SymbolTable::IdData & cdata : candidates ) { |
---|
[b69233ac] | 191 | const ast::DeclWithType * candidate = cdata.id; |
---|
| 192 | |
---|
[2fb35df] | 193 | // ignore deleted candidates. |
---|
| 194 | // NOTE: this behavior is different from main resolver. |
---|
| 195 | // further investigations might be needed to determine |
---|
| 196 | // if we should implement the same rule here |
---|
| 197 | // (i.e. error if unique best match is deleted) |
---|
[5b9a0ae] | 198 | if (candidate->isDeleted && candidate->linkage == ast::Linkage::AutoGen) continue; |
---|
[2fb35df] | 199 | |
---|
[b69233ac] | 200 | // build independent unification context for candidate |
---|
| 201 | ast::AssertionSet have, newNeed; |
---|
| 202 | ast::TypeEnvironment newEnv{ sat.cand->env }; |
---|
| 203 | ast::OpenVarSet newOpen{ sat.cand->open }; |
---|
[3e5dd913] | 204 | ast::ptr< ast::Type > toType = assn.first->result; |
---|
[aca6a54c] | 205 | ast::ptr< ast::Type > adjType = |
---|
[7583c02] | 206 | renameTyVars( adjustExprType( candidate->get_type(), newEnv, sat.symtab ), GEN_USAGE, false ); |
---|
[b69233ac] | 207 | |
---|
| 208 | // only keep candidates which unify |
---|
[ef1da0e2] | 209 | |
---|
| 210 | ast::OpenVarSet closed; |
---|
| 211 | findOpenVars( toType, newOpen, closed, newNeed, have, FirstClosed ); |
---|
| 212 | findOpenVars( adjType, newOpen, closed, newNeed, have, FirstOpen ); |
---|
| 213 | if ( allowConversion ) { |
---|
| 214 | if ( auto c = commonType( toType, adjType, newEnv, newNeed, have, newOpen, WidenMode {true, true}, sat.symtab ) ) { |
---|
| 215 | // set up binding slot for recursive assertions |
---|
| 216 | ast::UniqueId crntResnSlot = 0; |
---|
| 217 | if ( ! newNeed.empty() ) { |
---|
| 218 | crntResnSlot = ++globalResnSlot; |
---|
| 219 | for ( auto & a : newNeed ) { a.second.resnSlot = crntResnSlot; } |
---|
| 220 | } |
---|
| 221 | |
---|
| 222 | matches.emplace_back( |
---|
| 223 | cdata, adjType, std::move( newEnv ), std::move( have ), std::move( newNeed ), |
---|
| 224 | std::move( newOpen ), crntResnSlot ); |
---|
[b69233ac] | 225 | } |
---|
[ef1da0e2] | 226 | } |
---|
| 227 | else { |
---|
| 228 | if ( unifyExact( toType, adjType, newEnv, newNeed, have, newOpen, WidenMode {true, true}, sat.symtab ) ) { |
---|
| 229 | // set up binding slot for recursive assertions |
---|
| 230 | ast::UniqueId crntResnSlot = 0; |
---|
| 231 | if ( ! newNeed.empty() ) { |
---|
| 232 | crntResnSlot = ++globalResnSlot; |
---|
| 233 | for ( auto & a : newNeed ) { a.second.resnSlot = crntResnSlot; } |
---|
| 234 | } |
---|
[b69233ac] | 235 | |
---|
[ef1da0e2] | 236 | matches.emplace_back( |
---|
| 237 | cdata, adjType, std::move( newEnv ), std::move( have ), std::move( newNeed ), |
---|
| 238 | std::move( newOpen ), crntResnSlot ); |
---|
| 239 | } |
---|
[b69233ac] | 240 | } |
---|
| 241 | } |
---|
| 242 | |
---|
| 243 | // break if no satisfying match |
---|
| 244 | if ( matches.empty() ) return false; |
---|
| 245 | |
---|
| 246 | // defer if too many satisfying matches |
---|
| 247 | if ( matches.size() > 1 ) { |
---|
| 248 | sat.deferred.emplace_back( assn.first, assn.second, std::move( matches ) ); |
---|
| 249 | return true; |
---|
| 250 | } |
---|
| 251 | |
---|
| 252 | // otherwise bind unique match in ongoing scope |
---|
| 253 | AssnCandidate & match = matches.front(); |
---|
| 254 | addToSymbolTable( match.have, sat.symtab ); |
---|
| 255 | sat.newNeed.insert( match.need.begin(), match.need.end() ); |
---|
| 256 | sat.cand->env = std::move( match.env ); |
---|
| 257 | sat.cand->open = std::move( match.open ); |
---|
| 258 | |
---|
| 259 | bindAssertion( assn.first, assn.second, sat.cand, match, sat.inferred ); |
---|
| 260 | return true; |
---|
| 261 | } |
---|
| 262 | |
---|
| 263 | /// Map of candidate return types to recursive assertion satisfaction costs |
---|
| 264 | using PruneMap = std::unordered_map< std::string, CostVec >; |
---|
| 265 | |
---|
| 266 | /// Gets the pruning key for a candidate (derived from environment-adjusted return type) |
---|
| 267 | std::string pruneKey( const Candidate & cand ) { |
---|
| 268 | ast::ptr< ast::Type > resType = cand.expr->result; |
---|
| 269 | cand.env.apply( resType ); |
---|
| 270 | return Mangle::mangle( resType, Mangle::typeMode() ); |
---|
| 271 | } |
---|
| 272 | |
---|
| 273 | /// Associates inferred parameters with an expression |
---|
| 274 | struct InferMatcher final { |
---|
| 275 | InferCache & inferred; |
---|
| 276 | |
---|
| 277 | InferMatcher( InferCache & inferred ) : inferred( inferred ) {} |
---|
| 278 | |
---|
[81da70a5] | 279 | const ast::Expr * postvisit( const ast::Expr * expr ) { |
---|
[b69233ac] | 280 | // Skip if no slots to find |
---|
[07d867b] | 281 | if ( !expr->inferred.hasSlots() ) return expr; |
---|
| 282 | // if ( expr->inferred.mode != ast::Expr::InferUnion::Slots ) return expr; |
---|
| 283 | std::vector<UniqueId> missingSlots; |
---|
[b69233ac] | 284 | // find inferred parameters for resolution slots |
---|
[07d867b] | 285 | ast::InferredParams * newInferred = new ast::InferredParams(); |
---|
[b69233ac] | 286 | for ( UniqueId slot : expr->inferred.resnSlots() ) { |
---|
| 287 | // fail if no matching assertions found |
---|
| 288 | auto it = inferred.find( slot ); |
---|
| 289 | if ( it == inferred.end() ) { |
---|
[cdacb73] | 290 | // std::cerr << "missing assertion " << slot << std::endl; |
---|
[07d867b] | 291 | missingSlots.push_back(slot); |
---|
[81da70a5] | 292 | continue; |
---|
[b69233ac] | 293 | } |
---|
| 294 | |
---|
| 295 | // place inferred parameters into new map |
---|
| 296 | for ( auto & entry : it->second ) { |
---|
| 297 | // recurse on inferParams of resolved expressions |
---|
[81da70a5] | 298 | entry.second.expr = postvisit( entry.second.expr ); |
---|
[07d867b] | 299 | auto res = newInferred->emplace( entry ); |
---|
[b69233ac] | 300 | assert( res.second && "all assertions newly placed" ); |
---|
| 301 | } |
---|
| 302 | } |
---|
| 303 | |
---|
| 304 | ast::Expr * ret = mutate( expr ); |
---|
[07d867b] | 305 | ret->inferred.set_inferParams( newInferred ); |
---|
| 306 | if (!missingSlots.empty()) ret->inferred.resnSlots() = missingSlots; |
---|
[b69233ac] | 307 | return ret; |
---|
| 308 | } |
---|
| 309 | }; |
---|
| 310 | |
---|
[aca6a54c] | 311 | /// Replace ResnSlots with InferParams and add alternative to output list, if it meets pruning |
---|
[b69233ac] | 312 | /// threshold. |
---|
[aca6a54c] | 313 | void finalizeAssertions( |
---|
| 314 | CandidateRef & cand, InferCache & inferred, PruneMap & thresholds, CostVec && costs, |
---|
| 315 | CandidateList & out |
---|
[b69233ac] | 316 | ) { |
---|
| 317 | // prune if cheaper alternative for same key has already been generated |
---|
| 318 | std::string key = pruneKey( *cand ); |
---|
| 319 | auto it = thresholds.find( key ); |
---|
| 320 | if ( it != thresholds.end() ) { |
---|
| 321 | if ( it->second < costs ) return; |
---|
| 322 | } else { |
---|
| 323 | thresholds.emplace_hint( it, key, std::move( costs ) ); |
---|
| 324 | } |
---|
| 325 | |
---|
| 326 | // replace resolution slots with inferred parameters, add to output |
---|
| 327 | ast::Pass< InferMatcher > matcher{ inferred }; |
---|
| 328 | cand->expr = cand->expr->accept( matcher ); |
---|
| 329 | out.emplace_back( cand ); |
---|
| 330 | } |
---|
| 331 | |
---|
[aca6a54c] | 332 | /// Combo iterator that combines candidates into an output list, merging their environments. |
---|
| 333 | /// Rejects an appended candidate if environments cannot be merged. See `Common/FilterCombos.h` |
---|
[b69233ac] | 334 | /// for description of "combo iterator". |
---|
| 335 | class CandidateEnvMerger { |
---|
| 336 | /// Current list of merged candidates |
---|
| 337 | std::vector< DeferRef > crnt; |
---|
| 338 | /// Stack of environments to support backtracking |
---|
| 339 | std::vector< ast::TypeEnvironment > envs; |
---|
| 340 | /// Stack of open variables to support backtracking |
---|
| 341 | std::vector< ast::OpenVarSet > opens; |
---|
| 342 | /// Symbol table to use for merges |
---|
| 343 | const ast::SymbolTable & symtab; |
---|
| 344 | |
---|
| 345 | public: |
---|
| 346 | /// The merged environment/open variables and the list of candidates |
---|
| 347 | struct OutType { |
---|
| 348 | ast::TypeEnvironment env; |
---|
| 349 | ast::OpenVarSet open; |
---|
| 350 | std::vector< DeferRef > assns; |
---|
| 351 | Cost cost; |
---|
| 352 | |
---|
[aca6a54c] | 353 | OutType( |
---|
| 354 | const ast::TypeEnvironment & e, const ast::OpenVarSet & o, |
---|
[b69233ac] | 355 | const std::vector< DeferRef > & as, const ast::SymbolTable & symtab ) |
---|
| 356 | : env( e ), open( o ), assns( as ), cost( Cost::zero ) { |
---|
| 357 | // compute combined conversion cost |
---|
| 358 | for ( const DeferRef & assn : assns ) { |
---|
| 359 | // compute conversion cost from satisfying decl to assertion |
---|
[aca6a54c] | 360 | cost += computeConversionCost( |
---|
[3e5dd913] | 361 | assn.match.adjType, assn.expr->result, false, symtab, env ); |
---|
[aca6a54c] | 362 | |
---|
[b69233ac] | 363 | // mark vars+specialization on function-type assertions |
---|
[aca6a54c] | 364 | const ast::FunctionType * func = |
---|
[b69233ac] | 365 | GenPoly::getFunctionType( assn.match.cdata.id->get_type() ); |
---|
| 366 | if ( ! func ) continue; |
---|
| 367 | |
---|
[954c954] | 368 | for ( const auto & param : func->params ) { |
---|
| 369 | cost.decSpec( specCost( param ) ); |
---|
[b69233ac] | 370 | } |
---|
[aca6a54c] | 371 | |
---|
[b69233ac] | 372 | cost.incVar( func->forall.size() ); |
---|
[aca6a54c] | 373 | |
---|
[3e5dd913] | 374 | cost.decSpec( func->assertions.size() ); |
---|
[b69233ac] | 375 | } |
---|
| 376 | } |
---|
| 377 | |
---|
| 378 | bool operator< ( const OutType & o ) const { return cost < o.cost; } |
---|
| 379 | }; |
---|
| 380 | |
---|
[aca6a54c] | 381 | CandidateEnvMerger( |
---|
| 382 | const ast::TypeEnvironment & env, const ast::OpenVarSet & open, |
---|
[b69233ac] | 383 | const ast::SymbolTable & syms ) |
---|
| 384 | : crnt(), envs{ env }, opens{ open }, symtab( syms ) {} |
---|
| 385 | |
---|
| 386 | bool append( DeferRef i ) { |
---|
| 387 | ast::TypeEnvironment env = envs.back(); |
---|
| 388 | ast::OpenVarSet open = opens.back(); |
---|
| 389 | mergeOpenVars( open, i.match.open ); |
---|
| 390 | |
---|
| 391 | if ( ! env.combine( i.match.env, open, symtab ) ) return false; |
---|
| 392 | |
---|
| 393 | crnt.emplace_back( i ); |
---|
| 394 | envs.emplace_back( std::move( env ) ); |
---|
| 395 | opens.emplace_back( std::move( open ) ); |
---|
| 396 | return true; |
---|
| 397 | } |
---|
| 398 | |
---|
| 399 | void backtrack() { |
---|
| 400 | crnt.pop_back(); |
---|
| 401 | envs.pop_back(); |
---|
| 402 | opens.pop_back(); |
---|
| 403 | } |
---|
| 404 | |
---|
| 405 | OutType finalize() { return { envs.back(), opens.back(), crnt, symtab }; } |
---|
| 406 | }; |
---|
| 407 | |
---|
| 408 | /// Limit to depth of recursion of assertion satisfaction |
---|
[1958fec] | 409 | static const int recursionLimit = 8; |
---|
[b69233ac] | 410 | /// Maximum number of simultaneously-deferred assertions to attempt concurrent satisfaction of |
---|
| 411 | static const int deferLimit = 10; |
---|
| 412 | } // anonymous namespace |
---|
| 413 | |
---|
[aca6a54c] | 414 | void satisfyAssertions( |
---|
| 415 | CandidateRef & cand, const ast::SymbolTable & symtab, CandidateList & out, |
---|
[396037d] | 416 | std::vector<std::string> & errors |
---|
| 417 | ) { |
---|
[b69233ac] | 418 | // finish early if no assertions to satisfy |
---|
| 419 | if ( cand->need.empty() ) { |
---|
| 420 | out.emplace_back( cand ); |
---|
| 421 | return; |
---|
| 422 | } |
---|
| 423 | |
---|
| 424 | // build list of possible combinations of satisfying declarations |
---|
| 425 | std::vector< SatState > sats{ SatState{ cand, symtab } }; |
---|
| 426 | std::vector< SatState > nextSats{}; |
---|
| 427 | |
---|
| 428 | // pruning thresholds by result type of output candidates. |
---|
| 429 | // Candidates *should* be generated in sorted order, so no need to retroactively prune |
---|
| 430 | PruneMap thresholds; |
---|
| 431 | |
---|
| 432 | // satisfy assertions in breadth-first order over the recursion tree of assertion satisfaction. |
---|
| 433 | // Stop recursion at a limited number of levels deep to avoid infinite loops. |
---|
| 434 | for ( unsigned level = 0; level < recursionLimit; ++level ) { |
---|
| 435 | // for each current mutually-compatible set of assertions |
---|
| 436 | for ( SatState & sat : sats ) { |
---|
[ef1da0e2] | 437 | bool allowConversion = false; |
---|
[b69233ac] | 438 | // stop this branch if a better option is already found |
---|
| 439 | auto it = thresholds.find( pruneKey( *sat.cand ) ); |
---|
| 440 | if ( it != thresholds.end() && it->second < sat.costs ) goto nextSat; |
---|
| 441 | |
---|
[7583c02] | 442 | // should a limit be imposed? worst case here is O(n^2) but very unlikely to happen. |
---|
[ef1da0e2] | 443 | |
---|
[eef8dfb] | 444 | for (unsigned resetCount = 0; ; ++resetCount) { |
---|
[7583c02] | 445 | ast::AssertionList next; |
---|
| 446 | resetTyVarRenaming(); |
---|
| 447 | // make initial pass at matching assertions |
---|
| 448 | for ( auto & assn : sat.need ) { |
---|
| 449 | // fail early if any assertion is not satisfiable |
---|
[ef1da0e2] | 450 | if ( ! satisfyAssertion( assn, sat, allowConversion, !next.empty() ) ) { |
---|
[7583c02] | 451 | next.emplace_back(assn); |
---|
| 452 | // goto nextSat; |
---|
| 453 | } |
---|
| 454 | } |
---|
| 455 | // success |
---|
| 456 | if (next.empty()) break; |
---|
| 457 | // fail if nothing resolves |
---|
| 458 | else if (next.size() == sat.need.size()) { |
---|
[ef1da0e2] | 459 | if (allowConversion) { |
---|
| 460 | Indenter tabs{ 3 }; |
---|
| 461 | std::ostringstream ss; |
---|
| 462 | ss << tabs << "Unsatisfiable alternative:\n"; |
---|
| 463 | print( ss, *sat.cand, ++tabs ); |
---|
| 464 | ss << (tabs-1) << "Could not satisfy assertion:\n"; |
---|
| 465 | ast::print( ss, next[0].first, tabs ); |
---|
| 466 | |
---|
| 467 | errors.emplace_back( ss.str() ); |
---|
| 468 | goto nextSat; |
---|
| 469 | } |
---|
| 470 | |
---|
| 471 | else { |
---|
| 472 | allowConversion = true; |
---|
| 473 | continue; |
---|
| 474 | } |
---|
[b69233ac] | 475 | } |
---|
[ef1da0e2] | 476 | allowConversion = false; |
---|
[7583c02] | 477 | sat.need = std::move(next); |
---|
[b69233ac] | 478 | } |
---|
| 479 | |
---|
| 480 | if ( sat.deferred.empty() ) { |
---|
| 481 | // either add successful match or push back next state |
---|
| 482 | if ( sat.newNeed.empty() ) { |
---|
[aca6a54c] | 483 | finalizeAssertions( |
---|
[b69233ac] | 484 | sat.cand, sat.inferred, thresholds, std::move( sat.costs ), out ); |
---|
| 485 | } else { |
---|
| 486 | nextSats.emplace_back( std::move( sat ), IterateState ); |
---|
| 487 | } |
---|
| 488 | } else if ( sat.deferred.size() > deferLimit ) { |
---|
| 489 | // too many deferred assertions to attempt mutual compatibility |
---|
| 490 | Indenter tabs{ 3 }; |
---|
| 491 | std::ostringstream ss; |
---|
| 492 | ss << tabs << "Unsatisfiable alternative:\n"; |
---|
| 493 | print( ss, *sat.cand, ++tabs ); |
---|
| 494 | ss << (tabs-1) << "Too many non-unique satisfying assignments for assertions:\n"; |
---|
| 495 | for ( const auto & d : sat.deferred ) { |
---|
[3e5dd913] | 496 | ast::print( ss, d.expr, tabs ); |
---|
[b69233ac] | 497 | } |
---|
| 498 | |
---|
| 499 | errors.emplace_back( ss.str() ); |
---|
| 500 | goto nextSat; |
---|
| 501 | } else { |
---|
| 502 | // combine deferred assertions by mutual compatibility |
---|
| 503 | std::vector< CandidateEnvMerger::OutType > compatible = filterCombos( |
---|
| 504 | sat.deferred, CandidateEnvMerger{ sat.cand->env, sat.cand->open, sat.symtab } ); |
---|
[aca6a54c] | 505 | |
---|
[b69233ac] | 506 | // fail early if no mutually-compatible assertion satisfaction |
---|
| 507 | if ( compatible.empty() ) { |
---|
| 508 | Indenter tabs{ 3 }; |
---|
| 509 | std::ostringstream ss; |
---|
| 510 | ss << tabs << "Unsatisfiable alternative:\n"; |
---|
| 511 | print( ss, *sat.cand, ++tabs ); |
---|
| 512 | ss << (tabs-1) << "No mutually-compatible satisfaction for assertions:\n"; |
---|
| 513 | for ( const auto& d : sat.deferred ) { |
---|
[3e5dd913] | 514 | ast::print( ss, d.expr, tabs ); |
---|
[b69233ac] | 515 | } |
---|
| 516 | |
---|
| 517 | errors.emplace_back( ss.str() ); |
---|
| 518 | goto nextSat; |
---|
| 519 | } |
---|
| 520 | |
---|
| 521 | // sort by cost (for overall pruning order) |
---|
| 522 | std::sort( compatible.begin(), compatible.end() ); |
---|
| 523 | |
---|
| 524 | // process mutually-compatible combinations |
---|
| 525 | for ( auto & compat : compatible ) { |
---|
| 526 | // set up next satisfaction state |
---|
| 527 | CandidateRef nextCand = std::make_shared<Candidate>( |
---|
[aca6a54c] | 528 | sat.cand->expr, std::move( compat.env ), std::move( compat.open ), |
---|
[b69233ac] | 529 | ast::AssertionSet{} /* need moved into satisfaction state */, |
---|
| 530 | sat.cand->cost, sat.cand->cvtCost ); |
---|
| 531 | |
---|
| 532 | ast::AssertionSet nextNewNeed{ sat.newNeed }; |
---|
| 533 | InferCache nextInferred{ sat.inferred }; |
---|
[aca6a54c] | 534 | |
---|
[b69233ac] | 535 | CostVec nextCosts{ sat.costs }; |
---|
| 536 | nextCosts.back() += compat.cost; |
---|
[aca6a54c] | 537 | |
---|
[b69233ac] | 538 | ast::SymbolTable nextSymtab{ sat.symtab }; |
---|
| 539 | |
---|
| 540 | // add compatible assertions to new satisfaction state |
---|
| 541 | for ( DeferRef r : compat.assns ) { |
---|
| 542 | AssnCandidate match = r.match; |
---|
| 543 | addToSymbolTable( match.have, nextSymtab ); |
---|
| 544 | nextNewNeed.insert( match.need.begin(), match.need.end() ); |
---|
| 545 | |
---|
[3e5dd913] | 546 | bindAssertion( r.expr, r.info, nextCand, match, nextInferred ); |
---|
[b69233ac] | 547 | } |
---|
| 548 | |
---|
| 549 | // either add successful match or push back next state |
---|
| 550 | if ( nextNewNeed.empty() ) { |
---|
[aca6a54c] | 551 | finalizeAssertions( |
---|
[b69233ac] | 552 | nextCand, nextInferred, thresholds, std::move( nextCosts ), out ); |
---|
| 553 | } else { |
---|
[aca6a54c] | 554 | nextSats.emplace_back( |
---|
| 555 | std::move( nextCand ), std::move( nextNewNeed ), |
---|
| 556 | std::move( nextInferred ), std::move( nextCosts ), |
---|
[b69233ac] | 557 | std::move( nextSymtab ) ); |
---|
| 558 | } |
---|
| 559 | } |
---|
| 560 | } |
---|
| 561 | nextSat:; } |
---|
| 562 | |
---|
| 563 | // finish or reset for next round |
---|
| 564 | if ( nextSats.empty() ) return; |
---|
| 565 | sats.swap( nextSats ); |
---|
| 566 | nextSats.clear(); |
---|
| 567 | } |
---|
[aca6a54c] | 568 | |
---|
[b69233ac] | 569 | // exceeded recursion limit if reaches here |
---|
| 570 | if ( out.empty() ) { |
---|
| 571 | SemanticError( cand->expr->location, "Too many recursive assertions" ); |
---|
| 572 | } |
---|
[396037d] | 573 | } |
---|
| 574 | |
---|
| 575 | } // namespace ResolvExpr |
---|
| 576 | |
---|
| 577 | // Local Variables: // |
---|
| 578 | // tab-width: 4 // |
---|
| 579 | // mode: c++ // |
---|
| 580 | // compile-command: "make install" // |
---|
[aca6a54c] | 581 | // End: // |
---|