| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.google.javascript.jscomp; |
| 18 |
|
| 19 |
import com.google.common.base.Preconditions; |
| 20 |
import com.google.common.collect.Lists; |
| 21 |
import com.google.javascript.rhino.Node; |
| 22 |
import com.google.javascript.rhino.Token; |
| 23 |
|
| 24 |
import java.util.Collections; |
| 25 |
import java.util.Iterator; |
| 26 |
import java.util.List; |
| 27 |
import java.util.Stack; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
class NodeIterators { |
| 37 |
|
| 38 |
private NodeIterators() {} |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
static class FunctionlessLocalScope implements Iterator<Node> { |
| 44 |
private final Stack<Node> ancestors = new Stack<Node>(); |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
FunctionlessLocalScope(Node ... ancestors) { |
| 52 |
Preconditions.checkArgument(ancestors.length > 0); |
| 53 |
|
| 54 |
for (Node n : ancestors) { |
| 55 |
if (n.isFunction()) { |
| 56 |
break; |
| 57 |
} |
| 58 |
|
| 59 |
this.ancestors.add(0, n); |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
@Override |
| 64 |
public boolean hasNext() { |
| 65 |
|
| 66 |
return !(ancestors.size() == 1 && ancestors.peek().getNext() == null); |
| 67 |
} |
| 68 |
|
| 69 |
@Override |
| 70 |
public Node next() { |
| 71 |
Node current = ancestors.pop(); |
| 72 |
if (current.getNext() == null) { |
| 73 |
current = ancestors.peek(); |
| 74 |
|
| 75 |
|
| 76 |
if (current.isFunction()) { |
| 77 |
return next(); |
| 78 |
} |
| 79 |
} else { |
| 80 |
current = current.getNext(); |
| 81 |
ancestors.push(current); |
| 82 |
|
| 83 |
|
| 84 |
if (current.isFunction()) { |
| 85 |
return next(); |
| 86 |
} |
| 87 |
|
| 88 |
while (current.hasChildren()) { |
| 89 |
current = current.getFirstChild(); |
| 90 |
ancestors.push(current); |
| 91 |
|
| 92 |
|
| 93 |
if (current.isFunction()) { |
| 94 |
return next(); |
| 95 |
} |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
return current; |
| 100 |
} |
| 101 |
|
| 102 |
@Override |
| 103 |
public void remove() { |
| 104 |
throw new UnsupportedOperationException("Not implemented"); |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
protected Node current() { |
| 111 |
return ancestors.peek(); |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
protected Node currentParent() { |
| 118 |
return ancestors.size() >= 2 ? |
| 119 |
ancestors.get(ancestors.size() - 2) : null; |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
List<Node> currentAncestors() { |
| 127 |
List<Node> list = Lists.newArrayList(ancestors); |
| 128 |
Collections.reverse(list); |
| 129 |
return list; |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
static class LocalVarMotion implements Iterator<Node> { |
| 154 |
private final boolean valueHasSideEffects; |
| 155 |
private final FunctionlessLocalScope iterator; |
| 156 |
private final String varName; |
| 157 |
private Node lookAhead; |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
static LocalVarMotion forVar( |
| 164 |
Node name, Node var, Node block) { |
| 165 |
Preconditions.checkArgument(var.isVar()); |
| 166 |
Preconditions.checkArgument(NodeUtil.isStatement(var)); |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
return new LocalVarMotion( |
| 172 |
name, new FunctionlessLocalScope(name, var, block)); |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
static LocalVarMotion forAssign( |
| 180 |
Node name, Node assign, Node expr, Node block) { |
| 181 |
Preconditions.checkArgument(assign.isAssign()); |
| 182 |
Preconditions.checkArgument(expr.isExprResult()); |
| 183 |
|
| 184 |
|
| 185 |
return new LocalVarMotion( |
| 186 |
name, new FunctionlessLocalScope(assign, expr, block)); |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
private LocalVarMotion(Node nameNode, FunctionlessLocalScope iterator) { |
| 194 |
Preconditions.checkArgument(nameNode.isName()); |
| 195 |
Node valueNode = NodeUtil.getAssignedValue(nameNode); |
| 196 |
this.varName = nameNode.getString(); |
| 197 |
this.valueHasSideEffects = valueNode != null && |
| 198 |
NodeUtil.mayHaveSideEffects(valueNode); |
| 199 |
this.iterator = iterator; |
| 200 |
advanceLookAhead(true); |
| 201 |
} |
| 202 |
|
| 203 |
@Override |
| 204 |
public boolean hasNext() { |
| 205 |
return lookAhead != null; |
| 206 |
} |
| 207 |
|
| 208 |
@Override |
| 209 |
public Node next() { |
| 210 |
Node next = lookAhead; |
| 211 |
advanceLookAhead(false); |
| 212 |
return next; |
| 213 |
} |
| 214 |
|
| 215 |
@Override |
| 216 |
public void remove() { |
| 217 |
throw new UnsupportedOperationException("Not implemented"); |
| 218 |
} |
| 219 |
|
| 220 |
private void advanceLookAhead(boolean atStart) { |
| 221 |
if (!atStart) { |
| 222 |
if (lookAhead == null) { |
| 223 |
return; |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
Node curNode = iterator.current(); |
| 229 |
if (curNode.isName() && |
| 230 |
varName.equals(curNode.getString())) { |
| 231 |
lookAhead = null; |
| 232 |
return; |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
if (!iterator.hasNext()) { |
| 237 |
lookAhead = null; |
| 238 |
return; |
| 239 |
} |
| 240 |
|
| 241 |
Node nextNode = iterator.next(); |
| 242 |
Node nextParent = iterator.currentParent(); |
| 243 |
int type = nextNode.getType(); |
| 244 |
|
| 245 |
if (valueHasSideEffects) { |
| 246 |
|
| 247 |
boolean readsState = false; |
| 248 |
|
| 249 |
if ( |
| 250 |
(nextNode.isName() && !varName.equals(nextNode.getString())) || |
| 251 |
|
| 252 |
(nextNode.isGetProp() || nextNode.isGetElem())) { |
| 253 |
|
| 254 |
|
| 255 |
if (nextParent == null || |
| 256 |
!NodeUtil.isVarOrSimpleAssignLhs(nextNode, nextParent)) { |
| 257 |
readsState = true; |
| 258 |
} |
| 259 |
|
| 260 |
} else if (nextNode.isCall() || nextNode.isNew()) { |
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
readsState = true; |
| 267 |
} |
| 268 |
|
| 269 |
if (readsState) { |
| 270 |
lookAhead = null; |
| 271 |
return; |
| 272 |
} |
| 273 |
} |
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
if (NodeUtil.nodeTypeMayHaveSideEffects(nextNode) && type != Token.NAME |
| 282 |
|| type == Token.NAME && nextParent.isCatch()) { |
| 283 |
lookAhead = null; |
| 284 |
return; |
| 285 |
} |
| 286 |
|
| 287 |
lookAhead = nextNode; |
| 288 |
} |
| 289 |
} |
| 290 |
} |