001    /*
002     * Copyright 2010-2015 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.kotlin.resolve;
018    
019    import org.jetbrains.annotations.NotNull;
020    import org.jetbrains.annotations.Nullable;
021    import org.jetbrains.kotlin.builtins.KotlinBuiltIns;
022    import org.jetbrains.kotlin.cfg.ControlFlowInformationProvider;
023    import org.jetbrains.kotlin.descriptors.PropertyAccessorDescriptor;
024    import org.jetbrains.kotlin.descriptors.PropertyDescriptor;
025    import org.jetbrains.kotlin.descriptors.SimpleFunctionDescriptor;
026    import org.jetbrains.kotlin.psi.*;
027    import org.jetbrains.kotlin.types.KotlinType;
028    
029    import java.util.Map;
030    
031    import static org.jetbrains.kotlin.types.TypeUtils.NO_EXPECTED_TYPE;
032    
033    public class ControlFlowAnalyzer {
034        @NotNull private final BindingTrace trace;
035        @NotNull private final KotlinBuiltIns builtIns;
036    
037        public ControlFlowAnalyzer(@NotNull BindingTrace trace, @NotNull KotlinBuiltIns builtIns) {
038            this.trace = trace;
039            this.builtIns = builtIns;
040        }
041    
042        public void process(@NotNull BodiesResolveContext c) {
043            for (KtFile file : c.getFiles()) {
044                checkDeclarationContainer(c, file);
045            }
046            for (KtClassOrObject aClass : c.getDeclaredClasses().keySet()) {
047                checkDeclarationContainer(c, aClass);
048            }
049            for (KtScript script : c.getScripts().keySet()) {
050                checkDeclarationContainer(c, script);
051            }
052            for (KtSecondaryConstructor constructor : c.getSecondaryConstructors().keySet()) {
053                checkSecondaryConstructor(constructor);
054            }
055            for (Map.Entry<KtNamedFunction, SimpleFunctionDescriptor> entry : c.getFunctions().entrySet()) {
056                KtNamedFunction function = entry.getKey();
057                SimpleFunctionDescriptor functionDescriptor = entry.getValue();
058                KotlinType expectedReturnType = !function.hasBlockBody() && !function.hasDeclaredReturnType()
059                                                   ? NO_EXPECTED_TYPE
060                                                   : functionDescriptor.getReturnType();
061                checkFunction(c, function, expectedReturnType);
062            }
063            for (Map.Entry<KtProperty, PropertyDescriptor> entry : c.getProperties().entrySet()) {
064                KtProperty property = entry.getKey();
065                PropertyDescriptor propertyDescriptor = entry.getValue();
066                checkProperty(c, property, propertyDescriptor);
067            }
068        }
069    
070        private void checkSecondaryConstructor(@NotNull KtSecondaryConstructor constructor) {
071            ControlFlowInformationProvider controlFlowInformationProvider = new ControlFlowInformationProvider(constructor, trace);
072            controlFlowInformationProvider.checkDeclaration();
073            controlFlowInformationProvider.checkFunction(builtIns.getUnitType());
074        }
075    
076        private void checkDeclarationContainer(@NotNull BodiesResolveContext c, KtDeclarationContainer declarationContainer) {
077            // A pseudocode of class/object initialization corresponds to a class/object
078            // or initialization of properties corresponds to a package declared in a file
079            ControlFlowInformationProvider
080                    controlFlowInformationProvider = new ControlFlowInformationProvider((KtElement) declarationContainer, trace);
081            if (c.getTopDownAnalysisMode().isLocalDeclarations()) {
082                controlFlowInformationProvider.checkForLocalClassOrObjectMode();
083                return;
084            }
085            controlFlowInformationProvider.checkDeclaration();
086        }
087    
088        private void checkProperty(@NotNull BodiesResolveContext c, KtProperty property, PropertyDescriptor propertyDescriptor) {
089            for (KtPropertyAccessor accessor : property.getAccessors()) {
090                PropertyAccessorDescriptor accessorDescriptor = accessor.isGetter()
091                                                                ? propertyDescriptor.getGetter()
092                                                                : propertyDescriptor.getSetter();
093                assert accessorDescriptor != null : "no property accessor descriptor " + accessor.getText();
094                KotlinType returnType = accessorDescriptor.getReturnType();
095                checkFunction(c, accessor, returnType);
096            }
097        }
098    
099        private void checkFunction(@NotNull BodiesResolveContext c, @NotNull KtDeclarationWithBody function, @Nullable KotlinType expectedReturnType) {
100            if (!function.hasBody()) return;
101            ControlFlowInformationProvider controlFlowInformationProvider = new ControlFlowInformationProvider(function, trace);
102            if (c.getTopDownAnalysisMode().isLocalDeclarations()) {
103                controlFlowInformationProvider.checkForLocalClassOrObjectMode();
104                return;
105            }
106            controlFlowInformationProvider.checkDeclaration();
107            controlFlowInformationProvider.checkFunction(expectedReturnType);
108        }
109    }