Search in sources :

Example 1 with TypeParameterDescriptor

use of org.jetbrains.kotlin.descriptors.TypeParameterDescriptor in project kotlin by JetBrains.

the class BoundsSubstitutor method createUpperBoundsSubstitutor.

@NotNull
private static TypeSubstitutor createUpperBoundsSubstitutor(@NotNull List<TypeParameterDescriptor> typeParameters) {
    Map<TypeConstructor, TypeProjection> mutableSubstitution = new HashMap<TypeConstructor, TypeProjection>();
    TypeSubstitutor substitutor = TypeSubstitutor.create(mutableSubstitution);
    // todo assert: no loops
    for (TypeParameterDescriptor descriptor : topologicallySortTypeParameters(typeParameters)) {
        KotlinType upperBoundsAsType = TypeIntersector.getUpperBoundsAsType(descriptor);
        KotlinType substitutedUpperBoundsAsType = substitutor.substitute(upperBoundsAsType, Variance.INVARIANT);
        mutableSubstitution.put(descriptor.getTypeConstructor(), new TypeProjectionImpl(substitutedUpperBoundsAsType));
    }
    return substitutor;
}
Also used : TypeParameterDescriptor(org.jetbrains.kotlin.descriptors.TypeParameterDescriptor) HashMap(java.util.HashMap) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with TypeParameterDescriptor

use of org.jetbrains.kotlin.descriptors.TypeParameterDescriptor in project kotlin by JetBrains.

the class CommonSupertypes method computeSupertypeProjections.

// constructor - type constructor of a supertype to be instantiated
// types - instantiations of constructor occurring as supertypes of classes we are trying to intersect
@NotNull
private static SimpleType computeSupertypeProjections(@NotNull TypeConstructor constructor, @NotNull Set<SimpleType> types, int recursionDepth, int maxDepth) {
    assert !types.isEmpty();
    if (types.size() == 1) {
        return types.iterator().next();
    }
    List<TypeParameterDescriptor> parameters = constructor.getParameters();
    List<TypeProjection> newProjections = new ArrayList<TypeProjection>(parameters.size());
    for (TypeParameterDescriptor parameterDescriptor : parameters) {
        Set<TypeProjection> typeProjections = new HashSet<TypeProjection>();
        for (KotlinType type : types) {
            typeProjections.add(type.getArguments().get(parameterDescriptor.getIndex()));
        }
        newProjections.add(computeSupertypeProjection(parameterDescriptor, typeProjections, recursionDepth, maxDepth));
    }
    boolean nullable = false;
    for (KotlinType type : types) {
        nullable |= type.isMarkedNullable();
    }
    ClassifierDescriptor classifier = constructor.getDeclarationDescriptor();
    MemberScope newScope;
    if (classifier instanceof ClassDescriptor) {
        newScope = ((ClassDescriptor) classifier).getMemberScope(newProjections);
    } else if (classifier instanceof TypeParameterDescriptor) {
        newScope = classifier.getDefaultType().getMemberScope();
    } else {
        newScope = ErrorUtils.createErrorScope("A scope for common supertype which is not a normal classifier", true);
    }
    return KotlinTypeFactory.simpleType(Annotations.Companion.getEMPTY(), constructor, newProjections, nullable, newScope);
}
Also used : TypeParameterDescriptor(org.jetbrains.kotlin.descriptors.TypeParameterDescriptor) ClassDescriptor(org.jetbrains.kotlin.descriptors.ClassDescriptor) ClassifierDescriptor(org.jetbrains.kotlin.descriptors.ClassifierDescriptor) MemberScope(org.jetbrains.kotlin.resolve.scopes.MemberScope) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with TypeParameterDescriptor

use of org.jetbrains.kotlin.descriptors.TypeParameterDescriptor in project kotlin by JetBrains.

the class ResolvedCallImpl method setResultingSubstitutor.

@Override
public void setResultingSubstitutor(@NotNull TypeSubstitutor substitutor) {
    resultingDescriptor = (D) candidateDescriptor.substitute(substitutor);
    assert resultingDescriptor != null : candidateDescriptor;
    for (TypeParameterDescriptor typeParameter : candidateDescriptor.getTypeParameters()) {
        TypeProjection typeArgumentProjection = substitutor.getSubstitution().get(typeParameter.getDefaultType());
        if (typeArgumentProjection != null) {
            typeArguments.put(typeParameter, typeArgumentProjection.getType());
        }
    }
    if (candidateDescriptor.getValueParameters().isEmpty())
        return;
    List<ValueParameterDescriptor> substitutedParameters = resultingDescriptor.getValueParameters();
    Collection<Map.Entry<ValueParameterDescriptor, ResolvedValueArgument>> valueArgumentsBeforeSubstitution = new SmartList<Map.Entry<ValueParameterDescriptor, ResolvedValueArgument>>(valueArguments.entrySet());
    valueArguments.clear();
    for (Map.Entry<ValueParameterDescriptor, ResolvedValueArgument> entry : valueArgumentsBeforeSubstitution) {
        ValueParameterDescriptor substitutedVersion = substitutedParameters.get(entry.getKey().getIndex());
        assert substitutedVersion != null : entry.getKey();
        valueArguments.put(substitutedVersion, entry.getValue());
    }
    Collection<Map.Entry<ValueArgument, ArgumentMatchImpl>> unsubstitutedArgumentMappings = new SmartList<Map.Entry<ValueArgument, ArgumentMatchImpl>>(argumentToParameterMap.entrySet());
    argumentToParameterMap.clear();
    for (Map.Entry<ValueArgument, ArgumentMatchImpl> entry : unsubstitutedArgumentMappings) {
        ArgumentMatchImpl argumentMatch = entry.getValue();
        ValueParameterDescriptor valueParameterDescriptor = argumentMatch.getValueParameter();
        ValueParameterDescriptor substitutedVersion = substitutedParameters.get(valueParameterDescriptor.getIndex());
        assert substitutedVersion != null : valueParameterDescriptor;
        argumentToParameterMap.put(entry.getKey(), argumentMatch.replaceValueParameter(substitutedVersion));
    }
}
Also used : TypeParameterDescriptor(org.jetbrains.kotlin.descriptors.TypeParameterDescriptor) TypeProjection(org.jetbrains.kotlin.types.TypeProjection) ValueArgument(org.jetbrains.kotlin.psi.ValueArgument) ValueParameterDescriptor(org.jetbrains.kotlin.descriptors.ValueParameterDescriptor) SmartList(com.intellij.util.SmartList)

Example 4 with TypeParameterDescriptor

use of org.jetbrains.kotlin.descriptors.TypeParameterDescriptor in project kotlin by JetBrains.

the class ConstraintsUtil method getSubstitutorsForConflictingParameters.

@NotNull
public static Collection<TypeSubstitutor> getSubstitutorsForConflictingParameters(@NotNull ConstraintSystem constraintSystem) {
    TypeVariable firstConflictingVariable = getFirstConflictingVariable(constraintSystem);
    if (firstConflictingVariable == null)
        return Collections.emptyList();
    TypeParameterDescriptor firstConflictingParameter = firstConflictingVariable.getOriginalTypeParameter();
    Collection<KotlinType> conflictingTypes = constraintSystem.getTypeBounds(firstConflictingVariable).getValues();
    List<Map<TypeConstructor, TypeProjection>> substitutionContexts = Lists.newArrayList();
    for (KotlinType type : conflictingTypes) {
        Map<TypeConstructor, TypeProjection> context = Maps.newLinkedHashMap();
        context.put(firstConflictingParameter.getTypeConstructor(), new TypeProjectionImpl(type));
        substitutionContexts.add(context);
    }
    for (TypeVariable typeVariable : constraintSystem.getTypeVariables()) {
        if (typeVariable == firstConflictingVariable)
            continue;
        KotlinType safeType = getSafeValue(constraintSystem, typeVariable);
        for (Map<TypeConstructor, TypeProjection> context : substitutionContexts) {
            TypeProjection typeProjection = new TypeProjectionImpl(safeType);
            context.put(typeVariable.getOriginalTypeParameter().getTypeConstructor(), typeProjection);
        }
    }
    Collection<TypeSubstitutor> typeSubstitutors = new ArrayList<TypeSubstitutor>(substitutionContexts.size());
    for (Map<TypeConstructor, TypeProjection> context : substitutionContexts) {
        typeSubstitutors.add(TypeSubstitutor.create(context));
    }
    return typeSubstitutors;
}
Also used : TypeParameterDescriptor(org.jetbrains.kotlin.descriptors.TypeParameterDescriptor) NotNull(org.jetbrains.annotations.NotNull)

Example 5 with TypeParameterDescriptor

use of org.jetbrains.kotlin.descriptors.TypeParameterDescriptor in project kotlin by JetBrains.

the class SubstitutionUtils method fillInDeepSubstitutor.

// we use the mutability of the substitution map here
private static void fillInDeepSubstitutor(@NotNull KotlinType context, @NotNull TypeSubstitutor substitutor, @NotNull Map<TypeConstructor, TypeProjection> substitution, @Nullable Multimap<TypeParameterDescriptor, TypeProjection> typeParameterMapping) {
    List<TypeParameterDescriptor> parameters = context.getConstructor().getParameters();
    List<TypeProjection> arguments = context.getArguments();
    if (parameters.size() != arguments.size()) {
        throw new IllegalStateException();
    }
    for (int i = 0; i < arguments.size(); i++) {
        TypeProjection argument = arguments.get(i);
        TypeParameterDescriptor parameter = parameters.get(i);
        TypeProjection substitute = substitutor.substitute(argument);
        assert substitute != null;
        substitution.put(parameter.getTypeConstructor(), substitute);
        if (typeParameterMapping != null) {
            typeParameterMapping.put(parameter, substitute);
        }
    }
    if (KotlinBuiltIns.isNothingOrNullableNothing(context))
        return;
    for (KotlinType supertype : context.getConstructor().getSupertypes()) {
        fillInDeepSubstitutor(supertype, substitutor, substitution, typeParameterMapping);
    }
}
Also used : TypeParameterDescriptor(org.jetbrains.kotlin.descriptors.TypeParameterDescriptor)

Aggregations

TypeParameterDescriptor (org.jetbrains.kotlin.descriptors.TypeParameterDescriptor)15 NotNull (org.jetbrains.annotations.NotNull)5 ClassifierDescriptor (org.jetbrains.kotlin.descriptors.ClassifierDescriptor)4 HashMap (java.util.HashMap)2 ClassDescriptor (org.jetbrains.kotlin.descriptors.ClassDescriptor)2 KotlinType (org.jetbrains.kotlin.types.KotlinType)2 PsiElement (com.intellij.psi.PsiElement)1 Function (com.intellij.util.Function)1 SmartList (com.intellij.util.SmartList)1 File (java.io.File)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 Unit (kotlin.Unit)1 AnalysisResult (org.jetbrains.kotlin.analyzer.AnalysisResult)1 KotlinBuiltIns (org.jetbrains.kotlin.builtins.KotlinBuiltIns)1 ModuleDescriptor (org.jetbrains.kotlin.descriptors.ModuleDescriptor)1 ValueParameterDescriptor (org.jetbrains.kotlin.descriptors.ValueParameterDescriptor)1 TypeParameterDescriptorImpl (org.jetbrains.kotlin.descriptors.impl.TypeParameterDescriptorImpl)1 Name (org.jetbrains.kotlin.name.Name)1 KtFile (org.jetbrains.kotlin.psi.KtFile)1