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