Add SIR builder and printer for classes #KT-65905 fixed
Merge-request: KT-MR-14478 Merged-by: Artem Olkov <artem.olkov@jetbrains.com>
This commit is contained in:
@@ -41,9 +41,22 @@ public class SirAsSwiftSourcesPrinter(private val printer: SmartPrinter) : SirVi
|
||||
println("import ${import.moduleName}")
|
||||
}
|
||||
|
||||
override fun visitClass(klass: SirClass): Unit = with(printer) {
|
||||
printVisibility(klass)
|
||||
println(
|
||||
"class ",
|
||||
klass.name.swiftIdentifier,
|
||||
" {"
|
||||
)
|
||||
withIndent {
|
||||
klass.acceptChildren(this@SirAsSwiftSourcesPrinter)
|
||||
}
|
||||
println("}")
|
||||
}
|
||||
|
||||
override fun visitVariable(variable: SirVariable): Unit = with(printer) {
|
||||
printVisibility(variable)
|
||||
print(
|
||||
variable.visibility.takeIf { it != SirVisibility.INTERNAL }?.let { "${it.swift} " } ?: "",
|
||||
if (variable.isStatic) "static " else "",
|
||||
"var ",
|
||||
variable.name.swiftIdentifier,
|
||||
@@ -82,8 +95,8 @@ public class SirAsSwiftSourcesPrinter(private val printer: SmartPrinter) : SirVi
|
||||
|
||||
override fun visitFunction(function: SirFunction): Unit = with(printer) {
|
||||
function.documentation?.let { println(it) }
|
||||
printVisibility(function)
|
||||
print(
|
||||
function.visibility.takeIf { it != SirVisibility.INTERNAL }?.let { "${it.swift} " } ?: "",
|
||||
if (function.isStatic) {
|
||||
"static "
|
||||
} else {
|
||||
@@ -121,8 +134,8 @@ public class SirAsSwiftSourcesPrinter(private val printer: SmartPrinter) : SirVi
|
||||
}
|
||||
|
||||
override fun visitEnum(enum: SirEnum): Unit = with(printer) {
|
||||
printVisibility(enum)
|
||||
println(
|
||||
enum.visibility.takeIf { it != SirVisibility.INTERNAL }?.let { "${it.swift} " } ?: "",
|
||||
"enum ",
|
||||
enum.name.swiftIdentifier,
|
||||
" {"
|
||||
@@ -167,4 +180,10 @@ private val SirNamedDeclaration.swiftFqName: String
|
||||
|
||||
private val simpleIdentifierRegex = Regex("[_a-zA-Z][_a-zA-Z0-9]*")
|
||||
|
||||
private val String.swiftIdentifier get() = if (simpleIdentifierRegex.matches(this)) this else "`$this`"
|
||||
private val String.swiftIdentifier get() = if (simpleIdentifierRegex.matches(this)) this else "`$this`"
|
||||
|
||||
internal fun SmartPrinter.printVisibility(decl: SirDeclaration) {
|
||||
print(
|
||||
decl.visibility.takeIf { it != SirVisibility.INTERNAL }?.let { "${it.swift} " } ?: ""
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
public class Foo {
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
public class OUTER_CLASS {
|
||||
public class INNER_CLASS {
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
public enum MyEnum {
|
||||
public class Foo {
|
||||
}
|
||||
}
|
||||
+68
@@ -6,6 +6,8 @@
|
||||
package org.jetbrains.kotlin.sir.printer
|
||||
|
||||
import org.jetbrains.kotlin.sir.*
|
||||
import org.jetbrains.kotlin.sir.builder.buildClass
|
||||
import org.jetbrains.kotlin.sir.builder.buildEnum
|
||||
import org.jetbrains.kotlin.sir.builder.buildFunction
|
||||
import org.jetbrains.kotlin.sir.builder.buildModule
|
||||
import org.jetbrains.kotlin.sir.util.SirSwiftModule
|
||||
@@ -275,6 +277,72 @@ class SirAsSwiftSourcesPrinterTests {
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `should print empty class`() {
|
||||
|
||||
val module = buildModule {
|
||||
name = "Test"
|
||||
declarations.add(
|
||||
buildClass {
|
||||
origin = SirOrigin.Unknown
|
||||
visibility = SirVisibility.PUBLIC
|
||||
name = "Foo"
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
runTest(
|
||||
module,
|
||||
"testData/empty_class"
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `should print empty class inside enum`() {
|
||||
|
||||
val module = buildModule {
|
||||
name = "Test"
|
||||
declarations.add(
|
||||
buildEnum {
|
||||
origin = SirOrigin.Unknown
|
||||
name = "MyEnum"
|
||||
|
||||
declarations.add(
|
||||
buildClass {
|
||||
origin = SirOrigin.Unknown
|
||||
visibility = SirVisibility.PUBLIC
|
||||
name = "Foo"
|
||||
})})}
|
||||
|
||||
runTest(
|
||||
module,
|
||||
"testData/empty_class_inside_enum"
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `should print empty class inside class`() {
|
||||
|
||||
val module = buildModule {
|
||||
name = "Test"
|
||||
declarations.add(
|
||||
buildClass {
|
||||
origin = SirOrigin.Unknown
|
||||
name = "OUTER_CLASS"
|
||||
|
||||
declarations.add(
|
||||
buildClass {
|
||||
origin = SirOrigin.Unknown
|
||||
visibility = SirVisibility.PUBLIC
|
||||
name = "INNER_CLASS"
|
||||
})})}
|
||||
|
||||
runTest(
|
||||
module,
|
||||
"testData/empty_class_inside_class"
|
||||
)
|
||||
}
|
||||
|
||||
private fun runTest(module: SirModule, goldenDataFile: String) {
|
||||
val expectedSwiftSrc = File(KtTestUtil.getHomeDirectory()).resolve("$goldenDataFile.golden.swift")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user