Skip to content

Commit 26d8bad

Browse files
committed
Add DirectLookupRequest
1 parent e26b216 commit 26d8bad

File tree

7 files changed

+118
-27
lines changed

7 files changed

+118
-27
lines changed

include/swift/AST/Decl.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3326,6 +3326,7 @@ class NominalTypeDecl : public GenericTypeDecl, public IterableDeclContext {
33263326
friend class ExtensionDecl;
33273327
friend class DeclContext;
33283328
friend class IterableDeclContext;
3329+
friend class DirectLookupRequest;
33293330
friend ArrayRef<ValueDecl *>
33303331
ValueDecl::getSatisfiedProtocolRequirements(bool Sorted) const;
33313332

@@ -7413,6 +7414,9 @@ ParameterList *getParameterList(ValueDecl *source);
74137414
/// nullptr if the source does not have a parameter list.
74147415
const ParamDecl *getParameterAt(const ValueDecl *source, unsigned index);
74157416

7417+
void simple_display(llvm::raw_ostream &out,
7418+
OptionSet<NominalTypeDecl::LookupDirectFlags> options);
7419+
74167420
/// Display Decl subclasses.
74177421
void simple_display(llvm::raw_ostream &out, const Decl *decl);
74187422

include/swift/AST/NameLookupRequests.h

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,58 @@ class QualifiedLookupRequest
467467
NLOptions opts) const;
468468
};
469469

470+
/// The input type for a direct lookup request.
471+
class DirectLookupDescriptor final {
472+
using LookupOptions = OptionSet<NominalTypeDecl::LookupDirectFlags>;
473+
474+
public:
475+
NominalTypeDecl *DC;
476+
DeclName Name;
477+
LookupOptions Options;
478+
479+
DirectLookupDescriptor(NominalTypeDecl *dc, DeclName name,
480+
LookupOptions options = {})
481+
: DC(dc), Name(name), Options(options) {}
482+
483+
friend llvm::hash_code hash_value(const DirectLookupDescriptor &desc) {
484+
return llvm::hash_combine(desc.Name, desc.DC, desc.Options.toRaw());
485+
}
486+
487+
friend bool operator==(const DirectLookupDescriptor &lhs,
488+
const DirectLookupDescriptor &rhs) {
489+
return lhs.Name == rhs.Name && lhs.DC == rhs.DC &&
490+
lhs.Options.toRaw() == rhs.Options.toRaw();
491+
}
492+
493+
friend bool operator!=(const DirectLookupDescriptor &lhs,
494+
const DirectLookupDescriptor &rhs) {
495+
return !(lhs == rhs);
496+
}
497+
};
498+
499+
void simple_display(llvm::raw_ostream &out, const DirectLookupDescriptor &desc);
500+
501+
SourceLoc extractNearestSourceLoc(const DirectLookupDescriptor &desc);
502+
503+
class DirectLookupRequest
504+
: public SimpleRequest<DirectLookupRequest,
505+
TinyPtrVector<ValueDecl *>(DirectLookupDescriptor),
506+
CacheKind::Uncached> {
507+
public:
508+
using SimpleRequest::SimpleRequest;
509+
510+
private:
511+
friend SimpleRequest;
512+
513+
// Evaluation.
514+
llvm::Expected<TinyPtrVector<ValueDecl *>>
515+
evaluate(Evaluator &evaluator, DirectLookupDescriptor desc) const;
516+
517+
public:
518+
// Cycle handling
519+
void diagnoseCycle(DiagnosticEngine &diags) const;
520+
};
521+
470522
#define SWIFT_TYPEID_ZONE NameLookup
471523
#define SWIFT_TYPEID_HEADER "swift/AST/NameLookupTypeIDZone.def"
472524
#include "swift/Basic/DefineTypeIDZone.h"

include/swift/AST/NameLookupTypeIDZone.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ SWIFT_REQUEST(NameLookup, AnyObjectLookupRequest,
2121
SWIFT_REQUEST(NameLookup, CustomAttrNominalRequest,
2222
NominalTypeDecl *(CustomAttr *, DeclContext *), Cached,
2323
NoLocationInfo)
24+
SWIFT_REQUEST(NameLookup, DirectLookupRequest,
25+
TinyPtrVector<ValueDecl *>(DirectLookupDescriptor), Uncached,
26+
NoLocationInfo)
2427
SWIFT_REQUEST(NameLookup, ExpandASTScopeRequest,
2528
ast_scope::ASTScopeImpl* (ast_scope::ASTScopeImpl*, ast_scope::ScopeCreator*),
2629
SeparatelyCached,

include/swift/Basic/Statistics.def

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -240,9 +240,6 @@ FRONTEND_STATISTIC(Sema, NumLazyRequirementSignaturesLoaded)
240240
/// Number of lazy iterable declaration contexts constructed.
241241
FRONTEND_STATISTIC(Sema, NumLazyIterableDeclContexts)
242242

243-
/// Number of direct member-name lookups performed on nominal types.
244-
FRONTEND_STATISTIC(Sema, NominalTypeLookupDirectCount)
245-
246243
/// Number of member-name lookups that avoided loading all members.
247244
FRONTEND_STATISTIC(Sema, NamedLazyMemberLoadSuccessCount)
248245

lib/AST/Decl.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7917,6 +7917,15 @@ void swift::simple_display(llvm::raw_ostream &out, const Decl *decl) {
79177917
}
79187918
}
79197919

7920+
void swift::simple_display(llvm::raw_ostream &out,
7921+
OptionSet<NominalTypeDecl::LookupDirectFlags> opts) {
7922+
out << "{ ";
7923+
using LookupFlags = NominalTypeDecl::LookupDirectFlags;
7924+
if (opts.contains(LookupFlags::IncludeAttrImplements))
7925+
out << "IncludeAttrImplements";
7926+
out << " }";
7927+
}
7928+
79207929
void swift::simple_display(llvm::raw_ostream &out, const ValueDecl *decl) {
79217930
if (decl) decl->dumpRef(out);
79227931
else out << "(null)";

lib/AST/NameLookup.cpp

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1257,34 +1257,42 @@ maybeFilterOutAttrImplements(TinyPtrVector<ValueDecl *> decls,
12571257
TinyPtrVector<ValueDecl *>
12581258
NominalTypeDecl::lookupDirect(DeclName name,
12591259
OptionSet<LookupDirectFlags> flags) {
1260-
ASTContext &ctx = getASTContext();
1261-
if (auto s = ctx.Stats) {
1262-
++s->getFrontendCounters().NominalTypeLookupDirectCount;
1263-
}
1260+
return evaluateOrDefault(getASTContext().evaluator,
1261+
DirectLookupRequest({this, name, flags}), {});
1262+
}
1263+
1264+
llvm::Expected<TinyPtrVector<ValueDecl *>>
1265+
DirectLookupRequest::evaluate(Evaluator &evaluator,
1266+
DirectLookupDescriptor desc) const {
1267+
const auto &name = desc.Name;
1268+
const auto flags = desc.Options;
1269+
auto *decl = desc.DC;
12641270

12651271
// We only use NamedLazyMemberLoading when a user opts-in and we have
12661272
// not yet loaded all the members into the IDC list in the first place.
1273+
ASTContext &ctx = decl->getASTContext();
12671274
const bool useNamedLazyMemberLoading = (ctx.LangOpts.NamedLazyMemberLoading &&
1268-
hasLazyMembers());
1275+
decl->hasLazyMembers());
12691276

12701277
const bool includeAttrImplements =
1271-
flags.contains(LookupDirectFlags::IncludeAttrImplements);
1278+
flags.contains(NominalTypeDecl::LookupDirectFlags::IncludeAttrImplements);
12721279

1273-
LLVM_DEBUG(llvm::dbgs() << getNameStr() << ".lookupDirect("
1280+
LLVM_DEBUG(llvm::dbgs() << decl->getNameStr() << ".lookupDirect("
12741281
<< name << ")"
1275-
<< ", hasLazyMembers()=" << hasLazyMembers()
1276-
<< ", useNamedLazyMemberLoading=" << useNamedLazyMemberLoading
1282+
<< ", hasLazyMembers()=" << decl->hasLazyMembers()
1283+
<< ", useNamedLazyMemberLoading="
1284+
<< useNamedLazyMemberLoading
12771285
<< "\n");
12781286

12791287

1280-
prepareLookupTable();
1288+
decl->prepareLookupTable();
12811289

12821290
auto tryCacheLookup =
1283-
[=](MemberLookupTable *table,
1291+
[=](MemberLookupTable &table,
12841292
DeclName name) -> Optional<TinyPtrVector<ValueDecl *>> {
12851293
// Look for a declaration with this name.
1286-
auto known = table->find(name);
1287-
if (known == table->end()) {
1294+
auto known = table.find(name);
1295+
if (known == table.end()) {
12881296
return None;
12891297
}
12901298

@@ -1293,36 +1301,36 @@ NominalTypeDecl::lookupDirect(DeclName name,
12931301
includeAttrImplements);
12941302
};
12951303

1296-
auto updateLookupTable = [this](MemberLookupTable *table) {
1304+
auto updateLookupTable = [&decl](MemberLookupTable &table) {
12971305
// Make sure we have the complete list of members (in this nominal and in
12981306
// all extensions).
1299-
(void)getMembers();
1307+
(void)decl->getMembers();
13001308

1301-
for (auto E : getExtensions())
1309+
for (auto E : decl->getExtensions())
13021310
(void)E->getMembers();
13031311

1304-
LookupTable->updateLookupTable(this);
1312+
table.updateLookupTable(decl);
13051313
};
13061314

1315+
auto &Table = *decl->LookupTable;
13071316
if (!useNamedLazyMemberLoading) {
1308-
updateLookupTable(LookupTable);
1309-
} else if (!LookupTable->isLazilyComplete(name.getBaseName())) {
1317+
updateLookupTable(Table);
1318+
} else if (!Table.isLazilyComplete(name.getBaseName())) {
13101319
// The lookup table believes it doesn't have a complete accounting of this
13111320
// name - either because we're never seen it before, or another extension
13121321
// was registered since the last time we searched. Ask the loaders to give
13131322
// us a hand.
1314-
auto &Table = *LookupTable;
13151323
DeclBaseName baseName(name.getBaseName());
1316-
if (populateLookupTableEntryFromLazyIDCLoader(ctx, Table, baseName, this)) {
1317-
updateLookupTable(LookupTable);
1324+
if (populateLookupTableEntryFromLazyIDCLoader(ctx, Table, baseName, decl)) {
1325+
updateLookupTable(Table);
13181326
} else {
1319-
populateLookupTableEntryFromExtensions(ctx, Table, this, baseName);
1327+
populateLookupTableEntryFromExtensions(ctx, Table, decl, baseName);
13201328
}
13211329
Table.markLazilyComplete(baseName);
13221330
}
13231331

13241332
// Look for a declaration with this name.
1325-
return tryCacheLookup(LookupTable, name)
1333+
return tryCacheLookup(Table, name)
13261334
.getValueOr(TinyPtrVector<ValueDecl *>());
13271335
}
13281336

lib/AST/NameLookupRequests.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,24 @@ swift::extractNearestSourceLoc(const UnqualifiedLookupDescriptor &desc) {
188188
return extractNearestSourceLoc(desc.DC);
189189
}
190190

191+
//----------------------------------------------------------------------------//
192+
// DirectLookupRequest computation.
193+
//----------------------------------------------------------------------------//
194+
195+
void swift::simple_display(llvm::raw_ostream &out,
196+
const DirectLookupDescriptor &desc) {
197+
out << "directly looking up ";
198+
simple_display(out, desc.Name);
199+
out << " on ";
200+
simple_display(out, desc.DC);
201+
out << " with options ";
202+
simple_display(out, desc.Options);
203+
}
204+
205+
SourceLoc swift::extractNearestSourceLoc(const DirectLookupDescriptor &desc) {
206+
return extractNearestSourceLoc(desc.DC);
207+
}
208+
191209
// Define request evaluation functions for each of the name lookup requests.
192210
static AbstractRequestFunction *nameLookupRequestFunctions[] = {
193211
#define SWIFT_REQUEST(Zone, Name, Sig, Caching, LocOptions) \

0 commit comments

Comments
 (0)