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