Ejemplo n.º 1
0
  public static void reportCyclicInheritanceHierarchyError(
      @NotNull BindingTrace trace,
      @NotNull ClassDescriptor classDescriptor,
      @NotNull ClassDescriptor superclass) {
    PsiElement psiElement =
        BindingContextUtils.classDescriptorToDeclaration(
            trace.getBindingContext(), classDescriptor);

    PsiElement elementToMark = null;
    if (psiElement instanceof JetClassOrObject) {
      JetClassOrObject classOrObject = (JetClassOrObject) psiElement;
      for (JetDelegationSpecifier delegationSpecifier : classOrObject.getDelegationSpecifiers()) {
        JetTypeReference typeReference = delegationSpecifier.getTypeReference();
        if (typeReference == null) continue;
        JetType supertype = trace.get(TYPE, typeReference);
        if (supertype != null && supertype.getConstructor() == superclass.getTypeConstructor()) {
          elementToMark = typeReference;
        }
      }
    }
    if (elementToMark == null && psiElement instanceof PsiNameIdentifierOwner) {
      PsiNameIdentifierOwner namedElement = (PsiNameIdentifierOwner) psiElement;
      PsiElement nameIdentifier = namedElement.getNameIdentifier();
      if (nameIdentifier != null) {
        elementToMark = nameIdentifier;
      }
    }
    if (elementToMark != null) {
      trace.report(CYCLIC_INHERITANCE_HIERARCHY.on(elementToMark));
    }
  }
  @NotNull
  private static AnonymousFunctionDescriptor createFunctionDescriptor(
      @NotNull JetFunctionLiteralExpression expression,
      @NotNull ExpressionTypingContext context,
      boolean functionTypeExpected) {
    JetFunctionLiteral functionLiteral = expression.getFunctionLiteral();
    JetTypeReference receiverTypeRef = functionLiteral.getReceiverTypeRef();
    AnonymousFunctionDescriptor functionDescriptor =
        new AnonymousFunctionDescriptor(
            context.scope.getContainingDeclaration(),
            Collections.<AnnotationDescriptor>emptyList(),
            CallableMemberDescriptor.Kind.DECLARATION);

    List<ValueParameterDescriptor> valueParameterDescriptors =
        createValueParameterDescriptors(
            context, functionLiteral,
            functionDescriptor, functionTypeExpected);

    JetType effectiveReceiverType;
    if (receiverTypeRef == null) {
      if (functionTypeExpected) {
        effectiveReceiverType = KotlinBuiltIns.getInstance().getReceiverType(context.expectedType);
      } else {
        effectiveReceiverType = null;
      }
    } else {
      effectiveReceiverType =
          context
              .expressionTypingServices
              .getTypeResolver()
              .resolveType(context.scope, receiverTypeRef, context.trace, true);
    }
    functionDescriptor.initialize(
        effectiveReceiverType,
        ReceiverParameterDescriptor.NO_RECEIVER_PARAMETER,
        Collections.<TypeParameterDescriptorImpl>emptyList(),
        valueParameterDescriptors,
        /*unsubstitutedReturnType = */ null,
        Modality.FINAL,
        Visibilities.LOCAL);
    BindingContextUtils.recordFunctionDeclarationToDescriptor(
        context.trace, functionLiteral, functionDescriptor);
    return functionDescriptor;
  }