Skip to content

Commit

Permalink
Remove Hashable conformance from sealed interfaces that have child …
Browse files Browse the repository at this point in the history
…interfaces.
  • Loading branch information
FilipDolnik committed Sep 25, 2023
1 parent 7ed8df0 commit 43d8aec
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 18 deletions.
2 changes: 1 addition & 1 deletion SKIE/acceptance-tests
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package co.touchlab.skie.phases

import co.touchlab.skie.configuration.ConfigurationContainer
import co.touchlab.skie.configuration.SkieConfiguration
import co.touchlab.skie.kir.DescriptorProvider
import co.touchlab.skie.phases.analytics.performance.SkiePerformanceAnalytics
Expand All @@ -23,15 +24,15 @@ interface SkiePhase<C : SkiePhase.Context> {
context(C)
fun execute()

interface Context {
interface Context : ConfigurationContainer {

val context: Context
override val context: Context

val skiePhaseScheduler: SkiePhaseScheduler

val compilerConfiguration: CompilerConfiguration

val skieConfiguration: SkieConfiguration
override val skieConfiguration: SkieConfiguration

val swiftCompilerConfiguration: SwiftCompilerConfiguration

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,16 @@ class SealedEnumGeneratorDelegate(
override val context: SirPhase.Context,
) : SealedGeneratorExtensionContainer {

context(SwiftModelScope)
context(SirPhase.Context)
fun generate(swiftModel: KotlinClassSwiftModel): SirClass {
val enum = SirClass(
simpleName = "__Sealed",
kind = SirClass.Kind.Enum,
parent = sirProvider.getSkieNamespace(swiftModel),
superTypes = listOf(sirBuiltins.Swift.Hashable.defaultType),
)

enum.addConformanceToHashableIfPossible(swiftModel)

enum.copyTypeParametersFrom(swiftModel.primarySirClass)

enum.addSealedEnumCases(swiftModel)
Expand All @@ -32,6 +33,13 @@ class SealedEnumGeneratorDelegate(
return enum
}

context(SirPhase.Context)
private fun SirClass.addConformanceToHashableIfPossible(swiftModel: KotlinClassSwiftModel) {
if (swiftModel.areAllExposedChildrenHashable) {
this.superTypes.add(sirBuiltins.Swift.Hashable.defaultType)
}
}

context(SwiftModelScope)
private fun SirClass.addSealedEnumCases(swiftModel: KotlinClassSwiftModel) {
val preferredNamesCollide = swiftModel.enumCaseNamesBasedOnKotlinIdentifiersCollide
Expand Down Expand Up @@ -67,3 +75,6 @@ class SealedEnumGeneratorDelegate(
)
}
}

private val KotlinClassSwiftModel.areAllExposedChildrenHashable: Boolean
get() = exposedSealedSubclasses.all { it.primarySirClass.isHashable }
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class SealedInteropGenerator(
private val KotlinClassSwiftModel.isSealedInteropEnabled: Boolean
get() = this.getConfiguration(SealedInterop.Enabled)

context(MutableSwiftModelScope)
context(SirPhase.Context)
private fun generate(swiftModel: KotlinClassSwiftModel) {
val enum = sealedEnumGeneratorDelegate.generate(swiftModel)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import co.touchlab.skie.sir.builtin.SirBuiltins
import co.touchlab.skie.sir.element.SirClass
import co.touchlab.skie.sir.element.SirTypeAlias
import co.touchlab.skie.sir.element.SirTypeParameter
import co.touchlab.skie.sir.element.superClass
import co.touchlab.skie.sir.type.DeclaredSirType
import co.touchlab.skie.swiftmodel.SwiftExportScope
import co.touchlab.skie.swiftmodel.SwiftGenericExportScope
import co.touchlab.skie.swiftmodel.SwiftModelScope
import co.touchlab.skie.swiftmodel.type.FlowMappingStrategy
import co.touchlab.skie.swiftmodel.type.translation.SwiftTypeTranslator
Expand Down Expand Up @@ -50,7 +52,7 @@ class KotlinSirClassFactory(
kind = if (classDescriptor.kind.isInterface) SirClass.Kind.Protocol else SirClass.Kind.Class,
)

if (sirClass.kind != SirClass.Kind.Protocol) {
if (sirClass.kind == SirClass.Kind.Class) {
classDescriptor.typeConstructor.parameters.forEach { typeParameter ->
SirTypeParameter(
name = typeParameter.name.asString(),
Expand Down Expand Up @@ -81,16 +83,7 @@ class KotlinSirClassFactory(

context(SwiftModelScope)
private fun SirClass.initializeSuperTypes(classDescriptor: ClassDescriptor) {
val genericExportScope = co.touchlab.skie.swiftmodel.SwiftGenericExportScope.Class(classDescriptor, typeParameters)

val directlyInheritsAny = classDescriptor.defaultType
.constructor
.supertypes
.any { KotlinBuiltIns.isAnyOrNullableAny(it) }

if (directlyInheritsAny) {
superTypes.add(sirBuiltins.Stdlib.Base.defaultType)
}
val genericExportScope = SwiftGenericExportScope.Class(classDescriptor, typeParameters)

val superTypesWithoutAny = classDescriptor.defaultType
.constructor
Expand All @@ -101,6 +94,10 @@ class KotlinSirClassFactory(
}
.filterIsInstance<DeclaredSirType>()

if (this.kind == SirClass.Kind.Class && this.superClass == null) {
superTypes.add(sirBuiltins.Stdlib.Base.defaultType)
}

superTypes.addAll(superTypesWithoutAny)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ val SirClass.superClassType: DeclaredSirType?
get() = superTypes.map { it.resolveAsDirectClassSirType() }
.firstOrNull { (it?.declaration as? SirClass)?.kind == SirClass.Kind.Class }

val SirClass.superClass: SirClass?
get() = superClassType?.declaration as? SirClass

fun DeclaredSirType.resolveAsDirectClassSirType(): DeclaredSirType? =
when (declaration) {
is SirClass -> this
Expand Down

0 comments on commit 43d8aec

Please sign in to comment.