[JS IR] Use mappedNames in NameTable only for REPL
And throw exception for unsupported types in mapToKey
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user