Search in sources :

Example 21 with JsExpression

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

the class PatternTranslator method translateIsCheck.

@Nullable
public JsExpression translateIsCheck(@NotNull JsExpression subject, @NotNull KtTypeReference targetTypeReference) {
    KotlinType targetType = getTypeByReference(bindingContext(), targetTypeReference);
    JsExpression checkFunReference = doGetIsTypeCheckCallable(targetType);
    if (checkFunReference == null) {
        return null;
    }
    boolean isReifiedType = isReifiedTypeParameter(targetType);
    if (!isReifiedType && isNullableType(targetType) || isReifiedType && findChildByType(targetTypeReference, KtNodeTypes.NULLABLE_TYPE) != null) {
        checkFunReference = namer().orNull(checkFunReference);
    }
    return new JsInvocation(checkFunReference, subject);
}
Also used : JsExpression(org.jetbrains.kotlin.js.backend.ast.JsExpression) JsInvocation(org.jetbrains.kotlin.js.backend.ast.JsInvocation) KotlinType(org.jetbrains.kotlin.types.KotlinType) Nullable(org.jetbrains.annotations.Nullable)

Example 22 with JsExpression

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

the class InOperationTranslator method translateInt.

@NotNull
private JsExpression translateInt(@NotNull KtExpression lowerExpression, @NotNull KtExpression upperExpression) {
    JsExpression lower = Translation.translateAsExpression(lowerExpression, context());
    JsExpression upper = Translation.translateAsExpression(upperExpression, context());
    if (!negated) {
        JsExpression first = JsAstUtils.greaterThanEq(left, lower);
        JsExpression second = JsAstUtils.lessThanEq(left, upper);
        return JsAstUtils.and(first, second);
    } else {
        JsExpression first = JsAstUtils.lessThan(left, lower);
        JsExpression second = JsAstUtils.greaterThan(left, upper);
        return JsAstUtils.or(first, second);
    }
}
Also used : JsExpression(org.jetbrains.kotlin.js.backend.ast.JsExpression) NotNull(org.jetbrains.annotations.NotNull)

Example 23 with JsExpression

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

the class IncrementTranslator method asPrefix.

//TODO: decide if this expression can be optimised in case of direct access (not property)
@NotNull
private JsExpression asPrefix() {
    // code fragment: expr(a++)
    // generate: expr(a = a.inc(), a)
    JsExpression getExpression = accessTranslator.translateAsGet();
    JsExpression reassignment = variableReassignment(context().innerBlock(accessBlock), getExpression);
    accessBlock.getStatements().add(JsAstUtils.asSyntheticStatement(reassignment));
    JsExpression getNewValue = accessTranslator.translateAsGet();
    JsExpression result;
    if (accessBlock.getStatements().size() == 1) {
        result = new JsBinaryOperation(JsBinaryOperator.COMMA, reassignment, getNewValue);
    } else {
        context().getCurrentBlock().getStatements().addAll(accessBlock.getStatements());
        result = getNewValue;
    }
    MetadataProperties.setSynthetic(result, true);
    return result;
}
Also used : JsExpression(org.jetbrains.kotlin.js.backend.ast.JsExpression) JsBinaryOperation(org.jetbrains.kotlin.js.backend.ast.JsBinaryOperation) NotNull(org.jetbrains.annotations.NotNull)

Example 24 with JsExpression

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

Example 25 with JsExpression

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

the class IntrinsicAssignmentTranslator method translateAsAssignToCounterpart.

@NotNull
private JsExpression translateAsAssignToCounterpart() {
    JsBinaryOperator operator = getCounterpartOperator();
    JsExpression oldValue = accessTranslator.translateAsGet();
    if (!rightExpressionTrivial) {
        oldValue = context().defineTemporary(oldValue);
    }
    JsBinaryOperation counterpartOperation = new JsBinaryOperation(operator, oldValue, right);
    context().addStatementsToCurrentBlockFrom(rightBlock);
    return accessTranslator.translateAsSet(counterpartOperation);
}
Also used : JsExpression(org.jetbrains.kotlin.js.backend.ast.JsExpression) JsBinaryOperator(org.jetbrains.kotlin.js.backend.ast.JsBinaryOperator) JsBinaryOperation(org.jetbrains.kotlin.js.backend.ast.JsBinaryOperation) 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