ExpressionCodegen.java 22.1 KB
Newer Older
M
Maxim Shafirov 已提交
1 2
package org.jetbrains.jet.codegen;

3
import com.intellij.psi.*;
4
import com.intellij.psi.tree.IElementType;
D
Dmitry Jemerov 已提交
5
import org.jetbrains.annotations.Nullable;
M
Maxim Shafirov 已提交
6
import org.jetbrains.jet.lang.psi.*;
7
import org.jetbrains.jet.lang.resolve.BindingContext;
D
Dmitry Jemerov 已提交
8
import org.jetbrains.jet.lang.types.*;
D
Dmitry Jemerov 已提交
9
import org.jetbrains.jet.lexer.JetTokens;
M
Maxim Shafirov 已提交
10 11
import org.objectweb.asm.Label;
import org.objectweb.asm.MethodVisitor;
D
Dmitry Jemerov 已提交
12
import org.objectweb.asm.Opcodes;
M
Maxim Shafirov 已提交
13 14 15 16 17 18 19 20 21 22 23 24
import org.objectweb.asm.Type;
import org.objectweb.asm.commons.InstructionAdapter;

import java.util.List;
import java.util.Stack;

/**
 * @author max
 */
public class ExpressionCodegen extends JetVisitor {
    private final Stack<Label> myLoopStarts = new Stack<Label>();
    private final Stack<Label> myLoopEnds = new Stack<Label>();
25
    private final Stack<StackValue> myStack = new Stack<StackValue>();
M
Maxim Shafirov 已提交
26 27

    private final InstructionAdapter v;
D
Dmitry Jemerov 已提交
28
    private final JetStandardLibrary stdlib;
29
    private final FrameMap myMap;
30 31
    private final JetTypeMapper typeMapper;
    private final Type returnType;
32
    private final BindingContext bindingContext;
M
Maxim Shafirov 已提交
33

34 35
    public ExpressionCodegen(MethodVisitor v, BindingContext bindingContext, JetStandardLibrary stdlib, FrameMap myMap,
                             JetTypeMapper typeMapper, Type returnType) {
D
Dmitry Jemerov 已提交
36
        this.stdlib = stdlib;
37
        this.myMap = myMap;
38 39
        this.typeMapper = typeMapper;
        this.returnType = returnType;
M
Maxim Shafirov 已提交
40
        this.v = new InstructionAdapter(v);
41
        this.bindingContext = bindingContext;
M
Maxim Shafirov 已提交
42 43 44 45 46 47 48
    }

    private void gen(JetElement expr) {
        if (expr == null) throw new CompilationException();
        expr.accept(this);
    }

49 50 51 52 53 54 55 56 57
    private void gen(JetElement expr, Type type) {
        int oldStackDepth = myStack.size();
        gen(expr);
        if (myStack.size() == oldStackDepth+1) {
            StackValue value = myStack.pop();
            value.put(type, v);
        }
    }

M
Maxim Shafirov 已提交
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
    @Override
    public void visitExpression(JetExpression expression) {
        throw new UnsupportedOperationException("Codegen for " + expression + " is not yet implemented");
    }

    @Override
    public void visitParenthesizedExpression(JetParenthesizedExpression expression) {
        gen(expression.getExpression());
    }

    @Override
    public void visitAnnotatedExpression(JetAnnotatedExpression expression) {
        gen(expression.getBaseExpression());
    }

    @Override
    public void visitIfExpression(JetIfExpression expression) {
75 76
        JetType expressionType = bindingContext.getExpressionType(expression);
        Type asmType = typeMapper.mapType(expressionType);
77 78 79
        int oldStackDepth = myStack.size();
        gen(expression.getCondition());
        assert myStack.size() == oldStackDepth+1;
M
Maxim Shafirov 已提交
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99

        JetExpression thenExpression = expression.getThen();
        JetExpression elseExpression = expression.getElse();

        if (thenExpression == null && elseExpression == null) {
            throw new CompilationException();
        }

        if (thenExpression == null) {
            generateSingleBranchIf(elseExpression, false);
            return;
        }

        if (elseExpression == null) {
            generateSingleBranchIf(thenExpression, true);
            return;
        }


        Label elseLabel = new Label();
100
        myStack.pop().condJump(elseLabel, true, v);   // == 0, i.e. false
M
Maxim Shafirov 已提交
101

102
        gen(thenExpression, asmType);
M
Maxim Shafirov 已提交
103 104 105 106 107

        Label endLabel = new Label();
        v.goTo(endLabel);
        v.mark(elseLabel);

108
        gen(elseExpression, asmType);
M
Maxim Shafirov 已提交
109 110

        v.mark(endLabel);
D
Dmitry Jemerov 已提交
111 112 113
        if (asmType != Type.VOID_TYPE) {
            myStack.push(StackValue.onStack(asmType));
        }
M
Maxim Shafirov 已提交
114 115 116 117 118 119 120 121 122 123 124 125
    }

    @Override
    public void visitWhileExpression(JetWhileExpression expression) {
        Label condition = new Label();
        myLoopStarts.push(condition);
        v.mark(condition);

        Label end = new Label();
        myLoopEnds.push(end);

        gen(expression.getCondition());
126
        myStack.pop().condJump(end, true, v);
M
Maxim Shafirov 已提交
127

128
        gen(expression.getBody(), Type.VOID_TYPE);
M
Maxim Shafirov 已提交
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
        v.goTo(condition);

        v.mark(end);
        myLoopEnds.pop();
        myLoopStarts.pop();
    }

    @Override
    public void visitDoWhileExpression(JetDoWhileExpression expression) {
        Label condition = new Label();
        v.mark(condition);
        myLoopStarts.push(condition);

        Label end = new Label();
        myLoopEnds.push(end);

145
        gen(expression.getBody(), Type.VOID_TYPE);
M
Maxim Shafirov 已提交
146 147

        gen(expression.getCondition());
D
Dmitry Jemerov 已提交
148
        myStack.pop().condJump(condition, false, v);
M
Maxim Shafirov 已提交
149 150 151 152 153 154 155 156 157

        v.mark(end);

        myLoopEnds.pop();
        myLoopStarts.pop();
    }

    @Override
    public void visitBreakExpression(JetBreakExpression expression) {
A
Andrey Breslav 已提交
158
        JetSimpleNameExpression labelElement = expression.getTargetLabel();
M
Maxim Shafirov 已提交
159

A
Andrey Breslav 已提交
160
        Label label = labelElement == null ? myLoopEnds.peek() : null; // TODO:
M
Maxim Shafirov 已提交
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180

        v.goTo(label);
    }

    @Override
    public void visitContinueExpression(JetContinueExpression expression) {
        String labelName = expression.getLabelName();

        Label label = labelName == null ? myLoopStarts.peek() : null; // TODO:

        v.goTo(label);
    }

    private void unboxBoolean() {
        v.invokevirtual("java/lang/Boolean", "booleanValue", "()Z");
    }

    private void generateSingleBranchIf(JetExpression expression, boolean inverse) {
        Label endLabel = new Label();

181
        myStack.pop().condJump(endLabel, inverse, v);
M
Maxim Shafirov 已提交
182

183
        gen(expression, Type.VOID_TYPE);
M
Maxim Shafirov 已提交
184 185 186 187 188 189

        v.mark(endLabel);
    }

    @Override
    public void visitConstantExpression(JetConstantExpression expression) {
D
Dmitry Jemerov 已提交
190
        myStack.push(StackValue.constant(expression.getValue(), expressionType(expression)));
M
Maxim Shafirov 已提交
191 192 193 194
    }

    @Override
    public void visitBlockExpression(JetBlockExpression expression) {
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
        List<JetElement> statements = expression.getStatements();
        generateBlock(statements);
    }

    @Override
    public void visitFunctionLiteralExpression(JetFunctionLiteralExpression expression) {
        if (bindingContext.isBlock(expression)) {
            generateBlock(expression.getBody());
        }
        else {
            throw new UnsupportedOperationException("don't know how to generate non-block function literals");
        }
    }

    private void generateBlock(List<JetElement> statements) {
M
Maxim Shafirov 已提交
210 211 212 213 214
        Label blockStart = new Label();
        v.mark(blockStart);

        for (JetElement statement : statements) {
            if (statement instanceof JetProperty) {
D
Dmitry Jemerov 已提交
215 216 217
                final PropertyDescriptor propertyDescriptor = bindingContext.getPropertyDescriptor((JetProperty) statement);
                final Type type = typeMapper.mapType(propertyDescriptor.getOutType());
                myMap.enter(propertyDescriptor, type.getSize());
M
Maxim Shafirov 已提交
218 219 220 221 222 223 224 225 226 227 228 229 230
            }
        }

        for (JetElement statement : statements) {
            gen(statement);
        }

        Label blockEnd = new Label();
        v.mark(blockEnd);

        for (JetElement statement : statements) {
            if (statement instanceof JetProperty) {
                JetProperty var = (JetProperty) statement;
231 232 233 234 235
                PropertyDescriptor propertyDescriptor = bindingContext.getPropertyDescriptor(var);
                Type outType = typeMapper.mapType(propertyDescriptor.getOutType());

                int index = myMap.leave(propertyDescriptor);
                v.visitLocalVariable(var.getName(), outType.getDescriptor(), null, blockStart, blockEnd, index);
M
Maxim Shafirov 已提交
236 237 238 239
            }
        }
    }

D
Dmitry Jemerov 已提交
240 241 242 243
    @Override
    public void visitReturnExpression(JetReturnExpression expression) {
        final JetExpression returnedExpression = expression.getReturnedExpression();
        if (returnedExpression != null) {
244 245
            gen(returnedExpression, returnType);
            v.areturn(returnType);
D
Dmitry Jemerov 已提交
246 247 248 249 250 251
        }
        else {
            v.visitInsn(Opcodes.RETURN);
        }
    }

252 253 254 255 256 257 258 259
    public void returnTopOfStack() {
        if (myStack.size() > 0) {
            StackValue value = myStack.pop();
            value.put(returnType, v);
            v.areturn(returnType);
        }
    }

260
    @Override
261
    public void visitSimpleNameExpression(JetSimpleNameExpression expression) {
A
Andrey Breslav 已提交
262
        final DeclarationDescriptor descriptor = bindingContext.resolveReferenceExpression(expression);
D
Dmitry Jemerov 已提交
263 264 265 266 267 268 269 270 271 272 273 274
        if (descriptor instanceof PropertyDescriptor) {
            final DeclarationDescriptor container = descriptor.getContainingDeclaration();
            if (isClass(container, "Number")) {
                Type castType = getCastType(expression.getReferencedName());
                if (castType != null) {
                    final StackValue value = myStack.pop();
                    value.put(castType, v);
                    myStack.push(StackValue.onStack(castType));
                    return;
                }
            }
        }
D
Dmitry Jemerov 已提交
275 276 277 278 279 280 281 282 283 284 285 286
        PsiElement declaration = bindingContext.getDeclarationPsiElement(descriptor);
        if (declaration instanceof PsiField) {
            PsiField psiField = (PsiField) declaration;
            if (psiField.hasModifierProperty(PsiModifier.STATIC)) {
                v.visitFieldInsn(Opcodes.GETSTATIC,
                                 jvmName(psiField.getContainingClass()),
                                 psiField.getName(),
                                 psiTypeToAsm(psiField.getType()).getDescriptor());
            }
            else {
                throw new UnsupportedOperationException("don't know how to generate field reference " + descriptor);
            }
287 288
        }
        else {
D
Dmitry Jemerov 已提交
289 290
            int index = myMap.getIndex(descriptor);
            if (index >= 0) {
D
Dmitry Jemerov 已提交
291 292
                final JetType outType = ((PropertyDescriptor) descriptor).getOutType();
                myStack.push(StackValue.local(index, typeMapper.mapType(outType)));
D
Dmitry Jemerov 已提交
293 294 295 296
            }
            else {
                throw new UnsupportedOperationException("don't know how to generate reference " + descriptor);
            }
297 298 299
        }
    }

D
Dmitry Jemerov 已提交
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
    @Nullable
    private static Type getCastType(String castMethodName) {
        if ("dbl".equals(castMethodName)) {
            return Type.DOUBLE_TYPE;
        }
        if ("flt".equals(castMethodName)) {
            return Type.FLOAT_TYPE;
        }
        if ("lng".equals(castMethodName)) {
            return Type.LONG_TYPE;
        }
        if ("int".equals(castMethodName)) {
            return Type.INT_TYPE;
        }
        if ("chr".equals(castMethodName)) {
            return Type.CHAR_TYPE;
        }
        if ("sht".equals(castMethodName)) {
            return Type.SHORT_TYPE;
        }
        if ("byt".equals(castMethodName)) {
            return Type.BYTE_TYPE;
        }
        return null;
    }

326 327 328 329
    @Override
    public void visitCallExpression(JetCallExpression expression) {
        JetExpression callee = expression.getCalleeExpression();

330 331
        if (callee instanceof JetSimpleNameExpression) {
            DeclarationDescriptor funDescriptor = bindingContext.resolveReferenceExpression((JetSimpleNameExpression) callee);
332
            if (funDescriptor instanceof FunctionDescriptor) {
333 334 335
                PsiElement declarationPsiElement = bindingContext.getDeclarationPsiElement(funDescriptor);
                if (declarationPsiElement instanceof PsiMethod) {
                    PsiMethod method = (PsiMethod) declarationPsiElement;
336 337 338 339 340 341 342 343
                    PsiParameter[] parameters = method.getParameterList().getParameters();

                    List<JetArgument> args = expression.getValueArguments();
                    for (int i = 0, argsSize = args.size(); i < argsSize; i++) {
                        JetArgument arg = args.get(i);
                        gen(arg.getArgumentExpression(), psiTypeToAsm(parameters [i].getType()));
                    }

344
                    if (method.hasModifierProperty(PsiModifier.STATIC)) {
D
Dmitry Jemerov 已提交
345
                        v.visitMethodInsn(Opcodes.INVOKESTATIC,
D
Dmitry Jemerov 已提交
346 347 348 349 350 351
                                jvmName(method.getContainingClass()),
                                method.getName(),
                                getMethodDescriptor(method));
                    }
                    else {
                        v.visitMethodInsn(Opcodes.INVOKEVIRTUAL,
D
Dmitry Jemerov 已提交
352 353 354
                                          jvmName(method.getContainingClass()),
                                          method.getName(),
                                          getMethodDescriptor(method));
355
                    }
D
Dmitry Jemerov 已提交
356 357 358 359
                    final Type type = psiTypeToAsm(method.getReturnType());
                    if (type != Type.VOID_TYPE) {
                        myStack.push(StackValue.onStack(type));
                    }
360
                }
361 362 363 364 365 366 367 368 369 370
            }
            else {
                throw new CompilationException();
            }
        }
        else {
            throw new UnsupportedOperationException("Don't know how to generate a call");
        }
    }

371 372 373 374
    private static String jvmName(PsiClass containingClass) {
        return containingClass.getQualifiedName().replace(".", "/");
    }

D
Dmitry Jemerov 已提交
375 376 377 378 379 380 381 382 383 384 385
    private void unbox(PsiType type) {
        if (type instanceof PsiPrimitiveType) {
            if (type == PsiType.INT) {
                v.invokevirtual("java/lang/Integer", "intValue", "()I");
            }
            else {
                throw new UnsupportedOperationException("Don't know how to unbox type " + type);
            }
        }
    }

386
    private static String getMethodDescriptor(PsiMethod method) {
387 388 389 390 391 392 393 394 395
        Type returnType = psiTypeToAsm(method.getReturnType());
        PsiParameter[] parameters = method.getParameterList().getParameters();
        Type[] parameterTypes = new Type[parameters.length];
        for (int i = 0; i < parameters.length; i++) {
            parameterTypes[i] = psiTypeToAsm(parameters [i].getType());
        }
        return Type.getMethodDescriptor(returnType, parameterTypes);
    }

D
Dmitry Jemerov 已提交
396 397 398 399
    private Type expressionType(JetExpression expr) {
        return typeMapper.mapType(bindingContext.getExpressionType(expr));
    }

400
    private static Type psiTypeToAsm(PsiType type) {
401 402 403 404
        if (type instanceof PsiPrimitiveType) {
            if (type == PsiType.VOID) {
                return Type.VOID_TYPE;
            }
405 406 407
            if (type == PsiType.INT) {
                return Type.INT_TYPE;
            }
408 409 410
            if (type == PsiType.LONG) {
                return Type.LONG_TYPE;
            }
411 412 413
            if (type == PsiType.BOOLEAN) {
                return Type.BOOLEAN_TYPE;
            }
414 415 416 417 418 419 420 421 422 423 424 425 426 427 428
            if (type == PsiType.BYTE) {
                return Type.BYTE_TYPE;
            }
            if (type == PsiType.SHORT) {
                return Type.SHORT_TYPE;
            }
            if (type == PsiType.CHAR) {
                return Type.CHAR_TYPE;
            }
            if (type == PsiType.FLOAT) {
                return Type.FLOAT_TYPE;
            }
            if (type == PsiType.DOUBLE) {
                return Type.DOUBLE_TYPE;
            }
429
        }
430 431 432 433 434 435 436
        if (type instanceof PsiClassType) {
            PsiClass psiClass = ((PsiClassType) type).resolve();
            if (psiClass == null) {
                throw new UnsupportedOperationException("unresolved PsiClassType: " + type);
            }
            return Type.getType("L" + jvmName(psiClass) + ";");
        }
437 438 439
        throw new UnsupportedOperationException("don't know how to map  type " + type + " to ASM");
    }

440
    @Override
441
    public void visitDotQualifiedExpression(JetDotQualifiedExpression expression) {
D
Dmitry Jemerov 已提交
442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475
        JetExpression receiver = expression.getReceiverExpression();
        if (!resolvesToClassOrPackage(receiver)) {
            gen(expression.getReceiverExpression());
        }
        gen(expression.getSelectorExpression());
    }

    private boolean resolvesToClassOrPackage(JetExpression receiver) {
        if (receiver instanceof JetReferenceExpression) {
            DeclarationDescriptor declaration = bindingContext.resolveReferenceExpression((JetReferenceExpression) receiver);
            PsiElement declarationElement = bindingContext.getDeclarationPsiElement(declaration);
            if (declarationElement instanceof PsiClass) {
                return true;
            }
        }
        return false;
    }

    @Override
    public void visitSafeQualifiedExpression(JetSafeQualifiedExpression expression) {
        gen(expression.getReceiverExpression());
        Label ifnull = new Label();
        Label end = new Label();
        v.dup();
        v.ifnull(ifnull);
        gen(expression.getSelectorExpression());
        v.goTo(end);
        v.mark(ifnull);
        // null is already on stack here after the dup
        JetType expressionType = bindingContext.getExpressionType(expression);
        if (expressionType.equals(JetStandardClasses.getUnitType())) {
            v.pop();
        }
        v.mark(end);
476
    }
477

D
Dmitry Jemerov 已提交
478 479
    @Override
    public void visitBinaryExpression(JetBinaryExpression expression) {
480 481
        final IElementType opToken = expression.getOperationReference().getReferencedNameElementType();
        if (opToken == JetTokens.EQ) {
D
Dmitry Jemerov 已提交
482 483 484
            generateAssignmentExpression(expression);
            return;
        }
D
Dmitry Jemerov 已提交
485 486
        DeclarationDescriptor op = bindingContext.resolveReferenceExpression(expression.getOperationReference());
        if (op instanceof FunctionDescriptor) {
D
Float  
Dmitry Jemerov 已提交
487 488
            JetType returnType = bindingContext.getExpressionType(expression);
            final Type asmType = typeMapper.mapType(returnType);
D
Dmitry Jemerov 已提交
489
            DeclarationDescriptor cls = op.getContainingDeclaration();
D
Dmitry Jemerov 已提交
490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506
            if (isNumberPrimitive(cls)) {
                if (op.getName().equals("compareTo")) {
                    generateCompareOp(expression, opToken, asmType);
                }
                else {
                    int opcode = opcodeForMethod(op.getName());
                    generateBinaryOp(expression, (FunctionDescriptor) op, opcode);
                }
                return;
            }
            else if (isClass(cls, "Hashable")) {
                if (op.getName().equals("equals")) {
                    final Type leftType = typeMapper.mapType(bindingContext.getExpressionType(expression.getLeft()));
                    final Type rightType = typeMapper.mapType(bindingContext.getExpressionType(expression.getRight()));
                    if (isNumberPrimitive(leftType) && leftType == rightType) {
                        generateCompareOp(expression, opToken, leftType);
                        return;
507
                    }
508
                    else {
D
Dmitry Jemerov 已提交
509
                        throw new UnsupportedOperationException("Don't know how to generate equality for these types");
510 511
                    }
                }
D
Dmitry Jemerov 已提交
512
            }
513
        }
D
Dmitry Jemerov 已提交
514 515
        throw new UnsupportedOperationException("Don't know how to generate binary op " + expression);
    }
516

D
Dmitry Jemerov 已提交
517 518 519 520 521
    private static boolean isNumberPrimitive(DeclarationDescriptor descriptor) {
        if (!(descriptor instanceof ClassDescriptor)) {
            return false;
        }
        String className = descriptor.getName();
D
Float  
Dmitry Jemerov 已提交
522
        return className.equals("Int") || className.equals("Long") || className.equals("Short") ||
D
Double  
Dmitry Jemerov 已提交
523 524
               className.equals("Byte") || className.equals("Char") || className.equals("Float") ||
               className.equals("Double");
D
Float  
Dmitry Jemerov 已提交
525 526
    }

D
Dmitry Jemerov 已提交
527 528 529 530 531 532 533 534
    private static boolean isClass(DeclarationDescriptor descriptor, String name) {
        if (!(descriptor instanceof ClassDescriptor)) {
            return false;
        }
        String className = descriptor.getName();
        return className.equals(name);
    }

D
Float  
Dmitry Jemerov 已提交
535 536
    private static boolean isNumberPrimitive(Type type) {
        return type == Type.INT_TYPE || type == Type.SHORT_TYPE || type == Type.BYTE_TYPE || type == Type.CHAR_TYPE ||
D
Dmitry Jemerov 已提交
537
               type == Type.FLOAT_TYPE || type == Type.DOUBLE_TYPE || type == Type.LONG_TYPE;
D
Dmitry Jemerov 已提交
538 539
    }

D
Dmitry Jemerov 已提交
540
    private static int opcodeForMethod(final String name) {
D
div/mod  
Dmitry Jemerov 已提交
541 542 543 544 545
        if (name.equals("plus")) return Opcodes.IADD;
        if (name.equals("minus")) return Opcodes.ISUB;
        if (name.equals("times")) return Opcodes.IMUL;
        if (name.equals("div")) return Opcodes.IDIV;
        if (name.equals("mod")) return Opcodes.IREM;
D
Dmitry Jemerov 已提交
546 547 548
        throw new UnsupportedOperationException("Don't know how to generate binary op method " + name);
    }

549 550
    private void generateBinaryOp(JetBinaryExpression expression, FunctionDescriptor op, int opcode) {
        JetType returnType = op.getUnsubstitutedReturnType();
D
Dmitry Jemerov 已提交
551
        final Type asmType = typeMapper.mapType(returnType);
D
Double  
Dmitry Jemerov 已提交
552 553
        if (asmType == Type.INT_TYPE || asmType == Type.LONG_TYPE ||
            asmType == Type.FLOAT_TYPE || asmType == Type.DOUBLE_TYPE) {
D
Dmitry Jemerov 已提交
554 555 556 557
            gen(expression.getLeft(), asmType);
            gen(expression.getRight(), asmType);
            v.visitInsn(asmType.getOpcode(opcode));
            myStack.push(StackValue.onStack(asmType));
558 559 560 561 562 563
        }
        else {
            throw new UnsupportedOperationException("Don't know how to generate binary op with return type " + returnType);
        }
    }

D
Float  
Dmitry Jemerov 已提交
564 565 566 567
    private void generateCompareOp(JetBinaryExpression expression, IElementType opToken, Type type) {
        gen(expression.getLeft(), type);
        gen(expression.getRight(), type);
        myStack.push(StackValue.cmp(opToken, type));
568 569
    }

D
Dmitry Jemerov 已提交
570 571 572 573 574 575 576 577 578 579 580 581 582 583
    private void generateAssignmentExpression(JetBinaryExpression expression) {
        if (expression.getLeft() instanceof JetReferenceExpression) {
            final JetReferenceExpression lhs = (JetReferenceExpression) expression.getLeft();
            final DeclarationDescriptor declarationDescriptor = bindingContext.resolveReferenceExpression(lhs);
            final int index = myMap.getIndex(declarationDescriptor);
            final Type type = typeMapper.mapType(bindingContext.getExpressionType(lhs));
            gen(expression.getRight(), type);
            v.store(index, type);
        }
        else {
            throw new UnsupportedOperationException("Don't know how to generate assignment to " + expression.getLeft().getText());
        }
    }

M
Maxim Shafirov 已提交
584 585
    @Override
    public void visitProperty(JetProperty property) {
586 587
        PropertyDescriptor propertyDescriptor = bindingContext.getPropertyDescriptor(property);
        int index = myMap.getIndex(propertyDescriptor);
M
Maxim Shafirov 已提交
588 589 590 591 592

        assert index >= 0;

        JetExpression initializer = property.getInitializer();
        if (initializer != null) {
593 594 595
            Type type = typeMapper.mapType(propertyDescriptor.getOutType());
            gen(initializer, type);
            v.store(index, type);
M
Maxim Shafirov 已提交
596 597 598 599 600 601
        }
    }

    private static class CompilationException extends RuntimeException {
    }
}