[JS IR] Drop NameTables class
This commit is contained in:
committed by
Space Team
parent
43fbb0ba3e
commit
99e93258cd
@@ -16,7 +16,6 @@ import org.jetbrains.kotlin.ir.backend.js.lower.generateJsTests
|
|||||||
import org.jetbrains.kotlin.ir.backend.js.lower.moveBodilessDeclarationsToSeparatePlace
|
import org.jetbrains.kotlin.ir.backend.js.lower.moveBodilessDeclarationsToSeparatePlace
|
||||||
import org.jetbrains.kotlin.ir.backend.js.lower.serialization.ir.JsIrLinker
|
import org.jetbrains.kotlin.ir.backend.js.lower.serialization.ir.JsIrLinker
|
||||||
import org.jetbrains.kotlin.ir.backend.js.transformers.irToJs.TranslationMode
|
import org.jetbrains.kotlin.ir.backend.js.transformers.irToJs.TranslationMode
|
||||||
import org.jetbrains.kotlin.ir.backend.js.utils.NameTables
|
|
||||||
import org.jetbrains.kotlin.ir.declarations.IrFactory
|
import org.jetbrains.kotlin.ir.declarations.IrFactory
|
||||||
import org.jetbrains.kotlin.ir.declarations.IrModuleFragment
|
import org.jetbrains.kotlin.ir.declarations.IrModuleFragment
|
||||||
import org.jetbrains.kotlin.ir.util.ExternalDependenciesGenerator
|
import org.jetbrains.kotlin.ir.util.ExternalDependenciesGenerator
|
||||||
|
|||||||
@@ -178,168 +178,6 @@ fun jsFunctionSignature(
|
|||||||
return calculateJsFunctionSignature(declarationSignature, context)
|
return calculateJsFunctionSignature(declarationSignature, context)
|
||||||
}
|
}
|
||||||
|
|
||||||
class NameTables(
|
|
||||||
packages: Iterable<IrPackageFragment>,
|
|
||||||
reservedForGlobal: MutableSet<String> = mutableSetOf(),
|
|
||||||
reservedForMember: MutableSet<String> = mutableSetOf(),
|
|
||||||
val mappedNames: MutableMap<String, String>? = null,
|
|
||||||
private val context: JsIrBackendContext? = null
|
|
||||||
) {
|
|
||||||
val globalNames: NameTable<IrDeclaration>
|
|
||||||
private val memberNames: NameTable<IrField>
|
|
||||||
private val loopNames = mutableMapOf<IrLoop, String>()
|
|
||||||
|
|
||||||
init {
|
|
||||||
val stableNamesCollector = StableNamesCollector()
|
|
||||||
packages.forEach { it.acceptChildrenVoid(stableNamesCollector) }
|
|
||||||
|
|
||||||
globalNames = NameTable(
|
|
||||||
reserved = (stableNamesCollector.staticNames + reservedForGlobal).toMutableSet(),
|
|
||||||
mappedNames = mappedNames
|
|
||||||
)
|
|
||||||
|
|
||||||
memberNames = NameTable(
|
|
||||||
reserved = (stableNamesCollector.memberNames + reservedForMember).toMutableSet(),
|
|
||||||
mappedNames = mappedNames
|
|
||||||
)
|
|
||||||
|
|
||||||
for (p in packages) {
|
|
||||||
for (declaration in p.declarations) {
|
|
||||||
processTopLevelLocalDecl(declaration)
|
|
||||||
processNonTopLevelLocalDecl(declaration)
|
|
||||||
declaration.acceptChildrenVoid(object : IrElementVisitorVoid {
|
|
||||||
override fun visitElement(element: IrElement) {
|
|
||||||
element.acceptChildrenVoid(this)
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun visitDeclaration(declaration: IrDeclarationBase) {
|
|
||||||
processNonTopLevelLocalDecl(declaration)
|
|
||||||
super.visitDeclaration(declaration)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
if (declaration is IrScript) {
|
|
||||||
for (memberDecl in declaration.statements) {
|
|
||||||
if (memberDecl is IrDeclaration) {
|
|
||||||
processTopLevelLocalDecl(memberDecl)
|
|
||||||
if (memberDecl is IrClass) {
|
|
||||||
processNonTopLevelLocalDecl(memberDecl)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun acceptNonExternalClass(declaration: IrClass) {
|
|
||||||
for (memberDecl in declaration.declarations) {
|
|
||||||
when (memberDecl) {
|
|
||||||
is IrField ->
|
|
||||||
generateNameForMemberField(memberDecl)
|
|
||||||
is IrSimpleFunction -> {
|
|
||||||
if (declaration.isInterface && memberDecl.body != null) {
|
|
||||||
globalNames.declareFreshName(memberDecl, memberDecl.name.asString())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun processNonTopLevelLocalDecl(declaration: IrDeclaration) {
|
|
||||||
if (declaration is IrClass) {
|
|
||||||
if (!declaration.isEffectivelyExternal()) {
|
|
||||||
acceptNonExternalClass(declaration)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun <T, K> MutableMap<T, K>.addAllIfAbsent(other: Map<T, K>) {
|
|
||||||
this += other.filter { it.key !in this }
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun packagesAdded() = mappedNames.isNullOrEmpty()
|
|
||||||
|
|
||||||
fun merge(files: List<IrPackageFragment>, additionalPackages: List<IrPackageFragment>) {
|
|
||||||
val packages = mutableListOf<IrPackageFragment>().also { it.addAll(files) }
|
|
||||||
if (packagesAdded()) packages.addAll(additionalPackages)
|
|
||||||
|
|
||||||
val table = NameTables(
|
|
||||||
packages,
|
|
||||||
globalNames.reserved,
|
|
||||||
memberNames.reserved,
|
|
||||||
mappedNames,
|
|
||||||
context
|
|
||||||
)
|
|
||||||
|
|
||||||
globalNames.names.addAllIfAbsent(table.globalNames.names)
|
|
||||||
memberNames.names.addAllIfAbsent(table.memberNames.names)
|
|
||||||
loopNames.addAllIfAbsent(table.loopNames)
|
|
||||||
|
|
||||||
globalNames.reserved.addAll(table.globalNames.reserved)
|
|
||||||
memberNames.reserved.addAll(table.memberNames.reserved)
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun generateNameForMemberField(field: IrField) {
|
|
||||||
require(!field.isTopLevel)
|
|
||||||
require(!field.isStatic)
|
|
||||||
|
|
||||||
if (field.isEffectivelyExternal()) {
|
|
||||||
memberNames.declareStableName(field, field.name.identifier)
|
|
||||||
} else {
|
|
||||||
memberNames.declareFreshName(field, "_" + sanitizeName(field.name.asString()))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Suppress("unused")
|
|
||||||
fun dump(): String {
|
|
||||||
return "Global names:\n${globalNames.dump()}"
|
|
||||||
}
|
|
||||||
|
|
||||||
fun getNameForStaticDeclaration(declaration: IrDeclarationWithName): String {
|
|
||||||
val global: String? = globalNames.names[declaration]
|
|
||||||
if (global != null) return global
|
|
||||||
|
|
||||||
mappedNames?.get(mapToKey(declaration))?.let {
|
|
||||||
return it
|
|
||||||
}
|
|
||||||
|
|
||||||
error("Can't find name for declaration ${declaration.render()}")
|
|
||||||
}
|
|
||||||
|
|
||||||
fun getNameForMemberField(field: IrField): String {
|
|
||||||
val name = memberNames.names[field] ?: mappedNames?.get(mapToKey(field))
|
|
||||||
|
|
||||||
// TODO investigate
|
|
||||||
if (name == null) {
|
|
||||||
return sanitizeName(field.name.asString()) + "__error"
|
|
||||||
}
|
|
||||||
|
|
||||||
return name
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun processTopLevelLocalDecl(declaration: IrDeclaration) {
|
|
||||||
when {
|
|
||||||
declaration !is IrDeclarationWithName ->
|
|
||||||
return
|
|
||||||
|
|
||||||
// TODO: Handle JsQualifier
|
|
||||||
declaration.isEffectivelyExternal() && !declaration.isImportedFromModuleOnly() -> {
|
|
||||||
if (declaration.file.getJsModule() != null) {
|
|
||||||
globalNames.declareFreshName(declaration, declaration.name.asString())
|
|
||||||
} else {
|
|
||||||
globalNames.declareStableName(declaration, declaration.getJsNameOrKotlinName().identifier)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
else -> {
|
|
||||||
val name = declaration.nameIfPropertyAccessor() ?: declaration.name.asString()
|
|
||||||
globalNames.declareFreshName(declaration, name)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
class LocalNameGenerator(val variableNames: NameTable<IrDeclaration>) : IrElementVisitorVoid {
|
class LocalNameGenerator(val variableNames: NameTable<IrDeclaration>) : IrElementVisitorVoid {
|
||||||
val localLoopNames = NameTable<IrLoop>()
|
val localLoopNames = NameTable<IrLoop>()
|
||||||
val localReturnableBlockNames = NameTable<IrReturnableBlock>()
|
val localReturnableBlockNames = NameTable<IrReturnableBlock>()
|
||||||
|
|||||||
Reference in New Issue
Block a user