Skip to content

Commit

Permalink
Introduce different types of SirFile.
Browse files Browse the repository at this point in the history
  • Loading branch information
FilipDolnik committed Mar 18, 2024
1 parent a9a3160 commit 3f45bb2
Show file tree
Hide file tree
Showing 37 changed files with 301 additions and 181 deletions.
2 changes: 1 addition & 1 deletion SKIE/acceptance-tests
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,7 @@ class SkieBuildDirectory(

val custom: Custom = Custom(this)

class Generated(parent: Directory) : PermanentDirectory(parent, "generated") {
}
class Generated(parent: Directory) : PermanentDirectory(parent, "generated")

class Custom(parent: Directory) : PermanentDirectory(parent, "custom")
}
Expand All @@ -99,9 +98,11 @@ class SkieBuildDirectory(

class ObjectFiles(parent: Directory) : PermanentDirectory(parent, "object-files") {

// WIP Replace
val allFiles: List<File>
get() = directory.walkTopDown().toList()

// WIP Replace
val allObjectFiles: List<File>
get() = allFiles.filter { it.extension == "o" }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ import co.touchlab.skie.oir.type.translation.OirTypeTranslator
import co.touchlab.skie.phases.SirPhase
import co.touchlab.skie.phases.SkiePhase
import co.touchlab.skie.phases.oir.util.ExternalApiNotesProvider
import co.touchlab.skie.sir.ClassNamespaceProvider
import co.touchlab.skie.sir.SirFileProvider
import co.touchlab.skie.sir.SirProvider
import co.touchlab.skie.sir.SkieNamespaceProvider
import co.touchlab.skie.sir.builtin.SirBuiltins
import co.touchlab.skie.sir.type.translation.SirTypeTranslator
import org.jetbrains.kotlin.backend.konan.objcexport.ObjCExportNamer
Expand All @@ -30,13 +31,15 @@ class SirPhaseContext(

override val sirProvider: SirProvider = SirProvider(framework, kirProvider, configurationProvider, skieConfiguration)

override val sirFileProvider: SirFileProvider = sirProvider.fileProvider

override val kirBuiltins: KirBuiltins = kirProvider.kirBuiltins

override val oirBuiltins: OirBuiltins = oirProvider.oirBuiltins

override val sirBuiltins: SirBuiltins = sirProvider.sirBuiltins

override val skieNamespaceProvider: SkieNamespaceProvider = SkieNamespaceProvider(
override val classNamespaceProvider: ClassNamespaceProvider = ClassNamespaceProvider(
kirProvider = kirProvider,
sirProvider = sirProvider,
mainModuleDescriptor = mainSkieContext.mainModuleDescriptor,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class OirExtension(
override val parent: OirTopLevelDeclarationParent,
) : OirTopLevelDeclaration, OirCallableDeclarationParent {

// WIP Should be set
lateinit var sirExtension: SirExtension

override val callableDeclarations: MutableList<OirCallableDeclaration> = mutableListOf()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import co.touchlab.skie.oir.builtin.OirBuiltins
import co.touchlab.skie.oir.type.translation.OirTypeTranslator
import co.touchlab.skie.phases.oir.util.ExternalApiNotesProvider
import co.touchlab.skie.sir.SirProvider
import co.touchlab.skie.sir.SkieNamespaceProvider
import co.touchlab.skie.sir.ClassNamespaceProvider
import co.touchlab.skie.sir.SirFileProvider
import co.touchlab.skie.sir.builtin.SirBuiltins
import co.touchlab.skie.sir.type.translation.SirTypeTranslator
import org.jetbrains.kotlin.backend.konan.objcexport.ObjCExportNamer
Expand All @@ -25,13 +26,15 @@ interface SirPhase : SkiePhase<SirPhase.Context> {

val sirProvider: SirProvider

val sirFileProvider: SirFileProvider

val kirBuiltins: KirBuiltins

val oirBuiltins: OirBuiltins

val sirBuiltins: SirBuiltins

val skieNamespaceProvider: SkieNamespaceProvider
val classNamespaceProvider: ClassNamespaceProvider

val externalApiNotesProvider: ExternalApiNotesProvider

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ import co.touchlab.skie.phases.other.DeclareMissingSymbolsPhase
import co.touchlab.skie.phases.other.DeleteSkieFrameworkContentPhase
import co.touchlab.skie.phases.other.DisableWildcardExportPhase
import co.touchlab.skie.phases.other.ExtraClassExportPhase
import co.touchlab.skie.phases.other.FixDuplicatedOverridenFunctionsPhase
import co.touchlab.skie.phases.other.FixDuplicatedOverriddenFunctionsPhase
import co.touchlab.skie.phases.other.FixLibrariesShortNamePhase
import co.touchlab.skie.phases.other.VerifyMinOSVersionPhase
import co.touchlab.skie.phases.other.VerifyNoBitcodeEmbeddingPhase
Expand All @@ -61,9 +61,9 @@ import co.touchlab.skie.phases.sir.type.CreateStableNameTypeAliasesPhase
import co.touchlab.skie.phases.sir.type.FixNamesOfInaccessibleNestedClassesPhase
import co.touchlab.skie.phases.sir.type.InitializeSirTypesSuperTypesForOirPhase
import co.touchlab.skie.phases.swift.CompileSwiftPhase
import co.touchlab.skie.phases.swift.GenerateSirFileCodePhase
import co.touchlab.skie.phases.swift.ConvertSirIrFilesToSourceFilesPhase
import co.touchlab.skie.phases.swift.SwiftCacheSetupPhase
import co.touchlab.skie.phases.swift.WriteSirFileContentToDiskPhase
import co.touchlab.skie.phases.swift.ConvertSirSourceFilesToCompilableFilesPhase
import co.touchlab.skie.phases.typeconflicts.RenameTypesConflictingWithKeywordsPhase
import co.touchlab.skie.phases.typeconflicts.RenameTypesConflictingWithKotlinModulePhase
import co.touchlab.skie.phases.typeconflicts.RenameTypesConflictsWithOtherTypesPhase
Expand Down Expand Up @@ -145,6 +145,7 @@ class SkiePhaseScheduler {

KotlinRuntimeHidingPhase,
SwiftRuntimeGenerator,
// WIP Load custom files

RenameConflictingCallableDeclarationsPhase,

Expand Down Expand Up @@ -181,7 +182,7 @@ class SkiePhaseScheduler {
RenameCallableDeclarationsConflictingWithTypeDeclarationsPhase,
RenameParametersNamedSelfPhase,
RenameConflictingCallableDeclarationsPhase,
FixDuplicatedOverridenFunctionsPhase,
FixDuplicatedOverriddenFunctionsPhase,
TemporarilyRenameTypesConflictingWithExternalModulesPhase,
FixOirFunctionSignaturesForApiNotesPhase(context),
CreateFakeObjCConstructorsPhase,
Expand All @@ -195,8 +196,8 @@ class SkiePhaseScheduler {
ApiNotesGenerationPhase.ForSwiftCompilation,
FixForwardDeclarationsPhase(context),
AddTypeDefPhase(context),
GenerateSirFileCodePhase,
WriteSirFileContentToDiskPhase,
ConvertSirIrFilesToSourceFilesPhase,
ConvertSirSourceFilesToCompilableFilesPhase,
GenerateFakeObjCDependenciesPhase,
DisableWildcardExportPhase,
SwiftCacheSetupPhase,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import co.touchlab.skie.phases.util.doInPhase
import co.touchlab.skie.sir.element.SirClass
import co.touchlab.skie.sir.element.SirEnumCase
import co.touchlab.skie.sir.element.SirExtension
import co.touchlab.skie.sir.element.SirFile
import co.touchlab.skie.sir.element.SirGetter
import co.touchlab.skie.sir.element.SirIrFile
import co.touchlab.skie.sir.element.SirProperty
import co.touchlab.skie.sir.element.SirScope
import co.touchlab.skie.sir.element.SirSimpleFunction
Expand Down Expand Up @@ -79,9 +79,9 @@ private fun createBridgingEnum(enumKirClass: KirClass): SirClass =
parent = enumKirClass.originalSirClass.namespace?.let { namespace ->
sirProvider.getExtension(
classDeclaration = namespace.classDeclaration,
parent = skieNamespaceProvider.getNamespaceFile(enumKirClass),
parent = classNamespaceProvider.getNamespaceFile(enumKirClass),
)
} ?: skieNamespaceProvider.getNamespaceFile(enumKirClass),
} ?: classNamespaceProvider.getNamespaceFile(enumKirClass),
kind = SirClass.Kind.Enum,
).apply {
addEnumCases(enumKirClass)
Expand Down Expand Up @@ -146,14 +146,14 @@ private fun SirClass.addCompanionObjectPropertyIfNeeded(enum: KirClass) {

context(SirPhase.Context)
private fun KirClass.addConversionExtensions(bridgedEnum: SirClass) {
skieNamespaceProvider.getNamespaceFile(this).apply {
classNamespaceProvider.getNamespaceFile(this).apply {
addToKotlinConversionExtension(originalSirClass, bridgedEnum)
addToSwiftConversionExtension(originalSirClass, bridgedEnum)
}
}

context(SirPhase.Context)
private fun SirFile.addToKotlinConversionExtension(enum: SirClass, bridgedEnum: SirClass) {
private fun SirIrFile.addToKotlinConversionExtension(enum: SirClass, bridgedEnum: SirClass) {
this.getExtension(
classDeclaration = bridgedEnum,
).apply {
Expand All @@ -173,7 +173,7 @@ private fun SirExtension.addToKotlinConversionMethod(enum: SirClass) {
}

context(SirPhase.Context)
private fun SirFile.addToSwiftConversionExtension(enum: SirClass, bridgedEnum: SirClass) {
private fun SirIrFile.addToSwiftConversionExtension(enum: SirClass, bridgedEnum: SirClass) {
this.getExtension(
classDeclaration = enum,
).apply {
Expand All @@ -200,7 +200,7 @@ private fun createStableNameTypeAliasIfRequested(bridgedEnum: SirClass, kirClass

SirTypeAlias(
baseName = "Enum",
parent = skieNamespaceProvider.getNamespace(kirClass),
parent = classNamespaceProvider.getNamespace(kirClass),
visibility = SirVisibility.PublicButReplaced,
) {
bridgedEnum.defaultType.withFqName()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import co.touchlab.skie.sir.element.SirClass
import co.touchlab.skie.sir.element.SirConditionalConstraint
import co.touchlab.skie.sir.element.SirConstructor
import co.touchlab.skie.sir.element.SirExtension
import co.touchlab.skie.sir.element.SirFile
import co.touchlab.skie.sir.element.SirIrFile
import co.touchlab.skie.sir.element.SirSimpleFunction
import co.touchlab.skie.sir.element.SirTypeParameter
import co.touchlab.skie.sir.element.SirValueParameter
Expand All @@ -28,34 +28,34 @@ class FlowConversionConstructorsGenerator(

context(SirPhase.Context)
override fun execute() {
val file = sirProvider.getSkieNamespaceFile("FlowConversions")
val file = sirFileProvider.getIrFileFromSkieNamespace("FlowConversions")

SupportedFlow.values().forEach {
it.generateAllConversions(file)
}
}

private fun SupportedFlow.generateAllConversions(file: SirFile) {
private fun SupportedFlow.generateAllConversions(file: SirIrFile) {
requiredVariant.generateAllConversions(file)
optionalVariant.generateAllConversions(file)
}

private fun SupportedFlow.Variant.generateAllConversions(file: SirFile) {
private fun SupportedFlow.Variant.generateAllConversions(file: SirIrFile) {
generateAllKotlinClassConversions(this, file)
generateAllSwiftClassConversions(this, file)
}

private fun generateAllKotlinClassConversions(variant: SupportedFlow.Variant, file: SirFile) {
private fun generateAllKotlinClassConversions(variant: SupportedFlow.Variant, file: SirIrFile) {
generateKotlinClassWithAnyObjectConversions(variant, file)
generateKotlinClassWithBridgeableConversions(variant, file)
}

private fun generateAllSwiftClassConversions(variant: SupportedFlow.Variant, file: SirFile) {
private fun generateAllSwiftClassConversions(variant: SupportedFlow.Variant, file: SirIrFile) {
generateSwiftClassWithAnyObjectConversions(variant, file)
generateSwiftClassWithBridgeableConversions(variant, file)
}

private fun generateKotlinClassWithAnyObjectConversions(variant: SupportedFlow.Variant, file: SirFile) {
private fun generateKotlinClassWithAnyObjectConversions(variant: SupportedFlow.Variant, file: SirIrFile) {
file.addConversions(variant) { from ->
addSwiftToKotlinConversion(
from,
Expand All @@ -65,7 +65,7 @@ class FlowConversionConstructorsGenerator(
}
}

private fun generateKotlinClassWithBridgeableConversions(variant: SupportedFlow.Variant, file: SirFile) {
private fun generateKotlinClassWithBridgeableConversions(variant: SupportedFlow.Variant, file: SirIrFile) {
file.addConversions(variant) { from ->
addSwiftToKotlinConversion(
from,
Expand All @@ -75,7 +75,7 @@ class FlowConversionConstructorsGenerator(
}
}

private fun generateSwiftClassWithAnyObjectConversions(variant: SupportedFlow.Variant, file: SirFile) {
private fun generateSwiftClassWithAnyObjectConversions(variant: SupportedFlow.Variant, file: SirIrFile) {
generateSwiftClassConversions(
variant,
file,
Expand All @@ -88,7 +88,7 @@ class FlowConversionConstructorsGenerator(
}
}

private fun generateSwiftClassWithBridgeableConversions(variant: SupportedFlow.Variant, file: SirFile) {
private fun generateSwiftClassWithBridgeableConversions(variant: SupportedFlow.Variant, file: SirIrFile) {
generateSwiftClassConversions(
variant,
file,
Expand All @@ -107,7 +107,7 @@ class FlowConversionConstructorsGenerator(

private fun generateSwiftClassConversions(
variant: SupportedFlow.Variant,
file: SirFile,
file: SirIrFile,
typeBound: SirType,
bodyFactory: SirExtension.(SirTypeParameter) -> Unit,
) {
Expand Down Expand Up @@ -143,7 +143,7 @@ class FlowConversionConstructorsGenerator(
.forEach(action)
}

private fun SirFile.addSwiftToKotlinConversion(
private fun SirIrFile.addSwiftToKotlinConversion(
from: SupportedFlow.Variant,
to: SupportedFlow.Variant,
typeBound: SirType,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import co.touchlab.skie.sir.element.SirClass
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.SirIrFile
import co.touchlab.skie.sir.element.SirSimpleFunction
import co.touchlab.skie.sir.element.receiverDeclaration
import co.touchlab.skie.sir.element.resolveAsKirClass
Expand Down Expand Up @@ -54,7 +55,7 @@ class FileScopeConversionParentProvider(
return when (callableDeclaration.origin) {
KirCallableDeclaration.Origin.Member -> error("Member callable are not supported. Was: $callableDeclaration")
KirCallableDeclaration.Origin.Extension -> getExtensions(callableDeclaration, sirCallableDeclaration)
KirCallableDeclaration.Origin.Global -> context.skieNamespaceProvider.getNamespaceFile(callableDeclaration.owner).let(::listOf)
KirCallableDeclaration.Origin.Global -> context.classNamespaceProvider.getNamespaceFile(callableDeclaration.owner).let(::listOf)
}
}

Expand Down Expand Up @@ -99,11 +100,11 @@ class FileScopeConversionParentProvider(
private fun getExtensionNamespace(
callableDeclaration: KirCallableDeclaration<*>,
parentType: SirType,
): SirFile {
): SirIrFile {
val extensionReceiverKirClass = getExtensionReceiverKirClassIfExists(parentType)

return extensionReceiverKirClass?.let { context.skieNamespaceProvider.getNamespaceFile(it) }
?: context.skieNamespaceProvider.getNamespaceFile(callableDeclaration.owner)
return extensionReceiverKirClass?.let { context.classNamespaceProvider.getNamespaceFile(it) }
?: context.classNamespaceProvider.getNamespaceFile(callableDeclaration.owner)
}

private fun getExtensionReceiverKirClassIfExists(parentType: SirType): KirClass? =
Expand All @@ -114,7 +115,7 @@ class FileScopeConversionParentProvider(
else -> null
}

private fun createNonOptionalExtension(file: SirFile, sirClass: SirClass): SirExtension? =
private fun createNonOptionalExtension(file: SirIrFile, sirClass: SirClass): SirExtension? =
if (sirClass.typeParameters.isEmpty()) {
sirProvider.getExtension(
classDeclaration = sirClass,
Expand All @@ -125,6 +126,7 @@ class FileScopeConversionParentProvider(
null
}

@Suppress("UNUSED_PARAMETER")
private fun getOptionalExtensions(callableDeclaration: KirCallableDeclaration<*>, type: NullableSirType, namespace: SirFile): List<SirExtension> {
// val nonNullType = type.type
//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class SealedEnumGeneratorDelegate(
SirClass(
baseName = "Sealed",
kind = SirClass.Kind.Enum,
parent = skieNamespaceProvider.getNamespace(kirClass),
parent = classNamespaceProvider.getNamespace(kirClass),
visibility = SirVisibility.PublicButReplaced,
).apply {
addConformanceToHashableIfPossible(kirClass)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class SealedFunctionGeneratorDelegate(
): SirSimpleFunction =
SirSimpleFunction(
identifier = kirClass.enumConstructorFunctionName,
parent = context.skieNamespaceProvider.getNamespaceFile(kirClass),
parent = context.classNamespaceProvider.getNamespaceFile(kirClass),
returnType = enum.toTypeFromEnclosingTypeParameters(enum.typeParameters).let(returnTypeModifier),
).apply {
copyTypeParametersFrom(enum)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class SkieClassSuspendGenerator {
private fun createSkieClass(suspendFunctionOwner: KirClass): SirClass =
SirClass(
baseName = "Suspend",
parent = skieNamespaceProvider.getNamespace(suspendFunctionOwner),
parent = classNamespaceProvider.getNamespace(suspendFunctionOwner),
visibility = SirVisibility.PublicButReplaced,
kind = SirClass.Kind.Struct,
).apply {
Expand All @@ -42,7 +42,7 @@ class SkieClassSuspendGenerator {
private fun generateNamespaceProvider(suspendFunctionOwner: KirClass, skieClass: SirClass) {
SirSimpleFunction(
identifier = "skie",
parent = skieNamespaceProvider.getNamespaceFile(suspendFunctionOwner),
parent = classNamespaceProvider.getNamespaceFile(suspendFunctionOwner),
returnType = sirBuiltins.Swift.Void.defaultType,
).apply {
copyTypeParametersFrom(skieClass)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class SwiftSuspendGeneratorDelegate(

val extension = sirProvider.getExtension(
classDeclaration = bridgeModel.extensionTypeDeclarationForBridgingFunction,
parent = skieNamespaceProvider.getNamespaceFile(bridgeModel.suspendFunctionOwner),
parent = classNamespaceProvider.getNamespaceFile(bridgeModel.suspendFunctionOwner),
)

bridgeModel.suspendKirFunction.bridgedSirFunction = extension.createSwiftBridgingFunction(bridgeModel)
Expand Down
Loading

0 comments on commit 3f45bb2

Please sign in to comment.