[JS IR] Drop StableNamesCollector.kt

This commit is contained in:
Alexander Korepanov
2022-11-04 14:04:34 +01:00
committed by Space Team
parent 99e93258cd
commit f1bce9fad4
4 changed files with 28 additions and 122 deletions
@@ -5,13 +5,40 @@
package org.jetbrains.kotlin.ir.backend.js.transformers.irToJs
import org.jetbrains.kotlin.ir.backend.js.utils.RESERVED_IDENTIFIERS
import org.jetbrains.kotlin.js.backend.ast.*
import org.jetbrains.kotlin.js.common.isValidES5Identifier
import org.jetbrains.kotlin.serialization.js.ModuleKind
object ModuleWrapperTranslation {
object Namer {
private val RESERVED_IDENTIFIERS = setOf(
// keywords
"await", "break", "case", "catch", "continue", "debugger", "default", "delete", "do", "else", "finally", "for", "function", "if",
"in", "instanceof", "new", "return", "switch", "throw", "try", "typeof", "var", "void", "while", "with",
// future reserved words
"class", "const", "enum", "export", "extends", "import", "super",
// as future reserved words in strict mode
"implements", "interface", "let", "package", "private", "protected", "public", "static", "yield",
// additional reserved words
"null", "true", "false",
// disallowed as variable names in strict mode
"eval", "arguments",
// global identifiers usually declared in a typical JS interpreter
"NaN", "isNaN", "Infinity", "undefined",
"Error", "Object", "Number", "String",
"Math", "String", "Boolean", "Date", "Array", "RegExp", "JSON", "Map",
// global identifiers usually declared in know environments (node.js, browser, require.js, WebWorkers, etc)
"require", "define", "module", "window", "self", "globalThis"
)
fun requiresEscaping(name: String) =
!name.isValidES5Identifier() || name in RESERVED_IDENTIFIERS
}
@@ -11,7 +11,6 @@ import org.jetbrains.kotlin.ir.backend.js.transformers.irToJs.JsIrClassModel
import org.jetbrains.kotlin.ir.declarations.IrDeclaration
import org.jetbrains.kotlin.ir.symbols.IrClassSymbol
import org.jetbrains.kotlin.js.backend.ast.JsCompositeBlock
import org.jetbrains.kotlin.js.config.JSConfigurationKeys
class JsStaticContext(
@@ -21,7 +20,6 @@ class JsStaticContext(
) : IrNamer by irNamer {
val intrinsics = JsIntrinsicTransformers(backendContext)
val classModels = mutableMapOf<IrClassSymbol, JsIrClassModel>()
val coroutineImplDeclaration = backendContext.ir.symbols.coroutineImpl.owner
val initializerBlock = JsCompositeBlock()
}
@@ -1,118 +0,0 @@
/*
* Copyright 2010-2019 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.
*/
package org.jetbrains.kotlin.ir.backend.js.utils
import org.jetbrains.kotlin.ir.IrElement
import org.jetbrains.kotlin.ir.declarations.*
import org.jetbrains.kotlin.ir.util.*
import org.jetbrains.kotlin.ir.visitors.IrElementVisitorVoid
import org.jetbrains.kotlin.ir.visitors.acceptChildrenVoid
import org.jetbrains.kotlin.utils.addIfNotNull
class StableNamesCollector : IrElementVisitorVoid {
val staticNames = mutableSetOf<String>()
val memberNames = mutableSetOf<String>()
init {
staticNames.addAll(RESERVED_IDENTIFIERS)
staticNames.add(Namer.IMPLICIT_RECEIVER_NAME)
}
override fun visitElement(element: IrElement) {
element.acceptChildrenVoid(this)
}
override fun visitDeclaration(declaration: IrDeclarationBase) {
super.visitDeclaration(declaration)
if (declaration !is IrDeclarationWithName)
return
val isStatic = declaration.hasStaticDispatch()
val scope =
if (isStatic)
staticNames
else
memberNames
val stableName =
if (declaration.isEffectivelyExternal())
stableNameForExternalDeclaration(declaration)
else
stableNameForNonExternalDeclaration(declaration, isStatic)
scope.addIfNotNull(stableName)
}
private fun stableNameForNonExternalDeclaration(declaration: IrDeclarationWithName, isStatic: Boolean): String? =
declaration.getJsName() ?: run {
// TODO: since following code affects local variable naming some weird behaviour of js("..") is possible
// Remove check once 1. `js` function is re-designed and re-implemented, 2. JsExport is properly implemented
if (!isStatic) {
// Make sure property defined on prototype has stable name
val simpleFunction = declaration as? IrSimpleFunction
val property = simpleFunction?.correspondingPropertySymbol?.owner
property?.getJsNameOrKotlinName()?.identifier
} else null
}
private fun stableNameForExternalDeclaration(declaration: IrDeclarationWithName): String? {
if (declaration.isPropertyAccessor ||
declaration.isPropertyField ||
declaration is IrConstructor
) {
return null
}
val importedFromModuleOnly = declaration.isImportedFromModuleOnly()
val jsName = declaration.getJsName()
val jsQualifier = declaration.fileOrNull?.getJsQualifier()
return when {
importedFromModuleOnly ->
null
jsQualifier != null ->
jsQualifier.substringBefore('.')
jsName != null ->
jsName
else ->
declaration.name.identifier
}
}
}
val RESERVED_IDENTIFIERS = setOf(
// keywords
"await", "break", "case", "catch", "continue", "debugger", "default", "delete", "do", "else", "finally", "for", "function", "if",
"in", "instanceof", "new", "return", "switch", "throw", "try", "typeof", "var", "void", "while", "with",
// future reserved words
"class", "const", "enum", "export", "extends", "import", "super",
// as future reserved words in strict mode
"implements", "interface", "let", "package", "private", "protected", "public", "static", "yield",
// additional reserved words
"null", "true", "false",
// disallowed as variable names in strict mode
"eval", "arguments",
// global identifiers usually declared in a typical JS interpreter
"NaN", "isNaN", "Infinity", "undefined",
"Error", "Object", "Number", "String",
"Math", "String", "Boolean", "Date", "Array", "RegExp", "JSON", "Map",
// global identifiers usually declared in know environments (node.js, browser, require.js, WebWorkers, etc)
"require", "define", "module", "window", "self", "globalThis"
)
@@ -23,7 +23,6 @@ import org.jetbrains.kotlin.ir.util.isEffectivelyExternal
import org.jetbrains.kotlin.ir.util.isMethodOfAny
import org.jetbrains.kotlin.ir.util.isTopLevel
import org.jetbrains.kotlin.ir.util.isTopLevelDeclaration
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.util.OperatorNameConventions
fun TODO(element: IrElement): Nothing = TODO(element::class.java.simpleName + " is not supported yet here")