[JVM IR] KTIJ-24206 Add an option to stub orphaned expect symbols
- The cause for KTIJ-24206 is that the `expect` function's parent is an `IrFile` instead of an `IrClass`. This is because `ExpectDeclarationsRemoveLowering` removes `expect` declarations before `FileClassLowering` can replace `IrFile` parents. - That behavior is normally okay, but breaks down when an `expect` declaration has no associated `actual` declaration. In such cases, `ExpectDeclarationsRemoveLowering` doesn't replace `expect` symbols in expressions with their corresponding `actual` symbols, as it normally would. - The solution fills in `ExpectDeclarationsRemoveLowering`'s behavior by replacing `expect` symbols for which no `actual` symbols exist with stubs. See `stubOrphanedExpectSymbols`. - To not mess with the lowerings, `stubOrphanedExpectSymbols` is invoked during IR generation. It uses the same `ExpectSymbolTransformer` as `ExpectDeclarationRemover`. ^KTIJ-24206 fixed
This commit is contained in:
committed by
Space Team
parent
8b1e508740
commit
c9461a3827
+15
-3
@@ -60,7 +60,7 @@ open class JvmIrCodegenFactory(
|
||||
private val externalSymbolTable: SymbolTable? = null,
|
||||
private val jvmGeneratorExtensions: JvmGeneratorExtensionsImpl = JvmGeneratorExtensionsImpl(configuration),
|
||||
private val evaluatorFragmentInfoForPsi2Ir: EvaluatorFragmentInfo? = null,
|
||||
private val shouldStubAndNotLinkUnboundSymbols: Boolean = false,
|
||||
private val stubSettings: StubSettings = StubSettings(),
|
||||
) : CodegenFactory {
|
||||
|
||||
@IDEAPluginsCompatibilityAPI(IDEAPlatforms._221, message = "Please migrate to the other constructor", plugins = "Android Studio")
|
||||
@@ -81,7 +81,15 @@ open class JvmIrCodegenFactory(
|
||||
externalSymbolTable,
|
||||
jvmGeneratorExtensions,
|
||||
evaluatorFragmentInfoForPsi2Ir,
|
||||
shouldStubAndNotLinkUnboundSymbols
|
||||
StubSettings(shouldStubAndNotLinkUnboundSymbols = shouldStubAndNotLinkUnboundSymbols),
|
||||
)
|
||||
|
||||
/**
|
||||
* @param shouldStubOrphanedExpectSymbols See [stubOrphanedExpectSymbols].
|
||||
*/
|
||||
data class StubSettings(
|
||||
val shouldStubAndNotLinkUnboundSymbols: Boolean = false,
|
||||
val shouldStubOrphanedExpectSymbols: Boolean = false,
|
||||
)
|
||||
|
||||
data class JvmIrBackendInput(
|
||||
@@ -208,7 +216,7 @@ open class JvmIrCodegenFactory(
|
||||
}
|
||||
|
||||
|
||||
val irProviders = if (shouldStubAndNotLinkUnboundSymbols) {
|
||||
val irProviders = if (stubSettings.shouldStubAndNotLinkUnboundSymbols) {
|
||||
listOf(stubGenerator)
|
||||
} else {
|
||||
val stubGeneratorForMissingClasses = DeclarationStubGeneratorForNotFoundClasses(stubGenerator)
|
||||
@@ -232,6 +240,10 @@ open class JvmIrCodegenFactory(
|
||||
// We need to compile all files we reference in Klibs
|
||||
irModuleFragment.files.addAll(dependencies.flatMap { it.files })
|
||||
|
||||
if (stubSettings.shouldStubOrphanedExpectSymbols) {
|
||||
irModuleFragment.stubOrphanedExpectSymbols(stubGenerator)
|
||||
}
|
||||
|
||||
if (!input.configuration.getBoolean(JVMConfigurationKeys.DO_NOT_CLEAR_BINDING_CONTEXT)) {
|
||||
val originalBindingContext = input.bindingContext as? CleanableBindingContext
|
||||
?: error("BindingContext should be cleanable in JVM IR to avoid leaking memory: ${input.bindingContext}")
|
||||
|
||||
+132
@@ -0,0 +1,132 @@
|
||||
/*
|
||||
* Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.backend.jvm
|
||||
|
||||
import org.jetbrains.kotlin.backend.common.ir.ExpectSymbolTransformer
|
||||
import org.jetbrains.kotlin.descriptors.*
|
||||
import org.jetbrains.kotlin.ir.declarations.IrClass
|
||||
import org.jetbrains.kotlin.ir.declarations.IrDeclaration
|
||||
import org.jetbrains.kotlin.ir.declarations.IrModuleFragment
|
||||
import org.jetbrains.kotlin.ir.symbols.IrClassSymbol
|
||||
import org.jetbrains.kotlin.ir.symbols.IrConstructorSymbol
|
||||
import org.jetbrains.kotlin.ir.symbols.IrSimpleFunctionSymbol
|
||||
import org.jetbrains.kotlin.ir.util.DeclarationStubGenerator
|
||||
import org.jetbrains.kotlin.resolve.descriptorUtil.module
|
||||
import org.jetbrains.kotlin.resolve.multiplatform.findCompatibleActualsForExpected
|
||||
|
||||
/**
|
||||
* Replaces `expect` symbols for which no `actual` counterpart exists with an `actual` stub in all files of the [IrModuleFragment]. The
|
||||
* implementation keeps track of generated stubs and only generates a single stub for each unique `expect` symbol.
|
||||
*
|
||||
* [stubOrphanedExpectSymbols] is used by the IDE bytecode tool window to allow compiling source files with `expect` declarations for which
|
||||
* the compiled module has no `actual` declaration. (The `actual` declaration would be defined in a module dependent on the compiled
|
||||
* module, but choosing this module is non-trivial due to possibly multiple implementations of the same `expect` symbol. In addition, when
|
||||
* generating bytecode for a single source file, the number of source files to compile should be kept low. Stubbing helps with that.)
|
||||
*/
|
||||
internal fun IrModuleFragment.stubOrphanedExpectSymbols(stubGenerator: DeclarationStubGenerator) {
|
||||
val transformer = StubOrphanedExpectSymbolTransformer(stubGenerator)
|
||||
files.forEach(transformer::visitFile)
|
||||
}
|
||||
|
||||
private class StubOrphanedExpectSymbolTransformer(val stubGenerator: DeclarationStubGenerator) : ExpectSymbolTransformer() {
|
||||
|
||||
private val stubbedClasses = mutableMapOf<ClassDescriptor, IrClassSymbol>()
|
||||
private val stubbedProperties = mutableMapOf<PropertyDescriptor, ActualPropertyResult>()
|
||||
private val stubbedConstructors = mutableMapOf<ClassConstructorDescriptor, IrConstructorSymbol>()
|
||||
private val stubbedFunctions = mutableMapOf<FunctionDescriptor, IrSimpleFunctionSymbol>()
|
||||
|
||||
override fun getActualClass(descriptor: ClassDescriptor): IrClassSymbol? {
|
||||
if (!descriptor.isOrphanedExpect()) return null
|
||||
|
||||
return stubbedClasses.getOrPut(descriptor) {
|
||||
stubGenerator.generateClassStub(FakeActualClassDescriptor(descriptor)).symbol
|
||||
}
|
||||
}
|
||||
|
||||
override fun getActualProperty(descriptor: PropertyDescriptor): ActualPropertyResult? {
|
||||
if (!descriptor.isOrphanedExpect()) return null
|
||||
|
||||
return stubbedProperties.getOrPut(descriptor) {
|
||||
val irProperty =
|
||||
stubGenerator.generatePropertyStub(FakeActualPropertyDescriptor(descriptor)).apply { ensureClassParent(descriptor) }
|
||||
val irGetter = descriptor.getter?.let(::getActualFunction)
|
||||
val irSetter = descriptor.setter?.let(::getActualFunction)
|
||||
ActualPropertyResult(irProperty.symbol, irGetter, irSetter)
|
||||
}
|
||||
}
|
||||
|
||||
override fun getActualConstructor(descriptor: ClassConstructorDescriptor): IrConstructorSymbol? {
|
||||
if (!descriptor.isOrphanedExpect()) return null
|
||||
|
||||
return stubbedConstructors.getOrPut(descriptor) {
|
||||
stubGenerator.generateConstructorStub(FakeActualClassConstructorDescriptor(descriptor)).symbol
|
||||
}
|
||||
}
|
||||
|
||||
override fun getActualFunction(descriptor: FunctionDescriptor): IrSimpleFunctionSymbol? {
|
||||
if (!descriptor.isOrphanedExpect()) return null
|
||||
|
||||
return stubbedFunctions.getOrPut(descriptor) {
|
||||
stubGenerator
|
||||
.generateFunctionStub(FakeActualFunctionDescriptor(descriptor), createPropertyIfNeeded = false)
|
||||
.apply { ensureClassParent(descriptor) }
|
||||
.symbol
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* If an `actual` symbol exists, we shouldn't stub the `expect` symbol. This will be performed by
|
||||
* [org.jetbrains.kotlin.backend.common.lower.ExpectDeclarationsRemoveLowering] during lowering.
|
||||
*/
|
||||
private fun MemberDescriptor.isOrphanedExpect(): Boolean = findCompatibleActualsForExpected(module).isEmpty()
|
||||
|
||||
/**
|
||||
* [descriptor] should be the original descriptor, because the copied `actual` descriptor has no source.
|
||||
*/
|
||||
private fun IrDeclaration.ensureClassParent(descriptor: MemberDescriptor) {
|
||||
if (parent !is IrClass) {
|
||||
parent = stubGenerator.generateOrGetFacadeClass(descriptor) ?: return
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private class FakeActualClassDescriptor(original: ClassDescriptor) : ClassDescriptor by original {
|
||||
override fun isActual(): Boolean = true
|
||||
override fun isExpect(): Boolean = false
|
||||
|
||||
override fun getSource(): SourceElement = SourceElement.NO_SOURCE
|
||||
override fun getOriginal(): ClassDescriptor = this
|
||||
}
|
||||
|
||||
private class FakeActualPropertyDescriptor(original: PropertyDescriptor) : PropertyDescriptor by original {
|
||||
override fun isActual(): Boolean = true
|
||||
override fun isExpect(): Boolean = false
|
||||
|
||||
override fun getSource(): SourceElement = SourceElement.NO_SOURCE
|
||||
override fun getOriginal(): PropertyDescriptor = this
|
||||
}
|
||||
|
||||
private class FakeActualClassConstructorDescriptor(original: ClassConstructorDescriptor) : ClassConstructorDescriptor by original {
|
||||
override fun isActual(): Boolean = true
|
||||
override fun isExpect(): Boolean = false
|
||||
|
||||
override fun getSource(): SourceElement = SourceElement.NO_SOURCE
|
||||
override fun getOriginal(): ClassConstructorDescriptor = this
|
||||
}
|
||||
|
||||
private class FakeActualFunctionDescriptor(original: FunctionDescriptor) : FunctionDescriptor by original {
|
||||
override fun isActual(): Boolean = true
|
||||
override fun isExpect(): Boolean = false
|
||||
|
||||
// `actual` functions are stubbed without providing a body. Hence, they may not be inlined, even if the `expect` function is marked as
|
||||
// `inline`. Given that inlining requires meaningful bodies (assuming the generated bytecode is of interest), it does not suffice to
|
||||
// just supply an empty body stub.
|
||||
override fun isInline(): Boolean = false
|
||||
|
||||
override fun getSource(): SourceElement = SourceElement.NO_SOURCE
|
||||
override fun getOriginal(): FunctionDescriptor = this
|
||||
}
|
||||
Reference in New Issue
Block a user