Debian Codegraph @

src/com/google/javascript/jscomp/CompilationLevel.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.javascript.jscomp.CompilerOptions.Reach;
20
21 /**
22 * A CompilationLevel represents the level of optimization that should be
23 * applied when compiling JavaScript code.
24 *
25 * @author [email protected] (Michael Bolin)
26 */
27 public enum CompilationLevel {
28
29 /**
30 * WHITESPACE_ONLY removes comments and extra whitespace in the input JS.
31 */
32 WHITESPACE_ONLY,
33
34 /**
35 * SIMPLE_OPTIMIZATIONS performs transformations to the input JS that do not
36 * require any changes to JS that depend on the input JS. For example,
37 * function arguments are renamed (which should not matter to code that
38 * depends on the input JS), but functions themselves are not renamed (which
39 * would otherwise require external code to change to use the renamed function
40 * names).
41 */
42 SIMPLE_OPTIMIZATIONS,
43
44 /**
45 * ADVANCED_OPTIMIZATIONS aggressively reduces code size by renaming function
46 * names and variables, removing code which is never called, etc.
47 */
48 ADVANCED_OPTIMIZATIONS,
49 ;
50
51 private CompilationLevel() {}
52
53 public void setOptionsForCompilationLevel(CompilerOptions options) {
54 switch (this) {
55 case WHITESPACE_ONLY:
56 applyBasicCompilationOptions(options);
57 break;
58 case SIMPLE_OPTIMIZATIONS:
59 applySafeCompilationOptions(options);
60 break;
61 case ADVANCED_OPTIMIZATIONS:
62 applyFullCompilationOptions(options);
63 break;
64 default:
65 throw new RuntimeException("Unknown compilation level.");
66 }
67 }
68
69 public void setDebugOptionsForCompilationLevel(CompilerOptions options) {
70 options.anonymousFunctionNaming = AnonymousFunctionNamingPolicy.UNMAPPED;
71 options.generatePseudoNames = true;
72 options.removeClosureAsserts = false;
73 // Don't shadow variables as it is too confusing.
74 options.shadowVariables = false;
75 }
76
77 /**
78 * Gets options that only strip whitespace and comments.
79 * @param options The CompilerOptions object to set the options on.
80 */
81 private static void applyBasicCompilationOptions(CompilerOptions options) {
82 options.skipAllCompilerPasses();
83 }
84
85 /**
86 * Add options that are safe. Safe means options that won't break the
87 * JavaScript code even if no symbols are exported and no coding convention
88 * is used.
89 * @param options The CompilerOptions object to set the options on.
90 */
91 private static void applySafeCompilationOptions(CompilerOptions options) {
92 // ReplaceIdGenerators is on by default, but should run in simple mode.
93 options.replaceIdGenerators = false;
94
95 // Does not call applyBasicCompilationOptions(options) because the call to
96 // skipAllCompilerPasses() cannot be easily undone.
97 options.dependencyOptions.setDependencySorting(true);
98 options.closurePass = true;
99 options.setRenamingPolicy(
100 VariableRenamingPolicy.LOCAL, PropertyRenamingPolicy.OFF);
101 options.shadowVariables = true;
102 options.setInlineVariables(Reach.LOCAL_ONLY);
103 options.flowSensitiveInlineVariables = true;
104 options.setInlineFunctions(Reach.LOCAL_ONLY);
105 options.setAssumeClosuresOnlyCaptureReferences(false);
106 options.checkGlobalThisLevel = CheckLevel.OFF;
107 options.foldConstants = true;
108 options.coalesceVariableNames = true;
109 options.deadAssignmentElimination = true;
110 options.collapseVariableDeclarations = true;
111 options.convertToDottedProperties = true;
112 options.labelRenaming = true;
113 options.removeDeadCode = true;
114 options.optimizeArgumentsArray = true;
115 options.setRemoveUnusedVariables(Reach.LOCAL_ONLY);
116 options.collapseObjectLiterals = true;
117 options.protectHiddenSideEffects = true;
118 }
119
120 /**
121 * Add the options that will work only if the user exported all the symbols
122 * correctly.
123 * @param options The CompilerOptions object to set the options on.
124 */
125 private static void applyFullCompilationOptions(CompilerOptions options) {
126 // Do not call applySafeCompilationOptions(options) because the call can
127 // create possible conflicts between multiple diagnostic groups.
128
129 // All the safe optimizations.
130 options.dependencyOptions.setDependencySorting(true);
131 options.closurePass = true;
132 options.foldConstants = true;
133 options.coalesceVariableNames = true;
134 options.deadAssignmentElimination = true;
135 options.extractPrototypeMemberDeclarations = true;
136 options.collapseVariableDeclarations = true;
137 options.convertToDottedProperties = true;
138 options.rewriteFunctionExpressions = true;
139 options.labelRenaming = true;
140 options.removeDeadCode = true;
141 options.optimizeArgumentsArray = true;
142 options.collapseObjectLiterals = true;
143 options.protectHiddenSideEffects = true;
144
145 // All the advance optimizations.
146 options.removeClosureAsserts = true;
147 options.aliasKeywords = true;
148 options.reserveRawExports = true;
149 options.setRenamingPolicy(
150 VariableRenamingPolicy.ALL, PropertyRenamingPolicy.ALL_UNQUOTED);
151 options.shadowVariables = true;
152 options.removeUnusedPrototypeProperties = true;
153 options.removeUnusedPrototypePropertiesInExterns = true;
154 options.collapseAnonymousFunctions = true;
155 options.collapseProperties = true;
156 options.checkGlobalThisLevel = CheckLevel.WARNING;
157 options.rewriteFunctionExpressions = true;
158 options.smartNameRemoval = true;
159 options.inlineConstantVars = true;
160 options.setInlineFunctions(Reach.ALL);
161 options.setAssumeClosuresOnlyCaptureReferences(false);
162 options.inlineGetters = true;
163 options.setInlineVariables(Reach.ALL);
164 options.flowSensitiveInlineVariables = true;
165 options.computeFunctionSideEffects = true;
166
167 // Remove unused vars also removes unused functions.
168 options.setRemoveUnusedVariables(Reach.ALL);
169
170 // Move code around based on the defined modules.
171 options.crossModuleCodeMotion = true;
172 options.crossModuleMethodMotion = true;
173
174 // Call optimizations
175 options.devirtualizePrototypeMethods = true;
176 options.optimizeParameters = true;
177 options.optimizeReturns = true;
178 options.optimizeCalls = true;
179 }
180
181 /**
182 * Enable additional optimizations that use type information.
183 * @param options The CompilerOptions object to set the options on.
184 */
185 public void setTypeBasedOptimizationOptions(CompilerOptions options) {
186 switch (this) {
187 case ADVANCED_OPTIMIZATIONS:
188 options.inferTypes = true;
189 options.disambiguateProperties = true;
190 options.ambiguateProperties = true;
191 options.inlineProperties = true;
192 // TODO(johnlenz) :removeUnusedClassProperties isn't strictly a
193 // type based pass, but add it here for now because I may have to
194 // make it into one.
195 options.removeUnusedClassProperties = true;
196 break;
197 case SIMPLE_OPTIMIZATIONS:
198 // TODO(johnlenz): enable peephole type based optimization.
199 break;
200 case WHITESPACE_ONLY:
201 break;
202 }
203 }
204 }