提交 e74ea049 编写于 作者: D Dmitry Jemerov

elvis for primitive types; unboxing support

上级 023a6794
......@@ -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();
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册