Skip to content

Commit

Permalink
Add support for Kotlin 2.0.20-RC.
Browse files Browse the repository at this point in the history
  • Loading branch information
TadeasKriz committed Aug 13, 2024
1 parent aba336d commit 70ffc25
Show file tree
Hide file tree
Showing 42 changed files with 434 additions and 92 deletions.
2 changes: 1 addition & 1 deletion SKIE/acceptance-tests
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ sealed class PrimitiveOirType(
object uint64_t : PrimitiveOirType("uint64_t")
object float : PrimitiveOirType("float")
object double : PrimitiveOirType("double")
object vectorFloat128 : PrimitiveOirType("float __attribute__((__vector_size__(16)))")

override fun render(attrsAndName: String, needsNonnullAttribute: Boolean): String =
name.withAttrsAndName(attrsAndName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,24 @@ class SirBuiltins(

val Float by Struct(superTypes = listOf(Hashable.defaultType))
val Double by Struct(superTypes = listOf(Hashable.defaultType))

val SIMDScalar by Protocol()
// TODO: There are more superTypes, but we don't need them
val SIMD by Protocol(superTypes = listOf(Hashable.defaultType)) {
SirTypeParameter(
name = "Scalar",
isPrimaryAssociatedType = true,
)
// TODO: There's also "MaskStorage" associatedtype, but we don't need it.
}
val SIMD4 by Struct(superTypes = listOf(SIMD.defaultType)) {
SirTypeParameter(
"Scalar",
bounds = listOf(
SIMDScalar.defaultType.toConformanceBound(),
),
)
}
}

class Foundation(sirProvider: SirProvider, swift: Swift) : ModuleBase() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ class SirTypeTranslator(
PrimitiveOirType.uint64_t -> sirBuiltins.Swift.UInt64.defaultType
PrimitiveOirType.NSUInteger -> sirBuiltins.Swift.UInt.defaultType
PrimitiveOirType.NSConvertedUInteger -> sirBuiltins.Swift.Int.defaultType
PrimitiveOirType.vectorFloat128 -> sirBuiltins.Swift.SIMD4.toType(sirBuiltins.Swift.Float.defaultType)
}

private fun mapType(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package co.touchlab.skie.context

import co.touchlab.skie.configuration.SwiftCompilerConfiguration
import org.jetbrains.kotlin.backend.konan.KonanConfigKeys
import org.jetbrains.kotlin.config.CompilerConfiguration
import org.jetbrains.kotlin.backend.konan.BitcodeEmbedding

actual fun CompilerConfiguration.getBitcodeEmbeddingMode(): SwiftCompilerConfiguration.BitcodeEmbeddingMode {
return when (this[KonanConfigKeys.BITCODE_EMBEDDING_MODE]) {
BitcodeEmbedding.Mode.FULL -> SwiftCompilerConfiguration.BitcodeEmbeddingMode.Full
BitcodeEmbedding.Mode.MARKER -> SwiftCompilerConfiguration.BitcodeEmbeddingMode.Marker
BitcodeEmbedding.Mode.NONE, null -> SwiftCompilerConfiguration.BitcodeEmbeddingMode.None
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package co.touchlab.skie.kir.irbuilder.impl.symboltable

import org.jetbrains.kotlin.descriptors.ClassConstructorDescriptor
import org.jetbrains.kotlin.descriptors.DescriptorVisibility
import org.jetbrains.kotlin.ir.ObsoleteDescriptorBasedAPI
import org.jetbrains.kotlin.ir.declarations.IrConstructor
import org.jetbrains.kotlin.ir.declarations.IrDeclarationOrigin
import org.jetbrains.kotlin.ir.declarations.IrDeclarationParent
import org.jetbrains.kotlin.ir.declarations.IrFactory
import org.jetbrains.kotlin.ir.declarations.IrTypeParameter
import org.jetbrains.kotlin.ir.declarations.IrValueParameter
import org.jetbrains.kotlin.ir.declarations.MetadataSource
import org.jetbrains.kotlin.ir.expressions.IrBody
import org.jetbrains.kotlin.ir.expressions.IrConstructorCall
import org.jetbrains.kotlin.ir.symbols.IrConstructorSymbol
import org.jetbrains.kotlin.ir.types.IrType
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.serialization.deserialization.descriptors.DeserializedContainerSource


actual class DummyIrConstructor actual constructor(override val symbol: IrConstructorSymbol) : IrConstructor() {

override var isPrimary: Boolean by unsupported()
override val startOffset: Int by unsupported()
override val endOffset: Int by unsupported()
override val factory: IrFactory by unsupported()
override var origin: IrDeclarationOrigin by unsupported()
override var parent: IrDeclarationParent by unsupported()
override var name: Name by unsupported()
override var visibility: DescriptorVisibility by unsupported()
override var body: IrBody? by unsupported()
override var contextReceiverParametersCount: Int by unsupported()

@ObsoleteDescriptorBasedAPI
override val descriptor: ClassConstructorDescriptor by unsupported()
override var dispatchReceiverParameter: IrValueParameter? by unsupported()
override var extensionReceiverParameter: IrValueParameter? by unsupported()
override var isExpect: Boolean by unsupported()
override var isInline: Boolean by unsupported()
override var returnType: IrType by unsupported()
override var valueParameters: List<IrValueParameter> by unsupported()
override val containerSource: DeserializedContainerSource? by unsupported()
override var metadata: MetadataSource? by unsupported()
override var annotations: List<IrConstructorCall> by unsupported()
override var isExternal: Boolean by unsupported()
override var typeParameters: List<IrTypeParameter> by unsupported()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package co.touchlab.skie.kir.irbuilder.impl.symboltable

import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor
import org.jetbrains.kotlin.ir.declarations.IrSymbolOwner
import org.jetbrains.kotlin.ir.symbols.IrBindableSymbol
import org.jetbrains.kotlin.ir.symbols.IrTypeParameterSymbol
import org.jetbrains.kotlin.ir.util.IdSignature
import org.jetbrains.kotlin.ir.symbols.impl.IrPublicSymbolBase
import org.jetbrains.kotlin.ir.symbols.impl.IrTypeParameterPublicSymbolImpl as KotlinIrTypeParameterPublicSymbolImpl

actual fun IrTypeParameterPublicSymbolImpl(
signature: IdSignature,
descriptor: TypeParameterDescriptor,
): IrTypeParameterSymbol {
return KotlinIrTypeParameterPublicSymbolImpl(signature, descriptor)
}

actual abstract class IrBaseRebindablePublicSymbol<out Descriptor : DeclarationDescriptor, Owner : IrSymbolOwner> actual constructor(
signature: IdSignature,
descriptor: Descriptor,
) : IrBindableSymbol<Descriptor, Owner>, IrPublicSymbolBase<Descriptor>(signature, descriptor) {

private var _owner: Owner? = null
actual override val owner: Owner
get() = _owner ?: throw IllegalStateException("Symbol is not bound")

actual override fun bind(owner: Owner) {
this._owner = owner
}

actual fun unbind() {
this._owner = null
}

actual override val isBound: Boolean
get() = _owner != null

actual override var privateSignature: IdSignature? = null
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package co.touchlab.skie.kir.type.translation

import co.touchlab.skie.oir.type.OirType
import co.touchlab.skie.oir.type.PointerOirType
import co.touchlab.skie.oir.type.PrimitiveOirType
import co.touchlab.skie.oir.type.VoidOirType
import org.jetbrains.kotlin.backend.konan.binaryRepresentationIsNullable
import org.jetbrains.kotlin.backend.konan.objcexport.ObjCValueType
import org.jetbrains.kotlin.types.KotlinType

actual fun ObjCValueType.mapToOir(kotlinType: KotlinType): OirType =
when (this) {
ObjCValueType.BOOL -> PrimitiveOirType.BOOL
ObjCValueType.UNICHAR -> PrimitiveOirType.unichar
ObjCValueType.CHAR -> PrimitiveOirType.int8_t
ObjCValueType.SHORT -> PrimitiveOirType.int16_t
ObjCValueType.INT -> PrimitiveOirType.int32_t
ObjCValueType.LONG_LONG -> PrimitiveOirType.int64_t
ObjCValueType.UNSIGNED_CHAR -> PrimitiveOirType.uint8_t
ObjCValueType.UNSIGNED_SHORT -> PrimitiveOirType.uint16_t
ObjCValueType.UNSIGNED_INT -> PrimitiveOirType.uint32_t
ObjCValueType.UNSIGNED_LONG_LONG -> PrimitiveOirType.uint64_t
ObjCValueType.FLOAT -> PrimitiveOirType.float
ObjCValueType.DOUBLE -> PrimitiveOirType.double
ObjCValueType.POINTER -> PointerOirType(VoidOirType, kotlinType.binaryRepresentationIsNullable())
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package co.touchlab.skie.context

import co.touchlab.skie.configuration.SwiftCompilerConfiguration
import org.jetbrains.kotlin.config.CompilerConfiguration

actual fun CompilerConfiguration.getBitcodeEmbeddingMode(): SwiftCompilerConfiguration.BitcodeEmbeddingMode {
return SwiftCompilerConfiguration.BitcodeEmbeddingMode.None
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package co.touchlab.skie.kir.irbuilder.impl.symboltable

import org.jetbrains.kotlin.descriptors.ClassConstructorDescriptor
import org.jetbrains.kotlin.descriptors.DescriptorVisibility
import org.jetbrains.kotlin.ir.ObsoleteDescriptorBasedAPI
import org.jetbrains.kotlin.ir.declarations.IrConstructor
import org.jetbrains.kotlin.ir.declarations.IrDeclarationOrigin
import org.jetbrains.kotlin.ir.declarations.IrFactory
import org.jetbrains.kotlin.ir.declarations.IrTypeParameter
import org.jetbrains.kotlin.ir.declarations.IrValueParameter
import org.jetbrains.kotlin.ir.declarations.MetadataSource
import org.jetbrains.kotlin.ir.expressions.IrBody
import org.jetbrains.kotlin.ir.expressions.IrConstructorCall
import org.jetbrains.kotlin.ir.symbols.IrConstructorSymbol
import org.jetbrains.kotlin.ir.types.IrType
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.serialization.deserialization.descriptors.DeserializedContainerSource

actual class DummyIrConstructor actual constructor(override val symbol: IrConstructorSymbol) : IrConstructor() {

override var isPrimary: Boolean by unsupported()
override val startOffset: Int by unsupported()
override val endOffset: Int by unsupported()
override val factory: IrFactory by unsupported()
override var origin: IrDeclarationOrigin by unsupported()
override var name: Name by unsupported()
override var visibility: DescriptorVisibility by unsupported()
override var body: IrBody? by unsupported()
override var contextReceiverParametersCount: Int by unsupported()

@ObsoleteDescriptorBasedAPI
override val descriptor: ClassConstructorDescriptor by unsupported()
override var dispatchReceiverParameter: IrValueParameter? by unsupported()
override var extensionReceiverParameter: IrValueParameter? by unsupported()
override var isExpect: Boolean by unsupported()
override var isInline: Boolean by unsupported()
override var returnType: IrType by unsupported()
override var valueParameters: List<IrValueParameter> by unsupported()
override val containerSource: DeserializedContainerSource? by unsupported()
override var metadata: MetadataSource? by unsupported()
override var annotations: List<IrConstructorCall> by unsupported()
override var isExternal: Boolean by unsupported()
override var typeParameters: List<IrTypeParameter> by unsupported()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package co.touchlab.skie.kir.irbuilder.impl.symboltable

import org.jetbrains.kotlin.descriptors.DescriptorVisibility
import org.jetbrains.kotlin.descriptors.FunctionDescriptor
import org.jetbrains.kotlin.descriptors.Modality
import org.jetbrains.kotlin.ir.ObsoleteDescriptorBasedAPI
import org.jetbrains.kotlin.ir.declarations.IrAttributeContainer
import org.jetbrains.kotlin.ir.declarations.IrDeclarationOrigin
import org.jetbrains.kotlin.ir.declarations.IrDeclarationParent
import org.jetbrains.kotlin.ir.declarations.IrFactory
import org.jetbrains.kotlin.ir.declarations.IrSimpleFunction
import org.jetbrains.kotlin.ir.declarations.IrTypeParameter
import org.jetbrains.kotlin.ir.declarations.IrValueParameter
import org.jetbrains.kotlin.ir.declarations.MetadataSource
import org.jetbrains.kotlin.ir.expressions.IrBody
import org.jetbrains.kotlin.ir.expressions.IrConstructorCall
import org.jetbrains.kotlin.ir.symbols.IrPropertySymbol
import org.jetbrains.kotlin.ir.symbols.IrSimpleFunctionSymbol
import org.jetbrains.kotlin.ir.types.IrType
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.serialization.deserialization.descriptors.DeserializedContainerSource

actual class DummyIrSimpleFunction actual constructor(
override val symbol: IrSimpleFunctionSymbol,
) : IrSimpleFunction() {

override var startOffset: Int by unsupported()
override var endOffset: Int by unsupported()
override var attributeOwnerId: IrAttributeContainer by unsupported()
override val factory: IrFactory by unsupported()
override var origin: IrDeclarationOrigin by unsupported()
override var originalBeforeInline: IrAttributeContainer? by unsupported()
override var name: Name by unsupported()
override var visibility: DescriptorVisibility by unsupported()
override var body: IrBody? by unsupported()
override var contextReceiverParametersCount: Int by unsupported()

@ObsoleteDescriptorBasedAPI
override val descriptor: FunctionDescriptor by unsupported()
override var dispatchReceiverParameter: IrValueParameter? by unsupported()
override var extensionReceiverParameter: IrValueParameter? by unsupported()
override var isExpect: Boolean by unsupported()
override var isInline: Boolean by unsupported()
override var returnType: IrType by unsupported()
override var valueParameters: List<IrValueParameter> by unsupported()
override val containerSource: DeserializedContainerSource? by unsupported()
override var metadata: MetadataSource? by unsupported()
override var annotations: List<IrConstructorCall> by unsupported()
override var overriddenSymbols: List<IrSimpleFunctionSymbol> by unsupported()
override var modality: Modality by unsupported()
override var isExternal: Boolean by unsupported()
override var correspondingPropertySymbol: IrPropertySymbol? by unsupported()
override var isFakeOverride: Boolean by unsupported()
override var isInfix: Boolean by unsupported()
override var isOperator: Boolean by unsupported()
override var isSuspend: Boolean by unsupported()
override var isTailrec: Boolean by unsupported()
override var typeParameters: List<IrTypeParameter> by unsupported()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package co.touchlab.skie.kir.irbuilder.impl.symboltable

import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor
import org.jetbrains.kotlin.descriptors.ValueParameterDescriptor
import org.jetbrains.kotlin.ir.ObsoleteDescriptorBasedAPI
import org.jetbrains.kotlin.ir.declarations.IrDeclaration
import org.jetbrains.kotlin.ir.declarations.IrSymbolOwner
import org.jetbrains.kotlin.ir.descriptors.toIrBasedDescriptor
import org.jetbrains.kotlin.ir.symbols.IrBindableSymbol
import org.jetbrains.kotlin.ir.symbols.IrTypeParameterSymbol
import org.jetbrains.kotlin.ir.symbols.impl.IrTypeParameterSymbolImpl
import org.jetbrains.kotlin.ir.symbols.isPublicApi
import org.jetbrains.kotlin.ir.util.IdSignature
import org.jetbrains.kotlin.ir.util.render

actual fun IrTypeParameterPublicSymbolImpl(
signature: IdSignature,
descriptor: TypeParameterDescriptor,
): IrTypeParameterSymbol {
return IrTypeParameterSymbolImpl(descriptor, signature)
}

actual abstract class IrBaseRebindablePublicSymbol<out Descriptor : DeclarationDescriptor, Owner : IrSymbolOwner> actual constructor(
actual override val signature: IdSignature,
descriptor: Descriptor,
) : IrBindableSymbol<Descriptor, Owner> {

// private var _owner: B? = null
// override val owner: B
// get() = _owner ?: throw IllegalStateException("Symbol is not bound")
//
// override fun bind(owner: B) {
// this._owner = owner
// }
//
// fun unbind() {
// this._owner = null
// }
//
// override val isBound: Boolean
// get() = _owner != null
//
// override var privateSignature: IdSignature? = null

private val _descriptor: Descriptor? = descriptor

@ObsoleteDescriptorBasedAPI
@Suppress("UNCHECKED_CAST")
actual override val descriptor: Descriptor
get() = _descriptor ?: (owner as IrDeclaration).toIrBasedDescriptor() as Descriptor

@ObsoleteDescriptorBasedAPI
actual override val hasDescriptor: Boolean
get() = _descriptor != null

private var _owner: Owner? = null
actual override val owner: Owner
get() = _owner ?: error("${javaClass.simpleName} is unbound. Signature: $signature")

actual override var privateSignature: IdSignature? = null

init {
assert(descriptor == null || isOriginalDescriptor(descriptor)) {
"Substituted descriptor $descriptor for ${descriptor!!.original}"
}
if (!isPublicApi && descriptor != null) {
val containingDeclaration = descriptor.containingDeclaration
assert(containingDeclaration == null || isOriginalDescriptor(containingDeclaration)) {
"Substituted containing declaration: $containingDeclaration\nfor descriptor: $descriptor"
}
}
}

private fun isOriginalDescriptor(descriptor: DeclarationDescriptor): Boolean =
// TODO fix declaring/referencing value parameters: compute proper original descriptor
descriptor is ValueParameterDescriptor && isOriginalDescriptor(descriptor.containingDeclaration) ||
descriptor == descriptor.original

actual override val isBound: Boolean
get() = _owner != null

actual override fun bind(owner: Owner) {
this._owner = owner
}

actual fun unbind() {
this._owner = null
}

override fun toString(): String {
if (isBound) return owner.render()
return if (isPublicApi)
"Unbound public symbol ${this::class.java.simpleName}: $signature"
else
"Unbound private symbol " +
if (_descriptor != null) "${this::class.java.simpleName}: $_descriptor" else super.toString()
}
}
Loading

0 comments on commit 70ffc25

Please sign in to comment.