Changeset 9795142
- Timestamp:
- Apr 28, 2019, 6:50:19 PM (6 years ago)
- Branches:
- ADT, arm-eh, ast-experimental, cleanup-dtors, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, pthread-emulation, qualifiedEnum
- Children:
- 5fb17f1, b10c39a0
- Parents:
- c378e5e (diff), 052cd71 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)
links above to see all the changes relative to each parent. - Location:
- src
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
src/ResolvExpr/ResolveAssertions.cc
rc378e5e r9795142 35 35 #include "SynTree/Expression.h" // for InferredParams 36 36 #include "TypeEnvironment.h" // for TypeEnvironment, etc. 37 #include "typeops.h" // for adjustExprType 37 #include "typeops.h" // for adjustExprType, specCost 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 assertion61 struct AssnId {62 DeclarationWithType* decl; ///< Declaration of assertion63 AssertionSetValue info; ///< Information about assertion64 65 AssnId(DeclarationWithType* decl, const AssertionSetValue& info) : decl(decl), info(info) {}66 };67 68 /// Cached assertion items69 struct AssnCacheItem {70 CandidateList matches; ///< Possible matches for this assertion71 std::vector<AssnId> deferIds; ///< Deferred assertions which resolve to this item72 73 AssnCacheItem( CandidateList&& m ) : matches(std::move(m)), deferIds() {}74 };75 76 /// Cache of resolved assertions77 using AssnCache = std::unordered_map<std::string, AssnCacheItem>;78 79 60 /// Reference to single deferred item 80 61 struct DeferRef { 81 const AssnCacheItem& item; 62 const DeclarationWithType* decl; 63 const AssertionSetValue& info; 82 64 const AssnCandidate& match; 83 65 }; … … 86 68 /// Acts like indexed list of DeferRef 87 69 struct DeferItem { 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; } 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] }; } 109 82 }; 110 83 … … 181 154 for ( const auto& assn : x.assns ) { 182 155 k += computeConversionCost( 183 assn.match.adjType, assn.item.deferIds[0].decl->get_type(), indexer, 184 x.env ); 156 assn.match.adjType, assn.decl->get_type(), indexer, x.env ); 185 157 } 186 158 it = cache.emplace_hint( it, &x, k ); … … 253 225 254 226 /// Resolve a single assertion, in context 255 bool resolveAssertion( AssertionItem& assn, ResnState& resn , AssnCache& cache) {227 bool resolveAssertion( AssertionItem& assn, ResnState& resn ) { 256 228 // skip unused assertions 257 229 if ( ! assn.info.isUsed ) return true; 258 230 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 } 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 renameTyVars( adjType ); 247 248 // keep unifying candidates 249 if ( unify( assn.decl->get_type(), adjType, newEnv, newNeed, have, newOpenVars, 250 resn.indexer ) ) { 251 // set up binding slot for recursive assertions 252 UniqueId crntResnSlot = 0; 253 if ( ! newNeed.empty() ) { 254 crntResnSlot = ++globalResnSlot; 255 for ( auto& a : newNeed ) { 256 a.second.resnSlot = crntResnSlot; 292 257 } 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 }258 } 259 260 matches.emplace_back( cdata, adjType, std::move(newEnv), std::move(have), 261 std::move(newNeed), std::move(newOpenVars), crntResnSlot ); 262 } else { 263 delete adjType; 299 264 } 300 301 it = cache.emplace_hint( it, assnKey, AssnCacheItem{ std::move(matches) } ); 302 } 303 304 CandidateList& matches = it->second.matches; 265 } 305 266 306 267 // break if no suitable assertion … … 309 270 // defer if too many suitable assertions 310 271 if ( matches.size() > 1 ) { 311 it->second.deferIds.emplace_back( assn.decl, assn.info ); 312 resn.deferred.emplace_back( cache, assnKey ); 272 resn.deferred.emplace_back( assn.decl, assn.info, std::move(matches) ); 313 273 return true; 314 274 } … … 318 278 addToIndexer( match.have, resn.indexer ); 319 279 resn.newNeed.insert( match.need.begin(), match.need.end() ); 320 resn.alt.env = match.env;321 resn.alt.openVars = match.openVars;280 resn.alt.env = std::move(match.env); 281 resn.alt.openVars = std::move(match.openVars); 322 282 323 283 bindAssertion( assn.decl, assn.info, resn.alt, match, resn.inferred ); … … 380 340 ResnList resns{ ResnState{ alt, root_indexer } }; 381 341 ResnList new_resns{}; 382 AssnCache assnCache;383 342 384 343 // resolve assertions in breadth-first-order up to a limited number of levels deep … … 389 348 for ( auto& assn : resn.need ) { 390 349 // fail early if any assertion is not resolvable 391 if ( ! resolveAssertion( assn, resn , assnCache) ) {350 if ( ! resolveAssertion( assn, resn ) ) { 392 351 Indenter tabs{ Indenter::tabsize, 3 }; 393 352 std::ostringstream ss; … … 410 369 } 411 370 } else { 412 // only resolve each deferred assertion once413 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() );416 371 // resolve deferred assertions by mutual compatibility 417 372 std::vector<CandidateEnvMerger::OutType> compatible = filterCombos( … … 427 382 ++tabs; 428 383 for ( const auto& d : resn.deferred ) { 429 d. get_decl()->print( ss, tabs );384 d.decl->print( ss, tabs ); 430 385 } 431 386 … … 458 413 new_resn.newNeed.insert( match.need.begin(), match.need.end() ); 459 414 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 } 415 bindAssertion( r.decl, r.info, new_resn.alt, match, new_resn.inferred ); 465 416 } 466 417 -
src/SymTab/Mangler.cc
rc378e5e r9795142 38 38 struct Mangler : public WithShortCircuiting, public WithVisitorRef<Mangler>, public WithGuards { 39 39 Mangler( bool mangleOverridable, bool typeMode, bool mangleGenericParams ); 40 Mangler( const ResolvExpr::TypeEnvironment& env );41 40 Mangler( const Mangler & ) = delete; 42 41 … … 67 66 private: 68 67 std::ostringstream mangleName; ///< Mangled name being constructed 69 typedef std::map< std::string, std::pair< std::string, int > > VarMapType;68 typedef std::map< std::string, std::pair< int, int > > VarMapType; 70 69 VarMapType varNums; ///< Map of type variables to indices 71 70 int nextVarNum; ///< Next type variable index 72 const ResolvExpr::TypeEnvironment* env; ///< optional environment for substitutions73 71 bool isTopLevel; ///< Is the Mangler at the top level 74 72 bool mangleOverridable; ///< Specially mangle overridable built-in methods … … 80 78 public: 81 79 Mangler( bool mangleOverridable, bool typeMode, bool mangleGenericParams, 82 int nextVarNum, const ResolvExpr::TypeEnvironment* env, 83 const VarMapType& varNums ); 80 int nextVarNum, const VarMapType& varNums ); 84 81 85 82 private: … … 109 106 } 110 107 111 std::string mangleAssnKey( DeclarationWithType* decl,112 const ResolvExpr::TypeEnvironment& env ) {113 PassVisitor<Mangler> mangler( env );114 maybeAccept( decl, mangler );115 return mangler.pass.get_mangleName();116 }117 118 108 namespace { 119 109 Mangler::Mangler( bool mangleOverridable, bool typeMode, bool mangleGenericParams ) 120 : nextVarNum( 0 ), env(nullptr),isTopLevel( true ),110 : nextVarNum( 0 ), isTopLevel( true ), 121 111 mangleOverridable( mangleOverridable ), typeMode( typeMode ), 122 112 mangleGenericParams( mangleGenericParams ) {} 123 113 124 Mangler::Mangler( const ResolvExpr::TypeEnvironment& env )125 : nextVarNum( 0 ), env( &env ), isTopLevel( true ), mangleOverridable( false ),126 typeMode( false ), mangleGenericParams( true ) {}127 128 114 Mangler::Mangler( bool mangleOverridable, bool typeMode, bool mangleGenericParams, 129 int nextVarNum, const ResolvExpr::TypeEnvironment* env, 130 const VarMapType& varNums ) 131 : varNums( varNums ), nextVarNum( nextVarNum ), env( env ), isTopLevel( false ), 115 int nextVarNum, const VarMapType& varNums ) 116 : varNums( varNums ), nextVarNum( nextVarNum ), isTopLevel( false ), 132 117 mangleOverridable( mangleOverridable ), typeMode( typeMode ), 133 118 mangleGenericParams( mangleGenericParams ) {} … … 358 343 assert( false ); 359 344 } // switch 360 std::string varName; 361 // replace type with substitution name if environment is available and bound 362 if ( env ) { 363 const ResolvExpr::EqvClass* varClass = env->lookup( (*i)->name ); 364 if ( varClass && varClass->type ) { 365 PassVisitor<Mangler> sub_mangler( 366 mangleOverridable, typeMode, mangleGenericParams, nextVarNum, 367 env, varNums ); 368 varClass->type->accept( sub_mangler ); 369 varName = std::string{"%"} + sub_mangler.pass.get_mangleName(); 370 } 371 } 372 // otherwise just give type numeric name 373 if ( varName.empty() ) { 374 varName = std::to_string( nextVarNum++ ); 375 } 376 varNums[ (*i)->name ] = std::make_pair( varName, (int)(*i)->get_kind() ); 345 varNums[ (*i)->name ] = std::make_pair( nextVarNum, (int)(*i)->get_kind() ); 377 346 for ( std::list< DeclarationWithType* >::iterator assert = (*i)->assertions.begin(); assert != (*i)->assertions.end(); ++assert ) { 378 347 PassVisitor<Mangler> sub_mangler( 379 mangleOverridable, typeMode, mangleGenericParams, nextVarNum, env, 380 varNums ); 348 mangleOverridable, typeMode, mangleGenericParams, nextVarNum, varNums ); 381 349 (*assert)->accept( sub_mangler ); 382 350 assertionNames.push_back( sub_mangler.pass.get_mangleName() ); -
src/SymTab/Mangler.h
rc378e5e r9795142 44 44 /// Mangle ignoring generic type parameters 45 45 std::string mangleConcrete( Type* ty ); 46 /// Mangle for assertion key47 std::string mangleAssnKey( DeclarationWithType* decl,48 const ResolvExpr::TypeEnvironment& env );49 46 50 47 namespace Encoding {
Note: See TracChangeset
for help on using the changeset viewer.