Skip to content

Commit ddaf82d

Browse files
authored
[NameLookup] Add requests for module and AnyObject lookup (#28395)
[NameLookup] Add requests for module and AnyObject lookup
2 parents 654e601 + 6811586 commit ddaf82d

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>())
@@ -1798,31 +1801,28 @@ 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;
1818-
1819-
auto *stats = getASTContext().Stats;
1820-
if (stats)
1821-
stats->getFrontendCounters().NumLookupQualifiedInAnyObject++;
1821+
return decls;
18221822

18231823
// Collect all of the visible declarations.
18241824
SmallVector<ValueDecl *, 4> allDecls;
1825-
for (auto import : namelookup::getAllImports(this)) {
1825+
for (auto import : namelookup::getAllImports(dc)) {
18261826
import.second->lookupClassMember(import.first, member, allDecls);
18271827
}
18281828

@@ -1840,26 +1840,23 @@ bool DeclContext::lookupAnyObject(DeclName member, NLOptions options,
18401840
if (decl->getOverriddenDecl())
18411841
continue;
18421842

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

18481846
// If we didn't see this declaration before, and it's an acceptable
18491847
// result, add it to the list.
18501848
// declaration to the list.
18511849
if (knownDecls.insert(decl).second &&
1852-
isAcceptableLookupResult(this, options, decl,
1850+
isAcceptableLookupResult(dc, options, decl,
18531851
/*onlyCompleteObjectInits=*/false))
18541852
decls.push_back(decl);
18551853
}
18561854

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

18651862
void DeclContext::lookupAllObjCMethods(
@@ -2635,3 +2632,40 @@ void FindLocalVal::visitCatchStmt(CatchStmt *S) {
26352632
checkPattern(S->getErrorPattern(), DeclVisibilityKind::LocalVariable);
26362633
visit(S->getBody());
26372634
}
2635+
2636+
void swift::simple_display(llvm::raw_ostream &out, NLKind kind) {
2637+
switch (kind) {
2638+
case NLKind::QualifiedLookup:
2639+
out << "QualifiedLookup";
2640+
return;
2641+
case NLKind::UnqualifiedLookup:
2642+
out << "UnqualifiedLookup";
2643+
return;
2644+
}
2645+
llvm_unreachable("Unhandled case in switch");
2646+
}
2647+
2648+
void swift::simple_display(llvm::raw_ostream &out, NLOptions options) {
2649+
using Flag = std::pair<NLOptions, StringRef>;
2650+
Flag possibleFlags[] = {
2651+
#define FLAG(Name) {Name, #Name},
2652+
FLAG(NL_ProtocolMembers)
2653+
FLAG(NL_RemoveNonVisible)
2654+
FLAG(NL_RemoveOverridden)
2655+
FLAG(NL_IgnoreAccessControl)
2656+
FLAG(NL_KnownNonCascadingDependency)
2657+
FLAG(NL_KnownCascadingDependency)
2658+
FLAG(NL_OnlyTypes)
2659+
FLAG(NL_IncludeAttributeImplements)
2660+
#undef FLAG
2661+
};
2662+
2663+
auto flagsToPrint = llvm::make_filter_range(
2664+
possibleFlags, [&](Flag flag) { return options & flag.first; });
2665+
2666+
out << "{ ";
2667+
interleave(
2668+
flagsToPrint, [&](Flag flag) { out << flag.second; },
2669+
[&] { out << ", "; });
2670+
out << " }";
2671+
}

0 commit comments

Comments
 (0)