[JS IR] explicitly mark JsName's temporary or not

This commit is contained in:
Anton Bannykh
2021-09-26 15:05:22 +03:00
committed by teamcityserver
parent 7e7c84fcfb
commit 936383254a
12 changed files with 26 additions and 31 deletions
@@ -195,7 +195,7 @@ class IrToJs(
// Postpone initialization by putting it into a separate function
// Will be called later in proper order after class model is initialized
val initFunction = JsFunction(emptyScope, JsBlock(initializerBlock.statements), "init fun")
initFunction.name = JsName(unit.initFunctionName)
initFunction.name = JsName(unit.initFunctionName, false)
statements += initFunction.makeStmt()
statements += JsExport(initFunction.name)
}
@@ -205,7 +205,7 @@ class IrToJs(
val internalExports = mutableListOf<JsExport.Element>()
fun export(declaration: IrDeclarationWithName) {
internalExports += JsExport.Element(nameGenerator.getNameForStaticDeclaration(declaration), JsName(guid(declaration)))
internalExports += JsExport.Element(nameGenerator.getNameForStaticDeclaration(declaration), JsName(guid(declaration), false))
}
for (fragment in unit.packageFragments) {
@@ -346,7 +346,7 @@ class IrToJs(
val importElements = JsImport.Element(unit.initFunctionName, null)
indexJsStatements += JsImport("./$pathToSubModule", mutableListOf(importElements))
indexJsStatements += JsInvocation(JsNameRef(JsName(unit.initFunctionName))).makeStmt()
indexJsStatements += JsInvocation(JsNameRef(JsName(unit.initFunctionName, false))).makeStmt()
exportedDeclarations += generatedUnit.exportedDeclarations
@@ -32,12 +32,11 @@ class ExportModelToJsStatements(
for (element in elements) {
val newNamespace = "$currentNamespace$$element"
val newNameSpaceRef = namespaceToRefMap.getOrPut(newNamespace) {
val varName = declareNewNamespace(newNamespace)
val varRef = JsNameRef(varName)
val varName = JsName(declareNewNamespace(newNamespace), false)
val namespaceRef = JsNameRef(element, currentRef)
statements += JsVars(
JsVars.JsVar(
JsName(varName),
varName,
JsAstUtils.or(
namespaceRef,
jsAssignment(
@@ -47,7 +46,7 @@ class ExportModelToJsStatements(
)
)
)
varRef
JsNameRef(varName)
}
currentRef = newNameSpaceRef
currentNamespace = newNamespace
@@ -58,7 +57,7 @@ class ExportModelToJsStatements(
is ExportedFunction -> {
val name = namer.getNameForStaticDeclaration(declaration.ir)
if (namespace == null) {
listOf(JsExport(name, alias = JsName(declaration.name)))
listOf(JsExport(name, alias = JsName(declaration.name, false)))
} else {
listOf(
jsAssignment(
@@ -86,7 +85,7 @@ class ExportModelToJsStatements(
val name = namer.getNameForStaticDeclaration(declaration.ir)
val klassExport =
if (namespace == null) {
JsExport(name, alias = JsName(declaration.name))
JsExport(name, alias = JsName(declaration.name, false))
} else {
jsAssignment(
newNameSpace,
@@ -193,7 +193,7 @@ private class JsCodeOutlineTransformer(
}
val newFun = JsFunction(emptyScope, JsBlock(newStatements), "")
kotlinLocalsUsedInJs.forEach { irParameter ->
newFun.parameters.add(JsParameter(JsName(irParameter.name.identifier)))
newFun.parameters.add(JsParameter(JsName(irParameter.name.identifier, false)))
}
with(backendContext.createIrBuilder(container.symbol)) {
@@ -101,7 +101,7 @@ class IrModuleToJsTransformer(
val exports = exportData[m]!![f]!! // TODO
val internalModuleName = JsName("_")
val internalModuleName = JsName("_", false)
val globalNames = NameTable<String>(namer.globalNames)
val exportStatements =
ExportModelToJsStatements(nameGenerator, { globalNames.declareFreshName(it, it) }).generateModuleExport(
@@ -196,18 +196,18 @@ class IrModuleToJsTransformer(
val (importStatements, importedJsModules) =
generateImportStatements(
getNameForExternalDeclaration = { staticContext.getNameForStaticDeclaration(it) },
declareFreshGlobal = { JsName(sanitizeName(it)) } // TODO: Declare fresh name
declareFreshGlobal = { JsName(sanitizeName(it), false) } // TODO: Declare fresh name
)
val (moduleBody, callToMain, exportStatements) = generateModuleBody(modules, staticContext, fragments)
val internalModuleName = JsName("_")
val internalModuleName = JsName("_", false)
val (crossModuleImports, importedKotlinModules) = generateCrossModuleImports(
nameGenerator,
modules,
dependencies,
{ JsName(sanitizeName(it)) })
{ JsName(sanitizeName(it), false) })
val crossModuleExports = generateCrossModuleExports(modules, refInfo, internalModuleName)
val program = JsProgram()
@@ -302,7 +302,7 @@ class IrModuleToJsTransformer(
modules += JsImportedModule(module.externalModuleName(), moduleName, null, relativeRequirePath)
names.forEach {
imports += JsVars(JsVars.JsVar(JsName(it), JsNameRef(it, JsNameRef("\$crossModule\$", moduleName.makeRef()))))
imports += JsVars(JsVars.JsVar(JsName(it, false), JsNameRef(it, JsNameRef("\$crossModule\$", moduleName.makeRef()))))
}
}
@@ -166,7 +166,7 @@ class JsClassGenerator(private val irClass: IrClass, val context: JsGenerationCo
val setterForwarder = if (property.setter?.modality == Modality.FINAL) property.setter?.accessorRef()
else {
property.setter?.let {
val setterArgName = JsName("value")
val setterArgName = JsName("value", false)
it.propertyAccessorForwarder("setter forwarder") { setterRef ->
JsInvocation(
setterRef,
@@ -98,7 +98,7 @@ class JsIntrinsicTransformers(backendContext: JsIrBackendContext) {
}
add(intrinsics.jsNewTarget) { _, _ ->
JsNameRef(JsName("target"), JsNameRef(JsName("new")))
JsNameRef(JsName("target", false), JsNameRef(JsName("new", false)))
}
add(intrinsics.jsOpenInitializerBox) { call, context ->
@@ -179,7 +179,7 @@ fun translateCall(
)
} else {
// TODO: Do not create IIFE at all? (Currently there is no reliable way to create temporary variable in current scope)
val receiverName = JsName("\$externalVarargReceiverTmp")
val receiverName = JsName("\$externalVarargReceiverTmp", false)
val receiverRef = receiverName.makeRef()
val iifeFun = JsFunction(
@@ -52,17 +52,17 @@ class JsGenerationContext(
fun getNameForValueDeclaration(declaration: IrDeclarationWithName): JsName {
val name = localNames!!.variableNames.names[declaration]
?: error("Variable name is not found ${declaration.name}")
return JsName(name)
return JsName(name, false)
}
fun getNameForLoop(loop: IrLoop): JsName? {
val name = localNames!!.localLoopNames.names[loop] ?: return null
return JsName(name)
return JsName(name, false)
}
fun getNameForReturnableBlock(block: IrReturnableBlock): JsName? {
val name = localNames!!.localReturnableBlockNames.names[block] ?: return null
return JsName(name)
return JsName(name, false)
}
fun checkIfJsCode(symbol: IrFunctionSymbol): Boolean = symbol == staticContext.backendContext.intrinsics.jsCode
@@ -107,11 +107,11 @@ class NewNamerImpl(
)
// TODO: Webpack not minimize member names, it is long name, which is not minimized, so it affects final JS bundle size
// Use shorter names
return JsName("f_$fieldName")
return JsName("f_$fieldName", false)
}
override fun getNameForStaticDeclaration(declaration: IrDeclarationWithName): JsName {
staticNames.names[declaration]?.let { return JsName(it) }
staticNames.names[declaration]?.let { return JsName(it, false) }
fun registerImport(moduleId: String, importedName: String) {
val fullModuleId = if (moduleId.startsWith(".")) {
@@ -164,7 +164,7 @@ class NewNamerImpl(
}
}
return JsName(staticNames.names[declaration]!!)
return JsName(staticNames.names[declaration]!!, false)
}
}
@@ -14,7 +14,7 @@ class JsImport(
val name: JsName,
val alias: JsName?
) {
constructor(name: String, alias: String?) : this(JsName(name), alias?.let { JsName(it) })
constructor(name: String, alias: String?) : this(JsName(name, false), alias?.let { JsName(it, false) })
}
override fun accept(visitor: JsVisitor) {
@@ -25,10 +25,6 @@ public class JsName extends HasMetadata implements Symbol {
this.temporary = temporary;
}
public JsName(@NotNull String ident) {
this(ident, false);
}
public boolean isTemporary() {
return temporary;
}
@@ -241,7 +241,7 @@ private fun String.toAst(): JsNode {
val (name, index) = parts
if (ident != "${name}_$index") error("Unable to parse: $ident")
JsName(name).also {
JsName(name, false).also {
nameMapping[ident] = this to it
}
}
@@ -259,7 +259,7 @@ private fun JsNode.makeNamesUnique(): JsNode {
nameCounter[ident] = it + 1
}
JsName("${ident}_$index").also {
JsName("${ident}_$index", false).also {
nameMap[this] = it
}
}