Search in sources :

Example 96 with KotlinType

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

the class ExpressionCodegen method visitBinaryWithTypeRHSExpression.

@Override
public StackValue visitBinaryWithTypeRHSExpression(@NotNull KtBinaryExpressionWithTypeRHS expression, StackValue receiver) {
    KtExpression left = expression.getLeft();
    final IElementType opToken = expression.getOperationReference().getReferencedNameElementType();
    final KotlinType rightType = bindingContext.get(TYPE, expression.getRight());
    assert rightType != null;
    final StackValue value = genQualified(receiver, left);
    return StackValue.operation(boxType(asmType(rightType)), new Function1<InstructionAdapter, Unit>() {

        @Override
        public Unit invoke(InstructionAdapter v) {
            value.put(boxType(value.type), v);
            if (value.type == Type.VOID_TYPE) {
                StackValue.putUnitInstance(v);
            }
            boolean safeAs = opToken == KtTokens.AS_SAFE;
            Type type = boxType(asmType(rightType));
            if (TypeUtils.isReifiedTypeParameter(rightType)) {
                putReifiedOperationMarkerIfTypeIsReifiedParameter(rightType, safeAs ? ReifiedTypeInliner.OperationKind.SAFE_AS : ReifiedTypeInliner.OperationKind.AS);
                v.checkcast(type);
                return Unit.INSTANCE;
            }
            CodegenUtilKt.generateAsCast(v, rightType, type, safeAs);
            return Unit.INSTANCE;
        }
    });
}
Also used : IElementType(com.intellij.psi.tree.IElementType) IElementType(com.intellij.psi.tree.IElementType) Type(org.jetbrains.org.objectweb.asm.Type) KotlinType(org.jetbrains.kotlin.types.KotlinType) InstructionAdapter(org.jetbrains.org.objectweb.asm.commons.InstructionAdapter) KotlinType(org.jetbrains.kotlin.types.KotlinType) Unit(kotlin.Unit)

Example 97 with KotlinType

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

the class PropertyCodegen method generateBackingField.

private void generateBackingField(KtNamedDeclaration element, PropertyDescriptor propertyDescriptor, boolean isDelegate, KotlinType kotlinType, Object defaultValue, Annotations annotations) {
    int modifiers = getDeprecatedAccessFlag(propertyDescriptor);
    for (AnnotationCodegen.JvmFlagAnnotation flagAnnotation : AnnotationCodegen.FIELD_FLAGS) {
        if (flagAnnotation.hasAnnotation(propertyDescriptor.getOriginal())) {
            modifiers |= flagAnnotation.getJvmFlag();
        }
    }
    if (kind == OwnerKind.PACKAGE) {
        modifiers |= ACC_STATIC;
    }
    if (!propertyDescriptor.isLateInit() && (!propertyDescriptor.isVar() || isDelegate)) {
        modifiers |= ACC_FINAL;
    }
    if (AnnotationUtilKt.hasJvmSyntheticAnnotation(propertyDescriptor)) {
        modifiers |= ACC_SYNTHETIC;
    }
    Type type = typeMapper.mapType(kotlinType);
    ClassBuilder builder = v;
    FieldOwnerContext backingFieldContext = context;
    if (AsmUtil.isInstancePropertyWithStaticBackingField(propertyDescriptor)) {
        modifiers |= ACC_STATIC;
        if (JvmAbi.isPropertyWithBackingFieldInOuterClass(propertyDescriptor)) {
            ImplementationBodyCodegen codegen = (ImplementationBodyCodegen) memberCodegen.getParentCodegen();
            builder = codegen.v;
            backingFieldContext = codegen.context;
        }
    }
    modifiers |= getVisibilityForBackingField(propertyDescriptor, isDelegate);
    if (AsmUtil.isPropertyWithBackingFieldCopyInOuterClass(propertyDescriptor)) {
        ImplementationBodyCodegen parentBodyCodegen = (ImplementationBodyCodegen) memberCodegen.getParentCodegen();
        parentBodyCodegen.addCompanionObjectPropertyToCopy(propertyDescriptor, defaultValue);
    }
    String name = backingFieldContext.getFieldName(propertyDescriptor, isDelegate);
    v.getSerializationBindings().put(FIELD_FOR_PROPERTY, propertyDescriptor, Pair.create(type, name));
    FieldVisitor fv = builder.newField(JvmDeclarationOriginKt.OtherOrigin(element, propertyDescriptor), modifiers, name, type.getDescriptor(), isDelegate ? null : typeMapper.mapFieldSignature(kotlinType, propertyDescriptor), defaultValue);
    Annotated fieldAnnotated = new AnnotatedWithFakeAnnotations(propertyDescriptor, annotations);
    AnnotationCodegen.forField(fv, memberCodegen, typeMapper).genAnnotations(fieldAnnotated, type, isDelegate ? AnnotationUseSiteTarget.PROPERTY_DELEGATE_FIELD : AnnotationUseSiteTarget.FIELD);
}
Also used : Annotated(org.jetbrains.kotlin.descriptors.annotations.Annotated) KotlinType(org.jetbrains.kotlin.types.KotlinType) Type(org.jetbrains.org.objectweb.asm.Type) AnnotatedWithFakeAnnotations(org.jetbrains.kotlin.codegen.annotation.AnnotatedWithFakeAnnotations) FieldVisitor(org.jetbrains.org.objectweb.asm.FieldVisitor)

Example 98 with KotlinType

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

the class PropertyCodegen method getDelegateTypeForProperty.

@NotNull
private KotlinType getDelegateTypeForProperty(@NotNull KtProperty p, @NotNull PropertyDescriptor propertyDescriptor) {
    KotlinType delegateType = null;
    ResolvedCall<FunctionDescriptor> provideDelegateResolvedCall = bindingContext.get(BindingContext.PROVIDE_DELEGATE_RESOLVED_CALL, propertyDescriptor);
    KtExpression delegateExpression = p.getDelegateExpression();
    if (provideDelegateResolvedCall != null) {
        delegateType = provideDelegateResolvedCall.getResultingDescriptor().getReturnType();
    } else if (delegateExpression != null) {
        delegateType = bindingContext.getType(delegateExpression);
    }
    if (delegateType == null) {
        // Delegation convention is unresolved
        delegateType = ErrorUtils.createErrorType("Delegate type");
    }
    return delegateType;
}
Also used : KotlinType(org.jetbrains.kotlin.types.KotlinType) NotNull(org.jetbrains.annotations.NotNull)

Example 99 with KotlinType

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

the class RangeCodegenUtil method isPrimitiveRangeSpecializationOfType.

/*
     * Checks whether for expression 'x in a..b' a..b is primitive integral range
     * with same type as x.
     */
public static boolean isPrimitiveRangeSpecializationOfType(@NotNull Type argumentType, @NotNull KtExpression rangeExpression, @NotNull BindingContext bindingContext) {
    if (rangeExpression instanceof KtBinaryExpression && ((KtBinaryExpression) rangeExpression).getOperationReference().getReferencedNameElementType() == KtTokens.RANGE) {
        KotlinType kotlinType = bindingContext.getType(rangeExpression);
        assert kotlinType != null;
        DeclarationDescriptor descriptor = kotlinType.getConstructor().getDeclarationDescriptor();
        if (descriptor != null) {
            FqNameUnsafe fqName = DescriptorUtils.getFqName(descriptor);
            if (fqName.equals(KotlinBuiltIns.FQ_NAMES.longRange)) {
                return argumentType == Type.LONG_TYPE;
            }
            if (fqName.equals(KotlinBuiltIns.FQ_NAMES.charRange) || fqName.equals(KotlinBuiltIns.FQ_NAMES.intRange)) {
                return AsmUtil.isIntPrimitive(argumentType);
            }
        }
    }
    return false;
}
Also used : FqNameUnsafe(org.jetbrains.kotlin.name.FqNameUnsafe) KotlinType(org.jetbrains.kotlin.types.KotlinType)

Example 100 with KotlinType

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

the class RangeCodegenUtil method isArrayOrPrimitiveArrayIndices.

public static boolean isArrayOrPrimitiveArrayIndices(@NotNull CallableDescriptor descriptor) {
    if (!isTopLevelInPackage(descriptor, "indices", "kotlin.collections"))
        return false;
    ReceiverParameterDescriptor extensionReceiver = descriptor.getExtensionReceiverParameter();
    if (extensionReceiver == null)
        return false;
    KotlinType extensionReceiverType = extensionReceiver.getType();
    if (!KotlinBuiltIns.isArray(extensionReceiverType) && !KotlinBuiltIns.isPrimitiveArray(extensionReceiverType))
        return false;
    return true;
}
Also used : KotlinType(org.jetbrains.kotlin.types.KotlinType)

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