24 */ |
24 */ |
25 |
25 |
26 package jdk.nashorn.internal.ir; |
26 package jdk.nashorn.internal.ir; |
27 |
27 |
28 import java.util.IdentityHashMap; |
28 import java.util.IdentityHashMap; |
29 import java.util.List; |
29 |
30 import jdk.nashorn.internal.codegen.types.Type; |
30 import jdk.nashorn.internal.codegen.types.Type; |
31 import jdk.nashorn.internal.ir.visitor.NodeVisitor; |
31 import jdk.nashorn.internal.ir.visitor.NodeVisitor; |
32 import jdk.nashorn.internal.parser.Token; |
32 import jdk.nashorn.internal.parser.Token; |
33 import jdk.nashorn.internal.runtime.Source; |
33 import jdk.nashorn.internal.runtime.Source; |
34 |
34 |
36 * Nodes are used to compose Abstract Syntax Trees. |
36 * Nodes are used to compose Abstract Syntax Trees. |
37 * |
37 * |
38 */ |
38 */ |
39 public abstract class Node extends Location { |
39 public abstract class Node extends Location { |
40 /** Node symbol. */ |
40 /** Node symbol. */ |
41 private Symbol nodeSymbol; |
41 private Symbol symbol; |
42 |
42 |
43 /** Start of source range. */ |
43 /** Start of source range. */ |
44 protected int start; |
44 protected int start; |
45 |
45 |
46 /** End of source range. */ |
46 /** End of source range. */ |
78 * @param node source node |
78 * @param node source node |
79 */ |
79 */ |
80 protected Node(final Node node) { |
80 protected Node(final Node node) { |
81 super(node); |
81 super(node); |
82 |
82 |
83 this.nodeSymbol = node.nodeSymbol; |
83 this.symbol = node.symbol; |
84 this.isResolved = node.isResolved; |
84 this.isResolved = node.isResolved; |
85 this.isTerminal = node.isTerminal; |
85 this.isTerminal = node.isTerminal; |
86 this.hasGoto = node.hasGoto; |
86 this.hasGoto = node.hasGoto; |
87 this.shouldDiscard = node.shouldDiscard; |
87 this.shouldDiscard = node.shouldDiscard; |
88 this.start = node.start; |
88 this.start = node.start; |
105 * are stored in the node itself, unless it implements TypeOverride |
105 * are stored in the node itself, unless it implements TypeOverride |
106 * |
106 * |
107 * @return the type of the node. |
107 * @return the type of the node. |
108 */ |
108 */ |
109 public Type getType() { |
109 public Type getType() { |
110 assert hasType(); |
110 assert hasType() : this + " has no type"; |
111 return nodeSymbol.getSymbolType(); |
111 return symbol.getSymbolType(); |
112 } |
112 } |
113 |
113 |
114 /** |
114 /** |
115 * Is this an atom node - for example a literal or an identity |
115 * Is this an atom node - for example a literal or an identity |
116 * |
116 * |
377 * is the place where it's expression value is stored after evaluation |
377 * is the place where it's expression value is stored after evaluation |
378 * |
378 * |
379 * @return the symbol |
379 * @return the symbol |
380 */ |
380 */ |
381 public Symbol getSymbol() { |
381 public Symbol getSymbol() { |
382 return nodeSymbol; |
382 return symbol; |
383 } |
383 } |
384 |
384 |
385 /** |
385 /** |
386 * Assign a symbol to this node. See {@link Node#getSymbol()} for explanation |
386 * Assign a symbol to this node. See {@link Node#getSymbol()} for explanation |
387 * of what a symbol is |
387 * of what a symbol is |
388 * |
388 * |
389 * @param symbol the symbol |
389 * @param symbol the symbol |
390 */ |
390 */ |
391 public void setSymbol(final Symbol symbol) { |
391 public void setSymbol(final Symbol symbol) { |
392 nodeSymbol = symbol; |
392 this.symbol = symbol; |
393 } |
393 } |
394 |
394 |
395 /** |
395 /** |
396 * Is this a terminal Node, i.e. does it end control flow like a throw or return |
396 * Is this a terminal Node, i.e. does it end control flow like a throw or return |
397 * expression does? |
397 * expression does? |
410 */ |
410 */ |
411 public void setIsTerminal(final boolean isTerminal) { |
411 public void setIsTerminal(final boolean isTerminal) { |
412 this.isTerminal = isTerminal; |
412 this.isTerminal = isTerminal; |
413 } |
413 } |
414 |
414 |
415 /** |
|
416 * Return last node in a statement list. |
|
417 * |
|
418 * @param statements Statement list. |
|
419 * |
|
420 * @return Last (non-debug) statement or null if empty block. |
|
421 */ |
|
422 public static Node lastStatement(final List<Node> statements) { |
|
423 for (int lastIndex = statements.size() - 1; lastIndex >= 0; lastIndex--) { |
|
424 final Node node = statements.get(lastIndex); |
|
425 if (!node.isDebug()) { |
|
426 return node; |
|
427 } |
|
428 } |
|
429 |
|
430 return null; |
|
431 } |
|
432 } |
415 } |