[K/N] Support kType generation as ConstantValue
This commit is contained in:
@@ -0,0 +1,15 @@
|
||||
/*
|
||||
* Copyright 2010-2021 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.native.internal
|
||||
|
||||
class ArrayAsList<T>(val array: Array<T>) : AbstractList<T>(), RandomAccess {
|
||||
override val size: Int get() = array.size
|
||||
override fun isEmpty(): Boolean = array.isEmpty()
|
||||
override fun contains(element: T): Boolean = array.contains(element)
|
||||
override fun get(index: Int): T = array[index]
|
||||
override fun indexOf(element: T): Int = array.indexOf(element)
|
||||
override fun lastIndexOf(element: T): Int = array.lastIndexOf(element)
|
||||
}
|
||||
@@ -46,7 +46,6 @@ class IntrinsicType {
|
||||
const val OBJC_GET_SELECTOR = "OBJC_GET_SELECTOR"
|
||||
|
||||
// Other
|
||||
const val GET_CLASS_TYPE_INFO = "GET_CLASS_TYPE_INFO"
|
||||
const val INTEROP_READ_BITS = "INTEROP_READ_BITS"
|
||||
const val INTEROP_WRITE_BITS = "INTEROP_WRITE_BITS"
|
||||
const val CREATE_UNINITIALIZED_INSTANCE = "CREATE_UNINITIALIZED_INSTANCE"
|
||||
|
||||
@@ -78,10 +78,6 @@ private external fun findAssociatedObjectImpl(typeInfo: NativePtr, key: NativePt
|
||||
@GCUnsafeCall("Kotlin_Any_getTypeInfo")
|
||||
internal external fun getObjectTypeInfo(obj: Any): NativePtr
|
||||
|
||||
@ExportForCompiler
|
||||
@TypedIntrinsic(IntrinsicType.GET_CLASS_TYPE_INFO)
|
||||
internal external inline fun <reified T : Any> getClassTypeInfo(): NativePtr
|
||||
|
||||
@GCUnsafeCall("Kotlin_TypeInfo_getPackageName")
|
||||
private external fun getPackageName(typeInfo: NativePtr, checkFlags: Boolean): String?
|
||||
|
||||
|
||||
@@ -7,13 +7,60 @@ package kotlin.native.internal
|
||||
|
||||
import kotlin.reflect.*
|
||||
|
||||
internal class KTypeImpl(
|
||||
internal object KVarianceMapper {
|
||||
// this constants are copypasted to ReflectionSupport.kt
|
||||
const val VARIANCE_STAR = -1
|
||||
const val VARIANCE_INVARIANT = 0
|
||||
const val VARIANCE_IN = 1
|
||||
const val VARIANCE_OUT = 2
|
||||
|
||||
fun idByVariance(variance: KVariance) = when (variance) {
|
||||
KVariance.INVARIANT -> VARIANCE_INVARIANT
|
||||
KVariance.IN -> VARIANCE_IN
|
||||
KVariance.OUT -> VARIANCE_OUT
|
||||
}
|
||||
|
||||
fun varianceById(id: Int) = when (id) {
|
||||
VARIANCE_STAR -> null
|
||||
VARIANCE_INVARIANT -> KVariance.INVARIANT
|
||||
VARIANCE_IN -> KVariance.IN
|
||||
VARIANCE_OUT -> KVariance.OUT
|
||||
else -> throw IllegalStateException("Unknown variance id ${id}")
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* This class is used to avoid having enum inside KType class
|
||||
* Static initialization for enum objects is not supported yet,
|
||||
* so to initialize KType statically we need to avoid them.
|
||||
*
|
||||
* When this issue is resolved, this class can be replaced with just ArrayList
|
||||
*/
|
||||
internal class KTypeProjectionList(val variance: IntArray, val type: Array<KType?>) : AbstractList<KTypeProjection>() {
|
||||
override val size
|
||||
get() = variance.size
|
||||
|
||||
|
||||
override fun get(index: Int) : KTypeProjection {
|
||||
AbstractList.checkElementIndex(index, size)
|
||||
val kVariance = KVarianceMapper.varianceById(variance[index]) ?: return KTypeProjection.STAR
|
||||
return KTypeProjection(kVariance, type[index])
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
internal class KTypeImpl<T>(
|
||||
override val classifier: KClassifier?,
|
||||
override val arguments: List<KTypeProjection>,
|
||||
override val isMarkedNullable: Boolean
|
||||
) : KType {
|
||||
|
||||
@ExportForCompiler
|
||||
@ConstantConstructorIntrinsic("KTYPE_IMPL")
|
||||
constructor() : this(null, TODO("This is intrinsic constructor and it shouldn't be used directly"), false)
|
||||
|
||||
override fun equals(other: Any?) =
|
||||
other is KTypeImpl &&
|
||||
other is KTypeImpl<*> &&
|
||||
this.classifier == other.classifier &&
|
||||
this.arguments == other.arguments &&
|
||||
this.isMarkedNullable == other.isMarkedNullable
|
||||
|
||||
@@ -11,9 +11,15 @@ internal class KTypeParameterImpl(
|
||||
override val name: String,
|
||||
private val containerFqName: String,
|
||||
override val upperBounds: List<KType>,
|
||||
override val variance: KVariance,
|
||||
val varianceId: Int, // mapping is used to make static initialization possible
|
||||
override val isReified: Boolean
|
||||
) : KTypeParameter {
|
||||
override val variance: KVariance
|
||||
get() = KVarianceMapper.varianceById(varianceId)!!
|
||||
|
||||
constructor(name: String, containerFqName: String, upperBounds: List<KType>, variance: KVariance, isReified: Boolean) :
|
||||
this(name, containerFqName, upperBounds, KVarianceMapper.idByVariance(variance), isReified)
|
||||
|
||||
override fun toString(): String = when (variance) {
|
||||
KVariance.INVARIANT -> ""
|
||||
KVariance.IN -> "in "
|
||||
|
||||
Reference in New Issue
Block a user