Search in sources :

Example 1 with TemporaryVariable

use of org.jetbrains.kotlin.js.translate.context.TemporaryVariable in project kotlin by JetBrains.

the class Translation method translateAsExpression.

@NotNull
public static JsExpression translateAsExpression(@NotNull KtExpression expression, @NotNull TranslationContext context, @NotNull JsBlock block) {
    JsNode jsNode = translateExpression(expression, context, block);
    if (jsNode instanceof JsExpression) {
        KotlinType expressionType = context.bindingContext().getType(expression);
        return unboxIfNeeded((JsExpression) jsNode, expressionType != null && KotlinBuiltIns.isCharOrNullableChar(expressionType));
    }
    assert jsNode instanceof JsStatement : "Unexpected node of type: " + jsNode.getClass().toString();
    if (BindingContextUtilsKt.isUsedAsExpression(expression, context.bindingContext())) {
        TemporaryVariable result = context.declareTemporary(null);
        AssignToExpressionMutator saveResultToTemporaryMutator = new AssignToExpressionMutator(result.reference());
        block.getStatements().add(mutateLastExpression(jsNode, saveResultToTemporaryMutator));
        return result.reference();
    }
    block.getStatements().add(convertToStatement(jsNode));
    return JsLiteral.NULL;
}
Also used : TemporaryVariable(org.jetbrains.kotlin.js.translate.context.TemporaryVariable) AssignToExpressionMutator(org.jetbrains.kotlin.js.translate.utils.mutator.AssignToExpressionMutator) KotlinType(org.jetbrains.kotlin.types.KotlinType) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with TemporaryVariable

use of org.jetbrains.kotlin.js.translate.context.TemporaryVariable 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)

Example 3 with TemporaryVariable

use of org.jetbrains.kotlin.js.translate.context.TemporaryVariable in project kotlin by JetBrains.

the class IncrementTranslator method asPostfix.

//TODO: decide if this expression can be optimised in case of direct access (not property)
@NotNull
private JsExpression asPostfix() {
    // code fragment: expr(a++)
    // generate: expr( (t1 = a, t2 = t1, a = t1.inc(), t2) )
    TemporaryVariable t1 = context().declareTemporary(accessTranslator.translateAsGet());
    accessBlock.getStatements().add(t1.assignmentStatement());
    JsExpression variableReassignment = variableReassignment(context().innerBlock(accessBlock), t1.reference());
    accessBlock.getStatements().add(JsAstUtils.asSyntheticStatement(variableReassignment));
    JsExpression result;
    if (accessBlock.getStatements().size() == 2) {
        result = AstUtil.newSequence(t1.assignmentExpression(), variableReassignment, t1.reference());
    } else {
        context().getCurrentBlock().getStatements().addAll(accessBlock.getStatements());
        result = t1.reference();
    }
    MetadataProperties.setSynthetic(result, true);
    return result;
}
Also used : TemporaryVariable(org.jetbrains.kotlin.js.translate.context.TemporaryVariable) JsExpression(org.jetbrains.kotlin.js.backend.ast.JsExpression) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

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