forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[cxx-interop] Support scoped enums (enum classes).
Simply treat scoped enums as (pre-existing) "non frozen enums". C++ scoped enums are actually imported as Swift enums (unlike other enums) and no global variables need be created (given their "scoped" nature).
- Loading branch information
Showing
7 changed files
with
196 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
module BoolEnums { | ||
header "bool-enums.h" | ||
} | ||
|
||
module ScopedEnums { | ||
header "scoped-enums.h" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
enum class ScopedEnumDefined { x = 0, y = 2 }; | ||
|
||
enum class ScopedEnumBasic { x, y, z }; | ||
|
||
enum class ScopedEnumCharDefined : char { x = 0, y = 2 }; | ||
|
||
enum class ScopedEnumUnsignedDefined : unsigned int { x = 0, y = 2 }; | ||
|
||
enum class ScopedEnumUnsignedLongDefined : unsigned long { x = 0, y = 2 }; | ||
|
||
enum class ScopedEnumChar : char { x, y, z }; | ||
|
||
enum class ScopedEnumUnsigned : unsigned int { x, y, z }; | ||
|
||
enum class ScopedEnumUnsignedLong : unsigned long { x, y, z }; | ||
|
||
enum class ScopedEnumInt : int { x, y, z }; | ||
|
||
enum class ScopedEnumNegativeElement : int { x = -1, y = 0, z = 2 }; | ||
|
||
enum class MiddleDefinedScopedEnum { x, y = 42, z }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
// RUN: %target-swift-ide-test -print-module -module-to-print=ScopedEnums -I %S/Inputs -source-filename=x -enable-cxx-interop | %FileCheck %s | ||
|
||
// CHECK: enum ScopedEnumDefined : Int32 { | ||
// CHECK: init?(rawValue: Int32) | ||
// CHECK: var rawValue: Int32 { get } | ||
// CHECK: typealias RawValue = Int32 | ||
// CHECK: case x | ||
// CHECK: case y | ||
// CHECK: } | ||
|
||
// CHECK: enum ScopedEnumBasic : Int32 { | ||
// CHECK: init?(rawValue: Int32) | ||
// CHECK: var rawValue: Int32 { get } | ||
// CHECK: typealias RawValue = Int32 | ||
// CHECK: case x | ||
// CHECK: case y | ||
// CHECK: case z | ||
// CHECK: } | ||
|
||
// CHECK: enum ScopedEnumCharDefined : CChar { | ||
// CHECK: init?(rawValue: CChar) | ||
// CHECK: var rawValue: CChar { get } | ||
// CHECK: typealias RawValue = CChar | ||
// CHECK: case x | ||
// CHECK: case y | ||
// CHECK: } | ||
|
||
// CHECK: enum ScopedEnumUnsignedDefined : UInt32 { | ||
// CHECK: init?(rawValue: UInt32) | ||
// CHECK: var rawValue: UInt32 { get } | ||
// CHECK: typealias RawValue = UInt32 | ||
// CHECK: case x | ||
// CHECK: case y | ||
// CHECK: } | ||
|
||
// CHECK: enum ScopedEnumUnsignedLongDefined : [[UINT_T:UInt|UInt32]] { | ||
// CHECK: init?(rawValue: [[UINT_T]]) | ||
// CHECK: var rawValue: [[UINT_T]] { get } | ||
// CHECK: typealias RawValue = [[UINT_T]] | ||
// CHECK: case x | ||
// CHECK: case y | ||
// CHECK: } | ||
|
||
// CHECK: enum ScopedEnumChar : CChar { | ||
// CHECK: init?(rawValue: CChar) | ||
// CHECK: var rawValue: CChar { get } | ||
// CHECK: typealias RawValue = CChar | ||
// CHECK: case x | ||
// CHECK: case y | ||
// CHECK: case z | ||
// CHECK: } | ||
|
||
// CHECK: enum ScopedEnumUnsigned : UInt32 { | ||
// CHECK: init?(rawValue: UInt32) | ||
// CHECK: var rawValue: UInt32 { get } | ||
// CHECK: typealias RawValue = UInt32 | ||
// CHECK: case x | ||
// CHECK: case y | ||
// CHECK: case z | ||
// CHECK: } | ||
|
||
// CHECK: enum ScopedEnumUnsignedLong : [[UINT_T]] { | ||
// CHECK: init?(rawValue: [[UINT_T]]) | ||
// CHECK: var rawValue: [[UINT_T]] { get } | ||
// CHECK: typealias RawValue = [[UINT_T]] | ||
// CHECK: case x | ||
// CHECK: case y | ||
// CHECK: case z | ||
// CHECK: } | ||
|
||
// CHECK: enum ScopedEnumInt : Int32 { | ||
// CHECK: init?(rawValue: Int32) | ||
// CHECK: var rawValue: Int32 { get } | ||
// CHECK: typealias RawValue = Int32 | ||
// CHECK: case x | ||
// CHECK: case y | ||
// CHECK: case z | ||
// CHECK: } | ||
|
||
// CHECK: enum ScopedEnumNegativeElement : Int32 { | ||
// CHECK: init?(rawValue: Int32) | ||
// CHECK: var rawValue: Int32 { get } | ||
// CHECK: typealias RawValue = Int32 | ||
// CHECK: case x | ||
// CHECK: case y | ||
// CHECK: case z | ||
// CHECK: } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// RUN: %target-swift-emit-sil %s -I %S/Inputs -enable-cxx-interop | %FileCheck %s | ||
|
||
import ScopedEnums | ||
|
||
// CHECK-LABEL: sil @$s4main24returnsScopedEnumDefinedSo0cdE0VyF : $@convention(thin) () -> ScopedEnumDefined | ||
// CHECK: [[OUT:%.*]] = enum $ScopedEnumDefined, #ScopedEnumDefined.x!enumelt | ||
// CHECK: return [[OUT]] : $ScopedEnumDefined | ||
// CHECK-LABEL: end sil function '$s4main24returnsScopedEnumDefinedSo0cdE0VyF' | ||
public func returnsScopedEnumDefined() -> ScopedEnumDefined { | ||
return .x | ||
} | ||
|
||
// CHECK-LABEL: sil @$s4main22returnsScopedEnumBasicSo0cdE0VyF : $@convention(thin) () -> ScopedEnumBasic | ||
// CHECK: [[OUT:%.*]] = enum $ScopedEnumBasic, #ScopedEnumBasic.x!enumelt | ||
// CHECK: return [[OUT]] : $ScopedEnumBasic | ||
// CHECK-LABEL: end sil function '$s4main22returnsScopedEnumBasicSo0cdE0VyF' | ||
public func returnsScopedEnumBasic() -> ScopedEnumBasic { | ||
return .x | ||
} | ||
|
||
// CHECK-LABEL: sil @$s4main28returnsScopedEnumCharDefinedSo0cdeF0VyF : $@convention(thin) () -> ScopedEnumCharDefined | ||
// CHECK: [[OUT:%.*]] = enum $ScopedEnumCharDefined, #ScopedEnumCharDefined.x!enumelt | ||
// CHECK: return [[OUT]] : $ScopedEnumCharDefined | ||
// CHECK-LABEL: end sil function '$s4main28returnsScopedEnumCharDefinedSo0cdeF0VyF' | ||
public func returnsScopedEnumCharDefined() -> ScopedEnumCharDefined { | ||
return .x | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// RUN: %target-run-simple-swift(-I %S/Inputs -Xfrontend -enable-cxx-interop) | ||
|
||
// REQUIRES: executable_test | ||
|
||
import ScopedEnums | ||
import StdlibUnittest | ||
|
||
var ScopedEnumsTestSuite = TestSuite("Scoped Enums") | ||
|
||
ScopedEnumsTestSuite.test("Make and compare") { | ||
let val: ScopedEnumDefined = .x | ||
expectEqual(val, .x) | ||
} | ||
|
||
ScopedEnumsTestSuite.test("Make and compare (not equal)") { | ||
let val: ScopedEnumDefined = .x | ||
expectNotEqual(val, .y) | ||
} | ||
|
||
func makeScopedEnumBasic() -> ScopedEnumBasic { .z } | ||
|
||
ScopedEnumsTestSuite.test("Make and compare (ScopedEnumBasic)") { | ||
let val: ScopedEnumBasic = .x | ||
expectNotEqual(val, makeScopedEnumBasic()) | ||
expectEqual(.z, makeScopedEnumBasic()) | ||
} | ||
|
||
ScopedEnumsTestSuite.test("Make and compare (ScopedEnumCharDefined)") { | ||
expectEqual(ScopedEnumCharDefined(rawValue: 2), .y) | ||
expectNotEqual(ScopedEnumCharDefined(rawValue: 2), ScopedEnumCharDefined(rawValue: 0)) | ||
} | ||
|
||
ScopedEnumsTestSuite.test("Make and compare (ScopedEnumNegativeElement)") { | ||
expectEqual(ScopedEnumNegativeElement(rawValue: -1), .x) | ||
expectNotEqual(ScopedEnumNegativeElement(rawValue: 0), .x) | ||
} | ||
|
||
ScopedEnumsTestSuite.test("Make and compare (MiddleDefinedScopedEnum)") { | ||
expectEqual(MiddleDefinedScopedEnum(rawValue: 42), .y) | ||
expectEqual(MiddleDefinedScopedEnum(rawValue: 43), .z) | ||
} | ||
|
||
runAllTests() |