Skip to content

Commit

Permalink
Type Parameter!!!!😡
Browse files Browse the repository at this point in the history
  • Loading branch information
ForteScarlet committed Nov 13, 2024
1 parent 1a9f59c commit 64686c1
Show file tree
Hide file tree
Showing 8 changed files with 185 additions and 43 deletions.
2 changes: 1 addition & 1 deletion buildSrc/src/main/kotlin/IProject.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ object IProject : ProjectDetail() {
const val HOMEPAGE = "https://github.com/ForteScarlet/kotlin-suspend-transform-compiler-plugin"

// Remember the libs.versions.toml!
val ktVersion = "2.0.20"
val ktVersion = "2.0.21"
val pluginVersion = "0.9.4"

override val version: String = "$ktVersion-$pluginVersion"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import org.jetbrains.kotlin.fir.analysis.checkers.context.CheckerContext
import org.jetbrains.kotlin.fir.analysis.checkers.context.MutableCheckerContext
import org.jetbrains.kotlin.fir.analysis.checkers.getContainingClassSymbol
import org.jetbrains.kotlin.fir.analysis.checkers.processOverriddenFunctions
import org.jetbrains.kotlin.fir.builder.buildFunctionTypeParameter
import org.jetbrains.kotlin.fir.caches.FirCache
import org.jetbrains.kotlin.fir.caches.firCachesFactory
import org.jetbrains.kotlin.fir.caches.getValue
Expand All @@ -36,14 +35,14 @@ import org.jetbrains.kotlin.fir.resolve.SessionHolderImpl
import org.jetbrains.kotlin.fir.resolve.getSuperTypes
import org.jetbrains.kotlin.fir.resolve.transformers.ReturnTypeCalculatorForFullBodyResolve
import org.jetbrains.kotlin.fir.scopes.impl.FirClassDeclaredMemberScope
import org.jetbrains.kotlin.fir.scopes.impl.toConeType
import org.jetbrains.kotlin.fir.scopes.processAllFunctions
import org.jetbrains.kotlin.fir.symbols.FirBasedSymbol
import org.jetbrains.kotlin.fir.symbols.SymbolInternals
import org.jetbrains.kotlin.fir.symbols.impl.*
import org.jetbrains.kotlin.fir.types.*
import org.jetbrains.kotlin.fir.types.builder.buildErrorTypeRef
import org.jetbrains.kotlin.fir.types.builder.buildResolvedTypeRef
import org.jetbrains.kotlin.ir.builders.declarations.buildTypeParameter
import org.jetbrains.kotlin.name.CallableId
import org.jetbrains.kotlin.name.ClassId
import org.jetbrains.kotlin.name.FqName
Expand Down Expand Up @@ -150,14 +149,104 @@ class SuspendTransformFirTransformer(
// In the generated IR, data and dataBlocking will share an `A`, generating the error.
// The error: Duplicate IR node
// [IR VALIDATION] JvmIrValidationBeforeLoweringPhase: Duplicate IR node: TYPE_PARAMETER name:A index:0 variance: superTypes:[kotlin.Any?] reified:false of FUN GENERATED[...]
// TODO onebot type parameters?
// TODO copy to value parameters, receiver and return type?
val originalTypeParameterCache = mutableMapOf<FirTypeParameter, FirTypeParameter>()
typeParameters.replaceAll {
buildTypeParameterCopy(it) {
containingDeclarationSymbol = newFunSymbol // it.containingDeclarationSymbol
symbol = it.symbol // FirTypeParameterSymbol()
// symbol = it.symbol // FirTypeParameterSymbol()
symbol = FirTypeParameterSymbol()
}.also { new ->
originalTypeParameterCache[it] = new

}
}

// TODO
valueParameters.replaceAll { vp ->
buildValueParameterCopy(vp) {
symbol = FirValueParameterSymbol(vp.symbol.name)

val cachedTypeParameter = originalTypeParameterCache.entries.find { (k, v) ->
k.toConeType() == vp.returnTypeRef.coneTypeOrNull
}
val newReturnTypeRef = if (cachedTypeParameter != null) {
returnTypeRef.withReplacedConeType(cachedTypeParameter.value.toConeType())
} else {
println("returnTypeRef: $returnTypeRef")
returnTypeRef.coneType.typeArguments.forEach {
println("returnTypeRef.coneType.typeArguments: $it")
}

returnTypeRef
}

returnTypeRef = newReturnTypeRef
}
}

// valueParameters.replaceAll { vp ->
// buildValueParameterCopy(vp) {
// //println("find: ${originalTypeParameterCache[vp.returnTypeRef]}")
// val cachedTypeParameter = originalTypeParameterCache.entries.find { (k, v) ->
// k.toConeType() == vp.returnTypeRef.coneTypeOrNull
// }
// println("cache conetype: $cachedTypeParameter")
//
// println("returnTypeRef1: $returnTypeRef")
//
// val stack = ArrayDeque<Any>()
//
// fun resolveTypeCopy() {
//
// }
//
// val type = returnTypeRef.coneTypeOrNull
// if (type != null && type.typeArguments.isNotEmpty()) {
// for (subArguments in type.typeArguments) {
//
// }
// }
//
// returnTypeRef.accept(object : FirVisitorVoid() {
// override fun visitElement(element: FirElement) {
// println("visitElement($element)")
// element.acceptChildren(this)
// }
//
// override fun visitResolvedTypeRef(resolvedTypeRef: FirResolvedTypeRef) {
// println("visitResolvedTypeRef(${resolvedTypeRef})")
// resolvedTypeRef.type.typeArguments.forEach {
// it.type?.typeArguments
// }
// super.visitResolvedTypeRef(resolvedTypeRef)
// }
//
// override fun visitValueParameter(valueParameter: FirValueParameter) {
// println("visitValueParameter($valueParameter)")
// super.visitValueParameter(valueParameter)
// }
//
// override fun visitTypeParameter(typeParameter: FirTypeParameter) {
// println("visitTypeParameter($typeParameter)")
// super.visitTypeParameter(typeParameter)
// }
//
// override fun visitTypeParameterRef(typeParameterRef: FirTypeParameterRef) {
// println("visitTypeParameterRef($typeParameterRef)")
// super.visitTypeParameterRef(typeParameterRef)
// }
// })
//
// if (cachedTypeParameter != null) {
// returnTypeRef = returnTypeRef.withReplacedConeType(cachedTypeParameter.value.toConeType())
// }
//
// println("returnTypeRef2: $returnTypeRef")
// symbol = FirValueParameterSymbol(vp.symbol.name)
// }
// }

// valueParameters.replaceAll { vp ->
// buildValueParameterCopy(vp) {
// containingFunctionSymbol = newFunSymbol
Expand Down Expand Up @@ -185,7 +274,6 @@ class SuspendTransformFirTransformer(
).origin
}


funList.add(newFun.symbol)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,29 @@
public final class Api : java/lang/Object {
public void <init>()
}

public class Bar : java/lang/Object {
public void <init>()
}

public final class Foo$DefaultImpls : java/lang/Object {
public static java.util.concurrent.CompletableFuture runAsync(Foo $this)
public static java.util.concurrent.CompletableFuture runAsync(Foo $this, Api api)

public static java.lang.Object runBlocking(Foo $this)
public static java.lang.Object runBlocking(Foo $this, Api api)

public static java.util.concurrent.CompletableFuture valueAsync(Foo $this)

public static Bar valueBlocking(Foo $this)
}

final class Foo$runAsync$1 : kotlin/coroutines/jvm/internal/SuspendLambda, kotlin/jvm/functions/Function1 {
final Api $api

int label

final Foo this$0

void <init>(Foo $receiver, kotlin.coroutines.Continuation $completion)
void <init>(Foo $receiver, Api $api, kotlin.coroutines.Continuation $completion)

public final kotlin.coroutines.Continuation create(kotlin.coroutines.Continuation $completion)

Expand All @@ -29,11 +35,13 @@ final class Foo$runAsync$1 : kotlin/coroutines/jvm/internal/SuspendLambda, kotli
}

final class Foo$runBlocking$1 : kotlin/coroutines/jvm/internal/SuspendLambda, kotlin/jvm/functions/Function1 {
final Api $api

int label

final Foo this$0

void <init>(Foo $receiver, kotlin.coroutines.Continuation $completion)
void <init>(Foo $receiver, Api $api, kotlin.coroutines.Continuation $completion)

public final kotlin.coroutines.Continuation create(kotlin.coroutines.Continuation $completion)

Expand Down Expand Up @@ -77,11 +85,11 @@ final class Foo$valueBlocking$1 : kotlin/coroutines/jvm/internal/SuspendLambda,
}

public abstract interface Foo : java/lang/Object {
public abstract java.lang.Object run(kotlin.coroutines.Continuation p0)
public abstract java.lang.Object run(Api p0, kotlin.coroutines.Continuation p1)

public abstract java.util.concurrent.CompletableFuture runAsync()
public abstract java.util.concurrent.CompletableFuture runAsync(Api p0)

public abstract java.lang.Object runBlocking()
public abstract java.lang.Object runBlocking(Api p0)

public abstract java.lang.Object value(kotlin.coroutines.Continuation p0)

Expand All @@ -93,9 +101,11 @@ public abstract interface Foo : java/lang/Object {
public final class FooImpl : java/lang/Object, Foo {
public void <init>()

public java.util.concurrent.CompletableFuture runAsync()
public java.lang.Object run(Api api, kotlin.coroutines.Continuation $completion)

public java.util.concurrent.CompletableFuture runAsync(Api api)

public java.lang.Object runBlocking()
public java.lang.Object runBlocking(Api api)

public java.lang.Object value(kotlin.coroutines.Continuation $completion)

Expand Down
Loading

0 comments on commit 64686c1

Please sign in to comment.