Skip to content

Commit 9c31aa4

Browse files
committed
[Clang Importer] implement swift_newtype(enum) as struct
This is a temporary solution that implements swift_newtype(enum) as though it were written swift_newtype(struct). This is to work around to the fact that a String-backed enum does not actually have a String stored, and a struct is closer to reflecting that storage properly. Struct provides most of the functionality and appearance for now, though it does not allow for switching over the values. Full support for swift_newtype(enum) as a Swift enum is forthcoming.
1 parent faceb55 commit 9c31aa4

File tree

3 files changed

+41
-24
lines changed

3 files changed

+41
-24
lines changed

lib/ClangImporter/ImportDecl.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1280,6 +1280,10 @@ namespace {
12801280
if (auto newtypeAttr =
12811281
Decl->template getAttr<clang::SwiftNewtypeAttr>()) {
12821282
switch (newtypeAttr->getNewtypeKind()) {
1283+
case clang::SwiftNewtypeAttr::NK_Enum:
1284+
// TODO: import as closed enum instead
1285+
1286+
// For now, fall through and treat as a struct
12831287
case clang::SwiftNewtypeAttr::NK_Struct: {
12841288

12851289
auto underlyingType = Impl.importType(
@@ -1305,11 +1309,8 @@ namespace {
13051309
Impl.registerExternalDecl(structDecl);
13061310
return structDecl;
13071311
}
1312+
}
13081313

1309-
case clang::SwiftNewtypeAttr::NK_Enum:
1310-
// TODO: support enum
1311-
break;
1312-
}
13131314
}
13141315
}
13151316

test/IDE/Inputs/custom-modules/Newtype.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,10 @@ extern const SNTErrorDomain SNTFive
1717
__attribute((swift_name("stillAMember")));
1818
extern const SNTErrorDomain SNTElsewhere
1919
__attribute((swift_name("Foo.err")));
20+
21+
typedef NSString *SNTClosedEnum __attribute((swift_newtype(enum)))
22+
__attribute((swift_name("ClosedEnum")));
23+
24+
extern const SNTClosedEnum SNTFirstClosedEntryEnum;
25+
extern const SNTClosedEnum SNTSecondEntry;
26+
extern const SNTClosedEnum SNTClosedEnumThirdEntry;

test/IDE/newtype.swift

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,32 @@
22
// RUN: FileCheck %s -check-prefix=PRINT -strict-whitespace < %t.printed.A.txt
33
// REQUIRES: objc_interop
44

5-
// PRINT: struct ErrorDomain : RawRepresentable {
6-
// PRINT-NEXT: init(rawValue: NSString)
7-
// PRINT-NEXT: let rawValue: NSString
8-
// PRINT-NEXT: }
9-
// PRINT-NEXT: extension ErrorDomain {
10-
// PRINT-NEXT: func process()
11-
// PRINT-NEXT: static let one: ErrorDomain
12-
// PRINT-NEXT: static let errTwo: ErrorDomain
13-
// PRINT-NEXT: static let three: ErrorDomain
14-
// PRINT-NEXT: static let four: ErrorDomain
15-
// PRINT-NEXT: static let stillAMember: ErrorDomain
16-
// PRINT-NEXT: }
17-
// PRINT-NEXT: struct Foo {
18-
// PRINT-NEXT: init()
19-
// PRINT-NEXT: }
20-
// PRINT-NEXT: extension Foo {
21-
// PRINT-NEXT: static let err: ErrorDomain
22-
// PRINT-NEXT: }
23-
24-
5+
// PRINT-LABEL: struct ErrorDomain : RawRepresentable {
6+
// PRINT-NEXT: init(rawValue: NSString)
7+
// PRINT-NEXT: let rawValue: NSString
8+
// PRINT-NEXT: }
9+
// PRINT-NEXT: extension ErrorDomain {
10+
// PRINT-NEXT: func process()
11+
// PRINT-NEXT: static let one: ErrorDomain
12+
// PRINT-NEXT: static let errTwo: ErrorDomain
13+
// PRINT-NEXT: static let three: ErrorDomain
14+
// PRINT-NEXT: static let four: ErrorDomain
15+
// PRINT-NEXT: static let stillAMember: ErrorDomain
16+
// PRINT-NEXT: }
17+
// PRINT-NEXT: struct Foo {
18+
// PRINT-NEXT: init()
19+
// PRINT-NEXT: }
20+
// PRINT-NEXT: extension Foo {
21+
// PRINT-NEXT: static let err: ErrorDomain
22+
// PRINT-NEXT: }
23+
//
24+
// TODO: update test when we can import it as actual new enum
25+
// PRINT-LABEL: struct ClosedEnum : RawRepresentable {
26+
// PRINT-NEXT: init(rawValue: NSString)
27+
// PRINT-NEXT: let rawValue: NSString
28+
// PRINT-NEXT: }
29+
// PRINT-NEXT: extension ClosedEnum {
30+
// PRINT-NEXT: static let firstEntry: ClosedEnum
31+
// PRINT-NEXT: static let secondEntry: ClosedEnum
32+
// PRINT-NEXT: static let thirdEntry: ClosedEnum
33+
// PRINT-NEXT: }

0 commit comments

Comments
 (0)