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