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 ClassConstructorDescriptor primaryConstructor;
047 private final MemberScope scope;
048 private final NotNullLazyValue<Set<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<Set<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<Set<Name>> enumMemberNames,
075 @NotNull Annotations annotations,
076 @NotNull SourceElement source
077 ) {
078 super(storageManager, containingClass, name, source, /* isExternal = */ false);
079 assert containingClass.getKind() == ClassKind.ENUM_CLASS;
080
081 this.annotations = annotations;
082 this.typeConstructor = new ClassTypeConstructorImpl(
083 this, true, Collections.<TypeParameterDescriptor>emptyList(), Collections.singleton(supertype)
084 );
085
086 this.scope = new EnumEntryScope(storageManager);
087 this.enumMemberNames = enumMemberNames;
088
089 ClassConstructorDescriptorImpl 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<ClassConstructorDescriptor> 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 @Override
158 public boolean isHeader() {
159 return false;
160 }
161
162 @Override
163 public boolean isImpl() {
164 return false;
165 }
166
167 @Nullable
168 @Override
169 public ClassConstructorDescriptor getUnsubstitutedPrimaryConstructor() {
170 return primaryConstructor;
171 }
172
173 @NotNull
174 @Override
175 public Annotations getAnnotations() {
176 return annotations;
177 }
178
179 @Override
180 public String toString() {
181 return "enum entry " + getName();
182 }
183
184 @NotNull
185 @Override
186 public List<TypeParameterDescriptor> getDeclaredTypeParameters() {
187 return Collections.emptyList();
188 }
189
190 @NotNull
191 @Override
192 public Collection<ClassDescriptor> getSealedSubclasses() {
193 return Collections.emptyList();
194 }
195
196 private class EnumEntryScope extends MemberScopeImpl {
197 private final MemoizedFunctionToNotNull<Name, Collection<SimpleFunctionDescriptor>> functions;
198 private final MemoizedFunctionToNotNull<Name, Collection<PropertyDescriptor>> properties;
199 private final NotNullLazyValue<Collection<DeclarationDescriptor>> allDescriptors;
200
201 public EnumEntryScope(@NotNull StorageManager storageManager) {
202 this.functions = storageManager.createMemoizedFunction(new Function1<Name, Collection<SimpleFunctionDescriptor>>() {
203 @Override
204 public Collection<SimpleFunctionDescriptor> invoke(Name name) {
205 return computeFunctions(name);
206 }
207 });
208
209 this.properties = storageManager.createMemoizedFunction(new Function1<Name, Collection<PropertyDescriptor>>() {
210 @Override
211 public Collection<PropertyDescriptor> invoke(Name name) {
212 return computeProperties(name);
213 }
214 });
215 this.allDescriptors = storageManager.createLazyValue(new Function0<Collection<DeclarationDescriptor>>() {
216 @Override
217 public Collection<DeclarationDescriptor> invoke() {
218 return computeAllDeclarations();
219 }
220 });
221 }
222
223 @NotNull
224 @Override
225 // TODO: Convert to Kotlin or add @JvmWildcard to MemberScope declarations
226 // method is covariantly overridden in Kotlin, but collections in Java are invariant
227 @SuppressWarnings({"unchecked"})
228 public Collection getContributedVariables(@NotNull Name name, @NotNull LookupLocation location) {
229 return properties.invoke(name);
230 }
231
232 @NotNull
233 // TODO: Convert to Kotlin or add @JvmWildcard to MemberScope declarations
234 // method is covariantly overridden in Kotlin, but collections in Java are invariant
235 @SuppressWarnings({"unchecked"})
236 private Collection<PropertyDescriptor> computeProperties(@NotNull Name name) {
237 return resolveFakeOverrides(name, (Collection) getSupertypeScope().getContributedVariables(name, NoLookupLocation.FOR_NON_TRACKED_SCOPE));
238 }
239
240 @NotNull
241 @Override
242 // TODO: Convert to Kotlin or add @JvmWildcard to MemberScope declarations
243 // method is covariantly overridden in Kotlin, but collections in Java are invariant
244 @SuppressWarnings({"unchecked"})
245 public Collection getContributedFunctions(@NotNull Name name, @NotNull LookupLocation location) {
246 return functions.invoke(name);
247 }
248
249 @NotNull
250 private Collection<SimpleFunctionDescriptor> computeFunctions(@NotNull Name name) {
251 return resolveFakeOverrides(name, getSupertypeScope().getContributedFunctions(name, NoLookupLocation.FOR_NON_TRACKED_SCOPE));
252 }
253
254 @NotNull
255 private MemberScope getSupertypeScope() {
256 Collection<KotlinType> supertype = getTypeConstructor().getSupertypes();
257 assert supertype.size() == 1 : "Enum entry and its companion object both should have exactly one supertype: " + supertype;
258 return supertype.iterator().next().getMemberScope();
259 }
260
261 @NotNull
262 private <D extends CallableMemberDescriptor> Collection<D> resolveFakeOverrides(
263 @NotNull Name name,
264 @NotNull Collection<D> fromSupertypes
265 ) {
266 final Set<D> result = new LinkedHashSet<D>();
267
268 OverridingUtil.generateOverridesInFunctionGroup(
269 name, fromSupertypes, Collections.<D>emptySet(), EnumEntrySyntheticClassDescriptor.this,
270 new NonReportingOverrideStrategy() {
271 @Override
272 @SuppressWarnings("unchecked")
273 public void addFakeOverride(@NotNull CallableMemberDescriptor fakeOverride) {
274 OverridingUtil.resolveUnknownVisibilityForMember(fakeOverride, null);
275 result.add((D) fakeOverride);
276 }
277
278 @Override
279 protected void conflict(@NotNull CallableMemberDescriptor fromSuper, @NotNull CallableMemberDescriptor fromCurrent) {
280 // Do nothing
281 }
282 }
283 );
284
285 return result;
286 }
287
288 @NotNull
289 @Override
290 public Collection<DeclarationDescriptor> getContributedDescriptors(
291 @NotNull DescriptorKindFilter kindFilter,
292 @NotNull Function1<? super Name, Boolean> nameFilter
293 ) {
294 return allDescriptors.invoke();
295 }
296
297 @NotNull
298 private Collection<DeclarationDescriptor> computeAllDeclarations() {
299 Collection<DeclarationDescriptor> result = new HashSet<DeclarationDescriptor>();
300 for (Name name : enumMemberNames.invoke()) {
301 result.addAll(getContributedFunctions(name, NoLookupLocation.FOR_NON_TRACKED_SCOPE));
302 result.addAll(getContributedVariables(name, NoLookupLocation.FOR_NON_TRACKED_SCOPE));
303 }
304 return result;
305 }
306
307 @NotNull
308 @Override
309 public Set<Name> getFunctionNames() {
310 return enumMemberNames.invoke();
311 }
312
313 @NotNull
314 @Override
315 public Set<Name> getVariableNames() {
316 return enumMemberNames.invoke();
317 }
318
319 @Override
320 public void printScopeStructure(@NotNull Printer p) {
321 p.println("enum entry scope for " + EnumEntrySyntheticClassDescriptor.this);
322 }
323 }
324 }