[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 { private fun createInvokeMethod(clazz: IrClass): IrSimpleFunction {
val superMethods = superFunctionInterface.declarations.filterIsInstance<IrSimpleFunction>() val superMethod = superFunctionInterface.invokeFun!!
val superMethod = superMethods.single { it.name.asString() == "invoke" }
return clazz.addFunction { return clazz.addFunction {
setSourceRange(if (isLambda) function else reference) setSourceRange(if (isLambda) function else reference)
name = superMethod.name name = superMethod.name
@@ -255,9 +254,7 @@ class CallableReferenceLowering(private val context: CommonBackendContext) : Bod
isSuspend = superMethod.isSuspend isSuspend = superMethod.isSuspend
isOperator = superMethod.isOperator isOperator = superMethod.isOperator
}.apply { }.apply {
val secondSuperMethods: List<IrSimpleFunction>? = val secondSuperMethod = secondFunctionInterface?.let { it.invokeFun!! }
secondFunctionInterface?.declarations?.filterIsInstance<IrSimpleFunction>()
val secondSuperMethod = secondSuperMethods?.single { it.name.asString() == "invoke" }
overriddenSymbols = listOfNotNull( overriddenSymbols = listOfNotNull(
superMethod.symbol, superMethod.symbol,
@@ -218,7 +218,7 @@ class InteropCallableReferenceLowering(val context: JsIrBackendContext) : BodyLo
lambdaClass: IrClass, lambdaClass: IrClass,
newDeclarations: MutableList<IrDeclaration> newDeclarations: MutableList<IrDeclaration>
): IrBlockBody { ): 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 superInvokeFun = invokeFun.overriddenSymbols.first { it.owner.isSuspend == invokeFun.isSuspend }.owner
val lambdaName = Name.identifier("${lambdaClass.name.asString()}\$lambda") 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.expressions.IrExpressionBody
import org.jetbrains.kotlin.ir.types.getClass import org.jetbrains.kotlin.ir.types.getClass
import org.jetbrains.kotlin.ir.types.isNullableAny 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.isEffectivelyExternal
import org.jetbrains.kotlin.ir.util.isTopLevelDeclaration import org.jetbrains.kotlin.ir.util.isTopLevelDeclaration
import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.name.Name
@@ -116,8 +117,6 @@ fun invokeFunForLambda(call: IrCall) =
call.extensionReceiver!! call.extensionReceiver!!
.type .type
.getClass()!! .getClass()!!
.declarations .invokeFun!!
.filterIsInstance<IrSimpleFunction>()
.single { it.name.asString() == "invoke" }
fun IrFunction.isInlineFunWithReifiedParameter() = isInline && typeParameters.any { it.isReified } fun IrFunction.isInlineFunWithReifiedParameter() = isInline && typeParameters.any { it.isReified }
@@ -197,6 +197,9 @@ val IrClassSymbol.fields: Sequence<IrFieldSymbol>
val IrClass.primaryConstructor: IrConstructor? val IrClass.primaryConstructor: IrConstructor?
get() = this.declarations.singleOrNull { it is IrConstructor && it.isPrimary } as 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> val IrDeclarationContainer.properties: Sequence<IrProperty>
get() = declarations.asSequence().filterIsInstance<IrProperty>() get() = declarations.asSequence().filterIsInstance<IrProperty>()