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.MemberScope;
028    import org.jetbrains.kotlin.storage.LockBasedStorageManager;
029    import org.jetbrains.kotlin.types.ClassTypeConstructorImpl;
030    import org.jetbrains.kotlin.types.KotlinType;
031    import org.jetbrains.kotlin.types.TypeConstructor;
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<KotlinType> supertypes = new ArrayList<KotlinType>();
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.Companion.getEMPTY();
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 isData() {
105            return false;
106        }
107    
108        @Override
109        public boolean isCompanionObject() {
110            return false;
111        }
112    
113        @NotNull
114        @Override
115        public TypeConstructor getTypeConstructor() {
116            return typeConstructor;
117        }
118    
119        public void addSupertype(@NotNull KotlinType supertype) {
120            assert !supertype.isError() : "Error types must be filtered out in DescriptorResolver";
121            if (TypeUtils.getClassDescriptor(supertype) != null) {
122                // See the Errors.SUPERTYPE_NOT_A_CLASS_OR_INTERFACE
123                supertypes.add(supertype);
124            }
125        }
126    
127        @NotNull
128        @Override
129        public Set<ConstructorDescriptor> getConstructors() {
130            return Collections.emptySet();
131        }
132    
133        @Override
134        @Nullable
135        public ConstructorDescriptor getUnsubstitutedPrimaryConstructor() {
136            return null;
137        }
138    
139        public void setTypeParameterDescriptors(@NotNull List<TypeParameterDescriptor> typeParameters) {
140            if (this.typeParameters != null) {
141                throw new IllegalStateException("Type parameters are already set for " + getName());
142            }
143            this.typeParameters = new ArrayList<TypeParameterDescriptor>(typeParameters);
144        }
145    
146        @NotNull
147        @Override
148        public List<TypeParameterDescriptor> getDeclaredTypeParameters() {
149            return typeParameters;
150        }
151    
152        public void createTypeConstructor() {
153            assert typeConstructor == null : typeConstructor;
154            this.typeConstructor = new ClassTypeConstructorImpl(
155                    this, Annotations.Companion.getEMPTY(), ModalityKt.isFinalClass(this), typeParameters, supertypes
156            );
157            for (FunctionDescriptor functionDescriptor : getConstructors()) {
158                ((ConstructorDescriptorImpl) functionDescriptor).setReturnType(getDefaultType());
159            }
160        }
161    
162        @Override
163        @NotNull
164        public MemberScope getUnsubstitutedMemberScope() {
165            return MemberScope.Empty.INSTANCE; // used for getDefaultType
166        }
167    
168        @NotNull
169        @Override
170        public MemberScope getStaticScope() {
171            return MemberScope.Empty.INSTANCE;
172        }
173    
174        @Override
175        public String toString() {
176            return DeclarationDescriptorImpl.toString(this);
177        }
178    }