Ejemplo n.º 1
0
    private void reportCyclicInheritanceHierarchyError(
        @NotNull BindingTrace trace,
        @NotNull ClassDescriptor classDescriptor,
        @NotNull ClassDescriptor superclass) {
      PsiElement psiElement = DescriptorToSourceUtils.getSourceFromDescriptor(classDescriptor);

      PsiElement elementToMark = null;
      if (psiElement instanceof KtClassOrObject) {
        KtClassOrObject classOrObject = (KtClassOrObject) psiElement;
        for (KtSuperTypeListEntry delegationSpecifier : classOrObject.getSuperTypeListEntries()) {
          KtTypeReference typeReference = delegationSpecifier.getTypeReference();
          if (typeReference == null) continue;
          KotlinType 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));
      }
    }
Ejemplo n.º 2
0
 public static boolean isDirectSubclass(
     @NotNull ClassDescriptor subClass, @NotNull ClassDescriptor superClass) {
   for (KotlinType superType : subClass.getTypeConstructor().getSupertypes()) {
     if (isSameClass(superType, superClass.getOriginal())) {
       return true;
     }
   }
   return false;
 }
Ejemplo n.º 3
0
 @Nullable
 public static ClassDescriptor getSuperClassDescriptor(@NotNull ClassDescriptor classDescriptor) {
   Collection<KotlinType> superclassTypes = classDescriptor.getTypeConstructor().getSupertypes();
   for (KotlinType type : superclassTypes) {
     ClassDescriptor superClassDescriptor = getClassDescriptorForType(type);
     if (superClassDescriptor.getKind() != ClassKind.INTERFACE) {
       return superClassDescriptor;
     }
   }
   return null;
 }
Ejemplo n.º 4
0
 @NotNull
 public static KotlinType getSuperClassType(@NotNull ClassDescriptor classDescriptor) {
   Collection<KotlinType> superclassTypes = classDescriptor.getTypeConstructor().getSupertypes();
   for (KotlinType type : superclassTypes) {
     ClassDescriptor superClassDescriptor = getClassDescriptorForType(type);
     if (superClassDescriptor.getKind() != ClassKind.INTERFACE) {
       return type;
     }
   }
   return getBuiltIns(classDescriptor).getAnyType();
 }
Ejemplo n.º 5
0
 @NotNull
 public static List<ClassDescriptor> getSuperclassDescriptors(
     @NotNull ClassDescriptor classDescriptor) {
   Collection<KotlinType> superclassTypes = classDescriptor.getTypeConstructor().getSupertypes();
   List<ClassDescriptor> superClassDescriptors = new ArrayList<ClassDescriptor>();
   for (KotlinType type : superclassTypes) {
     ClassDescriptor result = getClassDescriptorForType(type);
     if (!isAny(result)) {
       superClassDescriptors.add(result);
     }
   }
   return superClassDescriptors;
 }
Ejemplo n.º 6
0
 @NotNull
 public static KotlinType makeUnsubstitutedType(
     ClassDescriptor classDescriptor, MemberScope unsubstitutedMemberScope) {
   if (ErrorUtils.isError(classDescriptor)) {
     return ErrorUtils.createErrorType("Unsubstituted type for " + classDescriptor);
   }
   TypeConstructor typeConstructor = classDescriptor.getTypeConstructor();
   List<TypeProjection> arguments = getDefaultTypeProjections(typeConstructor.getParameters());
   return KotlinTypeImpl.create(
       Annotations.Companion.getEMPTY(),
       typeConstructor,
       false,
       arguments,
       unsubstitutedMemberScope);
 }
Ejemplo n.º 7
0
  @NotNull
  public JetType getFunctionType(
      @NotNull Annotations annotations,
      @Nullable JetType receiverType,
      @NotNull List<JetType> parameterTypes,
      @NotNull JetType returnType) {
    List<TypeProjection> arguments =
        getFunctionTypeArgumentProjections(receiverType, parameterTypes, returnType);
    int size = parameterTypes.size();
    ClassDescriptor classDescriptor =
        receiverType == null ? getFunction(size) : getExtensionFunction(size);
    TypeConstructor constructor = classDescriptor.getTypeConstructor();

    return new JetTypeImpl(
        annotations, constructor, false, arguments, classDescriptor.getMemberScope(arguments));
  }
Ejemplo n.º 8
0
  public ClosureCodegen(
      @NotNull GenerationState state,
      @NotNull JetElement element,
      @Nullable SamType samType,
      @NotNull ClosureContext context,
      @NotNull KotlinSyntheticClass.Kind syntheticClassKind,
      @NotNull FunctionGenerationStrategy strategy,
      @NotNull MemberCodegen<?> parentCodegen,
      @NotNull ClassBuilder classBuilder) {
    super(state, parentCodegen, context, element, classBuilder);

    this.funDescriptor = context.getFunctionDescriptor();
    this.classDescriptor = context.getContextDescriptor();
    this.samType = samType;
    this.syntheticClassKind = syntheticClassKind;
    this.strategy = strategy;

    if (samType == null) {
      this.superInterfaceTypes = new ArrayList<JetType>();

      JetType superClassType = null;
      for (JetType supertype : classDescriptor.getTypeConstructor().getSupertypes()) {
        ClassifierDescriptor classifier = supertype.getConstructor().getDeclarationDescriptor();
        if (DescriptorUtils.isTrait(classifier)) {
          superInterfaceTypes.add(supertype);
        } else {
          assert superClassType == null
              : "Closure class can't have more than one superclass: " + funDescriptor;
          superClassType = supertype;
        }
      }
      assert superClassType != null : "Closure class should have a superclass: " + funDescriptor;

      this.superClassType = superClassType;
    } else {
      this.superInterfaceTypes = Collections.singletonList(samType.getType());
      this.superClassType = getBuiltIns(funDescriptor).getAnyType();
    }

    this.closure = bindingContext.get(CLOSURE, classDescriptor);
    assert closure != null : "Closure must be calculated for class: " + classDescriptor;

    this.asmType = typeMapper.mapClass(classDescriptor);

    visibilityFlag = AsmUtil.getVisibilityAccessFlagForAnonymous(classDescriptor);
  }
Ejemplo n.º 9
0
  @NotNull
  public static KotlinType substituteProjectionsForParameters(
      @NotNull ClassDescriptor clazz, @NotNull List<TypeProjection> projections) {
    List<TypeParameterDescriptor> clazzTypeParameters = clazz.getTypeConstructor().getParameters();
    if (clazzTypeParameters.size() != projections.size()) {
      throw new IllegalArgumentException(
          "type parameter counts do not match: " + clazz + ", " + projections);
    }

    Map<TypeConstructor, TypeProjection> substitutions =
        org.jetbrains.kotlin.utils.CollectionsKt.newHashMapWithExpectedSize(
            clazzTypeParameters.size());

    for (int i = 0; i < clazzTypeParameters.size(); ++i) {
      TypeConstructor typeConstructor = clazzTypeParameters.get(i).getTypeConstructor();
      substitutions.put(typeConstructor, projections.get(i));
    }

    return TypeSubstitutor.create(substitutions)
        .substitute(clazz.getDefaultType(), Variance.INVARIANT);
  }
Ejemplo n.º 10
0
 // Returns a set of enum or sealed types of which supertypeOwner is an entry or a member
 @NotNull
 private static Set<TypeConstructor> getAllowedFinalSupertypes(
     @NotNull ClassDescriptor descriptor, @NotNull JetClassOrObject jetClass) {
   Set<TypeConstructor> parentEnumOrSealed;
   if (jetClass instanceof JetEnumEntry) {
     parentEnumOrSealed =
         Collections.singleton(
             ((ClassDescriptor) descriptor.getContainingDeclaration()).getTypeConstructor());
   } else {
     parentEnumOrSealed = Collections.emptySet();
     ClassDescriptor currentDescriptor = descriptor;
     while (currentDescriptor.getContainingDeclaration() instanceof ClassDescriptor) {
       currentDescriptor = (ClassDescriptor) currentDescriptor.getContainingDeclaration();
       if (currentDescriptor.getModality() == Modality.SEALED) {
         if (parentEnumOrSealed.isEmpty()) {
           parentEnumOrSealed = new HashSet<TypeConstructor>();
         }
         parentEnumOrSealed.add(currentDescriptor.getTypeConstructor());
       }
     }
   }
   return parentEnumOrSealed;
 }