001 /*
002 * Copyright 2010-2015 JetBrains s.r.o.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017 package org.jetbrains.kotlin.types;
018
019 import org.jetbrains.annotations.Mutable;
020 import org.jetbrains.annotations.NotNull;
021 import org.jetbrains.kotlin.descriptors.DeclarationDescriptor;
022 import org.jetbrains.kotlin.descriptors.SourceElement;
023 import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor;
024 import org.jetbrains.kotlin.descriptors.impl.TypeParameterDescriptorImpl;
025
026 import java.util.HashMap;
027 import java.util.List;
028 import java.util.Map;
029
030 public class DescriptorSubstitutor {
031 private DescriptorSubstitutor() {
032 }
033
034 @NotNull
035 public static TypeSubstitutor substituteTypeParameters(
036 @NotNull List<TypeParameterDescriptor> typeParameters,
037 @NotNull final TypeSubstitutor originalSubstitutor,
038 @NotNull DeclarationDescriptor newContainingDeclaration,
039 @NotNull @Mutable List<TypeParameterDescriptor> result
040 ) {
041 final Map<TypeConstructor, TypeProjection> mutableSubstitution = new HashMap<TypeConstructor, TypeProjection>();
042 TypeSubstitutor substitutor = TypeSubstitutor.create(new TypeSubstitution() {
043 @Override
044 public TypeProjection get(TypeConstructor key) {
045 if (originalSubstitutor.inRange(key)) {
046 return originalSubstitutor.getSubstitution().get(key);
047 }
048 return mutableSubstitution.get(key);
049 }
050
051 @Override
052 public boolean isEmpty() {
053 return originalSubstitutor.isEmpty() && mutableSubstitution.isEmpty();
054 }
055
056 @Override
057 public boolean approximateCapturedTypes() {
058 return originalSubstitutor.getSubstitution().approximateCapturedTypes();
059 }
060
061 @Override
062 public String toString() {
063 return "DescriptorSubstitutor.substituteTypeParameters(" + mutableSubstitution + " / " + originalSubstitutor.getSubstitution() + ")";
064 }
065 });
066
067 Map<TypeParameterDescriptor, TypeParameterDescriptorImpl> substitutedMap =
068 new HashMap<TypeParameterDescriptor, TypeParameterDescriptorImpl>();
069 for (TypeParameterDescriptor descriptor : typeParameters) {
070 TypeParameterDescriptorImpl substituted = TypeParameterDescriptorImpl.createForFurtherModification(
071 newContainingDeclaration,
072 descriptor.getAnnotations(),
073 descriptor.isReified(),
074 descriptor.getVariance(),
075 descriptor.getName(),
076 descriptor.getIndex(),
077 SourceElement.NO_SOURCE
078 );
079 substituted.setInitialized();
080
081 mutableSubstitution.put(descriptor.getTypeConstructor(), new TypeProjectionImpl(substituted.getDefaultType()));
082
083 substitutedMap.put(descriptor, substituted);
084 result.add(substituted);
085 }
086
087 for (TypeParameterDescriptor descriptor : typeParameters) {
088 TypeParameterDescriptorImpl substituted = substitutedMap.get(descriptor);
089 for (JetType upperBound : descriptor.getUpperBounds()) {
090 substituted.getUpperBounds().add(substitutor.substitute(upperBound, Variance.INVARIANT));
091 }
092 }
093
094 return substitutor;
095 }
096 }