001 /*
002 * Copyright 2010-2013 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.jet.lang.types;
018
019 import kotlin.Function1;
020 import org.jetbrains.annotations.NotNull;
021 import org.jetbrains.annotations.Nullable;
022 import org.jetbrains.jet.lang.PlatformToKotlinClassMap;
023 import org.jetbrains.jet.lang.descriptors.*;
024 import org.jetbrains.jet.lang.descriptors.annotations.Annotations;
025 import org.jetbrains.jet.lang.descriptors.impl.PropertyDescriptorImpl;
026 import org.jetbrains.jet.lang.descriptors.impl.TypeParameterDescriptorImpl;
027 import org.jetbrains.jet.lang.resolve.ImportPath;
028 import org.jetbrains.jet.lang.resolve.name.LabelName;
029 import org.jetbrains.jet.lang.resolve.name.Name;
030 import org.jetbrains.jet.lang.resolve.scopes.JetScope;
031 import org.jetbrains.jet.lang.types.error.ErrorClassDescriptor;
032 import org.jetbrains.jet.lang.types.error.ErrorSimpleFunctionDescriptorImpl;
033 import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns;
034 import org.jetbrains.jet.utils.Printer;
035
036 import java.util.Collection;
037 import java.util.Collections;
038 import java.util.List;
039 import java.util.Set;
040
041 public class ErrorUtils {
042
043 private static final ModuleDescriptor ERROR_MODULE;
044 static {
045 ERROR_MODULE = new ModuleDescriptorImpl(
046 Name.special("<ERROR MODULE>"),
047 Collections.<ImportPath>emptyList(),
048 PlatformToKotlinClassMap.EMPTY
049 );
050 }
051
052 public static boolean containsErrorType(@NotNull FunctionDescriptor function) {
053 if (containsErrorType(function.getReturnType())) {
054 return true;
055 }
056 ReceiverParameterDescriptor receiverParameter = function.getReceiverParameter();
057 if (receiverParameter != null && containsErrorType(receiverParameter.getType())) {
058 return true;
059 }
060 for (ValueParameterDescriptor parameter : function.getValueParameters()) {
061 if (containsErrorType(parameter.getType())) {
062 return true;
063 }
064 }
065 for (TypeParameterDescriptor parameter : function.getTypeParameters()) {
066 for (JetType upperBound : parameter.getUpperBounds()) {
067 if (containsErrorType(upperBound)) {
068 return true;
069 }
070 }
071 }
072
073 return false;
074 }
075
076
077 public static class ErrorScope implements JetScope {
078 private final String debugMessage;
079
080 private ErrorScope(@NotNull String debugMessage) {
081 this.debugMessage = debugMessage;
082 }
083
084 @Override
085 public ClassifierDescriptor getClassifier(@NotNull Name name) {
086 return ERROR_CLASS;
087 }
088
089 @NotNull
090 @Override
091 public Set<VariableDescriptor> getProperties(@NotNull Name name) {
092 return ERROR_PROPERTY_GROUP;
093 }
094
095 @Override
096 public VariableDescriptor getLocalVariable(@NotNull Name name) {
097 return ERROR_PROPERTY;
098 }
099
100 @Override
101 public PackageViewDescriptor getPackage(@NotNull Name name) {
102 return null;
103 }
104
105 @NotNull
106 @Override
107 public List<ReceiverParameterDescriptor> getImplicitReceiversHierarchy() {
108 return Collections.emptyList();
109 }
110
111 @NotNull
112 @Override
113 public Set<FunctionDescriptor> getFunctions(@NotNull Name name) {
114 return Collections.<FunctionDescriptor>singleton(createErrorFunction(this));
115 }
116
117 @NotNull
118 @Override
119 public DeclarationDescriptor getContainingDeclaration() {
120 return ERROR_MODULE;
121 }
122
123 @NotNull
124 @Override
125 public Collection<DeclarationDescriptor> getDeclarationsByLabel(@NotNull LabelName labelName) {
126 return Collections.emptyList();
127 }
128
129 @NotNull
130 @Override
131 public Collection<DeclarationDescriptor> getAllDescriptors() {
132 return Collections.emptyList();
133 }
134
135 @NotNull
136 @Override
137 public Collection<DeclarationDescriptor> getOwnDeclaredDescriptors() {
138 return Collections.emptyList();
139 }
140
141 @Override
142 public String toString() {
143 return "ErrorScope{" + debugMessage + '}';
144 }
145
146 @Override
147 public void printScopeStructure(@NotNull Printer p) {
148 p.println(getClass().getSimpleName(), ": ", debugMessage);
149 }
150 }
151
152 private static class ThrowingScope implements JetScope {
153 private final String debugMessage;
154
155 private ThrowingScope(@NotNull String message) {
156 debugMessage = message;
157 }
158
159 @Nullable
160 @Override
161 public ClassifierDescriptor getClassifier(@NotNull Name name) {
162 throw new IllegalStateException();
163 }
164
165 @Nullable
166 @Override
167 public PackageViewDescriptor getPackage(@NotNull Name name) {
168 throw new IllegalStateException();
169 }
170
171 @NotNull
172 @Override
173 public Collection<VariableDescriptor> getProperties(@NotNull Name name) {
174 throw new IllegalStateException();
175 }
176
177 @Nullable
178 @Override
179 public VariableDescriptor getLocalVariable(@NotNull Name name) {
180 throw new IllegalStateException();
181 }
182
183 @NotNull
184 @Override
185 public Collection<FunctionDescriptor> getFunctions(@NotNull Name name) {
186 throw new IllegalStateException();
187 }
188
189 @NotNull
190 @Override
191 public DeclarationDescriptor getContainingDeclaration() {
192 return ERROR_MODULE;
193 }
194
195 @NotNull
196 @Override
197 public Collection<DeclarationDescriptor> getDeclarationsByLabel(@NotNull LabelName labelName) {
198 throw new IllegalStateException();
199 }
200
201 @NotNull
202 @Override
203 public Collection<DeclarationDescriptor> getAllDescriptors() {
204 throw new IllegalStateException();
205 }
206
207 @NotNull
208 @Override
209 public List<ReceiverParameterDescriptor> getImplicitReceiversHierarchy() {
210 throw new IllegalStateException();
211 }
212
213 @NotNull
214 @Override
215 public Collection<DeclarationDescriptor> getOwnDeclaredDescriptors() {
216 throw new IllegalStateException();
217 }
218
219 @Override
220 public String toString() {
221 return "ThrowingScope{" + debugMessage + '}';
222 }
223
224 @Override
225 public void printScopeStructure(@NotNull Printer p) {
226 p.println(getClass().getSimpleName(), ": ", debugMessage);
227 }
228 }
229
230 private static final ErrorClassDescriptor ERROR_CLASS = new ErrorClassDescriptor("");
231
232 @NotNull
233 public static JetScope createErrorScope(@NotNull String debugMessage) {
234 return createErrorScope(debugMessage, false);
235 }
236
237 @NotNull
238 public static JetScope createErrorScope(@NotNull String debugMessage, boolean throwExceptions) {
239 if (throwExceptions) {
240 return new ThrowingScope(debugMessage);
241 }
242 return new ErrorScope(debugMessage);
243 }
244
245 private static final JetType ERROR_PROPERTY_TYPE = createErrorType("<ERROR PROPERTY TYPE>");
246 private static final VariableDescriptor ERROR_PROPERTY = createErrorProperty();
247
248 private static final Set<VariableDescriptor> ERROR_PROPERTY_GROUP = Collections.singleton(ERROR_PROPERTY);
249
250 @NotNull
251 private static PropertyDescriptorImpl createErrorProperty() {
252 PropertyDescriptorImpl descriptor = PropertyDescriptorImpl.create(
253 ERROR_CLASS,
254 Annotations.EMPTY,
255 Modality.OPEN,
256 Visibilities.INTERNAL,
257 true,
258 Name.special("<ERROR PROPERTY>"),
259 CallableMemberDescriptor.Kind.DECLARATION
260 );
261 descriptor.setType(ERROR_PROPERTY_TYPE,
262 Collections.<TypeParameterDescriptor>emptyList(),
263 ReceiverParameterDescriptor.NO_RECEIVER_PARAMETER,
264 (JetType) null
265 );
266
267 return descriptor;
268 }
269
270 @NotNull
271 private static SimpleFunctionDescriptor createErrorFunction(@NotNull ErrorScope ownerScope) {
272 ErrorSimpleFunctionDescriptorImpl function = new ErrorSimpleFunctionDescriptorImpl(ownerScope);
273 function.initialize(
274 null,
275 ReceiverParameterDescriptor.NO_RECEIVER_PARAMETER,
276 Collections.<TypeParameterDescriptorImpl>emptyList(), // TODO
277 Collections.<ValueParameterDescriptor>emptyList(), // TODO
278 createErrorType("<ERROR FUNCTION RETURN TYPE>"),
279 Modality.OPEN,
280 Visibilities.INTERNAL
281 );
282 return function;
283 }
284
285 @NotNull
286 public static JetType createErrorType(@NotNull String debugMessage) {
287 return new ErrorTypeImpl(createErrorTypeConstructor(debugMessage), createErrorScope(debugMessage));
288 }
289
290 @NotNull
291 public static JetType createErrorTypeWithCustomDebugName(@NotNull String debugName) {
292 return new ErrorTypeImpl(createErrorTypeConstructorWithCustomDebugName(debugName), createErrorScope(debugName));
293 }
294
295 @NotNull
296 public static TypeConstructor createErrorTypeConstructor(@NotNull String debugMessage) {
297 return createErrorTypeConstructorWithCustomDebugName("[ERROR : " + debugMessage + "]");
298 }
299
300 @NotNull
301 private static TypeConstructor createErrorTypeConstructorWithCustomDebugName(@NotNull String debugName) {
302 return new TypeConstructorImpl(ERROR_CLASS, Annotations.EMPTY, false, debugName,
303 Collections.<TypeParameterDescriptorImpl>emptyList(),
304 Collections.singleton(KotlinBuiltIns.getInstance().getAnyType()));
305 }
306
307 @NotNull
308 public static ClassDescriptor getErrorClass() {
309 return ERROR_CLASS;
310 }
311
312 public static boolean containsErrorType(@Nullable JetType type) {
313 if (type == null) return false;
314 if (type instanceof PackageType) return false;
315 if (type.isError()) return true;
316 for (TypeProjection projection : type.getArguments()) {
317 if (containsErrorType(projection.getType())) return true;
318 }
319 return false;
320 }
321
322 public static boolean isError(@NotNull DeclarationDescriptor candidate) {
323 return isErrorClass(candidate) || isErrorClass(candidate.getContainingDeclaration()) || candidate == ERROR_MODULE;
324 }
325
326 private static boolean isErrorClass(@Nullable DeclarationDescriptor candidate) {
327 return candidate instanceof ErrorClassDescriptor;
328 }
329
330 @NotNull
331 public static TypeParameterDescriptor createErrorTypeParameter(int index, @NotNull String debugMessage) {
332 return TypeParameterDescriptorImpl.createWithDefaultBound(
333 ERROR_CLASS,
334 Annotations.EMPTY,
335 false,
336 Variance.INVARIANT,
337 Name.special("<ERROR: " + debugMessage + ">"),
338 index
339 );
340 }
341
342 private static class ErrorTypeImpl implements JetType {
343 private final TypeConstructor constructor;
344 private final JetScope memberScope;
345
346 private ErrorTypeImpl(@NotNull TypeConstructor constructor, @NotNull JetScope memberScope) {
347 this.constructor = constructor;
348 this.memberScope = memberScope;
349 }
350
351 @NotNull
352 @Override
353 public TypeConstructor getConstructor() {
354 return constructor;
355 }
356
357 @NotNull
358 @Override
359 public List<TypeProjection> getArguments() {
360 return Collections.emptyList();
361 }
362
363 @Override
364 public boolean isNullable() {
365 return false;
366 }
367
368 @NotNull
369 @Override
370 public JetScope getMemberScope() {
371 return memberScope;
372 }
373
374 @Override
375 public boolean isError() {
376 return true;
377 }
378
379 @NotNull
380 @Override
381 public Annotations getAnnotations() {
382 return Annotations.EMPTY;
383 }
384
385 @Override
386 public String toString() {
387 return constructor.toString();
388 }
389 }
390
391 @NotNull
392 public static ModuleDescriptor getErrorModule() {
393 return ERROR_MODULE;
394 }
395
396 public static boolean isUninferredParameter(@Nullable JetType type) {
397 return type instanceof UninferredParameterType;
398 }
399
400 public static boolean containsUninferredParameter(@Nullable JetType type) {
401 return TypeUtils.containsSpecialType(type, new Function1<JetType, Boolean>() {
402 @Override
403 public Boolean invoke(JetType argumentType) {
404 return isUninferredParameter(argumentType);
405 }
406 });
407 }
408
409 public static UninferredParameterType createUninferredParameterType(@NotNull TypeParameterDescriptor typeParameterDescriptor) {
410 return new UninferredParameterType(typeParameterDescriptor);
411 }
412
413 public static class UninferredParameterType extends ErrorTypeImpl {
414 private final TypeParameterDescriptor typeParameterDescriptor;
415
416 private UninferredParameterType(
417 @NotNull TypeParameterDescriptor descriptor
418 ) {
419 super(createErrorTypeConstructorWithCustomDebugName("CANT_INFER_TYPE_PARAMETER: " + descriptor.getName()),
420 createErrorScope("Scope for error type for not inferred parameter: " + descriptor.getName()));
421 typeParameterDescriptor = descriptor;
422 }
423
424 @NotNull
425 public TypeParameterDescriptor getTypeParameterDescriptor() {
426 return typeParameterDescriptor;
427 }
428 }
429
430 private ErrorUtils() {}
431 }