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