Search in sources :

Example 1 with JsExpression

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

the class DestructuringDeclarationTranslator method translate.

private JsVars translate() {
    if (initializer != null) {
        context().getCurrentBlock().getStatements().add(JsAstUtils.newVar(multiObjectName, initializer));
    }
    List<JsVars.JsVar> jsVars = new ArrayList<JsVars.JsVar>();
    JsNameRef multiObjNameRef = multiObjectName.makeRef();
    for (KtDestructuringDeclarationEntry entry : multiDeclaration.getEntries()) {
        VariableDescriptor descriptor = BindingContextUtils.getNotNull(context().bindingContext(), BindingContext.VARIABLE, entry);
        // Do not call `componentX` for destructuring entry called _
        if (descriptor.getName().isSpecial())
            continue;
        ResolvedCall<FunctionDescriptor> entryInitCall = context().bindingContext().get(BindingContext.COMPONENT_RESOLVED_CALL, entry);
        assert entryInitCall != null : "Entry init call must be not null";
        JsExpression entryInitializer = CallTranslator.translate(context(), entryInitCall, multiObjNameRef);
        FunctionDescriptor candidateDescriptor = entryInitCall.getCandidateDescriptor();
        if (CallExpressionTranslator.shouldBeInlined(candidateDescriptor, context())) {
            setInlineCallMetadata(entryInitializer, entry, entryInitCall, context());
        }
        KotlinType returnType = candidateDescriptor.getReturnType();
        if (returnType != null && KotlinBuiltIns.isCharOrNullableChar(returnType) && !KotlinBuiltIns.isCharOrNullableChar(descriptor.getType())) {
            entryInitializer = JsAstUtils.charToBoxedChar(entryInitializer);
        }
        JsName name = context().getNameForDescriptor(descriptor);
        if (isVarCapturedInClosure(context().bindingContext(), descriptor)) {
            JsNameRef alias = getCapturedVarAccessor(name.makeRef());
            entryInitializer = JsAstUtils.wrapValue(alias, entryInitializer);
        }
        jsVars.add(new JsVars.JsVar(name, entryInitializer));
    }
    return new JsVars(jsVars, true);
}
Also used : JsExpression(org.jetbrains.kotlin.js.backend.ast.JsExpression) KotlinType(org.jetbrains.kotlin.types.KotlinType) ArrayList(java.util.ArrayList) FunctionDescriptor(org.jetbrains.kotlin.descriptors.FunctionDescriptor) VariableDescriptor(org.jetbrains.kotlin.descriptors.VariableDescriptor) JsName(org.jetbrains.kotlin.js.backend.ast.JsName) JsNameRef(org.jetbrains.kotlin.js.backend.ast.JsNameRef) JsVars(org.jetbrains.kotlin.js.backend.ast.JsVars) KtDestructuringDeclarationEntry(org.jetbrains.kotlin.psi.KtDestructuringDeclarationEntry)

Example 2 with JsExpression

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

the class PatternTranslator method translateExpressionPattern.

@NotNull
public JsExpression translateExpressionPattern(@NotNull KotlinType type, @NotNull JsExpression expressionToMatch, @NotNull KtExpression patternExpression) {
    JsExpression expressionToMatchAgainst = translateExpressionForExpressionPattern(patternExpression);
    KotlinType patternType = BindingUtils.getTypeForExpression(bindingContext(), patternExpression);
    EqualityType matchEquality = equalityType(type);
    EqualityType patternEquality = equalityType(patternType);
    if (matchEquality == EqualityType.PRIMITIVE && patternEquality == EqualityType.PRIMITIVE) {
        return equality(expressionToMatch, expressionToMatchAgainst);
    } else if (expressionToMatchAgainst == JsLiteral.NULL) {
        return TranslationUtils.nullCheck(expressionToMatch, false);
    } else {
        return TopLevelFIF.KOTLIN_EQUALS.apply(expressionToMatch, Collections.singletonList(expressionToMatchAgainst), context());
    }
}
Also used : JsExpression(org.jetbrains.kotlin.js.backend.ast.JsExpression) KotlinType(org.jetbrains.kotlin.types.KotlinType) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with JsExpression

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

the class PatternTranslator method translateIsExpression.

@NotNull
public JsExpression translateIsExpression(@NotNull KtIsExpression expression) {
    KtExpression left = expression.getLeftHandSide();
    JsExpression expressionToCheck = Translation.translateAsExpression(left, context());
    KotlinType leftType = context().bindingContext().getType(left);
    if (leftType != null && KotlinBuiltIns.isChar(leftType)) {
        expressionToCheck = JsAstUtils.charToBoxedChar(expressionToCheck);
    }
    KtTypeReference typeReference = expression.getTypeReference();
    assert typeReference != null;
    JsExpression result = translateIsCheck(expressionToCheck, typeReference);
    if (result == null)
        return JsLiteral.getBoolean(!expression.isNegated());
    if (expression.isNegated()) {
        return not(result);
    }
    return result;
}
Also used : JsExpression(org.jetbrains.kotlin.js.backend.ast.JsExpression) KotlinType(org.jetbrains.kotlin.types.KotlinType) KtExpression(org.jetbrains.kotlin.psi.KtExpression) KtTypeReference(org.jetbrains.kotlin.psi.KtTypeReference) NotNull(org.jetbrains.annotations.NotNull)

Example 4 with JsExpression

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

the class PatternTranslator method doGetIsTypeCheckCallable.

@Nullable
private JsExpression doGetIsTypeCheckCallable(@NotNull KotlinType type) {
    ClassifierDescriptor targetDescriptor = type.getConstructor().getDeclarationDescriptor();
    if (targetDescriptor != null && AnnotationsUtils.isNativeInterface(targetDescriptor)) {
        return type.isMarkedNullable() ? null : namer().isInstanceOf(JsAstUtils.pureFqn("Object", null));
    }
    JsExpression builtinCheck = getIsTypeCheckCallableForBuiltin(type);
    if (builtinCheck != null)
        return builtinCheck;
    builtinCheck = getIsTypeCheckCallableForPrimitiveBuiltin(type);
    if (builtinCheck != null)
        return builtinCheck;
    TypeParameterDescriptor typeParameterDescriptor = getTypeParameterDescriptorOrNull(type);
    if (typeParameterDescriptor != null) {
        if (typeParameterDescriptor.isReified()) {
            return getIsTypeCheckCallableForReifiedType(typeParameterDescriptor);
        }
        JsExpression result = null;
        for (KotlinType upperBound : typeParameterDescriptor.getUpperBounds()) {
            JsExpression next = doGetIsTypeCheckCallable(upperBound);
            if (next != null) {
                result = result != null ? namer().andPredicate(result, next) : next;
            }
        }
        return result;
    }
    ClassDescriptor referencedClass = DescriptorUtils.getClassDescriptorForType(type);
    JsExpression typeName = ReferenceTranslator.translateAsTypeReference(referencedClass, context());
    return namer().isInstanceOf(typeName);
}
Also used : JsExpression(org.jetbrains.kotlin.js.backend.ast.JsExpression) KotlinType(org.jetbrains.kotlin.types.KotlinType) Nullable(org.jetbrains.annotations.Nullable)

Example 5 with JsExpression

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

the class InOperationTranslator method translate.

@NotNull
public JsExpression translate() {
    ResolvedCall<? extends FunctionDescriptor> call = CallUtilKt.getFunctionResolvedCallWithAssert(operation, bindingContext());
    if (INT_SPECIALIZATION_TEST.apply(call.getResultingDescriptor())) {
        JsExpression candidate = translateInt();
        if (candidate != null) {
            return candidate;
        }
    }
    JsExpression rightTranslated = Translation.translateAsExpression(right, context());
    return translateGeneral(call, rightTranslated);
}
Also used : JsExpression(org.jetbrains.kotlin.js.backend.ast.JsExpression) 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