Skip to content

Commit f473851

Browse files
committed
[PrintObjC] Support @objc(name) on enum cases
1 parent a3c62ca commit f473851

File tree

2 files changed

+27
-8
lines changed

2 files changed

+27
-8
lines changed

lib/PrintAsObjC/PrintAsObjC.cpp

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,17 +71,18 @@ namespace {
7171
};
7272
}
7373

74-
static Identifier getNameForObjC(const NominalTypeDecl *NTD,
74+
static Identifier getNameForObjC(const ValueDecl *VD,
7575
CustomNamesOnly_t customNamesOnly = Normal) {
76-
assert(isa<ClassDecl>(NTD) || isa<ProtocolDecl>(NTD) || isa<EnumDecl>(NTD));
77-
if (auto objc = NTD->getAttrs().getAttribute<ObjCAttr>()) {
76+
assert(isa<ClassDecl>(VD) || isa<ProtocolDecl>(VD)
77+
|| isa<EnumDecl>(VD) || isa<EnumElementDecl>(VD));
78+
if (auto objc = VD->getAttrs().getAttribute<ObjCAttr>()) {
7879
if (auto name = objc->getName()) {
7980
assert(name->getNumSelectorPieces() == 1);
8081
return name->getSelectorPieces().front();
8182
}
8283
}
8384

84-
return customNamesOnly ? Identifier() : NTD->getName();
85+
return customNamesOnly ? Identifier() : VD->getName();
8586
}
8687

8788

@@ -255,12 +256,18 @@ class ObjCPrinter : private DeclVisitor<ObjCPrinter>,
255256
// Print the cases as the concatenation of the enum name with the case
256257
// name.
257258
os << " ";
258-
if (customName.empty()) {
259-
os << ED->getName();
259+
Identifier customEltName = getNameForObjC(Elt, CustomNamesOnly);
260+
if (customEltName.empty()) {
261+
if (customName.empty()) {
262+
os << ED->getName();
263+
} else {
264+
os << customName;
265+
}
266+
os << Elt->getName();
260267
} else {
261-
os << customName;
268+
os << customEltName
269+
<< " SWIFT_COMPILE_NAME(\"" << Elt->getName() << "\")";
262270
}
263-
os << Elt->getName();
264271

265272
if (auto ILE = cast_or_null<IntegerLiteralExpr>(Elt->getRawValueExpr())) {
266273
os << " = ";

test/PrintAsObjC/enums.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,18 @@ import Foundation
3636
case A, B, C
3737
}
3838

39+
// CHECK-LABEL: typedef SWIFT_ENUM(NSInteger, EnumWithNamedConstants) {
40+
// CHECK-NEXT: kEnumA SWIFT_COMPILE_NAME("A") = 0,
41+
// CHECK-NEXT: kEnumB SWIFT_COMPILE_NAME("B") = 1,
42+
// CHECK-NEXT: kEnumC SWIFT_COMPILE_NAME("C") = 2,
43+
// CHECK-NEXT: };
44+
45+
@objc enum EnumWithNamedConstants: Int {
46+
@objc(kEnumA) case A
47+
@objc(kEnumB) case B
48+
@objc(kEnumC) case C
49+
}
50+
3951
// CHECK-LABEL: typedef SWIFT_ENUM(unsigned int, ExplicitValues) {
4052
// CHECK-NEXT: ExplicitValuesZim = 0,
4153
// CHECK-NEXT: ExplicitValuesZang = 219,

0 commit comments

Comments
 (0)