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.JsExpression;
021 import org.jetbrains.annotations.NotNull;
022 import org.jetbrains.jet.lang.psi.JetUnaryExpression;
023 import org.jetbrains.jet.lexer.JetTokens;
024 import org.jetbrains.k2js.translate.context.TranslationContext;
025 import org.jetbrains.k2js.translate.reference.CallBuilder;
026 import org.jetbrains.k2js.translate.reference.CallType;
027 import org.jetbrains.k2js.translate.utils.TranslationUtils;
028
029 import java.util.Collections;
030
031 import static org.jetbrains.k2js.translate.general.Translation.translateAsExpression;
032 import static org.jetbrains.k2js.translate.utils.BindingUtils.getResolvedCall;
033 import static org.jetbrains.k2js.translate.utils.PsiUtils.getBaseExpression;
034 import static org.jetbrains.k2js.translate.utils.PsiUtils.getOperationToken;
035 import static org.jetbrains.k2js.translate.utils.TranslationUtils.isEqualLikeOperator;
036 import static org.jetbrains.k2js.translate.utils.TranslationUtils.sure;
037 import static org.jetbrains.k2js.translate.utils.TranslationUtils.translateExclForBinaryEqualLikeExpr;
038
039 public final class UnaryOperationTranslator {
040 private UnaryOperationTranslator() {
041 }
042
043 @NotNull
044 public static JsExpression translate(@NotNull JetUnaryExpression expression,
045 @NotNull TranslationContext context) {
046 if (isExclExcl(expression)) {
047 return translateExclExclOperator(expression, context);
048 }
049 if (IncrementTranslator.isIncrement(expression)) {
050 return IncrementTranslator.translate(expression, context);
051 }
052
053 JsExpression baseExpression = TranslationUtils.translateBaseExpression(context, expression);
054 if (isExclForBinaryEqualLikeExpr(expression, baseExpression)) {
055 return translateExclForBinaryEqualLikeExpr((JsBinaryOperation) baseExpression);
056 }
057 return translateAsCall(expression, context, baseExpression);
058 }
059
060 private static boolean isExclExcl(@NotNull JetUnaryExpression expression) {
061 return getOperationToken(expression).equals(JetTokens.EXCLEXCL);
062 }
063
064 @NotNull
065 private static JsExpression translateExclExclOperator(@NotNull JetUnaryExpression expression, @NotNull TranslationContext context) {
066 return sure(translateAsExpression(getBaseExpression(expression), context), context);
067 }
068
069 private static boolean isExclForBinaryEqualLikeExpr(@NotNull JetUnaryExpression expression, @NotNull JsExpression baseExpression) {
070 if (getOperationToken(expression).equals(JetTokens.EXCL)) {
071 if (baseExpression instanceof JsBinaryOperation) {
072 return isEqualLikeOperator(((JsBinaryOperation) baseExpression).getOperator());
073 }
074 }
075 return false;
076 }
077
078 @NotNull
079 private static JsExpression translateAsCall(@NotNull JetUnaryExpression expression,
080 @NotNull TranslationContext context,
081 @NotNull JsExpression baseExpression) {
082 return CallBuilder.build(context)
083 .receiver(baseExpression)
084 .args(Collections.<JsExpression>emptyList())
085 .resolvedCall(getResolvedCall(context.bindingContext(), expression.getOperationReference()))
086 .type(CallType.NORMAL).translate();
087 }
088 }