Fix empty vararg processing in intrinsics

This commit is contained in:
Mikhael Bogdanov
2017-11-28 14:28:36 +01:00
parent 420b9fdaa9
commit a936f75423
@@ -19,16 +19,39 @@ package org.jetbrains.kotlin.backend.jvm.intrinsics
import org.jetbrains.kotlin.backend.jvm.JvmBackendContext
import org.jetbrains.kotlin.backend.jvm.codegen.BlockInfo
import org.jetbrains.kotlin.backend.jvm.codegen.ExpressionCodegen
import org.jetbrains.kotlin.codegen.AsmUtil
import org.jetbrains.kotlin.codegen.Callable
import org.jetbrains.kotlin.codegen.StackValue
import org.jetbrains.kotlin.descriptors.FunctionDescriptor
import org.jetbrains.kotlin.ir.UNDEFINED_OFFSET
import org.jetbrains.kotlin.ir.expressions.IrExpression
import org.jetbrains.kotlin.ir.expressions.IrMemberAccessExpression
import org.jetbrains.kotlin.ir.visitors.IrElementTransformer
import org.jetbrains.kotlin.ir.visitors.IrElementVisitor
import org.jetbrains.kotlin.resolve.calls.components.isVararg
import org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmMethodSignature
import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.org.objectweb.asm.Type
import org.jetbrains.org.objectweb.asm.commons.InstructionAdapter
import java.util.*
private class IrEmptyVarargExpression(
override val type: KotlinType,
override val startOffset: Int,
override val endOffset: Int
) : IrExpression {
override fun <R, D> accept(visitor: IrElementVisitor<R, D>, data: D): R {
TODO("not implemented")
}
override fun <D> acceptChildren(visitor: IrElementVisitor<Unit, D>, data: D) {
TODO("not implemented")
}
override fun <D> transformChildren(transformer: IrElementTransformer<D>, data: D) {
TODO("not implemented")
}
}
open class IrIntrinsicFunction(
val expression: IrMemberAccessExpression,
@@ -60,12 +83,26 @@ open class IrIntrinsicFunction(
}
open fun invoke(v: InstructionAdapter, codegen: ExpressionCodegen, data: BlockInfo):StackValue {
val args = mutableListOf(expression.dispatchReceiver, expression.extensionReceiver)
args.addAll(expression.descriptor.valueParameters.mapIndexed {
i, _ ->
expression.getValueArgument(i)
})
args.filterNotNull().forEachIndexed { i, irExpression -> genArg(irExpression, codegen, i, data) }
val args = listOfNotNull(expression.dispatchReceiver, expression.extensionReceiver) +
expression.descriptor.valueParameters.mapIndexed { i, descriptor ->
expression.getValueArgument(i) ?:
if (descriptor.isVararg)
IrEmptyVarargExpression(descriptor.type, UNDEFINED_OFFSET, UNDEFINED_OFFSET)
else error("Unknown parameter: $descriptor in $expression")
}
args.forEachIndexed { i, irExpression ->
if (irExpression is IrEmptyVarargExpression) {
val parameterType = codegen.typeMapper.mapType(irExpression.type)
StackValue.operation(parameterType) {
it.aconst(0)
it.newarray(AsmUtil.correctElementType(parameterType))
}.put(parameterType, codegen.mv)
}
else {
genArg(irExpression, codegen, i, data)
}
}
genInvokeInstruction(v)
return StackValue.onStack(returnType)
}