Skip to content

Added an annotation for function positioning in K2 TargetMarker #73

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion buildSrc/src/main/kotlin/IProject.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ object IProject : ProjectDetail() {

// Remember the libs.versions.toml!
val ktVersion = "2.1.0"
val pluginVersion = "0.9.4"
val pluginVersion = "0.10.0"

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

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//internal object LocalLoggerHelper {
// private val dir = System.getenv("kstcp.logger.dir")
// private val path = Path(dir ?: ".plugin-logger") / Path("${System.currentTimeMillis()}.log")
// init {
// path.parent.createDirectories()
// if (path.notExists()) {
// path.createFile()
// }
// }
//
// fun println(value: Any?) {
// kotlin.io.println(value)
// path.appendText("[${LocalDateTime.now()}] $value\n", Charsets.UTF_8)
// }
//}
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,13 @@ open class SuspendTransformConfiguration {

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

/**
* 在 K2 中,用于使 IR 的合成函数可以定位到 FIR 中原始函数的辅助注解。
*
* @since *-0.10.0
*/
open var targetMarker: ClassInfo? = targetMarkerClassInfo

open fun clear() {
transformers.clear()
}
Expand Down Expand Up @@ -217,6 +224,8 @@ open class SuspendTransformConfiguration {
}

companion object {
val targetMarkerClassInfo = ClassInfo("love.forte.plugin.suspendtrans.annotation", "TargetMarker")

//region JVM defaults
@JvmStatic
val jvmSyntheticClassInfo = ClassInfo("kotlin.jvm", "JvmSynthetic")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,25 @@ package love.forte.plugin.suspendtrans

import org.jetbrains.kotlin.descriptors.CallableDescriptor
import org.jetbrains.kotlin.descriptors.SimpleFunctionDescriptor
import org.jetbrains.kotlin.fir.FirSession
import org.jetbrains.kotlin.fir.analysis.checkers.toClassLikeSymbol
import org.jetbrains.kotlin.fir.declarations.ExpectForActualMatchingData
import org.jetbrains.kotlin.fir.declarations.FirTypeParameter
import org.jetbrains.kotlin.fir.declarations.FirValueParameter
import org.jetbrains.kotlin.fir.declarations.expectForActual
import org.jetbrains.kotlin.fir.scopes.impl.toConeType
import org.jetbrains.kotlin.fir.symbols.SymbolInternals
import org.jetbrains.kotlin.fir.symbols.impl.FirNamedFunctionSymbol
import org.jetbrains.kotlin.fir.types.ConeKotlinType
import org.jetbrains.kotlin.fir.types.ConeTypeParameterType
import org.jetbrains.kotlin.fir.types.classId
import org.jetbrains.kotlin.fir.types.coneTypeOrNull
import org.jetbrains.kotlin.ir.declarations.IrFunction
import org.jetbrains.kotlin.ir.declarations.IrTypeParameter
import org.jetbrains.kotlin.ir.declarations.IrValueParameter
import org.jetbrains.kotlin.ir.expressions.IrConst
import org.jetbrains.kotlin.ir.types.classFqName
import org.jetbrains.kotlin.ir.util.callableId
import org.jetbrains.kotlin.ir.util.hasEqualFqName
import org.jetbrains.kotlin.ir.util.*
import org.jetbrains.kotlin.js.descriptorUtils.getKotlinTypeFqName
import org.jetbrains.kotlin.name.CallableId
import org.jetbrains.kotlin.name.ClassId
Expand Down Expand Up @@ -134,48 +142,147 @@ data class OriginValueParameter(

data class SuspendTransformUserDataFir(
val originSymbol: OriginSymbol,
val markerId: String,
val asProperty: Boolean,
val transformer: Transformer
)

fun FirNamedFunctionSymbol.asOriginSymbol(
targetMarker: ClassId?,
typeParameters: List<FirTypeParameter>,
valueParameters: List<FirValueParameter>,
returnType: ClassId?
returnType: ClassId?,
session: FirSession,
): OriginSymbol {
return OriginSymbol(
targetMarker,
symbol = this,
callableId = this.callableId,
typeParameters = typeParameters.map { it.toTypeParameter() },
valueParameters = valueParameters.map { it.toValueParameter() },
valueParameters = valueParameters.mapIndexed { index, p -> p.toValueParameter(session, index) },
returnType
)
}

data class OriginSymbol(
val targetMarker: ClassId?,
val symbol: FirNamedFunctionSymbol,
val callableId: CallableId,
val typeParameters: List<TypeParameter>,
val valueParameters: List<ValueParameter>,
val returnType: ClassId?
)

data class TypeParameter(val name: Name, val varianceOrdinal: Int, val isReified: Boolean, val bounds: List<ClassId?>)
data class TypeParameter(
val name: Name,
val varianceOrdinal: Int,
val isReified: Boolean,
val bounds: List<ClassId?>,
val type: ConeTypeParameterType,
)

private fun FirTypeParameter.toTypeParameter(): TypeParameter =
TypeParameter(
private fun FirTypeParameter.toTypeParameter(): TypeParameter {
return TypeParameter(
name,
variance.ordinal,
isReified,
bounds.map { it.coneTypeOrNull?.classId }
bounds.map { it.coneTypeOrNull?.classId },
toConeType(),
)
}


data class ValueParameter(
val fir: FirValueParameter,
val name: Name,
val index: Int,
val coneType: ConeKotlinType?,
val type: ClassId?,
val expectForActual: ExpectForActualMatchingData?
)

@OptIn(SymbolInternals::class)
private fun FirValueParameter.toValueParameter(session: FirSession, index: Int): ValueParameter {
// LocalLoggerHelper.println("returnTypeRef = $returnTypeRef")
// LocalLoggerHelper.println("symbol.resolvedReturnTypeRef = ${symbol.resolvedReturnTypeRef}")
// LocalLoggerHelper.println("symbol.resolvedReturnTypeRef.coneType = ${symbol.resolvedReturnTypeRef.coneType}")
// LocalLoggerHelper.println("symbol.resolvedReturnTypeRef.coneType.isTypealiasExpansion = ${symbol.resolvedReturnTypeRef.coneType.isTypealiasExpansion}")
// LocalLoggerHelper.println(
// "symbol.resolvedReturnTypeRef.coneType.fullyExpandedType(session) = ${
// symbol.resolvedReturnTypeRef.coneType.fullyExpandedType(
// session
// )
// }"
// )
//
// LocalLoggerHelper.println(
// "returnTypeRef.coneType.toClassLikeSymbol(session)?.isActual: ${
// returnTypeRef.coneType.toClassLikeSymbol(
// session
// )?.isActual
// }"
// )
// LocalLoggerHelper.println(
// "returnTypeRef.coneType.toClassLikeSymbol(session)?.isExpect: ${
// returnTypeRef.coneType.toClassLikeSymbol(
// session
// )?.isExpect
// }"
// )
//
// LocalLoggerHelper.println(
// "returnTypeRef.coneType.toRegularClassSymbol(session): ${
// returnTypeRef.coneType.toRegularClassSymbol(
// session
// )
// }"
// )
// LocalLoggerHelper.println(
// "returnTypeRef.coneType.toClassLikeSymbol(session): ${
// returnTypeRef.coneType.toClassLikeSymbol(
// session
// )
// }"
// )
//
// LocalLoggerHelper.println(
// "returnTypeRef.coneType.toRegularClassSymbol(session)?.fir?.expectForActual: " +
// "${returnTypeRef.coneType.toRegularClassSymbol(session)?.fir?.expectForActual}"
// )
//
// LocalLoggerHelper.println(
// "returnTypeRef.coneType.toRegularClassSymbol(session)?.fir?.memberExpectForActual: " +
// "${returnTypeRef.coneType.toRegularClassSymbol(session)?.fir?.memberExpectForActual}"
// )
//
// LocalLoggerHelper.println(
// "returnTypeRef.coneType.toRegularClassSymbol(session)?.fir?.fullyExpandedClass.defaultType: " +
// "${
// returnTypeRef.coneType.toRegularClassSymbol(session)?.fir?.fullyExpandedClass(session)
// ?.defaultType()
// }"
// )

return ValueParameter(
this,
name,
index,
returnTypeRef.coneTypeOrNull,
returnTypeRef.coneTypeOrNull?.classId,
returnTypeRef.toClassLikeSymbol(session)?.expectForActual
)
}

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

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

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

fun OriginSymbol.checkSame(declaration: IrFunction): Boolean {
// callableId
if (callableId != declaration.callableId) return false
// return type
Expand Down Expand Up @@ -215,6 +322,6 @@ private infix fun IrTypeParameter.isSameAs(typeParameter: TypeParameter): Boolea
}

private infix fun IrValueParameter.isSameAs(valueParameter: ValueParameter): Boolean {
if (name != valueParameter.name) return false
if (index != valueParameter.index) return false
return type.classFqName == valueParameter.type?.asSingleFqName()
}
Loading