Skip to content

Commit 0b502d8

Browse files
committed
AST: loadNamedMembers() never fails
This allows us to simplify lookupDirect() a fair bit as well.
1 parent d5cc01d commit 0b502d8

File tree

7 files changed

+37
-67
lines changed

7 files changed

+37
-67
lines changed

include/swift/AST/LazyResolver.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,7 @@ class alignas(void*) LazyMemberLoader {
8181

8282
/// Populates a vector with all members of \p IDC that have DeclName
8383
/// matching \p N.
84-
///
85-
/// Returns None if an error occurred \em or named member-lookup
86-
/// was otherwise unsupported in this implementation or Decl.
87-
virtual Optional<TinyPtrVector<ValueDecl *>>
84+
virtual TinyPtrVector<ValueDecl *>
8885
loadNamedMembers(const IterableDeclContext *IDC, DeclBaseName N,
8986
uint64_t contextData) = 0;
9087

include/swift/Basic/Statistics.def

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -243,9 +243,6 @@ FRONTEND_STATISTIC(Sema, NumLazyIterableDeclContexts)
243243
/// Number of member-name lookups that avoided loading all members.
244244
FRONTEND_STATISTIC(Sema, NamedLazyMemberLoadSuccessCount)
245245

246-
/// Number of member-name lookups that wound up loading all members.
247-
FRONTEND_STATISTIC(Sema, NamedLazyMemberLoadFailureCount)
248-
249246
/// Number of types deserialized.
250247
FRONTEND_STATISTIC(Sema, NumTypesDeserialized)
251248

lib/AST/NameLookup.cpp

Lines changed: 32 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1143,34 +1143,27 @@ void ExtensionDecl::addedMember(Decl *member) {
11431143
// MemberLookupTable is constructed (and possibly has entries in it),
11441144
// MemberLookupTable is incrementally reconstituted with new members.
11451145

1146-
static bool
1146+
static void
11471147
populateLookupTableEntryFromLazyIDCLoader(ASTContext &ctx,
11481148
MemberLookupTable &LookupTable,
11491149
DeclBaseName name,
11501150
IterableDeclContext *IDC) {
11511151
auto ci = ctx.getOrCreateLazyIterableContextData(IDC,
11521152
/*lazyLoader=*/nullptr);
1153-
if (auto res = ci->loader->loadNamedMembers(IDC, name, ci->memberData)) {
1154-
if (auto s = ctx.Stats) {
1155-
++s->getFrontendCounters().NamedLazyMemberLoadSuccessCount;
1156-
}
1157-
for (auto d : *res) {
1158-
LookupTable.addMember(d);
1159-
}
1160-
return false;
1161-
} else {
1162-
if (auto s = ctx.Stats) {
1163-
++s->getFrontendCounters().NamedLazyMemberLoadFailureCount;
1164-
}
1165-
return true;
1153+
auto res = ci->loader->loadNamedMembers(IDC, name, ci->memberData);
1154+
if (auto s = ctx.Stats) {
1155+
++s->getFrontendCounters().NamedLazyMemberLoadSuccessCount;
1156+
}
1157+
for (auto d : res) {
1158+
LookupTable.addMember(d);
11661159
}
11671160
}
11681161

11691162
static void
11701163
populateLookupTableEntryFromExtensions(ASTContext &ctx,
11711164
MemberLookupTable &table,
1172-
NominalTypeDecl *nominal,
1173-
DeclBaseName name) {
1165+
DeclBaseName name,
1166+
NominalTypeDecl *nominal) {
11741167
assert(!table.isLazilyComplete(name) &&
11751168
"Should not be searching extensions for complete name!");
11761169

@@ -1185,12 +1178,7 @@ populateLookupTableEntryFromExtensions(ASTContext &ctx,
11851178
"Extension without deserializable content has lazy members!");
11861179
assert(!e->hasUnparsedMembers());
11871180

1188-
// Try lazy loading. If that fails, then we fall back by loading the
1189-
// entire extension. FIXME: It's rather unfortunate that we fall off the
1190-
// happy path because the Clang Importer can't handle lazy import-as-member.
1191-
if (populateLookupTableEntryFromLazyIDCLoader(ctx, table, name, e)) {
1192-
e->loadAllMembers();
1193-
}
1181+
populateLookupTableEntryFromLazyIDCLoader(ctx, table, name, e);
11941182
}
11951183
}
11961184

@@ -1280,55 +1268,44 @@ DirectLookupRequest::evaluate(Evaluator &evaluator,
12801268

12811269
decl->prepareLookupTable();
12821270

1283-
auto tryCacheLookup =
1284-
[=](MemberLookupTable &table,
1285-
DeclName name) -> Optional<TinyPtrVector<ValueDecl *>> {
1286-
// Look for a declaration with this name.
1287-
auto known = table.find(name);
1288-
if (known == table.end()) {
1289-
return None;
1290-
}
1291-
1292-
// We found something; return it.
1293-
return maybeFilterOutAttrImplements(known->second, name,
1294-
includeAttrImplements);
1295-
};
1296-
1297-
auto updateLookupTable = [&decl](MemberLookupTable &table,
1298-
bool noExtensions) {
1271+
auto &Table = *decl->LookupTable;
1272+
if (!useNamedLazyMemberLoading) {
12991273
// Make sure we have the complete list of members (in this nominal and in
13001274
// all extensions).
13011275
(void)decl->getMembers();
13021276

1303-
if (noExtensions)
1304-
return;
1305-
1306-
for (auto E : decl->getExtensions())
1307-
(void)E->getMembers();
1277+
if (!disableAdditionalExtensionLoading) {
1278+
for (auto E : decl->getExtensions())
1279+
(void)E->getMembers();
13081280

1309-
table.updateLookupTable(decl);
1310-
};
1311-
1312-
auto &Table = *decl->LookupTable;
1313-
if (!useNamedLazyMemberLoading) {
1314-
updateLookupTable(Table, disableAdditionalExtensionLoading);
1281+
Table.updateLookupTable(decl);
1282+
}
13151283
} else if (!Table.isLazilyComplete(name.getBaseName())) {
13161284
// The lookup table believes it doesn't have a complete accounting of this
13171285
// name - either because we're never seen it before, or another extension
13181286
// was registered since the last time we searched. Ask the loaders to give
13191287
// us a hand.
13201288
DeclBaseName baseName(name.getBaseName());
1321-
if (populateLookupTableEntryFromLazyIDCLoader(ctx, Table, baseName, decl)) {
1322-
updateLookupTable(Table, disableAdditionalExtensionLoading);
1323-
} else if (!disableAdditionalExtensionLoading) {
1324-
populateLookupTableEntryFromExtensions(ctx, Table, decl, baseName);
1289+
populateLookupTableEntryFromLazyIDCLoader(ctx, Table, baseName, decl);
1290+
1291+
if (!disableAdditionalExtensionLoading) {
1292+
populateLookupTableEntryFromExtensions(ctx, Table, baseName, decl);
13251293
}
1294+
1295+
// FIXME: If disableAdditionalExtensionLoading is true, we should
1296+
// not mark the entry as complete.
13261297
Table.markLazilyComplete(baseName);
13271298
}
13281299

13291300
// Look for a declaration with this name.
1330-
return tryCacheLookup(Table, name)
1331-
.getValueOr(TinyPtrVector<ValueDecl *>());
1301+
auto known = Table.find(name);
1302+
if (known == Table.end()) {
1303+
return TinyPtrVector<ValueDecl *>();
1304+
}
1305+
1306+
// We found something; return it.
1307+
return maybeFilterOutAttrImplements(known->second, name,
1308+
includeAttrImplements);
13321309
}
13331310

13341311
void ClassDecl::createObjCMethodLookup() {

lib/ClangImporter/ClangImporter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3723,7 +3723,7 @@ void ClangImporter::Implementation::lookupAllObjCMembers(
37233723
}
37243724
}
37253725

3726-
Optional<TinyPtrVector<ValueDecl *>>
3726+
TinyPtrVector<ValueDecl *>
37273727
ClangImporter::Implementation::loadNamedMembers(
37283728
const IterableDeclContext *IDC, DeclBaseName N, uint64_t contextData) {
37293729

lib/ClangImporter/ImporterImpl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1238,7 +1238,7 @@ class LLVM_LIBRARY_VISIBILITY ClangImporter::Implementation
12381238
virtual void
12391239
loadAllMembers(Decl *D, uint64_t unused) override;
12401240

1241-
virtual Optional<TinyPtrVector<ValueDecl *>>
1241+
virtual TinyPtrVector<ValueDecl *>
12421242
loadNamedMembers(const IterableDeclContext *IDC, DeclBaseName N,
12431243
uint64_t contextData) override;
12441244

lib/Serialization/ModuleFile.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2376,7 +2376,7 @@ void ModuleFile::loadObjCMethods(
23762376
}
23772377
}
23782378

2379-
Optional<TinyPtrVector<ValueDecl *>>
2379+
TinyPtrVector<ValueDecl *>
23802380
ModuleFile::loadNamedMembers(const IterableDeclContext *IDC, DeclBaseName N,
23812381
uint64_t contextData) {
23822382
PrettyStackTraceDecl trace("loading members for", IDC->getDecl());

lib/Serialization/ModuleFile.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -835,8 +835,7 @@ class ModuleFile
835835
virtual void loadAllMembers(Decl *D,
836836
uint64_t contextData) override;
837837

838-
virtual
839-
Optional<TinyPtrVector<ValueDecl *>>
838+
virtual TinyPtrVector<ValueDecl *>
840839
loadNamedMembers(const IterableDeclContext *IDC, DeclBaseName N,
841840
uint64_t contextData) override;
842841

0 commit comments

Comments
 (0)