Skip to content

Commit d9a0cd3

Browse files
authored
Added gRPC service generation to the compiler plugin (#428)
1 parent 5d6b675 commit d9a0cd3

File tree

47 files changed

+2068
-941
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+2068
-941
lines changed

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

Lines changed: 93 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ package kotlinx.rpc.codegen.extension
66

77
import kotlinx.rpc.codegen.VersionSpecificApi
88
import org.jetbrains.kotlin.backend.common.extensions.IrPluginContext
9+
import org.jetbrains.kotlin.ir.declarations.IrEnumEntry
910
import org.jetbrains.kotlin.ir.declarations.IrSimpleFunction
1011
import org.jetbrains.kotlin.ir.symbols.IrClassSymbol
12+
import org.jetbrains.kotlin.ir.symbols.IrEnumEntrySymbol
1113
import org.jetbrains.kotlin.ir.symbols.IrPropertySymbol
1214
import org.jetbrains.kotlin.ir.symbols.IrSimpleFunctionSymbol
1315
import org.jetbrains.kotlin.ir.types.makeNullable
@@ -16,6 +18,7 @@ import org.jetbrains.kotlin.ir.util.functions
1618
import org.jetbrains.kotlin.ir.util.isVararg
1719
import org.jetbrains.kotlin.ir.util.nestedClasses
1820
import org.jetbrains.kotlin.ir.util.properties
21+
import org.jetbrains.kotlin.platform.jvm.isJvm
1922
import org.jetbrains.kotlin.platform.konan.isNative
2023
import org.jetbrains.kotlin.types.Variance
2124

@@ -33,22 +36,10 @@ internal class RpcIrContext(
3336
irBuiltIns.arrayClass.typeWith(anyNullable, Variance.OUT_VARIANCE)
3437
}
3538

36-
val listOfAnnotations by lazy {
37-
irBuiltIns.listClass.typeWith(irBuiltIns.annotationType)
38-
}
39-
40-
val arrayOfAnnotations by lazy {
41-
irBuiltIns.arrayClass.typeWith(irBuiltIns.annotationType, Variance.OUT_VARIANCE)
42-
}
43-
4439
val kTypeClass by lazy {
4540
getIrClassSymbol("kotlin.reflect", "KType")
4641
}
4742

48-
val suspendFunction2 by lazy {
49-
getIrClassSymbol("kotlin.coroutines", "SuspendFunction2")
50-
}
51-
5243
val flow by lazy {
5344
getIrClassSymbol("kotlinx.coroutines.flow", "Flow")
5445
}
@@ -77,8 +68,56 @@ internal class RpcIrContext(
7768
getIrClassSymbol("kotlinx.rpc.grpc.descriptor", "GrpcServiceDescriptor")
7869
}
7970

80-
val grpcDelegate by lazy {
81-
getIrClassSymbol("kotlinx.rpc.grpc.descriptor", "GrpcDelegate")
71+
val grpcServiceDelegate by lazy {
72+
getIrClassSymbol("kotlinx.rpc.grpc.descriptor", "GrpcServiceDelegate")
73+
}
74+
75+
val grpcPlatformServiceDescriptor by lazy {
76+
if (isJvmTarget()) {
77+
getIrClassSymbol("io.grpc", "ServiceDescriptor")
78+
} else {
79+
getIrClassSymbol("kotlinx.rpc.grpc.internal", "ServiceDescriptor")
80+
}
81+
}
82+
83+
val grpcPlatformMethodDescriptor by lazy {
84+
if (isJvmTarget()) {
85+
getIrClassSymbol("io.grpc", "MethodDescriptor")
86+
} else {
87+
getIrClassSymbol("kotlinx.rpc.grpc.internal", "MethodDescriptor")
88+
}
89+
}
90+
91+
val grpcPlatformMethodType by lazy {
92+
getIrClassSymbol("kotlinx.rpc.grpc.internal", "MethodType")
93+
}
94+
95+
val grpcPlatformMethodTypeUnary by lazy {
96+
grpcPlatformMethodType.enumEntry("UNARY")
97+
}
98+
99+
val grpcPlatformMethodTypeServerStreaming by lazy {
100+
grpcPlatformMethodType.enumEntry("SERVER_STREAMING")
101+
}
102+
103+
val grpcPlatformMethodTypeClientStreaming by lazy {
104+
grpcPlatformMethodType.enumEntry("CLIENT_STREAMING")
105+
}
106+
107+
val grpcPlatformMethodTypeBidiStreaming by lazy {
108+
grpcPlatformMethodType.enumEntry("BIDI_STREAMING")
109+
}
110+
111+
val grpcMessageCodec by lazy {
112+
getIrClassSymbol("kotlinx.rpc.grpc.codec", "MessageCodec")
113+
}
114+
115+
val grpcMessageCodecResolver by lazy {
116+
getIrClassSymbol("kotlinx.rpc.grpc.codec", "MessageCodecResolver")
117+
}
118+
119+
val withCodecAnnotation by lazy {
120+
getIrClassSymbol("kotlinx.rpc.grpc.codec", "WithCodec")
82121
}
83122

84123
val rpcType by lazy {
@@ -105,8 +144,12 @@ internal class RpcIrContext(
105144
getRpcIrClassSymbol("RpcInvokator", "descriptor")
106145
}
107146

108-
val rpcInvokatorMethod by lazy {
109-
rpcInvokator.subClass("Method")
147+
val rpcInvokatorUnaryResponse by lazy {
148+
rpcInvokator.subClass("UnaryResponse")
149+
}
150+
151+
val rpcInvokatorFlowResponse by lazy {
152+
rpcInvokator.subClass("FlowResponse")
110153
}
111154

112155
val rpcParameter by lazy {
@@ -137,6 +180,10 @@ internal class RpcIrContext(
137180
return pluginContext.platform.isNative()
138181
}
139182

183+
fun isJvmTarget(): Boolean {
184+
return pluginContext.platform.isJvm()
185+
}
186+
140187
fun isWasmTarget(): Boolean {
141188
return versionSpecificApi.isWasm(pluginContext.platform)
142189
}
@@ -196,6 +243,22 @@ internal class RpcIrContext(
196243
namedFunction("kotlin", "to")
197244
}
198245

246+
val serviceDescriptor by lazy {
247+
namedFunction("kotlinx.rpc.grpc.internal", "serviceDescriptor")
248+
}
249+
250+
val methodDescriptor by lazy {
251+
namedFunction("kotlinx.rpc.grpc.internal", "methodDescriptor")
252+
}
253+
254+
val grpcServiceDescriptorDelegate by lazy {
255+
grpcServiceDescriptor.namedFunction("delegate")
256+
}
257+
258+
val grpcMessageCodecResolverResolve by lazy {
259+
grpcMessageCodecResolver.namedFunction("resolve")
260+
}
261+
199262
private fun IrClassSymbol.namedFunction(name: String): IrSimpleFunction {
200263
return owner.functions.single { it.name.asString() == name }
201264
}
@@ -214,12 +277,20 @@ internal class RpcIrContext(
214277
val properties = Properties()
215278

216279
inner class Properties {
280+
val rpcServiceDescriptorSimpleName by lazy {
281+
rpcServiceDescriptor.namedProperty("simpleName")
282+
}
283+
217284
val rpcServiceDescriptorFqName by lazy {
218285
rpcServiceDescriptor.namedProperty("fqName")
219286
}
220287

221-
val grpcServiceDescriptorDelegate by lazy {
222-
grpcServiceDescriptor.namedProperty("delegate")
288+
val rpcServiceDescriptorCallables by lazy {
289+
rpcServiceDescriptor.namedProperty("callables")
290+
}
291+
292+
val mapValues by lazy {
293+
irBuiltIns.mapClass.namedProperty("values")
223294
}
224295

225296
private fun IrClassSymbol.namedProperty(name: String): IrPropertySymbol {
@@ -231,6 +302,10 @@ internal class RpcIrContext(
231302
return owner.nestedClasses.single { it.name.asString() == name }.symbol
232303
}
233304

305+
private fun IrClassSymbol.enumEntry(name: String): IrEnumEntrySymbol {
306+
return owner.declarations.filterIsInstance<IrEnumEntry>().single { it.name.asString() == name }.symbol
307+
}
308+
234309
private fun getRpcIrClassSymbol(name: String, subpackage: String? = null): IrClassSymbol {
235310
val suffix = subpackage?.let { ".$subpackage" } ?: ""
236311
return getIrClassSymbol("kotlinx.rpc$suffix", name)

0 commit comments

Comments
 (0)