Fixes several compiler problems related to varargs.
1) The original ICE would occur when not specifying any varargs at all to a vararg function. The problem was trying to get source info from a non-existent parameter. I fixed this, however...
2) Not specifying any params to a method with a single vararg would fail in a different way, because JDT would give us a null array, causing us to early out and not run the varargs synthesis. I fixed this by making fixing up the array to a length 0 and running the rest of the function. However...
3) This caused us to die processing a call to the implicit super constructor in the Enum class, which takes a String and int for name and ordinal. The synthetic args for these methods are added elsewhere and don't exist in JDT. I special-cased this special case with an early return and assertion.
Review by: bobv
git-svn-id: https://google-web-toolkit.googlecode.com/svn/trunk@1694 8db76d5a-ed1c-0410-87a9-c151d255dfc7
diff --git a/dev/core/src/com/google/gwt/dev/jjs/impl/GenerateJavaAST.java b/dev/core/src/com/google/gwt/dev/jjs/impl/GenerateJavaAST.java
index 1343814..5e742ed 100644
--- a/dev/core/src/com/google/gwt/dev/jjs/impl/GenerateJavaAST.java
+++ b/dev/core/src/com/google/gwt/dev/jjs/impl/GenerateJavaAST.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2007 Google Inc.
+ * 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
@@ -2000,45 +2000,53 @@
private void addCallArgs(Expression[] args, JMethodCall call,
MethodBinding binding) {
- if (args != null) {
- TypeBinding[] params = binding.parameters;
- int n = params.length;
+ if (args == null) {
+ args = new Expression[0];
+ }
- if (binding.isVarargs()) {
- // Do everything but the last arg.
- --n;
- }
+ TypeBinding[] params = binding.parameters;
+ int n = params.length;
- for (int i = 0; i < n; ++i) {
- call.getArgs().add(dispProcessExpression(args[i]));
- }
+ if (binding.isVarargs()) {
+ // Do everything but the last arg.
+ --n;
+ }
- if (binding.isVarargs()) {
- // Handle the last arg.
- JArrayType type = (JArrayType) typeMap.get(params[n]);
+ if (args.length < n) {
+ // Hackish, the super call to Enum() won't have enough parameters.
+ assert (call.getTarget().getName().equals("Enum"));
+ return;
+ }
- // See if there is only one arg and it's an array of the correct dims.
- if (args.length == n + 1) {
- JType lastArgType = (JType) typeMap.get(args[n].resolvedType);
- if (lastArgType instanceof JArrayType) {
- JArrayType lastArgArrayType = (JArrayType) lastArgType;
- if (lastArgArrayType.getDims() == type.getDims()) {
- // Looks like it's already an array.
- call.getArgs().add(dispProcessExpression(args[n]));
- return;
- }
+ ArrayList<JExpression> callArgs = call.getArgs();
+
+ for (int i = 0; i < n; ++i) {
+ callArgs.add(dispProcessExpression(args[i]));
+ }
+
+ if (binding.isVarargs()) {
+ // Handle the last arg.
+ JArrayType type = (JArrayType) typeMap.get(params[n]);
+
+ // See if there is only one arg and it's an array of the correct dims.
+ if (args.length == n + 1) {
+ JType lastArgType = (JType) typeMap.get(args[n].resolvedType);
+ if (lastArgType instanceof JArrayType) {
+ JArrayType lastArgArrayType = (JArrayType) lastArgType;
+ if (lastArgArrayType.getDims() == type.getDims()) {
+ // Looks like it's already an array.
+ callArgs.add(dispProcessExpression(args[n]));
+ return;
}
}
-
- // Fall through: must synthesize a new array allocation.
- SourceInfo info = makeSourceInfo(args[n]);
- JNewArray newArray = new JNewArray(program, info, type);
- newArray.initializers = new ArrayList<JExpression>();
- for (int i = n; i < args.length; ++i) {
- newArray.initializers.add(dispProcessExpression(args[i]));
- }
- call.getArgs().add(newArray);
}
+
+ JNewArray newArray = new JNewArray(program, call.getSourceInfo(), type);
+ newArray.initializers = new ArrayList<JExpression>();
+ for (int i = n; i < args.length; ++i) {
+ newArray.initializers.add(dispProcessExpression(args[i]));
+ }
+ callArgs.add(newArray);
}
}
diff --git a/user/test/com/google/gwt/dev/jjs/test/VarargsTest.java b/user/test/com/google/gwt/dev/jjs/test/VarargsTest.java
index da4d1ae..b824191 100644
--- a/user/test/com/google/gwt/dev/jjs/test/VarargsTest.java
+++ b/user/test/com/google/gwt/dev/jjs/test/VarargsTest.java
@@ -1,12 +1,12 @@
/*
- * Copyright 2007 Google Inc.
- *
+ * 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
@@ -21,7 +21,6 @@
/**
* Tests the new JDK 1.5 varargs functionality.
- *
*/
public class VarargsTest extends GWTTestCase {
@@ -30,17 +29,25 @@
}
public void testVararg() {
- String[] strings = new String[] { "1", "2", "3" };
- String[] results = vararg( "1", "2", "3" );
- assertTrue(Arrays.equals(results, strings));
+ String[] expected = new String[] {"1", "2", "3"};
+ String[] actual = vararg("1", "2", "3");
+ assertTrue(Arrays.equals(expected, actual));
+
+ expected = new String[] {};
+ actual = vararg();
+ assertTrue(Arrays.equals(expected, actual));
}
public void testVarargBoxing() {
- int[] ints = new int[] { 1, 2, 3 };
- int[] results = varargUnboxed( 1, 2, 3 );
- assertTrue(Arrays.equals(results, ints));
- int[] results2 = varargUnboxed( new Integer(1), 2, new Integer(3) );
- assertTrue(Arrays.equals(ints, results2));
+ int[] expected = new int[] {1, 2, 3};
+ int[] actual = varargUnboxed(1, 2, 3);
+ assertTrue(Arrays.equals(expected, actual));
+ actual = varargUnboxed(new Integer(1), 2, new Integer(3));
+ assertTrue(Arrays.equals(expected, actual));
+
+ expected = new int[] {};
+ actual = varargUnboxed();
+ assertTrue(Arrays.equals(expected, actual));
}
private String[] vararg(String... args) {