From c30c83ec8441f8bb20d52cff8a67a30d0486e0df Mon Sep 17 00:00:00 2001 From: Ting-Yuan Huang Date: Fri, 30 Aug 2024 15:15:42 -0700 Subject: [PATCH] Handle explicit JvmName (cherry picked from commit 6ed123e1defeb4afbd899af240e7267d94e05377) --- .../devtools/ksp/impl/ResolverAAImpl.kt | 29 ++++++++++++++----- .../devtools/ksp/impl/symbol/kotlin/util.kt | 13 +++++++-- 2 files changed, 33 insertions(+), 9 deletions(-) diff --git a/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/ResolverAAImpl.kt b/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/ResolverAAImpl.kt index 7e435a2b66..6f9142004c 100644 --- a/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/ResolverAAImpl.kt +++ b/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/ResolverAAImpl.kt @@ -505,15 +505,23 @@ class ResolverAAImpl( if (accessor.receiver.closestClassDeclaration()?.classKind == ClassKind.ANNOTATION_CLASS) { return accessor.receiver.simpleName.asString() } + + val symbol: KaPropertyAccessorSymbol? = when (accessor) { + is KSPropertyAccessorImpl -> accessor.ktPropertyAccessorSymbol + else -> null + } + + symbol?.explictJvmName()?.let { + return it + } + val prefix = if (accessor is KSPropertyGetter) { "get" } else { "set" } - val inlineSuffix = when (accessor) { - is KSPropertyAccessorImpl -> accessor.ktPropertyAccessorSymbol.inlineSuffix - else -> "" - } + + val inlineSuffix = symbol?.inlineSuffix ?: "" val mangledName = if (accessor.modifiers.contains(Modifier.INTERNAL)) { "\$${ktModule.name}" } else "" @@ -529,10 +537,17 @@ class ResolverAAImpl( }?.let { return it.name } - val inlineSuffix = when (declaration) { - is KSFunctionDeclarationImpl -> declaration.ktFunctionSymbol.inlineSuffix - else -> "" + + val symbol: KaFunctionSymbol? = when (declaration) { + is KSFunctionDeclarationImpl -> declaration.ktFunctionSymbol + else -> null + } + + symbol?.explictJvmName()?.let { + return it } + + val inlineSuffix = symbol?.inlineSuffix ?: "" val mangledName = if (declaration.modifiers.contains(Modifier.INTERNAL)) { "\$${ktModule.name}" } else "" diff --git a/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/symbol/kotlin/util.kt b/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/symbol/kotlin/util.kt index 57dab5301c..44cc9867d4 100644 --- a/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/symbol/kotlin/util.kt +++ b/kotlin-analysis-api/src/main/kotlin/com/google/devtools/ksp/impl/symbol/kotlin/util.kt @@ -965,7 +965,7 @@ internal val KaFunctionSymbol.inlineSuffix: String valueParameters.map { it.returnType }, returnType, analyze { - returnType.requiresMangling() && isKotlin && this@inlineSuffix.containingSymbol is KaClassSymbol + returnType.requiresMangling() && isKotlin && containingDeclaration != null } ) @@ -975,7 +975,16 @@ internal val KaPropertyAccessorSymbol.inlineSuffix: String mangleInlineSuffix( emptyList(), returnType, - returnType.requiresMangling() && isKotlin + analyze { + returnType.requiresMangling() && isKotlin && containingDeclaration?.containingDeclaration != null + } ) is KaPropertySetterSymbol -> mangleInlineSuffix(listOf(parameter.returnType), null, false) } + +private val jvmNameClassId = ClassId.fromString("kotlin/jvm/JvmName") +internal fun KaCallableSymbol.explictJvmName(): String? { + return annotations.singleOrNull() { + it.classId == jvmNameClassId + }?.arguments?.single()?.expression?.toValue() as? String +}