From f1bce9fad48915ce03cf98eb54bdc18d077ced5f Mon Sep 17 00:00:00 2001 From: Alexander Korepanov Date: Fri, 4 Nov 2022 14:04:34 +0100 Subject: [PATCH] [JS IR] Drop StableNamesCollector.kt --- .../irToJs/ModuleWrapperTranslation.kt | 29 ++++- .../ir/backend/js/utils/JsStaticContext.kt | 2 - .../backend/js/utils/StableNamesCollector.kt | 118 ------------------ .../kotlin/ir/backend/js/utils/misc.kt | 1 - 4 files changed, 28 insertions(+), 122 deletions(-) delete mode 100644 compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/utils/StableNamesCollector.kt diff --git a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/transformers/irToJs/ModuleWrapperTranslation.kt b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/transformers/irToJs/ModuleWrapperTranslation.kt index d8f844cfe94..dc46f3c129b 100644 --- a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/transformers/irToJs/ModuleWrapperTranslation.kt +++ b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/transformers/irToJs/ModuleWrapperTranslation.kt @@ -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 } diff --git a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/utils/JsStaticContext.kt b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/utils/JsStaticContext.kt index 2f9c2e1be27..49a16f996d6 100644 --- a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/utils/JsStaticContext.kt +++ b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/utils/JsStaticContext.kt @@ -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() - val coroutineImplDeclaration = backendContext.ir.symbols.coroutineImpl.owner val initializerBlock = JsCompositeBlock() } diff --git a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/utils/StableNamesCollector.kt b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/utils/StableNamesCollector.kt deleted file mode 100644 index 4d8b0d73534..00000000000 --- a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/utils/StableNamesCollector.kt +++ /dev/null @@ -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() - val memberNames = mutableSetOf() - - 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" -) diff --git a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/utils/misc.kt b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/utils/misc.kt index 16549151f83..002cf1fd2a1 100644 --- a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/utils/misc.kt +++ b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/utils/misc.kt @@ -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")