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.resolve.lazy.descriptors;
018    
019    import com.google.common.collect.Sets;
020    import org.jetbrains.annotations.NotNull;
021    import org.jetbrains.kotlin.descriptors.impl.AbstractLazyTypeParameterDescriptor;
022    import org.jetbrains.kotlin.lexer.KtTokens;
023    import org.jetbrains.kotlin.psi.*;
024    import org.jetbrains.kotlin.resolve.BindingContext;
025    import org.jetbrains.kotlin.resolve.lazy.ForceResolveUtil;
026    import org.jetbrains.kotlin.resolve.lazy.LazyClassContext;
027    import org.jetbrains.kotlin.resolve.lazy.LazyEntity;
028    import org.jetbrains.kotlin.resolve.source.KotlinSourceElementKt;
029    import org.jetbrains.kotlin.types.KotlinType;
030    
031    import java.util.Set;
032    
033    public class LazyTypeParameterDescriptor extends AbstractLazyTypeParameterDescriptor implements LazyEntity {
034        private final LazyClassContext c;
035    
036        private final KtTypeParameter jetTypeParameter;
037    
038        public LazyTypeParameterDescriptor(
039                @NotNull LazyClassContext c,
040                @NotNull LazyClassDescriptor containingDeclaration,
041                @NotNull KtTypeParameter jetTypeParameter,
042                int index) {
043            super(
044                    c.getStorageManager(),
045                    containingDeclaration,
046                    jetTypeParameter.getNameAsSafeName(),
047                    jetTypeParameter.getVariance(),
048                    jetTypeParameter.hasModifier(KtTokens.REIFIED_KEYWORD),
049                    index,
050                    KotlinSourceElementKt.toSourceElement(jetTypeParameter)
051            );
052            this.c = c;
053            this.jetTypeParameter = jetTypeParameter;
054    
055            this.c.getTrace().record(BindingContext.TYPE_PARAMETER, jetTypeParameter, this);
056        }
057    
058        @NotNull
059        @Override
060        protected Set<KotlinType> resolveUpperBounds() {
061            Set<KotlinType> upperBounds = Sets.newLinkedHashSet();
062    
063            KtTypeParameter jetTypeParameter = this.jetTypeParameter;
064    
065            KtTypeReference extendsBound = jetTypeParameter.getExtendsBound();
066            if (extendsBound != null) {
067                KotlinType boundType = c.getDescriptorResolver().resolveTypeParameterExtendsBound(
068                        this, extendsBound, getContainingDeclaration().getScopeForClassHeaderResolution(), c.getTrace());
069                upperBounds.add(boundType);
070            }
071    
072            resolveUpperBoundsFromWhereClause(upperBounds);
073    
074            if (upperBounds.isEmpty()) {
075                upperBounds.add(c.getModuleDescriptor().getBuiltIns().getDefaultBound());
076            }
077    
078            return upperBounds;
079        }
080    
081        private void resolveUpperBoundsFromWhereClause(Set<KotlinType> upperBounds) {
082            KtClassOrObject classOrObject = KtStubbedPsiUtil.getPsiOrStubParent(jetTypeParameter, KtClassOrObject.class, true);
083            if (classOrObject instanceof KtClass) {
084                KtClass ktClass = (KtClass) classOrObject;
085                for (KtTypeConstraint jetTypeConstraint : ktClass.getTypeConstraints()) {
086                    KtSimpleNameExpression constrainedParameterName = jetTypeConstraint.getSubjectTypeParameterName();
087                    if (constrainedParameterName != null) {
088                        if (getName().equals(constrainedParameterName.getReferencedNameAsName())) {
089                            c.getTrace().record(BindingContext.REFERENCE_TARGET, constrainedParameterName, this);
090    
091                            KtTypeReference boundTypeReference = jetTypeConstraint.getBoundTypeReference();
092                            if (boundTypeReference != null) {
093                                KotlinType boundType = resolveBoundType(boundTypeReference);
094                                upperBounds.add(boundType);
095                            }
096                        }
097                    }
098                }
099            }
100    
101        }
102    
103        private KotlinType resolveBoundType(@NotNull KtTypeReference boundTypeReference) {
104            return c.getTypeResolver()
105                        .resolveType(getContainingDeclaration().getScopeForClassHeaderResolution(), boundTypeReference,
106                                     c.getTrace(), false);
107        }
108    
109        @NotNull
110        @Override
111        public LazyClassDescriptor getContainingDeclaration() {
112            return (LazyClassDescriptor) super.getContainingDeclaration();
113        }
114    
115        @Override
116        public void forceResolveAllContents() {
117            ForceResolveUtil.forceResolveAllContents(getAnnotations());
118            getContainingDeclaration();
119            getDefaultType();
120            getIndex();
121            ForceResolveUtil.forceResolveAllContents(getLowerBounds());
122            getOriginal();
123            ForceResolveUtil.forceResolveAllContents(getTypeConstructor());
124            ForceResolveUtil.forceResolveAllContents(getUpperBounds());
125            getUpperBoundsAsType();
126            getVariance();
127        }
128    }