Skip to content

Commit eaaba79

Browse files
authored
2.2.0 compiler migration (#335)
1 parent 3f26262 commit eaaba79

File tree

40 files changed

+1115
-325
lines changed

40 files changed

+1115
-325
lines changed

compiler-plugin/compiler-plugin-backend/src/main/core/kotlinx/rpc/codegen/VersionSpecificApi.kt

+8
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@
66

77
package kotlinx.rpc.codegen
88

9+
import kotlinx.rpc.codegen.extension.IrMemberAccessExpressionData
910
import org.jetbrains.kotlin.backend.common.extensions.IrPluginContext
1011
import org.jetbrains.kotlin.cli.common.messages.MessageCollector
1112
import org.jetbrains.kotlin.config.CompilerConfigurationKey
1213
import org.jetbrains.kotlin.descriptors.SourceElement
1314
import org.jetbrains.kotlin.ir.builders.declarations.IrFieldBuilder
1415
import org.jetbrains.kotlin.ir.declarations.*
1516
import org.jetbrains.kotlin.ir.expressions.IrCall
17+
import org.jetbrains.kotlin.ir.expressions.IrMemberAccessExpression
1618
import org.jetbrains.kotlin.ir.expressions.IrStatementOrigin
1719
import org.jetbrains.kotlin.ir.expressions.impl.IrCallImpl
1820
import org.jetbrains.kotlin.ir.expressions.impl.IrConstructorCallImpl
@@ -79,6 +81,12 @@ interface VersionSpecificApi {
7981
origin: IrStatementOrigin? = null,
8082
source: SourceElement = SourceElement.NO_SOURCE,
8183
): IrConstructorCallImpl
84+
85+
fun IrFunction.valueParametersVS(): List<IrValueParameter>
86+
val IrFunction.extensionReceiverParameterVS: IrValueParameter?
87+
var IrFunction.dispatchReceiverParameterVS: IrValueParameter?
88+
89+
fun IrMemberAccessExpressionData.buildFor(access: IrMemberAccessExpression<*>)
8290
}
8391

8492
@Suppress("unused")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Copyright 2023-2025 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
3+
*/
4+
5+
package kotlinx.rpc.codegen.extension
6+
7+
import kotlinx.rpc.codegen.VersionSpecificApi
8+
import org.jetbrains.kotlin.ir.expressions.IrExpression
9+
import org.jetbrains.kotlin.ir.expressions.IrMemberAccessExpression
10+
import org.jetbrains.kotlin.ir.types.IrType
11+
12+
class IrMemberAccessExpressionData(
13+
val dispatchReceiver: IrExpression?,
14+
val extensionReceiver: IrExpression?,
15+
val typeArguments: List<IrType>,
16+
val valueArguments: List<IrExpression>,
17+
)
18+
19+
class IrMemberAccessExpressionBuilder(private val vsApi: VersionSpecificApi) {
20+
var dispatchReceiver: IrExpression? = null
21+
var extensionReceiver: IrExpression? = null
22+
23+
private var valueArguments: List<IrExpression> = emptyList()
24+
private var typeArguments: List<IrType> = emptyList()
25+
26+
val typeBuilder = TypeBuilder()
27+
28+
fun types(builder: TypeBuilder.() -> Unit) {
29+
typeBuilder.builder()
30+
}
31+
32+
val valueBuilder = ValueBuilder()
33+
34+
fun values(builder: ValueBuilder.() -> Unit) {
35+
valueBuilder.builder()
36+
}
37+
38+
inner class TypeBuilder {
39+
operator fun IrType.unaryPlus() {
40+
typeArguments += this
41+
}
42+
}
43+
44+
inner class ValueBuilder {
45+
operator fun IrExpression.unaryPlus() {
46+
valueArguments += this
47+
}
48+
}
49+
50+
fun build(): IrMemberAccessExpressionData = IrMemberAccessExpressionData(
51+
dispatchReceiver,
52+
extensionReceiver,
53+
typeArguments,
54+
valueArguments
55+
)
56+
}
57+
58+
inline fun IrMemberAccessExpression<*>.arguments(vsApi: VersionSpecificApi, builder: IrMemberAccessExpressionBuilder.() -> Unit) {
59+
IrMemberAccessExpressionBuilder(vsApi).apply(builder).build().let { data ->
60+
with(vsApi) {
61+
data.buildFor(this@arguments)
62+
}
63+
}
64+
}

compiler-plugin/compiler-plugin-backend/src/main/core/kotlinx/rpc/codegen/extension/IrUtils.kt

-4
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,6 @@ import org.jetbrains.kotlin.name.Name
2323
import org.jetbrains.kotlin.types.Variance
2424
import java.util.*
2525

26-
fun String.capitalized(): String {
27-
return replaceFirstChar { if (it.isLowerCase()) it.titlecase(Locale.getDefault()) else it.toString() }
28-
}
29-
3026
fun IrClassifierSymbol.typeWith(type: IrType, variance: Variance): IrType {
3127
return IrSimpleTypeImpl(
3228
classifier = this,

compiler-plugin/compiler-plugin-backend/src/main/core/kotlinx/rpc/codegen/extension/RpcDeclarationScanner.kt

+4-2
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,10 @@ internal object RpcDeclarationScanner {
3434

3535
ServiceDeclaration.Method(
3636
function = declaration,
37-
arguments = declaration.valueParameters.memoryOptimizedMap { param ->
38-
ServiceDeclaration.Method.Argument(param, param.type)
37+
arguments = ctx.versionSpecificApi.run {
38+
declaration.valueParametersVS().memoryOptimizedMap { param ->
39+
ServiceDeclaration.Method.Argument(param, param.type)
40+
}
3941
},
4042
)
4143
}

compiler-plugin/compiler-plugin-backend/src/main/core/kotlinx/rpc/codegen/extension/RpcIrContext.kt

+16-4
Original file line numberDiff line numberDiff line change
@@ -202,19 +202,25 @@ internal class RpcIrContext(
202202

203203
val lazy by lazy {
204204
namedFunction("kotlin", "lazy") {
205-
it.owner.valueParameters.size == 1
205+
vsApi {
206+
it.owner.valueParametersVS().size == 1
207+
}
206208
}
207209
}
208210

209211
val lazyGetValue by lazy {
210212
namedFunction("kotlin", "getValue") {
211-
it.owner.extensionReceiverParameter?.type?.classOrNull == this@RpcIrContext.lazy
213+
vsApi {
214+
it.owner.extensionReceiverParameterVS?.type?.classOrNull == this@RpcIrContext.lazy
215+
}
212216
}
213217
}
214218

215219
val listOf by lazy {
216220
namedFunction("kotlin.collections", "listOf") {
217-
it.owner.valueParameters.singleOrNull()?.isVararg ?: false
221+
vsApi {
222+
it.owner.valueParametersVS().singleOrNull()?.isVararg ?: false
223+
}
218224
}
219225
}
220226

@@ -224,7 +230,9 @@ internal class RpcIrContext(
224230

225231
val mapOf by lazy {
226232
namedFunction("kotlin.collections", "mapOf") {
227-
it.owner.valueParameters.singleOrNull()?.isVararg ?: false
233+
vsApi {
234+
it.owner.valueParametersVS().singleOrNull()?.isVararg ?: false
235+
}
228236
}
229237
}
230238

@@ -284,4 +292,8 @@ internal class RpcIrContext(
284292
return versionSpecificApi.referenceClass(pluginContext, packageName, name)
285293
?: error("Unable to find symbol. Package: $packageName, name: $name")
286294
}
295+
296+
private fun <T> vsApi(body: VersionSpecificApi.() -> T): T {
297+
return versionSpecificApi.run(body)
298+
}
287299
}

0 commit comments

Comments
 (0)