Merge branch 'master' of ssh://git.labs.intellij.net/jet
This commit is contained in:
@@ -200,7 +200,7 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> {
|
|||||||
final JetExpression loopRange = expression.getLoopRange();
|
final JetExpression loopRange = expression.getLoopRange();
|
||||||
final JetType expressionType = bindingContext.get(BindingContext.EXPRESSION_TYPE, loopRange);
|
final JetType expressionType = bindingContext.get(BindingContext.EXPRESSION_TYPE, loopRange);
|
||||||
Type loopRangeType = typeMapper.mapType(expressionType);
|
Type loopRangeType = typeMapper.mapType(expressionType);
|
||||||
if (state.getStandardLibrary().getArray().equals(expressionType.getConstructor().getDeclarationDescriptor())) {
|
if (loopRangeType.getSort() == Type.ARRAY) {
|
||||||
new ForInArrayLoopGenerator(expression, loopRangeType).invoke();
|
new ForInArrayLoopGenerator(expression, loopRangeType).invoke();
|
||||||
return StackValue.none();
|
return StackValue.none();
|
||||||
}
|
}
|
||||||
@@ -1688,12 +1688,21 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> {
|
|||||||
|
|
||||||
private void generateNewArray(JetCallExpression expression, JetType arrayType) {
|
private void generateNewArray(JetCallExpression expression, JetType arrayType) {
|
||||||
List<? extends ValueArgument> args = expression.getValueArguments();
|
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);
|
gen(args.get(0).getArgumentExpression(), Type.INT_TYPE);
|
||||||
|
|
||||||
if(state.getStandardLibrary().getArray().equals(arrayType.getConstructor().getDeclarationDescriptor())) {
|
if(isArray) {
|
||||||
JetType elementType = typeMapper.getGenericsElementType(arrayType);
|
JetType elementType = typeMapper.getGenericsElementType(arrayType);
|
||||||
if(elementType != null) {
|
if(elementType != null) {
|
||||||
generateTypeInfo(elementType);
|
generateTypeInfo(elementType);
|
||||||
@@ -1707,6 +1716,42 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> {
|
|||||||
Type type = typeMapper.mapType(arrayType, OwnerKind.IMPLEMENTATION);
|
Type type = typeMapper.mapType(arrayType, OwnerKind.IMPLEMENTATION);
|
||||||
v.newarray(type.getElementType());
|
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
|
@Override
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package org.jetbrains.jet.codegen;
|
|||||||
|
|
||||||
import com.intellij.psi.*;
|
import com.intellij.psi.*;
|
||||||
import com.intellij.psi.util.PsiTreeUtil;
|
import com.intellij.psi.util.PsiTreeUtil;
|
||||||
|
import jet.Function1;
|
||||||
import jet.JetObject;
|
import jet.JetObject;
|
||||||
import jet.typeinfo.TypeInfo;
|
import jet.typeinfo.TypeInfo;
|
||||||
import jet.typeinfo.TypeInfoProjection;
|
import jet.typeinfo.TypeInfoProjection;
|
||||||
@@ -56,6 +57,7 @@ public class JetTypeMapper {
|
|||||||
private final Map<String, Integer> anonymousSubclassesCount = new HashMap<String, Integer>();
|
private final Map<String, Integer> anonymousSubclassesCount = new HashMap<String, Integer>();
|
||||||
|
|
||||||
private final HashMap<JetType,String> knowTypes = new HashMap<JetType, String>();
|
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) {
|
public JetTypeMapper(JetStandardLibrary standardLibrary, BindingContext bindingContext) {
|
||||||
this.standardLibrary = standardLibrary;
|
this.standardLibrary = standardLibrary;
|
||||||
|
|||||||
@@ -49,7 +49,7 @@ trait Iterable<out T> {
|
|||||||
fun iterator() : Iterator<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 get(index : Int) : T
|
||||||
fun set(index : Int, value : T) : Unit
|
fun set(index : Int, value : T) : Unit
|
||||||
|
|
||||||
|
|||||||
Binary file not shown.
@@ -1,5 +1,5 @@
|
|||||||
fun box() : String {
|
fun box() : String {
|
||||||
val a = Array<Int> (5)
|
val a = Array<Int> (5, {0})
|
||||||
var i = 0
|
var i = 0
|
||||||
var sum = 0
|
var sum = 0
|
||||||
for(el in 0..4) {
|
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"
|
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) {
|
for(el in 0..4) {
|
||||||
a[i] = i++
|
a[i] = i++
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ fun main(args: Array<String>?) {
|
|||||||
//do not compile
|
//do not compile
|
||||||
System.out?.println(fff<Unit>(())) //do not compile
|
System.out?.println(fff<Unit>(())) //do not compile
|
||||||
System.out?.println(id<Unit>(y)) //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>()
|
class A<T>()
|
||||||
|
|
||||||
@@ -30,7 +30,7 @@ fun almostFilter(array: Array<Int>, action: fun(Int): Int) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun box() : String {
|
fun box() : String {
|
||||||
val a = Array<Int> (3)
|
val a = Array<Int> (3,{-1})
|
||||||
a[0] = 0
|
a[0] = 0
|
||||||
a[1] = 1
|
a[1] = 1
|
||||||
a[2] = 2
|
a[2] = 2
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ fun t1 () {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun t2 () {
|
fun t2 () {
|
||||||
val a2 = Array<Int>(1)
|
val a2 = Array<Int>(1,{0})
|
||||||
a2[0] = 0 //ok
|
a2[0] = 0 //ok
|
||||||
var i = a2[0] //ok
|
var i = a2[0] //ok
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -28,7 +28,7 @@ fun box() : String {
|
|||||||
println(b.a[0])
|
println(b.a[0])
|
||||||
|
|
||||||
val c = List<Array<Int>>(1)
|
val c = List<Array<Int>>(1)
|
||||||
c.a[0] = Array<Int>(4)
|
c.a[0] = Array<Int>(4,{-1})
|
||||||
println(c.a[0].size)
|
println(c.a[0].size)
|
||||||
|
|
||||||
val e = List<Int>(5)
|
val e = List<Int>(5)
|
||||||
|
|||||||
@@ -17,15 +17,24 @@ public class ArrayGenTest extends CodegenTestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void testCreateMultiInt () throws Exception {
|
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();
|
Method foo = generateFunction();
|
||||||
Object invoke = foo.invoke(null);
|
Integer[][] invoke = (Integer[][]) foo.invoke(null);
|
||||||
System.out.println(invoke.getClass());
|
assertEquals(invoke[2].length, 2);
|
||||||
assertTrue(invoke instanceof Integer[][]);
|
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 {
|
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();
|
Method foo = generateFunction();
|
||||||
Object invoke = foo.invoke(null);
|
Object invoke = foo.invoke(null);
|
||||||
System.out.println(invoke.getClass());
|
System.out.println(invoke.getClass());
|
||||||
|
|||||||
@@ -151,6 +151,10 @@ public class ControlStructuresTest extends CodegenTestCase {
|
|||||||
blackBoxFile("controlStructures/forIntArray.jet");
|
blackBoxFile("controlStructures/forIntArray.jet");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void testForPrimitiveIntArray() throws Exception {
|
||||||
|
blackBoxFile("controlStructures/forPrimitiveIntArray.jet");
|
||||||
|
}
|
||||||
|
|
||||||
public void testForNullableIntArray() throws Exception {
|
public void testForNullableIntArray() throws Exception {
|
||||||
blackBoxFile("controlStructures/forNullableIntArray.jet");
|
blackBoxFile("controlStructures/forNullableIntArray.jet");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -374,13 +374,24 @@ public class NamespaceGenTest extends CodegenTestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void testArrayNew() throws Exception {
|
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());
|
System.out.println(generateToText());
|
||||||
final Method main = generateFunction();
|
final Method main = generateFunction();
|
||||||
Integer[] result = (Integer[]) main.invoke(null);
|
Integer[] result = (Integer[]) main.invoke(null);
|
||||||
assertEquals(4, result.length);
|
assertEquals(4, result.length);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testFloatArrayNew() throws Exception {
|
public void testFloatArrayNew() throws Exception {
|
||||||
loadText("fun foo() = FloatArray(4)");
|
loadText("fun foo() = FloatArray(4)");
|
||||||
System.out.println(generateToText());
|
System.out.println(generateToText());
|
||||||
@@ -390,11 +401,12 @@ public class NamespaceGenTest extends CodegenTestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void testFloatArrayArrayNew() throws Exception {
|
public void testFloatArrayArrayNew() throws Exception {
|
||||||
loadText("fun foo() = Array<FloatArray>(4)");
|
loadText("fun foo() = Array<FloatArray>(4, { FloatArray(5-it) })");
|
||||||
System.out.println(generateToText());
|
System.out.println(generateToText());
|
||||||
final Method main = generateFunction();
|
final Method main = generateFunction();
|
||||||
float[][] result = (float[][]) main.invoke(null);
|
float[][] result = (float[][]) main.invoke(null);
|
||||||
assertEquals(4, result.length);
|
assertEquals(4, result.length);
|
||||||
|
assertEquals(2, result[3].length);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testArraySize() throws Exception {
|
public void testArraySize() throws Exception {
|
||||||
|
|||||||
Reference in New Issue
Block a user