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 com.intellij.psi.tree.IElementType;
022    import org.jetbrains.annotations.NotNull;
023    import org.jetbrains.jet.lang.psi.JetUnaryExpression;
024    import org.jetbrains.jet.lexer.JetTokens;
025    import org.jetbrains.k2js.translate.context.TranslationContext;
026    import org.jetbrains.k2js.translate.reference.CallBuilder;
027    import org.jetbrains.k2js.translate.utils.TranslationUtils;
028    
029    import static org.jetbrains.k2js.translate.general.Translation.translateAsExpression;
030    import static org.jetbrains.k2js.translate.utils.BindingUtils.getResolvedCall;
031    import static org.jetbrains.k2js.translate.utils.PsiUtils.getBaseExpression;
032    import static org.jetbrains.k2js.translate.utils.PsiUtils.getOperationToken;
033    import static org.jetbrains.k2js.translate.utils.TranslationUtils.isEqualLikeOperator;
034    import static org.jetbrains.k2js.translate.utils.TranslationUtils.sure;
035    import static org.jetbrains.k2js.translate.utils.TranslationUtils.translateExclForBinaryEqualLikeExpr;
036    
037    public final class UnaryOperationTranslator {
038        private UnaryOperationTranslator() {
039        }
040    
041        @NotNull
042        public static JsExpression translate(
043                @NotNull JetUnaryExpression expression,
044                @NotNull TranslationContext context
045        ) {
046            IElementType operationToken = expression.getOperationReference().getReferencedNameElementType();
047            if (operationToken == JetTokens.EXCLEXCL) {
048                return sure(translateAsExpression(getBaseExpression(expression), context), context);
049            }
050            if (IncrementTranslator.isIncrement(operationToken)) {
051                return IncrementTranslator.translate(expression, context);
052            }
053    
054            JsExpression baseExpression = TranslationUtils.translateBaseExpression(context, expression);
055            if (isExclForBinaryEqualLikeExpr(expression, baseExpression)) {
056                return translateExclForBinaryEqualLikeExpr((JsBinaryOperation) baseExpression);
057            }
058    
059            return CallBuilder.build(context)
060                    .receiver(TranslationUtils.translateBaseExpression(context, expression))
061                    .resolvedCall(getResolvedCall(context.bindingContext(), expression.getOperationReference()))
062                    .translate();
063        }
064    
065        private static boolean isExclForBinaryEqualLikeExpr(@NotNull JetUnaryExpression expression, @NotNull JsExpression baseExpression) {
066            if (getOperationToken(expression).equals(JetTokens.EXCL)) {
067                if (baseExpression instanceof JsBinaryOperation) {
068                    return isEqualLikeOperator(((JsBinaryOperation) baseExpression).getOperator());
069                }
070            }
071            return false;
072        }
073    }