@@ -4511,7 +4511,8 @@ namespace {
4511
4511
// / methods become class methods on NSObject).
4512
4512
void importMirroredProtocolMembers (const clang::ObjCContainerDecl *decl,
4513
4513
DeclContext *dc,
4514
- SmallVectorImpl<Decl *> &members);
4514
+ Optional<DeclBaseName> name,
4515
+ SmallVectorImpl<Decl *> &newMembers);
4515
4516
4516
4517
void importNonOverriddenMirroredMethods (DeclContext *dc,
4517
4518
MutableArrayRef<MirroredMethodEntry> entries,
@@ -6927,9 +6928,16 @@ Optional<GenericParamList *> SwiftDeclConverter::importObjCGenericParams(
6927
6928
genericParams, Impl.importSourceLoc (typeParamList->getRAngleLoc ()));
6928
6929
}
6929
6930
6931
+ void ClangImporter::Implementation::importMirroredProtocolMembers (
6932
+ const clang::ObjCContainerDecl *decl, DeclContext *dc,
6933
+ Optional<DeclBaseName> name, SmallVectorImpl<Decl *> &members) {
6934
+ SwiftDeclConverter converter (*this , CurrentVersion);
6935
+ converter.importMirroredProtocolMembers (decl, dc, name, members);
6936
+ }
6937
+
6930
6938
void SwiftDeclConverter::importMirroredProtocolMembers (
6931
6939
const clang::ObjCContainerDecl *decl, DeclContext *dc,
6932
- SmallVectorImpl<Decl *> &members) {
6940
+ Optional<DeclBaseName> name, SmallVectorImpl<Decl *> &members) {
6933
6941
assert (dc);
6934
6942
const clang::ObjCInterfaceDecl *interfaceDecl = nullptr ;
6935
6943
const ClangModuleUnit *declModule;
@@ -6975,24 +6983,24 @@ void SwiftDeclConverter::importMirroredProtocolMembers(
6975
6983
6976
6984
const auto &languageVersion =
6977
6985
Impl.SwiftContext .LangOpts .EffectiveLanguageVersion ;
6978
- for ( auto member : proto-> getMembers () ) {
6986
+ auto importProtocolRequirement = [&](Decl *member ) {
6979
6987
// Skip compatibility stubs; there's no reason to mirror them.
6980
6988
if (member->getAttrs ().isUnavailableInSwiftVersion (languageVersion))
6981
- continue ;
6989
+ return ;
6982
6990
6983
6991
if (auto prop = dyn_cast<VarDecl>(member)) {
6984
6992
auto objcProp =
6985
6993
dyn_cast_or_null<clang::ObjCPropertyDecl>(prop->getClangDecl ());
6986
6994
if (!objcProp)
6987
- continue ;
6995
+ return ;
6988
6996
6989
6997
// We can't import a property if there's already a method with this
6990
6998
// name. (This also covers other properties with that same name.)
6991
6999
// FIXME: We should still mirror the setter as a method if it's
6992
7000
// not already there.
6993
7001
clang::Selector sel = objcProp->getGetterName ();
6994
7002
if (interfaceDecl->getInstanceMethod (sel))
6995
- continue ;
7003
+ return ;
6996
7004
6997
7005
bool inNearbyCategory =
6998
7006
std::any_of (interfaceDecl->visible_categories_begin (),
@@ -7009,7 +7017,7 @@ void SwiftDeclConverter::importMirroredProtocolMembers(
7009
7017
return category->getInstanceMethod (sel);
7010
7018
});
7011
7019
if (inNearbyCategory)
7012
- continue ;
7020
+ return ;
7013
7021
7014
7022
if (auto imported =
7015
7023
Impl.importMirroredDecl (objcProp, dc, getVersion (), proto)) {
@@ -7018,24 +7026,38 @@ void SwiftDeclConverter::importMirroredProtocolMembers(
7018
7026
// metatype.
7019
7027
}
7020
7028
7021
- continue ;
7029
+ return ;
7022
7030
}
7023
7031
7024
7032
auto afd = dyn_cast<AbstractFunctionDecl>(member);
7025
7033
if (!afd)
7026
- continue ;
7034
+ return ;
7027
7035
7028
7036
if (isa<AccessorDecl>(afd))
7029
- continue ;
7037
+ return ;
7030
7038
7031
7039
auto objcMethod =
7032
7040
dyn_cast_or_null<clang::ObjCMethodDecl>(member->getClangDecl ());
7033
7041
if (!objcMethod)
7034
- continue ;
7042
+ return ;
7035
7043
7036
7044
// For now, just remember that we saw this method.
7037
7045
methodsByName[objcMethod->getSelector ()]
7038
7046
.push_back (MirroredMethodEntry{objcMethod, proto});
7047
+ };
7048
+
7049
+ if (name) {
7050
+ // If we're asked to import a specific name only, look for that in the
7051
+ // protocol.
7052
+ auto results = proto->lookupDirect (*name);
7053
+ for (auto *member : results)
7054
+ if (member->getDeclContext () == proto)
7055
+ importProtocolRequirement (member);
7056
+
7057
+ } else {
7058
+ // Otherwise, import all mirrored members.
7059
+ for (auto *member : proto->getMembers ())
7060
+ importProtocolRequirement (member);
7039
7061
}
7040
7062
}
7041
7063
@@ -8676,20 +8698,22 @@ void ClangImporter::Implementation::collectMembersToAdd(
8676
8698
insertMembersAndAlternates (nd, members);
8677
8699
}
8678
8700
8701
+ // Objective-C protocols don't require any special handling.
8702
+ if (isa<clang::ObjCProtocolDecl>(objcContainer))
8703
+ return ;
8704
+
8705
+ // Objective-C interfaces can inherit constructors from their superclass,
8706
+ // which we must model explicitly.
8679
8707
if (auto clangClass = dyn_cast<clang::ObjCInterfaceDecl>(objcContainer)) {
8680
8708
importInheritedConstructors (cast<ClassDecl>(D), members);
8681
-
8682
8709
objcContainer = clangClass->getDefinition ();
8683
- } else if (auto clangProto
8684
- = dyn_cast<clang::ObjCProtocolDecl>(objcContainer)) {
8685
- objcContainer = clangProto->getDefinition ();
8686
8710
}
8687
8711
8688
- // Import mirrored declarations for protocols to which this category
8689
- // or extension conforms.
8712
+ // Interfaces and categories can declare protocol conformances, and
8713
+ // members of those protocols are mirrored into the interface or
8714
+ // category.
8690
8715
// FIXME: This is supposed to be a short-term hack.
8691
- SwiftDeclConverter converter (*this , CurrentVersion);
8692
- converter.importMirroredProtocolMembers (objcContainer, DC, members);
8716
+ importMirroredProtocolMembers (objcContainer, DC, None, members);
8693
8717
}
8694
8718
8695
8719
void ClangImporter::Implementation::loadAllConformances (
0 commit comments