[AA] Add KtEnumEntryInitializerSymbol

- An enum entry's body is an initializer with members only accessible
  within that body. Because users of the Analysis API might want to
  analyze the members of the enum entry initializer, we expose this
  initializer via `KtEnumEntrySymbol`. The initializer only exists if
  the enum entry has a body.
  - We already have some usages of the initializer inside symbol light
    classes, which generate a light class for each enum entry, which
    includes the enum entry's hidden members.
- To hide the implementation detail that initializers are anonymous
  objects, `KtEnumEntryInitializerSymbol` is simply a
  `KtSymbolWithMembers`.
- The advantage of making it a `KtSymbolWithMembers`, instead of
  providing a custom way to get a member scope, is that we can pass
  around the initializer easily, e.g. in `KtEnumEntrySymbolRenderer`.
- We implement `KtEnumEntryInitializerSymbol` directly as a
  `KtFirAnonymousObjectSymbol` without a wrapper. This has a few
  advantages:
  1. We can directly benefit from the anonymous object symbol being a
     `KtSymbolWithMembers`, so we don't have to handle enum entry
     initializers specially in e.g. `KtFirScopeProvider`.
  2. We don't have to implement a new symbol restoration mechanism for
     the initializer.
  3. This implementation matches the actual FIR tree structure (with a
     simplification that the connecting anonymous object expression
     between the enum entry and the initializing anonymous object is
     omitted).

^KT-61425 fixed
This commit is contained in:
Marco Pennekamp
2023-08-24 21:54:02 +02:00
committed by Space Team
parent e72a38dc82
commit 80efa34926
21 changed files with 106 additions and 11 deletions
@@ -107,7 +107,17 @@ internal class SymbolLightClassForEnumEntry(
val result = mutableListOf<KtLightField>()
// Then, add instance fields: properties from parameters, and then member properties
addPropertyBackingFields(result, enumEntrySymbol)
enumEntrySymbol.enumEntryInitializer?.let { initializer ->
addPropertyBackingFields(
result,
initializer,
// `addPropertyBackingFields` detects that property fields should be static when the given symbol with members is an
// object. Unfortunately, the enum entry's initializer is an anonymous object, yet we want the enum entry's light class
// to have non-static properties.
forceIsStaticTo = false,
)
}
result
}
@@ -117,11 +127,13 @@ internal class SymbolLightClassForEnumEntry(
enumConstant.withEnumEntrySymbol { enumEntrySymbol ->
val result = mutableListOf<KtLightMethod>()
val declaredMemberScope = enumEntrySymbol.getDeclaredMemberScope()
val visibleDeclarations = declaredMemberScope.getCallableSymbols()
enumEntrySymbol.enumEntryInitializer?.let { initializer ->
val declaredMemberScope = initializer.getDeclaredMemberScope()
val visibleDeclarations = declaredMemberScope.getCallableSymbols()
createMethods(visibleDeclarations, result)
createConstructors(declaredMemberScope.getConstructors(), result)
createMethods(visibleDeclarations, result)
createConstructors(declaredMemberScope.getConstructors(), result)
}
result
}
@@ -619,6 +619,7 @@ context(KtAnalysisSession)
internal fun SymbolLightClassBase.addPropertyBackingFields(
result: MutableList<KtLightField>,
symbolWithMembers: KtSymbolWithMembers,
forceIsStaticTo: Boolean? = null,
) {
val propertySymbols = symbolWithMembers.getDeclaredMemberScope().getCallableSymbols()
.filterIsInstance<KtPropertySymbol>()
@@ -633,7 +634,7 @@ internal fun SymbolLightClassBase.addPropertyBackingFields(
val nameGenerator = SymbolLightField.FieldNameGenerator()
val isStatic = symbolWithMembers is KtClassOrObjectSymbol && symbolWithMembers.classKind.isObject
val isStatic = forceIsStaticTo ?: (symbolWithMembers is KtClassOrObjectSymbol && symbolWithMembers.classKind.isObject)
fun addPropertyBackingField(propertySymbol: KtPropertySymbol) {
createField(
declaration = propertySymbol,
@@ -96,7 +96,8 @@ internal fun KtClassOrObjectSymbol.enumClassModality(): String? {
context(KtAnalysisSession)
private fun KtEnumEntrySymbol.requiresSubClass(): Boolean {
return getDeclaredMemberScope().getAllSymbols().any { it !is KtConstructorSymbol }
val initializer = enumEntryInitializer ?: return false
return initializer.getDeclaredMemberScope().getAllSymbols().any { it !is KtConstructorSymbol }
}
internal fun KtSymbolWithVisibility.toPsiVisibilityForMember(): String = visibility.toPsiVisibilityForMember()