Debian Codegraph @

src/com/google/javascript/jscomp/StatementFusion.java

raw symbols SCIP
1 /*
2 * Copyright 2011 The Closure Compiler Authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
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 * Tries to fuse all the statements in a block into a one statement by using
25 * COMMAs.
26 *
27 * Because COMMAs has the lowest precedence, we never need to insert
28 * extra () around. Once we have only one statement in a block, we can then
29 * eliminate a pair of {}'s. Further more, we can also fold a single
30 * statement IF into && or create further opportunities for all the other
31 * goodies in {@link PeepholeSubstituteAlternateSyntax}.
32 *
33 */
34 public class StatementFusion extends AbstractPeepholeOptimization {
35
36 @Override
37 Node optimizeSubtree(Node n) {
38 // The block of a function body always need { }.
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 // Fold only statement block. NOT scripts block.
48 if (!block.isBlock()) {
49 return false;
50 }
51
52 // Nothing to do here.
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 // TODO(user): Support more control statement for fusion.
66 // FOR
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 // We don't want to add a new return value.
75 return last.hasChildren();
76 case Token.FOR:
77 return NodeUtil.isForIn(last) &&
78 // Avoid cases where we have for(var x = foo() in a) { ....
79 !mayHaveSideEffects(last.getFirstChild());
80 }
81
82 return false;
83 }
84
85 private void fuseIntoOneStatement(Node block) {
86 Node cur = block.removeFirstChild();
87
88 // Starts building a tree.
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 // Now we are just left with two statements. The comma tree of the first
101 // n - 1 statements (which can be used in an expression) and the last
102 // statement. We perform specific fusion based on the last statement's type.
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 // exp1, exp1
122 private static Node fuseExpressionIntoExpression(Node exp1, Node exp2) {
123 Node comma = new Node(Token.COMMA, exp1);
124 comma.copyInformationFrom(exp2);
125
126 // We can just join the new comma expression with another comma but
127 // lets keep all the comma's in a straight line. That way we can use
128 // tree comparison.
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 }