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