Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kotlin 2.1.20-Beta1 support via reflection #456

Merged
merged 2 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ jobs:
strategy:
fail-fast: false
matrix:
poko_sample_kotlin_version: [ ~ ]
poko_sample_kotlin_version: [ ~, 2.1.20-Beta1 ]
env:
poko_sample_kotlin_version: ${{ matrix.poko_sample_kotlin_version }}
steps:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package dev.drewhamilton.poko.ir

import org.jetbrains.kotlin.ir.builders.IrBuilderWithScope
import org.jetbrains.kotlin.ir.builders.irCall
import org.jetbrains.kotlin.ir.expressions.IrCall
import org.jetbrains.kotlin.ir.expressions.IrStatementOrigin
import org.jetbrains.kotlin.ir.symbols.IrSimpleFunctionSymbol
import org.jetbrains.kotlin.ir.symbols.UnsafeDuringIrConstructionAPI
import org.jetbrains.kotlin.ir.types.IrType

/**
* Alias for [irCall] from 2.1.0 – 2.1.20.
*/
@OptIn(UnsafeDuringIrConstructionAPI::class)
internal fun IrBuilderWithScope.irCallCompat(
callee: IrSimpleFunctionSymbol,
type: IrType,
valueArgumentsCount: Int = callee.owner.valueParameters.size,
typeArgumentsCount: Int = callee.owner.typeParameters.size,
origin: IrStatementOrigin? = null,
): IrCall {
return try {
// 2.1.0:
irCall(
callee = callee,
type = type,
valueArgumentsCount = valueArgumentsCount,
typeArgumentsCount = typeArgumentsCount,
origin = origin,
)
} catch (noSuchMethodError: NoSuchMethodError) {
// TODO: Flip order when 2.1.20 is the compile version
// https://github.com/JetBrains/kotlin/blob/v2.1.20-Beta1/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/builders/ExpressionHelpers.kt#L240
javaClass.classLoader.loadClass("org.jetbrains.kotlin.ir.builders.ExpressionHelpersKt")
.methods
.single { function ->
function.name == "irCall" &&
function.parameters.map { it.type } == listOf(
IrBuilderWithScope::class.java, // extension receiver
IrSimpleFunctionSymbol::class.java, // callee
IrType::class.java, // type
Int::class.java, // typeArgumentsCount
IrStatementOrigin::class.java, // origin
)
}
.invoke(
null, // static invocation
this, // extension receiver
callee, // param: callee
type, // param: type
typeArgumentsCount, // param: type
origin, // param: origin
) as IrCall
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import org.jetbrains.kotlin.cli.common.messages.MessageCollector
import org.jetbrains.kotlin.ir.builders.IrBlockBodyBuilder
import org.jetbrains.kotlin.ir.builders.IrBuilderWithScope
import org.jetbrains.kotlin.ir.builders.irBranch
import org.jetbrains.kotlin.ir.builders.irCall
import org.jetbrains.kotlin.ir.builders.irElseBranch
import org.jetbrains.kotlin.ir.builders.irEqeqeq
import org.jetbrains.kotlin.ir.builders.irEquals
Expand Down Expand Up @@ -195,7 +194,7 @@ private fun IrBuilderWithScope.irCallContentDeepEquals(
receiver: IrExpression,
argument: IrExpression,
): IrExpression {
return irCall(
return irCallCompat(
callee = findContentDeepEqualsFunctionSymbol(context, classifier),
type = context.irBuiltIns.booleanType,
valueArgumentsCount = 1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ private fun IrBlockBodyBuilder.irArrayTypeCheckAndContentDeepHashCodeBranch(
val type = classSymbol.createArrayType(context)
return irBranch(
condition = irIs(value, type),
result = irCall(
result = irCallCompat(
callee = findArrayContentDeepHashCodeFunction(context, classSymbol),
type = context.irBuiltIns.intType,
).apply {
Expand Down Expand Up @@ -336,7 +336,7 @@ private fun IrBlockBodyBuilder.irCallHashCodeFunction(
val (hasDispatchReceiver, hasExtensionReceiver) = with(hashCodeFunctionSymbol.owner) {
(dispatchReceiverParameter != null) to (extensionReceiverParameter != null)
}
return irCall(
return irCallCompat(
callee = hashCodeFunctionSymbol,
type = context.irBuiltIns.intType,
valueArgumentsCount = if (hasDispatchReceiver || hasExtensionReceiver) 0 else 1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import org.jetbrains.kotlin.builtins.PrimitiveType
import org.jetbrains.kotlin.cli.common.messages.MessageCollector
import org.jetbrains.kotlin.ir.builders.IrBlockBodyBuilder
import org.jetbrains.kotlin.ir.builders.irBranch
import org.jetbrains.kotlin.ir.builders.irCall
import org.jetbrains.kotlin.ir.builders.irConcat
import org.jetbrains.kotlin.ir.builders.irElseBranch
import org.jetbrains.kotlin.ir.builders.irGetField
Expand Down Expand Up @@ -212,7 +211,7 @@ private fun IrBlockBodyBuilder.irCallToStringFunction(
toStringFunctionSymbol: IrSimpleFunctionSymbol,
value: IrExpression,
): IrExpression {
return irCall(
return irCallCompat(
callee = toStringFunctionSymbol,
type = context.irBuiltIns.stringType,
).apply {
Expand Down
Loading