Skip to content

Commit 1e38b25

Browse files
authored
Merge pull request #73 from ForteScarlet/fix-72
Added an annotation for function positioning in K2 TargetMarker
2 parents 939a596 + a76553a commit 1e38b25

File tree

32 files changed

+849
-124
lines changed

32 files changed

+849
-124
lines changed

buildSrc/src/main/kotlin/IProject.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ object IProject : ProjectDetail() {
1111

1212
// Remember the libs.versions.toml!
1313
val ktVersion = "2.1.0"
14-
val pluginVersion = "0.9.4"
14+
val pluginVersion = "0.10.0"
1515

1616
override val version: String = "$ktVersion-$pluginVersion"
1717

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//internal object LocalLoggerHelper {
2+
// private val dir = System.getenv("kstcp.logger.dir")
3+
// private val path = Path(dir ?: ".plugin-logger") / Path("${System.currentTimeMillis()}.log")
4+
// init {
5+
// path.parent.createDirectories()
6+
// if (path.notExists()) {
7+
// path.createFile()
8+
// }
9+
// }
10+
//
11+
// fun println(value: Any?) {
12+
// kotlin.io.println(value)
13+
// path.appendText("[${LocalDateTime.now()}] $value\n", Charsets.UTF_8)
14+
// }
15+
//}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,13 @@ open class SuspendTransformConfiguration {
159159

160160
open var transformers: MutableMap<TargetPlatform, List<Transformer>> = mutableMapOf()
161161

162+
/**
163+
* 在 K2 中,用于使 IR 的合成函数可以定位到 FIR 中原始函数的辅助注解。
164+
*
165+
* @since *-0.10.0
166+
*/
167+
open var targetMarker: ClassInfo? = targetMarkerClassInfo
168+
162169
open fun clear() {
163170
transformers.clear()
164171
}
@@ -217,6 +224,8 @@ open class SuspendTransformConfiguration {
217224
}
218225

219226
companion object {
227+
val targetMarkerClassInfo = ClassInfo("love.forte.plugin.suspendtrans.annotation", "TargetMarker")
228+
220229
//region JVM defaults
221230
@JvmStatic
222231
val jvmSyntheticClassInfo = ClassInfo("kotlin.jvm", "JvmSynthetic")

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

Lines changed: 120 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,25 @@ package love.forte.plugin.suspendtrans
22

33
import org.jetbrains.kotlin.descriptors.CallableDescriptor
44
import org.jetbrains.kotlin.descriptors.SimpleFunctionDescriptor
5+
import org.jetbrains.kotlin.fir.FirSession
6+
import org.jetbrains.kotlin.fir.analysis.checkers.toClassLikeSymbol
7+
import org.jetbrains.kotlin.fir.declarations.ExpectForActualMatchingData
58
import org.jetbrains.kotlin.fir.declarations.FirTypeParameter
69
import org.jetbrains.kotlin.fir.declarations.FirValueParameter
10+
import org.jetbrains.kotlin.fir.declarations.expectForActual
11+
import org.jetbrains.kotlin.fir.scopes.impl.toConeType
12+
import org.jetbrains.kotlin.fir.symbols.SymbolInternals
713
import org.jetbrains.kotlin.fir.symbols.impl.FirNamedFunctionSymbol
14+
import org.jetbrains.kotlin.fir.types.ConeKotlinType
15+
import org.jetbrains.kotlin.fir.types.ConeTypeParameterType
816
import org.jetbrains.kotlin.fir.types.classId
917
import org.jetbrains.kotlin.fir.types.coneTypeOrNull
1018
import org.jetbrains.kotlin.ir.declarations.IrFunction
1119
import org.jetbrains.kotlin.ir.declarations.IrTypeParameter
1220
import org.jetbrains.kotlin.ir.declarations.IrValueParameter
21+
import org.jetbrains.kotlin.ir.expressions.IrConst
1322
import org.jetbrains.kotlin.ir.types.classFqName
14-
import org.jetbrains.kotlin.ir.util.callableId
15-
import org.jetbrains.kotlin.ir.util.hasEqualFqName
23+
import org.jetbrains.kotlin.ir.util.*
1624
import org.jetbrains.kotlin.js.descriptorUtils.getKotlinTypeFqName
1725
import org.jetbrains.kotlin.name.CallableId
1826
import org.jetbrains.kotlin.name.ClassId
@@ -134,48 +142,147 @@ data class OriginValueParameter(
134142

135143
data class SuspendTransformUserDataFir(
136144
val originSymbol: OriginSymbol,
145+
val markerId: String,
137146
val asProperty: Boolean,
138147
val transformer: Transformer
139148
)
140149

141150
fun FirNamedFunctionSymbol.asOriginSymbol(
151+
targetMarker: ClassId?,
142152
typeParameters: List<FirTypeParameter>,
143153
valueParameters: List<FirValueParameter>,
144-
returnType: ClassId?
154+
returnType: ClassId?,
155+
session: FirSession,
145156
): OriginSymbol {
146157
return OriginSymbol(
158+
targetMarker,
159+
symbol = this,
147160
callableId = this.callableId,
148161
typeParameters = typeParameters.map { it.toTypeParameter() },
149-
valueParameters = valueParameters.map { it.toValueParameter() },
162+
valueParameters = valueParameters.mapIndexed { index, p -> p.toValueParameter(session, index) },
150163
returnType
151164
)
152165
}
153166

154167
data class OriginSymbol(
168+
val targetMarker: ClassId?,
169+
val symbol: FirNamedFunctionSymbol,
155170
val callableId: CallableId,
156171
val typeParameters: List<TypeParameter>,
157172
val valueParameters: List<ValueParameter>,
158173
val returnType: ClassId?
159174
)
160175

161-
data class TypeParameter(val name: Name, val varianceOrdinal: Int, val isReified: Boolean, val bounds: List<ClassId?>)
176+
data class TypeParameter(
177+
val name: Name,
178+
val varianceOrdinal: Int,
179+
val isReified: Boolean,
180+
val bounds: List<ClassId?>,
181+
val type: ConeTypeParameterType,
182+
)
162183

163-
private fun FirTypeParameter.toTypeParameter(): TypeParameter =
164-
TypeParameter(
184+
private fun FirTypeParameter.toTypeParameter(): TypeParameter {
185+
return TypeParameter(
165186
name,
166187
variance.ordinal,
167188
isReified,
168-
bounds.map { it.coneTypeOrNull?.classId }
189+
bounds.map { it.coneTypeOrNull?.classId },
190+
toConeType(),
169191
)
192+
}
193+
194+
195+
data class ValueParameter(
196+
val fir: FirValueParameter,
197+
val name: Name,
198+
val index: Int,
199+
val coneType: ConeKotlinType?,
200+
val type: ClassId?,
201+
val expectForActual: ExpectForActualMatchingData?
202+
)
170203

204+
@OptIn(SymbolInternals::class)
205+
private fun FirValueParameter.toValueParameter(session: FirSession, index: Int): ValueParameter {
206+
// LocalLoggerHelper.println("returnTypeRef = $returnTypeRef")
207+
// LocalLoggerHelper.println("symbol.resolvedReturnTypeRef = ${symbol.resolvedReturnTypeRef}")
208+
// LocalLoggerHelper.println("symbol.resolvedReturnTypeRef.coneType = ${symbol.resolvedReturnTypeRef.coneType}")
209+
// LocalLoggerHelper.println("symbol.resolvedReturnTypeRef.coneType.isTypealiasExpansion = ${symbol.resolvedReturnTypeRef.coneType.isTypealiasExpansion}")
210+
// LocalLoggerHelper.println(
211+
// "symbol.resolvedReturnTypeRef.coneType.fullyExpandedType(session) = ${
212+
// symbol.resolvedReturnTypeRef.coneType.fullyExpandedType(
213+
// session
214+
// )
215+
// }"
216+
// )
217+
//
218+
// LocalLoggerHelper.println(
219+
// "returnTypeRef.coneType.toClassLikeSymbol(session)?.isActual: ${
220+
// returnTypeRef.coneType.toClassLikeSymbol(
221+
// session
222+
// )?.isActual
223+
// }"
224+
// )
225+
// LocalLoggerHelper.println(
226+
// "returnTypeRef.coneType.toClassLikeSymbol(session)?.isExpect: ${
227+
// returnTypeRef.coneType.toClassLikeSymbol(
228+
// session
229+
// )?.isExpect
230+
// }"
231+
// )
232+
//
233+
// LocalLoggerHelper.println(
234+
// "returnTypeRef.coneType.toRegularClassSymbol(session): ${
235+
// returnTypeRef.coneType.toRegularClassSymbol(
236+
// session
237+
// )
238+
// }"
239+
// )
240+
// LocalLoggerHelper.println(
241+
// "returnTypeRef.coneType.toClassLikeSymbol(session): ${
242+
// returnTypeRef.coneType.toClassLikeSymbol(
243+
// session
244+
// )
245+
// }"
246+
// )
247+
//
248+
// LocalLoggerHelper.println(
249+
// "returnTypeRef.coneType.toRegularClassSymbol(session)?.fir?.expectForActual: " +
250+
// "${returnTypeRef.coneType.toRegularClassSymbol(session)?.fir?.expectForActual}"
251+
// )
252+
//
253+
// LocalLoggerHelper.println(
254+
// "returnTypeRef.coneType.toRegularClassSymbol(session)?.fir?.memberExpectForActual: " +
255+
// "${returnTypeRef.coneType.toRegularClassSymbol(session)?.fir?.memberExpectForActual}"
256+
// )
257+
//
258+
// LocalLoggerHelper.println(
259+
// "returnTypeRef.coneType.toRegularClassSymbol(session)?.fir?.fullyExpandedClass.defaultType: " +
260+
// "${
261+
// returnTypeRef.coneType.toRegularClassSymbol(session)?.fir?.fullyExpandedClass(session)
262+
// ?.defaultType()
263+
// }"
264+
// )
265+
266+
return ValueParameter(
267+
this,
268+
name,
269+
index,
270+
returnTypeRef.coneTypeOrNull,
271+
returnTypeRef.coneTypeOrNull?.classId,
272+
returnTypeRef.toClassLikeSymbol(session)?.expectForActual
273+
)
274+
}
171275

172-
data class ValueParameter(val name: Name, val type: ClassId?)
173276

174-
private fun FirValueParameter.toValueParameter(): ValueParameter =
175-
ValueParameter(name, returnTypeRef.coneTypeOrNull?.classId)
277+
fun OriginSymbol.checkSame(markerId: String, declaration: IrFunction): Boolean {
278+
if (targetMarker != null) {
279+
val anno = declaration.annotations.firstOrNull { it.symbol.owner.parentAsClass.classId == targetMarker }
280+
if (anno == null) return false
176281

282+
val valueArgument = anno.getValueArgument(Name.identifier("value")) as? IrConst ?: return false
283+
return markerId == valueArgument.value
284+
}
177285

178-
fun OriginSymbol.checkSame(declaration: IrFunction): Boolean {
179286
// callableId
180287
if (callableId != declaration.callableId) return false
181288
// return type
@@ -215,6 +322,6 @@ private infix fun IrTypeParameter.isSameAs(typeParameter: TypeParameter): Boolea
215322
}
216323

217324
private infix fun IrValueParameter.isSameAs(valueParameter: ValueParameter): Boolean {
218-
if (name != valueParameter.name) return false
325+
if (index != valueParameter.index) return false
219326
return type.classFqName == valueParameter.type?.asSingleFqName()
220327
}

0 commit comments

Comments
 (0)