1
1
package io.github.stslex.compiler_plugin.utils
2
2
3
3
import io.github.stslex.compiler_plugin.DistinctChangeCache
4
+ import io.github.stslex.compiler_plugin.GENERATED_FIELD_NAME
4
5
import org.jetbrains.kotlin.backend.common.extensions.IrPluginContext
5
6
import org.jetbrains.kotlin.backend.common.lower.DeclarationIrBuilder
6
7
import org.jetbrains.kotlin.backend.jvm.ir.fileParentOrNull
7
8
import org.jetbrains.kotlin.descriptors.DescriptorVisibilities
8
9
import org.jetbrains.kotlin.descriptors.Modality
10
+ import org.jetbrains.kotlin.ir.ObsoleteDescriptorBasedAPI
9
11
import org.jetbrains.kotlin.ir.builders.declarations.buildFun
10
12
import org.jetbrains.kotlin.ir.declarations.IrClass
11
13
import org.jetbrains.kotlin.ir.declarations.IrConstructor
@@ -116,7 +118,8 @@ internal fun IrPluginContext.buildSaveInCacheCall(
116
118
lambdaExpr : IrExpression ,
117
119
function : IrSimpleFunction ,
118
120
logger : CompileLogger ,
119
- backingField : IrFieldSymbolImpl
121
+ backingField : IrFieldSymbolImpl ,
122
+ qualifierArgs : IrExpression
120
123
): IrExpression {
121
124
logger.i(" buildSaveInCacheCall for ${function.name} , args: ${argsListExpr.dump()} " )
122
125
@@ -150,7 +153,7 @@ internal fun IrPluginContext.buildSaveInCacheCall(
150
153
type = function.returnType,
151
154
symbol = invokeFunSymbol.symbol,
152
155
typeArgumentsCount = 1 ,
153
- valueArgumentsCount = 3 ,
156
+ valueArgumentsCount = 4 ,
154
157
origin = null
155
158
)
156
159
.also { it.patchDeclarationParents(function.parent) }
@@ -160,21 +163,36 @@ internal fun IrPluginContext.buildSaveInCacheCall(
160
163
putTypeArgument(0 , function.returnType)
161
164
putValueArgument(0 , keyLiteral)
162
165
putValueArgument(1 , argsListExpr)
163
- putValueArgument(2 , lambdaExpr)
166
+ putValueArgument(2 , qualifierArgs)
167
+ putValueArgument(3 , lambdaExpr)
164
168
}
165
169
}
166
170
167
- @OptIn(UnsafeDuringIrConstructionAPI ::class )
171
+ @OptIn(UnsafeDuringIrConstructionAPI ::class , ObsoleteDescriptorBasedAPI :: class )
168
172
internal fun IrPluginContext.generateFields (
169
173
function : IrSimpleFunction ,
170
- qualifierArgs : IrExpression ,
171
174
logger : CompileLogger
172
175
): IrFieldSymbolImpl {
173
176
logger.i(" generateFields for ${function.name} parent: ${function.file} " )
174
177
175
178
val parentClass = function.parentClassOrNull
176
179
val parentFile = function.fileParentOrNull
177
180
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
+
178
196
val errorNotFound =
179
197
" function ${function.name} in ${function.file} couldn't be used with @DistinctUntilChangeFun"
180
198
@@ -194,7 +212,7 @@ internal fun IrPluginContext.generateFields(
194
212
endOffset = endOffset,
195
213
origin = IrDeclarationOrigin .PROPERTY_BACKING_FIELD ,
196
214
symbol = fieldSymbol,
197
- name = Name .identifier(" _distinctCache " ),
215
+ name = Name .identifier(GENERATED_FIELD_NAME ),
198
216
type = distinctChangeClass.defaultType,
199
217
visibility = DescriptorVisibilities .PRIVATE ,
200
218
isFinal = true ,
@@ -213,9 +231,6 @@ internal fun IrPluginContext.generateFields(
213
231
type = distinctChangeClass.defaultType,
214
232
constructorSymbol = constructorSymbol.symbol
215
233
)
216
- .apply {
217
- putValueArgument(0 , qualifierArgs)
218
- }
219
234
220
235
backingField.parent = function.parent
221
236
backingField.initializer = irFactory.createExpressionBody(callDistInit)
0 commit comments