@NotNull
 /*package*/ MemberScope getMemberScopeDeclaredIn(
     @NotNull KtDeclaration declaration, @NotNull LookupLocation location) {
   KtDeclaration parentDeclaration = KtStubbedPsiUtil.getContainingDeclaration(declaration);
   boolean isTopLevel = parentDeclaration == null;
   if (isTopLevel) { // for top level declarations we search directly in package because of
     // possible conflicts with imports
     FqName fqName = ((KtFile) declaration.getContainingFile()).getPackageFqName();
     LazyPackageDescriptor packageDescriptor =
         topLevelDescriptorProvider.getPackageFragment(fqName);
     assert packageDescriptor != null;
     return packageDescriptor.getMemberScope();
   } else {
     if (parentDeclaration instanceof KtClassOrObject) {
       return getClassDescriptor((KtClassOrObject) parentDeclaration, location)
           .getUnsubstitutedMemberScope();
     } else if (parentDeclaration instanceof KtScript) {
       return getScriptDescriptor((KtScript) parentDeclaration, location)
           .getUnsubstitutedMemberScope();
     } else {
       throw new IllegalStateException(
           "Don't call this method for local declarations: "
               + declaration
               + "\n"
               + PsiUtilsKt.getElementTextWithContext(declaration));
     }
   }
 }