diff --git a/Interop/JsRuntime/src/main/kotlin/jsinterop.kt b/Interop/JsRuntime/src/main/kotlin/jsinterop.kt index e330f75a79d..feb92373d28 100644 --- a/Interop/JsRuntime/src/main/kotlin/jsinterop.kt +++ b/Interop/JsRuntime/src/main/kotlin/jsinterop.kt @@ -15,6 +15,7 @@ */ package kotlinx.wasm.jsinterop + import kotlin.native.* import kotlin.native.internal.ExportForCppRuntime import kotlinx.cinterop.* @@ -28,15 +29,15 @@ typealias Pointer = Int * from internalization and DCE */ -@Used +@Retain @SymbolName("Konan_js_allocateArena") external public fun allocateArena(): Arena -@Used +@Retain @SymbolName("Konan_js_freeArena") external public fun freeArena(arena: Arena) -@Used +@Retain @SymbolName("Konan_js_pushIntToArena") external public fun pushIntToArena(arena: Arena, value: Int) @@ -50,15 +51,15 @@ fun doubleUpper(value: Double): Int = fun doubleLower(value: Double): Int = (value.toBits() and 0x00000000ffffffff) .toInt() -@Used +@Retain @SymbolName("ReturnSlot_getDouble") external public fun ReturnSlot_getDouble(): Double -@Used +@Retain @SymbolName("Kotlin_String_utf16pointer") external public fun stringPointer(message: String): Pointer -@Used +@Retain @SymbolName("Kotlin_String_utf16length") external public fun stringLengthBytes(message: String): Int @@ -69,7 +70,7 @@ fun wrapFunction(func: KtFunction): Int { return ptr.toInt() // TODO: LP64 unsafe. } -@Used +@Retain @ExportForCppRuntime("Konan_js_runLambda") fun runLambda(pointer: Int, argumentsArena: Arena, argumentsArenaSize: Int): Int { val arguments = arrayListOf() @@ -105,19 +106,19 @@ open class JsArray(arena: Arena, index: Object): JsValue(arena, index) { get() = this.getInt("length") } -@Used +@Retain @SymbolName("Konan_js_getInt") external public fun getInt(arena: Arena, obj: Object, propertyPtr: Pointer, propertyLen: Int): Int; -@Used +@Retain @SymbolName("Konan_js_getProperty") external public fun Konan_js_getProperty(arena: Arena, obj: Object, propertyPtr: Pointer, propertyLen: Int): Int; -@Used +@Retain @SymbolName("Konan_js_setFunction") external public fun setFunction(arena: Arena, obj: Object, propertyName: Pointer, propertyLength: Int , function: Int) -@Used +@Retain @SymbolName("Konan_js_setString") external public fun setString(arena: Arena, obj: Object, propertyName: Pointer, propertyLength: Int, stringPtr: Pointer, stringLength: Int ) diff --git a/Interop/Runtime/src/native/kotlin/kotlinx/cinterop/NativeTypes.kt b/Interop/Runtime/src/native/kotlin/kotlinx/cinterop/NativeTypes.kt index 831fe753d5c..f5e491d2d58 100644 --- a/Interop/Runtime/src/native/kotlin/kotlinx/cinterop/NativeTypes.kt +++ b/Interop/Runtime/src/native/kotlin/kotlinx/cinterop/NativeTypes.kt @@ -17,8 +17,9 @@ package kotlinx.cinterop import kotlin.native.internal.getNativeNullPtr -import kotlin.native.internal.Intrinsic import kotlin.native.internal.reinterpret +import kotlin.native.internal.Intrinsic +import kotlin.native.internal.VolatileLambda typealias NativePtr = kotlin.native.internal.NativePtr internal typealias NonNullNativePtr = kotlin.native.internal.NonNullNativePtr diff --git a/Interop/Runtime/src/native/kotlin/kotlinx/cinterop/ObjectiveCImpl.kt b/Interop/Runtime/src/native/kotlin/kotlinx/cinterop/ObjectiveCImpl.kt index 2fa9b25435d..bb241131823 100644 --- a/Interop/Runtime/src/native/kotlin/kotlinx/cinterop/ObjectiveCImpl.kt +++ b/Interop/Runtime/src/native/kotlin/kotlinx/cinterop/ObjectiveCImpl.kt @@ -18,6 +18,7 @@ package kotlinx.cinterop import kotlin.native.* +import kotlin.native.internal.ExportTypeInfo interface ObjCObject interface ObjCClass : ObjCObject diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/CAdapterGenerator.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/CAdapterGenerator.kt index 448ac8f9a99..f32276c2e14 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/CAdapterGenerator.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/CAdapterGenerator.kt @@ -109,7 +109,7 @@ private val cKeywords = setOf( "xor_eq" ) -private val cnameAnnotation = FqName("kotlin.native.internal.CName") +private val cnameAnnotation = FqName("kotlin.native.CName") private fun org.jetbrains.kotlin.types.KotlinType.isGeneric() = constructor.declarationDescriptor is TypeParameterDescriptor diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/Context.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/Context.kt index 97b72672067..db7e5264036 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/Context.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/Context.kt @@ -291,8 +291,8 @@ internal class Context(config: KonanConfig) : KonanBackendContext(config) { val nativePtrPlusLong by lazy { nativePtr.unsubstitutedMemberScope.getContributedFunctions("plus").single() } val nativePtrToLong by lazy { nativePtr.unsubstitutedMemberScope.getContributedFunctions("toLong").single() } val getNativeNullPtr by lazy { packageScope.getContributedFunctions("getNativeNullPtr").single() } - val immutableBinaryBlobOf by lazy { - builtIns.builtInsModule.getPackage(KonanFqNames.packageName).memberScope.getContributedFunctions("immutableBinaryBlobOf").single() + val immutableBlobOf by lazy { + builtIns.builtInsModule.getPackage(KonanFqNames.packageName).memberScope.getContributedFunctions("immutableBlobOf").single() } val specialDeclarationsFactory = SpecialDeclarationsFactory(this) diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/descriptors/DescriptorUtils.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/descriptors/DescriptorUtils.kt index 59121f1cd4b..7ace17c7f05 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/descriptors/DescriptorUtils.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/descriptors/DescriptorUtils.kt @@ -108,7 +108,7 @@ internal val arrayTypes = setOf( "kotlin.FloatArray", "kotlin.DoubleArray", "kotlin.BooleanArray", - "kotlin.native.ImmutableBinaryBlob" + "kotlin.native.ImmutableBlob" ) diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/ir/Ir.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/ir/Ir.kt index 3f72b515d52..c37ea8f2c87 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/ir/Ir.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/ir/Ir.kt @@ -200,14 +200,12 @@ internal class KonanSymbols(context: Context, val symbolTable: SymbolTable, val it to symbolTable.referenceSimpleFunction(context.getInternalFunctions(name).single()) } - val immutableBinaryBlob = symbolTable.referenceClass( + val immutableBlob = symbolTable.referenceClass( builtInsPackage("kotlin", "native").getContributedClassifier( - Name.identifier("ImmutableBinaryBlob"), NoLookupLocation.FROM_BACKEND + Name.identifier("ImmutableBlob"), NoLookupLocation.FROM_BACKEND ) as ClassDescriptor ) - val immutableBinaryBlobOf = symbolTable.referenceSimpleFunction(context.immutableBinaryBlobOf) - val scheduleImpl = symbolTable.referenceSimpleFunction(context.interopBuiltIns.scheduleImplFunction) val areEqualByValue = context.getInternalFunctions("areEqualByValue").map { diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/BinaryInterface.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/BinaryInterface.kt index 4414c6f3c70..f568c94fa3b 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/BinaryInterface.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/BinaryInterface.kt @@ -69,9 +69,6 @@ internal tailrec fun DeclarationDescriptor.isExported(): Boolean { if (descriptorAnnotations.hasAnnotation(publishedApiAnnotation)){ return true } - if (descriptorAnnotations.hasAnnotation(inlineExposedAnnotation)){ - return true - } if (this.isAnonymousObject) return false @@ -87,8 +84,7 @@ internal tailrec fun DeclarationDescriptor.isExported(): Boolean { // TODO: this code is required because accessor doesn't have a reference to property. if (descriptor is PropertyAccessorDescriptor) { val property = descriptor.correspondingProperty - if (property.annotations.hasAnnotation(inlineExposedAnnotation) || - property.annotations.hasAnnotation(publishedApiAnnotation)) return true + if (property.annotations.hasAnnotation(publishedApiAnnotation)) return true } } @@ -119,16 +115,14 @@ internal tailrec fun DeclarationDescriptor.isExported(): Boolean { private val symbolNameAnnotation = FqName("kotlin.native.SymbolName") -private val exportForCppRuntimeAnnotation = FqName("kotlin.native.internal.ExportForCppRuntime") +private val cnameAnnotation = FqName("kotlin.native.CName") -private val cnameAnnotation = FqName("kotlin.native.internal.CName") +private val exportForCppRuntimeAnnotation = FqName("kotlin.native.internal.ExportForCppRuntime") private val exportForCompilerAnnotation = FqName("kotlin.native.internal.ExportForCompiler") private val publishedApiAnnotation = FqName("kotlin.PublishedApi") -private val inlineExposedAnnotation = FqName("kotlin.internal.InlineExposed") - private fun acyclicTypeMangler(visited: MutableSet, type: IrType): String { val descriptor = (type.classifierOrNull as? IrTypeParameterSymbol)?.owner if (descriptor != null) { diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/IrToBitcode.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/IrToBitcode.kt index c69198041bf..05c163376c1 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/IrToBitcode.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/IrToBitcode.kt @@ -1745,10 +1745,10 @@ internal class CodeGeneratorVisitor(val context: Context, val lifetimes: Map { + context.immutableBlobOf -> { @Suppress("UNCHECKED_CAST") val arg = expression.getValueArgument(0) as IrConst - return context.llvm.staticData.createImmutableBinaryBlob(arg) + return context.llvm.staticData.createImmutableBlob(arg) } context.ir.symbols.initInstance.descriptor -> { diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/RTTIGenerator.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/RTTIGenerator.kt index 91da07b53fe..ea9c04243d6 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/RTTIGenerator.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/RTTIGenerator.kt @@ -97,11 +97,13 @@ internal class RTTIGenerator(override val context: Context) : ContextUtils { staticData.kotlinStringLiteral(string) } + private val EXPORT_TYPE_INFO_FQ_NAME = FqName.fromSegments(listOf("kotlin", "native", "internal", "ExportTypeInfo")) + private fun exportTypeInfoIfRequired(classDesc: ClassDescriptor, typeInfoGlobal: LLVMValueRef?) { - val annot = classDesc.descriptor.annotations.findAnnotation(FqName("kotlin.native.ExportTypeInfo")) + val annot = classDesc.descriptor.annotations.findAnnotation(EXPORT_TYPE_INFO_FQ_NAME) if (annot != null) { val name = getStringValue(annot)!! - // TODO: use LLVMAddAlias? + // TODO: use LLVMAddAlias. val global = addGlobal(name, pointerType(runtime.typeInfoType), isExported = true) LLVMSetInitializer(global, typeInfoGlobal) } @@ -118,7 +120,7 @@ internal class RTTIGenerator(override val context: Context) : ContextUtils { "kotlin.DoubleArray" to LLVMDoubleType()!!, "kotlin.BooleanArray" to LLVMInt8Type()!!, "kotlin.String" to LLVMInt16Type()!!, - "kotlin.native.ImmutableBinaryBlob" to LLVMInt8Type()!! + "kotlin.native.ImmutableBlob" to LLVMInt8Type()!! ) // Keep in sync with Konan_RuntimeType. diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/StaticData.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/StaticData.kt index e46abe22f59..0e18814d515 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/StaticData.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/StaticData.kt @@ -183,7 +183,7 @@ internal class StaticData(override val context: Context): ContextUtils { * * @param args data for constant creation. */ -internal fun StaticData.createImmutableBinaryBlob(value: IrConst): LLVMValueRef { +internal fun StaticData.createImmutableBlob(value: IrConst): LLVMValueRef { val args = value.value.map { Int8(it.toByte()).llvm } - return createKotlinArray(context.ir.symbols.immutableBinaryBlob.owner, args) + return createKotlinArray(context.ir.symbols.immutableBlob.owner, args) } \ No newline at end of file diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/CallableReferenceLowering.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/CallableReferenceLowering.kt index 454e5da6d2b..57ca68d5441 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/CallableReferenceLowering.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/CallableReferenceLowering.kt @@ -107,7 +107,7 @@ internal class CallableReferenceLowering(val context: Context): FileLoweringPass val argumentDescriptor = descriptor.valueParameters.singleOrNull { cur.getValueArgument(it.index) == argument } - if (argumentDescriptor != null && argumentDescriptor.annotations.findAnnotation(FqName("kotlin.native.VolatileLambda")) != null) { + if (argumentDescriptor?.annotations?.findAnnotation(VOLATILE_LAMBDA_FQ_NAME) != null) { return expression } break @@ -141,9 +141,9 @@ internal class CallableReferenceLowering(val context: Context): FileLoweringPass val functionReferenceConstructor: IrConstructor) private val COROUTINES_FQ_NAME = FqName.fromSegments(listOf("kotlin", "coroutines", "experimental")) + private val VOLATILE_LAMBDA_FQ_NAME = FqName.fromSegments(listOf("kotlin", "native", "internal", "VolatileLambda")) private val coroutinesScope = context.irModule!!.descriptor.getPackage(COROUTINES_FQ_NAME).memberScope - private val kotlinPackageScope = context.builtIns.builtInsPackageScope private val continuationClassDescriptor = coroutinesScope .getContributedClassifier(Name.identifier("Continuation"), NoLookupLocation.FROM_BACKEND) as ClassDescriptor diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/PostInlineLowering.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/PostInlineLowering.kt index 1d51cd0acce..584c5fe7bc6 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/PostInlineLowering.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/PostInlineLowering.kt @@ -31,7 +31,7 @@ import org.jetbrains.kotlin.ir.visitors.transformChildrenVoid /** * This pass runs after inlining and performs the following additional transformations over some operations: - * - Convert immutableBinaryBlobOf() arguments to special IrConst. + * - Convert immutableBlobOf() arguments to special IrConst. * - Convert `obj::class` and `Class::class` to calls. */ internal class PostInlineLowering(val context: Context) : FileLoweringPass { @@ -77,7 +77,7 @@ internal class PostInlineLowering(val context: Context) : FileLoweringPass { // Function inlining is changing function symbol at callsite // and unbound symbol replacement is happening later. // So we compare descriptors for now. - if (expression.descriptor == context.immutableBinaryBlobOf) { + if (expression.descriptor == context.immutableBlobOf) { // Convert arguments of the binary blob to special IrConst structure, so that // vararg lowering will not affect it. val args = expression.getValueArgument(0) as? IrVararg @@ -105,7 +105,7 @@ internal class PostInlineLowering(val context: Context) : FileLoweringPass { } expression.putValueArgument(0, IrConstImpl( expression.startOffset, expression.endOffset, - context.ir.symbols.immutableBinaryBlob.typeWithoutArguments, + context.ir.symbols.immutableBlob.typeWithoutArguments, IrConstKind.String, builder.toString())) } diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/optimizations/DataFlowIR.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/optimizations/DataFlowIR.kt index 6818e512459..e55a9def19b 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/optimizations/DataFlowIR.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/optimizations/DataFlowIR.kt @@ -456,7 +456,7 @@ internal object DataFlowIR { private val NAME_ESCAPES = Name.identifier("Escapes") private val NAME_POINTS_TO = Name.identifier("PointsTo") - private val FQ_NAME_KONAN = FqName.fromSegments(listOf("kotlin", "native")) + private val FQ_NAME_KONAN = FqName.fromSegments(listOf("kotlin", "native", "internal")) private val FQ_NAME_ESCAPES = FQ_NAME_KONAN.child(NAME_ESCAPES) private val FQ_NAME_POINTS_TO = FQ_NAME_KONAN.child(NAME_POINTS_TO) diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/serialization/DescriptorTable.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/serialization/DescriptorTable.kt index 97aa8b9ea94..443b1b4dac5 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/serialization/DescriptorTable.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/serialization/DescriptorTable.kt @@ -131,9 +131,6 @@ internal tailrec fun DeclarationDescriptor.isExported(): Boolean { if (this.annotations.hasAnnotation(publishedApiAnnotation)){ return true } - if (this.annotations.hasAnnotation(inlineExposedAnnotation)){ - return true - } if (DescriptorUtils.isAnonymousObject(this)) return false @@ -172,8 +169,6 @@ private val exportForCompilerAnnotation = FqName("kotlin.native.internal.ExportF private val publishedApiAnnotation = FqName("kotlin.PublishedApi") -private val inlineExposedAnnotation = FqName("kotlin.internal.InlineExposed") - private fun acyclicTypeMangler(visited: MutableSet, type: KotlinType): String { val descriptor = TypeUtils.getTypeParameterDescriptorOrNull(type) if (descriptor != null) { diff --git a/backend.native/tests/build.gradle b/backend.native/tests/build.gradle index ba85260a25c..f466acd06b5 100644 --- a/backend.native/tests/build.gradle +++ b/backend.native/tests/build.gradle @@ -1481,7 +1481,7 @@ task if_else(type: RunKonanTest) { } task immutable_binary_blob_in_lambda(type: RunKonanTest) { - source = "lower/immutable_binary_blob_in_lambda.kt" + source = "lower/immutable_blob_in_lambda.kt" goldValue = "123\n" } diff --git a/backend.native/tests/interop/basics/5.kt b/backend.native/tests/interop/basics/5.kt index 20ba5801aee..ca30136f517 100644 --- a/backend.native/tests/interop/basics/5.kt +++ b/backend.native/tests/interop/basics/5.kt @@ -1,6 +1,6 @@ import platform.posix.printf -val golden = immutableBinaryBlobOf(0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x21, 0x00).asCPointer(0) +val golden = immutableBlobOf(0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x21, 0x00).asCPointer(0) fun main(args: Array) { printf("%s\n", golden) diff --git a/backend.native/tests/interop/platform_zlib.kt b/backend.native/tests/interop/platform_zlib.kt index 07230bdf9a6..436dd96e143 100644 --- a/backend.native/tests/interop/platform_zlib.kt +++ b/backend.native/tests/interop/platform_zlib.kt @@ -2,8 +2,8 @@ import kotlinx.cinterop.* import platform.zlib.* -val source = immutableBinaryBlobOf(0xF3, 0x48, 0xCD, 0xC9, 0xC9, 0x57, 0x04, 0x00).asCPointer() -val golden = immutableBinaryBlobOf(0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x21, 0x00).asCPointer() +val source = immutableBlobOf(0xF3, 0x48, 0xCD, 0xC9, 0xC9, 0x57, 0x04, 0x00).asCPointer() +val golden = immutableBlobOf(0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x21, 0x00).asCPointer() fun main(args: Array) = memScoped { val buffer = ByteArray(32) diff --git a/backend.native/tests/lower/immutable_binary_blob_in_lambda.kt b/backend.native/tests/lower/immutable_binary_blob_in_lambda.kt deleted file mode 100644 index 5baf3dd780d..00000000000 --- a/backend.native/tests/lower/immutable_binary_blob_in_lambda.kt +++ /dev/null @@ -1,9 +0,0 @@ -package lower.immutable_binary_blob_in_lambda - -import kotlin.test.* - -@Test -fun runTest() = run { - val golden = immutableBinaryBlobOf(123) - println(golden[0]) -} \ No newline at end of file diff --git a/backend.native/tests/lower/immutable_blob_in_lambda.kt b/backend.native/tests/lower/immutable_blob_in_lambda.kt new file mode 100644 index 00000000000..666448f6f4f --- /dev/null +++ b/backend.native/tests/lower/immutable_blob_in_lambda.kt @@ -0,0 +1,9 @@ +package lower.immutable_blob_in_lambda + +import kotlin.test.* + +@Test +fun runTest() = run { + val golden = immutableBlobOf(123) + println(golden[0]) +} \ No newline at end of file diff --git a/backend.native/tests/produce_dynamic/simple/hello.kt b/backend.native/tests/produce_dynamic/simple/hello.kt index bfc1318435c..4213c7b97e5 100644 --- a/backend.native/tests/produce_dynamic/simple/hello.kt +++ b/backend.native/tests/produce_dynamic/simple/hello.kt @@ -1,5 +1,7 @@ import kotlinx.cinterop.* +import kotlin.native.CName + // Top level functions. fun hello() { println("Hello, dynamic!") @@ -13,15 +15,15 @@ open class Base { open fun fooParam(arg0: String, arg1: Int) = println("Base.fooParam: $arg0 $arg1") - @kotlin.native.internal.CName(fullName = "", shortName = "strangeName") fun странноеИмя() = 111 + @CName(fullName = "", shortName = "strangeName") fun странноеИмя() = 111 } // Top level functions. -@kotlin.native.internal.CName(fullName = "topLevelFunctionFromC", shortName = "topLevelFunctionFromCShort") +@CName(fullName = "topLevelFunctionFromC", shortName = "topLevelFunctionFromCShort") fun topLevelFunction(x1: Int, x2: Int) = x1 - x2 -@kotlin.native.internal.CName("topLevelFunctionVoidFromC") +@CName("topLevelFunctionVoidFromC") fun topLevelFunctionVoid(x1: Int, pointer: COpaquePointer?) { assert(x1 == 42) assert(pointer == null) diff --git a/backend.native/tests/runtime/collections/array3.kt b/backend.native/tests/runtime/collections/array3.kt index 1c7c035487d..184ecf0f5d0 100644 --- a/backend.native/tests/runtime/collections/array3.kt +++ b/backend.native/tests/runtime/collections/array3.kt @@ -5,7 +5,7 @@ import kotlin.test.* import kotlin.native.* @Test fun runTest() { - val data = immutableBinaryBlobOf(0x1, 0x2, 0x3, 0x7, 0x8, 0x9, 0x80, 0xff) + val data = immutableBlobOf(0x1, 0x2, 0x3, 0x7, 0x8, 0x9, 0x80, 0xff) for (b in data) { print("$b ") } diff --git a/backend.native/tests/runtime/collections/typed_array0.kt b/backend.native/tests/runtime/collections/typed_array0.kt index 761392c2270..b3ce024682c 100644 --- a/backend.native/tests/runtime/collections/typed_array0.kt +++ b/backend.native/tests/runtime/collections/typed_array0.kt @@ -10,5 +10,10 @@ import kotlin.test.* expect(0xdef0.toInt()) { array.charAt(5).toInt() } expect(0x9abc.toShort()) { array.shortAt(7) } expect(0x1234_5678) { array.intAt(9) } + expect(0xdef0_0000u) { array.uintAt(3) } + expect(0xf0_00u) { array.ushortAt(4) } + expect(0xf0u) { array.ubyteAt(5) } + expect(0x1234_5678_9abcuL) { array.ulongAt(7) } + println("OK") } \ No newline at end of file diff --git a/runtime/src/main/cpp/Arrays.cpp b/runtime/src/main/cpp/Arrays.cpp index af6fa0315f7..8e424544b42 100644 --- a/runtime/src/main/cpp/Arrays.cpp +++ b/runtime/src/main/cpp/Arrays.cpp @@ -464,7 +464,7 @@ KInt Kotlin_BooleanArray_getArrayLength(KConstRef thiz) { return array->count_; } -OBJ_GETTER(Kotlin_ImmutableBinaryBlob_toByteArray, KConstRef thiz, KInt start, KInt count) { +OBJ_GETTER(Kotlin_ImmutableBlob_toByteArray, KConstRef thiz, KInt start, KInt count) { const ArrayHeader* array = thiz->array(); if (start < 0 || count < 0 || start > array->count_ - count) { ThrowArrayIndexOutOfBoundsException(); @@ -477,7 +477,7 @@ OBJ_GETTER(Kotlin_ImmutableBinaryBlob_toByteArray, KConstRef thiz, KInt start, K RETURN_OBJ(result->obj()); } -KNativePtr Kotlin_ImmutableBinaryBlob_asCPointerImpl(KRef thiz, KInt offset) { +KNativePtr Kotlin_ImmutableBlob_asCPointerImpl(KRef thiz, KInt offset) { ArrayHeader* array = thiz->array(); if (offset < 0 || offset > array->count_) { ThrowArrayIndexOutOfBoundsException(); diff --git a/runtime/src/main/kotlin/kotlin/Any.kt b/runtime/src/main/kotlin/kotlin/Any.kt index 23e4a0a7185..4065080f099 100644 --- a/runtime/src/main/kotlin/kotlin/Any.kt +++ b/runtime/src/main/kotlin/kotlin/Any.kt @@ -16,6 +16,8 @@ package kotlin +import kotlin.native.internal.ExportTypeInfo + /** * The root of the Kotlin class hierarchy. Every Kotlin class has [Any] as a superclass. */ diff --git a/runtime/src/main/kotlin/kotlin/Array.kt b/runtime/src/main/kotlin/kotlin/Array.kt index 7216c3fb4b6..5ec529d2b1a 100644 --- a/runtime/src/main/kotlin/kotlin/Array.kt +++ b/runtime/src/main/kotlin/kotlin/Array.kt @@ -15,10 +15,12 @@ */ package kotlin + import kotlin.native.internal.ExportForCompiler +import kotlin.native.internal.ExportTypeInfo import kotlin.native.internal.InlineConstructor +import kotlin.native.internal.PointsTo -// TODO: remove that, as RTTI shall be per instantiation. @ExportTypeInfo("theArrayTypeInfo") public final class Array { // Constructors are handled with compiler magic. diff --git a/runtime/src/main/kotlin/kotlin/Arrays.kt b/runtime/src/main/kotlin/kotlin/Arrays.kt index 39c93322f91..557a3aa2f7f 100644 --- a/runtime/src/main/kotlin/kotlin/Arrays.kt +++ b/runtime/src/main/kotlin/kotlin/Arrays.kt @@ -14,18 +14,18 @@ * limitations under the License. */ -// TODO: Add SortedSed class and implement toSortedSet extensions +// TODO: Add SortedSet class and implement toSortedSet extensions // TODO: Add fill and binary search methods for primitive arrays (with tests) package kotlin -import kotlin.native.internal.InlineConstructor import kotlin.collections.* import kotlin.internal.PureReifiable +import kotlin.native.internal.ExportTypeInfo +import kotlin.native.internal.InlineConstructor +import kotlin.util.sortArray import kotlin.util.sortArrayComparable import kotlin.util.sortArrayWith -import kotlin.util.sortArray -// TODO: make all iterator() methods inline. /** * An array of bytes. @@ -40,7 +40,6 @@ public final class ByteArray { * Creates a new array of the specified [size], where each element is calculated by calling the specified * [init] function. The [init] function returns an array element given its index. */ - // TODO: What about inline constructors? @InlineConstructor public constructor(size: Int, init: (Int) -> Byte): this(size) { for (i in 0..size - 1) { @@ -66,7 +65,6 @@ public final class ByteArray { } } -// TODO: replace with generics, once implemented. private class ByteIteratorImpl(val collection: ByteArray) : ByteIterator() { var index : Int = 0 diff --git a/runtime/src/main/kotlin/kotlin/Function.kt b/runtime/src/main/kotlin/kotlin/Function.kt index 491c26f0d73..468f874e9a4 100644 --- a/runtime/src/main/kotlin/kotlin/Function.kt +++ b/runtime/src/main/kotlin/kotlin/Function.kt @@ -16,6 +16,8 @@ package kotlin +import kotlin.native.internal.FixmeReflection + /** * Represents a value of a functional type, such as a lambda, an anonymous function or a function reference. * diff --git a/runtime/src/main/kotlin/kotlin/Lazy.kt b/runtime/src/main/kotlin/kotlin/Lazy.kt index d9b4b408995..20d662822fa 100644 --- a/runtime/src/main/kotlin/kotlin/Lazy.kt +++ b/runtime/src/main/kotlin/kotlin/Lazy.kt @@ -17,6 +17,7 @@ package kotlin import kotlin.reflect.KProperty +import kotlin.native.internal.FixmeConcurrency /** * Creates a new instance of the [Lazy] that uses the specified initialization function [initializer] @@ -27,7 +28,6 @@ import kotlin.reflect.KProperty * Note that the returned instance uses itself to synchronize on. Do not synchronize from external code on * the returned instance as it may cause accidental deadlock. Also this behavior can be changed in the future. */ -@FixmeConcurrency public actual fun lazy(initializer: () -> T): Lazy = kotlin.native.worker.FreezeAwareLazyImpl(initializer) /** @@ -43,8 +43,8 @@ public actual fun lazy(initializer: () -> T): Lazy = kotlin.native.worker @FixmeConcurrency public actual fun lazy(mode: LazyThreadSafetyMode, initializer: () -> T): Lazy = when (mode) { - LazyThreadSafetyMode.SYNCHRONIZED -> TODO()//SynchronizedLazyImpl(initializer) - LazyThreadSafetyMode.PUBLICATION -> TODO()//SafePublicationLazyImpl(initializer) + LazyThreadSafetyMode.SYNCHRONIZED -> TODO() // was SynchronizedLazyImpl(initializer) + LazyThreadSafetyMode.PUBLICATION -> TODO() // was SafePublicationLazyImpl(initializer) LazyThreadSafetyMode.NONE -> UnsafeLazyImpl(initializer) } @@ -61,4 +61,4 @@ public actual fun lazy(mode: LazyThreadSafetyMode, initializer: () -> T): La */ @FixmeConcurrency @Suppress("UNUSED_PARAMETER") -public actual fun lazy(lock: Any?, initializer: () -> T): Lazy = TODO()//SynchronizedLazyImpl(initializer, lock) \ No newline at end of file +public actual fun lazy(lock: Any?, initializer: () -> T): Lazy = TODO() // was SynchronizedLazyImpl(initializer, lock) \ No newline at end of file diff --git a/runtime/src/main/kotlin/kotlin/String.kt b/runtime/src/main/kotlin/kotlin/String.kt index da13b329fc0..dde413beee1 100644 --- a/runtime/src/main/kotlin/kotlin/String.kt +++ b/runtime/src/main/kotlin/kotlin/String.kt @@ -16,8 +16,11 @@ package kotlin +import kotlin.native.internal.ExportTypeInfo +import kotlin.native.internal.Frozen + @ExportTypeInfo("theStringTypeInfo") -@kotlin.native.internal.Frozen +@Frozen public final class String : Comparable, CharSequence { public companion object { } diff --git a/runtime/src/main/kotlin/kotlin/SuspendFunction.kt b/runtime/src/main/kotlin/kotlin/SuspendFunction.kt index fe1818a7311..3750ad11e83 100644 --- a/runtime/src/main/kotlin/kotlin/SuspendFunction.kt +++ b/runtime/src/main/kotlin/kotlin/SuspendFunction.kt @@ -16,6 +16,8 @@ package kotlin +import kotlin.native.internal.FixmeReflection + /** * Represents a value of a functional type, such as a lambda, an anonymous function or a function reference. * diff --git a/runtime/src/main/kotlin/kotlin/Throwable.kt b/runtime/src/main/kotlin/kotlin/Throwable.kt index d8f96ba1483..ec7969fcdb0 100644 --- a/runtime/src/main/kotlin/kotlin/Throwable.kt +++ b/runtime/src/main/kotlin/kotlin/Throwable.kt @@ -16,6 +16,8 @@ package kotlin +import kotlin.native.internal.ExportTypeInfo + /** * The base class for all errors and exceptions. Only instances of this class can be thrown or caught. * diff --git a/runtime/src/main/kotlin/kotlin/Unit.kt b/runtime/src/main/kotlin/kotlin/Unit.kt index bfa46829d3e..483cde74619 100644 --- a/runtime/src/main/kotlin/kotlin/Unit.kt +++ b/runtime/src/main/kotlin/kotlin/Unit.kt @@ -16,6 +16,8 @@ package kotlin +import kotlin.native.internal.ExportTypeInfo + @ExportTypeInfo("theUnitTypeInfo") public object Unit { override fun toString() = "kotlin.Unit" diff --git a/runtime/src/main/kotlin/kotlin/collections/ArrayUtil.kt b/runtime/src/main/kotlin/kotlin/collections/ArrayUtil.kt index 706f63d3739..5db849a0645 100644 --- a/runtime/src/main/kotlin/kotlin/collections/ArrayUtil.kt +++ b/runtime/src/main/kotlin/kotlin/collections/ArrayUtil.kt @@ -16,12 +16,14 @@ package kotlin.collections +import kotlin.native.internal.PointsTo + /** * Returns an array of objects of the given type with the given [size], initialized with _uninitialized_ values. * Attempts to read _uninitialized_ values from this array work in implementation-dependent manner, * either throwing exception or returning some kind of implementation-specific default value. */ -@kotlin.internal.InlineExposed +@PublishedApi internal fun arrayOfUninitializedElements(size: Int): Array { // TODO: special case for size == 0? @Suppress("TYPE_PARAMETER_AS_REIFIED") @@ -33,21 +35,41 @@ internal fun arrayOfUninitializedElements(size: Int): Array { * Attempts to read _uninitialized_ values from this array work in implementation-dependent manner, * either throwing exception or returning some kind of implementation-specific default value. */ -fun Array.copyOfUninitializedElements(newSize: Int): Array = copyOfUninitializedElements(0, newSize) -fun ByteArray.copyOfUninitializedElements(newSize: Int): ByteArray = copyOfUninitializedElements(0, newSize) -fun ShortArray.copyOfUninitializedElements(newSize: Int): ShortArray = copyOfUninitializedElements(0, newSize) -fun IntArray.copyOfUninitializedElements(newSize: Int): IntArray = copyOfUninitializedElements(0, newSize) -fun LongArray.copyOfUninitializedElements(newSize: Int): LongArray = copyOfUninitializedElements(0, newSize) -fun CharArray.copyOfUninitializedElements(newSize: Int): CharArray = copyOfUninitializedElements(0, newSize) -fun FloatArray.copyOfUninitializedElements(newSize: Int): FloatArray = copyOfUninitializedElements(0, newSize) -fun DoubleArray.copyOfUninitializedElements(newSize: Int): DoubleArray = copyOfUninitializedElements(0, newSize) -fun BooleanArray.copyOfUninitializedElements(newSize: Int): BooleanArray = copyOfUninitializedElements(0, newSize) +@PublishedApi +internal fun Array.copyOfUninitializedElements(newSize: Int): Array = copyOfUninitializedElements(0, newSize) + +@PublishedApi +internal fun ByteArray.copyOfUninitializedElements(newSize: Int): ByteArray = copyOfUninitializedElements(0, newSize) + +@PublishedApi +internal fun ShortArray.copyOfUninitializedElements(newSize: Int): ShortArray = copyOfUninitializedElements(0, newSize) + +@PublishedApi +internal fun IntArray.copyOfUninitializedElements(newSize: Int): IntArray = copyOfUninitializedElements(0, newSize) + +@PublishedApi +internal fun LongArray.copyOfUninitializedElements(newSize: Int): LongArray = copyOfUninitializedElements(0, newSize) + +@PublishedApi +internal fun CharArray.copyOfUninitializedElements(newSize: Int): CharArray = copyOfUninitializedElements(0, newSize) + +@PublishedApi +internal fun FloatArray.copyOfUninitializedElements(newSize: Int): FloatArray = copyOfUninitializedElements(0, newSize) + +@PublishedApi +internal fun DoubleArray.copyOfUninitializedElements(newSize: Int): DoubleArray = copyOfUninitializedElements(0, newSize) + +@PublishedApi +internal fun BooleanArray.copyOfUninitializedElements(newSize: Int): BooleanArray = copyOfUninitializedElements(0, newSize) /** * Returns a new array which is a copy of the original array with new elements filled with null values. */ -fun Array.copyOfNulls(newSize: Int): Array = copyOfNulls(0, newSize) -fun Array.copyOfNulls(fromIndex: Int, toIndex: Int): Array { +@PublishedApi +internal fun Array.copyOfNulls(newSize: Int): Array = copyOfNulls(0, newSize) + +@PublishedApi +internal fun Array.copyOfNulls(fromIndex: Int, toIndex: Int): Array { val newSize = toIndex - fromIndex if (newSize < 0) { throw IllegalArgumentException("$fromIndex > $toIndex") @@ -90,7 +112,8 @@ internal fun collectionToArray(collection: Collection): Array * Attempts to read _uninitialized_ values from this array work in implementation-dependent manner, * either throwing exception or returning some kind of implementation-specific default value. */ -fun Array.copyOfUninitializedElements(fromIndex: Int, toIndex: Int): Array { +@PublishedApi +internal fun Array.copyOfUninitializedElements(fromIndex: Int, toIndex: Int): Array { val newSize = toIndex - fromIndex if (newSize < 0) { throw IllegalArgumentException("$fromIndex > $toIndex") @@ -100,7 +123,8 @@ fun Array.copyOfUninitializedElements(fromIndex: Int, toIndex: Int): Arra return result } -fun ByteArray.copyOfUninitializedElements(fromIndex: Int, toIndex: Int): ByteArray { +@PublishedApi +internal fun ByteArray.copyOfUninitializedElements(fromIndex: Int, toIndex: Int): ByteArray { val newSize = toIndex - fromIndex if (newSize < 0) { throw IllegalArgumentException("$fromIndex > $toIndex") @@ -110,7 +134,8 @@ fun ByteArray.copyOfUninitializedElements(fromIndex: Int, toIndex: Int): ByteArr return result } -fun ShortArray.copyOfUninitializedElements(fromIndex: Int, toIndex: Int): ShortArray { +@PublishedApi +internal fun ShortArray.copyOfUninitializedElements(fromIndex: Int, toIndex: Int): ShortArray { val newSize = toIndex - fromIndex if (newSize < 0) { throw IllegalArgumentException("$fromIndex > $toIndex") @@ -120,7 +145,8 @@ fun ShortArray.copyOfUninitializedElements(fromIndex: Int, toIndex: Int): ShortA return result } -fun IntArray.copyOfUninitializedElements(fromIndex: Int, toIndex: Int): IntArray { +@PublishedApi +internal fun IntArray.copyOfUninitializedElements(fromIndex: Int, toIndex: Int): IntArray { val newSize = toIndex - fromIndex if (newSize < 0) { throw IllegalArgumentException("$fromIndex > $toIndex") @@ -130,7 +156,8 @@ fun IntArray.copyOfUninitializedElements(fromIndex: Int, toIndex: Int): IntArray return result } -fun LongArray.copyOfUninitializedElements(fromIndex: Int, toIndex: Int): LongArray { +@PublishedApi +internal fun LongArray.copyOfUninitializedElements(fromIndex: Int, toIndex: Int): LongArray { val newSize = toIndex - fromIndex if (newSize < 0) { throw IllegalArgumentException("$fromIndex > $toIndex") @@ -140,7 +167,8 @@ fun LongArray.copyOfUninitializedElements(fromIndex: Int, toIndex: Int): LongArr return result } -fun CharArray.copyOfUninitializedElements(fromIndex: Int, toIndex: Int): CharArray { +@PublishedApi +internal fun CharArray.copyOfUninitializedElements(fromIndex: Int, toIndex: Int): CharArray { val newSize = toIndex - fromIndex if (newSize < 0) { throw IllegalArgumentException("$fromIndex > $toIndex") @@ -150,7 +178,8 @@ fun CharArray.copyOfUninitializedElements(fromIndex: Int, toIndex: Int): CharArr return result } -fun FloatArray.copyOfUninitializedElements(fromIndex: Int, toIndex: Int): FloatArray { +@PublishedApi +internal fun FloatArray.copyOfUninitializedElements(fromIndex: Int, toIndex: Int): FloatArray { val newSize = toIndex - fromIndex if (newSize < 0) { throw IllegalArgumentException("$fromIndex > $toIndex") @@ -160,7 +189,8 @@ fun FloatArray.copyOfUninitializedElements(fromIndex: Int, toIndex: Int): FloatA return result } -fun DoubleArray.copyOfUninitializedElements(fromIndex: Int, toIndex: Int): DoubleArray { +@PublishedApi +internal fun DoubleArray.copyOfUninitializedElements(fromIndex: Int, toIndex: Int): DoubleArray { val newSize = toIndex - fromIndex if (newSize < 0) { throw IllegalArgumentException("$fromIndex > $toIndex") @@ -170,7 +200,8 @@ fun DoubleArray.copyOfUninitializedElements(fromIndex: Int, toIndex: Int): Doubl return result } -fun BooleanArray.copyOfUninitializedElements(fromIndex: Int, toIndex: Int): BooleanArray { +@PublishedApi +internal fun BooleanArray.copyOfUninitializedElements(fromIndex: Int, toIndex: Int): BooleanArray { val newSize = toIndex - fromIndex if (newSize < 0) { throw IllegalArgumentException("$fromIndex > $toIndex") @@ -282,66 +313,66 @@ fun Array.copyRangeTo(destination: Array, fromIndex: Int, toInd destinationIndex, toIndex - fromIndex) } -fun ByteArray.copyRangeTo(destination: ByteArray, fromIndex: Int, toIndex: Int, destinationIndex: Int = 0) { +internal fun ByteArray.copyRangeTo(destination: ByteArray, fromIndex: Int, toIndex: Int, destinationIndex: Int = 0) { copyImpl(this, fromIndex, destination, destinationIndex, toIndex - fromIndex) } -fun ShortArray.copyRangeTo(destination: ShortArray, fromIndex: Int, toIndex: Int, destinationIndex: Int = 0) { +internal fun ShortArray.copyRangeTo(destination: ShortArray, fromIndex: Int, toIndex: Int, destinationIndex: Int = 0) { copyImpl(this, fromIndex, destination, destinationIndex, toIndex - fromIndex) } -fun CharArray.copyRangeTo(destination: CharArray, fromIndex: Int, toIndex: Int, destinationIndex: Int = 0) { +internal fun CharArray.copyRangeTo(destination: CharArray, fromIndex: Int, toIndex: Int, destinationIndex: Int = 0) { copyImpl(this, fromIndex, destination, destinationIndex, toIndex - fromIndex) } -fun IntArray.copyRangeTo(destination: IntArray, fromIndex: Int, toIndex: Int, destinationIndex: Int = 0) { +internal fun IntArray.copyRangeTo(destination: IntArray, fromIndex: Int, toIndex: Int, destinationIndex: Int = 0) { copyImpl(this, fromIndex, destination, destinationIndex, toIndex - fromIndex) } -fun LongArray.copyRangeTo(destination: LongArray, fromIndex: Int, toIndex: Int, destinationIndex: Int = 0) { +internal fun LongArray.copyRangeTo(destination: LongArray, fromIndex: Int, toIndex: Int, destinationIndex: Int = 0) { copyImpl(this, fromIndex, destination, destinationIndex, toIndex - fromIndex) } @SinceKotlin("1.3") @ExperimentalUnsignedTypes -fun UByteArray.copyRangeTo(destination: UByteArray, fromIndex: Int, toIndex: Int, destinationIndex: Int = 0) { +internal fun UByteArray.copyRangeTo(destination: UByteArray, fromIndex: Int, toIndex: Int, destinationIndex: Int = 0) { copyImpl(this, fromIndex, destination, destinationIndex, toIndex - fromIndex) } @SinceKotlin("1.3") @ExperimentalUnsignedTypes -fun UShortArray.copyRangeTo(destination: UShortArray, fromIndex: Int, toIndex: Int, destinationIndex: Int = 0) { +internal fun UShortArray.copyRangeTo(destination: UShortArray, fromIndex: Int, toIndex: Int, destinationIndex: Int = 0) { copyImpl(this, fromIndex, destination, destinationIndex, toIndex - fromIndex) } @SinceKotlin("1.3") @ExperimentalUnsignedTypes -fun UIntArray.copyRangeTo(destination: UIntArray, fromIndex: Int, toIndex: Int, destinationIndex: Int = 0) { +internal fun UIntArray.copyRangeTo(destination: UIntArray, fromIndex: Int, toIndex: Int, destinationIndex: Int = 0) { copyImpl(this, fromIndex, destination, destinationIndex, toIndex - fromIndex) } @SinceKotlin("1.3") @ExperimentalUnsignedTypes -fun ULongArray.copyRangeTo(destination: ULongArray, fromIndex: Int, toIndex: Int, destinationIndex: Int = 0) { +internal fun ULongArray.copyRangeTo(destination: ULongArray, fromIndex: Int, toIndex: Int, destinationIndex: Int = 0) { copyImpl(this, fromIndex, destination, destinationIndex, toIndex - fromIndex) } -fun FloatArray.copyRangeTo(destination: FloatArray, fromIndex: Int, toIndex: Int, destinationIndex: Int = 0) { +internal fun FloatArray.copyRangeTo(destination: FloatArray, fromIndex: Int, toIndex: Int, destinationIndex: Int = 0) { copyImpl(this, fromIndex, destination, destinationIndex, toIndex - fromIndex) } -fun DoubleArray.copyRangeTo(destination: DoubleArray, fromIndex: Int, toIndex: Int, destinationIndex: Int = 0) { +internal fun DoubleArray.copyRangeTo(destination: DoubleArray, fromIndex: Int, toIndex: Int, destinationIndex: Int = 0) { copyImpl(this, fromIndex, destination, destinationIndex, toIndex - fromIndex) } -fun BooleanArray.copyRangeTo(destination: BooleanArray, fromIndex: Int, toIndex: Int, destinationIndex: Int = 0) { +internal fun BooleanArray.copyRangeTo(destination: BooleanArray, fromIndex: Int, toIndex: Int, destinationIndex: Int = 0) { copyImpl(this, fromIndex, destination, destinationIndex, toIndex - fromIndex) } /** * Copies a range of array elements at a specified [fromIndex] (inclusive) to [toIndex] (exclusive) range of indices * to another part of this array starting at [destinationIndex]. */ -fun Array.copyRange(fromIndex: Int, toIndex: Int, destinationIndex: Int = 0) { +public fun Array.copyRange(fromIndex: Int, toIndex: Int, destinationIndex: Int = 0) { copyRangeTo(this, fromIndex, toIndex, destinationIndex) } diff --git a/runtime/src/main/kotlin/kotlin/collections/Arrays.kt b/runtime/src/main/kotlin/kotlin/collections/Arrays.kt index 08ad6811edf..76a03b88518 100644 --- a/runtime/src/main/kotlin/kotlin/collections/Arrays.kt +++ b/runtime/src/main/kotlin/kotlin/collections/Arrays.kt @@ -1152,9 +1152,8 @@ public actual fun CharArray.contentHashCode(): Int { return result } -// From JS collections.kt. internal actual fun arrayOfNulls(reference: Array, size: Int): Array { - return arrayOfNulls(size) as Array + return (@Suppress("UNCHECKED_CAST")(arrayOfNulls(size)) as Array) } internal actual fun copyToArrayImpl(collection: Collection<*>): Array { @@ -1176,7 +1175,7 @@ internal actual fun copyToArrayImpl(collection: Collection<*>, array: Array< array[index++] = iterator.next() as T } if (index < array.size) { - return (array as Array).copyOf(index) as Array + return (@Suppress("UNCHECKED_CAST")(array as Array)).copyOf(index) as Array } return array } diff --git a/runtime/src/main/kotlin/kotlin/internal/Annotations.kt b/runtime/src/main/kotlin/kotlin/internal/Annotations.kt index 85dd367949c..6e2ca47558f 100644 --- a/runtime/src/main/kotlin/kotlin/internal/Annotations.kt +++ b/runtime/src/main/kotlin/kotlin/internal/Annotations.kt @@ -16,13 +16,6 @@ package kotlin.internal -/** - * Specifies that this part of internal API is effectively public exposed by using in public inline function - */ -@Target(AnnotationTarget.CLASS, AnnotationTarget.CONSTRUCTOR, AnnotationTarget.FUNCTION, AnnotationTarget.PROPERTY) -@Retention(AnnotationRetention.BINARY) -internal annotation class InlineExposed - /** * Specifies that the corresponding type parameter is not used for unsafe operations such as casts or 'is' checks * That means it's completely safe to use generic types as argument for such parameter. diff --git a/runtime/src/main/kotlin/kotlin/internal/progressionUtil.kt b/runtime/src/main/kotlin/kotlin/internal/ProgressionUtil.kt similarity index 100% rename from runtime/src/main/kotlin/kotlin/internal/progressionUtil.kt rename to runtime/src/main/kotlin/kotlin/internal/ProgressionUtil.kt diff --git a/runtime/src/main/kotlin/kotlin/native/Annotations.kt b/runtime/src/main/kotlin/kotlin/native/Annotations.kt index c1ad580c2ae..5f61f6ec90c 100644 --- a/runtime/src/main/kotlin/kotlin/native/Annotations.kt +++ b/runtime/src/main/kotlin/kotlin/native/Annotations.kt @@ -25,25 +25,15 @@ import kotlin.reflect.KClass * so it should probably be allowed on `internal` and `private` functions only. */ @Target(AnnotationTarget.FUNCTION) -// @Retention(AnnotationRetention.SOURCE) -annotation class SymbolName(val name: String) - -/** - * Exports the TypeInfo of this class by given name to use it from runtime. - */ -@Target(AnnotationTarget.CLASS) -@Retention(AnnotationRetention.SOURCE) -annotation class ExportTypeInfo(val name: String) - -/** - * If a lambda shall be carefully lowered by the compiler. - */ -annotation class VolatileLambda +@Retention(AnnotationRetention.BINARY) +public annotation class SymbolName(val name: String) /** - * Preserve the function entry point during global optimizations + * Preserve the function entry point during global optimizations. */ -public annotation class Used +@Target(AnnotationTarget.FUNCTION, AnnotationTarget.CLASS) +@Retention(AnnotationRetention.BINARY) +public annotation class Retain // TODO: merge with [kotlin.jvm.Throws] /** @@ -61,22 +51,6 @@ public annotation class Used @Retention(AnnotationRetention.SOURCE) public annotation class Throws(vararg val exceptionClasses: KClass) -/** - * Need to be fixed because of reflection. - */ -public annotation class FixmeReflection - -/** - * Need to be fixed because of concurrency. - */ -public annotation class FixmeConcurrency - -/** - * Escape analysis annotations. - */ -internal annotation class Escapes(val who: Int) -internal annotation class PointsTo(vararg val onWhom: Int) - /** * Top level variable or object is thread local, and so could be mutable. * One may use this annotation as the stopgap measure for singleton @@ -85,4 +59,13 @@ internal annotation class PointsTo(vararg val onWhom: Int) */ @Target(AnnotationTarget.FIELD, AnnotationTarget.CLASS) @Retention(AnnotationRetention.BINARY) -public annotation class ThreadLocal \ No newline at end of file +public annotation class ThreadLocal + +/** + * Makes top level function available from C/C++ code with the given name. + * `fullName` controls the name of top level function, `shortName` controls the short name. + * If `fullName` is empty, no top level declaration is being created. + */ +@Target(AnnotationTarget.FUNCTION) +@Retention(AnnotationRetention.BINARY) +public annotation class CName(val fullName: String = "", val shortName: String = "") \ No newline at end of file diff --git a/runtime/src/main/kotlin/kotlin/native/BinaryBlob.kt b/runtime/src/main/kotlin/kotlin/native/BinaryBlob.kt deleted file mode 100644 index 0ce81a432f0..00000000000 --- a/runtime/src/main/kotlin/kotlin/native/BinaryBlob.kt +++ /dev/null @@ -1,75 +0,0 @@ -/* - * Copyright 2010-2017 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package kotlin.native - -import kotlin.native.internal.* -import kotlinx.cinterop.* - -/** - * An immutable compile-time array of bytes. - */ -@ExportTypeInfo("theImmutableBinaryBlobTypeInfo") -@Frozen -public final class ImmutableBinaryBlob private constructor() { - public val size: Int - get() = getArrayLength() - - // Data layout is the same as for ByteArray, so we can share native functions. - @SymbolName("Kotlin_ByteArray_get") - external public operator fun get(index: Int): Byte - - @SymbolName("Kotlin_ByteArray_getArrayLength") - external private fun getArrayLength(): Int - - /** Creates an iterator over the elements of the array. */ - public operator fun iterator(): ByteIterator { - return ImmutableBinaryBlobIteratorImpl(this) - } -} - -private class ImmutableBinaryBlobIteratorImpl( - val collection: ImmutableBinaryBlob) : ByteIterator() { - var index : Int = 0 - - public override fun nextByte(): Byte { - if (!hasNext()) throw NoSuchElementException("$index") - return collection[index++] - } - - public override operator fun hasNext(): Boolean { - return index < collection.size - } -} - -// Allocates new ByteArray and copies the data. -@SymbolName("Kotlin_ImmutableBinaryBlob_toByteArray") -public external fun ImmutableBinaryBlob.toByteArray(start: Int, count: Int): ByteArray -public fun ImmutableBinaryBlob.toByteArray() = toByteArray(0, size) - -// Returns stable C pointer to data at certain offset, useful as a way to pass resource -// to C API. -public fun ImmutableBinaryBlob.asCPointer(offset: Int = 0) = - interpretCPointer(asCPointerImpl(offset))!! -@SymbolName("Kotlin_ImmutableBinaryBlob_asCPointerImpl") -private external fun ImmutableBinaryBlob.asCPointerImpl(offset: Int): kotlin.native.internal.NativePtr - -// Creates ImmutableBinaryBlob out of compile-time constant data. -// This method accepts Short type, so that values in range 0x80 .. 0xff can be -// provided without toByte() cast. One element still represent one byte in the output data. -// This is the only way to create ImmutableBinaryBlob for now. -// TODO: reconsider? -@Intrinsic -public external fun immutableBinaryBlobOf(vararg elements: Short): ImmutableBinaryBlob diff --git a/runtime/src/main/kotlin/kotlin/native/Blob.kt b/runtime/src/main/kotlin/kotlin/native/Blob.kt new file mode 100644 index 00000000000..c19461d637d --- /dev/null +++ b/runtime/src/main/kotlin/kotlin/native/Blob.kt @@ -0,0 +1,88 @@ +/* + * Copyright 2010-2017 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package kotlin.native + +import kotlin.native.internal.* +import kotlinx.cinterop.* + +/** + * An immutable compile-time array of bytes. + */ +@ExportTypeInfo("theImmutableBlobTypeInfo") +public final class ImmutableBlob private constructor() { + public val size: Int + get() = getArrayLength() + + // Data layout is the same as for ByteArray, so we can share native functions. + @SymbolName("Kotlin_ByteArray_get") + external public operator fun get(index: Int): Byte + + @SymbolName("Kotlin_ByteArray_getArrayLength") + external private fun getArrayLength(): Int + + /** Creates an iterator over the elements of the array. */ + public operator fun iterator(): ByteIterator { + return ImmutableBlobIteratorImpl(this) + } +} + +private class ImmutableBlobIteratorImpl(val blob: ImmutableBlob) : ByteIterator() { + var index : Int = 0 + + public override fun nextByte(): Byte { + if (!hasNext()) throw NoSuchElementException("$index") + return blob[index++] + } + + public override operator fun hasNext(): Boolean { + return index < blob.size + } +} + +/** + * Allocates new ByteArray or UByteArray and copies the data from blob. + */ +@SymbolName("Kotlin_ImmutableBlob_toByteArray") +public external fun ImmutableBlob.toByteArray(start: Int = 0, count: Int = size): ByteArray + +@ExperimentalUnsignedTypes +@SymbolName("Kotlin_ImmutableBlob_toByteArray") +public external fun ImmutableBlob.toUByteArray(start: Int = 0, count: Int = size): UByteArray + +/** + * Returns stable C pointer to data at certain offset, useful as a way to pass resource + * to C APIs. + */ +public fun ImmutableBlob.asCPointer(offset: Int = 0): CPointer = + interpretCPointer(asCPointerImpl(offset))!! + +/* +public fun ImmutableBlob.asUCPointer(offset: Int = 0): CPointer = + interpretCPointer(asCPointerImpl(offset))!! +*/ + +@SymbolName("Kotlin_ImmutableBlob_asCPointerImpl") +private external fun ImmutableBlob.asCPointerImpl(offset: Int): kotlin.native.internal.NativePtr + +/** + * Creates ImmutableBlob out of compile-time constant data. + * This method accepts Short type, so that values in range 0x80 .. 0xff can be + * provided without toByte() cast or 'u' suffix. + * One element still represent one byte in the output data. + * This is the only way to create ImmutableBlob for now. + */ +@Intrinsic +public external fun immutableBlobOf(vararg elements: Short): ImmutableBlob diff --git a/runtime/src/main/kotlin/kotlin/native/concurrent.kt b/runtime/src/main/kotlin/kotlin/native/Compatibility.kt similarity index 94% rename from runtime/src/main/kotlin/kotlin/native/concurrent.kt rename to runtime/src/main/kotlin/kotlin/native/Compatibility.kt index 07ab529ba52..3bf5104618d 100644 --- a/runtime/src/main/kotlin/kotlin/native/concurrent.kt +++ b/runtime/src/main/kotlin/kotlin/native/Compatibility.kt @@ -17,7 +17,8 @@ package kotlin // Note: -// Right now we don't want to have neither 'volatile' nor 'synchronized' at runtime. +// Right now we don't want to have neither 'volatile' nor 'synchronized' at runtime, as it has different +// concurrency approach. @Target(AnnotationTarget.PROPERTY, AnnotationTarget.FIELD) @Retention(AnnotationRetention.SOURCE) diff --git a/runtime/src/main/kotlin/kotlin/native/runtime.kt b/runtime/src/main/kotlin/kotlin/native/Runtime.kt similarity index 100% rename from runtime/src/main/kotlin/kotlin/native/runtime.kt rename to runtime/src/main/kotlin/kotlin/native/Runtime.kt diff --git a/runtime/src/main/kotlin/kotlin/native/TypedArrays.kt b/runtime/src/main/kotlin/kotlin/native/TypedArrays.kt index 27ef0c88428..6e614f5dd15 100644 --- a/runtime/src/main/kotlin/kotlin/native/TypedArrays.kt +++ b/runtime/src/main/kotlin/kotlin/native/TypedArrays.kt @@ -17,18 +17,32 @@ package kotlin.native import kotlin.native.SymbolName +fun ByteArray.ubyteAt(index: Int): UByte = UByte(get(index)) + @SymbolName("Kotlin_ByteArray_getCharAt") external fun ByteArray.charAt(index: Int): Char @SymbolName("Kotlin_ByteArray_getShortAt") external fun ByteArray.shortAt(index: Int): Short +@SymbolName("Kotlin_ByteArray_getShortAt") +@ExperimentalUnsignedTypes +external fun ByteArray.ushortAt(index: Int): UShort + @SymbolName("Kotlin_ByteArray_getIntAt") external fun ByteArray.intAt(index: Int): Int +@SymbolName("Kotlin_ByteArray_getIntAt") +@ExperimentalUnsignedTypes +external fun ByteArray.uintAt(index: Int): UInt + @SymbolName("Kotlin_ByteArray_getLongAt") external fun ByteArray.longAt(index: Int): Long +@SymbolName("Kotlin_ByteArray_getLongAt") +@ExperimentalUnsignedTypes +external fun ByteArray.ulongAt(index: Int): ULong + @SymbolName("Kotlin_ByteArray_getFloatAt") external fun ByteArray.floatAt(index: Int): Float @@ -41,12 +55,23 @@ external fun ByteArray.setCharAt(index: Int, value: Char) @SymbolName("Kotlin_ByteArray_setShortAt") external fun ByteArray.setShortAt(index: Int, value: Short) +@SymbolName("Kotlin_ByteArray_setShortAt") +@ExperimentalUnsignedTypes +external fun ByteArray.setUShortAt(index: Int, value: UShort) + @SymbolName("Kotlin_ByteArray_setIntAt") external fun ByteArray.setIntAt(index: Int, value: Int) +@SymbolName("Kotlin_ByteArray_setIntAt") +external fun ByteArray.setUIntAt(index: Int, value: UInt) + @SymbolName("Kotlin_ByteArray_setLongAt") external fun ByteArray.setLongAt(index: Int, value: Long) +@SymbolName("Kotlin_ByteArray_setLongAt") +@ExperimentalUnsignedTypes +external fun ByteArray.setULongAt(index: Int, value: ULong) + @SymbolName("Kotlin_ByteArray_setFloatAt") external fun ByteArray.setFloatAt(index: Int, value: Float) diff --git a/runtime/src/main/kotlin/kotlin/native/internal/Annotations.kt b/runtime/src/main/kotlin/kotlin/native/internal/Annotations.kt index c455f18a3a3..fcd55301915 100644 --- a/runtime/src/main/kotlin/kotlin/native/internal/Annotations.kt +++ b/runtime/src/main/kotlin/kotlin/native/internal/Annotations.kt @@ -23,40 +23,35 @@ package kotlin.native.internal * The function to call from C++ can be a wrapper around the original function. * * If the name is not specified, the function to call will be available by its Kotlin unqualified name. + * + * This annotation is not intended for the general consumption and is public only for the launcher! */ @Target(AnnotationTarget.FUNCTION, AnnotationTarget.CONSTRUCTOR) @Retention(AnnotationRetention.BINARY) -annotation class ExportForCppRuntime(val name: String = "") - -/** - * Makes top level function available from C/C++ code with the given name. - * `fullName` controls the name of top level function, `shortName` controls the short name. - * If `fullName` is empty, no top level declaration is being created. - */ -@Target(AnnotationTarget.FUNCTION) -@Retention(AnnotationRetention.BINARY) -annotation class CName(val fullName: String = "", val shortName: String = "") +public annotation class ExportForCppRuntime(val name: String = "") /** * This annotation denotes that the element is intrinsic and its usages require special handling in compiler. */ @Target(AnnotationTarget.FUNCTION, AnnotationTarget.PROPERTY) @Retention(AnnotationRetention.BINARY) -annotation class Intrinsic +internal annotation class Intrinsic /** * Exports symbol for compiler needs. + * + * This annotation is not intended for the general consumption and is public only for interop! */ @Target(AnnotationTarget.FUNCTION, AnnotationTarget.CONSTRUCTOR, AnnotationTarget.CLASS) @Retention(AnnotationRetention.BINARY) -annotation class ExportForCompiler +public annotation class ExportForCompiler /** * Annotated constructor will be inlined. */ @Target(AnnotationTarget.CONSTRUCTOR) @Retention(AnnotationRetention.BINARY) -annotation class InlineConstructor +internal annotation class InlineConstructor /** * Class is frozen by default. Also this annotation is (ab)used for marking objects @@ -71,4 +66,43 @@ internal annotation class Frozen */ @Target(AnnotationTarget.CLASS) @Retention(AnnotationRetention.BINARY) -internal annotation class NoReorderFields \ No newline at end of file +internal annotation class NoReorderFields + +/** + * Exports the TypeInfo of this class by given name to use it from runtime. + */ +@Target(AnnotationTarget.CLASS) +@Retention(AnnotationRetention.BINARY) +public annotation class ExportTypeInfo(val name: String) + +/** + * If a lambda shall be carefully lowered by the compiler. + */ +@Target(AnnotationTarget.VALUE_PARAMETER) +@Retention(AnnotationRetention.BINARY) +internal annotation class VolatileLambda + +/** + * Need to be fixed because of reflection. + */ +@Target(AnnotationTarget.FUNCTION, AnnotationTarget.CLASS) +@Retention(AnnotationRetention.SOURCE) +internal annotation class FixmeReflection + +/** + * Need to be fixed because of concurrency. + */ +@Target(AnnotationTarget.FUNCTION, AnnotationTarget.CLASS) +@Retention(AnnotationRetention.SOURCE) +internal annotation class FixmeConcurrency + +/** + * Escape analysis annotations. + */ +@Target(AnnotationTarget.FUNCTION) +@Retention(AnnotationRetention.BINARY) +internal annotation class Escapes(val who: Int) + +@Target(AnnotationTarget.FUNCTION) +@Retention(AnnotationRetention.BINARY) +internal annotation class PointsTo(vararg val onWhom: Int) diff --git a/runtime/src/main/kotlin/kotlin/native/worker/Future.kt b/runtime/src/main/kotlin/kotlin/native/worker/Future.kt index fb2140f7410..4ebd615fba9 100644 --- a/runtime/src/main/kotlin/kotlin/native/worker/Future.kt +++ b/runtime/src/main/kotlin/kotlin/native/worker/Future.kt @@ -100,7 +100,7 @@ fun Collection>.waitForMultipleFutures(millis: Int): Set external internal fun stateOfFuture(id: FutureId): Int @SymbolName("Kotlin_Worker_consumeFuture") -@kotlin.internal.InlineExposed +@PublishedApi external internal fun consumeFuture(id: FutureId): Any? @SymbolName("Kotlin_Worker_waitForAnyFuture") diff --git a/runtime/src/main/kotlin/kotlin/native/worker/Worker.kt b/runtime/src/main/kotlin/kotlin/native/worker/Worker.kt index c3a29b0246a..885a6b93644 100644 --- a/runtime/src/main/kotlin/kotlin/native/worker/Worker.kt +++ b/runtime/src/main/kotlin/kotlin/native/worker/Worker.kt @@ -18,6 +18,7 @@ package kotlin.native.worker import kotlin.native.SymbolName import kotlin.native.internal.ExportForCppRuntime +import kotlin.native.internal.VolatileLambda import kotlinx.cinterop.* /** diff --git a/runtime/src/main/kotlin/kotlin/reflect/KAnnotatedElement.kt b/runtime/src/main/kotlin/kotlin/reflect/KAnnotatedElement.kt index 083b6b9d51e..9a30cff9788 100644 --- a/runtime/src/main/kotlin/kotlin/reflect/KAnnotatedElement.kt +++ b/runtime/src/main/kotlin/kotlin/reflect/KAnnotatedElement.kt @@ -16,6 +16,8 @@ package kotlin.reflect +import kotlin.native.internal.FixmeReflection + /** * Represents an annotated element and allows to obtain its annotations. * See the [Kotlin language documentation](http://kotlinlang.org/docs/reference/annotations.html) diff --git a/runtime/src/main/kotlin/kotlin/reflect/KCallable.kt b/runtime/src/main/kotlin/kotlin/reflect/KCallable.kt index 276c28fcd6d..f1bf8c5710e 100644 --- a/runtime/src/main/kotlin/kotlin/reflect/KCallable.kt +++ b/runtime/src/main/kotlin/kotlin/reflect/KCallable.kt @@ -16,6 +16,8 @@ package kotlin.reflect +import kotlin.native.internal.FixmeReflection + /** * Represents a callable entity, such as a function or a property. * diff --git a/runtime/src/main/kotlin/kotlin/reflect/KProperty.kt b/runtime/src/main/kotlin/kotlin/reflect/KProperty.kt index ed6bdd626c9..f9aeb32e4cc 100644 --- a/runtime/src/main/kotlin/kotlin/reflect/KProperty.kt +++ b/runtime/src/main/kotlin/kotlin/reflect/KProperty.kt @@ -16,7 +16,7 @@ package kotlin.reflect -import kotlin.native.* +import kotlin.native.internal.FixmeReflection /** * Represents a property, such as a named `val` or `var` declaration. diff --git a/runtime/src/main/kotlin/kotlin/sequences/Sequences.kt b/runtime/src/main/kotlin/kotlin/sequences/Sequences.kt index 10875d9e5f9..1a644aa4aa4 100644 --- a/runtime/src/main/kotlin/kotlin/sequences/Sequences.kt +++ b/runtime/src/main/kotlin/kotlin/sequences/Sequences.kt @@ -18,6 +18,7 @@ package kotlin.sequences import kotlin.comparisons.* import kotlin.coroutines.experimental.* +import kotlin.native.internal.FixmeConcurrency @FixmeConcurrency internal actual class ConstrainedOnceSequence actual constructor(sequence: Sequence) : Sequence {