Search in sources :

Example 1 with JsNameRef

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

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

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

the class ReferenceTranslator method getLazyReferenceToObject.

@NotNull
private static JsExpression getLazyReferenceToObject(@NotNull ClassDescriptor descriptor, @NotNull TranslationContext context) {
    DeclarationDescriptor container = descriptor.getContainingDeclaration();
    JsExpression qualifier = context.getInnerReference(container);
    return new JsNameRef(context.getNameForDescriptor(descriptor), qualifier);
}
Also used : JsExpression(org.jetbrains.kotlin.js.backend.ast.JsExpression) JsNameRef(org.jetbrains.kotlin.js.backend.ast.JsNameRef) NotNull(org.jetbrains.annotations.NotNull)

Example 4 with JsNameRef

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

the class InitializerVisitor method visitProperty.

@Override
public final Void visitProperty(@NotNull KtProperty property, @NotNull TranslationContext context) {
    PropertyDescriptor descriptor = BindingUtils.getPropertyDescriptor(context.bindingContext(), property);
    JsExpression value = PropertyTranslatorKt.translateDelegateOrInitializerExpression(context, property);
    JsStatement statement = null;
    KtExpression initializer = property.getInitializer();
    KtExpression delegate = property.getDelegateExpression();
    if (initializer != null) {
        assert value != null;
        statement = generateInitializerForProperty(context, descriptor, value);
    } else if (delegate != null) {
        assert value != null;
        statement = generateInitializerForDelegate(descriptor, value);
    } else if (Boolean.TRUE.equals(context.bindingContext().get(BindingContext.BACKING_FIELD_REQUIRED, descriptor))) {
        JsNameRef backingFieldReference = TranslationUtils.backingFieldReference(context, descriptor);
        JsExpression defaultValue = generateDefaultValue(descriptor, context, backingFieldReference);
        statement = TranslationUtils.assignmentToBackingField(context, descriptor, defaultValue).makeStmt();
    } else if (JsDescriptorUtils.isSimpleFinalProperty(descriptor)) {
        JsNameRef propRef = new JsNameRef(context.getNameForDescriptor(descriptor), JsLiteral.THIS);
        JsExpression defaultValue = generateDefaultValue(descriptor, context, propRef);
        statement = JsAstUtils.assignment(propRef, defaultValue).makeStmt();
    }
    if (statement != null && !JsAstUtils.isEmptyStatement(statement)) {
        context.addStatementsToCurrentBlock(JsAstUtils.flattenStatement(statement));
    }
    return null;
}
Also used : JsExpression(org.jetbrains.kotlin.js.backend.ast.JsExpression) PropertyDescriptor(org.jetbrains.kotlin.descriptors.PropertyDescriptor) JsStatement(org.jetbrains.kotlin.js.backend.ast.JsStatement) JsNameRef(org.jetbrains.kotlin.js.backend.ast.JsNameRef)

Example 5 with JsNameRef

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

Aggregations

JsNameRef (org.jetbrains.kotlin.js.backend.ast.JsNameRef)6 JsExpression (org.jetbrains.kotlin.js.backend.ast.JsExpression)5 NotNull (org.jetbrains.annotations.NotNull)4 JsInvocation (org.jetbrains.kotlin.js.backend.ast.JsInvocation)2 JsName (org.jetbrains.kotlin.js.backend.ast.JsName)2 ArrayList (java.util.ArrayList)1 FunctionDescriptor (org.jetbrains.kotlin.descriptors.FunctionDescriptor)1 PropertyDescriptor (org.jetbrains.kotlin.descriptors.PropertyDescriptor)1 VariableDescriptor (org.jetbrains.kotlin.descriptors.VariableDescriptor)1 JsStatement (org.jetbrains.kotlin.js.backend.ast.JsStatement)1 JsVars (org.jetbrains.kotlin.js.backend.ast.JsVars)1 KtDestructuringDeclarationEntry (org.jetbrains.kotlin.psi.KtDestructuringDeclarationEntry)1 KotlinType (org.jetbrains.kotlin.types.KotlinType)1