Deprecate CVariable.Type machinery, C enum .byValue and CEnum type

This commit is contained in:
SvyatoslavScherbina
2020-09-28 15:26:51 +03:00
committed by GitHub
parent 0058928e67
commit c1f74bd0cb
14 changed files with 85 additions and 12 deletions
@@ -1,5 +1,5 @@
@file:JvmName("clang")
@file:Suppress("UNUSED_VARIABLE", "UNUSED_EXPRESSION")
@file:Suppress("UNUSED_VARIABLE", "UNUSED_EXPRESSION", "DEPRECATION")
package clang
import kotlinx.cinterop.*
@@ -439,7 +439,7 @@ internal fun indexTranslationUnit(index: CXIndex, translationUnit: CXTranslation
val indexAction = clang_IndexAction_create(index)
try {
val result = clang_indexTranslationUnit(indexAction, clientData,
indexerCallbacks.ptr, IndexerCallbacks.size.toInt(), options, translationUnit)
indexerCallbacks.ptr, sizeOf<IndexerCallbacks>().toInt(), options, translationUnit)
if (result != 0) {
throw Error("clang_indexTranslationUnit returned $result")
@@ -98,6 +98,7 @@ private fun getStructCType(structClass: KClass<*>): CType<*> = structTypeCache.c
}
}
@Suppress("DEPRECATION")
val structType = structClass.companionObjectInstance as CVariable.Type
Struct(structType.size, structType.align, fieldCTypes)
@@ -138,7 +139,7 @@ private fun getArgOrRetValCType(type: KType): CType<*> {
CPointer::class -> Pointer
// TODO: floats
CValue::class -> getStructValueCType(type)
else -> if (classifier.isSubclassOf(CEnum::class)) {
else -> if (classifier.isSubclassOf(@Suppress("DEPRECATION") CEnum::class)) {
getEnumCType(classifier)
} else {
null
@@ -357,6 +358,7 @@ private class Struct(val size: Long, val align: Int, elementTypes: List<CType<*>
override fun write(location: NativePtr, value: CValue<*>) = value.write(location)
}
@Suppress("DEPRECATION")
private class CEnumType(private val rawValueCType: CType<Any>) : CType<CEnum>(rawValueCType.ffiType) {
override fun read(location: NativePtr): CEnum {
@@ -28,10 +28,13 @@ public val nativeNullPtr: NativePtr = 0L
// TODO: the functions below should eventually be intrinsified
@Suppress("DEPRECATION")
private val typeOfCache = ConcurrentHashMap<Class<*>, CVariable.Type>()
@Deprecated("Use sizeOf<T>() or alignOf<T>() instead.")
@Suppress("NON_PUBLIC_CALL_FROM_PUBLIC_INLINE")
inline fun <reified T : CVariable> typeOf() =
@Suppress("DEPRECATION")
typeOfCache.computeIfAbsent(T::class.java) { T::class.companionObjectInstance as CVariable.Type }
/**
@@ -216,6 +216,7 @@ public abstract class CVariable(rawPtr: NativePtr) : CPointed(rawPtr) {
* @param align the alignments in bytes that is enough for this data type.
* It may be greater than actually required for simplicity.
*/
@Deprecated("Use sizeOf<T>() or alignOf<T>() instead.")
public open class Type(val size: Long, val align: Int) {
init {
@@ -225,7 +226,10 @@ public abstract class CVariable(rawPtr: NativePtr) : CPointed(rawPtr) {
}
}
@Suppress("DEPRECATION")
public inline fun <reified T : CVariable> sizeOf() = typeOf<T>().size
@Suppress("DEPRECATION")
public inline fun <reified T : CVariable> alignOf() = typeOf<T>().align
/**
@@ -243,6 +247,8 @@ public inline fun <reified T : CVariable> CStructVar.arrayMemberAt(offset: Long)
* The C struct-typed variable located in memory.
*/
public abstract class CStructVar(rawPtr: NativePtr) : CVariable(rawPtr) {
@Deprecated("Use sizeOf<T>() or alignOf<T>() instead.")
@Suppress("DEPRECATION")
open class Type(size: Long, align: Int) : CVariable.Type(size, align)
}
@@ -251,9 +257,12 @@ public abstract class CStructVar(rawPtr: NativePtr) : CVariable(rawPtr) {
*/
sealed class CPrimitiveVar(rawPtr: NativePtr) : CVariable(rawPtr) {
// aligning by size is obviously enough
@Deprecated("Use sizeOf<T>() or alignOf<T>() instead.")
@Suppress("DEPRECATION")
open class Type(size: Int) : CVariable.Type(size.toLong(), align = size)
}
@Deprecated("Will be removed.")
public interface CEnum {
public val value: Any
}
@@ -265,56 +274,78 @@ public abstract class CEnumVar(rawPtr: NativePtr) : CPrimitiveVar(rawPtr)
@Suppress("FINAL_UPPER_BOUND")
public class BooleanVarOf<T : Boolean>(rawPtr: NativePtr) : CPrimitiveVar(rawPtr) {
@Deprecated("Use sizeOf<T>() or alignOf<T>() instead.")
@Suppress("DEPRECATION")
companion object : Type(1)
}
@Suppress("FINAL_UPPER_BOUND")
public class ByteVarOf<T : Byte>(rawPtr: NativePtr) : CPrimitiveVar(rawPtr) {
@Deprecated("Use sizeOf<T>() or alignOf<T>() instead.")
@Suppress("DEPRECATION")
companion object : Type(1)
}
@Suppress("FINAL_UPPER_BOUND")
public class ShortVarOf<T : Short>(rawPtr: NativePtr) : CPrimitiveVar(rawPtr) {
@Deprecated("Use sizeOf<T>() or alignOf<T>() instead.")
@Suppress("DEPRECATION")
companion object : Type(2)
}
@Suppress("FINAL_UPPER_BOUND")
public class IntVarOf<T : Int>(rawPtr: NativePtr) : CPrimitiveVar(rawPtr) {
@Deprecated("Use sizeOf<T>() or alignOf<T>() instead.")
@Suppress("DEPRECATION")
companion object : Type(4)
}
@Suppress("FINAL_UPPER_BOUND")
public class LongVarOf<T : Long>(rawPtr: NativePtr) : CPrimitiveVar(rawPtr) {
@Deprecated("Use sizeOf<T>() or alignOf<T>() instead.")
@Suppress("DEPRECATION")
companion object : Type(8)
}
@Suppress("FINAL_UPPER_BOUND")
public class UByteVarOf<T : UByte>(rawPtr: NativePtr) : CPrimitiveVar(rawPtr) {
@Deprecated("Use sizeOf<T>() or alignOf<T>() instead.")
@Suppress("DEPRECATION")
companion object : Type(1)
}
@Suppress("FINAL_UPPER_BOUND")
public class UShortVarOf<T : UShort>(rawPtr: NativePtr) : CPrimitiveVar(rawPtr) {
@Deprecated("Use sizeOf<T>() or alignOf<T>() instead.")
@Suppress("DEPRECATION")
companion object : Type(2)
}
@Suppress("FINAL_UPPER_BOUND")
public class UIntVarOf<T : UInt>(rawPtr: NativePtr) : CPrimitiveVar(rawPtr) {
@Deprecated("Use sizeOf<T>() or alignOf<T>() instead.")
@Suppress("DEPRECATION")
companion object : Type(4)
}
@Suppress("FINAL_UPPER_BOUND")
public class ULongVarOf<T : ULong>(rawPtr: NativePtr) : CPrimitiveVar(rawPtr) {
@Deprecated("Use sizeOf<T>() or alignOf<T>() instead.")
@Suppress("DEPRECATION")
companion object : Type(8)
}
@Suppress("FINAL_UPPER_BOUND")
public class FloatVarOf<T : Float>(rawPtr: NativePtr) : CPrimitiveVar(rawPtr) {
@Deprecated("Use sizeOf<T>() or alignOf<T>() instead.")
@Suppress("DEPRECATION")
companion object : Type(4)
}
@Suppress("FINAL_UPPER_BOUND")
public class DoubleVarOf<T : Double>(rawPtr: NativePtr) : CPrimitiveVar(rawPtr) {
@Deprecated("Use sizeOf<T>() or alignOf<T>() instead.")
@Suppress("DEPRECATION")
companion object : Type(8)
}
@@ -398,6 +429,8 @@ public var <T : Double> DoubleVarOf<T>.value: T
public class CPointerVarOf<T : CPointer<*>>(rawPtr: NativePtr) : CVariable(rawPtr) {
@Deprecated("Use sizeOf<T>() or alignOf<T>() instead.")
@Suppress("DEPRECATION")
companion object : CVariable.Type(pointerSize.toLong(), pointerSize)
}
@@ -107,9 +107,11 @@ public class Arena(parent: NativeFreeablePlacement = nativeHeap) : ArenaBase(par
* @param T must not be abstract
*/
public inline fun <reified T : CVariable> NativePlacement.alloc(): T =
@Suppress("DEPRECATION")
alloc(typeOf<T>()).reinterpret()
@PublishedApi
@Suppress("DEPRECATION")
internal fun NativePlacement.alloc(type: CVariable.Type): NativePointed =
alloc(type.size, type.align)
@@ -284,11 +286,13 @@ public fun <T : CVariable> CPointed.readValue(size: Long, align: Int): CValue<T>
}
}
@Suppress("DEPRECATION")
@PublishedApi internal fun <T : CVariable> CPointed.readValue(type: CVariable.Type): CValue<T> =
readValue(type.size, type.align)
// Note: can't be declared as property due to possible clash with a struct field.
// TODO: find better name.
@Suppress("DEPRECATION")
public inline fun <reified T : CStructVar> T.readValue(): CValue<T> = this.readValue(typeOf<T>())
public fun <T: CVariable> CValue<T>.write(location: NativePtr) {
@@ -32,6 +32,8 @@ internal inline fun NativePtr.toNonNull() = this.reinterpret<NativePtr, NonNullN
inline val nativeNullPtr: NativePtr
get() = getNativeNullPtr()
@Deprecated("Use sizeOf<T>() or alignOf<T>() instead.")
@Suppress("DEPRECATION")
fun <T : CVariable> typeOf(): CVariable.Type = throw Error("typeOf() is called with erased argument")
/**
@@ -57,6 +59,8 @@ external fun CPointer<*>.getRawValue(): NativePtr
internal fun CPointer<*>.cPointerToString() = "CPointer(raw=$rawValue)"
public class Vector128VarOf<T : Vector128>(rawPtr: NativePtr) : CVariable(rawPtr) {
@Deprecated("Use sizeOf<T>() or alignOf<T>() instead.")
@Suppress("DEPRECATION")
companion object : Type(size = 16, align = 16)
}
@@ -85,10 +85,14 @@ inline fun <reified T : Any> unwrapKotlinObjectHolder(holder: Any?): T {
external internal fun unwrapKotlinObjectHolderImpl(ptr: NativePtr): Any
class ObjCObjectVar<T>(rawPtr: NativePtr) : CVariable(rawPtr) {
@Deprecated("Use sizeOf<T>() or alignOf<T>() instead.")
@Suppress("DEPRECATION")
companion object : CVariable.Type(pointerSize.toLong(), pointerSize)
}
class ObjCNotImplementedVar<T : Any?>(rawPtr: NativePtr) : CVariable(rawPtr) {
@Deprecated("Use sizeOf<T>() or alignOf<T>() instead.")
@Suppress("DEPRECATION")
companion object : CVariable.Type(pointerSize.toLong(), pointerSize)
}
@@ -240,7 +240,7 @@ private fun deprecatedInit(className: String, initParameterNames: List<String>,
val replacement = if (factory) "$className.create" else className
val replacementKind = if (factory) "factory method" else "constructor"
val replaceWith = "$replacement(${initParameterNames.joinToString { it.asSimpleName() }})"
return AnnotationStub.Deprecated("Use $replacementKind instead", replaceWith)
return AnnotationStub.Deprecated("Use $replacementKind instead", replaceWith, DeprecationLevel.ERROR)
}
internal val ObjCMethod.kotlinName: String
@@ -215,10 +215,26 @@ sealed class AnnotationStub(val classifier: Classifier) {
class CLength(val length: Long) :
AnnotationStub(Classifier.topLevel(cinteropPackage, "CLength"))
class Deprecated(val message: String, val replaceWith: String) :
class Deprecated(val message: String, val replaceWith: String, val level: DeprecationLevel) :
AnnotationStub(Classifier.topLevel("kotlin", "Deprecated")) {
companion object {
val unableToImport = Deprecated("Unable to import this declaration", "")
val unableToImport = Deprecated(
"Unable to import this declaration",
"",
DeprecationLevel.ERROR
)
val deprecatedCVariableCompanion = Deprecated(
"Use sizeOf<T>() or alignOf<T>() instead.",
"",
DeprecationLevel.WARNING
)
val deprecatedCEnumByValue = Deprecated(
"Will be removed.",
"",
DeprecationLevel.WARNING
)
}
}
@@ -179,7 +179,11 @@ internal class StructStubBuilder(
val annotation = AnnotationStub.CStruct.VarType(def.size, def.align).takeIf {
context.generationMode == GenerationMode.METADATA
}
val companion = ClassStub.Companion(companionClassifier, superClassInit = companionSuperInit, annotations = listOfNotNull(annotation))
val companion = ClassStub.Companion(
companionClassifier,
superClassInit = companionSuperInit,
annotations = listOfNotNull(annotation, AnnotationStub.Deprecated.deprecatedCVariableCompanion)
)
return listOf(ClassStub.Simple(
classifier,
@@ -301,7 +305,7 @@ internal class EnumStubBuilder(
origin = StubOrigin.Synthetic.EnumByValue(enumDef),
receiver = null,
modality = MemberStubModality.FINAL,
annotations = emptyList()
annotations = listOf(AnnotationStub.Deprecated.deprecatedCEnumByValue)
)
val companion = ClassStub.Companion(
@@ -366,7 +370,7 @@ internal class EnumStubBuilder(
val companion = ClassStub.Companion(
classifier = enumVarClassifier.nested("Companion"),
superClassInit = SuperClassInit(companionSuper, listOf(typeSize)),
annotations = listOfNotNull(varSizeAnnotation)
annotations = listOfNotNull(varSizeAnnotation, AnnotationStub.Deprecated.deprecatedCVariableCompanion)
)
val valueProperty = PropertyStub(
name = "value",
@@ -418,7 +418,7 @@ private class MappingExtensions(
is AnnotationStub.Deprecated -> mapOfNotNull(
("message" to message).asAnnotationArgument(),
("replaceWith" to replaceWith(replaceWith)),
("level" to deprecationLevel(DeprecationLevel.ERROR))
("level" to deprecationLevel(level))
)
is AnnotationStub.CEnumEntryAlias -> mapOfNotNull(
("entryName" to entryName).asAnnotationArgument()
@@ -94,6 +94,7 @@ class StubIrTextEmitter(
}
val suppress = mutableListOf("UNUSED_VARIABLE", "UNUSED_EXPRESSION").apply {
add("DEPRECATION") // CVariable.Type and CEnum companion deprecations.
if (context.configuration.library.language == Language.OBJECTIVE_C) {
add("CONFLICTING_OVERLOADS")
add("RETURN_TYPE_MISMATCH_ON_INHERITANCE")
@@ -272,7 +273,8 @@ class StubIrTextEmitter(
val typeMirror = builderResult.bridgeGenerationComponents.enumToTypeMirror.getValue(enum)
val basePointedTypeName = typeMirror.pointedType.render(kotlinFile)
block("class Var(rawPtr: NativePtr) : CEnumVar(rawPtr)") {
out("companion object : Type($basePointedTypeName.size.toInt())")
out("@Deprecated(\"Use sizeOf<T>() or alignOf<T>() instead.\")")
out("companion object : Type(sizeOf<$basePointedTypeName>().toInt())")
out("var value: $simpleKotlinName")
out(" get() = byValue(this.reinterpret<$basePointedTypeName>().value)")
out(" set(value) { this.reinterpret<$basePointedTypeName>().value = value.value }")
@@ -491,7 +493,7 @@ class StubIrTextEmitter(
is AnnotationStub.Deprecated ->
"@Deprecated(${annotationStub.message.quoteAsKotlinLiteral()}, " +
"ReplaceWith(${annotationStub.replaceWith.quoteAsKotlinLiteral()}), " +
"DeprecationLevel.ERROR)"
"DeprecationLevel.${annotationStub.level.name})"
is AnnotationStub.CEnumEntryAlias,
is AnnotationStub.CEnumVarTypeSize,
is AnnotationStub.CStruct.MemberAt,
@@ -220,6 +220,7 @@ internal interface ContextUtils : RuntimeAware {
* It must be declared identically with [Runtime.globalHashType].
*/
fun GlobalHash.getBytes(): ByteArray {
@Suppress("DEPRECATION")
val size = GlobalHash.size
assert(size == LLVMStoreSizeOfType(llvmTargetData, runtime.globalHashType))