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