- All Implemented Interfaces:
- CompilerPass
final class RenameLabels
extends java.lang.Object
implements CompilerPass
RenameLabels renames all the labels so that they have short names, to reduce
code size and also to obfuscate the code.
Label names have a unique namespace, so variable or function names clashes
are not a concern, but keywords clashes are.
Additionally, labels names are only within the statements include in the
label and do not cross function boundaries. This means that it is possible to
create one label name that is used for labels at any given depth of label
nesting. Typically, the name "a" will be used for all top-level labels, "b"
for the next nested label, and so on. For example:
function bar() {
a: {
b: {
foo();
}
}
a: {
b: break a;
}
}
The general processes is as follows: process() is the entry point for the
CompilerPass, and from there a standard "ScopedCallback" traversal is done,
where "shouldTraverse" is called when descending the tree, and the "visit" is
called in a depth first manner. The name for the label is selected during the
decent in "shouldTraverse", and the references to the label name are renamed
as they are encountered during the "visit". This means that if the label is
unreferenced, it is known when the label node is visited, and, if so, can be
safely removed.