@@ -35,28 +35,19 @@ using namespace swift;
35
35
using namespace namelookup ;
36
36
using namespace ast_scope ;
37
37
38
- static bool isLocWithinAnInactiveClause (const SourceLoc loc, SourceFile *SF);
39
-
40
38
llvm::SmallVector<const ASTScopeImpl *, 0 > ASTScopeImpl::unqualifiedLookup (
41
39
SourceFile *sourceFile, const DeclNameRef name, const SourceLoc loc,
42
- const DeclContext * const startingContext, DeclConsumer consumer) {
40
+ DeclConsumer consumer) {
43
41
SmallVector<const ASTScopeImpl *, 0 > history;
44
42
const auto *start =
45
- findStartingScopeForLookup (sourceFile, name, loc, startingContext );
43
+ findStartingScopeForLookup (sourceFile, name, loc);
46
44
if (start)
47
45
start->lookup (history, nullptr , nullptr , consumer);
48
46
return history;
49
47
}
50
48
51
49
const ASTScopeImpl *ASTScopeImpl::findStartingScopeForLookup (
52
- SourceFile *sourceFile, const DeclNameRef name, const SourceLoc loc,
53
- const DeclContext *const startingContext) {
54
- // At present, use legacy code in unqualifiedLookup.cpp to handle module-level
55
- // lookups
56
- // TODO: implement module scope someday
57
- if (startingContext->getContextKind () == DeclContextKind::Module)
58
- return nullptr ;
59
-
50
+ SourceFile *sourceFile, const DeclNameRef name, const SourceLoc loc) {
60
51
auto *const fileScope = sourceFile->getScope ().impl ;
61
52
// Parser may have added decls to source file, since previous lookup
62
53
if (name.isOperator ())
@@ -66,63 +57,7 @@ const ASTScopeImpl *ASTScopeImpl::findStartingScopeForLookup(
66
57
ASTScopeAssert (innermost->getWasExpanded (),
67
58
" If looking in a scope, it must have been expanded." );
68
59
69
- // The legacy lookup code gets passed both a SourceLoc and a starting context.
70
- // However, our ultimate intent is for clients to not have to pass in a
71
- // DeclContext at all, since the SourceLoc should be enough. While we are
72
- // debugging the new ASTScope lookup code, we can catch bugs by comparing the
73
- // DeclContext of the ASTScope found from the desired SourceLoc to the
74
- // DeclContext passed in by the client.
75
-
76
- const auto *startingScope = innermost;
77
- for (; startingScope &&
78
- !startingScope->doesContextMatchStartingContext (startingContext);
79
- startingScope = startingScope->getParent ().getPtrOrNull ()) {
80
- }
81
- // Someday, just use the assertion below. For now, print out lots of info for
82
- // debugging.
83
- if (!startingScope) {
84
-
85
- // Be lenient in code completion mode. There are cases where the decl
86
- // context doesn't match with the ASTScope. e.g. dangling attributes.
87
- // FIXME: Create ASTScope tree even for invalid code.
88
- if (innermost &&
89
- startingContext->getASTContext ().SourceMgr .hasCodeCompletionBuffer ()) {
90
- return innermost;
91
- }
92
-
93
- llvm::errs () << " ASTScopeImpl: resorting to startingScope hack, file: "
94
- << sourceFile->getFilename () << " \n " ;
95
- // The check is costly, and inactive lookups will end up here, so don't
96
- // do the check unless we can't find the startingScope.
97
- const bool isInInactiveClause =
98
- isLocWithinAnInactiveClause (loc, sourceFile);
99
- if (isInInactiveClause)
100
- llvm::errs () << " because location is within an inactive clause\n " ;
101
- llvm::errs () << " '" ;
102
- name.print (llvm::errs ());
103
- llvm::errs () << " ' " ;
104
- llvm::errs () << " loc: " ;
105
- loc.print (llvm::errs (), sourceFile->getASTContext ().SourceMgr );
106
- llvm::errs () << " \n starting context:\n " ;
107
- startingContext->printContext (llvm::errs ());
108
- // llvm::errs() << "\ninnermost: ";
109
- // innermost->dump();
110
- // llvm::errs() << "in: \n";
111
- // fileScope->dump();
112
- llvm::errs () << " \n\n " ;
113
-
114
- // Might distort things
115
- // if (fileScope->crossCheckWithAST())
116
- // llvm::errs() << "Tree creation missed some DeclContexts.\n";
117
-
118
- // Crash compilation even if NDEBUG
119
- if (isInInactiveClause)
120
- llvm::report_fatal_error (
121
- " A lookup was attempted into an inactive clause" );
122
- }
123
-
124
- ASTScopeAssert (startingScope, " ASTScopeImpl: could not find startingScope" );
125
- return startingScope;
60
+ return innermost;
126
61
}
127
62
128
63
ASTScopeImpl *
@@ -189,53 +124,6 @@ ASTScopeImpl::findChildContaining(SourceLoc loc,
189
124
return nullptr ;
190
125
}
191
126
192
- #pragma mark doesContextMatchStartingContext
193
- // Match existing UnqualifiedLookupBehavior
194
-
195
- bool ASTScopeImpl::doesContextMatchStartingContext (
196
- const DeclContext *context) const {
197
- // Why are we not checking the loc for this--because already did binary search
198
- // on loc to find the start First, try MY DeclContext
199
- if (auto myDCForL = getDeclContext ())
200
- return myDCForL == context;
201
- // If I don't have one, ask my parent.
202
- // (Choose innermost scope with matching loc & context.)
203
- if (auto p = getParent ())
204
- return p.get ()->doesContextMatchStartingContext (context);
205
- // Topmost scope always has a context, the SourceFile.
206
- ASTScope_unreachable (" topmost scope always has a context, the SourceFile" );
207
- }
208
-
209
- // For a SubscriptDecl with generic parameters, the call tries to do lookups
210
- // with startingContext equal to either the get or set subscript
211
- // AbstractFunctionDecls. Since the generic parameters are in the
212
- // SubscriptDeclScope, and not the AbstractFunctionDecl scopes (after all how
213
- // could one parameter be in two scopes?), GenericParamScope intercepts the
214
- // match query here and tests against the accessor DeclContexts.
215
- bool GenericParamScope::doesContextMatchStartingContext (
216
- const DeclContext *context) const {
217
- if (auto *asd = dyn_cast<AbstractStorageDecl>(holder)) {
218
- for (auto accessor : asd->getAllAccessors ()) {
219
- if (up_cast<DeclContext>(accessor) == context)
220
- return true ;
221
- }
222
- }
223
- return false ;
224
- }
225
-
226
- bool DifferentiableAttributeScope::doesContextMatchStartingContext (
227
- const DeclContext *context) const {
228
- // Need special logic to handle case where `attributedDeclaration` is an
229
- // `AbstractStorageDecl` (`SubscriptDecl` or `VarDecl`). The initial starting
230
- // context in `ASTScopeImpl::findStartingScopeForLookup` will be an accessor
231
- // of the `attributedDeclaration`.
232
- if (auto *asd = dyn_cast<AbstractStorageDecl>(attributedDeclaration))
233
- for (auto accessor : asd->getAllAccessors ())
234
- if (up_cast<DeclContext>(accessor) == context)
235
- return true ;
236
- return false ;
237
- }
238
-
239
127
#pragma mark lookup methods that run once per scope
240
128
241
129
void ASTScopeImpl::lookup (SmallVectorImpl<const ASTScopeImpl *> &history,
@@ -841,39 +729,6 @@ Optional<bool> PatternEntryInitializerScope::resolveIsCascadingUseForThisScope(
841
729
return isCascadingUse;
842
730
}
843
731
844
- bool isLocWithinAnInactiveClause (const SourceLoc loc, SourceFile *SF) {
845
- class InactiveClauseTester : public ASTWalker {
846
- const SourceLoc loc;
847
- const SourceManager &SM;
848
-
849
- public:
850
- bool wasFoundWithinInactiveClause = false ;
851
-
852
- InactiveClauseTester (const SourceLoc loc, const SourceManager &SM)
853
- : loc(loc), SM(SM) {}
854
-
855
- bool walkToDeclPre (Decl *D) override {
856
- if (const auto *ifc = dyn_cast<IfConfigDecl>(D)) {
857
- for (const auto &clause : ifc->getClauses ()) {
858
- if (clause.isActive )
859
- continue ;
860
- for (const auto &n : clause.Elements ) {
861
- SourceRange sr = n.getSourceRange ();
862
- if (sr.isValid () && SM.rangeContainsTokenLoc (sr, loc)) {
863
- wasFoundWithinInactiveClause = true ;
864
- return false ;
865
- }
866
- }
867
- }
868
- }
869
- return ASTWalker::walkToDeclPre (D);
870
- }
871
- };
872
- InactiveClauseTester tester (loc, SF->getASTContext ().SourceMgr );
873
- SF->walk (tester);
874
- return tester.wasFoundWithinInactiveClause ;
875
- }
876
-
877
732
#pragma mark isLabeledStmtLookupTerminator implementations
878
733
bool ASTScopeImpl::isLabeledStmtLookupTerminator () const {
879
734
return true ;
0 commit comments