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    
030    import java.util.Collection;
031    
032    import static org.jetbrains.jet.lang.resolve.DescriptorUtils.getEnumEntriesScope;
033    
034    public final class WhenChecker {
035        private WhenChecker() {
036        }
037    
038        public static boolean isWhenExhaustive(@NotNull JetWhenExpression expression, @NotNull BindingTrace trace) {
039            JetExpression subjectExpression = expression.getSubjectExpression();
040            if (subjectExpression == null) return false;
041            JetType type = trace.get(BindingContext.EXPRESSION_TYPE, subjectExpression);
042            if (type == null) return false;
043            DeclarationDescriptor declarationDescriptor = type.getConstructor().getDeclarationDescriptor();
044            if (!(declarationDescriptor instanceof ClassDescriptor)) return false;
045            ClassDescriptor classDescriptor = (ClassDescriptor) declarationDescriptor;
046            if (classDescriptor.getKind() != ClassKind.ENUM_CLASS || classDescriptor.getModality().isOverridable()) return false;
047            Collection<ClassDescriptor> objectDescriptors = getEnumEntriesScope(classDescriptor).getObjectDescriptors();
048            boolean isExhaust = true;
049            boolean notEmpty = false;
050            for (ClassDescriptor descriptor : objectDescriptors) {
051                if (descriptor.getKind() == ClassKind.ENUM_ENTRY) {
052                    notEmpty = true;
053                    if (!containsEnumEntryCase(expression, descriptor, trace)) {
054                        isExhaust = false;
055                    }
056                }
057            }
058            return isExhaust && notEmpty;
059        }
060    
061        private static boolean containsEnumEntryCase(
062                @NotNull JetWhenExpression whenExpression,
063                @NotNull ClassDescriptor enumEntry,
064                @NotNull BindingTrace trace
065        ) {
066            assert enumEntry.getKind() == ClassKind.ENUM_ENTRY;
067            for (JetWhenEntry whenEntry : whenExpression.getEntries()) {
068                for (JetWhenCondition condition : whenEntry.getConditions()) {
069                    if (!(condition instanceof JetWhenConditionWithExpression)) {
070                        continue;
071                    }
072                    if (isCheckForEnumEntry((JetWhenConditionWithExpression) condition, enumEntry, trace)) {
073                        return true;
074                    }
075                }
076            }
077            return false;
078        }
079    
080        private static boolean isCheckForEnumEntry(
081                @NotNull JetWhenConditionWithExpression whenExpression,
082                @NotNull ClassDescriptor enumEntry,
083                @NotNull BindingTrace trace
084        ) {
085            JetSimpleNameExpression reference = getReference(whenExpression.getExpression());
086            if (reference == null) return false;
087    
088            DeclarationDescriptor target = trace.get(BindingContext.REFERENCE_TARGET, reference);
089            if (!(target instanceof VariableDescriptorForObject)) {
090                return false;
091            }
092    
093            ClassDescriptor classDescriptor = ((VariableDescriptorForObject) target).getObjectClass();
094            return classDescriptor == enumEntry;
095        }
096    
097        @Nullable
098        private static JetSimpleNameExpression getReference(@Nullable JetExpression expression) {
099            if (expression == null) {
100                return null;
101            }
102            if (expression instanceof JetSimpleNameExpression) {
103                return (JetSimpleNameExpression) expression;
104            }
105            if (expression instanceof JetQualifiedExpression) {
106                return getReference(((JetQualifiedExpression) expression).getSelectorExpression());
107            }
108            return null;
109        }
110    }