Debian Codegraph @

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

raw symbols SCIP
1 /*
2 * Copyright 2009 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.common.collect.Lists;
21 import com.google.common.collect.Sets;
22 import com.google.javascript.jscomp.DefinitionsRemover.Definition;
23 import com.google.javascript.jscomp.NodeTraversal.AbstractPostOrderCallback;
24 import com.google.javascript.rhino.JSDocInfo;
25 import com.google.javascript.rhino.Node;
26 import com.google.javascript.rhino.Token;
27
28 import java.util.Collection;
29 import java.util.List;
30 import java.util.Set;
31
32 /**
33 * Set the NoSideEffects property for function and constructor calls
34 * that refer to functions that are known to have no side effects.
35 * Current implementation relies on @nosideeffects annotations at
36 * function definition sites; eventually we should traverse function
37 * bodies to determine if they have side effects.
38 *
39 */
40 class MarkNoSideEffectCalls implements CompilerPass {
41 static final DiagnosticType INVALID_NO_SIDE_EFFECT_ANNOTATION =
42 DiagnosticType.error(
43 "JSC_INVALID_NO_SIDE_EFFECT_ANNOTATION",
44 "@nosideeffects may only appear in externs files.");
45
46 private final AbstractCompiler compiler;
47
48 // Left hand side expression associated with a function node that
49 // has a @nosideeffects annotation.
50 private final Set<Node> noSideEffectFunctionNames;
51
52 MarkNoSideEffectCalls(AbstractCompiler compiler) {
53 this.compiler = compiler;
54 this.noSideEffectFunctionNames = Sets.newHashSet();
55 }
56
57 @Override
58 public void process(Node externs, Node root) {
59 SimpleDefinitionFinder defFinder = new SimpleDefinitionFinder(compiler);
60 defFinder.process(externs, root);
61
62 // Gather the list of function nodes that have @nosideeffects annotations.
63 // For use by SetNoSideEffectCallProperty.
64 NodeTraversal.traverse(
65 compiler, externs, new GatherNoSideEffectFunctions(true));
66 NodeTraversal.traverse(
67 compiler, root, new GatherNoSideEffectFunctions(false));
68
69 NodeTraversal.traverse(compiler, root,
70 new SetNoSideEffectCallProperty(defFinder));
71 }
72
73 /**
74 * Determines if the type of the value of the RHS expression can
75 * be a function node.
76 */
77 private static boolean definitionTypeContainsFunctionType(Definition def) {
78 Node rhs = def.getRValue();
79 if (rhs == null) {
80 return true;
81 }
82
83 switch (rhs.getType()) {
84 case Token.ASSIGN:
85 case Token.AND:
86 case Token.CALL:
87 case Token.GETPROP:
88 case Token.GETELEM:
89 case Token.FUNCTION:
90 case Token.HOOK:
91 case Token.NAME:
92 case Token.NEW:
93 case Token.OR:
94 return true;
95 default:
96 return false;
97 }
98 }
99
100 /**
101 * Get the value of the @nosideeffects annotation stored in the
102 * doc info.
103 */
104 private static boolean hasNoSideEffectsAnnotation(Node node) {
105 JSDocInfo docInfo = node.getJSDocInfo();
106 return docInfo != null && docInfo.isNoSideEffects();
107 }
108
109 /**
110 * Gather function nodes that have @nosideeffects annotations.
111 */
112 private class GatherNoSideEffectFunctions extends AbstractPostOrderCallback {
113 private final boolean inExterns;
114
115 GatherNoSideEffectFunctions(boolean inExterns) {
116 this.inExterns = inExterns;
117 }
118
119 @Override
120 public void visit(NodeTraversal traversal, Node node, Node parent) {
121 if (!inExterns && hasNoSideEffectsAnnotation(node)) {
122 traversal.report(node, INVALID_NO_SIDE_EFFECT_ANNOTATION);
123 }
124
125 if (node.isGetProp()) {
126 if (parent.isExprResult() &&
127 hasNoSideEffectsAnnotation(node)) {
128 noSideEffectFunctionNames.add(node);
129 }
130 } else if (node.isFunction()) {
131
132 // The annotation may attached to the function node, the
133 // variable declaration or assignment expression.
134 boolean hasAnnotation = hasNoSideEffectsAnnotation(node);
135 List<Node> nameNodes = Lists.newArrayList();
136 nameNodes.add(node.getFirstChild());
137
138 if (parent.isName()) {
139 Node gramp = parent.getParent();
140 if (gramp.isVar() &&
141 gramp.hasOneChild() &&
142 hasNoSideEffectsAnnotation(gramp)) {
143 hasAnnotation = true;
144 }
145
146 nameNodes.add(parent);
147 } else if (parent.isAssign()) {
148 if (hasNoSideEffectsAnnotation(parent)) {
149 hasAnnotation = true;
150 }
151
152 nameNodes.add(parent.getFirstChild());
153 }
154
155 if (hasAnnotation) {
156 noSideEffectFunctionNames.addAll(nameNodes);
157 }
158 }
159 }
160 }
161
162 /**
163 * Set the no side effects property for CALL and NEW nodes that
164 * refer to function names that are known to have no side effects.
165 */
166 private class SetNoSideEffectCallProperty extends AbstractPostOrderCallback {
167 private final SimpleDefinitionFinder defFinder;
168
169 SetNoSideEffectCallProperty(SimpleDefinitionFinder defFinder) {
170 this.defFinder = defFinder;
171 }
172
173 @Override
174 public void visit(NodeTraversal traversal, Node node, Node parent) {
175 if (!node.isCall() && !node.isNew()) {
176 return;
177 }
178
179 Collection<Definition> definitions =
180 defFinder.getDefinitionsReferencedAt(node.getFirstChild());
181 if (definitions == null) {
182 return;
183 }
184
185 for (Definition def : definitions) {
186 Node lValue = def.getLValue();
187 Preconditions.checkNotNull(lValue);
188 if (!noSideEffectFunctionNames.contains(lValue) &&
189 definitionTypeContainsFunctionType(def)) {
190 return;
191 }
192 }
193
194 node.setSideEffectFlags(Node.NO_SIDE_EFFECTS);
195 }
196 }
197 }