Collect classes for JVM that must be regenerated after IR inline

This commit is contained in:
Ivan Kylchik
2022-12-14 16:45:31 +01:00
committed by Space Team
parent 41b56e5f16
commit 4baf970d5b
13 changed files with 561 additions and 49 deletions
@@ -25,8 +25,8 @@ import org.jetbrains.kotlin.descriptors.ClassKind
import org.jetbrains.kotlin.descriptors.DescriptorVisibilities
import org.jetbrains.kotlin.descriptors.Modality
import org.jetbrains.kotlin.ir.declarations.*
import org.jetbrains.kotlin.ir.expressions.IrCall
import org.jetbrains.kotlin.ir.expressions.IrExpressionBody
import org.jetbrains.kotlin.ir.expressions.*
import org.jetbrains.kotlin.ir.symbols.IrClassSymbol
import org.jetbrains.kotlin.ir.symbols.IrSymbol
import org.jetbrains.kotlin.ir.symbols.IrTypeParameterSymbol
import org.jetbrains.kotlin.ir.types.*
@@ -67,8 +67,19 @@ fun IrFrameMap.leave(irDeclaration: IrSymbolOwner): Int {
return leave(irDeclaration.symbol)
}
fun IrClass.getOriginalDeclaration(): IrDeclaration? {
val originalClass = this.attributeOwnerIdBeforeInline ?: return null
return when (originalClass) {
is IrClass -> return originalClass
is IrFunctionExpression -> originalClass.function
is IrFunctionReference -> originalClass.symbol.owner
else -> null
}
}
fun JvmBackendContext.getSourceMapper(declaration: IrClass): SourceMapper {
val fileEntry = declaration.fileParent.fileEntry
val originalDeclarationBeforeInline = declaration.getOriginalDeclaration() ?: declaration
val fileEntry = originalDeclarationBeforeInline.fileParent.fileEntry
// NOTE: apparently inliner requires the source range to cover the
// whole file the class is declared in rather than the class only.
val endLineNumber = when (fileEntry) {
@@ -77,12 +88,13 @@ fun JvmBackendContext.getSourceMapper(declaration: IrClass): SourceMapper {
}
val sourceFileName = when (fileEntry) {
is MultifileFacadeFileEntry -> fileEntry.partFiles.singleOrNull()?.name
else -> declaration.fileParent.name
else -> originalDeclarationBeforeInline.fileParent.name
}
val type = declaration.attributeOwnerIdBeforeInline?.let { getLocalClassType(it)!! } ?: defaultTypeMapper.mapClass(declaration)
return SourceMapper(
SourceInfo(
sourceFileName,
defaultTypeMapper.mapClass(declaration).internalName,
type.internalName,
endLineNumber + 1
)
)
@@ -70,6 +70,7 @@ class IrIntrinsicMethods(val irBuiltIns: IrBuiltIns, val symbols: JvmSymbols) {
irBuiltIns.ororSymbol.toKey()!! to OrOr,
irBuiltIns.dataClassArrayMemberHashCodeSymbol.toKey()!! to IrDataClassArrayMemberHashCode,
irBuiltIns.dataClassArrayMemberToStringSymbol.toKey()!! to IrDataClassArrayMemberToString,
symbols.singleArgumentInlineFunction.toKey()!! to SingleArgumentInlineFunctionIntrinsic,
symbols.unsafeCoerceIntrinsic.toKey()!! to UnsafeCoerce,
symbols.signatureStringIntrinsic.toKey()!! to SignatureString,
symbols.throwNullPointerException.toKey()!! to ThrowException(Type.getObjectType("java/lang/NullPointerException")),
@@ -0,0 +1,24 @@
/*
* Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.backend.jvm.intrinsics
import org.jetbrains.kotlin.backend.jvm.codegen.*
import org.jetbrains.kotlin.backend.jvm.ir.unwrapInlineLambda
import org.jetbrains.kotlin.ir.expressions.IrFunctionAccessExpression
object SingleArgumentInlineFunctionIntrinsic : IntrinsicMethod() {
override fun invoke(expression: IrFunctionAccessExpression, codegen: ExpressionCodegen, data: BlockInfo): PromisedValue? {
val sourceCompiler = IrSourceCompilerForInline(codegen.state, expression, expression.symbol.owner, codegen, data)
val argumentExpression = expression.getValueArgument(0)!!
val inlineLambda = argumentExpression.unwrapInlineLambda()
if (inlineLambda != null) {
val lambdaInfo = IrExpressionLambdaImpl(codegen, inlineLambda)
lambdaInfo.generateLambdaBody(sourceCompiler)
}
return codegen.unitValue
}
}