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.JetFlowInformationProvider;
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.JetType;
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 (JetFile file : c.getFiles()) {
044 checkDeclarationContainer(c, file);
045 }
046 for (JetClassOrObject aClass : c.getDeclaredClasses().keySet()) {
047 checkDeclarationContainer(c, aClass);
048 }
049 for (JetSecondaryConstructor constructor : c.getSecondaryConstructors().keySet()) {
050 checkSecondaryConstructor(constructor);
051 }
052 for (Map.Entry<JetNamedFunction, SimpleFunctionDescriptor> entry : c.getFunctions().entrySet()) {
053 JetNamedFunction function = entry.getKey();
054 SimpleFunctionDescriptor functionDescriptor = entry.getValue();
055 JetType expectedReturnType = !function.hasBlockBody() && !function.hasDeclaredReturnType()
056 ? NO_EXPECTED_TYPE
057 : functionDescriptor.getReturnType();
058 checkFunction(c, function, expectedReturnType);
059 }
060 for (Map.Entry<JetProperty, PropertyDescriptor> entry : c.getProperties().entrySet()) {
061 JetProperty property = entry.getKey();
062 PropertyDescriptor propertyDescriptor = entry.getValue();
063 checkProperty(c, property, propertyDescriptor);
064 }
065 }
066
067 private void checkSecondaryConstructor(@NotNull JetSecondaryConstructor constructor) {
068 JetFlowInformationProvider flowInformationProvider = new JetFlowInformationProvider(constructor, trace);
069 flowInformationProvider.checkDeclaration();
070 flowInformationProvider.checkFunction(builtIns.getUnitType());
071 }
072
073 private void checkDeclarationContainer(@NotNull BodiesResolveContext c, JetDeclarationContainer declarationContainer) {
074 // A pseudocode of class/object initialization corresponds to a class/object
075 // or initialization of properties corresponds to a package declared in a file
076 JetFlowInformationProvider flowInformationProvider = new JetFlowInformationProvider((JetElement) declarationContainer, trace);
077 if (c.getTopDownAnalysisMode().getIsLocalDeclarations()) {
078 flowInformationProvider.checkForLocalClassOrObjectMode();
079 return;
080 }
081 flowInformationProvider.checkDeclaration();
082 }
083
084 private void checkProperty(@NotNull BodiesResolveContext c, JetProperty property, PropertyDescriptor propertyDescriptor) {
085 for (JetPropertyAccessor accessor : property.getAccessors()) {
086 PropertyAccessorDescriptor accessorDescriptor = accessor.isGetter()
087 ? propertyDescriptor.getGetter()
088 : propertyDescriptor.getSetter();
089 assert accessorDescriptor != null : "no property accessor descriptor " + accessor.getText();
090 JetType returnType = accessorDescriptor.getReturnType();
091 checkFunction(c, accessor, returnType);
092 }
093 }
094
095 private void checkFunction(@NotNull BodiesResolveContext c, @NotNull JetDeclarationWithBody function, @Nullable JetType expectedReturnType) {
096 if (!function.hasBody()) return;
097 JetFlowInformationProvider flowInformationProvider = new JetFlowInformationProvider(function, trace);
098 if (c.getTopDownAnalysisMode().getIsLocalDeclarations()) {
099 flowInformationProvider.checkForLocalClassOrObjectMode();
100 return;
101 }
102 flowInformationProvider.checkDeclaration();
103 flowInformationProvider.checkFunction(expectedReturnType);
104 }
105 }