From 0a1907d79fb073d41c51a8c9bab74151d90ff929 Mon Sep 17 00:00:00 2001 From: Gleb Lukianets Date: Tue, 1 Oct 2024 11:40:56 +0000 Subject: [PATCH] [Swift Export] KT-71598: resolve init overrides calculateAllAvailableInitializers initKind -> { isConvenience, isRequired } Merge-request: KT-MR-18154 Merged-by: Gleb Lukianets --- .../nodes/SirClassFromKtSymbol.kt | 30 +++++---- .../nodes/SirFunctionFromKtSymbol.kt | 7 +- .../lightclasses/nodes/SirInitFromKtSymbol.kt | 11 +-- .../nodes/SirVariableFromKtSymbol.kt | 4 +- .../sir/lightclasses/utils/OverrideUtils.kt | 53 ++++++++++++++- .../sir/printer/SirAsSwiftSourcesPrinter.kt | 48 +++++++------ .../printer/SirAsSwiftSourcesPrinterTests.kt | 6 +- .../providers/utils/KotlinRuntimeModule.kt | 2 - .../sir/providers/utils/OverrideUtils.kt | 29 -------- .../gen/org/jetbrains/kotlin/sir/SirInit.kt | 3 +- .../kotlin/sir/builder/SirInitBuilder.kt | 9 ++- .../jetbrains/kotlin/sir/impl/SirInitImpl.kt | 3 +- .../src/org/jetbrains/kotlin/sir/SirImport.kt | 3 +- .../kotlin/sir/SirInitializerKind.kt | 13 ---- .../sir/tree/generator/BuilderConfigurator.kt | 8 +++ .../kotlin/sir/tree/generator/SwiftIrTree.kt | 3 +- .../kotlin/sir/tree/generator/Types.kt | 1 - .../referenceTypes/referenceTypes.kt | 8 +-- .../referenceTypes/referenceTypes.swift | 19 ++++-- .../any/golden_result/main/main.swift | 2 +- .../classes/golden_result/main/main.swift | 2 +- .../golden_result/main_three/main_three.swift | 2 +- .../golden_result/main_two/main_two.swift | 2 +- .../golden_result/main/main.swift | 4 +- .../functions/golden_result/main/main.swift | 2 +- .../golden_result/main/main.swift | 2 +- .../golden_result/overrides/overrides.h | 16 ++++- .../golden_result/overrides/overrides.kt | 62 ++++++++++++++--- .../golden_result/overrides/overrides.swift | 67 +++++++++++++++---- .../overrides_across_modules.h | 2 +- .../overrides_across_modules.kt | 7 +- .../overrides_across_modules.swift | 12 ++-- .../generation/overrides/overrides.kt | 16 +++-- .../golden_result/main/main.swift | 2 +- .../typealiases/golden_result/main/main.swift | 2 +- .../variables/golden_result/main/main.swift | 2 +- 36 files changed, 304 insertions(+), 160 deletions(-) delete mode 100644 native/swift/sir-providers/src/org/jetbrains/kotlin/sir/providers/utils/OverrideUtils.kt delete mode 100644 native/swift/sir/src/org/jetbrains/kotlin/sir/SirInitializerKind.kt diff --git a/native/swift/sir-light-classes/src/org/jetbrains/sir/lightclasses/nodes/SirClassFromKtSymbol.kt b/native/swift/sir-light-classes/src/org/jetbrains/sir/lightclasses/nodes/SirClassFromKtSymbol.kt index 8b4a9e336f5c8..137611b952712 100644 --- a/native/swift/sir-light-classes/src/org/jetbrains/sir/lightclasses/nodes/SirClassFromKtSymbol.kt +++ b/native/swift/sir-light-classes/src/org/jetbrains/sir/lightclasses/nodes/SirClassFromKtSymbol.kt @@ -7,10 +7,7 @@ package org.jetbrains.sir.lightclasses.nodes import org.jetbrains.kotlin.analysis.api.components.DefaultTypeClassIds import org.jetbrains.kotlin.analysis.api.projectStructure.KaModule -import org.jetbrains.kotlin.analysis.api.symbols.KaClassKind -import org.jetbrains.kotlin.analysis.api.symbols.KaClassSymbol -import org.jetbrains.kotlin.analysis.api.symbols.KaNamedClassSymbol -import org.jetbrains.kotlin.analysis.api.symbols.KaSymbolModality +import org.jetbrains.kotlin.analysis.api.symbols.* import org.jetbrains.kotlin.analysis.api.types.symbol import org.jetbrains.kotlin.sir.* import org.jetbrains.kotlin.sir.builder.buildGetter @@ -19,7 +16,6 @@ import org.jetbrains.kotlin.sir.builder.buildVariable import org.jetbrains.kotlin.sir.providers.SirSession import org.jetbrains.kotlin.sir.providers.source.KotlinSource import org.jetbrains.kotlin.sir.providers.utils.KotlinRuntimeModule -import org.jetbrains.kotlin.sir.providers.utils.computeIsOverrideForDesignatedInit import org.jetbrains.kotlin.sir.providers.utils.containingModule import org.jetbrains.kotlin.sir.providers.utils.updateImport import org.jetbrains.kotlin.sir.util.SirSwiftModule @@ -27,6 +23,7 @@ import org.jetbrains.sir.lightclasses.SirFromKtSymbol import org.jetbrains.sir.lightclasses.extensions.documentation import org.jetbrains.sir.lightclasses.extensions.lazyWithSessions import org.jetbrains.sir.lightclasses.extensions.withSessions +import org.jetbrains.sir.lightclasses.utils.computeIsOverride internal class SirClassFromKtSymbol( override val ktSymbol: KaNamedClassSymbol, @@ -93,7 +90,6 @@ internal class SirClassFromKtSymbol( private fun kotlinBaseInitDeclaration(): SirDeclaration = buildInit { origin = SirOrigin.KotlinBaseInitOverride(`for` = KotlinSource(ktSymbol)) isFailable = false - initKind = SirInitializerKind.ORDINARY isOverride = true parameters.add( SirParameter( @@ -106,13 +102,7 @@ internal class SirClassFromKtSymbol( private fun syntheticDeclarations(): List = when (ktSymbol.classKind) { KaClassKind.OBJECT, KaClassKind.COMPANION_OBJECT -> listOf( kotlinBaseInitDeclaration(), - buildInit { - origin = SirOrigin.PrivateObjectInit(`for` = KotlinSource(ktSymbol)) - visibility = SirVisibility.PRIVATE - isFailable = false - initKind = SirInitializerKind.ORDINARY - isOverride = computeIsOverrideForDesignatedInit(this@SirClassFromKtSymbol, emptyList()) - }, + SirObjectSyntheticInit(ktSymbol), buildVariable { origin = SirOrigin.ObjectAccessor(`for` = KotlinSource(ktSymbol)) visibility = SirVisibility.PUBLIC @@ -129,3 +119,17 @@ internal class SirClassFromKtSymbol( else -> listOf(kotlinBaseInitDeclaration()) } } + +internal class SirObjectSyntheticInit(ktSymbol: KaNamedClassSymbol) : SirInit() { + override val origin: SirOrigin = SirOrigin.PrivateObjectInit(`for` = KotlinSource(ktSymbol)) + override val visibility: SirVisibility = SirVisibility.PRIVATE + override val isFailable: Boolean = false + override val parameters: List = emptyList() + override val documentation: String? = null + override val isRequired: Boolean = false + override val isConvenience: Boolean = false + override val isOverride: Boolean get() = computeIsOverride() + override lateinit var parent: SirDeclarationParent + override val attributes: MutableList = mutableListOf() + override var body: SirFunctionBody? = null +} \ No newline at end of file diff --git a/native/swift/sir-light-classes/src/org/jetbrains/sir/lightclasses/nodes/SirFunctionFromKtSymbol.kt b/native/swift/sir-light-classes/src/org/jetbrains/sir/lightclasses/nodes/SirFunctionFromKtSymbol.kt index d3cdca11db4ef..5cfa76ae5774a 100644 --- a/native/swift/sir-light-classes/src/org/jetbrains/sir/lightclasses/nodes/SirFunctionFromKtSymbol.kt +++ b/native/swift/sir-light-classes/src/org/jetbrains/sir/lightclasses/nodes/SirFunctionFromKtSymbol.kt @@ -16,7 +16,8 @@ import org.jetbrains.sir.lightclasses.extensions.* import org.jetbrains.sir.lightclasses.extensions.documentation import org.jetbrains.sir.lightclasses.extensions.lazyWithSessions import org.jetbrains.sir.lightclasses.extensions.withSessions -import org.jetbrains.sir.lightclasses.utils.isSuitableForCovariantOverrideOf +import org.jetbrains.sir.lightclasses.utils.* +import org.jetbrains.sir.lightclasses.utils.isSubtypeOf import org.jetbrains.sir.lightclasses.utils.overridableCandidates import org.jetbrains.sir.lightclasses.utils.translateParameters import org.jetbrains.sir.lightclasses.utils.translateReturnType @@ -53,8 +54,8 @@ internal class SirFunctionFromKtSymbol( override val isOverride: Boolean get() = isInstance && overridableCandidates.any { this.name == it.name && - this.parameters == it.parameters && - this.returnType.isSuitableForCovariantOverrideOf(it.returnType) && + this.parameters.isSuitableForOverrideOf(it.parameters) && + this.returnType.isSubtypeOf(it.returnType) && this.isInstance == it.isInstance } diff --git a/native/swift/sir-light-classes/src/org/jetbrains/sir/lightclasses/nodes/SirInitFromKtSymbol.kt b/native/swift/sir-light-classes/src/org/jetbrains/sir/lightclasses/nodes/SirInitFromKtSymbol.kt index 07170d7497745..e889f1ac3603b 100644 --- a/native/swift/sir-light-classes/src/org/jetbrains/sir/lightclasses/nodes/SirInitFromKtSymbol.kt +++ b/native/swift/sir-light-classes/src/org/jetbrains/sir/lightclasses/nodes/SirInitFromKtSymbol.kt @@ -10,11 +10,11 @@ import org.jetbrains.kotlin.analysis.api.projectStructure.KaModule import org.jetbrains.kotlin.sir.* import org.jetbrains.kotlin.sir.providers.SirSession import org.jetbrains.kotlin.sir.providers.source.KotlinSource -import org.jetbrains.kotlin.sir.providers.utils.computeIsOverrideForDesignatedInit import org.jetbrains.sir.lightclasses.SirFromKtSymbol import org.jetbrains.sir.lightclasses.extensions.documentation import org.jetbrains.sir.lightclasses.extensions.lazyWithSessions import org.jetbrains.sir.lightclasses.extensions.withSessions +import org.jetbrains.sir.lightclasses.utils.computeIsOverride import org.jetbrains.sir.lightclasses.utils.translateParameters internal class SirInitFromKtSymbol( @@ -25,7 +25,6 @@ internal class SirInitFromKtSymbol( override val visibility: SirVisibility = SirVisibility.PUBLIC override val isFailable: Boolean = false - override val initKind: SirInitializerKind = SirInitializerKind.ORDINARY override val origin: SirOrigin by lazy { KotlinSource(ktSymbol) @@ -37,9 +36,11 @@ internal class SirInitFromKtSymbol( ktSymbol.documentation() } - override val isOverride: Boolean by lazy { - computeIsOverrideForDesignatedInit(parent, parameters) - } + override val isRequired: Boolean = false + + override val isConvenience: Boolean = false + + override val isOverride: Boolean get() = computeIsOverride() override var parent: SirDeclarationParent get() = withSessions { diff --git a/native/swift/sir-light-classes/src/org/jetbrains/sir/lightclasses/nodes/SirVariableFromKtSymbol.kt b/native/swift/sir-light-classes/src/org/jetbrains/sir/lightclasses/nodes/SirVariableFromKtSymbol.kt index a10d17dc21178..c330a4a27dfd9 100644 --- a/native/swift/sir-light-classes/src/org/jetbrains/sir/lightclasses/nodes/SirVariableFromKtSymbol.kt +++ b/native/swift/sir-light-classes/src/org/jetbrains/sir/lightclasses/nodes/SirVariableFromKtSymbol.kt @@ -18,7 +18,7 @@ import org.jetbrains.sir.lightclasses.extensions.* import org.jetbrains.sir.lightclasses.extensions.documentation import org.jetbrains.sir.lightclasses.extensions.lazyWithSessions import org.jetbrains.sir.lightclasses.extensions.withSessions -import org.jetbrains.sir.lightclasses.utils.isSuitableForCovariantOverrideOf +import org.jetbrains.sir.lightclasses.utils.isSubtypeOf import org.jetbrains.sir.lightclasses.utils.overridableCandidates import org.jetbrains.sir.lightclasses.utils.translateReturnType @@ -68,7 +68,7 @@ internal class SirVariableFromKtSymbol( override val isOverride: Boolean get() = isInstance && overridableCandidates.any { this.name == it.name && - (it.setter == null && this.type.isSuitableForCovariantOverrideOf(it.type) || this.type == it.type) && + (it.setter == null && this.type.isSubtypeOf(it.type) || this.type == it.type) && this.isInstance == it.isInstance } diff --git a/native/swift/sir-light-classes/src/org/jetbrains/sir/lightclasses/utils/OverrideUtils.kt b/native/swift/sir-light-classes/src/org/jetbrains/sir/lightclasses/utils/OverrideUtils.kt index b0b63aa92b46a..c608b29574c45 100644 --- a/native/swift/sir-light-classes/src/org/jetbrains/sir/lightclasses/utils/OverrideUtils.kt +++ b/native/swift/sir-light-classes/src/org/jetbrains/sir/lightclasses/utils/OverrideUtils.kt @@ -15,12 +15,13 @@ internal inline val T.overridableCandida .filter { it.modality != SirModality.FINAL } .toList() + private val SirClass.superClassDeclaration: SirClass? get() = (superClass as? SirNominalType)?.typeDeclaration as? SirClass -internal fun SirType.isSuitableForCovariantOverrideOf(other: SirType): Boolean = when (this) { - is SirOptionalType -> (other as? SirOptionalType)?.let { wrappedType.isSuitableForCovariantOverrideOf(it.wrappedType) } ?: false +internal fun SirType.isSubtypeOf(other: SirType): Boolean = when (this) { + is SirOptionalType -> (other as? SirOptionalType)?.let { wrappedType.isSubtypeOf(it.wrappedType) } ?: false is SirNominalType -> when (other) { - is SirOptionalType -> this.isSuitableForCovariantOverrideOf(other.wrappedType) + is SirOptionalType -> this.isSubtypeOf(other.wrappedType) is SirNominalType -> this.typeDeclaration.isSubclassOf(other.typeDeclaration) else -> false } @@ -28,3 +29,49 @@ internal fun SirType.isSuitableForCovariantOverrideOf(other: SirType): Boolean = } private fun SirDeclaration.isSubclassOf(other: SirDeclaration): Boolean = this == other || this is SirClass && (superClass as? SirNominalType)?.typeDeclaration?.isSubclassOf(other) ?: false + +internal fun SirInit.computeIsOverride(): Boolean = (this.parent as? SirClass)?.superClassDeclaration?.let { cls -> + cls.overrideableInitializers.any { this.isViableOverrideFor(it) } +} ?: false + +internal fun List.isSuitableForOverrideOf(other: List): Boolean = + this.size == other.size && this.zip(other).all { it.second.type.isSubtypeOf(it.first.type) } + +private fun SirInit.isViableOverrideFor(other: SirInit): Boolean = + this.parameters.isSuitableForOverrideOf(other.parameters) && (!this.isFailable || this.isFailable && other.isFailable) + +private val SirClass.overrideableInitializers: List + // By swift rules: + // 1) Only inits matching designated initializers from the superclass need to be marked `override` + // 2) Class inherits its parent's designated initializers if it doesn't itself define any designated (or required) initializers + get() = this.declarations.filterIsInstance().let { initializers -> + if (initializers.all { it.isConvenience }) { + return this.superClassDeclaration?.overrideableInitializers ?: emptyList() + } else { + return initializers.filter { !it.isConvenience } + } + } + +/** + * Returns all available initializers to call, which includes both own and initializers inherited from parent + * Doesn't take into account initializers generated by the swift compiler and macros + * + * See https://docs.swift.org/swift-book/documentation/the-swift-programming-language/initialization + */ +public fun SirClass.calculateAllAvailableInitializers(): List { + val parentInitializers = this.superClassDeclaration?.calculateAllAvailableInitializers() ?: emptyList() + val ownInitializers = this.declarations.filterIsInstance() + val result = ownInitializers.toMutableList() + + // If the class does not define own designated initializers, it inherits designated initializers from parent + if (ownInitializers.none { !it.isConvenience }) { + result.addAll(parentInitializers.filter { !it.isConvenience }) + } + + // If the class overrides or inherits every parent designated initializer, it inherits convenience initializers from its parent + if (parentInitializers.filter { !it.isConvenience }.all { base -> result.any { it.isViableOverrideFor(base) } }) { + result.addAll(parentInitializers.filter { it.isConvenience }) + } + + return result +} \ No newline at end of file diff --git a/native/swift/sir-printer/src/org/jetbrains/sir/printer/SirAsSwiftSourcesPrinter.kt b/native/swift/sir-printer/src/org/jetbrains/sir/printer/SirAsSwiftSourcesPrinter.kt index 3435afc9ae351..20cfa8644ff76 100644 --- a/native/swift/sir-printer/src/org/jetbrains/sir/printer/SirAsSwiftSourcesPrinter.kt +++ b/native/swift/sir-printer/src/org/jetbrains/sir/printer/SirAsSwiftSourcesPrinter.kt @@ -61,13 +61,22 @@ public class SirAsSwiftSourcesPrinter( } private fun SirModule.printImports() { - val lastImport = imports.lastOrNull() - imports.forEach { - it.print() - if (it == lastImport) { + imports + .let { + if (stableDeclarationsOrder) + imports.sortedWith(compareBy( + { it.moduleName }, + { it.mode }, + )) + else + imports + }.takeIf { + it.isNotEmpty() + }?.forEach { + it.print() + }?.also { println() } - } } private fun SirTypealias.print() { @@ -203,7 +212,7 @@ public class SirAsSwiftSourcesPrinter( private fun SirCallable.printOverride() { when (this) { - is SirInit -> if (this.isOverride) { + is SirInit -> if (this.isOverride && !this.isRequired) { print("override ") } is SirClassMemberDeclaration -> (this as SirClassMemberDeclaration).printOverride() @@ -322,11 +331,20 @@ public class SirAsSwiftSourcesPrinter( } } - private fun SirCallable.printPreNameKeywords() = when (this) { - is SirInit -> initKind.print() - is SirFunction -> {} - is SirGetter -> print("get") - is SirSetter -> print("set") + private fun SirCallable.printPreNameKeywords() = this.also { + when (this) { + is SirInit -> { + if (isRequired) { + print("required ") + } + if (isConvenience) { + print("convenience ") + } + } + is SirFunction -> {} + is SirGetter -> print("get") + is SirSetter -> print("set") + } } private fun SirCallable.printName() = print( @@ -364,14 +382,6 @@ public class SirAsSwiftSourcesPrinter( } ) - private fun SirInitializerKind.print() = print( - when (this) { - SirInitializerKind.ORDINARY -> "" - SirInitializerKind.REQUIRED -> "required " - SirInitializerKind.CONVENIENCE -> "convenience " - } - ) - private fun List.print() = takeIf { it.isNotEmpty() } ?.let { diff --git a/native/swift/sir-printer/tests/org/jetbrains/kotlin/sir/printer/SirAsSwiftSourcesPrinterTests.kt b/native/swift/sir-printer/tests/org/jetbrains/kotlin/sir/printer/SirAsSwiftSourcesPrinterTests.kt index 6647976ec1982..f81a3dc274d19 100644 --- a/native/swift/sir-printer/tests/org/jetbrains/kotlin/sir/printer/SirAsSwiftSourcesPrinterTests.kt +++ b/native/swift/sir-printer/tests/org/jetbrains/kotlin/sir/printer/SirAsSwiftSourcesPrinterTests.kt @@ -574,7 +574,6 @@ class SirAsSwiftSourcesPrinterTests { declarations.add( buildInit { origin = SirOrigin.Unknown - initKind = SirInitializerKind.ORDINARY visibility = SirVisibility.PUBLIC isFailable = true isOverride = false @@ -620,7 +619,6 @@ class SirAsSwiftSourcesPrinterTests { declarations.add( buildInit { origin = SirOrigin.Unknown - initKind = SirInitializerKind.ORDINARY visibility = SirVisibility.PUBLIC isFailable = false isOverride = false @@ -650,7 +648,7 @@ class SirAsSwiftSourcesPrinterTests { declarations.add( buildInit { origin = SirOrigin.Unknown - initKind = SirInitializerKind.REQUIRED + isRequired = true visibility = SirVisibility.PUBLIC isFailable = false isOverride = false @@ -680,7 +678,7 @@ class SirAsSwiftSourcesPrinterTests { declarations.add( buildInit { origin = SirOrigin.Unknown - initKind = SirInitializerKind.CONVENIENCE + isConvenience = true visibility = SirVisibility.PUBLIC isFailable = false isOverride = false diff --git a/native/swift/sir-providers/src/org/jetbrains/kotlin/sir/providers/utils/KotlinRuntimeModule.kt b/native/swift/sir-providers/src/org/jetbrains/kotlin/sir/providers/utils/KotlinRuntimeModule.kt index 70d290644e39c..8f174d5afc10b 100644 --- a/native/swift/sir-providers/src/org/jetbrains/kotlin/sir/providers/utils/KotlinRuntimeModule.kt +++ b/native/swift/sir-providers/src/org/jetbrains/kotlin/sir/providers/utils/KotlinRuntimeModule.kt @@ -34,13 +34,11 @@ public object KotlinRuntimeModule : SirModule() { declarations += buildInit { origin = KotlinRuntimeElement() isFailable = false - initKind = SirInitializerKind.ORDINARY isOverride = false } declarations += buildInit { origin = KotlinRuntimeElement() isFailable = false - initKind = SirInitializerKind.ORDINARY isOverride = false parameters.add( SirParameter( diff --git a/native/swift/sir-providers/src/org/jetbrains/kotlin/sir/providers/utils/OverrideUtils.kt b/native/swift/sir-providers/src/org/jetbrains/kotlin/sir/providers/utils/OverrideUtils.kt deleted file mode 100644 index bef95af3c9b48..0000000000000 --- a/native/swift/sir-providers/src/org/jetbrains/kotlin/sir/providers/utils/OverrideUtils.kt +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Copyright 2010-2024 JetBrains s.r.o. and Kotlin Programming Language contributors. - * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. - */ - -package org.jetbrains.kotlin.sir.providers.utils - -import org.jetbrains.kotlin.sir.* - -/** - * Should [SirInit] node be marked as `override`? - * - * For now, the implementation is rather trivial: it just checks that the superclass has an init method with the same list of parameters. - */ -public fun computeIsOverrideForDesignatedInit(parent: SirDeclarationParent, parameters: List): Boolean { - if (parent !is SirClass) return false - val superClass = (parent.superClass as? SirNominalType)?.typeDeclaration - ?: return false - require(superClass is SirClass) - val overridesDesignatedInitFromSuper = superClass.declarations - .filterIsInstance() - .filter { it.initKind == SirInitializerKind.ORDINARY } - .any { superInit -> superInit.parameters == parameters } - return if (overridesDesignatedInitFromSuper) { - true - } else { - computeIsOverrideForDesignatedInit(superClass, parameters) - } -} \ No newline at end of file diff --git a/native/swift/sir/gen/org/jetbrains/kotlin/sir/SirInit.kt b/native/swift/sir/gen/org/jetbrains/kotlin/sir/SirInit.kt index a6da7a8330f62..dac4c0e4f9ecf 100644 --- a/native/swift/sir/gen/org/jetbrains/kotlin/sir/SirInit.kt +++ b/native/swift/sir/gen/org/jetbrains/kotlin/sir/SirInit.kt @@ -20,6 +20,7 @@ abstract class SirInit : SirElementBase(), SirCallable { abstract override var body: SirFunctionBody? abstract val isFailable: Boolean abstract val parameters: List - abstract val initKind: SirInitializerKind + abstract val isConvenience: Boolean + abstract val isRequired: Boolean abstract val isOverride: Boolean } diff --git a/native/swift/sir/gen/org/jetbrains/kotlin/sir/builder/SirInitBuilder.kt b/native/swift/sir/gen/org/jetbrains/kotlin/sir/builder/SirInitBuilder.kt index b94ed49d3d551..47255fbc8262d 100644 --- a/native/swift/sir/gen/org/jetbrains/kotlin/sir/builder/SirInitBuilder.kt +++ b/native/swift/sir/gen/org/jetbrains/kotlin/sir/builder/SirInitBuilder.kt @@ -23,7 +23,8 @@ class SirInitBuilder { var body: SirFunctionBody? = null var isFailable: Boolean by kotlin.properties.Delegates.notNull() val parameters: MutableList = mutableListOf() - lateinit var initKind: SirInitializerKind + var isConvenience: Boolean = false + var isRequired: Boolean = false var isOverride: Boolean = false fun build(): SirInit { @@ -35,7 +36,8 @@ class SirInitBuilder { body, isFailable, parameters, - initKind, + isConvenience, + isRequired, isOverride, ) } @@ -63,7 +65,8 @@ inline fun buildInitCopy(original: SirInit, init: SirInitBuilder.() -> Unit): Si copyBuilder.body = original.body copyBuilder.isFailable = original.isFailable copyBuilder.parameters.addAll(original.parameters) - copyBuilder.initKind = original.initKind + copyBuilder.isConvenience = original.isConvenience + copyBuilder.isRequired = original.isRequired copyBuilder.isOverride = original.isOverride return copyBuilder.apply(init).build() } diff --git a/native/swift/sir/gen/org/jetbrains/kotlin/sir/impl/SirInitImpl.kt b/native/swift/sir/gen/org/jetbrains/kotlin/sir/impl/SirInitImpl.kt index 74868555b1353..b955609bd4490 100644 --- a/native/swift/sir/gen/org/jetbrains/kotlin/sir/impl/SirInitImpl.kt +++ b/native/swift/sir/gen/org/jetbrains/kotlin/sir/impl/SirInitImpl.kt @@ -20,7 +20,8 @@ internal class SirInitImpl( override var body: SirFunctionBody?, override val isFailable: Boolean, override val parameters: MutableList, - override val initKind: SirInitializerKind, + override val isConvenience: Boolean, + override val isRequired: Boolean, override val isOverride: Boolean, ) : SirInit() { override lateinit var parent: SirDeclarationParent diff --git a/native/swift/sir/src/org/jetbrains/kotlin/sir/SirImport.kt b/native/swift/sir/src/org/jetbrains/kotlin/sir/SirImport.kt index 8c398f1686564..f977ea297bbbc 100644 --- a/native/swift/sir/src/org/jetbrains/kotlin/sir/SirImport.kt +++ b/native/swift/sir/src/org/jetbrains/kotlin/sir/SirImport.kt @@ -23,6 +23,7 @@ class SirImport( } enum class Mode { - Exported, ImplementationOnly, + ImplementationOnly, + Exported, } } diff --git a/native/swift/sir/src/org/jetbrains/kotlin/sir/SirInitializerKind.kt b/native/swift/sir/src/org/jetbrains/kotlin/sir/SirInitializerKind.kt deleted file mode 100644 index 5ace075a90222..0000000000000 --- a/native/swift/sir/src/org/jetbrains/kotlin/sir/SirInitializerKind.kt +++ /dev/null @@ -1,13 +0,0 @@ -/* - * Copyright 2010-2024 JetBrains s.r.o. and Kotlin Programming Language contributors. - * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. - */ - -package org.jetbrains.kotlin.sir - -enum class SirInitializerKind { - // Probably should be called designated instead. - ORDINARY, - REQUIRED, - CONVENIENCE -} diff --git a/native/swift/sir/tree-generator/src/org/jetbrains/kotlin/sir/tree/generator/BuilderConfigurator.kt b/native/swift/sir/tree-generator/src/org/jetbrains/kotlin/sir/tree/generator/BuilderConfigurator.kt index 524080373566f..f09a6150e6636 100644 --- a/native/swift/sir/tree-generator/src/org/jetbrains/kotlin/sir/tree/generator/BuilderConfigurator.kt +++ b/native/swift/sir/tree-generator/src/org/jetbrains/kotlin/sir/tree/generator/BuilderConfigurator.kt @@ -35,6 +35,14 @@ class BuilderConfigurator(model: Model) : AbstractSwiftIrTreeBuilderConfigurator default(it, "SirModality.UNSPECIFIED") } + configureFieldInAllLeafBuilders("isConvenience") { + default(it, "false") + } + + configureFieldInAllLeafBuilders("isRequired") { + default(it, "false") + } + builder(setter) { default("parameterName", "\"newValue\"") } diff --git a/native/swift/sir/tree-generator/src/org/jetbrains/kotlin/sir/tree/generator/SwiftIrTree.kt b/native/swift/sir/tree-generator/src/org/jetbrains/kotlin/sir/tree/generator/SwiftIrTree.kt index 0e294535e5684..20bda08d3f65c 100644 --- a/native/swift/sir/tree-generator/src/org/jetbrains/kotlin/sir/tree/generator/SwiftIrTree.kt +++ b/native/swift/sir/tree-generator/src/org/jetbrains/kotlin/sir/tree/generator/SwiftIrTree.kt @@ -125,7 +125,8 @@ object SwiftIrTree : AbstractSwiftIrTreeBuilder() { +field("isFailable", boolean) +listField("parameters", parameterType) - +field("initKind", initKind) + +field("isConvenience", boolean) + +field("isRequired", boolean) +field("isOverride", boolean) } diff --git a/native/swift/sir/tree-generator/src/org/jetbrains/kotlin/sir/tree/generator/Types.kt b/native/swift/sir/tree-generator/src/org/jetbrains/kotlin/sir/tree/generator/Types.kt index c33b78f42b1e0..7dc6938e95adb 100644 --- a/native/swift/sir/tree-generator/src/org/jetbrains/kotlin/sir/tree/generator/Types.kt +++ b/native/swift/sir/tree-generator/src/org/jetbrains/kotlin/sir/tree/generator/Types.kt @@ -16,7 +16,6 @@ val typeType = type(BASE_PACKAGE, "SirType", TypeKind.Class) val enumCaseType = type(BASE_PACKAGE, "SirEnumCase", TypeKind.Class) val functionBodyType = type(BASE_PACKAGE, "SirFunctionBody", TypeKind.Class) val callableKind = type(BASE_PACKAGE, "SirCallableKind", TypeKind.Class) -val initKind = type(BASE_PACKAGE, "SirInitializerKind", TypeKind.Class) val importType = type(BASE_PACKAGE, "SirImport", TypeKind.Class) val modalityKind = type(BASE_PACKAGE, "SirModality", TypeKind.Class) val attributeType = type(BASE_PACKAGE, "SirAttribute", TypeKind.Class) diff --git a/native/swift/swift-export-standalone/testData/execution/referenceTypes/referenceTypes.kt b/native/swift/swift-export-standalone/testData/execution/referenceTypes/referenceTypes.kt index c0f5d54fce06e..db75255e3b1f7 100644 --- a/native/swift/swift-export-standalone/testData/execution/referenceTypes/referenceTypes.kt +++ b/native/swift/swift-export-standalone/testData/execution/referenceTypes/referenceTypes.kt @@ -148,7 +148,7 @@ class HostDerived : HostBase() { // EXPORT_TO_SWIFT // FILE: overrides.kt -open class Parent() { +open class Parent(val value: String) { open fun foo(): String = "Parent" open var bar: Int = 10 @@ -159,7 +159,7 @@ open class Parent() { open fun nullable(): Parent? = this } -open class Child : Parent() { +open class Child(value: Int) : Parent("$value") { override fun foo(): String = "Child" override var bar: Int = 20 @@ -170,7 +170,7 @@ open class Child : Parent() { override fun nullable(): Parent? = this } -class GrandChild : Child() { +class GrandChild(value: Short) : Child(value.toInt()) { final override fun foo(): String = "GrandChild" final override var bar: Int get() = 42 @@ -187,7 +187,7 @@ class GrandChild : Child() { // EXPORT_TO_SWIFT // FILE: overrides_across_modules.kt -open class Cousin : Parent() { +open class Cousin(value: String) : Parent(value) { override fun foo(): String = "Cousin" override var bar: Int = 21 diff --git a/native/swift/swift-export-standalone/testData/execution/referenceTypes/referenceTypes.swift b/native/swift/swift-export-standalone/testData/execution/referenceTypes/referenceTypes.swift index 1949b50cd6102..885e068822051 100644 --- a/native/swift/swift-export-standalone/testData/execution/referenceTypes/referenceTypes.swift +++ b/native/swift/swift-export-standalone/testData/execution/referenceTypes/referenceTypes.swift @@ -286,9 +286,13 @@ func companionObject() throws { } func overridesShouldWork() throws { - let parent: Parent = Parent() - let child: Parent = Child() - let grandchild: Parent = GrandChild() + let parent: Parent = Parent(value: "10") + let child: Parent = Child(value: 20) + let grandchild: Parent = GrandChild(value: Int16(30)) + + try assertEquals(actual: parent.value, expected: "10") + try assertEquals(actual: child.value, expected: "20") + try assertEquals(actual: grandchild.value, expected: "30") try assertEquals(actual: parent.foo(), expected: "Parent") try assertEquals(actual: child.foo(), expected: "Child") @@ -322,8 +326,11 @@ func overridesShouldWork() throws { } func overridesShouldWorkAcrossModules() throws { - let parent: Parent = Parent() - let cousin: Parent = Cousin() + let parent: Parent = Parent(value: "parent") + let cousin: Parent = Cousin(value: "cousin") + + try assertEquals(actual: parent.value, expected: "parent") + try assertEquals(actual: cousin.value, expected: "cousin") try assertEquals(actual: parent.foo(), expected: "Parent") try assertEquals(actual: cousin.foo(), expected: "Cousin") @@ -386,7 +393,7 @@ class ReferenceTypesTests : TestProvider { TestCase(name: "openClassesAdhereToLSP", method: withAutorelease(openClassesAdhereToLSP)), TestCase(name: "companionObject", method: withAutorelease(companionObject)), TestCase(name: "overridesShouldWork", method: withAutorelease(overridesShouldWork)), - TestCase(name: "overridesShouldWork", method: withAutorelease(overridesShouldWorkAcrossModules)), + TestCase(name: "overridesShouldWorkAcrossModules", method: withAutorelease(overridesShouldWorkAcrossModules)), ] } } diff --git a/native/swift/swift-export-standalone/testData/generation/any/golden_result/main/main.swift b/native/swift/swift-export-standalone/testData/generation/any/golden_result/main/main.swift index 38b2271f62cbd..1efa07883ac4c 100644 --- a/native/swift/swift-export-standalone/testData/generation/any/golden_result/main/main.swift +++ b/native/swift/swift-export-standalone/testData/generation/any/golden_result/main/main.swift @@ -1,5 +1,5 @@ -import KotlinRuntime @_implementationOnly import KotlinBridges_main +import KotlinRuntime public final class MyObject : KotlinRuntime.KotlinBase { public static var shared: main.MyObject { diff --git a/native/swift/swift-export-standalone/testData/generation/classes/golden_result/main/main.swift b/native/swift/swift-export-standalone/testData/generation/classes/golden_result/main/main.swift index 9079f4f4a9b59..e20da5526b877 100644 --- a/native/swift/swift-export-standalone/testData/generation/classes/golden_result/main/main.swift +++ b/native/swift/swift-export-standalone/testData/generation/classes/golden_result/main/main.swift @@ -1,6 +1,6 @@ @_exported import ExportedKotlinPackages -import KotlinRuntime @_implementationOnly import KotlinBridges_main +import KotlinRuntime public final class CLASS_WITH_SAME_NAME : KotlinRuntime.KotlinBase { public override init() { diff --git a/native/swift/swift-export-standalone/testData/generation/consuming_dependencies/golden_result/main_three/main_three.swift b/native/swift/swift-export-standalone/testData/generation/consuming_dependencies/golden_result/main_three/main_three.swift index 44f7adf106a35..975423064a4b0 100644 --- a/native/swift/swift-export-standalone/testData/generation/consuming_dependencies/golden_result/main_three/main_three.swift +++ b/native/swift/swift-export-standalone/testData/generation/consuming_dependencies/golden_result/main_three/main_three.swift @@ -1,5 +1,5 @@ -import dependency_deeper_neighbor_exported @_implementationOnly import KotlinBridges_main_three +import dependency_deeper_neighbor_exported public typealias Foo = ExportedKotlinPackages.dependency.four.AnotherBar public var deps_instance_3: main_three.Foo { diff --git a/native/swift/swift-export-standalone/testData/generation/consuming_dependencies/golden_result/main_two/main_two.swift b/native/swift/swift-export-standalone/testData/generation/consuming_dependencies/golden_result/main_two/main_two.swift index e411b33b85b61..9cb2c37827ae4 100644 --- a/native/swift/swift-export-standalone/testData/generation/consuming_dependencies/golden_result/main_two/main_two.swift +++ b/native/swift/swift-export-standalone/testData/generation/consuming_dependencies/golden_result/main_two/main_two.swift @@ -1,6 +1,6 @@ @_exported import ExportedKotlinPackages -import KotlinRuntime @_implementationOnly import KotlinBridges_main_two +import KotlinRuntime public extension ExportedKotlinPackages.org.main.second { public static var deps_instance_2: KotlinRuntime.KotlinBase { diff --git a/native/swift/swift-export-standalone/testData/generation/functionAndClassWithSameName/golden_result/main/main.swift b/native/swift/swift-export-standalone/testData/generation/functionAndClassWithSameName/golden_result/main/main.swift index e618c05cd589d..40fd968653267 100644 --- a/native/swift/swift-export-standalone/testData/generation/functionAndClassWithSameName/golden_result/main/main.swift +++ b/native/swift/swift-export-standalone/testData/generation/functionAndClassWithSameName/golden_result/main/main.swift @@ -1,8 +1,8 @@ @_exported import ExportedKotlinPackages -import flattened +@_implementationOnly import KotlinBridges_main import KotlinRuntime import dep -@_implementationOnly import KotlinBridges_main +import flattened public final class ClassWithFactoryWithoutParameters : KotlinRuntime.KotlinBase { public var value: Swift.Int32 { diff --git a/native/swift/swift-export-standalone/testData/generation/functions/golden_result/main/main.swift b/native/swift/swift-export-standalone/testData/generation/functions/golden_result/main/main.swift index adf120e1dda21..4e0e2e1e79330 100644 --- a/native/swift/swift-export-standalone/testData/generation/functions/golden_result/main/main.swift +++ b/native/swift/swift-export-standalone/testData/generation/functions/golden_result/main/main.swift @@ -1,6 +1,6 @@ @_exported import ExportedKotlinPackages -import KotlinRuntime @_implementationOnly import KotlinBridges_main +import KotlinRuntime public func foo() -> Swift.Int32 { return __root___foo() diff --git a/native/swift/swift-export-standalone/testData/generation/nullable_type/golden_result/main/main.swift b/native/swift/swift-export-standalone/testData/generation/nullable_type/golden_result/main/main.swift index b77fd34f3d62d..1b316d472297b 100644 --- a/native/swift/swift-export-standalone/testData/generation/nullable_type/golden_result/main/main.swift +++ b/native/swift/swift-export-standalone/testData/generation/nullable_type/golden_result/main/main.swift @@ -1,5 +1,5 @@ -import KotlinRuntime @_implementationOnly import KotlinBridges_main +import KotlinRuntime public typealias NonoptionalRef = main.Bar public typealias OptOptRef = main.OptionalRef diff --git a/native/swift/swift-export-standalone/testData/generation/overrides/golden_result/overrides/overrides.h b/native/swift/swift-export-standalone/testData/generation/overrides/golden_result/overrides/overrides.h index 30a585d5df762..0e02aa8cd9c58 100644 --- a/native/swift/swift-export-standalone/testData/generation/overrides/golden_result/overrides/overrides.h +++ b/native/swift/swift-export-standalone/testData/generation/overrides/golden_result/overrides/overrides.h @@ -1,6 +1,8 @@ #include #include +void Child_actuallyOverride__TypesOfArguments__Swift_Int32_opt__overrides_Parent_overrides_Parent_opt___(uintptr_t self, NSNumber * nullable, uintptr_t poly, uintptr_t nullablePoly); + void Child_finalOverrideFunc(uintptr_t self); uintptr_t Child_nonoverride(uintptr_t self) __attribute((noreturn)); @@ -37,6 +39,8 @@ void GrandChild_hopFunc(uintptr_t self); void GrandChild_overrideChainFunc(uintptr_t self); +void Parent_actuallyOverride__TypesOfArguments__Swift_Int32_overrides_Child_overrides_Child__(uintptr_t self, int32_t nullable, uintptr_t poly, uintptr_t nullablePoly); + void Parent_finalOverrideFunc(uintptr_t self); void Parent_finalOverrideHopFunc(uintptr_t self); @@ -71,15 +75,21 @@ NSNumber * Parent_subtypeOptionalPrimitiveFunc(uintptr_t self); NSNumber * Parent_subtypeOptionalPrimitiveVar_get(uintptr_t self); +NSString * Parent_value_get(uintptr_t self); + uintptr_t __root___Child_init_allocate(); -void __root___Child_init_initialize__TypesOfArguments__Swift_UInt__(uintptr_t __kt); +void __root___Child_init_initialize__TypesOfArguments__Swift_UInt_Swift_Int32__(uintptr_t __kt, int32_t value); + +void __root___Child_init_initialize__TypesOfArguments__Swift_UInt_Swift_Int32_overrides_Parent_overrides_Parent__(uintptr_t __kt, int32_t nullable, uintptr_t poly, uintptr_t nullablePoly); + +void __root___Child_init_initialize__TypesOfArguments__Swift_UInt_Swift_String__(uintptr_t __kt, NSString * value); uintptr_t __root___GrandChild_init_allocate(); -void __root___GrandChild_init_initialize__TypesOfArguments__Swift_UInt__(uintptr_t __kt); +void __root___GrandChild_init_initialize__TypesOfArguments__Swift_UInt_Swift_Int32__(uintptr_t __kt, int32_t value); uintptr_t __root___Parent_init_allocate(); -void __root___Parent_init_initialize__TypesOfArguments__Swift_UInt__(uintptr_t __kt); +void __root___Parent_init_initialize__TypesOfArguments__Swift_UInt_Swift_String__(uintptr_t __kt, NSString * value); diff --git a/native/swift/swift-export-standalone/testData/generation/overrides/golden_result/overrides/overrides.kt b/native/swift/swift-export-standalone/testData/generation/overrides/golden_result/overrides/overrides.kt index 6a4ae38f1c550..527cebc3892a0 100644 --- a/native/swift/swift-export-standalone/testData/generation/overrides/golden_result/overrides/overrides.kt +++ b/native/swift/swift-export-standalone/testData/generation/overrides/golden_result/overrides/overrides.kt @@ -5,6 +5,15 @@ import kotlin.native.internal.ExportedBridge import kotlinx.cinterop.* +@ExportedBridge("Child_actuallyOverride__TypesOfArguments__Swift_Int32_opt__overrides_Parent_overrides_Parent_opt___") +public fun Child_actuallyOverride__TypesOfArguments__Swift_Int32_opt__overrides_Parent_overrides_Parent_opt___(self: kotlin.native.internal.NativePtr, nullable: kotlin.native.internal.NativePtr, poly: kotlin.native.internal.NativePtr, nullablePoly: kotlin.native.internal.NativePtr): Unit { + val __self = kotlin.native.internal.ref.dereferenceExternalRCRef(self) as Child + val __nullable = if (nullable == kotlin.native.internal.NativePtr.NULL) null else interpretObjCPointer(nullable) + val __poly = kotlin.native.internal.ref.dereferenceExternalRCRef(poly) as Parent + val __nullablePoly = if (nullablePoly == kotlin.native.internal.NativePtr.NULL) null else kotlin.native.internal.ref.dereferenceExternalRCRef(nullablePoly) as Parent + __self.actuallyOverride(__nullable, __poly, __nullablePoly) +} + @ExportedBridge("Child_finalOverrideFunc") public fun Child_finalOverrideFunc(self: kotlin.native.internal.NativePtr): Unit { val __self = kotlin.native.internal.ref.dereferenceExternalRCRef(self) as Child @@ -130,6 +139,15 @@ public fun GrandChild_overrideChainFunc(self: kotlin.native.internal.NativePtr): __self.overrideChainFunc() } +@ExportedBridge("Parent_actuallyOverride__TypesOfArguments__Swift_Int32_overrides_Child_overrides_Child__") +public fun Parent_actuallyOverride__TypesOfArguments__Swift_Int32_overrides_Child_overrides_Child__(self: kotlin.native.internal.NativePtr, nullable: Int, poly: kotlin.native.internal.NativePtr, nullablePoly: kotlin.native.internal.NativePtr): Unit { + val __self = kotlin.native.internal.ref.dereferenceExternalRCRef(self) as Parent + val __nullable = nullable + val __poly = kotlin.native.internal.ref.dereferenceExternalRCRef(poly) as Child + val __nullablePoly = kotlin.native.internal.ref.dereferenceExternalRCRef(nullablePoly) as Child + __self.actuallyOverride(__nullable, __poly, __nullablePoly) +} + @ExportedBridge("Parent_finalOverrideFunc") public fun Parent_finalOverrideFunc(self: kotlin.native.internal.NativePtr): Unit { val __self = kotlin.native.internal.ref.dereferenceExternalRCRef(self) as Parent @@ -249,16 +267,40 @@ public fun Parent_subtypeOptionalPrimitiveVar_get(self: kotlin.native.internal.N return if (_result == null) return kotlin.native.internal.NativePtr.NULL else return _result.objcPtr() } +@ExportedBridge("Parent_value_get") +public fun Parent_value_get(self: kotlin.native.internal.NativePtr): kotlin.native.internal.NativePtr { + val __self = kotlin.native.internal.ref.dereferenceExternalRCRef(self) as Parent + val _result = __self.value + return _result.objcPtr() +} + @ExportedBridge("__root___Child_init_allocate") public fun __root___Child_init_allocate(): kotlin.native.internal.NativePtr { val _result = kotlin.native.internal.createUninitializedInstance() return kotlin.native.internal.ref.createRetainedExternalRCRef(_result) } -@ExportedBridge("__root___Child_init_initialize__TypesOfArguments__Swift_UInt__") -public fun __root___Child_init_initialize__TypesOfArguments__Swift_UInt__(__kt: kotlin.native.internal.NativePtr): Unit { +@ExportedBridge("__root___Child_init_initialize__TypesOfArguments__Swift_UInt_Swift_Int32__") +public fun __root___Child_init_initialize__TypesOfArguments__Swift_UInt_Swift_Int32__(__kt: kotlin.native.internal.NativePtr, value: Int): Unit { + val ____kt = kotlin.native.internal.ref.dereferenceExternalRCRef(__kt) + val __value = value + kotlin.native.internal.initInstance(____kt, Child(__value)) +} + +@ExportedBridge("__root___Child_init_initialize__TypesOfArguments__Swift_UInt_Swift_Int32_overrides_Parent_overrides_Parent__") +public fun __root___Child_init_initialize__TypesOfArguments__Swift_UInt_Swift_Int32_overrides_Parent_overrides_Parent__(__kt: kotlin.native.internal.NativePtr, nullable: Int, poly: kotlin.native.internal.NativePtr, nullablePoly: kotlin.native.internal.NativePtr): Unit { + val ____kt = kotlin.native.internal.ref.dereferenceExternalRCRef(__kt) + val __nullable = nullable + val __poly = kotlin.native.internal.ref.dereferenceExternalRCRef(poly) as Parent + val __nullablePoly = kotlin.native.internal.ref.dereferenceExternalRCRef(nullablePoly) as Parent + kotlin.native.internal.initInstance(____kt, Child(__nullable, __poly, __nullablePoly)) +} + +@ExportedBridge("__root___Child_init_initialize__TypesOfArguments__Swift_UInt_Swift_String__") +public fun __root___Child_init_initialize__TypesOfArguments__Swift_UInt_Swift_String__(__kt: kotlin.native.internal.NativePtr, value: kotlin.native.internal.NativePtr): Unit { val ____kt = kotlin.native.internal.ref.dereferenceExternalRCRef(__kt) - kotlin.native.internal.initInstance(____kt, Child()) + val __value = interpretObjCPointer(value) + kotlin.native.internal.initInstance(____kt, Child(__value)) } @ExportedBridge("__root___GrandChild_init_allocate") @@ -267,10 +309,11 @@ public fun __root___GrandChild_init_allocate(): kotlin.native.internal.NativePtr return kotlin.native.internal.ref.createRetainedExternalRCRef(_result) } -@ExportedBridge("__root___GrandChild_init_initialize__TypesOfArguments__Swift_UInt__") -public fun __root___GrandChild_init_initialize__TypesOfArguments__Swift_UInt__(__kt: kotlin.native.internal.NativePtr): Unit { +@ExportedBridge("__root___GrandChild_init_initialize__TypesOfArguments__Swift_UInt_Swift_Int32__") +public fun __root___GrandChild_init_initialize__TypesOfArguments__Swift_UInt_Swift_Int32__(__kt: kotlin.native.internal.NativePtr, value: Int): Unit { val ____kt = kotlin.native.internal.ref.dereferenceExternalRCRef(__kt) - kotlin.native.internal.initInstance(____kt, GrandChild()) + val __value = value + kotlin.native.internal.initInstance(____kt, GrandChild(__value)) } @ExportedBridge("__root___Parent_init_allocate") @@ -279,9 +322,10 @@ public fun __root___Parent_init_allocate(): kotlin.native.internal.NativePtr { return kotlin.native.internal.ref.createRetainedExternalRCRef(_result) } -@ExportedBridge("__root___Parent_init_initialize__TypesOfArguments__Swift_UInt__") -public fun __root___Parent_init_initialize__TypesOfArguments__Swift_UInt__(__kt: kotlin.native.internal.NativePtr): Unit { +@ExportedBridge("__root___Parent_init_initialize__TypesOfArguments__Swift_UInt_Swift_String__") +public fun __root___Parent_init_initialize__TypesOfArguments__Swift_UInt_Swift_String__(__kt: kotlin.native.internal.NativePtr, value: kotlin.native.internal.NativePtr): Unit { val ____kt = kotlin.native.internal.ref.dereferenceExternalRCRef(__kt) - kotlin.native.internal.initInstance(____kt, Parent()) + val __value = interpretObjCPointer(value) + kotlin.native.internal.initInstance(____kt, Parent(__value)) } diff --git a/native/swift/swift-export-standalone/testData/generation/overrides/golden_result/overrides/overrides.swift b/native/swift/swift-export-standalone/testData/generation/overrides/golden_result/overrides/overrides.swift index 2376f4f7057a4..c4c9af4953856 100644 --- a/native/swift/swift-export-standalone/testData/generation/overrides/golden_result/overrides/overrides.swift +++ b/native/swift/swift-export-standalone/testData/generation/overrides/golden_result/overrides/overrides.swift @@ -32,15 +32,40 @@ open class Child : overrides.Parent { return Child_subtypeOptionalPrimitiveVar_get(self.__externalRCRef()) } } - public override init() { + public override init( + __externalRCRef: Swift.UInt + ) { + super.init(__externalRCRef: __externalRCRef) + } + public init( + value: Swift.Int32 + ) { let __kt = __root___Child_init_allocate() super.init(__externalRCRef: __kt) - __root___Child_init_initialize__TypesOfArguments__Swift_UInt__(__kt) + __root___Child_init_initialize__TypesOfArguments__Swift_UInt_Swift_Int32__(__kt, value) } public override init( - __externalRCRef: Swift.UInt + value: Swift.String ) { - super.init(__externalRCRef: __externalRCRef) + let __kt = __root___Child_init_allocate() + super.init(__externalRCRef: __kt) + __root___Child_init_initialize__TypesOfArguments__Swift_UInt_Swift_String__(__kt, value) + } + public init( + nullable: Swift.Int32, + poly: overrides.Parent, + nullablePoly: overrides.Parent + ) { + let __kt = __root___Child_init_allocate() + super.init(__externalRCRef: __kt) + __root___Child_init_initialize__TypesOfArguments__Swift_UInt_Swift_Int32_overrides_Parent_overrides_Parent__(__kt, nullable, poly.__externalRCRef(), nullablePoly.__externalRCRef()) + } + open override func actuallyOverride( + nullable: Swift.Int32?, + poly: overrides.Parent, + nullablePoly: overrides.Parent? + ) -> Swift.Void { + return Child_actuallyOverride__TypesOfArguments__Swift_Int32_opt__overrides_Parent_overrides_Parent_opt___(self.__externalRCRef(), nullable.flatMap { it in NSNumber(value: it) }, poly.__externalRCRef(), nullablePoly?.__externalRCRef() ?? 0) } public final override func finalOverrideFunc() -> Swift.Void { return Child_finalOverrideFunc(self.__externalRCRef()) @@ -79,16 +104,18 @@ open class Child : overrides.Parent { } } public final class GrandChild : overrides.Child { - public override init() { - let __kt = __root___GrandChild_init_allocate() - super.init(__externalRCRef: __kt) - __root___GrandChild_init_initialize__TypesOfArguments__Swift_UInt__(__kt) - } public override init( __externalRCRef: Swift.UInt ) { super.init(__externalRCRef: __externalRCRef) } + public override init( + value: Swift.Int32 + ) { + let __kt = __root___GrandChild_init_allocate() + super.init(__externalRCRef: __kt) + __root___GrandChild_init_initialize__TypesOfArguments__Swift_UInt_Swift_Int32__(__kt, value) + } public override func finalOverrideHopFunc() -> Swift.Void { return GrandChild_finalOverrideHopFunc(self.__externalRCRef()) } @@ -130,16 +157,30 @@ open class Parent : KotlinRuntime.KotlinBase { return Parent_subtypeOptionalPrimitiveVar_get(self.__externalRCRef())?.int32Value } } - public override init() { - let __kt = __root___Parent_init_allocate() - super.init(__externalRCRef: __kt) - __root___Parent_init_initialize__TypesOfArguments__Swift_UInt__(__kt) + public final var value: Swift.String { + get { + return Parent_value_get(self.__externalRCRef()) + } } public override init( __externalRCRef: Swift.UInt ) { super.init(__externalRCRef: __externalRCRef) } + public init( + value: Swift.String + ) { + let __kt = __root___Parent_init_allocate() + super.init(__externalRCRef: __kt) + __root___Parent_init_initialize__TypesOfArguments__Swift_UInt_Swift_String__(__kt, value) + } + open func actuallyOverride( + nullable: Swift.Int32, + poly: overrides.Child, + nullablePoly: overrides.Child + ) -> Swift.Void { + return Parent_actuallyOverride__TypesOfArguments__Swift_Int32_overrides_Child_overrides_Child__(self.__externalRCRef(), nullable, poly.__externalRCRef(), nullablePoly.__externalRCRef()) + } open func finalOverrideFunc() -> Swift.Void { return Parent_finalOverrideFunc(self.__externalRCRef()) } diff --git a/native/swift/swift-export-standalone/testData/generation/overrides/golden_result/overrides_across_modules/overrides_across_modules.h b/native/swift/swift-export-standalone/testData/generation/overrides/golden_result/overrides_across_modules/overrides_across_modules.h index 6e94229bceec0..5e892282fc4ad 100644 --- a/native/swift/swift-export-standalone/testData/generation/overrides/golden_result/overrides_across_modules/overrides_across_modules.h +++ b/native/swift/swift-export-standalone/testData/generation/overrides/golden_result/overrides_across_modules/overrides_across_modules.h @@ -9,5 +9,5 @@ int32_t Cousin_primitiveTypeVar_get(uintptr_t self); uintptr_t __root___Cousin_init_allocate(); -void __root___Cousin_init_initialize__TypesOfArguments__Swift_UInt__(uintptr_t __kt); +void __root___Cousin_init_initialize__TypesOfArguments__Swift_UInt_Swift_String__(uintptr_t __kt, NSString * value); diff --git a/native/swift/swift-export-standalone/testData/generation/overrides/golden_result/overrides_across_modules/overrides_across_modules.kt b/native/swift/swift-export-standalone/testData/generation/overrides/golden_result/overrides_across_modules/overrides_across_modules.kt index 3b2e96dc5963d..813822f59fd5a 100644 --- a/native/swift/swift-export-standalone/testData/generation/overrides/golden_result/overrides_across_modules/overrides_across_modules.kt +++ b/native/swift/swift-export-standalone/testData/generation/overrides/golden_result/overrides_across_modules/overrides_across_modules.kt @@ -30,9 +30,10 @@ public fun __root___Cousin_init_allocate(): kotlin.native.internal.NativePtr { return kotlin.native.internal.ref.createRetainedExternalRCRef(_result) } -@ExportedBridge("__root___Cousin_init_initialize__TypesOfArguments__Swift_UInt__") -public fun __root___Cousin_init_initialize__TypesOfArguments__Swift_UInt__(__kt: kotlin.native.internal.NativePtr): Unit { +@ExportedBridge("__root___Cousin_init_initialize__TypesOfArguments__Swift_UInt_Swift_String__") +public fun __root___Cousin_init_initialize__TypesOfArguments__Swift_UInt_Swift_String__(__kt: kotlin.native.internal.NativePtr, value: kotlin.native.internal.NativePtr): Unit { val ____kt = kotlin.native.internal.ref.dereferenceExternalRCRef(__kt) - kotlin.native.internal.initInstance(____kt, Cousin()) + val __value = interpretObjCPointer(value) + kotlin.native.internal.initInstance(____kt, Cousin(__value)) } diff --git a/native/swift/swift-export-standalone/testData/generation/overrides/golden_result/overrides_across_modules/overrides_across_modules.swift b/native/swift/swift-export-standalone/testData/generation/overrides/golden_result/overrides_across_modules/overrides_across_modules.swift index f1b06508d7583..a69d8f0d66e75 100644 --- a/native/swift/swift-export-standalone/testData/generation/overrides/golden_result/overrides_across_modules/overrides_across_modules.swift +++ b/native/swift/swift-export-standalone/testData/generation/overrides/golden_result/overrides_across_modules/overrides_across_modules.swift @@ -7,16 +7,18 @@ open class Cousin : overrides.Parent { return Cousin_primitiveTypeVar_get(self.__externalRCRef()) } } - public override init() { - let __kt = __root___Cousin_init_allocate() - super.init(__externalRCRef: __kt) - __root___Cousin_init_initialize__TypesOfArguments__Swift_UInt__(__kt) - } public override init( __externalRCRef: Swift.UInt ) { super.init(__externalRCRef: __externalRCRef) } + public override init( + value: Swift.String + ) { + let __kt = __root___Cousin_init_allocate() + super.init(__externalRCRef: __kt) + __root___Cousin_init_initialize__TypesOfArguments__Swift_UInt_Swift_String__(__kt, value) + } public final override func finalOverrideFunc() -> Swift.Void { return Cousin_finalOverrideFunc(self.__externalRCRef()) } diff --git a/native/swift/swift-export-standalone/testData/generation/overrides/overrides.kt b/native/swift/swift-export-standalone/testData/generation/overrides/overrides.kt index 4563cec3d2a72..ebc49ec67184b 100644 --- a/native/swift/swift-export-standalone/testData/generation/overrides/overrides.kt +++ b/native/swift/swift-export-standalone/testData/generation/overrides/overrides.kt @@ -3,7 +3,9 @@ // EXPORT_TO_SWIFT // FILE: inheritance.kt -open class Parent() { +open class Parent(val value: String) { + open fun actuallyOverride(nullable: Int, poly: Child, nullablePoly: Child) = Unit + open fun nonoverride(): Int = 42 open fun primitiveTypeFunc(arg: Int): Int = arg @@ -26,7 +28,13 @@ open class Parent() { open fun overrideChainFunc() = Unit } -open class Child : Parent() { +open class Child(value: Int) : Parent("$value") { + constructor(nullable: Int, poly: Parent, nullablePoly: Parent): this(42) + constructor(value: String): this(43) + + open fun actuallyOverride(nullable: Int?, poly: Parent, nullablePoly: Parent?): Unit = + TODO("This is actually an override in swift") + override fun nonoverride(): Nothing = TODO("This is not an override in swift") override fun primitiveTypeFunc(arg: Int): Int = 43 @@ -47,7 +55,7 @@ open class Child : Parent() { open override fun overrideChainFunc() = Unit } -class GrandChild : Child() { +class GrandChild(value: Int) : Child(value) { override fun hopFunc() {} final override fun finalOverrideHopFunc() {} final override fun overrideChainFunc() = Unit @@ -57,7 +65,7 @@ class GrandChild : Child() { // EXPORT_TO_SWIFT // FILE: overrides_across_modules.kt -open class Cousin : Parent() { +open class Cousin(value: String) : Parent(value) { override fun primitiveTypeFunc(arg: Int): Int = 10 override val primitiveTypeVar: Int get() = 20 final override fun finalOverrideFunc() {} diff --git a/native/swift/swift-export-standalone/testData/generation/type_reference/golden_result/main/main.swift b/native/swift/swift-export-standalone/testData/generation/type_reference/golden_result/main/main.swift index e201755b5b18f..02c4a7dc9bac2 100644 --- a/native/swift/swift-export-standalone/testData/generation/type_reference/golden_result/main/main.swift +++ b/native/swift/swift-export-standalone/testData/generation/type_reference/golden_result/main/main.swift @@ -1,6 +1,6 @@ @_exported import ExportedKotlinPackages -import KotlinRuntime @_implementationOnly import KotlinBridges_main +import KotlinRuntime public final class Class_without_package : KotlinRuntime.KotlinBase { public final class INNER_CLASS : KotlinRuntime.KotlinBase { diff --git a/native/swift/swift-export-standalone/testData/generation/typealiases/golden_result/main/main.swift b/native/swift/swift-export-standalone/testData/generation/typealiases/golden_result/main/main.swift index a32fcd64c8062..a8308737d0bed 100644 --- a/native/swift/swift-export-standalone/testData/generation/typealiases/golden_result/main/main.swift +++ b/native/swift/swift-export-standalone/testData/generation/typealiases/golden_result/main/main.swift @@ -1,6 +1,6 @@ @_exported import ExportedKotlinPackages -import KotlinRuntime @_implementationOnly import KotlinBridges_main +import KotlinRuntime public typealias DefaultInteger = main.RegularInteger public typealias RegularInteger = Swift.Int32 diff --git a/native/swift/swift-export-standalone/testData/generation/variables/golden_result/main/main.swift b/native/swift/swift-export-standalone/testData/generation/variables/golden_result/main/main.swift index 3a5dc84412c57..20d20ea34d9b7 100644 --- a/native/swift/swift-export-standalone/testData/generation/variables/golden_result/main/main.swift +++ b/native/swift/swift-export-standalone/testData/generation/variables/golden_result/main/main.swift @@ -1,6 +1,6 @@ @_exported import ExportedKotlinPackages -import KotlinRuntime @_implementationOnly import KotlinBridges_main +import KotlinRuntime public var BOOLEAN_CONST: Swift.Bool { get {