Skip to content

Commit d4ac04d

Browse files
authored
Move access-path filtering into ModuleNameLookup (out of lookupValue) (swiftlang#27097)
Removes duplicated logic from the implementations of FileUnit::lookupValue, and simplifies the interface to ModuleDecl::lookupValue, where everyone was passing an empty (non-filtering) access path anyway /except/ during actual lookup from source code. No functionality change.
1 parent 04ae94f commit d4ac04d

21 files changed

+52
-83
lines changed

include/swift/AST/Module.h

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ class ModuleDecl : public DeclContext, public TypeDecl {
361361
/// within the current module.
362362
///
363363
/// This does a simple local lookup, not recursively looking through imports.
364-
void lookupValue(AccessPathTy AccessPath, DeclName Name, NLKind LookupKind,
364+
void lookupValue(DeclName Name, NLKind LookupKind,
365365
SmallVectorImpl<ValueDecl*> &Result) const;
366366

367367
/// Look up a local type declaration by its mangled name.
@@ -629,8 +629,7 @@ class FileUnit : public DeclContext {
629629
/// within this file.
630630
///
631631
/// This does a simple local lookup, not recursively looking through imports.
632-
virtual void lookupValue(ModuleDecl::AccessPathTy accessPath, DeclName name,
633-
NLKind lookupKind,
632+
virtual void lookupValue(DeclName name, NLKind lookupKind,
634633
SmallVectorImpl<ValueDecl*> &result) const = 0;
635634

636635
/// Look up a local type declaration by its mangled name.
@@ -1056,8 +1055,7 @@ class SourceFile final : public FileUnit {
10561055
void cacheVisibleDecls(SmallVectorImpl<ValueDecl *> &&globals) const;
10571056
const SmallVectorImpl<ValueDecl *> &getCachedVisibleDecls() const;
10581057

1059-
virtual void lookupValue(ModuleDecl::AccessPathTy accessPath, DeclName name,
1060-
NLKind lookupKind,
1058+
virtual void lookupValue(DeclName name, NLKind lookupKind,
10611059
SmallVectorImpl<ValueDecl*> &result) const override;
10621060

10631061
virtual void lookupVisibleDecls(ModuleDecl::AccessPathTy accessPath,
@@ -1281,8 +1279,7 @@ class BuiltinUnit final : public FileUnit {
12811279
public:
12821280
explicit BuiltinUnit(ModuleDecl &M);
12831281

1284-
virtual void lookupValue(ModuleDecl::AccessPathTy accessPath, DeclName name,
1285-
NLKind lookupKind,
1282+
virtual void lookupValue(DeclName name, NLKind lookupKind,
12861283
SmallVectorImpl<ValueDecl*> &result) const override;
12871284

12881285
/// Find all Objective-C methods with the given selector.

include/swift/ClangImporter/ClangModule.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,7 @@ class ClangModuleUnit final : public LoadedFile {
6464

6565
virtual bool isSystemModule() const override;
6666

67-
virtual void lookupValue(ModuleDecl::AccessPathTy accessPath,
68-
DeclName name, NLKind lookupKind,
67+
virtual void lookupValue(DeclName name, NLKind lookupKind,
6968
SmallVectorImpl<ValueDecl*> &results) const override;
7069

7170
virtual TypeDecl *

include/swift/Serialization/SerializedModuleLoader.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,8 +268,7 @@ class SerializedASTFile final : public LoadedFile {
268268

269269
virtual bool isSystemModule() const override;
270270

271-
virtual void lookupValue(ModuleDecl::AccessPathTy accessPath,
272-
DeclName name, NLKind lookupKind,
271+
virtual void lookupValue(DeclName name, NLKind lookupKind,
273272
SmallVectorImpl<ValueDecl*> &results) const override;
274273

275274
virtual StringRef

lib/AST/ASTContext.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -654,7 +654,7 @@ void ASTContext::lookupInSwiftModule(
654654

655655
// Find all of the declarations with this name in the Swift module.
656656
auto identifier = getIdentifier(name);
657-
M->lookupValue({ }, identifier, NLKind::UnqualifiedLookup, results);
657+
M->lookupValue(identifier, NLKind::UnqualifiedLookup, results);
658658
}
659659

660660
FuncDecl *ASTContext::getPlusFunctionOnRangeReplaceableCollection() const {
@@ -832,7 +832,7 @@ StructDecl *ASTContext::getObjCBoolDecl() const {
832832
SmallVector<ValueDecl *, 1> results;
833833
auto *Context = const_cast<ASTContext *>(this);
834834
if (ModuleDecl *M = Context->getModuleByName(Id_ObjectiveC.str())) {
835-
M->lookupValue({ }, getIdentifier("ObjCBool"), NLKind::UnqualifiedLookup,
835+
M->lookupValue(getIdentifier("ObjCBool"), NLKind::UnqualifiedLookup,
836836
results);
837837
for (auto result : results) {
838838
if (auto structDecl = dyn_cast<StructDecl>(result)) {
@@ -899,7 +899,7 @@ ProtocolDecl *ASTContext::getProtocol(KnownProtocolKind kind) const {
899899

900900
if (!M)
901901
return nullptr;
902-
M->lookupValue({ }, getIdentifier(getProtocolName(kind)),
902+
M->lookupValue(getIdentifier(getProtocolName(kind)),
903903
NLKind::UnqualifiedLookup, results);
904904

905905
for (auto result : results) {
@@ -3959,7 +3959,7 @@ static NominalTypeDecl *findUnderlyingTypeInModule(ASTContext &ctx,
39593959
ModuleDecl *module) {
39603960
// Find all of the declarations with this name in the Swift module.
39613961
SmallVector<ValueDecl *, 1> results;
3962-
module->lookupValue({ }, name, NLKind::UnqualifiedLookup, results);
3962+
module->lookupValue(name, NLKind::UnqualifiedLookup, results);
39633963
for (auto result : results) {
39643964
if (auto nominal = dyn_cast<NominalTypeDecl>(result))
39653965
return nominal;

lib/AST/ASTDemangler.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,9 @@ ASTBuilder::createBuiltinType(StringRef builtinName,
9797
if (builtinName.startswith(BUILTIN_TYPE_NAME_PREFIX)) {
9898
SmallVector<ValueDecl *, 1> decls;
9999

100-
ModuleDecl::AccessPathTy accessPath;
101100
StringRef strippedName =
102101
builtinName.drop_front(BUILTIN_TYPE_NAME_PREFIX.size());
103-
Ctx.TheBuiltinModule->lookupValue(accessPath,
104-
Ctx.getIdentifier(strippedName),
102+
Ctx.TheBuiltinModule->lookupValue(Ctx.getIdentifier(strippedName),
105103
NLKind::QualifiedLookup,
106104
decls);
107105

lib/AST/Module.cpp

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,8 @@ class swift::SourceLookupCache {
168168
/// Throw away as much memory as possible.
169169
void invalidate();
170170

171-
void lookupValue(AccessPathTy AccessPath, DeclName Name,
172-
NLKind LookupKind, SmallVectorImpl<ValueDecl*> &Result);
171+
void lookupValue(DeclName Name, NLKind LookupKind,
172+
SmallVectorImpl<ValueDecl*> &Result);
173173

174174
void lookupVisibleDecls(AccessPathTy AccessPath,
175175
VisibleDeclConsumer &Consumer,
@@ -284,14 +284,8 @@ SourceLookupCache::SourceLookupCache(const ModuleDecl &M) {
284284
}
285285
}
286286

287-
void SourceLookupCache::lookupValue(AccessPathTy AccessPath, DeclName Name,
288-
NLKind LookupKind,
287+
void SourceLookupCache::lookupValue(DeclName Name, NLKind LookupKind,
289288
SmallVectorImpl<ValueDecl*> &Result) {
290-
// If this import is specific to some named type or decl ("import Swift.int")
291-
// then filter out any lookups that don't match.
292-
if (!ModuleDecl::matchesAccessPath(AccessPath, Name))
293-
return;
294-
295289
auto I = TopLevelValues.find(Name);
296290
if (I == TopLevelValues.end()) return;
297291

@@ -457,19 +451,18 @@ static bool isParsedModule(const ModuleDecl *mod) {
457451
cast<SourceFile>(files[0])->Kind != SourceFileKind::SIL);
458452
}
459453

460-
void ModuleDecl::lookupValue(AccessPathTy AccessPath, DeclName Name,
461-
NLKind LookupKind,
454+
void ModuleDecl::lookupValue(DeclName Name, NLKind LookupKind,
462455
SmallVectorImpl<ValueDecl*> &Result) const {
463456
auto *stats = getASTContext().Stats;
464457
if (stats)
465458
stats->getFrontendCounters().NumModuleLookupValue++;
466459

467460
if (isParsedModule(this)) {
468-
getSourceLookupCache().lookupValue(AccessPath, Name, LookupKind, Result);
461+
getSourceLookupCache().lookupValue(Name, LookupKind, Result);
469462
return;
470463
}
471464

472-
FORWARD(lookupValue, (AccessPath, Name, LookupKind, Result));
465+
FORWARD(lookupValue, (Name, LookupKind, Result));
473466
}
474467

475468
TypeDecl * ModuleDecl::lookupLocalType(StringRef MangledName) const {
@@ -517,7 +510,7 @@ void ModuleDecl::lookupMember(SmallVectorImpl<ValueDecl*> &results,
517510
alreadyInPrivateContext = true;
518511
} else if (isa<ModuleDecl>(containerDecl)) {
519512
assert(container == this);
520-
this->lookupValue({}, name, NLKind::QualifiedLookup, results);
513+
this->lookupValue(name, NLKind::QualifiedLookup, results);
521514
} else if (!isa<GenericTypeDecl>(containerDecl)) {
522515
// If ExtensionDecl, then use ExtensionDecl::lookupDirect instead.
523516
llvm_unreachable("This context does not support lookup.");
@@ -557,8 +550,7 @@ void ModuleDecl::lookupObjCMethods(
557550
FORWARD(lookupObjCMethods, (selector, results));
558551
}
559552

560-
void BuiltinUnit::lookupValue(ModuleDecl::AccessPathTy accessPath, DeclName name,
561-
NLKind lookupKind,
553+
void BuiltinUnit::lookupValue(DeclName name, NLKind lookupKind,
562554
SmallVectorImpl<ValueDecl*> &result) const {
563555
getCache().lookupValue(name.getBaseIdentifier(), lookupKind, *this, result);
564556
}
@@ -569,10 +561,9 @@ void BuiltinUnit::lookupObjCMethods(
569561
// No @objc methods in the Builtin module.
570562
}
571563

572-
void SourceFile::lookupValue(ModuleDecl::AccessPathTy accessPath, DeclName name,
573-
NLKind lookupKind,
564+
void SourceFile::lookupValue(DeclName name, NLKind lookupKind,
574565
SmallVectorImpl<ValueDecl*> &result) const {
575-
getCache().lookupValue(accessPath, name, lookupKind, result);
566+
getCache().lookupValue(name, lookupKind, result);
576567
}
577568

578569
void ModuleDecl::lookupVisibleDecls(AccessPathTy AccessPath,

lib/AST/ModuleNameLookup.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,11 @@ class LookupByName : public ModuleNameLookup<LookupByName> {
8080

8181
void doLocalLookup(ModuleDecl *module, ModuleDecl::AccessPathTy path,
8282
SmallVectorImpl<ValueDecl *> &localDecls) {
83-
module->lookupValue(path, name, lookupKind, localDecls);
83+
// If this import is specific to some named decl ("import Swift.Int")
84+
// then filter out any lookups that don't match.
85+
if (!ModuleDecl::matchesAccessPath(path, name))
86+
return;
87+
module->lookupValue(name, lookupKind, localDecls);
8488
}
8589
};
8690

lib/ClangImporter/ClangImporter.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2728,12 +2728,8 @@ void ClangModuleUnit::getDisplayDecls(SmallVectorImpl<Decl*> &results) const {
27282728
getTopLevelDecls(results);
27292729
}
27302730

2731-
void ClangModuleUnit::lookupValue(ModuleDecl::AccessPathTy accessPath,
2732-
DeclName name, NLKind lookupKind,
2731+
void ClangModuleUnit::lookupValue(DeclName name, NLKind lookupKind,
27332732
SmallVectorImpl<ValueDecl*> &results) const {
2734-
if (!ModuleDecl::matchesAccessPath(accessPath, name))
2735-
return;
2736-
27372733
// FIXME: Ignore submodules, which are empty for now.
27382734
if (clangModule && clangModule->isSubModule())
27392735
return;

lib/ClangImporter/DWARFImporter.cpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,9 @@ class DWARFModuleUnit final : public LoadedFile {
3232
/// Forwards the request to the ClangImporter, which forwards it to the
3333
/// DWARFimporterDelegate.
3434
virtual void
35-
lookupValue(ModuleDecl::AccessPathTy accessPath, DeclName name,
36-
NLKind lookupKind,
35+
lookupValue(DeclName name, NLKind lookupKind,
3736
SmallVectorImpl<ValueDecl *> &results) const override {
38-
Owner.lookupValueDWARF(accessPath, name, lookupKind,
37+
Owner.lookupValueDWARF(name, lookupKind,
3938
getParentModule()->getName(), results);
4039
}
4140

@@ -130,11 +129,8 @@ ModuleDecl *ClangImporter::Implementation::loadModuleDWARF(
130129
}
131130

132131
void ClangImporter::Implementation::lookupValueDWARF(
133-
ModuleDecl::AccessPathTy accessPath, DeclName name, NLKind lookupKind,
134-
Identifier inModule, SmallVectorImpl<ValueDecl *> &results) {
135-
if (!swift::ModuleDecl::matchesAccessPath(accessPath, name))
136-
return;
137-
132+
DeclName name, NLKind lookupKind, Identifier inModule,
133+
SmallVectorImpl<ValueDecl *> &results) {
138134
if (lookupKind != NLKind::QualifiedLookup)
139135
return;
140136

lib/ClangImporter/ImportDecl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4576,7 +4576,7 @@ namespace {
45764576
Impl.SwiftContext.LangOpts.EffectiveLanguageVersion;
45774577

45784578
SmallVector<ValueDecl *, 4> results;
4579-
overlay->lookupValue({}, name, NLKind::QualifiedLookup, results);
4579+
overlay->lookupValue(name, NLKind::QualifiedLookup, results);
45804580
T *found = nullptr;
45814581
for (auto result : results) {
45824582
if (auto singleResult = dyn_cast<T>(result)) {

0 commit comments

Comments
 (0)