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 parentClassOrObject = (KtClassOrObject) parentDeclaration;
064                LazyClassDescriptor parentClassDescriptor = (LazyClassDescriptor) lazyDeclarationResolver.getClassDescriptor(parentClassOrObject, NoLookupLocation.WHEN_GET_DECLARATION_SCOPE);
065    
066                if (ktDeclaration instanceof KtAnonymousInitializer || ktDeclaration instanceof KtProperty) {
067                    return parentClassDescriptor.getScopeForInitializerResolution();
068                }
069    
070                if (ktDeclaration instanceof KtObjectDeclaration && ((KtObjectDeclaration) ktDeclaration).isCompanion()) {
071                    return parentClassDescriptor.getScopeForCompanionObjectHeaderResolution();
072                }
073    
074                if (ktDeclaration instanceof KtObjectDeclaration ||
075                    ktDeclaration instanceof KtClass && !((KtClass) ktDeclaration).isInner()) {
076                    return parentClassDescriptor.getScopeForStaticMemberDeclarationResolution();
077                }
078    
079                return parentClassDescriptor.getScopeForMemberDeclarationResolution();
080            }
081            //TODO: this is not how it works for classes and for exact parity we can try to use the code above
082            if (parentDeclaration instanceof KtScript) {
083                LazyScriptDescriptor scriptDescriptor = (LazyScriptDescriptor) lazyDeclarationResolver.resolveToDescriptor(parentDeclaration);
084                return scriptDescriptor.getScopeForInitializerResolution();
085            }
086    
087            throw new IllegalStateException("Don't call this method for local declarations: " + ktDeclaration + "\n" +
088                                            PsiUtilsKt.getElementTextWithContext(ktDeclaration));
089        }
090    
091        @NotNull
092        @Override
093        public DataFlowInfo getOuterDataFlowInfoForDeclaration(@NotNull PsiElement elementOfDeclaration) {
094            return DataFlowInfo.EMPTY;
095        }
096    }