Skip to content

Commit 8a81ea4

Browse files
committed
[Name lookup] Teach lookupQualified() to accept TypeDecls.
Rather than require clients of lookupQualified() to resolve their type declarations to nominal type declarations (and separately cope with modules), have lookupQualified() accept an array of TypeDecls and handle the resolution to nominal type declarations (where it can look directly) and module declarations, combining the results.
1 parent 6e2d3fe commit 8a81ea4

File tree

2 files changed

+37
-30
lines changed

2 files changed

+37
-30
lines changed

include/swift/AST/DeclContext.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,7 @@ class alignas(1 << DeclContextAlignInBits) DeclContext {
500500
/// lookup.
501501
///
502502
/// \returns true if anything was found.
503-
bool lookupQualified(ArrayRef<NominalTypeDecl *> types, DeclName member,
503+
bool lookupQualified(ArrayRef<TypeDecl *> types, DeclName member,
504504
NLOptions options,
505505
SmallVectorImpl<ValueDecl *> &decls) const;
506506

lib/AST/NameLookup.cpp

Lines changed: 36 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -716,7 +716,7 @@ UnqualifiedLookup::UnqualifiedLookup(DeclName Name, DeclContext *DC,
716716
if (!nominal) continue;
717717

718718
// Dig out the type we're looking into.
719-
SmallVector<NominalTypeDecl *, 2> lookupDecls;
719+
SmallVector<TypeDecl *, 2> lookupDecls;
720720
lookupDecls.push_back(nominal);
721721

722722
// For a protocol extension, check whether there are additional
@@ -793,7 +793,7 @@ UnqualifiedLookup::UnqualifiedLookup(DeclName Name, DeclContext *DC,
793793
GenericParamList *GenericParams = nullptr;
794794

795795
// Dig out the type we're looking into.
796-
SmallVector<NominalTypeDecl *, 2> lookupDecls;
796+
SmallVector<TypeDecl *, 2> lookupDecls;
797797

798798
// Local function to populate the set of lookup declarations from
799799
// the given DeclContext.
@@ -1824,10 +1824,15 @@ bool DeclContext::lookupQualified(Type type,
18241824
SmallVector<NominalTypeDecl *, 4> nominalTypesToLookInto;
18251825
extractDirectlyReferencedNominalTypes(type, nominalTypesToLookInto);
18261826

1827-
return lookupQualified(nominalTypesToLookInto, member, options, decls);
1827+
SmallVector<TypeDecl *, 4> typeDeclsToLookInto;
1828+
typeDeclsToLookInto.reserve(nominalTypesToLookInto.size());
1829+
for (auto nominal : nominalTypesToLookInto)
1830+
typeDeclsToLookInto.push_back(nominal);
1831+
1832+
return lookupQualified(typeDeclsToLookInto, member, options, decls);
18281833
}
18291834

1830-
bool DeclContext::lookupQualified(ArrayRef<NominalTypeDecl *> typeDecls,
1835+
bool DeclContext::lookupQualified(ArrayRef<TypeDecl *> typeDecls,
18311836
DeclName member,
18321837
NLOptions options,
18331838
SmallVectorImpl<ValueDecl *> &decls) const {
@@ -1856,12 +1861,33 @@ bool DeclContext::lookupQualified(ArrayRef<NominalTypeDecl *> typeDecls,
18561861
return true;
18571862
};
18581863

1859-
// Look through the type declarations we were given, resolving
1864+
// Look through the type declarations we were given, resolving them down
1865+
// to nominal type declarations, module declarations, and
18601866
ASTContext &ctx = getASTContext();
1861-
for (auto nominal : typeDecls) {
1867+
SmallVector<ModuleDecl *, 2> moduleDecls;
1868+
bool anyObject = false;
1869+
auto nominalTypeDecls =
1870+
resolveTypeDeclsToNominal(ctx.evaluator, ctx, typeDecls, moduleDecls,
1871+
anyObject);
1872+
1873+
// If the only declaration we were given was AnyObject, this is AnyObject
1874+
// lookup.
1875+
if (anyObject && nominalTypeDecls.empty() && moduleDecls.empty())
1876+
return lookupAnyObject(member, options, decls);
1877+
1878+
// Add all of the nominal types to the stack.
1879+
for (auto nominal : nominalTypeDecls) {
18621880
addNominalType(nominal);
18631881
}
18641882

1883+
// Search all of the modules.
1884+
for (auto module : moduleDecls) {
1885+
auto innerOptions = options;
1886+
innerOptions &= ~NL_RemoveOverridden;
1887+
innerOptions &= ~NL_RemoveNonVisible;
1888+
lookupQualified(module, member, innerOptions, decls);
1889+
}
1890+
18651891
// Whether we only want to return complete object initializers.
18661892
bool onlyCompleteObjectInits = false;
18671893

@@ -1960,7 +1986,6 @@ bool DeclContext::lookupQualified(ModuleDecl *module, DeclName member,
19601986
NLOptions options,
19611987
SmallVectorImpl<ValueDecl *> &decls) const {
19621988
using namespace namelookup;
1963-
assert(decls.empty() && "additive lookup not supported");
19641989

19651990
// Configure lookup and dig out the tracker.
19661991
ReferencedNameTracker *tracker = nullptr;
@@ -2185,14 +2210,6 @@ directReferencesForQualifiedTypeLookup(Evaluator &evaluator,
21852210
ArrayRef<TypeDecl *> baseTypes,
21862211
DeclName name,
21872212
DeclContext *dc) {
2188-
// Look through the base types to find something on which we can perform
2189-
// qualified name lookup.
2190-
SmallVector<ModuleDecl *, 2> moduleBaseTypes;
2191-
bool anyObject = false;
2192-
auto nominalBaseTypes =
2193-
resolveTypeDeclsToNominal(evaluator, ctx, baseTypes, moduleBaseTypes,
2194-
anyObject);
2195-
21962213
DirectlyReferencedTypeDecls result;
21972214
auto addResults = [&result](ArrayRef<ValueDecl *> found){
21982215
for (auto decl : found){
@@ -2203,21 +2220,11 @@ directReferencesForQualifiedTypeLookup(Evaluator &evaluator,
22032220
};
22042221

22052222
{
2206-
// Look through nominal types.
2207-
SmallVector<ValueDecl *, 4> nominalMembers;
2208-
auto options = NL_RemoveNonVisible | NL_OnlyTypes;
2209-
dc->lookupQualified(nominalBaseTypes, name, options, nominalMembers);
2210-
addResults(nominalMembers);
2211-
}
2212-
2213-
{
2214-
// Look through modules.
2223+
// Look into the base types.
2224+
SmallVector<ValueDecl *, 4> members;
22152225
auto options = NL_RemoveNonVisible | NL_OnlyTypes;
2216-
for (auto module : moduleBaseTypes) {
2217-
SmallVector<ValueDecl *, 4> moduleMembers;
2218-
dc->lookupQualified(module, name, options, moduleMembers);
2219-
addResults(moduleMembers);
2220-
}
2226+
dc->lookupQualified(baseTypes, name, options, members);
2227+
addResults(members);
22212228
}
22222229

22232230
return result;

0 commit comments

Comments
 (0)