-
Notifications
You must be signed in to change notification settings - Fork 6.1k
[KT-48068] Add opt-in support for NSEnum for Kotlin Native iOS via an annotation #5539
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 1 commit
99127a7
e709855
16d934e
2cc890d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| /* | ||
| * Copyright 2010-2025 JetBrains s.r.o. and Kotlin Programming Language contributors. | ||
| * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. | ||
| */ | ||
|
|
||
| package kotlin.experimental | ||
|
|
||
| /** | ||
| * This annotation marks the experimental [ObjCEnum][kotlin.native.ObjCEnum] annotation. | ||
|
||
| */ | ||
| @RequiresOptIn | ||
| @Target(AnnotationTarget.ANNOTATION_CLASS) | ||
| @Retention(AnnotationRetention.BINARY) | ||
| @MustBeDocumented | ||
| @SinceKotlin("2.3") | ||
| public annotation class ExperimentalObjCEnum | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| package nativeEnum | ||
|
|
||
| import kotlin.native.ObjCEnum | ||
| import kotlin.experimental.ExperimentalObjCEnum | ||
|
|
||
| @OptIn(kotlin.experimental.ExperimentalObjCEnum::class) | ||
| @ObjCEnum("OBJCFoo", swiftName="SwiftFoo") | ||
| enum class MyKotlinEnum { | ||
| ALPHA, COPY, BAR_FOO | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| import Kt | ||
|
|
||
|
|
||
| private func testNativeEnumValues() throws { | ||
| let ktEnum = MyKotlinEnum.barFoo | ||
| let nsEnum = ktEnum.nsEnum | ||
|
|
||
| switch(nsEnum) { | ||
| case SwiftFoo.alpha: try fail() | ||
| case .barFoo: try assertEquals(actual: nsEnum, expected: ktEnum.nsEnum) | ||
| case .theCopy: try fail() | ||
| } | ||
| } | ||
|
|
||
| class NativeEnumTests : SimpleTestProvider { | ||
| override init() { | ||
| super.init() | ||
|
|
||
| test("TestNativeEnumValues", testNativeEnumValues) | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| /* | ||
| * Copyright 2010-2025 JetBrains s.r.o. and Kotlin Programming Language contributors. | ||
| * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. | ||
| */ | ||
|
|
||
| package org.jetbrains.kotlin.objcexport | ||
|
|
||
| import org.jetbrains.kotlin.backend.konan.objcexport.ObjCClass | ||
| import org.jetbrains.kotlin.backend.konan.objcexport.ObjCTopLevel | ||
|
|
||
| class ObjCExportTranslatedClass( | ||
| val auxiliaryDeclarations: List<ObjCTopLevel>, | ||
| val objCClass: ObjCClass, | ||
| ) | ||
|
|
||
| fun ObjCExportTranslatedClass(objCClass: ObjCClass?) = objCClass?.let { ObjCExportTranslatedClass(emptyList(), it) } | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| /* | ||
| * Copyright 2010-2025 JetBrains s.r.o. and Kotlin Programming Language contributors. | ||
| * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. | ||
| */ | ||
|
|
||
| package org.jetbrains.kotlin.objcexport | ||
|
|
||
| import org.jetbrains.kotlin.analysis.api.symbols.KaClassSymbol | ||
| import org.jetbrains.kotlin.backend.konan.KonanFqNames | ||
| import org.jetbrains.kotlin.backend.konan.objcexport.ObjCExportNSEnumTypeName | ||
| import org.jetbrains.kotlin.name.ClassId | ||
|
|
||
| /** Returns the NSEnum type for the given enum type if the corresponding annotation is set; null otherwise */ | ||
| fun ObjCExportContext.getNSEnumTypeName(symbol: KaClassSymbol): ObjCExportNSEnumTypeName? { | ||
| val classId = ClassId.topLevel(KonanFqNames.objCEnum) | ||
| val annotation = symbol.annotations[classId].firstOrNull() ?: return null | ||
|
|
||
| val name = annotation.findArgument("name")?.resolveStringConstantValue()?.ifEmpty { null } | ||
| ?: (getObjCClassOrProtocolName(symbol).objCName + "NSEnum") | ||
| val swiftName = annotation.findArgument("swiftName")?.resolveStringConstantValue()?.ifEmpty { null } ?: name | ||
| return ObjCExportNSEnumTypeName(swiftName = swiftName, objCName = name) | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ideally, we should check that the annotation is not applied to non-
enumclasses.A new frontend checker is needed for that. See an example here:
kotlin/compiler/fir/checkers/checkers.native/src/org/jetbrains/kotlin/fir/analysis/native/checkers/FirNativeThreadLocalChecker.kt
Line 24 in 360c000
To avoid stalling this PR, this can be done separately.