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.descriptors.FunctionDescriptor;
024    import org.jetbrains.jet.lang.psi.JetUnaryExpression;
025    import org.jetbrains.jet.lang.resolve.calls.model.ResolvedCall;
026    import org.jetbrains.jet.lexer.JetTokens;
027    import org.jetbrains.k2js.translate.callTranslator.CallTranslator;
028    import org.jetbrains.k2js.translate.context.TranslationContext;
029    import org.jetbrains.k2js.translate.utils.TranslationUtils;
030    
031    import static org.jetbrains.k2js.translate.general.Translation.translateAsExpression;
032    import static org.jetbrains.k2js.translate.utils.BindingUtils.getFunctionResolvedCall;
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.*;
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            ResolvedCall<? extends FunctionDescriptor> resolvedCall = getFunctionResolvedCall(context.bindingContext(), expression.getOperationReference());
060            return CallTranslator.instance$.translate(context, resolvedCall, baseExpression);
061        }
062    
063        private static boolean isExclForBinaryEqualLikeExpr(@NotNull JetUnaryExpression expression, @NotNull JsExpression baseExpression) {
064            if (getOperationToken(expression).equals(JetTokens.EXCL)) {
065                if (baseExpression instanceof JsBinaryOperation) {
066                    return isEqualLikeOperator(((JsBinaryOperation) baseExpression).getOperator());
067                }
068            }
069            return false;
070        }
071    }