Unbind FIR mangler from use-site session (by Simon Ogorodnik)

This commit is contained in:
Mikhail Glukhikh
2023-01-11 14:23:35 +01:00
committed by Space Team
parent 2237c964b5
commit ecc81098a6
9 changed files with 34 additions and 35 deletions
@@ -12,11 +12,12 @@ import org.jetbrains.kotlin.fir.backend.jvm.FirJvmKotlinMangler
import org.jetbrains.kotlin.fir.signaturer.FirBasedSignatureComposer import org.jetbrains.kotlin.fir.signaturer.FirBasedSignatureComposer
@NoMutableState @NoMutableState
data class IdeSessionComponents(val signatureComposer: FirBasedSignatureComposer): FirSessionComponent { data class IdeSessionComponents(val signatureComposer: FirBasedSignatureComposer) : FirSessionComponent {
companion object { companion object {
fun create(session: FirSession) = IdeSessionComponents( fun create(): IdeSessionComponents = IdeSessionComponents(
signatureComposer = FirBasedSignatureComposer(FirJvmKotlinMangler(session)) signatureComposer = FirBasedSignatureComposer(FirJvmKotlinMangler())
) )
} }
} }
val FirSession.ideSessionComponents: IdeSessionComponents by FirSession.sessionComponentAccessor() val FirSession.ideSessionComponents: IdeSessionComponents by FirSession.sessionComponentAccessor()
@@ -38,7 +38,7 @@ import org.jetbrains.kotlin.load.java.createJavaClassFinder
@SessionConfiguration @SessionConfiguration
internal fun LLFirSession.registerIdeComponents(project: Project) { internal fun LLFirSession.registerIdeComponents(project: Project) {
register(IdeSessionComponents::class, IdeSessionComponents.create(this)) register(IdeSessionComponents::class, IdeSessionComponents.create())
register(FirCachesFactory::class, FirThreadSafeCachesFactory) register(FirCachesFactory::class, FirThreadSafeCachesFactory)
register(SealedClassInheritorsProvider::class, project.createSealedInheritorsProvider()) register(SealedClassInheritorsProvider::class, project.createSealedInheritorsProvider())
register(SealedClassInheritorsProvider::class, project.createSealedInheritorsProvider()) register(SealedClassInheritorsProvider::class, project.createSealedInheritorsProvider())
@@ -590,7 +590,7 @@ class K2JsIrCompiler : CLICompiler<K2JSCompilerArguments>() {
session, scopeSession, firFiles + commonFirFiles, session, scopeSession, firFiles + commonFirFiles,
configuration.languageVersionSettings, signaturer, configuration.languageVersionSettings, signaturer,
fir2IrExtensions, fir2IrExtensions,
FirJvmKotlinMangler(session), // TODO: replace with potentially simpler JS version FirJvmKotlinMangler(), // TODO: replace with potentially simpler JS version
JsManglerIr, IrFactoryImpl, JsManglerIr, IrFactoryImpl,
Fir2IrVisibilityConverter.Default, Fir2IrVisibilityConverter.Default,
Fir2IrJvmSpecialAnnotationSymbolProvider(), // TODO: replace with appropriate (probably empty) implementation Fir2IrJvmSpecialAnnotationSymbolProvider(), // TODO: replace with appropriate (probably empty) implementation
@@ -87,7 +87,7 @@ private fun ModuleCompilerAnalyzedOutput.convertToIr(
return Fir2IrConverter.createModuleFragmentWithSignaturesIfNeeded( return Fir2IrConverter.createModuleFragmentWithSignaturesIfNeeded(
session, scopeSession, fir, session, scopeSession, fir,
session.languageVersionSettings, signaturer, fir2IrExtensions, session.languageVersionSettings, signaturer, fir2IrExtensions,
FirJvmKotlinMangler(session), FirJvmKotlinMangler(),
JvmIrMangler, IrFactoryImpl, FirJvmVisibilityConverter, JvmIrMangler, IrFactoryImpl, FirJvmVisibilityConverter,
Fir2IrJvmSpecialAnnotationSymbolProvider(), Fir2IrJvmSpecialAnnotationSymbolProvider(),
irGeneratorExtensions, irGeneratorExtensions,
@@ -100,7 +100,7 @@ private fun ModuleCompilerAnalyzedOutput.convertToIr(
return Fir2IrConverter.createModuleFragmentWithoutSignatures( return Fir2IrConverter.createModuleFragmentWithoutSignatures(
session, scopeSession, fir, session, scopeSession, fir,
session.languageVersionSettings, fir2IrExtensions, session.languageVersionSettings, fir2IrExtensions,
FirJvmKotlinMangler(session), FirJvmKotlinMangler(),
JvmIrMangler, IrFactoryImpl, FirJvmVisibilityConverter, JvmIrMangler, IrFactoryImpl, FirJvmVisibilityConverter,
Fir2IrJvmSpecialAnnotationSymbolProvider(), Fir2IrJvmSpecialAnnotationSymbolProvider(),
irGeneratorExtensions, irGeneratorExtensions,
@@ -6,13 +6,12 @@
package org.jetbrains.kotlin.fir.backend.jvm package org.jetbrains.kotlin.fir.backend.jvm
import org.jetbrains.kotlin.backend.common.serialization.mangle.* import org.jetbrains.kotlin.backend.common.serialization.mangle.*
import org.jetbrains.kotlin.fir.FirSession
import org.jetbrains.kotlin.fir.NoMutableState import org.jetbrains.kotlin.fir.NoMutableState
import org.jetbrains.kotlin.fir.declarations.FirDeclaration import org.jetbrains.kotlin.fir.declarations.FirDeclaration
import org.jetbrains.kotlin.fir.signaturer.FirMangler import org.jetbrains.kotlin.fir.signaturer.FirMangler
@NoMutableState @NoMutableState
class FirJvmKotlinMangler(private val session: FirSession) : AbstractKotlinMangler<FirDeclaration>(), FirMangler { class FirJvmKotlinMangler : AbstractKotlinMangler<FirDeclaration>(), FirMangler {
override fun FirDeclaration.mangleString(compatibleMode: Boolean): String = getMangleComputer(MangleMode.FULL, compatibleMode).computeMangle(this) override fun FirDeclaration.mangleString(compatibleMode: Boolean): String = getMangleComputer(MangleMode.FULL, compatibleMode).computeMangle(this)
@@ -31,6 +30,6 @@ class FirJvmKotlinMangler(private val session: FirSession) : AbstractKotlinMangl
} }
override fun getMangleComputer(mode: MangleMode, compatibleMode: Boolean): KotlinMangleComputer<FirDeclaration> { override fun getMangleComputer(mode: MangleMode, compatibleMode: Boolean): KotlinMangleComputer<FirDeclaration> {
return FirJvmMangleComputer(StringBuilder(256), mode, session) return FirJvmMangleComputer(StringBuilder(256), mode)
} }
} }
@@ -31,8 +31,7 @@ import org.jetbrains.kotlin.utils.addToStdlib.ifNotEmpty
open class FirJvmMangleComputer( open class FirJvmMangleComputer(
private val builder: StringBuilder, private val builder: StringBuilder,
private val mode: MangleMode, private val mode: MangleMode
private val session: FirSession
) : FirVisitor<Unit, Boolean>(), KotlinMangleComputer<FirDeclaration> { ) : FirVisitor<Unit, Boolean>(), KotlinMangleComputer<FirDeclaration> {
private val typeParameterContainer = ArrayList<FirMemberDeclaration>(4) private val typeParameterContainer = ArrayList<FirMemberDeclaration>(4)
@@ -43,7 +42,7 @@ open class FirJvmMangleComputer(
open fun FirFunction.platformSpecificSuffix(): String? = open fun FirFunction.platformSpecificSuffix(): String? =
if (this is FirSimpleFunction && name.asString() == "main") if (this is FirSimpleFunction && name.asString() == "main")
this@FirJvmMangleComputer.session.firProvider.getFirCallableContainerFile(symbol)?.name this.moduleData.session.firProvider.getFirCallableContainerFile(symbol)?.name
else null else null
open fun FirFunction.specialValueParamPrefix(param: FirValueParameter): String = "" open fun FirFunction.specialValueParamPrefix(param: FirValueParameter): String = ""
@@ -51,7 +50,7 @@ open class FirJvmMangleComputer(
private fun addReturnType(): Boolean = true private fun addReturnType(): Boolean = true
override fun copy(newMode: MangleMode): FirJvmMangleComputer = override fun copy(newMode: MangleMode): FirJvmMangleComputer =
FirJvmMangleComputer(builder, newMode, session) FirJvmMangleComputer(builder, newMode)
private fun StringBuilder.appendName(s: String) { private fun StringBuilder.appendName(s: String) {
if (mode.fqn) { if (mode.fqn) {
@@ -90,7 +89,7 @@ open class FirJvmMangleComputer(
else -> return else -> return
} }
if (parentClassId != null && !parentClassId.isLocal) { if (parentClassId != null && !parentClassId.isLocal) {
val parentClassLike = this@FirJvmMangleComputer.session.symbolProvider.getClassLikeSymbolByClassId(parentClassId)?.fir val parentClassLike = this.moduleData.session.symbolProvider.getClassLikeSymbolByClassId(parentClassId)?.fir
?: error("Attempt to find parent ($parentClassId) for probably-local declaration!") ?: error("Attempt to find parent ($parentClassId) for probably-local declaration!")
if (parentClassLike is FirRegularClass || parentClassLike is FirTypeAlias) { if (parentClassLike is FirRegularClass || parentClassLike is FirTypeAlias) {
parentClassLike.accept(this@FirJvmMangleComputer, false) parentClassLike.accept(this@FirJvmMangleComputer, false)
@@ -151,13 +150,13 @@ open class FirJvmMangleComputer(
contextReceivers.forEach { contextReceivers.forEach {
builder.appendSignature(MangleConstant.CONTEXT_RECEIVER_PREFIX) builder.appendSignature(MangleConstant.CONTEXT_RECEIVER_PREFIX)
mangleType(builder, it.typeRef.coneType) mangleType(builder, it.typeRef.coneType, moduleData.session)
} }
val receiverType = receiverParameter?.typeRef ?: (this as? FirPropertyAccessor)?.propertySymbol?.fir?.receiverParameter?.typeRef val receiverType = receiverParameter?.typeRef ?: (this as? FirPropertyAccessor)?.propertySymbol?.fir?.receiverParameter?.typeRef
receiverType?.let { receiverType?.let {
builder.appendSignature(MangleConstant.EXTENSION_RECEIVER_PREFIX) builder.appendSignature(MangleConstant.EXTENSION_RECEIVER_PREFIX)
mangleType(builder, it.coneType) mangleType(builder, it.coneType, moduleData.session)
} }
valueParameters.collectForMangler(builder, MangleConstant.VALUE_PARAMETERS) { valueParameters.collectForMangler(builder, MangleConstant.VALUE_PARAMETERS) {
@@ -170,7 +169,7 @@ open class FirJvmMangleComputer(
} }
if (!isCtor && !returnTypeRef.isUnit && addReturnType()) { if (!isCtor && !returnTypeRef.isUnit && addReturnType()) {
mangleType(builder, returnTypeRef.coneType) mangleType(builder, returnTypeRef.coneType, moduleData.session)
} }
} }
@@ -190,7 +189,7 @@ open class FirJvmMangleComputer(
} }
private fun mangleValueParameter(vpBuilder: StringBuilder, param: FirValueParameter) { private fun mangleValueParameter(vpBuilder: StringBuilder, param: FirValueParameter) {
mangleType(vpBuilder, param.returnTypeRef.coneType) mangleType(vpBuilder, param.returnTypeRef.coneType, param.moduleData.session)
if (param.isVararg) { if (param.isVararg) {
vpBuilder.appendSignature(MangleConstant.VAR_ARG_MARK) vpBuilder.appendSignature(MangleConstant.VAR_ARG_MARK)
@@ -202,7 +201,7 @@ open class FirJvmMangleComputer(
tpBuilder.appendSignature(MangleConstant.UPPER_BOUND_SEPARATOR) tpBuilder.appendSignature(MangleConstant.UPPER_BOUND_SEPARATOR)
param.bounds.map { it.coneType }.collectForMangler(tpBuilder, MangleConstant.UPPER_BOUNDS) { param.bounds.map { it.coneType }.collectForMangler(tpBuilder, MangleConstant.UPPER_BOUNDS) {
mangleType(this, it) mangleType(this, it, param.moduleData.session)
} }
} }
@@ -215,12 +214,12 @@ open class FirJvmMangleComputer(
appendSignature(parent.typeParameters.indexOf(typeParameter)) appendSignature(parent.typeParameters.indexOf(typeParameter))
} }
private fun mangleType(tBuilder: StringBuilder, type: ConeKotlinType) { private fun mangleType(tBuilder: StringBuilder, type: ConeKotlinType, declarationSiteSession: FirSession) {
when (type) { when (type) {
is ConeLookupTagBasedType -> { is ConeLookupTagBasedType -> {
when (val symbol = type.lookupTag.toSymbol(session)) { when (val symbol = type.lookupTag.toSymbol(declarationSiteSession)) {
is FirTypeAliasSymbol -> { is FirTypeAliasSymbol -> {
mangleType(tBuilder, type.fullyExpandedType(session)) mangleType(tBuilder, type.fullyExpandedType(declarationSiteSession), declarationSiteSession)
return return
} }
@@ -242,7 +241,7 @@ open class FirJvmMangleComputer(
appendSignature(MangleConstant.VARIANCE_SEPARATOR) appendSignature(MangleConstant.VARIANCE_SEPARATOR)
} }
mangleType(this, arg.type) mangleType(this, arg.type, declarationSiteSession)
} }
} }
} }
@@ -258,11 +257,11 @@ open class FirJvmMangleComputer(
} }
is ConeRawType -> { is ConeRawType -> {
mangleType(tBuilder, type.lowerBound) mangleType(tBuilder, type.lowerBound, declarationSiteSession)
} }
is ConeFlexibleType -> { is ConeFlexibleType -> {
with(session.typeContext) { with(declarationSiteSession.typeContext) {
// Need to reproduce type approximation done for flexible types in TypeTranslator. // Need to reproduce type approximation done for flexible types in TypeTranslator.
// For now, we replicate the current behaviour of Fir2IrTypeConverter and just take the upper bound // For now, we replicate the current behaviour of Fir2IrTypeConverter and just take the upper bound
val upper = type.upperBound val upper = type.upperBound
@@ -272,23 +271,23 @@ open class FirJvmMangleComputer(
lower.replaceArguments(upper.getArguments()) lower.replaceArguments(upper.getArguments())
} else lower } else lower
val mixed = if (upper.isNullable) intermediate.makeNullable() else intermediate.makeDefinitelyNotNullOrNotNull() val mixed = if (upper.isNullable) intermediate.makeNullable() else intermediate.makeDefinitelyNotNullOrNotNull()
mangleType(tBuilder, mixed as ConeKotlinType) mangleType(tBuilder, mixed as ConeKotlinType, declarationSiteSession)
} else mangleType(tBuilder, upper) } else mangleType(tBuilder, upper, declarationSiteSession)
} }
} }
is ConeDefinitelyNotNullType -> { is ConeDefinitelyNotNullType -> {
// E.g. not-null type parameter in Java // E.g. not-null type parameter in Java
mangleType(tBuilder, type.original) mangleType(tBuilder, type.original, declarationSiteSession)
} }
is ConeCapturedType -> { is ConeCapturedType -> {
mangleType(tBuilder, type.lowerType ?: type.constructor.supertypes!!.first()) mangleType(tBuilder, type.lowerType ?: type.constructor.supertypes!!.first(), declarationSiteSession)
} }
is ConeIntersectionType -> { is ConeIntersectionType -> {
// TODO: add intersectionTypeApproximation // TODO: add intersectionTypeApproximation
mangleType(tBuilder, type.intersectedTypes.first()) mangleType(tBuilder, type.intersectedTypes.first(), declarationSiteSession)
} }
else -> error("Unexpected type $type") else -> error("Unexpected type $type")
@@ -319,7 +318,7 @@ open class FirJvmMangleComputer(
variable.receiverParameter?.typeRef?.let { variable.receiverParameter?.typeRef?.let {
builder.appendSignature(MangleConstant.EXTENSION_RECEIVER_PREFIX) builder.appendSignature(MangleConstant.EXTENSION_RECEIVER_PREFIX)
mangleType(builder, it.coneType) mangleType(builder, it.coneType, variable.moduleData.session)
} }
variable.typeParameters.withIndex().toList().collectForMangler(builder, MangleConstant.TYPE_PARAMETERS) { (index, typeParameter) -> variable.typeParameters.withIndex().toList().collectForMangler(builder, MangleConstant.TYPE_PARAMETERS) { (index, typeParameter) ->
@@ -120,7 +120,7 @@ fun AbstractFirAnalyzerFacade.convertToJsIr(
session, scopeSession, firFiles + commonFirFiles, session, scopeSession, firFiles + commonFirFiles,
languageVersionSettings, signaturer, languageVersionSettings, signaturer,
fir2IrExtensions, fir2IrExtensions,
FirJvmKotlinMangler(session), // TODO: replace with potentially simpler JS version FirJvmKotlinMangler(), // TODO: replace with potentially simpler JS version
JsManglerIr, IrFactoryImpl, JsManglerIr, IrFactoryImpl,
Fir2IrVisibilityConverter.Default, Fir2IrVisibilityConverter.Default,
Fir2IrJvmSpecialAnnotationSymbolProvider(), // TODO: replace with appropriate (probably empty) implementation Fir2IrJvmSpecialAnnotationSymbolProvider(), // TODO: replace with appropriate (probably empty) implementation
@@ -121,7 +121,7 @@ class FirAnalyzerFacade(
session, _scopeSession!!, firFiles!!, session, _scopeSession!!, firFiles!!,
languageVersionSettings, signaturer, languageVersionSettings, signaturer,
fir2IrExtensions, fir2IrExtensions,
FirJvmKotlinMangler(session), JvmIrMangler, IrFactoryImpl, FirJvmKotlinMangler(), JvmIrMangler, IrFactoryImpl,
FirJvmVisibilityConverter, FirJvmVisibilityConverter,
Fir2IrJvmSpecialAnnotationSymbolProvider(), Fir2IrJvmSpecialAnnotationSymbolProvider(),
irGeneratorExtensions, irGeneratorExtensions,
@@ -72,7 +72,7 @@ internal fun PhaseContext.fir2Ir(
input.session, input.scopeSession, input.firFiles + commonFirFiles, input.session, input.scopeSession, input.firFiles + commonFirFiles,
configuration.languageVersionSettings, signaturer, configuration.languageVersionSettings, signaturer,
fir2IrExtensions, fir2IrExtensions,
FirJvmKotlinMangler(input.session), // TODO: replace with potentially simpler Konan version FirJvmKotlinMangler(), // TODO: replace with potentially simpler Konan version
KonanManglerIr, IrFactoryImpl, KonanManglerIr, IrFactoryImpl,
Fir2IrVisibilityConverter.Default, Fir2IrVisibilityConverter.Default,
Fir2IrJvmSpecialAnnotationSymbolProvider(), // TODO: replace with appropriate (probably empty) implementation Fir2IrJvmSpecialAnnotationSymbolProvider(), // TODO: replace with appropriate (probably empty) implementation