final class RenameVars extends java.lang.Object implements CompilerPass
Modifier and Type | Class and Description |
---|---|
(package private) class |
RenameVars.Assignment |
(package private) class |
RenameVars.ProcessVars
Iterate through the nodes, collect all the NAME nodes that need to be
renamed, and count how many times each variable name is referenced.
|
Modifier and Type | Field and Description |
---|---|
private int |
assignmentCount
Counter for each assignment
|
private java.lang.StringBuilder |
assignmentLog
Logs all name assignments
|
private java.util.Map<java.lang.String,RenameVars.Assignment> |
assignments
Maps an old name to a new name assignment
|
private AbstractCompiler |
compiler |
private java.util.Set<java.lang.String> |
externNames
Set of extern variable names
|
private static java.util.Comparator<RenameVars.Assignment> |
FREQUENCY_COMPARATOR
Sorts Assignment objects by their count, breaking ties by their order of
occurrence in the source to ensure a deterministic total ordering.
|
private java.util.ArrayList<Node> |
globalNameNodes
List of global NAME nodes
|
static java.lang.String |
LOCAL_VAR_PREFIX
A prefix to distinguish temporary local names from global names
|
private java.util.Set<Var> |
localBleedingFunctions |
private com.google.common.collect.ArrayListMultimap<Scope,Var> |
localBleedingFunctionsPerScope |
private java.util.ArrayList<Node> |
localNameNodes
List of local NAME nodes
|
private boolean |
localRenamingOnly
Whether renaming should apply to local variables only.
|
private static int |
MAX_LOCALS_IN_SCOPE_TO_TEMP_RENAME
Limit on number of locals in a scope for temporary local renaming
when
preferStableNames is true. |
private NameGenerator |
nameGenerator |
private static java.util.Comparator<RenameVars.Assignment> |
ORDER_OF_OCCURRENCE_COMPARATOR
Sorts Assignment objects by the order the variable name first appears in
the source.
|
private boolean |
preferStableNames |
private java.lang.String |
prefix
The global name prefix
|
private boolean |
preserveFunctionExpressionNames
Whether function expression names should be preserved.
|
private VariableMap |
prevUsedRenameMap
The previously used rename map.
|
private java.util.Map<Node,java.lang.String> |
pseudoNameMap
Maps a name node to its pseudo name, null if we are not generating so
there will be no overhead unless we are debugging.
|
private java.util.Map<java.lang.String,java.lang.String> |
renameMap
The renaming map
|
private char[] |
reservedCharacters
Characters that shouldn't be used in variable names.
|
private java.util.Set<java.lang.String> |
reservedNames
Set of reserved variable names
|
private boolean |
shouldShadow |
Constructor and Description |
---|
RenameVars(AbstractCompiler compiler,
java.lang.String prefix,
boolean localRenamingOnly,
boolean preserveFunctionExpressionNames,
boolean generatePseudoNames,
boolean shouldShadow,
boolean preferStableNames,
VariableMap prevUsedRenameMap,
char[] reservedCharacters,
java.util.Set<java.lang.String> reservedNames,
NameGenerator nameGenerator) |
Modifier and Type | Method and Description |
---|---|
private void |
assignNames(java.util.SortedSet<RenameVars.Assignment> varsToRename)
Determines which new names to substitute for the original names.
|
private void |
finalizeNameAssignment(RenameVars.Assignment a,
java.lang.String newName)
Makes a final name assignment.
|
private int |
getLocalVarIndex(Var v)
Returns the index within the scope stack.
|
private java.lang.String |
getNewGlobalName(Node n) |
private java.lang.String |
getNewLocalName(Node n) |
(package private) VariableMap |
getVariableMap()
Gets the variable map.
|
private boolean |
okToRenameVar(java.lang.String name,
boolean isLocal)
Determines whether a variable name is okay to rename.
|
void |
process(Node externs,
Node root)
Process the JS with root node root.
|
private void |
recordPseudoName(Node n) |
private void |
reusePreviouslyUsedVariableMap()
Runs through the assignments and reuses as many names as possible from the
previously used variable map.
|
private boolean |
shouldTemporarilyRenameLocalsInScope(Scope s)
Returns true if the local variables in a scope should be given
temporary names (eg, 'L 123') prior to renaming to allow reuse of
names across scopes.
|
private static final int MAX_LOCALS_IN_SCOPE_TO_TEMP_RENAME
preferStableNames
is true.private final AbstractCompiler compiler
private final java.util.ArrayList<Node> globalNameNodes
private final java.util.ArrayList<Node> localNameNodes
private final java.util.Map<Node,java.lang.String> pseudoNameMap
private final java.util.Set<java.lang.String> externNames
private final java.util.Set<java.lang.String> reservedNames
private final java.util.Map<java.lang.String,java.lang.String> renameMap
private final VariableMap prevUsedRenameMap
private final java.lang.String prefix
private int assignmentCount
private java.lang.StringBuilder assignmentLog
private final java.util.Set<Var> localBleedingFunctions
private final com.google.common.collect.ArrayListMultimap<Scope,Var> localBleedingFunctionsPerScope
private final java.util.Map<java.lang.String,RenameVars.Assignment> assignments
private final boolean localRenamingOnly
private final boolean preserveFunctionExpressionNames
NameAnonymousFunctions
private final boolean shouldShadow
private final boolean preferStableNames
private final char[] reservedCharacters
public static final java.lang.String LOCAL_VAR_PREFIX
private final NameGenerator nameGenerator
private static final java.util.Comparator<RenameVars.Assignment> FREQUENCY_COMPARATOR
private static final java.util.Comparator<RenameVars.Assignment> ORDER_OF_OCCURRENCE_COMPARATOR
RenameVars(AbstractCompiler compiler, java.lang.String prefix, boolean localRenamingOnly, boolean preserveFunctionExpressionNames, boolean generatePseudoNames, boolean shouldShadow, boolean preferStableNames, VariableMap prevUsedRenameMap, @Nullable char[] reservedCharacters, @Nullable java.util.Set<java.lang.String> reservedNames, NameGenerator nameGenerator)
public void process(Node externs, Node root)
CompilerPass
process
in interface CompilerPass
externs
- Top of external JS treeroot
- Top of JS treeprivate java.lang.String getNewGlobalName(Node n)
private java.lang.String getNewLocalName(Node n)
private void recordPseudoName(Node n)
private void reusePreviouslyUsedVariableMap()
private void assignNames(java.util.SortedSet<RenameVars.Assignment> varsToRename)
private void finalizeNameAssignment(RenameVars.Assignment a, java.lang.String newName)
VariableMap getVariableMap()
private boolean okToRenameVar(java.lang.String name, boolean isLocal)
private int getLocalVarIndex(Var v)
private boolean shouldTemporarilyRenameLocalsInScope(Scope s)
preferStableNames
, temporary
renaming is disabled if the number of locals in the scope is
above a heuristic threshold to allow effective reuse of rename
maps (see prevUsedRenameMap
). In scopes with many
variables the temporary name given to a variable is unlikely to
be the same temporary name used when the rename map was created.