-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
MarkNoSideEffectCalls.java
195 lines (170 loc) · 5.91 KB
/
MarkNoSideEffectCalls.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*
* Copyright 2009 The Closure Compiler Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.javascript.jscomp;
import com.google.common.base.Preconditions;
import com.google.javascript.jscomp.DefinitionsRemover.Definition;
import com.google.javascript.jscomp.NodeTraversal.AbstractPostOrderCallback;
import com.google.javascript.rhino.JSDocInfo;
import com.google.javascript.rhino.Node;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/**
* Set the NoSideEffects property for function and constructor calls
* that refer to functions that are known to have no side effects.
* Current implementation relies on @nosideeffects annotations at
* function definition sites; eventually we should traverse function
* bodies to determine if they have side effects.
*
*/
class MarkNoSideEffectCalls implements CompilerPass {
private final AbstractCompiler compiler;
// Left hand side expression associated with a function node that
// has a @nosideeffects annotation.
private final Set<Node> noSideEffectFunctionNames;
MarkNoSideEffectCalls(AbstractCompiler compiler) {
this.compiler = compiler;
this.noSideEffectFunctionNames = new HashSet<>();
}
@Override
public void process(Node externs, Node root) {
NameBasedDefinitionProvider defFinder = new NameBasedDefinitionProvider(compiler);
defFinder.process(externs, root);
// Gather the list of function nodes that have @nosideeffects annotations.
// For use by SetNoSideEffectCallProperty.
NodeTraversal.traverseEs6(
compiler, externs, new GatherNoSideEffectFunctions(true));
NodeTraversal.traverseEs6(
compiler, root, new GatherNoSideEffectFunctions(false));
NodeTraversal.traverseEs6(compiler, root,
new SetNoSideEffectCallProperty(defFinder));
}
/**
* Determines if the type of the value of the RHS expression can
* be a function node.
*/
private static boolean definitionTypeContainsFunctionType(Definition def) {
Node rhs = def.getRValue();
if (rhs == null) {
return true;
}
switch (rhs.getType()) {
case ASSIGN:
case AND:
case CALL:
case GETPROP:
case GETELEM:
case FUNCTION:
case HOOK:
case NAME:
case NEW:
case OR:
return true;
default:
return false;
}
}
/**
* Get the value of the @nosideeffects annotation stored in the
* doc info.
*/
private static boolean hasNoSideEffectsAnnotation(Node node) {
JSDocInfo docInfo = node.getJSDocInfo();
return docInfo != null && docInfo.isNoSideEffects();
}
/**
* Gather function nodes that have @nosideeffects annotations.
*/
private class GatherNoSideEffectFunctions extends AbstractPostOrderCallback {
private final boolean inExterns;
GatherNoSideEffectFunctions(boolean inExterns) {
this.inExterns = inExterns;
}
@Override
public void visit(NodeTraversal traversal, Node node, Node parent) {
if (node.isGetProp()) {
if (parent.isExprResult() &&
hasNoSideEffectsAnnotation(node)) {
noSideEffectFunctionNames.add(node);
}
} else if (node.isFunction()) {
// The annotation may attached to the function node, the
// variable declaration or assignment expression.
boolean hasAnnotation = hasNoSideEffectsAnnotation(node);
List<Node> nameNodes = new ArrayList<>();
nameNodes.add(node.getFirstChild());
if (parent.isName()) {
Node gramp = parent.getParent();
if (gramp.isVar() &&
gramp.hasOneChild() &&
hasNoSideEffectsAnnotation(gramp)) {
hasAnnotation = true;
}
nameNodes.add(parent);
} else if (parent.isAssign()) {
if (hasNoSideEffectsAnnotation(parent)) {
hasAnnotation = true;
}
nameNodes.add(parent.getFirstChild());
}
if (hasAnnotation) {
noSideEffectFunctionNames.addAll(nameNodes);
}
}
}
}
/**
* Set the no side effects property for CALL and NEW nodes that
* refer to function names that are known to have no side effects.
*/
private class SetNoSideEffectCallProperty extends AbstractPostOrderCallback {
private final NameBasedDefinitionProvider defFinder;
SetNoSideEffectCallProperty(NameBasedDefinitionProvider defFinder) {
this.defFinder = defFinder;
}
@Override
public void visit(NodeTraversal traversal, Node node, Node parent) {
if (!NodeUtil.isCallOrNew(node)) {
return;
}
Node nameNode = node.getFirstChild();
// This is the result of an anonymous function execution. function() {}();
if (!nameNode.isName() && !nameNode.isGetProp()) {
return;
}
Collection<Definition> definitions = defFinder.getDefinitionsReferencedAt(nameNode);
if (definitions == null) {
return;
}
boolean maybeFunction = false;
for (Definition def : definitions) {
Node lValue = def.getLValue();
Preconditions.checkNotNull(lValue);
if (definitionTypeContainsFunctionType(def)) {
maybeFunction = true;
if (!noSideEffectFunctionNames.contains(lValue)) {
return;
}
}
}
if (maybeFunction) {
node.setSideEffectFlags(Node.NO_SIDE_EFFECTS);
}
}
}
}