From 70db3b4c6898fe9e44c3d532fec8b02663ebb6cd Mon Sep 17 00:00:00 2001 From: Vladimir Sukharev Date: Wed, 17 Aug 2022 14:23:12 +0000 Subject: [PATCH] Count in interfaces/classes/packages when resolving name clashes with "_" suffix mangling ^KT-41904 Fixed Merge-request: KT-MR-6862 Merged-by: Vladimir Sukharev --- .../kotlin/backend/konan/CAdapterGenerator.kt | 10 +++- .../backend.native/tests/build.gradle | 7 +++ .../produce_dynamic/kt-41904/hello-main.out | 5 ++ .../tests/produce_dynamic/kt-41904/hello.kt | 54 +++++++++++++++++++ .../tests/produce_dynamic/kt-41904/main.c | 25 +++++++++ 5 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 kotlin-native/backend.native/tests/produce_dynamic/kt-41904/hello-main.out create mode 100644 kotlin-native/backend.native/tests/produce_dynamic/kt-41904/hello.kt create mode 100644 kotlin-native/backend.native/tests/produce_dynamic/kt-41904/main.c diff --git a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/CAdapterGenerator.kt b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/CAdapterGenerator.kt index d246c3d2344..3047ac9d07e 100644 --- a/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/CAdapterGenerator.kt +++ b/kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/CAdapterGenerator.kt @@ -170,6 +170,11 @@ private class ExportedElementScope(val kind: ScopeKind, val name: String) { } } + // collects names of inner scopes to make sure function<->scope name clashes would be detected, and functions would be mangled with "_" suffix + fun collectInnerScopeName(innerScope: ExportedElementScope) { + scopeNames += innerScope.name + } + fun scopeUniqueName(descriptor: DeclarationDescriptor, shortName: Boolean): String { scopeNamesMap[descriptor to shortName]?.apply { return this } var computedName = when (descriptor) { @@ -787,8 +792,11 @@ internal class CAdapterGenerator(val context: Context) : DeclarationDescriptorVi return if (kind == DefinitionKind.C_HEADER_STRUCT) output("struct {", indent) if (kind == DefinitionKind.C_SOURCE_STRUCT) output(".${scope.name} = {", indent) + scope.scopes.forEach { + scope.collectInnerScopeName(it) + makeScopeDefinitions(it, kind, indent + 1) + } scope.elements.forEach { makeElementDefinition(it, kind, indent + 1) } - scope.scopes.forEach { makeScopeDefinitions(it, kind, indent + 1) } if (kind == DefinitionKind.C_HEADER_STRUCT) output("} ${scope.name};", indent) if (kind == DefinitionKind.C_SOURCE_STRUCT) output("},", indent) } diff --git a/kotlin-native/backend.native/tests/build.gradle b/kotlin-native/backend.native/tests/build.gradle index f18e6020102..6da93396b15 100644 --- a/kotlin-native/backend.native/tests/build.gradle +++ b/kotlin-native/backend.native/tests/build.gradle @@ -5225,6 +5225,13 @@ dynamicTest("kt39496") { useGoldenData = true } +dynamicTest("kt41904") { + disabled = (project.testTarget == 'wasm32') // wasm doesn't support -produce dynamic + source = "produce_dynamic/kt-41904/hello.kt" + cSource = "$projectDir/produce_dynamic/kt-41904/main.c" + useGoldenData = true +} + for (i in 0..2) { dynamicTest("kt42796_$i") { clangTool = "clang++" diff --git a/kotlin-native/backend.native/tests/produce_dynamic/kt-41904/hello-main.out b/kotlin-native/backend.native/tests/produce_dynamic/kt-41904/hello-main.out new file mode 100644 index 00000000000..74d55779251 --- /dev/null +++ b/kotlin-native/backend.native/tests/produce_dynamic/kt-41904/hello-main.out @@ -0,0 +1,5 @@ +TestInterface constructor(Int): 153.0 +TestInterface constructor(Double): 42.0 +InnerInterface constructor(Int): 153.0 +InnerInterface constructor(Double): 42.0 +TestInterface initialized from inner package: 19.0 diff --git a/kotlin-native/backend.native/tests/produce_dynamic/kt-41904/hello.kt b/kotlin-native/backend.native/tests/produce_dynamic/kt-41904/hello.kt new file mode 100644 index 00000000000..59efc13e3bf --- /dev/null +++ b/kotlin-native/backend.native/tests/produce_dynamic/kt-41904/hello.kt @@ -0,0 +1,54 @@ +/* + * Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. + */ + +// FILE: outerPackage.kt + +interface TestInterface { + fun doSomething(): Double +} + +// These functions have name clashes with an interface, so their names should be mangled with repetitive "_" suffixes +fun TestInterface(double: Double): TestInterface = object : TestInterface { + override fun doSomething(): Double { + return double + } +} +fun TestInterface(int: Int): TestInterface = object : TestInterface { + override fun doSomething(): Double { + return int.toDouble() + } +} + +class ContainingClass { + interface InnerInterface { + fun doSomething(): Double + } + + // These functions have name clashes with an interface, so their names should be mangled with repetitive "_" suffixes + fun InnerInterface(double: Double): InnerInterface = object : InnerInterface { + override fun doSomething(): Double { + return double + } + } + fun InnerInterface(int: Int): InnerInterface = object : InnerInterface { + override fun doSomething(): Double { + return int.toDouble() + } + } +} + +// this function is intended to have name clash with package TestPackage +fun TestPackage(double: Double): TestInterface = object : TestInterface { + override fun doSomething(): Double { + return double + } +} + + +// FILE: innerPackage.kt + +package TestPackage + +fun identity(double: Double) = double diff --git a/kotlin-native/backend.native/tests/produce_dynamic/kt-41904/main.c b/kotlin-native/backend.native/tests/produce_dynamic/kt-41904/main.c new file mode 100644 index 00000000000..1efbd028260 --- /dev/null +++ b/kotlin-native/backend.native/tests/produce_dynamic/kt-41904/main.c @@ -0,0 +1,25 @@ +/* + * Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. + */ +#include "testlib_api.h" +#define __ testlib_symbols()-> +#define T_(x) testlib_kref_ ## x + +#include + +int main(int argc, char** argv) { + T_(TestInterface) tiFromInt = __ kotlin.root.TestInterface_ (153); + printf("TestInterface constructor(Int): %.1f\n", __ kotlin.root.TestInterface.doSomething(tiFromInt)); + T_(TestInterface) tiFromDouble = __ kotlin.root.TestInterface__ (42.0); + printf("TestInterface constructor(Double): %.1f\n", __ kotlin.root.TestInterface.doSomething(tiFromDouble)); + + T_(ContainingClass) containingClassInstance = __ kotlin.root.ContainingClass.ContainingClass(); + T_(ContainingClass_InnerInterface) iiFromInt = __ kotlin.root.ContainingClass.InnerInterface_ (containingClassInstance, 153); + printf("InnerInterface constructor(Int): %.1f\n", __ kotlin.root.ContainingClass.InnerInterface.doSomething(iiFromInt)); + T_(ContainingClass_InnerInterface) iiFromDouble = __ kotlin.root.ContainingClass.InnerInterface__ (containingClassInstance, 42.0); + printf("InnerInterface constructor(Double): %.1f\n", __ kotlin.root.ContainingClass.InnerInterface.doSomething(iiFromDouble)); + + T_(TestInterface) tiFromInnerPackage = __ kotlin.root.TestPackage_ (__ kotlin.root.TestPackage.identity(19.0)); + printf("TestInterface initialized from inner package: %.1f\n", __ kotlin.root.TestInterface.doSomething(tiFromInnerPackage)); +}