Skip to content

Commit

Permalink
Upgrade to the most recent dev version:
Browse files Browse the repository at this point in the history
* Get rid of swift-export-experimental repo
* Fix swift export API usages
* Add a bit of tests on strings and chars
  • Loading branch information
sbogolepov committed Jul 31, 2024
1 parent fd2b6c9 commit acd57be
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 46 deletions.
1 change: 0 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ allprojects {
maven("https://maven.pkg.jetbrains.space/kotlin/p/kotlin/bootstrap")
maven("https://maven.pkg.jetbrains.space/kotlin/p/wasm/experimental")
maven("https://maven.pkg.jetbrains.space/public/p/compose/dev")
maven("https://maven.pkg.jetbrains.space/kotlin/p/kotlin/swift-export-experimental")
}
afterEvaluate {
dependencies {
Expand Down
3 changes: 1 addition & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
systemProp.kotlinVersion=2.0.0
systemProp.kotlinVersion=2.1.0-dev-3023
systemProp.kotlinIdeVersion=1.9.20-506
systemProp.kotlinIdeVersionSuffix=IJ8109.175
systemProp.swiftExportVersion=2.0.20-dev-3623
systemProp.policy=executor.policy
systemProp.indexes=indexes.json
systemProp.indexesJs=indexesJs.json
Expand Down
1 change: 1 addition & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ pluginManagement {
gradlePluginPortal()
mavenCentral()
maven("https://maven.pkg.jetbrains.space/kotlin/p/kotlin/bootstrap")
maven("https://maven.pkg.jetbrains.space/kotlin/p/kotlin/dev")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import com.compiler.server.model.ErrorDescriptor
import com.compiler.server.model.ProjectSeveriry
import com.compiler.server.model.TextInterval
import org.jetbrains.kotlin.cli.common.CLICompiler
import org.jetbrains.kotlin.cli.common.CLITool
import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity
import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity.*
import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSourceLocation
Expand Down Expand Up @@ -56,7 +55,7 @@ fun <T> CLICompiler<*>.tryCompilation(inputDirectory: Path, inputFiles: List<Pat
inputFiles.forEach { put(it.outputFilePathString(), mutableListOf()) }
}
val defaultFileName = inputFiles.singleOrNull()?.outputFilePathString() ?: ""
val exitCode = CLITool.doMainNoExit(this, arguments.toTypedArray(), object : MessageRenderer {
val exitCode = CLICompiler.doMainNoExit(this, arguments.toTypedArray(), object : MessageRenderer {
override fun renderPreamble(): String = ""

override fun render(
Expand Down
35 changes: 23 additions & 12 deletions src/test/kotlin/com/compiler/server/SwiftConverterTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import com.compiler.server.base.BaseExecutorTest
import org.junit.jupiter.api.Test
import kotlin.test.assertContains
import kotlin.test.assertEquals
import kotlin.test.assertTrue

class SwiftConverterTest : BaseExecutorTest() {

Expand All @@ -18,11 +17,6 @@ class SwiftConverterTest : BaseExecutorTest() {
assertContains(actual.swiftCode.trimEnd(), expected)
}

private fun shouldFailTest(input: String) {
val actual = translateToSwift(input)
assertTrue(actual.hasErrors())
}

@Test
fun basicSwiftExportTest() = containsTest(
input = """
Expand All @@ -46,11 +40,16 @@ class SwiftConverterTest : BaseExecutorTest() {
input = "public class MyClass { public fun A() {}}",
expected = """
import KotlinRuntime
public class MyClass : KotlinRuntime.KotlinBase {
public override init() {
stub()
}
public override init(
__externalRCRef: Swift.UInt
) {
stub()
}
public func A() -> Swift.Void {
stub()
}
Expand All @@ -66,17 +65,15 @@ class SwiftConverterTest : BaseExecutorTest() {
val myProperty: Int = 42
""".trimIndent(),
expected = """
public extension Playground.foo.bar {
@_exported import pkg
public extension pkg.foo.bar {
public static var myProperty: Swift.Int32 {
get {
stub()
}
}
}
public enum foo {
public enum bar {
}
}
""".trimIndent()
)

Expand All @@ -96,4 +93,18 @@ class SwiftConverterTest : BaseExecutorTest() {
}
""".trimIndent()
)

@Test
fun `unsupported type declaration`() = exactTest(
input = """
interface Foo
fun produceFoo(): Foo = TODO()
""".trimIndent(),
expected = """
public func produceFoo() -> Swift.Never {
stub()
}
""".trimIndent()
)
}
29 changes: 14 additions & 15 deletions swift-export-playground/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,29 @@ repositories {
mavenCentral()
// For Analysis API components
maven("https://maven.pkg.jetbrains.space/kotlin/p/kotlin/kotlin-ide-plugin-dependencies")
maven("https://maven.pkg.jetbrains.space/kotlin/p/kotlin/swift-export-experimental")
maven("https://maven.pkg.jetbrains.space/kotlin/p/kotlin/dev")
}

val kotlinVersion = rootProject.properties["systemProp.kotlinVersion"]
val swiftExportVersion = rootProject.properties["systemProp.swiftExportVersion"]
val aaVersion = "2.1.0-dev-2515"

dependencies {
implementation("org.jetbrains.kotlin:kotlin-compiler:$kotlinVersion")
// For K/N Distribution class
implementation("org.jetbrains.kotlin:kotlin-native-utils:$kotlinVersion")
implementation("org.jetbrains.kotlin:kotlin-compiler:$aaVersion")

// Analysis API components which are required for the Swift export
implementation("org.jetbrains.kotlin:analysis-api-standalone-for-ide:$kotlinVersion") { isTransitive = false }
implementation("org.jetbrains.kotlin:high-level-api-for-ide:$kotlinVersion") { isTransitive = false }
implementation("org.jetbrains.kotlin:high-level-api-fir-for-ide:$kotlinVersion") { isTransitive = false }
implementation("org.jetbrains.kotlin:high-level-api-impl-base-for-ide:$kotlinVersion") { isTransitive = false }
implementation("org.jetbrains.kotlin:low-level-api-fir-for-ide:$kotlinVersion") { isTransitive = false }
implementation("org.jetbrains.kotlin:symbol-light-classes-for-ide:$kotlinVersion") { isTransitive = false }
implementation("org.jetbrains.kotlin:analysis-api-standalone-for-ide:$aaVersion") { isTransitive = false }
implementation("org.jetbrains.kotlin:high-level-api-for-ide:$aaVersion") { isTransitive = false }
implementation("org.jetbrains.kotlin:high-level-api-fir-for-ide:$aaVersion") { isTransitive = false }
implementation("org.jetbrains.kotlin:high-level-api-impl-base-for-ide:$aaVersion") { isTransitive = false }
implementation("org.jetbrains.kotlin:low-level-api-fir-for-ide:$aaVersion") { isTransitive = false }
implementation("org.jetbrains.kotlin:symbol-light-classes-for-ide:$aaVersion") { isTransitive = false }
implementation("org.jetbrains.kotlin:analysis-api-platform-interface-for-ide:$aaVersion") { isTransitive = false }

// Swift export not-yet-published dependencies.
implementation("org.jetbrains.kotlin:sir:$swiftExportVersion") { isTransitive = false }
implementation("org.jetbrains.kotlin:sir-providers:$swiftExportVersion") { isTransitive = false }
implementation("org.jetbrains.kotlin:sir-light-classes:$swiftExportVersion") { isTransitive = false }
implementation("org.jetbrains.kotlin:sir-printer:$swiftExportVersion") { isTransitive = false }
implementation("org.jetbrains.kotlin:sir:$kotlinVersion") { isTransitive = false }
implementation("org.jetbrains.kotlin:sir-providers:$kotlinVersion") { isTransitive = false }
implementation("org.jetbrains.kotlin:sir-light-classes:$kotlinVersion") { isTransitive = false }
implementation("org.jetbrains.kotlin:sir-printer:$kotlinVersion") { isTransitive = false }

testImplementation("junit:junit:4.13.2")
testImplementation("org.jetbrains.kotlin:kotlin-test:$kotlinVersion")
Expand Down
19 changes: 15 additions & 4 deletions swift-export-playground/src/main/kotlin/PlaygroundSirSession.kt
Original file line number Diff line number Diff line change
@@ -1,31 +1,42 @@
import org.jetbrains.kotlin.analysis.project.structure.KtModule
import org.jetbrains.kotlin.analysis.api.projectStructure.KaModule
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.sir.SirModule

import org.jetbrains.kotlin.sir.providers.SirSession
import org.jetbrains.kotlin.sir.providers.SirTrampolineDeclarationsProvider
import org.jetbrains.kotlin.sir.providers.SirTypeProvider
import org.jetbrains.kotlin.sir.providers.impl.*
import org.jetbrains.kotlin.sir.providers.utils.UnsupportedDeclarationReporter
import org.jetbrains.sir.lightclasses.SirDeclarationFromKtSymbolProvider

internal class PlaygroundSirSession(
ktModule: KtModule,
ktModule: KaModule,
moduleForPackageEnums: SirModule,
unsupportedDeclarationReporter: UnsupportedDeclarationReporter,
targetPackageFqName: FqName?,
) : SirSession {
override val declarationNamer = SirDeclarationNamerImpl()
override val enumGenerator = SirEnumGeneratorImpl()
override val moduleProvider = SirSingleModuleProvider("Playground")
override val declarationProvider = CachingSirDeclarationProvider(
declarationsProvider = SirDeclarationFromKtSymbolProvider(
ktModule = ktModule,
sirSession = sirSession,
)
)
override val enumGenerator = SirEnumGeneratorImpl(moduleForPackageEnums)
override val parentProvider = SirParentProviderImpl(
sirSession = sirSession,
packageEnumGenerator = enumGenerator,
)
override val typeProvider = SirTypeProviderImpl(
errorTypeStrategy = SirTypeProvider.ErrorTypeStrategy.ErrorType,
unsupportedTypeStrategy = SirTypeProvider.ErrorTypeStrategy.ErrorType,
sirSession = sirSession,
)
override val visibilityChecker = SirVisibilityCheckerImpl()
override val visibilityChecker = SirVisibilityCheckerImpl(unsupportedDeclarationReporter)
override val childrenProvider = SirDeclarationChildrenProviderImpl(
sirSession = sirSession,
)

override val trampolineDeclarationsProvider: SirTrampolineDeclarationsProvider = SirTrampolineDeclarationsProviderImpl(sirSession, targetPackageFqName)
}
16 changes: 8 additions & 8 deletions swift-export-playground/src/main/kotlin/Runner.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import org.jetbrains.kotlin.analysis.api.KtAnalysisApiInternals
import org.jetbrains.kotlin.analysis.api.analyze
import org.jetbrains.kotlin.analysis.api.lifetime.KtLifetimeTokenProvider
import org.jetbrains.kotlin.analysis.api.standalone.KtAlwaysAccessibleLifetimeTokenProvider
import org.jetbrains.kotlin.analysis.api.standalone.buildStandaloneAnalysisAPISession
import org.jetbrains.kotlin.analysis.project.structure.KtModule
import org.jetbrains.kotlin.analysis.project.structure.builder.buildKtLibraryModule
Expand All @@ -11,6 +8,8 @@ import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.sir.SirFunctionBody
import org.jetbrains.kotlin.sir.SirModule
import org.jetbrains.kotlin.sir.SirMutableDeclarationContainer
import org.jetbrains.kotlin.sir.builder.buildModule
import org.jetbrains.kotlin.sir.providers.utils.SimpleUnsupportedDeclarationReporter
import org.jetbrains.kotlin.sir.util.addChild
import org.jetbrains.sir.printer.SirAsSwiftSourcesPrinter
import java.nio.file.Path
Expand All @@ -26,11 +25,15 @@ fun runSwiftExport(
val (ktModule, sources) = collectModuleAndSources(sourceFile, "Playground", stdlibPath)

return analyze(ktModule) {
val sirSession = PlaygroundSirSession(ktModule)
val pkgModule = buildModule {
name = "pkg"
}
val unsupportedDeclarationReporter = SimpleUnsupportedDeclarationReporter()
val sirSession = PlaygroundSirSession(ktModule, pkgModule, unsupportedDeclarationReporter, targetPackageFqName = null)
val sirModule: SirModule = with(sirSession) {
ktModule.sirModule().also {
sources.flatMap { file ->
file.getFileSymbol().getFileScope().extractDeclarations(analysisSession)
file.symbol.fileScope.extractDeclarations(useSiteSession)
}.forEach { topLevelDeclaration ->
val parent = topLevelDeclaration.parent as? SirMutableDeclarationContainer
?: error("top level declaration can contain only module or extension to package as a parent")
Expand All @@ -49,15 +52,12 @@ fun runSwiftExport(
}
}

@OptIn(KtAnalysisApiInternals::class)
private fun collectModuleAndSources(
sourceRoot: Path,
kotlinModuleName: String,
stdlibPath: Path?,
): Pair<KtModule, List<KtFile>> {
val analysisAPISession = buildStandaloneAnalysisAPISession {
registerProjectService(KtLifetimeTokenProvider::class.java, KtAlwaysAccessibleLifetimeTokenProvider())

buildKtModuleProvider {
platform = NativePlatforms.unspecifiedNativePlatform

Expand Down
33 changes: 31 additions & 2 deletions swift-export-playground/src/test/kotlin/Tests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,16 @@ class SwiftExportTests {
""".trimIndent(),
"""
import KotlinRuntime
public class A : KotlinRuntime.KotlinBase {
public override init() {
stub()
}
public override init(
__externalRCRef: Swift.UInt
) {
stub()
}
}
""".trimIndent()
)
Expand All @@ -55,7 +60,7 @@ class SwiftExportTests {
""".trimIndent(),
"""
import KotlinRuntime
public class O : KotlinRuntime.KotlinBase {
public static var shared: Playground.O {
get {
Expand All @@ -65,6 +70,11 @@ class SwiftExportTests {
private override init() {
stub()
}
public override init(
__externalRCRef: Swift.UInt
) {
stub()
}
}
""".trimIndent()
)
Expand All @@ -78,4 +88,23 @@ class SwiftExportTests {
public typealias MyInt = Swift.Int32
""".trimIndent()
)

@Test
fun `strings and chars`() = testSources(
"""
fun produceString(): String = "hello"
fun firstChar(str: String): Char = str.first()
""".trimIndent(),
"""
public func firstChar(
str: Swift.String
) -> Swift.Unicode.UTF16.CodeUnit {
stub()
}
public func produceString() -> Swift.String {
stub()
}
""".trimIndent()
)
}

0 comments on commit acd57be

Please sign in to comment.