turn on explicitApi for SIR modules

Merge-request: KT-MR-13448
Merged-by: Artem Olkov <artem.olkov@jetbrains.com>
This commit is contained in:
Artem Olkov
2023-12-12 09:02:55 +00:00
committed by Space Team
parent a03fd2e29a
commit 6c4a6e2a02
10 changed files with 48 additions and 32 deletions
@@ -4,6 +4,10 @@ plugins {
description = "Build Swift IR from Analysis API" description = "Build Swift IR from Analysis API"
kotlin {
explicitApi()
}
dependencies { dependencies {
compileOnly(kotlinStdlib()) compileOnly(kotlinStdlib())
@@ -17,11 +17,11 @@ import org.jetbrains.kotlin.sir.analysisapi.transformers.toForeignFunction
/** /**
* A root interface for classes that produce Swift IR elements. * A root interface for classes that produce Swift IR elements.
*/ */
interface SirFactory { public interface SirFactory {
fun build(fromFile: KtFile): List<SirDeclaration> public fun build(fromFile: KtFile): List<SirDeclaration>
} }
class SirGenerator : SirFactory { public class SirGenerator : SirFactory {
override fun build(fromFile: KtFile): List<SirDeclaration> = analyze(fromFile) { override fun build(fromFile: KtFile): List<SirDeclaration> = analyze(fromFile) {
val res = mutableListOf<SirForeignFunction>() val res = mutableListOf<SirForeignFunction>()
fromFile.accept(Visitor(res)) fromFile.accept(Visitor(res))
@@ -4,6 +4,10 @@ plugins {
description = "SIR to Kotlin bindings generator" description = "SIR to Kotlin bindings generator"
kotlin {
explicitApi()
}
dependencies { dependencies {
compileOnly(kotlinStdlib()) compileOnly(kotlinStdlib())
@@ -16,10 +16,10 @@ import org.jetbrains.kotlin.sir.bridge.impl.KotlinBridgePrinter
* @param function SIR function we are generating bridge for * @param function SIR function we are generating bridge for
* @param bridgeName C name of the bridge * @param bridgeName C name of the bridge
*/ */
class BridgeRequest( public class BridgeRequest(
val function: SirFunction, public val function: SirFunction,
val bridgeName: String, public val bridgeName: String,
val fqName: List<String>, public val fqName: List<String>,
) )
/** /**
@@ -27,9 +27,9 @@ class BridgeRequest(
* Abstracts away all nuances of Kotlin compiler ABI, making it possible * Abstracts away all nuances of Kotlin compiler ABI, making it possible
* to call Kotlin code from other environments. * to call Kotlin code from other environments.
*/ */
class FunctionBridge( public class FunctionBridge(
val kotlinFunctionBridge: KotlinFunctionBridge, public val kotlinFunctionBridge: KotlinFunctionBridge,
val cDeclarationBridge: CFunctionBridge, public val cDeclarationBridge: CFunctionBridge,
) )
/** /**
@@ -38,9 +38,9 @@ class FunctionBridge(
* @param lines with function declaration. * @param lines with function declaration.
* @param headerDependencies required headers. * @param headerDependencies required headers.
*/ */
class CFunctionBridge( public class CFunctionBridge(
val lines: List<String>, public val lines: List<String>,
val headerDependencies: List<String>, public val headerDependencies: List<String>,
) )
/** /**
@@ -49,39 +49,39 @@ class CFunctionBridge(
* @param lines definition of Kotlin bridge. * @param lines definition of Kotlin bridge.
* @param packageDependencies required packages to make sources compile. * @param packageDependencies required packages to make sources compile.
*/ */
class KotlinFunctionBridge( public class KotlinFunctionBridge(
val lines: List<String>, public val lines: List<String>,
val packageDependencies: List<String>, public val packageDependencies: List<String>,
) )
/** /**
* Generates [FunctionBridge] that binds SIR function to its Kotlin origin. * Generates [FunctionBridge] that binds SIR function to its Kotlin origin.
*/ */
interface BridgeGenerator { public interface BridgeGenerator {
fun generate(request: BridgeRequest): FunctionBridge public fun generate(request: BridgeRequest): FunctionBridge
} }
/** /**
* A common interface for classes that serialize [FunctionBridge] in some form. * A common interface for classes that serialize [FunctionBridge] in some form.
*/ */
interface BridgePrinter { public interface BridgePrinter {
/** /**
* Populate printer with an additional [bridge]. * Populate printer with an additional [bridge].
*/ */
fun add(bridge: FunctionBridge) public fun add(bridge: FunctionBridge)
/** /**
* Outputs the aggregated result. * Outputs the aggregated result.
*/ */
fun print(): Sequence<String> public fun print(): Sequence<String>
} }
fun createBridgeGenerator(): BridgeGenerator = public fun createBridgeGenerator(): BridgeGenerator =
BridgeGeneratorImpl() BridgeGeneratorImpl()
fun createCBridgePrinter(): BridgePrinter = public fun createCBridgePrinter(): BridgePrinter =
CBridgePrinter() CBridgePrinter()
fun createKotlinBridgePrinter(): BridgePrinter = public fun createKotlinBridgePrinter(): BridgePrinter =
KotlinBridgePrinter() KotlinBridgePrinter()
@@ -96,7 +96,7 @@ private fun bridgeParameter(parameter: SirParameter): BridgeParameter {
) )
} }
fun createBridgeParameterName(kotlinName: String): String { public fun createBridgeParameterName(kotlinName: String): String {
// TODO: Post-process because C has stricter naming conventions. // TODO: Post-process because C has stricter naming conventions.
return kotlinName return kotlinName
} }
@@ -111,7 +111,7 @@ internal data class CBridgeParameter(
val type: CType, val type: CType,
) )
enum class CType(val repr: String) { public enum class CType(public val repr: String) {
Bool("_Bool"), Bool("_Bool"),
Int8("int8_t"), Int8("int8_t"),
+4
View File
@@ -4,6 +4,10 @@ plugins {
description = "Infrastructure of transformations over SIR" description = "Infrastructure of transformations over SIR"
kotlin {
explicitApi()
}
dependencies { dependencies {
compileOnly(kotlinStdlib()) compileOnly(kotlinStdlib())
@@ -11,7 +11,7 @@ import org.jetbrains.kotlin.sir.SirElement
* Swift IR is supposed to be transformed by a series of passes. * Swift IR is supposed to be transformed by a series of passes.
* This is a base interface that all such passes should implement. * This is a base interface that all such passes should implement.
*/ */
interface SirPass<out R, in T> { public interface SirPass<out R, in T> {
/** /**
* Executes the pass over the given [SirElement]. * Executes the pass over the given [SirElement].
@@ -20,5 +20,5 @@ interface SirPass<out R, in T> {
* @param data Additional data that is required to run the pass. * @param data Additional data that is required to run the pass.
* @return The result of the pass. * @return The result of the pass.
*/ */
fun run(element: SirElement, data: T): R public fun run(element: SirElement, data: T): R
} }
@@ -22,7 +22,7 @@ import java.lang.IllegalStateException
* or `element` does not contain origin of type `SirOrigin.KotlinEntity.Function`, * or `element` does not contain origin of type `SirOrigin.KotlinEntity.Function`,
* returns original element. * returns original element.
*/ */
class ForeignIntoSwiftFunctionTranslationPass : SirPass<SirElement, Unit> { public class ForeignIntoSwiftFunctionTranslationPass : SirPass<SirElement, Unit> {
private class Transformer : SirTransformer<Unit>() { private class Transformer : SirTransformer<Unit>() {
override fun <E : SirElement> transformElement(element: E, data: Unit): E { override fun <E : SirElement> transformElement(element: E, data: Unit): E {
@@ -4,6 +4,10 @@ plugins {
description = "Printer for SIR" description = "Printer for SIR"
kotlin {
explicitApi()
}
dependencies { dependencies {
compileOnly(kotlinStdlib()) compileOnly(kotlinStdlib())
@@ -11,12 +11,12 @@ import org.jetbrains.kotlin.utils.SmartPrinter
private const val DEFAULT_INDENT: String = " " private const val DEFAULT_INDENT: String = " "
class SirAsSwiftSourcesPrinter : SirVisitor<Unit, SmartPrinter>() { public class SirAsSwiftSourcesPrinter : SirVisitor<Unit, SmartPrinter>() {
fun print(element: SirElement): String = buildString { public fun print(element: SirElement): String = buildString {
element.accept(this@SirAsSwiftSourcesPrinter, SmartPrinter(this)) element.accept(this@SirAsSwiftSourcesPrinter, SmartPrinter(this))
}.trim() }.trim()
override fun visitModule(module: SirModule, data: SmartPrinter) = with(data) { override fun visitModule(module: SirModule, data: SmartPrinter): Unit = with(data) {
module.declarations.forEach { module.declarations.forEach {
it.accept(this@SirAsSwiftSourcesPrinter, this) it.accept(this@SirAsSwiftSourcesPrinter, this)
if (module.declarations.last() != it) { if (module.declarations.last() != it) {