From 90412ade8a60b0768ca1b1eccf290d691baa7d2c Mon Sep 17 00:00:00 2001 From: pyos Date: Tue, 25 May 2021 14:11:02 +0200 Subject: [PATCH] JVM: remove second parameter of putClosureParametersOnStack Only used by the old backend. --- .../kotlin/codegen/inline/InlineCodegen.kt | 8 ++- .../kotlin/codegen/inline/PsiInlineCodegen.kt | 54 +++++++++---------- .../backend/jvm/codegen/IrInlineCodegen.kt | 2 +- 3 files changed, 31 insertions(+), 33 deletions(-) diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/InlineCodegen.kt b/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/InlineCodegen.kt index 3642380542c..d7cba6c4f3d 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/InlineCodegen.kt +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/InlineCodegen.kt @@ -23,7 +23,6 @@ import org.jetbrains.kotlin.renderer.DescriptorRenderer import org.jetbrains.kotlin.resolve.DescriptorToSourceUtils import org.jetbrains.kotlin.resolve.DescriptorUtils import org.jetbrains.kotlin.resolve.ImportedFromObjectCallableDescriptor -import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall import org.jetbrains.kotlin.resolve.inline.InlineUtil import org.jetbrains.kotlin.resolve.inline.isInlineOnly import org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmMethodSignature @@ -37,7 +36,6 @@ import org.jetbrains.kotlin.types.model.TypeParameterMarker import org.jetbrains.org.objectweb.asm.Label import org.jetbrains.org.objectweb.asm.Opcodes import org.jetbrains.org.objectweb.asm.Type -import org.jetbrains.org.objectweb.asm.commons.InstructionAdapter import org.jetbrains.org.objectweb.asm.commons.Method import org.jetbrains.org.objectweb.asm.tree.* import kotlin.math.max @@ -435,15 +433,15 @@ abstract class InlineCodegen( private fun putClosureParametersOnStack() { for (next in expressionMap.values) { - //closure parameters for bounded callable references are generated inplace if (next is LambdaInfo) { + // Closure parameters for bound references have already been generated in between other arguments. if (next is ExpressionLambda && next.isBoundCallableReference) continue - putClosureParametersOnStack(next, null) + putClosureParametersOnStack(next) } } } - abstract protected fun putClosureParametersOnStack(next: LambdaInfo, functionReferenceReceiver: StackValue?) + protected abstract fun putClosureParametersOnStack(next: LambdaInfo) protected fun rememberCapturedForDefaultLambda(defaultLambda: DefaultLambda) { for ((paramIndex, captured) in defaultLambda.capturedVars.withIndex()) { diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/PsiInlineCodegen.kt b/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/PsiInlineCodegen.kt index e34ce8e742f..bfec4a9806c 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/PsiInlineCodegen.kt +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/PsiInlineCodegen.kt @@ -105,22 +105,28 @@ class PsiInlineCodegen( var activeLambda: LambdaInfo? = null private set - override fun putClosureParametersOnStack(next: LambdaInfo, functionReferenceReceiver: StackValue?) { + override fun putClosureParametersOnStack(next: LambdaInfo) { activeLambda = next when (next) { - is PsiExpressionLambda -> codegen.pushClosureOnStack(next.classDescriptor, true, this, functionReferenceReceiver) + is PsiExpressionLambda -> { + val receiverValue = next.boundReceiver?.let { boundReceiver -> + val receiver = codegen.generateReceiverValue(boundReceiver, false) + val receiverKotlinType = receiver.kotlinType + val boxedReceiver = + if (receiverKotlinType != null) + DescriptorAsmUtil.boxType(receiver.type, receiverKotlinType, state.typeMapper) + else + AsmUtil.boxType(receiver.type) + StackValue.coercion(receiver, boxedReceiver, receiverKotlinType) + } + codegen.pushClosureOnStack(next.classDescriptor, true, this, receiverValue) + } is DefaultLambda -> rememberCapturedForDefaultLambda(next) else -> throw RuntimeException("Unknown lambda: $next") } activeLambda = null } - private fun getBoundCallableReferenceReceiver(argumentExpression: KtExpression): ReceiverValue? { - val deparenthesized = KtPsiUtil.deparenthesize(argumentExpression) as? KtCallableReferenceExpression ?: return null - val resolvedCall = deparenthesized.callableReference.getResolvedCallWithAssert(state.bindingContext) - return JvmCodegenUtil.getBoundCallableReferenceReceiver(resolvedCall) - } - /*lambda or callable reference*/ private fun isInliningParameter(expression: KtExpression, valueParameterDescriptor: ValueParameterDescriptor): Boolean { //TODO deparenthesize typed @@ -142,21 +148,9 @@ class PsiInlineCodegen( if (isInliningParameter(argumentExpression, valueParameterDescriptor)) { val lambdaInfo = rememberClosure(argumentExpression, parameterType.type, valueParameterDescriptor) - - val receiverValue = getBoundCallableReferenceReceiver(argumentExpression) - if (receiverValue != null) { - val receiver = codegen.generateReceiverValue(receiverValue, false) - val receiverKotlinType = receiver.kotlinType - val boxedReceiver = - if (receiverKotlinType != null) - DescriptorAsmUtil.boxType(receiver.type, receiverKotlinType, state.typeMapper) - else - AsmUtil.boxType(receiver.type) - - putClosureParametersOnStack( - lambdaInfo, - StackValue.coercion(receiver, boxedReceiver, receiverKotlinType) - ) + if (lambdaInfo.boundReceiver != null) { + // Has to be done immediately to preserve evaluation order. + putClosureParametersOnStack(lambdaInfo) } } else { val value = codegen.gen(argumentExpression) @@ -176,13 +170,17 @@ class PsiInlineCodegen( private fun isCallSiteIsSuspend(descriptor: ValueParameterDescriptor): Boolean = state.bindingContext[CodegenBinding.CALL_SITE_IS_SUSPEND_FOR_CROSSINLINE_LAMBDA, descriptor] == true - private fun rememberClosure(expression: KtExpression, type: Type, parameter: ValueParameterDescriptor): LambdaInfo { + private fun rememberClosure(expression: KtExpression, type: Type, parameter: ValueParameterDescriptor): PsiExpressionLambda { val ktLambda = KtPsiUtil.deparenthesize(expression) assert(isInlinableParameterExpression(ktLambda)) { "Couldn't find inline expression in ${expression.text}" } + val boundReceiver = if (ktLambda is KtCallableReferenceExpression) { + val resolvedCall = ktLambda.callableReference.getResolvedCallWithAssert(state.bindingContext) + JvmCodegenUtil.getBoundCallableReferenceReceiver(resolvedCall) + } else null + return PsiExpressionLambda( - ktLambda!!, state.typeMapper, state.languageVersionSettings, - parameter.isCrossinline, getBoundCallableReferenceReceiver(expression) != null + ktLambda!!, state.typeMapper, state.languageVersionSettings, parameter.isCrossinline, boundReceiver ).also { lambda -> val closureInfo = invocationParamBuilder.addNextValueParameter(type, true, null, parameter.index) closureInfo.functionalArgument = lambda @@ -233,8 +231,10 @@ class PsiExpressionLambda( private val typeMapper: KotlinTypeMapper, private val languageVersionSettings: LanguageVersionSettings, isCrossInline: Boolean, - override val isBoundCallableReference: Boolean + val boundReceiver: ReceiverValue? ) : ExpressionLambda(isCrossInline) { + override val isBoundCallableReference: Boolean + get() = boundReceiver != null override val lambdaClassType: Type diff --git a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/codegen/IrInlineCodegen.kt b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/codegen/IrInlineCodegen.kt index 9a31d0bde32..3b5176bc11c 100644 --- a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/codegen/IrInlineCodegen.kt +++ b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/codegen/IrInlineCodegen.kt @@ -57,7 +57,7 @@ class IrInlineCodegen( } } - override fun putClosureParametersOnStack(next: LambdaInfo, functionReferenceReceiver: StackValue?) { + override fun putClosureParametersOnStack(next: LambdaInfo) { when (next) { is IrExpressionLambdaImpl -> next.reference.getArgumentsWithIr().forEachIndexed { index, (_, ir) -> putCapturedValueOnStack(ir, next.capturedVars[index], index)