Revision: 6052
Author: [email protected]
Date: Mon Aug 31 14:02:51 2009
Log: Make the GWT compiler pluggable through an  
experimental "x.compiler.class" configuration property.

This allows experimenting with the GWT compile chain.

Review by: spoon
http://code.google.com/p/google-web-toolkit/source/detail?r=6052

Added:
  /trunk/dev/core/src/com/google/gwt/dev/jjs/AbstractCompiler.java
  /trunk/dev/core/src/com/google/gwt/dev/jjs/JavaScriptCompiler.java
Modified:
  /trunk/dev/core/src/com/google/gwt/dev/CompilePerms.java
  /trunk/dev/core/src/com/google/gwt/dev/Permutation.java
  /trunk/dev/core/src/com/google/gwt/dev/Precompile.java
  /trunk/dev/core/src/com/google/gwt/dev/jjs/JavaToJavaScriptCompiler.java
  /trunk/dev/core/src/com/google/gwt/dev/jjs/UnifiedAst.java

=======================================
--- /dev/null
+++ /trunk/dev/core/src/com/google/gwt/dev/jjs/AbstractCompiler.java    Mon  
Aug 31 14:02:51 2009
@@ -0,0 +1,49 @@
+/*
+ * Copyright 2009 Google Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may  
not
+ * use this file except in compliance with the License. You may obtain a  
copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,  
WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations  
under
+ * the License.
+ */
+package com.google.gwt.dev.jjs;
+
+import com.google.gwt.core.ext.TreeLogger;
+import com.google.gwt.core.ext.UnableToCompleteException;
+import com.google.gwt.dev.cfg.ModuleDef;
+import com.google.gwt.dev.jdt.RebindPermutationOracle;
+
+/**
+ * A Compiler used to compile a GWT project into artifacts.
+ */
+public interface AbstractCompiler {
+  /**
+   * Performs a precompilation, returning an object that can then be used  
to
+   * compile individual permutations..
+   *
+   * @param logger the logger to use
+   * @param module the module to compile
+   * @param rpo the RebindPermutationOracle
+   * @param declEntryPts the set of entry classes declared in a GWT module;
+   *          these will be automatically rebound
+   * @param additionalRootTypes additional classes that should serve as  
code
+   *          roots; will not be rebound; may be <code>null</code>
+   * @param options the compiler options
+   * @param singlePermutation if true, do not pre-optimize the resulting  
AST or
+   *          allow serialization of the result
+   * @return the unified AST used to drive permutation compiles
+   * @throws UnableToCompleteException if an error other than
+   *           {...@link OutOfMemoryError} occurs
+   */
+  UnifiedAst precompile(TreeLogger logger, ModuleDef module,
+      RebindPermutationOracle rpo, String[] declEntryPts,
+      String[] additionalRootTypes, JJSOptions options,
+      boolean singlePermutation) throws UnableToCompleteException;
+}
=======================================
--- /dev/null
+++ /trunk/dev/core/src/com/google/gwt/dev/jjs/JavaScriptCompiler.java  Mon  
Aug 31 14:02:51 2009
@@ -0,0 +1,36 @@
+/*
+ * Copyright 2009 Google Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may  
not
+ * use this file except in compliance with the License. You may obtain a  
copy of
+ * the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,  
WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations  
under
+ * the License.
+ */
+package com.google.gwt.dev.jjs;
+
+import com.google.gwt.core.ext.TreeLogger;
+import com.google.gwt.core.ext.UnableToCompleteException;
+import com.google.gwt.dev.cfg.ModuleDef;
+import com.google.gwt.dev.jdt.RebindPermutationOracle;
+
+/**
+ * Uses the default compiler {...@link JavaToJavaScriptCompiler}.
+ */
+public class JavaScriptCompiler implements AbstractCompiler {
+
+  public UnifiedAst precompile(TreeLogger logger, ModuleDef module,
+      RebindPermutationOracle rpo, String[] declEntryPts,
+      String[] additionalRootTypes, JJSOptions options,
+      boolean singlePermutation) throws UnableToCompleteException {
+    return JavaToJavaScriptCompiler.precompile(logger, module, rpo,
+        declEntryPts, additionalRootTypes, options, singlePermutation);
+  }
+
+}
=======================================
--- /trunk/dev/core/src/com/google/gwt/dev/CompilePerms.java    Tue Jul 28  
12:27:56 2009
+++ /trunk/dev/core/src/com/google/gwt/dev/CompilePerms.java    Mon Aug 31  
14:02:51 2009
@@ -18,7 +18,6 @@
  import com.google.gwt.core.ext.TreeLogger;
  import com.google.gwt.core.ext.UnableToCompleteException;
  import com.google.gwt.dev.CompileTaskRunner.CompileTask;
-import com.google.gwt.dev.jjs.JavaToJavaScriptCompiler;
  import com.google.gwt.dev.jjs.PermutationResult;
  import com.google.gwt.dev.jjs.UnifiedAst;
  import com.google.gwt.dev.util.FileBackedObject;
@@ -185,7 +184,7 @@
    public static PermutationResult compile(TreeLogger logger,
        Permutation permutation, UnifiedAst unifiedAst)
        throws UnableToCompleteException {
-    return JavaToJavaScriptCompiler.compilePermutation(logger, unifiedAst,
+    return unifiedAst.compilePermutation(logger,
          permutation.getRebindAnswers(), permutation.getPropertyOracles(),
          permutation.getId());
    }
=======================================
--- /trunk/dev/core/src/com/google/gwt/dev/Permutation.java     Tue Dec 23  
16:45:21 2008
+++ /trunk/dev/core/src/com/google/gwt/dev/Permutation.java     Mon Aug 31  
14:02:51 2009
@@ -19,9 +19,10 @@

  import java.io.Serializable;
  import java.util.ArrayList;
+import java.util.Collections;
  import java.util.List;
-import java.util.Set;
  import java.util.SortedMap;
+import java.util.SortedSet;
  import java.util.TreeMap;

  /**
@@ -58,11 +59,17 @@
    }

    public SortedMap<String, String> getRebindAnswers() {
-    return rebindAnswers;
+    return Collections.unmodifiableSortedMap(rebindAnswers);
    }

-  public void mergeFrom(Permutation other) {
-    assert rebindAnswers.equals(other.rebindAnswers);
+  public void mergeFrom(Permutation other, SortedSet<String>  
liveRebindRequests) {
+    if (getClass().desiredAssertionStatus()) {
+      for (String rebindRequest : liveRebindRequests) {
+        String myAnswer = rebindAnswers.get(rebindRequest);
+        String otherAnswer = other.rebindAnswers.get(rebindRequest);
+        assert myAnswer.equals(otherAnswer);
+      }
+    }
      assert !propertyOracles.isEmpty();
      assert !other.propertyOracles.isEmpty();
      propertyOracles.addAll(other.propertyOracles);
@@ -72,8 +79,4 @@
    public void putRebindAnswer(String requestType, String resultType) {
      rebindAnswers.put(requestType, resultType);
    }
-
-  public void reduceRebindAnswers(Set<String> liveRebindRequests) {
-    rebindAnswers.keySet().retainAll(liveRebindRequests);
-  }
-}
+}
=======================================
--- /trunk/dev/core/src/com/google/gwt/dev/Precompile.java      Mon Aug 17  
09:47:12 2009
+++ /trunk/dev/core/src/com/google/gwt/dev/Precompile.java      Mon Aug 31  
14:02:51 2009
@@ -31,9 +31,10 @@
  import com.google.gwt.dev.javac.StandardGeneratorContext;
  import com.google.gwt.dev.jdt.RebindOracle;
  import com.google.gwt.dev.jdt.RebindPermutationOracle;
+import com.google.gwt.dev.jjs.AbstractCompiler;
  import com.google.gwt.dev.jjs.JJSOptions;
  import com.google.gwt.dev.jjs.JJSOptionsImpl;
-import com.google.gwt.dev.jjs.JavaToJavaScriptCompiler;
+import com.google.gwt.dev.jjs.JavaScriptCompiler;
  import com.google.gwt.dev.jjs.JsOutputOption;
  import com.google.gwt.dev.jjs.UnifiedAst;
  import com.google.gwt.dev.shell.CheckForUpdates;
@@ -448,7 +449,7 @@
            generatorResourcesDir);
        // Never optimize on a validation run.
        jjsOptions.setOptimizePrecompile(false);
-      JavaToJavaScriptCompiler.precompile(logger, module, rpo,  
declEntryPts,
+      getCompiler(module).precompile(logger, module, rpo, declEntryPts,
            additionalRootTypes, jjsOptions, true);
        return true;
      } catch (UnableToCompleteException e) {
@@ -456,6 +457,28 @@
        return false;
      }
    }
+
+  private static AbstractCompiler getCompiler(ModuleDef module) {
+    ConfigurationProperty compilerClassProp =  
module.getProperties().createConfiguration(
+        "x.compiler.class", false);
+    String compilerClassName = compilerClassProp.getValue();
+    if (compilerClassName == null || compilerClassName.length() == 0) {
+      return new JavaScriptCompiler();
+    }
+    Throwable caught;
+    try {
+      Class<?> compilerClass = Class.forName(compilerClassName);
+      return (AbstractCompiler) compilerClass.newInstance();
+    } catch (ClassNotFoundException e) {
+      caught = e;
+    } catch (InstantiationException e) {
+      caught = e;
+    } catch (IllegalAccessException e) {
+      caught = e;
+    }
+    throw new RuntimeException("Unable to instantiate compiler class '"
+        + compilerClassName + "'", caught);
+  }

    private static Precompilation precompile(TreeLogger logger,
        JJSOptions jjsOptions, ModuleDef module, int permutationBase,
@@ -481,7 +504,7 @@
            module, compilationState, generatedArtifacts, allPermutations,
            genDir, generatorResourcesDir);
        PerfLogger.start("Precompile");
-      UnifiedAst unifiedAst = JavaToJavaScriptCompiler.precompile(logger,
+      UnifiedAst unifiedAst = getCompiler(module).precompile(logger,
            module, rpo, declEntryPts, null, jjsOptions,
            rpo.getPermuationCount() == 1);
        PerfLogger.end();
@@ -490,15 +513,18 @@
        Permutation[] permutations = rpo.getPermutations();
        // Sort the permutations by an ordered key to ensure determinism.
        SortedMap<String, Permutation> merged = new TreeMap<String,  
Permutation>();
+      SortedSet<String> liveRebindRequests =  
unifiedAst.getRebindRequests();
        for (Permutation permutation : permutations) {
-        permutation.reduceRebindAnswers(unifiedAst.getRebindRequests());
-        // Arbitrarily choose as a key the stringified map of rebind  
answers.
-        String rebindResultsString =  
permutation.getRebindAnswers().toString();
-        if (merged.containsKey(rebindResultsString)) {
-          Permutation existing = merged.get(rebindResultsString);
-          existing.mergeFrom(permutation);
+        // Construct a key from the stringified map of live rebind answers.
+        SortedMap<String, String> rebindAnswers = new TreeMap<String,  
String>(
+            permutation.getRebindAnswers());
+        rebindAnswers.keySet().retainAll(liveRebindRequests);
+        String key = rebindAnswers.toString();
+        if (merged.containsKey(key)) {
+          Permutation existing = merged.get(key);
+          existing.mergeFrom(permutation, liveRebindRequests);
          } else {
-          merged.put(rebindResultsString, permutation);
+          merged.put(key, permutation);
          }
        }
        return new Precompilation(unifiedAst, merged.values(),  
permutationBase,
=======================================
---  
/trunk/dev/core/src/com/google/gwt/dev/jjs/JavaToJavaScriptCompiler.java        
 
Mon Aug 17 13:00:36 2009
+++  
/trunk/dev/core/src/com/google/gwt/dev/jjs/JavaToJavaScriptCompiler.java        
 
Mon Aug 31 14:02:51 2009
@@ -191,9 +191,10 @@
     * @param unifiedAst the result of a
     *          {...@link #precompile(TreeLogger, WebModeCompilerFrontEnd,  
String[], JJSOptions, boolean)}
     * @param rebindAnswers the set of rebind answers to resolve all  
outstanding
-   *          rebind decisions
-   * @param propertyOracles All property oracles corresponding to this
-   *          permutation.
+   *          rebind decisions for this permutation
+   * @param propertyOracles all property oracles corresponding to this
+   *          permutation
+   * @param permutationId the unique id of this permutation
     * @return the output JavaScript
     * @throws UnableToCompleteException if an error other than
     *           {...@link OutOfMemoryError} occurs
=======================================
--- /trunk/dev/core/src/com/google/gwt/dev/jjs/UnifiedAst.java  Fri Nov 21  
07:53:38 2008
+++ /trunk/dev/core/src/com/google/gwt/dev/jjs/UnifiedAst.java  Mon Aug 31  
14:02:51 2009
@@ -15,6 +15,9 @@
   */
  package com.google.gwt.dev.jjs;

+import com.google.gwt.core.ext.PropertyOracle;
+import com.google.gwt.core.ext.TreeLogger;
+import com.google.gwt.core.ext.UnableToCompleteException;
  import com.google.gwt.dev.jjs.ast.JProgram;
  import com.google.gwt.dev.js.ast.JsProgram;
  import com.google.gwt.dev.util.PerfLogger;
@@ -26,6 +29,7 @@
  import java.io.ObjectOutputStream;
  import java.io.Serializable;
  import java.util.Collections;
+import java.util.Map;
  import java.util.Set;
  import java.util.SortedSet;
  import java.util.TreeSet;
@@ -34,7 +38,7 @@
   * Represents a unified, non-permutation specific AST. This AST is used to  
drive
   * per-permutation compiles.
   */
-public final class UnifiedAst implements Serializable {
+public class UnifiedAst implements Serializable {

    /**
     * Encapsulates the combined programs.
@@ -128,6 +132,37 @@
          rebindRequests));
      this.serializedAst = singlePermutation ? null :  
serializeAst(initialAst);
    }
+
+  /**
+   * Copy constructor, invalidates the original.
+   */
+  UnifiedAst(UnifiedAst other) {
+    this.options = other.options;
+    this.initialAst = other.initialAst;
+    other.initialAst = null; // steal its copy
+    this.rebindRequests = other.rebindRequests;
+    this.serializedAst = other.serializedAst;
+  }
+
+  /**
+   * Compiles a particular permutation.
+   *
+   * @param logger the logger to use
+   * @param rebindAnswers the set of rebind answers to resolve all  
outstanding
+   *          rebind decisions for this permutation
+   * @param propertyOracles all property oracles corresponding to this
+   *          permutation
+   * @param permutationId the unique id of this permutation
+   * @return the permutation result
+   * @throws UnableToCompleteException if an error other than
+   *           {...@link OutOfMemoryError} occurs
+   */
+  public PermutationResult compilePermutation(TreeLogger logger,
+      Map<String, String> rebindAnswers, PropertyOracle[] propertyOracles,
+      int permutationId) throws UnableToCompleteException {
+    return JavaToJavaScriptCompiler.compilePermutation(logger, this,
+        rebindAnswers, propertyOracles, permutationId);
+  }

    /**
     * Returns the active set of JJS options associated with this compile.

--~--~---------~--~----~------------~-------~--~----~
http://groups.google.com/group/Google-Web-Toolkit-Contributors
-~----------~----~----~----~------~----~------~--~---

Reply via email to