@@ -55,10 +55,51 @@ SILGenFunction::SILGenFunction(SILGenModule &SGM, SILFunction &F,
5555 assert (DC && " creating SGF without a DeclContext?" );
5656 B.setInsertionPoint (createBasicBlock ());
5757 B.setCurrentDebugScope (F.getDebugScope ());
58+
59+ // Populate VarDeclScopeMap.
5860 SourceLoc SLoc = F.getLocation ().getSourceLoc ();
5961 if (SF && SLoc) {
6062 FnASTScope = ast_scope::ASTScopeImpl::findStartingScopeForLookup (SF, SLoc);
6163 ScopeMap.insert ({{FnASTScope, nullptr }, F.getDebugScope ()});
64+
65+ // Collect all variable declarations in this scope.
66+ struct Consumer : public namelookup ::AbstractASTScopeDeclConsumer {
67+ const ast_scope::ASTScopeImpl *ASTScope;
68+ VarDeclScopeMapTy &VarDeclScopeMap;
69+ Consumer (const ast_scope::ASTScopeImpl *ASTScope,
70+ VarDeclScopeMapTy &VarDeclScopeMap)
71+ : ASTScope(ASTScope), VarDeclScopeMap(VarDeclScopeMap) {}
72+
73+ bool consume (ArrayRef<ValueDecl *> values,
74+ NullablePtr<DeclContext> baseDC) override {
75+ LLVM_DEBUG (ASTScope->print (llvm::errs (), 0 , false , false ));
76+ for (auto &value : values) {
77+ LLVM_DEBUG ({
78+ if (value->hasName ())
79+ llvm::dbgs () << " + " << value->getBaseIdentifier () << " \n " ;
80+ });
81+
82+ assert ((VarDeclScopeMap.count (value) == 0 ||
83+ VarDeclScopeMap[value] == ASTScope) &&
84+ " VarDecl appears twice" );
85+ VarDeclScopeMap.insert ({value, ASTScope});
86+ }
87+ return false ;
88+ }
89+ bool lookInMembers (const DeclContext *) const override { return false ; }
90+ #ifndef NDEBUG
91+ void startingNextLookupStep () override {}
92+ void finishingLookup (std::string) const override {}
93+ bool isTargetLookup () const override { return false ; }
94+ #endif
95+ };
96+ const_cast <ast_scope::ASTScopeImpl *>(FnASTScope)
97+ ->preOrderChildrenDo ([&](ast_scope::ASTScopeImpl *ASTScope) {
98+ if (!ASTScope->ignoreInDebugInfo ()) {
99+ Consumer consumer (ASTScope, VarDeclScopeMap);
100+ ASTScope->lookupLocalsOrMembers (consumer);
101+ }
102+ });
62103 }
63104}
64105
@@ -202,8 +243,6 @@ const SILDebugScope *SILGenFunction::getScopeOrNull(SILLocation Loc,
202243 SourceLoc SLoc = Loc.getSourceLoc ();
203244 if (!SF || LastSourceLoc == SLoc)
204245 return nullptr ;
205- // Prime VarDeclScopeMap.
206- auto Scope = getOrCreateScope (SLoc);
207246 if (ForMetaInstruction)
208247 if (ValueDecl *ValDecl = Loc.getAsASTNode <ValueDecl>()) {
209248 // The source location of a VarDecl isn't necessarily in the same scope
@@ -212,7 +251,7 @@ const SILDebugScope *SILGenFunction::getScopeOrNull(SILLocation Loc,
212251 if (ValueScope != VarDeclScopeMap.end ())
213252 return getOrCreateScope (ValueScope->second , F.getDebugScope ());
214253 }
215- return Scope ;
254+ return getOrCreateScope (SLoc) ;
216255}
217256
218257const SILDebugScope *SILGenFunction::getOrCreateScope (SourceLoc SLoc) {
@@ -406,32 +445,6 @@ SILGenFunction::getOrCreateScope(const ast_scope::ASTScopeImpl *ASTScope,
406445 return ParentScope->InlinedCallSite != InlinedAt ? FnScope : ParentScope;
407446 }
408447
409- // Collect all variable declarations in this scope.
410- struct Consumer : public namelookup ::AbstractASTScopeDeclConsumer {
411- const ast_scope::ASTScopeImpl *ASTScope;
412- VarDeclScopeMapTy &VarDeclScopeMap;
413- Consumer (const ast_scope::ASTScopeImpl *ASTScope,
414- VarDeclScopeMapTy &VarDeclScopeMap)
415- : ASTScope(ASTScope), VarDeclScopeMap(VarDeclScopeMap) {}
416-
417- bool consume (ArrayRef<ValueDecl *> values,
418- NullablePtr<DeclContext> baseDC) override {
419- for (auto &value : values) {
420- assert (VarDeclScopeMap.count (value) == 0 && " VarDecl appears twice" );
421- VarDeclScopeMap.insert ({value, ASTScope});
422- }
423- return false ;
424- }
425- bool lookInMembers (const DeclContext *) const override { return false ; }
426- #ifndef NDEBUG
427- void startingNextLookupStep () override {}
428- void finishingLookup (std::string) const override {}
429- bool isTargetLookup () const override { return false ; }
430- #endif
431- };
432- Consumer consumer (ASTScope, VarDeclScopeMap);
433- ASTScope->lookupLocalsOrMembers (consumer);
434-
435448 // Collapse BraceStmtScopes whose parent is a .*BodyScope.
436449 if (auto Parent = ASTScope->getParent ().getPtrOrNull ())
437450 if (Parent->getSourceRangeOfThisASTNode () ==
0 commit comments