Skip to content

Commit

Permalink
Mangle jvm names for inline class
Browse files Browse the repository at this point in the history
  • Loading branch information
ting-yuan committed Sep 3, 2024
1 parent 1438dae commit 207b849
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -510,10 +510,14 @@ class ResolverAAImpl(
} else {
"set"
}
val inlineSuffix = when (accessor) {
is KSPropertyAccessorImpl -> accessor.ktPropertyAccessorSymbol.inlineSuffix
else -> ""
}
val mangledName = if (accessor.modifiers.contains(Modifier.INTERNAL)) {
"\$${ktModule.name}"
} else ""
return "${prefix}${accessor.receiver.simpleName.asString().capitalize()}$mangledName"
return "${prefix}${accessor.receiver.simpleName.asString().capitalize()}$inlineSuffix$mangledName"
}

// TODO: handle library symbols
Expand All @@ -525,10 +529,14 @@ class ResolverAAImpl(
}?.let {
return it.name
}
val inlineSuffix = when (declaration) {
is KSFunctionDeclarationImpl -> declaration.ktFunctionSymbol.inlineSuffix
else -> ""
}
val mangledName = if (declaration.modifiers.contains(Modifier.INTERNAL)) {
"\$${ktModule.name}"
} else ""
return declaration.simpleName.asString() + mangledName
return declaration.simpleName.asString() + inlineSuffix + mangledName
}

override fun getKSNameFromString(name: String): KSName {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ import org.jetbrains.kotlin.analysis.api.symbols.*
import org.jetbrains.kotlin.analysis.api.symbols.markers.KaDeclarationContainerSymbol
import org.jetbrains.kotlin.analysis.api.types.*
import org.jetbrains.kotlin.builtins.jvm.JavaToKotlinClassMap
import org.jetbrains.kotlin.codegen.state.InfoForMangling
import org.jetbrains.kotlin.codegen.state.collectFunctionSignatureForManglingSuffix
import org.jetbrains.kotlin.codegen.state.md5base64
import org.jetbrains.kotlin.fir.declarations.FirResolvePhase
import org.jetbrains.kotlin.fir.declarations.getTargetType
import org.jetbrains.kotlin.fir.expressions.FirAnnotation
Expand Down Expand Up @@ -923,3 +926,56 @@ fun <T : KaSymbol?> T.tryResolveToTypePhase(): T {
(this as? KaFirSymbol<*>)?.firSymbol?.lazyResolveToPhase(FirResolvePhase.TYPES)
return this
}

private val KaType.upperBoundIfFlexible: KaType
get() = (this as? KaFlexibleType)?.upperBound ?: this

private fun KaType.requiresMangling(): Boolean = (symbol as? KaNamedClassSymbol)?.isInline ?: false

private fun KaType.asInfoForMangling(): InfoForMangling? {
val upperBound = upperBoundIfFlexible
val fqName = upperBound.symbol?.classId?.asSingleFqName()?.toUnsafe() ?: return null
val isValue = upperBound.requiresMangling()
val isNullable = upperBound.nullability.isNullable
return InfoForMangling(fqName = fqName, isValue = isValue, isNullable = isNullable)
}

private fun mangleInlineSuffix(
parameters: List<KaType>,
returnType: KaType?,
shouldMangleReturnType: Boolean
): String {
val signature = collectFunctionSignatureForManglingSuffix(
false,
parameters.any { it.requiresMangling() },
parameters.map { it.asInfoForMangling() },
if (shouldMangleReturnType) returnType?.asInfoForMangling() else null
) ?: return ""
return "-${md5base64(signature)}"
}

private val KaSymbol.isKotlin: Boolean
get() = when (origin) {
KaSymbolOrigin.SOURCE, KaSymbolOrigin.LIBRARY -> true
else -> false
}

internal val KaFunctionSymbol.inlineSuffix: String
get() = mangleInlineSuffix(
valueParameters.map { it.returnType },
returnType,
analyze {
returnType.requiresMangling() && isKotlin && this@inlineSuffix.containingSymbol is KaClassSymbol
}
)

internal val KaPropertyAccessorSymbol.inlineSuffix: String
get() = when (this) {
is KaPropertyGetterSymbol ->
mangleInlineSuffix(
emptyList(),
returnType,
returnType.requiresMangling() && isKotlin
)
is KaPropertySetterSymbol -> mangleInlineSuffix(listOf(parameter.returnType), null, false)
}

0 comments on commit 207b849

Please sign in to comment.