Skip to content

Commit b6c2ebd

Browse files
committed
疑似修复了某些情况下不生成合成函数或属性的问题
1 parent 83cfcb3 commit b6c2ebd

File tree

9 files changed

+308
-232
lines changed

9 files changed

+308
-232
lines changed

compiler/suspend-transform-plugin/src/main/kotlin/love/forte/plugin/suspendtrans/fir/SuspendTransformFirTransformer.kt

Lines changed: 55 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package love.forte.plugin.suspendtrans.fir
22

33
import love.forte.plugin.suspendtrans.configuration.MarkAnnotation
44
import love.forte.plugin.suspendtrans.configuration.SuspendTransformConfiguration
5+
import love.forte.plugin.suspendtrans.configuration.TargetPlatform
56
import love.forte.plugin.suspendtrans.configuration.Transformer
67
import love.forte.plugin.suspendtrans.fqn
78
import love.forte.plugin.suspendtrans.utils.*
@@ -101,16 +102,24 @@ class SuspendTransformFirTransformer(
101102
Name.identifier("CoroutineScope")
102103
)
103104

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+
107111
}
108112

109-
private fun initTransformerFunctionSymbolMap() {
113+
private fun initTransformerFunctionSymbolMap(
114+
classSymbol: FirClassSymbol<*>,
115+
memberScope: FirClassDeclaredMemberScope?
116+
): Map<Transformer, FirNamedFunctionSymbol> {
110117
// 尝试找到所有配置的 bridge function, 例如 `runBlocking` 等
111118
val symbolProvider = session.symbolProvider
112119
val dependenciesSymbolProvider = session.dependenciesSymbolProvider
113120

121+
val map = mutableMapOf<Transformer, FirNamedFunctionSymbol>()
122+
114123
suspendTransformConfiguration.transformers
115124
.forEach { (_, transformerList) ->
116125
for (transformer in transformerList) {
@@ -136,6 +145,7 @@ class SuspendTransformFirTransformer(
136145
if (transformerFunctionSymbols.isNotEmpty()) {
137146
if (transformerFunctionSymbols.size == 1) {
138147
transformerFunctionSymbolMap[transformer] = transformerFunctionSymbols.first()
148+
map[transformer] = transformerFunctionSymbols.first()
139149
} else {
140150
error("Found multiple transformer function symbols for transformer: $transformer")
141151
}
@@ -145,15 +155,18 @@ class SuspendTransformFirTransformer(
145155
}
146156
}
147157
}
158+
159+
return map
148160
}
149161

150162
// private val cache: FirCache<Pair<FirClassSymbol<*>, FirClassDeclaredMemberScope?>, Map<Name, Map<FirNamedFunctionSymbol, FunData>>?, Nothing?> =
151163
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
153166
initScopeSymbol()
154-
initTransformerFunctionSymbolMap()
167+
val transformerFunctionMap = initTransformerFunctionSymbolMap(symbol, scope)
155168

156-
createCache(symbol, scope)
169+
createCache(symbol, scope, transformerFunctionMap)
157170
}
158171

159172

@@ -337,9 +350,12 @@ class SuspendTransformFirTransformer(
337350
val lambdaTarget = FirFunctionTarget(null, isLambda = true)
338351
val lambda = buildAnonymousFunction {
339352
this.resolvePhase = FirResolvePhase.BODY_RESOLVE
353+
// this.resolvePhase = FirResolvePhase.RAW_FIR
340354
this.isLambda = true
341355
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)
343359
this.returnTypeRef = originFunSymbol.resolvedReturnTypeRef
344360
this.hasExplicitParameterList = false
345361
this.status = FirResolvedDeclarationStatusImpl.DEFAULT_STATUS_FOR_SUSPEND_FUNCTION_EXPRESSION
@@ -385,7 +401,6 @@ class SuspendTransformFirTransformer(
385401
source = thisReceiverParameter.source
386402
calleeReference = buildImplicitThisReference {
387403
boundSymbol = thisReceiverParameter.symbol
388-
// println("[${newFunSymbol}] thisReceiverParameter.symbol: ${thisReceiverParameter.symbol}")
389404
}
390405
}
391406
}
@@ -591,6 +606,7 @@ class SuspendTransformFirTransformer(
591606

592607
val newFunTarget = FirFunctionTarget(null, isLambda = false)
593608
val newFun = buildSimpleFunctionCopy(originFunc) {
609+
origin = FirDeclarationOrigin.Plugin(SuspendTransformK2V3Key)
594610
name = callableId.callableName
595611
symbol = newFunSymbol
596612
status = originFunc.status.copy(
@@ -871,46 +887,53 @@ class SuspendTransformFirTransformer(
871887
return isOverride
872888
}
873889

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 }
888894
}
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+
890912

891913
/**
892914
* NB: The predict needs to be *registered* in order to parse the [@XSerializable] type
893915
* otherwise, the annotation remains unresolved
894916
*/
895917
override fun FirDeclarationPredicateRegistrar.registerPredicates() {
896-
annotationPredicates?.also { register(it) }
918+
register(annotationPredicates)
897919
}
898920

899921
private fun createCache(
900922
classSymbol: FirClassSymbol<*>,
901-
declaredScope: FirClassDeclaredMemberScope?
923+
declaredScope: FirClassDeclaredMemberScope?,
924+
transformerFunctionSymbolMap: Map<Transformer, FirNamedFunctionSymbol>
902925
): Map<Name, Map<FirNamedFunctionSymbol, SyntheticFunData>>? {
903926
if (declaredScope == null) return null
904927

905-
fun check(targetPlatform: love.forte.plugin.suspendtrans.configuration.TargetPlatform): Boolean {
928+
fun check(targetPlatform: TargetPlatform): Boolean {
906929
val platform = classSymbol.moduleData.platform
907930

908931
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
914937
else -> false
915938
}
916939
}
@@ -945,7 +968,6 @@ class SuspendTransformFirTransformer(
945968
// 读不到注解的参数?
946969
// 必须使用 anno.getXxxArgument(Name(argument name)),
947970
// 使用 argumentMapping.mapping 获取不到结果
948-
// println("RAW AnnoData: ${anno.argumentMapping.mapping}")
949971
val annoData = anno.toTransformAnnotationData(markAnnotation, functionName)
950972

951973
val syntheticFunNameString = annoData.functionName
@@ -1274,11 +1296,9 @@ class SuspendTransformFirTransformer(
12741296

12751297
when (this) {
12761298
is ConeDynamicType -> {
1277-
//println("Dynamic type: $this")
12781299
}
12791300

12801301
is ConeFlexibleType -> {
1281-
//println("Flexible type: $this")
12821302
}
12831303

12841304
is ConeClassLikeType -> {

plugins/suspend-transform-plugin-gradle/src/main/kotlin/love/forte/plugin/suspendtrans/gradle/SuspendTransformGradlePlugin.kt

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ open class SuspendTransformGradlePlugin : KotlinCompilerPluginSupportPlugin {
5757
val isApplicable = project.plugins.hasPlugin(SuspendTransformGradlePlugin::class.java)
5858
&& project.configOrNull?.enabled?.get() != false
5959

60+
project.logger.info("Is suspend transform plugin applicable for {}: {}", kotlinCompilation, isApplicable)
61+
6062
return isApplicable
6163
}
6264

@@ -75,22 +77,21 @@ open class SuspendTransformGradlePlugin : KotlinCompilerPluginSupportPlugin {
7577
val target = kotlinCompilation.target
7678
val project = target.project
7779

78-
return project.provider { resolveSubpluginOptions(target, project) }
80+
project.logger.info("Apply suspend transform plugin to compilation {}, target: {}", kotlinCompilation, target)
81+
82+
val extension = resolveExtension(project)
83+
return project.provider { extension.toSubpluginOptions(target, project) }
7984
}
8085

81-
private fun resolveSubpluginOptions(target: KotlinTarget, project: Project): List<SubpluginOption> {
86+
private fun resolveExtension(project: Project): SuspendTransformPluginExtension {
8287
val extension = project.extensions.getByType(SuspendTransformPluginExtension::class.java)
8388

8489
@Suppress("DEPRECATION") val oldExtension =
8590
project.extensions.getByType(SuspendTransformGradleExtension::class.java)
8691
@Suppress("DEPRECATION")
87-
if (oldExtension.enabled || oldExtension.transformers.isNotEmpty()) {
88-
val showError =
89-
project.providers.gradleProperty("love.forte.plugin.suspend-transform.deprecatedExtensionError")
90-
.map { it.toBoolean() }.getOrElse(true)
91-
92-
val showWarn =
93-
project.providers.gradleProperty("love.forte.plugin.suspend-transform.deprecatedExtensionWarn")
92+
if (oldExtension.enabled && oldExtension.transformers.isNotEmpty()) {
93+
val dontShowWarn =
94+
project.providers.gradleProperty("love.forte.plugin.suspend-transform.suppressDeprecatedExtensionWarn")
9495
.map { it.toBoolean() }.getOrElse(false)
9596

9697
val msg = "The `love.forte.plugin.suspendtrans.gradle.SuspendTransformGradleExtension` " +
@@ -101,20 +102,14 @@ open class SuspendTransformGradlePlugin : KotlinCompilerPluginSupportPlugin {
101102
"will currently be aggregated with `SuspendTransformPluginExtension`, " +
102103
"but it will soon be deprecated completely. "
103104

104-
when {
105-
showError -> {
106-
project.logger.error(msg)
107-
}
108-
109-
showWarn -> {
110-
project.logger.warn(msg)
111-
}
105+
if (!dontShowWarn) {
106+
project.logger.warn(msg)
112107
}
113108

114109
oldExtension.mergeTo(extension)
115110
}
116111

117-
return extension.toSubpluginOptions(target, project)
112+
return extension
118113
}
119114
}
120115

@@ -132,13 +127,13 @@ private fun SuspendTransformGradleExtension.mergeTo(extension: SuspendTransformP
132127
// Not the default value
133128
if (deprecatedAnnotationVersion != SuspendTransPluginConstants.ANNOTATION_VERSION) {
134129
extension.annotationDependency {
135-
it.version.convention(deprecatedAnnotationVersion)
130+
version.convention(deprecatedAnnotationVersion)
136131
}
137132
}
138133
val deprecatedAnnotationConfigurationName = this.annotationConfigurationName
139134
if (deprecatedAnnotationConfigurationName != "compileOnly") {
140135
extension.annotationDependency {
141-
it.configurationName.convention(deprecatedAnnotationConfigurationName)
136+
configurationName.convention(deprecatedAnnotationConfigurationName)
142137
}
143138
}
144139

@@ -150,13 +145,13 @@ private fun SuspendTransformGradleExtension.mergeTo(extension: SuspendTransformP
150145
val deprecatedRuntimeVersion = this.runtimeDependencyVersion
151146
if (deprecatedRuntimeVersion != SuspendTransPluginConstants.RUNTIME_VERSION) {
152147
extension.runtimeDependency {
153-
it.version.convention(deprecatedRuntimeVersion)
148+
version.convention(deprecatedRuntimeVersion)
154149
}
155150
}
156151
val deprecatedRuntimeConfigurationName = this.runtimeConfigurationName
157152
if (deprecatedRuntimeConfigurationName != "implementation") {
158153
extension.runtimeDependency {
159-
it.configurationName.convention(deprecatedRuntimeConfigurationName)
154+
configurationName.convention(deprecatedRuntimeConfigurationName)
160155
}
161156
}
162157

@@ -370,6 +365,9 @@ fun Project.configureMultiplatformDependency(conf: SuspendTransformPluginExtensi
370365
return
371366
}
372367

368+
val sourceSetsByCompilation = sourceSetsByCompilation()
369+
project.logger.info("Suspend transform sourceSetsByCompilation: $sourceSetsByCompilation")
370+
373371
// 时间久远,已经忘记为什么要做这个判断了,也忘记这段是在哪儿参考来的了💀
374372
if (rootProject.getBooleanProperty("kotlin.mpp.enableGranularSourceSetsMetadata")) {
375373
val multiplatformExtensions = project.extensions.getByType(KotlinMultiplatformExtension::class.java)
@@ -403,7 +401,7 @@ fun Project.configureMultiplatformDependency(conf: SuspendTransformPluginExtensi
403401

404402
// For each source set that is only used in Native compilations, add an implementation dependency so that it
405403
// gets published and is properly consumed as a transitive dependency:
406-
sourceSetsByCompilation().forEach { (sourceSet, compilations) ->
404+
sourceSetsByCompilation.forEach { (sourceSet, compilations) ->
407405
val isSharedSourceSet = compilations.all {
408406
it.platformType == KotlinPlatformType.common || it.platformType == KotlinPlatformType.native
409407
|| it.platformType == KotlinPlatformType.js || it.platformType == KotlinPlatformType.wasm
@@ -434,7 +432,7 @@ fun Project.configureMultiplatformDependency(conf: SuspendTransformPluginExtensi
434432
}
435433
}
436434
} else {
437-
sourceSetsByCompilation().forEach { (sourceSet, compilations) ->
435+
sourceSetsByCompilation.forEach { (sourceSet, compilations) ->
438436
val platformTypes = compilations.map { it.platformType }.toSet()
439437
logger.info(
440438
"Configure sourceSet [{}]. compilations: {}, platformTypes: {}",

0 commit comments

Comments
 (0)