Implement JS-IR intrinsic for rangeUntil builtin member operator #KT-52933

This commit is contained in:
Ilya Gorbunov
2022-08-10 15:52:05 +02:00
committed by Space
parent cda90dbfa5
commit 98ebad2d75
2 changed files with 24 additions and 3 deletions
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
* 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.
*/
@@ -180,6 +180,13 @@ class JsIntrinsics(private val irBuiltIns: IrBuiltIns, val context: JsIrBackendC
val jsNumberRangeToNumber = getInternalFunction("numberRangeToNumber")
val jsNumberRangeToLong = getInternalFunction("numberRangeToLong")
private val _rangeUntilFunctions = irBuiltIns.findFunctions(Name.identifier("until"), "kotlin", "ranges")
val rangeUntilFunctions by lazy {
_rangeUntilFunctions
.filter { it.owner.extensionReceiverParameter != null && it.owner.valueParameters.size == 1 }
.associateBy { it.owner.extensionReceiverParameter!!.type to it.owner.valueParameters[0].type }
}
val longClassSymbol = getInternalClassWithoutPackage("kotlin.Long")
val promiseClassSymbol: IrClassSymbol by context.lazy2 {
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
* 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.
*/
@@ -17,6 +17,7 @@ import org.jetbrains.kotlin.ir.symbols.IrSimpleFunctionSymbol
import org.jetbrains.kotlin.ir.types.*
import org.jetbrains.kotlin.ir.util.irCall
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.util.OperatorNameConventions
private val HASH_CODE_NAME = Name.identifier("hashCode")
@@ -61,7 +62,8 @@ class NumberOperatorCallsTransformer(context: JsIrBackendContext) : CallsTransfo
}
for (type in primitiveNumbers) {
add(type, Name.identifier("rangeTo"), ::transformRangeTo)
add(type, OperatorNameConventions.RANGE_TO, ::transformRangeTo)
add(type, OperatorNameConventions.RANGE_UNTIL, ::transformRangeUntil)
add(type, HASH_CODE_NAME, ::transformHashCode)
}
@@ -104,6 +106,18 @@ class NumberOperatorCallsTransformer(context: JsIrBackendContext) : CallsTransfo
}
}
private fun transformRangeUntil(call: IrFunctionAccessExpression): IrExpression {
if (call.valueArgumentsCount != 1) return call
with(call.symbol.owner) {
val function = intrinsics.rangeUntilFunctions[dispatchReceiverParameter!!.type to valueParameters[0].type] ?:
error("No 'until' function found for descriptor: $this")
return irCall(call, function).apply {
extensionReceiver = dispatchReceiver
dispatchReceiver = null
}
}
}
private fun transformHashCode(call: IrFunctionAccessExpression): IrExpression {
return with(call.symbol.owner.dispatchReceiverParameter!!.type) {
when {