Skip to content

Commit f8b8fe6

Browse files
committed
Rewriting the struct package
1 parent d4f54d7 commit f8b8fe6

12 files changed

+271
-312
lines changed

compiler/src/main/kotlin/land/sungbin/composeinvestigator/compiler/lower/ComposeInvestigatorBaseLower.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ import land.sungbin.composeinvestigator.compiler.Stability_RUNTIME
2121
import land.sungbin.composeinvestigator.compiler.Stability_UNKNOWN
2222
import land.sungbin.composeinvestigator.compiler.fromFqName
2323
import land.sungbin.composeinvestigator.compiler.struct.IrInvalidationLogger
24-
import land.sungbin.composeinvestigator.compiler.struct.IrInvalidationTraceTable
25-
import land.sungbin.composeinvestigator.compiler.struct.IrInvalidationTraceTableHolder
24+
import land.sungbin.composeinvestigator.compiler.struct.IrComposeInvestigator
25+
import land.sungbin.composeinvestigator.compiler.struct.IrComposeInvestigatorHolder
2626
import land.sungbin.composeinvestigator.compiler.struct.IrValueArgument
2727
import land.sungbin.composeinvestigator.compiler.struct.get
2828
import org.jetbrains.kotlin.backend.common.IrElementTransformerVoidWithContext
@@ -93,7 +93,7 @@ import org.jetbrains.kotlin.types.Variance
9393
public open class ComposeInvestigatorBaseLower(
9494
protected val context: IrPluginContext,
9595
protected val messageCollector: MessageCollector, // TODO context.createDiagnosticReporter() (Blocked: "This API is not supported for K2")
96-
protected val tables: IrInvalidationTraceTableHolder,
96+
protected val tables: IrComposeInvestigatorHolder,
9797
) : IrElementTransformerVoidWithContext() {
9898
private var ownStabilitySymbol: IrClassSymbol? = null
9999
private var ownStabilityCertainSymbol: IrClassSymbol? = null
@@ -217,21 +217,21 @@ public open class ComposeInvestigatorBaseLower(
217217
protected open fun firstTransformComposableBody(
218218
composable: IrSimpleFunction,
219219
body: IrBody,
220-
table: IrInvalidationTraceTable,
220+
table: IrComposeInvestigator,
221221
): IrBody = body
222222

223223
// (MUST) LoadingOrder.FIRST
224224
protected open fun firstTransformStateInitializer(
225225
name: Name,
226226
initializer: IrExpression,
227-
table: IrInvalidationTraceTable,
227+
table: IrComposeInvestigator,
228228
): IrExpression = initializer
229229

230230
// (MUST) LoadingOrder.LAST
231231
protected open fun lastTransformSkipToGroupEndCall(
232232
composable: IrSimpleFunction,
233233
expression: IrCall,
234-
table: IrInvalidationTraceTable,
234+
table: IrComposeInvestigator,
235235
): IrExpression = expression
236236

237237
private fun IrVariable.isStateDeclaration(): Boolean {

compiler/src/main/kotlin/land/sungbin/composeinvestigator/compiler/lower/InvalidationProcessTracingFirstTransformer.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import land.sungbin.composeinvestigator.compiler.analysis.DurationWritableSlices
1010
import land.sungbin.composeinvestigator.compiler.fromFqName
1111
import land.sungbin.composeinvestigator.compiler.log
1212
import land.sungbin.composeinvestigator.compiler.struct.IrComposableInformation
13-
import land.sungbin.composeinvestigator.compiler.struct.IrInvalidationTraceTable
14-
import land.sungbin.composeinvestigator.compiler.struct.IrInvalidationTraceTableHolder
13+
import land.sungbin.composeinvestigator.compiler.struct.IrComposeInvestigator
14+
import land.sungbin.composeinvestigator.compiler.struct.IrComposeInvestigatorHolder
1515
import org.jetbrains.kotlin.backend.common.extensions.IrPluginContext
1616
import org.jetbrains.kotlin.backend.common.getCompilerMessageLocation
1717
import org.jetbrains.kotlin.cli.common.messages.MessageCollector
@@ -82,7 +82,7 @@ import org.jetbrains.kotlin.name.SpecialNames
8282
public class InvalidationProcessTracingFirstTransformer(
8383
context: IrPluginContext,
8484
messageCollector: MessageCollector,
85-
tables: IrInvalidationTraceTableHolder,
85+
tables: IrComposeInvestigatorHolder,
8686
private val stabilityInferencer: StabilityInferencer,
8787
) : ComposeInvestigatorBaseLower(context, messageCollector, tables) {
8888
private val currentComposerSymbol: IrPropertySymbol =
@@ -92,7 +92,7 @@ public class InvalidationProcessTracingFirstTransformer(
9292
override fun firstTransformComposableBody(
9393
composable: IrSimpleFunction,
9494
body: IrBody,
95-
table: IrInvalidationTraceTable,
95+
table: IrComposeInvestigator,
9696
): IrBody {
9797
messageCollector.log(
9898
"Visit composable body: ${composable.name}",
@@ -183,7 +183,7 @@ public class InvalidationProcessTracingFirstTransformer(
183183
newStatements += invalidationReasonVariable
184184

185185
val invalidationResultProcessed = irGetValue(invalidationReasonVariable)
186-
.apply { type = invalidationLogger.irInvalidationResultSymbol.defaultType }
186+
.apply { type = invalidationLogger.invalidationResultSymbol.defaultType }
187187
val logger = invalidationLogger.irLog(affectedComposable, result = invalidationResultProcessed)
188188

189189
newStatements += logger

compiler/src/main/kotlin/land/sungbin/composeinvestigator/compiler/lower/InvalidationSkipTracingLastTransformer.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import land.sungbin.composeinvestigator.compiler.COMPOSER_FQN
77
import land.sungbin.composeinvestigator.compiler.analysis.DurationWritableSlices
88
import land.sungbin.composeinvestigator.compiler.log
99
import land.sungbin.composeinvestigator.compiler.struct.IrComposableInformation
10-
import land.sungbin.composeinvestigator.compiler.struct.IrInvalidationTraceTable
11-
import land.sungbin.composeinvestigator.compiler.struct.IrInvalidationTraceTableHolder
10+
import land.sungbin.composeinvestigator.compiler.struct.IrComposeInvestigator
11+
import land.sungbin.composeinvestigator.compiler.struct.IrComposeInvestigatorHolder
1212
import org.jetbrains.kotlin.backend.common.extensions.IrPluginContext
1313
import org.jetbrains.kotlin.backend.common.getCompilerMessageLocation
1414
import org.jetbrains.kotlin.cli.common.messages.MessageCollector
@@ -57,12 +57,12 @@ import org.jetbrains.kotlin.ir.util.file
5757
public class InvalidationSkipTracingLastTransformer(
5858
context: IrPluginContext,
5959
messageCollector: MessageCollector,
60-
table: IrInvalidationTraceTableHolder,
60+
table: IrComposeInvestigatorHolder,
6161
) : ComposeInvestigatorBaseLower(context, messageCollector, table) {
6262
override fun lastTransformSkipToGroupEndCall(
6363
composable: IrSimpleFunction,
6464
expression: IrCall,
65-
table: IrInvalidationTraceTable,
65+
table: IrComposeInvestigator,
6666
): IrExpression {
6767
messageCollector.log(
6868
"Visit skipToGroupEnd call: ${composable.name}",
@@ -92,7 +92,7 @@ public class InvalidationSkipTracingLastTransformer(
9292
}
9393

9494
val invalidationResultSkipped = invalidationLogger.irInvalidationResultSkipped()
95-
.apply { type = invalidationLogger.irInvalidationResultSymbol.defaultType }
95+
.apply { type = invalidationLogger.invalidationResultSymbol.defaultType }
9696
val logger = invalidationLogger.irLog(affectedComposable, result = invalidationResultSkipped)
9797

9898
return IrBlockImpl(

compiler/src/main/kotlin/land/sungbin/composeinvestigator/compiler/lower/InvalidationTraceTableInstantiateTransformer.kt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ package land.sungbin.composeinvestigator.compiler.lower
55
import land.sungbin.composeinvestigator.compiler.COMPOSABLE_INVALIDATION_TRACE_TABLE_FQN
66
import land.sungbin.composeinvestigator.compiler.NO_INVESTIGATION_FQN
77
import land.sungbin.composeinvestigator.compiler.log
8-
import land.sungbin.composeinvestigator.compiler.struct.IrInvalidationTraceTable
9-
import land.sungbin.composeinvestigator.compiler.struct.IrInvalidationTraceTableHolder
8+
import land.sungbin.composeinvestigator.compiler.struct.IrComposeInvestigator
9+
import land.sungbin.composeinvestigator.compiler.struct.IrComposeInvestigatorHolder
1010
import org.jetbrains.kotlin.backend.common.extensions.IrPluginContext
1111
import org.jetbrains.kotlin.backend.jvm.ir.getKtFile
1212
import org.jetbrains.kotlin.cli.common.messages.MessageCollector
@@ -45,10 +45,10 @@ import org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid
4545
public class InvalidationTraceTableInstantiateTransformer(
4646
private val context: IrPluginContext,
4747
private val messageCollector: MessageCollector, // TODO context.createDiagnosticReporter() (Blocked: "This API is not supported for K2")
48-
) : IrElementTransformerVoid(), IrInvalidationTraceTableHolder {
49-
private val tables = mutableMapOf<IrFile, IrInvalidationTraceTable>()
48+
) : IrElementTransformerVoid(), IrComposeInvestigatorHolder {
49+
private val tables = mutableMapOf<IrFile, IrComposeInvestigator>()
5050

51-
override fun tableByFile(file: IrFile): IrInvalidationTraceTable =
51+
override fun getByFile(file: IrFile): IrComposeInvestigator =
5252
tables[file] ?: throw CompilationException(
5353
"No table for ${file.name}",
5454
/* cause = */ null,
@@ -69,8 +69,8 @@ public class InvalidationTraceTableInstantiateTransformer(
6969
val existsTable = declaration.findDeclaration<IrProperty> { property ->
7070
property.backingField?.type?.classFqName == COMPOSABLE_INVALIDATION_TRACE_TABLE_FQN
7171
}
72-
val table = existsTable?.let(IrInvalidationTraceTable::from) ?: run {
73-
IrInvalidationTraceTable.create(context, declaration).also { table ->
72+
val table = existsTable?.let(IrComposeInvestigator::from) ?: run {
73+
IrComposeInvestigator.create(context, declaration).also { table ->
7474
declaration.declarations.add(0, table.rawProp.also { prop -> prop.setDeclarationsParent(declaration) })
7575
}
7676
}

compiler/src/main/kotlin/land/sungbin/composeinvestigator/compiler/lower/InvalidationTraceTableIntrinsicCallTransformer.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import land.sungbin.composeinvestigator.compiler.analysis.DurationWritableSlices
1313
import land.sungbin.composeinvestigator.compiler.analysis.set
1414
import land.sungbin.composeinvestigator.compiler.fromFqName
1515
import land.sungbin.composeinvestigator.compiler.struct.IrComposableInformation
16-
import land.sungbin.composeinvestigator.compiler.struct.IrInvalidationTraceTableHolder
16+
import land.sungbin.composeinvestigator.compiler.struct.IrComposeInvestigatorHolder
1717
import land.sungbin.composeinvestigator.compiler.struct.get
1818
import org.jetbrains.kotlin.backend.common.IrElementTransformerVoidWithContext
1919
import org.jetbrains.kotlin.backend.common.extensions.IrPluginContext
@@ -43,7 +43,7 @@ import org.jetbrains.kotlin.utils.addToStdlib.cast
4343
public class InvalidationTraceTableIntrinsicCallTransformer(
4444
private val context: IrPluginContext,
4545
private val irComposableInformation: IrComposableInformation,
46-
private val tables: IrInvalidationTraceTableHolder,
46+
private val tables: IrComposeInvestigatorHolder,
4747
) : IrElementTransformerVoidWithContext() {
4848
private val tableSymbol = context.referenceClass(ClassId.topLevel(COMPOSABLE_INVALIDATION_TRACE_TABLE_FQN))!!
4949
private val composableNameSymbol = context.referenceClass(ClassId.topLevel(COMPOSABLE_NAME_FQN))!!.owner

compiler/src/main/kotlin/land/sungbin/composeinvestigator/compiler/lower/StateInitializerFirstTransformer.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
package land.sungbin.composeinvestigator.compiler.lower
44

55
import land.sungbin.composeinvestigator.compiler.log
6-
import land.sungbin.composeinvestigator.compiler.struct.IrInvalidationTraceTable
7-
import land.sungbin.composeinvestigator.compiler.struct.IrInvalidationTraceTableHolder
6+
import land.sungbin.composeinvestigator.compiler.struct.IrComposeInvestigator
7+
import land.sungbin.composeinvestigator.compiler.struct.IrComposeInvestigatorHolder
88
import org.jetbrains.kotlin.backend.common.extensions.IrPluginContext
99
import org.jetbrains.kotlin.backend.common.getCompilerMessageLocation
1010
import org.jetbrains.kotlin.cli.common.messages.MessageCollector
@@ -40,21 +40,21 @@ import org.jetbrains.kotlin.name.Name
4040
public class StateInitializerFirstTransformer(
4141
context: IrPluginContext,
4242
messageCollector: MessageCollector,
43-
tables: IrInvalidationTraceTableHolder,
43+
tables: IrComposeInvestigatorHolder,
4444
) : ComposeInvestigatorBaseLower(context, messageCollector, tables) {
4545
// TODO Special behaviour of 'remember' and 'rememberSaveable':
4646
// Transforms should be performed inside the 'remember[Saveable]' lambda, not outside of it.
4747
override fun firstTransformStateInitializer(
4848
name: Name,
4949
initializer: IrExpression,
50-
table: IrInvalidationTraceTable,
50+
table: IrComposeInvestigator,
5151
): IrExpression {
5252
messageCollector.log(
5353
"Visit state initializer: ${name.asString()}",
5454
initializer.getCompilerMessageLocation(table.rawProp.file),
5555
)
5656

57-
return table.irRegisterStateObject(initializer, irString(name.asString())).also {
57+
return table.callRegisterStateObject(initializer, irString(name.asString())).also {
5858
messageCollector.log(
5959
"Transform state initializer succeed: ${name.asString()}",
6060
initializer.getCompilerMessageLocation(table.rawProp.file),

compiler/src/main/kotlin/land/sungbin/composeinvestigator/compiler/struct/IrComposableInformation.kt

Lines changed: 14 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -2,68 +2,33 @@
22
// SPDX-License-Identifier: Apache-2.0
33
package land.sungbin.composeinvestigator.compiler.struct
44

5-
import java.lang.ref.WeakReference
6-
import land.sungbin.composeinvestigator.compiler.COMPOSABLE_INFORMATION_FQN
7-
import land.sungbin.composeinvestigator.compiler.ComposableInformation_WITH_COMPOUND_KEY
8-
import land.sungbin.composeinvestigator.compiler.fromFqName
5+
import land.sungbin.composeinvestigator.compiler.InvestigatorNames
96
import org.jetbrains.kotlin.backend.common.extensions.IrPluginContext
107
import org.jetbrains.kotlin.ir.expressions.IrConst
11-
import org.jetbrains.kotlin.ir.expressions.IrConstructorCall
8+
import org.jetbrains.kotlin.ir.expressions.IrExpression
129
import org.jetbrains.kotlin.ir.expressions.impl.IrConstructorCallImpl
1310
import org.jetbrains.kotlin.ir.expressions.impl.fromSymbolOwner
14-
import org.jetbrains.kotlin.ir.symbols.IrSimpleFunctionSymbol
1511
import org.jetbrains.kotlin.ir.types.defaultType
1612
import org.jetbrains.kotlin.ir.util.constructors
17-
import org.jetbrains.kotlin.name.CallableId
1813
import org.jetbrains.kotlin.name.ClassId
19-
import org.jetbrains.kotlin.utils.addToStdlib.cast
2014

2115
/** Helper class to make the `ComposableInformation` class easier to handle in IR. */
2216
public class IrComposableInformation(context: IrPluginContext) {
23-
private val symbol = context.referenceClass(ClassId.topLevel(COMPOSABLE_INFORMATION_FQN))!!
17+
private val symbol = context.referenceClass(ClassId.topLevel(InvestigatorNames.composableInformation))!!
2418

25-
/** Create a `ComposableInformation` constructor except for the `compoundKey` value. */
2619
public operator fun invoke(
2720
name: IrConst,
2821
packageName: IrConst,
2922
fileName: IrConst,
30-
): IrConstructorCallImpl = IrConstructorCallImpl.fromSymbolOwner(
31-
type = symbol.defaultType,
32-
constructorSymbol = symbol.constructors.single(),
33-
).apply {
34-
putValueArgument(0, name)
35-
putValueArgument(1, packageName)
36-
putValueArgument(2, fileName)
37-
}
38-
39-
/**
40-
* Copy the `ComposableInformation` constructor call, changing only `name` to the
41-
* new value.
42-
*
43-
* @param target The target `ComposableInformation` constructor call to copy from.
44-
* @param name The new value for the `name` parameter.
45-
*/
46-
public fun copyFrom(
47-
target: IrConstructorCall,
48-
name: IrConst,
49-
): IrConstructorCallImpl = invoke(
50-
name = name,
51-
packageName = target.getValueArgument(1)!!.cast(),
52-
fileName = target.getValueArgument(2)!!.cast(),
53-
)
54-
55-
public companion object {
56-
private var _withCompoundKeySymbol: WeakReference<IrSimpleFunctionSymbol>? = null
57-
58-
/** Get the `name` value from the `ComposableInformation` constructor call. */
59-
public fun getName(target: IrConstructorCall): IrConst = target.getValueArgument(0)!!.cast()
60-
61-
/** Gets the `ComposableInformation#withCompoundKey` symbol. */
62-
public fun withCompoundKeySymbol(context: IrPluginContext): IrSimpleFunctionSymbol =
63-
_withCompoundKeySymbol?.get() ?: run {
64-
val targetFqn = COMPOSABLE_INFORMATION_FQN.child(ComposableInformation_WITH_COMPOUND_KEY)
65-
context.referenceFunctions(CallableId.fromFqName(targetFqn)).single()
66-
}
67-
.also { symbol -> _withCompoundKeySymbol = WeakReference(symbol) }
68-
}
23+
compoundKey: IrExpression?,
24+
): IrConstructorCallImpl =
25+
IrConstructorCallImpl.fromSymbolOwner(
26+
type = symbol.defaultType,
27+
constructorSymbol = symbol.constructors.single(),
28+
).apply {
29+
putValueArgument(0, name)
30+
putValueArgument(1, packageName)
31+
putValueArgument(2, fileName)
32+
if (compoundKey != null) putValueArgument(3, compoundKey)
33+
}
6934
}

0 commit comments

Comments
 (0)