[JS IR] Generate context dependent names for anonymous objects and classes.

This commit is contained in:
Alexander Korepanov
2021-12-02 00:25:00 +03:00
committed by Space
parent b1643075f2
commit a34c97ebd5
10 changed files with 167 additions and 3 deletions
@@ -67,6 +67,7 @@ class JsIrBackendContext(
val fileToInitializerPureness: MutableMap<IrFile, Boolean> = mutableMapOf()
val fieldToInitializer: MutableMap<IrField, IrExpression> = mutableMapOf()
val localClassNames: MutableMap<IrClass, String> = mutableMapOf()
val extractedLocalClasses: MutableSet<IrClass> = hashSetOf()
override val builtIns = module.builtIns
@@ -149,6 +149,12 @@ val createScriptFunctionsPhase = makeJsModulePhase(
description = "Create functions for initialize and evaluate script"
).toModuleLowering()
private val inventNamesForLocalClassesPhase = makeJsModulePhase(
::JsInventNamesForLocalClasses,
name = "InventNamesForLocalClasses",
description = "Invent names for local classes and anonymous objects",
).toModuleLowering()
private val annotationInstantiationLowering = makeDeclarationTransformerPhase(
::JsAnnotationImplementationTransformer,
name = "AnnotationImplementation",
@@ -220,13 +226,15 @@ private val sharedVariablesLoweringPhase = makeBodyLoweringPhase(
private val localClassesInInlineLambdasPhase = makeBodyLoweringPhase(
::LocalClassesInInlineLambdasLowering,
name = "LocalClassesInInlineLambdasPhase",
description = "Extract local classes from inline lambdas"
description = "Extract local classes from inline lambdas",
prerequisite = setOf(inventNamesForLocalClassesPhase)
)
private val localClassesInInlineFunctionsPhase = makeBodyLoweringPhase(
::LocalClassesInInlineFunctionsLowering,
name = "LocalClassesInInlineFunctionsPhase",
description = "Extract local classes from inline functions"
description = "Extract local classes from inline functions",
prerequisite = setOf(inventNamesForLocalClassesPhase)
)
private val localClassesExtractionFromInlineFunctionsPhase = makeBodyLoweringPhase(
@@ -807,6 +815,7 @@ private val jsSuspendArityStorePhase = makeDeclarationTransformerPhase(
val loweringList = listOf<Lowering>(
scriptRemoveReceiverLowering,
validateIrBeforeLowering,
inventNamesForLocalClassesPhase,
annotationInstantiationLowering,
expectDeclarationsRemovingPhase,
stripTypeAliasDeclarationsPhase,
@@ -0,0 +1,24 @@
/*
* Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.ir.backend.js.lower
import org.jetbrains.kotlin.backend.common.lower.InventNamesForLocalClasses
import org.jetbrains.kotlin.ir.backend.js.JsIrBackendContext
import org.jetbrains.kotlin.ir.backend.js.utils.sanitizeName
import org.jetbrains.kotlin.ir.declarations.IrAttributeContainer
import org.jetbrains.kotlin.ir.declarations.IrClass
class JsInventNamesForLocalClasses(private val context: JsIrBackendContext) : InventNamesForLocalClasses(allowTopLevelCallables = true) {
override fun computeTopLevelClassName(clazz: IrClass): String = clazz.name.toString()
override fun sanitizeNameIfNeeded(name: String): String = sanitizeName(name, withHash = false)
override fun putLocalClassName(declaration: IrAttributeContainer, localClassName: String) {
if (declaration is IrClass) {
context.localClassNames[declaration] = localClassName
}
}
}
@@ -21,7 +21,10 @@ class JsNameLinkingNamer(private val context: JsIrBackendContext) : IrNamerBase(
val nameMap = mutableMapOf<IrDeclaration, JsName>()
private fun IrDeclarationWithName.getName(prefix: String = ""): JsName {
return nameMap.getOrPut(this) { JsName(sanitizeName(prefix + getJsNameOrKotlinName().asString()), true) }
return nameMap.getOrPut(this) {
val name = (this as? IrClass)?.let { context.localClassNames[this] } ?: getJsNameOrKotlinName().asString()
JsName(sanitizeName(prefix + name), true)
}
}
val importedModules = mutableListOf<JsImportedModule>()