Skip to content

Commit

Permalink
Refactor Origin usage for Oir and Sir classes.
Browse files Browse the repository at this point in the history
  • Loading branch information
FilipDolnik committed Nov 22, 2023
1 parent 99935b4 commit 13e1dcc
Show file tree
Hide file tree
Showing 14 changed files with 36 additions and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,6 @@ class KirProvider(

private lateinit var fqNameCache: Map<String, KirClass>

private lateinit var oirClassCache: Map<OirClass, KirClass>

private lateinit var sirClassCache: Map<SirClass, KirClass>

private lateinit var descriptorsToCallableDeclarationsCache: Map<CallableMemberDescriptor, KirCallableDeclaration<*>>

private lateinit var sirToCallableDeclarationsCache: Map<SirCallableDeclaration, KirCallableDeclaration<*>>
Expand Down Expand Up @@ -124,14 +120,6 @@ class KirProvider(
kirClass.classes.forEach { cacheClassesRecursively(it, visitedClasses) }
}

fun initializeOirClassCache() {
oirClassCache = allClasses.associateBy { it.oirClass }
}

fun initializeSirClassCache() {
sirClassCache = allClasses.associateBy { it.originalSirClass }
}

fun initializeCallableDeclarationsCache() {
descriptorsToCallableDeclarationsCache = allClasses.flatMap { it.callableDeclarations }.associateBy { it.descriptor }

Expand All @@ -158,14 +146,6 @@ class KirProvider(
findClass(sourceFile)
?: error("Class not found: $sourceFile. This error usually means that the class is not exposed to Objective-C.")

fun getClass(oirClass: OirClass): KirClass =
findClass(oirClass)
?: error("Class not found: $oirClass. This error usually means that the class is not exposed to Objective-C.")

fun getClass(sirClass: SirClass): KirClass =
findClass(sirClass)
?: error("Class not found: $sirClass. This error usually means that the class is not exposed to Objective-C.")

fun getClassByFqName(fqName: String): KirClass =
findClassByFqName(fqName)
?: error("Class not found: $fqName. This error usually means that the class is not exposed to Objective-C.")
Expand All @@ -184,12 +164,6 @@ class KirProvider(
fun findClass(sourceFile: SourceFile): KirClass? =
fileCache[sourceFile]

fun findClass(oirClass: OirClass): KirClass? =
oirClassCache[oirClass]

fun findClass(sirClass: SirClass): KirClass? =
sirClassCache[sirClass]

fun findClassByFqName(fqName: String): KirClass? =
fqNameCache[fqName]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import co.touchlab.skie.oir.element.OirClass
import co.touchlab.skie.oir.element.OirFile
import co.touchlab.skie.oir.element.OirModule
import co.touchlab.skie.phases.oir.CreateOirTypesPhase
import co.touchlab.skie.sir.element.SirClass
import org.jetbrains.kotlin.backend.konan.objcexport.ObjCExportNamer
import org.jetbrains.kotlin.descriptors.ClassDescriptor
import org.jetbrains.kotlin.descriptors.isInterface
Expand All @@ -29,8 +28,6 @@ class OirProvider(
lateinit var allKotlinClassesAndProtocols: List<OirClass>
private set

private lateinit var sirClassCache: Map<SirClass, OirClass>

val allExternalClasses: List<OirClass>
get() = allExternalClassesAndProtocols.filter { it.kind == OirClass.Kind.Class }

Expand Down Expand Up @@ -104,11 +101,4 @@ class OirProvider(
allKotlinClasses = allKotlinClassesAndProtocols.filter { it.kind == OirClass.Kind.Class }
allKotlinProtocols = allKotlinClassesAndProtocols.filter { it.kind == OirClass.Kind.Protocol }
}

fun initializeSirClassCache() {
sirClassCache = allClassesAndProtocols.associateBy { it.originalSirClass }
}

fun findClass(sirClass: SirClass): OirClass? =
sirClassCache[sirClass]
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ import co.touchlab.skie.phases.sir.type.CreateExternalSirTypesPhase
import co.touchlab.skie.phases.sir.type.CreateKotlinSirTypesPhase
import co.touchlab.skie.phases.sir.type.CreateStableNameTypeAliasesPhase
import co.touchlab.skie.phases.sir.type.FixNamesOfInaccessibleNestedClassesPhase
import co.touchlab.skie.phases.sir.type.InitializeSirTypesCachePhase
import co.touchlab.skie.phases.sir.type.InitializeSirTypesSuperTypesForOirPhase
import co.touchlab.skie.phases.swift.CompileSwiftPhase
import co.touchlab.skie.phases.swift.GenerateSirFileCodePhase
Expand Down Expand Up @@ -125,7 +124,6 @@ class SkiePhaseScheduler {

CreateKotlinSirTypesPhase(context),
CreateExternalSirTypesPhase,
InitializeSirTypesCachePhase,
InitializeSirTypesSuperTypesForOirPhase,
ConfigureExternalOirTypesBridgingPhase(context),

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import co.touchlab.skie.sir.element.SirSimpleFunction
import co.touchlab.skie.sir.element.SirTypeAlias
import co.touchlab.skie.sir.element.SirVisibility
import co.touchlab.skie.sir.element.isExported
import co.touchlab.skie.sir.element.oirClassOrNull
import co.touchlab.skie.sir.getExtension

object ExhaustiveEnumsGenerator : SirPhase {
Expand Down Expand Up @@ -110,14 +111,13 @@ private fun SirClass.addEnumCases(enum: KirClass) {
}
}

context(SirPhase.Context)
private fun SirClass.addNestedClassTypeAliases(enum: KirClass) {
enum.originalSirClass.declarations
.filterIsInstance<SirClass>()
.forEach { nestedClass ->
addNestedClassTypeAlias(nestedClass)

oirProvider.findClass(nestedClass)?.bridgedSirClass?.let { addNestedClassTypeAlias(it) }
nestedClass.oirClassOrNull?.bridgedSirClass?.let { addNestedClassTypeAlias(it) }
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,17 @@ package co.touchlab.skie.phases.features.functions

import co.touchlab.skie.kir.element.KirCallableDeclaration
import co.touchlab.skie.kir.element.KirClass
import co.touchlab.skie.oir.element.kirClassOrNull
import co.touchlab.skie.phases.SirPhase
import co.touchlab.skie.sir.element.SirCallableDeclaration
import co.touchlab.skie.sir.element.SirClass
import co.touchlab.skie.sir.element.SirConditionalConstraint
import co.touchlab.skie.sir.element.SirDeclarationParent
import co.touchlab.skie.sir.element.SirExtension
import co.touchlab.skie.sir.element.SirFile
import co.touchlab.skie.sir.element.SirSimpleFunction
import co.touchlab.skie.sir.element.receiverDeclaration
import co.touchlab.skie.sir.element.resolveAsKirClass
import co.touchlab.skie.sir.element.resolveAsSirClass
import co.touchlab.skie.sir.element.resolveAsSirClassType
import co.touchlab.skie.sir.type.DeclaredSirType
import co.touchlab.skie.sir.type.NullableSirType
import co.touchlab.skie.sir.type.OirDeclaredSirType
import co.touchlab.skie.sir.type.SirDeclaredSirType
Expand Down Expand Up @@ -109,8 +108,8 @@ class FileScopeConversionParentProvider(

private fun getExtensionReceiverKirClassIfExists(parentType: SirType): KirClass? =
when (parentType) {
is SirDeclaredSirType -> parentType.declaration.resolveAsSirClass()?.let { context.kirProvider.findClass(it) }
is OirDeclaredSirType -> context.kirProvider.findClass(parentType.declaration)
is SirDeclaredSirType -> parentType.declaration.resolveAsKirClass()
is OirDeclaredSirType -> parentType.declaration.kirClassOrNull
is NullableSirType -> getExtensionReceiverKirClassIfExists(parentType.type)
else -> null
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import co.touchlab.skie.oir.element.OirProperty
import co.touchlab.skie.oir.element.OirScope
import co.touchlab.skie.oir.element.OirSimpleFunction
import co.touchlab.skie.oir.element.OirValueParameter
import co.touchlab.skie.oir.element.kirClassOrNull
import co.touchlab.skie.oir.type.translation.OirTypeParameterScope
import co.touchlab.skie.oir.type.translation.typeParameterScope
import co.touchlab.skie.phases.SirPhase
Expand Down Expand Up @@ -201,5 +202,5 @@ class CreateOirMembersPhase(
}

private val OirClass.genericsScope: OirTypeParameterScope
get() = kirProvider.getClass(this).typeParameterScope
get() = this.kirClassOrNull?.typeParameterScope ?: error("OirClass $this does not originate from KirClass.")
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ class CreateOirTypesPhase(

initializeSuperTypesForAllClasses()

kirProvider.initializeOirClassCache()

oirProvider.initializeKotlinClassCache()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,10 @@ class CreateKotlinSirTypesPhase(
private val sirProvider = context.sirProvider
private val sirBuiltins = context.sirBuiltins

context(SirPhase.Context)
override fun execute() {
createClasses()

kirProvider.initializeSirClassCache()
}

private val kirToSirClasses = mutableMapOf<KirClass, SirClass>()

private fun createClasses() {
context(SirPhase.Context)
override fun execute() {
kirProvider.allClasses.forEach(::getOrCreateClass)
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import co.touchlab.skie.sir.element.SirClass
import co.touchlab.skie.sir.element.SirTypeDeclaration
import co.touchlab.skie.sir.element.SirVisibility
import co.touchlab.skie.sir.element.isRemoved
import co.touchlab.skie.sir.element.kirClassOrNull
import co.touchlab.skie.sir.element.oirClassOrNull
import co.touchlab.skie.sir.element.resolveAsKirClass
import co.touchlab.skie.util.resolveCollisionWithWarning
import org.jetbrains.kotlin.descriptors.ClassDescriptor
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameSafe
Expand All @@ -16,7 +19,7 @@ object RenameTypesConflictsWithOtherTypesPhase : SirPhase {

context(SirPhase.Context)
override fun execute() {
val sortedTypeDeclarations = sirProvider.allLocalTypeDeclarations.sortedWith(getCollisionResolutionPriorityComparator())
val sortedTypeDeclarations = sirProvider.allLocalTypeDeclarations.sortedWith(collisionResolutionPriorityComparator)

buildUniqueSignatureSet(sortedTypeDeclarations)
}
Expand All @@ -32,9 +35,8 @@ object RenameTypesConflictsWithOtherTypesPhase : SirPhase {
* Kotlin fqName if available
* Kotlin SirClasses with shorter Obj-C names are prioritized
*/
context(SirPhase.Context)
private fun getCollisionResolutionPriorityComparator(): Comparator<SirTypeDeclaration> =
compareBy<SirTypeDeclaration> {
private val collisionResolutionPriorityComparator: Comparator<SirTypeDeclaration>
get() = compareBy<SirTypeDeclaration> {
it.fqName.depth
}
.thenBy { it.isRemoved }
Expand All @@ -49,19 +51,15 @@ object RenameTypesConflictsWithOtherTypesPhase : SirPhase {
}
}
.thenByDescending { it is SirClass }
.thenByDescending { it.getKirClassOrNull() != null }
.thenByDescending { it.getKirClassOrNull()?.kind != KirClass.Kind.File }
.thenBy { it.getKirClassOrNull()?.kotlinClassNestingLevel ?: 0 }
.thenBy { it.getKirClassOrNull()?.kotlinName ?: "" }
.thenByDescending { it.resolveAsKirClass() != null }
.thenByDescending { it.resolveAsKirClass()?.kind != KirClass.Kind.File }
.thenBy { it.resolveAsKirClass()?.kotlinClassNestingLevel ?: 0 }
.thenBy { it.resolveAsKirClass()?.kotlinName ?: "" }
.thenBy { it.getOirClassOrNull()?.name?.length ?: Int.MAX_VALUE }

private val SirFqName.depth: Int
get() = 1 + (this.parent?.depth ?: 0)

context(SirPhase.Context)
private fun SirTypeDeclaration.getKirClassOrNull(): KirClass? =
(this as? SirClass)?.let { kirProvider.findClass(it) }

private val KirClass.kotlinName: String
get() = when (val descriptor = this.descriptor) {
is KirClass.Descriptor.Class -> descriptor.value.fqNameSafe.asString()
Expand All @@ -77,9 +75,8 @@ object RenameTypesConflictsWithOtherTypesPhase : SirPhase {
private val ClassDescriptor.kotlinClassNestingLevel: Int
get() = 1 + ((this.containingDeclaration as? ClassDescriptor)?.kotlinClassNestingLevel ?: 0)

context(SirPhase.Context)
private fun SirTypeDeclaration.getOirClassOrNull(): OirClass? =
(this as? SirClass)?.let { oirProvider.findClass(it) }
(this as? SirClass)?.oirClassOrNull

context(SirPhase.Context)
private fun buildUniqueSignatureSet(typeDeclarations: List<SirTypeDeclaration>) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import co.touchlab.skie.configuration.ConfigurationProvider
import co.touchlab.skie.configuration.SkieConfiguration
import co.touchlab.skie.kir.KirProvider
import co.touchlab.skie.oir.element.OirClass
import co.touchlab.skie.oir.element.cinteropClassDescriptorOrNull
import co.touchlab.skie.phases.SirPhase
import co.touchlab.skie.sir.builtin.SirBuiltins
import co.touchlab.skie.sir.element.SirCallableDeclaration
Expand Down Expand Up @@ -121,9 +122,9 @@ class SirProvider(
?: error("SirClass with fqName $fqName not found.")

fun findExternalModule(oirClass: OirClass): SirModule? {
val origin = oirClass.origin as? OirClass.Origin.CinteropType ?: error("Invalid origin for OirClass: $oirClass")
val classDescriptor = oirClass.cinteropClassDescriptorOrNull ?: error("Invalid origin for OirClass: $oirClass")

val moduleName = configurationProvider.getConfiguration(origin.classDescriptor)[ClassInterop.CInteropFrameworkName] ?: return null
val moduleName = configurationProvider.getConfiguration(classDescriptor)[ClassInterop.CInteropFrameworkName] ?: return null

return getExternalModule(moduleName)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package co.touchlab.skie.sir.element

import co.touchlab.skie.kir.element.KirClass
import co.touchlab.skie.oir.element.OirClass
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
Expand Down Expand Up @@ -154,3 +155,6 @@ val SirClass.oirClassOrNull: OirClass?
SirClass.Origin.Generated -> null
SirClass.Origin.ExternalSwiftFramework -> null
}

val SirClass.kirClassOrNull: KirClass?
get() = oirClassOrNull?.kirClassOrNull
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package co.touchlab.skie.sir.element

import co.touchlab.skie.kir.element.KirClass
import co.touchlab.skie.sir.SirFqName
import co.touchlab.skie.sir.type.DeclaredSirType
import co.touchlab.skie.sir.type.SirDeclaredSirType
Expand Down Expand Up @@ -76,3 +77,6 @@ fun SirTypeDeclaration.resolveAsSirClass(): SirClass? =
}
}
}

fun SirTypeDeclaration.resolveAsKirClass(): KirClass? =
resolveAsSirClass()?.kirClassOrNull
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ import co.touchlab.skie.kir.element.KirEnumEntry
import co.touchlab.skie.kir.element.classDescriptorOrNull
import co.touchlab.skie.phases.SirPhase
import co.touchlab.skie.sir.element.SirCallableDeclaration
import co.touchlab.skie.sir.element.SirClass
import co.touchlab.skie.sir.element.SirConstructor
import co.touchlab.skie.sir.element.SirEnumCase
import co.touchlab.skie.sir.element.SirProperty
import co.touchlab.skie.sir.element.SirSimpleFunction
import co.touchlab.skie.sir.element.SirTypeDeclaration
import co.touchlab.skie.sir.element.kirClassOrNull
import co.touchlab.skie.sir.element.resolveAsKirClass
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor

context(SirPhase.Context)
Expand Down Expand Up @@ -57,7 +58,7 @@ fun SirEnumCase.resolveCollisionWithWarning(collisionReasonProvider: SirEnumCase
collisionReasonProvider = collisionReasonProvider,
rename = { simpleName += "_" },
getName = { parent.fqName.toLocalString() + "." + simpleName },
findKirElement = { kirProvider.findClass(parent)?.enumEntries?.get(index) },
findKirElement = { parent.kirClassOrNull?.enumEntries?.get(index) },
getDescriptor = { descriptor },
)

Expand Down Expand Up @@ -92,7 +93,7 @@ private inline fun <T : SirTypeDeclaration> T.resolveCollisionWithWarning(
collisionReasonProvider = collisionReasonProvider,
rename = rename,
getName = { fqName.toLocalString() },
findKirElement = { (this as? SirClass)?.let { kirProvider.findClass(it) } },
findKirElement = { resolveAsKirClass() },
getDescriptor = { classDescriptorOrNull },
)

Expand Down

0 comments on commit 13e1dcc

Please sign in to comment.