Search in sources :

Example 11 with KtExpression

use of org.jetbrains.kotlin.psi.KtExpression in project kotlin by JetBrains.

the class ClassInitializerTranslator method emulateSuperCallToNativeError.

public static void emulateSuperCallToNativeError(@NotNull TranslationContext context, @NotNull ClassDescriptor classDescriptor, @NotNull ResolvedCall<? extends FunctionDescriptor> superCall, @NotNull JsExpression receiver) {
    ClassDescriptor superClass = DescriptorUtilsKt.getSuperClassOrAny(classDescriptor);
    JsExpression superClassRef = ReferenceTranslator.translateAsTypeReference(superClass, context);
    JsExpression superInvocation = new JsInvocation(Namer.getFunctionCallRef(superClassRef), receiver.deepCopy());
    List<JsStatement> statements = context.getCurrentBlock().getStatements();
    statements.add(JsAstUtils.asSyntheticStatement(superInvocation));
    JsExpression messageArgument = Namer.getUndefinedExpression();
    JsExpression causeArgument = JsLiteral.NULL;
    for (ValueParameterDescriptor param : superCall.getResultingDescriptor().getValueParameters()) {
        ResolvedValueArgument argument = superCall.getValueArguments().get(param);
        if (!(argument instanceof ExpressionValueArgument))
            continue;
        ExpressionValueArgument exprArgument = (ExpressionValueArgument) argument;
        assert exprArgument.getValueArgument() != null;
        KtExpression value = exprArgument.getValueArgument().getArgumentExpression();
        assert value != null;
        JsExpression jsValue = Translation.translateAsExpression(value, context);
        if (KotlinBuiltIns.isStringOrNullableString(param.getType())) {
            messageArgument = context.cacheExpressionIfNeeded(jsValue);
        } else if (TypeUtilsKt.isConstructedFromClassWithGivenFqName(param.getType(), KotlinBuiltIns.FQ_NAMES.throwable)) {
            causeArgument = context.cacheExpressionIfNeeded(jsValue);
        } else {
            statements.add(JsAstUtils.asSyntheticStatement(jsValue));
        }
    }
    PropertyDescriptor messageProperty = DescriptorUtils.getPropertyByName(classDescriptor.getUnsubstitutedMemberScope(), Name.identifier("message"));
    JsExpression messageRef = pureFqn(context.getNameForBackingField(messageProperty), receiver.deepCopy());
    JsExpression messageIsUndefined = JsAstUtils.typeOfIs(messageArgument, context.program().getStringLiteral("undefined"));
    JsExpression causeIsNull = new JsBinaryOperation(JsBinaryOperator.NEQ, causeArgument, JsLiteral.NULL);
    JsExpression causeToStringCond = JsAstUtils.and(messageIsUndefined, causeIsNull);
    JsExpression causeToString = new JsInvocation(pureFqn("toString", Namer.kotlinObject()), causeArgument.deepCopy());
    JsExpression correctedMessage;
    if (causeArgument == JsLiteral.NULL) {
        correctedMessage = messageArgument.deepCopy();
    } else {
        if (JsAstUtils.isUndefinedExpression(messageArgument)) {
            causeToStringCond = causeIsNull;
        }
        correctedMessage = new JsConditional(causeToStringCond, causeToString, messageArgument);
    }
    statements.add(JsAstUtils.asSyntheticStatement(JsAstUtils.assignment(messageRef, correctedMessage)));
    PropertyDescriptor causeProperty = DescriptorUtils.getPropertyByName(classDescriptor.getUnsubstitutedMemberScope(), Name.identifier("cause"));
    JsExpression causeRef = pureFqn(context.getNameForBackingField(causeProperty), receiver.deepCopy());
    statements.add(JsAstUtils.asSyntheticStatement(JsAstUtils.assignment(causeRef, causeArgument.deepCopy())));
}
Also used : ResolvedValueArgument(org.jetbrains.kotlin.resolve.calls.model.ResolvedValueArgument) ExpressionValueArgument(org.jetbrains.kotlin.resolve.calls.model.ExpressionValueArgument) KtExpression(org.jetbrains.kotlin.psi.KtExpression)

Example 12 with KtExpression

use of org.jetbrains.kotlin.psi.KtExpression in project kotlin by JetBrains.

the class FunctionBodyTranslator method translate.

@NotNull
private JsBlock translate() {
    KtExpression jetBodyExpression = declaration.getBodyExpression();
    assert jetBodyExpression != null : "Cannot translate a body of an abstract function.";
    JsBlock jsBlock = new JsBlock();
    JsNode jsBody = Translation.translateExpression(jetBodyExpression, context(), jsBlock);
    jsBlock.getStatements().addAll(mayBeWrapWithReturn(jsBody).getStatements());
    return jsBlock;
}
Also used : KtExpression(org.jetbrains.kotlin.psi.KtExpression) NotNull(org.jetbrains.annotations.NotNull)

Example 13 with KtExpression

use of org.jetbrains.kotlin.psi.KtExpression in project kotlin by JetBrains.

the class KotlinTypeCheckerTest method assertType.

private void assertType(String expression, KotlinType expectedType) {
    Project project = getProject();
    KtExpression ktExpression = KtPsiFactoryKt.KtPsiFactory(project).createExpression(expression);
    KotlinType type = expressionTypingServices.getType(scopeWithImports, ktExpression, TypeUtils.NO_EXPECTED_TYPE, DataFlowInfoFactory.EMPTY, KotlinTestUtils.DUMMY_TRACE);
    assertTrue(type + " != " + expectedType, type.equals(expectedType));
}
Also used : Project(com.intellij.openapi.project.Project) KtExpression(org.jetbrains.kotlin.psi.KtExpression)

Example 14 with KtExpression

use of org.jetbrains.kotlin.psi.KtExpression in project kotlin by JetBrains.

the class KotlinTypeCheckerTest method assertType.

private void assertType(LexicalScope scope, String expression, String expectedTypeStr) {
    Project project = getProject();
    KtExpression ktExpression = KtPsiFactoryKt.KtPsiFactory(project).createExpression(expression);
    KotlinType type = expressionTypingServices.getType(scope, ktExpression, TypeUtils.NO_EXPECTED_TYPE, DataFlowInfoFactory.EMPTY, new BindingTraceContext());
    KotlinType expectedType = expectedTypeStr == null ? null : makeType(expectedTypeStr);
    assertEquals(expectedType, type);
}
Also used : Project(com.intellij.openapi.project.Project) KtExpression(org.jetbrains.kotlin.psi.KtExpression) BindingTraceContext(org.jetbrains.kotlin.resolve.BindingTraceContext)

Example 15 with KtExpression

use of org.jetbrains.kotlin.psi.KtExpression in project kotlin by JetBrains.

the class CheckerTestUtil method getDebugInfoDiagnostics.

@SuppressWarnings("TestOnlyProblems")
@NotNull
private static List<ActualDiagnostic> getDebugInfoDiagnostics(@NotNull PsiElement root, @NotNull BindingContext bindingContext, final boolean markDynamicCalls, @Nullable final List<DeclarationDescriptor> dynamicCallDescriptors, @Nullable final String platform) {
    final List<ActualDiagnostic> debugAnnotations = new ArrayList<ActualDiagnostic>();
    DebugInfoUtil.markDebugAnnotations(root, bindingContext, new DebugInfoUtil.DebugInfoReporter() {

        @Override
        public void reportElementWithErrorType(@NotNull KtReferenceExpression expression) {
            newDiagnostic(expression, DebugInfoDiagnosticFactory.ELEMENT_WITH_ERROR_TYPE);
        }

        @Override
        public void reportMissingUnresolved(@NotNull KtReferenceExpression expression) {
            newDiagnostic(expression, DebugInfoDiagnosticFactory.MISSING_UNRESOLVED);
        }

        @Override
        public void reportUnresolvedWithTarget(@NotNull KtReferenceExpression expression, @NotNull String target) {
            newDiagnostic(expression, DebugInfoDiagnosticFactory.UNRESOLVED_WITH_TARGET);
        }

        @Override
        public void reportDynamicCall(@NotNull KtElement element, DeclarationDescriptor declarationDescriptor) {
            if (dynamicCallDescriptors != null) {
                dynamicCallDescriptors.add(declarationDescriptor);
            }
            if (markDynamicCalls) {
                newDiagnostic(element, DebugInfoDiagnosticFactory.DYNAMIC);
            }
        }

        private void newDiagnostic(KtElement element, DebugInfoDiagnosticFactory factory) {
            debugAnnotations.add(new ActualDiagnostic(new DebugInfoDiagnostic(element, factory), platform));
        }
    });
    //noinspection unchecked
    for (Pair<? extends WritableSlice<? extends KtExpression, ?>, DebugInfoDiagnosticFactory> factory : Arrays.asList(TuplesKt.to(BindingContext.SMARTCAST, DebugInfoDiagnosticFactory.SMARTCAST), TuplesKt.to(BindingContext.IMPLICIT_RECEIVER_SMARTCAST, DebugInfoDiagnosticFactory.IMPLICIT_RECEIVER_SMARTCAST), TuplesKt.to(BindingContext.SMARTCAST_NULL, DebugInfoDiagnosticFactory.CONSTANT), TuplesKt.to(BindingContext.LEAKING_THIS, DebugInfoDiagnosticFactory.LEAKING_THIS), TuplesKt.to(BindingContext.IMPLICIT_EXHAUSTIVE_WHEN, DebugInfoDiagnosticFactory.IMPLICIT_EXHAUSTIVE))) {
        for (KtExpression expression : bindingContext.getSliceContents(factory.getFirst()).keySet()) {
            if (PsiTreeUtil.isAncestor(root, expression, false)) {
                debugAnnotations.add(new ActualDiagnostic(new DebugInfoDiagnostic(expression, factory.getSecond()), platform));
            }
        }
    }
    return debugAnnotations;
}
Also used : KtElement(org.jetbrains.kotlin.psi.KtElement) KtReferenceExpression(org.jetbrains.kotlin.psi.KtReferenceExpression) DeclarationDescriptor(org.jetbrains.kotlin.descriptors.DeclarationDescriptor) KtExpression(org.jetbrains.kotlin.psi.KtExpression) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

KtExpression (org.jetbrains.kotlin.psi.KtExpression)28 NotNull (org.jetbrains.annotations.NotNull)13 JsExpression (org.jetbrains.kotlin.js.backend.ast.JsExpression)6 KotlinType (org.jetbrains.kotlin.types.KotlinType)5 Nullable (org.jetbrains.annotations.Nullable)4 ValueArgument (org.jetbrains.kotlin.psi.ValueArgument)4 Project (com.intellij.openapi.project.Project)3 TextRange (com.intellij.openapi.util.TextRange)3 ValueParameterDescriptor (org.jetbrains.kotlin.descriptors.ValueParameterDescriptor)3 HashMap (java.util.HashMap)2 FunctionDescriptor (org.jetbrains.kotlin.descriptors.FunctionDescriptor)2 KtElement (org.jetbrains.kotlin.psi.KtElement)2 KtParenthesizedExpression (org.jetbrains.kotlin.psi.KtParenthesizedExpression)2 KtTypeReference (org.jetbrains.kotlin.psi.KtTypeReference)2 ExpressionValueArgument (org.jetbrains.kotlin.resolve.calls.model.ExpressionValueArgument)2 ResolvedValueArgument (org.jetbrains.kotlin.resolve.calls.model.ResolvedValueArgument)2 Type (org.jetbrains.org.objectweb.asm.Type)2 PsiCodeFragment (com.intellij.psi.PsiCodeFragment)1 IElementType (com.intellij.psi.tree.IElementType)1 ParameterTableModelItemBase (com.intellij.refactoring.changeSignature.ParameterTableModelItemBase)1