Search in sources :

Example 46 with KotlinType

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

the class PatternTranslator method translateExpressionPattern.

@NotNull
public JsExpression translateExpressionPattern(@NotNull KotlinType type, @NotNull JsExpression expressionToMatch, @NotNull KtExpression patternExpression) {
    JsExpression expressionToMatchAgainst = translateExpressionForExpressionPattern(patternExpression);
    KotlinType patternType = BindingUtils.getTypeForExpression(bindingContext(), patternExpression);
    EqualityType matchEquality = equalityType(type);
    EqualityType patternEquality = equalityType(patternType);
    if (matchEquality == EqualityType.PRIMITIVE && patternEquality == EqualityType.PRIMITIVE) {
        return equality(expressionToMatch, expressionToMatchAgainst);
    } else if (expressionToMatchAgainst == JsLiteral.NULL) {
        return TranslationUtils.nullCheck(expressionToMatch, false);
    } else {
        return TopLevelFIF.KOTLIN_EQUALS.apply(expressionToMatch, Collections.singletonList(expressionToMatchAgainst), context());
    }
}
Also used : JsExpression(org.jetbrains.kotlin.js.backend.ast.JsExpression) KotlinType(org.jetbrains.kotlin.types.KotlinType) NotNull(org.jetbrains.annotations.NotNull)

Example 47 with KotlinType

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

the class PatternTranslator method translateIsExpression.

@NotNull
public JsExpression translateIsExpression(@NotNull KtIsExpression expression) {
    KtExpression left = expression.getLeftHandSide();
    JsExpression expressionToCheck = Translation.translateAsExpression(left, context());
    KotlinType leftType = context().bindingContext().getType(left);
    if (leftType != null && KotlinBuiltIns.isChar(leftType)) {
        expressionToCheck = JsAstUtils.charToBoxedChar(expressionToCheck);
    }
    KtTypeReference typeReference = expression.getTypeReference();
    assert typeReference != null;
    JsExpression result = translateIsCheck(expressionToCheck, typeReference);
    if (result == null)
        return JsLiteral.getBoolean(!expression.isNegated());
    if (expression.isNegated()) {
        return not(result);
    }
    return result;
}
Also used : JsExpression(org.jetbrains.kotlin.js.backend.ast.JsExpression) KotlinType(org.jetbrains.kotlin.types.KotlinType) KtExpression(org.jetbrains.kotlin.psi.KtExpression) KtTypeReference(org.jetbrains.kotlin.psi.KtTypeReference) NotNull(org.jetbrains.annotations.NotNull)

Example 48 with KotlinType

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

the class PatternTranslator method doGetIsTypeCheckCallable.

@Nullable
private JsExpression doGetIsTypeCheckCallable(@NotNull KotlinType type) {
    ClassifierDescriptor targetDescriptor = type.getConstructor().getDeclarationDescriptor();
    if (targetDescriptor != null && AnnotationsUtils.isNativeInterface(targetDescriptor)) {
        return type.isMarkedNullable() ? null : namer().isInstanceOf(JsAstUtils.pureFqn("Object", null));
    }
    JsExpression builtinCheck = getIsTypeCheckCallableForBuiltin(type);
    if (builtinCheck != null)
        return builtinCheck;
    builtinCheck = getIsTypeCheckCallableForPrimitiveBuiltin(type);
    if (builtinCheck != null)
        return builtinCheck;
    TypeParameterDescriptor typeParameterDescriptor = getTypeParameterDescriptorOrNull(type);
    if (typeParameterDescriptor != null) {
        if (typeParameterDescriptor.isReified()) {
            return getIsTypeCheckCallableForReifiedType(typeParameterDescriptor);
        }
        JsExpression result = null;
        for (KotlinType upperBound : typeParameterDescriptor.getUpperBounds()) {
            JsExpression next = doGetIsTypeCheckCallable(upperBound);
            if (next != null) {
                result = result != null ? namer().andPredicate(result, next) : next;
            }
        }
        return result;
    }
    ClassDescriptor referencedClass = DescriptorUtils.getClassDescriptorForType(type);
    JsExpression typeName = ReferenceTranslator.translateAsTypeReference(referencedClass, context());
    return namer().isInstanceOf(typeName);
}
Also used : JsExpression(org.jetbrains.kotlin.js.backend.ast.JsExpression) KotlinType(org.jetbrains.kotlin.types.KotlinType) Nullable(org.jetbrains.annotations.Nullable)

Example 49 with KotlinType

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

the class IntrinsicAssignmentTranslator method translateRightExpression.

private JsExpression translateRightExpression(TranslationContext context, KtBinaryExpression expression) {
    JsExpression result = TranslationUtils.translateRightExpression(context, expression, rightBlock);
    KotlinType leftType = context.bindingContext().getType(expression.getLeft());
    KotlinType rightType = context.bindingContext().getType(expression.getRight());
    if (rightType != null && KotlinBuiltIns.isCharOrNullableChar(rightType)) {
        if (leftType != null && KotlinBuiltIns.isStringOrNullableString(leftType)) {
            result = JsAstUtils.charToString(result);
        } else if (leftType == null || !KotlinBuiltIns.isCharOrNullableChar(leftType)) {
            result = JsAstUtils.charToBoxedChar(result);
        }
    }
    return result;
}
Also used : JsExpression(org.jetbrains.kotlin.js.backend.ast.JsExpression) KotlinType(org.jetbrains.kotlin.types.KotlinType)

Example 50 with KotlinType

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

the class Translation method translateConstant.

@Nullable
public static JsExpression translateConstant(@NotNull CompileTimeConstant compileTimeValue, @NotNull KtExpression expression, @NotNull TranslationContext context) {
    KotlinType expectedType = context.bindingContext().getType(expression);
    ConstantValue<?> constant = compileTimeValue.toConstantValue(expectedType != null ? expectedType : TypeUtils.NO_EXPECTED_TYPE);
    if (constant instanceof NullValue) {
        return JsLiteral.NULL;
    }
    Object value = constant.getValue();
    if (value instanceof Integer || value instanceof Short || value instanceof Byte) {
        return context.program().getNumberLiteral(((Number) value).intValue());
    } else if (value instanceof Long) {
        return JsAstUtils.newLong((Long) value, context);
    } else if (value instanceof Float) {
        float floatValue = (Float) value;
        double doubleValue;
        if (Float.isInfinite(floatValue) || Float.isNaN(floatValue)) {
            doubleValue = floatValue;
        } else {
            doubleValue = Double.parseDouble(Float.toString(floatValue));
        }
        return context.program().getNumberLiteral(doubleValue);
    } else if (value instanceof Number) {
        return context.program().getNumberLiteral(((Number) value).doubleValue());
    } else if (value instanceof Boolean) {
        return JsLiteral.getBoolean((Boolean) value);
    }
    //TODO: test
    if (value instanceof String) {
        return context.program().getStringLiteral((String) value);
    }
    if (value instanceof Character) {
        return context.program().getNumberLiteral(((Character) value).charValue());
    }
    return null;
}
Also used : KotlinType(org.jetbrains.kotlin.types.KotlinType) NullValue(org.jetbrains.kotlin.resolve.constants.NullValue) Nullable(org.jetbrains.annotations.Nullable)

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