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 org.jetbrains.annotations.NotNull;
020    import org.jetbrains.annotations.Nullable;
021    import org.jetbrains.jet.lang.PlatformToKotlinClassMap;
022    import org.jetbrains.jet.lang.descriptors.*;
023    import org.jetbrains.jet.lang.descriptors.annotations.AnnotationDescriptor;
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 = new PropertyDescriptorImpl(
247                ERROR_CLASS,
248                Annotations.EMPTY,
249                Modality.OPEN,
250                Visibilities.INTERNAL,
251                true,
252                null,
253                ReceiverParameterDescriptor.NO_RECEIVER_PARAMETER,
254                Name.special("<ERROR PROPERTY>"),
255                ERROR_PROPERTY_TYPE,
256                CallableMemberDescriptor.Kind.DECLARATION);
257        private static final Set<VariableDescriptor> ERROR_PROPERTY_GROUP = Collections.singleton(ERROR_PROPERTY);
258    
259        @NotNull
260        private static SimpleFunctionDescriptor createErrorFunction(@NotNull ErrorScope ownerScope) {
261            ErrorSimpleFunctionDescriptorImpl function = new ErrorSimpleFunctionDescriptorImpl(ownerScope);
262            function.initialize(
263                    null,
264                    ReceiverParameterDescriptor.NO_RECEIVER_PARAMETER,
265                    Collections.<TypeParameterDescriptorImpl>emptyList(), // TODO
266                    Collections.<ValueParameterDescriptor>emptyList(), // TODO
267                    createErrorType("<ERROR FUNCTION RETURN TYPE>"),
268                    Modality.OPEN,
269                    Visibilities.INTERNAL
270            );
271            return function;
272        }
273    
274        @NotNull
275        public static JetType createErrorType(@NotNull String debugMessage) {
276            return new ErrorTypeImpl(createErrorTypeConstructor(debugMessage), createErrorScope(debugMessage));
277        }
278    
279        @NotNull
280        public static JetType createErrorTypeWithCustomDebugName(@NotNull String debugName) {
281            return new ErrorTypeImpl(createErrorTypeConstructorWithCustomDebugName(debugName), createErrorScope(debugName));
282        }
283    
284        @NotNull
285        public static TypeConstructor createErrorTypeConstructor(@NotNull String debugMessage) {
286            return createErrorTypeConstructorWithCustomDebugName("[ERROR : " + debugMessage + "]");
287        }
288    
289        @NotNull
290        private static TypeConstructor createErrorTypeConstructorWithCustomDebugName(@NotNull String debugName) {
291            return new TypeConstructorImpl(ERROR_CLASS, Annotations.EMPTY, false, debugName,
292                                    Collections.<TypeParameterDescriptorImpl>emptyList(),
293                                    Collections.singleton(KotlinBuiltIns.getInstance().getAnyType()));
294        }
295    
296        @NotNull
297        public static ClassDescriptor getErrorClass() {
298            return ERROR_CLASS;
299        }
300    
301        public static boolean containsErrorType(@Nullable JetType type) {
302            if (type == null) return false;
303            if (type instanceof PackageType) return false;
304            if (type.isError()) return true;
305            for (TypeProjection projection : type.getArguments()) {
306                if (containsErrorType(projection.getType())) return true;
307            }
308            return false;
309        }
310    
311        public static boolean isError(@NotNull DeclarationDescriptor candidate) {
312            return isErrorClass(candidate) || isErrorClass(candidate.getContainingDeclaration()) || candidate == ERROR_MODULE;
313        }
314    
315        private static boolean isErrorClass(@Nullable DeclarationDescriptor candidate) {
316            return candidate instanceof ErrorClassDescriptor;
317        }
318    
319        @NotNull
320        public static TypeParameterDescriptor createErrorTypeParameter(int index, @NotNull String debugMessage) {
321            return TypeParameterDescriptorImpl.createWithDefaultBound(
322                    ERROR_CLASS,
323                    Annotations.EMPTY,
324                    false,
325                    Variance.INVARIANT,
326                    Name.special("<ERROR: " + debugMessage + ">"),
327                    index
328            );
329        }
330    
331        private static class ErrorTypeImpl implements JetType {
332            private final TypeConstructor constructor;
333            private final JetScope memberScope;
334    
335            private ErrorTypeImpl(@NotNull TypeConstructor constructor, @NotNull JetScope memberScope) {
336                this.constructor = constructor;
337                this.memberScope = memberScope;
338            }
339    
340            @NotNull
341            @Override
342            public TypeConstructor getConstructor() {
343                return constructor;
344            }
345    
346            @NotNull
347            @Override
348            public List<TypeProjection> getArguments() {
349                return Collections.emptyList();
350            }
351    
352            @Override
353            public boolean isNullable() {
354                return false;
355            }
356    
357            @NotNull
358            @Override
359            public JetScope getMemberScope() {
360                return memberScope;
361            }
362    
363            @Override
364            public boolean isError() {
365                return true;
366            }
367    
368            @NotNull
369            @Override
370            public Annotations getAnnotations() {
371                return Annotations.EMPTY;
372            }
373    
374            @Override
375            public String toString() {
376                return constructor.toString();
377            }
378        }
379    
380        @NotNull
381        public static ModuleDescriptor getErrorModule() {
382            return ERROR_MODULE;
383        }
384    
385        private ErrorUtils() {}
386    }