elvis for primitive types; unboxing support
This commit is contained in:
@@ -62,6 +62,10 @@ public class ExpressionCodegen extends JetVisitor {
|
||||
}
|
||||
}
|
||||
|
||||
private void genToStack(JetExpression expr) {
|
||||
gen(expr, expressionType(expr));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitExpression(JetExpression expression) {
|
||||
throw new UnsupportedOperationException("Codegen for " + expression + " is not yet implemented");
|
||||
@@ -621,11 +625,13 @@ public class ExpressionCodegen extends JetVisitor {
|
||||
|
||||
private void generateElvis(JetBinaryExpression expression) {
|
||||
final Type exprType = expressionType(expression);
|
||||
gen(expression.getLeft(), exprType);
|
||||
final Type leftType = expressionType(expression.getLeft());
|
||||
gen(expression.getLeft(), leftType);
|
||||
v.dup();
|
||||
Label end = new Label();
|
||||
Label ifNull = new Label();
|
||||
v.ifnull(ifNull);
|
||||
StackValue.onStack(leftType).put(exprType, v);
|
||||
v.goTo(end);
|
||||
v.mark(ifNull);
|
||||
v.pop();
|
||||
|
||||
@@ -69,10 +69,40 @@ public abstract class StackValue {
|
||||
}
|
||||
}
|
||||
|
||||
private static void unbox(final Type type, InstructionAdapter v) {
|
||||
if (type == Type.INT_TYPE) {
|
||||
v.invokevirtual("java/lang/Number", "intValue", "()I");
|
||||
}
|
||||
else if (type == Type.BOOLEAN_TYPE) {
|
||||
v.invokevirtual("java/lang/Boolean", "booleanValue", "()Z");
|
||||
}
|
||||
else if (type == Type.CHAR_TYPE) {
|
||||
v.invokevirtual("java/lang/Character", "charValue", "()C");
|
||||
}
|
||||
else if (type == Type.SHORT_TYPE) {
|
||||
v.invokevirtual("java/lang/Number", "shortValue", "()S");
|
||||
}
|
||||
else if (type == Type.LONG_TYPE) {
|
||||
v.invokevirtual("java/lang/Number", "longValue", "()J");
|
||||
}
|
||||
else if (type == Type.BYTE_TYPE) {
|
||||
v.invokevirtual("java/lang/Number", "byteValue", "()B");
|
||||
}
|
||||
else if (type == Type.FLOAT_TYPE) {
|
||||
v.invokevirtual("java/lang/Number", "floatValue", "()F");
|
||||
}
|
||||
else if (type == Type.DOUBLE_TYPE) {
|
||||
v.invokevirtual("java/lang/Number", "doubleValue", "()D");
|
||||
}
|
||||
}
|
||||
|
||||
protected void coerce(Type type, InstructionAdapter v) {
|
||||
if (type.getSort() == Type.OBJECT) {
|
||||
box(this.type, v);
|
||||
}
|
||||
else if (this.type.getSort() == Type.OBJECT && type.getSort() <= Type.DOUBLE) {
|
||||
unbox(type, v);
|
||||
}
|
||||
else if (type != this.type) {
|
||||
v.cast(this.type, type);
|
||||
}
|
||||
|
||||
@@ -275,6 +275,13 @@ public class NamespaceGenTest extends CodegenTestCase {
|
||||
assertEquals("null", main.invoke(null, new Object[] { null }));
|
||||
}
|
||||
|
||||
public void testElvisInt() throws Exception {
|
||||
loadText("fun foo(a: Int?): Int = a ?: 239");
|
||||
final Method main = generateFunction();
|
||||
assertEquals(610, main.invoke(null, 610));
|
||||
assertEquals(239, main.invoke(null, new Object[]{null}));
|
||||
}
|
||||
|
||||
public void _testVarargs() throws Exception {
|
||||
loadText("fun foo() = java.util.Arrays.asList(\"IntelliJ\", \"IDEA\")");
|
||||
final Method main = generateFunction();
|
||||
|
||||
Reference in New Issue
Block a user