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.codegen;
018
019 import org.jetbrains.annotations.NotNull;
020 import org.jetbrains.annotations.Nullable;
021 import org.jetbrains.kotlin.descriptors.*;
022 import org.jetbrains.kotlin.descriptors.annotations.Annotations;
023 import org.jetbrains.kotlin.descriptors.impl.ClassDescriptorBase;
024 import org.jetbrains.kotlin.descriptors.impl.ConstructorDescriptorImpl;
025 import org.jetbrains.kotlin.descriptors.impl.DeclarationDescriptorImpl;
026 import org.jetbrains.kotlin.name.Name;
027 import org.jetbrains.kotlin.resolve.scopes.JetScope;
028 import org.jetbrains.kotlin.storage.LockBasedStorageManager;
029 import org.jetbrains.kotlin.types.JetType;
030 import org.jetbrains.kotlin.types.TypeConstructor;
031 import org.jetbrains.kotlin.types.TypeConstructorImpl;
032 import org.jetbrains.kotlin.types.TypeUtils;
033
034 import java.util.*;
035
036 public class MutableClassDescriptor extends ClassDescriptorBase implements ClassDescriptor {
037 private final ClassKind kind;
038 private final boolean isInner;
039
040 private Modality modality;
041 private Visibility visibility;
042 private TypeConstructor typeConstructor;
043 private List<TypeParameterDescriptor> typeParameters;
044 private final Collection<JetType> supertypes = new ArrayList<JetType>();
045
046 public MutableClassDescriptor(
047 @NotNull DeclarationDescriptor containingDeclaration,
048 @NotNull ClassKind kind,
049 boolean isInner,
050 @NotNull Name name,
051 @NotNull SourceElement source
052 ) {
053 super(LockBasedStorageManager.NO_LOCKS, containingDeclaration, name, source);
054 assert kind != ClassKind.OBJECT : "Fix isCompanionObject()";
055
056 this.kind = kind;
057 this.isInner = isInner;
058 }
059
060 @Nullable
061 @Override
062 public ClassDescriptor getCompanionObjectDescriptor() {
063 return null;
064 }
065
066 @NotNull
067 @Override
068 public Annotations getAnnotations() {
069 return Annotations.EMPTY;
070 }
071
072 public void setModality(@NotNull Modality modality) {
073 this.modality = modality;
074 }
075
076 @Override
077 @NotNull
078 public Modality getModality() {
079 return modality;
080 }
081
082 @NotNull
083 @Override
084 public ClassKind getKind() {
085 return kind;
086 }
087
088 public void setVisibility(@NotNull Visibility visibility) {
089 this.visibility = visibility;
090 }
091
092 @NotNull
093 @Override
094 public Visibility getVisibility() {
095 return visibility;
096 }
097
098 @Override
099 public boolean isInner() {
100 return isInner;
101 }
102
103 @Override
104 public boolean isCompanionObject() {
105 return false;
106 }
107
108 @NotNull
109 @Override
110 public TypeConstructor getTypeConstructor() {
111 return typeConstructor;
112 }
113
114 public void addSupertype(@NotNull JetType supertype) {
115 assert !supertype.isError() : "Error types must be filtered out in DescriptorResolver";
116 if (TypeUtils.getClassDescriptor(supertype) != null) {
117 // See the Errors.SUPERTYPE_NOT_A_CLASS_OR_TRAIT
118 supertypes.add(supertype);
119 }
120 }
121
122 @NotNull
123 @Override
124 public Set<ConstructorDescriptor> getConstructors() {
125 return Collections.emptySet();
126 }
127
128 @Override
129 @Nullable
130 public ConstructorDescriptor getUnsubstitutedPrimaryConstructor() {
131 return null;
132 }
133
134 public void setTypeParameterDescriptors(@NotNull List<TypeParameterDescriptor> typeParameters) {
135 if (this.typeParameters != null) {
136 throw new IllegalStateException("Type parameters are already set for " + getName());
137 }
138 this.typeParameters = new ArrayList<TypeParameterDescriptor>(typeParameters);
139 }
140
141 public void createTypeConstructor() {
142 assert typeConstructor == null : typeConstructor;
143 this.typeConstructor = TypeConstructorImpl.createForClass(
144 this,
145 Annotations.EMPTY,
146 !getModality().isOverridable(),
147 getName().asString(),
148 typeParameters,
149 supertypes
150 );
151 for (FunctionDescriptor functionDescriptor : getConstructors()) {
152 ((ConstructorDescriptorImpl) functionDescriptor).setReturnType(getDefaultType());
153 }
154 }
155
156 @Override
157 @NotNull
158 public JetScope getUnsubstitutedMemberScope() {
159 return JetScope.Empty.INSTANCE$; // used for getDefaultType
160 }
161
162 @NotNull
163 @Override
164 public JetScope getStaticScope() {
165 return JetScope.Empty.INSTANCE$;
166 }
167
168 @Override
169 public String toString() {
170 return DeclarationDescriptorImpl.toString(this);
171 }
172 }