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 ExpressionTypingServices expressionTypingServices;
047 @NotNull
048 private BindingTrace trace;
049
050 public void setContext(@NotNull TopDownAnalysisContext context) {
051 }
052
053 @Inject
054 public void setExpressionTypingServices(@NotNull ExpressionTypingServices expressionTypingServices) {
055 this.expressionTypingServices = expressionTypingServices;
056 }
057
058 @Inject
059 public void setTrace(@NotNull BindingTrace trace) {
060 this.trace = trace;
061 }
062
063
064
065 public void resolveScriptBodies(@NotNull BodiesResolveContext c) {
066 for (Map.Entry<JetScript, ScriptDescriptor> e : c.getScripts().entrySet()) {
067 JetScript declaration = e.getKey();
068 ScriptDescriptor descriptor = e.getValue();
069 WritableScope scope = c.getScriptScopes().get(declaration);
070
071 // TODO: lock in resolveScriptDeclarations
072 scope.changeLockLevel(WritableScope.LockLevel.READING);
073
074 ExpressionTypingContext context = ExpressionTypingContext.newContext(
075 expressionTypingServices,
076 trace,
077 scope,
078 DataFlowInfo.EMPTY,
079 NO_EXPECTED_TYPE
080 );
081 JetType returnType = expressionTypingServices.getBlockReturnedType(declaration.getBlockExpression(), CoercionStrategy.NO_COERCION, context).getType();
082 if (returnType == null) {
083 returnType = ErrorUtils.createErrorType("getBlockReturnedType returned null");
084 }
085
086 List<PropertyDescriptorImpl> properties = new ArrayList<PropertyDescriptorImpl>();
087 List<SimpleFunctionDescriptor> functions = new ArrayList<SimpleFunctionDescriptor>();
088
089 BindingContext bindingContext = trace.getBindingContext();
090 for (JetDeclaration jetDeclaration : declaration.getDeclarations()) {
091 if (jetDeclaration instanceof JetProperty) {
092 properties.add((PropertyDescriptorImpl) bindingContext.get(BindingContext.VARIABLE, jetDeclaration));
093 }
094 else if (jetDeclaration instanceof JetNamedFunction) {
095 SimpleFunctionDescriptor function = bindingContext.get(BindingContext.FUNCTION, jetDeclaration);
096 assert function != null;
097 functions.add(function.copy(descriptor.getClassDescriptor(), function.getModality(), function.getVisibility(),
098 CallableMemberDescriptor.Kind.DECLARATION, false));
099 }
100 }
101
102 descriptor.initialize(returnType, properties, functions);
103 }
104 }
105
106 }