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 kotlin.jvm.functions.Function0;
020    import kotlin.jvm.functions.Function1;
021    import org.jetbrains.annotations.NotNull;
022    import org.jetbrains.annotations.Nullable;
023    import org.jetbrains.kotlin.descriptors.*;
024    import org.jetbrains.kotlin.descriptors.annotations.Annotations;
025    import org.jetbrains.kotlin.incremental.components.LookupLocation;
026    import org.jetbrains.kotlin.incremental.components.NoLookupLocation;
027    import org.jetbrains.kotlin.name.Name;
028    import org.jetbrains.kotlin.resolve.DescriptorFactory;
029    import org.jetbrains.kotlin.resolve.OverridingUtil;
030    import org.jetbrains.kotlin.resolve.scopes.DescriptorKindFilter;
031    import org.jetbrains.kotlin.resolve.scopes.JetScope;
032    import org.jetbrains.kotlin.resolve.scopes.JetScopeImpl;
033    import org.jetbrains.kotlin.resolve.scopes.StaticScopeForKotlinClass;
034    import org.jetbrains.kotlin.storage.MemoizedFunctionToNotNull;
035    import org.jetbrains.kotlin.storage.NotNullLazyValue;
036    import org.jetbrains.kotlin.storage.StorageManager;
037    import org.jetbrains.kotlin.types.JetType;
038    import org.jetbrains.kotlin.types.TypeConstructor;
039    import org.jetbrains.kotlin.types.TypeConstructorImpl;
040    import org.jetbrains.kotlin.utils.Printer;
041    
042    import java.util.Collection;
043    import java.util.Collections;
044    import java.util.HashSet;
045    import java.util.Set;
046    
047    public class EnumEntrySyntheticClassDescriptor extends ClassDescriptorBase {
048        private final TypeConstructor typeConstructor;
049        private final ConstructorDescriptor primaryConstructor;
050        private final JetScope scope;
051        private final JetScope staticScope = new StaticScopeForKotlinClass(this);
052        private final NotNullLazyValue<Collection<Name>> enumMemberNames;
053    
054        /**
055         * Creates and initializes descriptors for enum entry with the given name and its companion object
056         * @param enumMemberNames needed for fake overrides resolution
057         */
058        @NotNull
059        public static EnumEntrySyntheticClassDescriptor create(
060                @NotNull StorageManager storageManager,
061                @NotNull ClassDescriptor enumClass,
062                @NotNull Name name,
063                @NotNull NotNullLazyValue<Collection<Name>> enumMemberNames,
064                @NotNull SourceElement source
065        ) {
066            JetType enumType = enumClass.getDefaultType();
067    
068            return new EnumEntrySyntheticClassDescriptor(storageManager, enumClass, enumType, name, enumMemberNames, source);
069        }
070    
071        private EnumEntrySyntheticClassDescriptor(
072                @NotNull StorageManager storageManager,
073                @NotNull ClassDescriptor containingClass,
074                @NotNull JetType supertype,
075                @NotNull Name name,
076                @NotNull NotNullLazyValue<Collection<Name>> enumMemberNames,
077                @NotNull SourceElement source
078        ) {
079            super(storageManager, containingClass, name, source);
080            assert containingClass.getKind() == ClassKind.ENUM_CLASS;
081    
082            this.typeConstructor =
083                    TypeConstructorImpl.createForClass(this, getAnnotations(), true, "enum entry", Collections.<TypeParameterDescriptor>emptyList(),
084                                            Collections.singleton(supertype));
085    
086            this.scope = new EnumEntryScope(storageManager);
087            this.enumMemberNames = enumMemberNames;
088    
089            ConstructorDescriptorImpl primaryConstructor = DescriptorFactory.createPrimaryConstructorForObject(this, source);
090            primaryConstructor.setReturnType(getDefaultType());
091            this.primaryConstructor = primaryConstructor;
092        }
093    
094        @NotNull
095        @Override
096        public JetScope getUnsubstitutedMemberScope() {
097            return scope;
098        }
099    
100        @NotNull
101        @Override
102        public JetScope getStaticScope() {
103            return staticScope;
104        }
105    
106        @NotNull
107        @Override
108        public Collection<ConstructorDescriptor> getConstructors() {
109            return Collections.singleton(primaryConstructor);
110        }
111    
112        @NotNull
113        @Override
114        public TypeConstructor getTypeConstructor() {
115            return typeConstructor;
116        }
117    
118        @Nullable
119        @Override
120        public ClassDescriptor getCompanionObjectDescriptor() {
121            return null;
122        }
123    
124        @NotNull
125        @Override
126        public ClassKind getKind() {
127            return ClassKind.ENUM_ENTRY;
128        }
129    
130        @NotNull
131        @Override
132        public Modality getModality() {
133            return Modality.FINAL;
134        }
135    
136        @NotNull
137        @Override
138        public Visibility getVisibility() {
139            return Visibilities.PUBLIC;
140        }
141    
142        @Override
143        public boolean isInner() {
144            return false;
145        }
146    
147        @Override
148        public boolean isCompanionObject() {
149            return false;
150        }
151    
152        @Nullable
153        @Override
154        public ConstructorDescriptor getUnsubstitutedPrimaryConstructor() {
155            return primaryConstructor;
156        }
157    
158        @NotNull
159        @Override
160        public Annotations getAnnotations() {
161            // TODO
162            return Annotations.EMPTY;
163        }
164    
165        @Override
166        public String toString() {
167            return "enum entry " + getName();
168        }
169    
170        private class EnumEntryScope extends JetScopeImpl {
171            private final MemoizedFunctionToNotNull<Name, Collection<FunctionDescriptor>> functions;
172            private final MemoizedFunctionToNotNull<Name, Collection<PropertyDescriptor>> properties;
173            private final NotNullLazyValue<Collection<DeclarationDescriptor>> allDescriptors;
174    
175            public EnumEntryScope(@NotNull StorageManager storageManager) {
176                this.functions = storageManager.createMemoizedFunction(new Function1<Name, Collection<FunctionDescriptor>>() {
177                    @Override
178                    public Collection<FunctionDescriptor> invoke(Name name) {
179                        return computeFunctions(name);
180                    }
181                });
182    
183                this.properties = storageManager.createMemoizedFunction(new Function1<Name, Collection<PropertyDescriptor>>() {
184                    @Override
185                    public Collection<PropertyDescriptor> invoke(Name name) {
186                        return computeProperties(name);
187                    }
188                });
189                this.allDescriptors = storageManager.createLazyValue(new Function0<Collection<DeclarationDescriptor>>() {
190                    @Override
191                    public Collection<DeclarationDescriptor> invoke() {
192                        return computeAllDeclarations();
193                    }
194                });
195            }
196    
197            @NotNull
198            @Override
199            @SuppressWarnings("unchecked")
200            public Collection<VariableDescriptor> getProperties(@NotNull Name name, @NotNull LookupLocation location) {
201                return (Collection) properties.invoke(name);
202            }
203    
204            @NotNull
205            @SuppressWarnings("unchecked")
206            private Collection<PropertyDescriptor> computeProperties(@NotNull Name name) {
207                return resolveFakeOverrides(name, (Collection) getSupertypeScope().getProperties(name, NoLookupLocation.UNSORTED));
208            }
209    
210            @NotNull
211            @Override
212            public Collection<FunctionDescriptor> getFunctions(@NotNull Name name, @NotNull LookupLocation location) {
213                return functions.invoke(name);
214            }
215    
216            @NotNull
217            private Collection<FunctionDescriptor> computeFunctions(@NotNull Name name) {
218                return resolveFakeOverrides(name, getSupertypeScope().getFunctions(name, NoLookupLocation.UNSORTED));
219            }
220    
221            @NotNull
222            private JetScope getSupertypeScope() {
223                Collection<JetType> supertype = getTypeConstructor().getSupertypes();
224                assert supertype.size() == 1 : "Enum entry and its companion object both should have exactly one supertype: " + supertype;
225                return supertype.iterator().next().getMemberScope();
226            }
227    
228            @NotNull
229            private <D extends CallableMemberDescriptor> Collection<D> resolveFakeOverrides(
230                    @NotNull Name name,
231                    @NotNull Collection<D> fromSupertypes
232            ) {
233                final Set<D> result = new HashSet<D>();
234    
235                OverridingUtil.generateOverridesInFunctionGroup(
236                        name, fromSupertypes, Collections.<D>emptySet(), EnumEntrySyntheticClassDescriptor.this,
237                        new OverridingUtil.DescriptorSink() {
238                            @Override
239                            @SuppressWarnings("unchecked")
240                            public void addFakeOverride(@NotNull CallableMemberDescriptor fakeOverride) {
241                                OverridingUtil.resolveUnknownVisibilityForMember(fakeOverride, null);
242                                result.add((D) fakeOverride);
243                            }
244    
245                            @Override
246                            public void conflict(@NotNull CallableMemberDescriptor fromSuper, @NotNull CallableMemberDescriptor fromCurrent) {
247                                // Do nothing
248                            }
249                        }
250                );
251    
252                return result;
253            }
254    
255            @NotNull
256            @Override
257            public DeclarationDescriptor getContainingDeclaration() {
258                return EnumEntrySyntheticClassDescriptor.this;
259            }
260    
261            @NotNull
262            @Override
263            public Collection<DeclarationDescriptor> getDescriptors(
264                    @NotNull DescriptorKindFilter kindFilter,
265                    @NotNull Function1<? super Name, ? extends Boolean> nameFilter
266            ) {
267                return allDescriptors.invoke();
268            }
269    
270            @NotNull
271            private Collection<DeclarationDescriptor> computeAllDeclarations() {
272                Collection<DeclarationDescriptor> result = new HashSet<DeclarationDescriptor>();
273                for (Name name : enumMemberNames.invoke()) {
274                    result.addAll(getFunctions(name, NoLookupLocation.UNSORTED));
275                    result.addAll(getProperties(name, NoLookupLocation.UNSORTED));
276                }
277                return result;
278            }
279    
280            @Override
281            public void printScopeStructure(@NotNull Printer p) {
282                p.println("enum entry scope for " + EnumEntrySyntheticClassDescriptor.this);
283            }
284        }
285    }