11package io.github.stslex.compiler_plugin.utils
22
33import io.github.stslex.compiler_plugin.DistinctChangeCache
4+ import io.github.stslex.compiler_plugin.GENERATED_FIELD_NAME
45import org.jetbrains.kotlin.backend.common.extensions.IrPluginContext
56import org.jetbrains.kotlin.backend.common.lower.DeclarationIrBuilder
67import org.jetbrains.kotlin.backend.jvm.ir.fileParentOrNull
78import org.jetbrains.kotlin.descriptors.DescriptorVisibilities
89import org.jetbrains.kotlin.descriptors.Modality
10+ import org.jetbrains.kotlin.ir.ObsoleteDescriptorBasedAPI
911import org.jetbrains.kotlin.ir.builders.declarations.buildFun
1012import org.jetbrains.kotlin.ir.declarations.IrClass
1113import org.jetbrains.kotlin.ir.declarations.IrConstructor
@@ -116,7 +118,8 @@ internal fun IrPluginContext.buildSaveInCacheCall(
116118 lambdaExpr : IrExpression ,
117119 function : IrSimpleFunction ,
118120 logger : CompileLogger ,
119- backingField : IrFieldSymbolImpl
121+ backingField : IrFieldSymbolImpl ,
122+ qualifierArgs : IrExpression
120123): IrExpression {
121124 logger.i(" buildSaveInCacheCall for ${function.name} , args: ${argsListExpr.dump()} " )
122125
@@ -150,7 +153,7 @@ internal fun IrPluginContext.buildSaveInCacheCall(
150153 type = function.returnType,
151154 symbol = invokeFunSymbol.symbol,
152155 typeArgumentsCount = 1 ,
153- valueArgumentsCount = 3 ,
156+ valueArgumentsCount = 4 ,
154157 origin = null
155158 )
156159 .also { it.patchDeclarationParents(function.parent) }
@@ -160,21 +163,36 @@ internal fun IrPluginContext.buildSaveInCacheCall(
160163 putTypeArgument(0 , function.returnType)
161164 putValueArgument(0 , keyLiteral)
162165 putValueArgument(1 , argsListExpr)
163- putValueArgument(2 , lambdaExpr)
166+ putValueArgument(2 , qualifierArgs)
167+ putValueArgument(3 , lambdaExpr)
164168 }
165169}
166170
167- @OptIn(UnsafeDuringIrConstructionAPI ::class )
171+ @OptIn(UnsafeDuringIrConstructionAPI ::class , ObsoleteDescriptorBasedAPI :: class )
168172internal fun IrPluginContext.generateFields (
169173 function : IrSimpleFunction ,
170- qualifierArgs : IrExpression ,
171174 logger : CompileLogger
172175): IrFieldSymbolImpl {
173176 logger.i(" generateFields for ${function.name} parent: ${function.file} " )
174177
175178 val parentClass = function.parentClassOrNull
176179 val parentFile = function.fileParentOrNull
177180
181+ // check if parentClass or parentFile already contains _generatedField
182+ val createdField = when {
183+ parentClass != null -> parentClass.declarations.find {
184+ it.descriptor.name.identifierOrNullIfSpecial == GENERATED_FIELD_NAME
185+ }
186+
187+ parentFile != null -> parentFile.declarations.find {
188+ it.descriptor.name.identifierOrNullIfSpecial == GENERATED_FIELD_NAME
189+ }
190+
191+ else -> null
192+ }?.symbol as ? IrFieldSymbolImpl
193+
194+ if (createdField != null ) return createdField
195+
178196 val errorNotFound =
179197 " function ${function.name} in ${function.file} couldn't be used with @DistinctUntilChangeFun"
180198
@@ -194,7 +212,7 @@ internal fun IrPluginContext.generateFields(
194212 endOffset = endOffset,
195213 origin = IrDeclarationOrigin .PROPERTY_BACKING_FIELD ,
196214 symbol = fieldSymbol,
197- name = Name .identifier(" _distinctCache " ),
215+ name = Name .identifier(GENERATED_FIELD_NAME ),
198216 type = distinctChangeClass.defaultType,
199217 visibility = DescriptorVisibilities .PRIVATE ,
200218 isFinal = true ,
@@ -213,9 +231,6 @@ internal fun IrPluginContext.generateFields(
213231 type = distinctChangeClass.defaultType,
214232 constructorSymbol = constructorSymbol.symbol
215233 )
216- .apply {
217- putValueArgument(0 , qualifierArgs)
218- }
219234
220235 backingField.parent = function.parent
221236 backingField.initializer = irFactory.createExpressionBody(callDistInit)
0 commit comments