Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 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
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@

package kotlinx.rpc.codegen

import kotlinx.rpc.codegen.extension.IrMemberAccessExpressionData
import org.jetbrains.kotlin.backend.common.extensions.IrPluginContext
import org.jetbrains.kotlin.cli.common.messages.MessageCollector
import org.jetbrains.kotlin.config.CompilerConfigurationKey
import org.jetbrains.kotlin.descriptors.SourceElement
import org.jetbrains.kotlin.ir.builders.declarations.IrFieldBuilder
import org.jetbrains.kotlin.ir.declarations.*
import org.jetbrains.kotlin.ir.expressions.IrCall
import org.jetbrains.kotlin.ir.expressions.IrMemberAccessExpression
import org.jetbrains.kotlin.ir.expressions.IrStatementOrigin
import org.jetbrains.kotlin.ir.expressions.impl.IrCallImpl
import org.jetbrains.kotlin.ir.expressions.impl.IrConstructorCallImpl
Expand Down Expand Up @@ -79,6 +81,12 @@ interface VersionSpecificApi {
origin: IrStatementOrigin? = null,
source: SourceElement = SourceElement.NO_SOURCE,
): IrConstructorCallImpl

fun IrFunction.valueParametersVS(): List<IrValueParameter>
val IrFunction.extensionReceiverParameterVS: IrValueParameter?
var IrFunction.dispatchReceiverParameterVS: IrValueParameter?

fun IrMemberAccessExpressionData.buildFor(access: IrMemberAccessExpression<*>)
}

@Suppress("unused")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright 2023-2025 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
*/

package kotlinx.rpc.codegen.extension

import kotlinx.rpc.codegen.VersionSpecificApi
import org.jetbrains.kotlin.ir.expressions.IrExpression
import org.jetbrains.kotlin.ir.expressions.IrMemberAccessExpression
import org.jetbrains.kotlin.ir.types.IrType

class IrMemberAccessExpressionData(
val dispatchReceiver: IrExpression?,
val extensionReceiver: IrExpression?,
val typeArguments: List<IrType>,
val valueArguments: List<IrExpression>,
)

class IrMemberAccessExpressionBuilder(private val vsApi: VersionSpecificApi) {
var dispatchReceiver: IrExpression? = null
var extensionReceiver: IrExpression? = null

private var valueArguments: List<IrExpression> = emptyList()
private var typeArguments: List<IrType> = emptyList()

val typeBuilder = TypeBuilder()

fun types(builder: TypeBuilder.() -> Unit) {
typeBuilder.builder()
}

val valueBuilder = ValueBuilder()

fun values(builder: ValueBuilder.() -> Unit) {
valueBuilder.builder()
}

inner class TypeBuilder {
operator fun IrType.unaryPlus() {
typeArguments += this
}
}

inner class ValueBuilder {
operator fun IrExpression.unaryPlus() {
valueArguments += this
}
}

fun build(): IrMemberAccessExpressionData = IrMemberAccessExpressionData(
dispatchReceiver,
extensionReceiver,
typeArguments,
valueArguments
)
}

inline fun IrMemberAccessExpression<*>.arguments(vsApi: VersionSpecificApi, builder: IrMemberAccessExpressionBuilder.() -> Unit) {
IrMemberAccessExpressionBuilder(vsApi).apply(builder).build().let { data ->
with(vsApi) {
data.buildFor(this@arguments)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,6 @@ import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.types.Variance
import java.util.*

fun String.capitalized(): String {
return replaceFirstChar { if (it.isLowerCase()) it.titlecase(Locale.getDefault()) else it.toString() }
}

fun IrClassifierSymbol.typeWith(type: IrType, variance: Variance): IrType {
return IrSimpleTypeImpl(
classifier = this,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@ internal object RpcDeclarationScanner {

ServiceDeclaration.Method(
function = declaration,
arguments = declaration.valueParameters.memoryOptimizedMap { param ->
ServiceDeclaration.Method.Argument(param, param.type)
arguments = ctx.versionSpecificApi.run {
declaration.valueParametersVS().memoryOptimizedMap { param ->
ServiceDeclaration.Method.Argument(param, param.type)
}
},
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,19 +202,25 @@ internal class RpcIrContext(

val lazy by lazy {
namedFunction("kotlin", "lazy") {
it.owner.valueParameters.size == 1
vsApi {
it.owner.valueParametersVS().size == 1
}
}
}

val lazyGetValue by lazy {
namedFunction("kotlin", "getValue") {
it.owner.extensionReceiverParameter?.type?.classOrNull == [email protected]
vsApi {
it.owner.extensionReceiverParameterVS?.type?.classOrNull == [email protected]
}
}
}

val listOf by lazy {
namedFunction("kotlin.collections", "listOf") {
it.owner.valueParameters.singleOrNull()?.isVararg ?: false
vsApi {
it.owner.valueParametersVS().singleOrNull()?.isVararg ?: false
}
}
}

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

val mapOf by lazy {
namedFunction("kotlin.collections", "mapOf") {
it.owner.valueParameters.singleOrNull()?.isVararg ?: false
vsApi {
it.owner.valueParametersVS().singleOrNull()?.isVararg ?: false
}
}
}

Expand Down Expand Up @@ -284,4 +292,8 @@ internal class RpcIrContext(
return versionSpecificApi.referenceClass(pluginContext, packageName, name)
?: error("Unable to find symbol. Package: $packageName, name: $name")
}

private fun <T> vsApi(body: VersionSpecificApi.() -> T): T {
return versionSpecificApi.run(body)
}
}
Loading