class PeepholeSubstituteAlternateSyntax extends AbstractPeepholeOptimization
Modifier and Type | Class and Description |
---|---|
private static class |
PeepholeSubstituteAlternateSyntax.FoldArrayAction |
Modifier and Type | Field and Description |
---|---|
(package private) static DiagnosticType |
INVALID_REGULAR_EXPRESSION_FLAGS |
private boolean |
late |
private static CodeGenerator |
REGEXP_ESCAPER |
private static Pattern |
REGEXP_FLAGS_RE |
private static com.google.common.collect.ImmutableSet<java.lang.String> |
STANDARD_OBJECT_CONSTRUCTORS |
private static int |
STRING_SPLIT_OVERHEAD |
Constructor and Description |
---|
PeepholeSubstituteAlternateSyntax(boolean late) |
Modifier and Type | Method and Description |
---|---|
private static void |
addParameterAfter(Node parameterList,
Node after) |
private boolean |
areSafeFlagsToFold(java.lang.String flags)
are the given flags safe to fold?
We don't fold the regular expression if global ('g') flag is on,
because in this case it isn't really a constant: its 'lastIndex'
property contains the state of last execution, so replacing
'new RegExp('foobar','g')' with '/foobar/g' may change the behavior of
the program if the RegExp is used inside a loop, for example.
|
private static boolean |
areValidRegexpFlags(java.lang.String flags)
are the given flags valid regular expression flags?
JavaScript recognizes several suffix flags for regular expressions,
'g' - global replace, 'i' - case insensitive, 'm' - multi-line.
|
private boolean |
canFoldStandardConstructors(Node n) |
(package private) static boolean |
containsUnicodeEscape(java.lang.String s)
true if the JavaScript string would contain a Unicode escape when written
out as the body of a regular expression literal.
|
private static PeepholeSubstituteAlternateSyntax.FoldArrayAction |
isSafeToFoldArrayConstructor(Node arg)
Checks if it is safe to fold Array() constructor into [].
|
private static Node |
makeForwardSlashBracketSafe(Node n)
returns a string node that can safely be rendered inside /brackets/.
|
Node |
optimizeSubtree(Node node)
Tries apply our various peephole minimizations on the passed in node.
|
private static java.lang.String |
pickDelimiter(java.lang.String[] strings)
Find a delimiter that does not occur in the given strings
|
private Node |
reduceSubstractionAssignment(Node n) |
private Node |
reduceTrueFalse(Node n) |
private Node |
tryFoldImmediateCallToBoundFunction(Node n) |
private Node |
tryFoldLiteralConstructor(Node n)
Replaces a new Array, Object, or RegExp node with a literal, unless the
call is to a local constructor function with the same name.
|
private Node |
tryFoldRegularExpressionConstructor(Node n) |
private Node |
tryFoldSimpleFunctionCall(Node n) |
private Node |
tryFoldStandardConstructors(Node n)
Fold "new Object()" to "Object()".
|
private Node |
tryMinimizeArrayLiteral(Node n) |
private Node |
tryMinimizeStringArrayLiteral(Node n) |
private Node |
tryMinimizeWindowRefs(Node node) |
private Node |
tryReduceReturn(Node n)
Reduce "return undefined" or "return void 0" to simply "return".
|
private Node |
tryReplaceUndefined(Node n)
Use "void 0" in place of "undefined"
|
private Node |
tryRotateAssociativeOperator(Node n) |
private Node |
trySplitComma(Node n) |
areDeclaredGlobalExternsOnWindow, areNodesEqualForInlining, beginTraversal, endTraversal, getCodingConvention, isASTNormalized, isEcmaScript5OrGreater, mayEffectMutableState, mayHaveSideEffects, nodeTypeMayHaveSideEffects, report, reportCodeChange
private static final CodeGenerator REGEXP_ESCAPER
private final boolean late
private static final int STRING_SPLIT_OVERHEAD
static final DiagnosticType INVALID_REGULAR_EXPRESSION_FLAGS
private static final com.google.common.collect.ImmutableSet<java.lang.String> STANDARD_OBJECT_CONSTRUCTORS
private static final Pattern REGEXP_FLAGS_RE
PeepholeSubstituteAlternateSyntax(boolean late)
late
- When late is false, this mean we are currently running before
most of the other optimizations. In this case we would avoid optimizations
that would make the code harder to analyze (such as using string splitting,
merging statements with commas, etc). When this is true, we would
do anything to minimize for size.public Node optimizeSubtree(Node node)
optimizeSubtree
in class AbstractPeepholeOptimization
node
- The subtree that will be optimized.subtree
.private Node tryReduceReturn(Node n)
private Node tryFoldStandardConstructors(Node n)
private boolean canFoldStandardConstructors(Node n)
n
.private Node tryFoldLiteralConstructor(Node n)
private static PeepholeSubstituteAlternateSyntax.FoldArrayAction isSafeToFoldArrayConstructor(Node arg)
private static java.lang.String pickDelimiter(java.lang.String[] strings)
strings
- The strings that must be separated.private static boolean areValidRegexpFlags(java.lang.String flags)
private boolean areSafeFlagsToFold(java.lang.String flags)
ECMAScript 5 explicitly disallows pooling of regular expression literals so
in ECMAScript 5, /foo/g
and new RegExp('foo', 'g')
are
equivalent.
From section 7.8.5:
"Then each time the literal is evaluated, a new object is created as if by
the expression new RegExp(Pattern, Flags) where RegExp is the standard
built-in constructor with that name."
private static Node makeForwardSlashBracketSafe(Node n)
static boolean containsUnicodeEscape(java.lang.String s)