Skip to content

Commit a51bbdf

Browse files
Improve readability
1 parent 1dbf3c7 commit a51bbdf

File tree

1 file changed

+38
-44
lines changed

1 file changed

+38
-44
lines changed

lib/ClangImporter/ImportDecl.cpp

Lines changed: 38 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -4535,30 +4535,35 @@ namespace {
45354535
}
45364536

45374537
template <typename T, typename U>
4538-
T *resolveSwiftDeclImpl(const U *decl, Identifier name, bool hasCustomName,
4539-
ModuleDecl *overlay) {
4538+
T *resolveSwiftDeclImpl(const U *decl, Identifier name,
4539+
bool hasKnownSwiftName, ModuleDecl *overlay) {
45404540
const auto &languageVersion =
45414541
Impl.SwiftContext.LangOpts.EffectiveLanguageVersion;
45424542

4543-
// None = not enough information
4544-
// true/false = definitive answer
4545-
auto isMatch = [&](const T *singleResult) -> Optional<bool> {
4543+
auto isMatch = [&](const T *singleResult, bool baseNameMatches) -> bool {
45464544
const DeclAttributes &attrs = singleResult->getAttrs();
45474545

45484546
// Skip versioned variants.
45494547
if (attrs.isUnavailableInSwiftVersion(languageVersion))
45504548
return false;
45514549

4552-
// If Clang decl has a custom Swift name, then `name` is that name.
4553-
if (hasCustomName)
4554-
return None;
4550+
// Skip if type not exposed to Objective-C.
4551+
// If the base name doesn't match, then a matching
4552+
// custom name in an @objc attribute is required.
4553+
if (baseNameMatches && !singleResult->isObjC())
4554+
return false;
4555+
4556+
// If Clang decl has a custom Swift name, then we know that
4557+
// `name` is the base name we're looking for.
4558+
if (hasKnownSwiftName)
4559+
return baseNameMatches;
45554560

45564561
// Skip if a different name is used for Objective-C.
45574562
if (auto objcAttr = attrs.getAttribute<ObjCAttr>())
45584563
if (auto objcName = objcAttr->getName())
45594564
return objcName->getSimpleName() == name;
45604565

4561-
return None;
4566+
return baseNameMatches;
45624567
};
45634568

45644569
// First look at Swift types with the same name.
@@ -4567,38 +4572,26 @@ namespace {
45674572
T *found = nullptr;
45684573
for (auto result : results) {
45694574
if (auto singleResult = dyn_cast<T>(result)) {
4570-
// Eliminate false positives (Swift name matches but Obj-C name doesn't).
4571-
Optional<bool> matched = isMatch(singleResult);
4572-
if (matched.hasValue() && !*matched)
4573-
continue;
4574-
4575-
// Skip if type not exposed to Objective-C.
4576-
if (!hasCustomName && !singleResult->isObjC())
4577-
continue;
4578-
4579-
if (found)
4580-
return nullptr;
4581-
4582-
found = singleResult;
4575+
if (isMatch(singleResult, /*baseNameMatches=*/true)) {
4576+
if (found)
4577+
return nullptr;
4578+
found = singleResult;
4579+
}
45834580
}
45844581
}
45854582

4586-
if (!found && !hasCustomName) {
4587-
// Try harder to find match with custom Objective-C name.
4588-
// Only positive matches can be used, since we've already eliminated
4589-
// all native Swift types with the same native name.
4583+
if (!found && !hasKnownSwiftName) {
4584+
// Try harder to find a match looking at just custom Objective-C names.
45904585
SmallVector<Decl *, 64> results;
45914586
overlay->getTopLevelDecls(results);
45924587
for (auto result : results) {
45934588
if (auto singleResult = dyn_cast<T>(result)) {
4594-
Optional<bool> matched = isMatch(singleResult);
4595-
if (!(matched.hasValue() && *matched))
4596-
continue;
4597-
4598-
if (found)
4599-
return nullptr;
4600-
4601-
found = singleResult;
4589+
// The base name _could_ match but it's irrelevant here.
4590+
if (isMatch(singleResult, /*baseNameMatches=*/false)) {
4591+
if (found)
4592+
return nullptr;
4593+
found = singleResult;
4594+
}
46024595
}
46034596
}
46044597
}
@@ -4611,17 +4604,17 @@ namespace {
46114604
}
46124605

46134606
template <typename T, typename U>
4614-
T *resolveSwiftDecl(const U *decl, Identifier name, bool hasCustomName,
4615-
ClangModuleUnit *clangModule) {
4607+
T *resolveSwiftDecl(const U *decl, Identifier name,
4608+
bool hasKnownSwiftName, ClangModuleUnit *clangModule) {
46164609
if (auto overlay = clangModule->getOverlayModule())
4617-
return resolveSwiftDeclImpl<T>(decl, name, hasCustomName, overlay);
4610+
return resolveSwiftDeclImpl<T>(decl, name, hasKnownSwiftName, overlay);
46184611
if (clangModule == Impl.ImportedHeaderUnit) {
46194612
// Use an index-based loop because new owners can come in as we're
46204613
// iterating.
46214614
for (size_t i = 0; i < Impl.ImportedHeaderOwners.size(); ++i) {
46224615
ModuleDecl *owner = Impl.ImportedHeaderOwners[i];
4623-
if (T *result = resolveSwiftDeclImpl<T>(decl, name, hasCustomName,
4624-
owner))
4616+
if (T *result = resolveSwiftDeclImpl<T>(decl, name,
4617+
hasKnownSwiftName, owner))
46254618
return result;
46264619
}
46274620
}
@@ -4634,7 +4627,7 @@ namespace {
46344627
if (!importer::hasNativeSwiftDecl(decl))
46354628
return false;
46364629
auto wrapperUnit = cast<ClangModuleUnit>(dc->getModuleScopeContext());
4637-
swiftDecl = resolveSwiftDecl<T>(decl, name, /*hasCustomName=*/true,
4630+
swiftDecl = resolveSwiftDecl<T>(decl, name, /*hasCustomSwiftName=*/true,
46384631
wrapperUnit);
46394632
return true;
46404633
}
@@ -4664,14 +4657,15 @@ namespace {
46644657
*correctSwiftName);
46654658

46664659
Identifier name = importedName.getDeclName().getBaseIdentifier();
4667-
bool hasCustomName = importedName.hasCustomName();
4660+
bool hasKnownSwiftName = importedName.hasCustomName();
46684661

46694662
// FIXME: Figure out how to deal with incomplete protocols, since that
46704663
// notion doesn't exist in Swift.
46714664
if (!decl->hasDefinition()) {
46724665
// Check if this protocol is implemented in its overlay.
46734666
if (auto clangModule = Impl.getClangModuleForDecl(decl, true))
4674-
if (auto native = resolveSwiftDecl<ProtocolDecl>(decl, name, hasCustomName,
4667+
if (auto native = resolveSwiftDecl<ProtocolDecl>(decl, name,
4668+
hasKnownSwiftName,
46754669
clangModule))
46764670
return native;
46774671

@@ -4783,13 +4777,13 @@ namespace {
47834777
*correctSwiftName);
47844778

47854779
auto name = importedName.getDeclName().getBaseIdentifier();
4786-
bool hasCustomName = importedName.hasCustomName();
4780+
bool hasKnownSwiftName = importedName.hasCustomName();
47874781

47884782
if (!decl->hasDefinition()) {
47894783
// Check if this class is implemented in its overlay.
47904784
if (auto clangModule = Impl.getClangModuleForDecl(decl, true)) {
47914785
if (auto native = resolveSwiftDecl<ClassDecl>(decl, name,
4792-
hasCustomName,
4786+
hasKnownSwiftName,
47934787
clangModule)) {
47944788
return native;
47954789
}

0 commit comments

Comments
 (0)