Merge branch 'master' of ssh://git.labs.intellij.net/jet

This commit is contained in:
svtk
2011-10-03 17:47:39 +04:00
13 changed files with 105 additions and 19 deletions
@@ -200,7 +200,7 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> {
final JetExpression loopRange = expression.getLoopRange();
final JetType expressionType = bindingContext.get(BindingContext.EXPRESSION_TYPE, loopRange);
Type loopRangeType = typeMapper.mapType(expressionType);
if (state.getStandardLibrary().getArray().equals(expressionType.getConstructor().getDeclarationDescriptor())) {
if (loopRangeType.getSort() == Type.ARRAY) {
new ForInArrayLoopGenerator(expression, loopRangeType).invoke();
return StackValue.none();
}
@@ -1688,12 +1688,21 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> {
private void generateNewArray(JetCallExpression expression, JetType arrayType) {
List<? extends ValueArgument> args = expression.getValueArguments();
if (args.size() != 1) {
throw new CompilationException("array constructor requires one value argument");
boolean isArray = state.getStandardLibrary().getArray().equals(arrayType.getConstructor().getDeclarationDescriptor());
if(isArray) {
if (args.size() != 2 && !arrayType.getArguments().get(0).getType().isNullable()) {
throw new CompilationException("array constructor of non-nullable type requires two arguments");
}
}
else {
if (args.size() != 1) {
throw new CompilationException("primitive array constructor requires one argument");
}
}
gen(args.get(0).getArgumentExpression(), Type.INT_TYPE);
if(state.getStandardLibrary().getArray().equals(arrayType.getConstructor().getDeclarationDescriptor())) {
if(isArray) {
JetType elementType = typeMapper.getGenericsElementType(arrayType);
if(elementType != null) {
generateTypeInfo(elementType);
@@ -1707,6 +1716,42 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> {
Type type = typeMapper.mapType(arrayType, OwnerKind.IMPLEMENTATION);
v.newarray(type.getElementType());
}
if(args.size() == 2) {
int sizeIndex = myMap.enterTemp(2);
int indexIndex = sizeIndex+1;
v.dup();
v.arraylength();
v.store(sizeIndex, Type.INT_TYPE);
v.iconst(0);
v.store(indexIndex, Type.INT_TYPE);
gen(args.get(1).getArgumentExpression(), JetTypeMapper.TYPE_FUNCTION1);
Label begin = new Label();
Label end = new Label();
v.visitLabel(begin);
v.load(indexIndex, Type.INT_TYPE);
v.load(sizeIndex, Type.INT_TYPE);
v.ificmpge(end);
v.dup2();
v.load(indexIndex, Type.INT_TYPE);
v.invokestatic("java/lang/Integer", "valueOf", "(I)Ljava/lang/Integer;");
v.invokevirtual("jet/Function1", "invoke", "(Ljava/lang/Object;)Ljava/lang/Object;");
v.load(indexIndex, Type.INT_TYPE);
v.iinc(indexIndex, 1);
v.swap();
v.astore(JetTypeMapper.TYPE_OBJECT);
v.goTo(begin);
v.visitLabel(end);
v.pop();
myMap.leaveTemp(2);
}
}
@Override
@@ -2,6 +2,7 @@ package org.jetbrains.jet.codegen;
import com.intellij.psi.*;
import com.intellij.psi.util.PsiTreeUtil;
import jet.Function1;
import jet.JetObject;
import jet.typeinfo.TypeInfo;
import jet.typeinfo.TypeInfoProjection;
@@ -56,6 +57,7 @@ public class JetTypeMapper {
private final Map<String, Integer> anonymousSubclassesCount = new HashMap<String, Integer>();
private final HashMap<JetType,String> knowTypes = new HashMap<JetType, String>();
public static final Type TYPE_FUNCTION1 = Type.getObjectType("jet/Function1");
public JetTypeMapper(JetStandardLibrary standardLibrary, BindingContext bindingContext) {
this.standardLibrary = standardLibrary;
+1 -1
View File
@@ -49,7 +49,7 @@ trait Iterable<out T> {
fun iterator() : Iterator<T>
}
class Array<T>(val size : Int) {
class Array<T>(val size : Int, init : fun(Int) : T = null ) {
fun get(index : Int) : T
fun set(index : Int, value : T) : Unit
Binary file not shown.
@@ -1,5 +1,5 @@
fun box() : String {
val a = Array<Int> (5)
val a = Array<Int> (5, {0})
var i = 0
var sum = 0
for(el in 0..4) {
@@ -0,0 +1,14 @@
fun box() : String {
val a = IntArray (5)
var i = 0
var sum = 0
for(el in 0..4) {
a[i] = i++
}
for (el in a) {
sum = sum + el
}
if(sum != 10) return "a failed"
return "OK"
}
@@ -44,7 +44,7 @@ fun box() : String {
}
if(sum != 15) return "c4 failed"
val a : Array<Int> = Array<Int> (5)
val a : Array<Int> = Array<Int> (5, {0})
for(el in 0..4) {
a[i] = i++
}
+2 -2
View File
@@ -5,7 +5,7 @@ fun main(args: Array<String>?) {
//do not compile
System.out?.println(fff<Unit>(())) //do not compile
System.out?.println(id<Unit>(y)) //do not compile
System.out?.println(fff<Unit>(id<Unit>(y)) == id<Unit>(foreach(Array<Int>(0),{(e : Int) : Unit => }))) //do not compile
System.out?.println(fff<Unit>(id<Unit>(y)) == id<Unit>(foreach(Array<Int>(0,{0}),{(e : Int) : Unit => }))) //do not compile
}
class A<T>()
@@ -30,7 +30,7 @@ fun almostFilter(array: Array<Int>, action: fun(Int): Int) {
}
fun box() : String {
val a = Array<Int> (3)
val a = Array<Int> (3,{-1})
a[0] = 0
a[1] = 1
a[2] = 2
+1 -1
View File
@@ -5,7 +5,7 @@ fun t1 () {
}
fun t2 () {
val a2 = Array<Int>(1)
val a2 = Array<Int>(1,{0})
a2[0] = 0 //ok
var i = a2[0] //ok
}
+1 -1
View File
@@ -28,7 +28,7 @@ fun box() : String {
println(b.a[0])
val c = List<Array<Int>>(1)
c.a[0] = Array<Int>(4)
c.a[0] = Array<Int>(4,{-1})
println(c.a[0].size)
val e = List<Int>(5)
@@ -17,15 +17,24 @@ public class ArrayGenTest extends CodegenTestCase {
}
public void testCreateMultiInt () throws Exception {
loadText("fun foo() = Array<Array<Int>> (5)");
loadText("fun foo() = Array<Array<Int>> (5, { Array<Int>(it, {239}) })");
Method foo = generateFunction();
Object invoke = foo.invoke(null);
System.out.println(invoke.getClass());
assertTrue(invoke instanceof Integer[][]);
Integer[][] invoke = (Integer[][]) foo.invoke(null);
assertEquals(invoke[2].length, 2);
assertEquals(invoke[4].length, 4);
assertEquals(invoke[4][2].intValue(), 239);
}
public void testCreateMultiIntNullable () throws Exception {
loadText("fun foo() = Array<Array<Int?>> (5, { Array<Int?>(it) })");
Method foo = generateFunction();
Integer[][] invoke = (Integer[][]) foo.invoke(null);
assertEquals(invoke[2].length, 2);
assertEquals(invoke[4].length, 4);
}
public void testCreateMultiString () throws Exception {
loadText("fun foo() = Array<Array<String>> (5)");
loadText("fun foo() = Array<Array<String>> (5, { Array<String>(0,{\"\"}) })");
Method foo = generateFunction();
Object invoke = foo.invoke(null);
System.out.println(invoke.getClass());
@@ -151,6 +151,10 @@ public class ControlStructuresTest extends CodegenTestCase {
blackBoxFile("controlStructures/forIntArray.jet");
}
public void testForPrimitiveIntArray() throws Exception {
blackBoxFile("controlStructures/forPrimitiveIntArray.jet");
}
public void testForNullableIntArray() throws Exception {
blackBoxFile("controlStructures/forNullableIntArray.jet");
}
@@ -374,13 +374,24 @@ public class NamespaceGenTest extends CodegenTestCase {
}
public void testArrayNew() throws Exception {
loadText("fun foo() = Array<Int>(4)");
loadText("fun foo() = Array<Int>(4, { it })");
System.out.println(generateToText());
final Method main = generateFunction();
Integer[] result = (Integer[]) main.invoke(null);
assertEquals(4, result.length);
assertEquals(0, result[0].intValue());
assertEquals(1, result[1].intValue());
assertEquals(2, result[2].intValue());
assertEquals(3, result[3].intValue());
}
public void testArrayNewNullable() throws Exception {
loadText("fun foo() = Array<Int?>(4)");
System.out.println(generateToText());
final Method main = generateFunction();
Integer[] result = (Integer[]) main.invoke(null);
assertEquals(4, result.length);
}
public void testFloatArrayNew() throws Exception {
loadText("fun foo() = FloatArray(4)");
System.out.println(generateToText());
@@ -390,11 +401,12 @@ public class NamespaceGenTest extends CodegenTestCase {
}
public void testFloatArrayArrayNew() throws Exception {
loadText("fun foo() = Array<FloatArray>(4)");
loadText("fun foo() = Array<FloatArray>(4, { FloatArray(5-it) })");
System.out.println(generateToText());
final Method main = generateFunction();
float[][] result = (float[][]) main.invoke(null);
assertEquals(4, result.length);
assertEquals(2, result[3].length);
}
public void testArraySize() throws Exception {