From 550163cc7bbeb1f22372390877ce952e0316677f Mon Sep 17 00:00:00 2001 From: Svyatoslav Scherbina Date: Mon, 28 Jan 2019 18:29:37 +0300 Subject: [PATCH] Fix #2571 Add swift_name attribute to properties in framework header too. --- .../konan/objcexport/ObjCExportHeaderGenerator.kt | 5 +++-- .../kotlin/backend/konan/objcexport/StubRenderer.kt | 13 +++++++++---- .../kotlin/backend/konan/objcexport/stubs.kt | 9 +++++++-- backend.native/tests/framework/values/values.kt | 3 +++ backend.native/tests/framework/values/values.swift | 5 +++++ 5 files changed, 27 insertions(+), 8 deletions(-) diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/objcexport/ObjCExportHeaderGenerator.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/objcexport/ObjCExportHeaderGenerator.kt index 85c72ae5653..ee59c12cd2c 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/objcexport/ObjCExportHeaderGenerator.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/objcexport/ObjCExportHeaderGenerator.kt @@ -435,7 +435,8 @@ abstract class ObjCExportHeaderGenerator( descriptor.enumEntries.forEach { val entryName = namer.getEnumEntrySelector(it) - +ObjCProperty(entryName, null, type, listOf("class", "readonly")) + +ObjCProperty(entryName, null, type, listOf("class", "readonly"), + declarationAttributes = listOf(swiftNameAttribute(entryName))) } } else -> { @@ -657,7 +658,7 @@ abstract class ObjCExportHeaderGenerator( val getterSelector = getSelector(baseProperty.getter!!) val getterName: String? = if (getterSelector != name) getterSelector else null - return ObjCProperty(name, property, type, attributes, setterName, getterName) + return ObjCProperty(name, property, type, attributes, setterName, getterName, listOf(swiftNameAttribute(name))) } private fun buildMethod(method: FunctionDescriptor, baseMethod: FunctionDescriptor): ObjCMethod { diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/objcexport/StubRenderer.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/objcexport/StubRenderer.kt index 21acf8c8f43..a2bc1ad2cd7 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/objcexport/StubRenderer.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/objcexport/StubRenderer.kt @@ -46,9 +46,9 @@ object StubRenderer { } fun ObjCProperty.getAllAttributes(): List { - if (getterName == null && setterName == null) return attributes + if (getterName == null && setterName == null) return propertyAttributes - val allAttributes = attributes.toMutableList() + val allAttributes = propertyAttributes.toMutableList() getterName?.let { allAttributes += "getter=$it" } setterName?.let { allAttributes += "setter=$it" } return allAttributes @@ -65,6 +65,7 @@ object StubRenderer { append("@property") appendAttributes() appendTypeAndName() + appendPostfixDeclarationAttributes(property.declarationAttributes) append(';') } @@ -105,8 +106,7 @@ object StubRenderer { } fun appendAttributes() { - if (method.attributes.isNotEmpty()) append(' ') - method.attributes.joinTo(this, separator = " ", transform = ::renderAttribute) + appendPostfixDeclarationAttributes(method.attributes) } appendStaticness() @@ -116,6 +116,11 @@ object StubRenderer { append(';') } + private fun Appendable.appendPostfixDeclarationAttributes(attributes: List) { + if (attributes.isNotEmpty()) this.append(' ') + attributes.joinTo(this, separator = " ", transform = this@StubRenderer::renderAttribute) + } + private fun ObjCProtocol.renderProtocolHeader() = buildString { append("@protocol ") append(name) diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/objcexport/stubs.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/objcexport/stubs.kt index 96ac3f2c8a7..60fd9061503 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/objcexport/stubs.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/objcexport/stubs.kt @@ -47,9 +47,14 @@ class ObjCParameter(name: String, class ObjCProperty(name: String, descriptor: PropertyDescriptor?, val type: ObjCType, - val attributes: List, + val propertyAttributes: List, val setterName: String? = null, - val getterName: String? = null) : Stub(name, descriptor) + val getterName: String? = null, + val declarationAttributes: List = emptyList()) : Stub(name, descriptor) { + + @Deprecated("", ReplaceWith("this.propertyAttributes"), DeprecationLevel.WARNING) + val attributes: List get() = propertyAttributes +} private fun buildMethodName(selectors: List, parameters: List): String = if (selectors.size == 1 && parameters.size == 0) { diff --git a/backend.native/tests/framework/values/values.kt b/backend.native/tests/framework/values/values.kt index 1af9f7d2fe5..54523d48f7f 100644 --- a/backend.native/tests/framework/values/values.kt +++ b/backend.native/tests/framework/values/values.kt @@ -266,3 +266,6 @@ class Bridge : BridgeBase() { } fun Any.same() = this + +// https://github.com/JetBrains/kotlin-native/issues/2571 +val PROPERTY_NAME_MUST_NOT_BE_ALTERED_BY_SWIFT = 111 diff --git a/backend.native/tests/framework/values/values.swift b/backend.native/tests/framework/values/values.swift index 11d86ec5d7c..e08ff1dad15 100644 --- a/backend.native/tests/framework/values/values.swift +++ b/backend.native/tests/framework/values/values.swift @@ -490,6 +490,10 @@ func testPureSwiftClasses() throws { try assertEquals(actual: "pure", expected: ValuesKt.iFunExt(PureSwiftKotlinInterfaceImpl())) } +func testNames() throws { + try assertEquals(actual: ValuesKt.PROPERTY_NAME_MUST_NOT_BE_ALTERED_BY_SWIFT, expected: 111) +} + // -------- Execution of the test -------- class ValuesTests : TestProvider { @@ -526,6 +530,7 @@ class ValuesTests : TestProvider { TestCase(name: "TestInlineClasses", method: withAutorelease(testInlineClasses)), TestCase(name: "TestShared", method: withAutorelease(testShared)), TestCase(name: "TestPureSwiftClasses", method: withAutorelease(testPureSwiftClasses)), + TestCase(name: "TestNames", method: withAutorelease(testNames)), ] } }