From 7a5ded4cf853cdd22ba463805c8177f53fb83164 Mon Sep 17 00:00:00 2001 From: Ting-Yuan Huang Date: Fri, 30 Aug 2024 15:38:06 -0700 Subject: [PATCH] Partially fix mangling of internal functions The other part is passing the right module names for dependencies. (cherry picked from commit 2bd017e7ff5c4bbc9c201d9ab00eaf5ee0c26039) --- .../devtools/ksp/impl/ResolverAAImpl.kt | 8 ++---- .../devtools/ksp/impl/symbol/kotlin/util.kt | 28 +++++++++++++++++++ 2 files changed, 30 insertions(+), 6 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 6f9142004c..8a3d11fd7c 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 @@ -522,9 +522,7 @@ class ResolverAAImpl( } val inlineSuffix = symbol?.inlineSuffix ?: "" - val mangledName = if (accessor.modifiers.contains(Modifier.INTERNAL)) { - "\$${ktModule.name}" - } else "" + val mangledName = symbol?.internalSuffix ?: "" return "${prefix}${accessor.receiver.simpleName.asString().capitalize()}$inlineSuffix$mangledName" } @@ -548,9 +546,7 @@ class ResolverAAImpl( } val inlineSuffix = symbol?.inlineSuffix ?: "" - val mangledName = if (declaration.modifiers.contains(Modifier.INTERNAL)) { - "\$${ktModule.name}" - } else "" + val mangledName = symbol?.internalSuffix ?: "" return declaration.simpleName.asString() + inlineSuffix + mangledName } 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 44cc9867d4..c6408ba337 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 @@ -51,6 +51,8 @@ import org.jetbrains.kotlin.analysis.api.impl.base.types.KaBaseStarTypeProjectio import org.jetbrains.kotlin.analysis.api.impl.base.types.KaBaseTypeArgumentWithVariance import org.jetbrains.kotlin.analysis.api.platform.lifetime.KotlinAlwaysAccessibleLifetimeToken import org.jetbrains.kotlin.analysis.api.projectStructure.KaLibraryModule +import org.jetbrains.kotlin.analysis.api.projectStructure.KaLibrarySourceModule +import org.jetbrains.kotlin.analysis.api.projectStructure.KaSourceModule import org.jetbrains.kotlin.analysis.api.symbols.* import org.jetbrains.kotlin.analysis.api.symbols.markers.KaDeclarationContainerSymbol import org.jetbrains.kotlin.analysis.api.types.* @@ -988,3 +990,29 @@ internal fun KaCallableSymbol.explictJvmName(): String? { it.classId == jvmNameClassId }?.arguments?.single()?.expression?.toValue() as? String } + +internal val KaDeclarationSymbol.internalSuffix: String + get() = analyze { + if (visibility != KaSymbolVisibility.INTERNAL) + return@analyze "" + + // Skip top level functions and properties + when (this@internalSuffix) { + is KaPropertyAccessorSymbol -> { + if (containingDeclaration?.containingDeclaration == null) + return@analyze "" + } + is KaFunctionSymbol -> { + if (containingDeclaration == null) + return@analyze "" + } + else -> {} + } + + fun String.toSuffix(): String = "\$$this" + when (val module = containingModule) { + is KaSourceModule -> module.name.toSuffix() + is KaLibraryModule -> module.libraryName.toSuffix() + else -> "" + } + }