Skip to content

Commit 2fa3da4

Browse files
committed
Drop the notion of "alias" names for CF types.
Previously we imported a Core Foundation type "CCFooRef" as "CCFoo", but also provided a typealias "CCFooRef". In Swift 3, we decided to mark "CCFooRef" unavailable to force developers to consistently use "CCFoo". Now that we have infrastructure to mark /all/ renamed declarations as unavailable, just use that to track the renaming, i.e. pretend that "CCFooRef" was the "Swift 2" name for the type. This doesn't change the conflict resolution behavior: if there's another name "CCFoo" in the same module, the CF type will be imported as just "CCFooRef". Groundwork cleanup for rdar://problem/26347297, which notes that our import-as-member fix-its use the "Ref" names rather than the short names.
1 parent 37d96dc commit 2fa3da4

File tree

11 files changed

+67
-190
lines changed

11 files changed

+67
-190
lines changed

lib/ClangImporter/CFTypeInfo.cpp

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -101,14 +101,6 @@ CFPointeeInfo::classifyTypedef(const clang::TypedefNameDecl *typedefDecl) {
101101
return forInvalid();
102102
}
103103

104-
/// Return the name to import a CF typedef as.
105-
static StringRef getImportedCFTypeName(StringRef name) {
106-
// If the name ends in the CF typedef suffix ("Ref"), drop that.
107-
if (name.endswith(SWIFT_CFTYPE_SUFFIX))
108-
return name.drop_back(strlen(SWIFT_CFTYPE_SUFFIX));
109-
return name;
110-
}
111-
112104
bool ClangImporter::Implementation::isCFTypeDecl(
113105
const clang::TypedefNameDecl *Decl) {
114106
if (CFPointeeInfo::classifyTypedef(Decl))
@@ -117,19 +109,12 @@ bool ClangImporter::Implementation::isCFTypeDecl(
117109
}
118110

119111
StringRef ClangImporter::Implementation::getCFTypeName(
120-
const clang::TypedefNameDecl *decl,
121-
StringRef *secondaryName) {
122-
if (secondaryName) *secondaryName = "";
123-
112+
const clang::TypedefNameDecl *decl) {
124113
if (auto pointee = CFPointeeInfo::classifyTypedef(decl)) {
125114
auto name = decl->getName();
126-
if (pointee.isRecord() || pointee.isTypedef()) {
127-
auto resultName = getImportedCFTypeName(name);
128-
if (secondaryName && name != resultName)
129-
*secondaryName = name;
130-
131-
return resultName;
132-
}
115+
if (pointee.isRecord() || pointee.isTypedef())
116+
if (name.endswith(SWIFT_CFTYPE_SUFFIX))
117+
return name.drop_back(strlen(SWIFT_CFTYPE_SUFFIX));
133118

134119
return name;
135120
}

lib/ClangImporter/ClangImporter.cpp

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -779,10 +779,6 @@ void ClangImporter::Implementation::addEntryToLookupTable(
779779
if (auto importedName = importFullName(named, None, &clangSema)) {
780780
table.addEntry(importedName.Imported, named, importedName.EffectiveContext);
781781

782-
// Also add the alias, if needed.
783-
if (importedName.Alias)
784-
table.addEntry(importedName.Alias, named, importedName.EffectiveContext);
785-
786782
// Also add the subscript entry, if needed.
787783
if (importedName.isSubscriptAccessor())
788784
table.addEntry(DeclName(SwiftContext, SwiftContext.Id_subscript,
@@ -2784,19 +2780,11 @@ auto ClangImporter::Implementation::importFullName(
27842780
// Typedef declarations might be CF types that will drop the "Ref"
27852781
// suffix.
27862782
clang::ASTContext &clangCtx = clangSema.Context;
2787-
bool aliasIsFunction = false;
2788-
bool aliasIsInitializer = false;
2789-
StringRef aliasBaseName;
2790-
SmallVector<StringRef, 4> aliasArgumentNames;
2791-
if (auto typedefNameDecl = dyn_cast<clang::TypedefNameDecl>(D)) {
2792-
auto swiftName = getCFTypeName(typedefNameDecl, &aliasBaseName);
2793-
if (!swiftName.empty()) {
2794-
if (!aliasBaseName.empty() &&
2795-
hasConflict(&clangCtx.Idents.get(swiftName), typedefNameDecl)) {
2796-
// Use the alias name (the "Ref" name), only.
2797-
baseName = aliasBaseName;
2798-
aliasBaseName = StringRef();
2799-
} else {
2783+
if (!swift2Name) {
2784+
if (auto typedefNameDecl = dyn_cast<clang::TypedefNameDecl>(D)) {
2785+
auto swiftName = getCFTypeName(typedefNameDecl);
2786+
if (!swiftName.empty() &&
2787+
!hasConflict(&clangCtx.Idents.get(swiftName), typedefNameDecl)) {
28002788
// Adopt the requested name.
28012789
baseName = swiftName;
28022790
}
@@ -2926,7 +2914,6 @@ auto ClangImporter::Implementation::importFullName(
29262914
// If this declaration has the swift_private attribute, prepend "__" to the
29272915
// appropriate place.
29282916
SmallString<16> swiftPrivateScratch;
2929-
SmallString<16> swiftPrivateAliasScratch;
29302917
if (hasSwiftPrivate(D)) {
29312918
// Make the given name private.
29322919
//
@@ -2966,19 +2953,10 @@ auto ClangImporter::Implementation::importFullName(
29662953
if (makeNamePrivate(isInitializer, baseName, argumentNames,
29672954
result.InitKind, swiftPrivateScratch))
29682955
return result;
2969-
2970-
// If we have an alias name, make it private as well.
2971-
if (!aliasBaseName.empty()) {
2972-
(void)makeNamePrivate(aliasIsInitializer, aliasBaseName,
2973-
aliasArgumentNames, CtorInitializerKind::Designated,
2974-
swiftPrivateAliasScratch);
2975-
}
29762956
}
29772957

29782958
result.Imported = formDeclName(SwiftContext, baseName, argumentNames,
29792959
isFunction);
2980-
result.Alias = formDeclName(SwiftContext, aliasBaseName, aliasArgumentNames,
2981-
aliasIsFunction);
29822960
return result;
29832961
}
29842962

lib/ClangImporter/ImportDecl.cpp

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1452,33 +1452,6 @@ namespace {
14521452
if (!DC)
14531453
return nullptr;
14541454

1455-
// Local function to create the alias, if needed.
1456-
auto createAlias = [&](TypeDecl *primary) {
1457-
if (!importedName.Alias) return;
1458-
1459-
auto aliasRef = Impl.createDeclWithClangNode<TypeAliasDecl>(
1460-
Decl,
1461-
Impl.importSourceLoc(Decl->getLocStart()),
1462-
importedName.Alias.getBaseName(),
1463-
Impl.importSourceLoc(Decl->getLocation()),
1464-
TypeLoc::withoutLoc(
1465-
primary->getDeclaredInterfaceType()),
1466-
/*genericparams*/nullptr, DC);
1467-
aliasRef->computeType();
1468-
1469-
// Record this as the alternate declaration.
1470-
Impl.AlternateDecls[primary] = aliasRef;
1471-
1472-
// The "Ref" variants have been removed.
1473-
auto attr =
1474-
AvailableAttr::createUnconditional(
1475-
Impl.SwiftContext,
1476-
"",
1477-
primary->getName().str(),
1478-
UnconditionalAvailabilityKind::UnavailableInSwift);
1479-
aliasRef->getAttrs().add(attr);
1480-
};
1481-
14821455
if (auto pointee = CFPointeeInfo::classifyTypedef(Decl)) {
14831456
// If the pointee is a record, consider creating a class type.
14841457
if (pointee.isRecord()) {
@@ -1489,7 +1462,6 @@ namespace {
14891462

14901463
Impl.SpecialTypedefNames[Decl->getCanonicalDecl()] =
14911464
MappedTypeNameKind::DefineAndUse;
1492-
createAlias(swiftClass);
14931465
return swiftClass;
14941466
}
14951467

@@ -1524,7 +1496,6 @@ namespace {
15241496

15251497
Impl.SpecialTypedefNames[Decl->getCanonicalDecl()] =
15261498
MappedTypeNameKind::DefineAndUse;
1527-
createAlias(typealias);
15281499
return typealias;
15291500
}
15301501

@@ -1550,7 +1521,6 @@ namespace {
15501521

15511522
Impl.SpecialTypedefNames[Decl->getCanonicalDecl()] =
15521523
MappedTypeNameKind::DefineAndUse;
1553-
createAlias(typealias);
15541524
return typealias;
15551525
}
15561526
}

lib/ClangImporter/ImporterImpl.h

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -780,8 +780,7 @@ class LLVM_LIBRARY_VISIBILITY ClangImporter::Implementation
780780

781781
/// Determine the imported CF type for the given typedef-name, or the empty
782782
/// string if this is not an imported CF type name.
783-
static StringRef getCFTypeName(const clang::TypedefNameDecl *decl,
784-
StringRef *secondaryName = nullptr);
783+
static StringRef getCFTypeName(const clang::TypedefNameDecl *decl);
785784

786785
/// Retrieve the type name of a Clang type for the purposes of
787786
/// omitting unneeded words.
@@ -837,10 +836,6 @@ class LLVM_LIBRARY_VISIBILITY ClangImporter::Implementation
837836
/// The imported name.
838837
DeclName Imported;
839838

840-
/// An additional alias to the imported name, which should be
841-
/// recorded in name lookup tables as well.
842-
DeclName Alias;
843-
844839
/// Whether this name was explicitly specified via a Clang
845840
/// swift_name attribute.
846841
bool HasCustomName = false;

test/ClangModules/Inputs/SwiftPrivateAttr.txt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,7 @@ struct NSOptions : OptionSet {
9595
static var __privA: NSOptions { get }
9696
static var B: NSOptions { get }
9797
}
98-
@available(*, unavailable, renamed: "__PrivCFType", message: "Not available in Swift")
99-
typealias __PrivCFTypeRef = __PrivCFType
10098
class __PrivCFType {
10199
}
102-
@available(*, unavailable, renamed: "__PrivCFSub", message: "Not available in Swift")
103-
typealias __PrivCFSubRef = __PrivCFSub
104100
typealias __PrivCFSub = __PrivCFType
105101
typealias __PrivInt = Int32

test/IDE/Inputs/mock-sdk/Foo.annotated.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,8 +266,6 @@ class <loc>FooClassDerived</loc> : <ref:Class>FooClassBase</ref>, <ref:Protocol>
266266
}</decl>
267267
<decl:Class>class <loc>FooCFType</loc> {
268268
}</decl>
269-
<decl:TypeAlias>@available(*, unavailable, renamed: "FooCFType", message: "Not available in Swift")
270-
typealias <loc>FooCFTypeRef</loc> = <ref:Class>FooCFType</ref></decl>
271269
<decl:Func>@available(*, unavailable, message: "Core Foundation objects are automatically memory managed")
272270
func <loc>FooCFTypeRelease(<decl:Param>_: <ref:Class>FooCFType</ref>!</decl>)</loc></decl>
273271
<decl:Class>class <loc>FooRepeatedMembers</loc> : <ref:Class>FooClassBase</ref> {

test/IDE/Inputs/mock-sdk/Foo.printed.recursive.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,8 +266,6 @@ class FooUnavailableMembers : FooClassBase {
266266
}
267267
class FooCFType {
268268
}
269-
@available(*, unavailable, renamed: "FooCFType", message: "Not available in Swift")
270-
typealias FooCFTypeRef = FooCFType
271269
@available(*, unavailable, message: "Core Foundation objects are automatically memory managed")
272270
func FooCFTypeRelease(_: FooCFType!)
273271
class FooRepeatedMembers : FooClassBase {

test/IDE/Inputs/mock-sdk/Foo.printed.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -321,8 +321,6 @@ class FooUnavailableMembers : FooClassBase {
321321

322322
class FooCFType {
323323
}
324-
@available(*, unavailable, renamed: "FooCFType", message: "Not available in Swift")
325-
typealias FooCFTypeRef = FooCFType
326324
@available(*, unavailable, message: "Core Foundation objects are automatically memory managed")
327325
func FooCFTypeRelease(_: FooCFType!)
328326

test/IDE/infer_import_as_member.swift

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,6 @@ let mine = IAMStruct1()
101101
// PRINT-LABEL: /// Class
102102
// PRINT-NEXT: class IAMClass {
103103
// PRINT-NEXT: }
104-
//
105-
// PRINT-LABEL: /// Class
106-
// PRINT-NEXT: @available(*, unavailable, renamed: "IAMClass", message: "Not available in Swift")
107-
// PRINT-NEXT: typealias IAMClassRef = IAMClass
108104
// PRINT-NEXT: typealias IAMOtherName = IAMClass
109105
// PRINT-NEXT: extension IAMClass {
110106
// PRINT-NEXT: class var typeID: UInt32 { get }

test/IDE/print_omit_needless_words.swift

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -222,10 +222,6 @@
222222
// Note: no lowercasing of initialisms when there might be a prefix.
223223
// CHECK-CORECOOLING: func CFBottom() ->
224224

225-
// Note: "Ref" variants are unavailable.
226-
// CHECK-CORECOOLING: @available(*, unavailable, renamed: "CCPowerSupply", message: "Not available in Swift")
227-
// CHECK-CORECOOLING-NEXT: typealias CCPowerSupplyRef = CCPowerSupply
228-
229225
// Note: Skipping over "Ref"
230226
// CHECK-CORECOOLING: func replace(_: CCPowerSupply!)
231227

0 commit comments

Comments
 (0)