| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 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 |
|
| 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 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 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 , canModifyExterns, anchorUnusedVars); |
| 73 |
analyzer.process(externRoot, root); |
| 74 |
removeUnusedSymbols(analyzer.getAllNameInfo()); |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 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 |
|
| 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 |
|
| 133 |
|
| 134 |
|
| 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 |
} |