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())));
}
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;
}
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));
}
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);
}
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;
}
Aggregations