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.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 JetFlowInformationProvider flowInformationProvider = new JetFlowInformationProvider(constructor, trace); 072 flowInformationProvider.checkDeclaration(); 073 flowInformationProvider.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 JetFlowInformationProvider flowInformationProvider = new JetFlowInformationProvider((KtElement) declarationContainer, trace); 080 if (c.getTopDownAnalysisMode().isLocalDeclarations()) { 081 flowInformationProvider.checkForLocalClassOrObjectMode(); 082 return; 083 } 084 flowInformationProvider.checkDeclaration(); 085 } 086 087 private void checkProperty(@NotNull BodiesResolveContext c, KtProperty property, PropertyDescriptor propertyDescriptor) { 088 for (KtPropertyAccessor accessor : property.getAccessors()) { 089 PropertyAccessorDescriptor accessorDescriptor = accessor.isGetter() 090 ? propertyDescriptor.getGetter() 091 : propertyDescriptor.getSetter(); 092 assert accessorDescriptor != null : "no property accessor descriptor " + accessor.getText(); 093 KotlinType returnType = accessorDescriptor.getReturnType(); 094 checkFunction(c, accessor, returnType); 095 } 096 } 097 098 private void checkFunction(@NotNull BodiesResolveContext c, @NotNull KtDeclarationWithBody function, @Nullable KotlinType expectedReturnType) { 099 if (!function.hasBody()) return; 100 JetFlowInformationProvider flowInformationProvider = new JetFlowInformationProvider(function, trace); 101 if (c.getTopDownAnalysisMode().isLocalDeclarations()) { 102 flowInformationProvider.checkForLocalClassOrObjectMode(); 103 return; 104 } 105 flowInformationProvider.checkDeclaration(); 106 flowInformationProvider.checkFunction(expectedReturnType); 107 } 108 }