Skip to content

Commit

Permalink
[KT] Move noWhenBranchMatchedException intrinsic handling to a lowe…
Browse files Browse the repository at this point in the history
…ring

In 2.1.0 this will become important as we no longer will see object members as
being static -- instead calls must be lowered. Therefore, these intrinsics need
to be implemented in lowerings rather than later on in CUB.

PiperOrigin-RevId: 700114377
  • Loading branch information
kevinoconnor7 authored and copybara-github committed Nov 25, 2024
1 parent f4a7f6d commit 751cd63
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -683,7 +683,6 @@ class CompilationUnitBuilder(
irCall.isDataClassArrayMemberToString -> convertDataClassArrayMemberCall(irCall, "toString")
irCall.isAnyToString -> convertAnyToStringCall(irCall)
irCall.isCheckNotNullCall -> convertCheckNotNullCall(irCall)
irCall.isNoWhenBranchMatchedException -> convertNoWhenBranchMatchedException(irCall)
irCall.isJavaClassPropertyReference -> convertJavaClassPropertyReference(irCall)
irCall.isKClassJavaPropertyReference ->
convertKClassJavaPropertyReference(irCall, wrapPrimitives = false)
Expand Down Expand Up @@ -952,19 +951,6 @@ class CompilationUnitBuilder(
}
}

private fun convertNoWhenBranchMatchedException(irCall: IrCall): Expression {
require(irCall.valueArgumentsCount == 0) {
"throwNoWhenBranchMatchedException should have no arguments"
}
return MethodCall.Builder.from(
TypeDescriptors.get()
.kotlinJvmInternalIntrinsics!!
.getMethodDescriptor("throwNoWhenBranchMatchedException")
)
.setSourcePosition(getSourcePosition(irCall))
.build()
}

private fun convertPrefixOperation(irCall: IrCall): Expression {
val prefixOperator = requireNotNull(intrinsicMethods.getPrefixOperator(irCall.symbol))

Expand Down Expand Up @@ -1698,9 +1684,6 @@ class CompilationUnitBuilder(
private val IrCall.isCheckNotNullCall: Boolean
get() = intrinsicMethods.isCheckNotNull(this)

private val IrCall.isNoWhenBranchMatchedException: Boolean
get() = intrinsicMethods.isNoWhenBranchMatchedException(this)

private val IrCall.isJavaClassPropertyReference: Boolean
get() = intrinsicMethods.isJavaClassProperty(this)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package com.google.j2cl.transpiler.frontend.kotlin.lower

import org.jetbrains.kotlin.backend.common.FileLoweringPass
import org.jetbrains.kotlin.backend.jvm.functionByName
import org.jetbrains.kotlin.backend.jvm.ir.createJvmIrBuilder
import org.jetbrains.kotlin.backend.jvm.ir.findEnumValuesFunction
import org.jetbrains.kotlin.ir.backend.js.utils.typeArguments
Expand All @@ -38,6 +39,7 @@ import org.jetbrains.kotlin.ir.util.isPrimitiveArray
import org.jetbrains.kotlin.ir.util.parentAsClass
import org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid
import org.jetbrains.kotlin.name.CallableId
import org.jetbrains.kotlin.name.ClassId
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.name.Name

Expand All @@ -56,6 +58,7 @@ class IntrinsicFunctionCallsLowering(j2clBackendContext: J2clBackendContext) :
expression.isEnumEntries || expression.isEnumGetEntries -> lowerEnumEntries(expression)
expression.isRangeUntilCall -> lowerRangeUntilCall(expression)
expression.isArrayIteratorCall -> lowerArrayIteratorCall(expression)
expression.isNoWhenBranchMatchedException -> lowerNoWhenBranchMatchedException(expression)
else -> super.visitCall(expression)
}
}
Expand Down Expand Up @@ -143,6 +146,21 @@ class IntrinsicFunctionCallsLowering(j2clBackendContext: J2clBackendContext) :
}
}

private val jvmIntrinsicsClass by lazy {
context.irPluginContext!!.referenceClass(
ClassId.topLevel(FqName("kotlin.jvm.internal.Intrinsics"))
)!!
}

private val throwNoWhenBranchMatchedException by lazy {
jvmIntrinsicsClass.functionByName("throwNoWhenBranchMatchedException")
}

private fun lowerNoWhenBranchMatchedException(irCall: IrCall) =
context.createJvmIrBuilder(throwNoWhenBranchMatchedException, irCall).run {
irCall(throwNoWhenBranchMatchedException)
}

private val IrCall.isEnumEntries: Boolean
get() = intrinsics.isEnumEntries(this)

Expand All @@ -154,4 +172,7 @@ class IntrinsicFunctionCallsLowering(j2clBackendContext: J2clBackendContext) :

private val IrCall.isEnumGetEntries: Boolean
get() = intrinsics.isEnumGetEntries(this)

private val IrCall.isNoWhenBranchMatchedException: Boolean
get() = intrinsics.isNoWhenBranchMatchedException(this)
}

0 comments on commit 751cd63

Please sign in to comment.