Debian Codegraph @

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

raw symbols SCIP
1 /*
2 * Copyright 2006 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.jscomp.AnalyzePrototypeProperties.AssignmentProperty;
21 import com.google.javascript.jscomp.AnalyzePrototypeProperties.GlobalFunction;
22 import com.google.javascript.jscomp.AnalyzePrototypeProperties.LiteralProperty;
23 import com.google.javascript.jscomp.AnalyzePrototypeProperties.NameInfo;
24 import com.google.javascript.jscomp.AnalyzePrototypeProperties.Symbol;
25 import com.google.javascript.rhino.Node;
26
27 import java.util.Collection;
28 import java.util.logging.Logger;
29
30 /**
31 * Removes unused properties from prototypes.
32 *
33 */
34 class RemoveUnusedPrototypeProperties implements
35 SpecializationAwareCompilerPass {
36
37 private static final Logger logger =
38 Logger.getLogger(RemoveUnusedPrototypeProperties.class.getName());
39
40 private final AbstractCompiler compiler;
41 private final boolean canModifyExterns;
42 private final boolean anchorUnusedVars;
43 private SpecializeModule.SpecializationState specializationState;
44
45 /**
46 * Creates a new pass for removing unused prototype properties, based
47 * on the uniqueness of property names.
48 * @param compiler The compiler.
49 * @param canModifyExterns If true, then we can remove prototype
50 * properties that are declared in the externs file.
51 * @param anchorUnusedVars If true, then we must keep unused variables
52 * and the prototype properties they reference, even if they are
53 * never used.
54 */
55 RemoveUnusedPrototypeProperties(AbstractCompiler compiler,
56 boolean canModifyExterns,
57 boolean anchorUnusedVars) {
58 this.compiler = compiler;
59 this.canModifyExterns = canModifyExterns;
60 this.anchorUnusedVars = anchorUnusedVars;
61 }
62
63 @Override
64 public void enableSpecialization(SpecializeModule.SpecializationState state) {
65 this.specializationState = state;
66 }
67
68 @Override
69 public void process(Node externRoot, Node root) {
70 AnalyzePrototypeProperties analyzer =
71 new AnalyzePrototypeProperties(compiler,
72 null /* no module graph */, canModifyExterns, anchorUnusedVars);
73 analyzer.process(externRoot, root);
74 removeUnusedSymbols(analyzer.getAllNameInfo());
75 }
76
77 /**
78 * Remove all properties under a given name if the property name is
79 * never referenced.
80 */
81 private void removeUnusedSymbols(Collection<NameInfo> allNameInfo) {
82 boolean changed = false;
83 for (NameInfo nameInfo : allNameInfo) {
84 if (!nameInfo.isReferenced()) {
85 for (Symbol declaration : nameInfo.getDeclarations()) {
86 boolean canRemove = false;
87
88 if (specializationState == null) {
89 canRemove = true;
90 } else {
91 Node specializableFunction =
92 getSpecializableFunctionFromSymbol(declaration);
93
94 if (specializableFunction != null) {
95 specializationState.reportRemovedFunction(
96 specializableFunction, null);
97 canRemove = true;
98 }
99 }
100
101 if (canRemove) {
102 declaration.remove();
103 changed = true;
104 }
105 }
106
107 logger.fine("Removed unused prototype property: " + nameInfo.name);
108 }
109 }
110
111 if (changed) {
112 compiler.reportCodeChange();
113 }
114 }
115
116 /**
117 * Attempts to find a specializable function from the Symbol.
118 */
119 private Node getSpecializableFunctionFromSymbol(Symbol symbol) {
120 Preconditions.checkNotNull(specializationState);
121
122 Node specializableFunction = null;
123
124 if (symbol instanceof GlobalFunction) {
125 specializableFunction = ((GlobalFunction) symbol).getFunctionNode();
126 } else if (symbol instanceof AssignmentProperty) {
127 Node propertyValue = ((AssignmentProperty) symbol).getValue();
128 if (propertyValue.isFunction()) {
129 specializableFunction = propertyValue;
130 }
131 } else if (symbol instanceof LiteralProperty) {
132 // Module specialization doesn't know how to handle these
133 // because the "name" of the function isn't the name
134 // it needs to add an unspecialized version of.
135
136 return null;
137 } else {
138 Preconditions.checkState(false, "Should be unreachable.");
139 }
140
141 if (specializableFunction != null &&
142 specializationState.canFixupFunction(specializableFunction)) {
143 return specializableFunction;
144 } else {
145 return null;
146 }
147 }
148 }