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