@@ -2,6 +2,7 @@ package love.forte.plugin.suspendtrans.fir
2
2
3
3
import love.forte.plugin.suspendtrans.configuration.MarkAnnotation
4
4
import love.forte.plugin.suspendtrans.configuration.SuspendTransformConfiguration
5
+ import love.forte.plugin.suspendtrans.configuration.TargetPlatform
5
6
import love.forte.plugin.suspendtrans.configuration.Transformer
6
7
import love.forte.plugin.suspendtrans.fqn
7
8
import love.forte.plugin.suspendtrans.utils.*
@@ -101,16 +102,24 @@ class SuspendTransformFirTransformer(
101
102
Name .identifier(" CoroutineScope" )
102
103
)
103
104
104
- coroutineScopeSymbol = session.symbolProvider.getClassLikeSymbolByClassId(classId)
105
- ? : session.dependenciesSymbolProvider.getClassLikeSymbolByClassId(classId)
106
- ? : error(" Cannot resolve `kotlinx.coroutines.CoroutineScope` symbol." )
105
+ if (! (::coroutineScopeSymbol.isInitialized)) {
106
+ coroutineScopeSymbol = session.symbolProvider.getClassLikeSymbolByClassId(classId)
107
+ ? : session.dependenciesSymbolProvider.getClassLikeSymbolByClassId(classId)
108
+ ? : error(" Cannot resolve `kotlinx.coroutines.CoroutineScope` symbol." )
109
+ }
110
+
107
111
}
108
112
109
- private fun initTransformerFunctionSymbolMap () {
113
+ private fun initTransformerFunctionSymbolMap (
114
+ classSymbol : FirClassSymbol <* >,
115
+ memberScope : FirClassDeclaredMemberScope ?
116
+ ): Map <Transformer , FirNamedFunctionSymbol > {
110
117
// 尝试找到所有配置的 bridge function, 例如 `runBlocking` 等
111
118
val symbolProvider = session.symbolProvider
112
119
val dependenciesSymbolProvider = session.dependenciesSymbolProvider
113
120
121
+ val map = mutableMapOf<Transformer , FirNamedFunctionSymbol >()
122
+
114
123
suspendTransformConfiguration.transformers
115
124
.forEach { (_, transformerList) ->
116
125
for (transformer in transformerList) {
@@ -136,6 +145,7 @@ class SuspendTransformFirTransformer(
136
145
if (transformerFunctionSymbols.isNotEmpty()) {
137
146
if (transformerFunctionSymbols.size == 1 ) {
138
147
transformerFunctionSymbolMap[transformer] = transformerFunctionSymbols.first()
148
+ map[transformer] = transformerFunctionSymbols.first()
139
149
} else {
140
150
error(" Found multiple transformer function symbols for transformer: $transformer " )
141
151
}
@@ -145,15 +155,18 @@ class SuspendTransformFirTransformer(
145
155
}
146
156
}
147
157
}
158
+
159
+ return map
148
160
}
149
161
150
162
// private val cache: FirCache<Pair<FirClassSymbol<*>, FirClassDeclaredMemberScope?>, Map<Name, Map<FirNamedFunctionSymbol, FunData>>?, Nothing?> =
151
163
private val cache: FirCache <FirCacheKey , Map <Name , Map <FirNamedFunctionSymbol , SyntheticFunData >>? , Nothing? > =
152
- session.firCachesFactory.createCache { (symbol, scope), c ->
164
+ session.firCachesFactory.createCache { cacheKey, c ->
165
+ val (symbol, scope) = cacheKey
153
166
initScopeSymbol()
154
- initTransformerFunctionSymbolMap()
167
+ val transformerFunctionMap = initTransformerFunctionSymbolMap(symbol, scope )
155
168
156
- createCache(symbol, scope)
169
+ createCache(symbol, scope, transformerFunctionMap )
157
170
}
158
171
159
172
@@ -337,9 +350,12 @@ class SuspendTransformFirTransformer(
337
350
val lambdaTarget = FirFunctionTarget (null , isLambda = true )
338
351
val lambda = buildAnonymousFunction {
339
352
this .resolvePhase = FirResolvePhase .BODY_RESOLVE
353
+ // this.resolvePhase = FirResolvePhase.RAW_FIR
340
354
this .isLambda = true
341
355
this .moduleData = originFunSymbol.moduleData
342
- this .origin = FirDeclarationOrigin .Synthetic .FakeFunction
356
+ // this.origin = FirDeclarationOrigin.Source
357
+ // this.origin = FirDeclarationOrigin.Synthetic.FakeFunction
358
+ this .origin = FirDeclarationOrigin .Plugin (SuspendTransformK2V3Key )
343
359
this .returnTypeRef = originFunSymbol.resolvedReturnTypeRef
344
360
this .hasExplicitParameterList = false
345
361
this .status = FirResolvedDeclarationStatusImpl .DEFAULT_STATUS_FOR_SUSPEND_FUNCTION_EXPRESSION
@@ -385,7 +401,6 @@ class SuspendTransformFirTransformer(
385
401
source = thisReceiverParameter.source
386
402
calleeReference = buildImplicitThisReference {
387
403
boundSymbol = thisReceiverParameter.symbol
388
- // println("[${newFunSymbol}] thisReceiverParameter.symbol: ${thisReceiverParameter.symbol}")
389
404
}
390
405
}
391
406
}
@@ -591,6 +606,7 @@ class SuspendTransformFirTransformer(
591
606
592
607
val newFunTarget = FirFunctionTarget (null , isLambda = false )
593
608
val newFun = buildSimpleFunctionCopy(originFunc) {
609
+ origin = FirDeclarationOrigin .Plugin (SuspendTransformK2V3Key )
594
610
name = callableId.callableName
595
611
symbol = newFunSymbol
596
612
status = originFunc.status.copy(
@@ -871,46 +887,53 @@ class SuspendTransformFirTransformer(
871
887
return isOverride
872
888
}
873
889
874
- private val annotationPredicates: DeclarationPredicate ? =
875
- if (suspendTransformConfiguration.transformers.values.isEmpty()) {
876
- null
877
- } else {
878
- DeclarationPredicate .create {
879
- var predicate: DeclarationPredicate ? = null
880
- for (value in suspendTransformConfiguration.transformers.values) {
881
- for (transformer in value) {
882
- val afq = transformer.markAnnotation.fqName
883
- predicate = predicate?.also { p -> p or annotated(afq) } ? : annotated(afq)
884
- }
885
- }
886
-
887
- predicate ? : annotated()
890
+ private val annotationPredicates = DeclarationPredicate .create {
891
+ val annotationFqNames = suspendTransformConfiguration.transformers.values
892
+ .flatMapTo(mutableSetOf ()) { transformerList ->
893
+ transformerList.map { it.markAnnotation.fqName }
888
894
}
889
- }
895
+
896
+ hasAnnotated(annotationFqNames)
897
+ // var predicate: DeclarationPredicate? = null
898
+ // for (value in suspendTransformConfiguration.transformers.values) {
899
+ // for (transformer in value) {
900
+ // val afq = transformer.markAnnotation.fqName
901
+ // predicate = if (predicate == null) {
902
+ // annotated(afq)
903
+ // } else {
904
+ // predicate or annotated(afq)
905
+ // }
906
+ // }
907
+ // }
908
+ //
909
+ // predicate ?: annotated()
910
+ }
911
+
890
912
891
913
/* *
892
914
* NB: The predict needs to be *registered* in order to parse the [@XSerializable] type
893
915
* otherwise, the annotation remains unresolved
894
916
*/
895
917
override fun FirDeclarationPredicateRegistrar.registerPredicates () {
896
- annotationPredicates?. also { register(it) }
918
+ register(annotationPredicates)
897
919
}
898
920
899
921
private fun createCache (
900
922
classSymbol : FirClassSymbol <* >,
901
- declaredScope : FirClassDeclaredMemberScope ?
923
+ declaredScope : FirClassDeclaredMemberScope ? ,
924
+ transformerFunctionSymbolMap : Map <Transformer , FirNamedFunctionSymbol >
902
925
): Map <Name , Map <FirNamedFunctionSymbol , SyntheticFunData >>? {
903
926
if (declaredScope == null ) return null
904
927
905
- fun check (targetPlatform : love.forte.plugin.suspendtrans.configuration. TargetPlatform ): Boolean {
928
+ fun check (targetPlatform : TargetPlatform ): Boolean {
906
929
val platform = classSymbol.moduleData.platform
907
930
908
931
return when {
909
- platform.isJvm() && targetPlatform == love.forte.plugin.suspendtrans.configuration. TargetPlatform .JVM -> true
910
- platform.isJs() && targetPlatform == love.forte.plugin.suspendtrans.configuration. TargetPlatform .JS -> true
911
- platform.isWasm() && targetPlatform == love.forte.plugin.suspendtrans.configuration. TargetPlatform .WASM -> true
912
- platform.isNative() && targetPlatform == love.forte.plugin.suspendtrans.configuration. TargetPlatform .NATIVE -> true
913
- platform.isCommon() && targetPlatform == love.forte.plugin.suspendtrans.configuration. TargetPlatform .COMMON -> true
932
+ platform.isJvm() && targetPlatform == TargetPlatform .JVM -> true
933
+ platform.isJs() && targetPlatform == TargetPlatform .JS -> true
934
+ platform.isWasm() && targetPlatform == TargetPlatform .WASM -> true
935
+ platform.isNative() && targetPlatform == TargetPlatform .NATIVE -> true
936
+ platform.isCommon() && targetPlatform == TargetPlatform .COMMON -> true
914
937
else -> false
915
938
}
916
939
}
@@ -945,7 +968,6 @@ class SuspendTransformFirTransformer(
945
968
// 读不到注解的参数?
946
969
// 必须使用 anno.getXxxArgument(Name(argument name)),
947
970
// 使用 argumentMapping.mapping 获取不到结果
948
- // println("RAW AnnoData: ${anno.argumentMapping.mapping}")
949
971
val annoData = anno.toTransformAnnotationData(markAnnotation, functionName)
950
972
951
973
val syntheticFunNameString = annoData.functionName
@@ -1274,11 +1296,9 @@ class SuspendTransformFirTransformer(
1274
1296
1275
1297
when (this ) {
1276
1298
is ConeDynamicType -> {
1277
- // println("Dynamic type: $this")
1278
1299
}
1279
1300
1280
1301
is ConeFlexibleType -> {
1281
- // println("Flexible type: $this")
1282
1302
}
1283
1303
1284
1304
is ConeClassLikeType -> {
0 commit comments