Skip to content

Commit

Permalink
Move state out of DefaultPassConfig and into Compiler.
Browse files Browse the repository at this point in the history
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=160201722
  • Loading branch information
rluble authored and brad4d committed Jun 27, 2017
1 parent 1d36fa6 commit 59bc852
Show file tree
Hide file tree
Showing 9 changed files with 222 additions and 165 deletions.
44 changes: 37 additions & 7 deletions src/com/google/javascript/jscomp/AbstractCompiler.java
Expand Up @@ -92,16 +92,46 @@ public abstract class AbstractCompiler implements SourceExcerptProvider {
*/ */
abstract List<CompilerInput> getInputsInOrder(); abstract List<CompilerInput> getInputsInOrder();


/** //
* Adds exported names to keep track. // Intermediate state and results produced and needed by particular passes.
*/ // TODO(rluble): move these into the general structure for keeping state between pass runs.
//
/** Adds exported names to keep track. */
public abstract void addExportedNames(Set<String> exportedVariableNames); public abstract void addExportedNames(Set<String> exportedVariableNames);


/** /** Gets the names that have been exported. */
* Gets the names that have been exported so far.
*/
public abstract Set<String> getExportedNames(); public abstract Set<String> getExportedNames();


/** Sets the variable renaming map */
public abstract void setVariableMap(VariableMap variableMap);

/** Sets the property renaming map */
public abstract void setPropertyMap(VariableMap propertyMap);

/** Sets the string replacement map */
public abstract void setStringMap(VariableMap stringMap);

/** Sets the fully qualified function name and globally unique id mapping. */
public abstract void setFunctionNames(FunctionNames functionNames);

/** Gets the fully qualified function name and globally unique id mapping. */
public abstract FunctionNames getFunctionNames();

/** Sets the css names found during compilation. */
public abstract void setCssNames(Map<String, Integer> newCssNames);

/** Sets the id generator for cross-module motion. */
public abstract void setIdGeneratorMap(String serializedIdMappings);

/** Gets the id generator for cross-module motion. */
public abstract IdGenerator getCrossModuleIdGenerator();

/** Sets the naming map for anonymous functions */
public abstract void setAnonymousFunctionNameMap(VariableMap functionMap);
//
// End of intermediate state needed by passes.
//

static enum MostRecentTypechecker { static enum MostRecentTypechecker {
NONE, NONE,
OTI, OTI,
Expand Down
7 changes: 0 additions & 7 deletions src/com/google/javascript/jscomp/CleanupPasses.java
Expand Up @@ -33,8 +33,6 @@
*/ */
class CleanupPasses extends PassConfig { class CleanupPasses extends PassConfig {


private State state;

public CleanupPasses(CompilerOptions options) { public CleanupPasses(CompilerOptions options) {
super(options); super(options);
} }
Expand All @@ -48,11 +46,6 @@ protected List<PassFactory> getChecks() {
return checks; return checks;
} }


@Override
protected State getIntermediateState() {
return state;
}

@Override @Override
protected List<PassFactory> getOptimizations() { protected List<PassFactory> getOptimizations() {
return ImmutableList.of(); return ImmutableList.of();
Expand Down
148 changes: 125 additions & 23 deletions src/com/google/javascript/jscomp/Compiler.java
Expand Up @@ -1234,7 +1234,6 @@ void stopTracer(Tracer t, String passName) {
* Returns the result of the compilation. * Returns the result of the compilation.
*/ */
public Result getResult() { public Result getResult() {
PassConfig.State state = getPassConfig().getIntermediateState();
Set<SourceFile> transpiledFiles = new HashSet<>(); Set<SourceFile> transpiledFiles = new HashSet<>();
if (jsRoot != null) { if (jsRoot != null) {
for (Node scriptNode : jsRoot.children()) { for (Node scriptNode : jsRoot.children()) {
Expand All @@ -1244,9 +1243,9 @@ public Result getResult() {
} }
} }
return new Result(getErrors(), getWarnings(), debugLog.toString(), return new Result(getErrors(), getWarnings(), debugLog.toString(),
state.variableMap, state.propertyMap, this.variableMap, this.propertyMap,
state.anonymousFunctionNameMap, state.stringMap, functionInformationMap, this.anonymousFunctionNameMap, this.stringMap, this.functionInformationMap,
sourceMap, externExports, state.cssNames, state.idGeneratorMap, transpiledFiles); this.sourceMap, this.externExports, this.cssNames, this.idGeneratorMap, transpiledFiles);
} }


/** /**
Expand Down Expand Up @@ -2491,8 +2490,7 @@ void recordFunctionInformation() {
logger.fine("Recording function information"); logger.fine("Recording function information");
startPass("recordFunctionInformation"); startPass("recordFunctionInformation");
RecordFunctionInformation recordFunctionInfoPass = RecordFunctionInformation recordFunctionInfoPass =
new RecordFunctionInformation( new RecordFunctionInformation(this, this.functionNames);
this, getPassConfig().getIntermediateState().functionNames);
process(recordFunctionInfoPass); process(recordFunctionInfoPass);
functionInformationMap = recordFunctionInfoPass.getMap(); functionInformationMap = recordFunctionInfoPass.getMap();
endPass("recordFunctionInformation"); endPass("recordFunctionInformation");
Expand Down Expand Up @@ -2970,18 +2968,111 @@ public SourceMap getSourceMap() {
return sourceMap; return sourceMap;
} }


/**
* Ids for cross-module method stubbing, so that each method has
* a unique id.
*/
private IdGenerator crossModuleIdGenerator =
new IdGenerator();

/**
* Keys are arguments passed to getCssName() found during compilation; values
* are the number of times the key appeared as an argument to getCssName().
*/
private Map<String, Integer> cssNames = null;

/** The variable renaming map */
private VariableMap variableMap = null;

/** The property renaming map */
private VariableMap propertyMap = null;

/** The naming map for anonymous functions */
private VariableMap anonymousFunctionNameMap = null;

/** Fully qualified function names and globally unique ids */
private FunctionNames functionNames = null;

/** String replacement map */
private VariableMap stringMap = null;

/** Id generator map */
private String idGeneratorMap = null;

/** Names exported by goog.exportSymbol. */
private final Set<String> exportedNames = new LinkedHashSet<>();

@Override
public void setVariableMap(VariableMap variableMap) {
this.variableMap = variableMap;
}

VariableMap getVariableMap() { VariableMap getVariableMap() {
return getPassConfig().getIntermediateState().variableMap; return variableMap;
}

@Override
public void setPropertyMap(VariableMap propertyMap) {
this.propertyMap = propertyMap;
} }


VariableMap getPropertyMap() { VariableMap getPropertyMap() {
return getPassConfig().getIntermediateState().propertyMap; return this.propertyMap;
}

@Override
public void setStringMap(VariableMap stringMap) {
this.stringMap = stringMap;
}

@Override
public void setFunctionNames(FunctionNames functionNames) {
this.functionNames = functionNames;
}

@Override
public void setCssNames(Map<String, Integer> cssNames) {
this.cssNames = cssNames;
}

Map<String, Integer> getCssNames() {
return cssNames;
}

@Override
public void setIdGeneratorMap(String serializedIdMappings) {
this.idGeneratorMap = serializedIdMappings;
}

@Override
public IdGenerator getCrossModuleIdGenerator() {
return crossModuleIdGenerator;
}

@Override
public void setAnonymousFunctionNameMap(VariableMap functionMap) {
this.anonymousFunctionNameMap = functionMap;
}

@Override
public FunctionNames getFunctionNames() {
return functionNames;
} }


VariableMap getStringMap() { VariableMap getStringMap() {
return getPassConfig().getIntermediateState().stringMap; return this.stringMap;
} }



@Override
public void addExportedNames(Set<String> exportedNames) {
this.exportedNames.addAll(exportedNames);
}

@Override
public Set<String> getExportedNames() {
return exportedNames;
}
@Override @Override
CompilerOptions getOptions() { CompilerOptions getOptions() {
return options; return options;
Expand Down Expand Up @@ -3022,20 +3113,6 @@ List<CompilerInput> getInputsInOrder() {
return Collections.unmodifiableList(inputs); return Collections.unmodifiableList(inputs);
} }


/** Names exported by goog.exportSymbol. */
private final Set<String> exportedNames = new LinkedHashSet<>();

@Override
public void addExportedNames(Set<String> exportedNames) {
this.exportedNames.addAll(exportedNames);

}

@Override
public Set<String> getExportedNames() {
return exportedNames;
}

/** /**
* Returns an unmodifiable view of the compiler inputs indexed by id. * Returns an unmodifiable view of the compiler inputs indexed by id.
*/ */
Expand Down Expand Up @@ -3393,6 +3470,14 @@ private static class CompilerState implements Serializable {
private final List<JSModule> modules; private final List<JSModule> modules;
private final int uniqueNameId; private final int uniqueNameId;
private final Set<String> exportedNames; private final Set<String> exportedNames;
private final Map<String, Integer> cssNames;
private final VariableMap variableMap;
private final VariableMap propertyMap;
private final VariableMap anonymousFunctionaMap;
private final FunctionNames functioNames;
private final VariableMap stringMap;
private final String idGeneratorMap;
private final IdGenerator crossModuleIdGenerator;


CompilerState(Compiler compiler) { CompilerState(Compiler compiler) {
this.externsRoot = checkNotNull(compiler.externsRoot); this.externsRoot = checkNotNull(compiler.externsRoot);
Expand Down Expand Up @@ -3420,6 +3505,15 @@ private static class CompilerState implements Serializable {
this.modules = compiler.modules; this.modules = compiler.modules;
this.uniqueNameId = compiler.uniqueNameId; this.uniqueNameId = compiler.uniqueNameId;
this.exportedNames = compiler.exportedNames; this.exportedNames = compiler.exportedNames;
this.cssNames = compiler.cssNames;
this.variableMap = compiler.variableMap;
this.propertyMap = compiler.propertyMap;
this.anonymousFunctionaMap = compiler.anonymousFunctionNameMap;
this.functioNames = compiler.functionNames;
this.stringMap = compiler.stringMap;
this.idGeneratorMap = compiler.idGeneratorMap;
this.crossModuleIdGenerator = compiler.crossModuleIdGenerator;

} }
} }


Expand Down Expand Up @@ -3483,6 +3577,14 @@ public CompilerState call() throws Exception {
uniqueNameId = compilerState.uniqueNameId; uniqueNameId = compilerState.uniqueNameId;
exportedNames.clear(); exportedNames.clear();
exportedNames.addAll(compilerState.exportedNames); exportedNames.addAll(compilerState.exportedNames);
cssNames = compilerState.cssNames;
variableMap = compilerState.variableMap;
propertyMap = compilerState.propertyMap;
stringMap = compilerState.stringMap;
anonymousFunctionNameMap = compilerState.anonymousFunctionaMap;
idGeneratorMap = compilerState.idGeneratorMap;
crossModuleIdGenerator = compilerState.crossModuleIdGenerator;
functionNames = compilerState.functioNames;


// Reapply module names to deserialized modules // Reapply module names to deserialized modules
renameModules(newModules, modules); renameModules(newModules, modules);
Expand Down
25 changes: 0 additions & 25 deletions src/com/google/javascript/jscomp/CrossModuleMethodMotion.java
Expand Up @@ -21,7 +21,6 @@
import com.google.javascript.jscomp.AnalyzePrototypeProperties.Symbol; import com.google.javascript.jscomp.AnalyzePrototypeProperties.Symbol;
import com.google.javascript.rhino.IR; import com.google.javascript.rhino.IR;
import com.google.javascript.rhino.Node; import com.google.javascript.rhino.Node;
import java.io.Serializable;
import java.util.Collection; import java.util.Collection;
import java.util.Iterator; import java.util.Iterator;


Expand Down Expand Up @@ -255,28 +254,4 @@ static boolean hasUnmovableRedeclaration(NameInfo nameInfo, Property prop) {
} }
return false; return false;
} }

static class IdGenerator implements Serializable {
private static final long serialVersionUID = 0L;

/**
* Ids for cross-module method stubbing, so that each method has
* a unique id.
*/
private int currentId = 0;

/**
* Returns whether we've generated any new ids.
*/
boolean hasGeneratedAnyIds() {
return currentId != 0;
}

/**
* Creates a new id for stubbing a method.
*/
int newId() {
return currentId++;
}
}
} }

0 comments on commit 59bc852

Please sign in to comment.