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;
}
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);
}
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));
}
}
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;
}
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);
}
}
Aggregations