[JS IR] Use mappedNames in NameTable only for REPL

And throw exception for unsupported types in mapToKey
This commit is contained in:
Zalim Bashorov
2020-12-07 19:58:02 +03:00
parent faa24e3230
commit 7c5ea414b7
6 changed files with 29 additions and 24 deletions
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Copyright 2010-2020 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.
*/
@@ -26,24 +26,27 @@ import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.utils.addToStdlib.ifNotEmpty
import java.util.*
fun <T> mapToKey(declaration: T): String {
// TODO remove direct usages of [mapToKey] from [NameTable] & co and move it to scripting & REPL infrastructure. Review usages.
private fun <T> mapToKey(declaration: T): String {
return with(JsManglerIr) {
if (declaration is IrDeclaration && isPublic(declaration)) {
declaration.hashedMangle.toString()
} else if (declaration is Signature) {
declaration.toString().hashMangle.toString()
} else "key_have_not_generated"
} else {
error("Key is not generated for " + declaration?.let { it::class.simpleName })
}
}
}
fun JsManglerIr.isPublic(declaration: IrDeclaration) =
private fun JsManglerIr.isPublic(declaration: IrDeclaration) =
declaration.isExported() && declaration !is IrScript && declaration !is IrVariable && declaration !is IrValueParameter
class NameTable<T>(
val parent: NameTable<*>? = null,
val reserved: MutableSet<String> = mutableSetOf(),
val sanitizer: (String) -> String = ::sanitizeName,
val mappedNames: MutableMap<String, String> = mutableMapOf()
val mappedNames: MutableMap<String, String>? = null
) {
var finished = false
val names = mutableMapOf<T, String>()
@@ -59,7 +62,7 @@ class NameTable<T>(
assert(!finished)
names[declaration] = name
reserved.add(name)
mappedNames[mapToKey(declaration)] = name
mappedNames?.set(mapToKey(declaration), name)
}
fun declareFreshName(declaration: T, suggestedName: String): String {
@@ -151,7 +154,7 @@ class NameTables(
packages: List<IrPackageFragment>,
reservedForGlobal: MutableSet<String> = mutableSetOf(),
reservedForMember: MutableSet<String> = mutableSetOf(),
val mappedNames: MutableMap<String, String> = mutableMapOf()
val mappedNames: MutableMap<String, String>? = null
) {
val globalNames: NameTable<IrDeclaration>
private val memberNames: NameTable<Signature>
@@ -172,8 +175,6 @@ class NameTables(
mappedNames = mappedNames
)
mappedNames.addAllIfAbsent(mappedNames)
val classDeclaration = mutableListOf<IrClass>()
for (p in packages) {
for (declaration in p.declarations) {
@@ -249,7 +250,7 @@ class NameTables(
this += other.filter { it.key !in this }
}
private fun packagesAdded() = mappedNames.isEmpty()
private fun packagesAdded() = mappedNames.isNullOrEmpty()
fun merge(files: List<IrPackageFragment>, additionalPackages: List<IrPackageFragment>) {
val packages = mutableListOf<IrPackageFragment>().also { it.addAll(files) }
@@ -311,13 +312,17 @@ class NameTables(
parent = parent.parent
}
return mappedNames[mapToKey(declaration)]
?: error("Can't find name for declaration ${declaration.render()}")
mappedNames?.get(mapToKey(declaration))?.let {
return it
}
error("Can't find name for declaration ${declaration.render()}")
}
fun getNameForMemberField(field: IrField): String {
val signature = fieldSignature(field)
val name = memberNames.names[signature] ?: mappedNames[mapToKey(signature)]
val name = memberNames.names[signature] ?: mappedNames?.get(mapToKey(signature))
// TODO investigate
if (name == null) {
@@ -329,7 +334,7 @@ class NameTables(
fun getNameForMemberFunction(function: IrSimpleFunction): String {
val signature = jsFunctionSignature(function)
val name = memberNames.names[signature] ?: mappedNames[mapToKey(signature)]
val name = memberNames.names[signature] ?: mappedNames?.get(mapToKey(signature))
// TODO Add a compiler flag, which enables this behaviour
// TODO remove in DCE
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Copyright 2010-2020 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.
*/
@@ -23,7 +23,7 @@ class JsReplTestAgainstBinaries : AbstractJsReplTest() {
private val dependencies = readLibrariesFromConfiguration(environment.configuration)
init {
val nameTable = NameTables(emptyList())
val nameTable = NameTables(emptyList(), mappedNames = mutableMapOf())
val compiler = JsScriptDependencyCompiler(environment.configuration, nameTable, createSymbolTable())
val runtimeBinary = compiler.compile(dependencies)
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Copyright 2010-2020 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.
*/
@@ -21,7 +21,7 @@ class JsReplTestAgainstKlib : AbstractJsReplTest() {
private var dependencyCode: String? = null
override fun createCompilationState(): JsReplCompilationState {
val nameTables = NameTables(emptyList())
val nameTables = NameTables(emptyList(), mappedNames = mutableMapOf())
val symbolTable = SymbolTable(IdSignatureDescriptor(JsManglerDesc), IrFactoryImpl)
val dependencyCompiler = JsScriptDependencyCompiler(environment.configuration, nameTables, symbolTable)
val dependencies = readLibrariesFromConfiguration(environment.configuration)
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Copyright 2010-2020 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.
*/
@@ -22,7 +22,7 @@ import org.jetbrains.kotlin.scripting.repl.js.readLibrariesFromConfiguration
import kotlin.script.experimental.api.*
class JsScriptCompilerWithDependenciesProxy(private val environment: KotlinCoreEnvironment) : ScriptCompilerProxy {
private val nameTables = NameTables(emptyList())
private val nameTables = NameTables(emptyList(), mappedNames = mutableMapOf())
private val symbolTable = SymbolTable(IdSignatureDescriptor(JsManglerDesc), IrFactoryImpl)
private val dependencies: List<ModuleDescriptor> = readLibrariesFromConfiguration(environment.configuration)
private val compiler = JsCoreScriptingCompiler(environment, nameTables, symbolTable, dependencies)
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Copyright 2010-2020 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.
*/
@@ -21,7 +21,7 @@ class JsReplCompiler(private val environment: KotlinCoreEnvironment) : ReplCompi
override fun createState(lock: ReentrantReadWriteLock): IReplStageState<*> {
return JsReplCompilationState(
lock,
NameTables(emptyList()),
NameTables(emptyList(), mappedNames = mutableMapOf()),
readLibrariesFromConfiguration(environment.configuration),
ReplCodeAnalyzerBase.ResettableAnalyzerState(),
SymbolTable(IdSignatureDescriptor(JsManglerDesc), IrFactoryImpl)
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Copyright 2010-2020 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.
*/
@@ -172,7 +172,7 @@ class DependencyLoader {
fun writeNames(nameTables: NameTables): ByteArray {
val result = StringBuilder()
for (entry in nameTables.mappedNames) {
for (entry in nameTables.mappedNames.orEmpty()) {
result.append("${entry.key} ${entry.value}" + System.lineSeparator())
}
return result.toString().toByteArray(Charset.defaultCharset())