Skip to content

Commit 256c129

Browse files
committed
[NameLookup] Add AnyObjectLookupRequest
1 parent 2ca30c6 commit 256c129

File tree

5 files changed

+68
-23
lines changed

5 files changed

+68
-23
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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ static inline NLOptions operator~(NLOptions value) {
105105
return NLOptions(~(unsigned)value);
106106
}
107107

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

110112
#endif

include/swift/AST/NameLookupRequests.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,24 @@ class LookupInModuleRequest
386386
const DeclContext *moduleScopeContext) const;
387387
};
388388

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+
389407
#define SWIFT_TYPEID_ZONE NameLookup
390408
#define SWIFT_TYPEID_HEADER "swift/AST/NameLookupTypeIDZone.def"
391409
#include "swift/Basic/DefineTypeIDZone.h"

include/swift/AST/NameLookupTypeIDZone.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,6 @@ SWIFT_REQUEST(NameLookup, LookupInModuleRequest,
5353
namelookup::ResolutionKind,
5454
const DeclContext *),
5555
Uncached, NoLocationInfo)
56+
SWIFT_REQUEST(NameLookup, AnyObjectLookupRequest,
57+
QualifiedLookupResult(const DeclContext *, DeclName, NLOptions),
58+
Uncached, NoLocationInfo)

lib/AST/NameLookup.cpp

Lines changed: 45 additions & 19 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>())
@@ -1798,31 +1801,32 @@ bool DeclContext::lookupQualified(ModuleDecl *module, DeclName member,
17981801
return !decls.empty();
17991802
}
18001803

1801-
bool DeclContext::lookupAnyObject(DeclName member, NLOptions options,
1802-
SmallVectorImpl<ValueDecl *> &decls) const {
1804+
llvm::Expected<QualifiedLookupResult>
1805+
AnyObjectLookupRequest::evaluate(Evaluator &evaluator, const DeclContext *dc,
1806+
DeclName member, NLOptions options) const {
18031807
using namespace namelookup;
1804-
assert(decls.empty() && "additive lookup not supported");
1808+
QualifiedLookupResult decls;
18051809

18061810
// Configure lookup and dig out the tracker.
18071811
ReferencedNameTracker *tracker = nullptr;
18081812
bool isLookupCascading;
1809-
configureLookup(this, options, tracker, isLookupCascading);
1813+
configureLookup(dc, options, tracker, isLookupCascading);
18101814

18111815
// Record this lookup.
18121816
if (tracker)
18131817
tracker->addDynamicLookupName(member.getBaseName(), isLookupCascading);
18141818

18151819
// Type-only lookup won't find anything on AnyObject.
18161820
if (options & NL_OnlyTypes)
1817-
return false;
1821+
return decls;
18181822

1819-
auto *stats = getASTContext().Stats;
1823+
auto *stats = dc->getASTContext().Stats;
18201824
if (stats)
18211825
stats->getFrontendCounters().NumLookupQualifiedInAnyObject++;
18221826

18231827
// Collect all of the visible declarations.
18241828
SmallVector<ValueDecl *, 4> allDecls;
1825-
for (auto import : namelookup::getAllImports(this)) {
1829+
for (auto import : namelookup::getAllImports(dc)) {
18261830
import.second->lookupClassMember(import.first, member, allDecls);
18271831
}
18281832

@@ -1840,26 +1844,23 @@ bool DeclContext::lookupAnyObject(DeclName member, NLOptions options,
18401844
if (decl->getOverriddenDecl())
18411845
continue;
18421846

1843-
auto dc = decl->getDeclContext();
1844-
auto nominal = dc->getSelfNominalTypeDecl();
1845-
assert(nominal && "Couldn't find nominal type?");
1846-
(void)nominal;
1847+
assert(decl->getDeclContext()->isTypeContext() &&
1848+
"Couldn't find nominal type?");
18471849

18481850
// If we didn't see this declaration before, and it's an acceptable
18491851
// result, add it to the list.
18501852
// declaration to the list.
18511853
if (knownDecls.insert(decl).second &&
1852-
isAcceptableLookupResult(this, options, decl,
1854+
isAcceptableLookupResult(dc, options, decl,
18531855
/*onlyCompleteObjectInits=*/false))
18541856
decls.push_back(decl);
18551857
}
18561858

1857-
pruneLookupResultSet(this, options, decls);
1858-
if (auto *debugClient = this->getParentModule()->getDebugClient()) {
1859-
debugClient->finishLookupInAnyObject(this, member, options, decls);
1859+
pruneLookupResultSet(dc, options, decls);
1860+
if (auto *debugClient = dc->getParentModule()->getDebugClient()) {
1861+
debugClient->finishLookupInAnyObject(dc, member, options, decls);
18601862
}
1861-
// We're done. Report success/failure.
1862-
return !decls.empty();
1863+
return decls;
18631864
}
18641865

18651866
void DeclContext::lookupAllObjCMethods(
@@ -2647,3 +2648,28 @@ void swift::simple_display(llvm::raw_ostream &out, NLKind kind) {
26472648
}
26482649
llvm_unreachable("Unhandled case in switch");
26492650
}
2651+
2652+
void swift::simple_display(llvm::raw_ostream &out, NLOptions options) {
2653+
using Flag = std::pair<NLOptions, StringRef>;
2654+
Flag possibleFlags[] = {
2655+
#define FLAG(Name) {Name, #Name},
2656+
FLAG(NL_ProtocolMembers)
2657+
FLAG(NL_RemoveNonVisible)
2658+
FLAG(NL_RemoveOverridden)
2659+
FLAG(NL_IgnoreAccessControl)
2660+
FLAG(NL_KnownNonCascadingDependency)
2661+
FLAG(NL_KnownCascadingDependency)
2662+
FLAG(NL_OnlyTypes)
2663+
FLAG(NL_IncludeAttributeImplements)
2664+
#undef FLAG
2665+
};
2666+
2667+
auto flagsToPrint = llvm::make_filter_range(
2668+
possibleFlags, [&](Flag flag) { return options & flag.first; });
2669+
2670+
out << "{ ";
2671+
interleave(
2672+
flagsToPrint, [&](Flag flag) { out << flag.second; },
2673+
[&] { out << ", "; });
2674+
out << " }";
2675+
}

0 commit comments

Comments
 (0)