Debian Codegraph @

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

raw symbols SCIP
1 /*
2 * Copyright 2010 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 package com.google.javascript.jscomp;
17
18 import com.google.common.base.Preconditions;
19 import com.google.common.collect.Sets;
20 import com.google.javascript.jscomp.DefinitionsRemover.Definition;
21 import com.google.javascript.rhino.Node;
22 import com.google.javascript.rhino.Token;
23
24 import java.util.Set;
25
26 /**
27 * Uses {@link SimpleDefinitionFinder} to determine if a function has been
28 * aliased or exposed to .call() or .apply().
29 *
30 * @author [email protected] (Devin Coughlin)
31 */
32 class SimpleFunctionAliasAnalysis {
33 private Set<Node> aliasedFunctions;
34
35 private Set<Node> functionsExposedToCallOrApply;
36
37 /**
38 * Returns true if the function is aliased.
39 *
40 * Must only be called after {@link #analyze(SimpleDefinitionFinder)}
41 * has been called.
42 */
43 public boolean isAliased(Node functionNode) {
44 Preconditions.checkNotNull(aliasedFunctions);
45 Preconditions.checkArgument(functionNode.isFunction());
46
47 return aliasedFunctions.contains(functionNode);
48 }
49
50 /**
51 * Returns true if the function ever exposed to .call() or .apply().
52 *
53 * Must only be called after {@link #analyze(SimpleDefinitionFinder)}
54 * has been called.
55 */
56 public boolean isExposedToCallOrApply(Node functionNode) {
57 Preconditions.checkNotNull(functionsExposedToCallOrApply);
58 Preconditions.checkArgument(functionNode.isFunction());
59
60 return functionsExposedToCallOrApply.contains(functionNode);
61 }
62
63 /**
64 * Uses the provided {@link SimpleDefinitionFinder} to determine
65 * which functions are aliased or exposed to .call() or .apply().
66 */
67 public void analyze(SimpleDefinitionFinder finder) {
68 Preconditions.checkState(aliasedFunctions == null);
69
70 aliasedFunctions = Sets.newHashSet();
71 functionsExposedToCallOrApply = Sets.newHashSet();
72
73 for (DefinitionSite definitionSite : finder.getDefinitionSites()) {
74 Definition definition = definitionSite.definition;
75
76 if (!definition.isExtern()) {
77 Node rValue = definition.getRValue();
78
79 if (rValue != null && rValue.isFunction()) {
80 // rValue is a Token.FUNCTION from a definition
81
82 for (UseSite useSite : finder.getUseSites(definition)) {
83 updateFunctionForUse(rValue, useSite.node);
84 }
85 }
86 }
87 }
88 }
89
90 /**
91 * Updates alias and exposure information based a site where the function is
92 * used.
93 *
94 * Note: this method may be called multiple times per Function, each time
95 * with a different useNode.
96 */
97 private void updateFunctionForUse(Node function, Node useNode) {
98 Node useParent = useNode.getParent();
99 int parentType = useParent.getType();
100
101 if ((parentType == Token.CALL || parentType == Token.NEW)
102 && useParent.getFirstChild() == useNode) {
103 // Regular call sites don't count as aliases
104 } else if (NodeUtil.isGet(useParent)) {
105 // GET{PROP,ELEM} don't count as aliases
106 // but we have to check for using them in .call and .apply.
107
108 if (useParent.isGetProp()) {
109 Node gramps = useParent.getParent();
110 if (NodeUtil.isFunctionObjectApply(gramps) ||
111 NodeUtil.isFunctionObjectCall(gramps)) {
112 functionsExposedToCallOrApply.add(function);
113 }
114 }
115 } else {
116 aliasedFunctions.add(function);
117 }
118 }
119 }