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 com.google.common.base.Function;
020 import com.intellij.psi.PsiElement;
021 import com.intellij.psi.PsiFile;
022 import org.jetbrains.annotations.NotNull;
023 import org.jetbrains.jet.lang.descriptors.PropertyDescriptor;
024 import org.jetbrains.jet.lang.descriptors.ScriptDescriptor;
025 import org.jetbrains.jet.lang.descriptors.SimpleFunctionDescriptor;
026 import org.jetbrains.jet.lang.descriptors.impl.MutableClassDescriptor;
027 import org.jetbrains.jet.lang.psi.*;
028 import org.jetbrains.jet.lang.resolve.calls.autocasts.DataFlowInfo;
029 import org.jetbrains.jet.lang.resolve.scopes.JetScope;
030 import org.jetbrains.jet.lang.resolve.scopes.WritableScope;
031
032 import java.util.Collection;
033 import java.util.Collections;
034 import java.util.Map;
035
036 /**
037 * A storage for the part of {@see TopDownAnalysisContext} collected during headers analysis that will be used during resolution of
038 * bodies
039 */
040 public class CachedBodiesResolveContext implements BodiesResolveContext {
041 private final Collection<JetFile> files;
042 private final Map<JetClassOrObject, MutableClassDescriptor> classes;
043 private final Map<JetProperty, PropertyDescriptor> properties;
044 private final Map<JetNamedFunction, SimpleFunctionDescriptor> functions;
045 private final Function<JetDeclaration, JetScope> declaringScopes;
046 private final Map<JetScript, ScriptDescriptor> scripts;
047 private final Map<JetScript, WritableScope> scriptScopes;
048 private final DataFlowInfo outerDataFlowInfo;
049
050 private @NotNull TopDownAnalysisParameters topDownAnalysisParameters;
051
052 public CachedBodiesResolveContext(TopDownAnalysisContext context) {
053 files = Collections.unmodifiableCollection(context.getFiles());
054 classes = Collections.unmodifiableMap(context.getClasses());
055 properties = Collections.unmodifiableMap(context.getProperties());
056 functions = Collections.unmodifiableMap(context.getFunctions());
057 declaringScopes = context.getDeclaringScopes();
058 scripts = Collections.unmodifiableMap(context.getScripts());
059 scriptScopes = Collections.unmodifiableMap(context.getScriptScopes());
060 outerDataFlowInfo = context.getOuterDataFlowInfo();
061
062 topDownAnalysisParameters = context.getTopDownAnalysisParameters();
063 }
064
065 @Override
066 public Collection<JetFile> getFiles() {
067 return files;
068 }
069
070 @Override
071 public Map<JetClassOrObject, MutableClassDescriptor> getClasses() {
072 return classes;
073 }
074
075 @Override
076 public Map<JetProperty, PropertyDescriptor> getProperties() {
077 return properties;
078 }
079
080 @Override
081 public Map<JetNamedFunction, SimpleFunctionDescriptor> getFunctions() {
082 return functions;
083 }
084
085 @Override
086 public Function<JetDeclaration, JetScope> getDeclaringScopes() {
087 return declaringScopes;
088 }
089
090 @Override
091 public Map<JetScript, ScriptDescriptor> getScripts() {
092 return scripts;
093 }
094
095 @Override
096 public Map<JetScript, WritableScope> getScriptScopes() {
097 return scriptScopes;
098 }
099
100 @Override
101 public DataFlowInfo getOuterDataFlowInfo() {
102 return outerDataFlowInfo;
103 }
104
105 @Override
106 public void setTopDownAnalysisParameters(@NotNull TopDownAnalysisParameters parameters) {
107 topDownAnalysisParameters = parameters;
108 }
109
110 @Override
111 public boolean completeAnalysisNeeded(@NotNull PsiElement element) {
112 PsiFile containingFile = element.getContainingFile();
113 return containingFile != null && topDownAnalysisParameters.getAnalyzeCompletely().apply(containingFile);
114 }
115 }