[FIR] Don't create initializers for simple enum entries
Usually FIR enum entry is initialized by anonymous object, which is the container for all enum entry' declarations. However, for simple enum entries there is no need of initializer at all.
This commit is contained in:
+21
-5
@@ -316,6 +316,12 @@ class DeclarationsConverter(
|
||||
}
|
||||
}
|
||||
|
||||
private fun LighterASTNode.hasValueParameters(): Boolean {
|
||||
return getChildNodesByType(VALUE_PARAMETER_LIST).let {
|
||||
it.isNotEmpty() && it.first().getChildNodesByType(VALUE_PARAMETER).isNotEmpty()
|
||||
}
|
||||
}
|
||||
|
||||
/***** DECLARATIONS *****/
|
||||
/**
|
||||
* @see org.jetbrains.kotlin.parsing.KotlinParsing.parseClassOrObject
|
||||
@@ -417,9 +423,12 @@ class DeclarationsConverter(
|
||||
|
||||
this.superTypeRefs += superTypeRefs
|
||||
|
||||
val secondaryConstructors = classBody.getChildNodesByType(SECONDARY_CONSTRUCTOR)
|
||||
val classWrapper = ClassWrapper(
|
||||
className, modifiers, classKind, primaryConstructor != null,
|
||||
classBody.getChildNodesByType(SECONDARY_CONSTRUCTOR).isNotEmpty(),
|
||||
secondaryConstructors.isNotEmpty(),
|
||||
if (primaryConstructor != null) !primaryConstructor!!.hasValueParameters()
|
||||
else secondaryConstructors.isEmpty() || secondaryConstructors.any { !it.hasValueParameters() },
|
||||
selfType,
|
||||
delegatedSuperTypeRef ?: defaultDelegatedSuperTypeRef, superTypeCallEntry
|
||||
)
|
||||
@@ -503,6 +512,7 @@ class DeclarationsConverter(
|
||||
val classWrapper = ClassWrapper(
|
||||
SpecialNames.NO_NAME_PROVIDED, modifiers, ClassKind.OBJECT, hasPrimaryConstructor = false,
|
||||
hasSecondaryConstructor = classBody.getChildNodesByType(SECONDARY_CONSTRUCTOR).isNotEmpty(),
|
||||
hasDefaultConstructor = false,
|
||||
delegatedSelfTypeRef = delegatedType,
|
||||
delegatedSuperTypeRef = delegatedType,
|
||||
superTypeCallEntry = superTypeCallEntry
|
||||
@@ -541,6 +551,15 @@ class DeclarationsConverter(
|
||||
session = baseSession
|
||||
returnTypeRef = classWrapper.delegatedSelfTypeRef
|
||||
name = enumEntryName
|
||||
symbol = FirVariableSymbol(CallableId(context.currentClassId, enumEntryName))
|
||||
status = FirDeclarationStatusImpl(Visibilities.PUBLIC, Modality.FINAL).apply {
|
||||
isStatic = true
|
||||
}
|
||||
if (classWrapper.hasDefaultConstructor && enumEntry.getChildNodeByType(INITIALIZER_LIST) == null &&
|
||||
modifiers.annotations.isEmpty() && classBodyNode == null
|
||||
) {
|
||||
return@buildEnumEntry
|
||||
}
|
||||
initializer = withChildClassName(enumEntryName) {
|
||||
buildAnonymousObject {
|
||||
source = this@buildEnumEntry.source
|
||||
@@ -552,6 +571,7 @@ class DeclarationsConverter(
|
||||
val enumClassWrapper = ClassWrapper(
|
||||
enumEntryName, modifiers, ClassKind.ENUM_ENTRY, hasPrimaryConstructor = true,
|
||||
hasSecondaryConstructor = classBodyNode.getChildNodesByType(SECONDARY_CONSTRUCTOR).isNotEmpty(),
|
||||
hasDefaultConstructor = false,
|
||||
delegatedSelfTypeRef = buildResolvedTypeRef {
|
||||
type = ConeClassLikeTypeImpl(
|
||||
this@buildAnonymousObject.symbol.toLookupTag(),
|
||||
@@ -567,10 +587,6 @@ class DeclarationsConverter(
|
||||
classBodyNode?.also { declarations += convertClassBody(it, enumClassWrapper) }
|
||||
}
|
||||
}
|
||||
symbol = FirVariableSymbol(CallableId(context.currentClassId, enumEntryName))
|
||||
status = FirDeclarationStatusImpl(Visibilities.PUBLIC, Modality.FINAL).apply {
|
||||
isStatic = true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -21,6 +21,7 @@ class ClassWrapper(
|
||||
private val classKind: ClassKind,
|
||||
val hasPrimaryConstructor: Boolean,
|
||||
val hasSecondaryConstructor: Boolean,
|
||||
val hasDefaultConstructor: Boolean,
|
||||
val delegatedSelfTypeRef: FirTypeRef,
|
||||
val delegatedSuperTypeRef: FirTypeRef,
|
||||
val superTypeCallEntry: MutableList<FirExpression>
|
||||
|
||||
@@ -154,16 +154,25 @@ class RawFirBuilder(
|
||||
delegatedSuperType: FirTypeRef?, delegatedSelfType: FirResolvedTypeRef?, owner: KtClassOrObject, hasPrimaryConstructor: Boolean,
|
||||
): FirDeclaration {
|
||||
return when (this) {
|
||||
is KtSecondaryConstructor -> toFirConstructor(
|
||||
delegatedSuperType,
|
||||
delegatedSelfType ?: buildErrorTypeRef {
|
||||
source = this@toFirDeclaration.toFirSourceElement()
|
||||
diagnostic = FirSimpleDiagnostic("Constructor in object", DiagnosticKind.ConstructorInObject)
|
||||
},
|
||||
owner,
|
||||
hasPrimaryConstructor,
|
||||
)
|
||||
is KtEnumEntry -> toFirEnumEntry(delegatedSelfType!!)
|
||||
is KtSecondaryConstructor -> {
|
||||
toFirConstructor(
|
||||
delegatedSuperType,
|
||||
delegatedSelfType ?: buildErrorTypeRef {
|
||||
source = this@toFirDeclaration.toFirSourceElement()
|
||||
diagnostic = FirSimpleDiagnostic("Constructor in object", DiagnosticKind.ConstructorInObject)
|
||||
},
|
||||
owner,
|
||||
hasPrimaryConstructor,
|
||||
)
|
||||
}
|
||||
is KtEnumEntry -> {
|
||||
val primaryConstructor = owner.primaryConstructor
|
||||
val ownerClassHasDefaultConstructor =
|
||||
primaryConstructor?.valueParameters?.isEmpty() ?: owner.secondaryConstructors.let { constructors ->
|
||||
constructors.isEmpty() || constructors.any { it.valueParameters.isEmpty() }
|
||||
}
|
||||
toFirEnumEntry(delegatedSelfType!!, ownerClassHasDefaultConstructor)
|
||||
}
|
||||
else -> convert()
|
||||
}
|
||||
}
|
||||
@@ -518,13 +527,26 @@ class RawFirBuilder(
|
||||
}
|
||||
}
|
||||
|
||||
private fun KtEnumEntry.toFirEnumEntry(delegatedEnumSelfTypeRef: FirResolvedTypeRef): FirDeclaration {
|
||||
private fun KtEnumEntry.toFirEnumEntry(
|
||||
delegatedEnumSelfTypeRef: FirResolvedTypeRef,
|
||||
ownerClassHasDefaultConstructor: Boolean
|
||||
): FirDeclaration {
|
||||
val ktEnumEntry = this@toFirEnumEntry
|
||||
return buildEnumEntry {
|
||||
source = toFirSourceElement()
|
||||
session = baseSession
|
||||
returnTypeRef = delegatedEnumSelfTypeRef
|
||||
name = nameAsSafeName
|
||||
status = FirDeclarationStatusImpl(Visibilities.PUBLIC, Modality.FINAL).apply {
|
||||
isStatic = true
|
||||
}
|
||||
symbol = FirVariableSymbol(callableIdForName(nameAsSafeName))
|
||||
// NB: not sure should annotations be on enum entry itself, or on its corresponding object
|
||||
if (ownerClassHasDefaultConstructor && ktEnumEntry.initializerList == null &&
|
||||
ktEnumEntry.annotationEntries.isEmpty() && ktEnumEntry.body == null
|
||||
) {
|
||||
return@buildEnumEntry
|
||||
}
|
||||
initializer = withChildClassName(nameAsSafeName) {
|
||||
buildAnonymousObject {
|
||||
source = toFirSourceElement()
|
||||
@@ -559,12 +581,6 @@ class RawFirBuilder(
|
||||
}
|
||||
}
|
||||
}
|
||||
status = FirDeclarationStatusImpl(
|
||||
Visibilities.PUBLIC, Modality.FINAL,
|
||||
).apply {
|
||||
isStatic = true
|
||||
}
|
||||
symbol = FirVariableSymbol(callableIdForName(nameAsSafeName))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -30,4 +30,17 @@ enum class Planet(val m: Double, internal val r: Double) {
|
||||
companion object {
|
||||
const val G = 6.67e-11
|
||||
}
|
||||
}
|
||||
|
||||
enum class PseudoInsn(val signature: String = "()V") {
|
||||
FIX_STACK_BEFORE_JUMP,
|
||||
FAKE_ALWAYS_TRUE_IFEQ("()I"),
|
||||
FAKE_ALWAYS_FALSE_IFEQ("()I"),
|
||||
SAVE_STACK_BEFORE_TRY,
|
||||
RESTORE_STACK_IN_TRY_CATCH,
|
||||
STORE_NOT_NULL,
|
||||
AS_NOT_NULL("(Ljava/lang/Object;)Ljava/lang/Object;")
|
||||
;
|
||||
|
||||
fun emit() {}
|
||||
}
|
||||
+70
-21
@@ -4,27 +4,9 @@ FILE: enums.kt
|
||||
super<R|kotlin/Enum<Order>|>()
|
||||
}
|
||||
|
||||
public final static enum entry FIRST: R|Order| = object : R|Order| {
|
||||
public? constructor(): R|anonymous| {
|
||||
super<R|Order|>()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public final static enum entry SECOND: R|Order| = object : R|Order| {
|
||||
public? constructor(): R|anonymous| {
|
||||
super<R|Order|>()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public final static enum entry THIRD: R|Order| = object : R|Order| {
|
||||
public? constructor(): R|anonymous| {
|
||||
super<R|Order|>()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public final static enum entry FIRST: R|Order|
|
||||
public final static enum entry SECOND: R|Order|
|
||||
public final static enum entry THIRD: R|Order|
|
||||
public final static fun values(): R|kotlin/Array<Order>| {
|
||||
}
|
||||
|
||||
@@ -98,3 +80,70 @@ FILE: enums.kt
|
||||
}
|
||||
|
||||
}
|
||||
public? final? enum class PseudoInsn : R|kotlin/Enum<PseudoInsn>| {
|
||||
public? constructor(signature: String = String(()V)): R|PseudoInsn| {
|
||||
super<R|kotlin/Enum<PseudoInsn>|>()
|
||||
}
|
||||
|
||||
public? final? val signature: String = R|<local>/signature|
|
||||
public? get(): String
|
||||
|
||||
public final static enum entry FIX_STACK_BEFORE_JUMP: R|PseudoInsn| = object : R|PseudoInsn| {
|
||||
public? constructor(): R|anonymous| {
|
||||
super<R|PseudoInsn|>()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public final static enum entry FAKE_ALWAYS_TRUE_IFEQ: R|PseudoInsn| = object : R|PseudoInsn| {
|
||||
public? constructor(): R|anonymous| {
|
||||
super<R|PseudoInsn|>(String(()I))
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public final static enum entry FAKE_ALWAYS_FALSE_IFEQ: R|PseudoInsn| = object : R|PseudoInsn| {
|
||||
public? constructor(): R|anonymous| {
|
||||
super<R|PseudoInsn|>(String(()I))
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public final static enum entry SAVE_STACK_BEFORE_TRY: R|PseudoInsn| = object : R|PseudoInsn| {
|
||||
public? constructor(): R|anonymous| {
|
||||
super<R|PseudoInsn|>()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public final static enum entry RESTORE_STACK_IN_TRY_CATCH: R|PseudoInsn| = object : R|PseudoInsn| {
|
||||
public? constructor(): R|anonymous| {
|
||||
super<R|PseudoInsn|>()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public final static enum entry STORE_NOT_NULL: R|PseudoInsn| = object : R|PseudoInsn| {
|
||||
public? constructor(): R|anonymous| {
|
||||
super<R|PseudoInsn|>()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public final static enum entry AS_NOT_NULL: R|PseudoInsn| = object : R|PseudoInsn| {
|
||||
public? constructor(): R|anonymous| {
|
||||
super<R|PseudoInsn|>(String((Ljava/lang/Object;)Ljava/lang/Object;))
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public? final? fun emit(): R|kotlin/Unit| {
|
||||
}
|
||||
|
||||
public final static fun values(): R|kotlin/Array<PseudoInsn>| {
|
||||
}
|
||||
|
||||
public final static fun valueOf(value: R|kotlin/String|): R|PseudoInsn| {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+21
@@ -13,4 +13,25 @@ enum class SomeEnum(val x: Some) {
|
||||
};
|
||||
|
||||
abstract fun check(y: Some): Boolean
|
||||
}
|
||||
|
||||
enum class E {
|
||||
A; // no constructor call needed
|
||||
constructor()
|
||||
}
|
||||
|
||||
enum class EnumClass {
|
||||
E1 {
|
||||
override fun foo() = 1
|
||||
override val bar: String = "a"
|
||||
},
|
||||
|
||||
E2 {
|
||||
|
||||
},
|
||||
|
||||
E3();
|
||||
|
||||
abstract fun foo(): Int
|
||||
abstract val bar: String
|
||||
}
|
||||
+58
@@ -52,3 +52,61 @@ FILE: enum.kt
|
||||
}
|
||||
|
||||
}
|
||||
public final enum class E : R|kotlin/Enum<E>| {
|
||||
public final static enum entry A: R|E|
|
||||
private constructor(): R|E| {
|
||||
super<R|kotlin/Enum<E>|>()
|
||||
}
|
||||
|
||||
public final static fun values(): R|kotlin/Array<E>| {
|
||||
}
|
||||
|
||||
public final static fun valueOf(value: R|kotlin/String|): R|E| {
|
||||
}
|
||||
|
||||
}
|
||||
public final enum class EnumClass : R|kotlin/Enum<EnumClass>| {
|
||||
private constructor(): R|EnumClass| {
|
||||
super<R|kotlin/Enum<EnumClass>|>()
|
||||
}
|
||||
|
||||
public final static enum entry E1: R|EnumClass| = object : R|EnumClass| {
|
||||
private constructor(): R|anonymous| {
|
||||
super<R|EnumClass|>()
|
||||
}
|
||||
|
||||
public final override fun foo(): R|kotlin/Int| {
|
||||
^foo Int(1)
|
||||
}
|
||||
|
||||
public final override val bar: R|kotlin/String| = String(a)
|
||||
public get(): R|kotlin/String|
|
||||
|
||||
}
|
||||
|
||||
public final static enum entry E2: R|EnumClass| = object : R|EnumClass| {
|
||||
private constructor(): R|anonymous| {
|
||||
super<R|EnumClass|>()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public final static enum entry E3: R|EnumClass| = object : R|EnumClass| {
|
||||
private constructor(): R|anonymous| {
|
||||
super<R|EnumClass|>()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public abstract fun foo(): R|kotlin/Int|
|
||||
|
||||
public abstract val bar: R|kotlin/String|
|
||||
public get(): R|kotlin/String|
|
||||
|
||||
public final static fun values(): R|kotlin/Array<EnumClass>| {
|
||||
}
|
||||
|
||||
public final static fun valueOf(value: R|kotlin/String|): R|EnumClass| {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+2
-14
@@ -4,20 +4,8 @@ FILE: enumWithCompanion.kt
|
||||
super<R|kotlin/Enum<EC>|>()
|
||||
}
|
||||
|
||||
public final static enum entry A: R|EC| = object : R|EC| {
|
||||
private constructor(): R|anonymous| {
|
||||
super<R|EC|>()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public final static enum entry B: R|EC| = object : R|EC| {
|
||||
private constructor(): R|anonymous| {
|
||||
super<R|EC|>()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public final static enum entry A: R|EC|
|
||||
public final static enum entry B: R|EC|
|
||||
public final companion object Companion : R|kotlin/Any| {
|
||||
private constructor(): R|EC.Companion| {
|
||||
super<R|kotlin/Any|>()
|
||||
|
||||
@@ -4,27 +4,9 @@ FILE: main.kt
|
||||
super<R|kotlin/Enum<E>|>()
|
||||
}
|
||||
|
||||
public final static enum entry A: R|E| = object : R|E| {
|
||||
private constructor(): R|anonymous| {
|
||||
super<R|E|>()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public final static enum entry B: R|E| = object : R|E| {
|
||||
private constructor(): R|anonymous| {
|
||||
super<R|E|>()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public final static enum entry C: R|E| = object : R|E| {
|
||||
private constructor(): R|anonymous| {
|
||||
super<R|E|>()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public final static enum entry A: R|E|
|
||||
public final static enum entry B: R|E|
|
||||
public final static enum entry C: R|E|
|
||||
public final static fun values(): R|kotlin/Array<E>| {
|
||||
}
|
||||
|
||||
|
||||
@@ -4,27 +4,9 @@ FILE: exhaustiveness_enum.kt
|
||||
super<R|kotlin/Enum<Enum>|>()
|
||||
}
|
||||
|
||||
public final static enum entry A: R|Enum| = object : R|Enum| {
|
||||
private constructor(): R|anonymous| {
|
||||
super<R|Enum|>()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public final static enum entry B: R|Enum| = object : R|Enum| {
|
||||
private constructor(): R|anonymous| {
|
||||
super<R|Enum|>()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public final static enum entry C: R|Enum| = object : R|Enum| {
|
||||
private constructor(): R|anonymous| {
|
||||
super<R|Enum|>()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public final static enum entry A: R|Enum|
|
||||
public final static enum entry B: R|Enum|
|
||||
public final static enum entry C: R|Enum|
|
||||
public final static fun values(): R|kotlin/Array<Enum>| {
|
||||
}
|
||||
|
||||
|
||||
@@ -4,20 +4,8 @@ FILE: enumEntryUse.kt
|
||||
super<R|kotlin/Enum<TestEnum>|>()
|
||||
}
|
||||
|
||||
public final static enum entry FIRST: R|TestEnum| = object : R|TestEnum| {
|
||||
private constructor(): R|anonymous| {
|
||||
super<R|TestEnum|>()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public final static enum entry SECOND: R|TestEnum| = object : R|TestEnum| {
|
||||
private constructor(): R|anonymous| {
|
||||
super<R|TestEnum|>()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public final static enum entry FIRST: R|TestEnum|
|
||||
public final static enum entry SECOND: R|TestEnum|
|
||||
public final static enum entry THIRD: R|TestEnum| = object : R|TestEnum| {
|
||||
private constructor(): R|anonymous| {
|
||||
super<R|TestEnum|>()
|
||||
|
||||
@@ -4,27 +4,9 @@ FILE: enumValues.kt
|
||||
super<R|kotlin/Enum<MyEnum>|>()
|
||||
}
|
||||
|
||||
public final static enum entry FIRST: R|MyEnum| = object : R|MyEnum| {
|
||||
private constructor(): R|anonymous| {
|
||||
super<R|MyEnum|>()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public final static enum entry SECOND: R|MyEnum| = object : R|MyEnum| {
|
||||
private constructor(): R|anonymous| {
|
||||
super<R|MyEnum|>()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public final static enum entry LAST: R|MyEnum| = object : R|MyEnum| {
|
||||
private constructor(): R|anonymous| {
|
||||
super<R|MyEnum|>()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public final static enum entry FIRST: R|MyEnum|
|
||||
public final static enum entry SECOND: R|MyEnum|
|
||||
public final static enum entry LAST: R|MyEnum|
|
||||
public final fun bar(): R|kotlin/Int| {
|
||||
^bar Int(42)
|
||||
}
|
||||
|
||||
+1
-7
@@ -33,13 +33,7 @@ FILE: qualifiedExpressions.kt
|
||||
super<R|kotlin/Enum<a/b/E>|>()
|
||||
}
|
||||
|
||||
public final static enum entry entry: R|a/b/E| = object : R|a/b/E| {
|
||||
private constructor(): R|anonymous| {
|
||||
super<R|a/b/E|>()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public final static enum entry entry: R|a/b/E|
|
||||
public final static fun values(): R|kotlin/Array<a/b/E>| {
|
||||
}
|
||||
|
||||
|
||||
@@ -66,13 +66,7 @@ FILE: qualifierPriority.kt
|
||||
super<R|kotlin/Enum<G>|>()
|
||||
}
|
||||
|
||||
public final static enum entry H: R|G| = object : R|G| {
|
||||
private constructor(): R|anonymous| {
|
||||
super<R|G|>()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public final static enum entry H: R|G|
|
||||
public final fun foo(): R|kotlin/Unit| {
|
||||
R|/G.values|()
|
||||
}
|
||||
|
||||
+3
-21
@@ -4,27 +4,9 @@ FILE: enums.kt
|
||||
super<R|kotlin/Enum<Order>|>()
|
||||
}
|
||||
|
||||
public final static enum entry FIRST: R|Order| = object : R|Order| {
|
||||
private constructor(): R|anonymous| {
|
||||
super<R|Order|>()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public final static enum entry SECOND: R|Order| = object : R|Order| {
|
||||
private constructor(): R|anonymous| {
|
||||
super<R|Order|>()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public final static enum entry THIRD: R|Order| = object : R|Order| {
|
||||
private constructor(): R|anonymous| {
|
||||
super<R|Order|>()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public final static enum entry FIRST: R|Order|
|
||||
public final static enum entry SECOND: R|Order|
|
||||
public final static enum entry THIRD: R|Order|
|
||||
public final static fun values(): R|kotlin/Array<Order>| {
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user