001    /*
002     * Copyright 2010-2015 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.kotlin.js.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.kotlin.descriptors.FunctionDescriptor;
024    import org.jetbrains.kotlin.js.translate.callTranslator.CallTranslator;
025    import org.jetbrains.kotlin.js.translate.context.TranslationContext;
026    import org.jetbrains.kotlin.js.translate.utils.JsAstUtils;
027    import org.jetbrains.kotlin.js.translate.utils.TranslationUtils;
028    import org.jetbrains.kotlin.lexer.JetTokens;
029    import org.jetbrains.kotlin.psi.JetConstantExpression;
030    import org.jetbrains.kotlin.psi.JetExpression;
031    import org.jetbrains.kotlin.psi.JetUnaryExpression;
032    import org.jetbrains.kotlin.resolve.BindingContextUtils;
033    import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall;
034    import org.jetbrains.kotlin.resolve.constants.CompileTimeConstant;
035    import org.jetbrains.kotlin.resolve.constants.evaluate.ConstantExpressionEvaluator;
036    import org.jetbrains.kotlin.types.JetType;
037    
038    import static org.jetbrains.kotlin.js.translate.general.Translation.translateAsExpression;
039    import static org.jetbrains.kotlin.js.translate.utils.BindingUtils.getCompileTimeValue;
040    import static org.jetbrains.kotlin.js.translate.utils.ErrorReportingUtils.message;
041    import static org.jetbrains.kotlin.js.translate.utils.PsiUtils.getBaseExpression;
042    import static org.jetbrains.kotlin.js.translate.utils.PsiUtils.getOperationToken;
043    import static org.jetbrains.kotlin.js.translate.utils.TranslationUtils.*;
044    import static org.jetbrains.kotlin.resolve.calls.callUtil.CallUtilPackage.getFunctionResolvedCallWithAssert;
045    
046    public final class UnaryOperationTranslator {
047        private UnaryOperationTranslator() {
048        }
049    
050        @NotNull
051        public static JsExpression translate(
052                @NotNull JetUnaryExpression expression,
053                @NotNull TranslationContext context
054        ) {
055            IElementType operationToken = expression.getOperationReference().getReferencedNameElementType();
056            if (operationToken == JetTokens.EXCLEXCL) {
057                JetExpression baseExpression = getBaseExpression(expression);
058                JetType type = BindingContextUtils.getTypeNotNull(context.bindingContext(), baseExpression);
059                JsExpression translatedExpression = translateAsExpression(baseExpression, context);
060                return type.isMarkedNullable() ? sure(translatedExpression, context) : translatedExpression;
061            }
062    
063            if (operationToken == JetTokens.MINUS) {
064                JetExpression baseExpression = getBaseExpression(expression);
065                if (baseExpression instanceof JetConstantExpression) {
066                    CompileTimeConstant<?> compileTimeValue = ConstantExpressionEvaluator.getConstant(expression, context.bindingContext());
067                    assert compileTimeValue != null : message(expression, "Expression is not compile time value: " + expression.getText() + " ");
068                    Object value = getCompileTimeValue(context.bindingContext(), expression, compileTimeValue);
069                    if (value instanceof Long) {
070                        return JsAstUtils.newLong((Long) value, context);
071                    }
072                }
073            }
074    
075            if (IncrementTranslator.isIncrement(operationToken)) {
076                return IncrementTranslator.translate(expression, context);
077            }
078    
079            JsExpression baseExpression = TranslationUtils.translateBaseExpression(context, expression);
080            if (isExclForBinaryEqualLikeExpr(expression, baseExpression)) {
081                return translateExclForBinaryEqualLikeExpr((JsBinaryOperation) baseExpression);
082            }
083    
084            ResolvedCall<? extends FunctionDescriptor> resolvedCall = getFunctionResolvedCallWithAssert(expression, context.bindingContext());
085            return CallTranslator.translate(context, resolvedCall, baseExpression);
086        }
087    
088        private static boolean isExclForBinaryEqualLikeExpr(@NotNull JetUnaryExpression expression, @NotNull JsExpression baseExpression) {
089            if (getOperationToken(expression).equals(JetTokens.EXCL)) {
090                if (baseExpression instanceof JsBinaryOperation) {
091                    return isEqualLikeOperator(((JsBinaryOperation) baseExpression).getOperator());
092                }
093            }
094            return false;
095        }
096    }