Search in sources :

Example 76 with KotlinType

use of org.jetbrains.kotlin.types.KotlinType in project kotlin by JetBrains.

the class WhenTranslator method isExhaustive.

private boolean isExhaustive() {
    KotlinType type = bindingContext().getType(whenExpression);
    boolean isStatement = type != null && KotlinBuiltIns.isUnit(type) && !type.isMarkedNullable();
    return CodegenUtil.isExhaustive(bindingContext(), whenExpression, isStatement);
}
Also used : KotlinType(org.jetbrains.kotlin.types.KotlinType)

Example 77 with KotlinType

use of org.jetbrains.kotlin.types.KotlinType in project kotlin by JetBrains.

the class WhenTranslator method translateExpressionCondition.

@NotNull
private JsExpression translateExpressionCondition(@NotNull KtWhenConditionWithExpression condition, @NotNull TranslationContext context) {
    KtExpression patternExpression = condition.getExpression();
    assert patternExpression != null : "Expression pattern should have an expression.";
    JsExpression expressionToMatch = getExpressionToMatch();
    if (expressionToMatch == null) {
        return Translation.patternTranslator(context).translateExpressionForExpressionPattern(patternExpression);
    } else {
        KtExpression subject = whenExpression.getSubjectExpression();
        assert subject != null : "Subject must be non-null since expressionToMatch is non-null: " + PsiUtilsKt.getTextWithLocation(condition);
        KotlinType type = BindingUtils.getTypeForExpression(bindingContext(), whenExpression.getSubjectExpression());
        return Translation.patternTranslator(context).translateExpressionPattern(type, expressionToMatch, patternExpression);
    }
}
Also used : KotlinType(org.jetbrains.kotlin.types.KotlinType) NotNull(org.jetbrains.annotations.NotNull)

Example 78 with KotlinType

use of org.jetbrains.kotlin.types.KotlinType in project kotlin by JetBrains.

the class JetTestFunctionDetector method isTest.

private static boolean isTest(@NotNull FunctionDescriptor functionDescriptor) {
    Annotations annotations = functionDescriptor.getAnnotations();
    for (AnnotationDescriptor annotation : annotations) {
        // TODO ideally we should find the fully qualified name here...
        KotlinType type = annotation.getType();
        String name = type.toString();
        if (name.equals("Test")) {
            return true;
        }
    }
    /*
        if (function.getName().startsWith("test")) {
            List<JetParameter> parameters = function.getValueParameters();
            return parameters.size() == 0;
        }
        */
    return false;
}
Also used : AnnotationDescriptor(org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptor) Annotations(org.jetbrains.kotlin.descriptors.annotations.Annotations) KotlinType(org.jetbrains.kotlin.types.KotlinType)

Example 79 with KotlinType

use of org.jetbrains.kotlin.types.KotlinType in project kotlin by JetBrains.

the class Translation method translateExpression.

@NotNull
public static JsNode translateExpression(@NotNull KtExpression expression, @NotNull TranslationContext context, @NotNull JsBlock block) {
    JsExpression aliasForExpression = context.aliasingContext().getAliasForExpression(expression);
    if (aliasForExpression != null) {
        return aliasForExpression;
    }
    CompileTimeConstant<?> compileTimeValue = ConstantExpressionEvaluator.getConstant(expression, context.bindingContext());
    if (compileTimeValue != null) {
        KotlinType type = context.bindingContext().getType(expression);
        if (type != null) {
            if (KotlinBuiltIns.isLong(type) || (KotlinBuiltIns.isInt(type) && expression instanceof KtUnaryExpression)) {
                JsExpression constantResult = translateConstant(compileTimeValue, expression, context);
                if (constantResult != null)
                    return constantResult;
            }
        }
    }
    TranslationContext innerContext = context.innerBlock();
    JsNode result = doTranslateExpression(expression, innerContext);
    context.moveVarsFrom(innerContext);
    block.getStatements().addAll(innerContext.dynamicContext().jsBlock().getStatements());
    return result;
}
Also used : KtUnaryExpression(org.jetbrains.kotlin.psi.KtUnaryExpression) KotlinType(org.jetbrains.kotlin.types.KotlinType) TranslationContext(org.jetbrains.kotlin.js.translate.context.TranslationContext) NotNull(org.jetbrains.annotations.NotNull)

Example 80 with KotlinType

use of org.jetbrains.kotlin.types.KotlinType in project kotlin by JetBrains.

the class Translation method translateAsExpression.

@NotNull
public static JsExpression translateAsExpression(@NotNull KtExpression expression, @NotNull TranslationContext context, @NotNull JsBlock block) {
    JsNode jsNode = translateExpression(expression, context, block);
    if (jsNode instanceof JsExpression) {
        KotlinType expressionType = context.bindingContext().getType(expression);
        return unboxIfNeeded((JsExpression) jsNode, expressionType != null && KotlinBuiltIns.isCharOrNullableChar(expressionType));
    }
    assert jsNode instanceof JsStatement : "Unexpected node of type: " + jsNode.getClass().toString();
    if (BindingContextUtilsKt.isUsedAsExpression(expression, context.bindingContext())) {
        TemporaryVariable result = context.declareTemporary(null);
        AssignToExpressionMutator saveResultToTemporaryMutator = new AssignToExpressionMutator(result.reference());
        block.getStatements().add(mutateLastExpression(jsNode, saveResultToTemporaryMutator));
        return result.reference();
    }
    block.getStatements().add(convertToStatement(jsNode));
    return JsLiteral.NULL;
}
Also used : TemporaryVariable(org.jetbrains.kotlin.js.translate.context.TemporaryVariable) AssignToExpressionMutator(org.jetbrains.kotlin.js.translate.utils.mutator.AssignToExpressionMutator) KotlinType(org.jetbrains.kotlin.types.KotlinType) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

KotlinType (org.jetbrains.kotlin.types.KotlinType)110 NotNull (org.jetbrains.annotations.NotNull)34 IElementType (com.intellij.psi.tree.IElementType)16 Type (org.jetbrains.org.objectweb.asm.Type)16 Nullable (org.jetbrains.annotations.Nullable)10 JsExpression (org.jetbrains.kotlin.js.backend.ast.JsExpression)7 PsiElement (com.intellij.psi.PsiElement)6 Name (org.jetbrains.kotlin.name.Name)6 ArrayList (java.util.ArrayList)4 KtExpression (org.jetbrains.kotlin.psi.KtExpression)4 Map (java.util.Map)3 BothSignatureWriter (org.jetbrains.kotlin.codegen.signature.BothSignatureWriter)3 JvmSignatureWriter (org.jetbrains.kotlin.codegen.signature.JvmSignatureWriter)3 VariableDescriptor (org.jetbrains.kotlin.descriptors.VariableDescriptor)3 LocalVariableDescriptor (org.jetbrains.kotlin.descriptors.impl.LocalVariableDescriptor)3 DataFlowInfo (org.jetbrains.kotlin.resolve.calls.smartcasts.DataFlowInfo)3 ExpressionReceiver (org.jetbrains.kotlin.resolve.scopes.receivers.ExpressionReceiver)3 PrimitiveType (org.jetbrains.kotlin.builtins.PrimitiveType)2 CallableDescriptor (org.jetbrains.kotlin.descriptors.CallableDescriptor)2 DeclarationDescriptor (org.jetbrains.kotlin.descriptors.DeclarationDescriptor)2