Debian Codegraph @

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

raw symbols SCIP
1 /*
2 * Copyright 2012 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.collect.ImmutableList;
20 import com.google.common.collect.Lists;
21 import com.google.javascript.jscomp.DefaultPassConfig.HotSwapPassFactory;
22 import com.google.javascript.jscomp.GlobalVarReferenceMap.GlobalVarRefCleanupPass;
23 import com.google.javascript.jscomp.Scope.Var;
24 import com.google.javascript.rhino.Node;
25 import com.google.javascript.rhino.jstype.FunctionType;
26 import com.google.javascript.rhino.jstype.JSType;
27
28 import java.util.List;
29
30
31 /**
32 * Provides passes that should be run before hot-swap/incremental builds.
33 *
34 * @author [email protected] (Tyler Goodwin)
35 */
36 class CleanupPasses extends PassConfig {
37
38 private State state;
39
40 public CleanupPasses(CompilerOptions options) {
41 super(options);
42 }
43
44 @Override
45 protected List<PassFactory> getChecks() {
46 List<PassFactory> checks = Lists.newArrayList();
47 checks.add(fieldCleanupPassFactory);
48 checks.add(scopeCleanupPassFactory);
49 checks.add(globalVarRefCleanupPassFactory);
50 return checks;
51 }
52
53 @Override
54 protected State getIntermediateState() {
55 return state;
56 }
57
58 @Override
59 protected List<PassFactory> getOptimizations() {
60 return ImmutableList.of();
61 }
62
63 @Override
64 protected void setIntermediateState(State state) {
65 this.state = state;
66 }
67
68 final PassFactory fieldCleanupPassFactory =
69 new HotSwapPassFactory("FieldCleaupPassFactory", false) {
70 @Override
71 protected HotSwapCompilerPass create(
72 AbstractCompiler compiler) {
73 return new FieldCleanupPass(compiler);
74 }
75 };
76
77 final PassFactory scopeCleanupPassFactory =
78 new HotSwapPassFactory("ScopeCleanupPassFactory", false) {
79 @Override
80 protected HotSwapCompilerPass create(
81 AbstractCompiler compiler) {
82 return new MemoizedScopeCleanupPass(compiler);
83 }
84 };
85
86 final PassFactory globalVarRefCleanupPassFactory =
87 new HotSwapPassFactory("GlobalVarRefCleanupPassFactory", false) {
88 @Override
89 protected HotSwapCompilerPass create(
90 AbstractCompiler compiler) {
91 return new GlobalVarRefCleanupPass(compiler);
92 }
93 };
94
95 /**
96 * A CleanupPass implementation that will remove stored scopes from the
97 * MemoizedScopeCreator of the compiler instance for a the hot swapped script.
98 * <p>
99 * This pass will also clear out Source Nodes of Function Types declared on
100 * Vars tracked by MemoizedScopeCreator
101 */
102 static class MemoizedScopeCleanupPass implements HotSwapCompilerPass {
103
104 private final AbstractCompiler compiler;
105
106 public MemoizedScopeCleanupPass(AbstractCompiler compiler) {
107 this.compiler = compiler;
108 }
109
110 @Override
111 public void hotSwapScript(Node scriptRoot, Node originalRoot) {
112 ScopeCreator creator = compiler.getTypedScopeCreator();
113 if (creator instanceof MemoizedScopeCreator) {
114 MemoizedScopeCreator scopeCreator = (MemoizedScopeCreator) creator;
115 String newSrc = scriptRoot.getSourceFileName();
116 for (Var var : scopeCreator.getAllSymbols()) {
117 JSType type = var.getType();
118 if (type != null) {
119 FunctionType fnType = type.toMaybeFunctionType();
120 if (fnType != null
121 && newSrc.equals(NodeUtil.getSourceName(fnType.getSource()))) {
122 fnType.setSource(null);
123 }
124 }
125 }
126 scopeCreator.removeScopesForScript(originalRoot.getSourceFileName());
127 }
128 }
129
130 @Override
131 public void process(Node externs, Node root) {
132 // MemoizedScopeCleanupPass should not do work during process.
133 }
134 }
135 }