| 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.Objects; |
| 20 |
import com.google.common.base.Preconditions; |
| 21 |
import com.google.common.collect.Lists; |
| 22 |
import com.google.common.collect.Sets; |
| 23 |
import com.google.javascript.jscomp.ControlFlowGraph.Branch; |
| 24 |
import com.google.javascript.jscomp.NodeTraversal.AbstractPostOrderCallback; |
| 25 |
import com.google.javascript.jscomp.Scope.Var; |
| 26 |
import com.google.javascript.jscomp.graph.Annotation; |
| 27 |
import com.google.javascript.jscomp.graph.DiGraph.DiGraphNode; |
| 28 |
import com.google.javascript.jscomp.graph.LatticeElement; |
| 29 |
import com.google.javascript.rhino.Node; |
| 30 |
|
| 31 |
import java.util.ArrayList; |
| 32 |
import java.util.Comparator; |
| 33 |
import java.util.Iterator; |
| 34 |
import java.util.List; |
| 35 |
import java.util.Set; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
abstract class DataFlowAnalysis<N, L extends LatticeElement> { |
| 86 |
|
| 87 |
private final ControlFlowGraph<N> cfg; |
| 88 |
final JoinOp<L> joinOp; |
| 89 |
protected final Set<DiGraphNode<N, Branch>> orderedWorkSet; |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
public static final int MAX_STEPS = 200000; |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
DataFlowAnalysis(ControlFlowGraph<N> targetCfg, JoinOp<L> joinOp) { |
| 121 |
this.cfg = targetCfg; |
| 122 |
this.joinOp = joinOp; |
| 123 |
Comparator<DiGraphNode<N, Branch>> nodeComparator = |
| 124 |
cfg.getOptionalNodeComparator(isForward()); |
| 125 |
if (nodeComparator != null) { |
| 126 |
this.orderedWorkSet = Sets.newTreeSet(nodeComparator); |
| 127 |
} else { |
| 128 |
this.orderedWorkSet = Sets.newLinkedHashSet(); |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
final ControlFlowGraph<N> getCfg() { |
| 139 |
return cfg; |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
L getExitLatticeElement() { |
| 146 |
DiGraphNode<N, Branch> node = getCfg().getImplicitReturn(); |
| 147 |
FlowState<L> state = node.getAnnotation(); |
| 148 |
return state.getIn(); |
| 149 |
} |
| 150 |
|
| 151 |
@SuppressWarnings("unchecked") |
| 152 |
protected L join(L latticeA, L latticeB) { |
| 153 |
return joinOp.apply(Lists.<L>newArrayList(latticeA, latticeB)); |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
abstract boolean isForward(); |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
abstract L flowThrough(N node, L input); |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
final void analyze() { |
| 180 |
analyze(MAX_STEPS); |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
final void analyze(int maxSteps) { |
| 202 |
initialize(); |
| 203 |
int step = 0; |
| 204 |
while (!orderedWorkSet.isEmpty()) { |
| 205 |
if (step > maxSteps) { |
| 206 |
throw new MaxIterationsExceededException( |
| 207 |
"Analysis did not terminate after " + maxSteps + " iterations"); |
| 208 |
} |
| 209 |
DiGraphNode<N, Branch> curNode = orderedWorkSet.iterator().next(); |
| 210 |
orderedWorkSet.remove(curNode); |
| 211 |
joinInputs(curNode); |
| 212 |
if (flow(curNode)) { |
| 213 |
|
| 214 |
|
| 215 |
List<DiGraphNode<N, Branch>> nextNodes = isForward() ? |
| 216 |
cfg.getDirectedSuccNodes(curNode) : |
| 217 |
cfg.getDirectedPredNodes(curNode); |
| 218 |
for (DiGraphNode<N, Branch> nextNode : nextNodes) { |
| 219 |
if (nextNode != cfg.getImplicitReturn()) { |
| 220 |
orderedWorkSet.add(nextNode); |
| 221 |
} |
| 222 |
} |
| 223 |
} |
| 224 |
step++; |
| 225 |
} |
| 226 |
if (isForward()) { |
| 227 |
joinInputs(getCfg().getImplicitReturn()); |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
abstract L createInitialEstimateLattice(); |
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
abstract L createEntryLattice(); |
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
protected void initialize() { |
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
orderedWorkSet.clear(); |
| 253 |
for (DiGraphNode<N, Branch> node : cfg.getDirectedGraphNodes()) { |
| 254 |
node.setAnnotation(new FlowState<L>(createInitialEstimateLattice(), |
| 255 |
createInitialEstimateLattice())); |
| 256 |
if (node != cfg.getImplicitReturn()) { |
| 257 |
orderedWorkSet.add(node); |
| 258 |
} |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
protected boolean flow(DiGraphNode<N, Branch> node) { |
| 268 |
FlowState<L> state = node.getAnnotation(); |
| 269 |
if (isForward()) { |
| 270 |
L outBefore = state.out; |
| 271 |
state.out = flowThrough(node.getValue(), state.in); |
| 272 |
return !outBefore.equals(state.out); |
| 273 |
} else { |
| 274 |
L inBefore = state.in; |
| 275 |
state.in = flowThrough(node.getValue(), state.out); |
| 276 |
return !inBefore.equals(state.in); |
| 277 |
} |
| 278 |
} |
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
protected void joinInputs(DiGraphNode<N, Branch> node) { |
| 287 |
FlowState<L> state = node.getAnnotation(); |
| 288 |
if (isForward()) { |
| 289 |
if (cfg.getEntry() == node) { |
| 290 |
state.setIn(createEntryLattice()); |
| 291 |
} else { |
| 292 |
List<DiGraphNode<N, Branch>> inNodes = cfg.getDirectedPredNodes(node); |
| 293 |
if (inNodes.size() == 1) { |
| 294 |
FlowState<L> inNodeState = inNodes.get(0).getAnnotation(); |
| 295 |
state.setIn(inNodeState.getOut()); |
| 296 |
} else if (inNodes.size() > 1) { |
| 297 |
List<L> values = new ArrayList<L>(inNodes.size()); |
| 298 |
for (DiGraphNode<N, Branch> currentNode : inNodes) { |
| 299 |
FlowState<L> currentNodeState = currentNode.getAnnotation(); |
| 300 |
values.add(currentNodeState.getOut()); |
| 301 |
} |
| 302 |
state.setIn(joinOp.apply(values)); |
| 303 |
} |
| 304 |
} |
| 305 |
} else { |
| 306 |
List<DiGraphNode<N, Branch>> inNodes = cfg.getDirectedSuccNodes(node); |
| 307 |
if (inNodes.size() == 1) { |
| 308 |
DiGraphNode<N, Branch> inNode = inNodes.get(0); |
| 309 |
if (inNode == cfg.getImplicitReturn()) { |
| 310 |
state.setOut(createEntryLattice()); |
| 311 |
} else { |
| 312 |
FlowState<L> inNodeState = inNode.getAnnotation(); |
| 313 |
state.setOut(inNodeState.getIn()); |
| 314 |
} |
| 315 |
} else if (inNodes.size() > 1) { |
| 316 |
List<L> values = new ArrayList<L>(inNodes.size()); |
| 317 |
for (DiGraphNode<N, Branch> currentNode : inNodes) { |
| 318 |
FlowState<L> currentNodeState = currentNode.getAnnotation(); |
| 319 |
values.add(currentNodeState.getIn()); |
| 320 |
} |
| 321 |
state.setOut(joinOp.apply(values)); |
| 322 |
} |
| 323 |
} |
| 324 |
} |
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
static class FlowState<L extends LatticeElement> implements Annotation { |
| 332 |
private L in; |
| 333 |
private L out; |
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
private FlowState(L inState, L outState) { |
| 342 |
Preconditions.checkNotNull(inState); |
| 343 |
Preconditions.checkNotNull(outState); |
| 344 |
this.in = inState; |
| 345 |
this.out = outState; |
| 346 |
} |
| 347 |
|
| 348 |
L getIn() { |
| 349 |
return in; |
| 350 |
} |
| 351 |
|
| 352 |
void setIn(L in) { |
| 353 |
Preconditions.checkNotNull(in); |
| 354 |
this.in = in; |
| 355 |
} |
| 356 |
|
| 357 |
L getOut() { |
| 358 |
return out; |
| 359 |
} |
| 360 |
|
| 361 |
void setOut(L out) { |
| 362 |
Preconditions.checkNotNull(out); |
| 363 |
this.out = out; |
| 364 |
} |
| 365 |
|
| 366 |
@Override |
| 367 |
public String toString() { |
| 368 |
return String.format("IN: %s OUT: %s", in, out); |
| 369 |
} |
| 370 |
|
| 371 |
@Override |
| 372 |
public int hashCode() { |
| 373 |
return Objects.hashCode(in, out); |
| 374 |
} |
| 375 |
} |
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
static class MaxIterationsExceededException extends RuntimeException { |
| 383 |
private static final long serialVersionUID = 1L; |
| 384 |
MaxIterationsExceededException(String msg) { |
| 385 |
super(msg); |
| 386 |
} |
| 387 |
} |
| 388 |
|
| 389 |
abstract static class BranchedForwardDataFlowAnalysis |
| 390 |
<N, L extends LatticeElement> extends DataFlowAnalysis<N, L> { |
| 391 |
|
| 392 |
@Override |
| 393 |
protected void initialize() { |
| 394 |
orderedWorkSet.clear(); |
| 395 |
for (DiGraphNode<N, Branch> node : getCfg().getDirectedGraphNodes()) { |
| 396 |
int outEdgeCount = getCfg().getOutEdges(node.getValue()).size(); |
| 397 |
List<L> outLattices = Lists.newArrayList(); |
| 398 |
for (int i = 0; i < outEdgeCount; i++) { |
| 399 |
outLattices.add(createInitialEstimateLattice()); |
| 400 |
} |
| 401 |
node.setAnnotation(new BranchedFlowState<L>( |
| 402 |
createInitialEstimateLattice(), outLattices)); |
| 403 |
if (node != getCfg().getImplicitReturn()) { |
| 404 |
orderedWorkSet.add(node); |
| 405 |
} |
| 406 |
} |
| 407 |
} |
| 408 |
|
| 409 |
BranchedForwardDataFlowAnalysis(ControlFlowGraph<N> targetCfg, |
| 410 |
JoinOp<L> joinOp) { |
| 411 |
super(targetCfg, joinOp); |
| 412 |
} |
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
@Override |
| 419 |
L getExitLatticeElement() { |
| 420 |
DiGraphNode<N, Branch> node = getCfg().getImplicitReturn(); |
| 421 |
BranchedFlowState<L> state = node.getAnnotation(); |
| 422 |
return state.getIn(); |
| 423 |
} |
| 424 |
|
| 425 |
@Override |
| 426 |
final boolean isForward() { |
| 427 |
return true; |
| 428 |
} |
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
abstract List<L> branchedFlowThrough(N node, L input); |
| 442 |
|
| 443 |
@Override |
| 444 |
protected final boolean flow(DiGraphNode<N, Branch> node) { |
| 445 |
BranchedFlowState<L> state = node.getAnnotation(); |
| 446 |
List<L> outBefore = state.out; |
| 447 |
state.out = branchedFlowThrough(node.getValue(), state.in); |
| 448 |
Preconditions.checkState(outBefore.size() == state.out.size()); |
| 449 |
for (int i = 0; i < outBefore.size(); i++) { |
| 450 |
if (!outBefore.get(i).equals(state.out.get(i))) { |
| 451 |
return true; |
| 452 |
} |
| 453 |
} |
| 454 |
return false; |
| 455 |
} |
| 456 |
|
| 457 |
@Override |
| 458 |
protected void joinInputs(DiGraphNode<N, Branch> node) { |
| 459 |
BranchedFlowState<L> state = node.getAnnotation(); |
| 460 |
List<DiGraphNode<N, Branch>> predNodes = |
| 461 |
getCfg().getDirectedPredNodes(node); |
| 462 |
List<L> values = new ArrayList<L>(predNodes.size()); |
| 463 |
|
| 464 |
for (DiGraphNode<N, Branch> predNode : predNodes) { |
| 465 |
BranchedFlowState<L> predNodeState = predNode.getAnnotation(); |
| 466 |
|
| 467 |
L in = predNodeState.out.get( |
| 468 |
getCfg().getDirectedSuccNodes(predNode).indexOf(node)); |
| 469 |
|
| 470 |
values.add(in); |
| 471 |
} |
| 472 |
if (getCfg().getEntry() == node) { |
| 473 |
state.setIn(createEntryLattice()); |
| 474 |
} else if (!values.isEmpty()) { |
| 475 |
state.setIn(joinOp.apply(values)); |
| 476 |
} |
| 477 |
} |
| 478 |
} |
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
static class BranchedFlowState<L extends LatticeElement> |
| 486 |
implements Annotation { |
| 487 |
private L in; |
| 488 |
private List<L> out; |
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
private BranchedFlowState(L inState, List<L> outState) { |
| 497 |
Preconditions.checkNotNull(inState); |
| 498 |
Preconditions.checkNotNull(outState); |
| 499 |
this.in = inState; |
| 500 |
this.out = outState; |
| 501 |
} |
| 502 |
|
| 503 |
L getIn() { |
| 504 |
return in; |
| 505 |
} |
| 506 |
|
| 507 |
void setIn(L in) { |
| 508 |
Preconditions.checkNotNull(in); |
| 509 |
this.in = in; |
| 510 |
} |
| 511 |
|
| 512 |
List<L> getOut() { |
| 513 |
return out; |
| 514 |
} |
| 515 |
|
| 516 |
void setOut(List<L> out) { |
| 517 |
Preconditions.checkNotNull(out); |
| 518 |
for (L item : out) { |
| 519 |
Preconditions.checkNotNull(item); |
| 520 |
} |
| 521 |
this.out = out; |
| 522 |
} |
| 523 |
|
| 524 |
@Override |
| 525 |
public String toString() { |
| 526 |
return String.format("IN: %s OUT: %s", in, out); |
| 527 |
} |
| 528 |
|
| 529 |
@Override |
| 530 |
public int hashCode() { |
| 531 |
return Objects.hashCode(in, out); |
| 532 |
} |
| 533 |
} |
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
static void computeEscaped(final Scope jsScope, final Set<Var> escaped, |
| 549 |
AbstractCompiler compiler) { |
| 550 |
|
| 551 |
AbstractPostOrderCallback finder = new AbstractPostOrderCallback() { |
| 552 |
@Override |
| 553 |
public void visit(NodeTraversal t, Node n, Node parent) { |
| 554 |
if (jsScope == t.getScope() || !n.isName() |
| 555 |
|| parent.isFunction()) { |
| 556 |
return; |
| 557 |
} |
| 558 |
String name = n.getString(); |
| 559 |
Var var = t.getScope().getVar(name); |
| 560 |
if (var != null && var.scope == jsScope) { |
| 561 |
escaped.add(jsScope.getVar(name)); |
| 562 |
} |
| 563 |
} |
| 564 |
}; |
| 565 |
|
| 566 |
NodeTraversal t = new NodeTraversal(compiler, finder); |
| 567 |
t.traverseAtScope(jsScope); |
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
for (Iterator<Var> i = jsScope.getVars(); i.hasNext();) { |
| 572 |
Var var = i.next(); |
| 573 |
if (var.getParentNode().isCatch() || |
| 574 |
compiler.getCodingConvention().isExported(var.getName())) { |
| 575 |
escaped.add(var); |
| 576 |
} |
| 577 |
} |
| 578 |
} |
| 579 |
} |