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