Skip to content

Commit a82de5a

Browse files
committed
Try to support K2
1 parent 3ab334f commit a82de5a

File tree

19 files changed

+632
-245
lines changed

19 files changed

+632
-245
lines changed

buildSrc/src/main/kotlin/IProject.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ object IProject : ProjectDetail() {
99
const val DESCRIPTION = "Generate platform-compatible functions for Kotlin suspend functions"
1010
const val HOMEPAGE = "https://github.com/ForteScarlet/kotlin-suspend-transform-compiler-plugin"
1111

12-
override val version: Version = version(0, 7, 0) - version("beta1")
12+
override val version: Version = version(0, 7, 0) - version("dev2")
1313

1414
override val homepage: String get() = HOMEPAGE
1515

compiler/suspend-transform-plugin/build.gradle.kts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,14 @@ compileKotlin.kotlinOptions.freeCompilerArgs += listOf(
5050
"-opt-in=org.jetbrains.kotlin.ir.symbols.UnsafeDuringIrConstructionAPI",
5151
)
5252

53+
tasks.withType(KotlinCompile::class.java).configureEach {
54+
// see https://youtrack.jetbrains.com/issue/KTIJ-21563
55+
// see https://youtrack.jetbrains.com/issue/KT-57297
56+
// kotlinOptions {
57+
// languageVersion = "1.9"
58+
// apiVersion = "1.9"
59+
// }
60+
}
5361

5462
repositories {
5563
maven {

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

Lines changed: 110 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import org.jetbrains.kotlin.fir.symbols.impl.FirNamedFunctionSymbol
3434
import org.jetbrains.kotlin.fir.symbols.impl.FirPropertyAccessorSymbol
3535
import org.jetbrains.kotlin.fir.symbols.impl.FirPropertySymbol
3636
import org.jetbrains.kotlin.fir.types.*
37+
import org.jetbrains.kotlin.fir.types.builder.buildErrorTypeRef
3738
import org.jetbrains.kotlin.fir.types.builder.buildResolvedTypeRef
3839
import org.jetbrains.kotlin.name.CallableId
3940
import org.jetbrains.kotlin.name.ClassId
@@ -122,10 +123,10 @@ class SuspendTransformFirTransformer(
122123

123124
origin = SuspendTransformPluginKey(
124125
data = SuspendTransformUserDataFir(
125-
originSymbol = symbol.asOriginSymbol(
126-
typeParameters = typeParameters,
127-
valueParameters = valueParameters,
128-
returnType.coneTypeOrNull?.classId
126+
originSymbol = originFunc.symbol.asOriginSymbol(
127+
typeParameters = originFunc.typeParameters,
128+
valueParameters = originFunc.valueParameters,
129+
originFunc.returnTypeRef.coneTypeOrNull?.classId
129130
),
130131
asProperty = false,
131132
transformer = funData.transformer
@@ -161,67 +162,113 @@ class SuspendTransformFirTransformer(
161162
val (functionAnnotations, propertyAnnotations) =
162163
copyAnnotations(original, funData)
163164

164-
val p = buildProperty {
165+
// val p = createMemberProperty()
166+
// owner = owner,
167+
// key = SuspendTransformPluginKey(
168+
// data = SuspendTransformUserDataFir(
169+
// originSymbol = original.symbol.asOriginSymbol(
170+
// typeParameters = original.typeParameters,
171+
// valueParameters = original.valueParameters,
172+
// original.returnTypeRef.coneTypeOrNull?.classId
173+
// ),
174+
// asProperty = true,
175+
// transformer = funData.transformer
176+
// )
177+
// ),
178+
// name = callableId.callableName,
179+
// returnTypeProvider = { resolveReturnConeType(original, funData) },
180+
// isVal = true,
181+
// hasBackingField = false,
182+
// ) {
183+
// modality = original.syntheticModifier ?: Modality.FINAL
184+
// // TODO receiver?
185+
//// val receiverType = original.receiverParameter?.typeRef?.coneTypeOrNull
186+
//// if (receiverType != null) {
187+
//// extensionReceiverType(receiverType)
188+
//// }
189+
// }
190+
191+
val pSymbol = FirPropertySymbol(callableId)
192+
val pKey = SuspendTransformPluginKey(
193+
data = SuspendTransformUserDataFir(
194+
originSymbol = original.symbol.asOriginSymbol(
195+
typeParameters = original.typeParameters,
196+
valueParameters = original.valueParameters,
197+
original.returnTypeRef.coneTypeOrNull?.classId
198+
),
199+
asProperty = true,
200+
transformer = funData.transformer
201+
)
202+
)
203+
204+
val returnType = resolveReturnType(original, funData)
205+
206+
val p1 = buildProperty {
207+
symbol = pSymbol
165208
name = callableId.callableName
166-
symbol = FirPropertySymbol(callableId)
167209
source = original.source
168210
resolvePhase = original.resolvePhase
169211
moduleData = original.moduleData
170-
origin = original.origin
212+
origin = pKey.origin
171213
attributes = original.attributes.copy()
172214
status = original.status.copy(
173215
isSuspend = false,
216+
isFun = false,
217+
isInner = false,
174218
// modality = if (original.status.isOverride) Modality.OPEN else original.status.modality,
175219
modality = original.syntheticModifier,
176220
)
177-
returnTypeRef = original.returnTypeRef
178-
deprecationsProvider = original.deprecationsProvider
221+
isVar = false
222+
isLocal = false
223+
returnTypeRef = returnType
224+
deprecationsProvider = UnresolvedDeprecationProvider //original.deprecationsProvider
179225
containerSource = original.containerSource
180226
dispatchReceiverType = original.dispatchReceiverType
181227
contextReceivers.addAll(original.contextReceivers)
182228
// annotations
183229
annotations.addAll(propertyAnnotations)
184230
typeParameters.addAll(original.typeParameters)
231+
resolvePhase = FirResolvePhase.BODY_RESOLVE
232+
backingField = null
233+
bodyResolveState = FirPropertyBodyResolveState.NOTHING_RESOLVED
234+
185235
getter = buildPropertyAccessor {
236+
propertySymbol = pSymbol
186237
symbol = FirPropertyAccessorSymbol()
187-
source = original.source
188-
resolvePhase = original.resolvePhase
238+
isGetter = true
239+
resolvePhase = FirResolvePhase.BODY_RESOLVE
189240
moduleData = original.moduleData
241+
190242
// annotations
191243
annotations.addAll(functionAnnotations)
192244

193-
val returnType = resolveReturnType(original, funData)
194-
195245
returnTypeRef = returnType
196246

197-
origin = SuspendTransformPluginKey(
198-
data = SuspendTransformUserDataFir(
199-
originSymbol = symbol.asOriginSymbol(
200-
typeParameters = typeParameters,
201-
valueParameters = valueParameters,
202-
returnType.coneTypeOrNull?.classId
203-
),
204-
asProperty = true,
205-
transformer = funData.transformer
206-
)
207-
).origin
208-
209-
attributes = original.attributes.copy()
210-
status = original.status
247+
origin = pKey.origin
248+
249+
// attributes = original.attributes.copy()
250+
status = original.status.copy(
251+
isSuspend = false,
252+
isFun = false,
253+
isInner = false,
254+
modality = original.syntheticModifier,
255+
// visibility = this@buildProperty.status
256+
)
211257
returnTypeRef = original.returnTypeRef
212-
deprecationsProvider = original.deprecationsProvider
213-
containerSource = original.containerSource
214-
dispatchReceiverType = original.dispatchReceiverType
215-
contextReceivers.addAll(original.contextReceivers)
258+
// deprecationsProvider = original.deprecationsProvider
259+
// containerSource = original.containerSource
260+
// dispatchReceiverType = original.dispatchReceiverType
261+
// contextReceivers.addAll(original.contextReceivers)
216262
valueParameters.addAll(original.valueParameters)
217-
body = original.body
218-
contractDescription = original.contractDescription
219-
annotations.addAll(original.annotations) // TODO
263+
// body = null
264+
// contractDescription = original.contractDescription
265+
// annotations
266+
annotations.addAll(functionAnnotations)
220267
typeParameters.addAll(original.typeParameters)
221268
}
222269
}
223270

224-
propList.add(p.symbol)
271+
propList.add(p1.symbol)
225272
}
226273
}
227274

@@ -275,7 +322,6 @@ class SuspendTransformFirTransformer(
275322

276323
declaredScope.processAllFunctions { func ->
277324
if (!func.isSuspend) return@processAllFunctions
278-
// if (!func.visibility.isPublicAPI) return@processAllFunctions
279325

280326
val functionName = func.name.asString()
281327
suspendTransformConfiguration.transformers.asSequence()
@@ -294,7 +340,11 @@ class SuspendTransformFirTransformer(
294340
).firstOrNull()
295341
?: continue
296342

297-
// TODO 读不到注解的参数?
343+
344+
// 读不到注解的参数?
345+
// 必须使用 anno.getXxxArgument(Name(argument name)),
346+
// 使用 argumentMapping.mapping 获取不到结果
347+
// println("RAW AnnoData: ${anno.argumentMapping.mapping}")
298348

299349
val annoData = TransformAnnotationData.of(
300350
firAnnotation = anno,
@@ -316,15 +366,29 @@ class SuspendTransformFirTransformer(
316366
}
317367

318368
private fun resolveReturnType(original: FirSimpleFunction, funData: FunData): FirTypeRef {
319-
val originalReturnTypeRef = original.returnTypeRef
369+
val resultConeType = resolveReturnConeType(original, funData)
370+
371+
return if (resultConeType is ConeErrorType) {
372+
buildErrorTypeRef {
373+
diagnostic = resultConeType.diagnostic
374+
type = resultConeType
375+
}
376+
} else {
377+
buildResolvedTypeRef {
378+
type = resultConeType
379+
}
380+
}
381+
}
382+
383+
private fun resolveReturnConeType(original: FirSimpleFunction, funData: FunData): ConeKotlinType {
320384
val transformer = funData.transformer
321385
val returnType = transformer.transformReturnType
322-
?: return originalReturnTypeRef
386+
?: return original.symbol.resolvedReturnType
323387

324388
var typeArguments: Array<ConeTypeProjection> = emptyArray()
325389

326390
if (transformer.transformReturnTypeGeneric) {
327-
typeArguments = arrayOf(ConeKotlinTypeProjectionOut(originalReturnTypeRef.coneType))
391+
typeArguments = arrayOf(ConeKotlinTypeProjectionOut(original.returnTypeRef.coneType))
328392
}
329393

330394
val resultConeType = returnType.toClassId().createConeType(
@@ -333,12 +397,9 @@ class SuspendTransformFirTransformer(
333397
nullable = returnType.nullable
334398
)
335399

336-
return buildResolvedTypeRef {
337-
type = resultConeType
338-
}
400+
return resultConeType
339401
}
340402

341-
342403
/**
343404
* @return function annotations `to` property annotations.
344405
*/
@@ -366,7 +427,7 @@ class SuspendTransformFirTransformer(
366427
}
367428

368429
// try add @Generated(by = ...)
369-
runCatching {
430+
// runCatching {
370431
// val generatedAnnotation = buildAnnotation {
371432
// annotationTypeRef = buildResolvedTypeRef {
372433
// type = generatedAnnotationClassId.createConeType(session)
@@ -376,16 +437,14 @@ class SuspendTransformFirTransformer(
376437
// }
377438
// }
378439
// add(generatedAnnotation)
379-
}.getOrElse { e ->
380-
// Where is log?
381-
e.printStackTrace()
382-
}
440+
// }.getOrElse { e ->
441+
// // Where is log?
442+
// e.printStackTrace()
443+
// }
383444

384445
// add includes
385446
includes.forEach { include ->
386447
val classId = include.classId
387-
println("!!!include: $include")
388-
println("!!!include: $classId")
389448
val includeAnnotation = buildAnnotation {
390449
argumentMapping = buildAnnotationArgumentMapping()
391450
annotationTypeRef = buildResolvedTypeRef {

compiler/suspend-transform-plugin/src/main/kotlin/love/forte/plugin/suspendtrans/ir/SuspendTransformTransformer.kt

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,16 +53,26 @@ class SuspendTransformTransformer(
5353
@OptIn(ObsoleteDescriptorBasedAPI::class)
5454
override fun visitPropertyNew(declaration: IrProperty): IrStatement {
5555
val getter = declaration.getter ?: return super.visitPropertyNew(declaration)
56-
resolveFunctionBodyByDescriptor(getter, declaration.descriptor)
56+
resolveFunctionBodyByDescriptor(getter, declaration.descriptor, declaration)
5757

5858
return super.visitPropertyNew(declaration)
5959
}
6060

6161
@OptIn(ObsoleteDescriptorBasedAPI::class)
62-
private fun resolveFunctionBodyByDescriptor(declaration: IrFunction, descriptor: CallableDescriptor): IrFunction? {
62+
private fun resolveFunctionBodyByDescriptor(
63+
declaration: IrFunction,
64+
descriptor: CallableDescriptor,
65+
property: IrProperty? = null
66+
): IrFunction? {
6367
// K2
64-
val pluginKey = (declaration.origin as? IrDeclarationOrigin.GeneratedByPlugin)
65-
?.pluginKey as? SuspendTransformPluginKey
68+
val pluginKey = if (property != null) {
69+
// from property
70+
(property.origin as? IrDeclarationOrigin.GeneratedByPlugin)
71+
?.pluginKey as? SuspendTransformPluginKey
72+
} else {
73+
(declaration.origin as? IrDeclarationOrigin.GeneratedByPlugin)
74+
?.pluginKey as? SuspendTransformPluginKey
75+
}
6676

6777
// K1 ?
6878
val userData = descriptor.getUserData(SuspendTransformUserDataKey)
@@ -76,9 +86,15 @@ class SuspendTransformTransformer(
7686

7787
resolveFunctionBody(
7888
declaration,
79-
{ f -> pluginKey.data.originSymbol.checkSame(f).also { println("Check result: $it: ($f(${f.name}) same ${pluginKey.data.originSymbol})") } },
89+
{ f ->
90+
pluginKey.data.originSymbol.checkSame(f)
91+
},
8092
callableFunction
8193
)?.also { generatedOriginFunction ->
94+
if (property != null) {
95+
// NO! BACKING! FIELD!
96+
property.backingField = null
97+
}
8298
postProcessGenerateOriginFunction(
8399
generatedOriginFunction,
84100
pluginKey.data.transformer.originFunctionIncludeAnnotations
@@ -145,7 +161,7 @@ class SuspendTransformTransformer(
145161

146162
if (originFunctions.size != 1) {
147163
val message =
148-
"Transform function $function (${function.name}) 's originFunctions.size should be 1, but ${originFunctions.size} (findIn = ${(parent as? IrDeclaration)?.descriptor}, originFunctions = $originFunctions)"
164+
"Synthetic function $function (${function.name}) 's originFunctions.size should be 1, but ${originFunctions.size} (findIn = ${(parent as? IrDeclaration)?.descriptor}, originFunctions = $originFunctions)"
149165

150166
val location = when (val sourceLocation =
151167
function.getSourceLocation(runCatching { function.fileEntry }.getOrNull())) {

compiler/suspend-transform-plugin/src/main/kotlin/love/forte/plugin/suspendtrans/utils/AnnotationDescriptorUtils.kt

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import love.forte.plugin.suspendtrans.toJvmBlockingAnnotationName
77
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
88
import org.jetbrains.kotlin.descriptors.FunctionDescriptor
99
import org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptor
10+
import org.jetbrains.kotlin.fir.declarations.getBooleanArgument
11+
import org.jetbrains.kotlin.fir.declarations.getStringArgument
1012
import org.jetbrains.kotlin.fir.expressions.FirAnnotation
1113
import org.jetbrains.kotlin.ir.builders.IrBuilderWithScope
1214
import org.jetbrains.kotlin.ir.builders.irCall
@@ -96,15 +98,12 @@ data class TransformAnnotationData(
9698
defaultAsProperty: Boolean,
9799
): TransformAnnotationData {
98100

99-
val baseName = firAnnotation.argumentMapping.mapping[Name.identifier(annotationBaseNamePropertyName)]
100-
?.accept(AbstractFirAnnotationArgumentVoidDataVisitor.nullableString, null)
101+
val baseName = firAnnotation.getStringArgument(Name.identifier(annotationBaseNamePropertyName))
101102
?.takeIf { it.isNotEmpty() }
102103

103-
val suffix = firAnnotation.argumentMapping.mapping[Name.identifier(annotationSuffixPropertyName)]
104-
?.accept(AbstractFirAnnotationArgumentVoidDataVisitor.nullableString, null)
104+
val suffix = firAnnotation.getStringArgument(Name.identifier(annotationSuffixPropertyName))
105105

106-
val rawAsProperty = firAnnotation.argumentMapping.mapping[Name.identifier(annotationAsPropertyPropertyName)]
107-
?.accept(AbstractFirAnnotationArgumentVoidDataVisitor.nullableBoolean, null)
106+
val rawAsProperty = firAnnotation.getBooleanArgument(Name.identifier(annotationAsPropertyPropertyName))
108107

109108
val functionName = "${baseName ?: defaultBaseName}${suffix ?: defaultSuffix}"
110109

0 commit comments

Comments
 (0)