|
| 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.checkers |
| 6 | + |
| 7 | +import kotlinx.rpc.codegen.FirCheckersContext |
| 8 | +import kotlinx.rpc.codegen.FirRpcPredicates |
| 9 | +import kotlinx.rpc.codegen.checkers.diagnostics.FirRpcStrictModeDiagnostics |
| 10 | +import kotlinx.rpc.codegen.common.RpcCallableId |
| 11 | +import kotlinx.rpc.codegen.common.RpcClassId |
| 12 | +import kotlinx.rpc.codegen.vsApi |
| 13 | +import org.jetbrains.kotlin.KtSourceElement |
| 14 | +import org.jetbrains.kotlin.diagnostics.DiagnosticReporter |
| 15 | +import org.jetbrains.kotlin.diagnostics.KtDiagnosticFactory0 |
| 16 | +import org.jetbrains.kotlin.diagnostics.reportOn |
| 17 | +import org.jetbrains.kotlin.fir.analysis.checkers.MppCheckerKind |
| 18 | +import org.jetbrains.kotlin.fir.analysis.checkers.context.CheckerContext |
| 19 | +import org.jetbrains.kotlin.fir.analysis.checkers.declaration.FirClassChecker |
| 20 | +import org.jetbrains.kotlin.fir.analysis.checkers.expression.FirFunctionCallChecker |
| 21 | +import org.jetbrains.kotlin.fir.declarations.FirClass |
| 22 | +import org.jetbrains.kotlin.fir.declarations.FirProperty |
| 23 | +import org.jetbrains.kotlin.fir.declarations.FirSimpleFunction |
| 24 | +import org.jetbrains.kotlin.fir.declarations.utils.isSuspend |
| 25 | +import org.jetbrains.kotlin.fir.expressions.FirFunctionCall |
| 26 | +import org.jetbrains.kotlin.fir.extensions.predicateBasedProvider |
| 27 | +import org.jetbrains.kotlin.fir.references.toResolvedCallableSymbol |
| 28 | +import org.jetbrains.kotlin.fir.symbols.impl.FirClassSymbol |
| 29 | +import org.jetbrains.kotlin.fir.types.coneType |
| 30 | +import org.jetbrains.kotlin.utils.memoryOptimizedMap |
| 31 | +import org.jetbrains.kotlin.utils.memoryOptimizedPlus |
| 32 | +import org.jetbrains.kotlinx.serialization.compiler.fir.services.FirSerializablePropertiesProvider |
| 33 | +import org.jetbrains.kotlinx.serialization.compiler.fir.services.serializablePropertiesProvider |
| 34 | + |
| 35 | +class FirRpcStrictModeExpressionChecker( |
| 36 | + private val ctx: FirCheckersContext, |
| 37 | +) : FirFunctionCallChecker(MppCheckerKind.Common) { |
| 38 | + private val streamScopeFunctions = setOf( |
| 39 | + RpcCallableId.StreamScope, |
| 40 | + RpcCallableId.streamScoped, |
| 41 | + RpcCallableId.withStreamScope, |
| 42 | + RpcCallableId.invokeOnStreamScopeCompletion, |
| 43 | + ) |
| 44 | + |
| 45 | + override fun check( |
| 46 | + expression: FirFunctionCall, |
| 47 | + context: CheckerContext, |
| 48 | + reporter: DiagnosticReporter, |
| 49 | + ) { |
| 50 | + expression.calleeReference.toResolvedCallableSymbol()?.let { symbol -> |
| 51 | + if (symbol.callableId in streamScopeFunctions) { |
| 52 | + ctx.strictModeDiagnostics.STREAM_SCOPE_FUNCTION_IN_RPC?.let { |
| 53 | + reporter.reportOn(expression.calleeReference.source, it, context) |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +class FirRpcStrictModeClassChecker(private val ctx: FirCheckersContext) : FirClassChecker(MppCheckerKind.Common) { |
| 61 | + override fun check( |
| 62 | + declaration: FirClass, |
| 63 | + context: CheckerContext, |
| 64 | + reporter: DiagnosticReporter, |
| 65 | + ) { |
| 66 | + if (!context.session.predicateBasedProvider.matches(FirRpcPredicates.rpc, declaration)) { |
| 67 | + return |
| 68 | + } |
| 69 | + |
| 70 | + val serializablePropertiesProvider = context.session.serializablePropertiesProvider |
| 71 | + declaration.declarations.forEach { declaration -> |
| 72 | + when (declaration) { |
| 73 | + is FirProperty -> { |
| 74 | + ctx.strictModeDiagnostics.FIELD_IN_RPC_SERVICE?.let { |
| 75 | + reporter.reportOn(declaration.source, it, context) |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + is FirSimpleFunction -> { |
| 80 | + checkFunction(declaration, context, reporter, serializablePropertiesProvider) |
| 81 | + } |
| 82 | + |
| 83 | + else -> {} |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + private fun checkFunction( |
| 89 | + function: FirSimpleFunction, |
| 90 | + context: CheckerContext, |
| 91 | + reporter: DiagnosticReporter, |
| 92 | + serializablePropertiesProvider: FirSerializablePropertiesProvider, |
| 93 | + ) { |
| 94 | + fun reportOn(element: KtSourceElement?, checker: FirRpcStrictModeDiagnostics.() -> KtDiagnosticFactory0?) { |
| 95 | + reporter.reportOn(element, ctx.strictModeDiagnostics.checker() ?: return, context) |
| 96 | + } |
| 97 | + |
| 98 | + val returnClassSymbol = vsApi { |
| 99 | + function.returnTypeRef.coneType.toClassSymbolVS(context.session) |
| 100 | + } |
| 101 | + |
| 102 | + val types = function.valueParameters.memoryOptimizedMap { parameter -> |
| 103 | + parameter.source to vsApi { |
| 104 | + parameter.returnTypeRef.coneType.toClassSymbolVS(context.session) |
| 105 | + } |
| 106 | + } memoryOptimizedPlus (function.returnTypeRef.source to returnClassSymbol) |
| 107 | + |
| 108 | + types.filter { (_, symbol) -> |
| 109 | + symbol != null |
| 110 | + }.forEach { (source, symbol) -> |
| 111 | + checkSerializableTypes<FirClassSymbol<*>>( |
| 112 | + context = context, |
| 113 | + clazz = symbol!!, |
| 114 | + serializablePropertiesProvider = serializablePropertiesProvider, |
| 115 | + ) { symbol, parents -> |
| 116 | + when (symbol.classId) { |
| 117 | + RpcClassId.stateFlow -> { |
| 118 | + reportOn(source) { STATE_FLOW_IN_RPC_SERVICE } |
| 119 | + } |
| 120 | + |
| 121 | + RpcClassId.sharedFlow -> { |
| 122 | + reportOn(source) { SHARED_FLOW_IN_RPC_SERVICE } |
| 123 | + } |
| 124 | + |
| 125 | + RpcClassId.flow -> { |
| 126 | + if (parents.any { it.classId == RpcClassId.flow }) { |
| 127 | + reportOn(source) { NESTED_STREAMING_IN_RPC_SERVICE } |
| 128 | + } else if (parents.isNotEmpty() && parents[0] == returnClassSymbol) { |
| 129 | + reportOn(source) { NON_TOP_LEVEL_SERVER_STREAMING_IN_RPC_SERVICE } |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + symbol |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + if (returnClassSymbol?.classId == RpcClassId.flow && function.isSuspend) { |
| 139 | + reportOn(function.source) { SUSPENDING_SERVER_STREAMING_IN_RPC_SERVICE } |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + private fun <ContextElement> checkSerializableTypes( |
| 144 | + context: CheckerContext, |
| 145 | + clazz: FirClassSymbol<*>, |
| 146 | + serializablePropertiesProvider: FirSerializablePropertiesProvider, |
| 147 | + parentContext: List<ContextElement> = emptyList(), |
| 148 | + checker: (FirClassSymbol<*>, List<ContextElement>) -> ContextElement?, |
| 149 | + ) { |
| 150 | + val newElement = checker(clazz, parentContext) |
| 151 | + val nextContext = if (newElement != null) { |
| 152 | + parentContext memoryOptimizedPlus newElement |
| 153 | + } else { |
| 154 | + parentContext |
| 155 | + } |
| 156 | + |
| 157 | + serializablePropertiesProvider.getSerializablePropertiesForClass(clazz) |
| 158 | + .serializableProperties |
| 159 | + .mapNotNull { property -> |
| 160 | + vsApi { |
| 161 | + property.propertySymbol.resolvedReturnType.toClassSymbolVS(context.session) |
| 162 | + } |
| 163 | + }.forEach { symbol -> |
| 164 | + checkSerializableTypes( |
| 165 | + context = context, |
| 166 | + clazz = symbol, |
| 167 | + serializablePropertiesProvider = serializablePropertiesProvider, |
| 168 | + parentContext = nextContext, |
| 169 | + checker = checker, |
| 170 | + ) |
| 171 | + } |
| 172 | + } |
| 173 | +} |
0 commit comments