From 6c603c473aff3ed7a3405637d381565db95c6f8a Mon Sep 17 00:00:00 2001 From: Gogul Balakrishnan Date: Wed, 27 Feb 2019 15:24:28 -0800 Subject: [PATCH 1/6] Add an additional hook to the DebuggerClient during name lookups. --- include/swift/AST/DebuggerClient.h | 5 +++++ lib/AST/NameLookup.cpp | 1 + 2 files changed, 6 insertions(+) diff --git a/include/swift/AST/DebuggerClient.h b/include/swift/AST/DebuggerClient.h index b21d96be80312..eb8960ed997c2 100644 --- a/include/swift/AST/DebuggerClient.h +++ b/include/swift/AST/DebuggerClient.h @@ -61,6 +61,11 @@ class DebuggerClient { SourceLoc Loc, bool IsTypeLookup, ResultVector &RV) = 0; + /// Allows the DebuggerClient to prune the results of a name lookup before + /// returning to the caller. (See finishLookup in NameLookup.cpp.) + virtual void finishLookup(const DeclContext *dc, NLOptions options, + SmallVectorImpl &decls) {} + /// When evaluating an expression in the context of an existing source file, /// we may want to prefer declarations from that source file. /// The DebuggerClient can return a private-discriminator to tell lookup to diff --git a/lib/AST/NameLookup.cpp b/lib/AST/NameLookup.cpp index 9180840fffe26..6687ba1195c85 100644 --- a/lib/AST/NameLookup.cpp +++ b/lib/AST/NameLookup.cpp @@ -20,6 +20,7 @@ #include "swift/AST/ASTVisitor.h" #include "swift/AST/ClangModuleLoader.h" #include "swift/AST/DebuggerClient.h" +#include "swift/AST/DeclContext.h" #include "swift/AST/ExistentialLayout.h" #include "swift/AST/LazyResolver.h" #include "swift/AST/Initializer.h" From c54aed6c27122f6565fd64a04850f544f24f63c9 Mon Sep 17 00:00:00 2001 From: Gogul Balakrishnan Date: Fri, 1 Mar 2019 15:14:18 -0800 Subject: [PATCH 2/6] Added three versions of finishLookups. --- include/swift/AST/DebuggerClient.h | 21 +++++++++++++---- lib/AST/NameLookup.cpp | 37 +++++++++++++++++++++++++++--- 2 files changed, 51 insertions(+), 7 deletions(-) diff --git a/include/swift/AST/DebuggerClient.h b/include/swift/AST/DebuggerClient.h index eb8960ed997c2..c46e952a40f4a 100644 --- a/include/swift/AST/DebuggerClient.h +++ b/include/swift/AST/DebuggerClient.h @@ -61,10 +61,23 @@ class DebuggerClient { SourceLoc Loc, bool IsTypeLookup, ResultVector &RV) = 0; - /// Allows the DebuggerClient to prune the results of a name lookup before - /// returning to the caller. (See finishLookup in NameLookup.cpp.) - virtual void finishLookup(const DeclContext *dc, NLOptions options, - SmallVectorImpl &decls) {} + /// The following functions allow the debugger to prune the results of a a + /// qualfied lookup as needed. See the corresponding finishLookupInXYZ + /// functions defined in NameLookup.cpp. + /// + + virtual void finishLookupInNominals(const DeclContext *dc, + ArrayRef types, + DeclName member, NLOptions options, + SmallVectorImpl &decls) {} + + virtual void finishLookupInModule(const DeclContext *dc, ModuleDecl *module, + DeclName member, NLOptions options, + SmallVectorImpl &decls) {} + + virtual void finishLookupInAnyObject(const DeclContext *dc, DeclName member, + NLOptions options, + SmallVectorImpl &decls) {} /// When evaluating an expression in the context of an existing source file, /// we may want to prefer declarations from that source file. diff --git a/lib/AST/NameLookup.cpp b/lib/AST/NameLookup.cpp index 6687ba1195c85..c1ac69c3dc94b 100644 --- a/lib/AST/NameLookup.cpp +++ b/lib/AST/NameLookup.cpp @@ -1369,7 +1369,38 @@ bool namelookup::finishLookup(const DeclContext *dc, NLOptions options, removeShadowedDecls(decls, M); filterForDiscriminator(decls, M->getDebugClient()); +} + +static bool finishLookupInNominals(const DeclContext *dc, + ArrayRef types, + DeclName member, NLOptions options, + SmallVectorImpl &decls) { + finishLookup(dc, options, decls); + if (auto *debugClient = dc->getParentModule()->getDebugClient()) { + debugClient->finishLookupInNominals(dc, types, member, options, decls); + } + // We're done. Report success/failure. + return !decls.empty(); +} +static bool finishLookupInModule(const DeclContext *dc, ModuleDecl *module, + DeclName member, NLOptions options, + SmallVectorImpl &decls) { + finishLookup(dc, options, decls); + if (auto *debugClient = dc->getParentModule()->getDebugClient()) { + debugClient->finishLookupInModule(dc, module, member, options, decls); + } + // We're done. Report success/failure. + return !decls.empty(); +} + +static bool finishLookupInAnyObject(const DeclContext *dc, DeclName member, + NLOptions options, + SmallVectorImpl &decls) { + finishLookup(dc, options, decls); + if (auto *debugClient = dc->getParentModule()->getDebugClient()) { + debugClient->finishLookupInAnyObject(dc, member, options, decls); + } // We're done. Report success/failure. return !decls.empty(); } @@ -1575,7 +1606,7 @@ bool DeclContext::lookupQualified(ArrayRef typeDecls, } } - return finishLookup(this, options, decls); + return finishLookupInNominals(this, typeDecls, member, options, decls); } bool DeclContext::lookupQualified(ModuleDecl *module, DeclName member, @@ -1627,7 +1658,7 @@ bool DeclContext::lookupQualified(ModuleDecl *module, DeclName member, return !knownDecls.insert(vd).second; }), decls.end()); - return finishLookup(this, options, decls); + return finishLookupInModule(this, module, member, options, decls); } bool DeclContext::lookupAnyObject(DeclName member, NLOptions options, @@ -1682,7 +1713,7 @@ bool DeclContext::lookupAnyObject(DeclName member, NLOptions options, decls.push_back(decl); } - return finishLookup(this, options, decls); + return finishLookupInAnyObject(this, member, options, decls); } void DeclContext::lookupAllObjCMethods( From 11f394a02306d90628341492bb4c57023687564d Mon Sep 17 00:00:00 2001 From: Gogul Balakrishnan Date: Mon, 4 Mar 2019 12:15:51 -0800 Subject: [PATCH 3/6] Remove the file static functions. --- include/swift/AST/DebuggerClient.h | 7 ++-- include/swift/AST/NameLookup.h | 10 ++--- lib/AST/NameLookup.cpp | 61 +++++++++++------------------- 3 files changed, 31 insertions(+), 47 deletions(-) diff --git a/include/swift/AST/DebuggerClient.h b/include/swift/AST/DebuggerClient.h index c46e952a40f4a..b51207788b51d 100644 --- a/include/swift/AST/DebuggerClient.h +++ b/include/swift/AST/DebuggerClient.h @@ -61,9 +61,10 @@ class DebuggerClient { SourceLoc Loc, bool IsTypeLookup, ResultVector &RV) = 0; - /// The following functions allow the debugger to prune the results of a a - /// qualfied lookup as needed. See the corresponding finishLookupInXYZ - /// functions defined in NameLookup.cpp. + /// The following functions allow the debugger to modify the results of a + /// qualfied lookup as needed. These methods may add, remove or modify the + /// entries in `decls`. See the corresponding DeclContext::lookupInXYZ + /// functions defined in NameLookup.cpp for more context. /// virtual void finishLookupInNominals(const DeclContext *dc, diff --git a/include/swift/AST/NameLookup.h b/include/swift/AST/NameLookup.h index 263286989282d..1b9a7f226ecde 100644 --- a/include/swift/AST/NameLookup.h +++ b/include/swift/AST/NameLookup.h @@ -369,8 +369,8 @@ void forAllVisibleModules(const DeclContext *DC, const Fn &fn) { /// Only name lookup has gathered a set of results, perform any necessary /// steps to prune the result set before returning it to the caller. -bool finishLookup(const DeclContext *dc, NLOptions options, - SmallVectorImpl &decls); +bool pruneLookupResultSet(const DeclContext *dc, NLOptions options, + SmallVectorImpl &decls); /// Do nothing if debugClient is null. template @@ -441,7 +441,7 @@ class FindLocalVal : public StmtVisitor { } void checkPattern(const Pattern *Pat, DeclVisibilityKind Reason); - + void checkParameterList(const ParameterList *params); void checkGenericParams(GenericParamList *Params); @@ -477,7 +477,7 @@ class FindLocalVal : public StmtVisitor { void visitForEachStmt(ForEachStmt *S); void visitBraceStmt(BraceStmt *S, bool isTopLevelCode = false); - + void visitSwitchStmt(SwitchStmt *S); void visitCaseStmt(CaseStmt *S); @@ -485,7 +485,7 @@ class FindLocalVal : public StmtVisitor { void visitDoCatchStmt(DoCatchStmt *S); void visitCatchClauses(ArrayRef clauses); void visitCatchStmt(CatchStmt *S); - + }; } // end namespace namelookup diff --git a/lib/AST/NameLookup.cpp b/lib/AST/NameLookup.cpp index c1ac69c3dc94b..078aa6e7ad09c 100644 --- a/lib/AST/NameLookup.cpp +++ b/lib/AST/NameLookup.cpp @@ -1357,8 +1357,8 @@ static bool isAcceptableLookupResult(const DeclContext *dc, return true; } -bool namelookup::finishLookup(const DeclContext *dc, NLOptions options, - SmallVectorImpl &decls) { +bool namelookup::pruneLookupResultSet(const DeclContext *dc, NLOptions options, + SmallVectorImpl &decls) { // If we're supposed to remove overridden declarations, do so now. if (options & NL_RemoveOverridden) removeOverriddenDecls(decls); @@ -1371,40 +1371,6 @@ bool namelookup::finishLookup(const DeclContext *dc, NLOptions options, filterForDiscriminator(decls, M->getDebugClient()); } -static bool finishLookupInNominals(const DeclContext *dc, - ArrayRef types, - DeclName member, NLOptions options, - SmallVectorImpl &decls) { - finishLookup(dc, options, decls); - if (auto *debugClient = dc->getParentModule()->getDebugClient()) { - debugClient->finishLookupInNominals(dc, types, member, options, decls); - } - // We're done. Report success/failure. - return !decls.empty(); -} - -static bool finishLookupInModule(const DeclContext *dc, ModuleDecl *module, - DeclName member, NLOptions options, - SmallVectorImpl &decls) { - finishLookup(dc, options, decls); - if (auto *debugClient = dc->getParentModule()->getDebugClient()) { - debugClient->finishLookupInModule(dc, module, member, options, decls); - } - // We're done. Report success/failure. - return !decls.empty(); -} - -static bool finishLookupInAnyObject(const DeclContext *dc, DeclName member, - NLOptions options, - SmallVectorImpl &decls) { - finishLookup(dc, options, decls); - if (auto *debugClient = dc->getParentModule()->getDebugClient()) { - debugClient->finishLookupInAnyObject(dc, member, options, decls); - } - // We're done. Report success/failure. - return !decls.empty(); -} - /// Inspect the given type to determine which nominal type declarations it /// directly references, to facilitate name lookup into those types. static void extractDirectlyReferencedNominalTypes( @@ -1606,7 +1572,13 @@ bool DeclContext::lookupQualified(ArrayRef typeDecls, } } - return finishLookupInNominals(this, typeDecls, member, options, decls); + pruneLookupResultSet(this, options, decls); + if (auto *debugClient = this->getParentModule()->getDebugClient()) { + debugClient->finishLookupInNominals(this, typeDecls, member, options, + decls); + } + // We're done. Report success/failure. + return !decls.empty(); } bool DeclContext::lookupQualified(ModuleDecl *module, DeclName member, @@ -1658,7 +1630,13 @@ bool DeclContext::lookupQualified(ModuleDecl *module, DeclName member, return !knownDecls.insert(vd).second; }), decls.end()); - return finishLookupInModule(this, module, member, options, decls); + pruneLookupResultSet(this, options, decls); + + if (auto *debugClient = this->getParentModule()->getDebugClient()) { + debugClient->finishLookupInModule(this, module, member, options, decls); + } + // We're done. Report success/failure. + return !decls.empty(); } bool DeclContext::lookupAnyObject(DeclName member, NLOptions options, @@ -1713,7 +1691,12 @@ bool DeclContext::lookupAnyObject(DeclName member, NLOptions options, decls.push_back(decl); } - return finishLookupInAnyObject(this, member, options, decls); + pruneLookupResultSet(this, options, decls); + if (auto *debugClient = this->getParentModule()->getDebugClient()) { + debugClient->finishLookupInAnyObject(this, member, options, decls); + } + // We're done. Report success/failure. + return !decls.empty(); } void DeclContext::lookupAllObjCMethods( From 97dabb6458ee6b83d1d49879f375ed170c852f3c Mon Sep 17 00:00:00 2001 From: Gogul Balakrishnan Date: Mon, 4 Mar 2019 12:55:32 -0800 Subject: [PATCH 4/6] Remove redundant header and also change signature of pruneLookupResultSet to return void. --- include/swift/AST/NameLookup.h | 2 +- lib/AST/NameLookup.cpp | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/include/swift/AST/NameLookup.h b/include/swift/AST/NameLookup.h index 1b9a7f226ecde..856202eb41ff0 100644 --- a/include/swift/AST/NameLookup.h +++ b/include/swift/AST/NameLookup.h @@ -369,7 +369,7 @@ void forAllVisibleModules(const DeclContext *DC, const Fn &fn) { /// Only name lookup has gathered a set of results, perform any necessary /// steps to prune the result set before returning it to the caller. -bool pruneLookupResultSet(const DeclContext *dc, NLOptions options, +void pruneLookupResultSet(const DeclContext *dc, NLOptions options, SmallVectorImpl &decls); /// Do nothing if debugClient is null. diff --git a/lib/AST/NameLookup.cpp b/lib/AST/NameLookup.cpp index 078aa6e7ad09c..b7b8e0e062a2a 100644 --- a/lib/AST/NameLookup.cpp +++ b/lib/AST/NameLookup.cpp @@ -20,7 +20,6 @@ #include "swift/AST/ASTVisitor.h" #include "swift/AST/ClangModuleLoader.h" #include "swift/AST/DebuggerClient.h" -#include "swift/AST/DeclContext.h" #include "swift/AST/ExistentialLayout.h" #include "swift/AST/LazyResolver.h" #include "swift/AST/Initializer.h" @@ -1357,7 +1356,7 @@ static bool isAcceptableLookupResult(const DeclContext *dc, return true; } -bool namelookup::pruneLookupResultSet(const DeclContext *dc, NLOptions options, +void namelookup::pruneLookupResultSet(const DeclContext *dc, NLOptions options, SmallVectorImpl &decls) { // If we're supposed to remove overridden declarations, do so now. if (options & NL_RemoveOverridden) From f120c0846e52e26f4fe3aa5aa993a548c85cc971 Mon Sep 17 00:00:00 2001 From: Gogul Balakrishnan Date: Mon, 4 Mar 2019 12:58:05 -0800 Subject: [PATCH 5/6] Revert whitespace changes from this PR. --- include/swift/AST/NameLookup.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/include/swift/AST/NameLookup.h b/include/swift/AST/NameLookup.h index 856202eb41ff0..8cd507cf15149 100644 --- a/include/swift/AST/NameLookup.h +++ b/include/swift/AST/NameLookup.h @@ -441,7 +441,7 @@ class FindLocalVal : public StmtVisitor { } void checkPattern(const Pattern *Pat, DeclVisibilityKind Reason); - + void checkParameterList(const ParameterList *params); void checkGenericParams(GenericParamList *Params); @@ -477,7 +477,7 @@ class FindLocalVal : public StmtVisitor { void visitForEachStmt(ForEachStmt *S); void visitBraceStmt(BraceStmt *S, bool isTopLevelCode = false); - + void visitSwitchStmt(SwitchStmt *S); void visitCaseStmt(CaseStmt *S); @@ -485,7 +485,7 @@ class FindLocalVal : public StmtVisitor { void visitDoCatchStmt(DoCatchStmt *S); void visitCatchClauses(ArrayRef clauses); void visitCatchStmt(CatchStmt *S); - + }; } // end namespace namelookup From 880de77b99c5f35f6fb41f41e2b0a2cba27798bc Mon Sep 17 00:00:00 2001 From: Gogul Balakrishnan Date: Tue, 26 Mar 2019 09:42:37 -0700 Subject: [PATCH 6/6] Fixed a typo in comment --- include/swift/AST/NameLookup.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/swift/AST/NameLookup.h b/include/swift/AST/NameLookup.h index 8cd507cf15149..18851ddca2299 100644 --- a/include/swift/AST/NameLookup.h +++ b/include/swift/AST/NameLookup.h @@ -367,7 +367,7 @@ void forAllVisibleModules(const DeclContext *DC, const Fn &fn) { ->forAllVisibleModules(ModuleDecl::AccessPathTy(), fn); } -/// Only name lookup has gathered a set of results, perform any necessary +/// Once name lookup has gathered a set of results, perform any necessary /// steps to prune the result set before returning it to the caller. void pruneLookupResultSet(const DeclContext *dc, NLOptions options, SmallVectorImpl &decls);