Skip to content

Commit 64686c1

Browse files
committed
Type Parameter!!!!😡
1 parent 1a9f59c commit 64686c1

File tree

8 files changed

+185
-43
lines changed

8 files changed

+185
-43
lines changed

buildSrc/src/main/kotlin/IProject.kt

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

1212
// Remember the libs.versions.toml!
13-
val ktVersion = "2.0.20"
13+
val ktVersion = "2.0.21"
1414
val pluginVersion = "0.9.4"
1515

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

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

Lines changed: 93 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import org.jetbrains.kotlin.fir.analysis.checkers.context.CheckerContext
1111
import org.jetbrains.kotlin.fir.analysis.checkers.context.MutableCheckerContext
1212
import org.jetbrains.kotlin.fir.analysis.checkers.getContainingClassSymbol
1313
import org.jetbrains.kotlin.fir.analysis.checkers.processOverriddenFunctions
14-
import org.jetbrains.kotlin.fir.builder.buildFunctionTypeParameter
1514
import org.jetbrains.kotlin.fir.caches.FirCache
1615
import org.jetbrains.kotlin.fir.caches.firCachesFactory
1716
import org.jetbrains.kotlin.fir.caches.getValue
@@ -36,14 +35,14 @@ import org.jetbrains.kotlin.fir.resolve.SessionHolderImpl
3635
import org.jetbrains.kotlin.fir.resolve.getSuperTypes
3736
import org.jetbrains.kotlin.fir.resolve.transformers.ReturnTypeCalculatorForFullBodyResolve
3837
import org.jetbrains.kotlin.fir.scopes.impl.FirClassDeclaredMemberScope
38+
import org.jetbrains.kotlin.fir.scopes.impl.toConeType
3939
import org.jetbrains.kotlin.fir.scopes.processAllFunctions
4040
import org.jetbrains.kotlin.fir.symbols.FirBasedSymbol
4141
import org.jetbrains.kotlin.fir.symbols.SymbolInternals
4242
import org.jetbrains.kotlin.fir.symbols.impl.*
4343
import org.jetbrains.kotlin.fir.types.*
4444
import org.jetbrains.kotlin.fir.types.builder.buildErrorTypeRef
4545
import org.jetbrains.kotlin.fir.types.builder.buildResolvedTypeRef
46-
import org.jetbrains.kotlin.ir.builders.declarations.buildTypeParameter
4746
import org.jetbrains.kotlin.name.CallableId
4847
import org.jetbrains.kotlin.name.ClassId
4948
import org.jetbrains.kotlin.name.FqName
@@ -150,14 +149,104 @@ class SuspendTransformFirTransformer(
150149
// In the generated IR, data and dataBlocking will share an `A`, generating the error.
151150
// The error: Duplicate IR node
152151
// [IR VALIDATION] JvmIrValidationBeforeLoweringPhase: Duplicate IR node: TYPE_PARAMETER name:A index:0 variance: superTypes:[kotlin.Any?] reified:false of FUN GENERATED[...]
153-
// TODO onebot type parameters?
152+
// TODO copy to value parameters, receiver and return type?
153+
val originalTypeParameterCache = mutableMapOf<FirTypeParameter, FirTypeParameter>()
154154
typeParameters.replaceAll {
155155
buildTypeParameterCopy(it) {
156156
containingDeclarationSymbol = newFunSymbol // it.containingDeclarationSymbol
157-
symbol = it.symbol // FirTypeParameterSymbol()
157+
// symbol = it.symbol // FirTypeParameterSymbol()
158+
symbol = FirTypeParameterSymbol()
159+
}.also { new ->
160+
originalTypeParameterCache[it] = new
161+
158162
}
159163
}
160164

165+
// TODO
166+
valueParameters.replaceAll { vp ->
167+
buildValueParameterCopy(vp) {
168+
symbol = FirValueParameterSymbol(vp.symbol.name)
169+
170+
val cachedTypeParameter = originalTypeParameterCache.entries.find { (k, v) ->
171+
k.toConeType() == vp.returnTypeRef.coneTypeOrNull
172+
}
173+
val newReturnTypeRef = if (cachedTypeParameter != null) {
174+
returnTypeRef.withReplacedConeType(cachedTypeParameter.value.toConeType())
175+
} else {
176+
println("returnTypeRef: $returnTypeRef")
177+
returnTypeRef.coneType.typeArguments.forEach {
178+
println("returnTypeRef.coneType.typeArguments: $it")
179+
}
180+
181+
returnTypeRef
182+
}
183+
184+
returnTypeRef = newReturnTypeRef
185+
}
186+
}
187+
188+
// valueParameters.replaceAll { vp ->
189+
// buildValueParameterCopy(vp) {
190+
// //println("find: ${originalTypeParameterCache[vp.returnTypeRef]}")
191+
// val cachedTypeParameter = originalTypeParameterCache.entries.find { (k, v) ->
192+
// k.toConeType() == vp.returnTypeRef.coneTypeOrNull
193+
// }
194+
// println("cache conetype: $cachedTypeParameter")
195+
//
196+
// println("returnTypeRef1: $returnTypeRef")
197+
//
198+
// val stack = ArrayDeque<Any>()
199+
//
200+
// fun resolveTypeCopy() {
201+
//
202+
// }
203+
//
204+
// val type = returnTypeRef.coneTypeOrNull
205+
// if (type != null && type.typeArguments.isNotEmpty()) {
206+
// for (subArguments in type.typeArguments) {
207+
//
208+
// }
209+
// }
210+
//
211+
// returnTypeRef.accept(object : FirVisitorVoid() {
212+
// override fun visitElement(element: FirElement) {
213+
// println("visitElement($element)")
214+
// element.acceptChildren(this)
215+
// }
216+
//
217+
// override fun visitResolvedTypeRef(resolvedTypeRef: FirResolvedTypeRef) {
218+
// println("visitResolvedTypeRef(${resolvedTypeRef})")
219+
// resolvedTypeRef.type.typeArguments.forEach {
220+
// it.type?.typeArguments
221+
// }
222+
// super.visitResolvedTypeRef(resolvedTypeRef)
223+
// }
224+
//
225+
// override fun visitValueParameter(valueParameter: FirValueParameter) {
226+
// println("visitValueParameter($valueParameter)")
227+
// super.visitValueParameter(valueParameter)
228+
// }
229+
//
230+
// override fun visitTypeParameter(typeParameter: FirTypeParameter) {
231+
// println("visitTypeParameter($typeParameter)")
232+
// super.visitTypeParameter(typeParameter)
233+
// }
234+
//
235+
// override fun visitTypeParameterRef(typeParameterRef: FirTypeParameterRef) {
236+
// println("visitTypeParameterRef($typeParameterRef)")
237+
// super.visitTypeParameterRef(typeParameterRef)
238+
// }
239+
// })
240+
//
241+
// if (cachedTypeParameter != null) {
242+
// returnTypeRef = returnTypeRef.withReplacedConeType(cachedTypeParameter.value.toConeType())
243+
// }
244+
//
245+
// println("returnTypeRef2: $returnTypeRef")
246+
// symbol = FirValueParameterSymbol(vp.symbol.name)
247+
// }
248+
// }
249+
161250
// valueParameters.replaceAll { vp ->
162251
// buildValueParameterCopy(vp) {
163252
// containingFunctionSymbol = newFunSymbol
@@ -185,7 +274,6 @@ class SuspendTransformFirTransformer(
185274
).origin
186275
}
187276

188-
189277
funList.add(newFun.symbol)
190278
}
191279
}

compiler/suspend-transform-plugin/src/testData/codegen/typeAttr.asm.txt

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,29 @@
1+
public final class Api : java/lang/Object {
2+
public void <init>()
3+
}
4+
15
public class Bar : java/lang/Object {
26
public void <init>()
37
}
48

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

8-
public static java.lang.Object runBlocking(Foo $this)
12+
public static java.lang.Object runBlocking(Foo $this, Api api)
913

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

1216
public static Bar valueBlocking(Foo $this)
1317
}
1418

1519
final class Foo$runAsync$1 : kotlin/coroutines/jvm/internal/SuspendLambda, kotlin/jvm/functions/Function1 {
20+
final Api $api
21+
1622
int label
1723

1824
final Foo this$0
1925

20-
void <init>(Foo $receiver, kotlin.coroutines.Continuation $completion)
26+
void <init>(Foo $receiver, Api $api, kotlin.coroutines.Continuation $completion)
2127

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

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

3137
final class Foo$runBlocking$1 : kotlin/coroutines/jvm/internal/SuspendLambda, kotlin/jvm/functions/Function1 {
38+
final Api $api
39+
3240
int label
3341

3442
final Foo this$0
3543

36-
void <init>(Foo $receiver, kotlin.coroutines.Continuation $completion)
44+
void <init>(Foo $receiver, Api $api, kotlin.coroutines.Continuation $completion)
3745

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

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

7987
public abstract interface Foo : java/lang/Object {
80-
public abstract java.lang.Object run(kotlin.coroutines.Continuation p0)
88+
public abstract java.lang.Object run(Api p0, kotlin.coroutines.Continuation p1)
8189

82-
public abstract java.util.concurrent.CompletableFuture runAsync()
90+
public abstract java.util.concurrent.CompletableFuture runAsync(Api p0)
8391

84-
public abstract java.lang.Object runBlocking()
92+
public abstract java.lang.Object runBlocking(Api p0)
8593

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

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

96-
public java.util.concurrent.CompletableFuture runAsync()
104+
public java.lang.Object run(Api api, kotlin.coroutines.Continuation $completion)
105+
106+
public java.util.concurrent.CompletableFuture runAsync(Api api)
97107

98-
public java.lang.Object runBlocking()
108+
public java.lang.Object runBlocking(Api api)
99109

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

0 commit comments

Comments
 (0)