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