| 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.javascript.rhino.Node; |
| 21 |
import com.google.javascript.rhino.Token; |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
public class StatementFusion extends AbstractPeepholeOptimization { |
| 35 |
|
| 36 |
@Override |
| 37 |
Node optimizeSubtree(Node n) { |
| 38 |
|
| 39 |
if (!n.getParent().isFunction() && canFuseIntoOneStatement(n)) { |
| 40 |
fuseIntoOneStatement(n); |
| 41 |
reportCodeChange(); |
| 42 |
} |
| 43 |
return n; |
| 44 |
} |
| 45 |
|
| 46 |
private boolean canFuseIntoOneStatement(Node block) { |
| 47 |
|
| 48 |
if (!block.isBlock()) { |
| 49 |
return false; |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
if (!block.hasChildren() || block.hasOneChild()) { |
| 54 |
return false; |
| 55 |
} |
| 56 |
|
| 57 |
Node last = block.getLastChild(); |
| 58 |
|
| 59 |
for (Node c = block.getFirstChild(); c != null; c = c.getNext()) { |
| 60 |
if (!c.isExprResult() && c != last) { |
| 61 |
return false; |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
switch(last.getType()) { |
| 68 |
case Token.IF: |
| 69 |
case Token.THROW: |
| 70 |
case Token.SWITCH: |
| 71 |
case Token.EXPR_RESULT: |
| 72 |
return true; |
| 73 |
case Token.RETURN: |
| 74 |
|
| 75 |
return last.hasChildren(); |
| 76 |
case Token.FOR: |
| 77 |
return NodeUtil.isForIn(last) && |
| 78 |
|
| 79 |
!mayHaveSideEffects(last.getFirstChild()); |
| 80 |
} |
| 81 |
|
| 82 |
return false; |
| 83 |
} |
| 84 |
|
| 85 |
private void fuseIntoOneStatement(Node block) { |
| 86 |
Node cur = block.removeFirstChild(); |
| 87 |
|
| 88 |
|
| 89 |
Node commaTree = cur.removeFirstChild(); |
| 90 |
|
| 91 |
|
| 92 |
while (block.hasMoreThanOneChild()) { |
| 93 |
Node next = block.removeFirstChild().removeFirstChild(); |
| 94 |
commaTree = fuseExpressionIntoExpression(commaTree, next); |
| 95 |
} |
| 96 |
|
| 97 |
Preconditions.checkState(block.hasOneChild()); |
| 98 |
Node last = block.getLastChild(); |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
switch(last.getType()) { |
| 104 |
case Token.IF: |
| 105 |
case Token.RETURN: |
| 106 |
case Token.THROW: |
| 107 |
case Token.SWITCH: |
| 108 |
case Token.EXPR_RESULT: |
| 109 |
fuseExpresssonIntoFirstChild(commaTree, last); |
| 110 |
return; |
| 111 |
case Token.FOR: |
| 112 |
if (NodeUtil.isForIn(last)) { |
| 113 |
fuseExpresssonIntoSecondChild(commaTree, last); |
| 114 |
} |
| 115 |
return ; |
| 116 |
default: |
| 117 |
throw new IllegalStateException("Statement fusion missing."); |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
private static Node fuseExpressionIntoExpression(Node exp1, Node exp2) { |
| 123 |
Node comma = new Node(Token.COMMA, exp1); |
| 124 |
comma.copyInformationFrom(exp2); |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
if (exp2.isComma()) { |
| 130 |
Node leftMostChild = exp2; |
| 131 |
while(leftMostChild.isComma()) { |
| 132 |
leftMostChild = leftMostChild.getFirstChild(); |
| 133 |
} |
| 134 |
Node parent = leftMostChild.getParent(); |
| 135 |
comma.addChildToBack(leftMostChild.detachFromParent()); |
| 136 |
parent.addChildToFront(comma); |
| 137 |
return exp2; |
| 138 |
} else { |
| 139 |
comma.addChildToBack(exp2); |
| 140 |
return comma; |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
private static void fuseExpresssonIntoFirstChild(Node exp, Node stmt) { |
| 145 |
Node val = stmt.removeFirstChild(); |
| 146 |
Node comma = fuseExpressionIntoExpression(exp, val); |
| 147 |
stmt.addChildToFront(comma); |
| 148 |
} |
| 149 |
|
| 150 |
private static void fuseExpresssonIntoSecondChild(Node exp, Node stmt) { |
| 151 |
Node val = stmt.removeChildAfter(stmt.getFirstChild()); |
| 152 |
Node comma = fuseExpressionIntoExpression(exp, val); |
| 153 |
stmt.addChildAfter(comma, stmt.getFirstChild()); |
| 154 |
} |
| 155 |
} |