Skip to content
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

K2 plugin #116

Merged
merged 5 commits into from
Aug 6, 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

# Ignore Gradle build output directory
build
.kotlin

# idea files
.idea/*
Expand Down
2 changes: 2 additions & 0 deletions compiler-plugin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ kotlin {

dependencies {
compileOnly(libs.kotlin.compiler.embeddable)
implementation(projects.compilerPluginK2)
implementation(projects.compilerPluginCommon)
}

configureMetaTasks("cleanTest", "test")
Expand Down
18 changes: 18 additions & 0 deletions compiler-plugin/compiler-plugin-common/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* Copyright 2023-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
*/

import org.jetbrains.kotlin.gradle.dsl.ExplicitApiMode

plugins {
alias(libs.plugins.conventions.jvm)
alias(libs.plugins.compiler.specific.module)
}

kotlin {
explicitApi = ExplicitApiMode.Disabled
}

dependencies {
compileOnly(libs.kotlin.compiler.embeddable)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright 2023-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
*/

package kotlinx.rpc.codegen.common

import org.jetbrains.kotlin.name.ClassId
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.name.Name

object ClassDeclarations {
val rpcInterface = ClassId(FqName("kotlinx.rpc"), Name.identifier("RPC"))

val serializableAnnotation = ClassId(FqName("kotlinx.serialization"), Name.identifier("Serializable"))
val contextualAnnotation = ClassId(FqName("kotlinx.serialization"), Name.identifier("Contextual"))

val flow = ClassId(FqName("kotlinx.coroutines.flow"), Name.identifier("Flow"))
val sharedFlow = ClassId(FqName("kotlinx.coroutines.flow"), Name.identifier("SharedFlow"))
val stateFlow = ClassId(FqName("kotlinx.coroutines.flow"), Name.identifier("StateFlow"))
}

object RpcNames {
val SERVICE_STUB_NAME: Name = Name.identifier("\$rpcServiceStub")

const val METHOD_CLASS_NAME_SUFFIX = "\$rpcMethod"
}

val Name.rpcMethodClassName: Name get() = Name.identifier("$identifier${RpcNames.METHOD_CLASS_NAME_SUFFIX}")
val Name.rpcMethodName: Name get() = Name.identifier(identifier.removeSuffix(RpcNames.METHOD_CLASS_NAME_SUFFIX))
24 changes: 24 additions & 0 deletions compiler-plugin/compiler-plugin-k2/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright 2023-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
*/

import org.jetbrains.kotlin.gradle.dsl.ExplicitApiMode

plugins {
alias(libs.plugins.conventions.jvm)
alias(libs.plugins.compiler.specific.module)
}

kotlin {
explicitApi = ExplicitApiMode.Disabled

compilerOptions {
freeCompilerArgs.add("-Xcontext-receivers")
}
}

dependencies {
compileOnly(libs.kotlin.compiler.embeddable)
compileOnly(libs.serialization.plugin)
implementation(projects.compilerPluginCommon)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright 2023-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
*/

package kotlinx.rpc.codegen

import org.jetbrains.kotlin.GeneratedDeclarationKey
import org.jetbrains.kotlin.fir.declarations.FirDeclarationOrigin
import org.jetbrains.kotlin.fir.symbols.FirBasedSymbol
import org.jetbrains.kotlin.fir.symbols.impl.FirClassSymbol
import org.jetbrains.kotlin.fir.symbols.impl.FirFunctionSymbol
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlinx.serialization.compiler.fir.SerializationPluginKey

internal class RPCGeneratedStubKey(
private val serviceName: Name,
val functions: List<FirFunctionSymbol<*>>,
) : GeneratedDeclarationKey() {
override fun toString(): String {
return "RPCGeneratedStubKey.$serviceName"
}
}

internal val FirBasedSymbol<*>.generatedRpcServiceStubKey: RPCGeneratedStubKey? get() =
(origin as? FirDeclarationOrigin.Plugin)?.key as? RPCGeneratedStubKey

internal class RPCGeneratedRpcMethodClassKey(
val rpcMethod: FirFunctionSymbol<*>,
) : GeneratedDeclarationKey() {
val isObject = rpcMethod.valueParameterSymbols.isEmpty()

override fun toString(): String {
return "RPCGeneratedRpcMethodClassKey.${rpcMethod.name}"
}
}

internal val FirBasedSymbol<*>.generatedRpcMethodClassKey: RPCGeneratedRpcMethodClassKey? get() =
(origin as? FirDeclarationOrigin.Plugin)?.key as? RPCGeneratedRpcMethodClassKey

internal object FirRpcServiceStubCompanionObject : GeneratedDeclarationKey() {
override fun toString(): String {
return "FirRpcServiceStubCompanionObject"
}
}

internal val FirClassSymbol<*>.isFromSerializationPlugin: Boolean get() {
return (origin as? FirDeclarationOrigin.Plugin)?.key is SerializationPluginKey
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* Copyright 2023-2024 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
*/

package kotlinx.rpc.codegen

import org.jetbrains.kotlin.cli.common.CLIConfigurationKeys
import org.jetbrains.kotlin.cli.common.messages.MessageCollector
import org.jetbrains.kotlin.config.CompilerConfiguration
import org.jetbrains.kotlin.fir.extensions.FirExtensionRegistrar
import org.jetbrains.kotlin.fir.extensions.FirDeclarationGenerationExtension.Factory as GFactory

class FirRPCExtensionRegistrar(private val configuration: CompilerConfiguration) : FirExtensionRegistrar() {
override fun ExtensionRegistrarContext.configurePlugin() {
val logger = configuration.get(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY, MessageCollector.NONE)

+GFactory { FirRPCServiceGenerator(it, logger) }
}
}
Loading