Search in sources :

Example 1 with JsInvocation

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

the class ReferenceTranslator method translateAsValueReference.

@NotNull
public static JsExpression translateAsValueReference(@NotNull DeclarationDescriptor descriptor, @NotNull TranslationContext context) {
    JsExpression alias = context.getAliasForDescriptor(descriptor);
    if (alias != null)
        return alias;
    if (shouldTranslateAsFQN(descriptor, context)) {
        return context.getQualifiedReference(descriptor);
    }
    if (descriptor instanceof PropertyDescriptor) {
        PropertyDescriptor property = (PropertyDescriptor) descriptor;
        if (context.isFromCurrentModule(property)) {
            return context.getInnerReference(property);
        } else {
            JsExpression qualifier = context.getInnerReference(property.getContainingDeclaration());
            JsName name = context.getNameForDescriptor(property);
            return new JsNameRef(name, qualifier);
        }
    }
    if (DescriptorUtils.isObject(descriptor) || DescriptorUtils.isEnumEntry(descriptor)) {
        if (!context.isFromCurrentModule(descriptor)) {
            return getLazyReferenceToObject((ClassDescriptor) descriptor, context);
        } else {
            JsExpression functionRef = JsAstUtils.pureFqn(context.getNameForObjectInstance((ClassDescriptor) descriptor), null);
            return new JsInvocation(functionRef);
        }
    }
    return context.getInnerReference(descriptor);
}
Also used : JsExpression(org.jetbrains.kotlin.js.backend.ast.JsExpression) JsInvocation(org.jetbrains.kotlin.js.backend.ast.JsInvocation) JsName(org.jetbrains.kotlin.js.backend.ast.JsName) JsNameRef(org.jetbrains.kotlin.js.backend.ast.JsNameRef) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with JsInvocation

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

the class KotlinFunctionIntrinsic method apply.

@NotNull
@Override
public JsExpression apply(@Nullable JsExpression receiver, @NotNull List<? extends JsExpression> arguments, @NotNull TranslationContext context) {
    JsExpression function = JsAstUtils.pureFqn(functionName, Namer.kotlinObject());
    if (additionalArguments.length > 0) {
        List<JsExpression> newArguments = new ArrayList<JsExpression>(arguments);
        for (JsExpression e : additionalArguments) {
            newArguments.add(e.deepCopy());
        }
        arguments = newArguments;
    }
    return new JsInvocation(function, receiver == null ? arguments : TranslationUtils.generateInvocationArguments(receiver, arguments));
}
Also used : JsExpression(org.jetbrains.kotlin.js.backend.ast.JsExpression) JsInvocation(org.jetbrains.kotlin.js.backend.ast.JsInvocation) ArrayList(java.util.ArrayList) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with JsInvocation

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

the class ReferenceTranslator method getPrototypeIfNecessary.

@NotNull
private static JsExpression getPrototypeIfNecessary(@NotNull ClassDescriptor descriptor, @NotNull JsExpression reference) {
    if (DescriptorUtils.isObject(descriptor) || DescriptorUtils.isEnumEntry(descriptor)) {
        JsNameRef getPrototypeRef = JsAstUtils.pureFqn("getPrototypeOf", JsAstUtils.pureFqn("Object", null));
        JsInvocation getPrototypeInvocation = new JsInvocation(getPrototypeRef, reference);
        MetadataProperties.setSideEffects(getPrototypeInvocation, SideEffectKind.PURE);
        reference = JsAstUtils.pureFqn("constructor", getPrototypeInvocation);
    }
    return reference;
}
Also used : JsInvocation(org.jetbrains.kotlin.js.backend.ast.JsInvocation) JsNameRef(org.jetbrains.kotlin.js.backend.ast.JsNameRef) NotNull(org.jetbrains.annotations.NotNull)

Example 4 with JsInvocation

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

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

Aggregations

JsInvocation (org.jetbrains.kotlin.js.backend.ast.JsInvocation)5 NotNull (org.jetbrains.annotations.NotNull)4 JsExpression (org.jetbrains.kotlin.js.backend.ast.JsExpression)4 JsNameRef (org.jetbrains.kotlin.js.backend.ast.JsNameRef)2 KotlinType (org.jetbrains.kotlin.types.KotlinType)2 ArrayList (java.util.ArrayList)1 Nullable (org.jetbrains.annotations.Nullable)1 JsConditional (org.jetbrains.kotlin.js.backend.ast.JsConditional)1 JsName (org.jetbrains.kotlin.js.backend.ast.JsName)1 TemporaryVariable (org.jetbrains.kotlin.js.translate.context.TemporaryVariable)1 KtExpression (org.jetbrains.kotlin.psi.KtExpression)1 KtTypeReference (org.jetbrains.kotlin.psi.KtTypeReference)1