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.descriptors.impl;
018
019 import org.jetbrains.annotations.NotNull;
020 import org.jetbrains.kotlin.descriptors.DeclarationDescriptor;
021 import org.jetbrains.kotlin.descriptors.SourceElement;
022 import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor;
023 import org.jetbrains.kotlin.descriptors.annotations.Annotations;
024 import org.jetbrains.kotlin.name.Name;
025 import org.jetbrains.kotlin.resolve.DescriptorUtils;
026 import org.jetbrains.kotlin.storage.LockBasedStorageManager;
027 import org.jetbrains.kotlin.types.JetType;
028 import org.jetbrains.kotlin.types.TypeConstructor;
029 import org.jetbrains.kotlin.types.TypeConstructorImpl;
030 import org.jetbrains.kotlin.types.Variance;
031 import org.jetbrains.kotlin.utils.SmartSet;
032
033 import java.util.Collections;
034 import java.util.Set;
035
036 import static org.jetbrains.kotlin.resolve.descriptorUtil.DescriptorUtilPackage.getBuiltIns;
037
038 public class TypeParameterDescriptorImpl extends AbstractTypeParameterDescriptor {
039 public static TypeParameterDescriptor createWithDefaultBound(
040 @NotNull DeclarationDescriptor containingDeclaration,
041 @NotNull Annotations annotations,
042 boolean reified,
043 @NotNull Variance variance,
044 @NotNull Name name,
045 int index
046 ) {
047 TypeParameterDescriptorImpl typeParameterDescriptor =
048 createForFurtherModification(containingDeclaration, annotations, reified, variance, name, index, SourceElement.NO_SOURCE);
049 typeParameterDescriptor.addUpperBound(getBuiltIns(containingDeclaration).getDefaultBound());
050 typeParameterDescriptor.setInitialized();
051 return typeParameterDescriptor;
052 }
053
054 public static TypeParameterDescriptorImpl createForFurtherModification(
055 @NotNull DeclarationDescriptor containingDeclaration,
056 @NotNull Annotations annotations,
057 boolean reified,
058 @NotNull Variance variance,
059 @NotNull Name name,
060 int index,
061 @NotNull SourceElement source
062 ) {
063 return new TypeParameterDescriptorImpl(containingDeclaration, annotations, reified, variance, name, index, source);
064 }
065
066 private final Set<JetType> upperBounds = SmartSet.create();
067 private boolean initialized = false;
068
069 private TypeParameterDescriptorImpl(
070 @NotNull DeclarationDescriptor containingDeclaration,
071 @NotNull Annotations annotations,
072 boolean reified,
073 @NotNull Variance variance,
074 @NotNull Name name,
075 int index,
076 @NotNull SourceElement source
077 ) {
078 super(LockBasedStorageManager.NO_LOCKS, containingDeclaration, annotations, name, variance, reified, index, source);
079 }
080
081 @NotNull
082 @Override
083 protected TypeConstructor createTypeConstructor() {
084 // TODO: Should we actually pass the annotations on to the type constructor?
085 return TypeConstructorImpl.createForTypeParameter(
086 this,
087 getAnnotations(),
088 false,
089 getName().asString(),
090 Collections.<TypeParameterDescriptor>emptyList(),
091 upperBounds
092 );
093 }
094
095 private void checkInitialized() {
096 if (!initialized) {
097 throw new IllegalStateException("Type parameter descriptor is not initialized: " + nameForAssertions());
098 }
099 }
100
101 private void checkUninitialized() {
102 if (initialized) {
103 throw new IllegalStateException("Type parameter descriptor is already initialized: " + nameForAssertions());
104 }
105 }
106
107 private String nameForAssertions() {
108 return getName() + " declared in " + DescriptorUtils.getFqName(getContainingDeclaration());
109 }
110
111 public void setInitialized() {
112 checkUninitialized();
113 initialized = true;
114 }
115
116 public void addUpperBound(@NotNull JetType bound) {
117 checkUninitialized();
118 doAddUpperBound(bound);
119 }
120
121 private void doAddUpperBound(JetType bound) {
122 upperBounds.add(bound); // TODO : Duplicates?
123 }
124
125 public void addDefaultUpperBound() {
126 checkUninitialized();
127
128 if (upperBounds.isEmpty()) {
129 doAddUpperBound(getBuiltIns(getContainingDeclaration()).getDefaultBound());
130 }
131 }
132
133 @NotNull
134 @Override
135 protected Set<JetType> resolveUpperBounds() {
136 checkInitialized();
137 return upperBounds;
138 }
139 }