From 3069daab935ad61a7f5b8955ef3587b66db331a0 Mon Sep 17 00:00:00 2001 From: blickly Date: Tue, 12 Jul 2016 07:50:14 -0700 Subject: [PATCH] Make state for all the type-based optimizations private to CompilerOptions. This paves the way for making the useTypeBasedOptimizations option turn them all on. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=127198720 --- .../javascript/jscomp/CompilationLevel.java | 10 +++++----- src/com/google/javascript/jscomp/Compiler.java | 16 ++++++++-------- .../javascript/jscomp/CompilerOptions.java | 18 +++++++++++++++--- .../jscomp/DartSuperAccessorsPass.java | 3 ++- .../javascript/jscomp/DefaultPassConfig.java | 6 +++--- .../javascript/jscomp/IntegrationTest.java | 8 ++++---- 6 files changed, 37 insertions(+), 24 deletions(-) diff --git a/src/com/google/javascript/jscomp/CompilationLevel.java b/src/com/google/javascript/jscomp/CompilationLevel.java index 1225a0b9574..bc3518392c1 100644 --- a/src/com/google/javascript/jscomp/CompilationLevel.java +++ b/src/com/google/javascript/jscomp/CompilationLevel.java @@ -183,13 +183,13 @@ public void setTypeBasedOptimizationOptions(CompilerOptions options) { switch (this) { case ADVANCED_OPTIMIZATIONS: options.inferTypes = true; - options.disambiguateProperties = true; - options.ambiguateProperties = true; - options.inlineProperties = true; - options.useTypesForOptimization = true; + options.setDisambiguateProperties(true); + options.setAmbiguateProperties(true); + options.setInlineProperties(true); + options.setUseTypesForOptimization(true); break; case SIMPLE_OPTIMIZATIONS: - options.useTypesForOptimization = true; + options.setUseTypesForOptimization(true); break; case WHITESPACE_ONLY: break; diff --git a/src/com/google/javascript/jscomp/Compiler.java b/src/com/google/javascript/jscomp/Compiler.java index 7c7bb8e84ce..7502b721f9f 100644 --- a/src/com/google/javascript/jscomp/Compiler.java +++ b/src/com/google/javascript/jscomp/Compiler.java @@ -316,15 +316,15 @@ public void initOptions(CompilerOptions options) { // Turn off type-based optimizations when type checking is off if (!options.checkTypes) { - options.disambiguateProperties = false; - options.ambiguateProperties = false; - options.inlineProperties = false; - options.useTypesForOptimization = false; + options.setDisambiguateProperties(false); + options.setAmbiguateProperties(false); + options.setInlineProperties(false); + options.setUseTypesForOptimization(false); } if (options.legacyCodeCompile) { - options.disambiguateProperties = false; - options.ambiguateProperties = false; + options.setDisambiguateProperties(false); + options.setAmbiguateProperties(false); options.useNonStrictWarningsGuard(); } @@ -1043,8 +1043,8 @@ public String get() { @Override boolean areNodesEqualForInlining(Node n1, Node n2) { - if (options.ambiguateProperties || - options.disambiguateProperties) { + if (options.shouldAmbiguateProperties() || + options.shouldDisambiguateProperties()) { // The type based optimizations require that type information is preserved // during other optimizations. return n1.isEquivalentToTyped(n2); diff --git a/src/com/google/javascript/jscomp/CompilerOptions.java b/src/com/google/javascript/jscomp/CompilerOptions.java index ffc2b820693..af8dc2fd57b 100644 --- a/src/com/google/javascript/jscomp/CompilerOptions.java +++ b/src/com/google/javascript/jscomp/CompilerOptions.java @@ -345,7 +345,7 @@ public void setLegacyCodeCompile(boolean legacy) { boolean assumeClosuresOnlyCaptureReferences; /** Inlines properties */ - boolean inlineProperties; + private boolean inlineProperties; /** Move code to a deeper module */ public boolean crossModuleCodeMotion; @@ -575,10 +575,10 @@ public void setCollapseObjectLiterals(boolean enabled) { * Rename properties to disambiguate between unrelated fields based on * type information. */ - public boolean disambiguateProperties; + private boolean disambiguateProperties; /** Rename unrelated properties to the same name to reduce code size. */ - public boolean ambiguateProperties; + private boolean ambiguateProperties; /** Input sourcemap files, indexed by the JS files they refer to */ ImmutableMap inputSourceMaps; @@ -1485,6 +1485,10 @@ public void setInlineProperties(boolean enable) { inlineProperties = enable; } + boolean shouldInlineProperties() { + return inlineProperties; + } + /** * Set the variable removal policy for the compiler. */ @@ -2213,10 +2217,18 @@ public void setDisambiguateProperties(boolean disambiguateProperties) { this.disambiguateProperties = disambiguateProperties; } + boolean shouldDisambiguateProperties() { + return this.disambiguateProperties; + } + public void setAmbiguateProperties(boolean ambiguateProperties) { this.ambiguateProperties = ambiguateProperties; } + boolean shouldAmbiguateProperties() { + return this.ambiguateProperties; + } + public void setAnonymousFunctionNaming( AnonymousFunctionNamingPolicy anonymousFunctionNaming) { this.anonymousFunctionNaming = anonymousFunctionNaming; diff --git a/src/com/google/javascript/jscomp/DartSuperAccessorsPass.java b/src/com/google/javascript/jscomp/DartSuperAccessorsPass.java index 5da1bb2befd..6d47239b400 100644 --- a/src/com/google/javascript/jscomp/DartSuperAccessorsPass.java +++ b/src/com/google/javascript/jscomp/DartSuperAccessorsPass.java @@ -56,7 +56,8 @@ public DartSuperAccessorsPass(AbstractCompiler compiler) { // We currently rely on JSCompiler_renameProperty, which is not type-aware. // We would need something like goog.reflect.object (with the super class type), // but right now this would yield much larger code. - Preconditions.checkState(!options.ambiguateProperties && !options.disambiguateProperties, + Preconditions.checkState( + !options.shouldAmbiguateProperties() && !options.shouldDisambiguateProperties(), "Dart super accessors pass is not compatible with property (dis)ambiguation yet"); } diff --git a/src/com/google/javascript/jscomp/DefaultPassConfig.java b/src/com/google/javascript/jscomp/DefaultPassConfig.java index f75c502e82b..ccd485a0114 100644 --- a/src/com/google/javascript/jscomp/DefaultPassConfig.java +++ b/src/com/google/javascript/jscomp/DefaultPassConfig.java @@ -579,7 +579,7 @@ protected List getOptimizations() { // soon after type checking, both so that it can make use of type // information and so that other passes can take advantage of the renamed // properties. - if (options.disambiguateProperties) { + if (options.shouldDisambiguateProperties()) { passes.add(disambiguateProperties); } @@ -731,7 +731,7 @@ protected List getOptimizations() { passes.add(extractPrototypeMemberDeclarations); } - if (options.ambiguateProperties && + if (options.shouldAmbiguateProperties() && (options.propertyRenaming == PropertyRenamingPolicy.ALL_UNQUOTED)) { passes.add(ambiguateProperties); } @@ -862,7 +862,7 @@ private List getMainOptimizationLoop() { passes.add(inlineFunctions); } - if (options.inlineProperties) { + if (options.shouldInlineProperties()) { passes.add(inlineProperties); } diff --git a/test/com/google/javascript/jscomp/IntegrationTest.java b/test/com/google/javascript/jscomp/IntegrationTest.java index 9c0a763d2ec..21e22141504 100644 --- a/test/com/google/javascript/jscomp/IntegrationTest.java +++ b/test/com/google/javascript/jscomp/IntegrationTest.java @@ -3240,7 +3240,7 @@ public void testInlineProperties() { "/** @constructor */\n" + "ns.C = function () {this.someProperty = 1}\n" + "alert(new ns.C().someProperty + new ns.C().someProperty);\n"; - assertTrue(options.inlineProperties); + assertTrue(options.shouldInlineProperties()); assertTrue(options.collapseProperties); // CollapseProperties used to prevent inlining this property. test(options, code, "alert(2);"); @@ -3260,7 +3260,7 @@ public void testGoogDefineClass1() { " constructor: function () {this.someProperty = 1}\n" + "});\n" + "alert(new ns.C().someProperty + new ns.C().someProperty);\n"; - assertTrue(options.inlineProperties); + assertTrue(options.shouldInlineProperties()); assertTrue(options.collapseProperties); // CollapseProperties used to prevent inlining this property. test(options, code, "alert(2);"); @@ -3279,7 +3279,7 @@ public void testGoogDefineClass2() { " constructor: function () {this.someProperty = 1}\n" + "});\n" + "alert(new C().someProperty + new C().someProperty);\n"; - assertTrue(options.inlineProperties); + assertTrue(options.shouldInlineProperties()); assertTrue(options.collapseProperties); // CollapseProperties used to prevent inlining this property. test(options, code, "alert(2);"); @@ -3304,7 +3304,7 @@ public void testGoogDefineClass3() { "});" + "var x = new C();\n" + "x.someMethod(x.someProperty);\n"; - assertTrue(options.inlineProperties); + assertTrue(options.shouldInlineProperties()); assertTrue(options.collapseProperties); // CollapseProperties used to prevent inlining this property. test(options, code, TypeValidator.TYPE_MISMATCH_WARNING);