Skip to content

Commit

Permalink
Fix naming of enums nested in enums.
Browse files Browse the repository at this point in the history
  • Loading branch information
FilipDolnik committed Oct 17, 2023
1 parent e7bb3a5 commit 88ae348
Show file tree
Hide file tree
Showing 12 changed files with 61 additions and 28 deletions.
2 changes: 1 addition & 1 deletion SKIE/acceptance-tests
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ class NativeDescriptorProvider(
classDescriptor.unsubstitutedInnerClassesScope
.getDescriptorsFiltered(DescriptorKindFilter.CLASSIFIERS)
.filterIsInstance<ClassDescriptor>()
.filter { it.kind == ClassKind.CLASS || it.kind == ClassKind.OBJECT }
.filter { it.kind == ClassKind.CLASS || it.kind == ClassKind.OBJECT || it.kind == ClassKind.ENUM_CLASS }
.filter { it.isExposable }

override fun getExposedEnumEntries(classDescriptor: ClassDescriptor): List<ClassDescriptor> =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ class SkiePhaseScheduler {
FixCallableMembersConflictsPhase,

FlowConversionConstructorsGenerator,
ExhaustiveEnumsGenerator.FunctionGeneratorPhase,
ExhaustiveEnumsGenerator.MembersGeneratorPhase,
SuspendGenerator.SwiftBridgeGeneratorPhase,

TemporarilyRenameTypesConflictingWithExternalModulesPhase,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ private fun SirClass.createReplacementTypeAlias(namespace: SirDeclarationNamespa
)

typeAlias.copyTypeParametersFrom(this)
if (publicTypeAlias == null) {
publicTypeAlias = typeAlias
}
if (internalTypeAlias == null) {
internalTypeAlias = typeAlias
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ object ApiNotesFactory {
ApiNotesType(
objCFqName = this.objCFqName.asString(),
bridgeFqName = this.bridgedSirClass?.fqName?.toLocalString(),
swiftFqName = this.kotlinSirClass.fqName.toLocalString(),
swiftFqName = this.kotlinSirClass.publicName.toLocalString(),
isHidden = this.visibility.isHiddenOrReplaced,
availability = this.visibility.availability,
methods = this.allDirectlyCallableMembers.filterIsInstance<KotlinFunctionSwiftModel>().map { it.toApiNote(this) },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ object ExhaustiveEnumsGenerator : SirPhase {
classSwiftModel.configureBridging(skieClass)
}

object FunctionGeneratorPhase : StatefulSirPhase()
object MembersGeneratorPhase : StatefulSirPhase()
}

private fun MutableKotlinClassSwiftModel.configureBridging(skieClass: SirClass) {
Expand Down Expand Up @@ -104,7 +104,6 @@ private fun KotlinClassSwiftModel.createBridgingEnum(): SirClass =
}

addEnumCases()
addNestedClassTypeAliases()

superTypes += listOf(
sirBuiltins.Swift.Hashable.defaultType,
Expand All @@ -114,11 +113,11 @@ private fun KotlinClassSwiftModel.createBridgingEnum(): SirClass =

attributes.add("frozen")

addCompanionObjectPropertyIfNeeded()
addObjcBridgeableImplementation(this@createBridgingEnum)

doInPhase(ExhaustiveEnumsGenerator.FunctionGeneratorPhase) {
doInPhase(ExhaustiveEnumsGenerator.MembersGeneratorPhase) {
addPassthroughForMembers()
addNestedClassTypeAliases()
addCompanionObjectPropertyIfNeeded()
addObjcBridgeableImplementation(this@createBridgingEnum)
}
}

Expand All @@ -131,17 +130,6 @@ private fun SirClass.addEnumCases() {
}
}

context(KotlinClassSwiftModel)
private fun SirClass.addNestedClassTypeAliases() {
nestedClasses.forEach { nestedClass ->
SirTypeAlias(
simpleName = nestedClass.primarySirClass.fqName.simpleName,
) {
DeclaredSirType(nestedClass.primarySirClass)
}
}
}

context(SirPhase.Context, KotlinClassSwiftModel)
private fun SirClass.addPassthroughForMembers() {
allAccessibleDirectlyCallableMembers
Expand All @@ -150,6 +138,24 @@ private fun SirClass.addPassthroughForMembers() {
}
}

context(KotlinClassSwiftModel)
private fun SirClass.addNestedClassTypeAliases() {
nestedClasses.forEach { nestedClass ->
addNestedClassTypeAlias(nestedClass.kotlinSirClass)
nestedClass.bridgedSirClass?.let { addNestedClassTypeAlias(it) }
}
}

context(KotlinClassSwiftModel)
private fun SirClass.addNestedClassTypeAlias(sirClass: SirClass) {
SirTypeAlias(
simpleName = sirClass.publicName.simpleName,
visibility = sirClass.visibility,
) {
sirClass.defaultType
}
}

context(KotlinClassSwiftModel)
private fun SirClass.addCompanionObjectPropertyIfNeeded() {
val companion = companionObject ?: return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,4 +200,4 @@ private fun CodeBlock.Builder.addFromObjectiveCBodyCase(
}

private val KotlinClassSwiftModel.fatalErrorFromObjectiveC: CodeBlock
get() = CodeBlock.of("""fatalError("Couldn't map value of \(Swift.String(describing: source)) to ${kotlinSirClass.fqName}")""")
get() = CodeBlock.of("""fatalError("Couldn't map value of \(Swift.String(describing: source)) to ${kotlinSirClass.publicName}")""")
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ interface SealedGeneratorExtensionContainer {
get() {
val configuredName = configurationProvider.getConfiguration(this, SealedInterop.Case.Name)

return configuredName ?: this.kotlinSirClass.fqName.toLocalString().toValidSwiftIdentifier()
return configuredName ?: this.kotlinSirClass.publicName.toLocalString().toValidSwiftIdentifier()
}

val KotlinClassSwiftModel.hasElseCase: Boolean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class SirClass(
override var visibility: SirVisibility = SirVisibility.Public,
superTypes: List<DeclaredSirType> = emptyList(),
attributes: List<String> = emptyList(),
var publicTypeAlias: SirTypeAlias? = null,
var internalTypeAlias: SirTypeAlias? = null,
var isInherentlyHashable: Boolean = false,
override var isPrimitive: Boolean = false,
Expand All @@ -33,18 +34,27 @@ class SirClass(

override val declarations: MutableList<SirDeclaration> = mutableListOf()

/**
* Name used to generate SKIE code.
*/
override val fqName: SirFqName
get() = super.fqName

override val originalFqName: SirFqName = fqName

/**
* Name that is expected to be used by external Swift code.
*/
override val publicName: SirFqName
get() = publicTypeAlias?.publicName ?: fqName

/**
* Name used by SKIE generated code to avoid many problems with ambiguous identifiers and bugs in Swift compiler.
*/
override val internalName: SirFqName
get() = internalTypeAlias?.fqName ?: fqName
get() = internalTypeAlias?.internalName ?: publicName

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

enum class Kind {
Class,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ class SirExtension(
override val fqName: SirFqName
get() = classDeclaration.fqName

val internalName: SirFqName
get() = classDeclaration.internalName

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ class SirTypeAlias(

override val originalFqName: SirFqName = fqName

override val publicName: SirFqName
get() = fqName

override val internalName: SirFqName
get() = fqName

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,37 @@ import co.touchlab.skie.sir.type.SirType

sealed interface SirTypeDeclaration : SirDeclaration {

/**
* Last component of fqName.
*/
var simpleName: String

var visibility: SirVisibility

override var parent: SirDeclarationParent

/**
* Base component of fqName.
*/
var namespace: SirDeclarationNamespace?
get() = parent as? SirDeclarationNamespace
set(value) {
parent = value ?: namespaceParent
}

/**
* Name used to generate SKIE code.
*/
val fqName: SirFqName
get() = namespace?.fqName?.nested(simpleName) ?: SirFqName(module, simpleName)

val originalFqName: SirFqName

/**
* Name that is expected to be used by external Swift code.
*/
val publicName: SirFqName

/**
* Name used by SKIE generated code to avoid many problems with ambiguous identifiers and bugs in Swift compiler.
*/
Expand Down

0 comments on commit 88ae348

Please sign in to comment.