Search in sources :

Example 1 with SyntheticJavaPropertyDescriptor

use of org.jetbrains.kotlin.synthetic.SyntheticJavaPropertyDescriptor 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 2 with SyntheticJavaPropertyDescriptor

use of org.jetbrains.kotlin.synthetic.SyntheticJavaPropertyDescriptor in project kotlin by JetBrains.

the class AsmUtil method specialCaseVisibility.

@Nullable
private static Integer specialCaseVisibility(@NotNull MemberDescriptor memberDescriptor) {
    DeclarationDescriptor containingDeclaration = memberDescriptor.getContainingDeclaration();
    Visibility memberVisibility = memberDescriptor.getVisibility();
    if (isInlineOnlyOrReifiable(memberDescriptor))
        return ACC_PRIVATE;
    if (memberVisibility == Visibilities.LOCAL && memberDescriptor instanceof CallableMemberDescriptor) {
        return ACC_PUBLIC;
    }
    if (isEnumEntry(memberDescriptor)) {
        return NO_FLAG_PACKAGE_PRIVATE;
    }
    if (isToArrayFromCollection(memberDescriptor)) {
        return ACC_PUBLIC;
    }
    if (memberDescriptor instanceof ConstructorDescriptor && isAnonymousObject(memberDescriptor.getContainingDeclaration())) {
        return getVisibilityAccessFlagForAnonymous((ClassDescriptor) memberDescriptor.getContainingDeclaration());
    }
    if (memberDescriptor instanceof SyntheticJavaPropertyDescriptor) {
        return getVisibilityAccessFlag(((SyntheticJavaPropertyDescriptor) memberDescriptor).getGetMethod());
    }
    if (memberDescriptor instanceof PropertyAccessorDescriptor) {
        PropertyDescriptor property = ((PropertyAccessorDescriptor) memberDescriptor).getCorrespondingProperty();
        if (property instanceof SyntheticJavaPropertyDescriptor) {
            FunctionDescriptor method = memberDescriptor == property.getGetter() ? ((SyntheticJavaPropertyDescriptor) property).getGetMethod() : ((SyntheticJavaPropertyDescriptor) property).getSetMethod();
            assert method != null : "No get/set method in SyntheticJavaPropertyDescriptor: " + property;
            return getVisibilityAccessFlag(method);
        }
    }
    if (memberDescriptor instanceof CallableDescriptor && memberVisibility == Visibilities.PROTECTED) {
        for (CallableDescriptor overridden : DescriptorUtils.getAllOverriddenDescriptors((CallableDescriptor) memberDescriptor)) {
            if (isJvmInterface(overridden.getContainingDeclaration())) {
                return ACC_PUBLIC;
            }
        }
    }
    if (!Visibilities.isPrivate(memberVisibility)) {
        return null;
    }
    // the following code is only for PRIVATE visibility of member
    if (memberDescriptor instanceof ConstructorDescriptor) {
        if (isEnumEntry(containingDeclaration)) {
            return NO_FLAG_PACKAGE_PRIVATE;
        }
        if (isEnumClass(containingDeclaration)) {
            // see http://youtrack.jetbrains.com/issue/KT-2680
            return ACC_PROTECTED;
        }
    }
    return null;
}
Also used : SyntheticJavaPropertyDescriptor(org.jetbrains.kotlin.synthetic.SyntheticJavaPropertyDescriptor) SyntheticJavaPropertyDescriptor(org.jetbrains.kotlin.synthetic.SyntheticJavaPropertyDescriptor) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

SyntheticJavaPropertyDescriptor (org.jetbrains.kotlin.synthetic.SyntheticJavaPropertyDescriptor)2 IElementType (com.intellij.psi.tree.IElementType)1 Nullable (org.jetbrains.annotations.Nullable)1 KotlinType (org.jetbrains.kotlin.types.KotlinType)1 Type (org.jetbrains.org.objectweb.asm.Type)1