[JS IR] local var clashes

This commit is contained in:
Anton Bannykh
2021-10-18 21:11:07 +03:00
committed by TeamCityServer
parent 05e8bfcd2a
commit 5100cb058f
@@ -5,10 +5,8 @@
package org.jetbrains.kotlin.ir.backend.js.utils
import org.jetbrains.kotlin.ir.declarations.IrDeclarationWithName
import org.jetbrains.kotlin.ir.declarations.IrFile
import org.jetbrains.kotlin.ir.declarations.IrFunction
import org.jetbrains.kotlin.ir.declarations.IrSimpleFunction
import org.jetbrains.kotlin.ir.IrElement
import org.jetbrains.kotlin.ir.declarations.*
import org.jetbrains.kotlin.ir.expressions.IrLoop
import org.jetbrains.kotlin.ir.expressions.IrReturnableBlock
import org.jetbrains.kotlin.ir.symbols.IrFunctionSymbol
@@ -49,20 +47,28 @@ class JsGenerationContext(
)
}
private val nameCache = mutableMapOf<IrElement, JsName>()
fun getNameForValueDeclaration(declaration: IrDeclarationWithName): JsName {
val name = localNames!!.variableNames.names[declaration]
?: error("Variable name is not found ${declaration.name}")
return JsName(name, false)
return nameCache.getOrPut(declaration) {
val name = localNames!!.variableNames.names[declaration]
?: error("Variable name is not found ${declaration.name}")
JsName(name, true)
}
}
fun getNameForLoop(loop: IrLoop): JsName? {
val name = localNames!!.localLoopNames.names[loop] ?: return null
return JsName(name, false)
return nameCache.getOrPut(loop) {
val name = localNames!!.localLoopNames.names[loop] ?: return null
JsName(name, true)
}
}
fun getNameForReturnableBlock(block: IrReturnableBlock): JsName? {
val name = localNames!!.localReturnableBlockNames.names[block] ?: return null
return JsName(name, false)
return nameCache.getOrPut(block) {
val name = localNames!!.localReturnableBlockNames.names[block] ?: return null
return JsName(name, true)
}
}
fun checkIfJsCode(symbol: IrFunctionSymbol): Boolean = symbol == staticContext.backendContext.intrinsics.jsCode