diff --git a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/codegen/ClassCodegen.kt b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/codegen/ClassCodegen.kt index 5110e81b085..5140a1180ff 100644 --- a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/codegen/ClassCodegen.kt +++ b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/codegen/ClassCodegen.kt @@ -345,17 +345,6 @@ open class ClassCodegen protected constructor( fun createLocalClassCodegen(klass: IrClass, parentFunction: IrFunction): ClassCodegen = ClassCodegen(klass, context, this, parentFunction, withinInline = withinInline || parentFunction.isInline) - fun createClassCodegenForLambdaBody(parentClass: IrClass, lambda: IrFunction): ClassCodegen = - object : ClassCodegen(parentClass, context, parentFunction = parentFunction, withinInline = withinInline) { - override fun createClassBuilder(): ClassBuilder { - return object : AbstractClassBuilder() { - override fun getVisitor(): ClassVisitor { - TODO("Expect to be _not_ reached") - } - } - } - } - private fun generateField(field: IrField) { if (field.origin == IrDeclarationOrigin.FAKE_OVERRIDE) return diff --git a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/codegen/ExpressionCodegen.kt b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/codegen/ExpressionCodegen.kt index 487b9c34036..4aa853b76c0 100644 --- a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/codegen/ExpressionCodegen.kt +++ b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/codegen/ExpressionCodegen.kt @@ -109,7 +109,8 @@ class ExpressionCodegen( override val frameMap: IrFrameMap, val mv: InstructionAdapter, val classCodegen: ClassCodegen, - val inlinedInto: ExpressionCodegen? + val inlinedInto: ExpressionCodegen?, + val smapOverride: DefaultSourceMapper? ) : IrElementVisitor, BaseExpressionCodegen { var finallyDepth = 0 diff --git a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/codegen/FunctionCodegen.kt b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/codegen/FunctionCodegen.kt index e677e074715..702e08852c6 100644 --- a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/codegen/FunctionCodegen.kt +++ b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/codegen/FunctionCodegen.kt @@ -12,6 +12,7 @@ import org.jetbrains.kotlin.backend.jvm.JvmBackendContext import org.jetbrains.kotlin.backend.jvm.JvmLoweredDeclarationOrigin import org.jetbrains.kotlin.backend.jvm.lower.suspendFunctionOriginal import org.jetbrains.kotlin.codegen.AsmUtil +import org.jetbrains.kotlin.codegen.inline.DefaultSourceMapper import org.jetbrains.kotlin.codegen.mangleNameIfNeeded import org.jetbrains.kotlin.codegen.state.GenerationState import org.jetbrains.kotlin.codegen.visitAnnotableParameterCount @@ -38,7 +39,7 @@ import org.jetbrains.org.objectweb.asm.commons.InstructionAdapter open class FunctionCodegen( private val irFunction: IrFunction, private val classCodegen: ClassCodegen, - private val inlinedInto: ExpressionCodegen? = null, + private val inlinedInto: ExpressionCodegen? = null ) { val context = classCodegen.context val state = classCodegen.state @@ -47,14 +48,14 @@ open class FunctionCodegen( classCodegen.createLocalClassCodegen(irFunction.continuationClass(), irFunction).also { it.generate() } } - fun generate(): JvmMethodGenericSignature = + fun generate(smapOverride: DefaultSourceMapper? = null): JvmMethodGenericSignature = try { - doGenerate() + doGenerate(smapOverride) } catch (e: Throwable) { throw RuntimeException("Exception while generating code for:\n${irFunction.dump()}", e) } - private fun doGenerate(): JvmMethodGenericSignature { + private fun doGenerate(smapOverride: DefaultSourceMapper?): JvmMethodGenericSignature { val signature = classCodegen.methodSignatureMapper.mapSignatureWithGeneric(irFunction) val flags = calculateMethodFlags(irFunction.isStatic) @@ -113,7 +114,8 @@ open class FunctionCodegen( } context.state.globalInlineContext.enterDeclaration(irFunction.suspendFunctionOriginal().descriptor) try { - ExpressionCodegen(irFunction, signature, frameMap, InstructionAdapter(methodVisitor), classCodegen, inlinedInto).generate() + val adapter = InstructionAdapter(methodVisitor) + ExpressionCodegen(irFunction, signature, frameMap, adapter, classCodegen, inlinedInto, smapOverride).generate() } finally { context.state.globalInlineContext.exitDeclaration() } diff --git a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/codegen/IrSourceCompilerForInline.kt b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/codegen/IrSourceCompilerForInline.kt index b0f2b4347b5..dca62282a45 100644 --- a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/codegen/IrSourceCompilerForInline.kt +++ b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/codegen/IrSourceCompilerForInline.kt @@ -95,10 +95,14 @@ class IrSourceCompilerForInline( } override val lazySourceMapper: DefaultSourceMapper - get() = codegen.classCodegen.getOrCreateSourceMapper() + get() = codegen.smapOverride ?: codegen.classCodegen.getOrCreateSourceMapper() private fun makeInlineNode(function: IrFunction, classCodegen: ClassCodegen, isLambda: Boolean): SMAPAndMethodNode { var node: MethodNode? = null + val smap = if (isLambda) + codegen.context.getSourceMapper(codegen.classCodegen.irClass) + else + classCodegen.getOrCreateSourceMapper() val functionCodegen = object : FunctionCodegen(function, classCodegen, codegen.takeIf { isLambda }) { override fun createMethod(flags: Int, signature: JvmMethodGenericSignature): MethodVisitor { val asmMethod = signature.asmMethod @@ -113,13 +117,12 @@ class IrSourceCompilerForInline( return wrapWithMaxLocalCalc(node!!) } } - functionCodegen.generate() - return SMAPAndMethodNode(node!!, SMAP(classCodegen.getOrCreateSourceMapper().resultMappings)) + functionCodegen.generate(smap) + return SMAPAndMethodNode(node!!, SMAP(smap.resultMappings)) } override fun generateLambdaBody(lambdaInfo: ExpressionLambda): SMAPAndMethodNode { - val function = (lambdaInfo as IrExpressionLambdaImpl).function - return makeInlineNode(function, codegen.classCodegen.createClassCodegenForLambdaBody(codegen.classCodegen.irClass, function), true) + return makeInlineNode((lambdaInfo as IrExpressionLambdaImpl).function, codegen.classCodegen, true) } override fun doCreateMethodNodeFromSource( @@ -151,7 +154,7 @@ class IrSourceCompilerForInline( override fun createCodegenForExternalFinallyBlockGenerationOnNonLocalReturn(finallyNode: MethodNode, curFinallyDepth: Int) = ExpressionCodegen( codegen.irFunction, codegen.signature, codegen.frameMap, InstructionAdapter(finallyNode), codegen.classCodegen, - codegen.inlinedInto + codegen.inlinedInto, codegen.smapOverride ).also { it.finallyDepth = curFinallyDepth } diff --git a/compiler/testData/codegen/boxInline/reified/nonCapturingObjectInLambda_ir.txt b/compiler/testData/codegen/boxInline/reified/nonCapturingObjectInLambda_ir.txt index ca136207bf4..9148d54ff65 100644 --- a/compiler/testData/codegen/boxInline/reified/nonCapturingObjectInLambda_ir.txt +++ b/compiler/testData/codegen/boxInline/reified/nonCapturingObjectInLambda_ir.txt @@ -14,6 +14,7 @@ public final class _2Kt$complicatedCast$1$1 { @kotlin.Metadata public final class _2Kt { + inner class _2Kt$complicatedCast$1$1 public final static @org.jetbrains.annotations.NotNull method box(): java.lang.String public synthetic final static method complicatedCast(@org.jetbrains.annotations.Nullable p0: java.lang.Object): java.lang.Object } diff --git a/compiler/testData/codegen/bytecodeText/kt10259.kt b/compiler/testData/codegen/bytecodeText/kt10259.kt index dd6b189b482..0f18e54a4f7 100644 --- a/compiler/testData/codegen/bytecodeText/kt10259.kt +++ b/compiler/testData/codegen/bytecodeText/kt10259.kt @@ -17,9 +17,8 @@ inline fun test(s: () -> Unit) { s() } -// 4 INNERCLASS - // JVM_TEMPLATES +// 4 INNERCLASS // 2 INNERCLASS Kt10259Kt\$box\$\$inlined\$test\$lambda\$1\s // 2 INNERCLASS Kt10259Kt\$box\$\$inlined\$test\$lambda\$1\$1 @@ -28,7 +27,8 @@ inline fun test(s: () -> Unit) { // this behavior is equivalent to javac and seems to be correct. // JVM_IR_TEMPLATES -// 2 INNERCLASS Kt10259Kt\$box\$1\$1\s +// 5 INNERCLASS +// 3 INNERCLASS Kt10259Kt\$box\$1\$1\s // 2 INNERCLASS Kt10259Kt\$box\$1\$1\$1 // 1 class Kt10259Kt\$box\$1\$1\ extends // 1 class Kt10259Kt\$box\$1\$1\$1 extends \ No newline at end of file diff --git a/compiler/testData/codegen/bytecodeText/kt10259_3.kt b/compiler/testData/codegen/bytecodeText/kt10259_3.kt index bffb2edc7f1..5eade375019 100644 --- a/compiler/testData/codegen/bytecodeText/kt10259_3.kt +++ b/compiler/testData/codegen/bytecodeText/kt10259_3.kt @@ -21,9 +21,9 @@ inline fun test(crossinline s: () -> Unit) { // 2 INNERCLASS Kt10259_3Kt\$box\$\$inlined\$test\$1\s // 2 INNERCLASS Kt10259_3Kt\$box\$\$inlined\$test\$1\$1\s -// 13 INNERCLASS // JVM_TEMPLATES +// 13 INNERCLASS // 3 INNERCLASS Kt10259_3Kt\$test\$1 null // 2 INNERCLASS Kt10259_3Kt\$test\$1\$1 // inlined: @@ -38,7 +38,8 @@ inline fun test(crossinline s: () -> Unit) { // this behavior is equivalent to javac and seems to be correct. // JVM_IR_TEMPLATES -// 2 INNERCLASS Kt10259_3Kt\$box\$1\$1\s +// 14 INNERCLASS +// 3 INNERCLASS Kt10259_3Kt\$box\$1\$1\s // 2 INNERCLASS Kt10259_3Kt\$box\$1\$1\$1\s // 3 INNERCLASS Kt10259_3Kt\$test\$1\s // 2 INNERCLASS Kt10259_3Kt\$test\$1\$1\s