| 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.Maps; |
| 21 |
import com.google.common.collect.Sets; |
| 22 |
import com.google.javascript.jscomp.ControlFlowGraph.AbstractCfgNodeTraversalCallback; |
| 23 |
import com.google.javascript.jscomp.ControlFlowGraph.Branch; |
| 24 |
import com.google.javascript.jscomp.Scope.Var; |
| 25 |
import com.google.javascript.jscomp.graph.GraphNode; |
| 26 |
import com.google.javascript.jscomp.graph.LatticeElement; |
| 27 |
import com.google.javascript.rhino.Node; |
| 28 |
import com.google.javascript.rhino.Token; |
| 29 |
|
| 30 |
import java.util.Iterator; |
| 31 |
import java.util.Map; |
| 32 |
import java.util.Map.Entry; |
| 33 |
import java.util.Set; |
| 34 |
|
| 35 |
import javax.annotation.Nullable; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
final class MustBeReachingVariableDef extends |
| 46 |
DataFlowAnalysis<Node, MustBeReachingVariableDef.MustDef> { |
| 47 |
|
| 48 |
|
| 49 |
private final Scope jsScope; |
| 50 |
private final AbstractCompiler compiler; |
| 51 |
private final Set<Var> escaped; |
| 52 |
|
| 53 |
MustBeReachingVariableDef( |
| 54 |
ControlFlowGraph<Node> cfg, Scope jsScope, AbstractCompiler compiler) { |
| 55 |
super(cfg, new MustDefJoin()); |
| 56 |
this.jsScope = jsScope; |
| 57 |
this.compiler = compiler; |
| 58 |
this.escaped = Sets.newHashSet(); |
| 59 |
computeEscaped(jsScope, escaped, compiler); |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
static class Definition { |
| 69 |
final Node node; |
| 70 |
final Set<Var> depends = Sets.newHashSet(); |
| 71 |
private boolean unknownDependencies = false; |
| 72 |
|
| 73 |
Definition(Node node) { |
| 74 |
this.node = node; |
| 75 |
} |
| 76 |
|
| 77 |
@Override |
| 78 |
public boolean equals(Object other) { |
| 79 |
if (!(other instanceof Definition)) { |
| 80 |
return false; |
| 81 |
} |
| 82 |
Definition otherDef = (Definition) other; |
| 83 |
|
| 84 |
|
| 85 |
return otherDef.node == node; |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
static final class MustDef implements LatticeElement { |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
final Map<Var, Definition> reachingDef; |
| 115 |
|
| 116 |
public MustDef() { |
| 117 |
reachingDef = Maps.newHashMap(); |
| 118 |
} |
| 119 |
|
| 120 |
public MustDef(Iterator<Var> vars) { |
| 121 |
this(); |
| 122 |
while(vars.hasNext()) { |
| 123 |
Var var = vars.next(); |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
reachingDef.put(var, new Definition(var.scope.getRootNode())); |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
public MustDef(MustDef other) { |
| 137 |
reachingDef = Maps.newHashMap(other.reachingDef); |
| 138 |
} |
| 139 |
|
| 140 |
@Override |
| 141 |
public boolean equals(Object other) { |
| 142 |
return (other instanceof MustDef) && |
| 143 |
((MustDef) other).reachingDef.equals(this.reachingDef); |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
private static class MustDefJoin extends JoinOp.BinaryJoinOp<MustDef> { |
| 148 |
@Override |
| 149 |
public MustDef apply(MustDef a, MustDef b) { |
| 150 |
MustDef result = new MustDef(); |
| 151 |
Map<Var, Definition> resultMap = result.reachingDef; |
| 152 |
|
| 153 |
|
| 154 |
for (Map.Entry<Var, Definition> varEntry : a.reachingDef.entrySet()) { |
| 155 |
Var var = varEntry.getKey(); |
| 156 |
Definition aDef = varEntry.getValue(); |
| 157 |
|
| 158 |
if (aDef == null) { |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
resultMap.put(var, null); |
| 163 |
continue; |
| 164 |
} |
| 165 |
|
| 166 |
if (b.reachingDef.containsKey(var)) { |
| 167 |
Definition bDef = b.reachingDef.get(var); |
| 168 |
|
| 169 |
if (aDef.equals(bDef)) { |
| 170 |
resultMap.put(var, aDef); |
| 171 |
} else { |
| 172 |
resultMap.put(var, null); |
| 173 |
} |
| 174 |
} else { |
| 175 |
resultMap.put(var, aDef); |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
for (Map.Entry<Var, Definition> entry : b.reachingDef.entrySet()) { |
| 182 |
Var var = entry.getKey(); |
| 183 |
if (!a.reachingDef.containsKey(var)) { |
| 184 |
resultMap.put(var, entry.getValue()); |
| 185 |
} |
| 186 |
} |
| 187 |
return result; |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
@Override |
| 192 |
boolean isForward() { |
| 193 |
return true; |
| 194 |
} |
| 195 |
|
| 196 |
@Override |
| 197 |
MustDef createEntryLattice() { |
| 198 |
return new MustDef(jsScope.getVars()); |
| 199 |
} |
| 200 |
|
| 201 |
@Override |
| 202 |
MustDef createInitialEstimateLattice() { |
| 203 |
return new MustDef(); |
| 204 |
} |
| 205 |
|
| 206 |
@Override |
| 207 |
MustDef flowThrough(Node n, MustDef input) { |
| 208 |
|
| 209 |
|
| 210 |
MustDef output = new MustDef(input); |
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
computeMustDef(n, n, output, false); |
| 215 |
return output; |
| 216 |
} |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
private void computeMustDef( |
| 224 |
Node n, Node cfgNode, MustDef output, boolean conditional) { |
| 225 |
switch (n.getType()) { |
| 226 |
|
| 227 |
case Token.BLOCK: |
| 228 |
case Token.FUNCTION: |
| 229 |
return; |
| 230 |
|
| 231 |
case Token.WHILE: |
| 232 |
case Token.DO: |
| 233 |
case Token.IF: |
| 234 |
computeMustDef( |
| 235 |
NodeUtil.getConditionExpression(n), cfgNode, output, conditional); |
| 236 |
return; |
| 237 |
|
| 238 |
case Token.FOR: |
| 239 |
if (!NodeUtil.isForIn(n)) { |
| 240 |
computeMustDef( |
| 241 |
NodeUtil.getConditionExpression(n), cfgNode, output, conditional); |
| 242 |
} else { |
| 243 |
|
| 244 |
Node lhs = n.getFirstChild(); |
| 245 |
Node rhs = lhs.getNext(); |
| 246 |
if (lhs.isVar()) { |
| 247 |
lhs = lhs.getLastChild(); |
| 248 |
} |
| 249 |
if (lhs.isName()) { |
| 250 |
addToDefIfLocal(lhs.getString(), cfgNode, rhs, output); |
| 251 |
} |
| 252 |
} |
| 253 |
return; |
| 254 |
|
| 255 |
case Token.AND: |
| 256 |
case Token.OR: |
| 257 |
computeMustDef(n.getFirstChild(), cfgNode, output, conditional); |
| 258 |
computeMustDef(n.getLastChild(), cfgNode, output, true); |
| 259 |
return; |
| 260 |
|
| 261 |
case Token.HOOK: |
| 262 |
computeMustDef(n.getFirstChild(), cfgNode, output, conditional); |
| 263 |
computeMustDef(n.getFirstChild().getNext(), cfgNode, output, true); |
| 264 |
computeMustDef(n.getLastChild(), cfgNode, output, true); |
| 265 |
return; |
| 266 |
|
| 267 |
case Token.VAR: |
| 268 |
for (Node c = n.getFirstChild(); c != null; c = c.getNext()) { |
| 269 |
if (c.hasChildren()) { |
| 270 |
computeMustDef(c.getFirstChild(), cfgNode, output, conditional); |
| 271 |
addToDefIfLocal(c.getString(), conditional ? null : cfgNode, |
| 272 |
c.getFirstChild(), output); |
| 273 |
} |
| 274 |
} |
| 275 |
return; |
| 276 |
|
| 277 |
default: |
| 278 |
if (NodeUtil.isAssignmentOp(n)) { |
| 279 |
if (n.getFirstChild().isName()) { |
| 280 |
Node name = n.getFirstChild(); |
| 281 |
computeMustDef(name.getNext(), cfgNode, output, conditional); |
| 282 |
addToDefIfLocal(name.getString(), conditional ? null : cfgNode, |
| 283 |
n.getLastChild(), output); |
| 284 |
return; |
| 285 |
} else if (NodeUtil.isGet(n.getFirstChild())) { |
| 286 |
|
| 287 |
|
| 288 |
Node obj = n.getFirstChild().getFirstChild(); |
| 289 |
if (obj.isName() && "arguments".equals(obj.getString())) { |
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
escapeParameters(output); |
| 294 |
} |
| 295 |
} |
| 296 |
} |
| 297 |
|
| 298 |
if (n.isName() && "arguments".equals(n.getString())) { |
| 299 |
escapeParameters(output); |
| 300 |
} |
| 301 |
|
| 302 |
|
| 303 |
if (n.isDec() || n.isInc()) { |
| 304 |
Node target = n.getFirstChild(); |
| 305 |
if (target.isName()) { |
| 306 |
addToDefIfLocal(target.getString(), |
| 307 |
conditional ? null : cfgNode, null, output); |
| 308 |
return; |
| 309 |
} |
| 310 |
} |
| 311 |
|
| 312 |
for (Node c = n.getFirstChild(); c != null; c = c.getNext()) { |
| 313 |
computeMustDef(c, cfgNode, output, conditional); |
| 314 |
} |
| 315 |
} |
| 316 |
} |
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
private void addToDefIfLocal( String name, @Nullable Node node, |
| 326 |
@Nullable Node rValue, MustDef def) { |
| 327 |
Var var = jsScope.getVar(name); |
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
if (var == null || var.scope != jsScope) { |
| 332 |
return; |
| 333 |
} |
| 334 |
|
| 335 |
for (Var other : def.reachingDef.keySet()) { |
| 336 |
Definition otherDef = def.reachingDef.get(other); |
| 337 |
if (otherDef == null) { |
| 338 |
continue; |
| 339 |
} |
| 340 |
if (otherDef.depends.contains(var)) { |
| 341 |
def.reachingDef.put(other, null); |
| 342 |
} |
| 343 |
} |
| 344 |
|
| 345 |
if (!escaped.contains(var)) { |
| 346 |
if (node == null) { |
| 347 |
def.reachingDef.put(var, null); |
| 348 |
} else { |
| 349 |
Definition definition = new Definition(node); |
| 350 |
if (rValue != null) { |
| 351 |
computeDependence(definition, rValue); |
| 352 |
} |
| 353 |
def.reachingDef.put(var, definition); |
| 354 |
} |
| 355 |
} |
| 356 |
} |
| 357 |
|
| 358 |
private void escapeParameters(MustDef output) { |
| 359 |
for (Iterator<Var> i = jsScope.getVars(); i.hasNext();) { |
| 360 |
Var v = i.next(); |
| 361 |
if (isParameter(v)) { |
| 362 |
|
| 363 |
|
| 364 |
output.reachingDef.put(v, null); |
| 365 |
} |
| 366 |
} |
| 367 |
|
| 368 |
|
| 369 |
for (Entry<Var, Definition> pair: output.reachingDef.entrySet()) { |
| 370 |
Definition value = pair.getValue(); |
| 371 |
if (value == null) { |
| 372 |
continue; |
| 373 |
} |
| 374 |
for (Var dep : value.depends) { |
| 375 |
if (isParameter(dep)) { |
| 376 |
output.reachingDef.put(pair.getKey(), null); |
| 377 |
} |
| 378 |
} |
| 379 |
} |
| 380 |
} |
| 381 |
|
| 382 |
private boolean isParameter(Var v) { |
| 383 |
return v.getParentNode().isParamList(); |
| 384 |
} |
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
private void computeDependence(final Definition def, Node rValue) { |
| 391 |
NodeTraversal.traverse(compiler, rValue, |
| 392 |
new AbstractCfgNodeTraversalCallback() { |
| 393 |
@Override |
| 394 |
public void visit(NodeTraversal t, Node n, Node parent) { |
| 395 |
if (n.isName()) { |
| 396 |
Var dep = jsScope.getVar(n.getString()); |
| 397 |
if (dep == null) { |
| 398 |
def.unknownDependencies = true; |
| 399 |
} else { |
| 400 |
def.depends.add(dep); |
| 401 |
} |
| 402 |
} |
| 403 |
} |
| 404 |
}); |
| 405 |
} |
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
Definition getDef(String name, Node useNode) { |
| 416 |
Preconditions.checkArgument(getCfg().hasNode(useNode)); |
| 417 |
GraphNode<Node, Branch> n = getCfg().getNode(useNode); |
| 418 |
FlowState<MustDef> state = n.getAnnotation(); |
| 419 |
return state.getIn().reachingDef.get(jsScope.getVar(name)); |
| 420 |
} |
| 421 |
|
| 422 |
Node getDefNode(String name, Node useNode) { |
| 423 |
Definition def = getDef(name, useNode); |
| 424 |
return def == null ? null : def.node; |
| 425 |
} |
| 426 |
|
| 427 |
boolean dependsOnOuterScopeVars(Definition def) { |
| 428 |
if (def.unknownDependencies) { |
| 429 |
return true; |
| 430 |
} |
| 431 |
|
| 432 |
for (Var s : def.depends) { |
| 433 |
if (s.scope != jsScope) { |
| 434 |
return true; |
| 435 |
} |
| 436 |
} |
| 437 |
return false; |
| 438 |
} |
| 439 |
} |