[AA] Provide separate non-static and static declared member scopes

- Member scopes already don't contain static callables, only their
  static member scope counterparts. However, declared member scopes
  contained both non-static and static callables, which was confusing to
  users. See for example KT-61255.
- Now declared member scopes only contain non-static callables and
  static declared member scopes only contain static callables.
- In `KtFirScopeProvider`, the new implementation is different for
  Kotlin and Java classes, because the standard declared member scope
  doesn't work for Java. Instead, we have to get the Java *enhancement*
  scopes from `JavaScopeProvider`. Unfortunately, `JavaScopeProvider`
  doesn't have a direct enhancement declared member scope. This results
  in a somewhat complex scope structure with the declared members filter
  scope around the use-site/static Java enhancement scope, but since the
  declared members filtering scope properly reduces the set of callable
  names and scopes in general are cached, this shouldn't be an issue.
- `getCombinedDeclaredMemberScope` is introduced as a separate public
  function because for Kotlin scopes, we don't actually have to create a
  combined scope, as the non-static and static scopes are just filters
  around a combined declared member scope provided by the compiler. It's
  also important to have a convenient function to get the combined
  declared member scope, because some usages explicitly want access to
  all declared members (such as symbol light classes).
- This commit also fixes KT-61901, because
  `getFirJavaDeclaredMemberScope` now provides a proper static scope for
  Java classes, which will be accessible via the combined declared
  member scope as well.

^KT-61800 fixed
^KT-61901 fixed
^KT-61255 fixed
This commit is contained in:
Marco Pennekamp
2023-09-13 20:58:40 +02:00
committed by Space Team
parent 8b5f87f15f
commit 71017298d9
6 changed files with 181 additions and 55 deletions
@@ -24,6 +24,10 @@ public abstract class KtScopeProvider : KtAnalysisSessionComponent() {
public abstract fun getDeclaredMemberScope(classSymbol: KtSymbolWithMembers): KtScope
public abstract fun getStaticDeclaredMemberScope(classSymbol: KtSymbolWithMembers): KtScope
public abstract fun getCombinedDeclaredMemberScope(classSymbol: KtSymbolWithMembers): KtScope
public abstract fun getDelegatedMemberScope(classSymbol: KtSymbolWithMembers): KtScope
public abstract fun getStaticMemberScope(symbol: KtSymbolWithMembers): KtScope
@@ -56,9 +60,34 @@ public interface KtScopeProviderMixIn : KtAnalysisSessionMixIn {
public fun KtSymbolWithMembers.getMemberScope(): KtScope =
withValidityAssertion { analysisSession.scopeProvider.getMemberScope(this) }
/**
* Returns a [KtScope] containing the *non-static* callables and all classifiers explicitly declared in the given [KtSymbolWithMembers].
*
* @see getStaticDeclaredMemberScope
*/
public fun KtSymbolWithMembers.getDeclaredMemberScope(): KtScope =
withValidityAssertion { analysisSession.scopeProvider.getDeclaredMemberScope(this) }
/**
* Returns a [KtScope] containing the *static* members explicitly declared in the given [KtSymbolWithMembers].
*
* It is worth noting that, while Java classes may contain declarations of static callables freely, in Kotlin only enum classes define
* static callables. Hence, for non-enum Kotlin classes, it is not expected that the static declared member scope will contain any
* callables.
*
* @see getDeclaredMemberScope
*/
public fun KtSymbolWithMembers.getStaticDeclaredMemberScope(): KtScope =
withValidityAssertion { analysisSession.scopeProvider.getStaticDeclaredMemberScope(this) }
/**
* Returns a [KtScope] containing *all* members explicitly declared in the given [KtSymbolWithMembers].
*
* In contrast to [getDeclaredMemberScope] and [getStaticDeclaredMemberScope], this scope contains both static and non-static members.
*/
public fun KtSymbolWithMembers.getCombinedDeclaredMemberScope(): KtScope =
withValidityAssertion { analysisSession.scopeProvider.getCombinedDeclaredMemberScope(this) }
public fun KtSymbolWithMembers.getDelegatedMemberScope(): KtScope =
withValidityAssertion { analysisSession.scopeProvider.getDelegatedMemberScope(this) }