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