Skip to content

Commit ea1d5ef

Browse files
authored
Merge pull request #28816 from CodaFi/you-know-my-name-lookup-up-my-number
Add Qualified Lookup Requests
2 parents 66c2429 + f22fb81 commit ea1d5ef

File tree

7 files changed

+107
-44
lines changed

7 files changed

+107
-44
lines changed

include/swift/AST/Module.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -629,6 +629,11 @@ inline bool DeclContext::isModuleScopeContext() const {
629629
return isModuleContext();
630630
}
631631

632+
/// Extract the source location from the given module declaration.
633+
inline SourceLoc extractNearestSourceLoc(const ModuleDecl *mod) {
634+
return extractNearestSourceLoc(static_cast<const Decl *>(mod));
635+
}
636+
632637
} // end namespace swift
633638

634639
namespace llvm {

include/swift/AST/NameLookupRequests.h

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,45 @@ class AnyObjectLookupRequest
405405
NLOptions options) const;
406406
};
407407

408+
class ModuleQualifiedLookupRequest
409+
: public SimpleRequest<ModuleQualifiedLookupRequest,
410+
QualifiedLookupResult(const DeclContext *,
411+
ModuleDecl *, DeclNameRef,
412+
NLOptions),
413+
CacheKind::Uncached> {
414+
public:
415+
using SimpleRequest::SimpleRequest;
416+
417+
private:
418+
friend SimpleRequest;
419+
420+
// Evaluation.
421+
llvm::Expected<QualifiedLookupResult> evaluate(Evaluator &evaluator,
422+
const DeclContext *DC,
423+
ModuleDecl *mod, DeclNameRef name,
424+
NLOptions opts) const;
425+
};
426+
427+
class QualifiedLookupRequest
428+
: public SimpleRequest<QualifiedLookupRequest,
429+
QualifiedLookupResult(const DeclContext *,
430+
SmallVector<NominalTypeDecl *, 4>,
431+
DeclNameRef, NLOptions),
432+
CacheKind::Uncached> {
433+
public:
434+
using SimpleRequest::SimpleRequest;
435+
436+
private:
437+
friend SimpleRequest;
438+
439+
// Evaluation.
440+
llvm::Expected<QualifiedLookupResult>
441+
evaluate(Evaluator &evaluator, const DeclContext *DC,
442+
SmallVector<NominalTypeDecl *, 4> decls,
443+
DeclNameRef name,
444+
NLOptions opts) const;
445+
};
446+
408447
#define SWIFT_TYPEID_ZONE NameLookup
409448
#define SWIFT_TYPEID_HEADER "swift/AST/NameLookupTypeIDZone.def"
410449
#include "swift/Basic/DefineTypeIDZone.h"

include/swift/AST/NameLookupTypeIDZone.def

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
//
1616
//===----------------------------------------------------------------------===//
1717

18+
SWIFT_REQUEST(NameLookup, AnyObjectLookupRequest,
19+
QualifiedLookupResult(const DeclContext *, DeclName, NLOptions),
20+
Uncached, NoLocationInfo)
1821
SWIFT_REQUEST(NameLookup, CustomAttrNominalRequest,
1922
NominalTypeDecl *(CustomAttr *, DeclContext *), Cached,
2023
NoLocationInfo)
@@ -34,6 +37,20 @@ SWIFT_REQUEST(NameLookup, InheritedDeclsReferencedRequest,
3437
DirectlyReferencedTypeDecls(
3538
llvm::PointerUnion<TypeDecl *, ExtensionDecl *>, unsigned),
3639
Uncached, HasNearestLocation)
40+
SWIFT_REQUEST(NameLookup, LookupInModuleRequest,
41+
QualifiedLookupResult(const DeclContext *, DeclName, NLKind,
42+
namelookup::ResolutionKind,
43+
const DeclContext *),
44+
Uncached, NoLocationInfo)
45+
SWIFT_REQUEST(NameLookup, ModuleQualifiedLookupRequest,
46+
QualifiedLookupResult(
47+
const DeclContext *, ModuleDecl *, DeclNameRef, NLOptions),
48+
Uncached, NoLocationInfo)
49+
SWIFT_REQUEST(NameLookup, QualifiedLookupRequest,
50+
QualifiedLookupResult(
51+
const DeclContext *, SmallVector<NominalTypeDecl *, 4>,
52+
DeclNameRef, NLOptions),
53+
Uncached, NoLocationInfo)
3754
SWIFT_REQUEST(NameLookup, SelfBoundsFromWhereClauseRequest,
3855
SelfBounds(llvm::PointerUnion<TypeDecl *, ExtensionDecl *>),
3956
Uncached, NoLocationInfo)
@@ -48,11 +65,3 @@ SWIFT_REQUEST(NameLookup, UnderlyingTypeDeclsReferencedRequest,
4865
SWIFT_REQUEST(NameLookup, UnqualifiedLookupRequest,
4966
LookupResult(UnqualifiedLookupDescriptor), Uncached,
5067
NoLocationInfo)
51-
SWIFT_REQUEST(NameLookup, LookupInModuleRequest,
52-
QualifiedLookupResult(const DeclContext *, DeclName, NLKind,
53-
namelookup::ResolutionKind,
54-
const DeclContext *),
55-
Uncached, NoLocationInfo)
56-
SWIFT_REQUEST(NameLookup, AnyObjectLookupRequest,
57-
QualifiedLookupResult(const DeclContext *, DeclName, NLOptions),
58-
Uncached, NoLocationInfo)

include/swift/Basic/Statistics.def

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -138,12 +138,6 @@ FRONTEND_STATISTIC(AST, NumPrefixOperators)
138138
/// Number of precedence groups in the AST context.
139139
FRONTEND_STATISTIC(AST, NumPrecedenceGroups)
140140

141-
/// Number of qualified lookups into a nominal type.
142-
FRONTEND_STATISTIC(AST, NumLookupQualifiedInNominal)
143-
144-
/// Number of qualified lookups into a module.
145-
FRONTEND_STATISTIC(AST, NumLookupQualifiedInModule)
146-
147141
/// Number of local lookups into a module.
148142
FRONTEND_STATISTIC(AST, NumModuleLookupValue)
149143

lib/AST/NameLookup.cpp

Lines changed: 40 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1587,17 +1587,24 @@ bool DeclContext::lookupQualified(ArrayRef<NominalTypeDecl *> typeDecls,
15871587
DeclNameRef member,
15881588
NLOptions options,
15891589
SmallVectorImpl<ValueDecl *> &decls) const {
1590-
using namespace namelookup;
15911590
assert(decls.empty() && "additive lookup not supported");
1591+
QualifiedLookupRequest req{this, {typeDecls.begin(), typeDecls.end()},
1592+
member, options};
1593+
decls = evaluateOrDefault(getASTContext().evaluator, req, {});
1594+
return !decls.empty();
1595+
}
15921596

1593-
auto *stats = getASTContext().Stats;
1594-
if (stats)
1595-
stats->getFrontendCounters().NumLookupQualifiedInNominal++;
1597+
llvm::Expected<QualifiedLookupResult>
1598+
QualifiedLookupRequest::evaluate(Evaluator &eval, const DeclContext *DC,
1599+
SmallVector<NominalTypeDecl *, 4> typeDecls,
1600+
DeclNameRef member, NLOptions options) const {
1601+
using namespace namelookup;
1602+
QualifiedLookupResult decls;
15961603

15971604
// Configure lookup and dig out the tracker.
15981605
ReferencedNameTracker *tracker = nullptr;
15991606
bool isLookupCascading;
1600-
configureLookup(this, options, tracker, isLookupCascading);
1607+
configureLookup(DC, options, tracker, isLookupCascading);
16011608

16021609
// Tracking for the nominal types we'll visit.
16031610
SmallVector<NominalTypeDecl *, 4> stack;
@@ -1645,8 +1652,7 @@ bool DeclContext::lookupQualified(ArrayRef<NominalTypeDecl *> typeDecls,
16451652
if ((options & NL_OnlyTypes) && !isa<TypeDecl>(decl))
16461653
continue;
16471654

1648-
if (isAcceptableLookupResult(this, options, decl,
1649-
onlyCompleteObjectInits))
1655+
if (isAcceptableLookupResult(DC, options, decl, onlyCompleteObjectInits))
16501656
decls.push_back(decl);
16511657
}
16521658

@@ -1706,34 +1712,40 @@ bool DeclContext::lookupQualified(ArrayRef<NominalTypeDecl *> typeDecls,
17061712
}
17071713
}
17081714

1709-
pruneLookupResultSet(this, options, decls);
1710-
if (auto *debugClient = this->getParentModule()->getDebugClient()) {
1711-
debugClient->finishLookupInNominals(this, typeDecls, member.getFullName(),
1715+
pruneLookupResultSet(DC, options, decls);
1716+
if (auto *debugClient = DC->getParentModule()->getDebugClient()) {
1717+
debugClient->finishLookupInNominals(DC, typeDecls, member.getFullName(),
17121718
options, decls);
17131719
}
1714-
// We're done. Report success/failure.
1715-
return !decls.empty();
1720+
1721+
return decls;
17161722
}
17171723

17181724
bool DeclContext::lookupQualified(ModuleDecl *module, DeclNameRef member,
17191725
NLOptions options,
17201726
SmallVectorImpl<ValueDecl *> &decls) const {
1721-
using namespace namelookup;
1727+
assert(decls.empty() && "additive lookup not supported");
1728+
ModuleQualifiedLookupRequest req{this, module, member, options};
1729+
decls = evaluateOrDefault(getASTContext().evaluator, req, {});
1730+
return !decls.empty();
1731+
}
17221732

1723-
auto &ctx = getASTContext();
1724-
auto *stats = ctx.Stats;
1725-
if (stats)
1726-
stats->getFrontendCounters().NumLookupQualifiedInModule++;
1733+
llvm::Expected<QualifiedLookupResult>
1734+
ModuleQualifiedLookupRequest::evaluate(Evaluator &eval, const DeclContext *DC,
1735+
ModuleDecl *module, DeclNameRef member,
1736+
NLOptions options) const {
1737+
using namespace namelookup;
1738+
QualifiedLookupResult decls;
17271739

17281740
// Configure lookup and dig out the tracker.
17291741
ReferencedNameTracker *tracker = nullptr;
17301742
bool isLookupCascading;
1731-
configureLookup(this, options, tracker, isLookupCascading);
1743+
configureLookup(DC, options, tracker, isLookupCascading);
17321744

17331745
auto kind = (options & NL_OnlyTypes
17341746
? ResolutionKind::TypesOnly
17351747
: ResolutionKind::Overloadable);
1736-
auto topLevelScope = getModuleScopeContext();
1748+
auto topLevelScope = DC->getModuleScopeContext();
17371749
if (module == topLevelScope->getParentModule()) {
17381750
if (tracker) {
17391751
recordLookupOfTopLevelName(topLevelScope, member.getFullName(),
@@ -1748,6 +1760,7 @@ bool DeclContext::lookupQualified(ModuleDecl *module, DeclNameRef member,
17481760
// anything in this one.
17491761

17501762
// Perform the lookup in all imports of this module.
1763+
auto &ctx = DC->getASTContext();
17511764
auto accessPaths = ctx.getImportCache().getAllVisibleAccessPaths(
17521765
module, topLevelScope);
17531766
if (llvm::any_of(accessPaths,
@@ -1760,14 +1773,14 @@ bool DeclContext::lookupQualified(ModuleDecl *module, DeclNameRef member,
17601773
}
17611774
}
17621775

1763-
pruneLookupResultSet(this, options, decls);
1776+
pruneLookupResultSet(DC, options, decls);
17641777

1765-
if (auto *debugClient = this->getParentModule()->getDebugClient()) {
1766-
debugClient->finishLookupInModule(this, module, member.getFullName(),
1778+
if (auto *debugClient = DC->getParentModule()->getDebugClient()) {
1779+
debugClient->finishLookupInModule(DC, module, member.getFullName(),
17671780
options, decls);
17681781
}
1769-
// We're done. Report success/failure.
1770-
return !decls.empty();
1782+
1783+
return decls;
17711784
}
17721785

17731786
llvm::Expected<QualifiedLookupResult>
@@ -1997,7 +2010,9 @@ directReferencesForQualifiedTypeLookup(Evaluator &evaluator,
19972010
auto innerOptions = options;
19982011
innerOptions &= ~NL_RemoveOverridden;
19992012
innerOptions &= ~NL_RemoveNonVisible;
2000-
dc->lookupQualified(module, name, innerOptions, members);
2013+
SmallVector<ValueDecl *, 4> moduleMembers;
2014+
dc->lookupQualified(module, name, innerOptions, moduleMembers);
2015+
members.append(moduleMembers.begin(), moduleMembers.end());
20012016
}
20022017

20032018
addResults(members);

test/decl/circularity.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ class C4 {
9696
required init(x: Int) {}
9797
}
9898

99-
class D4 : C4, P1 { // expected-note {{through reference here}}
99+
class D4 : C4, P1 { // expected-note 2 {{through reference here}}
100100
required init(x: X) { // expected-error {{circular reference}}
101101
// expected-note@-1 2{{through reference here}}
102102
super.init(x: x)

test/decl/class/circular_inheritance.swift

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88
// Check that we produced superclass type requests.
99
// RUN: %{python} %utils/process-stats-dir.py --evaluate 'SuperclassTypeRequest == 18' %t/stats-dir
1010

11-
class Left // expected-error {{circular reference}}
11+
class Left // expected-error {{circular reference}} expected-note {{through reference here}}
1212
: Right.Hand { // expected-note {{through reference here}}
1313
class Hand {}
1414
}
1515

16-
class Right // expected-note {{through reference here}}
16+
class Right // expected-note 2 {{through reference here}}
1717
: Left.Hand { // expected-note {{through reference here}}
1818
class Hand {}
1919
}
@@ -35,13 +35,13 @@ class Outer {
3535
class Inner : Outer {}
3636
}
3737

38-
class Outer2 // expected-error {{circular reference}}
38+
class Outer2 // expected-error {{circular reference}} expected-note {{through reference here}}
3939
: Outer2.Inner { // expected-note {{through reference here}}
4040

4141
class Inner {}
4242
}
4343

44-
class Outer3 // expected-error {{circular reference}}
44+
class Outer3 // expected-error {{circular reference}} expected-note {{through reference here}}
4545
: Outer3.Inner<Int> { // expected-note {{through reference here}}
4646
class Inner<T> {}
4747
}
@@ -79,6 +79,7 @@ class WithDesignatedInit : WithDesignatedInit {
7979
// expected-error@-1 {{'WithDesignatedInit' inherits from itself}}
8080
// expected-error@-2 {{circular reference}}
8181
// expected-note@-3 {{through reference here}}
82+
// expected-note@-4 2 {{through reference here}}
8283

8384
init(x: Int) {} // expected-error {{circular reference}}
8485
}

0 commit comments

Comments
 (0)