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