From 71bfed3253212b4d0ae8487729aff5d0b155c912 Mon Sep 17 00:00:00 2001 From: Ilya Goncharov Date: Tue, 27 Oct 2020 17:40:48 +0300 Subject: [PATCH] [JS IR] From searcher get only properties, mutate in lowering ^KT-43222 fixed --- .../js/lower/PropertyLazyInitLowering.kt | 41 +++++++++++-------- 1 file changed, 23 insertions(+), 18 deletions(-) diff --git a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/lower/PropertyLazyInitLowering.kt b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/lower/PropertyLazyInitLowering.kt index ec04f3bb0b2..c9112701ef2 100644 --- a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/lower/PropertyLazyInitLowering.kt +++ b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/lower/PropertyLazyInitLowering.kt @@ -23,7 +23,6 @@ import org.jetbrains.kotlin.ir.visitors.transformChildrenVoid import org.jetbrains.kotlin.name.Name import kotlin.collections.component1 import kotlin.collections.component2 -import kotlin.collections.set class PropertyLazyInitLowering(private val context: JsIrBackendContext) : FileLoweringPass { private val irBuiltIns @@ -35,9 +34,21 @@ class PropertyLazyInitLowering(private val context: JsIrBackendContext) : FileLo get() = context.irFactory override fun lower(irFile: IrFile) { - val initializers = PropertyInitializerMover(context).process(irFile) + val properties = PropertySearcher() + .search(irFile) - if (initializers.isEmpty()) return + val fieldToInitializer = properties + .asSequence() + .filterNot { it.isDelegated } + .mapNotNull { it.backingField } + .filter { it.initializer != null } + .map { + it to it.initializer!!.expression + } + .toMap() + .onEach { it.key.initializer = null } + + if (fieldToInitializer.isEmpty()) return val fileName = irFile.name val initialisedField = irFactory.createInitialisationField(fileName) @@ -52,7 +63,7 @@ class PropertyLazyInitLowering(private val context: JsIrBackendContext) : FileLo origin = JsIrBuilder.SYNTHESIZED_DECLARATION }.apply { buildPropertiesInitializationBody( - initializers, + fieldToInitializer, initialisedField ) } @@ -120,31 +131,25 @@ class PropertyLazyInitLowering(private val context: JsIrBackendContext) : FileLo } } -private class PropertyInitializerMover( - private val context: JsIrBackendContext -) : IrElementTransformerVoid() { +private class PropertySearcher : IrElementTransformerVoid() { - private val fieldToInitializers = mutableMapOf() + private val propertyToFunction = mutableSetOf() - fun process(irFile: IrFile): Map { + fun search(irFile: IrFile): Set { irFile.transformChildrenVoid(this) - return fieldToInitializers + return propertyToFunction } override fun visitSimpleFunction(declaration: IrSimpleFunction): IrStatement { + declaration.correspondingPropertySymbol ?.owner ?.takeIf { !it.isConst } - ?.takeIf { !it.isDelegated } - ?.backingField - ?.takeIf { it !in fieldToInitializers } - ?.takeIf { it.initializer != null } - ?.let { field -> - fieldToInitializers[field] = field.initializer!!.expression + ?.takeIf { it !in propertyToFunction } + ?.let { + propertyToFunction.add(it) } - fieldToInitializers.forEach { it.key.initializer = null } - return super.visitSimpleFunction(declaration) } } \ No newline at end of file