Search in sources :

Example 1 with KtConstantExpression

use of org.jetbrains.kotlin.psi.KtConstantExpression in project kotlin by JetBrains.

the class UnaryOperationTranslator method translate.

@NotNull
public static JsExpression translate(@NotNull KtUnaryExpression expression, @NotNull TranslationContext context) {
    IElementType operationToken = expression.getOperationReference().getReferencedNameElementType();
    if (operationToken == KtTokens.EXCLEXCL) {
        KtExpression baseExpression = getBaseExpression(expression);
        JsExpression translatedExpression = translateAsExpression(baseExpression, context);
        return sure(translatedExpression, context);
    }
    if (operationToken == KtTokens.MINUS) {
        KtExpression baseExpression = getBaseExpression(expression);
        if (baseExpression instanceof KtConstantExpression) {
            CompileTimeConstant<?> compileTimeValue = ConstantExpressionEvaluator.getConstant(expression, context.bindingContext());
            assert compileTimeValue != null : message(expression, "Expression is not compile time value: " + expression.getText() + " ");
            Object value = getCompileTimeValue(context.bindingContext(), expression, compileTimeValue);
            if (value instanceof Long) {
                return JsAstUtils.newLong((Long) value, context);
            }
        }
    }
    if (IncrementTranslator.isIncrement(operationToken)) {
        return IncrementTranslator.translate(expression, context);
    }
    JsExpression baseExpression = TranslationUtils.translateBaseExpression(context, expression);
    if (isExclForBinaryEqualLikeExpr(expression, baseExpression)) {
        return translateExclForBinaryEqualLikeExpr((JsBinaryOperation) baseExpression);
    }
    ResolvedCall<? extends FunctionDescriptor> resolvedCall = CallUtilKt.getFunctionResolvedCallWithAssert(expression, context.bindingContext());
    return CallTranslator.translate(context, resolvedCall, baseExpression);
}
Also used : IElementType(com.intellij.psi.tree.IElementType) JsExpression(org.jetbrains.kotlin.js.backend.ast.JsExpression) KtExpression(org.jetbrains.kotlin.psi.KtExpression) KtConstantExpression(org.jetbrains.kotlin.psi.KtConstantExpression) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

IElementType (com.intellij.psi.tree.IElementType)1 NotNull (org.jetbrains.annotations.NotNull)1 JsExpression (org.jetbrains.kotlin.js.backend.ast.JsExpression)1 KtConstantExpression (org.jetbrains.kotlin.psi.KtConstantExpression)1 KtExpression (org.jetbrains.kotlin.psi.KtExpression)1