@NotNull private JetType modifyTypeAccordingToSuperMethods( @NotNull JetType autoType, @NotNull List<TypeAndVariance> typesFromSuper, @NotNull TypeUsage howThisTypeIsUsed) { if (ErrorUtils.isErrorType(autoType)) { return autoType; } boolean resultNullable = typeMustBeNullable(autoType, typesFromSuper, howThisTypeIsUsed); ClassifierDescriptor resultClassifier = modifyTypeClassifier(autoType, typesFromSuper); List<TypeProjection> resultArguments = getTypeArgsOfType(autoType, resultClassifier, typesFromSuper); JetScope resultScope; if (resultClassifier instanceof ClassDescriptor) { resultScope = ((ClassDescriptor) resultClassifier).getMemberScope(resultArguments); } else { resultScope = autoType.getMemberScope(); } JetTypeImpl type = new JetTypeImpl( autoType.getAnnotations(), resultClassifier.getTypeConstructor(), resultNullable, resultArguments, resultScope); PropagationHeuristics.checkArrayInReturnType(this, type, typesFromSuper); return type; }
@NotNull private static Multimap<FqName, Pair<FunctionDescriptor, PsiMethod>> getSuperclassToFunctionsMultimap( @NotNull PsiMethodWrapper method, @NotNull BindingContext bindingContext, @NotNull ClassDescriptor containingClass) { Multimap<FqName, Pair<FunctionDescriptor, PsiMethod>> result = HashMultimap.create(); Name functionName = Name.identifier(method.getName()); int parameterCount = method.getParameters().size(); for (JetType supertype : TypeUtils.getAllSupertypes(containingClass.getDefaultType())) { ClassifierDescriptor klass = supertype.getConstructor().getDeclarationDescriptor(); assert klass != null; FqName fqName = DescriptorUtils.getFQName(klass).toSafe(); for (FunctionDescriptor fun : klass.getDefaultType().getMemberScope().getFunctions(functionName)) { if (fun.getKind().isReal() && fun.getValueParameters().size() == parameterCount) { PsiElement declaration = BindingContextUtils.descriptorToDeclaration(bindingContext, fun); if (declaration instanceof PsiMethod) { result.put(fqName, Pair.create(fun, (PsiMethod) declaration)); } // else declaration is null or JetNamedFunction: both cases are processed later } } } return result; }
private static Map<ClassDescriptor, JetType> getSuperclassToSupertypeMap( ClassDescriptor containingClass) { Map<ClassDescriptor, JetType> superclassToSupertype = Maps.newHashMap(); for (JetType supertype : TypeUtils.getAllSupertypes(containingClass.getDefaultType())) { ClassifierDescriptor superclass = supertype.getConstructor().getDeclarationDescriptor(); assert superclass instanceof ClassDescriptor; superclassToSupertype.put((ClassDescriptor) superclass, supertype); } return superclassToSupertype; }
@NotNull private ClassifierDescriptor modifyTypeClassifier( @NotNull JetType autoType, @NotNull List<TypeAndVariance> typesFromSuper) { ClassifierDescriptor classifier = autoType.getConstructor().getDeclarationDescriptor(); if (!(classifier instanceof ClassDescriptor)) { assert classifier != null : "no declaration descriptor for type " + autoType; if (classifier instanceof TypeParameterDescriptor && autoTypeParameterToModified.containsKey(classifier)) { return autoTypeParameterToModified.get(classifier); } return classifier; } ClassDescriptor klass = (ClassDescriptor) classifier; CollectionClassMapping collectionMapping = CollectionClassMapping.getInstance(); boolean someSupersMutable = false; boolean someSupersCovariantReadOnly = false; boolean someSupersNotCovariantReadOnly = false; for (TypeAndVariance typeFromSuper : typesFromSuper) { ClassifierDescriptor classifierFromSuper = typeFromSuper.type.getConstructor().getDeclarationDescriptor(); if (classifierFromSuper instanceof ClassDescriptor) { ClassDescriptor classFromSuper = (ClassDescriptor) classifierFromSuper; if (collectionMapping.isMutableCollection(classFromSuper)) { someSupersMutable = true; } else if (collectionMapping.isReadOnlyCollection(classFromSuper)) { if (typeFromSuper.varianceOfPosition == Variance.OUT_VARIANCE) { someSupersCovariantReadOnly = true; } else { someSupersNotCovariantReadOnly = true; } } } } if (someSupersMutable && someSupersNotCovariantReadOnly) { reportError("Incompatible types in superclasses: " + typesFromSuper); return classifier; } else if (someSupersMutable) { if (collectionMapping.isReadOnlyCollection(klass)) { return collectionMapping.convertReadOnlyToMutable(klass); } } else if (someSupersNotCovariantReadOnly || someSupersCovariantReadOnly) { if (collectionMapping.isMutableCollection(klass)) { return collectionMapping.convertMutableToReadOnly(klass); } } ClassifierDescriptor fixed = PropagationHeuristics.tryToFixOverridingTWithRawType(this, typesFromSuper); return fixed != null ? fixed : classifier; }
private boolean typeMustBeNullable( @NotNull JetType autoType, @NotNull List<TypeAndVariance> typesFromSuper, @NotNull TypeUsage howThisTypeIsUsed) { boolean someSupersNotCovariantNullable = false; boolean someSupersCovariantNullable = false; boolean someSupersNotNull = false; for (TypeAndVariance typeFromSuper : typesFromSuper) { if (!typeFromSuper.type.isNullable()) { someSupersNotNull = true; } else { if (typeFromSuper.varianceOfPosition == Variance.OUT_VARIANCE) { someSupersCovariantNullable = true; } else { someSupersNotCovariantNullable = true; } } } if (someSupersNotNull && someSupersNotCovariantNullable) { reportError("Incompatible types in superclasses: " + typesFromSuper); return autoType.isNullable(); } else if (someSupersNotNull) { return false; } else if (someSupersNotCovariantNullable || someSupersCovariantNullable) { boolean annotatedAsNotNull = howThisTypeIsUsed != TYPE_ARGUMENT && !autoType.isNullable(); if (annotatedAsNotNull && someSupersNotCovariantNullable) { reportError( "In superclass type is nullable: " + typesFromSuper + ", in subclass it is not: " + autoType); return true; } return !annotatedAsNotNull; } return autoType.isNullable(); }
@NotNull private List<TypeProjection> getTypeArgsOfType( @NotNull JetType autoType, @NotNull ClassifierDescriptor classifier, @NotNull List<TypeAndVariance> typesFromSuper) { List<TypeProjection> autoArguments = autoType.getArguments(); if (!(classifier instanceof ClassDescriptor)) { assert autoArguments.isEmpty() : "Unexpected type arguments when type constructor is not ClassDescriptor, type = " + autoType; return autoArguments; } List<List<TypeProjectionAndVariance>> typeArgumentsFromSuper = calculateTypeArgumentsFromSuper((ClassDescriptor) classifier, typesFromSuper); // Modify type arguments using info from typesFromSuper List<TypeProjection> resultArguments = Lists.newArrayList(); for (TypeParameterDescriptor parameter : classifier.getTypeConstructor().getParameters()) { TypeProjection argument = autoArguments.get(parameter.getIndex()); JetType argumentType = argument.getType(); List<TypeProjectionAndVariance> projectionsFromSuper = typeArgumentsFromSuper.get(parameter.getIndex()); List<TypeAndVariance> argTypesFromSuper = getTypes(projectionsFromSuper); JetType type = modifyTypeAccordingToSuperMethods(argumentType, argTypesFromSuper, TYPE_ARGUMENT); Variance projectionKind = calculateArgumentProjectionKindFromSuper(argument, projectionsFromSuper); resultArguments.add(new TypeProjection(projectionKind, type)); } return resultArguments; }
public String toString() { return type.toString(); }