Skip to content

Commit 3bd8857

Browse files
committed
K2 plugin (#116)
* Introduce K2 plugin * Update IR backend * Update to 2.0 * Fix for KT-70132
1 parent ae61059 commit 3bd8857

File tree

55 files changed

+1192
-174
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+1192
-174
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
# Ignore Gradle build output directory
55
build
6+
.kotlin
67

78
# idea files
89
.idea/*

compiler-plugin/build.gradle.kts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ kotlin {
2424

2525
dependencies {
2626
compileOnly(libs.kotlin.compiler.embeddable)
27+
implementation(projects.compilerPluginK2)
28+
implementation(projects.compilerPluginCommon)
2729
}
2830

2931
configureMetaTasks("cleanTest", "test")
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
* Copyright 2023-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
3+
*/
4+
5+
import org.jetbrains.kotlin.gradle.dsl.ExplicitApiMode
6+
7+
plugins {
8+
alias(libs.plugins.conventions.jvm)
9+
alias(libs.plugins.compiler.specific.module)
10+
}
11+
12+
kotlin {
13+
explicitApi = ExplicitApiMode.Disabled
14+
}
15+
16+
dependencies {
17+
compileOnly(libs.kotlin.compiler.embeddable)
18+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright 2023-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
3+
*/
4+
5+
package kotlinx.rpc.codegen.common
6+
7+
import org.jetbrains.kotlin.name.ClassId
8+
import org.jetbrains.kotlin.name.FqName
9+
import org.jetbrains.kotlin.name.Name
10+
11+
object ClassDeclarations {
12+
val rpcInterface = ClassId(FqName("kotlinx.rpc"), Name.identifier("RPC"))
13+
14+
val serializableAnnotation = ClassId(FqName("kotlinx.serialization"), Name.identifier("Serializable"))
15+
val contextualAnnotation = ClassId(FqName("kotlinx.serialization"), Name.identifier("Contextual"))
16+
17+
val flow = ClassId(FqName("kotlinx.coroutines.flow"), Name.identifier("Flow"))
18+
val sharedFlow = ClassId(FqName("kotlinx.coroutines.flow"), Name.identifier("SharedFlow"))
19+
val stateFlow = ClassId(FqName("kotlinx.coroutines.flow"), Name.identifier("StateFlow"))
20+
}
21+
22+
object RpcNames {
23+
val SERVICE_STUB_NAME: Name = Name.identifier("\$rpcServiceStub")
24+
25+
const val METHOD_CLASS_NAME_SUFFIX = "\$rpcMethod"
26+
}
27+
28+
val Name.rpcMethodClassName: Name get() = Name.identifier("$identifier${RpcNames.METHOD_CLASS_NAME_SUFFIX}")
29+
val Name.rpcMethodName: Name get() = Name.identifier(identifier.removeSuffix(RpcNames.METHOD_CLASS_NAME_SUFFIX))
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright 2023-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
3+
*/
4+
5+
import org.jetbrains.kotlin.gradle.dsl.ExplicitApiMode
6+
7+
plugins {
8+
alias(libs.plugins.conventions.jvm)
9+
alias(libs.plugins.compiler.specific.module)
10+
}
11+
12+
kotlin {
13+
explicitApi = ExplicitApiMode.Disabled
14+
15+
compilerOptions {
16+
freeCompilerArgs.add("-Xcontext-receivers")
17+
}
18+
}
19+
20+
dependencies {
21+
compileOnly(libs.kotlin.compiler.embeddable)
22+
compileOnly(libs.serialization.plugin)
23+
implementation(projects.compilerPluginCommon)
24+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright 2023-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
3+
*/
4+
5+
package kotlinx.rpc.codegen
6+
7+
import org.jetbrains.kotlin.GeneratedDeclarationKey
8+
import org.jetbrains.kotlin.fir.declarations.FirDeclarationOrigin
9+
import org.jetbrains.kotlin.fir.symbols.FirBasedSymbol
10+
import org.jetbrains.kotlin.fir.symbols.impl.FirClassSymbol
11+
import org.jetbrains.kotlin.fir.symbols.impl.FirFunctionSymbol
12+
import org.jetbrains.kotlin.name.Name
13+
import org.jetbrains.kotlinx.serialization.compiler.fir.SerializationPluginKey
14+
15+
internal class RPCGeneratedStubKey(
16+
private val serviceName: Name,
17+
val functions: List<FirFunctionSymbol<*>>,
18+
) : GeneratedDeclarationKey() {
19+
override fun toString(): String {
20+
return "RPCGeneratedStubKey.$serviceName"
21+
}
22+
}
23+
24+
internal val FirBasedSymbol<*>.generatedRpcServiceStubKey: RPCGeneratedStubKey? get() =
25+
(origin as? FirDeclarationOrigin.Plugin)?.key as? RPCGeneratedStubKey
26+
27+
internal class RPCGeneratedRpcMethodClassKey(
28+
val rpcMethod: FirFunctionSymbol<*>,
29+
) : GeneratedDeclarationKey() {
30+
val isObject = rpcMethod.valueParameterSymbols.isEmpty()
31+
32+
override fun toString(): String {
33+
return "RPCGeneratedRpcMethodClassKey.${rpcMethod.name}"
34+
}
35+
}
36+
37+
internal val FirBasedSymbol<*>.generatedRpcMethodClassKey: RPCGeneratedRpcMethodClassKey? get() =
38+
(origin as? FirDeclarationOrigin.Plugin)?.key as? RPCGeneratedRpcMethodClassKey
39+
40+
internal object FirRpcServiceStubCompanionObject : GeneratedDeclarationKey() {
41+
override fun toString(): String {
42+
return "FirRpcServiceStubCompanionObject"
43+
}
44+
}
45+
46+
internal val FirClassSymbol<*>.isFromSerializationPlugin: Boolean get() {
47+
return (origin as? FirDeclarationOrigin.Plugin)?.key is SerializationPluginKey
48+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* Copyright 2023-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
3+
*/
4+
5+
package kotlinx.rpc.codegen
6+
7+
import org.jetbrains.kotlin.cli.common.CLIConfigurationKeys
8+
import org.jetbrains.kotlin.cli.common.messages.MessageCollector
9+
import org.jetbrains.kotlin.config.CompilerConfiguration
10+
import org.jetbrains.kotlin.fir.extensions.FirExtensionRegistrar
11+
import org.jetbrains.kotlin.fir.extensions.FirDeclarationGenerationExtension.Factory as GFactory
12+
13+
class FirRPCExtensionRegistrar(private val configuration: CompilerConfiguration) : FirExtensionRegistrar() {
14+
override fun ExtensionRegistrarContext.configurePlugin() {
15+
val logger = configuration.get(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY, MessageCollector.NONE)
16+
17+
+GFactory { FirRPCServiceGenerator(it, logger) }
18+
}
19+
}

0 commit comments

Comments
 (0)