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.NonReportingOverrideStrategy;
030    import org.jetbrains.kotlin.resolve.OverridingUtil;
031    import org.jetbrains.kotlin.resolve.scopes.DescriptorKindFilter;
032    import org.jetbrains.kotlin.resolve.scopes.MemberScope;
033    import org.jetbrains.kotlin.resolve.scopes.MemberScopeImpl;
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.ClassTypeConstructorImpl;
038    import org.jetbrains.kotlin.types.KotlinType;
039    import org.jetbrains.kotlin.types.TypeConstructor;
040    import org.jetbrains.kotlin.utils.Printer;
041    
042    import java.util.*;
043    
044    public class EnumEntrySyntheticClassDescriptor extends ClassDescriptorBase {
045        private final TypeConstructor typeConstructor;
046        private final ConstructorDescriptor primaryConstructor;
047        private final MemberScope scope;
048        private final NotNullLazyValue<Collection<Name>> enumMemberNames;
049        private final Annotations annotations;
050    
051        /**
052         * Creates and initializes descriptors for enum entry with the given name and its companion object
053         * @param enumMemberNames needed for fake overrides resolution
054         */
055        @NotNull
056        public static EnumEntrySyntheticClassDescriptor create(
057                @NotNull StorageManager storageManager,
058                @NotNull ClassDescriptor enumClass,
059                @NotNull Name name,
060                @NotNull NotNullLazyValue<Collection<Name>> enumMemberNames,
061                @NotNull Annotations annotations,
062                @NotNull SourceElement source
063        ) {
064            KotlinType enumType = enumClass.getDefaultType();
065    
066            return new EnumEntrySyntheticClassDescriptor(storageManager, enumClass, enumType, name, enumMemberNames, annotations, source);
067        }
068    
069        private EnumEntrySyntheticClassDescriptor(
070                @NotNull StorageManager storageManager,
071                @NotNull ClassDescriptor containingClass,
072                @NotNull KotlinType supertype,
073                @NotNull Name name,
074                @NotNull NotNullLazyValue<Collection<Name>> enumMemberNames,
075                @NotNull Annotations annotations,
076                @NotNull SourceElement source
077        ) {
078            super(storageManager, containingClass, name, source);
079            assert containingClass.getKind() == ClassKind.ENUM_CLASS;
080    
081            this.annotations = annotations;
082            this.typeConstructor = new ClassTypeConstructorImpl(
083                    this, getAnnotations(), true, Collections.<TypeParameterDescriptor>emptyList(), Collections.singleton(supertype)
084            );
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 MemberScope getUnsubstitutedMemberScope() {
097            return scope;
098        }
099    
100        @NotNull
101        @Override
102        public MemberScope getStaticScope() {
103            return MemberScope.Empty.INSTANCE;
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 isData() {
149            return false;
150        }
151    
152        @Override
153        public boolean isCompanionObject() {
154            return false;
155        }
156    
157        @Nullable
158        @Override
159        public ConstructorDescriptor getUnsubstitutedPrimaryConstructor() {
160            return primaryConstructor;
161        }
162    
163        @NotNull
164        @Override
165        public Annotations getAnnotations() {
166            return annotations;
167        }
168    
169        @Override
170        public String toString() {
171            return "enum entry " + getName();
172        }
173    
174        @NotNull
175        @Override
176        public List<TypeParameterDescriptor> getDeclaredTypeParameters() {
177            return Collections.emptyList();
178        }
179    
180        private class EnumEntryScope extends MemberScopeImpl {
181            private final MemoizedFunctionToNotNull<Name, Collection<SimpleFunctionDescriptor>> functions;
182            private final MemoizedFunctionToNotNull<Name, Collection<PropertyDescriptor>> properties;
183            private final NotNullLazyValue<Collection<DeclarationDescriptor>> allDescriptors;
184    
185            public EnumEntryScope(@NotNull StorageManager storageManager) {
186                this.functions = storageManager.createMemoizedFunction(new Function1<Name, Collection<SimpleFunctionDescriptor>>() {
187                    @Override
188                    public Collection<SimpleFunctionDescriptor> invoke(Name name) {
189                        return computeFunctions(name);
190                    }
191                });
192    
193                this.properties = storageManager.createMemoizedFunction(new Function1<Name, Collection<PropertyDescriptor>>() {
194                    @Override
195                    public Collection<PropertyDescriptor> invoke(Name name) {
196                        return computeProperties(name);
197                    }
198                });
199                this.allDescriptors = storageManager.createLazyValue(new Function0<Collection<DeclarationDescriptor>>() {
200                    @Override
201                    public Collection<DeclarationDescriptor> invoke() {
202                        return computeAllDeclarations();
203                    }
204                });
205            }
206    
207            @NotNull
208            @Override
209            // TODO: Convert to Kotlin or add @JvmWildcard to MemberScope declarations
210            // method is covariantly overridden in Kotlin, but collections in Java are invariant
211            @SuppressWarnings({"unchecked"})
212            public Collection getContributedVariables(@NotNull Name name, @NotNull LookupLocation location) {
213                return properties.invoke(name);
214            }
215    
216            @NotNull
217            // TODO: Convert to Kotlin or add @JvmWildcard to MemberScope declarations
218            // method is covariantly overridden in Kotlin, but collections in Java are invariant
219            @SuppressWarnings({"unchecked"})
220            private Collection<PropertyDescriptor> computeProperties(@NotNull Name name) {
221                return resolveFakeOverrides(name, (Collection) getSupertypeScope().getContributedVariables(name, NoLookupLocation.FOR_NON_TRACKED_SCOPE));
222            }
223    
224            @NotNull
225            @Override
226            // TODO: Convert to Kotlin or add @JvmWildcard to MemberScope declarations
227            // method is covariantly overridden in Kotlin, but collections in Java are invariant
228            @SuppressWarnings({"unchecked"})
229            public Collection getContributedFunctions(@NotNull Name name, @NotNull LookupLocation location) {
230                return functions.invoke(name);
231            }
232    
233            @NotNull
234            private Collection<SimpleFunctionDescriptor> computeFunctions(@NotNull Name name) {
235                return resolveFakeOverrides(name, getSupertypeScope().getContributedFunctions(name, NoLookupLocation.FOR_NON_TRACKED_SCOPE));
236            }
237    
238            @NotNull
239            private MemberScope getSupertypeScope() {
240                Collection<KotlinType> supertype = getTypeConstructor().getSupertypes();
241                assert supertype.size() == 1 : "Enum entry and its companion object both should have exactly one supertype: " + supertype;
242                return supertype.iterator().next().getMemberScope();
243            }
244    
245            @NotNull
246            private <D extends CallableMemberDescriptor> Collection<D> resolveFakeOverrides(
247                    @NotNull Name name,
248                    @NotNull Collection<D> fromSupertypes
249            ) {
250                final Set<D> result = new LinkedHashSet<D>();
251    
252                OverridingUtil.generateOverridesInFunctionGroup(
253                        name, fromSupertypes, Collections.<D>emptySet(), EnumEntrySyntheticClassDescriptor.this,
254                        new NonReportingOverrideStrategy() {
255                            @Override
256                            @SuppressWarnings("unchecked")
257                            public void addFakeOverride(@NotNull CallableMemberDescriptor fakeOverride) {
258                                OverridingUtil.resolveUnknownVisibilityForMember(fakeOverride, null);
259                                result.add((D) fakeOverride);
260                            }
261    
262                            @Override
263                            protected void conflict(@NotNull CallableMemberDescriptor fromSuper, @NotNull CallableMemberDescriptor fromCurrent) {
264                                // Do nothing
265                            }
266                        }
267                );
268    
269                return result;
270            }
271    
272            @NotNull
273            @Override
274            public Collection<DeclarationDescriptor> getContributedDescriptors(
275                    @NotNull DescriptorKindFilter kindFilter,
276                    @NotNull Function1<? super Name, Boolean> nameFilter
277            ) {
278                return allDescriptors.invoke();
279            }
280    
281            @NotNull
282            private Collection<DeclarationDescriptor> computeAllDeclarations() {
283                Collection<DeclarationDescriptor> result = new HashSet<DeclarationDescriptor>();
284                for (Name name : enumMemberNames.invoke()) {
285                    result.addAll(getContributedFunctions(name, NoLookupLocation.FOR_NON_TRACKED_SCOPE));
286                    result.addAll(getContributedVariables(name, NoLookupLocation.FOR_NON_TRACKED_SCOPE));
287                }
288                return result;
289            }
290    
291            @Override
292            public void printScopeStructure(@NotNull Printer p) {
293                p.println("enum entry scope for " + EnumEntrySyntheticClassDescriptor.this);
294            }
295        }
296    }