From fcd3fe4fe3680db4146ba82ea221c6ed931ec589 Mon Sep 17 00:00:00 2001 From: Ivan Kylchik Date: Thu, 2 Feb 2023 12:19:13 +0100 Subject: [PATCH] [Native] Mark necessary methods that must be evaluated at compile-time It includes methods and properties from Boolean, Char, String, Enum and KCallable. --- .../runtime/src/main/kotlin/kotlin/Boolean.kt | 8 ++++++++ .../runtime/src/main/kotlin/kotlin/Char.kt | 14 ++++++++++++++ .../runtime/src/main/kotlin/kotlin/Enum.kt | 2 +- .../runtime/src/main/kotlin/kotlin/String.kt | 6 ++++++ .../src/main/kotlin/kotlin/reflect/KCallable.kt | 1 + 5 files changed, 30 insertions(+), 1 deletion(-) diff --git a/kotlin-native/runtime/src/main/kotlin/kotlin/Boolean.kt b/kotlin-native/runtime/src/main/kotlin/kotlin/Boolean.kt index 5ec2f74b617..c395f9fee39 100644 --- a/kotlin-native/runtime/src/main/kotlin/kotlin/Boolean.kt +++ b/kotlin-native/runtime/src/main/kotlin/kotlin/Boolean.kt @@ -21,6 +21,7 @@ public class Boolean private constructor() : Comparable { * Returns the inverse of this boolean. */ @TypedIntrinsic(IntrinsicType.NOT) + @kotlin.internal.IntrinsicConstEvaluation external public operator fun not(): Boolean /** @@ -28,6 +29,7 @@ public class Boolean private constructor() : Comparable { * this function does not perform short-circuit evaluation. Both `this` and [other] will always be evaluated. */ @TypedIntrinsic(IntrinsicType.AND) + @kotlin.internal.IntrinsicConstEvaluation external public infix fun and(other: Boolean): Boolean /** @@ -35,22 +37,28 @@ public class Boolean private constructor() : Comparable { * this function does not perform short-circuit evaluation. Both `this` and [other] will always be evaluated. */ @TypedIntrinsic(IntrinsicType.OR) + @kotlin.internal.IntrinsicConstEvaluation external public infix fun or(other: Boolean): Boolean /** * Performs a logical `xor` operation between this Boolean and the [other] one. */ @TypedIntrinsic(IntrinsicType.XOR) + @kotlin.internal.IntrinsicConstEvaluation external public infix fun xor(other: Boolean): Boolean @TypedIntrinsic(IntrinsicType.UNSIGNED_COMPARE_TO) + @kotlin.internal.IntrinsicConstEvaluation external public override fun compareTo(other: Boolean): Int + @kotlin.internal.IntrinsicConstEvaluation public fun equals(other: Boolean): Boolean = kotlin.native.internal.areEqualByValue(this, other) + @kotlin.internal.IntrinsicConstEvaluation public override fun equals(other: Any?): Boolean = other is Boolean && kotlin.native.internal.areEqualByValue(this, other) + @kotlin.internal.IntrinsicConstEvaluation public override fun toString() = if (this) "true" else "false" public override fun hashCode() = if (this) 1 else 0 diff --git a/kotlin-native/runtime/src/main/kotlin/kotlin/Char.kt b/kotlin-native/runtime/src/main/kotlin/kotlin/Char.kt index 6b2f7a44df9..62a7a8c0bd0 100644 --- a/kotlin-native/runtime/src/main/kotlin/kotlin/Char.kt +++ b/kotlin-native/runtime/src/main/kotlin/kotlin/Char.kt @@ -22,15 +22,19 @@ public class Char private constructor() : Comparable { * or a positive number if it's greater than other. */ @TypedIntrinsic(IntrinsicType.UNSIGNED_COMPARE_TO) + @kotlin.internal.IntrinsicConstEvaluation external public override fun compareTo(other: Char): Int /** Adds the other Int value to this value resulting a Char. */ + @kotlin.internal.IntrinsicConstEvaluation public inline operator fun plus(other: Int): Char = (this.code + other).toChar() /** Subtracts the other Char value from this value resulting an Int. */ + @kotlin.internal.IntrinsicConstEvaluation public inline operator fun minus(other: Char): Int = this.code - other.code /** Subtracts the other Int value from this value resulting a Char. */ + @kotlin.internal.IntrinsicConstEvaluation public inline operator fun minus(other: Int): Char = (this.code - other).toChar() @@ -67,33 +71,40 @@ public class Char private constructor() : Comparable { @Deprecated("Conversion of Char to Number is deprecated. Use Char.code property instead.", ReplaceWith("this.code.toByte()")) @DeprecatedSinceKotlin(warningSince = "1.5") @TypedIntrinsic(IntrinsicType.INT_TRUNCATE) + @kotlin.internal.IntrinsicConstEvaluation external public fun toByte(): Byte /** Returns the value of this character as a `Char`. */ + @kotlin.internal.IntrinsicConstEvaluation public inline fun toChar(): Char = this /** Returns the value of this character as a `Short`. */ @Deprecated("Conversion of Char to Number is deprecated. Use Char.code property instead.", ReplaceWith("this.code.toShort()")) @DeprecatedSinceKotlin(warningSince = "1.5") @TypedIntrinsic(IntrinsicType.ZERO_EXTEND) + @kotlin.internal.IntrinsicConstEvaluation external public fun toShort(): Short /** Returns the value of this character as a `Int`. */ @Deprecated("Conversion of Char to Number is deprecated. Use Char.code property instead.", ReplaceWith("this.code")) @DeprecatedSinceKotlin(warningSince = "1.5") @TypedIntrinsic(IntrinsicType.ZERO_EXTEND) + @kotlin.internal.IntrinsicConstEvaluation external public fun toInt(): Int /** Returns the value of this character as a `Long`. */ @Deprecated("Conversion of Char to Number is deprecated. Use Char.code property instead.", ReplaceWith("this.code.toLong()")) @DeprecatedSinceKotlin(warningSince = "1.5") @TypedIntrinsic(IntrinsicType.ZERO_EXTEND) + @kotlin.internal.IntrinsicConstEvaluation external public fun toLong(): Long /** Returns the value of this character as a `Float`. */ @Deprecated("Conversion of Char to Number is deprecated. Use Char.code property instead.", ReplaceWith("this.code.toFloat()")) @DeprecatedSinceKotlin(warningSince = "1.5") @TypedIntrinsic(IntrinsicType.UNSIGNED_TO_FLOAT) + @kotlin.internal.IntrinsicConstEvaluation external public fun toFloat(): Float /** Returns the value of this character as a `Double`. */ @Deprecated("Conversion of Char to Number is deprecated. Use Char.code property instead.", ReplaceWith("this.code.toDouble()")) @DeprecatedSinceKotlin(warningSince = "1.5") @TypedIntrinsic(IntrinsicType.UNSIGNED_TO_FLOAT) + @kotlin.internal.IntrinsicConstEvaluation external public fun toDouble(): Double @kotlin.native.internal.CanBePrecreated @@ -193,12 +204,15 @@ public class Char private constructor() : Comparable { public const val MAX_RADIX: Int = 36 } + @kotlin.internal.IntrinsicConstEvaluation public fun equals(other: Char): Boolean = this == other + @kotlin.internal.IntrinsicConstEvaluation public override fun equals(other: Any?): Boolean = other is Char && this.equals(other) @GCUnsafeCall("Kotlin_Char_toString") + @kotlin.internal.IntrinsicConstEvaluation external public override fun toString(): String public override fun hashCode(): Int { diff --git a/kotlin-native/runtime/src/main/kotlin/kotlin/Enum.kt b/kotlin-native/runtime/src/main/kotlin/kotlin/Enum.kt index 406e525e89b..10ab5ffdde7 100644 --- a/kotlin-native/runtime/src/main/kotlin/kotlin/Enum.kt +++ b/kotlin-native/runtime/src/main/kotlin/kotlin/Enum.kt @@ -13,7 +13,7 @@ import kotlin.native.internal.enumValuesIntrinsic * See the [Kotlin language documentation](https://kotlinlang.org/docs/reference/enum-classes.html) for more * information on enum classes. */ -public abstract class Enum>(public val name: String, public val ordinal: Int): Comparable { +public abstract class Enum>(@kotlin.internal.IntrinsicConstEvaluation public val name: String, public val ordinal: Int): Comparable { public companion object { } diff --git a/kotlin-native/runtime/src/main/kotlin/kotlin/String.kt b/kotlin-native/runtime/src/main/kotlin/kotlin/String.kt index 742e17902bd..81591d3f3a1 100644 --- a/kotlin-native/runtime/src/main/kotlin/kotlin/String.kt +++ b/kotlin-native/runtime/src/main/kotlin/kotlin/String.kt @@ -21,15 +21,18 @@ public final class String : Comparable, CharSequence { @GCUnsafeCall("Kotlin_String_hashCode") external public override fun hashCode(): Int + @kotlin.internal.IntrinsicConstEvaluation public operator fun plus(other: Any?): String { return plusImpl(other.toString()) } @TypedIntrinsic(IntrinsicType.IDENTITY) + @kotlin.internal.IntrinsicConstEvaluation override public fun toString(): String { return this } + @kotlin.internal.IntrinsicConstEvaluation public override val length: Int get() = getStringLength() @@ -39,12 +42,14 @@ public final class String : Comparable, CharSequence { * If the [index] is out of bounds of this string, throws an [IndexOutOfBoundsException]. */ @GCUnsafeCall("Kotlin_String_get") + @kotlin.internal.IntrinsicConstEvaluation public external override fun get(index: Int): Char @GCUnsafeCall("Kotlin_String_subSequence") public external override fun subSequence(startIndex: Int, endIndex: Int): CharSequence @GCUnsafeCall("Kotlin_String_compareTo") + @kotlin.internal.IntrinsicConstEvaluation public external override fun compareTo(other: String): Int @GCUnsafeCall("Kotlin_String_getStringLength") @@ -55,6 +60,7 @@ public final class String : Comparable, CharSequence { internal external fun plusImpl(other: String): String @GCUnsafeCall("Kotlin_String_equals") + @kotlin.internal.IntrinsicConstEvaluation external override fun equals(other: Any?): Boolean } diff --git a/kotlin-native/runtime/src/main/kotlin/kotlin/reflect/KCallable.kt b/kotlin-native/runtime/src/main/kotlin/kotlin/reflect/KCallable.kt index 446e2d889c5..624013db688 100644 --- a/kotlin-native/runtime/src/main/kotlin/kotlin/reflect/KCallable.kt +++ b/kotlin-native/runtime/src/main/kotlin/kotlin/reflect/KCallable.kt @@ -19,6 +19,7 @@ public actual interface KCallable : KAnnotatedElement { * - property accessors: the getter for a property named "foo" will have the name "", * the setter, similarly, will have the name "". */ + @kotlin.internal.IntrinsicConstEvaluation public actual val name: String /**