From 98ebad2d75b701404e481d391a32532555241e70 Mon Sep 17 00:00:00 2001 From: Ilya Gorbunov Date: Wed, 10 Aug 2022 15:52:05 +0200 Subject: [PATCH] Implement JS-IR intrinsic for rangeUntil builtin member operator #KT-52933 --- .../kotlin/ir/backend/js/JsIntrinsics.kt | 9 ++++++++- .../calls/NumberOperatorCallsTransformer.kt | 18 ++++++++++++++++-- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/JsIntrinsics.kt b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/JsIntrinsics.kt index 494127b5c8f..c8222e35368 100644 --- a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/JsIntrinsics.kt +++ b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/JsIntrinsics.kt @@ -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 { diff --git a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/lower/calls/NumberOperatorCallsTransformer.kt b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/lower/calls/NumberOperatorCallsTransformer.kt index 25cdb209df8..b706ce71872 100644 --- a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/lower/calls/NumberOperatorCallsTransformer.kt +++ b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/lower/calls/NumberOperatorCallsTransformer.kt @@ -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 {