@@ -1893,28 +1893,54 @@ namespace {
1893
1893
// / Note: Use this rather than calling Impl.importFullName directly!
1894
1894
ImportedName importFullName (const clang::NamedDecl *D,
1895
1895
Optional<ImportedName> &correctSwiftName) {
1896
- if (isActiveSwiftVersion ()) {
1897
- // Just import the current Swift name.
1898
- correctSwiftName = None;
1899
- return Impl.importFullName (D, getVersion ());
1896
+ ImportNameVersion canonicalVersion = getActiveSwiftVersion ();
1897
+ if (isa<clang::TypeDecl>(D) || isa<clang::ObjCContainerDecl>(D)) {
1898
+ canonicalVersion = ImportNameVersion::ForTypes;
1899
+ }
1900
+ correctSwiftName = None;
1901
+
1902
+ // First, import based on the Swift name of the canonical declaration:
1903
+ // the latest version for types and the current version for non-type
1904
+ // values. If that fails, we won't do anything.
1905
+ auto canonicalName = Impl.importFullName (D, canonicalVersion);
1906
+ if (!canonicalName)
1907
+ return ImportedName ();
1908
+
1909
+ if (getVersion () == canonicalVersion) {
1910
+ // Make sure we don't try to import the same type twice as canonical.
1911
+ if (canonicalVersion != getActiveSwiftVersion ()) {
1912
+ auto activeName = Impl.importFullName (D, getActiveSwiftVersion ());
1913
+ if (activeName &&
1914
+ activeName.getDeclName () == canonicalName.getDeclName ()) {
1915
+ return ImportedName ();
1916
+ }
1917
+ }
1918
+
1919
+ return canonicalName;
1900
1920
}
1901
1921
1902
1922
// Special handling when we import using the older Swift name.
1903
1923
//
1904
- // First, import based on the current Swift name. If that fails, we won't
1905
- // do anything.
1906
- correctSwiftName = Impl.importFullName (D, getActiveSwiftVersion ());
1907
- if (!*correctSwiftName)
1908
- return {};
1909
-
1910
1924
// Import using the alternate Swift name. If that fails, or if it's
1911
1925
// identical to the active Swift name, we won't introduce an alternate
1912
1926
// Swift name stub declaration.
1913
1927
auto alternateName = Impl.importFullName (D, getVersion ());
1914
- if (!alternateName || alternateName. getDeclName () == correctSwiftName-> getDeclName () )
1928
+ if (!alternateName)
1915
1929
return ImportedName ();
1916
1930
1917
- // Okay, return the alternate Swift name.
1931
+ if (alternateName.getDeclName () == canonicalName.getDeclName ()) {
1932
+ if (getVersion () == getActiveSwiftVersion ()) {
1933
+ assert (canonicalVersion != getActiveSwiftVersion ());
1934
+ return alternateName;
1935
+ }
1936
+ return ImportedName ();
1937
+ }
1938
+
1939
+ // Always use the active version as the preferred name, even if the
1940
+ // canonical name is a different version.
1941
+ correctSwiftName = Impl.importFullName (D, getActiveSwiftVersion ());
1942
+ assert (correctSwiftName);
1943
+
1918
1944
return alternateName;
1919
1945
}
1920
1946
@@ -2035,6 +2061,11 @@ namespace {
2035
2061
// / Mark the given declaration as an older Swift version variant of the
2036
2062
// / current name.
2037
2063
void markAsVariant (Decl *decl, ImportedName correctSwiftName) {
2064
+ // Types always import using the latest version. Make sure all names up
2065
+ // to that version are considered available.
2066
+ if (isa<TypeDecl>(decl) && getVersion () >= getActiveSwiftVersion ())
2067
+ return ;
2068
+
2038
2069
// TODO: some versions should be deprecated instead of unavailable
2039
2070
2040
2071
ASTContext &ctx = decl->getASTContext ();
@@ -3853,7 +3884,7 @@ namespace {
3853
3884
return nullptr ;
3854
3885
3855
3886
// Find the Swift class being extended.
3856
- auto objcClass = cast_or_null <ClassDecl>(
3887
+ auto objcClass = castIgnoringCompatibilityAlias <ClassDecl>(
3857
3888
Impl.importDecl (decl->getClassInterface (), getActiveSwiftVersion ()));
3858
3889
if (!objcClass)
3859
3890
return nullptr ;
@@ -4669,7 +4700,7 @@ SwiftDeclConverter::importCFClassType(const clang::TypedefNameDecl *decl,
4669
4700
if (auto attr = record->getAttr <clang::ObjCBridgeAttr>()) {
4670
4701
// Record the Objective-C class to which this CF type is toll-free
4671
4702
// bridged.
4672
- if (ClassDecl *objcClass = dyn_cast_or_null <ClassDecl>(
4703
+ if (ClassDecl *objcClass = dynCastIgnoringCompatibilityAlias <ClassDecl>(
4673
4704
Impl.importDeclByName (attr->getBridgedType ()->getName ()))) {
4674
4705
theClass->getAttrs ().add (new (Impl.SwiftContext )
4675
4706
ObjCBridgedAttr (objcClass));
@@ -4679,7 +4710,7 @@ SwiftDeclConverter::importCFClassType(const clang::TypedefNameDecl *decl,
4679
4710
if (auto attr = record->getAttr <clang::ObjCBridgeMutableAttr>()) {
4680
4711
// Record the Objective-C class to which this CF type is toll-free
4681
4712
// bridged.
4682
- if (ClassDecl *objcClass = dyn_cast_or_null <ClassDecl>(
4713
+ if (ClassDecl *objcClass = dynCastIgnoringCompatibilityAlias <ClassDecl>(
4683
4714
Impl.importDeclByName (attr->getBridgedType ()->getName ()))) {
4684
4715
theClass->getAttrs ().add (new (Impl.SwiftContext )
4685
4716
ObjCBridgedAttr (objcClass));
@@ -4696,7 +4727,11 @@ Decl *SwiftDeclConverter::importCompatibilityTypeAlias(
4696
4727
ImportedName correctSwiftName) {
4697
4728
// Import the referenced declaration. If it doesn't come in as a type,
4698
4729
// we don't care.
4699
- auto importedDecl = Impl.importDecl (decl, getActiveSwiftVersion ());
4730
+ Decl *importedDecl = nullptr ;
4731
+ if (getVersion () >= getActiveSwiftVersion ())
4732
+ importedDecl = Impl.importDecl (decl, ImportNameVersion::ForTypes);
4733
+ if (!importedDecl)
4734
+ importedDecl = Impl.importDecl (decl, getActiveSwiftVersion ());
4700
4735
auto typeDecl = dyn_cast_or_null<TypeDecl>(importedDecl);
4701
4736
if (!typeDecl)
4702
4737
return nullptr ;
@@ -6193,7 +6228,7 @@ void SwiftDeclConverter::importObjCProtocols(
6193
6228
6194
6229
for (auto cp = clangProtocols.begin (), cpEnd = clangProtocols.end ();
6195
6230
cp != cpEnd; ++cp) {
6196
- if (auto proto = cast_or_null <ProtocolDecl>(
6231
+ if (auto proto = castIgnoringCompatibilityAlias <ProtocolDecl>(
6197
6232
Impl.importDecl (*cp, getActiveSwiftVersion ()))) {
6198
6233
addProtocols (proto, protocols, knownProtocols);
6199
6234
inheritedTypes.push_back (TypeLoc::withoutLoc (proto->getDeclaredType ()));
@@ -6275,7 +6310,7 @@ Optional<GenericParamList *> SwiftDeclConverter::importObjCGenericParams(
6275
6310
inherited.push_back (TypeLoc::withoutLoc (superclassType));
6276
6311
}
6277
6312
for (clang::ObjCProtocolDecl *clangProto : clangBound->quals ()) {
6278
- ProtocolDecl *proto = cast_or_null <ProtocolDecl>(
6313
+ ProtocolDecl *proto = castIgnoringCompatibilityAlias <ProtocolDecl>(
6279
6314
Impl.importDecl (clangProto, getActiveSwiftVersion ()));
6280
6315
if (!proto) {
6281
6316
return None;
@@ -6915,7 +6950,7 @@ ClangImporter::Implementation::importDeclImpl(const clang::NamedDecl *ClangDecl,
6915
6950
6916
6951
if (hasMissingRequiredMember) {
6917
6952
// Mark the protocol as having missing requirements.
6918
- if (auto proto = cast_or_null <ProtocolDecl>(
6953
+ if (auto proto = castIgnoringCompatibilityAlias <ProtocolDecl>(
6919
6954
importDecl (clangProto, CurrentVersion))) {
6920
6955
proto->setHasMissingRequirements (true );
6921
6956
}
@@ -6929,7 +6964,7 @@ ClangImporter::Implementation::importDeclImpl(const clang::NamedDecl *ClangDecl,
6929
6964
// Only allow this to affect declarations in the same top-level module
6930
6965
// as the original class.
6931
6966
if (getClangModuleForDecl (theClass) == getClangModuleForDecl (method)) {
6932
- if (auto swiftClass = cast_or_null <ClassDecl>(
6967
+ if (auto swiftClass = castIgnoringCompatibilityAlias <ClassDecl>(
6933
6968
importDecl (theClass, CurrentVersion))) {
6934
6969
swiftClass->setHasMissingDesignatedInitializers ();
6935
6970
}
0 commit comments