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