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.DescriptorResolver;
026 import org.jetbrains.kotlin.resolve.lazy.ForceResolveUtil;
027 import org.jetbrains.kotlin.resolve.lazy.LazyClassContext;
028 import org.jetbrains.kotlin.resolve.lazy.LazyEntity;
029 import org.jetbrains.kotlin.types.JetType;
030
031 import java.util.Set;
032
033 import static org.jetbrains.kotlin.resolve.source.SourcePackage.toSourceElement;
034
035 public class LazyTypeParameterDescriptor extends AbstractLazyTypeParameterDescriptor implements LazyEntity {
036 private final LazyClassContext c;
037
038 private final JetTypeParameter jetTypeParameter;
039
040 public LazyTypeParameterDescriptor(
041 @NotNull LazyClassContext c,
042 @NotNull LazyClassDescriptor containingDeclaration,
043 @NotNull JetTypeParameter jetTypeParameter,
044 int index) {
045 super(
046 c.getStorageManager(),
047 containingDeclaration,
048 jetTypeParameter.getNameAsSafeName(),
049 jetTypeParameter.getVariance(),
050 jetTypeParameter.hasModifier(JetTokens.REIFIED_KEYWORD),
051 index,
052 toSourceElement(jetTypeParameter)
053 );
054 this.c = c;
055 this.jetTypeParameter = jetTypeParameter;
056
057 this.c.getTrace().record(BindingContext.TYPE_PARAMETER, jetTypeParameter, this);
058 }
059
060 @NotNull
061 @Override
062 protected Set<JetType> resolveUpperBounds() {
063 Set<JetType> upperBounds = Sets.newLinkedHashSet();
064
065 JetTypeParameter jetTypeParameter = this.jetTypeParameter;
066
067 JetTypeReference extendsBound = jetTypeParameter.getExtendsBound();
068 if (extendsBound != null) {
069 upperBounds.add(resolveBoundType(extendsBound));
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<JetType> upperBounds) {
082 JetClassOrObject classOrObject = JetStubbedPsiUtil.getPsiOrStubParent(jetTypeParameter, JetClassOrObject.class, true);
083 if (classOrObject instanceof JetClass) {
084 JetClass jetClass = (JetClass) classOrObject;
085 for (JetTypeConstraint jetTypeConstraint : jetClass.getTypeConstraints()) {
086 JetSimpleNameExpression constrainedParameterName = jetTypeConstraint.getSubjectTypeParameterName();
087 if (constrainedParameterName != null) {
088 if (getName().equals(constrainedParameterName.getReferencedNameAsName())) {
089 c.getTrace().record(BindingContext.REFERENCE_TARGET, constrainedParameterName, this);
090
091 JetTypeReference boundTypeReference = jetTypeConstraint.getBoundTypeReference();
092 if (boundTypeReference != null) {
093 JetType boundType = resolveBoundType(boundTypeReference);
094 upperBounds.add(boundType);
095 }
096 }
097 }
098 }
099 }
100
101 }
102
103 private JetType resolveBoundType(@NotNull JetTypeReference 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 }