001/* 002 * Copyright 2010-2013 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 017package org.jetbrains.jet.lang.resolve; 018 019import org.jetbrains.annotations.NotNull; 020import org.jetbrains.jet.lang.descriptors.ScriptDescriptor; 021import org.jetbrains.jet.lang.psi.JetScript; 022import org.jetbrains.jet.lang.resolve.calls.autocasts.DataFlowInfo; 023import org.jetbrains.jet.lang.resolve.calls.context.ExpressionPosition; 024import org.jetbrains.jet.lang.resolve.scopes.WritableScope; 025import org.jetbrains.jet.lang.types.ErrorUtils; 026import org.jetbrains.jet.lang.types.JetType; 027import org.jetbrains.jet.lang.types.expressions.CoercionStrategy; 028import org.jetbrains.jet.lang.types.expressions.ExpressionTypingContext; 029import org.jetbrains.jet.lang.types.expressions.ExpressionTypingServices; 030 031import javax.inject.Inject; 032import java.util.Map; 033 034import static org.jetbrains.jet.lang.types.TypeUtils.NO_EXPECTED_TYPE; 035 036public class ScriptBodyResolver { 037 038 @NotNull 039 private TopDownAnalysisContext context; 040 @NotNull 041 private ExpressionTypingServices expressionTypingServices; 042 @NotNull 043 private BindingTrace trace; 044 045 @Inject 046 public void setContext(@NotNull TopDownAnalysisContext context) { 047 this.context = context; 048 } 049 050 @Inject 051 public void setExpressionTypingServices(@NotNull ExpressionTypingServices expressionTypingServices) { 052 this.expressionTypingServices = expressionTypingServices; 053 } 054 055 @Inject 056 public void setTrace(@NotNull BindingTrace trace) { 057 this.trace = trace; 058 } 059 060 061 062 public void resolveScriptBodies() { 063 for (Map.Entry<JetScript, ScriptDescriptor> e : context.getScripts().entrySet()) { 064 JetScript declaration = e.getKey(); 065 ScriptDescriptor descriptor = e.getValue(); 066 WritableScope scope = context.getScriptScopes().get(declaration); 067 068 // TODO: lock in resolveScriptDeclarations 069 scope.changeLockLevel(WritableScope.LockLevel.READING); 070 071 ExpressionTypingContext context = ExpressionTypingContext.newContext( 072 expressionTypingServices, 073 trace, 074 scope, 075 DataFlowInfo.EMPTY, 076 NO_EXPECTED_TYPE, 077 ExpressionPosition.FREE); 078 JetType returnType = expressionTypingServices.getBlockReturnedType(scope, declaration.getBlockExpression(), CoercionStrategy.NO_COERCION, context, trace).getType(); 079 if (returnType == null) { 080 returnType = ErrorUtils.createErrorType("getBlockReturnedType returned null"); 081 } 082 descriptor.initialize(returnType, declaration, trace.getBindingContext()); 083 } 084 } 085 086}