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.incremental.components.NoLookupLocation;
022    import org.jetbrains.kotlin.psi.*;
023    import org.jetbrains.kotlin.psi.psiUtil.PsiUtilsKt;
024    import org.jetbrains.kotlin.resolve.calls.smartcasts.DataFlowInfo;
025    import org.jetbrains.kotlin.resolve.lazy.descriptors.LazyClassDescriptor;
026    import org.jetbrains.kotlin.resolve.lazy.descriptors.LazyScriptDescriptor;
027    import org.jetbrains.kotlin.resolve.scopes.LexicalScope;
028    
029    public class DeclarationScopeProviderImpl implements DeclarationScopeProvider {
030    
031        private final LazyDeclarationResolver lazyDeclarationResolver;
032    
033        private final FileScopeProvider fileScopeProvider;
034    
035        public DeclarationScopeProviderImpl(
036                @NotNull LazyDeclarationResolver lazyDeclarationResolver,
037                @NotNull FileScopeProvider fileScopeProvider
038        ) {
039            this.lazyDeclarationResolver = lazyDeclarationResolver;
040            this.fileScopeProvider = fileScopeProvider;
041        }
042    
043        @Override
044        @NotNull
045        public LexicalScope getResolutionScopeForDeclaration(@NotNull PsiElement elementOfDeclaration) {
046            KtDeclaration ktDeclaration = KtStubbedPsiUtil.getPsiOrStubParent(elementOfDeclaration, KtDeclaration.class, false);
047    
048            assert !(elementOfDeclaration instanceof KtDeclaration) || ktDeclaration == elementOfDeclaration :
049                    "For JetDeclaration element getParentOfType() should return itself.";
050            assert ktDeclaration != null : "Should be contained inside declaration.";
051    
052            KtDeclaration parentDeclaration = KtStubbedPsiUtil.getContainingDeclaration(ktDeclaration);
053    
054            if (ktDeclaration instanceof KtPropertyAccessor) {
055                parentDeclaration = KtStubbedPsiUtil.getContainingDeclaration(parentDeclaration, KtDeclaration.class);
056            }
057    
058            if (parentDeclaration == null) {
059                return fileScopeProvider.getFileResolutionScope((KtFile) elementOfDeclaration.getContainingFile());
060            }
061    
062            if (parentDeclaration instanceof KtClassOrObject) {
063                KtClassOrObject classOrObject = (KtClassOrObject) parentDeclaration;
064                LazyClassDescriptor classDescriptor = (LazyClassDescriptor) lazyDeclarationResolver.getClassDescriptor(classOrObject, NoLookupLocation.WHEN_GET_DECLARATION_SCOPE);
065                if (ktDeclaration instanceof KtAnonymousInitializer || ktDeclaration instanceof KtProperty) {
066                    return classDescriptor.getScopeForInitializerResolution();
067                }
068                if (ktDeclaration instanceof KtObjectDeclaration
069                    || (ktDeclaration instanceof KtClass && !((KtClass) ktDeclaration).isInner())) {
070                    return classDescriptor.getScopeForStaticMemberDeclarationResolution();
071                }
072    
073                return classDescriptor.getScopeForMemberDeclarationResolution();
074            }
075            //TODO: this is not how it works for classes and for exact parity we can try to use the code above
076            if (parentDeclaration instanceof KtScript) {
077                LazyScriptDescriptor scriptDescriptor = (LazyScriptDescriptor) lazyDeclarationResolver.resolveToDescriptor(parentDeclaration);
078                return scriptDescriptor.getScopeForInitializerResolution();
079            }
080    
081            throw new IllegalStateException("Don't call this method for local declarations: " + ktDeclaration + "\n" +
082                                            PsiUtilsKt.getElementTextWithContext(ktDeclaration));
083        }
084    
085        @NotNull
086        @Override
087        public DataFlowInfo getOuterDataFlowInfoForDeclaration(@NotNull PsiElement elementOfDeclaration) {
088            return DataFlowInfo.EMPTY;
089        }
090    }