Search in sources :

Example 1 with JsStatement

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

Aggregations

PropertyDescriptor (org.jetbrains.kotlin.descriptors.PropertyDescriptor)1 JsExpression (org.jetbrains.kotlin.js.backend.ast.JsExpression)1 JsNameRef (org.jetbrains.kotlin.js.backend.ast.JsNameRef)1 JsStatement (org.jetbrains.kotlin.js.backend.ast.JsStatement)1