@@ -32,6 +32,7 @@ import org.jetbrains.kotlin.fir.references.builder.buildResolvedNamedReference
32
32
import org.jetbrains.kotlin.fir.resolve.ScopeSession
33
33
import org.jetbrains.kotlin.fir.resolve.SessionHolderImpl
34
34
import org.jetbrains.kotlin.fir.resolve.getSuperTypes
35
+ import org.jetbrains.kotlin.fir.resolve.providers.dependenciesSymbolProvider
35
36
import org.jetbrains.kotlin.fir.resolve.providers.symbolProvider
36
37
import org.jetbrains.kotlin.fir.resolve.toRegularClassSymbol
37
38
import org.jetbrains.kotlin.fir.resolve.transformers.ReturnTypeCalculatorForFullBodyResolve
@@ -90,22 +91,29 @@ class SuspendTransformFirTransformer(
90
91
)
91
92
92
93
private fun initScopeSymbol () {
93
- coroutineScopeSymbol = session.symbolProvider.getClassLikeSymbolByClassId(
94
- ClassId (
95
- FqName .fromSegments(listOf (" kotlinx" , " coroutines" )),
96
- Name .identifier(" CoroutineScope" )
97
- )
98
- ) ? : error(" Cannot resolve `kotlinx.coroutines.CoroutineScope` symbol." )
94
+
95
+ val classId = ClassId (
96
+ FqName .fromSegments(listOf (" kotlinx" , " coroutines" )),
97
+ Name .identifier(" CoroutineScope" )
98
+ )
99
+
100
+ coroutineScopeSymbol = session.symbolProvider.getClassLikeSymbolByClassId(classId)
101
+ ? : session.dependenciesSymbolProvider.getClassLikeSymbolByClassId(classId)
102
+ ? : error(" Cannot resolve `kotlinx.coroutines.CoroutineScope` symbol." )
99
103
}
100
104
101
105
private fun initTransformerFunctionSymbolMap () {
102
106
// 尝试找到所有配置的 bridge function, 例如 `runBlocking` 等
107
+ val symbolProvider = session.symbolProvider
108
+ val dependenciesSymbolProvider = session.dependenciesSymbolProvider
109
+
103
110
suspendTransformConfiguration.transformers
104
- .forEach { (_ , transformerList) ->
111
+ .forEach { (platform , transformerList) ->
105
112
for (transformer in transformerList) {
106
113
val transformFunctionInfo = transformer.transformFunctionInfo
107
114
val packageName = transformFunctionInfo.packageName
108
115
val functionName = transformFunctionInfo.functionName
116
+
109
117
@Suppress(" DEPRECATION" )
110
118
val className = transformFunctionInfo.className
111
119
if (className != null ) {
@@ -117,11 +125,18 @@ class SuspendTransformFirTransformer(
117
125
118
126
// TODO 校验funcs?
119
127
128
+ val functionNameIdentifier = Name .identifier(functionName)
129
+
120
130
val transformerFunctionSymbols =
121
- session. symbolProvider.getTopLevelFunctionSymbols(
131
+ symbolProvider.getTopLevelFunctionSymbols(
122
132
packageName.fqn,
123
- Name .identifier(functionName)
124
- )
133
+ functionNameIdentifier
134
+ ).ifEmpty {
135
+ dependenciesSymbolProvider.getTopLevelFunctionSymbols(
136
+ packageName.fqn,
137
+ functionNameIdentifier
138
+ )
139
+ }
125
140
126
141
if (transformerFunctionSymbols.isNotEmpty()) {
127
142
if (transformerFunctionSymbols.size == 1 ) {
@@ -130,7 +145,8 @@ class SuspendTransformFirTransformer(
130
145
error(" Found multiple transformer function symbols for transformer: $transformer " )
131
146
}
132
147
} else {
133
- error(" Cannot find transformer function symbol for transformer: $transformer " )
148
+ // 有时候在不同平台中寻找,可能会找不到,例如在jvm中找不到js的函数
149
+ // error("Cannot find transformer function symbol $packageName.$functionName (${session.moduleData.platform}) for transformer: $transformer")
134
150
}
135
151
}
136
152
}
@@ -331,7 +347,6 @@ class SuspendTransformFirTransformer(
331
347
}
332
348
}
333
349
334
- // TODO?
335
350
this .contextReceiverArguments.addAll(thisContextReceivers.map { receiver ->
336
351
buildThisReceiverExpression {
337
352
coneTypeOrNull = receiver.typeRef.coneTypeOrNull
@@ -390,13 +405,13 @@ class SuspendTransformFirTransformer(
390
405
this .resolvedSymbol = bridgeFunSymbol
391
406
}
392
407
393
- // this.dispatchReceiver = buildThisReceiverExpression {
394
- // coneTypeOrNull = originFunSymbol.dispatchReceiverType
395
- // source = originFunSymbol.source
396
- // calleeReference = buildImplicitThisReference {
397
- // boundSymbol = owner
398
- // }
399
- // }
408
+ // this.dispatchReceiver = buildThisReceiverExpression {
409
+ // coneTypeOrNull = originFunSymbol.dispatchReceiverType
410
+ // source = originFunSymbol.source
411
+ // calleeReference = buildImplicitThisReference {
412
+ // boundSymbol = owner
413
+ // }
414
+ // }
400
415
401
416
this .argumentList = buildResolvedArgumentList(
402
417
null ,
@@ -416,21 +431,36 @@ class SuspendTransformFirTransformer(
416
431
bridgeFunSymbol.valueParameterSymbols
417
432
418
433
if (valueParameterSymbols.size > 1 ) {
434
+ // 支持:
435
+ // CoroutineScope? -> this as? CoroutineScope
436
+ // CoroutineScope -> this or throw error
437
+ // CoroutineScope (optional) -> this or ignore
438
+ // TODO Any -> this
439
+ // TODO 后面的所有参数处理
440
+
441
+ // val listIterator = valueParameterSymbols.listIterator(1)
442
+ // listIterator.forEach { otherParameter ->
443
+ //
444
+ // }
445
+
419
446
val secondParameter = valueParameterSymbols[1 ]
420
447
421
448
val secondParameterType = secondParameter.resolvedReturnType
422
449
val secondParameterTypeNotNullable =
423
450
secondParameterType.makeConeTypeDefinitelyNotNullOrNotNull(session.typeContext)
424
451
425
- val isCoroutineScope = secondParameterTypeNotNullable.isSubtypeOf(
426
- coroutineScopeSymbol.toLookupTag().constructClassType(),
427
- session,
428
- )
452
+ fun ConeKotlinType.isCoroutineScope (): Boolean {
453
+ return isSubtypeOf(
454
+ coroutineScopeSymbol.toLookupTag().constructClassType(),
455
+ session
456
+ )
457
+ }
458
+
459
+ val isCoroutineScope = secondParameterTypeNotNullable.isCoroutineScope()
429
460
430
461
if (isCoroutineScope) {
431
462
if (secondParameterType.isMarkedNullable) {
432
463
// this as? CoroutineScope
433
-
434
464
val secondParameterTypeNotNullable =
435
465
secondParameterType.makeConeTypeDefinitelyNotNullOrNotNull(
436
466
session.typeContext
@@ -458,15 +488,39 @@ class SuspendTransformFirTransformer(
458
488
operation = FirOperation .SAFE_AS
459
489
conversionTypeRef =
460
490
secondParameterTypeNotNullable.toFirResolvedTypeRef()
461
-
462
491
},
463
492
secondParameter.fir
464
493
)
465
494
466
495
} else {
467
- // this as CoroutineScope
468
- // or throw error?
496
+ // if this is CoroutineScope, put, or throw error?
497
+
498
+ var ownerIsCoroutineScopeOrParameterIsOptional = secondParameter.hasDefaultValue
499
+
500
+ for (superType in owner.getSuperTypes(session, recursive = false )) {
501
+ if (superType.isCoroutineScope()) {
502
+ put(
503
+ buildThisReceiverExpression {
504
+ coneTypeOrNull = originFunSymbol.dispatchReceiverType
505
+ source = originFunSymbol.source
506
+ calleeReference = buildImplicitThisReference {
507
+ boundSymbol = owner
508
+ }
509
+ },
510
+ secondParameter.fir
511
+ )
512
+ ownerIsCoroutineScopeOrParameterIsOptional = true
513
+ break
514
+ }
515
+ }
469
516
517
+ // or throw error?
518
+ if (! ownerIsCoroutineScopeOrParameterIsOptional) {
519
+ error(
520
+ " Owner is not a CoroutineScope, " +
521
+ " and the transformer function requires a `CoroutineScope` parameter."
522
+ )
523
+ }
470
524
}
471
525
472
526
}
@@ -535,10 +589,6 @@ class SuspendTransformFirTransformer(
535
589
),
536
590
)
537
591
538
- // val returnType = resolveReturnType(originFunc, funData, returnTypeRef)
539
- // returnTypeRef = returnType
540
- // returnTypeRef = funData.bridgeFunData.returnType
541
-
542
592
// Copy the typeParameters.
543
593
// Otherwise, in functions like the following, an error will occur
544
594
// suspend fun <A> data(value: A): T = ...
@@ -869,6 +919,7 @@ class SuspendTransformFirTransformer(
869
919
val anno = firAnnotation(func, markAnnotation, classSymbol)
870
920
? : continue
871
921
922
+
872
923
val transformerFunctionSymbol = transformerFunctionSymbolMap[transformer]
873
924
? : error(" Cannot find transformer function symbol for transformer: $transformer " )
874
925
0 commit comments