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.JetExpression;
025    import org.jetbrains.jet.lang.psi.JetUnaryExpression;
026    import org.jetbrains.jet.lang.resolve.BindingContext;
027    import org.jetbrains.jet.lang.resolve.BindingContextUtils;
028    import org.jetbrains.jet.lang.resolve.calls.model.ResolvedCall;
029    import org.jetbrains.jet.lang.types.JetType;
030    import org.jetbrains.jet.lexer.JetTokens;
031    import org.jetbrains.k2js.translate.callTranslator.CallTranslator;
032    import org.jetbrains.k2js.translate.context.TranslationContext;
033    import org.jetbrains.k2js.translate.utils.TranslationUtils;
034    
035    import static org.jetbrains.k2js.translate.general.Translation.translateAsExpression;
036    import static org.jetbrains.k2js.translate.utils.BindingUtils.getFunctionResolvedCall;
037    import static org.jetbrains.k2js.translate.utils.PsiUtils.getBaseExpression;
038    import static org.jetbrains.k2js.translate.utils.PsiUtils.getOperationToken;
039    import static org.jetbrains.k2js.translate.utils.TranslationUtils.*;
040    
041    public final class UnaryOperationTranslator {
042        private UnaryOperationTranslator() {
043        }
044    
045        @NotNull
046        public static JsExpression translate(
047                @NotNull JetUnaryExpression expression,
048                @NotNull TranslationContext context
049        ) {
050            IElementType operationToken = expression.getOperationReference().getReferencedNameElementType();
051            if (operationToken == JetTokens.EXCLEXCL) {
052                JetExpression baseExpression = getBaseExpression(expression);
053                JetType type = BindingContextUtils.getNotNull(context.bindingContext(), BindingContext.EXPRESSION_TYPE, baseExpression);
054                JsExpression translatedExpression = translateAsExpression(baseExpression, context);
055                return type.isNullable() ? sure(translatedExpression, context) : translatedExpression;
056            }
057    
058            if (IncrementTranslator.isIncrement(operationToken)) {
059                return IncrementTranslator.translate(expression, context);
060            }
061    
062            JsExpression baseExpression = TranslationUtils.translateBaseExpression(context, expression);
063            if (isExclForBinaryEqualLikeExpr(expression, baseExpression)) {
064                return translateExclForBinaryEqualLikeExpr((JsBinaryOperation) baseExpression);
065            }
066    
067            ResolvedCall<? extends FunctionDescriptor> resolvedCall = getFunctionResolvedCall(context.bindingContext(), expression.getOperationReference());
068            return CallTranslator.instance$.translate(context, resolvedCall, baseExpression);
069        }
070    
071        private static boolean isExclForBinaryEqualLikeExpr(@NotNull JetUnaryExpression expression, @NotNull JsExpression baseExpression) {
072            if (getOperationToken(expression).equals(JetTokens.EXCL)) {
073                if (baseExpression instanceof JsBinaryOperation) {
074                    return isEqualLikeOperator(((JsBinaryOperation) baseExpression).getOperator());
075                }
076            }
077            return false;
078        }
079    }