primitive iterators

This commit is contained in:
Alex Tkachman
2011-11-20 11:05:40 +02:00
parent 7abf87b797
commit f6426bc6d4
15 changed files with 332 additions and 110 deletions
@@ -887,7 +887,11 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> {
generateFromResolvedCall(resolvedCall.getThisObject());
}
else {
receiver.put(typeMapper.mapType(((ClassDescriptor)resolvedCall.getResultingDescriptor().getContainingDeclaration()).getDefaultType()), v);
ClassDescriptor containingDeclaration = (ClassDescriptor) resolvedCall.getResultingDescriptor().getContainingDeclaration();
if(CodegenUtil.isInterface(containingDeclaration))
receiver.put(JetTypeMapper.TYPE_OBJECT, v);
else
receiver.put(typeMapper.mapType(containingDeclaration.getDefaultType()), v);
}
}
}
@@ -74,6 +74,14 @@ public class JetTypeMapper {
public static final Type TYPE_SHARED_CHAR = Type.getObjectType("jet/runtime/SharedVar$Char");
public static final Type TYPE_SHARED_LONG = Type.getObjectType("jet/runtime/SharedVar$Long");
public static final Type TYPE_SHARED_BOOLEAN = Type.getObjectType("jet/runtime/SharedVar$Boolean");
public static final Type TYPE_BOOLEAN_ITERATOR = Type.getObjectType("jet/BooleanIterator");
public static final Type TYPE_CHAR_ITERATOR = Type.getObjectType("jet/CharIterator");
public static final Type TYPE_BYTE_ITERATOR = Type.getObjectType("jet/ByteIterator");
public static final Type TYPE_SHORT_ITERATOR = Type.getObjectType("jet/ShortIterator");
public static final Type TYPE_INT_ITERATOR = Type.getObjectType("jet/IntIterator");
public static final Type TYPE_LONG_ITERATOR = Type.getObjectType("jet/LongIterator");
public static final Type TYPE_FLOAT_ITERATOR = Type.getObjectType("jet/FloatIterator");
public static final Type TYPE_DOUBLE_ITERATOR = Type.getObjectType("jet/DoubleIterator");
public JetTypeMapper(JetStandardLibrary standardLibrary, BindingContext bindingContext) {
this.standardLibrary = standardLibrary;
@@ -32,34 +32,42 @@ public class ArrayIterator implements IntrinsicMethod {
if(containingDeclaration.equals(standardLibrary.getArray())) {
codegen.generateTypeInfo(funDescriptor.getReturnType().getArguments().get(0).getType());
v.invokestatic("jet/runtime/ArrayIterator", "iterator", "([Ljava/lang/Object;Ljet/typeinfo/TypeInfo;)Ljet/Iterator;");
return StackValue.onStack(JetTypeMapper.TYPE_ITERATOR);
}
else if(containingDeclaration.equals(standardLibrary.getByteArrayClass())) {
v.invokestatic("jet/runtime/ArrayIterator", "iterator", "([B)Ljet/Iterator;");
v.invokestatic("jet/runtime/ArrayIterator", "iterator", "([B)Ljet/ByteIterator;");
return StackValue.onStack(JetTypeMapper.TYPE_BYTE_ITERATOR);
}
else if(containingDeclaration.equals(standardLibrary.getShortArrayClass())) {
v.invokestatic("jet/runtime/ArrayIterator", "iterator", "([S)Ljet/Iterator;");
v.invokestatic("jet/runtime/ArrayIterator", "iterator", "([S)Ljet/ShortIterator;");
return StackValue.onStack(JetTypeMapper.TYPE_SHORT_ITERATOR);
}
else if(containingDeclaration.equals(standardLibrary.getIntArrayClass())) {
v.invokestatic("jet/runtime/ArrayIterator", "iterator", "([I)Ljet/Iterator;");
v.invokestatic("jet/runtime/ArrayIterator", "iterator", "([I)Ljet/IntIterator;");
return StackValue.onStack(JetTypeMapper.TYPE_INT_ITERATOR);
}
else if(containingDeclaration.equals(standardLibrary.getLongArrayClass())) {
v.invokestatic("jet/runtime/ArrayIterator", "iterator", "([J)Ljet/Iterator;");
v.invokestatic("jet/runtime/ArrayIterator", "iterator", "([J)Ljet/LongIterator;");
return StackValue.onStack(JetTypeMapper.TYPE_LONG_ITERATOR);
}
else if(containingDeclaration.equals(standardLibrary.getFloatArrayClass())) {
v.invokestatic("jet/runtime/ArrayIterator", "iterator", "([F)Ljet/Iterator;");
v.invokestatic("jet/runtime/ArrayIterator", "iterator", "([F)Ljet/FloatIterator;");
return StackValue.onStack(JetTypeMapper.TYPE_FLOAT_ITERATOR);
}
else if(containingDeclaration.equals(standardLibrary.getDoubleArrayClass())) {
v.invokestatic("jet/runtime/ArrayIterator", "iterator", "([D)Ljet/Iterator;");
v.invokestatic("jet/runtime/ArrayIterator", "iterator", "([D)Ljet/DoubleIterator;");
return StackValue.onStack(JetTypeMapper.TYPE_DOUBLE_ITERATOR);
}
else if(containingDeclaration.equals(standardLibrary.getCharArrayClass())) {
v.invokestatic("jet/runtime/ArrayIterator", "iterator", "([C)Ljet/Iterator;");
v.invokestatic("jet/runtime/ArrayIterator", "iterator", "([C)Ljet/CharIterator;");
return StackValue.onStack(JetTypeMapper.TYPE_CHAR_ITERATOR);
}
else if(containingDeclaration.equals(standardLibrary.getBooleanArrayClass())) {
v.invokestatic("jet/runtime/ArrayIterator", "iterator", "([Z)Ljet/Iterator;");
v.invokestatic("jet/runtime/ArrayIterator", "iterator", "([Z)Ljet/BooleanIterator;");
return StackValue.onStack(JetTypeMapper.TYPE_BOOLEAN_ITERATOR);
}
else {
throw new UnsupportedOperationException(containingDeclaration.toString());
}
return StackValue.onStack(JetTypeMapper.TYPE_ITERATOR);
}
}
@@ -11,6 +11,9 @@ import org.jetbrains.jet.compiler.CompileEnvironmentException;
*/
@SuppressWarnings("UseOfSystemOutOrSystemErr")
public class KotlinCompiler {
private KotlinCompiler() {
}
public static class Arguments {
@Argument(value = "output", description = "output directory")
public String outputDir;
+40 -8
View File
@@ -53,6 +53,38 @@ trait Iterator<out T> {
val hasNext : Boolean
}
abstract open class ByteIterator : Iterator<Byte> {
fun nextByte() : Byte
}
abstract open class ShortIterator : Iterator<Short> {
fun nextShort() : Short
}
abstract open class CharIterator : Iterator<Char> {
fun nextChar() : Char
}
abstract open class IntIterator : Iterator<Int> {
fun nextInt() : Int
}
abstract open class LongIterator : Iterator<Long> {
fun nextLong() : Long
}
abstract open class FloatIterator : Iterator<Float> {
fun nextFloat() : Float
}
abstract open class DoubleIterator : Iterator<Double> {
fun nextDouble() : Double
}
abstract open class BooleanIterator : Iterator<Boolean> {
fun nextBoolean() : Boolean
}
trait Iterable<out T> {
fun iterator() : Iterator<T>
}
@@ -70,7 +102,7 @@ class ByteArray(val size : Int) {
fun get(index : Int) : Byte
fun set(index : Int, value : Byte) : Unit
fun iterator() : Iterator<Byte>
fun iterator() : ByteIterator
val indices : IntRange
}
@@ -79,7 +111,7 @@ class ShortArray(val size : Int) {
fun get(index : Int) : Short
fun set(index : Int, value : Short) : Unit
fun iterator() : Iterator<Short>
fun iterator() : ShortIterator
val indices : IntRange
}
@@ -88,7 +120,7 @@ class IntArray(val size : Int) {
fun get(index : Int) : Int
fun set(index : Int, value : Int) : Unit
fun iterator() : Iterator<Int>
fun iterator() : IntIterator
val indices : IntRange
}
@@ -97,7 +129,7 @@ class LongArray(val size : Int) {
fun get(index : Int) : Long
fun set(index : Int, value : Long) : Unit
fun iterator() : Iterator<Long>
fun iterator() : LongIterator
val indices : IntRange
}
@@ -106,7 +138,7 @@ class FloatArray(val size : Int) {
fun get(index : Int) : Float
fun set(index : Int, value : Float) : Unit
fun iterator() : Iterator<Float>
fun iterator() : FloatIterator
val indices : IntRange
}
@@ -115,7 +147,7 @@ class DoubleArray(val size : Int) {
fun get(index : Int) : Double
fun set(index : Int, value : Double) : Unit
fun iterator() : Iterator<Double>
fun iterator() : DoubleIterator
val indices : IntRange
}
@@ -124,7 +156,7 @@ class CharArray(val size : Int) {
fun get(index : Int) : Char
fun set(index : Int, value : Char) : Unit
fun iterator() : Iterator<Char>
fun iterator() : CharIterator
val indices : IntRange
}
@@ -133,7 +165,7 @@ class BooleanArray(val size : Int) {
fun get(index : Int) : Boolean
fun set(index : Int, value : Boolean) : Unit
fun iterator() : Iterator<Boolean>
fun iterator() : BooleanIterator
val indices : IntRange
}
@@ -63,14 +63,70 @@ public class ArrayGenTest extends CodegenTestCase {
}
public void testPrimitiveIterator () throws Exception {
loadText("fun box() { val x = ByteArray(5).iterator(); while(x.hasNext) { java.lang.System.out?.println(x.next()) } }");
loadText("fun box() { val x = ByteArray(5).iterator(); while(x.hasNext) { java.lang.System.out?.println(x.nextByte()) } }");
System.out.println(generateToText());
Method foo = generateFunction();
foo.invoke(null);
}
public void testPrimitiveIteratorByte () throws Exception {
loadText("fun box() { for(x in ByteArray(5)) { java.lang.System.out?.println(x) } }");
System.out.println(generateToText());
Method foo = generateFunction();
foo.invoke(null);
}
public void testPrimitiveIteratorShort () throws Exception {
loadText("fun box() { for(x in ShortArray(5)) { java.lang.System.out?.println(x) } }");
System.out.println(generateToText());
Method foo = generateFunction();
foo.invoke(null);
}
public void testPrimitiveIteratorInt () throws Exception {
loadText("fun box() { for(x in IntArray(5)) { java.lang.System.out?.println(x) } }");
System.out.println(generateToText());
Method foo = generateFunction();
foo.invoke(null);
}
public void testPrimitiveIteratorLong () throws Exception {
loadText("fun box() { for(x in LongArray(5)) { java.lang.System.out?.println(x) } }");
System.out.println(generateToText());
Method foo = generateFunction();
foo.invoke(null);
}
public void testPrimitiveIteratorFloat () throws Exception {
loadText("fun box() { for(x in FloatArray(5)) { java.lang.System.out?.println(x) } }");
System.out.println(generateToText());
Method foo = generateFunction();
foo.invoke(null);
}
public void testPrimitiveIteratorDouble () throws Exception {
loadText("fun box() { for(x in DoubleArray(5)) { java.lang.System.out?.println(x) } }");
System.out.println(generateToText());
Method foo = generateFunction();
foo.invoke(null);
}
public void testPrimitiveIteratorChar () throws Exception {
loadText("fun box() { for(x in CharArray(5)) { java.lang.System.out?.println(x) } }");
System.out.println(generateToText());
Method foo = generateFunction();
foo.invoke(null);
}
public void testPrimitiveIteratorBoolean () throws Exception {
loadText("fun box() { for(x in BooleanArray(5)) { java.lang.System.out?.println(x) } }");
System.out.println(generateToText());
Method foo = generateFunction();
foo.invoke(null);
}
public void testLongIterator () throws Exception {
loadText("fun box() { val x = LongArray(5).iterator(); while(x.hasNext) { java.lang.System.out?.println(x.next()) } }");
loadText("fun box() { val x = LongArray(5).iterator(); while(x.hasNext) { java.lang.System.out?.println(x.nextLong()) } }");
System.out.println(generateToText());
Method foo = generateFunction();
foo.invoke(null);