@@ -55,10 +55,51 @@ SILGenFunction::SILGenFunction(SILGenModule &SGM, SILFunction &F,
55
55
assert (DC && " creating SGF without a DeclContext?" );
56
56
B.setInsertionPoint (createBasicBlock ());
57
57
B.setCurrentDebugScope (F.getDebugScope ());
58
+
59
+ // Populate VarDeclScopeMap.
58
60
SourceLoc SLoc = F.getLocation ().getSourceLoc ();
59
61
if (SF && SLoc) {
60
62
FnASTScope = ast_scope::ASTScopeImpl::findStartingScopeForLookup (SF, SLoc);
61
63
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
+ });
62
103
}
63
104
}
64
105
@@ -202,8 +243,6 @@ const SILDebugScope *SILGenFunction::getScopeOrNull(SILLocation Loc,
202
243
SourceLoc SLoc = Loc.getSourceLoc ();
203
244
if (!SF || LastSourceLoc == SLoc)
204
245
return nullptr ;
205
- // Prime VarDeclScopeMap.
206
- auto Scope = getOrCreateScope (SLoc);
207
246
if (ForMetaInstruction)
208
247
if (ValueDecl *ValDecl = Loc.getAsASTNode <ValueDecl>()) {
209
248
// The source location of a VarDecl isn't necessarily in the same scope
@@ -212,7 +251,7 @@ const SILDebugScope *SILGenFunction::getScopeOrNull(SILLocation Loc,
212
251
if (ValueScope != VarDeclScopeMap.end ())
213
252
return getOrCreateScope (ValueScope->second , F.getDebugScope ());
214
253
}
215
- return Scope ;
254
+ return getOrCreateScope (SLoc) ;
216
255
}
217
256
218
257
const SILDebugScope *SILGenFunction::getOrCreateScope (SourceLoc SLoc) {
@@ -406,32 +445,6 @@ SILGenFunction::getOrCreateScope(const ast_scope::ASTScopeImpl *ASTScope,
406
445
return ParentScope->InlinedCallSite != InlinedAt ? FnScope : ParentScope;
407
446
}
408
447
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
-
435
448
// Collapse BraceStmtScopes whose parent is a .*BodyScope.
436
449
if (auto Parent = ASTScope->getParent ().getPtrOrNull ())
437
450
if (Parent->getSourceRangeOfThisASTNode () ==
0 commit comments