Search in sources :

Example 11 with JsExpression

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

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

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

the class JSTestGenerator method generateCodeForTestMethod.

private static void generateCodeForTestMethod(@NotNull TranslationContext context, @NotNull FunctionDescriptor functionDescriptor, @NotNull ClassDescriptor classDescriptor, @NotNull JSTester tester) {
    JsExpression expression = ReferenceTranslator.translateAsValueReference(classDescriptor, context);
    JsNew testClass = new JsNew(expression);
    JsExpression functionToTestCall = CallTranslator.INSTANCE.buildCall(context, functionDescriptor, Collections.<JsExpression>emptyList(), testClass);
    JsStringLiteral testName = context.program().getStringLiteral(classDescriptor.getName() + "." + functionDescriptor.getName());
    tester.constructTestMethodInvocation(functionToTestCall, testName);
}
Also used : JsExpression(org.jetbrains.kotlin.js.backend.ast.JsExpression) JsStringLiteral(org.jetbrains.kotlin.js.backend.ast.JsStringLiteral) JsNew(org.jetbrains.kotlin.js.backend.ast.JsNew)

Example 14 with JsExpression

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

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

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