From 043452fb62c6e0a53c4937203e846094653cbaa5 Mon Sep 17 00:00:00 2001 From: pyos Date: Tue, 25 May 2021 18:39:54 +0200 Subject: [PATCH] JVM: make handling of captured params for default lambdas shorter --- .../jetbrains/kotlin/codegen/CallGenerator.kt | 1 - .../kotlin/codegen/inline/InlineCodegen.kt | 36 +++++++------------ 2 files changed, 12 insertions(+), 25 deletions(-) diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/CallGenerator.kt b/compiler/backend/src/org/jetbrains/kotlin/codegen/CallGenerator.kt index f30d6fb2cdd..74ed48546e9 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/CallGenerator.kt +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/CallGenerator.kt @@ -18,7 +18,6 @@ enum class ValueKind { DEFAULT_MASK, METHOD_HANDLE_IN_DEFAULT, CAPTURED, - DEFAULT_LAMBDA_CAPTURED_PARAMETER, NON_INLINEABLE_ARGUMENT_FOR_INLINE_PARAMETER_CALLED_IN_SUSPEND, NON_INLINEABLE_ARGUMENT_FOR_INLINE_SUSPEND_PARAMETER } 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 b0309657e52..de08917e008 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/InlineCodegen.kt +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/inline/InlineCodegen.kt @@ -268,18 +268,14 @@ abstract class InlineCodegen( abstract fun descriptorIsDeserialized(memberDescriptor: CallableMemberDescriptor): Boolean - fun generateAndInsertFinallyBlocks( + private fun generateAndInsertFinallyBlocks( intoNode: MethodNode, insertPoints: List, offsetForFinallyLocalVar: Int ) { if (!sourceCompiler.hasFinallyBlocks()) return - val extensionPoints = HashMap() - for (insertPoint in insertPoints) { - extensionPoints.put(insertPoint.beforeIns, insertPoint) - } - + val extensionPoints = insertPoints.associateBy { it.beforeIns } val processor = DefaultProcessor(intoNode, offsetForFinallyLocalVar) var curFinallyDepth = 0 @@ -321,7 +317,7 @@ abstract class InlineCodegen( val splitBy = SimpleInterval(start.info as LabelNode, extension.finallyIntervalEnd) processor.tryBlocksMetaInfo.splitAndRemoveCurrentIntervals(splitBy, true) - processor.localVarsMetaInfo.splitAndRemoveCurrentIntervals(splitBy, true); + processor.localVarsMetaInfo.splitAndRemoveCurrentIntervals(splitBy, true) mark.dropTo() } @@ -330,7 +326,7 @@ abstract class InlineCodegen( } processor.substituteTryBlockNodes(intoNode) - processor.substituteLocalVarTable(intoNode); + processor.substituteLocalVarTable(intoNode) } protected abstract fun generateAssertFieldIfNeeded(info: RootInliningContext) @@ -362,7 +358,7 @@ abstract class InlineCodegen( if (!asFunctionInline && Type.VOID_TYPE !== jvmType) { //TODO remap only inlinable closure => otherwise we could get a lot of problem - val couldBeRemapped = !shouldPutGeneralValue(jvmType, kotlinType, stackValue) && kind !== ValueKind.DEFAULT_PARAMETER + val couldBeRemapped = !shouldPutGeneralValue(jvmType, kotlinType, stackValue) && !isDefaultParameter val remappedValue = if (couldBeRemapped) stackValue else null val info: ParameterInfo @@ -379,11 +375,7 @@ abstract class InlineCodegen( } } - recordParameterValueInLocalVal( - false, - isDefaultParameter || kind === ValueKind.DEFAULT_LAMBDA_CAPTURED_PARAMETER, - info - ) + recordParameterValueInLocalVal(false, isDefaultParameter, info) } } @@ -430,16 +422,12 @@ abstract class InlineCodegen( } private fun rememberCapturedForDefaultLambda(defaultLambda: DefaultLambda) { - for ((paramIndex, captured) in defaultLambda.capturedVars.withIndex()) { - putArgumentOrCapturedToLocalVal( - JvmKotlinType(captured.type), - //HACK: actually parameter would be placed on stack in default function - // also see ValueKind.DEFAULT_LAMBDA_CAPTURED_PARAMETER check - StackValue.onStack(captured.type), - captured, - paramIndex, - ValueKind.DEFAULT_LAMBDA_CAPTURED_PARAMETER - ) + if (!asFunctionInline) { + for (captured in defaultLambda.capturedVars) { + val info = invocationParamBuilder.addCapturedParam(captured, captured.fieldName, false) + info.remapValue = StackValue.local(codegen.frameMap.enterTemp(info.type), info.type) + info.isSynthetic = true + } } }