augmented assignments via StackValue.store()

This commit is contained in:
Dmitry Jemerov
2011-04-15 19:36:11 +02:00
parent a8ac0c05d5
commit f13da41f43
2 changed files with 18 additions and 20 deletions
@@ -785,27 +785,15 @@ public class ExpressionCodegen extends JetVisitor {
}
private void generateAugmentedAssignment(JetBinaryExpression expression) {
DeclarationDescriptor op = bindingContext.resolveReferenceExpression(expression.getOperationReference());
final JetExpression lhs = expression.getLeft();
if (lhs instanceof JetReferenceExpression) {
DeclarationDescriptor op = bindingContext.resolveReferenceExpression(expression.getOperationReference());
final JetType leftType = bindingContext.getExpressionType(lhs);
final Type asmType = typeMapper.mapType(leftType);
if (isNumberPrimitive(asmType)) {
final int index = indexOfLocal((JetReferenceExpression) lhs);
assert index >= 0;
v.load(index, asmType);
gen(expression.getRight(), asmType);
int opcode = opcodeForMethod(op.getName());
v.visitInsn(asmType.getOpcode(opcode));
v.store(index, asmType);
}
else {
throw new UnsupportedOperationException("Don't know how to generate augmented assignment for non-numeric types");
}
}
else {
throw new UnsupportedOperationException("Don't know how to generate augmented assignment to " + lhs.getText());
}
Type asmType = expressionType(lhs);
StackValue value = generateIntermediateValue(lhs);
value.put(asmType, v);
genToJVMStack(expression.getRight());
v.visitInsn(asmType.getOpcode(opcodeForMethod(op.getName())));
value = generateIntermediateValue(lhs);
value.store(v, true);
}
private void generateConcatenation(JetBinaryExpression expression) {
@@ -315,6 +315,16 @@ public class NamespaceGenTest extends CodegenTestCase {
assertEquals(610, c.gridx);
}
public void testFieldAugAssign() throws Exception {
loadText("import java.awt.*; fun foo(c: GridBagConstraints) { c.gridx *= 2; return; }");
System.out.println(generateToText());
final Method main = generateFunction();
GridBagConstraints c = new GridBagConstraints();
c.gridx = 305;
main.invoke(null, c);
assertEquals(610, c.gridx);
}
public void testIncrementAsLastOperation() throws Exception {
loadText("fun foo() { var a = 0; a++; }");
generateFunction(); // make sure we're not falling off end of code