more tests for for-

This commit is contained in:
Alex Tkachman
2011-09-03 17:09:49 +02:00
parent c20a39dbd8
commit 07b2ab347d
3 changed files with 70 additions and 16 deletions
@@ -1,9 +1,7 @@
package org.jetbrains.jet.codegen; package org.jetbrains.jet.codegen;
import com.intellij.openapi.editor.Document; import com.intellij.openapi.editor.Document;
import com.intellij.openapi.project.Project;
import com.intellij.psi.*; import com.intellij.psi.*;
import com.intellij.psi.search.ProjectScope;
import com.intellij.psi.tree.IElementType; import com.intellij.psi.tree.IElementType;
import com.intellij.psi.util.PsiTreeUtil; import com.intellij.psi.util.PsiTreeUtil;
import gnu.trove.THashSet; import gnu.trove.THashSet;
@@ -34,6 +32,7 @@ import java.util.*;
/** /**
* @author max * @author max
* @author yole * @author yole
* @author alex.tkachman
*/ */
public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> { public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> {
private static final String CLASS_OBJECT = "java/lang/Object"; private static final String CLASS_OBJECT = "java/lang/Object";
@@ -308,6 +307,7 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> {
v.mark(end); v.mark(end);
int paramIndex = myMap.leave(parameterDescriptor); int paramIndex = myMap.leave(parameterDescriptor);
//noinspection ConstantConditions
v.visitLocalVariable(loopParameter.getName(), asmParamType.getDescriptor(), null, begin, end, paramIndex); v.visitLocalVariable(loopParameter.getName(), asmParamType.getDescriptor(), null, begin, end, paramIndex);
myMap.leaveTemp(); myMap.leaveTemp();
myBreakTargets.pop(); myBreakTargets.pop();
@@ -363,6 +363,7 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> {
cleanupTemp(); cleanupTemp();
final int paramIndex = myMap.leave(parameterDescriptor); final int paramIndex = myMap.leave(parameterDescriptor);
//noinspection ConstantConditions
v.visitLocalVariable(expression.getLoopParameter().getName(), asmParamType.getDescriptor(), null, condition, end, paramIndex); v.visitLocalVariable(expression.getLoopParameter().getName(), asmParamType.getDescriptor(), null, condition, end, paramIndex);
myBreakTargets.pop(); myBreakTargets.pop();
myContinueTargets.pop(); myContinueTargets.pop();
@@ -822,8 +823,10 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> {
setter = null; setter = null;
} }
else { else {
//noinspection ConstantConditions
getter = isInsideClass && (propertyDescriptor.getGetter() == null || propertyDescriptor.getGetter().isDefault()) getter = isInsideClass && (propertyDescriptor.getGetter() == null || propertyDescriptor.getGetter().isDefault())
? null : typeMapper.mapGetterSignature(propertyDescriptor); ? null : typeMapper.mapGetterSignature(propertyDescriptor);
//noinspection ConstantConditions
setter = isInsideClass && (propertyDescriptor.getSetter() == null || propertyDescriptor.getSetter().isDefault()) setter = isInsideClass && (propertyDescriptor.getSetter() == null || propertyDescriptor.getSetter().isDefault())
? null : typeMapper.mapSetterSignature(propertyDescriptor); ? null : typeMapper.mapSetterSignature(propertyDescriptor);
} }
@@ -1434,12 +1437,15 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> {
value.dupReceiver(v, 0); value.dupReceiver(v, 0);
value.put(asmType, v); value.put(asmType, v);
if (asmType == Type.LONG_TYPE) { if (asmType == Type.LONG_TYPE) {
//noinspection UnnecessaryBoxing
v.aconst(Long.valueOf(increment)); v.aconst(Long.valueOf(increment));
} }
else if (asmType == Type.FLOAT_TYPE) { else if (asmType == Type.FLOAT_TYPE) {
//noinspection UnnecessaryBoxing
v.aconst(Float.valueOf(increment)); v.aconst(Float.valueOf(increment));
} }
else if (asmType == Type.DOUBLE_TYPE) { else if (asmType == Type.DOUBLE_TYPE) {
//noinspection UnnecessaryBoxing
v.aconst(Double.valueOf(increment)); v.aconst(Double.valueOf(increment));
} }
else { else {
@@ -1741,7 +1747,6 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> {
v.mark(lblCheck); v.mark(lblCheck);
for (int i = 0; i < entries.size(); i++) { for (int i = 0; i < entries.size(); i++) {
final boolean isLast = i == entries.size() - 1;
final StackValue tupleField = StackValue.field(OBJECT_TYPE, tupleClassName, "_" + (i + 1), false); final StackValue tupleField = StackValue.field(OBJECT_TYPE, tupleClassName, "_" + (i + 1), false);
final StackValue stackValue = generatePatternMatch(entries.get(i).getPattern(), false, tupleField, nextEntry); final StackValue stackValue = generatePatternMatch(entries.get(i).getPattern(), false, tupleField, nextEntry);
stackValue.condJump(lblPopAndFail, true, v); stackValue.condJump(lblPopAndFail, true, v);
@@ -1956,7 +1961,7 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> {
final String className = "jet/Tuple" + entries.size(); final String className = "jet/Tuple" + entries.size();
Type tupleType = Type.getObjectType(className); Type tupleType = Type.getObjectType(className);
StringBuilder signature = new StringBuilder("("); StringBuilder signature = new StringBuilder("(");
for (JetExpression entry : entries) { for (int i = 0; i != entries.size(); ++i) {
signature.append("Ljava/lang/Object;"); signature.append("Ljava/lang/Object;");
} }
signature.append(")V"); signature.append(")V");
@@ -133,8 +133,8 @@ public class FunctionCodegen {
if(overriddenFunctions.size() > 0) { if(overriddenFunctions.size() > 0) {
for (FunctionDescriptor overriddenFunction : overriddenFunctions) { for (FunctionDescriptor overriddenFunction : overriddenFunctions) {
// TODO should we check params here as well? // TODO should we check params here as well?
if(!JetTypeImpl.equalTypeClasses(overriddenFunction.getReturnType(), functionDescriptor.getReturnType())) { if(!JetTypeImpl.equalTypeClasses(overriddenFunction.getOriginal().getReturnType(), functionDescriptor.getReturnType())) {
generateBridgeMethod(jvmSignature, state.getTypeMapper().mapSignature(overriddenFunction.getName(), overriddenFunction)); generateBridgeMethod(jvmSignature, state.getTypeMapper().mapSignature(overriddenFunction.getName(), overriddenFunction.getOriginal()));
} }
} }
} }
@@ -1,25 +1,70 @@
fun box() : String { fun box() : String {
var sum : Int = 0
var i = 0
val c6 = MyCollection4() val c6 = MyCollection4()
sum = 0
for (el in c6) { for (el in c6) {
System.out?.println(el) sum = sum + el
} }
if(sum != 15) return "c6 failed"
val c5 = MyCollection3() val c5 = MyCollection3()
sum = 0
for (el in c5) { for (el in c5) {
System.out?.println(el) sum = sum + (el ?: 0)
} }
if(sum != 15) return "c5 failed"
val c1: java.lang.Iterable<Int> = MyCollection1() val c1: java.lang.Iterable<Int> = MyCollection1()
for (el in c1) {} sum = 0
for (el in c1) {
sum = sum + el
}
if(sum != 15) return "c1 failed"
val c2 = MyCollection1() val c2 = MyCollection1()
for (el in c2) {} sum = 0
for (el in c2) {
sum = sum + el
}
if(sum != 15) return "c2 failed"
val c3: Iterable<Int> = MyCollection2() val c3: Iterable<Int> = MyCollection2()
for (el in c3) {} sum = 0
for (el in c3) {
sum = sum + el
}
if(sum != 15) return "c3 failed"
val c4 = MyCollection2() val c4 = MyCollection2()
for (el in c4) {} sum = 0
for (el in c4) {
sum = sum + el
}
if(sum != 15) return "c4 failed"
val a : Array<Int> = Array<Int> (5)
for(el in 0..4) {
a[i] = i++
}
sum = 0
for (el in a) {
sum = sum + el
}
if(sum != 10) return "a failed"
val b : Array<Int?> = Array<Int?> (5)
i = 0
while(i < 5) {
b[i] = i++
}
sum = 0
for (el in b) {
sum = sum + (el ?: 0)
}
System.out?.println(sum)
if(sum != 10) return "b failed"
return "OK" return "OK"
} }
@@ -28,8 +73,10 @@ class MyCollection1(): java.lang.Iterable<Int> {
fun iterator(): java.util.Iterator<Int> = MyIterator() fun iterator(): java.util.Iterator<Int> = MyIterator()
class MyIterator(): java.util.Iterator<Int> { class MyIterator(): java.util.Iterator<Int> {
fun next() = 0 var k : Int = 5
fun hasNext() = false
fun next() : Int = k--
fun hasNext() = k > 0
} }
} }
@@ -37,8 +84,10 @@ class MyCollection2(): Iterable<Int> {
fun iterator(): Iterator<Int> = MyIterator() fun iterator(): Iterator<Int> = MyIterator()
class MyIterator(): Iterator<Int> { class MyIterator(): Iterator<Int> {
fun next() = 0 var k : Int = 5
fun hasNext() = false
fun next() : Int = k--
fun hasNext() = k > 0
} }
} }