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.ScriptDescriptorImpl;
023    import org.jetbrains.jet.lang.descriptors.SimpleFunctionDescriptor;
024    import org.jetbrains.jet.lang.descriptors.impl.PropertyDescriptorImpl;
025    import org.jetbrains.jet.lang.psi.*;
026    import org.jetbrains.jet.lang.resolve.calls.autocasts.DataFlowInfo;
027    import org.jetbrains.jet.lang.resolve.lazy.ForceResolveUtil;
028    import org.jetbrains.jet.lang.resolve.lazy.data.DataPackage;
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    
044    // SCRIPT: resolve symbols in scripts
045    public class ScriptBodyResolver {
046    
047        @NotNull
048        private ExpressionTypingServices expressionTypingServices;
049    
050        @Inject
051        public void setExpressionTypingServices(@NotNull ExpressionTypingServices expressionTypingServices) {
052            this.expressionTypingServices = expressionTypingServices;
053        }
054    
055    
056    
057        public void resolveScriptBodies(@NotNull BodiesResolveContext c, @NotNull BindingTrace trace) {
058            for (Map.Entry<JetScript, ScriptDescriptor> e : c.getScripts().entrySet()) {
059                JetScript declaration = e.getKey();
060                ScriptDescriptor descriptor = e.getValue();
061    
062                if (c.getTopDownAnalysisParameters().isLazyTopDownAnalysis()) {
063                    ForceResolveUtil.forceResolveAllContents(descriptor);
064                    continue;
065                }
066    
067                ScriptDescriptorImpl descriptorImpl = (ScriptDescriptorImpl) descriptor;
068    
069                // TODO: lock in resolveScriptDeclarations
070                descriptorImpl.getScopeForBodyResolution().changeLockLevel(WritableScope.LockLevel.READING);
071    
072                JetType returnType = resolveScriptReturnType(declaration, descriptor, trace);
073    
074                List<PropertyDescriptorImpl> properties = new ArrayList<PropertyDescriptorImpl>();
075                List<SimpleFunctionDescriptor> functions = new ArrayList<SimpleFunctionDescriptor>();
076    
077                BindingContext bindingContext = trace.getBindingContext();
078                for (JetDeclaration jetDeclaration : declaration.getDeclarations()) {
079                    if (jetDeclaration instanceof JetProperty) {
080                        if (!DataPackage.shouldBeScriptClassMember(jetDeclaration)) continue;
081    
082                        PropertyDescriptorImpl propertyDescriptor = (PropertyDescriptorImpl) bindingContext.get(BindingContext.VARIABLE, jetDeclaration);
083                        properties.add(propertyDescriptor);
084                    }
085                    else if (jetDeclaration instanceof JetNamedFunction) {
086                        if (!DataPackage.shouldBeScriptClassMember(jetDeclaration)) continue;
087    
088                        SimpleFunctionDescriptor function = bindingContext.get(BindingContext.FUNCTION, jetDeclaration);
089                        assert function != null;
090                        functions.add(function.copy(descriptor.getClassDescriptor(), function.getModality(), function.getVisibility(),
091                                                    CallableMemberDescriptor.Kind.DECLARATION, false));
092                    }
093                }
094    
095                descriptorImpl.initialize(returnType, properties, functions);
096            }
097        }
098    
099        @NotNull
100        public JetType resolveScriptReturnType(
101                @NotNull JetScript script,
102                @NotNull ScriptDescriptor scriptDescriptor,
103                @NotNull BindingTrace trace
104        ) {
105            // Resolve all contents of the script
106            ExpressionTypingContext context = ExpressionTypingContext.newContext(
107                    expressionTypingServices,
108                    trace,
109                    scriptDescriptor.getScopeForBodyResolution(),
110                    DataFlowInfo.EMPTY,
111                    NO_EXPECTED_TYPE
112            );
113            JetType returnType = expressionTypingServices.getBlockReturnedType(script.getBlockExpression(), CoercionStrategy.NO_COERCION, context).getType();
114            if (returnType == null) {
115                returnType = ErrorUtils.createErrorType("getBlockReturnedType returned null");
116            }
117            return returnType;
118        }
119    }