Debian Codegraph @

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

raw symbols SCIP
1 /*
2 * Copyright 2008 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.IR;
21 import com.google.javascript.rhino.Node;
22 import com.google.javascript.rhino.Token;
23
24 /**
25 * Prepare the AST before we do any checks or optimizations on it.
26 *
27 * This pass must run. It should bring the AST into a consistent state,
28 * and add annotations where necessary. It should not make any transformations
29 * on the tree that would lose source information, since we need that source
30 * information for checks.
31 *
32 * @author [email protected] (John Lenz)
33 */
34 class PrepareAst implements CompilerPass {
35
36 private final AbstractCompiler compiler;
37 private final boolean checkOnly;
38
39 PrepareAst(AbstractCompiler compiler) {
40 this(compiler, false);
41 }
42
43 PrepareAst(AbstractCompiler compiler, boolean checkOnly) {
44 this.compiler = compiler;
45 this.checkOnly = checkOnly;
46 }
47
48 private void reportChange() {
49 if (checkOnly) {
50 Preconditions.checkState(false, "normalizeNodeType constraints violated");
51 }
52 }
53
54 @Override
55 public void process(Node externs, Node root) {
56 if (checkOnly) {
57 normalizeNodeTypes(root);
58 } else {
59 // Don't perform "PrepareAnnotations" when doing checks as
60 // they currently aren't valid during sanity checks. In particular,
61 // they DIRECT_EVAL shouldn't be applied after inlining has been
62 // performed.
63 if (externs != null) {
64 NodeTraversal.traverse(
65 compiler, externs, new PrepareAnnotations());
66 }
67 if (root != null) {
68 NodeTraversal.traverse(
69 compiler, root, new PrepareAnnotations());
70 }
71 }
72 }
73
74 /**
75 * Covert EXPR_VOID to EXPR_RESULT to simplify the rest of the code.
76 */
77 private void normalizeNodeTypes(Node n) {
78 normalizeBlocks(n);
79
80 for (Node child = n.getFirstChild();
81 child != null; child = child.getNext()) {
82 // This pass is run during the CompilerTestCase validation, so this
83 // parent pointer check serves as a more general check.
84 Preconditions.checkState(child.getParent() == n);
85
86 normalizeNodeTypes(child);
87 }
88 }
89
90 /**
91 * Add blocks to IF, WHILE, DO, etc.
92 */
93 private void normalizeBlocks(Node n) {
94 if (NodeUtil.isControlStructure(n)
95 && !n.isLabel()
96 && !n.isSwitch()) {
97 for (Node c = n.getFirstChild(); c != null; c = c.getNext()) {
98 if (NodeUtil.isControlStructureCodeBlock(n,c) &&
99 !c.isBlock()) {
100 Node newBlock = IR.block().srcref(n);
101 n.replaceChild(c, newBlock);
102 if (!c.isEmpty()) {
103 newBlock.addChildrenToFront(c);
104 } else {
105 newBlock.setWasEmptyNode(true);
106 }
107 c = newBlock;
108 reportChange();
109 }
110 }
111 }
112 }
113
114 /**
115 * Normalize where annotations appear on the AST. Copies
116 * around existing JSDoc annotations as well as internal annotations.
117 */
118 static class PrepareAnnotations
119 implements NodeTraversal.Callback {
120
121 PrepareAnnotations() {
122 }
123
124 @Override
125 public boolean shouldTraverse(NodeTraversal t, Node n, Node parent) {
126 if (n.isObjectLit()) {
127 normalizeObjectLiteralAnnotations(n);
128 }
129 return true;
130 }
131
132 @Override
133 public void visit(NodeTraversal t, Node n, Node parent) {
134 switch (n.getType()) {
135 case Token.CALL:
136 annotateCalls(n);
137 break;
138
139 case Token.FUNCTION:
140 annotateDispatchers(n, parent);
141 break;
142 }
143 }
144
145 private void normalizeObjectLiteralAnnotations(Node objlit) {
146 Preconditions.checkState(objlit.isObjectLit());
147 for (Node key = objlit.getFirstChild();
148 key != null; key = key.getNext()) {
149 Node value = key.getFirstChild();
150 normalizeObjectLiteralKeyAnnotations(objlit, key, value);
151 }
152 }
153
154 /**
155 * There are two types of calls we are interested in calls without explicit
156 * "this" values (what we are call "free" calls) and direct call to eval.
157 */
158 private void annotateCalls(Node n) {
159 Preconditions.checkState(n.isCall());
160
161 // Keep track of of the "this" context of a call. A call without an
162 // explicit "this" is a free call.
163 Node first = n.getFirstChild();
164 if (!NodeUtil.isGet(first)) {
165 n.putBooleanProp(Node.FREE_CALL, true);
166 }
167
168 // Keep track of the context in which eval is called. It is important
169 // to distinguish between "(0, eval)()" and "eval()".
170 if (first.isName() &&
171 "eval".equals(first.getString())) {
172 first.putBooleanProp(Node.DIRECT_EVAL, true);
173 }
174 }
175
176 /**
177 * Translate dispatcher info into the property expected node.
178 */
179 private void annotateDispatchers(Node n, Node parent) {
180 Preconditions.checkState(n.isFunction());
181 if (parent.getJSDocInfo() != null
182 && parent.getJSDocInfo().isJavaDispatch()) {
183 if (parent.isAssign()) {
184 Preconditions.checkState(parent.getLastChild() == n);
185 n.putBooleanProp(Node.IS_DISPATCHER, true);
186 }
187 }
188 }
189
190 /**
191 * In the AST that Rhino gives us, it needs to make a distinction
192 * between JsDoc on the object literal node and JsDoc on the object literal
193 * value. For example,
194 * <pre>
195 * var x = {
196 * / JSDOC /
197 * a: 'b',
198 * c: / JSDOC / 'd'
199 * };
200 * </pre>
201 *
202 * But in few narrow cases (in particular, function literals), it's
203 * a lot easier for us if the doc is attached to the value.
204 */
205 private void normalizeObjectLiteralKeyAnnotations(
206 Node objlit, Node key, Node value) {
207 Preconditions.checkState(objlit.isObjectLit());
208 if (key.getJSDocInfo() != null &&
209 value.isFunction()) {
210 value.setJSDocInfo(key.getJSDocInfo());
211 }
212 }
213 }
214 }