Search in sources :

Example 86 with KotlinType

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

the class PatternTranslator method translateCastExpression.

@NotNull
public JsExpression translateCastExpression(@NotNull KtBinaryExpressionWithTypeRHS expression) {
    assert isCastExpression(expression) : "Expected cast expression, got " + expression;
    KtExpression left = expression.getLeft();
    JsExpression expressionToCast = Translation.translateAsExpression(left, context());
    KtTypeReference typeReference = expression.getRight();
    assert typeReference != null : "Cast expression must have type reference";
    KotlinType leftType = context().bindingContext().getType(left);
    if (leftType != null && KotlinBuiltIns.isChar(leftType)) {
        expressionToCast = JsAstUtils.charToBoxedChar(expressionToCast);
    }
    TemporaryVariable temporary = context().declareTemporary(expressionToCast);
    JsExpression isCheck = translateIsCheck(temporary.assignmentExpression(), typeReference);
    if (isCheck == null)
        return expressionToCast;
    JsExpression onFail;
    if (isSafeCast(expression)) {
        onFail = JsLiteral.NULL;
    } else {
        JsExpression throwCCEFunRef = Namer.throwClassCastExceptionFunRef();
        onFail = new JsInvocation(throwCCEFunRef);
    }
    JsExpression result = new JsConditional(isCheck, temporary.reference(), onFail);
    KotlinType expressionType = context().bindingContext().getType(expression);
    if (expressionType != null && KotlinBuiltIns.isCharOrNullableChar(expressionType)) {
        result = JsAstUtils.boxedCharToChar(result);
    }
    return result;
}
Also used : TemporaryVariable(org.jetbrains.kotlin.js.translate.context.TemporaryVariable) JsExpression(org.jetbrains.kotlin.js.backend.ast.JsExpression) JsInvocation(org.jetbrains.kotlin.js.backend.ast.JsInvocation) JsConditional(org.jetbrains.kotlin.js.backend.ast.JsConditional) KotlinType(org.jetbrains.kotlin.types.KotlinType) KtExpression(org.jetbrains.kotlin.psi.KtExpression) KtTypeReference(org.jetbrains.kotlin.psi.KtTypeReference) NotNull(org.jetbrains.annotations.NotNull)

Example 87 with KotlinType

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

the class PatternTranslator method translateIsCheck.

@Nullable
public JsExpression translateIsCheck(@NotNull JsExpression subject, @NotNull KtTypeReference targetTypeReference) {
    KotlinType targetType = getTypeByReference(bindingContext(), targetTypeReference);
    JsExpression checkFunReference = doGetIsTypeCheckCallable(targetType);
    if (checkFunReference == null) {
        return null;
    }
    boolean isReifiedType = isReifiedTypeParameter(targetType);
    if (!isReifiedType && isNullableType(targetType) || isReifiedType && findChildByType(targetTypeReference, KtNodeTypes.NULLABLE_TYPE) != null) {
        checkFunReference = namer().orNull(checkFunReference);
    }
    return new JsInvocation(checkFunReference, subject);
}
Also used : JsExpression(org.jetbrains.kotlin.js.backend.ast.JsExpression) JsInvocation(org.jetbrains.kotlin.js.backend.ast.JsInvocation) KotlinType(org.jetbrains.kotlin.types.KotlinType) Nullable(org.jetbrains.annotations.Nullable)

Example 88 with KotlinType

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

the class TranslationUtils method translateInitializerForProperty.

@Nullable
public static JsExpression translateInitializerForProperty(@NotNull KtProperty declaration, @NotNull TranslationContext context) {
    JsExpression jsInitExpression = null;
    KtExpression initializer = declaration.getInitializer();
    if (initializer != null) {
        jsInitExpression = Translation.translateAsExpression(initializer, context);
        KotlinType propertyType = BindingContextUtils.getNotNull(context.bindingContext(), BindingContext.VARIABLE, declaration).getType();
        KotlinType initType = context.bindingContext().getType(initializer);
        if (initType != null && KotlinBuiltIns.isCharOrNullableChar(initType) && !KotlinBuiltIns.isCharOrNullableChar(propertyType)) {
            jsInitExpression = JsAstUtils.charToBoxedChar(jsInitExpression);
        }
    }
    return jsInitExpression;
}
Also used : KotlinType(org.jetbrains.kotlin.types.KotlinType) Nullable(org.jetbrains.annotations.Nullable)

Example 89 with KotlinType

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

the class AsmUtil method genParamAssertion.

private static void genParamAssertion(@NotNull InstructionAdapter v, @NotNull KotlinTypeMapper typeMapper, @NotNull FrameMap frameMap, @NotNull CallableDescriptor parameter, @NotNull String name) {
    KotlinType type = parameter.getReturnType();
    if (type == null || isNullableType(type))
        return;
    int index = frameMap.getIndex(parameter);
    Type asmType = typeMapper.mapType(type);
    if (asmType.getSort() == Type.OBJECT || asmType.getSort() == Type.ARRAY) {
        v.load(index, asmType);
        v.visitLdcInsn(name);
        v.invokestatic(IntrinsicMethods.INTRINSICS_CLASS_NAME, "checkParameterIsNotNull", "(Ljava/lang/Object;Ljava/lang/String;)V", false);
    }
}
Also used : IElementType(com.intellij.psi.tree.IElementType) JvmPrimitiveType(org.jetbrains.kotlin.resolve.jvm.JvmPrimitiveType) PrimitiveType(org.jetbrains.kotlin.builtins.PrimitiveType) KotlinType(org.jetbrains.kotlin.types.KotlinType) TypeUtils.isNullableType(org.jetbrains.kotlin.types.TypeUtils.isNullableType) KotlinType(org.jetbrains.kotlin.types.KotlinType)

Example 90 with KotlinType

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

the class ClosureCodegen method calculateConstructorParameters.

@NotNull
public static List<FieldInfo> calculateConstructorParameters(@NotNull KotlinTypeMapper typeMapper, @NotNull CalculatedClosure closure, @NotNull Type ownerType) {
    List<FieldInfo> args = Lists.newArrayList();
    ClassDescriptor captureThis = closure.getCaptureThis();
    if (captureThis != null) {
        Type type = typeMapper.mapType(captureThis);
        args.add(FieldInfo.createForHiddenField(ownerType, type, CAPTURED_THIS_FIELD));
    }
    KotlinType captureReceiverType = closure.getCaptureReceiverType();
    if (captureReceiverType != null) {
        args.add(FieldInfo.createForHiddenField(ownerType, typeMapper.mapType(captureReceiverType), CAPTURED_RECEIVER_FIELD));
    }
    for (EnclosedValueDescriptor enclosedValueDescriptor : closure.getCaptureVariables().values()) {
        DeclarationDescriptor descriptor = enclosedValueDescriptor.getDescriptor();
        if ((descriptor instanceof VariableDescriptor && !(descriptor instanceof PropertyDescriptor)) || ExpressionTypingUtils.isLocalFunction(descriptor)) {
            args.add(FieldInfo.createForHiddenField(ownerType, enclosedValueDescriptor.getType(), enclosedValueDescriptor.getFieldName()));
        } else if (descriptor instanceof FunctionDescriptor) {
            assert captureReceiverType != null;
        }
    }
    return args;
}
Also used : KotlinType(org.jetbrains.kotlin.types.KotlinType) Type(org.jetbrains.org.objectweb.asm.Type) KotlinType(org.jetbrains.kotlin.types.KotlinType) EnclosedValueDescriptor(org.jetbrains.kotlin.codegen.context.EnclosedValueDescriptor) 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