001 /*
002 * Copyright 2010-2013 JetBrains s.r.o.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017 package org.jetbrains.k2js.translate.operation;
018
019 import com.google.dart.compiler.backend.js.ast.JsBinaryOperation;
020 import com.google.dart.compiler.backend.js.ast.JsBinaryOperator;
021 import com.google.dart.compiler.backend.js.ast.JsExpression;
022 import org.jetbrains.annotations.NotNull;
023 import org.jetbrains.jet.lang.psi.JetBinaryExpression;
024 import org.jetbrains.jet.lang.psi.JetSimpleNameExpression;
025 import org.jetbrains.jet.lang.types.expressions.OperatorConventions;
026 import org.jetbrains.jet.lexer.JetToken;
027 import org.jetbrains.k2js.translate.context.TranslationContext;
028 import org.jetbrains.k2js.translate.utils.JsAstUtils;
029
030 import static org.jetbrains.k2js.translate.utils.PsiUtils.getOperationToken;
031 import static org.jetbrains.k2js.translate.utils.PsiUtils.isAssignment;
032
033 public final class IntrinsicAssignmentTranslator extends AssignmentTranslator {
034
035
036 @NotNull
037 public static JsExpression doTranslate(@NotNull JetBinaryExpression expression,
038 @NotNull TranslationContext context) {
039 return (new IntrinsicAssignmentTranslator(expression, context)).translate();
040 }
041
042 private IntrinsicAssignmentTranslator(@NotNull JetBinaryExpression expression,
043 @NotNull TranslationContext context) {
044 super(expression, context);
045 }
046
047 @NotNull
048 private JsExpression translate() {
049 if (isAssignment(getOperationToken(expression))) {
050 return translateAsPlainAssignment();
051 }
052 return translateAsAssignmentOperation();
053 }
054
055 @NotNull
056 private JsExpression translateAsAssignmentOperation() {
057 if (expression.getLeft() instanceof JetSimpleNameExpression) {
058 return translateAsPlainAssignmentOperation();
059 }
060 return translateAsAssignToCounterpart();
061 }
062
063 @NotNull
064 private JsExpression translateAsAssignToCounterpart() {
065 JsBinaryOperator operator = getCounterpartOperator();
066 JsBinaryOperation counterpartOperation =
067 new JsBinaryOperation(operator, accessTranslator.translateAsGet(), right);
068 return accessTranslator.translateAsSet(counterpartOperation);
069 }
070
071 @NotNull
072 private JsBinaryOperator getCounterpartOperator() {
073 JetToken assignmentOperationToken = getOperationToken(expression);
074 assert OperatorConventions.ASSIGNMENT_OPERATIONS.containsKey(assignmentOperationToken);
075 JetToken counterpartToken = OperatorConventions.ASSIGNMENT_OPERATION_COUNTERPARTS.get(assignmentOperationToken);
076 assert OperatorTable.hasCorrespondingBinaryOperator(counterpartToken) :
077 "Unsupported token encountered: " + counterpartToken.toString();
078 return OperatorTable.getBinaryOperator(counterpartToken);
079 }
080
081 @NotNull
082 private JsExpression translateAsPlainAssignmentOperation() {
083 JsBinaryOperator operator = getAssignmentOperator();
084 return new JsBinaryOperation(operator, accessTranslator.translateAsGet(), right);
085 }
086
087 @NotNull
088 private JsBinaryOperator getAssignmentOperator() {
089 JetToken token = getOperationToken(expression);
090 assert OperatorConventions.ASSIGNMENT_OPERATIONS.containsKey(token);
091 assert OperatorTable.hasCorrespondingBinaryOperator(token) :
092 "Unsupported token encountered: " + token.toString();
093 return OperatorTable.getBinaryOperator(token);
094 }
095
096 @NotNull
097 private JsExpression translateAsPlainAssignment() {
098 return accessTranslator.translateAsSet(right);
099 }
100 }