[JS IR BE] support SAM conversions in DCE-driven mode
This commit is contained in:
committed by
Anton Bannykh
parent
8fcd73e3cb
commit
abc6ecaa1c
+10
-8
@@ -21,11 +21,9 @@ import org.jetbrains.kotlin.ir.IrStatement
|
||||
import org.jetbrains.kotlin.ir.builders.*
|
||||
import org.jetbrains.kotlin.ir.builders.declarations.*
|
||||
import org.jetbrains.kotlin.ir.declarations.*
|
||||
import org.jetbrains.kotlin.ir.expressions.IrExpression
|
||||
import org.jetbrains.kotlin.ir.expressions.IrGetValue
|
||||
import org.jetbrains.kotlin.ir.expressions.IrTypeOperator
|
||||
import org.jetbrains.kotlin.ir.expressions.IrTypeOperatorCall
|
||||
import org.jetbrains.kotlin.ir.expressions.*
|
||||
import org.jetbrains.kotlin.ir.expressions.impl.IrInstanceInitializerCallImpl
|
||||
import org.jetbrains.kotlin.ir.symbols.IrSymbol
|
||||
import org.jetbrains.kotlin.ir.types.IrType
|
||||
import org.jetbrains.kotlin.ir.types.classifierOrFail
|
||||
import org.jetbrains.kotlin.ir.types.isNullable
|
||||
@@ -58,9 +56,9 @@ abstract class SingleAbstractMethodLowering(val context: CommonBackendContext) :
|
||||
// Coming from the frontend, every SAM interface is associated with exactly one function type
|
||||
// (see SamType.getKotlinFunctionType). This is why we can cache implementations just based on
|
||||
// the superType.
|
||||
private val cachedImplementations = mutableMapOf<IrType, IrClass>()
|
||||
private val inlineCachedImplementations = mutableMapOf<IrType, IrClass>()
|
||||
private var enclosingContainer: IrDeclarationContainer? = null
|
||||
protected val cachedImplementations = mutableMapOf<IrType, IrClass>()
|
||||
protected val inlineCachedImplementations = mutableMapOf<IrType, IrClass>()
|
||||
protected var enclosingContainer: IrDeclarationContainer? = null
|
||||
|
||||
abstract fun getWrapperVisibility(expression: IrTypeOperatorCall, scopes: List<ScopeWithIr>): Visibility
|
||||
|
||||
@@ -81,6 +79,10 @@ abstract class SingleAbstractMethodLowering(val context: CommonBackendContext) :
|
||||
}
|
||||
}
|
||||
|
||||
protected open fun currentScopeSymbol(): IrSymbol? {
|
||||
return currentScope?.scope?.scopeOwnerSymbol
|
||||
}
|
||||
|
||||
override fun visitClassNew(declaration: IrClass): IrStatement {
|
||||
val prevContainer = enclosingContainer
|
||||
if (prevContainer == null || prevContainer is IrFile)
|
||||
@@ -99,7 +101,7 @@ abstract class SingleAbstractMethodLowering(val context: CommonBackendContext) :
|
||||
val erasedSuperType = getSuperTypeForWrapper(expression.typeOperand)
|
||||
val superType = if (expression.typeOperand.isNullable()) erasedSuperType.makeNullable() else erasedSuperType
|
||||
val invokable = expression.argument.transform(this, null)
|
||||
context.createIrBuilder(currentScope!!.scope.scopeOwnerSymbol).apply {
|
||||
context.createIrBuilder(currentScopeSymbol()!!).apply {
|
||||
// Do not generate a wrapper class for null, it has no invoke() anyway.
|
||||
if (invokable.isNullConst())
|
||||
return invokable
|
||||
|
||||
@@ -521,11 +521,11 @@ private val bridgesConstructionPhase = makeDeclarationTransformerPhase(
|
||||
prerequisite = setOf(suspendFunctionsLoweringPhase)
|
||||
)
|
||||
|
||||
private val singleAbstractMethodPhase = makeJsModulePhase(
|
||||
private val singleAbstractMethodPhase = makeBodyLoweringPhase(
|
||||
::JsSingleAbstractMethodLowering,
|
||||
name = "SingleAbstractMethod",
|
||||
description = "Replace SAM conversions with instances of interface-implementing classes"
|
||||
).toModuleLowering()
|
||||
)
|
||||
|
||||
private val typeOperatorLoweringPhase = makeBodyLoweringPhase(
|
||||
::TypeOperatorLowering,
|
||||
|
||||
+35
-2
@@ -5,22 +5,55 @@
|
||||
|
||||
package org.jetbrains.kotlin.ir.backend.js.lower
|
||||
|
||||
import org.jetbrains.kotlin.backend.common.ScopeWithIr
|
||||
import org.jetbrains.kotlin.backend.common.BodyLoweringPass
|
||||
import org.jetbrains.kotlin.backend.common.lower.SingleAbstractMethodLowering
|
||||
import org.jetbrains.kotlin.backend.common.ScopeWithIr
|
||||
import org.jetbrains.kotlin.descriptors.Visibilities
|
||||
import org.jetbrains.kotlin.descriptors.Visibility
|
||||
import org.jetbrains.kotlin.ir.backend.js.JsIrBackendContext
|
||||
import org.jetbrains.kotlin.ir.declarations.*
|
||||
import org.jetbrains.kotlin.ir.expressions.IrBody
|
||||
import org.jetbrains.kotlin.ir.symbols.IrSymbol
|
||||
import org.jetbrains.kotlin.ir.expressions.IrTypeOperatorCall
|
||||
import org.jetbrains.kotlin.ir.types.IrType
|
||||
import org.jetbrains.kotlin.ir.types.classOrNull
|
||||
import org.jetbrains.kotlin.ir.types.defaultType
|
||||
import org.jetbrains.kotlin.ir.util.file
|
||||
import org.jetbrains.kotlin.ir.util.parentClassOrNull
|
||||
import org.jetbrains.kotlin.ir.util.render
|
||||
|
||||
class JsSingleAbstractMethodLowering(context: JsIrBackendContext) : SingleAbstractMethodLowering(context) {
|
||||
class JsSingleAbstractMethodLowering(context: JsIrBackendContext) : SingleAbstractMethodLowering(context), BodyLoweringPass {
|
||||
|
||||
override fun getWrapperVisibility(expression: IrTypeOperatorCall, scopes: List<ScopeWithIr>): Visibility {
|
||||
return Visibilities.PRIVATE
|
||||
}
|
||||
|
||||
private var enclosingBodyContainer: IrDeclaration? = null
|
||||
|
||||
override fun lower(irFile: IrFile) {
|
||||
super<SingleAbstractMethodLowering>.lower(irFile)
|
||||
}
|
||||
|
||||
override fun lower(irBody: IrBody, container: IrDeclaration) {
|
||||
cachedImplementations.clear()
|
||||
inlineCachedImplementations.clear()
|
||||
enclosingContainer = container.parentClassOrNull ?: container.file
|
||||
enclosingBodyContainer = container
|
||||
|
||||
irBody.transformChildrenVoid()
|
||||
|
||||
for (wrapper in cachedImplementations.values + inlineCachedImplementations.values) {
|
||||
val parentClass = wrapper.parent as IrDeclarationContainer
|
||||
stageController.unrestrictDeclarationListsAccess {
|
||||
parentClass.declarations += wrapper
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun currentScopeSymbol(): IrSymbol? {
|
||||
return super.currentScopeSymbol() ?: (enclosingBodyContainer as? IrSymbolOwner)?.symbol
|
||||
}
|
||||
|
||||
override fun getSuperTypeForWrapper(typeOperand: IrType): IrType {
|
||||
// FE doesn't allow type parameters for now.
|
||||
// And since there is a to-do in common SingleAbstractMethodLowering (at function visitTypeOperator),
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
// !LANGUAGE: +NewInference +FunctionalInterfaceConversion +SamConversionPerArgument +SamConversionForKotlinFunctions
|
||||
// SKIP_DCE_DRIVEN
|
||||
|
||||
fun interface Foo {
|
||||
fun invoke(): String
|
||||
|
||||
-1
@@ -1,5 +1,4 @@
|
||||
// !LANGUAGE: +NewInference +FunctionalInterfaceConversion +SamConversionPerArgument +SamConversionForKotlinFunctions
|
||||
// SKIP_DCE_DRIVEN
|
||||
|
||||
// FILE: lib.kt
|
||||
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
// !LANGUAGE: +NewInference +FunctionalInterfaceConversion +SamConversionPerArgument +SamConversionForKotlinFunctions
|
||||
// SKIP_DCE_DRIVEN
|
||||
|
||||
|
||||
fun interface KRunnable {
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
// IGNORE_BACKEND: JS_IR
|
||||
|
||||
fun interface FunInterface {
|
||||
fun invoke()
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
// !LANGUAGE: +NewInference +FunctionalInterfaceConversion +SamConversionPerArgument
|
||||
// IGNORE_BACKEND_FIR: JVM_IR
|
||||
// SKIP_DCE_DRIVEN
|
||||
|
||||
fun interface MyRunnable {
|
||||
fun run()
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
// !LANGUAGE: +NewInference +FunctionalInterfaceConversion +SamConversionPerArgument +SamConversionForKotlinFunctions
|
||||
// SKIP_DCE_DRIVEN
|
||||
|
||||
fun interface Base {
|
||||
fun doStuff(): String
|
||||
@@ -27,4 +26,4 @@ fun box(): String {
|
||||
if (runProxy { 10 } != "10") return "fail2"
|
||||
|
||||
return runBase { "OK" }
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,5 @@
|
||||
// IGNORE_BACKEND_FIR: JVM_IR
|
||||
// IGNORE_BACKEND: JS_IR_ES6
|
||||
// SKIP_DCE_DRIVEN
|
||||
|
||||
fun interface FunWithReceiver {
|
||||
fun String.foo(): String
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
// !LANGUAGE: +NewInference +FunctionalInterfaceConversion +SamConversionPerArgument +SamConversionForKotlinFunctions
|
||||
// WITH_RUNTIME
|
||||
// SKIP_DCE_DRIVEN
|
||||
|
||||
fun interface MyRunnable {
|
||||
fun invoke()
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
// !LANGUAGE: +NewInference +FunctionalInterfaceConversion +SamConversionPerArgument +SamConversionForKotlinFunctions
|
||||
// SKIP_DCE_DRIVEN
|
||||
|
||||
// MODULE: m1
|
||||
// FILE: m1.kt
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
// IGNORE_BACKEND: JS_IR
|
||||
|
||||
interface I {
|
||||
fun inherited(s: String): String = privateInherited(s)
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
// !LANGUAGE: +NewInference +FunctionalInterfaceConversion +SamConversionPerArgument +SamConversionForKotlinFunctions
|
||||
// WITH_RUNTIME
|
||||
// SKIP_DCE_DRIVEN
|
||||
|
||||
fun interface KRunnable {
|
||||
fun invoke()
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
// !LANGUAGE: +NewInference +FunctionalInterfaceConversion +SamConversionPerArgument +SamConversionForKotlinFunctions
|
||||
// WITH_RUNTIME
|
||||
// SKIP_DCE_DRIVEN
|
||||
|
||||
fun interface Fn<T, R> {
|
||||
fun run(s: String, i: Int, t: T): R
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
// !LANGUAGE: +NewInference +FunctionalInterfaceConversion +SamConversionPerArgument +SamConversionForKotlinFunctions
|
||||
|
||||
// SKIP_DCE_DRIVEN
|
||||
|
||||
// This test should check argument coercion between the SAM and the lambda.
|
||||
// For now it checks that Char is boxed in JS
|
||||
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
// !LANGUAGE: +NewInference +FunctionalInterfaceConversion +SamConversionPerArgument +SamConversionForKotlinFunctions
|
||||
// WITH_RUNTIME
|
||||
// SKIP_DCE_DRIVEN
|
||||
|
||||
fun interface KRunnable {
|
||||
fun invoke()
|
||||
|
||||
-1
@@ -1,5 +1,4 @@
|
||||
// !LANGUAGE: +FunctionalInterfaceConversion
|
||||
// SKIP_DCE_DRIVEN
|
||||
|
||||
fun interface S {
|
||||
fun invoke(): String
|
||||
|
||||
-1
@@ -1,7 +1,6 @@
|
||||
// !LANGUAGE: +NewInference +FunctionalInterfaceConversion +SamConversionPerArgument +SamConversionForKotlinFunctions
|
||||
// WITH_COROUTINES
|
||||
// WITH_RUNTIME
|
||||
// SKIP_DCE_DRIVEN
|
||||
|
||||
import helpers.*
|
||||
import kotlin.coroutines.*
|
||||
|
||||
Reference in New Issue
Block a user