Skip to content

Commit

Permalink
Mark code as normalized after variables are coalesced.
Browse files Browse the repository at this point in the history
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=163026332
  • Loading branch information
simran-arora authored and brad4d committed Jul 26, 2017
1 parent 26279c0 commit b2a1d45
Show file tree
Hide file tree
Showing 4 changed files with 189 additions and 162 deletions.
10 changes: 6 additions & 4 deletions src/com/google/javascript/jscomp/CoalesceVariableNames.java
Expand Up @@ -20,6 +20,7 @@
import static com.google.common.base.Preconditions.checkState;

import com.google.common.base.Joiner;
import com.google.javascript.jscomp.AbstractCompiler.LifeCycleStage;
import com.google.javascript.jscomp.ControlFlowGraph.AbstractCfgNodeTraversalCallback;
import com.google.javascript.jscomp.ControlFlowGraph.Branch;
import com.google.javascript.jscomp.DataFlowAnalysis.FlowState;
Expand Down Expand Up @@ -82,10 +83,10 @@ public int compare(Var v1, Var v2) {
* to foo, rename both variable to foo_bar.
*/
CoalesceVariableNames(AbstractCompiler compiler, boolean usePseudoNames) {
// The code is normalized at this point in the compilation process, however as a result of this
// pass, the code becomes unnormalized (since we are reusing variable names) and so we want to
// unconditionally mark the code as unnormalized. We mark unnormalized right before this pass.
checkState(!compiler.getLifeCycleStage().isNormalized());
// The code is normalized at this point in the compilation process. This allows us to use the
// fact that all variables have been given unique names. We can hoist coalesced variables to
// VARS because we know that shadowing can't occur.
checkState(compiler.getLifeCycleStage().isNormalized());

this.compiler = compiler;
colorings = new LinkedList<>();
Expand All @@ -98,6 +99,7 @@ public void process(Node externs, Node root) {
checkNotNull(externs);
checkNotNull(root);
NodeTraversal.traverseEs6(compiler, root, this);
compiler.setLifeCycleStage(LifeCycleStage.RAW);
}

private static boolean shouldOptimizeScope(NodeTraversal t) {
Expand Down
39 changes: 20 additions & 19 deletions src/com/google/javascript/jscomp/DefaultPassConfig.java
Expand Up @@ -828,11 +828,9 @@ protected List<PassFactory> getOptimizations() {
passes.add(aliasStrings);
}

// Passes after this point can no longer depend on normalized AST
// assumptions.
passes.add(markUnnormalized);

if (options.coalesceVariableNames) {
// Passes after this point can no longer depend on normalized AST
// assumptions because the code is marked as un-normalized
passes.add(coalesceVariableNames);

// coalesceVariables creates identity assignments and more redundant code
Expand All @@ -841,6 +839,10 @@ protected List<PassFactory> getOptimizations() {
if (options.foldConstants) {
passes.add(peepholeOptimizationsOnce);
}
} else {
// Passes after this point can no longer depend on normalized AST
// assumptions.
passes.add(markUnnormalized);
}

if (options.collapseVariableDeclarations) {
Expand Down Expand Up @@ -2830,25 +2832,24 @@ protected CompilerPass create(AbstractCompiler compiler) {
}
};

/**
* Mark the point at which the normalized AST assumptions no longer hold.
*/
/** Mark the point at which the normalized AST assumptions no longer hold. */
private final PassFactory markUnnormalized =
new PassFactory("markUnnormalized", true) {
@Override
protected CompilerPass create(final AbstractCompiler compiler) {
return new CompilerPass() {
@Override public void process(Node externs, Node root) {
compiler.setLifeCycleStage(LifeCycleStage.RAW);
@Override
protected CompilerPass create(final AbstractCompiler compiler) {
return new CompilerPass() {
@Override
public void process(Node externs, Node root) {
compiler.setLifeCycleStage(LifeCycleStage.RAW);
}
};
}
};
}

@Override
protected FeatureSet featureSet() {
return FeatureSet.latest();
}
};
@Override
protected FeatureSet featureSet() {
return FeatureSet.latest();
}
};

private final PassFactory hoistVars =
new PassFactory("hoistVars", true) {
Expand Down

0 comments on commit b2a1d45

Please sign in to comment.