001    /*
002     * Copyright 2010-2013 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.resolve.lazy.descriptors;
018    
019    import com.google.common.collect.Sets;
020    import com.intellij.psi.util.PsiTreeUtil;
021    import org.jetbrains.annotations.NotNull;
022    import org.jetbrains.jet.lang.descriptors.TypeParameterDescriptor;
023    import org.jetbrains.jet.lang.psi.*;
024    import org.jetbrains.jet.lang.resolve.BindingContext;
025    import org.jetbrains.jet.lang.resolve.lazy.LazyDescriptor;
026    import org.jetbrains.jet.lang.resolve.lazy.ResolveSession;
027    import org.jetbrains.jet.lang.resolve.lazy.ResolveSessionUtils;
028    import org.jetbrains.jet.lang.types.JetType;
029    import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns;
030    import org.jetbrains.jet.lexer.JetTokens;
031    
032    import java.util.Set;
033    
034    public class LazyTypeParameterDescriptor extends  AbstractLazyTypeParameterDescriptor implements TypeParameterDescriptor, LazyDescriptor {
035        private final ResolveSession resolveSession;
036    
037        private final JetTypeParameter jetTypeParameter;
038    
039        public LazyTypeParameterDescriptor(
040                @NotNull ResolveSession resolveSession,
041                @NotNull LazyClassDescriptor containingDeclaration,
042                @NotNull JetTypeParameter jetTypeParameter,
043                int index) {
044            super(
045                    resolveSession.getStorageManager(),
046                    containingDeclaration,
047                    ResolveSessionUtils.safeNameForLazyResolve(jetTypeParameter.getNameAsName()),
048                    jetTypeParameter.getVariance(),
049                    jetTypeParameter.hasModifier(JetTokens.REIFIED_KEYWORD),
050                    index
051            );
052            this.resolveSession = resolveSession;
053            this.jetTypeParameter = jetTypeParameter;
054    
055            this.resolveSession.getTrace().record(BindingContext.TYPE_PARAMETER, jetTypeParameter, this);
056        }
057    
058        @NotNull
059        @Override
060        protected Set<JetType> resolveUpperBounds() {
061            Set<JetType> upperBounds = Sets.newLinkedHashSet();
062    
063            JetTypeParameter jetTypeParameter = this.jetTypeParameter;
064    
065            resolveUpperBoundsFromWhereClause(upperBounds, false);
066    
067            JetTypeReference extendsBound = jetTypeParameter.getExtendsBound();
068            if (extendsBound != null) {
069                upperBounds.add(resolveBoundType(extendsBound));
070            }
071    
072            if (upperBounds.isEmpty()) {
073                upperBounds.add(KotlinBuiltIns.getInstance().getDefaultBound());
074            }
075    
076            return upperBounds;
077        }
078    
079        private void resolveUpperBoundsFromWhereClause(Set<JetType> upperBounds, boolean forClassObject) {
080            JetClassOrObject classOrObject = PsiTreeUtil.getParentOfType(jetTypeParameter, JetClassOrObject.class);
081            if (classOrObject instanceof JetClass) {
082                JetClass jetClass = (JetClass) classOrObject;
083                for (JetTypeConstraint jetTypeConstraint : jetClass.getTypeConstraints()) {
084                    if (jetTypeConstraint.isClassObjectContraint() != forClassObject) continue;
085    
086                    JetSimpleNameExpression constrainedParameterName = jetTypeConstraint.getSubjectTypeParameterName();
087                    if (constrainedParameterName != null) {
088                        if (getName().equals(constrainedParameterName.getReferencedNameAsName())) {
089    
090                            JetTypeReference boundTypeReference = jetTypeConstraint.getBoundTypeReference();
091                            if (boundTypeReference != null) {
092                                upperBounds.add(resolveBoundType(boundTypeReference));
093                            }
094                        }
095                    }
096                }
097            }
098    
099        }
100    
101        private JetType resolveBoundType(@NotNull JetTypeReference boundTypeReference) {
102            return resolveSession.getInjector().getTypeResolver()
103                        .resolveType(getContainingDeclaration().getScopeForClassHeaderResolution(), boundTypeReference,
104                                     resolveSession.getTrace(), false);
105        }
106    
107        @NotNull
108        @Override
109        public LazyClassDescriptor getContainingDeclaration() {
110            return (LazyClassDescriptor) super.getContainingDeclaration();
111        }
112    
113        @Override
114        public void forceResolveAllContents() {
115            getAnnotations();
116            getClassObjectType();
117            getContainingDeclaration();
118            getDefaultType();
119            getIndex();
120            getLowerBounds();
121            getLowerBoundsAsType();
122            getOriginal();
123            getTypeConstructor();
124            getUpperBounds();
125            getUpperBoundsAsType();
126            getVariance();
127        }
128    }