JS: prepare all necessary abstractions for new pipeline

This commit is contained in:
Alexey Andreev
2017-02-10 18:00:02 +03:00
parent 56810c5100
commit e909ef984c
15 changed files with 145 additions and 172 deletions
@@ -731,11 +731,6 @@ public class JsToStringGenerationVisitor extends JsVisitor {
p.print("<JsProgram>");
}
@Override
public void visitProgramFragment(@NotNull JsProgramFragment x) {
p.print("<JsProgramFragment>");
}
@Override
public void visitRegExp(@NotNull JsRegExp x) {
slash();
@@ -0,0 +1,19 @@
/*
* Copyright 2010-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.js.backend.ast
class JsClassModel(val name: JsName, val superName: JsName?)
@@ -0,0 +1,23 @@
/*
* Copyright 2010-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.js.backend.ast
class JsImportedModule(val externalName: String, val internalName: JsName, val plainReference: JsExpression?) {
val key = JsImportedModuleKey(externalName, plainReference?.toString())
}
data class JsImportedModuleKey(val baseName: String, val plainName: String?)
@@ -0,0 +1,19 @@
/*
* Copyright 2010-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.js.backend.ast
class JsNameBinding(val key: String, val name: JsName)
@@ -17,8 +17,7 @@ import java.util.Map;
* A JavaScript program.
*/
public final class JsProgram extends SourceInfoAwareJsNode {
private JsProgramFragment[] fragments;
private final JsGlobalBlock globalBlock = new JsGlobalBlock();
private final TDoubleObjectHashMap<JsDoubleLiteral> doubleLiteralMap = new TDoubleObjectHashMap<JsDoubleLiteral>();
private final TIntObjectHashMap<JsIntLiteral> intLiteralMap = new TIntObjectHashMap<JsIntLiteral>();
@@ -30,18 +29,10 @@ public final class JsProgram extends SourceInfoAwareJsNode {
public JsProgram() {
rootScope = new JsRootScope(this);
topScope = new JsObjectScope(rootScope, "Global");
setFragmentCount(1);
}
public JsBlock getFragmentBlock(int fragment) {
if (fragment < 0 || fragment >= fragments.length) {
throw new IllegalArgumentException("Invalid fragment: " + fragment);
}
return fragments[fragment].getGlobalBlock();
}
public JsBlock getGlobalBlock() {
return getFragmentBlock(0);
public JsGlobalBlock getGlobalBlock() {
return globalBlock;
}
public JsNumberLiteral getNumberLiteral(double value) {
@@ -94,13 +85,6 @@ public final class JsProgram extends SourceInfoAwareJsNode {
return literal;
}
public void setFragmentCount(int fragments) {
this.fragments = new JsProgramFragment[fragments];
for (int i = 0; i < fragments; i++) {
this.fragments[i] = new JsProgramFragment();
}
}
@Override
public void accept(JsVisitor v) {
v.visitProgram(this);
@@ -108,17 +92,13 @@ public final class JsProgram extends SourceInfoAwareJsNode {
@Override
public void acceptChildren(JsVisitor visitor) {
for (JsProgramFragment fragment : fragments) {
visitor.accept(fragment);
}
visitor.accept(globalBlock);
}
@Override
public void traverse(JsVisitorWithContext v, JsContext ctx) {
if (v.visit(this, ctx)) {
for (JsProgramFragment fragment : fragments) {
v.accept(fragment);
}
v.accept(globalBlock);
}
v.endVisit(this, ctx);
}
@@ -6,41 +6,59 @@ package org.jetbrains.kotlin.js.backend.ast;
import org.jetbrains.annotations.NotNull;
/**
* One independently loadable fragment of a {@link JsProgram}.
*/
public class JsProgramFragment extends SourceInfoAwareJsNode {
private final JsGlobalBlock globalBlock;
import java.util.*;
public JsProgramFragment() {
globalBlock = new JsGlobalBlock();
}
public class JsProgramFragment {
private final JsScope scope;
private final List<JsImportedModule> importedModules = new ArrayList<JsImportedModule>();
private final Map<String, JsExpression> imports = new LinkedHashMap<String, JsExpression>();
private final JsGlobalBlock declarationBlock = new JsGlobalBlock();
private final JsGlobalBlock exportBlock = new JsGlobalBlock();
private final JsGlobalBlock initializerBlock = new JsGlobalBlock();
private final List<JsNameBinding> nameBindings = new ArrayList<JsNameBinding>();
private final Map<JsName, JsClassModel> classes = new LinkedHashMap<JsName, JsClassModel>();
public JsBlock getGlobalBlock() {
return globalBlock;
}
@Override
public void accept(JsVisitor v) {
v.visitProgramFragment(this);
}
@Override
public void acceptChildren(JsVisitor visitor) {
visitor.accept(globalBlock);
}
@Override
public void traverse(JsVisitorWithContext v, JsContext ctx) {
if (v.visit(this, ctx)) {
v.acceptStatement(globalBlock);
}
v.endVisit(this, ctx);
public JsProgramFragment(@NotNull JsScope scope) {
this.scope = scope;
}
@NotNull
@Override
public JsProgramFragment deepCopy() {
throw new UnsupportedOperationException();
public JsScope getScope() {
return scope;
}
@NotNull
public List<JsImportedModule> getImportedModules() {
return importedModules;
}
@NotNull
public Map<String, JsExpression> getImports() {
return imports;
}
@NotNull
public JsBlock getDeclarationBlock() {
return declarationBlock;
}
@NotNull
public JsGlobalBlock getExportBlock() {
return exportBlock;
}
@NotNull
public JsGlobalBlock getInitializerBlock() {
return initializerBlock;
}
@NotNull
public List<JsNameBinding> getNameBindings() {
return nameBindings;
}
@NotNull
public Map<JsName, JsClassModel> getClasses() {
return classes;
}
}
@@ -120,9 +120,6 @@ abstract class JsVisitor {
open fun visitProgram(x: JsProgram): Unit =
visitElement(x)
open fun visitProgramFragment(x: JsProgramFragment): Unit =
visitElement(x)
open fun visitPropertyInitializer(x: JsPropertyInitializer): Unit =
visitElement(x)
@@ -152,9 +152,6 @@ public abstract class JsVisitorWithContext {
public void endVisit(@NotNull JsProgram x, @NotNull JsContext ctx) {
}
public void endVisit(@NotNull JsProgramFragment x, @NotNull JsContext ctx) {
}
public void endVisit(@NotNull JsPropertyInitializer x, @NotNull JsContext ctx) {
}
@@ -312,10 +309,6 @@ public abstract class JsVisitorWithContext {
return true;
}
public boolean visit(@NotNull JsProgramFragment x, @NotNull JsContext ctx) {
return true;
}
public boolean visit(@NotNull JsPropertyInitializer x, @NotNull JsContext ctx) {
return true;
}
@@ -399,7 +399,6 @@ internal open class JsExpressionVisitor() : JsVisitorWithContextImpl() {
override fun visit(x: JsFunction, ctx: JsContext<JsNode>): Boolean = false
override fun visit(x: JsObjectLiteral, ctx: JsContext<JsNode>): Boolean = false
override fun visit(x: JsPropertyInitializer, ctx: JsContext<JsNode>): Boolean = false
override fun visit(x: JsProgramFragment, ctx: JsContext<JsNode>): Boolean = false
override fun visit(x: JsProgram, ctx: JsContext<JsNode>): Boolean = false
override fun visit(x: JsParameter, ctx: JsContext<JsNode>): Boolean = false
override fun visit(x: JsCatch, ctx: JsContext<JsNode>): Boolean = false
@@ -16,6 +16,8 @@
package org.jetbrains.kotlin.js.facade;
import org.jetbrains.kotlin.js.backend.ast.JsImportedModule;
import org.jetbrains.kotlin.js.backend.ast.JsProgram;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.kotlin.descriptors.ModuleDescriptor;
@@ -97,7 +99,7 @@ public final class K2JSTranslator {
ProgressIndicatorAndCompilationCanceledStatus.checkCanceled();
List<String> importedModules = new ArrayList<>();
for (StaticContext.ImportedModule module : context.getImportedModules()) {
for (JsImportedModule module : context.getImportedModules()) {
importedModules.add(module.getExternalName());
}
return new TranslationResult.Success(config, files, program, diagnostics, importedModules, moduleDescriptor,
@@ -37,11 +37,6 @@ public class JsSourceGenerationVisitor extends JsToStringGenerationVisitor imple
out.setOutListener(this);
}
@Override
public void visitProgramFragment(@NotNull JsProgramFragment x) {
x.acceptChildren(this);
}
@Override
public void newLined() {
if (sourceMapBuilder != null) {
@@ -125,9 +125,9 @@ public final class StaticContext {
private final Map<DeclarationDescriptor, JsExpression> fqnCache = new HashMap<>();
@NotNull
private final Map<ImportedModuleKey, ImportedModule> importedModules = new LinkedHashMap<>();
private final Map<JsImportedModuleKey, JsImportedModule> importedModules = new LinkedHashMap<>();
private Collection<ImportedModule> readOnlyImportedModules;
private Collection<JsImportedModule> readOnlyImportedModules;
@NotNull
private final JsScope rootPackageScope;
@@ -175,8 +175,8 @@ public final class StaticContext {
rootPackageScope = new JsObjectScope(rootScope, "<root package>");
JsName kotlinName = rootScope.declareName(Namer.KOTLIN_NAME);
importedModules.put(new ImportedModuleKey(Namer.KOTLIN_LOWER_NAME, null),
new ImportedModule(Namer.KOTLIN_LOWER_NAME, kotlinName, null));
importedModules.put(new JsImportedModuleKey(Namer.KOTLIN_LOWER_NAME, null),
new JsImportedModule(Namer.KOTLIN_LOWER_NAME, kotlinName, null));
}
@NotNull
@@ -205,7 +205,7 @@ public final class StaticContext {
}
@NotNull
public Collection<ImportedModule> getImportedModules() {
public Collection<JsImportedModule> getImportedModules() {
if (readOnlyImportedModules == null) {
readOnlyImportedModules = Collections.unmodifiableCollection(importedModules.values());
}
@@ -286,7 +286,7 @@ public final class StaticContext {
if (config.getModuleKind() != ModuleKind.PLAIN) {
String moduleName = AnnotationsUtils.getModuleName(suggested.getDescriptor());
if (moduleName != null) {
return JsAstUtils.pureFqn(getImportedModule(moduleName, suggested.getDescriptor()).internalName, null);
return JsAstUtils.pureFqn(getImportedModule(moduleName, suggested.getDescriptor()).getInternalName(), null);
}
}
@@ -308,7 +308,7 @@ public final class StaticContext {
if (isNativeObject(suggested.getDescriptor()) && DescriptorUtils.isTopLevelDeclaration(suggested.getDescriptor())) {
String fileModuleName = AnnotationsUtils.getFileModuleName(getBindingContext(), suggested.getDescriptor());
if (fileModuleName != null) {
JsName moduleJsName = getImportedModule(fileModuleName, null).internalName;
JsName moduleJsName = getImportedModule(fileModuleName, null).getInternalName();
expression = pureFqn(moduleJsName, expression);
}
@@ -484,8 +484,8 @@ public final class StaticContext {
private JsName localOrImportedName(@NotNull DeclarationDescriptor descriptor, @NotNull String suggestedName) {
ModuleDescriptor module = DescriptorUtilsKt.getModule(descriptor);
JsName name = module != currentModule ?
importDeclaration(suggestedName, getQualifiedReference(descriptor)) :
rootFunction.getScope().declareTemporaryName(suggestedName);
importDeclaration(suggestedName, getQualifiedReference(descriptor)) :
rootFunction.getScope().declareTemporaryName(suggestedName);
MetadataProperties.setDescriptor(name, descriptor);
return name;
}
@@ -667,15 +667,14 @@ public final class StaticContext {
}
@NotNull
private ImportedModule getImportedModule(@NotNull String baseName, @Nullable DeclarationDescriptor descriptor) {
JsName plainName = descriptor != null && config.getModuleKind() == ModuleKind.UMD ?
rootScope.declareName(getPlainId(descriptor)) : null;
ImportedModuleKey key = new ImportedModuleKey(baseName, plainName);
private JsImportedModule getImportedModule(@NotNull String baseName, @Nullable DeclarationDescriptor descriptor) {
String plainName = descriptor != null && config.getModuleKind() == ModuleKind.UMD ? getPlainId(descriptor) : null;
JsImportedModuleKey key = new JsImportedModuleKey(baseName, plainName);
ImportedModule module = importedModules.get(key);
JsImportedModule module = importedModules.get(key);
if (module == null) {
JsName internalName = rootScope.declareTemporaryName(Namer.LOCAL_MODULE_PREFIX + Namer.suggestedModuleName(baseName));
module = new ImportedModule(baseName, internalName, plainName != null ? pureFqn(plainName, null) : null);
module = new JsImportedModule(baseName, internalName, plainName != null ? pureFqn(plainName, null) : null);
importedModules.put(key, module);
}
return module;
@@ -692,7 +691,7 @@ public final class StaticContext {
if (descriptor instanceof FunctionDescriptor ||
descriptor instanceof PackageFragmentDescriptor ||
descriptor instanceof ClassDescriptor
) {
) {
MetadataProperties.setSideEffects(expression, SideEffectKind.PURE);
}
return expression;
@@ -805,69 +804,4 @@ public final class StaticContext {
}
return false;
}
public static class ImportedModule {
@NotNull
private final String externalName;
@NotNull
private final JsName internalName;
@Nullable
private final JsExpression plainReference;
ImportedModule(@NotNull String externalName, @NotNull JsName internalName, @Nullable JsExpression plainReference) {
this.externalName = externalName;
this.internalName = internalName;
this.plainReference = plainReference;
}
@NotNull
public String getExternalName() {
return externalName;
}
@NotNull
public JsName getInternalName() {
return internalName;
}
@Nullable
public JsExpression getPlainReference() {
return plainReference;
}
}
private static class ImportedModuleKey {
@NotNull
private final String baseName;
@Nullable
private final JsName plainName;
public ImportedModuleKey(@NotNull String baseName, @Nullable JsName plainName) {
this.baseName = baseName;
this.plainName = plainName;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ImportedModuleKey key = (ImportedModuleKey) o;
if (!baseName.equals(key.baseName)) return false;
if (plainName != null ? !plainName.equals(key.plainName) : key.plainName != null) return false;
return true;
}
@Override
public int hashCode() {
int result = baseName.hashCode();
result = 31 * result + (plainName != null ? plainName.hashCode() : 0);
return result;
}
}
}
@@ -126,7 +126,7 @@ public class TranslationContext {
}
@NotNull
public Collection<StaticContext.ImportedModule> getImportedModules() {
public Collection<JsImportedModule> getImportedModules() {
return staticContext.getImportedModules();
}
@@ -544,7 +544,7 @@ public class TranslationContext {
if (result == null &&
classOrConstructor instanceof ConstructorDescriptor &&
((ConstructorDescriptor) classOrConstructor).isPrimary()
) {
) {
result = staticContext.getClassOrConstructorClosure((ClassDescriptor) classOrConstructor.getContainingDeclaration());
}
return result;
@@ -18,13 +18,12 @@ package org.jetbrains.kotlin.js.translate.general
import org.jetbrains.kotlin.js.backend.ast.*
import org.jetbrains.kotlin.js.translate.context.Namer
import org.jetbrains.kotlin.js.translate.context.StaticContext
import org.jetbrains.kotlin.js.translate.utils.JsAstUtils
import org.jetbrains.kotlin.serialization.js.ModuleKind
object ModuleWrapperTranslation {
@JvmStatic fun wrapIfNecessary(
moduleId: String, function: JsExpression, importedModules: List<StaticContext.ImportedModule>,
moduleId: String, function: JsExpression, importedModules: List<JsImportedModule>,
program: JsProgram, kind: ModuleKind
): List<JsStatement> {
return when (kind) {
@@ -37,7 +36,7 @@ object ModuleWrapperTranslation {
private fun wrapUmd(
moduleId: String, function: JsExpression,
importedModules: List<StaticContext.ImportedModule>, program: JsProgram
importedModules: List<JsImportedModule>, program: JsProgram
): List<JsStatement> {
val scope = program.scope
val defineName = scope.declareName("define")
@@ -79,7 +78,7 @@ object ModuleWrapperTranslation {
private fun wrapAmd(
function: JsExpression,
importedModules: List<StaticContext.ImportedModule>, program: JsProgram
importedModules: List<JsImportedModule>, program: JsProgram
): List<JsStatement> {
val scope = program.scope
val defineName = scope.declareName("define")
@@ -94,7 +93,7 @@ object ModuleWrapperTranslation {
private fun wrapCommonJs(
function: JsExpression,
importedModules: List<StaticContext.ImportedModule>,
importedModules: List<JsImportedModule>,
program: JsProgram
): List<JsStatement> {
val scope = program.scope
@@ -108,7 +107,7 @@ object ModuleWrapperTranslation {
private fun wrapPlain(
moduleId: String, function: JsExpression,
importedModules: List<StaticContext.ImportedModule>, program: JsProgram
importedModules: List<JsImportedModule>, program: JsProgram
): List<JsStatement> {
val invocation = makePlainInvocation(moduleId, function, importedModules, program)
val statements = mutableListOf<JsStatement>()
@@ -130,7 +129,7 @@ object ModuleWrapperTranslation {
private fun addModuleValidation(
currentModuleId: String,
program: JsProgram,
module: StaticContext.ImportedModule
module: JsImportedModule
): JsStatement {
val moduleRef = makePlainModuleRef(module, program)
val moduleExistsCond = JsAstUtils.typeOfIs(moduleRef, program.getStringLiteral("undefined"))
@@ -144,7 +143,7 @@ object ModuleWrapperTranslation {
private fun makePlainInvocation(
moduleId: String,
function: JsExpression,
importedModules: List<StaticContext.ImportedModule>,
importedModules: List<JsImportedModule>,
program: JsProgram
): JsInvocation {
val invocationArgs = importedModules.map { makePlainModuleRef(it, program) }
@@ -155,7 +154,7 @@ object ModuleWrapperTranslation {
return JsInvocation(function, listOf(selfArg) + invocationArgs)
}
private fun makePlainModuleRef(module: StaticContext.ImportedModule, program: JsProgram): JsExpression {
private fun makePlainModuleRef(module: JsImportedModule, program: JsProgram): JsExpression {
return module.plainReference ?: makePlainModuleRef(module.externalName, program)
}
@@ -283,9 +283,9 @@ public final class Translation {
// Invoke function passing modules as arguments
// This should help minifier tool to recognize references to these modules as local variables and make them shorter.
List<StaticContext.ImportedModule> importedModuleList = new ArrayList<>();
List<JsImportedModule> importedModuleList = new ArrayList<>();
for (StaticContext.ImportedModule importedModule : staticContext.getImportedModules()) {
for (JsImportedModule importedModule : staticContext.getImportedModules()) {
rootFunction.getParameters().add(new JsParameter(importedModule.getInternalName()));
importedModuleList.add(importedModule);
}