Search in sources :

Example 26 with KotlinType

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

the class ExpressionCodegen method generateExpressionMatch.

private StackValue generateExpressionMatch(StackValue expressionToMatch, KtExpression subjectExpression, KtExpression patternExpression) {
    if (expressionToMatch != null) {
        Type subjectType = expressionToMatch.type;
        markStartLineNumber(patternExpression);
        KotlinType condJetType = bindingContext.getType(patternExpression);
        Type condType;
        if (isNumberPrimitiveOrBoolean(subjectType)) {
            assert condJetType != null;
            condType = asmType(condJetType);
            if (!isNumberPrimitiveOrBoolean(condType)) {
                subjectType = boxType(subjectType);
            }
        } else {
            condType = OBJECT_TYPE;
        }
        return genEqualsForExpressionsPreferIEEE754Arithmetic(subjectExpression, patternExpression, KtTokens.EQEQ, subjectType, condType, expressionToMatch);
    } else {
        return gen(patternExpression);
    }
}
Also used : IElementType(com.intellij.psi.tree.IElementType) Type(org.jetbrains.org.objectweb.asm.Type) KotlinType(org.jetbrains.kotlin.types.KotlinType) KotlinType(org.jetbrains.kotlin.types.KotlinType)

Example 27 with KotlinType

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

the class ExpressionCodegen method generateIsCheck.

private StackValue generateIsCheck(StackValue expressionToMatch, KtTypeReference typeReference, boolean negated) {
    KotlinType jetType = bindingContext.get(TYPE, typeReference);
    markStartLineNumber(typeReference);
    StackValue value = generateIsCheck(expressionToMatch, jetType, false);
    return negated ? StackValue.not(value) : value;
}
Also used : KotlinType(org.jetbrains.kotlin.types.KotlinType)

Example 28 with KotlinType

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

the class ExpressionCodegen method intermediateValueForProperty.

public StackValue.Property intermediateValueForProperty(@NotNull PropertyDescriptor propertyDescriptor, boolean forceField, boolean syntheticBackingField, @Nullable ClassDescriptor superCallTarget, boolean skipAccessorsForPrivateFieldInOuterClass, @NotNull StackValue receiver, @Nullable ResolvedCall resolvedCall) {
    if (propertyDescriptor instanceof SyntheticJavaPropertyDescriptor) {
        return intermediateValueForSyntheticExtensionProperty((SyntheticJavaPropertyDescriptor) propertyDescriptor, receiver);
    }
    DeclarationDescriptor containingDeclaration = propertyDescriptor.getContainingDeclaration();
    FieldAccessorKind fieldAccessorKind = FieldAccessorKind.NORMAL;
    boolean isBackingFieldInClassCompanion = JvmAbi.isPropertyWithBackingFieldInOuterClass(propertyDescriptor);
    if (isBackingFieldInClassCompanion && (forceField || propertyDescriptor.isConst() && Visibilities.isPrivate(propertyDescriptor.getVisibility()))) {
        fieldAccessorKind = FieldAccessorKind.IN_CLASS_COMPANION;
    } else if (syntheticBackingField && context.getFirstCrossInlineOrNonInlineContext().getParentContext().getContextDescriptor() != containingDeclaration) {
        fieldAccessorKind = FieldAccessorKind.FIELD_FROM_LOCAL;
    }
    boolean isStaticBackingField = DescriptorUtils.isStaticDeclaration(propertyDescriptor) || AsmUtil.isInstancePropertyWithStaticBackingField(propertyDescriptor);
    boolean isSuper = superCallTarget != null;
    boolean isExtensionProperty = propertyDescriptor.getExtensionReceiverParameter() != null;
    KotlinType delegateType = JvmCodegenUtil.getPropertyDelegateType(propertyDescriptor, bindingContext);
    boolean isDelegatedProperty = delegateType != null;
    CallableMethod callableGetter = null;
    CallableMethod callableSetter = null;
    CodegenContext backingFieldContext = getBackingFieldContext(fieldAccessorKind, containingDeclaration);
    DeclarationDescriptor ownerDescriptor = containingDeclaration;
    boolean skipPropertyAccessors;
    PropertyDescriptor originalPropertyDescriptor = DescriptorUtils.unwrapFakeOverride(propertyDescriptor);
    if (fieldAccessorKind != FieldAccessorKind.NORMAL) {
        int flags = AsmUtil.getVisibilityForBackingField(propertyDescriptor, isDelegatedProperty);
        boolean isInlinedConst = propertyDescriptor.isConst() && state.getShouldInlineConstVals();
        skipPropertyAccessors = isInlinedConst || (flags & ACC_PRIVATE) == 0 || skipAccessorsForPrivateFieldInOuterClass;
        if (!skipPropertyAccessors) {
            //noinspection ConstantConditions
            propertyDescriptor = (PropertyDescriptor) backingFieldContext.getAccessor(propertyDescriptor, fieldAccessorKind, delegateType, superCallTarget);
            assert propertyDescriptor instanceof AccessorForPropertyBackingField : "Unexpected accessor descriptor: " + propertyDescriptor;
            ownerDescriptor = propertyDescriptor;
        }
    } else {
        if (!isBackingFieldInClassCompanion) {
            ownerDescriptor = propertyDescriptor;
        }
        skipPropertyAccessors = forceField;
    }
    if (!skipPropertyAccessors) {
        if (!couldUseDirectAccessToProperty(propertyDescriptor, true, isDelegatedProperty, context, state.getShouldInlineConstVals())) {
            propertyDescriptor = context.getAccessorForSuperCallIfNeeded(propertyDescriptor, superCallTarget, state);
            propertyDescriptor = context.accessibleDescriptor(propertyDescriptor, superCallTarget);
            PropertyGetterDescriptor getter = propertyDescriptor.getGetter();
            if (getter != null && !isConstOrHasJvmFieldAnnotation(propertyDescriptor)) {
                callableGetter = typeMapper.mapToCallableMethod(getter, isSuper);
            }
        }
        if (propertyDescriptor.isVar()) {
            PropertySetterDescriptor setter = propertyDescriptor.getSetter();
            if (setter != null && !couldUseDirectAccessToProperty(propertyDescriptor, false, isDelegatedProperty, context, state.getShouldInlineConstVals()) && !isConstOrHasJvmFieldAnnotation(propertyDescriptor)) {
                callableSetter = typeMapper.mapToCallableMethod(setter, isSuper);
            }
        }
    }
    if (!isStaticBackingField) {
        propertyDescriptor = DescriptorUtils.unwrapFakeOverride(propertyDescriptor);
    }
    Type backingFieldOwner = typeMapper.mapOwner(ownerDescriptor);
    String fieldName;
    if (isExtensionProperty && !isDelegatedProperty) {
        fieldName = null;
    } else if (originalPropertyDescriptor.getContainingDeclaration() == backingFieldContext.getContextDescriptor()) {
        assert backingFieldContext instanceof FieldOwnerContext : "Actual context is " + backingFieldContext + " but should be instance of FieldOwnerContext";
        fieldName = ((FieldOwnerContext) backingFieldContext).getFieldName(propertyDescriptor, isDelegatedProperty);
    } else {
        fieldName = KotlinTypeMapper.mapDefaultFieldName(propertyDescriptor, isDelegatedProperty);
    }
    return StackValue.property(propertyDescriptor, backingFieldOwner, typeMapper.mapType(isDelegatedProperty && forceField ? delegateType : propertyDescriptor.getOriginal().getType()), isStaticBackingField, fieldName, callableGetter, callableSetter, receiver, this, resolvedCall);
}
Also used : SyntheticJavaPropertyDescriptor(org.jetbrains.kotlin.synthetic.SyntheticJavaPropertyDescriptor) SyntheticJavaPropertyDescriptor(org.jetbrains.kotlin.synthetic.SyntheticJavaPropertyDescriptor) KotlinType(org.jetbrains.kotlin.types.KotlinType) IElementType(com.intellij.psi.tree.IElementType) Type(org.jetbrains.org.objectweb.asm.Type) KotlinType(org.jetbrains.kotlin.types.KotlinType)

Example 29 with KotlinType

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

the class RangeCodegenUtil method isCharSequenceIndices.

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

Example 30 with KotlinType

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

the class ExpressionCodegen method genVarargs.

public void genVarargs(@NotNull VarargValueArgument valueArgument, @NotNull KotlinType outType) {
    Type type = asmType(outType);
    assert type.getSort() == Type.ARRAY;
    Type elementType = correctElementType(type);
    List<ValueArgument> arguments = valueArgument.getArguments();
    int size = arguments.size();
    boolean hasSpread = false;
    for (int i = 0; i != size; ++i) {
        if (arguments.get(i).getSpreadElement() != null) {
            hasSpread = true;
            break;
        }
    }
    if (hasSpread) {
        boolean arrayOfReferences = KotlinBuiltIns.isArray(outType);
        if (size == 1) {
            // Arrays.copyOf(receiverValue, newLength)
            ValueArgument argument = arguments.get(0);
            Type arrayType = arrayOfReferences ? Type.getType("[Ljava/lang/Object;") : Type.getType("[" + elementType.getDescriptor());
            gen(argument.getArgumentExpression(), type);
            v.dup();
            v.arraylength();
            v.invokestatic("java/util/Arrays", "copyOf", Type.getMethodDescriptor(arrayType, arrayType, Type.INT_TYPE), false);
            if (arrayOfReferences) {
                v.checkcast(type);
            }
        } else {
            String owner;
            String addDescriptor;
            String toArrayDescriptor;
            if (arrayOfReferences) {
                owner = "kotlin/jvm/internal/SpreadBuilder";
                addDescriptor = "(Ljava/lang/Object;)V";
                toArrayDescriptor = "([Ljava/lang/Object;)[Ljava/lang/Object;";
            } else {
                String spreadBuilderClassName = AsmUtil.asmPrimitiveTypeToLangPrimitiveType(elementType).getTypeName().getIdentifier() + "SpreadBuilder";
                owner = "kotlin/jvm/internal/" + spreadBuilderClassName;
                addDescriptor = "(" + elementType.getDescriptor() + ")V";
                toArrayDescriptor = "()" + type.getDescriptor();
            }
            v.anew(Type.getObjectType(owner));
            v.dup();
            v.iconst(size);
            v.invokespecial(owner, "<init>", "(I)V", false);
            for (int i = 0; i != size; ++i) {
                v.dup();
                ValueArgument argument = arguments.get(i);
                if (argument.getSpreadElement() != null) {
                    gen(argument.getArgumentExpression(), OBJECT_TYPE);
                    v.invokevirtual(owner, "addSpread", "(Ljava/lang/Object;)V", false);
                } else {
                    gen(argument.getArgumentExpression(), elementType);
                    v.invokevirtual(owner, "add", addDescriptor, false);
                }
            }
            if (arrayOfReferences) {
                v.dup();
                v.invokevirtual(owner, "size", "()I", false);
                newArrayInstruction(outType);
                v.invokevirtual(owner, "toArray", toArrayDescriptor, false);
                v.checkcast(type);
            } else {
                v.invokevirtual(owner, "toArray", toArrayDescriptor, false);
            }
        }
    } else {
        v.iconst(arguments.size());
        newArrayInstruction(outType);
        for (int i = 0; i != size; ++i) {
            v.dup();
            StackValue rightSide = gen(arguments.get(i).getArgumentExpression());
            StackValue.arrayElement(elementType, StackValue.onStack(type), StackValue.constant(i, Type.INT_TYPE)).store(rightSide, v);
        }
    }
}
Also used : IElementType(com.intellij.psi.tree.IElementType) Type(org.jetbrains.org.objectweb.asm.Type) 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