Skip to content

Commit

Permalink
Add support for SirTypes of declarations nested in generic classes.
Browse files Browse the repository at this point in the history
  • Loading branch information
FilipDolnik committed May 30, 2024
1 parent 1274849 commit d2b93b0
Show file tree
Hide file tree
Showing 16 changed files with 110 additions and 77 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import co.touchlab.skie.sir.element.SirClass
import co.touchlab.skie.sir.element.SirDeclarationNamespace
import co.touchlab.skie.sir.element.SirTypeAlias
import co.touchlab.skie.sir.element.copyTypeParametersFrom
import co.touchlab.skie.sir.element.toFqNameTypeFromEnclosingTypeParameters
import co.touchlab.skie.sir.element.toTypeFromEnclosingTypeParameters

// Needed due to a bug in Swift compiler that incorrectly resolves bridges nested in other declarations.
Expand Down Expand Up @@ -33,7 +34,7 @@ private fun SirClass.createReplacementTypeAlias(namespace: SirDeclarationNamespa
baseName = baseName,
parent = namespace,
typeFactory = { typeAlias ->
this.toTypeFromEnclosingTypeParameters(typeAlias.typeParameters).withFqName()
this.toFqNameTypeFromEnclosingTypeParameters(typeAlias.typeParameters)
},
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ private fun createStableNameTypeAliasIfRequested(bridgedEnum: SirClass, kirClass
isReplaced = true,
isHidden = true,
) {
bridgedEnum.defaultType.withFqName()
bridgedEnum.toFqNameType()
}

if (SkieConfigurationFlag.Debug_UseStableTypeAliases.isEnabled) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ class FlowConversionConstructorsGenerator(
)

bodyBuilder.add {
addStatement("return %T(%L)", to.kotlinClass.defaultType.toSwiftPoetDeclaredTypeName(), "flow.delegate")
addStatement("return %T(%L)", to.kotlinClass.internalName.toSwiftPoetDeclaredTypeName(), "flow.delegate")
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import co.touchlab.skie.sir.element.SirClass
import co.touchlab.skie.sir.element.isAccessibleFromOtherModules
import co.touchlab.skie.sir.element.superClassType
import co.touchlab.skie.sir.element.toTypeParameterUsage
import co.touchlab.skie.sir.type.SirDeclaredSirType
import co.touchlab.skie.sir.type.SirType
import co.touchlab.skie.sir.type.TypeParameterUsageSirType
import co.touchlab.skie.util.swift.toValidSwiftIdentifier
Expand Down Expand Up @@ -53,12 +52,8 @@ interface SealedGeneratorExtensionContainer {
val KirClass.visibleSealedSubclasses: List<KirClass>
get() = this.sealedSubclasses.filter { it.configuration[SealedInterop.Case.Visible] && it.primarySirClass.visibility.isAccessibleFromOtherModules }

fun SirClass.getSealedSubclassType(
enum: SirClass,
): SirType = SirDeclaredSirType(
declaration = this,
typeArguments = this.getTypeArgumentsForEnumCase(enum),
)
fun SirClass.getSealedSubclassType(enum: SirClass): SirType =
this.toType(this.getTypeArgumentsForEnumCase(enum))

private fun SirClass.getTypeArgumentsForEnumCase(
enum: SirClass,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class SkieClassSuspendGenerator {
returnType = skieClass.toTypeFromEnclosingTypeParameters(typeParameters)

bodyBuilder.add {
addCode("return %T(kotlinObject)", skieClass.defaultType.toSwiftPoetDeclaredTypeName())
addCode("return %T(kotlinObject)", skieClass.internalName.toSwiftPoetDeclaredTypeName())
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ object CreateStableNameTypeAliasesPhase : SirPhase {
isReplaced = true,
isHidden = true,
) {
kirClass.originalSirClass.defaultType.withFqName()
kirClass.originalSirClass.toFqNameType()
}

if (useStableTypeAliases && kirClass.originalSirClass.internalTypeAlias == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ object SirCodeGenerator {

private fun FileSpec.Builder.generateExtension(extension: SirExtension) {
addExtension(
ExtensionSpec.builder(extension.classDeclaration.defaultType.toSwiftPoetDeclaredTypeName())
ExtensionSpec.builder(extension.classDeclaration.internalName.toSwiftPoetDeclaredTypeName())
.addConditionalConstraints(extension)
.addExtensionDeclarations(extension)
.build(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package co.touchlab.skie.sir

import co.touchlab.skie.sir.element.SirModule
import io.outfoxx.swiftpoet.DeclaredTypeName

// TODO Needs to support type parameters for types nested in generic classes (DeclaredSirType also needs to support this)
class SirFqName private constructor(
val module: SirModule,
val simpleName: String,
Expand All @@ -18,6 +18,10 @@ class SirFqName private constructor(
override fun toString(): String =
module.name + "." + toLocalString()

fun toSwiftPoetDeclaredTypeName(): DeclaredTypeName =
parent?.toSwiftPoetDeclaredTypeName()?.nestedType(simpleName)
?: DeclaredTypeName.qualifiedTypeName(module.name + "." + simpleName)

override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import co.touchlab.skie.oir.element.kirClassOrNull
import co.touchlab.skie.sir.SirFqName
import co.touchlab.skie.sir.element.util.sirDeclarationParent
import co.touchlab.skie.sir.type.SirDeclaredSirType
import co.touchlab.skie.sir.type.SirType
import io.outfoxx.swiftpoet.FunctionSpec

class SirClass(
Expand Down Expand Up @@ -71,7 +72,10 @@ class SirClass(
* Name used by SKIE generated code in cases it cannot use fqName.
*/
override val internalName: SirFqName
get() = internalTypeAlias?.internalName ?: publicName
get() = internalTypeAlias?.internalName ?: fqName

override fun toType(typeArguments: List<SirType>): SirDeclaredSirType =
SirDeclaredSirType({ internalTypeAlias ?: this }, typeArguments = typeArguments)

override fun toReadableString(): String =
kind.toString().lowercase() + " " + fqName.toString()
Expand Down Expand Up @@ -165,7 +169,7 @@ val SirClass.superClass: SirClass?
get() = superClassType?.declaration as? SirClass

fun SirDeclaredSirType.resolveAsSirClassType(): SirDeclaredSirType? =
when (declaration) {
when (val declaration = declaration) {
is SirClass -> this
is SirTypeAlias -> {
when (val type = declaration.type) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import co.touchlab.skie.sir.SirFqName
import co.touchlab.skie.sir.element.util.sirDeclarationParent
import co.touchlab.skie.sir.type.DeclaredSirType
import co.touchlab.skie.sir.type.OirDeclaredSirType
import co.touchlab.skie.sir.type.SirDeclaredSirType
import co.touchlab.skie.sir.type.SirType

class SirTypeAlias(
Expand Down Expand Up @@ -56,6 +57,9 @@ class SirTypeAlias(
override fun toReadableString(): String =
"typealias $fqName"

override fun toType(typeArguments: List<SirType>): SirDeclaredSirType =
toFqNameType(typeArguments)

override fun toString(): String = "${this::class.simpleName}: $fqName"

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,26 @@ sealed interface SirTypeDeclaration : SirDeclarationWithVisibility {

val defaultType: DeclaredSirType

fun toType(typeArguments: List<SirType>): SirDeclaredSirType =
SirDeclaredSirType(this, typeArguments = typeArguments)
fun toType(typeArguments: List<SirType>): SirDeclaredSirType

fun toType(vararg typeArguments: SirType): SirDeclaredSirType =
toType(typeArguments.toList())

fun toFqNameType(typeArguments: List<SirType>): SirDeclaredSirType =
SirDeclaredSirType({ this }, typeArguments = typeArguments)

fun toFqNameType(vararg typeArguments: SirType): SirDeclaredSirType =
toFqNameType(typeArguments.toList())

fun toReadableString(): String
}

fun SirTypeDeclaration.toTypeFromEnclosingTypeParameters(typeParameters: List<SirTypeParameter>): DeclaredSirType =
toType(typeParameters.map { it.toTypeParameterUsage() })

fun SirTypeDeclaration.toFqNameTypeFromEnclosingTypeParameters(typeParameters: List<SirTypeParameter>): DeclaredSirType =
toFqNameType(typeParameters.map { it.toTypeParameterUsage() })

fun SirTypeDeclaration.resolveAsSirClass(): SirClass? =
when (this) {
is SirClass -> this
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -345,12 +345,14 @@ sealed class Signature {

return when (val typeWithoutTypeAliases = evaluatedType.type.inlineTypeAliases()) {
is SirDeclaredSirType -> {
check(typeWithoutTypeAliases.declaration is SirClass) {
val declaration = typeWithoutTypeAliases.declaration

check(declaration is SirClass) {
"TypeAliases should have been inlined in the above step. Was: $this"
}

Type.Class(
sirClass = typeWithoutTypeAliases.declaration,
sirClass = declaration,
typeArguments = typeWithoutTypeAliases.typeArguments.map { Type.TypeArgument(it) },
sirHierarchyCache = this@SirHierarchyCache,
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
package co.touchlab.skie.sir.type

sealed class DeclaredSirType : NonNullSirType() {

abstract val pointsToInternalName: Boolean

abstract fun withFqName(): DeclaredSirType
}
sealed class DeclaredSirType : NonNullSirType()
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import co.touchlab.skie.sir.element.SirTypeParameter
data class OirDeclaredSirType(
val declaration: OirClass,
private val typeArguments: List<OirType> = emptyList(),
override val pointsToInternalName: Boolean = true,
private val mapTypeArgument: (OirType, SirTypeParameter) -> SirType,
) : DeclaredSirType() {

Expand Down Expand Up @@ -46,15 +45,11 @@ data class OirDeclaredSirType(
}

return SirDeclaredSirType(
declaration = selectedClass,
declarationProvider = { selectedClass.internalTypeAlias ?: selectedClass },
typeArguments = convertedTypeArguments,
pointsToInternalName = pointsToInternalName,
)
}

override fun withFqName(): DeclaredSirType =
copy(pointsToInternalName = false)

override fun substituteTypeParameters(substitutions: Map<SirTypeParameter, SirTypeParameter>): OirDeclaredSirType =
this

Expand Down
Loading

0 comments on commit d2b93b0

Please sign in to comment.