Search in sources :

Example 6 with JsExpression

use of org.jetbrains.kotlin.js.backend.ast.JsExpression in project kotlin by JetBrains.

the class IntrinsicAssignmentTranslator method translateRightExpression.

private JsExpression translateRightExpression(TranslationContext context, KtBinaryExpression expression) {
    JsExpression result = TranslationUtils.translateRightExpression(context, expression, rightBlock);
    KotlinType leftType = context.bindingContext().getType(expression.getLeft());
    KotlinType rightType = context.bindingContext().getType(expression.getRight());
    if (rightType != null && KotlinBuiltIns.isCharOrNullableChar(rightType)) {
        if (leftType != null && KotlinBuiltIns.isStringOrNullableString(leftType)) {
            result = JsAstUtils.charToString(result);
        } else if (leftType == null || !KotlinBuiltIns.isCharOrNullableChar(leftType)) {
            result = JsAstUtils.charToBoxedChar(result);
        }
    }
    return result;
}
Also used : JsExpression(org.jetbrains.kotlin.js.backend.ast.JsExpression) KotlinType(org.jetbrains.kotlin.types.KotlinType)

Example 7 with JsExpression

use of org.jetbrains.kotlin.js.backend.ast.JsExpression in project kotlin by JetBrains.

the class OverloadedAssignmentTranslator method overloadedMethodInvocation.

@NotNull
private JsExpression overloadedMethodInvocation(AccessTranslator accessTranslator) {
    JsBlock innerBlock = new JsBlock();
    TranslationContext innerContext = context().innerBlock(innerBlock);
    JsExpression oldValue = accessTranslator.translateAsGet();
    JsBlock argumentBlock = new JsBlock();
    TranslationContext argumentContext = innerContext.innerBlock(argumentBlock);
    KtExpression argumentPsi = expression.getRight();
    assert argumentPsi != null;
    JsExpression argument = Translation.translateAsExpression(argumentPsi, argumentContext);
    if (!argumentBlock.isEmpty()) {
        oldValue = innerContext.defineTemporary(oldValue);
        innerContext.addStatementsToCurrentBlockFrom(argumentBlock);
    }
    Map<KtExpression, JsExpression> aliases = new HashMap<KtExpression, JsExpression>();
    aliases.put(argumentPsi, argument);
    innerContext = innerContext.innerContextWithAliasesForExpressions(aliases);
    JsExpression result = CallTranslator.translate(innerContext, resolvedCall, oldValue);
    context().addStatementsToCurrentBlockFrom(innerBlock);
    return result;
}
Also used : JsExpression(org.jetbrains.kotlin.js.backend.ast.JsExpression) HashMap(java.util.HashMap) KtExpression(org.jetbrains.kotlin.psi.KtExpression) JsBlock(org.jetbrains.kotlin.js.backend.ast.JsBlock) TranslationContext(org.jetbrains.kotlin.js.translate.context.TranslationContext) NotNull(org.jetbrains.annotations.NotNull)

Example 8 with JsExpression

use of org.jetbrains.kotlin.js.backend.ast.JsExpression 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)

Example 9 with JsExpression

use of org.jetbrains.kotlin.js.backend.ast.JsExpression in project kotlin by JetBrains.

the class ArrayAccessTranslator method getCached.

@NotNull
@Override
public AccessTranslator getCached() {
    Map<KtExpression, JsExpression> aliases = new HashMap<KtExpression, JsExpression>();
    JsExpression arrayExpression = context().cacheExpressionIfNeeded(getArrayExpression());
    aliases.put(expression.getArrayExpression(), arrayExpression);
    for (KtExpression ktExpression : expression.getIndexExpressions()) {
        JsExpression jsExpression = context().cacheExpressionIfNeeded(Translation.translateAsExpression(ktExpression, context()));
        aliases.put(ktExpression, jsExpression);
    }
    return new CachedArrayAccessTranslator(expression, context().innerContextWithAliasesForExpressions(aliases), arrayExpression);
}
Also used : JsExpression(org.jetbrains.kotlin.js.backend.ast.JsExpression) HashMap(java.util.HashMap) KtExpression(org.jetbrains.kotlin.psi.KtExpression) NotNull(org.jetbrains.annotations.NotNull)

Example 10 with JsExpression

use of org.jetbrains.kotlin.js.backend.ast.JsExpression in project kotlin by JetBrains.

the class QualifiedExpressionTranslator method translateQualifiedExpression.

@NotNull
public static JsNode translateQualifiedExpression(@NotNull KtQualifiedExpression expression, @NotNull TranslationContext context) {
    ResolvedCall<?> call = CallUtilKt.getResolvedCall(expression, context.bindingContext());
    JsExpression receiver = null;
    if (call != null) {
        receiver = translateReceiver(expression, context);
    }
    KtExpression selector = getSelector(expression);
    return dispatchToCorrectTranslator(receiver, selector, context);
}
Also used : JsExpression(org.jetbrains.kotlin.js.backend.ast.JsExpression) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

JsExpression (org.jetbrains.kotlin.js.backend.ast.JsExpression)27 NotNull (org.jetbrains.annotations.NotNull)21 KotlinType (org.jetbrains.kotlin.types.KotlinType)7 KtExpression (org.jetbrains.kotlin.psi.KtExpression)6 JsNameRef (org.jetbrains.kotlin.js.backend.ast.JsNameRef)5 JsBinaryOperation (org.jetbrains.kotlin.js.backend.ast.JsBinaryOperation)4 JsInvocation (org.jetbrains.kotlin.js.backend.ast.JsInvocation)4 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Nullable (org.jetbrains.annotations.Nullable)2 JsBinaryOperator (org.jetbrains.kotlin.js.backend.ast.JsBinaryOperator)2 JsName (org.jetbrains.kotlin.js.backend.ast.JsName)2 TemporaryVariable (org.jetbrains.kotlin.js.translate.context.TemporaryVariable)2 TranslationContext (org.jetbrains.kotlin.js.translate.context.TranslationContext)2 KtTypeReference (org.jetbrains.kotlin.psi.KtTypeReference)2 IElementType (com.intellij.psi.tree.IElementType)1 LinkedHashMap (java.util.LinkedHashMap)1 FunctionDescriptor (org.jetbrains.kotlin.descriptors.FunctionDescriptor)1 PropertyDescriptor (org.jetbrains.kotlin.descriptors.PropertyDescriptor)1 VariableDescriptor (org.jetbrains.kotlin.descriptors.VariableDescriptor)1