Skip to content

Commit 658d4bf

Browse files
committed
SourceKit: Refactor walkRelatedDecls() to not use unqualified lookup
This function used to perform an unqualified lookup without a source location, to either find top-level members or members of a type. Since this form of unqualified lookup is no longer supported, let's instead explicitly call lookupInModule() or lookupQualified(), depending on whether we're looking inside a type or not.
1 parent bec3136 commit 658d4bf

File tree

1 file changed

+33
-24
lines changed

1 file changed

+33
-24
lines changed

tools/SourceKit/lib/SwiftLang/SwiftSourceDocInfo.cpp

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@
2020
#include "swift/AST/ASTDemangler.h"
2121
#include "swift/AST/ASTPrinter.h"
2222
#include "swift/AST/Decl.h"
23+
#include "swift/AST/LookupKinds.h"
24+
#include "swift/AST/ModuleNameLookup.h"
2325
#include "swift/AST/NameLookup.h"
24-
#include "swift/AST/NameLookupRequests.h"
2526
#include "swift/AST/SwiftNameTranslation.h"
2627
#include "swift/AST/GenericSignature.h"
2728
#include "swift/Basic/SourceManager.h"
@@ -484,40 +485,48 @@ void SwiftLangSupport::printFullyAnnotatedSynthesizedDeclaration(
484485

485486
template <typename FnTy>
486487
void walkRelatedDecls(const ValueDecl *VD, const FnTy &Fn) {
487-
llvm::SmallDenseMap<DeclName, unsigned, 16> NamesSeen;
488-
++NamesSeen[VD->getName()];
489-
SmallVector<LookupResultEntry, 8> RelatedDecls;
490-
491488
if (isa<ParamDecl>(VD))
492489
return; // Parameters don't have interesting related declarations.
493490

494-
// FIXME: Extract useful related declarations, overloaded functions,
495-
// if VD is an initializer, we should extract other initializers etc.
496-
// For now we use unqualified lookup to fetch other declarations with the same
497-
// base name.
498491
auto &ctx = VD->getASTContext();
499-
auto descriptor = UnqualifiedLookupDescriptor(DeclNameRef(VD->getBaseName()),
500-
VD->getDeclContext());
501-
auto lookup = evaluateOrDefault(ctx.evaluator,
502-
UnqualifiedLookupRequest{descriptor}, {});
503-
for (auto result : lookup) {
504-
ValueDecl *RelatedVD = result.getValueDecl();
505-
if (RelatedVD->getAttrs().isUnavailable(VD->getASTContext()))
492+
493+
llvm::SmallDenseMap<DeclName, unsigned, 16> NamesSeen;
494+
++NamesSeen[VD->getName()];
495+
496+
497+
auto *DC = VD->getDeclContext();
498+
bool typeLookup = DC->isTypeContext();
499+
500+
SmallVector<ValueDecl *, 4> results;
501+
502+
if (typeLookup) {
503+
auto type = DC->getDeclaredInterfaceType();
504+
if (!type->is<ErrorType>()) {
505+
DC->lookupQualified(type, DeclNameRef(VD->getBaseName()),
506+
NL_QualifiedDefault, results);
507+
}
508+
} else {
509+
namelookup::lookupInModule(DC->getModuleScopeContext(),
510+
VD->getBaseName(), results,
511+
NLKind::UnqualifiedLookup,
512+
namelookup::ResolutionKind::Overloadable,
513+
DC->getModuleScopeContext());
514+
}
515+
516+
SmallVector<ValueDecl *, 8> RelatedDecls;
517+
for (auto result : results) {
518+
if (result->getAttrs().isUnavailable(ctx))
506519
continue;
507520

508-
if (RelatedVD != VD) {
509-
++NamesSeen[RelatedVD->getName()];
521+
if (result != VD) {
522+
++NamesSeen[result->getName()];
510523
RelatedDecls.push_back(result);
511524
}
512525
}
513526

514527
// Now provide the results along with whether the name is duplicate or not.
515-
ValueDecl *OriginalBase = VD->getDeclContext()->getSelfNominalTypeDecl();
516-
for (auto Related : RelatedDecls) {
517-
ValueDecl *RelatedVD = Related.getValueDecl();
518-
bool SameBase = Related.getBaseDecl() && Related.getBaseDecl() == OriginalBase;
519-
Fn(RelatedVD, SameBase, NamesSeen[RelatedVD->getName()] > 1);
520-
}
528+
for (auto result : RelatedDecls)
529+
Fn(result, typeLookup, NamesSeen[result->getName()] > 1);
521530
}
522531

523532
//===----------------------------------------------------------------------===//

0 commit comments

Comments
 (0)