diff --git a/Interop/Indexer/prebuilt/nativeInteropStubs/kotlin/clang/clang.kt b/Interop/Indexer/prebuilt/nativeInteropStubs/kotlin/clang/clang.kt index a7ef69c73a5..f98d9bf03fa 100644 --- a/Interop/Indexer/prebuilt/nativeInteropStubs/kotlin/clang/clang.kt +++ b/Interop/Indexer/prebuilt/nativeInteropStubs/kotlin/clang/clang.kt @@ -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.* diff --git a/Interop/Indexer/src/main/kotlin/org/jetbrains/kotlin/native/interop/indexer/Utils.kt b/Interop/Indexer/src/main/kotlin/org/jetbrains/kotlin/native/interop/indexer/Utils.kt index f51bab67c61..371bc3c6b63 100644 --- a/Interop/Indexer/src/main/kotlin/org/jetbrains/kotlin/native/interop/indexer/Utils.kt +++ b/Interop/Indexer/src/main/kotlin/org/jetbrains/kotlin/native/interop/indexer/Utils.kt @@ -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().toInt(), options, translationUnit) if (result != 0) { throw Error("clang_indexTranslationUnit returned $result") diff --git a/Interop/Runtime/src/jvm/kotlin/kotlinx/cinterop/JvmCallbacks.kt b/Interop/Runtime/src/jvm/kotlin/kotlinx/cinterop/JvmCallbacks.kt index 9dcb2397f3e..fd01e1fedee 100644 --- a/Interop/Runtime/src/jvm/kotlin/kotlinx/cinterop/JvmCallbacks.kt +++ b/Interop/Runtime/src/jvm/kotlin/kotlinx/cinterop/JvmCallbacks.kt @@ -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 override fun write(location: NativePtr, value: CValue<*>) = value.write(location) } +@Suppress("DEPRECATION") private class CEnumType(private val rawValueCType: CType) : CType(rawValueCType.ffiType) { override fun read(location: NativePtr): CEnum { diff --git a/Interop/Runtime/src/jvm/kotlin/kotlinx/cinterop/JvmTypes.kt b/Interop/Runtime/src/jvm/kotlin/kotlinx/cinterop/JvmTypes.kt index f59ad3fe2a3..49b16c4231e 100644 --- a/Interop/Runtime/src/jvm/kotlin/kotlinx/cinterop/JvmTypes.kt +++ b/Interop/Runtime/src/jvm/kotlin/kotlinx/cinterop/JvmTypes.kt @@ -28,10 +28,13 @@ public val nativeNullPtr: NativePtr = 0L // TODO: the functions below should eventually be intrinsified +@Suppress("DEPRECATION") private val typeOfCache = ConcurrentHashMap, CVariable.Type>() +@Deprecated("Use sizeOf() or alignOf() instead.") @Suppress("NON_PUBLIC_CALL_FROM_PUBLIC_INLINE") inline fun typeOf() = + @Suppress("DEPRECATION") typeOfCache.computeIfAbsent(T::class.java) { T::class.companionObjectInstance as CVariable.Type } /** diff --git a/Interop/Runtime/src/main/kotlin/kotlinx/cinterop/Types.kt b/Interop/Runtime/src/main/kotlin/kotlinx/cinterop/Types.kt index f2a9dd5055b..e338c4928f9 100644 --- a/Interop/Runtime/src/main/kotlin/kotlinx/cinterop/Types.kt +++ b/Interop/Runtime/src/main/kotlin/kotlinx/cinterop/Types.kt @@ -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() or alignOf() 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 sizeOf() = typeOf().size + +@Suppress("DEPRECATION") public inline fun alignOf() = typeOf().align /** @@ -243,6 +247,8 @@ public inline fun CStructVar.arrayMemberAt(offset: Long) * The C struct-typed variable located in memory. */ public abstract class CStructVar(rawPtr: NativePtr) : CVariable(rawPtr) { + @Deprecated("Use sizeOf() or alignOf() 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() or alignOf() 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(rawPtr: NativePtr) : CPrimitiveVar(rawPtr) { + @Deprecated("Use sizeOf() or alignOf() instead.") + @Suppress("DEPRECATION") companion object : Type(1) } @Suppress("FINAL_UPPER_BOUND") public class ByteVarOf(rawPtr: NativePtr) : CPrimitiveVar(rawPtr) { + @Deprecated("Use sizeOf() or alignOf() instead.") + @Suppress("DEPRECATION") companion object : Type(1) } @Suppress("FINAL_UPPER_BOUND") public class ShortVarOf(rawPtr: NativePtr) : CPrimitiveVar(rawPtr) { + @Deprecated("Use sizeOf() or alignOf() instead.") + @Suppress("DEPRECATION") companion object : Type(2) } @Suppress("FINAL_UPPER_BOUND") public class IntVarOf(rawPtr: NativePtr) : CPrimitiveVar(rawPtr) { + @Deprecated("Use sizeOf() or alignOf() instead.") + @Suppress("DEPRECATION") companion object : Type(4) } @Suppress("FINAL_UPPER_BOUND") public class LongVarOf(rawPtr: NativePtr) : CPrimitiveVar(rawPtr) { + @Deprecated("Use sizeOf() or alignOf() instead.") + @Suppress("DEPRECATION") companion object : Type(8) } @Suppress("FINAL_UPPER_BOUND") public class UByteVarOf(rawPtr: NativePtr) : CPrimitiveVar(rawPtr) { + @Deprecated("Use sizeOf() or alignOf() instead.") + @Suppress("DEPRECATION") companion object : Type(1) } @Suppress("FINAL_UPPER_BOUND") public class UShortVarOf(rawPtr: NativePtr) : CPrimitiveVar(rawPtr) { + @Deprecated("Use sizeOf() or alignOf() instead.") + @Suppress("DEPRECATION") companion object : Type(2) } @Suppress("FINAL_UPPER_BOUND") public class UIntVarOf(rawPtr: NativePtr) : CPrimitiveVar(rawPtr) { + @Deprecated("Use sizeOf() or alignOf() instead.") + @Suppress("DEPRECATION") companion object : Type(4) } @Suppress("FINAL_UPPER_BOUND") public class ULongVarOf(rawPtr: NativePtr) : CPrimitiveVar(rawPtr) { + @Deprecated("Use sizeOf() or alignOf() instead.") + @Suppress("DEPRECATION") companion object : Type(8) } @Suppress("FINAL_UPPER_BOUND") public class FloatVarOf(rawPtr: NativePtr) : CPrimitiveVar(rawPtr) { + @Deprecated("Use sizeOf() or alignOf() instead.") + @Suppress("DEPRECATION") companion object : Type(4) } @Suppress("FINAL_UPPER_BOUND") public class DoubleVarOf(rawPtr: NativePtr) : CPrimitiveVar(rawPtr) { + @Deprecated("Use sizeOf() or alignOf() instead.") + @Suppress("DEPRECATION") companion object : Type(8) } @@ -398,6 +429,8 @@ public var DoubleVarOf.value: T public class CPointerVarOf>(rawPtr: NativePtr) : CVariable(rawPtr) { + @Deprecated("Use sizeOf() or alignOf() instead.") + @Suppress("DEPRECATION") companion object : CVariable.Type(pointerSize.toLong(), pointerSize) } diff --git a/Interop/Runtime/src/main/kotlin/kotlinx/cinterop/Utils.kt b/Interop/Runtime/src/main/kotlin/kotlinx/cinterop/Utils.kt index c85f8e9090e..1c6c9c2443f 100644 --- a/Interop/Runtime/src/main/kotlin/kotlinx/cinterop/Utils.kt +++ b/Interop/Runtime/src/main/kotlin/kotlinx/cinterop/Utils.kt @@ -107,9 +107,11 @@ public class Arena(parent: NativeFreeablePlacement = nativeHeap) : ArenaBase(par * @param T must not be abstract */ public inline fun NativePlacement.alloc(): T = + @Suppress("DEPRECATION") alloc(typeOf()).reinterpret() @PublishedApi +@Suppress("DEPRECATION") internal fun NativePlacement.alloc(type: CVariable.Type): NativePointed = alloc(type.size, type.align) @@ -284,11 +286,13 @@ public fun CPointed.readValue(size: Long, align: Int): CValue } } +@Suppress("DEPRECATION") @PublishedApi internal fun CPointed.readValue(type: CVariable.Type): CValue = 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 T.readValue(): CValue = this.readValue(typeOf()) public fun CValue.write(location: NativePtr) { diff --git a/Interop/Runtime/src/native/kotlin/kotlinx/cinterop/NativeTypes.kt b/Interop/Runtime/src/native/kotlin/kotlinx/cinterop/NativeTypes.kt index 89ba8eebf65..b7feb4f7359 100644 --- a/Interop/Runtime/src/native/kotlin/kotlinx/cinterop/NativeTypes.kt +++ b/Interop/Runtime/src/native/kotlin/kotlinx/cinterop/NativeTypes.kt @@ -32,6 +32,8 @@ internal inline fun NativePtr.toNonNull() = this.reinterpret() or alignOf() instead.") +@Suppress("DEPRECATION") fun 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(rawPtr: NativePtr) : CVariable(rawPtr) { + @Deprecated("Use sizeOf() or alignOf() instead.") + @Suppress("DEPRECATION") companion object : Type(size = 16, align = 16) } diff --git a/Interop/Runtime/src/native/kotlin/kotlinx/cinterop/ObjectiveCImpl.kt b/Interop/Runtime/src/native/kotlin/kotlinx/cinterop/ObjectiveCImpl.kt index 93ad4de3aba..8b1d65f5ffd 100644 --- a/Interop/Runtime/src/native/kotlin/kotlinx/cinterop/ObjectiveCImpl.kt +++ b/Interop/Runtime/src/native/kotlin/kotlinx/cinterop/ObjectiveCImpl.kt @@ -85,10 +85,14 @@ inline fun unwrapKotlinObjectHolder(holder: Any?): T { external internal fun unwrapKotlinObjectHolderImpl(ptr: NativePtr): Any class ObjCObjectVar(rawPtr: NativePtr) : CVariable(rawPtr) { + @Deprecated("Use sizeOf() or alignOf() instead.") + @Suppress("DEPRECATION") companion object : CVariable.Type(pointerSize.toLong(), pointerSize) } class ObjCNotImplementedVar(rawPtr: NativePtr) : CVariable(rawPtr) { + @Deprecated("Use sizeOf() or alignOf() instead.") + @Suppress("DEPRECATION") companion object : CVariable.Type(pointerSize.toLong(), pointerSize) } diff --git a/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/ObjCStubs.kt b/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/ObjCStubs.kt index 3dd512bb559..d327e946d4b 100644 --- a/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/ObjCStubs.kt +++ b/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/ObjCStubs.kt @@ -240,7 +240,7 @@ private fun deprecatedInit(className: String, initParameterNames: List, 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 diff --git a/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/StubIr.kt b/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/StubIr.kt index 0c933c1f95b..1a7b31ccc7d 100644 --- a/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/StubIr.kt +++ b/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/StubIr.kt @@ -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() or alignOf() instead.", + "", + DeprecationLevel.WARNING + ) + + val deprecatedCEnumByValue = Deprecated( + "Will be removed.", + "", + DeprecationLevel.WARNING + ) } } diff --git a/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/StubIrElementBuilders.kt b/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/StubIrElementBuilders.kt index 1e5ce84c82a..8d6e68d5d49 100644 --- a/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/StubIrElementBuilders.kt +++ b/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/StubIrElementBuilders.kt @@ -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", diff --git a/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/StubIrMetadataEmitter.kt b/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/StubIrMetadataEmitter.kt index 3f47a08766c..c720c004157 100644 --- a/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/StubIrMetadataEmitter.kt +++ b/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/StubIrMetadataEmitter.kt @@ -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() diff --git a/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/StubIrTextEmitter.kt b/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/StubIrTextEmitter.kt index 249d6d80cac..42fc8e06397 100644 --- a/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/StubIrTextEmitter.kt +++ b/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/StubIrTextEmitter.kt @@ -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() or alignOf() 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, diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/ContextUtils.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/ContextUtils.kt index 6888ee44744..7a2210d8bb3 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/ContextUtils.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/ContextUtils.kt @@ -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))