[JS IR] Return with createdOn hack
^KT-43222 fixed [JS IR] Not create initialisation function for file if there were attempt to create it earlier ^KT-43222 fixed [JS IR] Pureness for PIR and memoize file fields pureness ^KT-43222 fixed
This commit is contained in:
@@ -45,7 +45,8 @@ class JsIrBackendContext(
|
|||||||
override val scriptMode: Boolean = false,
|
override val scriptMode: Boolean = false,
|
||||||
override val es6mode: Boolean = false
|
override val es6mode: Boolean = false
|
||||||
) : JsCommonBackendContext {
|
) : JsCommonBackendContext {
|
||||||
val fileToInitialisationFuns: MutableMap<IrFile, IrSimpleFunction> = mutableMapOf()
|
val fileToInitialisationFuns: MutableMap<IrFile, IrSimpleFunction?> = mutableMapOf()
|
||||||
|
val fileToPurenessInitializers: MutableMap<IrFile, Boolean> = mutableMapOf()
|
||||||
|
|
||||||
override val extractedLocalClasses: MutableSet<IrClass> = hashSetOf()
|
override val extractedLocalClasses: MutableSet<IrClass> = hashSetOf()
|
||||||
|
|
||||||
|
|||||||
+43
-25
@@ -12,14 +12,13 @@ import org.jetbrains.kotlin.descriptors.DescriptorVisibilities.INTERNAL
|
|||||||
import org.jetbrains.kotlin.ir.IrStatement
|
import org.jetbrains.kotlin.ir.IrStatement
|
||||||
import org.jetbrains.kotlin.ir.UNDEFINED_OFFSET
|
import org.jetbrains.kotlin.ir.UNDEFINED_OFFSET
|
||||||
import org.jetbrains.kotlin.ir.backend.js.JsIrBackendContext
|
import org.jetbrains.kotlin.ir.backend.js.JsIrBackendContext
|
||||||
import org.jetbrains.kotlin.ir.backend.js.JsLoweredDeclarationOrigin
|
|
||||||
import org.jetbrains.kotlin.ir.backend.js.ir.JsIrArithBuilder
|
import org.jetbrains.kotlin.ir.backend.js.ir.JsIrArithBuilder
|
||||||
import org.jetbrains.kotlin.ir.backend.js.ir.JsIrBuilder
|
import org.jetbrains.kotlin.ir.backend.js.ir.JsIrBuilder
|
||||||
import org.jetbrains.kotlin.ir.backend.js.utils.isPure
|
import org.jetbrains.kotlin.ir.backend.js.utils.isPure
|
||||||
import org.jetbrains.kotlin.ir.builders.declarations.addFunction
|
import org.jetbrains.kotlin.ir.builders.declarations.addFunction
|
||||||
import org.jetbrains.kotlin.ir.builders.declarations.buildField
|
import org.jetbrains.kotlin.ir.builders.declarations.buildField
|
||||||
import org.jetbrains.kotlin.ir.declarations.*
|
import org.jetbrains.kotlin.ir.declarations.*
|
||||||
import org.jetbrains.kotlin.ir.declarations.persistent.carriers.DeclarationCarrier
|
import org.jetbrains.kotlin.ir.declarations.persistent.PersistentIrElementBase
|
||||||
import org.jetbrains.kotlin.ir.expressions.*
|
import org.jetbrains.kotlin.ir.expressions.*
|
||||||
import org.jetbrains.kotlin.name.Name
|
import org.jetbrains.kotlin.name.Name
|
||||||
import kotlin.collections.component1
|
import kotlin.collections.component1
|
||||||
@@ -40,6 +39,9 @@ class PropertyLazyInitLowering(
|
|||||||
val fileToInitialisationFuns
|
val fileToInitialisationFuns
|
||||||
get() = context.fileToInitialisationFuns
|
get() = context.fileToInitialisationFuns
|
||||||
|
|
||||||
|
val fileToPurenessInitializers
|
||||||
|
get() = context.fileToPurenessInitializers
|
||||||
|
|
||||||
override fun lower(irBody: IrBody, container: IrDeclaration) {
|
override fun lower(irBody: IrBody, container: IrDeclaration) {
|
||||||
if (container !is IrSimpleFunction && container !is IrField && container !is IrProperty)
|
if (container !is IrSimpleFunction && container !is IrField && container !is IrProperty)
|
||||||
return
|
return
|
||||||
@@ -49,9 +51,13 @@ class PropertyLazyInitLowering(
|
|||||||
val file = container.parent as? IrFile
|
val file = container.parent as? IrFile
|
||||||
?: return
|
?: return
|
||||||
|
|
||||||
val initFun = fileToInitialisationFuns[file]
|
val initFun = (if (file in fileToInitialisationFuns) {
|
||||||
?: createInitialisationFunction(file)?.also { fileToInitialisationFuns[file] = it }
|
fileToInitialisationFuns[file]
|
||||||
?: return
|
} else {
|
||||||
|
createInitialisationFunction(file).also {
|
||||||
|
fileToInitialisationFuns[file] = it
|
||||||
|
}
|
||||||
|
}) ?: return
|
||||||
|
|
||||||
val initialisationCall = JsIrBuilder.buildCall(
|
val initialisationCall = JsIrBuilder.buildCall(
|
||||||
target = initFun.symbol,
|
target = initFun.symbol,
|
||||||
@@ -80,7 +86,7 @@ class PropertyLazyInitLowering(
|
|||||||
): IrSimpleFunction? {
|
): IrSimpleFunction? {
|
||||||
val fileName = file.name
|
val fileName = file.name
|
||||||
|
|
||||||
val declarations = ArrayList(file.declarations)
|
val declarations = file.declarations.toList()
|
||||||
|
|
||||||
val fieldToInitializer = calculateFieldToExpression(
|
val fieldToInitializer = calculateFieldToExpression(
|
||||||
declarations
|
declarations
|
||||||
@@ -88,9 +94,10 @@ class PropertyLazyInitLowering(
|
|||||||
|
|
||||||
if (fieldToInitializer.isEmpty()) return null
|
if (fieldToInitializer.isEmpty()) return null
|
||||||
|
|
||||||
// if (allFieldsInFilePure(fieldToInitializer.values)) {
|
val allFieldsInFilePure = allFieldsInFilePure(fieldToInitializer.values)
|
||||||
// return null
|
if (allFieldsInFilePure) {
|
||||||
// }
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
val initialisedField = irFactory.createInitialisationField(fileName)
|
val initialisedField = irFactory.createInitialisationField(fileName)
|
||||||
.apply {
|
.apply {
|
||||||
@@ -202,18 +209,22 @@ class RemoveInitializersForLazyProperties(
|
|||||||
private val context: JsIrBackendContext
|
private val context: JsIrBackendContext
|
||||||
) : DeclarationTransformer {
|
) : DeclarationTransformer {
|
||||||
|
|
||||||
val fileToInitialisationFuns
|
val fileToPurenessInitializers
|
||||||
get() = context.fileToInitialisationFuns
|
get() = context.fileToPurenessInitializers
|
||||||
|
|
||||||
override fun transformFlat(declaration: IrDeclaration): List<IrDeclaration>? {
|
override fun transformFlat(declaration: IrDeclaration): List<IrDeclaration>? {
|
||||||
// val file = declaration.parent as? IrFile ?: return null
|
if (declaration !is IrField) return null
|
||||||
// val declarations = ArrayList(file.declarations)
|
|
||||||
//
|
val file = declaration.parent as? IrFile ?: return null
|
||||||
// val fields = calculateFieldToExpression(declarations).values
|
|
||||||
//
|
if (fileToPurenessInitializers[file] == true) return null
|
||||||
// if (allFieldsInFilePure(fields)) {
|
|
||||||
// return null
|
val allFieldsInFilePure = fileToPurenessInitializers[file]
|
||||||
// }
|
?: calculateFileFieldsPureness(file)
|
||||||
|
|
||||||
|
if (allFieldsInFilePure) {
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
declaration.correspondingProperty
|
declaration.correspondingProperty
|
||||||
?.takeIf { it.isForLazyInit() }
|
?.takeIf { it.isForLazyInit() }
|
||||||
@@ -222,6 +233,16 @@ class RemoveInitializersForLazyProperties(
|
|||||||
|
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun calculateFileFieldsPureness(file: IrFile): Boolean {
|
||||||
|
val declarations = file.declarations.toList()
|
||||||
|
val expressions = calculateFieldToExpression(declarations)
|
||||||
|
.values
|
||||||
|
|
||||||
|
val allFieldsInFilePure = allFieldsInFilePure(expressions)
|
||||||
|
fileToPurenessInitializers[file] = allFieldsInFilePure
|
||||||
|
return allFieldsInFilePure
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun calculateFieldToExpression(declarations: Collection<IrDeclaration>): Map<IrField, IrExpression> =
|
private fun calculateFieldToExpression(declarations: Collection<IrDeclaration>): Map<IrField, IrExpression> =
|
||||||
@@ -243,11 +264,6 @@ private val IrDeclaration.correspondingProperty: IrProperty?
|
|||||||
if (this !is IrSimpleFunction && this !is IrField && this !is IrProperty)
|
if (this !is IrSimpleFunction && this !is IrField && this !is IrProperty)
|
||||||
return null
|
return null
|
||||||
|
|
||||||
// Objects are in fact fields and we need to ignore them
|
|
||||||
val originField = (this as? DeclarationCarrier)?.originField
|
|
||||||
if (originField == JsLoweredDeclarationOrigin.OBJECT_GET_INSTANCE_FUNCTION || originField == IrDeclarationOrigin.FIELD_FOR_OBJECT_INSTANCE)
|
|
||||||
return null
|
|
||||||
|
|
||||||
return when (this) {
|
return when (this) {
|
||||||
is IrProperty -> this
|
is IrProperty -> this
|
||||||
is IrSimpleFunction -> propertyWithPersistentSafe {
|
is IrSimpleFunction -> propertyWithPersistentSafe {
|
||||||
@@ -261,4 +277,6 @@ private val IrDeclaration.correspondingProperty: IrProperty?
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun IrDeclaration.propertyWithPersistentSafe(transform: IrDeclaration.() -> IrProperty?): IrProperty? =
|
private fun IrDeclaration.propertyWithPersistentSafe(transform: IrDeclaration.() -> IrProperty?): IrProperty? =
|
||||||
transform()
|
if (((this as? PersistentIrElementBase<*>)?.createdOn ?: 0) <= stageController.currentStage) {
|
||||||
|
transform()
|
||||||
|
} else null
|
||||||
Reference in New Issue
Block a user