Give the generated entry method an enclosing class.  This
establishes the invariant that all methods have class.
 
 Review by: scottb
 

git-svn-id: https://google-web-toolkit.googlecode.com/svn/trunk@3736 8db76d5a-ed1c-0410-87a9-c151d255dfc7
diff --git a/dev/core/src/com/google/gwt/dev/jjs/JavaToJavaScriptCompiler.java b/dev/core/src/com/google/gwt/dev/jjs/JavaToJavaScriptCompiler.java
index 826df7b..fdc5741 100644
--- a/dev/core/src/com/google/gwt/dev/jjs/JavaToJavaScriptCompiler.java
+++ b/dev/core/src/com/google/gwt/dev/jjs/JavaToJavaScriptCompiler.java
@@ -154,8 +154,8 @@
       throws UnableToCompleteException {
     JMethod bootStrapMethod = program.createMethod(
         program.createSourceInfoSynthetic("Bootstrap method"),
-        "init".toCharArray(), null, program.getTypeVoid(), false, true, true,
-        false, false);
+        "init".toCharArray(), program.getIndexedType("EntryMethodHolder"),
+        program.getTypeVoid(), false, true, true, false, false);
     bootStrapMethod.freezeParamTypes();
 
     JMethodBody body = (JMethodBody) bootStrapMethod.getBody();
diff --git a/dev/core/src/com/google/gwt/dev/jjs/ast/JProgram.java b/dev/core/src/com/google/gwt/dev/jjs/ast/JProgram.java
index 4eca433..d4c81dc7 100644
--- a/dev/core/src/com/google/gwt/dev/jjs/ast/JProgram.java
+++ b/dev/core/src/com/google/gwt/dev/jjs/ast/JProgram.java
@@ -66,7 +66,8 @@
           "java.lang.Iterable", "java.util.Iterator",
           "com.google.gwt.core.client.GWT",
           "com.google.gwt.core.client.JavaScriptObject",
-          "com.google.gwt.lang.ClassLiteralHolder",}));
+          "com.google.gwt.lang.ClassLiteralHolder",
+          "com.google.gwt.lang.EntryMethodHolder",}));
 
   static final Map<String, Set<String>> traceMethods = new HashMap<String, Set<String>>();
 
@@ -269,6 +270,7 @@
   }
 
   public void addEntryMethod(JMethod entryPoint) {
+    assert entryPoint.isStatic();
     if (!entryMethods.contains(entryPoint)) {
       entryMethods.add(entryPoint);
     }
@@ -404,6 +406,7 @@
       JReferenceType enclosingType, JType returnType, boolean isAbstract,
       boolean isStatic, boolean isFinal, boolean isPrivate, boolean isNative) {
     assert (name != null);
+    assert (enclosingType != null);
     assert (returnType != null);
     assert (!isAbstract || !isNative);
 
@@ -863,7 +866,6 @@
 
   public void traverse(JVisitor visitor, Context ctx) {
     if (visitor.visit(this, ctx)) {
-      visitor.accept(entryMethods);
       visitor.accept(allTypes);
     }
     visitor.endVisit(this, ctx);
diff --git a/dev/core/src/com/google/gwt/dev/jjs/impl/GenerateJavaScriptAST.java b/dev/core/src/com/google/gwt/dev/jjs/impl/GenerateJavaScriptAST.java
index 23f9aa2..8224714 100644
--- a/dev/core/src/com/google/gwt/dev/jjs/impl/GenerateJavaScriptAST.java
+++ b/dev/core/src/com/google/gwt/dev/jjs/impl/GenerateJavaScriptAST.java
@@ -376,6 +376,18 @@
 
     private final JsName prototype = objectScope.declareName("prototype");
 
+    /**
+     * The JavaScript functions corresponding to the entry methods of the
+     * program ({@link JProgram#entryMethods}).
+     */
+    private JsFunction[] entryFunctions;
+
+    /**
+     * A reverse index for {@link JProgram#entryMethods}.  Each entry method
+     * is mapped to its integer index.
+     */
+    private Map<JMethod, Integer> entryMethodToIndex;
+
     {
       globalTemp.setObfuscatable(false);
       prototype.setObfuscatable(false);
@@ -835,6 +847,10 @@
       }
 
       push(jsFunc);
+      Integer entryIndex = entryMethodToIndex.get(x);
+      if (entryIndex != null) {
+        entryFunctions[entryIndex] = jsFunc;
+      }
       currentMethod = null;
     }
 
@@ -976,15 +992,7 @@
       // types don't push
 
       // Generate entry methods
-      List<JsFunction> entryFuncs = popList(x.entryMethods.size()); // entryMethods
-      for (int i = 0; i < entryFuncs.size(); ++i) {
-        JsFunction func = entryFuncs.get(i);
-        if (func != null) {
-          globalStmts.add(func.makeStmt());
-        }
-      }
-
-      generateGwtOnLoad(entryFuncs, globalStmts);
+      generateGwtOnLoad(entryFunctions, globalStmts);
       generateNullFunc(globalStmts);
 
       // Add a few things onto the beginning.
@@ -1128,6 +1136,16 @@
     }
 
     @Override
+    public boolean visit(JProgram x, Context ctx) {
+      entryFunctions = new JsFunction[x.entryMethods.size()];
+      entryMethodToIndex = new IdentityHashMap<JMethod, Integer>();
+      for (int i = 0; i < x.entryMethods.size(); i++) {
+        entryMethodToIndex.put(x.entryMethods.get(i), i);
+      }
+      return true;
+    }
+
+    @Override
     public boolean visit(JsniMethodBody x, Context ctx) {
       JsFunction jsFunc = x.getFunc();
 
@@ -1269,7 +1287,7 @@
       generateTypeId(x, globalStmts);
     }
 
-    private void generateGwtOnLoad(List<JsFunction> entryFuncs,
+    private void generateGwtOnLoad(JsFunction[] entryFuncs,
         List<JsStatement> globalStmts) {
       /**
        * <pre>
@@ -1319,8 +1337,7 @@
       JsBlock callBlock = new JsBlock(sourceInfo);
       jsIf.setElseStmt(callBlock);
       jsTry.setTryBlock(callBlock);
-      for (int i = 0; i < entryFuncs.size(); ++i) {
-        JsFunction func = entryFuncs.get(i);
+      for (JsFunction func : entryFuncs) {
         if (func != null) {
           JsInvocation call = new JsInvocation(sourceInfo);
           call.setQualifier(func.getName().makeRef(sourceInfo));
diff --git a/dev/core/src/com/google/gwt/dev/jjs/impl/Pruner.java b/dev/core/src/com/google/gwt/dev/jjs/impl/Pruner.java
index a9e3662..f3bb111 100644
--- a/dev/core/src/com/google/gwt/dev/jjs/impl/Pruner.java
+++ b/dev/core/src/com/google/gwt/dev/jjs/impl/Pruner.java
@@ -640,6 +640,18 @@
 
     @Override
     public boolean visit(final JMethod x, Context ctx) {
+      JReferenceType enclosingType = x.getEnclosingType();
+      if (program.isJavaScriptObject(enclosingType)) {
+        // Calls to JavaScriptObject types rescue those types.
+        boolean instance = !x.isStatic() || program.isStaticImpl(x);
+        rescue(enclosingType, true, instance);
+      }
+
+      if (x.isStatic()) {
+        // JLS 12.4.1: references to static methods rescue the enclosing class
+        rescue(enclosingType, true, false);
+      }
+
       if (x.isNative()) {
         // Manually rescue native parameter references
         final JsniMethodBody body = (JsniMethodBody) x.getBody();
@@ -667,17 +679,7 @@
 
     @Override
     public boolean visit(JMethodCall call, Context ctx) {
-      JMethod target = call.getTarget();
-      JReferenceType enclosingType = target.getEnclosingType();
-      if (program.isJavaScriptObject(enclosingType)) {
-        // Calls to JavaScriptObject types rescue those types.
-        boolean instance = !target.isStatic() || program.isStaticImpl(target);
-        rescue(enclosingType, true, instance);
-      } else if (target.isStatic()) {
-        // JLS 12.4.1: references to static methods rescue the enclosing class
-        rescue(enclosingType, true, false);
-      }
-      rescue(target);
+      rescue(call.getTarget());
       return true;
     }
 
diff --git a/dev/core/src/com/google/gwt/dev/jjs/impl/SourceGenerationVisitor.java b/dev/core/src/com/google/gwt/dev/jjs/impl/SourceGenerationVisitor.java
index fa8e23b..9817428 100644
--- a/dev/core/src/com/google/gwt/dev/jjs/impl/SourceGenerationVisitor.java
+++ b/dev/core/src/com/google/gwt/dev/jjs/impl/SourceGenerationVisitor.java
@@ -107,12 +107,6 @@
 
   @Override
   public boolean visit(JProgram x, Context ctx) {
-    for (int i = 0; i < x.entryMethods.size(); ++i) {
-      JMethod method = x.entryMethods.get(i);
-      accept(method);
-      newline();
-      newline();
-    }
     for (int i = 0; i < x.getDeclaredTypes().size(); ++i) {
       JReferenceType type = x.getDeclaredTypes().get(i);
       accept(type);
diff --git a/dev/core/super/com/google/gwt/dev/jjs/intrinsic/com/google/gwt/lang/EntryMethodHolder.java b/dev/core/super/com/google/gwt/dev/jjs/intrinsic/com/google/gwt/lang/EntryMethodHolder.java
new file mode 100644
index 0000000..8464c59
--- /dev/null
+++ b/dev/core/super/com/google/gwt/dev/jjs/intrinsic/com/google/gwt/lang/EntryMethodHolder.java
@@ -0,0 +1,23 @@
+/*
+ * Copyright 2008 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.lang;
+
+/**
+ * This class holds the boot strap entry method that the compiler generates.
+ */
+public class EntryMethodHolder {
+
+}