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