001    /*
002     * Copyright 2010-2015 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.kotlin.resolve.lazy;
018    
019    import com.intellij.psi.PsiElement;
020    import org.jetbrains.annotations.NotNull;
021    import org.jetbrains.kotlin.psi.*;
022    import org.jetbrains.kotlin.resolve.calls.smartcasts.DataFlowInfo;
023    import org.jetbrains.kotlin.resolve.lazy.descriptors.LazyClassDescriptor;
024    import org.jetbrains.kotlin.resolve.scopes.JetScope;
025    
026    import javax.inject.Inject;
027    
028    public class DeclarationScopeProviderImpl implements DeclarationScopeProvider {
029    
030        private final LazyDeclarationResolver lazyDeclarationResolver;
031    
032        private FileScopeProvider fileScopeProvider;
033    
034        @Inject
035        public void setFileScopeProvider(@NotNull FileScopeProvider fileScopeProvider) {
036            this.fileScopeProvider = fileScopeProvider;
037        }
038    
039        public DeclarationScopeProviderImpl(@NotNull LazyDeclarationResolver lazyDeclarationResolver) {
040            this.lazyDeclarationResolver = lazyDeclarationResolver;
041        }
042    
043        @Override
044        @NotNull
045        public JetScope getResolutionScopeForDeclaration(@NotNull PsiElement elementOfDeclaration) {
046            JetDeclaration jetDeclaration = JetStubbedPsiUtil.getPsiOrStubParent(elementOfDeclaration, JetDeclaration.class, false);
047    
048            assert !(elementOfDeclaration instanceof JetDeclaration) || jetDeclaration == elementOfDeclaration :
049                    "For JetDeclaration element getParentOfType() should return itself.";
050            assert jetDeclaration != null : "Should be contained inside declaration.";
051    
052            JetDeclaration parentDeclaration = JetStubbedPsiUtil.getContainingDeclaration(jetDeclaration);
053    
054            if (jetDeclaration instanceof JetPropertyAccessor) {
055                parentDeclaration = JetStubbedPsiUtil.getContainingDeclaration(parentDeclaration, JetDeclaration.class);
056            }
057    
058            if (parentDeclaration == null) {
059                return fileScopeProvider.getFileScope((JetFile) elementOfDeclaration.getContainingFile());
060            }
061    
062            if (parentDeclaration instanceof JetClassOrObject) {
063                JetClassOrObject classOrObject = (JetClassOrObject) parentDeclaration;
064                LazyClassDescriptor classDescriptor = (LazyClassDescriptor) lazyDeclarationResolver.getClassDescriptor(classOrObject);
065                if (jetDeclaration instanceof JetClassInitializer || jetDeclaration instanceof JetProperty) {
066                    return classDescriptor.getScopeForInitializerResolution();
067                }
068                return classDescriptor.getScopeForMemberDeclarationResolution();
069            }
070    
071            throw new IllegalStateException("Don't call this method for local declarations: " + jetDeclaration + "\n" +
072                                            JetPsiUtil.getElementTextWithContext(jetDeclaration));
073        }
074    
075        @NotNull
076        @Override
077        public DataFlowInfo getOuterDataFlowInfoForDeclaration(@NotNull PsiElement elementOfDeclaration) {
078            return DataFlowInfo.EMPTY;
079        }
080    }