Skip to content

Commit 673e167

Browse files
committed
AST: Refactor lookupQualified() to take a vector of NominalTypeDecls
Previously you could pass in a vector of TypeDecls and it handled module and AnyObject lookup for you. The AnyObject case was never used and the module was was only needed in one place, so clean things up to make them more direct here.
1 parent 8c7b924 commit 673e167

File tree

3 files changed

+28
-36
lines changed

3 files changed

+28
-36
lines changed

include/swift/AST/DeclContext.h

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

lib/AST/NameLookup.cpp

Lines changed: 26 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -603,7 +603,7 @@ SelfBoundsFromWhereClauseRequest::evaluate(Evaluator &evaluator,
603603

604604
static void
605605
populateLookupDeclsFromContext(DeclContext *dc,
606-
SmallVectorImpl<TypeDecl *> &lookupDecls) {
606+
SmallVectorImpl<NominalTypeDecl *> &lookupDecls) {
607607
auto nominal = dc->getSelfNominalTypeDecl();
608608
if (!nominal)
609609
return;
@@ -831,7 +831,7 @@ UnqualifiedLookup::UnqualifiedLookup(DeclName Name, DeclContext *DC,
831831
if (!nominal) continue;
832832

833833
// Dig out the type we're looking into.
834-
SmallVector<TypeDecl *, 2> lookupDecls;
834+
SmallVector<NominalTypeDecl *, 2> lookupDecls;
835835
populateLookupDeclsFromContext(dc, lookupDecls);
836836

837837
NLOptions options = baseNLOptions;
@@ -897,7 +897,7 @@ UnqualifiedLookup::UnqualifiedLookup(DeclName Name, DeclContext *DC,
897897
DeclContext *MetaBaseDC = nullptr;
898898
GenericParamList *GenericParams = nullptr;
899899

900-
SmallVector<TypeDecl *, 2> lookupDecls;
900+
SmallVector<NominalTypeDecl *, 2> lookupDecls;
901901

902902
if (auto *PBI = dyn_cast<PatternBindingInitializer>(DC)) {
903903
auto *PBD = PBI->getBinding();
@@ -1940,15 +1940,10 @@ bool DeclContext::lookupQualified(Type type,
19401940
SmallVector<NominalTypeDecl *, 4> nominalTypesToLookInto;
19411941
extractDirectlyReferencedNominalTypes(type, nominalTypesToLookInto);
19421942

1943-
SmallVector<TypeDecl *, 4> typeDeclsToLookInto;
1944-
typeDeclsToLookInto.reserve(nominalTypesToLookInto.size());
1945-
for (auto nominal : nominalTypesToLookInto)
1946-
typeDeclsToLookInto.push_back(nominal);
1947-
1948-
return lookupQualified(typeDeclsToLookInto, member, options, decls);
1943+
return lookupQualified(nominalTypesToLookInto, member, options, decls);
19491944
}
19501945

1951-
bool DeclContext::lookupQualified(ArrayRef<TypeDecl *> typeDecls,
1946+
bool DeclContext::lookupQualified(ArrayRef<NominalTypeDecl *> typeDecls,
19521947
DeclName member,
19531948
NLOptions options,
19541949
SmallVectorImpl<ValueDecl *> &decls) const {
@@ -1977,38 +1972,17 @@ bool DeclContext::lookupQualified(ArrayRef<TypeDecl *> typeDecls,
19771972
return true;
19781973
};
19791974

1980-
// Look through the type declarations we were given, resolving them down
1981-
// to nominal type declarations, module declarations, and
1982-
ASTContext &ctx = getASTContext();
1983-
SmallVector<ModuleDecl *, 2> moduleDecls;
1984-
bool anyObject = false;
1985-
auto nominalTypeDecls =
1986-
resolveTypeDeclsToNominal(ctx.evaluator, ctx, typeDecls, moduleDecls,
1987-
anyObject);
1988-
1989-
// If the only declaration we were given was AnyObject, this is AnyObject
1990-
// lookup.
1991-
if (anyObject && nominalTypeDecls.empty() && moduleDecls.empty())
1992-
return lookupAnyObject(member, options, decls);
1993-
19941975
// Add all of the nominal types to the stack.
1995-
for (auto nominal : nominalTypeDecls) {
1976+
for (auto nominal : typeDecls) {
19961977
addNominalType(nominal);
19971978
}
19981979

1999-
// Search all of the modules.
2000-
for (auto module : moduleDecls) {
2001-
auto innerOptions = options;
2002-
innerOptions &= ~NL_RemoveOverridden;
2003-
innerOptions &= ~NL_RemoveNonVisible;
2004-
lookupQualified(module, member, innerOptions, decls);
2005-
}
2006-
20071980
// Whether we only want to return complete object initializers.
20081981
bool onlyCompleteObjectInits = false;
20091982

20101983
// Visit all of the nominal types we know about, discovering any others
20111984
// we need along the way.
1985+
auto &ctx = getASTContext();
20121986
auto typeResolver = ctx.getLazyResolver();
20131987
bool wantProtocolMembers = (options & NL_ProtocolMembers);
20141988
while (!stack.empty()) {
@@ -2344,7 +2318,25 @@ directReferencesForQualifiedTypeLookup(Evaluator &evaluator,
23442318
// Look into the base types.
23452319
SmallVector<ValueDecl *, 4> members;
23462320
auto options = NL_RemoveNonVisible | NL_OnlyTypes;
2347-
dc->lookupQualified(baseTypes, name, options, members);
2321+
2322+
// Look through the type declarations we were given, resolving them down
2323+
// to nominal type declarations, module declarations, and
2324+
SmallVector<ModuleDecl *, 2> moduleDecls;
2325+
bool anyObject = false;
2326+
auto nominalTypeDecls =
2327+
resolveTypeDeclsToNominal(ctx.evaluator, ctx, baseTypes, moduleDecls,
2328+
anyObject);
2329+
2330+
dc->lookupQualified(nominalTypeDecls, name, options, members);
2331+
2332+
// Search all of the modules.
2333+
for (auto module : moduleDecls) {
2334+
auto innerOptions = options;
2335+
innerOptions &= ~NL_RemoveOverridden;
2336+
innerOptions &= ~NL_RemoveNonVisible;
2337+
dc->lookupQualified(module, name, innerOptions, members);
2338+
}
2339+
23482340
addResults(members);
23492341
}
23502342

lib/Sema/TypeCheckDeclOverride.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -549,7 +549,7 @@ namespace {
549549

550550
/// The set of declarations in which we'll look for overridden
551551
/// methods.
552-
DirectlyReferencedTypeDecls superContexts;
552+
SmallVector<NominalTypeDecl *, 2> superContexts;
553553

554554
/// Cached member lookup results.
555555
SmallVector<ValueDecl *, 4> members;

0 commit comments

Comments
 (0)