-
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
Open
stefanhaustein
wants to merge
23
commits into
JetBrains:master
Choose a base branch
from
stefanhaustein:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
2ab6493
make 'something' visible end-to-end
stefanhaustein b824f55
End-to-end wireup with test; method body missing
stefanhaustein e180ad4
current state (incomplete)
stefanhaustein 6ab07ae
cleanup
stefanhaustein b925ff2
cleanup
stefanhaustein e7ec42e
cleanup
stefanhaustein 7e4fb66
ordinal should be in KotlinEnum
stefanhaustein b713914
refactored to avoid ObjCMethodForKotlinMethod
stefanhaustein b126cd4
move symbol resolution from ObjCExportCodeSpec to ObjCExportCodeGener…
stefanhaustein f51d934
swift test compiles
stefanhaustein 854371f
remove footgun
stefanhaustein 4013372
objc/switft name distinction / adjustment
stefanhaustein c9de8d8
Merge branch 'JetBrains:master' into master
stefanhaustein 200acce
Merge branch 'JetBrains:master' into master
stefanhaustein 29e8031
first round of review comments / snapshot before changing to property
stefanhaustein de01982
Merge branch 'JetBrains:master' into master
stefanhaustein 0ccc118
Review comments addressed; mostly adding the missing AA impl
stefanhaustein 60cca82
Make the enum entry value / order more explicit / add a clarifying co…
stefanhaustein 8da8bb4
Naming consistency / fixes and test extensions / improvements
stefanhaustein 6a52203
Move ObjCEnum to the auto documentartion exclude list for AA, matchin…
stefanhaustein bfb6921
first part of addressing 2nd round of review comments
stefanhaustein 51cf02b
addressing review comments part 2
stefanhaustein 32645cb
Add a comment about ordering
stefanhaustein File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
16 changes: 16 additions & 0 deletions
16
libraries/stdlib/src/kotlin/experimental/ExperimentalObjCEnum.kt
This file contains hidden or 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,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 |
This file contains hidden or 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 hidden or 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 hidden or 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
10 changes: 10 additions & 0 deletions
10
native/native.tests/testData/framework/objcexport/nativeEnum.kt
This file contains hidden or 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,10 @@ | ||
| package nativeEnum | ||
|
|
||
| import kotlin.native.ObjCEnum | ||
| import kotlin.experimental.ExperimentalObjCEnum | ||
|
|
||
| @OptIn(kotlin.experimental.ExperimentalObjCEnum::class) | ||
| @ObjCEnum("OBJCFoo") | ||
| enum class MyKotlinEnum { | ||
| ALPHA, COPY, BAR_FOO | ||
| } |
21 changes: 21 additions & 0 deletions
21
native/native.tests/testData/framework/objcexport/nativeEnum.swift
This file contains hidden or 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 @@ | ||
| import Kt | ||
|
|
||
|
|
||
| private func testNativeEnumValues() throws { | ||
| let ktEnum = MyKotlinEnum.alpha | ||
| let nsEnum = ktEnum.nsEnum | ||
|
|
||
| switch(nsEnum) { | ||
| case .alpha: try assertEquals(actual: nsEnum, expected: ktEnum.nsEnum) | ||
| case .barFoo: try fail() | ||
| case .theCopy: try fail() | ||
| } | ||
| } | ||
|
|
||
| class NativeEnumTests : SimpleTestProvider { | ||
| override init() { | ||
| super.init() | ||
|
|
||
| test("TestNativeEnumValues", testNativeEnumValues) | ||
| } | ||
| } |
17 changes: 17 additions & 0 deletions
17
...erator/impl/analysis-api/src/org/jetbrains/kotlin/objcexport/ObjCExportTranslatedClass.kt
This file contains hidden or 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,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) } | ||
|
|
This file contains hidden or 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 hidden or 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
22 changes: 22 additions & 0 deletions
22
...ader-generator/impl/analysis-api/src/org/jetbrains/kotlin/objcexport/getNSEnumTypeName.kt
This file contains hidden or 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,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) | ||
| } |
This file contains hidden or 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 hidden or 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
47 changes: 47 additions & 0 deletions
47
...ader-generator/impl/analysis-api/src/org/jetbrains/kotlin/objcexport/translateToNSEnum.kt
This file contains hidden or 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,47 @@ | ||
| /* | ||
| * 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.analysis.api.symbols.KaEnumEntrySymbol | ||
| import org.jetbrains.kotlin.backend.konan.objcexport.ObjCExportNSEnumTypeName | ||
| import org.jetbrains.kotlin.backend.konan.objcexport.ObjCNSEnum | ||
| import org.jetbrains.kotlin.backend.konan.objcexport.ObjCProperty | ||
| import org.jetbrains.kotlin.backend.konan.objcexport.ObjCRawType | ||
| import org.jetbrains.kotlin.backend.konan.objcexport.ObjCTopLevel | ||
| import org.jetbrains.kotlin.backend.konan.objcexport.ObjCExportNativeEnumEntry | ||
| import org.jetbrains.kotlin.backend.konan.objcexport.ObjCExportStubOrigin | ||
|
|
||
| internal fun ObjCExportContext.translateNSEnum( | ||
| symbol: KaClassSymbol, | ||
| origin: ObjCExportStubOrigin, | ||
| nsEnumTypeName: ObjCExportNSEnumTypeName, | ||
| auxiliaryDeclarations: MutableList<ObjCTopLevel> | ||
| ): ObjCProperty { | ||
| auxiliaryDeclarations.add( | ||
| ObjCNSEnum(nsEnumTypeName.objCName, nsEnumTypeName.swiftName, origin, getNSEnumEntries(symbol, nsEnumTypeName.objCName))) | ||
| return ObjCProperty( | ||
| ObjCPropertyNames.nsEnumPropertyName, | ||
| null, | ||
| null, | ||
| ObjCRawType(nsEnumTypeName.objCName), | ||
| listOf("readonly") | ||
| ) | ||
| } | ||
|
|
||
|
|
||
| private fun ObjCExportContext.getNSEnumEntries(symbol: KaClassSymbol, objCTypeName: String): List<ObjCExportNativeEnumEntry> { | ||
| val staticMembers = with(analysisSession) { symbol.staticDeclaredMemberScope }.callables.toList() | ||
| // Map the enum entries in declaration order, preserving the ordinal | ||
| return staticMembers.filterIsInstance<KaEnumEntrySymbol>().mapIndexed { ordinal, entry -> | ||
| ObjCExportNativeEnumEntry( | ||
| getEnumEntryName(entry, true), | ||
| objCTypeName + getEnumEntryName(entry, false).replaceFirstChar { it.uppercaseChar() }, | ||
| ordinal | ||
| ) | ||
| } | ||
| } | ||
|
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.