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