diff --git a/klib/src/main/kotlin/org/jetbrains/kotlin/cli/klib/KlibPrinter.kt b/klib/src/main/kotlin/org/jetbrains/kotlin/cli/klib/KlibPrinter.kt index c7b1ac92e4e..85ff4f24a30 100644 --- a/klib/src/main/kotlin/org/jetbrains/kotlin/cli/klib/KlibPrinter.kt +++ b/klib/src/main/kotlin/org/jetbrains/kotlin/cli/klib/KlibPrinter.kt @@ -20,10 +20,7 @@ import org.jetbrains.kotlin.backend.konan.descriptors.getPackageFragments import org.jetbrains.kotlin.builtins.KotlinBuiltIns import org.jetbrains.kotlin.descriptors.* import org.jetbrains.kotlin.descriptors.impl.DeclarationDescriptorVisitorEmptyBodies -import org.jetbrains.kotlin.renderer.AnnotationArgumentsRenderingPolicy -import org.jetbrains.kotlin.renderer.DescriptorRenderer -import org.jetbrains.kotlin.renderer.DescriptorRendererModifier -import org.jetbrains.kotlin.renderer.OverrideRenderingPolicy +import org.jetbrains.kotlin.renderer.* import org.jetbrains.kotlin.utils.Printer class KlibPrinter(out: Appendable) { @@ -70,6 +67,20 @@ class KlibPrinter(out: Appendable) { return renderer.render(this) } + private fun PropertyAccessorDescriptor.render() = buildString { + annotations.forEach { + append(Renderers.DEFAULT.renderAnnotation(it)).append(" ") + } + if (visibility != Visibilities.DEFAULT_VISIBILITY) { + append(visibility.displayName).append(" ") + } + when (this@render) { + is PropertyGetterDescriptor -> append("get") + is PropertySetterDescriptor -> append("set") + else -> throw AssertionError("Unknown accessor descriptor type: ${this@render}") + } + } + fun print(module: ModuleDescriptor) { module.accept(PrinterVisitor(), Unit) } @@ -84,8 +95,8 @@ class KlibPrinter(out: Appendable) { override fun visitPackageFragmentDescriptor(descriptor: PackageFragmentDescriptor, data: Unit) { val children = descriptor.getMemberScope().getContributedDescriptors().filter { it.shouldBePrinted } if (children.isNotEmpty()) { - val pacakgeName = descriptor.fqName.let { if (it.isRoot) "" else it.asString() } - val header = "package $pacakgeName" + val packageName = descriptor.fqName.let { if (it.isRoot) "" else it.asString() } + val header = "package $packageName" printer.printBody(header) { children.forEach { it.accept(this, data) } } @@ -104,7 +115,6 @@ class KlibPrinter(out: Appendable) { } else { printer.println(header) } - } override fun visitFunctionDescriptor(descriptor: FunctionDescriptor, data: Unit) { @@ -113,6 +123,20 @@ class KlibPrinter(out: Appendable) { override fun visitPropertyDescriptor(descriptor: PropertyDescriptor, data: Unit) { printer.println(descriptor.render()) + descriptor.getter?.let { getter -> + if (!getter.annotations.isEmpty()) { + printer.pushIndent() + printer.println(getter.render()) + printer.popIndent() + } + } + descriptor.setter?.let { setter -> + if (!setter.annotations.isEmpty() || setter.visibility != descriptor.visibility) { + printer.pushIndent() + printer.println(setter.render()) + printer.popIndent() + } + } } override fun visitConstructorDescriptor(descriptor: ConstructorDescriptor, data: Unit) { diff --git a/klib/src/test/kotlin/org/jetbrains/kotlin/cli/klib/test/ContentsTest.kt b/klib/src/test/kotlin/org/jetbrains/kotlin/cli/klib/test/ContentsTest.kt index df56b46857c..c6a152341c6 100644 --- a/klib/src/test/kotlin/org/jetbrains/kotlin/cli/klib/test/ContentsTest.kt +++ b/klib/src/test/kotlin/org/jetbrains/kotlin/cli/klib/test/ContentsTest.kt @@ -246,7 +246,7 @@ class ContentsTest { } @Test - @Ignore // TODO: Do we need to print the overridden method in the C entry? + // TODO: Support enum entry methods in serialization/deserialization. fun enum() = klibContents(testLibrary("Enum")) { """ package { @@ -254,11 +254,7 @@ class ContentsTest { enum class E private constructor(x: Int = ...) : Enum { enum entry A enum entry B - - enum entry C { - override fun enumFun(): Int - } - + enum entry C val enumVal: Int = 0 var enumVar: String val x: Int @@ -266,10 +262,36 @@ class ContentsTest { } } + """.trimIndent() + } + + @Test + // TODO: Support getter/setter annotations in serialization/deserialization. + fun accessors() = klibContents(testLibrary("Accessors")) { """ + package custom.pkg { + annotation class A constructor() : Annotation + + class Foo constructor() { + @A val annotated: Int = 0 + var annotatedAccessors: Int + get + set + val annotatedGetter: Int = 0 + get + var annotatedSetter: Int + set + var privateSetter: Int + private set + protected val protectedSimple: Int = 0 + val simple: Int = 0 + } + + } + """.trimIndent() } companion object { - val LIBRARY_DIRECTORY = Paths.get("build/konan/libs").resolve( HostManager.host.visibleName) + val LIBRARY_DIRECTORY = Paths.get("build/konan/libs").resolve( HostManager.hostName) } } \ No newline at end of file diff --git a/klib/src/test/testData/Accessors.kt b/klib/src/test/testData/Accessors.kt new file mode 100644 index 00000000000..705e1058ab6 --- /dev/null +++ b/klib/src/test/testData/Accessors.kt @@ -0,0 +1,26 @@ +package custom.pkg + +annotation class A + +class Foo { + val simple = 0 + + private val privateSimple = 0 + + protected val protectedSimple = 0 + + var privateSetter = 0 + private set + + @A val annotated = 0 + + val annotatedGetter = 0 + @A get + + var annotatedSetter = 0 + @A set + + var annotatedAccessors = 0 + @A set + @A get +}