1
1
package love.forte.plugin.suspendtrans.ir
2
2
3
3
import love.forte.plugin.suspendtrans.*
4
+ import love.forte.plugin.suspendtrans.fir.SuspendTransformBridgeFunctionKey
5
+ import love.forte.plugin.suspendtrans.fir.SuspendTransformGeneratedDeclarationKey
4
6
import love.forte.plugin.suspendtrans.fir.SuspendTransformPluginKey
5
7
import love.forte.plugin.suspendtrans.utils.*
6
8
import org.jetbrains.kotlin.backend.common.IrElementTransformerVoidWithContext
@@ -15,10 +17,7 @@ import org.jetbrains.kotlin.ir.ObsoleteDescriptorBasedAPI
15
17
import org.jetbrains.kotlin.ir.UNDEFINED_OFFSET
16
18
import org.jetbrains.kotlin.ir.builders.*
17
19
import org.jetbrains.kotlin.ir.declarations.*
18
- import org.jetbrains.kotlin.ir.expressions.IrBody
19
- import org.jetbrains.kotlin.ir.expressions.IrCall
20
- import org.jetbrains.kotlin.ir.expressions.IrStatementOrigin
21
- import org.jetbrains.kotlin.ir.expressions.IrTypeOperator
20
+ import org.jetbrains.kotlin.ir.expressions.*
22
21
import org.jetbrains.kotlin.ir.expressions.impl.IrFunctionExpressionImpl
23
22
import org.jetbrains.kotlin.ir.expressions.impl.IrTypeOperatorCallImpl
24
23
import org.jetbrains.kotlin.ir.symbols.IrSimpleFunctionSymbol
@@ -72,43 +71,77 @@ class SuspendTransformTransformer(
72
71
val pluginKey = if (property != null ) {
73
72
// from property
74
73
(property.origin as ? IrDeclarationOrigin .GeneratedByPlugin )
75
- ?.pluginKey as ? SuspendTransformPluginKey
74
+ ?.pluginKey as ? SuspendTransformGeneratedDeclarationKey
76
75
} else {
77
76
(declaration.origin as ? IrDeclarationOrigin .GeneratedByPlugin )
78
- ?.pluginKey as ? SuspendTransformPluginKey
77
+ ?.pluginKey as ? SuspendTransformGeneratedDeclarationKey
79
78
}
80
79
81
80
// K1 ?
82
81
val userData = descriptor.getUserData(SuspendTransformUserDataKey )
83
82
84
83
val generatedOriginFunction: IrFunction ? = when {
85
84
pluginKey != null -> {
86
- val callableFunction =
87
- pluginContext.referenceFunctions(pluginKey.data.transformer.transformFunctionInfo.toCallableId())
88
- .firstOrNull()
89
- ? : throw IllegalStateException (" Transform function ${pluginKey.data.transformer.transformFunctionInfo} not found" )
90
85
91
- resolveFunctionBody(
92
- pluginKey,
93
- declaration,
94
- { f ->
95
- pluginKey.data.originSymbol.checkSame(pluginKey.data.markerId, f)
96
- },
97
- callableFunction
98
- ).also { generatedOriginFunction ->
99
- if (property != null ) {
100
- // NO! BACKING! FIELD!
101
- property.backingField = null
102
- }
103
-
104
- if (generatedOriginFunction != null ) {
105
- postProcessGenerateOriginFunction(
106
- generatedOriginFunction,
107
- pluginKey.data.transformer.originFunctionIncludeAnnotations
86
+ when (pluginKey) {
87
+ is SuspendTransformBridgeFunctionKey -> {
88
+ val callableFunction =
89
+ pluginContext.referenceFunctions(pluginKey.data.transformer.transformFunctionInfo.toCallableId())
90
+ .firstOrNull()
91
+ ? : throw IllegalStateException (" Transform function ${pluginKey.data.transformer.transformFunctionInfo} not found" )
92
+
93
+ resolveBridgeFunctionBody(
94
+ pluginKey,
95
+ declaration,
96
+ callableFunction
108
97
)
98
+
99
+ null
100
+ // .also { generatedOriginFunction ->
101
+ // if (property != null) {
102
+ // // NO! BACKING! FIELD!
103
+ // property.backingField = null
104
+ // }
105
+ //
106
+ // if (generatedOriginFunction != null) {
107
+ // postProcessGenerateOriginFunction(
108
+ // generatedOriginFunction,
109
+ // pluginKey.data.transformer.originFunctionIncludeAnnotations
110
+ // )
111
+ // }
112
+ // }
109
113
}
110
114
115
+ is SuspendTransformPluginKey -> {
116
+ val callableFunction =
117
+ pluginContext.referenceFunctions(pluginKey.data.transformer.transformFunctionInfo.toCallableId())
118
+ .firstOrNull()
119
+ ? : throw IllegalStateException (" Transform function ${pluginKey.data.transformer.transformFunctionInfo} not found" )
120
+
121
+ resolveFunctionBody(
122
+ pluginKey,
123
+ declaration,
124
+ { f ->
125
+ pluginKey.data.originSymbol.checkSame(pluginKey.data.markerId, f)
126
+ },
127
+ callableFunction
128
+ ).also { generatedOriginFunction ->
129
+ if (property != null ) {
130
+ // NO! BACKING! FIELD!
131
+ property.backingField = null
132
+ }
133
+
134
+ if (generatedOriginFunction != null ) {
135
+ postProcessGenerateOriginFunction(
136
+ generatedOriginFunction,
137
+ pluginKey.data.transformer.originFunctionIncludeAnnotations
138
+ )
139
+ }
140
+ }
141
+ }
111
142
}
143
+
144
+
112
145
}
113
146
114
147
userData != null -> {
@@ -159,6 +192,9 @@ class SuspendTransformTransformer(
159
192
}
160
193
}
161
194
195
+ /* *
196
+ * @return origin function
197
+ */
162
198
@OptIn(ObsoleteDescriptorBasedAPI ::class )
163
199
private inline fun resolveFunctionBody (
164
200
sourceKey : Any? ,
@@ -274,10 +310,25 @@ class SuspendTransformTransformer(
274
310
return originFunction
275
311
}
276
312
277
-
278
-
279
313
return null
280
314
}
315
+
316
+ /* *
317
+ * 直接为bridge fun生成body
318
+ */
319
+ private fun resolveBridgeFunctionBody (
320
+ sourceKey : Any? ,
321
+ function : IrFunction ,
322
+ transformTargetFunctionCall : IrSimpleFunctionSymbol ,
323
+ ) {
324
+ // body: return $transform(block, scope?)
325
+ function.body = generateTransformBodyForFunctionLambda(
326
+ pluginContext,
327
+ function,
328
+ null ,
329
+ transformTargetFunctionCall
330
+ )
331
+ }
281
332
}
282
333
283
334
private fun IrFunction.reportLocation (): CompilerMessageSourceLocation ? {
@@ -353,45 +404,54 @@ private fun generateTransformBodyForFunction(
353
404
private fun generateTransformBodyForFunctionLambda (
354
405
context : IrPluginContext ,
355
406
function : IrFunction ,
356
- originFunction : IrFunction ,
407
+ originFunction : IrFunction ? ,
357
408
transformTargetFunctionCall : IrSimpleFunctionSymbol ,
358
409
): IrBody {
359
- val originValueParameters = originFunction.valueParameters
360
- function.valueParameters.forEachIndexed { index, parameter ->
361
- val originFunctionValueParameter = originValueParameters[index]
362
- parameter.defaultValue = originFunctionValueParameter.defaultValue
410
+ // 不为null,说明是直接生成。否则是bridge
411
+ originFunction?.valueParameters?.also { originValueParameters ->
412
+ function.valueParameters.forEachIndexed { index, parameter ->
413
+ val originFunctionValueParameter = originValueParameters[index]
414
+ parameter.defaultValue = originFunctionValueParameter.defaultValue
415
+ }
363
416
}
364
417
365
418
return context.createIrBuilder(function.symbol).irBlockBody {
366
- val suspendLambdaFunc = context.createSuspendLambdaFunctionWithCoroutineScope(
367
- originFunction = originFunction,
368
- function = function
369
- )
370
419
371
- val lambdaType = context.symbols.suspendFunctionN(0 ).typeWith(suspendLambdaFunc.returnType)
420
+ val lambdaExpression: IrExpression = if (originFunction != null ) {
421
+ val suspendLambdaFunc = context.createSuspendLambdaFunctionWithCoroutineScope(
422
+ originFunction = originFunction,
423
+ function = function
424
+ )
372
425
373
- + irReturn(irCall(transformTargetFunctionCall).apply {
374
- putValueArgument(
375
- 0 ,
376
- IrFunctionExpressionImpl (
377
- UNDEFINED_OFFSET ,
378
- UNDEFINED_OFFSET ,
379
- lambdaType,
380
- suspendLambdaFunc,
381
- IrStatementOrigin .LAMBDA
382
- )
426
+ val lambdaType = context.symbols.suspendFunctionN(0 ).typeWith(suspendLambdaFunc.returnType)
427
+
428
+ IrFunctionExpressionImpl (
429
+ UNDEFINED_OFFSET ,
430
+ UNDEFINED_OFFSET ,
431
+ lambdaType,
432
+ suspendLambdaFunc,
433
+ IrStatementOrigin .LAMBDA
383
434
)
384
- // argument: 1, if is CoroutineScope, and this is CoroutineScope.
385
- val owner = transformTargetFunctionCall.owner
435
+ } else {
436
+ // is bridge fun, use the first param `block`
437
+ val blockParameter = function.valueParameters.first()
438
+ irGet(blockParameter)
439
+ }
440
+
441
+ + irReturn(irCall(transformTargetFunctionCall).apply {
442
+ putValueArgument(0 , lambdaExpression)
443
+
444
+ val transformFunctionOwner = transformTargetFunctionCall.owner
386
445
387
446
// CoroutineScope
388
- val ownerValueParameters = owner .valueParameters
447
+ val ownerValueParameters = transformFunctionOwner .valueParameters
389
448
449
+ // argument: 1, if is CoroutineScope, and this is CoroutineScope.
390
450
if (ownerValueParameters.size > 1 ) {
391
451
for (index in 1 .. ownerValueParameters.lastIndex) {
392
452
val valueParameter = ownerValueParameters[index]
393
453
val type = valueParameter.type
394
- tryResolveCoroutineScopeValueParameter(type, context, function, owner , this @irBlockBody, index)
454
+ tryResolveCoroutineScopeValueParameter(type, context, function, transformFunctionOwner , this @irBlockBody, index)
395
455
}
396
456
}
397
457
0 commit comments