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.cfg;
018    
019    import org.jetbrains.annotations.NotNull;
020    import org.jetbrains.annotations.Nullable;
021    import org.jetbrains.jet.lang.descriptors.ClassDescriptor;
022    import org.jetbrains.jet.lang.descriptors.ClassKind;
023    import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor;
024    import org.jetbrains.jet.lang.descriptors.VariableDescriptorForObject;
025    import org.jetbrains.jet.lang.psi.*;
026    import org.jetbrains.jet.lang.resolve.BindingContext;
027    import org.jetbrains.jet.lang.resolve.BindingTrace;
028    import org.jetbrains.jet.lang.types.JetType;
029    import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns;
030    
031    import java.util.Collection;
032    
033    import static org.jetbrains.jet.lang.resolve.DescriptorUtils.getEnumEntriesScope;
034    
035    public final class WhenChecker {
036        private WhenChecker() {
037        }
038    
039        public static boolean mustHaveElse(@NotNull JetWhenExpression expression, @NotNull BindingTrace trace) {
040            JetType expectedType = trace.get(BindingContext.EXPECTED_EXPRESSION_TYPE, expression);
041            boolean isUnit = expectedType != null && KotlinBuiltIns.getInstance().isUnit(expectedType);
042            // Some "statements" are actually expressions returned from lambdas, their expected types are non-null
043            boolean isStatement = Boolean.TRUE.equals(trace.get(BindingContext.STATEMENT, expression)) && expectedType == null;
044    
045            return !isUnit && !isStatement && !isWhenExhaustive(expression, trace);
046        }
047    
048        private static boolean isWhenExhaustive(@NotNull JetWhenExpression expression, @NotNull BindingTrace trace) {
049            JetExpression subjectExpression = expression.getSubjectExpression();
050            if (subjectExpression == null) return false;
051            JetType type = trace.get(BindingContext.EXPRESSION_TYPE, subjectExpression);
052            if (type == null) return false;
053            DeclarationDescriptor declarationDescriptor = type.getConstructor().getDeclarationDescriptor();
054            if (!(declarationDescriptor instanceof ClassDescriptor)) return false;
055            ClassDescriptor classDescriptor = (ClassDescriptor) declarationDescriptor;
056            if (classDescriptor.getKind() != ClassKind.ENUM_CLASS || classDescriptor.getModality().isOverridable()) return false;
057            Collection<ClassDescriptor> objectDescriptors = getEnumEntriesScope(classDescriptor).getObjectDescriptors();
058            boolean isExhaust = true;
059            boolean notEmpty = false;
060            for (ClassDescriptor descriptor : objectDescriptors) {
061                if (descriptor.getKind() == ClassKind.ENUM_ENTRY) {
062                    notEmpty = true;
063                    if (!containsEnumEntryCase(expression, descriptor, trace)) {
064                        isExhaust = false;
065                    }
066                }
067            }
068            boolean exhaustive = isExhaust && notEmpty;
069            if (exhaustive) {
070                trace.record(BindingContext.EXHAUSTIVE_WHEN, expression);
071            }
072            return exhaustive;
073        }
074    
075        private static boolean containsEnumEntryCase(
076                @NotNull JetWhenExpression whenExpression,
077                @NotNull ClassDescriptor enumEntry,
078                @NotNull BindingTrace trace
079        ) {
080            assert enumEntry.getKind() == ClassKind.ENUM_ENTRY;
081            for (JetWhenEntry whenEntry : whenExpression.getEntries()) {
082                for (JetWhenCondition condition : whenEntry.getConditions()) {
083                    if (!(condition instanceof JetWhenConditionWithExpression)) {
084                        continue;
085                    }
086                    if (isCheckForEnumEntry((JetWhenConditionWithExpression) condition, enumEntry, trace)) {
087                        return true;
088                    }
089                }
090            }
091            return false;
092        }
093    
094        private static boolean isCheckForEnumEntry(
095                @NotNull JetWhenConditionWithExpression whenExpression,
096                @NotNull ClassDescriptor enumEntry,
097                @NotNull BindingTrace trace
098        ) {
099            JetSimpleNameExpression reference = getReference(whenExpression.getExpression());
100            if (reference == null) return false;
101    
102            DeclarationDescriptor target = trace.get(BindingContext.REFERENCE_TARGET, reference);
103            if (!(target instanceof VariableDescriptorForObject)) {
104                return false;
105            }
106    
107            ClassDescriptor classDescriptor = ((VariableDescriptorForObject) target).getObjectClass();
108            return classDescriptor == enumEntry;
109        }
110    
111        @Nullable
112        private static JetSimpleNameExpression getReference(@Nullable JetExpression expression) {
113            if (expression == null) {
114                return null;
115            }
116            if (expression instanceof JetSimpleNameExpression) {
117                return (JetSimpleNameExpression) expression;
118            }
119            if (expression instanceof JetQualifiedExpression) {
120                return getReference(((JetQualifiedExpression) expression).getSelectorExpression());
121            }
122            return null;
123        }
124    }