26 package jdk.nashorn.internal.ir; |
26 package jdk.nashorn.internal.ir; |
27 |
27 |
28 import java.util.ArrayList; |
28 import java.util.ArrayList; |
29 import java.util.Collections; |
29 import java.util.Collections; |
30 import java.util.List; |
30 import java.util.List; |
31 |
|
32 import jdk.nashorn.internal.codegen.Label; |
31 import jdk.nashorn.internal.codegen.Label; |
33 import jdk.nashorn.internal.ir.annotations.Immutable; |
32 import jdk.nashorn.internal.ir.annotations.Immutable; |
34 import jdk.nashorn.internal.ir.visitor.NodeVisitor; |
33 import jdk.nashorn.internal.ir.visitor.NodeVisitor; |
35 |
34 |
36 /** |
35 /** |
37 * IR representation of a SWITCH statement. |
36 * IR representation of a SWITCH statement. |
38 */ |
37 */ |
39 @Immutable |
38 @Immutable |
40 public final class SwitchNode extends BreakableNode { |
39 public final class SwitchNode extends BreakableStatement { |
41 /** Switch expression. */ |
40 /** Switch expression. */ |
42 private final Node expression; |
41 private final Expression expression; |
43 |
42 |
44 /** Switch cases. */ |
43 /** Switch cases. */ |
45 private final List<CaseNode> cases; |
44 private final List<CaseNode> cases; |
46 |
45 |
47 /** Switch default index. */ |
46 /** Switch default index. */ |
58 * @param finish finish |
57 * @param finish finish |
59 * @param expression switch expression |
58 * @param expression switch expression |
60 * @param cases cases |
59 * @param cases cases |
61 * @param defaultCase the default case node - null if none, otherwise has to be present in cases list |
60 * @param defaultCase the default case node - null if none, otherwise has to be present in cases list |
62 */ |
61 */ |
63 public SwitchNode(final int lineNumber, final long token, final int finish, final Node expression, final List<CaseNode> cases, final CaseNode defaultCase) { |
62 public SwitchNode(final int lineNumber, final long token, final int finish, final Expression expression, final List<CaseNode> cases, final CaseNode defaultCase) { |
64 super(lineNumber, token, finish, new Label("switch_break")); |
63 super(lineNumber, token, finish, new Label("switch_break")); |
65 this.expression = expression; |
64 this.expression = expression; |
66 this.cases = cases; |
65 this.cases = cases; |
67 this.defaultCaseIndex = defaultCase == null ? -1 : cases.indexOf(defaultCase); |
66 this.defaultCaseIndex = defaultCase == null ? -1 : cases.indexOf(defaultCase); |
68 } |
67 } |
69 |
68 |
70 private SwitchNode(final SwitchNode switchNode, final Node expression, final List<CaseNode> cases, final int defaultCase) { |
69 private SwitchNode(final SwitchNode switchNode, final Expression expression, final List<CaseNode> cases, final int defaultCase) { |
71 super(switchNode); |
70 super(switchNode); |
72 this.expression = expression; |
71 this.expression = expression; |
73 this.cases = cases; |
72 this.cases = cases; |
74 this.defaultCaseIndex = defaultCase; |
73 this.defaultCaseIndex = defaultCase; |
75 this.tag = switchNode.getTag(); //TODO are symbols inhereted as references? |
74 this.tag = switchNode.getTag(); //TODO are symbols inhereted as references? |
101 |
100 |
102 @Override |
101 @Override |
103 public Node accept(final LexicalContext lc, final NodeVisitor<? extends LexicalContext> visitor) { |
102 public Node accept(final LexicalContext lc, final NodeVisitor<? extends LexicalContext> visitor) { |
104 if (visitor.enterSwitchNode(this)) { |
103 if (visitor.enterSwitchNode(this)) { |
105 return visitor.leaveSwitchNode( |
104 return visitor.leaveSwitchNode( |
106 setExpression(lc, expression.accept(visitor)). |
105 setExpression(lc, (Expression)expression.accept(visitor)). |
107 setCases(lc, Node.accept(visitor, CaseNode.class, cases), defaultCaseIndex)); |
106 setCases(lc, Node.accept(visitor, CaseNode.class, cases), defaultCaseIndex)); |
108 } |
107 } |
109 |
108 |
110 return this; |
109 return this; |
111 } |
110 } |
165 |
164 |
166 /** |
165 /** |
167 * Return the expression to switch on |
166 * Return the expression to switch on |
168 * @return switch expression |
167 * @return switch expression |
169 */ |
168 */ |
170 public Node getExpression() { |
169 public Expression getExpression() { |
171 return expression; |
170 return expression; |
172 } |
171 } |
173 |
172 |
174 /** |
173 /** |
175 * Set or reset the expression to switch on |
174 * Set or reset the expression to switch on |
176 * @param lc lexical context |
175 * @param lc lexical context |
177 * @param expression switch expression |
176 * @param expression switch expression |
178 * @return new switch node or same if no state was changed |
177 * @return new switch node or same if no state was changed |
179 */ |
178 */ |
180 public SwitchNode setExpression(final LexicalContext lc, final Node expression) { |
179 public SwitchNode setExpression(final LexicalContext lc, final Expression expression) { |
181 if (this.expression == expression) { |
180 if (this.expression == expression) { |
182 return this; |
181 return this; |
183 } |
182 } |
184 return Node.replaceInLexicalContext(lc, this, new SwitchNode(this, expression, cases, defaultCaseIndex)); |
183 return Node.replaceInLexicalContext(lc, this, new SwitchNode(this, expression, cases, defaultCaseIndex)); |
185 } |
184 } |