JVM_IR: lower calls to @JvmStatic functions from other files.
Note: this currently results in invalid IR (but valid bytecode) if the @JvmStatic function is imported, because its IR representation is unlowered and therefore has a dispatch receiver, but the call will not.
This commit is contained in:
+9
-10
@@ -51,10 +51,8 @@ internal val jvmStaticAnnotationPhase = makeIrFilePhase(
|
|||||||
private class JvmStaticAnnotationLowering(val context: JvmBackendContext) : IrElementTransformerVoid(), FileLoweringPass {
|
private class JvmStaticAnnotationLowering(val context: JvmBackendContext) : IrElementTransformerVoid(), FileLoweringPass {
|
||||||
override fun lower(irFile: IrFile) {
|
override fun lower(irFile: IrFile) {
|
||||||
CompanionObjectJvmStaticLowering(context).runOnFilePostfix(irFile)
|
CompanionObjectJvmStaticLowering(context).runOnFilePostfix(irFile)
|
||||||
|
SingletonObjectJvmStaticLowering(context).runOnFilePostfix(irFile)
|
||||||
val functionsMadeStatic =
|
irFile.transformChildrenVoid(MakeCallsStatic(context))
|
||||||
SingletonObjectJvmStaticLowering(context).apply { runOnFilePostfix(irFile) }.functionsMadeStatic
|
|
||||||
irFile.transformChildrenVoid(MakeCallsStatic(context, functionsMadeStatic))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -167,8 +165,6 @@ private class CompanionObjectJvmStaticLowering(val context: JvmBackendContext) :
|
|||||||
private class SingletonObjectJvmStaticLowering(
|
private class SingletonObjectJvmStaticLowering(
|
||||||
val context: JvmBackendContext
|
val context: JvmBackendContext
|
||||||
) : ClassLoweringPass {
|
) : ClassLoweringPass {
|
||||||
val functionsMadeStatic: MutableSet<IrFunctionSymbol> = mutableSetOf()
|
|
||||||
|
|
||||||
override fun lower(irClass: IrClass) {
|
override fun lower(irClass: IrClass) {
|
||||||
if (!irClass.isObject || irClass.isCompanion) return
|
if (!irClass.isObject || irClass.isCompanion) return
|
||||||
|
|
||||||
@@ -178,7 +174,6 @@ private class SingletonObjectJvmStaticLowering(
|
|||||||
jvmStaticFunction.dispatchReceiverParameter?.let { oldDispatchReceiverParameter ->
|
jvmStaticFunction.dispatchReceiverParameter?.let { oldDispatchReceiverParameter ->
|
||||||
jvmStaticFunction.dispatchReceiverParameter = null
|
jvmStaticFunction.dispatchReceiverParameter = null
|
||||||
modifyBody(jvmStaticFunction, irClass, oldDispatchReceiverParameter)
|
modifyBody(jvmStaticFunction, irClass, oldDispatchReceiverParameter)
|
||||||
functionsMadeStatic.add(jvmStaticFunction.symbol)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -188,12 +183,16 @@ private class SingletonObjectJvmStaticLowering(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun IrFunction.isJvmStaticInSingleton(): Boolean {
|
||||||
|
val parentClass = parent as? IrClass ?: return false
|
||||||
|
return isJvmStaticFunction(this) && parentClass.isObject && !parentClass.isCompanion
|
||||||
|
}
|
||||||
|
|
||||||
private class MakeCallsStatic(
|
private class MakeCallsStatic(
|
||||||
val context: JvmBackendContext,
|
val context: JvmBackendContext
|
||||||
val functionsMadeStatic: Set<IrFunctionSymbol>
|
|
||||||
) : IrElementTransformerVoid() {
|
) : IrElementTransformerVoid() {
|
||||||
override fun visitCall(expression: IrCall): IrExpression {
|
override fun visitCall(expression: IrCall): IrExpression {
|
||||||
if (functionsMadeStatic.contains(expression.symbol)) {
|
if (expression.symbol.owner.isJvmStaticInSingleton() && expression.dispatchReceiver != null) {
|
||||||
return context.createIrBuilder(expression.symbol, expression.startOffset, expression.endOffset).irBlock(expression) {
|
return context.createIrBuilder(expression.symbol, expression.startOffset, expression.endOffset).irBlock(expression) {
|
||||||
// OldReceiver has to be evaluated for its side effects.
|
// OldReceiver has to be evaluated for its side effects.
|
||||||
val oldReceiver = super.visitExpression(expression.dispatchReceiver!!)
|
val oldReceiver = super.visitExpression(expression.dispatchReceiver!!)
|
||||||
|
|||||||
-1
@@ -1,6 +1,5 @@
|
|||||||
// FILE: 1.kt
|
// FILE: 1.kt
|
||||||
// SKIP_INLINE_CHECK_IN: inlineFun$default
|
// SKIP_INLINE_CHECK_IN: inlineFun$default
|
||||||
// IGNORE_BACKEND: JVM_IR
|
|
||||||
// TARGET_BACKEND: JVM
|
// TARGET_BACKEND: JVM
|
||||||
//WITH_RUNTIME
|
//WITH_RUNTIME
|
||||||
package test
|
package test
|
||||||
|
|||||||
Reference in New Issue
Block a user