Search in sources :

Example 1 with KtDestructuringDeclarationEntry

use of org.jetbrains.kotlin.psi.KtDestructuringDeclarationEntry 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)

Aggregations

ArrayList (java.util.ArrayList)1 FunctionDescriptor (org.jetbrains.kotlin.descriptors.FunctionDescriptor)1 VariableDescriptor (org.jetbrains.kotlin.descriptors.VariableDescriptor)1 JsExpression (org.jetbrains.kotlin.js.backend.ast.JsExpression)1 JsName (org.jetbrains.kotlin.js.backend.ast.JsName)1 JsNameRef (org.jetbrains.kotlin.js.backend.ast.JsNameRef)1 JsVars (org.jetbrains.kotlin.js.backend.ast.JsVars)1 KtDestructuringDeclarationEntry (org.jetbrains.kotlin.psi.KtDestructuringDeclarationEntry)1 KotlinType (org.jetbrains.kotlin.types.KotlinType)1