Skip to content

Commit 3a00063

Browse files
committed
Compute VarDeclScopeMap up front.
1 parent 874ba08 commit 3a00063

File tree

1 file changed

+40
-29
lines changed

1 file changed

+40
-29
lines changed

lib/SILGen/SILGenFunction.cpp

Lines changed: 40 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,49 @@ 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+
Consumer consumer(FnASTScope, VarDeclScopeMap);
97+
const_cast<ast_scope::ASTScopeImpl *>(FnASTScope)
98+
->postOrderDo([&](ast_scope::ASTScopeImpl *ASTScope) {
99+
ASTScope->lookupLocalsOrMembers(consumer);
100+
});
62101
}
63102
}
64103

@@ -202,8 +241,6 @@ const SILDebugScope *SILGenFunction::getScopeOrNull(SILLocation Loc,
202241
SourceLoc SLoc = Loc.getSourceLoc();
203242
if (!SF || LastSourceLoc == SLoc)
204243
return nullptr;
205-
// Prime VarDeclScopeMap.
206-
auto Scope = getOrCreateScope(SLoc);
207244
if (ForMetaInstruction)
208245
if (ValueDecl *ValDecl = Loc.getAsASTNode<ValueDecl>()) {
209246
// The source location of a VarDecl isn't necessarily in the same scope
@@ -212,7 +249,7 @@ const SILDebugScope *SILGenFunction::getScopeOrNull(SILLocation Loc,
212249
if (ValueScope != VarDeclScopeMap.end())
213250
return getOrCreateScope(ValueScope->second, F.getDebugScope());
214251
}
215-
return Scope;
252+
return getOrCreateScope(SLoc);
216253
}
217254

218255
const SILDebugScope *SILGenFunction::getOrCreateScope(SourceLoc SLoc) {
@@ -406,32 +443,6 @@ SILGenFunction::getOrCreateScope(const ast_scope::ASTScopeImpl *ASTScope,
406443
return ParentScope->InlinedCallSite != InlinedAt ? FnScope : ParentScope;
407444
}
408445

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-
435446
// Collapse BraceStmtScopes whose parent is a .*BodyScope.
436447
if (auto Parent = ASTScope->getParent().getPtrOrNull())
437448
if (Parent->getSourceRangeOfThisASTNode() ==

0 commit comments

Comments
 (0)