author | lagergren |
Fri, 19 Apr 2013 16:11:16 +0200 | |
changeset 17233 | 72ccf78a8216 |
parent 16530 | 201d682e75f4 |
child 17523 | cb4a7c901e0d |
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 |
||
28 |
import jdk.nashorn.internal.codegen.types.Type; |
|
17233 | 29 |
import jdk.nashorn.internal.ir.annotations.Immutable; |
16147 | 30 |
import jdk.nashorn.internal.ir.visitor.NodeVisitor; |
31 |
import jdk.nashorn.internal.parser.TokenType; |
|
32 |
import jdk.nashorn.internal.runtime.Source; |
|
33 |
||
34 |
/** |
|
35 |
* BinaryNode nodes represent two operand operations. |
|
36 |
*/ |
|
17233 | 37 |
@Immutable |
38 |
public final class BinaryNode extends Node implements Assignment<Node> { |
|
16147 | 39 |
/** Left hand side argument. */ |
17233 | 40 |
private final Node lhs; |
41 |
||
42 |
private final Node rhs; |
|
16147 | 43 |
|
44 |
/** |
|
45 |
* Constructor |
|
46 |
* |
|
47 |
* @param source source code |
|
48 |
* @param token token |
|
49 |
* @param lhs left hand side |
|
50 |
* @param rhs right hand side |
|
51 |
*/ |
|
52 |
public BinaryNode(final Source source, final long token, final Node lhs, final Node rhs) { |
|
17233 | 53 |
super(source, token, lhs.getStart(), rhs.getFinish()); |
54 |
this.lhs = lhs; |
|
55 |
this.rhs = rhs; |
|
16147 | 56 |
} |
57 |
||
17233 | 58 |
private BinaryNode(final BinaryNode binaryNode, final Node lhs, final Node rhs) { |
59 |
super(binaryNode); |
|
60 |
this.lhs = lhs; |
|
61 |
this.rhs = rhs; |
|
16147 | 62 |
} |
63 |
||
64 |
/** |
|
65 |
* Return the widest possible type for this operation. This is used for compile time |
|
66 |
* static type inference |
|
67 |
* |
|
68 |
* @return Type |
|
69 |
*/ |
|
70 |
@Override |
|
71 |
public Type getWidestOperationType() { |
|
72 |
switch (tokenType()) { |
|
73 |
case SHR: |
|
74 |
case ASSIGN_SHR: |
|
75 |
return Type.LONG; |
|
76 |
case ASSIGN_SAR: |
|
77 |
case ASSIGN_SHL: |
|
78 |
case ASSIGN_BIT_AND: |
|
79 |
case ASSIGN_BIT_OR: |
|
80 |
case ASSIGN_BIT_XOR: |
|
81 |
case SAR: |
|
82 |
case SHL: |
|
83 |
return Type.INT; |
|
84 |
case DIV: |
|
85 |
case MOD: |
|
86 |
case MUL: |
|
87 |
case ASSIGN_DIV: |
|
88 |
case ASSIGN_MOD: |
|
89 |
case ASSIGN_MUL: |
|
90 |
case ASSIGN_SUB: |
|
91 |
return Type.NUMBER; |
|
92 |
default: |
|
93 |
return Type.OBJECT; |
|
94 |
} |
|
95 |
} |
|
96 |
||
97 |
/** |
|
98 |
* Check if this node is an assigment |
|
99 |
* |
|
100 |
* @return true if this node assigns a value |
|
101 |
*/ |
|
102 |
@Override |
|
103 |
public boolean isAssignment() { |
|
104 |
switch (tokenType()) { |
|
105 |
case ASSIGN: |
|
106 |
case ASSIGN_ADD: |
|
107 |
case ASSIGN_BIT_AND: |
|
108 |
case ASSIGN_BIT_OR: |
|
109 |
case ASSIGN_BIT_XOR: |
|
110 |
case ASSIGN_DIV: |
|
111 |
case ASSIGN_MOD: |
|
112 |
case ASSIGN_MUL: |
|
113 |
case ASSIGN_SAR: |
|
114 |
case ASSIGN_SHL: |
|
115 |
case ASSIGN_SHR: |
|
116 |
case ASSIGN_SUB: |
|
117 |
return true; |
|
118 |
default: |
|
119 |
return false; |
|
120 |
} |
|
121 |
} |
|
122 |
||
123 |
@Override |
|
124 |
public boolean isSelfModifying() { |
|
125 |
return isAssignment() && tokenType() != TokenType.ASSIGN; |
|
126 |
} |
|
127 |
||
128 |
@Override |
|
129 |
public Node getAssignmentDest() { |
|
130 |
return isAssignment() ? lhs() : null; |
|
131 |
} |
|
132 |
||
133 |
@Override |
|
16530
201d682e75f4
8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents:
16269
diff
changeset
|
134 |
public Node setAssignmentDest(Node n) { |
201d682e75f4
8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents:
16269
diff
changeset
|
135 |
return setLHS(n); |
201d682e75f4
8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents:
16269
diff
changeset
|
136 |
} |
201d682e75f4
8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents:
16269
diff
changeset
|
137 |
|
201d682e75f4
8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents:
16269
diff
changeset
|
138 |
@Override |
16147 | 139 |
public Node getAssignmentSource() { |
140 |
return rhs(); |
|
141 |
} |
|
142 |
||
143 |
/** |
|
144 |
* Assist in IR navigation. |
|
145 |
* @param visitor IR navigating visitor. |
|
146 |
*/ |
|
147 |
@Override |
|
148 |
public Node accept(final NodeVisitor visitor) { |
|
17233 | 149 |
if (visitor.enterBinaryNode(this)) { |
150 |
return visitor.leaveBinaryNode(setLHS(lhs.accept(visitor)).setRHS(rhs.accept(visitor))); |
|
16147 | 151 |
} |
152 |
||
153 |
return this; |
|
154 |
} |
|
155 |
||
156 |
@Override |
|
157 |
public void toString(final StringBuilder sb) { |
|
158 |
final TokenType type = tokenType(); |
|
159 |
||
160 |
final boolean lhsParen = type.needsParens(lhs().tokenType(), true); |
|
161 |
final boolean rhsParen = type.needsParens(rhs().tokenType(), false); |
|
162 |
||
163 |
if (lhsParen) { |
|
164 |
sb.append('('); |
|
165 |
} |
|
166 |
||
167 |
lhs().toString(sb); |
|
168 |
||
169 |
if (lhsParen) { |
|
170 |
sb.append(')'); |
|
171 |
} |
|
172 |
||
173 |
sb.append(' '); |
|
174 |
||
175 |
switch (type) { |
|
176 |
case COMMALEFT: |
|
177 |
sb.append(",<"); |
|
178 |
break; |
|
179 |
case COMMARIGHT: |
|
180 |
sb.append(",>"); |
|
181 |
break; |
|
182 |
case INCPREFIX: |
|
183 |
case DECPREFIX: |
|
184 |
sb.append("++"); |
|
185 |
break; |
|
186 |
default: |
|
187 |
sb.append(type.getName()); |
|
188 |
break; |
|
189 |
} |
|
190 |
||
191 |
sb.append(' '); |
|
192 |
||
193 |
if (rhsParen) { |
|
194 |
sb.append('('); |
|
195 |
} |
|
196 |
rhs().toString(sb); |
|
197 |
if (rhsParen) { |
|
198 |
sb.append(')'); |
|
199 |
} |
|
200 |
} |
|
201 |
||
202 |
/** |
|
203 |
* Get the left hand side expression for this node |
|
204 |
* @return the left hand side expression |
|
205 |
*/ |
|
206 |
public Node lhs() { |
|
207 |
return lhs; |
|
208 |
} |
|
209 |
||
210 |
/** |
|
17233 | 211 |
* Get the right hand side expression for this node |
212 |
* @return the left hand side expression |
|
213 |
*/ |
|
214 |
public Node rhs() { |
|
215 |
return rhs; |
|
216 |
} |
|
217 |
||
218 |
/** |
|
16147 | 219 |
* Set the left hand side expression for this node |
220 |
* @param lhs new left hand side expression |
|
16530
201d682e75f4
8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents:
16269
diff
changeset
|
221 |
* @return a node equivalent to this one except for the requested change. |
16147 | 222 |
*/ |
16530
201d682e75f4
8010652: Eliminate non-child references in Block/FunctionNode, and make few node types immutable
attila
parents:
16269
diff
changeset
|
223 |
public BinaryNode setLHS(final Node lhs) { |
17233 | 224 |
if (this.lhs == lhs) { |
225 |
return this; |
|
226 |
} |
|
227 |
return new BinaryNode(this, lhs, rhs); |
|
16147 | 228 |
} |
17233 | 229 |
|
230 |
/** |
|
231 |
* Set the right hand side expression for this node |
|
232 |
* @param rhs new left hand side expression |
|
233 |
* @return a node equivalent to this one except for the requested change. |
|
234 |
*/ |
|
235 |
public BinaryNode setRHS(final Node rhs) { |
|
236 |
if (this.rhs == rhs) { |
|
237 |
return this; |
|
238 |
} |
|
239 |
return new BinaryNode(this, lhs, rhs); |
|
240 |
} |
|
241 |
||
16147 | 242 |
} |