Search in sources :

Example 16 with JsExpression

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

the class CompareToTranslator method translate.

@NotNull
private JsExpression translate() {
    JsBinaryOperator correspondingOperator = OperatorTable.getBinaryOperator(getOperationToken(expression));
    JsExpression methodCall = BinaryOperationTranslator.translateAsOverloadedCall(expression, context());
    return new JsBinaryOperation(correspondingOperator, methodCall, context().program().getNumberLiteral(0));
}
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)

Example 17 with JsExpression

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

the class VariableAccessTranslator method translateAsGet.

@NotNull
@Override
public JsExpression translateAsGet() {
    JsExpression e = CallTranslator.INSTANCE.translateGet(context(), resolvedCall, receiver);
    CallableDescriptor original = resolvedCall.getResultingDescriptor().getOriginal();
    if (original instanceof PropertyDescriptor) {
        PropertyGetterDescriptor getter = ((PropertyDescriptor) original).getGetter();
        if (InlineUtil.isInline(getter)) {
            if (e instanceof JsNameRef) {
                // Get was translated as a name reference
                setInlineCallMetadata((JsNameRef) e, referenceExpression, getter);
            } else {
                setInlineCallMetadata(e, referenceExpression, getter, context());
            }
        }
    }
    return e;
}
Also used : JsExpression(org.jetbrains.kotlin.js.backend.ast.JsExpression) JsNameRef(org.jetbrains.kotlin.js.backend.ast.JsNameRef) NotNull(org.jetbrains.annotations.NotNull)

Example 18 with JsExpression

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

the class VariableAccessTranslator method translateAsSet.

@NotNull
@Override
public JsExpression translateAsSet(@NotNull JsExpression setTo) {
    JsExpression e = CallTranslator.INSTANCE.translateSet(context(), resolvedCall, setTo, receiver);
    CallableDescriptor original = resolvedCall.getResultingDescriptor().getOriginal();
    if (original instanceof PropertyDescriptor) {
        PropertySetterDescriptor setter = ((PropertyDescriptor) original).getSetter();
        if (InlineUtil.isInline(setter)) {
            if (e instanceof JsBinaryOperation && ((JsBinaryOperation) e).getOperator().isAssignment()) {
                // Set was translated as an assignment
                setInlineCallMetadata((JsNameRef) (((JsBinaryOperation) e).getArg1()), referenceExpression, setter);
            } else {
                setInlineCallMetadata(e, referenceExpression, setter, context());
            }
        }
    }
    return e;
}
Also used : JsExpression(org.jetbrains.kotlin.js.backend.ast.JsExpression) JsBinaryOperation(org.jetbrains.kotlin.js.backend.ast.JsBinaryOperation) NotNull(org.jetbrains.annotations.NotNull)

Example 19 with JsExpression

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

the class PatternTranslator method getIsTypeCheckCallableForReifiedType.

@NotNull
private JsExpression getIsTypeCheckCallableForReifiedType(@NotNull TypeParameterDescriptor typeParameter) {
    assert typeParameter.isReified() : "Expected reified type, actual: " + typeParameter;
    DeclarationDescriptor containingDeclaration = typeParameter.getContainingDeclaration();
    assert containingDeclaration instanceof CallableDescriptor : "Expected type parameter " + typeParameter + " to be contained in CallableDescriptor, actual: " + containingDeclaration.getClass();
    JsExpression alias = context().getAliasForDescriptor(typeParameter);
    assert alias != null : "No alias found for reified type parameter: " + typeParameter;
    return alias;
}
Also used : JsExpression(org.jetbrains.kotlin.js.backend.ast.JsExpression) NotNull(org.jetbrains.annotations.NotNull)

Example 20 with JsExpression

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

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