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    
017    package org.jetbrains.jet.lang.resolve;
018    
019    import org.jetbrains.annotations.NotNull;
020    import org.jetbrains.jet.lang.descriptors.CallableMemberDescriptor;
021    import org.jetbrains.jet.lang.descriptors.ScriptDescriptor;
022    import org.jetbrains.jet.lang.descriptors.SimpleFunctionDescriptor;
023    import org.jetbrains.jet.lang.descriptors.impl.PropertyDescriptorImpl;
024    import org.jetbrains.jet.lang.psi.JetDeclaration;
025    import org.jetbrains.jet.lang.psi.JetNamedFunction;
026    import org.jetbrains.jet.lang.psi.JetProperty;
027    import org.jetbrains.jet.lang.psi.JetScript;
028    import org.jetbrains.jet.lang.resolve.calls.autocasts.DataFlowInfo;
029    import org.jetbrains.jet.lang.resolve.scopes.WritableScope;
030    import org.jetbrains.jet.lang.types.ErrorUtils;
031    import org.jetbrains.jet.lang.types.JetType;
032    import org.jetbrains.jet.lang.types.expressions.CoercionStrategy;
033    import org.jetbrains.jet.lang.types.expressions.ExpressionTypingContext;
034    import org.jetbrains.jet.lang.types.expressions.ExpressionTypingServices;
035    
036    import javax.inject.Inject;
037    import java.util.ArrayList;
038    import java.util.List;
039    import java.util.Map;
040    
041    import static org.jetbrains.jet.lang.types.TypeUtils.NO_EXPECTED_TYPE;
042    
043    public class ScriptBodyResolver {
044    
045        @NotNull
046        private TopDownAnalysisContext context;
047        @NotNull
048        private ExpressionTypingServices expressionTypingServices;
049        @NotNull
050        private BindingTrace trace;
051    
052        @Inject
053        public void setContext(@NotNull TopDownAnalysisContext context) {
054            this.context = context;
055        }
056    
057        @Inject
058        public void setExpressionTypingServices(@NotNull ExpressionTypingServices expressionTypingServices) {
059            this.expressionTypingServices = expressionTypingServices;
060        }
061    
062        @Inject
063        public void setTrace(@NotNull BindingTrace trace) {
064            this.trace = trace;
065        }
066    
067    
068    
069        public void resolveScriptBodies() {
070            for (Map.Entry<JetScript, ScriptDescriptor> e : context.getScripts().entrySet()) {
071                JetScript declaration = e.getKey();
072                ScriptDescriptor descriptor = e.getValue();
073                WritableScope scope = context.getScriptScopes().get(declaration);
074    
075                // TODO: lock in resolveScriptDeclarations
076                scope.changeLockLevel(WritableScope.LockLevel.READING);
077    
078                ExpressionTypingContext context = ExpressionTypingContext.newContext(
079                        expressionTypingServices,
080                        trace,
081                        scope,
082                        DataFlowInfo.EMPTY,
083                        NO_EXPECTED_TYPE
084                );
085                JetType returnType = expressionTypingServices.getBlockReturnedType(declaration.getBlockExpression(), CoercionStrategy.NO_COERCION, context).getType();
086                if (returnType == null) {
087                    returnType = ErrorUtils.createErrorType("getBlockReturnedType returned null");
088                }
089    
090                List<PropertyDescriptorImpl> properties = new ArrayList<PropertyDescriptorImpl>();
091                List<SimpleFunctionDescriptor> functions = new ArrayList<SimpleFunctionDescriptor>();
092    
093                BindingContext bindingContext = trace.getBindingContext();
094                for (JetDeclaration jetDeclaration : declaration.getDeclarations()) {
095                    if (jetDeclaration instanceof JetProperty) {
096                        properties.add((PropertyDescriptorImpl) bindingContext.get(BindingContext.VARIABLE, jetDeclaration));
097                    }
098                    else if (jetDeclaration instanceof JetNamedFunction) {
099                        SimpleFunctionDescriptor function = bindingContext.get(BindingContext.FUNCTION, jetDeclaration);
100                        assert function != null;
101                        functions.add(function.copy(descriptor.getClassDescriptor(), function.getModality(), function.getVisibility(),
102                                                    CallableMemberDescriptor.Kind.DECLARATION, false));
103                    }
104                }
105    
106                descriptor.initialize(returnType, properties, functions);
107            }
108        }
109    
110    }