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 com.google.common.base.Function;
020    import com.google.common.collect.Collections2;
021    import org.jetbrains.annotations.NotNull;
022    import org.jetbrains.annotations.Nullable;
023    import org.jetbrains.kotlin.descriptors.CallableDescriptor;
024    import org.jetbrains.kotlin.descriptors.ClassifierDescriptor;
025    import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor;
026    import org.jetbrains.kotlin.utils.DFS;
027    
028    import java.util.Collections;
029    import java.util.HashMap;
030    import java.util.List;
031    import java.util.Map;
032    
033    public class BoundsSubstitutor {
034        private static final Function<TypeProjection,KotlinType> PROJECTIONS_TO_TYPES = new Function<TypeProjection, KotlinType>() {
035            @Override
036            public KotlinType apply(TypeProjection projection) {
037                return projection.getType();
038            }
039        };
040    
041        private BoundsSubstitutor() {
042        }
043    
044        @NotNull
045        public static <D extends CallableDescriptor> D substituteBounds(@NotNull D functionDescriptor) {
046            List<TypeParameterDescriptor> typeParameters = functionDescriptor.getTypeParameters();
047            if (typeParameters.isEmpty()) return functionDescriptor;
048    
049            // TODO: this does not handle any recursion in the bounds
050            @SuppressWarnings("unchecked")
051            D substitutedFunction = (D) functionDescriptor.substitute(createUpperBoundsSubstitutor(typeParameters));
052            assert substitutedFunction != null : "Substituting upper bounds should always be legal";
053    
054            return substitutedFunction;
055        }
056    
057        @NotNull
058        public static <D extends CallableDescriptor> TypeSubstitutor createUpperBoundsSubstitutor(@NotNull D callableDescriptor) {
059            return createUpperBoundsSubstitutor(callableDescriptor.getTypeParameters());
060        }
061    
062        @NotNull
063        private static TypeSubstitutor createUpperBoundsSubstitutor(@NotNull List<TypeParameterDescriptor> typeParameters) {
064            Map<TypeConstructor, TypeProjection> mutableSubstitution = new HashMap<TypeConstructor, TypeProjection>();
065            TypeSubstitutor substitutor = TypeSubstitutor.create(mutableSubstitution);
066    
067            // todo assert: no loops
068            for (TypeParameterDescriptor descriptor : topologicallySortTypeParameters(typeParameters)) {
069                KotlinType upperBoundsAsType = TypeIntersector.getUpperBoundsAsType(descriptor);
070                KotlinType substitutedUpperBoundsAsType = substitutor.substitute(upperBoundsAsType, Variance.INVARIANT);
071                mutableSubstitution.put(descriptor.getTypeConstructor(), new TypeProjectionImpl(substitutedUpperBoundsAsType));
072            }
073    
074            return substitutor;
075        }
076    
077        @NotNull
078        private static List<TypeParameterDescriptor> topologicallySortTypeParameters(@NotNull final List<TypeParameterDescriptor> typeParameters) {
079            // In the end, we want every parameter to have no references to those after it in the list
080            // This gives us the reversed order: the one that refers to everybody else comes first
081            List<TypeParameterDescriptor> topOrder = DFS.topologicalOrder(
082                    typeParameters,
083                    new DFS.Neighbors<TypeParameterDescriptor>() {
084                        @NotNull
085                        @Override
086                        public Iterable<TypeParameterDescriptor> getNeighbors(TypeParameterDescriptor current) {
087                            return getTypeParametersFromUpperBounds(current, typeParameters);
088                        }
089                    });
090    
091            assert topOrder.size() == typeParameters.size() : "All type parameters must be visited, but only " + topOrder + " were";
092    
093            // Now, the one that refers to everybody else stands in the last position
094            Collections.reverse(topOrder);
095            return topOrder;
096        }
097    
098        @NotNull
099        private static List<TypeParameterDescriptor> getTypeParametersFromUpperBounds(
100                @NotNull TypeParameterDescriptor current,
101                @NotNull final List<TypeParameterDescriptor> typeParameters
102        ) {
103            return DFS.dfs(
104                    current.getUpperBounds(),
105                    new DFS.Neighbors<KotlinType>() {
106                        @NotNull
107                        @Override
108                        public Iterable<KotlinType> getNeighbors(KotlinType current) {
109                            return Collections2.transform(current.getArguments(), PROJECTIONS_TO_TYPES);
110                        }
111                    },
112                    new DFS.NodeHandlerWithListResult<KotlinType, TypeParameterDescriptor>() {
113                        @Override
114                        public boolean beforeChildren(KotlinType current) {
115                            ClassifierDescriptor declarationDescriptor = current.getConstructor().getDeclarationDescriptor();
116                            // typeParameters in a list, but it contains very few elements, so it's fine to call contains() on it
117                            //noinspection SuspiciousMethodCalls
118                            if (typeParameters.contains(declarationDescriptor)) {
119                                result.add((TypeParameterDescriptor) declarationDescriptor);
120                            }
121    
122                            return true;
123                        }
124                    }
125            );
126        }
127    }