Search in sources :

Example 1 with TypeParameterDescriptorImpl

use of org.jetbrains.kotlin.descriptors.impl.TypeParameterDescriptorImpl in project kotlin by JetBrains.

the class SingleAbstractMethodUtils method recreateAndInitializeTypeParameters.

@NotNull
private static TypeParameters recreateAndInitializeTypeParameters(@NotNull List<TypeParameterDescriptor> originalParameters, @Nullable DeclarationDescriptor newOwner) {
    if (newOwner instanceof SamAdapterClassConstructorDescriptor) {
        return new TypeParameters(originalParameters, TypeSubstitutor.EMPTY);
    }
    Map<TypeParameterDescriptor, TypeParameterDescriptorImpl> traitToFunTypeParameters = JavaResolverUtils.recreateTypeParametersAndReturnMapping(originalParameters, newOwner);
    TypeSubstitutor typeParametersSubstitutor = JavaResolverUtils.createSubstitutorForTypeParameters(traitToFunTypeParameters);
    for (Map.Entry<TypeParameterDescriptor, TypeParameterDescriptorImpl> mapEntry : traitToFunTypeParameters.entrySet()) {
        TypeParameterDescriptor traitTypeParameter = mapEntry.getKey();
        TypeParameterDescriptorImpl funTypeParameter = mapEntry.getValue();
        for (KotlinType upperBound : traitTypeParameter.getUpperBounds()) {
            KotlinType upperBoundSubstituted = typeParametersSubstitutor.substitute(upperBound, Variance.INVARIANT);
            assert upperBoundSubstituted != null : "couldn't substitute type: " + upperBound + ", substitutor = " + typeParametersSubstitutor;
            funTypeParameter.addUpperBound(upperBoundSubstituted);
        }
        funTypeParameter.setInitialized();
    }
    List<TypeParameterDescriptor> typeParameters = new ArrayList<TypeParameterDescriptor>(traitToFunTypeParameters.values());
    return new TypeParameters(typeParameters, typeParametersSubstitutor);
}
Also used : TypeParameterDescriptorImpl(org.jetbrains.kotlin.descriptors.impl.TypeParameterDescriptorImpl) ArrayList(java.util.ArrayList) Map(java.util.Map) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with TypeParameterDescriptorImpl

use of org.jetbrains.kotlin.descriptors.impl.TypeParameterDescriptorImpl in project kotlin by JetBrains.

the class DescriptorSubstitutor method substituteTypeParameters.

@NotNull
public static TypeSubstitutor substituteTypeParameters(@ReadOnly @NotNull List<TypeParameterDescriptor> typeParameters, @NotNull TypeSubstitution originalSubstitution, @NotNull DeclarationDescriptor newContainingDeclaration, @NotNull @Mutable List<TypeParameterDescriptor> result) {
    Map<TypeConstructor, TypeProjection> mutableSubstitution = new HashMap<TypeConstructor, TypeProjection>();
    Map<TypeParameterDescriptor, TypeParameterDescriptorImpl> substitutedMap = new HashMap<TypeParameterDescriptor, TypeParameterDescriptorImpl>();
    int index = 0;
    for (TypeParameterDescriptor descriptor : typeParameters) {
        TypeParameterDescriptorImpl substituted = TypeParameterDescriptorImpl.createForFurtherModification(newContainingDeclaration, descriptor.getAnnotations(), descriptor.isReified(), descriptor.getVariance(), descriptor.getName(), index++, SourceElement.NO_SOURCE);
        mutableSubstitution.put(descriptor.getTypeConstructor(), new TypeProjectionImpl(substituted.getDefaultType()));
        substitutedMap.put(descriptor, substituted);
        result.add(substituted);
    }
    TypeSubstitutor substitutor = TypeSubstitutor.createChainedSubstitutor(originalSubstitution, TypeConstructorSubstitution.createByConstructorsMap(mutableSubstitution));
    for (TypeParameterDescriptor descriptor : typeParameters) {
        TypeParameterDescriptorImpl substituted = substitutedMap.get(descriptor);
        for (KotlinType upperBound : descriptor.getUpperBounds()) {
            KotlinType substitutedBound = substitutor.substitute(upperBound, Variance.IN_VARIANCE);
            assert substitutedBound != null : "Upper bound failed to substitute: " + descriptor;
            substituted.addUpperBound(substitutedBound);
        }
        substituted.setInitialized();
    }
    return substitutor;
}
Also used : TypeParameterDescriptor(org.jetbrains.kotlin.descriptors.TypeParameterDescriptor) HashMap(java.util.HashMap) TypeParameterDescriptorImpl(org.jetbrains.kotlin.descriptors.impl.TypeParameterDescriptorImpl) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

NotNull (org.jetbrains.annotations.NotNull)2 TypeParameterDescriptorImpl (org.jetbrains.kotlin.descriptors.impl.TypeParameterDescriptorImpl)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 TypeParameterDescriptor (org.jetbrains.kotlin.descriptors.TypeParameterDescriptor)1