Generated default implementations for Collection.toArray().
#KT-3352 in progress
This commit is contained in:
@@ -51,6 +51,7 @@ import org.jetbrains.jet.lang.resolve.BindingContext;
|
|||||||
import org.jetbrains.jet.lang.resolve.BindingContextUtils;
|
import org.jetbrains.jet.lang.resolve.BindingContextUtils;
|
||||||
import org.jetbrains.jet.lang.resolve.DescriptorUtils;
|
import org.jetbrains.jet.lang.resolve.DescriptorUtils;
|
||||||
import org.jetbrains.jet.lang.resolve.OverridingUtil;
|
import org.jetbrains.jet.lang.resolve.OverridingUtil;
|
||||||
|
import org.jetbrains.jet.lang.resolve.calls.CallResolverUtil;
|
||||||
import org.jetbrains.jet.lang.resolve.calls.model.ResolvedCall;
|
import org.jetbrains.jet.lang.resolve.calls.model.ResolvedCall;
|
||||||
import org.jetbrains.jet.lang.resolve.constants.CompileTimeConstant;
|
import org.jetbrains.jet.lang.resolve.constants.CompileTimeConstant;
|
||||||
import org.jetbrains.jet.lang.resolve.java.AsmTypeConstants;
|
import org.jetbrains.jet.lang.resolve.java.AsmTypeConstants;
|
||||||
@@ -59,6 +60,7 @@ import org.jetbrains.jet.lang.resolve.java.JvmAnnotationNames;
|
|||||||
import org.jetbrains.jet.lang.resolve.java.JvmClassName;
|
import org.jetbrains.jet.lang.resolve.java.JvmClassName;
|
||||||
import org.jetbrains.jet.lang.resolve.name.Name;
|
import org.jetbrains.jet.lang.resolve.name.Name;
|
||||||
import org.jetbrains.jet.lang.types.*;
|
import org.jetbrains.jet.lang.types.*;
|
||||||
|
import org.jetbrains.jet.lang.types.checker.JetTypeChecker;
|
||||||
import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns;
|
import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns;
|
||||||
import org.jetbrains.jet.lexer.JetTokens;
|
import org.jetbrains.jet.lexer.JetTokens;
|
||||||
|
|
||||||
@@ -462,9 +464,67 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen {
|
|||||||
|
|
||||||
generateBuiltinMethodStubs();
|
generateBuiltinMethodStubs();
|
||||||
|
|
||||||
|
generateToArray();
|
||||||
|
|
||||||
genClosureFields(context.closure, v, state.getTypeMapper());
|
genClosureFields(context.closure, v, state.getTypeMapper());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private boolean isGenericToArrayPresent() {
|
||||||
|
Collection<FunctionDescriptor> functions = descriptor.getDefaultType().getMemberScope().getFunctions(Name.identifier("toArray"));
|
||||||
|
for (FunctionDescriptor function : functions) {
|
||||||
|
if (CallResolverUtil.isOrOverridesSynthesized(function)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (function.getValueParameters().size() != 1 || function.getTypeParameters().size() != 1) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
JetType arrayType = KotlinBuiltIns.getInstance().getArrayType(function.getTypeParameters().get(0).getDefaultType());
|
||||||
|
JetType returnType = function.getReturnType();
|
||||||
|
assert returnType != null : function.toString();
|
||||||
|
JetType paramType = function.getValueParameters().get(0).getType();
|
||||||
|
if (JetTypeChecker.INSTANCE.equalTypes(arrayType, returnType) && JetTypeChecker.INSTANCE.equalTypes(arrayType, paramType)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private void generateToArray() {
|
||||||
|
KotlinBuiltIns builtIns = KotlinBuiltIns.getInstance();
|
||||||
|
if (isSubclass(descriptor, builtIns.getCollection())) {
|
||||||
|
if (CodegenUtil.getDeclaredFunctionByRawSignature(descriptor, Name.identifier("toArray"), builtIns.getArray()) == null) {
|
||||||
|
MethodVisitor mv = v.getVisitor().visitMethod(ACC_PUBLIC, "toArray", "()[Ljava/lang/Object;", null, null);
|
||||||
|
InstructionAdapter iv = new InstructionAdapter(mv);
|
||||||
|
|
||||||
|
mv.visitCode();
|
||||||
|
|
||||||
|
iv.load(0, classAsmType);
|
||||||
|
iv.invokestatic("jet/runtime/CollectionToArray", "toArray", "(Ljava/util/Collection;)[Ljava/lang/Object;");
|
||||||
|
iv.areturn(Type.getObjectType("[Ljava/lang/Object;"));
|
||||||
|
|
||||||
|
FunctionCodegen.endVisit(mv, "toArray", myClass);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!isGenericToArrayPresent()) {
|
||||||
|
MethodVisitor mv = v.getVisitor().visitMethod(ACC_PUBLIC, "toArray", "([Ljava/lang/Object;)[Ljava/lang/Object;", null, null);
|
||||||
|
InstructionAdapter iv = new InstructionAdapter(mv);
|
||||||
|
|
||||||
|
mv.visitCode();
|
||||||
|
|
||||||
|
iv.load(0, classAsmType);
|
||||||
|
iv.load(1, Type.getObjectType("[Ljava/lang/Object;"));
|
||||||
|
|
||||||
|
iv.invokestatic("jet/runtime/CollectionToArray", "toArray", "(Ljava/util/Collection;[Ljava/lang/Object;)[Ljava/lang/Object;");
|
||||||
|
iv.areturn(Type.getObjectType("[Ljava/lang/Object;"));
|
||||||
|
|
||||||
|
FunctionCodegen.endVisit(mv, "toArray", myClass);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void generateMethodStub(
|
private void generateMethodStub(
|
||||||
@NotNull String name,
|
@NotNull String name,
|
||||||
@NotNull String desc,
|
@NotNull String desc,
|
||||||
|
|||||||
@@ -0,0 +1,21 @@
|
|||||||
|
import java.util.Arrays
|
||||||
|
|
||||||
|
class MyCollection<T>(val delegate: Collection<T>): Collection<T> by delegate
|
||||||
|
|
||||||
|
fun box(): String {
|
||||||
|
val collection = MyCollection(Arrays.asList(2, 3, 9)) as java.util.Collection<*>
|
||||||
|
|
||||||
|
val array1 = collection.toArray()
|
||||||
|
val array2 = collection.toArray(Array<Int>(3, { 0 }))
|
||||||
|
|
||||||
|
if (array1 !is Array<Any>) return (array1 as Object).getClass().toString()
|
||||||
|
if (array2 !is Array<Int>) return (array2 as Object).getClass().toString()
|
||||||
|
|
||||||
|
val s1 = Arrays.toString(array1)
|
||||||
|
val s2 = Arrays.toString(array2)
|
||||||
|
|
||||||
|
if (s1 != "[2, 3, 9]") return "s1 = $s1"
|
||||||
|
if (s2 != "[2, 3, 9]") return "s2 = $s2"
|
||||||
|
|
||||||
|
return "OK"
|
||||||
|
}
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
import java.util.Arrays
|
||||||
|
|
||||||
|
class MyCollection<T>(val delegate: Collection<T>): Collection<T> by delegate {
|
||||||
|
public fun toArray(): Array<Any?> = Array<Any?>(3, { it })
|
||||||
|
public fun <E> toArray(array: Array<E>): Array<E> {
|
||||||
|
val asIntArray = array as Array<Int>
|
||||||
|
asIntArray[0] = 0
|
||||||
|
asIntArray[1] = 1
|
||||||
|
asIntArray[2] = 2
|
||||||
|
return array
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun box(): String {
|
||||||
|
val collection = MyCollection(Arrays.asList(2, 3, 9)) as java.util.Collection<*>
|
||||||
|
|
||||||
|
val array1 = collection.toArray()
|
||||||
|
val array2 = collection.toArray(Array<Int>(3, { 0 }))
|
||||||
|
|
||||||
|
if (array1 !is Array<Any>) return (array1 as Object).getClass().toString()
|
||||||
|
if (array2 !is Array<Int>) return (array2 as Object).getClass().toString()
|
||||||
|
|
||||||
|
val s1 = Arrays.toString(array1)
|
||||||
|
val s2 = Arrays.toString(array2)
|
||||||
|
|
||||||
|
if (s1 != "[0, 1, 2]") return "s1 = $s1"
|
||||||
|
if (s2 != "[0, 1, 2]") return "s2 = $s2"
|
||||||
|
|
||||||
|
return "OK"
|
||||||
|
}
|
||||||
+20
-1
@@ -31,7 +31,7 @@ import org.jetbrains.jet.codegen.generated.AbstractBlackBoxCodegenTest;
|
|||||||
/** This class is generated by {@link org.jetbrains.jet.generators.tests.GenerateTests}. DO NOT MODIFY MANUALLY */
|
/** This class is generated by {@link org.jetbrains.jet.generators.tests.GenerateTests}. DO NOT MODIFY MANUALLY */
|
||||||
@SuppressWarnings("all")
|
@SuppressWarnings("all")
|
||||||
@TestMetadata("compiler/testData/codegen/box")
|
@TestMetadata("compiler/testData/codegen/box")
|
||||||
@InnerTestClasses({BlackBoxCodegenTestGenerated.Arrays.class, BlackBoxCodegenTestGenerated.Bridges.class, BlackBoxCodegenTestGenerated.BuiltinStubMethods.class, BlackBoxCodegenTestGenerated.CallableReference.class, BlackBoxCodegenTestGenerated.Casts.class, BlackBoxCodegenTestGenerated.Classes.class, BlackBoxCodegenTestGenerated.Closures.class, BlackBoxCodegenTestGenerated.Constants.class, BlackBoxCodegenTestGenerated.ControlStructures.class, BlackBoxCodegenTestGenerated.DefaultArguments.class, BlackBoxCodegenTestGenerated.DelegatedProperty.class, BlackBoxCodegenTestGenerated.Elvis.class, BlackBoxCodegenTestGenerated.Enum.class, BlackBoxCodegenTestGenerated.ExclExcl.class, BlackBoxCodegenTestGenerated.ExtensionFunctions.class, BlackBoxCodegenTestGenerated.ExtensionProperties.class, BlackBoxCodegenTestGenerated.FieldRename.class, BlackBoxCodegenTestGenerated.Finally.class, BlackBoxCodegenTestGenerated.Functions.class, BlackBoxCodegenTestGenerated.InnerNested.class, BlackBoxCodegenTestGenerated.Instructions.class, BlackBoxCodegenTestGenerated.Intrinsics.class, BlackBoxCodegenTestGenerated.Labels.class, BlackBoxCodegenTestGenerated.LocalClasses.class, BlackBoxCodegenTestGenerated.MultiDecl.class, BlackBoxCodegenTestGenerated.Namespace.class, BlackBoxCodegenTestGenerated.Objects.class, BlackBoxCodegenTestGenerated.OperatorConventions.class, BlackBoxCodegenTestGenerated.PrimitiveTypes.class, BlackBoxCodegenTestGenerated.Properties.class, BlackBoxCodegenTestGenerated.Reflection.class, BlackBoxCodegenTestGenerated.SafeCall.class, BlackBoxCodegenTestGenerated.SamConstructors.class, BlackBoxCodegenTestGenerated.Strings.class, BlackBoxCodegenTestGenerated.Super.class, BlackBoxCodegenTestGenerated.Traits.class, BlackBoxCodegenTestGenerated.TypeInfo.class, BlackBoxCodegenTestGenerated.Unit.class, BlackBoxCodegenTestGenerated.Vararg.class, BlackBoxCodegenTestGenerated.When.class})
|
@InnerTestClasses({BlackBoxCodegenTestGenerated.Arrays.class, BlackBoxCodegenTestGenerated.Bridges.class, BlackBoxCodegenTestGenerated.BuiltinStubMethods.class, BlackBoxCodegenTestGenerated.CallableReference.class, BlackBoxCodegenTestGenerated.Casts.class, BlackBoxCodegenTestGenerated.Classes.class, BlackBoxCodegenTestGenerated.Closures.class, BlackBoxCodegenTestGenerated.Constants.class, BlackBoxCodegenTestGenerated.ControlStructures.class, BlackBoxCodegenTestGenerated.DefaultArguments.class, BlackBoxCodegenTestGenerated.DelegatedProperty.class, BlackBoxCodegenTestGenerated.Elvis.class, BlackBoxCodegenTestGenerated.Enum.class, BlackBoxCodegenTestGenerated.ExclExcl.class, BlackBoxCodegenTestGenerated.ExtensionFunctions.class, BlackBoxCodegenTestGenerated.ExtensionProperties.class, BlackBoxCodegenTestGenerated.FieldRename.class, BlackBoxCodegenTestGenerated.Finally.class, BlackBoxCodegenTestGenerated.Functions.class, BlackBoxCodegenTestGenerated.InnerNested.class, BlackBoxCodegenTestGenerated.Instructions.class, BlackBoxCodegenTestGenerated.Intrinsics.class, BlackBoxCodegenTestGenerated.Labels.class, BlackBoxCodegenTestGenerated.LocalClasses.class, BlackBoxCodegenTestGenerated.MultiDecl.class, BlackBoxCodegenTestGenerated.Namespace.class, BlackBoxCodegenTestGenerated.Objects.class, BlackBoxCodegenTestGenerated.OperatorConventions.class, BlackBoxCodegenTestGenerated.PrimitiveTypes.class, BlackBoxCodegenTestGenerated.Properties.class, BlackBoxCodegenTestGenerated.Reflection.class, BlackBoxCodegenTestGenerated.SafeCall.class, BlackBoxCodegenTestGenerated.SamConstructors.class, BlackBoxCodegenTestGenerated.Strings.class, BlackBoxCodegenTestGenerated.Super.class, BlackBoxCodegenTestGenerated.ToArray.class, BlackBoxCodegenTestGenerated.Traits.class, BlackBoxCodegenTestGenerated.TypeInfo.class, BlackBoxCodegenTestGenerated.Unit.class, BlackBoxCodegenTestGenerated.Vararg.class, BlackBoxCodegenTestGenerated.When.class})
|
||||||
public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
|
public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
|
||||||
public void testAllFilesPresentInBox() throws Exception {
|
public void testAllFilesPresentInBox() throws Exception {
|
||||||
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("compiler/testData/codegen/box"), Pattern.compile("^(.+)\\.kt$"), true);
|
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("compiler/testData/codegen/box"), Pattern.compile("^(.+)\\.kt$"), true);
|
||||||
@@ -4142,6 +4142,24 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("compiler/testData/codegen/box/toArray")
|
||||||
|
public static class ToArray extends AbstractBlackBoxCodegenTest {
|
||||||
|
public void testAllFilesPresentInToArray() throws Exception {
|
||||||
|
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("compiler/testData/codegen/box/toArray"), Pattern.compile("^(.+)\\.kt$"), true);
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("toArray.kt")
|
||||||
|
public void testToArray() throws Exception {
|
||||||
|
doTest("compiler/testData/codegen/box/toArray/toArray.kt");
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("toArrayAlreadyPresent.kt")
|
||||||
|
public void testToArrayAlreadyPresent() throws Exception {
|
||||||
|
doTest("compiler/testData/codegen/box/toArray/toArrayAlreadyPresent.kt");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("compiler/testData/codegen/box/traits")
|
@TestMetadata("compiler/testData/codegen/box/traits")
|
||||||
public static class Traits extends AbstractBlackBoxCodegenTest {
|
public static class Traits extends AbstractBlackBoxCodegenTest {
|
||||||
public void testAllFilesPresentInTraits() throws Exception {
|
public void testAllFilesPresentInTraits() throws Exception {
|
||||||
@@ -4450,6 +4468,7 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
|
|||||||
suite.addTestSuite(SamConstructors.class);
|
suite.addTestSuite(SamConstructors.class);
|
||||||
suite.addTestSuite(Strings.class);
|
suite.addTestSuite(Strings.class);
|
||||||
suite.addTestSuite(Super.class);
|
suite.addTestSuite(Super.class);
|
||||||
|
suite.addTestSuite(ToArray.class);
|
||||||
suite.addTestSuite(Traits.class);
|
suite.addTestSuite(Traits.class);
|
||||||
suite.addTestSuite(TypeInfo.class);
|
suite.addTestSuite(TypeInfo.class);
|
||||||
suite.addTestSuite(Unit.class);
|
suite.addTestSuite(Unit.class);
|
||||||
|
|||||||
@@ -0,0 +1,63 @@
|
|||||||
|
package jet.runtime;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.Iterator;
|
||||||
|
|
||||||
|
public class CollectionToArray {
|
||||||
|
// Implementation copied from AbstractCollection
|
||||||
|
|
||||||
|
public static Object[] toArray(Collection<?> collection) {
|
||||||
|
Object[] r = new Object[collection.size()];
|
||||||
|
Iterator<?> it = collection.iterator();
|
||||||
|
for (int i = 0; i < r.length; i++) {
|
||||||
|
if (! it.hasNext()) // fewer elements than expected
|
||||||
|
return Arrays.copyOf(r, i);
|
||||||
|
r[i] = it.next();
|
||||||
|
}
|
||||||
|
return it.hasNext() ? finishToArray(r, it) : r;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <T, E> T[] toArray(Collection<E> collection, T[] a) {
|
||||||
|
// Estimate size of array; be prepared to see more or fewer elements
|
||||||
|
int size = collection.size();
|
||||||
|
T[] r = a.length >= size ? a :
|
||||||
|
(T[])java.lang.reflect.Array
|
||||||
|
.newInstance(a.getClass().getComponentType(), size);
|
||||||
|
Iterator<E> it = collection.iterator();
|
||||||
|
|
||||||
|
for (int i = 0; i < r.length; i++) {
|
||||||
|
if (! it.hasNext()) { // fewer elements than expected
|
||||||
|
if (a != r)
|
||||||
|
return Arrays.copyOf(r, i);
|
||||||
|
r[i] = null; // null-terminate
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
r[i] = (T)it.next();
|
||||||
|
}
|
||||||
|
return it.hasNext() ? finishToArray(r, it) : r;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static <T> T[] finishToArray(T[] r, Iterator<?> it) {
|
||||||
|
int i = r.length;
|
||||||
|
while (it.hasNext()) {
|
||||||
|
int cap = r.length;
|
||||||
|
if (i == cap) {
|
||||||
|
int newCap = ((cap / 2) + 1) * 3;
|
||||||
|
if (newCap <= cap) { // integer overflow
|
||||||
|
if (cap == Integer.MAX_VALUE)
|
||||||
|
throw new OutOfMemoryError
|
||||||
|
("Required array size too large");
|
||||||
|
newCap = Integer.MAX_VALUE;
|
||||||
|
}
|
||||||
|
r = Arrays.copyOf(r, newCap);
|
||||||
|
}
|
||||||
|
r[i++] = (T)it.next();
|
||||||
|
}
|
||||||
|
// trim if overallocated
|
||||||
|
return (i == r.length) ? r : Arrays.copyOf(r, i);
|
||||||
|
}
|
||||||
|
|
||||||
|
private CollectionToArray() {
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user