[IR] Add IrClass.invokeFun helper and use it throughout

This commit is contained in:
Sergej Jaskiewicz
2022-03-30 18:41:50 +03:00
committed by Space
parent 4b3a5ad875
commit 026f292f4f
4 changed files with 9 additions and 10 deletions
@@ -246,8 +246,7 @@ class CallableReferenceLowering(private val context: CommonBackendContext) : Bod
}
private fun createInvokeMethod(clazz: IrClass): IrSimpleFunction {
val superMethods = superFunctionInterface.declarations.filterIsInstance<IrSimpleFunction>()
val superMethod = superMethods.single { it.name.asString() == "invoke" }
val superMethod = superFunctionInterface.invokeFun!!
return clazz.addFunction {
setSourceRange(if (isLambda) function else reference)
name = superMethod.name
@@ -255,9 +254,7 @@ class CallableReferenceLowering(private val context: CommonBackendContext) : Bod
isSuspend = superMethod.isSuspend
isOperator = superMethod.isOperator
}.apply {
val secondSuperMethods: List<IrSimpleFunction>? =
secondFunctionInterface?.declarations?.filterIsInstance<IrSimpleFunction>()
val secondSuperMethod = secondSuperMethods?.single { it.name.asString() == "invoke" }
val secondSuperMethod = secondFunctionInterface?.let { it.invokeFun!! }
overriddenSymbols = listOfNotNull(
superMethod.symbol,
@@ -218,7 +218,7 @@ class InteropCallableReferenceLowering(val context: JsIrBackendContext) : BodyLo
lambdaClass: IrClass,
newDeclarations: MutableList<IrDeclaration>
): IrBlockBody {
val invokeFun = lambdaClass.declarations.filterIsInstance<IrSimpleFunction>().single { it.name.asString() == "invoke" }
val invokeFun = lambdaClass.invokeFun!!
val superInvokeFun = invokeFun.overriddenSymbols.first { it.owner.isSuspend == invokeFun.isSuspend }.owner
val lambdaName = Name.identifier("${lambdaClass.name.asString()}\$lambda")
@@ -20,6 +20,7 @@ import org.jetbrains.kotlin.ir.expressions.IrCall
import org.jetbrains.kotlin.ir.expressions.IrExpressionBody
import org.jetbrains.kotlin.ir.types.getClass
import org.jetbrains.kotlin.ir.types.isNullableAny
import org.jetbrains.kotlin.ir.util.invokeFun
import org.jetbrains.kotlin.ir.util.isEffectivelyExternal
import org.jetbrains.kotlin.ir.util.isTopLevelDeclaration
import org.jetbrains.kotlin.name.Name
@@ -116,8 +117,6 @@ fun invokeFunForLambda(call: IrCall) =
call.extensionReceiver!!
.type
.getClass()!!
.declarations
.filterIsInstance<IrSimpleFunction>()
.single { it.name.asString() == "invoke" }
.invokeFun!!
fun IrFunction.isInlineFunWithReifiedParameter() = isInline && typeParameters.any { it.isReified }
@@ -197,6 +197,9 @@ val IrClassSymbol.fields: Sequence<IrFieldSymbol>
val IrClass.primaryConstructor: IrConstructor?
get() = this.declarations.singleOrNull { it is IrConstructor && it.isPrimary } as IrConstructor?
val IrClass.invokeFun: IrSimpleFunction?
get() = declarations.filterIsInstance<IrSimpleFunction>().singleOrNull { it.name.asString() == "invoke" }
val IrDeclarationContainer.properties: Sequence<IrProperty>
get() = declarations.asSequence().filterIsInstance<IrProperty>()
@@ -623,4 +626,4 @@ val IrDeclarationParent.isFacadeClass: Boolean
get() = this is IrClass &&
(origin == IrDeclarationOrigin.JVM_MULTIFILE_CLASS ||
origin == IrDeclarationOrigin.FILE_CLASS ||
origin == IrDeclarationOrigin.SYNTHETIC_FILE_CLASS)
origin == IrDeclarationOrigin.SYNTHETIC_FILE_CLASS)