提交 232fc89c 编写于 作者: A Alex Tkachman

fix for KT-80

上级 ab63974f
......@@ -809,7 +809,7 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> {
v.visitMethodInsn(isStatic ? Opcodes.INVOKESTATIC : isInterface ? Opcodes.INVOKEINTERFACE : Opcodes.INVOKEVIRTUAL, owner, functionDescriptor.getName(), typeMapper.mapSignature(functionDescriptor.getName(),functionDescriptor).getDescriptor());
StackValue.onStack(typeMapper.mapType(functionDescriptor.getReturnType())).coerce(type, v);
}
public StackValue intermediateValueForProperty(PropertyDescriptor propertyDescriptor, final boolean forceField, boolean forceInterface) {
DeclarationDescriptor containingDeclaration = propertyDescriptor.getContainingDeclaration();
boolean isStatic = containingDeclaration instanceof NamespaceDescriptorImpl;
......@@ -1990,11 +1990,42 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> {
@Nullable Label nextEntry) {
StackValue conditionValue;
if (condition instanceof JetWhenConditionInRange) {
JetExpression range = ((JetWhenConditionInRange) condition).getRangeExpression();
gen(range, RANGE_TYPE);
new StackValue.Local(subjectLocal, subjectType).put(INTEGER_TYPE, v);
v.invokeinterface(CLASS_RANGE, "contains", "(Ljava/lang/Comparable;)Z");
conditionValue = new StackValue.OnStack(Type.BOOLEAN_TYPE);
JetWhenConditionInRange conditionInRange = (JetWhenConditionInRange) condition;
JetExpression rangeExpression = conditionInRange.getRangeExpression();
if(isIntRangeExpr(rangeExpression)) {
v.iconst(1);
//noinspection ConstantConditions
new StackValue.Local(subjectLocal, subjectType).put(Type.INT_TYPE, v);
JetBinaryExpression binaryExpression = (JetBinaryExpression) rangeExpression;
gen(binaryExpression.getLeft(), Type.INT_TYPE);
Label lok = new Label();
v.ificmpge(lok);
v.pop();
v.iconst(0);
v.mark(lok);
v.iconst(1);
new StackValue.Local(subjectLocal, subjectType).put(Type.INT_TYPE, v);
gen(binaryExpression.getRight(), Type.INT_TYPE);
Label rok = new Label();
v.ificmple(rok);
v.pop();
v.iconst(0);
v.mark(rok);
v.and(Type.INT_TYPE);
if(conditionInRange.getOperationReference().getReferencedNameElementType() == JetTokens.NOT_IN) {
v.iconst(1);
v.xor(Type.INT_TYPE);
}
}
else {
FunctionDescriptor op = (FunctionDescriptor) bindingContext.get(BindingContext.REFERENCE_TARGET, conditionInRange.getOperationReference());
genToJVMStack(rangeExpression);
new StackValue.Local(subjectLocal, subjectType).put(JetTypeMapper.TYPE_OBJECT, v);
invokeFunctionNoParams(op, Type.BOOLEAN_TYPE, v);
}
return StackValue.onStack(Type.BOOLEAN_TYPE);
}
else if (condition instanceof JetWhenConditionIsPattern) {
JetWhenConditionIsPattern patternCondition = (JetWhenConditionIsPattern) condition;
......@@ -2032,6 +2063,20 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> {
return conditionValue;
}
private boolean isIntRangeExpr(JetExpression rangeExpression) {
if(rangeExpression instanceof JetBinaryExpression) {
JetBinaryExpression binaryExpression = (JetBinaryExpression) rangeExpression;
if (binaryExpression.getOperationReference().getReferencedNameElementType() == JetTokens.RANGE) {
JetType jetType = bindingContext.get(BindingContext.EXPRESSION_TYPE, rangeExpression);
final DeclarationDescriptor descriptor = jetType.getConstructor().getDeclarationDescriptor();
if (isClass(descriptor, "IntRange")) { // TODO IntRange subclasses
return true;
}
}
}
return false ;
}
@Override
public StackValue visitTupleExpression(JetTupleExpression expression, StackValue receiver) {
final List<JetExpression> entries = expression.getEntries();
......
......@@ -275,7 +275,19 @@ public abstract class StackValue {
@Override
public void put(Type type, InstructionAdapter v) {
v.aconst(value);
if(value instanceof Integer)
v.iconst((Integer) value);
else
if(value instanceof Long)
v.lconst((Long) value);
else
if(value instanceof Float)
v.fconst((Float) value);
else
if(value instanceof Double)
v.dconst((Double) value);
else
v.aconst(value);
coerce(type, v);
}
......
fun isDigit(a: Int) = when(a) {
in 0..9 => "digit"
else => "something"
fun isDigit(a: Int) : String {
val aa = java.util.ArrayList<Int> ()
aa.add(239)
return when(a) {
in aa => "array list"
in 0..9 => "digit"
!in 0..100 => "not small"
else => "something"
}
}
......@@ -39,8 +39,12 @@ public class PatternMatchingTest extends CodegenTestCase {
loadFile();
System.out.println(generateToText());
Method foo = generateFunction();
assertEquals("array list", foo.invoke(null, 239));
assertEquals("digit", foo.invoke(null, 0));
assertEquals("digit", foo.invoke(null, 9));
assertEquals("digit", foo.invoke(null, 5));
assertEquals("something", foo.invoke(null, 19));
assertEquals("not small", foo.invoke(null, 190));
}
public void testRangeChar() throws Exception {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册