JVM IR: remove usages of JvmMethodParameterKind.RECEIVER/CONTEXT_RECEIVER

Also slightly refactor ExpressionCodegen.visitCall.
This commit is contained in:
Alexander Udalov
2022-02-04 02:41:47 +01:00
committed by Space Team
parent dbb7ab1760
commit d9bc714992
4 changed files with 31 additions and 32 deletions
@@ -55,7 +55,6 @@ import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.resolve.jvm.AsmTypes
import org.jetbrains.kotlin.resolve.jvm.AsmTypes.JAVA_STRING_TYPE
import org.jetbrains.kotlin.resolve.jvm.AsmTypes.OBJECT_TYPE
import org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmMethodParameterKind
import org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmMethodSignature
import org.jetbrains.kotlin.types.TypeSystemCommonBackendContext
import org.jetbrains.kotlin.types.computeExpandedTypeForInlineClass
@@ -607,31 +606,37 @@ class ExpressionCodegen(
callGenerator.beforeCallStart()
fun handleParameter(parameter: IrValueParameter, argument: IrExpression, asmType: Type) {
callGenerator.genValueAndPut(parameter, argument, asmType, this, data)
}
fun getValueArgument(i: Int): IrExpression =
expression.getValueArgument(i) ?: error(
"No argument for parameter ${callee.symbol.owner.valueParameters[i].render()}:\n${expression.dump()}"
)
expression.dispatchReceiver?.let { receiver ->
val type = if (expression.superQualifierSymbol != null) receiver.asmType else callable.owner
callGenerator.genValueAndPut(callee.dispatchReceiverParameter!!, receiver, type, this, data)
handleParameter(
callee.dispatchReceiverParameter!!,
receiver,
if (expression.superQualifierSymbol != null) receiver.asmType else callable.owner,
)
}
fun handleValueParameter(i: Int, irParameter: IrValueParameter) {
val arg = expression.getValueArgument(i)
val parameterType = callable.valueParameterTypes[i]
require(arg != null) {
"No argument for parameter ${irParameter.render()}:\n${expression.dump()}"
}
callGenerator.genValueAndPut(irParameter, arg, parameterType, this, data)
val valueParameterAsmTypes = callable.signature.valueParameters
val contextReceiverCount = callee.contextReceiverParametersCount
for (i in 0 until contextReceiverCount) {
handleParameter(callee.valueParameters[i], getValueArgument(i), valueParameterAsmTypes[i].asmType)
}
val contextReceivers = callee.valueParameters.subList(0, callee.contextReceiverParametersCount)
contextReceivers.forEachIndexed(::handleValueParameter)
expression.extensionReceiver?.let { receiver ->
val type = callable.signature.valueParameters.singleOrNull { it.kind == JvmMethodParameterKind.RECEIVER }?.asmType
?: error("No single extension receiver parameter: ${callable.signature.valueParameters}")
callGenerator.genValueAndPut(callee.extensionReceiverParameter!!, receiver, type, this, data)
handleParameter(callee.extensionReceiverParameter!!, receiver, valueParameterAsmTypes[contextReceiverCount].asmType)
}
callee.valueParameters.subList(callee.contextReceiverParametersCount, callee.valueParameters.size)
.forEachIndexed { i, valueParameter -> handleValueParameter(i + contextReceivers.size, valueParameter) }
val valueParametersShift = if (callee.extensionReceiverParameter != null) 1 else 0
for (i in contextReceiverCount until callee.valueParameters.size) {
handleParameter(callee.valueParameters[i], getValueArgument(i), valueParameterAsmTypes[i + valueParametersShift].asmType)
}
expression.markLineNumber(true)
@@ -31,7 +31,6 @@ import org.jetbrains.kotlin.name.JvmStandardClassIds.JVM_SYNTHETIC_ANNOTATION_FQ
import org.jetbrains.kotlin.name.JvmStandardClassIds.STRICTFP_ANNOTATION_FQ_NAME
import org.jetbrains.kotlin.name.JvmStandardClassIds.SYNCHRONIZED_ANNOTATION_FQ_NAME
import org.jetbrains.kotlin.resolve.annotations.JVM_THROWS_ANNOTATION_FQ_NAME
import org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmMethodParameterKind
import org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmMethodSignature
import org.jetbrains.org.objectweb.asm.*
import org.jetbrains.org.objectweb.asm.commons.InstructionAdapter
@@ -278,13 +277,13 @@ class FunctionCodegen(private val irFunction: IrFunction, private val classCodeg
visitAnnotableParameterCount(mv, kotlinParameterTypes.size - syntheticParameterCount)
kotlinParameterTypes.forEachIndexed { i, parameterSignature ->
val kind = parameterSignature.kind
val annotated = when (kind) {
JvmMethodParameterKind.RECEIVER -> irFunction.extensionReceiverParameter
else -> iterator.next()
}
val extensionReceiverParameter = irFunction.extensionReceiverParameter
val annotated = if (extensionReceiverParameter != null && i == irFunction.contextReceiverParametersCount)
extensionReceiverParameter
else
iterator.next()
if (annotated != null && !kind.isSkippedInGenericSignature && !annotated.isSyntheticMarkerParameter()) {
if (!parameterSignature.kind.isSkippedInGenericSignature && !annotated.isSyntheticMarkerParameter()) {
object : AnnotationCodegen(classCodegen, skipNullabilityAnnotations) {
override fun visitAnnotation(descr: String, visible: Boolean): AnnotationVisitor {
return mv.visitParameterAnnotation(
@@ -6,7 +6,6 @@
package org.jetbrains.kotlin.backend.jvm.mapping
import org.jetbrains.kotlin.ir.types.IrType
import org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmMethodParameterKind
import org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmMethodSignature
import org.jetbrains.org.objectweb.asm.Type
import org.jetbrains.org.objectweb.asm.commons.Method
@@ -21,9 +20,6 @@ class IrCallableMethod(
) {
val asmMethod: Method = signature.asmMethod
val valueParameterTypes: List<Type> =
signature.valueParameters.filter { it.kind != JvmMethodParameterKind.RECEIVER }.map { it.asmType }
override fun toString(): String =
"${Printer.OPCODES[invokeOpcode]} $owner.$asmMethod" + (if (isInterfaceMethod) " (itf)" else "")
}
@@ -5,7 +5,6 @@
package org.jetbrains.kotlin.backend.jvm.mapping
import org.jetbrains.kotlin.ir.util.parentsWithSelf
import org.jetbrains.kotlin.backend.jvm.JvmBackendContext
import org.jetbrains.kotlin.backend.jvm.JvmLoweredDeclarationOrigin
import org.jetbrains.kotlin.backend.jvm.ir.*
@@ -255,12 +254,12 @@ class MethodSignatureMapper(private val context: JvmBackendContext, private val
for (i in 0 until function.contextReceiverParametersCount) {
val contextReceiver = function.valueParameters[i]
writeParameter(sw, JvmMethodParameterKind.CONTEXT_RECEIVER, contextReceiver.type, function)
writeParameter(sw, JvmMethodParameterKind.VALUE, contextReceiver.type, function)
}
val receiverParameter = function.extensionReceiverParameter
if (receiverParameter != null) {
writeParameter(sw, JvmMethodParameterKind.RECEIVER, receiverParameter.type, function)
writeParameter(sw, JvmMethodParameterKind.VALUE, receiverParameter.type, function)
}
for (i in function.contextReceiverParametersCount until function.valueParameters.size) {