Search in sources :

Example 1 with JsConditional

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

the class PatternTranslator method translateCastExpression.

@NotNull
public JsExpression translateCastExpression(@NotNull KtBinaryExpressionWithTypeRHS expression) {
    assert isCastExpression(expression) : "Expected cast expression, got " + expression;
    KtExpression left = expression.getLeft();
    JsExpression expressionToCast = Translation.translateAsExpression(left, context());
    KtTypeReference typeReference = expression.getRight();
    assert typeReference != null : "Cast expression must have type reference";
    KotlinType leftType = context().bindingContext().getType(left);
    if (leftType != null && KotlinBuiltIns.isChar(leftType)) {
        expressionToCast = JsAstUtils.charToBoxedChar(expressionToCast);
    }
    TemporaryVariable temporary = context().declareTemporary(expressionToCast);
    JsExpression isCheck = translateIsCheck(temporary.assignmentExpression(), typeReference);
    if (isCheck == null)
        return expressionToCast;
    JsExpression onFail;
    if (isSafeCast(expression)) {
        onFail = JsLiteral.NULL;
    } else {
        JsExpression throwCCEFunRef = Namer.throwClassCastExceptionFunRef();
        onFail = new JsInvocation(throwCCEFunRef);
    }
    JsExpression result = new JsConditional(isCheck, temporary.reference(), onFail);
    KotlinType expressionType = context().bindingContext().getType(expression);
    if (expressionType != null && KotlinBuiltIns.isCharOrNullableChar(expressionType)) {
        result = JsAstUtils.boxedCharToChar(result);
    }
    return result;
}
Also used : TemporaryVariable(org.jetbrains.kotlin.js.translate.context.TemporaryVariable) JsExpression(org.jetbrains.kotlin.js.backend.ast.JsExpression) JsInvocation(org.jetbrains.kotlin.js.backend.ast.JsInvocation) JsConditional(org.jetbrains.kotlin.js.backend.ast.JsConditional) KotlinType(org.jetbrains.kotlin.types.KotlinType) KtExpression(org.jetbrains.kotlin.psi.KtExpression) KtTypeReference(org.jetbrains.kotlin.psi.KtTypeReference) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

NotNull (org.jetbrains.annotations.NotNull)1 JsConditional (org.jetbrains.kotlin.js.backend.ast.JsConditional)1 JsExpression (org.jetbrains.kotlin.js.backend.ast.JsExpression)1 JsInvocation (org.jetbrains.kotlin.js.backend.ast.JsInvocation)1 TemporaryVariable (org.jetbrains.kotlin.js.translate.context.TemporaryVariable)1 KtExpression (org.jetbrains.kotlin.psi.KtExpression)1 KtTypeReference (org.jetbrains.kotlin.psi.KtTypeReference)1 KotlinType (org.jetbrains.kotlin.types.KotlinType)1