001 /*
002 * Copyright 2010-2016 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.kotlin.js.translate.operation;
018
019 import org.jetbrains.annotations.NotNull;
020 import org.jetbrains.kotlin.js.backend.ast.*;
021 import org.jetbrains.kotlin.js.translate.context.TranslationContext;
022 import org.jetbrains.kotlin.lexer.KtToken;
023 import org.jetbrains.kotlin.lexer.KtTokens;
024 import org.jetbrains.kotlin.psi.KtUnaryExpression;
025
026 import static org.jetbrains.kotlin.js.translate.utils.PsiUtils.getOperationToken;
027 import static org.jetbrains.kotlin.js.translate.utils.PsiUtils.isPrefix;
028 import static org.jetbrains.kotlin.js.translate.utils.TranslationUtils.isSimpleNameExpressionNotDelegatedLocalVar;
029
030 public class DynamicIncrementTranslator extends IncrementTranslator {
031
032 @NotNull
033 public static JsExpression doTranslate(@NotNull KtUnaryExpression expression,
034 @NotNull TranslationContext context) {
035 return (new DynamicIncrementTranslator(expression, context))
036 .translate();
037 }
038
039 private DynamicIncrementTranslator(@NotNull KtUnaryExpression expression,
040 @NotNull TranslationContext context) {
041 super(expression, context);
042 }
043
044 @NotNull
045 private JsExpression translate() {
046 if (isSimpleNameExpressionNotDelegatedLocalVar(expression.getBaseExpression(), context())) {
047 return primitiveExpressionIncrement();
048 }
049 return translateIncrementExpression();
050 }
051
052 @NotNull
053 private JsExpression primitiveExpressionIncrement() {
054 JsUnaryOperator operator = OperatorTable.getUnaryOperator(getOperationToken(expression));
055 JsExpression getExpression = accessTranslator.translateAsGet();
056 if (isPrefix(expression)) {
057 return new JsPrefixOperation(operator, getExpression);
058 }
059 else {
060 return new JsPostfixOperation(operator, getExpression);
061 }
062 }
063
064 @Override
065 @NotNull
066 protected JsExpression operationExpression(@NotNull JsExpression receiver) {
067 return unaryAsBinary(receiver);
068 }
069
070 @NotNull
071 private JsBinaryOperation unaryAsBinary(@NotNull JsExpression leftExpression) {
072 JsNumberLiteral oneLiteral = program().getNumberLiteral(1);
073 KtToken token = getOperationToken(expression);
074 if (token.equals(KtTokens.PLUSPLUS)) {
075 return new JsBinaryOperation(JsBinaryOperator.ADD, leftExpression, oneLiteral);
076 }
077 if (token.equals(KtTokens.MINUSMINUS)) {
078 return new JsBinaryOperation(JsBinaryOperator.SUB, leftExpression, oneLiteral);
079 }
080 throw new AssertionError("This method should be called only for increment and decrement operators");
081 }
082
083 }