From ac47675d5040c5766da3eadb6a3f1e7f705c4fbe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filip=20Doln=C3=ADk?= Date: Mon, 27 Nov 2023 10:40:32 +0100 Subject: [PATCH] Fix that swift runtime classes were registered even without the runtime being present. --- .../touchlab/skie/context/SirPhaseContext.kt | 2 +- .../touchlab/skie/kir/builtin/KirBuiltins.kt | 2 +- .../co/touchlab/skie/sir/SirProvider.kt | 3 +- .../touchlab/skie/sir/builtin/SirBuiltin.kt | 58 +++++++++++++++---- 4 files changed, 50 insertions(+), 15 deletions(-) diff --git a/SKIE/compiler/kotlin-plugin/src/kgp_common/kotlin/co/touchlab/skie/context/SirPhaseContext.kt b/SKIE/compiler/kotlin-plugin/src/kgp_common/kotlin/co/touchlab/skie/context/SirPhaseContext.kt index cd52ff61..cdbd76d1 100644 --- a/SKIE/compiler/kotlin-plugin/src/kgp_common/kotlin/co/touchlab/skie/context/SirPhaseContext.kt +++ b/SKIE/compiler/kotlin-plugin/src/kgp_common/kotlin/co/touchlab/skie/context/SirPhaseContext.kt @@ -28,7 +28,7 @@ class SirPhaseContext( override val oirProvider: OirProvider = OirProvider(kirProvider.skieModule, extraDescriptorBuiltins, kirProvider, namer) - override val sirProvider: SirProvider = SirProvider(framework, kirProvider, configurationProvider) + override val sirProvider: SirProvider = SirProvider(framework, kirProvider, configurationProvider, skieConfiguration) override val kirBuiltins: KirBuiltins = kirProvider.kirBuiltins diff --git a/SKIE/compiler/kotlin-plugin/src/kgp_common/kotlin/co/touchlab/skie/kir/builtin/KirBuiltins.kt b/SKIE/compiler/kotlin-plugin/src/kgp_common/kotlin/co/touchlab/skie/kir/builtin/KirBuiltins.kt index 50684b49..9868ae65 100644 --- a/SKIE/compiler/kotlin-plugin/src/kgp_common/kotlin/co/touchlab/skie/kir/builtin/KirBuiltins.kt +++ b/SKIE/compiler/kotlin-plugin/src/kgp_common/kotlin/co/touchlab/skie/kir/builtin/KirBuiltins.kt @@ -26,7 +26,7 @@ class KirBuiltins( private val extraDescriptorBuiltins: ExtraDescriptorBuiltins, private val namer: ObjCExportNamer, ) { - // TODO Builtin methods are not supported yet + // TODO Not all Builtin methods are not supported yet (supported are only those converted from Kotlin equivalents in Any) val Base by Class( name = namer.kotlinAnyName, diff --git a/SKIE/compiler/kotlin-plugin/src/kgp_common/kotlin/co/touchlab/skie/sir/SirProvider.kt b/SKIE/compiler/kotlin-plugin/src/kgp_common/kotlin/co/touchlab/skie/sir/SirProvider.kt index ba7176e0..c3a74373 100644 --- a/SKIE/compiler/kotlin-plugin/src/kgp_common/kotlin/co/touchlab/skie/sir/SirProvider.kt +++ b/SKIE/compiler/kotlin-plugin/src/kgp_common/kotlin/co/touchlab/skie/sir/SirProvider.kt @@ -26,6 +26,7 @@ class SirProvider( framework: FrameworkLayout, private val kirProvider: KirProvider, private val configurationProvider: ConfigurationProvider, + skieConfiguration: SkieConfiguration, ) { val kotlinModule: SirModule.Kotlin = SirModule.Kotlin(framework.moduleName) @@ -33,7 +34,7 @@ class SirProvider( val skieModule: SirModule.Skie = SirModule.Skie(framework.moduleName) val sirBuiltins by lazy { - SirBuiltins(this) + SirBuiltins(this, skieConfiguration) } private val externalModuleCache = mutableMapOf() diff --git a/SKIE/compiler/kotlin-plugin/src/kgp_common/kotlin/co/touchlab/skie/sir/builtin/SirBuiltin.kt b/SKIE/compiler/kotlin-plugin/src/kgp_common/kotlin/co/touchlab/skie/sir/builtin/SirBuiltin.kt index 601ca044..f5dc684b 100644 --- a/SKIE/compiler/kotlin-plugin/src/kgp_common/kotlin/co/touchlab/skie/sir/builtin/SirBuiltin.kt +++ b/SKIE/compiler/kotlin-plugin/src/kgp_common/kotlin/co/touchlab/skie/sir/builtin/SirBuiltin.kt @@ -1,5 +1,7 @@ package co.touchlab.skie.sir.builtin +import co.touchlab.skie.configuration.SkieConfiguration +import co.touchlab.skie.configuration.SkieConfigurationFlag import co.touchlab.skie.sir.SirProvider import co.touchlab.skie.sir.element.SirClass import co.touchlab.skie.sir.element.SirDeclarationParent @@ -15,13 +17,14 @@ import kotlin.reflect.KProperty @Suppress("PropertyName", "FunctionName") class SirBuiltins( sirProvider: SirProvider, + skieConfiguration: SkieConfiguration, ) { val Swift = Modules.Swift(sirProvider) val Foundation = Modules.Foundation(sirProvider, Swift) - val Skie = Modules.Skie(sirProvider.skieModule) + val Skie = Modules.Skie(skieConfiguration, sirProvider.skieModule) object Modules { @@ -102,6 +105,7 @@ class SirBuiltins( } class Skie( + private val skieConfiguration: SkieConfiguration, override val module: SirModule.Skie, ) : ModuleBase() { @@ -109,45 +113,63 @@ class SirBuiltins( // The SkieSwiftFlow classes are only stubs (correct super types, and content are currently not needed) - val SkieSwiftFlow by Class { + val SkieSwiftFlow by RuntimeClass { SirTypeParameter("T") } - val SkieSwiftSharedFlow by Class { + val SkieSwiftSharedFlow by RuntimeClass { SirTypeParameter("T") } - val SkieSwiftMutableSharedFlow by Class { + val SkieSwiftMutableSharedFlow by RuntimeClass { SirTypeParameter("T") } - val SkieSwiftStateFlow by Class { + val SkieSwiftStateFlow by RuntimeClass { SirTypeParameter("T") } - val SkieSwiftMutableStateFlow by Class { + val SkieSwiftMutableStateFlow by RuntimeClass { SirTypeParameter("T") } - val SkieSwiftOptionalFlow by Class { + val SkieSwiftOptionalFlow by RuntimeClass { SirTypeParameter("T") } - val SkieSwiftOptionalSharedFlow by Class { + val SkieSwiftOptionalSharedFlow by RuntimeClass { SirTypeParameter("T") } - val SkieSwiftOptionalMutableSharedFlow by Class { + val SkieSwiftOptionalMutableSharedFlow by RuntimeClass { SirTypeParameter("T") } - val SkieSwiftOptionalStateFlow by Class { + val SkieSwiftOptionalStateFlow by RuntimeClass { SirTypeParameter("T") } - val SkieSwiftOptionalMutableStateFlow by Class { + val SkieSwiftOptionalMutableStateFlow by RuntimeClass { SirTypeParameter("T") } + + private fun RuntimeClass( + superTypes: List = emptyList(), + parent: SirDeclarationParent = module, + nameOverride: String? = null, + apply: (SirClass.() -> Unit) = { }, + ): PropertyDelegateProvider> = + if (SkieConfigurationFlag.Feature_CoroutinesInterop in skieConfiguration.enabledConfigurationFlags) { + ClassDeclarationPropertyProvider( + kind = SirClass.Kind.Class, + parent = parent, + superTypes = superTypes, + nameOverride = nameOverride, + apply = apply, + ) + } else { + NoClassDeclarationPropertyProvider + } } abstract class ModuleBase { @@ -201,7 +223,7 @@ class SirBuiltins( typeFactory = typeFactory, ) - inner class ClassDeclarationPropertyProvider( + protected inner class ClassDeclarationPropertyProvider( private val kind: SirClass.Kind, private val parent: SirDeclarationParent, private val superTypes: List = emptyList(), @@ -219,6 +241,18 @@ class SirBuiltins( ) } + protected object NoClassDeclarationPropertyProvider : PropertyDelegateProvider> { + + override fun provideDelegate(thisRef: Any?, property: KProperty<*>): ReadOnlyProperty = + NoClassDeclarationProperty + + private object NoClassDeclarationProperty : ReadOnlyProperty { + + override fun getValue(thisRef: Any?, property: KProperty<*>): SirClass? = + null + } + } + private inner class ClassDeclarationProperty( name: String, kind: SirClass.Kind,