[Wasm] Support enumValues and enumValueOf functions

^KT-57276 Fixed

Merge-request: KT-MR-9169
Merged-by: Svyatoslav Kuzmich <svyatoslav.kuzmich@jetbrains.com>
This commit is contained in:
Svyatoslav Kuzmich
2023-03-13 14:13:31 +00:00
committed by Space Team
parent bcf7c38288
commit a99ea1d3ce
22 changed files with 37 additions and 26 deletions
@@ -19,7 +19,7 @@ import org.jetbrains.kotlin.ir.util.isEnumClass
import org.jetbrains.kotlin.name.Name
class EnumIntrinsicsTransformer(private val context: JsIrBackendContext) : CallsTransformer {
object EnumIntrinsicsUtils {
private fun transformEnumTopLevelIntrinsic(
call: IrFunctionAccessExpression,
staticMethodPredicate: (IrSimpleFunction) -> Boolean
@@ -36,19 +36,21 @@ class EnumIntrinsicsTransformer(private val context: JsIrBackendContext) : Calls
return irCall(call, staticMethod.symbol)
}
private fun transformEnumValueOfIntrinsic(call: IrFunctionAccessExpression) = transformEnumTopLevelIntrinsic(call) {
fun transformEnumValueOfIntrinsic(call: IrFunctionAccessExpression) = transformEnumTopLevelIntrinsic(call) {
it.name == Name.identifier("valueOf") &&
it.valueParameters.count() == 1 &&
it.valueParameters[0].type.isString()
}
private fun transformEnumValuesIntrinsic(call: IrFunctionAccessExpression) = transformEnumTopLevelIntrinsic(call) {
fun transformEnumValuesIntrinsic(call: IrFunctionAccessExpression) = transformEnumTopLevelIntrinsic(call) {
it.name == Name.identifier("values") && it.valueParameters.count() == 0
}
}
class EnumIntrinsicsTransformer(private val context: JsIrBackendContext) : CallsTransformer {
override fun transformFunctionAccess(call: IrFunctionAccessExpression, doNotIntrinsify: Boolean) = when (call.symbol) {
context.intrinsics.enumValueOfIntrinsic -> transformEnumValueOfIntrinsic(call)
context.intrinsics.enumValuesIntrinsic -> transformEnumValuesIntrinsic(call)
context.intrinsics.enumValueOfIntrinsic -> EnumIntrinsicsUtils.transformEnumValueOfIntrinsic(call)
context.intrinsics.enumValuesIntrinsic -> EnumIntrinsicsUtils.transformEnumValuesIntrinsic(call)
else -> call
}
}
@@ -111,6 +111,9 @@ class WasmSymbols(
.find { it.valueParameters.firstOrNull()?.type?.isFunctionType == true }
.let { symbolTable.referenceSimpleFunction(it!!) }
val enumValueOfIntrinsic = getInternalFunction("enumValueOfIntrinsic")
val enumValuesIntrinsic = getInternalFunction("enumValuesIntrinsic")
val coroutineEmptyContinuation: IrPropertySymbol = symbolTable.referenceProperty(
getProperty(FqName.fromSegments(listOf("kotlin", "wasm", "internal", "EmptyContinuation")))
)
@@ -12,6 +12,7 @@ import org.jetbrains.kotlin.backend.common.lower.createIrBuilder
import org.jetbrains.kotlin.backend.wasm.WasmBackendContext
import org.jetbrains.kotlin.config.AnalysisFlags
import org.jetbrains.kotlin.config.languageVersionSettings
import org.jetbrains.kotlin.ir.backend.js.lower.calls.EnumIntrinsicsUtils
import org.jetbrains.kotlin.ir.backend.js.utils.erasedUpperBound
import org.jetbrains.kotlin.ir.backend.js.utils.isEqualsInheritedFromAny
import org.jetbrains.kotlin.ir.builders.*
@@ -172,6 +173,10 @@ class BuiltInsLowering(val context: WasmBackendContext) : FileLoweringPass {
}
}
}
symbols.enumValueOfIntrinsic ->
return EnumIntrinsicsUtils.transformEnumValueOfIntrinsic(call)
symbols.enumValuesIntrinsic ->
return EnumIntrinsicsUtils.transformEnumValuesIntrinsic(call)
}
return call
-1
View File
@@ -1,4 +1,3 @@
// IGNORE_BACKEND: WASM
enum class E { OK }
fun <T> id(x: T) = x
@@ -1,5 +1,4 @@
// IGNORE_BACKEND: JS
// IGNORE_BACKEND: WASM
var l = ""
-1
View File
@@ -1,4 +1,3 @@
// IGNORE_BACKEND: WASM
// WITH_STDLIB
// FILE: 1.kt
package test
-1
View File
@@ -1,4 +1,3 @@
// IGNORE_BACKEND: WASM
// WITH_STDLIB
// NO_CHECK_LAMBDA_INLINING
// FILE: 1.kt
-1
View File
@@ -1,4 +1,3 @@
// IGNORE_BACKEND: WASM
// WITH_STDLIB
// FILE: 1.kt
package test
@@ -1,4 +1,3 @@
// IGNORE_BACKEND: WASM
// WITH_STDLIB
// FILE: 1.kt
package test
@@ -1,4 +1,3 @@
// IGNORE_BACKEND: WASM
// WITH_STDLIB
// FILE: 1.kt
package test
@@ -1,4 +1,3 @@
// IGNORE_BACKEND: WASM
// WITH_STDLIB
// FILE: 1.kt
package test
@@ -1,4 +1,3 @@
// IGNORE_BACKEND: WASM
// WITH_STDLIB
// FILE: 1.kt
package test
-1
View File
@@ -1,4 +1,3 @@
// IGNORE_BACKEND: WASM
// WITH_STDLIB
// KJS_WITH_FULL_RUNTIME
// FILE: 1.kt
@@ -1,4 +1,3 @@
// IGNORE_BACKEND: WASM
// WITH_STDLIB
// KJS_WITH_FULL_RUNTIME
// FILE: 1.kt
@@ -1,4 +1,3 @@
// IGNORE_BACKEND: WASM
// WITH_STDLIB
// KJS_WITH_FULL_RUNTIME
// FILE: 1.kt
@@ -1,4 +1,3 @@
// IGNORE_BACKEND: WASM
// WITH_STDLIB
// KJS_WITH_FULL_RUNTIME
// FILE: 1.kt
@@ -1,4 +1,3 @@
// IGNORE_BACKEND: WASM
// WITH_STDLIB
// KJS_WITH_FULL_RUNTIME
// FILE: 1.kt
@@ -1,4 +1,3 @@
// IGNORE_BACKEND: WASM
// WITH_STDLIB
// KJS_WITH_FULL_RUNTIME
// FILE: 1.kt
@@ -1,4 +1,3 @@
// IGNORE_BACKEND: WASM
// JVM_TARGET: 1.8
// WITH_STDLIB
// FILE: 1.kt
@@ -1,4 +1,3 @@
// IGNORE_BACKEND: WASM
// JVM_TARGET: 1.8
// WITH_STDLIB
// FILE: 1.kt
@@ -8,6 +8,8 @@
package kotlin
import kotlin.internal.PureReifiable
import kotlin.wasm.internal.enumValueOfIntrinsic
import kotlin.wasm.internal.enumValuesIntrinsic
public inline fun <T> emptyArray(): Array<T> = arrayOf()
@@ -80,12 +82,12 @@ public inline fun booleanArrayOf(vararg elements: Boolean): BooleanArray = eleme
* Returns an array containing enum T entries.
*/
@SinceKotlin("1.1")
@Suppress("NON_MEMBER_FUNCTION_NO_BODY")
public inline fun <reified T : Enum<T>> enumValues(): Array<T>
public inline fun <reified T : Enum<T>> enumValues(): Array<T> =
enumValuesIntrinsic()
/**
* Returns an enum entry with specified name.
*/
@SinceKotlin("1.1")
@Suppress("NON_MEMBER_FUNCTION_NO_BODY", "UNUSED_PARAMETER")
public inline fun <reified T : Enum<T>> enumValueOf(name: String): T
public inline fun <reified T : Enum<T>> enumValueOf(name: String): T =
enumValueOfIntrinsic(name)
@@ -0,0 +1,16 @@
/*
* Copyright 2010-2023 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.wasm.internal
@PublishedApi
@ExcludedFromCodegen
internal fun <T : Enum<T>> enumValuesIntrinsic(): Array<T> =
implementedAsIntrinsic
@PublishedApi
@ExcludedFromCodegen
internal fun <T : Enum<T>> enumValueOfIntrinsic(@Suppress("UNUSED_PARAMETER") name: String): T =
implementedAsIntrinsic