From 47f8dedfa5efcfbe8172ba4b0b143075c8c6b15b Mon Sep 17 00:00:00 2001 From: Sergej Jaskiewicz Date: Wed, 8 Nov 2023 12:19:59 +0100 Subject: [PATCH] [IR] Remove methods for computing full mangled names from non-IR manglers A full mangled name of a declaration is a mangled name that includes the mangled names of all the declaration's parents. We don't use full mangled names for building `IdSignature`s. We use them in two places: - Generating stable names for JavaScript functions in Kotlin/JS (see `NameTables.kt`) - Generating names for LLVM symbols from lowered IR functions in Kotlin/Native (see `BinaryInterface.kt`) In both of these places we run the IR mangler, hence we don't need this functionality in other manglers. --- .../kotlin/fir/backend/FirMangler.kt | 1 - .../kotlin/fir/signaturer/FirMangler.kt | 4 +- .../jetbrains/kotlin/ir/util/KotlinMangler.kt | 67 +++++++++---------- .../serialization/mangle/ManglerChecker.kt | 6 -- .../DescriptorBasedKotlinManglerImpl.kt | 12 +--- .../DisabledIdSignatureDescriptor.kt | 9 --- 6 files changed, 36 insertions(+), 63 deletions(-) diff --git a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/FirMangler.kt b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/FirMangler.kt index 1cfec42d5a3..44db18b7785 100644 --- a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/FirMangler.kt +++ b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/FirMangler.kt @@ -12,7 +12,6 @@ import org.jetbrains.kotlin.backend.common.serialization.mangle.SpecialDeclarati import org.jetbrains.kotlin.fir.declarations.FirDeclaration abstract class FirMangler : AbstractKotlinMangler() { - override fun FirDeclaration.mangleString(compatibleMode: Boolean): String = getMangleComputer(MangleMode.FULL, compatibleMode).computeMangle(this) override fun FirDeclaration.signatureString(compatibleMode: Boolean): String = getMangleComputer(MangleMode.SIGNATURE, compatibleMode).computeMangle(this) diff --git a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/signaturer/FirMangler.kt b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/signaturer/FirMangler.kt index 9f4a90d158d..24f4e01209b 100644 --- a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/signaturer/FirMangler.kt +++ b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/signaturer/FirMangler.kt @@ -25,7 +25,9 @@ class Ir2FirManglerAdapter(private val delegate: FirMangler) : AbstractKotlinMan override fun IrDeclaration.isExported(compatibleMode: Boolean): Boolean = delegate.run { fir().isExported(compatibleMode) } - override fun IrDeclaration.mangleString(compatibleMode: Boolean): String = delegate.run { fir().mangleString(compatibleMode) } + override fun IrDeclaration.mangleString(compatibleMode: Boolean): String { + error("Should not be called") + } override fun IrDeclaration.signatureString(compatibleMode: Boolean): String = delegate.run { fir().signatureString(compatibleMode) } diff --git a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/util/KotlinMangler.kt b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/util/KotlinMangler.kt index ceffaf521a9..8aca76934c4 100644 --- a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/util/KotlinMangler.kt +++ b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/util/KotlinMangler.kt @@ -5,9 +5,7 @@ package org.jetbrains.kotlin.ir.util -import org.jetbrains.kotlin.descriptors.ClassDescriptor import org.jetbrains.kotlin.descriptors.DeclarationDescriptor -import org.jetbrains.kotlin.descriptors.PropertyDescriptor import org.jetbrains.kotlin.ir.declarations.IrDeclaration interface KotlinMangler { @@ -16,25 +14,6 @@ interface KotlinMangler { fun D.isExported(compatibleMode: Boolean): Boolean - /** - * Returns the mangled name for the declaration [D], prefixed by mangled names of all its parents. - * - * For example, for `foo`'s getter in the following code: - * ```kotlin - * class Test { - * val foo: Int - * } - * ``` - * the result of this function will be `"Test#(){}kotlin.Int"` or `"Test#(){}"` (depending on the target platform). - * - * The result of this function is only used for assigning names to binary symbols of Kotlin functions in the final executable produced - * by Kotlin/Native, and for generating stable names in the JavaScript code produced by Kotlin/JS. **It does not affect klib ABI.** - * - * @param compatibleMode If `true`, the mangled names of property backing fields are just those fields' names. - * Otherwise, mangles such fields exactly as their corresponding properties. - */ - fun D.mangleString(compatibleMode: Boolean): String - /** * Returns the mangled name for the declaration [D] to be used for computing that declaration's [IdSignature]. * @@ -55,16 +34,6 @@ interface KotlinMangler { */ fun D.signatureString(compatibleMode: Boolean): String - /** - * Computes the hash code of the string returned by [mangleString] using the CityHash64 algorithm. - * - * **The result of this function does not affect klib ABI.** - * - * @param compatibleMode If `true`, the mangled names of property backing fields are just those fields' names. - * Otherwise, mangles such fields exactly as their corresponding properties. - */ - fun D.hashedMangle(compatibleMode: Boolean): Long = mangleString(compatibleMode).hashMangle - /** * Computes the hash code of the string returned by [signatureString] using the CityHash64 algorithm. * @@ -86,13 +55,41 @@ interface KotlinMangler { interface DescriptorMangler : KotlinMangler { override val manglerName: String get() = "Descriptor" - - fun ClassDescriptor.mangleEnumEntryString(compatibleMode: Boolean): String - - fun PropertyDescriptor.mangleFieldString(compatibleMode: Boolean): String } interface IrMangler : KotlinMangler { + + /** + * Returns the mangled name for the declaration, prefixed by mangled names of all its parents. + * + * For example, for `foo`'s getter in the following code: + * ```kotlin + * class Test { + * val foo: Int + * } + * ``` + * the result of this function will be `"Test#(){}kotlin.Int"` or `"Test#(){}"` + * (depending on the target platform). + * + * The result of this function is only used for assigning names to binary symbols of Kotlin functions in the final executable + * produced by Kotlin/Native, and for generating stable names in the JavaScript code produced by Kotlin/JS. + * **It does not affect klib ABI.** + * + * @param compatibleMode If `true`, the mangled names of property backing fields are just those fields' names. + * Otherwise, mangles such fields exactly as their corresponding properties. + */ + fun IrDeclaration.mangleString(compatibleMode: Boolean): String + + /** + * Computes the hash code of the string returned by [mangleString] using the CityHash64 algorithm. + * + * **The result of this function does not affect klib ABI.** + * + * @param compatibleMode If `true`, the mangled names of property backing fields are just those fields' names. + * Otherwise, mangles such fields exactly as their corresponding properties. + */ + fun IrDeclaration.hashedMangle(compatibleMode: Boolean): Long = mangleString(compatibleMode).hashMangle + override val manglerName: String get() = "Ir" } diff --git a/compiler/ir/serialization.common/src/org/jetbrains/kotlin/backend/common/serialization/mangle/ManglerChecker.kt b/compiler/ir/serialization.common/src/org/jetbrains/kotlin/backend/common/serialization/mangle/ManglerChecker.kt index e9b4882faa2..29fec57ee2f 100644 --- a/compiler/ir/serialization.common/src/org/jetbrains/kotlin/backend/common/serialization/mangle/ManglerChecker.kt +++ b/compiler/ir/serialization.common/src/org/jetbrains/kotlin/backend/common/serialization/mangle/ManglerChecker.kt @@ -61,8 +61,6 @@ class ManglerChecker( private fun IrDeclaration.shouldBeSkipped(): Boolean = accept(skipper, null) private fun KotlinMangler.isExportCheck(declaration: IrDeclaration) = !declaration.shouldBeSkipped() && declaration.isExported(false) - private fun KotlinMangler.stringMangle(declaration: IrDeclaration) = - declaration.mangleString(compatibleMode = false) private fun KotlinMangler.signatureMangle(declaration: IrDeclaration) = declaration.signatureString(compatibleMode = false) @@ -99,10 +97,6 @@ class ManglerChecker( if (!exported) return - manglers.checkAllEqual("", { stringMangle(declaration) }) { m1, r1, m2, r2 -> - error("FULL: ${declaration.render()}\n ${m1.manglerName}: $r1\n ${m2.manglerName}: $r2\n") - } - manglers.checkAllEqual("", { signatureMangle(declaration) }) { m1, r1, m2, r2 -> error("SIG: ${declaration.render()}\n ${m1.manglerName}: $r1\n ${m2.manglerName}: $r2\n") } diff --git a/compiler/ir/serialization.common/src/org/jetbrains/kotlin/backend/common/serialization/mangle/descriptor/DescriptorBasedKotlinManglerImpl.kt b/compiler/ir/serialization.common/src/org/jetbrains/kotlin/backend/common/serialization/mangle/descriptor/DescriptorBasedKotlinManglerImpl.kt index 596c9ac8546..57780be4072 100644 --- a/compiler/ir/serialization.common/src/org/jetbrains/kotlin/backend/common/serialization/mangle/descriptor/DescriptorBasedKotlinManglerImpl.kt +++ b/compiler/ir/serialization.common/src/org/jetbrains/kotlin/backend/common/serialization/mangle/descriptor/DescriptorBasedKotlinManglerImpl.kt @@ -19,12 +19,6 @@ abstract class DescriptorBasedKotlinManglerImpl : AbstractKotlinMangler delegate.run { descriptor.mangleEnumEntryString(compatibleMode) } - is IrField -> delegate.run { descriptor.mangleFieldString(compatibleMode) } - else -> delegate.run { descriptor.mangleString(compatibleMode) } - } + error("Should not be called") } override fun IrDeclaration.signatureString(compatibleMode: Boolean): String = delegate.run { descriptor.signatureString(compatibleMode) } diff --git a/compiler/ir/serialization.jvm/src/org/jetbrains/kotlin/backend/jvm/serialization/DisabledIdSignatureDescriptor.kt b/compiler/ir/serialization.jvm/src/org/jetbrains/kotlin/backend/jvm/serialization/DisabledIdSignatureDescriptor.kt index 12f94d83983..c065090136f 100644 --- a/compiler/ir/serialization.jvm/src/org/jetbrains/kotlin/backend/jvm/serialization/DisabledIdSignatureDescriptor.kt +++ b/compiler/ir/serialization.jvm/src/org/jetbrains/kotlin/backend/jvm/serialization/DisabledIdSignatureDescriptor.kt @@ -20,17 +20,8 @@ object DisabledDescriptorMangler : KotlinMangler.DescriptorMangler { override fun DeclarationDescriptor.isExported(compatibleMode: Boolean): Boolean = error("Should not be called") - override fun DeclarationDescriptor.mangleString(compatibleMode: Boolean): String = - error("Should not be called") - override fun DeclarationDescriptor.signatureString(compatibleMode: Boolean): String = error("Should not be called") - - override fun ClassDescriptor.mangleEnumEntryString(compatibleMode: Boolean): String = - error("Should not be called") - - override fun PropertyDescriptor.mangleFieldString(compatibleMode: Boolean): String = - error("Should not be called") } object DisabledIdSignatureDescriptor : IdSignatureDescriptor(DisabledDescriptorMangler) {