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