[klib] Print accessors

This commit is contained in:
Ilya Matveev
2018-02-20 19:03:23 +03:00
committed by ilmat192
parent ba56d17cec
commit 2e958f0219
3 changed files with 86 additions and 14 deletions
@@ -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) "<root>" else it.asString() }
val header = "package $pacakgeName"
val packageName = descriptor.fqName.let { if (it.isRoot) "<root>" 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) {
@@ -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 <root> {
@@ -254,11 +254,7 @@ class ContentsTest {
enum class E private constructor(x: Int = ...) : Enum<E> {
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)
}
}
+26
View File
@@ -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
}