JVM_IR: Put continuation parameter before default mask and handler.

This is important for calls using reflection as the reflect
library assumes this ordering of arguments.

It would be nice if this could be handled in the lowerings.
Currently AddContinuationLowering is after
DefaultArgumentStubGenerator. If we could add the continuation
first and then do default stub generation maybe we could avoid
the reshuffling introduced in coroutine codegen in this change.
This commit is contained in:
Mads Ager
2020-01-09 15:09:52 +01:00
committed by Ilmir Usmanov
parent c948459ed5
commit b2f8a4e82a
3 changed files with 47 additions and 14 deletions
@@ -176,19 +176,37 @@ fun IrTypeParameter.copyToWithoutSuperTypes(
}
}
fun IrFunction.copyValueParametersFrom(from: IrFunction) {
// TODO: should dispatch receiver be copied?
private fun IrFunction.copyReceiverParametersFrom(from: IrFunction) {
dispatchReceiverParameter = from.dispatchReceiverParameter?.let {
IrValueParameterImpl(it.startOffset, it.endOffset, it.origin, it.descriptor, it.type, it.varargElementType).also {
it.parent = this
}
}
extensionReceiverParameter = from.extensionReceiverParameter?.copyTo(this)
}
fun IrFunction.copyValueParametersFrom(from: IrFunction) {
copyReceiverParametersFrom(from)
val shift = valueParameters.size
valueParameters += from.valueParameters.map { it.copyTo(this, index = it.index + shift) }
}
fun IrFunction.copyValueParametersInsertingContinuationFrom(from: IrFunction, insertContinuation: () -> Unit) {
copyReceiverParametersFrom(from)
val shift = valueParameters.size
var additionalShift = 0
from.valueParameters.forEach {
// The continuation parameter goes before the default argument mask and handler.
if (it.origin == IrDeclarationOrigin.MASK_FOR_DEFAULT_FUNCTION) {
insertContinuation()
additionalShift = 1
}
valueParameters.add(it.copyTo(this, index = it.index + shift + additionalShift))
}
// If there was no default argument mask and handler, the continuation goes last.
if (additionalShift == 0) insertContinuation()
}
fun IrFunction.copyParameterDeclarationsFrom(from: IrFunction) {
assert(typeParameters.isEmpty())
copyTypeParametersFrom(from)