001 /*
002 * Copyright 2010-2014 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.jet.lang.types;
018
019 import org.jetbrains.annotations.NotNull;
020 import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor;
021 import org.jetbrains.jet.lang.descriptors.TypeParameterDescriptor;
022 import org.jetbrains.jet.lang.descriptors.impl.TypeParameterDescriptorImpl;
023
024 import java.util.HashMap;
025 import java.util.List;
026 import java.util.Map;
027
028 public class DescriptorSubstitutor {
029 private DescriptorSubstitutor() {
030 }
031
032 @NotNull
033 public static TypeSubstitutor substituteTypeParameters(
034 @NotNull List<TypeParameterDescriptor> typeParameters,
035 @NotNull final TypeSubstitutor originalSubstitutor,
036 @NotNull DeclarationDescriptor newContainingDeclaration,
037 @NotNull List<TypeParameterDescriptor> result
038 ) {
039 final Map<TypeConstructor, TypeProjection> mutableSubstitution = new HashMap<TypeConstructor, TypeProjection>();
040 TypeSubstitutor substitutor = TypeSubstitutor.create(new TypeSubstitution() {
041 @Override
042 public TypeProjection get(TypeConstructor key) {
043 if (originalSubstitutor.inRange(key)) {
044 return originalSubstitutor.getSubstitution().get(key);
045 }
046 return mutableSubstitution.get(key);
047 }
048
049 @Override
050 public boolean isEmpty() {
051 return originalSubstitutor.isEmpty() && mutableSubstitution.isEmpty();
052 }
053
054 @Override
055 public String toString() {
056 return "DescriptorSubstitutor.substituteTypeParameters(" + mutableSubstitution + " / " + originalSubstitutor.getSubstitution() + ")";
057 }
058 });
059
060 Map<TypeParameterDescriptor, TypeParameterDescriptorImpl> substitutedMap =
061 new HashMap<TypeParameterDescriptor, TypeParameterDescriptorImpl>();
062 for (TypeParameterDescriptor descriptor : typeParameters) {
063 TypeParameterDescriptorImpl substituted = TypeParameterDescriptorImpl.createForFurtherModification(
064 newContainingDeclaration,
065 descriptor.getAnnotations(),
066 descriptor.isReified(),
067 descriptor.getVariance(),
068 descriptor.getName(),
069 descriptor.getIndex()
070 );
071 substituted.setInitialized();
072
073 mutableSubstitution.put(descriptor.getTypeConstructor(), new TypeProjectionImpl(substituted.getDefaultType()));
074
075 substitutedMap.put(descriptor, substituted);
076 result.add(substituted);
077 }
078
079 for (TypeParameterDescriptor descriptor : typeParameters) {
080 TypeParameterDescriptorImpl substituted = substitutedMap.get(descriptor);
081 for (JetType upperBound : descriptor.getUpperBounds()) {
082 substituted.getUpperBounds().add(substitutor.substitute(upperBound, Variance.INVARIANT));
083 }
084 }
085
086 return substitutor;
087 }
088 }