[JS IR BE] Fix varargs for inline arrays

This commit is contained in:
Roman Artemev
2019-05-14 18:07:43 +03:00
committed by romanart
parent 600ad7e088
commit fabd306437
4 changed files with 33 additions and 15 deletions
@@ -14,6 +14,8 @@ import org.jetbrains.kotlin.ir.declarations.IrSimpleFunction
import org.jetbrains.kotlin.ir.declarations.impl.IrExternalPackageFragmentImpl
import org.jetbrains.kotlin.ir.descriptors.IrBuiltIns
import org.jetbrains.kotlin.ir.symbols.impl.IrExternalPackageFragmentSymbolImpl
import org.jetbrains.kotlin.ir.types.impl.IrSimpleTypeBuilder
import org.jetbrains.kotlin.ir.types.impl.buildSimpleType
import org.jetbrains.kotlin.ir.types.isLong
import org.jetbrains.kotlin.ir.util.constructors
import org.jetbrains.kotlin.ir.util.findDeclaration
@@ -244,7 +246,7 @@ class JsIntrinsics(private val irBuiltIns: IrBuiltIns, val context: JsIrBackendC
val primitiveArrayConcat = getInternalWithoutPackage("primitiveArrayConcat")
val jsArraySlice = unOp("slice")
val jsArraySlice = defineJsSliceIntrinsic().symbol
val jsBind = defineJsBindIntrinsic()
@@ -322,6 +324,27 @@ class JsIntrinsics(private val irBuiltIns: IrBuiltIns, val context: JsIrBackendC
externalPackageFragment.declarations += it
}
private fun defineJsSliceIntrinsic(): IrSimpleFunction {
val typeParameter = JsIrBuilder.buildTypeParameter(Name.identifier("A"), 0, true).apply {
superTypes += irBuiltIns.anyType
}
val type = IrSimpleTypeBuilder().run {
classifier = typeParameter.symbol
buildSimpleType()
}
return JsIrBuilder.buildFunction(
"slice",
returnType = type,
parent = externalPackageFragment,
origin = JsLoweredDeclarationOrigin.JS_INTRINSICS_STUB
).also {
it.typeParameters += typeParameter.also { t -> t.parent = it }
it.valueParameters += JsIrBuilder.buildValueParameter("a", 0, type).also { v -> v.parent = it }
externalPackageFragment.declarations += it
}
}
private fun defineSetJSPropertyIntrinsic() =
JsIrBuilder.buildFunction(
"\$setJSProperty\$",
@@ -58,7 +58,7 @@ private class VarargTransformer(
fun IrExpression.unboxInlineClassIfNeeded(): IrExpression {
val inlinedClass = type.getInlinedClass() ?: return this
val field = getInlineClassBackingField(inlinedClass)
return IrGetFieldImpl(startOffset, endOffset, field.symbol, inlinedClass.defaultType, this)
return IrGetFieldImpl(startOffset, endOffset, field.symbol, field.type, this)
}
fun IrExpression.boxInlineClassIfNeeded(inlineClass: IrClass?) =
@@ -121,28 +121,25 @@ private class VarargTransformer(
// vararg with a single segment => no need to concatenate
if (segments.size == 1) {
return if (expression.elements.any { it is IrSpreadElement }) {
// Single spread operator => need to copy the array
val segment = segments.first()
val argument = if (expression.elements.any { it is IrSpreadElement }) {
IrCallImpl(
expression.startOffset,
expression.endOffset,
expression.type,
context.intrinsics.jsArraySlice
).apply {
putValueArgument(0, segments.first())
putTypeArgument(0, expression.type)
putValueArgument(0, segment)
}
} else {
val res = segments.first()
return if (needUnboxing)
res.boxInlineClassIfNeeded(arrayInlineClass!!)
else
res
}
} else segment
return if (needUnboxing) argument.boxInlineClassIfNeeded(arrayInlineClass!!) else argument
}
val arrayLiteral =
segments.toArrayLiteral(
IrSimpleTypeImpl(context.intrinsics.array, false, emptyList(), emptyList()),
IrSimpleTypeImpl(context.intrinsics.array, false, emptyList(), emptyList()), // TODO: Substitution
context.irBuiltIns.anyType
)
@@ -1,7 +1,6 @@
// IGNORE_BACKEND: JVM_IR
// WITH_RUNTIME
// KJS_WITH_FULL_RUNTIME
// IGNORE_BACKEND: JS_IR
fun uint(vararg us: UInt): UIntArray = us
@@ -1,5 +1,4 @@
// LANGUAGE_VERSION: 1.3
// IGNORE_BACKEND: JS_IR
// KJS_WITH_FULL_RUNTIME
// EXPECTED_REACHABLE_NODES: 1566
package foo