Skip to content

Commit acf282b

Browse files
authored
Merge pull request #28689 from hamishknight/lookup-requests
[NameLookup] Add requests for module and AnyObject lookup (master)
2 parents 43a3ab7 + 6811586 commit acf282b

File tree

8 files changed

+145
-42
lines changed

8 files changed

+145
-42
lines changed

include/swift/AST/DeclContext.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -530,10 +530,6 @@ class alignas(1 << DeclContextAlignInBits) DeclContext {
530530
bool lookupQualified(ModuleDecl *module, DeclName member, NLOptions options,
531531
SmallVectorImpl<ValueDecl *> &decls) const;
532532

533-
/// Perform \c AnyObject lookup for the given member.
534-
bool lookupAnyObject(DeclName member, NLOptions options,
535-
SmallVectorImpl<ValueDecl *> &decls) const;
536-
537533
/// Look up all Objective-C methods with the given selector visible
538534
/// in the enclosing module.
539535
void lookupAllObjCMethods(

include/swift/AST/LookupKinds.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ enum class NLKind {
2626
QualifiedLookup
2727
};
2828

29+
void simple_display(llvm::raw_ostream &out, NLKind kind);
30+
2931
/// Constants used to customize name lookup.
3032
enum NLOptions : unsigned {
3133
/// Consider declarations within protocols to which the context type conforms.
@@ -103,6 +105,8 @@ static inline NLOptions operator~(NLOptions value) {
103105
return NLOptions(~(unsigned)value);
104106
}
105107

108+
void simple_display(llvm::raw_ostream &out, NLOptions options);
109+
106110
} // end namespace swift
107111

108112
#endif

include/swift/AST/ModuleNameLookup.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ enum class ResolutionKind {
3939
TypesOnly
4040
};
4141

42+
void simple_display(llvm::raw_ostream &out, ResolutionKind kind);
43+
4244
/// Performs a lookup into the given module and it's imports.
4345
///
4446
/// If 'moduleOrFile' is a ModuleDecl, we search the module and it's

include/swift/AST/NameLookupRequests.h

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class DestructorDecl;
3131
class GenericContext;
3232
class GenericParamList;
3333
class LookupResult;
34+
enum class NLKind;
3435
class SourceLoc;
3536
class TypeAliasDecl;
3637
class TypeDecl;
@@ -39,6 +40,9 @@ namespace ast_scope {
3940
class ASTScopeImpl;
4041
class ScopeCreator;
4142
} // namespace ast_scope
43+
namespace namelookup {
44+
enum class ResolutionKind;
45+
} // namespace namelookup
4246

4347
/// Display a nominal type or extension thereof.
4448
void simple_display(
@@ -360,6 +364,46 @@ class UnqualifiedLookupRequest
360364
UnqualifiedLookupDescriptor desc) const;
361365
};
362366

367+
using QualifiedLookupResult = SmallVector<ValueDecl *, 4>;
368+
369+
/// Performs a lookup into a given module and its imports.
370+
class LookupInModuleRequest
371+
: public SimpleRequest<LookupInModuleRequest,
372+
QualifiedLookupResult(
373+
const DeclContext *, DeclName, NLKind,
374+
namelookup::ResolutionKind, const DeclContext *),
375+
CacheKind::Uncached> {
376+
public:
377+
using SimpleRequest::SimpleRequest;
378+
379+
private:
380+
friend SimpleRequest;
381+
382+
// Evaluation.
383+
llvm::Expected<QualifiedLookupResult>
384+
evaluate(Evaluator &evaluator, const DeclContext *moduleOrFile, DeclName name,
385+
NLKind lookupKind, namelookup::ResolutionKind resolutionKind,
386+
const DeclContext *moduleScopeContext) const;
387+
};
388+
389+
/// Perform \c AnyObject lookup for a given member.
390+
class AnyObjectLookupRequest
391+
: public SimpleRequest<AnyObjectLookupRequest,
392+
QualifiedLookupResult(const DeclContext *, DeclName,
393+
NLOptions),
394+
CacheKind::Uncached> {
395+
public:
396+
using SimpleRequest::SimpleRequest;
397+
398+
private:
399+
friend SimpleRequest;
400+
401+
llvm::Expected<QualifiedLookupResult> evaluate(Evaluator &evaluator,
402+
const DeclContext *dc,
403+
DeclName name,
404+
NLOptions options) const;
405+
};
406+
363407
#define SWIFT_TYPEID_ZONE NameLookup
364408
#define SWIFT_TYPEID_HEADER "swift/AST/NameLookupTypeIDZone.def"
365409
#include "swift/Basic/DefineTypeIDZone.h"

include/swift/AST/NameLookupTypeIDZone.def

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,11 @@ SWIFT_REQUEST(NameLookup, UnderlyingTypeDeclsReferencedRequest,
4848
SWIFT_REQUEST(NameLookup, UnqualifiedLookupRequest,
4949
LookupResult(UnqualifiedLookupDescriptor), Uncached,
5050
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
@@ -144,12 +144,6 @@ FRONTEND_STATISTIC(AST, NumLookupQualifiedInNominal)
144144
/// Number of qualified lookups into a module.
145145
FRONTEND_STATISTIC(AST, NumLookupQualifiedInModule)
146146

147-
/// Number of qualified lookups into AnyObject.
148-
FRONTEND_STATISTIC(AST, NumLookupQualifiedInAnyObject)
149-
150-
/// Number of lookups into a module and its imports.
151-
FRONTEND_STATISTIC(AST, NumLookupInModule)
152-
153147
/// Number of local lookups into a module.
154148
FRONTEND_STATISTIC(AST, NumModuleLookupValue)
155149

lib/AST/ModuleNameLookup.cpp

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "swift/AST/ClangModuleLoader.h"
1616
#include "swift/AST/ImportCache.h"
1717
#include "swift/AST/NameLookup.h"
18+
#include "swift/AST/NameLookupRequests.h"
1819
#include "llvm/Support/raw_ostream.h"
1920

2021
using namespace swift;
@@ -228,23 +229,32 @@ void ModuleNameLookup<LookupStrategy>::lookupInModule(
228229
decls.end());
229230
}
230231

232+
llvm::Expected<QualifiedLookupResult> LookupInModuleRequest::evaluate(
233+
Evaluator &evaluator, const DeclContext *moduleOrFile, DeclName name,
234+
NLKind lookupKind, ResolutionKind resolutionKind,
235+
const DeclContext *moduleScopeContext) const {
236+
assert(moduleScopeContext->isModuleScopeContext());
237+
238+
auto &ctx = moduleOrFile->getASTContext();
239+
FrontendStatsTracer tracer(ctx.Stats, "lookup-in-module");
240+
241+
QualifiedLookupResult decls;
242+
LookupByName lookup(ctx, resolutionKind, name, lookupKind);
243+
lookup.lookupInModule(decls, moduleOrFile, {}, moduleScopeContext);
244+
return decls;
245+
}
246+
231247
void namelookup::lookupInModule(const DeclContext *moduleOrFile,
232248
DeclName name,
233249
SmallVectorImpl<ValueDecl *> &decls,
234250
NLKind lookupKind,
235251
ResolutionKind resolutionKind,
236252
const DeclContext *moduleScopeContext) {
237-
assert(moduleScopeContext->isModuleScopeContext());
238-
239253
auto &ctx = moduleOrFile->getASTContext();
240-
auto *stats = ctx.Stats;
241-
if (stats)
242-
stats->getFrontendCounters().NumLookupInModule++;
243-
244-
FrontendStatsTracer tracer(stats, "lookup-in-module");
245-
246-
LookupByName lookup(ctx, resolutionKind, name, lookupKind);
247-
lookup.lookupInModule(decls, moduleOrFile, {}, moduleScopeContext);
254+
LookupInModuleRequest req(moduleOrFile, name, lookupKind, resolutionKind,
255+
moduleScopeContext);
256+
auto results = evaluateOrDefault(ctx.evaluator, req, {});
257+
decls.append(results.begin(), results.end());
248258
}
249259

250260
void namelookup::lookupVisibleDeclsInModule(
@@ -260,3 +270,14 @@ void namelookup::lookupVisibleDeclsInModule(
260270
lookup.lookupInModule(decls, moduleOrFile, accessPath, moduleScopeContext);
261271
}
262272

273+
void namelookup::simple_display(llvm::raw_ostream &out, ResolutionKind kind) {
274+
switch (kind) {
275+
case ResolutionKind::Overloadable:
276+
out << "Overloadable";
277+
return;
278+
case ResolutionKind::TypesOnly:
279+
out << "TypesOnly";
280+
return;
281+
}
282+
llvm_unreachable("Unhandled case in switch");
283+
}

lib/AST/NameLookup.cpp

Lines changed: 56 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1565,8 +1565,11 @@ bool DeclContext::lookupQualified(Type type,
15651565
assert(decls.empty() && "additive lookup not supported");
15661566

15671567
// Handle AnyObject lookup.
1568-
if (type->isAnyObject())
1569-
return lookupAnyObject(member, options, decls);
1568+
if (type->isAnyObject()) {
1569+
AnyObjectLookupRequest req(this, member, options);
1570+
decls = evaluateOrDefault(getASTContext().evaluator, req, {});
1571+
return !decls.empty();
1572+
}
15701573

15711574
// Handle lookup in a module.
15721575
if (auto moduleTy = type->getAs<ModuleType>())
@@ -1763,31 +1766,28 @@ bool DeclContext::lookupQualified(ModuleDecl *module, DeclName member,
17631766
return !decls.empty();
17641767
}
17651768

1766-
bool DeclContext::lookupAnyObject(DeclName member, NLOptions options,
1767-
SmallVectorImpl<ValueDecl *> &decls) const {
1769+
llvm::Expected<QualifiedLookupResult>
1770+
AnyObjectLookupRequest::evaluate(Evaluator &evaluator, const DeclContext *dc,
1771+
DeclName member, NLOptions options) const {
17681772
using namespace namelookup;
1769-
assert(decls.empty() && "additive lookup not supported");
1773+
QualifiedLookupResult decls;
17701774

17711775
// Configure lookup and dig out the tracker.
17721776
ReferencedNameTracker *tracker = nullptr;
17731777
bool isLookupCascading;
1774-
configureLookup(this, options, tracker, isLookupCascading);
1778+
configureLookup(dc, options, tracker, isLookupCascading);
17751779

17761780
// Record this lookup.
17771781
if (tracker)
17781782
tracker->addDynamicLookupName(member.getBaseName(), isLookupCascading);
17791783

17801784
// Type-only lookup won't find anything on AnyObject.
17811785
if (options & NL_OnlyTypes)
1782-
return false;
1783-
1784-
auto *stats = getASTContext().Stats;
1785-
if (stats)
1786-
stats->getFrontendCounters().NumLookupQualifiedInAnyObject++;
1786+
return decls;
17871787

17881788
// Collect all of the visible declarations.
17891789
SmallVector<ValueDecl *, 4> allDecls;
1790-
for (auto import : namelookup::getAllImports(this)) {
1790+
for (auto import : namelookup::getAllImports(dc)) {
17911791
import.second->lookupClassMember(import.first, member, allDecls);
17921792
}
17931793

@@ -1805,26 +1805,23 @@ bool DeclContext::lookupAnyObject(DeclName member, NLOptions options,
18051805
if (decl->getOverriddenDecl())
18061806
continue;
18071807

1808-
auto dc = decl->getDeclContext();
1809-
auto nominal = dc->getSelfNominalTypeDecl();
1810-
assert(nominal && "Couldn't find nominal type?");
1811-
(void)nominal;
1808+
assert(decl->getDeclContext()->isTypeContext() &&
1809+
"Couldn't find nominal type?");
18121810

18131811
// If we didn't see this declaration before, and it's an acceptable
18141812
// result, add it to the list.
18151813
// declaration to the list.
18161814
if (knownDecls.insert(decl).second &&
1817-
isAcceptableLookupResult(this, options, decl,
1815+
isAcceptableLookupResult(dc, options, decl,
18181816
/*onlyCompleteObjectInits=*/false))
18191817
decls.push_back(decl);
18201818
}
18211819

1822-
pruneLookupResultSet(this, options, decls);
1823-
if (auto *debugClient = this->getParentModule()->getDebugClient()) {
1824-
debugClient->finishLookupInAnyObject(this, member, options, decls);
1820+
pruneLookupResultSet(dc, options, decls);
1821+
if (auto *debugClient = dc->getParentModule()->getDebugClient()) {
1822+
debugClient->finishLookupInAnyObject(dc, member, options, decls);
18251823
}
1826-
// We're done. Report success/failure.
1827-
return !decls.empty();
1824+
return decls;
18281825
}
18291826

18301827
void DeclContext::lookupAllObjCMethods(
@@ -2621,3 +2618,40 @@ void FindLocalVal::visitCatchStmt(CatchStmt *S) {
26212618
checkPattern(S->getErrorPattern(), DeclVisibilityKind::LocalVariable);
26222619
visit(S->getBody());
26232620
}
2621+
2622+
void swift::simple_display(llvm::raw_ostream &out, NLKind kind) {
2623+
switch (kind) {
2624+
case NLKind::QualifiedLookup:
2625+
out << "QualifiedLookup";
2626+
return;
2627+
case NLKind::UnqualifiedLookup:
2628+
out << "UnqualifiedLookup";
2629+
return;
2630+
}
2631+
llvm_unreachable("Unhandled case in switch");
2632+
}
2633+
2634+
void swift::simple_display(llvm::raw_ostream &out, NLOptions options) {
2635+
using Flag = std::pair<NLOptions, StringRef>;
2636+
Flag possibleFlags[] = {
2637+
#define FLAG(Name) {Name, #Name},
2638+
FLAG(NL_ProtocolMembers)
2639+
FLAG(NL_RemoveNonVisible)
2640+
FLAG(NL_RemoveOverridden)
2641+
FLAG(NL_IgnoreAccessControl)
2642+
FLAG(NL_KnownNonCascadingDependency)
2643+
FLAG(NL_KnownCascadingDependency)
2644+
FLAG(NL_OnlyTypes)
2645+
FLAG(NL_IncludeAttributeImplements)
2646+
#undef FLAG
2647+
};
2648+
2649+
auto flagsToPrint = llvm::make_filter_range(
2650+
possibleFlags, [&](Flag flag) { return options & flag.first; });
2651+
2652+
out << "{ ";
2653+
interleave(
2654+
flagsToPrint, [&](Flag flag) { out << flag.second; },
2655+
[&] { out << ", "; });
2656+
out << " }";
2657+
}

0 commit comments

Comments
 (0)