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.kotlin.descriptors.ScriptDescriptor;
021 import org.jetbrains.kotlin.psi.JetScript;
022 import org.jetbrains.kotlin.resolve.calls.smartcasts.DataFlowInfo;
023 import org.jetbrains.kotlin.resolve.lazy.ForceResolveUtil;
024 import org.jetbrains.kotlin.types.ErrorUtils;
025 import org.jetbrains.kotlin.types.JetType;
026 import org.jetbrains.kotlin.types.expressions.CoercionStrategy;
027 import org.jetbrains.kotlin.types.expressions.ExpressionTypingContext;
028 import org.jetbrains.kotlin.types.expressions.ExpressionTypingServices;
029
030 import java.util.Map;
031
032 import static org.jetbrains.kotlin.types.TypeUtils.NO_EXPECTED_TYPE;
033
034
035 // SCRIPT: resolve symbols in scripts
036 public class ScriptBodyResolver {
037
038 @NotNull private final ExpressionTypingServices expressionTypingServices;
039
040 public ScriptBodyResolver(
041 @NotNull ExpressionTypingServices expressionTypingServices
042 ) {
043 this.expressionTypingServices = expressionTypingServices;
044 }
045
046 public void resolveScriptBodies(@NotNull BodiesResolveContext c) {
047 for (Map.Entry<JetScript, ScriptDescriptor> e : c.getScripts().entrySet()) {
048 ScriptDescriptor descriptor = e.getValue();
049 ForceResolveUtil.forceResolveAllContents(descriptor);
050 }
051 }
052
053 @NotNull
054 public JetType resolveScriptReturnType(
055 @NotNull JetScript script,
056 @NotNull ScriptDescriptor scriptDescriptor,
057 @NotNull BindingTrace trace
058 ) {
059 // Resolve all contents of the script
060 ExpressionTypingContext context = ExpressionTypingContext.newContext(
061 trace,
062 scriptDescriptor.getScopeForBodyResolution(),
063 DataFlowInfo.EMPTY,
064 NO_EXPECTED_TYPE
065 );
066 JetType returnType = expressionTypingServices.getBlockReturnedType(script.getBlockExpression(), CoercionStrategy.NO_COERCION, context).getType();
067 if (returnType == null) {
068 returnType = ErrorUtils.createErrorType("getBlockReturnedType returned null");
069 }
070 return returnType;
071 }
072 }