author | hannesw |
Fri, 25 Oct 2013 10:20:49 +0200 | |
changeset 21457 | 381acbd07fe5 |
parent 18867 | bc91e3fcc5ba |
child 24751 | ccbd9cd3f720 |
permissions | -rw-r--r-- |
16147 | 1 |
/* |
16151 | 2 |
* Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. |
16147 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Oracle designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
24 |
*/ |
|
25 |
||
26 |
package jdk.nashorn.internal.ir; |
|
27 |
||
17233 | 28 |
import jdk.nashorn.internal.ir.annotations.Immutable; |
16147 | 29 |
import jdk.nashorn.internal.ir.visitor.NodeVisitor; |
30 |
||
31 |
/** |
|
32 |
* TernaryNode nodes represent three operand operations (?:). |
|
33 |
*/ |
|
17233 | 34 |
@Immutable |
18867
bc91e3fcc5ba
8013925: Remove symbol fields from nodes that don't need them
attila
parents:
17769
diff
changeset
|
35 |
public final class TernaryNode extends Expression { |
bc91e3fcc5ba
8013925: Remove symbol fields from nodes that don't need them
attila
parents:
17769
diff
changeset
|
36 |
private final Expression test; |
17233 | 37 |
|
18867
bc91e3fcc5ba
8013925: Remove symbol fields from nodes that don't need them
attila
parents:
17769
diff
changeset
|
38 |
private final Expression trueExpr; |
17233 | 39 |
|
16147 | 40 |
/** Third argument. */ |
18867
bc91e3fcc5ba
8013925: Remove symbol fields from nodes that don't need them
attila
parents:
17769
diff
changeset
|
41 |
private final Expression falseExpr; |
16147 | 42 |
|
43 |
/** |
|
44 |
* Constructor |
|
45 |
* |
|
18867
bc91e3fcc5ba
8013925: Remove symbol fields from nodes that don't need them
attila
parents:
17769
diff
changeset
|
46 |
* @param token token |
bc91e3fcc5ba
8013925: Remove symbol fields from nodes that don't need them
attila
parents:
17769
diff
changeset
|
47 |
* @param test test expression |
bc91e3fcc5ba
8013925: Remove symbol fields from nodes that don't need them
attila
parents:
17769
diff
changeset
|
48 |
* @param trueExpr expression evaluated when test evaluates to true |
bc91e3fcc5ba
8013925: Remove symbol fields from nodes that don't need them
attila
parents:
17769
diff
changeset
|
49 |
* @param falseExpr expression evaluated when test evaluates to true |
16147 | 50 |
*/ |
18867
bc91e3fcc5ba
8013925: Remove symbol fields from nodes that don't need them
attila
parents:
17769
diff
changeset
|
51 |
public TernaryNode(final long token, final Expression test, final Expression trueExpr, final Expression falseExpr) { |
bc91e3fcc5ba
8013925: Remove symbol fields from nodes that don't need them
attila
parents:
17769
diff
changeset
|
52 |
super(token, falseExpr.getFinish()); |
bc91e3fcc5ba
8013925: Remove symbol fields from nodes that don't need them
attila
parents:
17769
diff
changeset
|
53 |
this.test = test; |
bc91e3fcc5ba
8013925: Remove symbol fields from nodes that don't need them
attila
parents:
17769
diff
changeset
|
54 |
this.trueExpr = trueExpr; |
bc91e3fcc5ba
8013925: Remove symbol fields from nodes that don't need them
attila
parents:
17769
diff
changeset
|
55 |
this.falseExpr = falseExpr; |
16147 | 56 |
} |
57 |
||
18867
bc91e3fcc5ba
8013925: Remove symbol fields from nodes that don't need them
attila
parents:
17769
diff
changeset
|
58 |
private TernaryNode(final TernaryNode ternaryNode, final Expression test, final Expression trueExpr, final Expression falseExpr) { |
17233 | 59 |
super(ternaryNode); |
18867
bc91e3fcc5ba
8013925: Remove symbol fields from nodes that don't need them
attila
parents:
17769
diff
changeset
|
60 |
this.test = test; |
bc91e3fcc5ba
8013925: Remove symbol fields from nodes that don't need them
attila
parents:
17769
diff
changeset
|
61 |
this.trueExpr = trueExpr; |
bc91e3fcc5ba
8013925: Remove symbol fields from nodes that don't need them
attila
parents:
17769
diff
changeset
|
62 |
this.falseExpr = falseExpr; |
16147 | 63 |
} |
64 |
||
65 |
@Override |
|
17769
14ea7feaf658
8012522: Clean up lexical contexts - split out stack based functionality in CodeGenerator and generify NodeVisitors based on their LexicalContext type to avoid casts
lagergren
parents:
17523
diff
changeset
|
66 |
public Node accept(final NodeVisitor<? extends LexicalContext> visitor) { |
17233 | 67 |
if (visitor.enterTernaryNode(this)) { |
18867
bc91e3fcc5ba
8013925: Remove symbol fields from nodes that don't need them
attila
parents:
17769
diff
changeset
|
68 |
final Expression newTest = (Expression)getTest().accept(visitor); |
bc91e3fcc5ba
8013925: Remove symbol fields from nodes that don't need them
attila
parents:
17769
diff
changeset
|
69 |
final Expression newTrueExpr = (Expression)getTrueExpression().accept(visitor); |
bc91e3fcc5ba
8013925: Remove symbol fields from nodes that don't need them
attila
parents:
17769
diff
changeset
|
70 |
final Expression newFalseExpr = (Expression)falseExpr.accept(visitor); |
bc91e3fcc5ba
8013925: Remove symbol fields from nodes that don't need them
attila
parents:
17769
diff
changeset
|
71 |
return visitor.leaveTernaryNode(setTest(newTest).setTrueExpression(newTrueExpr).setFalseExpression1(newFalseExpr)); |
16147 | 72 |
} |
73 |
||
74 |
return this; |
|
75 |
} |
|
76 |
||
77 |
@Override |
|
78 |
public void toString(final StringBuilder sb) { |
|
18867
bc91e3fcc5ba
8013925: Remove symbol fields from nodes that don't need them
attila
parents:
17769
diff
changeset
|
79 |
final boolean testParen = tokenType().needsParens(getTest().tokenType(), true); |
bc91e3fcc5ba
8013925: Remove symbol fields from nodes that don't need them
attila
parents:
17769
diff
changeset
|
80 |
final boolean trueParen = tokenType().needsParens(getTrueExpression().tokenType(), false); |
bc91e3fcc5ba
8013925: Remove symbol fields from nodes that don't need them
attila
parents:
17769
diff
changeset
|
81 |
final boolean falseParen = tokenType().needsParens(getFalseExpression().tokenType(), false); |
16147 | 82 |
|
18867
bc91e3fcc5ba
8013925: Remove symbol fields from nodes that don't need them
attila
parents:
17769
diff
changeset
|
83 |
if (testParen) { |
16147 | 84 |
sb.append('('); |
85 |
} |
|
18867
bc91e3fcc5ba
8013925: Remove symbol fields from nodes that don't need them
attila
parents:
17769
diff
changeset
|
86 |
getTest().toString(sb); |
bc91e3fcc5ba
8013925: Remove symbol fields from nodes that don't need them
attila
parents:
17769
diff
changeset
|
87 |
if (testParen) { |
16147 | 88 |
sb.append(')'); |
89 |
} |
|
90 |
||
91 |
sb.append(" ? "); |
|
92 |
||
18867
bc91e3fcc5ba
8013925: Remove symbol fields from nodes that don't need them
attila
parents:
17769
diff
changeset
|
93 |
if (trueParen) { |
16147 | 94 |
sb.append('('); |
95 |
} |
|
18867
bc91e3fcc5ba
8013925: Remove symbol fields from nodes that don't need them
attila
parents:
17769
diff
changeset
|
96 |
getTrueExpression().toString(sb); |
bc91e3fcc5ba
8013925: Remove symbol fields from nodes that don't need them
attila
parents:
17769
diff
changeset
|
97 |
if (trueParen) { |
16147 | 98 |
sb.append(')'); |
99 |
} |
|
100 |
||
101 |
sb.append(" : "); |
|
102 |
||
18867
bc91e3fcc5ba
8013925: Remove symbol fields from nodes that don't need them
attila
parents:
17769
diff
changeset
|
103 |
if (falseParen) { |
16147 | 104 |
sb.append('('); |
105 |
} |
|
18867
bc91e3fcc5ba
8013925: Remove symbol fields from nodes that don't need them
attila
parents:
17769
diff
changeset
|
106 |
getFalseExpression().toString(sb); |
bc91e3fcc5ba
8013925: Remove symbol fields from nodes that don't need them
attila
parents:
17769
diff
changeset
|
107 |
if (falseParen) { |
16147 | 108 |
sb.append(')'); |
109 |
} |
|
110 |
} |
|
111 |
||
21457
381acbd07fe5
8027042: Evaluation order for binary operators can be improved
hannesw
parents:
18867
diff
changeset
|
112 |
@Override |
381acbd07fe5
8027042: Evaluation order for binary operators can be improved
hannesw
parents:
18867
diff
changeset
|
113 |
public boolean isLocal() { |
381acbd07fe5
8027042: Evaluation order for binary operators can be improved
hannesw
parents:
18867
diff
changeset
|
114 |
return getTest().isLocal() |
381acbd07fe5
8027042: Evaluation order for binary operators can be improved
hannesw
parents:
18867
diff
changeset
|
115 |
&& getTrueExpression().isLocal() |
381acbd07fe5
8027042: Evaluation order for binary operators can be improved
hannesw
parents:
18867
diff
changeset
|
116 |
&& getFalseExpression().isLocal(); |
381acbd07fe5
8027042: Evaluation order for binary operators can be improved
hannesw
parents:
18867
diff
changeset
|
117 |
} |
381acbd07fe5
8027042: Evaluation order for binary operators can be improved
hannesw
parents:
18867
diff
changeset
|
118 |
|
16147 | 119 |
/** |
18867
bc91e3fcc5ba
8013925: Remove symbol fields from nodes that don't need them
attila
parents:
17769
diff
changeset
|
120 |
* Get the test expression for this ternary expression, i.e. "x" in x ? y : z |
bc91e3fcc5ba
8013925: Remove symbol fields from nodes that don't need them
attila
parents:
17769
diff
changeset
|
121 |
* @return the test expression |
17233 | 122 |
*/ |
18867
bc91e3fcc5ba
8013925: Remove symbol fields from nodes that don't need them
attila
parents:
17769
diff
changeset
|
123 |
public Expression getTest() { |
bc91e3fcc5ba
8013925: Remove symbol fields from nodes that don't need them
attila
parents:
17769
diff
changeset
|
124 |
return test; |
17233 | 125 |
} |
126 |
||
127 |
/** |
|
18867
bc91e3fcc5ba
8013925: Remove symbol fields from nodes that don't need them
attila
parents:
17769
diff
changeset
|
128 |
* Get the true expression for this ternary expression, i.e. "y" in x ? y : z |
bc91e3fcc5ba
8013925: Remove symbol fields from nodes that don't need them
attila
parents:
17769
diff
changeset
|
129 |
* @return the true expression |
17233 | 130 |
*/ |
18867
bc91e3fcc5ba
8013925: Remove symbol fields from nodes that don't need them
attila
parents:
17769
diff
changeset
|
131 |
public Expression getTrueExpression() { |
bc91e3fcc5ba
8013925: Remove symbol fields from nodes that don't need them
attila
parents:
17769
diff
changeset
|
132 |
return trueExpr; |
17233 | 133 |
} |
134 |
||
135 |
/** |
|
18867
bc91e3fcc5ba
8013925: Remove symbol fields from nodes that don't need them
attila
parents:
17769
diff
changeset
|
136 |
* Get the false expression for this ternary expression, i.e. "z" in x ? y : z |
bc91e3fcc5ba
8013925: Remove symbol fields from nodes that don't need them
attila
parents:
17769
diff
changeset
|
137 |
* @return the false expression |
16147 | 138 |
*/ |
18867
bc91e3fcc5ba
8013925: Remove symbol fields from nodes that don't need them
attila
parents:
17769
diff
changeset
|
139 |
public Expression getFalseExpression() { |
bc91e3fcc5ba
8013925: Remove symbol fields from nodes that don't need them
attila
parents:
17769
diff
changeset
|
140 |
return falseExpr; |
16147 | 141 |
} |
142 |
||
143 |
/** |
|
18867
bc91e3fcc5ba
8013925: Remove symbol fields from nodes that don't need them
attila
parents:
17769
diff
changeset
|
144 |
* Set the test expression for this node |
bc91e3fcc5ba
8013925: Remove symbol fields from nodes that don't need them
attila
parents:
17769
diff
changeset
|
145 |
* @param test new test expression |
17233 | 146 |
* @return a node equivalent to this one except for the requested change. |
147 |
*/ |
|
18867
bc91e3fcc5ba
8013925: Remove symbol fields from nodes that don't need them
attila
parents:
17769
diff
changeset
|
148 |
public TernaryNode setTest(final Expression test) { |
bc91e3fcc5ba
8013925: Remove symbol fields from nodes that don't need them
attila
parents:
17769
diff
changeset
|
149 |
if (this.test == test) { |
17233 | 150 |
return this; |
151 |
} |
|
18867
bc91e3fcc5ba
8013925: Remove symbol fields from nodes that don't need them
attila
parents:
17769
diff
changeset
|
152 |
return new TernaryNode(this, test, trueExpr, falseExpr); |
17233 | 153 |
} |
154 |
||
155 |
/** |
|
18867
bc91e3fcc5ba
8013925: Remove symbol fields from nodes that don't need them
attila
parents:
17769
diff
changeset
|
156 |
* Set the true expression for this node |
bc91e3fcc5ba
8013925: Remove symbol fields from nodes that don't need them
attila
parents:
17769
diff
changeset
|
157 |
* @param trueExpr new true expression |
17233 | 158 |
* @return a node equivalent to this one except for the requested change. |
159 |
*/ |
|
18867
bc91e3fcc5ba
8013925: Remove symbol fields from nodes that don't need them
attila
parents:
17769
diff
changeset
|
160 |
public TernaryNode setTrueExpression(final Expression trueExpr) { |
bc91e3fcc5ba
8013925: Remove symbol fields from nodes that don't need them
attila
parents:
17769
diff
changeset
|
161 |
if (this.trueExpr == trueExpr) { |
17233 | 162 |
return this; |
163 |
} |
|
18867
bc91e3fcc5ba
8013925: Remove symbol fields from nodes that don't need them
attila
parents:
17769
diff
changeset
|
164 |
return new TernaryNode(this, test, trueExpr, falseExpr); |
17233 | 165 |
} |
166 |
||
167 |
/** |
|
18867
bc91e3fcc5ba
8013925: Remove symbol fields from nodes that don't need them
attila
parents:
17769
diff
changeset
|
168 |
* Set the false expression for this node |
bc91e3fcc5ba
8013925: Remove symbol fields from nodes that don't need them
attila
parents:
17769
diff
changeset
|
169 |
* @param falseExpr new false expression |
16530
201d682e75f4
8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents:
16252
diff
changeset
|
170 |
* @return a node equivalent to this one except for the requested change. |
16147 | 171 |
*/ |
18867
bc91e3fcc5ba
8013925: Remove symbol fields from nodes that don't need them
attila
parents:
17769
diff
changeset
|
172 |
public TernaryNode setFalseExpression1(final Expression falseExpr) { |
bc91e3fcc5ba
8013925: Remove symbol fields from nodes that don't need them
attila
parents:
17769
diff
changeset
|
173 |
if (this.falseExpr == falseExpr) { |
17233 | 174 |
return this; |
175 |
} |
|
18867
bc91e3fcc5ba
8013925: Remove symbol fields from nodes that don't need them
attila
parents:
17769
diff
changeset
|
176 |
return new TernaryNode(this, test, trueExpr, falseExpr); |
16147 | 177 |
} |
178 |
} |