Exemplo n.º 1
0
 private static void collectOverriddenDeclarations(
     @NotNull CallableMemberDescriptor descriptor, @NotNull Set<CallableMemberDescriptor> result) {
   if (descriptor.getKind().isReal()) {
     result.add(descriptor);
   } else {
     if (descriptor.getOverriddenDescriptors().isEmpty()) {
       throw new IllegalStateException(
           "No overridden descriptors found for (fake override) " + descriptor);
     }
     for (CallableMemberDescriptor overridden : descriptor.getOverriddenDescriptors()) {
       collectOverriddenDeclarations(overridden, result);
     }
   }
 }
Exemplo n.º 2
0
 private void checkDeclaredTypeInPublicMember(
     JetNamedDeclaration member, CallableMemberDescriptor memberDescriptor) {
   boolean hasDeferredType;
   if (member instanceof JetProperty) {
     hasDeferredType =
         ((JetProperty) member).getTypeReference() == null
             && DescriptorResolver.hasBody((JetProperty) member);
   } else {
     assert member instanceof JetFunction;
     JetFunction function = (JetFunction) member;
     hasDeferredType =
         function.getTypeReference() == null && function.hasBody() && !function.hasBlockBody();
   }
   if ((memberDescriptor.getVisibility().isPublicAPI())
       && memberDescriptor.getOverriddenDescriptors().size() == 0
       && hasDeferredType) {
     trace.report(PUBLIC_MEMBER_SHOULD_SPECIFY_TYPE.on(member));
   }
 }
 @NotNull
 private static Collection<CallableMemberDescriptor>
     getAllDeclaredNonOverridableOverriddenDescriptors(
         @NotNull CallableMemberDescriptor callableMemberDescriptor) {
   Set<CallableMemberDescriptor> result = Sets.newHashSet();
   Collection<CallableMemberDescriptor> nonOverridableOverriddenDescriptors =
       retainNonOverridableMembers(callableMemberDescriptor.getOverriddenDescriptors());
   for (CallableMemberDescriptor overriddenDescriptor : nonOverridableOverriddenDescriptors) {
     CallableMemberDescriptor.Kind kind = overriddenDescriptor.getKind();
     if (kind == DECLARATION) {
       result.add(overriddenDescriptor);
     } else if (kind == FAKE_OVERRIDE || kind == DELEGATION) {
       result.addAll(getAllDeclaredNonOverridableOverriddenDescriptors(overriddenDescriptor));
     } else if (kind == SYNTHESIZED) {
       // do nothing, final synthesized members can't be made open
     } else {
       throw new UnsupportedOperationException("Unexpected callable kind " + kind);
     }
   }
   return result;
 }
Exemplo n.º 4
0
  public static void resolveUnknownVisibilityForMember(
      @NotNull CallableMemberDescriptor memberDescriptor,
      @Nullable Function1<CallableMemberDescriptor, Unit> cannotInferVisibility) {
    for (CallableMemberDescriptor descriptor : memberDescriptor.getOverriddenDescriptors()) {
      if (descriptor.getVisibility() == Visibilities.INHERITED) {
        resolveUnknownVisibilityForMember(descriptor, cannotInferVisibility);
      }
    }

    if (memberDescriptor.getVisibility() != Visibilities.INHERITED) {
      return;
    }

    Visibility maxVisibility = computeVisibilityToInherit(memberDescriptor);
    Visibility visibilityToInherit;
    if (maxVisibility == null) {
      if (cannotInferVisibility != null) {
        cannotInferVisibility.invoke(memberDescriptor);
      }
      visibilityToInherit = Visibilities.PUBLIC;
    } else {
      visibilityToInherit = maxVisibility;
    }

    if (memberDescriptor instanceof PropertyDescriptorImpl) {
      ((PropertyDescriptorImpl) memberDescriptor).setVisibility(visibilityToInherit);
      for (PropertyAccessorDescriptor accessor :
          ((PropertyDescriptor) memberDescriptor).getAccessors()) {
        // If we couldn't infer visibility for property, the diagnostic is already reported, no need
        // to report it again on accessors
        resolveUnknownVisibilityForMember(
            accessor, maxVisibility == null ? null : cannotInferVisibility);
      }
    } else if (memberDescriptor instanceof FunctionDescriptorImpl) {
      ((FunctionDescriptorImpl) memberDescriptor).setVisibility(visibilityToInherit);
    } else {
      assert memberDescriptor instanceof PropertyAccessorDescriptorImpl;
      ((PropertyAccessorDescriptorImpl) memberDescriptor).setVisibility(visibilityToInherit);
    }
  }
Exemplo n.º 5
0
 @Nullable
 private static Visibility computeVisibilityToInherit(
     @NotNull CallableMemberDescriptor memberDescriptor) {
   Collection<? extends CallableMemberDescriptor> overriddenDescriptors =
       memberDescriptor.getOverriddenDescriptors();
   Visibility maxVisibility = findMaxVisibility(overriddenDescriptors);
   if (maxVisibility == null) {
     return null;
   }
   if (memberDescriptor.getKind() == CallableMemberDescriptor.Kind.FAKE_OVERRIDE) {
     for (CallableMemberDescriptor overridden : overriddenDescriptors) {
       // An implementation (a non-abstract overridden member) of a fake override should have the
       // maximum possible visibility
       if (overridden.getModality() != Modality.ABSTRACT
           && !overridden.getVisibility().equals(maxVisibility)) {
         return null;
       }
     }
     return maxVisibility;
   }
   return maxVisibility.normalize();
 }
Exemplo n.º 6
0
  private static void createAndBindFakeOverride(
      @NotNull Collection<CallableMemberDescriptor> overridables,
      @NotNull ClassDescriptor current,
      @NotNull OverridingStrategy strategy) {
    Collection<CallableMemberDescriptor> visibleOverridables =
        filterVisibleFakeOverrides(current, overridables);
    boolean allInvisible = visibleOverridables.isEmpty();
    Collection<CallableMemberDescriptor> effectiveOverridden =
        allInvisible ? overridables : visibleOverridables;

    Modality modality = determineModality(effectiveOverridden);
    Visibility visibility = allInvisible ? Visibilities.INVISIBLE_FAKE : Visibilities.INHERITED;

    // FIXME doesn't work as expected for flexible types: should create a refined signature.
    // Current algorithm produces bad results in presence of annotated Java signatures such as:
    //      J: foo(s: String!): String -- @NotNull String foo(String s);
    //      K: foo(s: String): String?
    //  --> 'foo(s: String!): String' as an inherited signature with most specific return type.
    // This is bad because it can be overridden by 'foo(s: String?): String', which is not
    // override-equivalent with K::foo above.
    // Should be 'foo(s: String): String'.
    CallableMemberDescriptor mostSpecific =
        selectMostSpecificMember(
            effectiveOverridden,
            new Function1<CallableMemberDescriptor, CallableDescriptor>() {
              @Override
              public CallableMemberDescriptor invoke(CallableMemberDescriptor descriptor) {
                return descriptor;
              }
            });
    CallableMemberDescriptor fakeOverride =
        mostSpecific.copy(
            current, modality, visibility, CallableMemberDescriptor.Kind.FAKE_OVERRIDE, false);
    strategy.setOverriddenDescriptors(fakeOverride, effectiveOverridden);
    assert !fakeOverride.getOverriddenDescriptors().isEmpty()
        : "Overridden descriptors should be set for " + CallableMemberDescriptor.Kind.FAKE_OVERRIDE;
    strategy.addFakeOverride(fakeOverride);
  }
Exemplo n.º 7
0
 public static boolean isOverride(@NotNull CallableMemberDescriptor descriptor) {
   return !descriptor.getOverriddenDescriptors().isEmpty();
 }