Prevent JS AST nodes of several types to be shared

This commit is contained in:
Alexey Andreev
2017-05-05 17:42:43 +03:00
parent 3077a0f640
commit e9a2c8c0f1
73 changed files with 537 additions and 485 deletions
@@ -61,7 +61,7 @@ class JsPrecedenceVisitor extends JsVisitor {
}
@Override
public void visitBoolean(@NotNull JsLiteral.JsBooleanLiteral x) {
public void visitBoolean(@NotNull JsBooleanLiteral x) {
answer = 17; // primary
}
@@ -101,12 +101,12 @@ class JsPrecedenceVisitor extends JsVisitor {
}
@Override
public void visitInt(@NotNull JsNumberLiteral.JsIntLiteral x) {
public void visitInt(@NotNull JsIntLiteral x) {
answer = 17; // primary
}
@Override
public void visitDouble(@NotNull JsNumberLiteral.JsDoubleLiteral x) {
public void visitDouble(@NotNull JsDoubleLiteral x) {
answer = 17; // primary
}
@@ -141,7 +141,7 @@ class JsPrecedenceVisitor extends JsVisitor {
}
@Override
public void visitThis(@NotNull JsLiteral.JsThisRef x) {
public void visitThis(@NotNull JsThisRef x) {
answer = 17; // primary
}
@@ -5,8 +5,8 @@
package org.jetbrains.kotlin.js.backend;
import org.jetbrains.kotlin.js.backend.ast.*;
import org.jetbrains.kotlin.js.backend.ast.JsNumberLiteral.JsDoubleLiteral;
import org.jetbrains.kotlin.js.backend.ast.JsNumberLiteral.JsIntLiteral;
import org.jetbrains.kotlin.js.backend.ast.JsDoubleLiteral;
import org.jetbrains.kotlin.js.backend.ast.JsIntLiteral;
import org.jetbrains.kotlin.js.backend.ast.JsVars.JsVar;
import org.jetbrains.kotlin.js.util.TextOutput;
import gnu.trove.THashSet;
@@ -258,7 +258,7 @@ public class JsToStringGenerationVisitor extends JsVisitor {
}
@Override
public void visitBoolean(@NotNull JsLiteral.JsBooleanLiteral x) {
public void visitBoolean(@NotNull JsBooleanLiteral x) {
if (x.getValue()) {
p.print(CHARS_TRUE);
}
@@ -773,7 +773,7 @@ public class JsToStringGenerationVisitor extends JsVisitor {
}
@Override
public void visitThis(@NotNull JsLiteral.JsThisRef x) {
public void visitThis(@NotNull JsThisRef x) {
p.print(CHARS_THIS);
}
@@ -0,0 +1,49 @@
/*
* 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;
import org.jetbrains.annotations.NotNull;
public final class JsBooleanLiteral extends JsLiteral.JsValueLiteral {
private final boolean value;
// Should be interned by JsProgram
public JsBooleanLiteral(boolean value) {
this.value = value;
}
public boolean getValue() {
return value;
}
@Override
public void accept(JsVisitor v) {
v.visitBoolean(this);
}
@Override
public void traverse(JsVisitorWithContext v, JsContext ctx) {
v.visit(this, ctx);
v.endVisit(this, ctx);
}
@NotNull
@Override
public JsBooleanLiteral deepCopy() {
return new JsBooleanLiteral(value).withMetadataFrom(this);
}
}
@@ -0,0 +1,48 @@
/*
* 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;
import org.jetbrains.annotations.NotNull;
public final class JsDoubleLiteral extends JsNumberLiteral {
public final double value;
public JsDoubleLiteral(double value) {
this.value = value;
}
@Override
public void accept(JsVisitor v) {
v.visitDouble(this);
}
public String toString() {
return String.valueOf(value);
}
@Override
public void traverse(JsVisitorWithContext v, JsContext ctx) {
v.visit(this, ctx);
v.endVisit(this, ctx);
}
@NotNull
@Override
public JsDoubleLiteral deepCopy() {
return new JsDoubleLiteral(value).withMetadataFrom(this);
}
}
@@ -11,7 +11,7 @@ import java.util.List;
public abstract class JsExpression extends SourceInfoAwareJsNode {
/**
* Determines whether or not this expression is a leaf, such as a
* {@link JsNameRef}, {@link JsLiteral.JsBooleanLiteral}, and so on. Leaf expressions
* {@link JsNameRef}, {@link JsBooleanLiteral}, and so on. Leaf expressions
* never need to be parenthesized.
*/
public boolean isLeaf() {
@@ -0,0 +1,48 @@
/*
* 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;
import org.jetbrains.annotations.NotNull;
public final class JsIntLiteral extends JsNumberLiteral {
public final int value;
public JsIntLiteral(int value) {
this.value = value;
}
@Override
public void accept(JsVisitor v) {
v.visitInt(this);
}
public String toString() {
return String.valueOf(value);
}
@Override
public void traverse(JsVisitorWithContext v, JsContext ctx) {
v.visit(this, ctx);
v.endVisit(this, ctx);
}
@NotNull
@Override
public JsExpression deepCopy() {
return new JsIntLiteral(value).withMetadataFrom(this);
}
}
@@ -7,59 +7,6 @@ package org.jetbrains.kotlin.js.backend.ast;
import org.jetbrains.annotations.NotNull;
public abstract class JsLiteral extends JsExpression {
public static final JsValueLiteral THIS = new JsThisRef();
public static final JsNameRef UNDEFINED = new JsNameRef("undefined");
public static final JsNullLiteral NULL = new JsNullLiteral();
public static final JsBooleanLiteral TRUE = new JsBooleanLiteral(true);
public static final JsBooleanLiteral FALSE = new JsBooleanLiteral(false);
public static JsBooleanLiteral getBoolean(boolean truth) {
return truth ? TRUE : FALSE;
}
public static final class JsThisRef extends JsValueLiteral {
private JsThisRef() {
super();
}
@Override
public void accept(JsVisitor v) {
v.visitThis(this);
}
@Override
public void traverse(JsVisitorWithContext v, JsContext ctx) {
v.visit(this, ctx);
v.endVisit(this, ctx);
}
}
public static final class JsBooleanLiteral extends JsValueLiteral {
private final boolean value;
// Should be interned by JsProgram
private JsBooleanLiteral(boolean value) {
this.value = value;
}
public boolean getValue() {
return value;
}
@Override
public void accept(JsVisitor v) {
v.visitBoolean(this);
}
@Override
public void traverse(JsVisitorWithContext v, JsContext ctx) {
v.visit(this, ctx);
v.endVisit(this, ctx);
}
}
/**
* A JavaScript string literal expression.
*/
@@ -78,4 +25,12 @@ public abstract class JsLiteral extends JsExpression {
return this;
}
}
public static boolean isTrueBoolean(@NotNull JsExpression expression) {
return expression instanceof JsBooleanLiteral && ((JsBooleanLiteral) expression).getValue();
}
public static boolean isFalseBoolean(@NotNull JsExpression expression) {
return expression instanceof JsBooleanLiteral && !((JsBooleanLiteral) expression).getValue();
}
}
@@ -4,8 +4,10 @@
package org.jetbrains.kotlin.js.backend.ast;
import org.jetbrains.annotations.NotNull;
public final class JsNullLiteral extends JsLiteral.JsValueLiteral {
JsNullLiteral() {
public JsNullLiteral() {
}
@Override
@@ -18,4 +20,10 @@ public final class JsNullLiteral extends JsLiteral.JsValueLiteral {
v.visit(this, ctx);
v.endVisit(this, ctx);
}
@NotNull
@Override
public JsNullLiteral deepCopy() {
return new JsNullLiteral().withMetadataFrom(this);
}
}
@@ -5,51 +5,4 @@
package org.jetbrains.kotlin.js.backend.ast;
public abstract class JsNumberLiteral extends JsLiteral.JsValueLiteral {
public static final JsIntLiteral ZERO = new JsIntLiteral(0);
public static final class JsDoubleLiteral extends JsNumberLiteral {
public final double value;
JsDoubleLiteral(double value) {
this.value = value;
}
@Override
public void accept(JsVisitor v) {
v.visitDouble(this);
}
public String toString() {
return String.valueOf(value);
}
@Override
public void traverse(JsVisitorWithContext v, JsContext ctx) {
v.visit(this, ctx);
v.endVisit(this, ctx);
}
}
public static final class JsIntLiteral extends JsNumberLiteral {
public final int value;
JsIntLiteral(int value) {
this.value = value;
}
@Override
public void accept(JsVisitor v) {
v.visitInt(this);
}
public String toString() {
return String.valueOf(value);
}
@Override
public void traverse(JsVisitorWithContext v, JsContext ctx) {
v.visit(this, ctx);
v.endVisit(this, ctx);
}
}
}
@@ -4,14 +4,7 @@
package org.jetbrains.kotlin.js.backend.ast;
import gnu.trove.TDoubleObjectHashMap;
import gnu.trove.THashMap;
import gnu.trove.TIntObjectHashMap;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.kotlin.js.backend.ast.JsNumberLiteral.JsDoubleLiteral;
import org.jetbrains.kotlin.js.backend.ast.JsNumberLiteral.JsIntLiteral;
import java.util.Map;
/**
* A JavaScript program.
@@ -31,14 +24,6 @@ public final class JsProgram extends SourceInfoAwareJsNode {
return globalBlock;
}
public JsNumberLiteral getNumberLiteral(double value) {
return new JsDoubleLiteral(value);
}
public JsNumberLiteral getNumberLiteral(int value) {
return new JsIntLiteral(value);
}
/**
* Gets the quasi-mythical root scope. This is not the same as the top scope;
* all unresolvable identifiers wind up here, because they are considered
@@ -56,14 +41,6 @@ public final class JsProgram extends SourceInfoAwareJsNode {
return topScope;
}
/**
* Creates or retrieves a JsStringLiteral from an interned object pool.
*/
@NotNull
public JsStringLiteral getStringLiteral(String value) {
return new JsStringLiteral(value);
}
@Override
public void accept(JsVisitor v) {
v.visitProgram(this);
@@ -4,24 +4,21 @@
package org.jetbrains.kotlin.js.backend.ast;
public final class JsStringLiteral extends JsLiteral.JsValueLiteral {
public static JsStringLiteral createCharZero() {
return new JsStringLiteral("\0");
}
import org.jetbrains.annotations.NotNull;
public final class JsStringLiteral extends JsLiteral.JsValueLiteral {
private final String value;
// These only get created by JsProgram so that they can be interned.
JsStringLiteral(String value) {
this.value = value;
}
public JsStringLiteral(String value) {
this.value = value;
}
public String getValue() {
return value;
}
@Override
public void accept(JsVisitor v) {
@Override
public void accept(JsVisitor v) {
v.visitString(this);
}
@@ -30,4 +27,10 @@ public final class JsStringLiteral extends JsLiteral.JsValueLiteral {
v.visit(this, ctx);
v.endVisit(this, ctx);
}
@NotNull
@Override
public JsStringLiteral deepCopy() {
return new JsStringLiteral(value).withMetadataFrom(this);
}
}
@@ -0,0 +1,38 @@
/*
* 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;
import org.jetbrains.annotations.NotNull;
public final class JsThisRef extends JsLiteral.JsValueLiteral {
@Override
public void accept(JsVisitor v) {
v.visitThis(this);
}
@Override
public void traverse(JsVisitorWithContext v, JsContext ctx) {
v.visit(this, ctx);
v.endVisit(this, ctx);
}
@NotNull
@Override
public JsThisRef deepCopy() {
return new JsThisRef().withMetadataFrom(this);
}
}
@@ -39,7 +39,7 @@ abstract class JsVisitor {
open fun visitBlock(x: JsBlock): Unit =
visitElement(x)
open fun visitBoolean(x: JsLiteral.JsBooleanLiteral): Unit =
open fun visitBoolean(x: JsBooleanLiteral): Unit =
visitElement(x)
open fun visitBreak(x: JsBreak): Unit =
@@ -99,10 +99,10 @@ abstract class JsVisitor {
open fun visitNull(x: JsNullLiteral): Unit =
visitElement(x)
open fun visitInt(x: JsNumberLiteral.JsIntLiteral): Unit =
open fun visitInt(x: JsIntLiteral): Unit =
visitElement(x)
open fun visitDouble(x: JsNumberLiteral.JsDoubleLiteral): Unit =
open fun visitDouble(x: JsDoubleLiteral): Unit =
visitElement(x)
open fun visitObjectLiteral(x: JsObjectLiteral): Unit =
@@ -135,7 +135,7 @@ abstract class JsVisitor {
open fun visit(x: JsSwitch): Unit =
visitElement(x)
open fun visitThis(x: JsLiteral.JsThisRef): Unit =
open fun visitThis(x: JsThisRef): Unit =
visitElement(x)
open fun visitThrow(x: JsThrow): Unit =
@@ -71,7 +71,7 @@ public abstract class JsVisitorWithContext {
public void endVisit(@NotNull JsBlock x, @NotNull JsContext ctx) {
}
public void endVisit(@NotNull JsLiteral.JsBooleanLiteral x, @NotNull JsContext ctx) {
public void endVisit(@NotNull JsBooleanLiteral x, @NotNull JsContext ctx) {
}
public void endVisit(@NotNull JsBreak x, @NotNull JsContext ctx) {
@@ -167,7 +167,7 @@ public abstract class JsVisitorWithContext {
public void endVisit(@NotNull JsSwitch x, @NotNull JsContext ctx) {
}
public void endVisit(@NotNull JsLiteral.JsThisRef x, @NotNull JsContext ctx) {
public void endVisit(@NotNull JsThisRef x, @NotNull JsContext ctx) {
}
public void endVisit(@NotNull JsThrow x, @NotNull JsContext ctx) {
@@ -201,7 +201,7 @@ public abstract class JsVisitorWithContext {
return true;
}
public boolean visit(@NotNull JsLiteral.JsBooleanLiteral x, @NotNull JsContext ctx) {
public boolean visit(@NotNull JsBooleanLiteral x, @NotNull JsContext ctx) {
return true;
}
@@ -329,7 +329,7 @@ public abstract class JsVisitorWithContext {
return true;
}
public boolean visit(@NotNull JsLiteral.JsThisRef x, @NotNull JsContext ctx) {
public boolean visit(@NotNull JsThisRef x, @NotNull JsContext ctx) {
return true;
}