From 2e7720b00136094a67fd457bfc540fb3fb0c8c2b Mon Sep 17 00:00:00 2001 From: Eliezer Graber Date: Tue, 19 Nov 2024 19:59:56 -0500 Subject: [PATCH] Don't cache elements across rounds --- .../compiler/KmpComponentCreateGenerator.kt | 48 +++++++++++++------ .../inject/compiler/TypeResultResolver.kt | 2 +- .../inject/compiler/ksp/InjectProcessor.kt | 7 +-- .../compiler/ksp/ProcessKmpComponentCreate.kt | 29 +++++++++-- 4 files changed, 65 insertions(+), 21 deletions(-) diff --git a/kotlin-inject-compiler/core/src/main/kotlin/me/tatarka/inject/compiler/KmpComponentCreateGenerator.kt b/kotlin-inject-compiler/core/src/main/kotlin/me/tatarka/inject/compiler/KmpComponentCreateGenerator.kt index 1eba5f91..75ac91e2 100644 --- a/kotlin-inject-compiler/core/src/main/kotlin/me/tatarka/inject/compiler/KmpComponentCreateGenerator.kt +++ b/kotlin-inject-compiler/core/src/main/kotlin/me/tatarka/inject/compiler/KmpComponentCreateGenerator.kt @@ -1,20 +1,39 @@ package me.tatarka.inject.compiler +import com.squareup.kotlinpoet.AnnotationSpec +import com.squareup.kotlinpoet.ClassName import com.squareup.kotlinpoet.CodeBlock import com.squareup.kotlinpoet.FileSpec import com.squareup.kotlinpoet.FunSpec import com.squareup.kotlinpoet.KModifier -import me.tatarka.kotlin.ast.AstClass -import me.tatarka.kotlin.ast.AstFunction +import com.squareup.kotlinpoet.TypeName import me.tatarka.kotlin.ast.AstProvider +data class ComponentClassInfo( + val packageName: String, + val name: String, + val companionClassName: ClassName?, + val typeName: TypeName, + val className: ClassName, +) + +data class KmpComponentCreateFunctionInfo( + val name: String, + val annotations: List, + val visibility: KModifier, + val receiverParameterType: TypeName?, + val parameters: List>, + val parametersTemplate: String, + val parameterNames: List, +) + class KmpComponentCreateGenerator( private val provider: AstProvider, private val options: Options, ) { fun generate( - componentClass: AstClass, - kmpComponentCreateFunctions: List, + componentClass: ComponentClassInfo, + kmpComponentCreateFunctions: List, ) = with(provider) { FileSpec.builder( packageName = componentClass.packageName, @@ -24,36 +43,37 @@ class KmpComponentCreateGenerator( addFunction( FunSpec .builder(kmpComponentCreateFunction.name) - .addOriginatingElement(kmpComponentCreateFunction) +// .addOriginatingElement(kmpComponentCreateFunction) .apply { kmpComponentCreateFunction.annotations.forEach { annotation -> - addAnnotation(annotation.toAnnotationSpec()) + addAnnotation(annotation) } addModifiers( - kmpComponentCreateFunction.visibility.toKModifier(), + kmpComponentCreateFunction.visibility, KModifier.ACTUAL, ) - kmpComponentCreateFunction.receiverParameterType?.toTypeName()?.let(::receiver) + kmpComponentCreateFunction.receiverParameterType?.let(::receiver) for (param in kmpComponentCreateFunction.parameters) { - addParameter(param.name, param.type.toTypeName()) + val (name, typeName) = param + addParameter(name, typeName) } val funcParams = kmpComponentCreateFunction.parameters.joinToString { "%N" } - val funcParamsNames = kmpComponentCreateFunction.parameters.map { it.name }.toTypedArray() + val funcParamsNames = kmpComponentCreateFunction.parameterNames.toTypedArray() val returnTypeCompanion = when { - options.generateCompanionExtensions -> componentClass.companion?.type + options.generateCompanionExtensions -> componentClass.companionClassName else -> null } - val returnTypeName = componentClass.type.toTypeName() + val returnTypeName = componentClass.typeName val (createReceiver, createReceiverClassName) = when (returnTypeCompanion) { - null -> "%T::class" to componentClass.toClassName() - else -> "%T" to returnTypeCompanion.toAstClass().toClassName() + null -> "%T::class" to componentClass.className + else -> "%T" to returnTypeCompanion } addCode( CodeBlock.of( diff --git a/kotlin-inject-compiler/core/src/main/kotlin/me/tatarka/inject/compiler/TypeResultResolver.kt b/kotlin-inject-compiler/core/src/main/kotlin/me/tatarka/inject/compiler/TypeResultResolver.kt index 71ddced1..66b3e428 100644 --- a/kotlin-inject-compiler/core/src/main/kotlin/me/tatarka/inject/compiler/TypeResultResolver.kt +++ b/kotlin-inject-compiler/core/src/main/kotlin/me/tatarka/inject/compiler/TypeResultResolver.kt @@ -654,4 +654,4 @@ class TypeResultResolver(private val provider: AstProvider, private val options: // default values are present. val args: List>, ) -} \ No newline at end of file +} diff --git a/kotlin-inject-compiler/ksp/src/main/kotlin/me/tatarka/inject/compiler/ksp/InjectProcessor.kt b/kotlin-inject-compiler/ksp/src/main/kotlin/me/tatarka/inject/compiler/ksp/InjectProcessor.kt index 809be48b..d49b5ca2 100644 --- a/kotlin-inject-compiler/ksp/src/main/kotlin/me/tatarka/inject/compiler/ksp/InjectProcessor.kt +++ b/kotlin-inject-compiler/ksp/src/main/kotlin/me/tatarka/inject/compiler/ksp/InjectProcessor.kt @@ -11,12 +11,12 @@ import com.google.devtools.ksp.symbol.KSClassDeclaration import com.google.devtools.ksp.symbol.KSFunctionDeclaration import com.google.devtools.ksp.symbol.KSName import me.tatarka.inject.compiler.COMPONENT +import me.tatarka.inject.compiler.ComponentClassInfo import me.tatarka.inject.compiler.InjectGenerator import me.tatarka.inject.compiler.KMP_COMPONENT_CREATE +import me.tatarka.inject.compiler.KmpComponentCreateFunctionInfo import me.tatarka.inject.compiler.KmpComponentCreateGenerator import me.tatarka.inject.compiler.Options -import me.tatarka.kotlin.ast.AstClass -import me.tatarka.kotlin.ast.AstFunction import me.tatarka.kotlin.ast.KSAstProvider class InjectProcessor( @@ -32,7 +32,8 @@ class InjectProcessor( private var deferredClassNames: List = mutableListOf() private var deferredFunctionNames: List = mutableListOf() - private val kmpComponentCreateFunctionsByComponentType = mutableMapOf>() + private val kmpComponentCreateFunctionsByComponentType = + mutableMapOf>() override fun process(resolver: Resolver): List { lastResolver = resolver diff --git a/kotlin-inject-compiler/ksp/src/main/kotlin/me/tatarka/inject/compiler/ksp/ProcessKmpComponentCreate.kt b/kotlin-inject-compiler/ksp/src/main/kotlin/me/tatarka/inject/compiler/ksp/ProcessKmpComponentCreate.kt index 8c823fb7..a48d4975 100644 --- a/kotlin-inject-compiler/ksp/src/main/kotlin/me/tatarka/inject/compiler/ksp/ProcessKmpComponentCreate.kt +++ b/kotlin-inject-compiler/ksp/src/main/kotlin/me/tatarka/inject/compiler/ksp/ProcessKmpComponentCreate.kt @@ -4,15 +4,20 @@ import com.google.devtools.ksp.processing.CodeGenerator import com.google.devtools.ksp.symbol.KSFunctionDeclaration import com.squareup.kotlinpoet.ksp.writeTo import me.tatarka.inject.compiler.COMPONENT +import me.tatarka.inject.compiler.ComponentClassInfo +import me.tatarka.inject.compiler.KmpComponentCreateFunctionInfo import me.tatarka.inject.compiler.KmpComponentCreateGenerator import me.tatarka.kotlin.ast.AstClass import me.tatarka.kotlin.ast.AstFunction import me.tatarka.kotlin.ast.KSAstProvider +private typealias KmpComponentCreateFunctionsByComponentType = + MutableMap> + internal fun processKmpComponentCreate( element: KSFunctionDeclaration, provider: KSAstProvider, - kmpComponentCreateFunctionsByComponentType: MutableMap> + kmpComponentCreateFunctionsByComponentType: KmpComponentCreateFunctionsByComponentType ): Boolean = with(provider) { val astFunction = element.toAstFunction() val returnType = astFunction.returnType @@ -26,7 +31,25 @@ internal fun processKmpComponentCreate( val returnTypeClass = returnType.resolvedType().toAstClass() if (!astFunction.validateReturnType(returnTypeClass, provider)) return true - kmpComponentCreateFunctionsByComponentType.getOrPut(returnTypeClass, ::ArrayList).add(astFunction) + val returnTypeClassInfo = ComponentClassInfo( + packageName = returnTypeClass.packageName, + name = returnTypeClass.name, + companionClassName = returnTypeClass.companion?.type?.toAstClass()?.toClassName(), + typeName = returnTypeClass.type.toTypeName(), + className = returnTypeClass.toClassName(), + ) + + val functionInfo = KmpComponentCreateFunctionInfo( + name = astFunction.name, + annotations = astFunction.annotations.map { it.toAnnotationSpec() }.toList(), + visibility = astFunction.visibility.toKModifier(), + receiverParameterType = astFunction.receiverParameterType?.toTypeName(), + parameters = astFunction.parameters.map { it.name to it.type.toTypeName() }, + parametersTemplate = astFunction.parameters.joinToString { "%N" }, + parameterNames = astFunction.parameters.map { it.name }, + ) + + kmpComponentCreateFunctionsByComponentType.getOrPut(returnTypeClassInfo, ::ArrayList).add(functionInfo) true } @@ -34,7 +57,7 @@ internal fun processKmpComponentCreate( internal fun generateKmpComponentCreateFiles( codeGenerator: CodeGenerator, generator: KmpComponentCreateGenerator, - kmpComponentCreateFunctionsByComponentType: Map> + kmpComponentCreateFunctionsByComponentType: Map> ) { kmpComponentCreateFunctionsByComponentType.forEach { (componentType, kmpComponentCreateFunctions) -> val file = generator.generate(componentType, kmpComponentCreateFunctions)