aboutsummaryrefslogtreecommitdiff
path: root/src/jdk/nashorn/internal/ir/Node.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/jdk/nashorn/internal/ir/Node.java')
-rw-r--r--src/jdk/nashorn/internal/ir/Node.java110
1 files changed, 85 insertions, 25 deletions
diff --git a/src/jdk/nashorn/internal/ir/Node.java b/src/jdk/nashorn/internal/ir/Node.java
index dfed903d..0fb95cc3 100644
--- a/src/jdk/nashorn/internal/ir/Node.java
+++ b/src/jdk/nashorn/internal/ir/Node.java
@@ -27,16 +27,15 @@ package jdk.nashorn.internal.ir;
import java.util.ArrayList;
import java.util.List;
-
import jdk.nashorn.internal.codegen.types.Type;
import jdk.nashorn.internal.ir.visitor.NodeVisitor;
import jdk.nashorn.internal.parser.Token;
-import jdk.nashorn.internal.runtime.Source;
+import jdk.nashorn.internal.parser.TokenType;
/**
* Nodes are used to compose Abstract Syntax Trees.
*/
-public abstract class Node extends Location {
+public abstract class Node implements Cloneable {
/** Node symbol. */
private Symbol symbol;
@@ -46,16 +45,17 @@ public abstract class Node extends Location {
/** End of source range. */
protected int finish;
+ /** Token descriptor. */
+ private final long token;
+
/**
* Constructor
*
- * @param source the source
* @param token token
* @param finish finish
*/
- public Node(final Source source, final long token, final int finish) {
- super(source, token);
-
+ public Node(final long token, final int finish) {
+ this.token = token;
this.start = Token.descPosition(token);
this.finish = finish;
}
@@ -63,16 +63,14 @@ public abstract class Node extends Location {
/**
* Constructor
*
- * @param source source
* @param token token
* @param start start
* @param finish finish
*/
- protected Node(final Source source, final long token, final int start, final int finish) {
- super(source, token);
-
+ protected Node(final long token, final int start, final int finish) {
this.start = start;
this.finish = finish;
+ this.token = token;
}
/**
@@ -81,8 +79,7 @@ public abstract class Node extends Location {
* @param node source node
*/
protected Node(final Node node) {
- super(node);
-
+ this.token = node.token;
this.symbol = node.symbol;
this.start = node.start;
this.finish = node.finish;
@@ -156,15 +153,6 @@ public abstract class Node extends Location {
}
/**
- * Is this a debug info node like LineNumberNode etc?
- *
- * @return true if this is a debug node
- */
- public boolean isDebug() {
- return false;
- }
-
- /**
* For reference copies - ensure that labels in the copy node are unique
* using an appropriate copy constructor
* @param lc lexical context
@@ -248,14 +236,86 @@ public abstract class Node extends Location {
return symbol;
}
+ @Override
+ protected Object clone() {
+ try {
+ return super.clone();
+ } catch (final CloneNotSupportedException e) {
+ throw new AssertionError(e);
+ }
+ }
+
/**
* Assign a symbol to this node. See {@link Node#getSymbol()} for explanation
* of what a symbol is
*
+ * @param lc lexical context
* @param symbol the symbol
+ * @return new node
+ */
+ public Node setSymbol(final LexicalContext lc, final Symbol symbol) {
+ if (this.symbol == symbol) {
+ return this;
+ }
+ final Node newNode = (Node)clone();
+ newNode.symbol = symbol;
+ return newNode;
+ }
+
+
+ @Override
+ public final boolean equals(final Object other) {
+ return super.equals(other);
+ }
+
+ @Override
+ public final int hashCode() {
+ return super.hashCode();
+ }
+
+ /**
+ * Return token position from a token descriptor.
+ *
+ * @return Start position of the token in the source.
+ */
+ public int position() {
+ return Token.descPosition(token);
+ }
+
+ /**
+ * Return token length from a token descriptor.
+ *
+ * @return Length of the token.
+ */
+ public int length() {
+ return Token.descLength(token);
+ }
+
+ /**
+ * Return token tokenType from a token descriptor.
+ *
+ * @return Type of token.
+ */
+ public TokenType tokenType() {
+ return Token.descType(token);
+ }
+
+ /**
+ * Test token tokenType.
+ *
+ * @param type a type to check this token against
+ * @return true if token types match.
+ */
+ public boolean isTokenType(final TokenType type) {
+ return Token.descType(token) == type;
+ }
+
+ /**
+ * Get the token for this location
+ * @return the token
*/
- public void setSymbol(final Symbol symbol) {
- this.symbol = symbol;
+ public long getToken() {
+ return token;
}
/**
@@ -274,7 +334,7 @@ public abstract class Node extends Location {
final List<T> newList = new ArrayList<>();
for (final Node node : list) {
- final T newNode = clazz.cast(node.accept(visitor));
+ final T newNode = node == null ? null : clazz.cast(node.accept(visitor));
if (newNode != node) {
changed = true;
}