@@ -210,52 +210,14 @@ private fun generateTransformBodyForFunction(
210210 val owner = transformTargetFunctionCall.owner
211211
212212 // CoroutineScope
213- if (owner.valueParameters.size > 1 ) {
214- owner.valueParameters.forEachIndexed { index, valueParameter ->
215- if (index == 0 ) {
216- return @forEachIndexed
217- }
213+ val ownerValueParameters = owner.valueParameters
218214
215+ if (ownerValueParameters.size > 1 ) {
216+ for (index in 1 .. ownerValueParameters.lastIndex) {
217+ val valueParameter = ownerValueParameters[index]
219218 val type = valueParameter.type
220219 tryResolveCoroutineScopeValueParameter(type, context, function, owner, this @irBlockBody, index)
221220 }
222-
223- // val secondType = owner.valueParameters[1].type
224- // val coroutineScopeTypeName = "kotlinx.coroutines.CoroutineScope".fqn
225- // val coroutineScopeTypeClassId = ClassId.topLevel("kotlinx.coroutines.CoroutineScope".fqn)
226- // val coroutineScopeTypeNameUnsafe = coroutineScopeTypeName.toUnsafe()
227- // if (secondType.isClassType(coroutineScopeTypeNameUnsafe)) {
228- // function.dispatchReceiverParameter?.also { dispatchReceiverParameter ->
229- // context.referenceClass(coroutineScopeTypeClassId)?.also { coroutineScopeRef ->
230- // // irGet(dispatchReceiverParameter)
231- // // irAs(irGet(dispatchReceiverParameter), coroutineScopeRef).safeAs<>()
232- // if (dispatchReceiverParameter.type.isSubtypeOfClass(coroutineScopeRef)) {
233- // // put 'this' to second arg
234- // putValueArgument(1, irGet(dispatchReceiverParameter))
235- // } else {
236- // // scope safe cast
237- // val scopeType = coroutineScopeRef.defaultType
238- //
239- // val scopeParameter = owner.valueParameters.getOrNull(1)
240- //
241- // if (scopeParameter?.type?.isNullable() == true) {
242- // val irSafeAs = IrTypeOperatorCallImpl(
243- // startOffset,
244- // endOffset,
245- // scopeType,
246- // IrTypeOperator.SAFE_CAST,
247- // scopeType,
248- // irGet(dispatchReceiverParameter)
249- // )
250- //
251- // putValueArgument(1, irSafeAs)
252- // }
253- // // irAs(irGet(dispatchReceiverParameter), coroutineScopeRef.defaultType)
254- // }
255- // }
256- // }
257- // }
258-
259221 }
260222
261223 })
@@ -269,8 +231,9 @@ private val coroutineScopeTypeNameUnsafe = coroutineScopeTypeName.toUnsafe()
269231/* *
270232 * 解析类型为 CoroutineScope 的参数。
271233 * 如果当前参数类型为 CoroutineScope:
272- * - 如果当前 dispatcher 即为 CoroutineScope 类型,将其填充
273- * - 如果当前 dispatcher 不是 CoroutineScope 类型,但是此参数可以为 null,则使用 safe-cast 将 dispatcher 转化为 CoroutineScope ( `dispatcher as? CoroutineScope` )
234+ * - 如果当前 receiver 即为 CoroutineScope 类型,将其填充
235+ * - 如果当前 receiver 不是 CoroutineScope 类型,但是此参数可以为 null,
236+ * 则使用 safe-cast 将 receiver 转化为 CoroutineScope ( `dispatcher as? CoroutineScope` )
274237 * - 其他情况忽略此参数(适用于此参数有默认值的情况)
275238 */
276239private fun IrCall.tryResolveCoroutineScopeValueParameter (
@@ -281,34 +244,33 @@ private fun IrCall.tryResolveCoroutineScopeValueParameter(
281244 builderWithScope : IrBuilderWithScope ,
282245 index : Int
283246) {
284- if (type.isClassType(coroutineScopeTypeNameUnsafe)) {
285- function.dispatchReceiverParameter?.also { dispatchReceiverParameter ->
286- context.referenceClass(coroutineScopeTypeClassId)?.also { coroutineScopeRef ->
287- // irGet(dispatchReceiverParameter)
288- // irAs(irGet(dispatchReceiverParameter), coroutineScopeRef).safeAs<>()
289- if (dispatchReceiverParameter.type.isSubtypeOfClass(coroutineScopeRef)) {
290- // put 'this' to second arg
291- putValueArgument(index, builderWithScope.irGet(dispatchReceiverParameter))
292- } else {
293- // scope safe cast
294- val scopeType = coroutineScopeRef.defaultType
295-
296- val scopeParameter = owner.valueParameters.getOrNull(1 )
297-
298- if (scopeParameter?.type?.isNullable() == true ) {
299- val irSafeAs = IrTypeOperatorCallImpl (
300- startOffset,
301- endOffset,
302- scopeType,
303- IrTypeOperator .SAFE_CAST ,
304- scopeType,
305- builderWithScope.irGet(dispatchReceiverParameter)
306- )
307-
308- putValueArgument(index, irSafeAs)
309- }
310- // irAs(irGet(dispatchReceiverParameter), coroutineScopeRef.defaultType)
247+ if (! type.isClassType(coroutineScopeTypeNameUnsafe)) {
248+ return
249+ }
250+
251+ function.dispatchReceiverParameter?.also { dispatchReceiverParameter ->
252+ context.referenceClass(coroutineScopeTypeClassId)?.also { coroutineScopeRef ->
253+ if (dispatchReceiverParameter.type.isSubtypeOfClass(coroutineScopeRef)) {
254+ // put 'this' to the arg
255+ putValueArgument(index, builderWithScope.irGet(dispatchReceiverParameter))
256+ } else {
257+ val scopeType = coroutineScopeRef.defaultType
258+
259+ val scopeParameter = owner.valueParameters.getOrNull(1 )
260+
261+ if (scopeParameter?.type?.isNullable() == true ) {
262+ val irSafeAs = IrTypeOperatorCallImpl (
263+ startOffset,
264+ endOffset,
265+ scopeType,
266+ IrTypeOperator .SAFE_CAST ,
267+ scopeType,
268+ builderWithScope.irGet(dispatchReceiverParameter)
269+ )
270+
271+ putValueArgument(index, irSafeAs)
311272 }
273+ // irAs(irGet(dispatchReceiverParameter), coroutineScopeRef.defaultType)
312274 }
313275 }
314276 }
0 commit comments