diff --git a/compiler/fir/lightTree/src/org/jetbrains/kotlin/fir/lightTree/converter/DeclarationsConverter.kt b/compiler/fir/lightTree/src/org/jetbrains/kotlin/fir/lightTree/converter/DeclarationsConverter.kt index 2ab85e53f78..2973120ef06 100644 --- a/compiler/fir/lightTree/src/org/jetbrains/kotlin/fir/lightTree/converter/DeclarationsConverter.kt +++ b/compiler/fir/lightTree/src/org/jetbrains/kotlin/fir/lightTree/converter/DeclarationsConverter.kt @@ -539,31 +539,33 @@ class DeclarationsConverter( session, classWrapper.delegatedSelfTypeRef, name = enumEntryName, - initializer = FirAnonymousObjectImpl( - null, - session, - ClassKind.ENUM_ENTRY, - scopeProvider, - FirAnonymousObjectSymbol() - ).apply { - annotations += modifiers.annotations - val enumClassWrapper = ClassWrapper( - enumEntryName, modifiers, ClassKind.ENUM_ENTRY, hasPrimaryConstructor = true, - hasSecondaryConstructor = classBodyNode.getChildNodesByType(SECONDARY_CONSTRUCTOR).isNotEmpty(), - delegatedSelfTypeRef = FirResolvedTypeRefImpl( - null, - ConeClassLikeTypeImpl( - symbol.toLookupTag(), - emptyArray(), - isNullable = false - ) - ), - delegatedSuperTypeRef = classWrapper.delegatedSelfTypeRef, - superTypeCallEntry = enumSuperTypeCallEntry - ) - superTypeRefs += enumClassWrapper.delegatedSuperTypeRef - convertPrimaryConstructor(null, enumClassWrapper)?.let { declarations += it.firConstructor } - classBodyNode?.also { declarations += convertClassBody(it, enumClassWrapper) } + initializer = withChildClassName(enumEntryName) { + FirAnonymousObjectImpl( + null, + session, + ClassKind.ENUM_ENTRY, + scopeProvider, + FirAnonymousObjectSymbol() + ).apply { + annotations += modifiers.annotations + val enumClassWrapper = ClassWrapper( + enumEntryName, modifiers, ClassKind.ENUM_ENTRY, hasPrimaryConstructor = true, + hasSecondaryConstructor = classBodyNode.getChildNodesByType(SECONDARY_CONSTRUCTOR).isNotEmpty(), + delegatedSelfTypeRef = FirResolvedTypeRefImpl( + null, + ConeClassLikeTypeImpl( + symbol.toLookupTag(), + emptyArray(), + isNullable = false + ) + ), + delegatedSuperTypeRef = classWrapper.delegatedSelfTypeRef, + superTypeCallEntry = enumSuperTypeCallEntry + ) + superTypeRefs += enumClassWrapper.delegatedSuperTypeRef + convertPrimaryConstructor(null, enumClassWrapper)?.let { declarations += it.firConstructor } + classBodyNode?.also { declarations += convertClassBody(it, enumClassWrapper) } + } }, symbol = FirVariableSymbol(CallableId(context.currentClassId, enumEntryName)), status = FirDeclarationStatusImpl(Visibilities.PUBLIC, Modality.FINAL).apply { diff --git a/compiler/fir/psi2fir/src/org/jetbrains/kotlin/fir/builder/RawFirBuilder.kt b/compiler/fir/psi2fir/src/org/jetbrains/kotlin/fir/builder/RawFirBuilder.kt index 8d1d058f26e..fc0cfe28faa 100644 --- a/compiler/fir/psi2fir/src/org/jetbrains/kotlin/fir/builder/RawFirBuilder.kt +++ b/compiler/fir/psi2fir/src/org/jetbrains/kotlin/fir/builder/RawFirBuilder.kt @@ -479,48 +479,49 @@ class RawFirBuilder(session: FirSession, val scopeProvider: FirScopeProvider, va } private fun KtEnumEntry.toFirEnumEntry(delegatedEnumSelfTypeRef: FirResolvedTypeRef): FirDeclaration { - val obj = FirAnonymousObjectImpl( - source = toFirSourceElement(), - session, - ClassKind.ENUM_ENTRY, - scopeProvider, - FirAnonymousObjectSymbol() - ) - - val delegatedEntrySelfType = FirResolvedTypeRefImpl( - source = null, - type = ConeClassLikeTypeImpl(obj.symbol.toLookupTag(), emptyArray(), isNullable = false) - ) - - extractAnnotationsTo(obj) - obj.superTypeRefs += delegatedEnumSelfTypeRef - val superTypeCallEntry = superTypeListEntries.firstIsInstanceOrNull() - val correctedEnumSelfTypeRef = FirResolvedTypeRefImpl( - source = superTypeCallEntry?.calleeExpression?.typeReference?.toFirSourceElement(), - type = delegatedEnumSelfTypeRef.type - ) - obj.declarations += primaryConstructor.toFirConstructor( - superTypeCallEntry, - correctedEnumSelfTypeRef, - delegatedEntrySelfType, - owner = this - ) - - for (declaration in declarations) { - obj.declarations += declaration.toFirDeclaration( - correctedEnumSelfTypeRef, - delegatedSelfType = delegatedEntrySelfType, - this, - hasPrimaryConstructor = true - ) - } - return FirEnumEntryImpl( source = toFirSourceElement(), session, delegatedEnumSelfTypeRef, name = nameAsSafeName, - initializer = obj, + initializer = withChildClassName(nameAsSafeName) { + val obj = FirAnonymousObjectImpl( + source = toFirSourceElement(), + session, + ClassKind.ENUM_ENTRY, + scopeProvider, + FirAnonymousObjectSymbol() + ) + + val delegatedEntrySelfType = FirResolvedTypeRefImpl( + source = null, + type = ConeClassLikeTypeImpl(obj.symbol.toLookupTag(), emptyArray(), isNullable = false) + ) + + extractAnnotationsTo(obj) + obj.superTypeRefs += delegatedEnumSelfTypeRef + val superTypeCallEntry = superTypeListEntries.firstIsInstanceOrNull() + val correctedEnumSelfTypeRef = FirResolvedTypeRefImpl( + source = superTypeCallEntry?.calleeExpression?.typeReference?.toFirSourceElement(), + type = delegatedEnumSelfTypeRef.type + ) + obj.declarations += primaryConstructor.toFirConstructor( + superTypeCallEntry, + correctedEnumSelfTypeRef, + delegatedEntrySelfType, + owner = this + ) + + for (declaration in declarations) { + obj.declarations += declaration.toFirDeclaration( + correctedEnumSelfTypeRef, + delegatedSelfType = delegatedEntrySelfType, + this, + hasPrimaryConstructor = true + ) + } + obj + }, status = FirDeclarationStatusImpl( Visibilities.PUBLIC, Modality.FINAL ).apply { diff --git a/compiler/testData/ir/irText/expressions/enumEntryReferenceFromEnumEntryClass.fir.txt b/compiler/testData/ir/irText/expressions/enumEntryReferenceFromEnumEntryClass.fir.txt index 20a03446546..39738270039 100644 --- a/compiler/testData/ir/irText/expressions/enumEntryReferenceFromEnumEntryClass.fir.txt +++ b/compiler/testData/ir/irText/expressions/enumEntryReferenceFromEnumEntryClass.fir.txt @@ -5,107 +5,95 @@ FILE fqName: fileName:/enumEntryReferenceFromEnumEntryClass.kt BLOCK_BODY DELEGATING_CONSTRUCTOR_CALL 'public constructor () [primary] declared in kotlin.Any' INSTANCE_INITIALIZER_CALL classDescriptor='CLASS ENUM_CLASS name:MyEnum modality:FINAL visibility:public superTypes:[kotlin.Enum<.MyEnum>]' - CLASS ENUM_ENTRY name:Z modality:FINAL visibility:public superTypes:[kotlin.Any] - $this: VALUE_PARAMETER INSTANCE_RECEIVER name: type:.MyEnum.Z - CONSTRUCTOR visibility:public <> () returnType:.MyEnum.Z [primary] - BLOCK_BODY - DELEGATING_CONSTRUCTOR_CALL 'public constructor () [primary] declared in kotlin.Any' - INSTANCE_INITIALIZER_CALL classDescriptor='CLASS ENUM_ENTRY name:Z modality:FINAL visibility:public superTypes:[kotlin.Any]' - PROPERTY name:counter visibility:public modality:FINAL [var] - FIELD PROPERTY_BACKING_FIELD name:counter type:kotlin.Int visibility:private - EXPRESSION_BODY - CONST Int type=kotlin.Int value=0 - FUN DEFAULT_PROPERTY_ACCESSOR name: visibility:public modality:FINAL <> ($this:.MyEnum.Z) returnType:kotlin.Int - correspondingProperty: PROPERTY name:counter visibility:public modality:FINAL [var] + ENUM_ENTRY name:Z + class: CLASS ENUM_ENTRY name:Z modality:FINAL visibility:local superTypes:[.MyEnum] + $this: VALUE_PARAMETER INSTANCE_RECEIVER name: type:.MyEnum.Z + CONSTRUCTOR visibility:private <> () returnType:. [primary] + BLOCK_BODY + DELEGATING_CONSTRUCTOR_CALL 'private constructor () [primary] declared in .MyEnum' + INSTANCE_INITIALIZER_CALL classDescriptor='CLASS ENUM_ENTRY name:Z modality:FINAL visibility:local superTypes:[.MyEnum]' + PROPERTY name:counter visibility:public modality:FINAL [var] + FIELD PROPERTY_BACKING_FIELD name:counter type:kotlin.Int visibility:private + EXPRESSION_BODY + CONST Int type=kotlin.Int value=0 + FUN DEFAULT_PROPERTY_ACCESSOR name: visibility:public modality:FINAL <> ($this:.MyEnum.Z) returnType:kotlin.Int + correspondingProperty: PROPERTY name:counter visibility:public modality:FINAL [var] + $this: VALUE_PARAMETER name: type:.MyEnum.Z + BLOCK_BODY + RETURN type=kotlin.Nothing from='public final fun (): kotlin.Int declared in .MyEnum.Z' + GET_FIELD 'FIELD PROPERTY_BACKING_FIELD name:counter type:kotlin.Int visibility:private' type=kotlin.Int origin=null + receiver: GET_VAR ': .MyEnum.Z declared in .MyEnum.Z.' type=.MyEnum.Z origin=null + FUN DEFAULT_PROPERTY_ACCESSOR name: visibility:public modality:FINAL <> ($this:.MyEnum.Z, :kotlin.Int) returnType:kotlin.Unit + correspondingProperty: PROPERTY name:counter visibility:public modality:FINAL [var] + $this: VALUE_PARAMETER name: type:.MyEnum.Z + VALUE_PARAMETER name: index:0 type:kotlin.Int + BLOCK_BODY + SET_FIELD 'FIELD PROPERTY_BACKING_FIELD name:counter type:kotlin.Int visibility:private' type=kotlin.Unit origin=null + receiver: GET_VAR ': .MyEnum.Z declared in .MyEnum.Z.' type=.MyEnum.Z origin=null + value: GET_VAR ': kotlin.Int declared in .MyEnum.Z.' type=kotlin.Int origin=null + FUN name:foo visibility:public modality:FINAL <> ($this:.MyEnum.Z) returnType:kotlin.Unit $this: VALUE_PARAMETER name: type:.MyEnum.Z BLOCK_BODY - RETURN type=kotlin.Nothing from='public final fun (): kotlin.Int declared in .MyEnum.Z' - GET_FIELD 'FIELD PROPERTY_BACKING_FIELD name:counter type:kotlin.Int visibility:private' type=kotlin.Int origin=null - receiver: GET_VAR ': .MyEnum.Z declared in .MyEnum.Z.' type=.MyEnum.Z origin=null - FUN DEFAULT_PROPERTY_ACCESSOR name: visibility:public modality:FINAL <> ($this:.MyEnum.Z, :kotlin.Int) returnType:kotlin.Unit - correspondingProperty: PROPERTY name:counter visibility:public modality:FINAL [var] + FUN name:bar visibility:public modality:FINAL <> ($this:.MyEnum.Z) returnType:kotlin.Unit $this: VALUE_PARAMETER name: type:.MyEnum.Z - VALUE_PARAMETER name: index:0 type:kotlin.Int BLOCK_BODY SET_FIELD 'FIELD PROPERTY_BACKING_FIELD name:counter type:kotlin.Int visibility:private' type=kotlin.Unit origin=null - receiver: GET_VAR ': .MyEnum.Z declared in .MyEnum.Z.' type=.MyEnum.Z origin=null - value: GET_VAR ': kotlin.Int declared in .MyEnum.Z.' type=kotlin.Int origin=null - FUN name:foo visibility:public modality:FINAL <> ($this:.MyEnum.Z) returnType:kotlin.Unit - $this: VALUE_PARAMETER name: type:.MyEnum.Z - BLOCK_BODY - FUN name:bar visibility:public modality:FINAL <> ($this:.MyEnum.Z) returnType:kotlin.Unit - $this: VALUE_PARAMETER name: type:.MyEnum.Z - BLOCK_BODY - SET_FIELD 'FIELD PROPERTY_BACKING_FIELD name:counter type:kotlin.Int visibility:private' type=kotlin.Unit origin=null - receiver: GET_VAR ': .MyEnum.Z declared in .MyEnum.Z' type=.MyEnum.Z origin=null - value: CONST Int type=kotlin.Int value=1 - CALL 'public final fun foo (): kotlin.Unit declared in .MyEnum.Z' type=kotlin.Unit origin=null - $this: GET_VAR ': .MyEnum.Z declared in .MyEnum.Z' type=.MyEnum.Z origin=null - PROPERTY name:aLambda visibility:public modality:FINAL [val] - FIELD PROPERTY_BACKING_FIELD name:aLambda type:kotlin.Function0 visibility:private [final] - EXPRESSION_BODY - FUN_EXPR type=kotlin.Function0 origin=LAMBDA - FUN LOCAL_FUNCTION_FOR_LAMBDA name: visibility:local modality:FINAL <> ($this:.MyEnum.Z) returnType:kotlin.Unit - $this: VALUE_PARAMETER name: type:.MyEnum.Z - BLOCK_BODY - SET_FIELD 'FIELD PROPERTY_BACKING_FIELD name:counter type:kotlin.Int visibility:private' type=kotlin.Unit origin=null - receiver: GET_VAR ': .MyEnum.Z declared in .MyEnum.Z' type=.MyEnum.Z origin=null - value: CONST Int type=kotlin.Int value=1 - CALL 'public final fun foo (): kotlin.Unit declared in .MyEnum.Z' type=kotlin.Unit origin=null - $this: GET_VAR ': .MyEnum.Z declared in .MyEnum.Z' type=.MyEnum.Z origin=null - FUN DEFAULT_PROPERTY_ACCESSOR name: visibility:public modality:FINAL <> ($this:.MyEnum.Z) returnType:kotlin.Function0 - correspondingProperty: PROPERTY name:aLambda visibility:public modality:FINAL [val] - $this: VALUE_PARAMETER name: type:.MyEnum.Z - BLOCK_BODY - RETURN type=kotlin.Nothing from='public final fun (): kotlin.Function0 declared in .MyEnum.Z' - GET_FIELD 'FIELD PROPERTY_BACKING_FIELD name:aLambda type:kotlin.Function0 visibility:private [final]' type=kotlin.Function0 origin=null - receiver: GET_VAR ': .MyEnum.Z declared in .MyEnum.Z.' type=.MyEnum.Z origin=null - PROPERTY name:anObject visibility:public modality:FINAL [val] - FIELD PROPERTY_BACKING_FIELD name:anObject type:. visibility:private [final] - EXPRESSION_BODY - BLOCK type=.MyEnum.Z.anObject. origin=OBJECT_LITERAL - CLASS OBJECT name: modality:FINAL visibility:local superTypes:[kotlin.Any] - $this: VALUE_PARAMETER INSTANCE_RECEIVER name: type:.MyEnum.Z.anObject. - CONSTRUCTOR visibility:private <> () returnType:kotlin.Any [primary] - BLOCK_BODY - DELEGATING_CONSTRUCTOR_CALL 'public constructor () [primary] declared in kotlin.Any' - INSTANCE_INITIALIZER_CALL classDescriptor='CLASS OBJECT name: modality:FINAL visibility:local superTypes:[kotlin.Any]' - ANONYMOUS_INITIALIZER isStatic=false + receiver: GET_VAR ': . declared in .' type=. origin=null + value: CONST Int type=kotlin.Int value=1 + CALL 'public final fun foo (): kotlin.Unit declared in .MyEnum.Z' type=kotlin.Unit origin=null + $this: GET_VAR ': . declared in .' type=. origin=null + PROPERTY name:aLambda visibility:public modality:FINAL [val] + FIELD PROPERTY_BACKING_FIELD name:aLambda type:kotlin.Function0 visibility:private [final] + EXPRESSION_BODY + FUN_EXPR type=kotlin.Function0 origin=LAMBDA + FUN LOCAL_FUNCTION_FOR_LAMBDA name: visibility:local modality:FINAL <> ($this:.MyEnum.Z) returnType:kotlin.Unit + $this: VALUE_PARAMETER name: type:.MyEnum.Z BLOCK_BODY SET_FIELD 'FIELD PROPERTY_BACKING_FIELD name:counter type:kotlin.Int visibility:private' type=kotlin.Unit origin=null - receiver: GET_VAR ': .MyEnum.Z declared in .MyEnum.Z' type=.MyEnum.Z origin=null + receiver: GET_VAR ': . declared in .' type=. origin=null value: CONST Int type=kotlin.Int value=1 CALL 'public final fun foo (): kotlin.Unit declared in .MyEnum.Z' type=kotlin.Unit origin=null - $this: GET_VAR ': .MyEnum.Z declared in .MyEnum.Z' type=.MyEnum.Z origin=null - FUN name:test visibility:public modality:FINAL <> ($this:.MyEnum.Z.anObject.) returnType:kotlin.Unit - $this: VALUE_PARAMETER name: type:.MyEnum.Z.anObject. - BLOCK_BODY - SET_FIELD 'FIELD PROPERTY_BACKING_FIELD name:counter type:kotlin.Int visibility:private' type=kotlin.Unit origin=null - receiver: GET_VAR ': .MyEnum.Z declared in .MyEnum.Z' type=.MyEnum.Z origin=null - value: CONST Int type=kotlin.Int value=1 - CALL 'public final fun foo (): kotlin.Unit declared in .MyEnum.Z' type=kotlin.Unit origin=null - $this: GET_VAR ': .MyEnum.Z declared in .MyEnum.Z' type=.MyEnum.Z origin=null - CONSTRUCTOR_CALL 'private constructor () [primary] declared in .MyEnum.Z.anObject.' type=.MyEnum.Z.anObject. origin=null - FUN DEFAULT_PROPERTY_ACCESSOR name: visibility:public modality:FINAL <> ($this:.MyEnum.Z) returnType:. - correspondingProperty: PROPERTY name:anObject visibility:public modality:FINAL [val] - $this: VALUE_PARAMETER name: type:.MyEnum.Z - BLOCK_BODY - RETURN type=kotlin.Nothing from='public final fun (): . declared in .MyEnum.Z' - GET_FIELD 'FIELD PROPERTY_BACKING_FIELD name:anObject type:. visibility:private [final]' type=. origin=null - receiver: GET_VAR ': .MyEnum.Z declared in .MyEnum.Z.' type=.MyEnum.Z origin=null - FUN FAKE_OVERRIDE name:equals visibility:public modality:OPEN <> ($this:kotlin.Any, other:kotlin.Any?) returnType:kotlin.Boolean [fake_override,operator] - overridden: - public open fun equals (other: kotlin.Any?): kotlin.Boolean [operator] declared in kotlin.Any - $this: VALUE_PARAMETER name: type:kotlin.Any - VALUE_PARAMETER name:other index:0 type:kotlin.Any? - FUN FAKE_OVERRIDE name:hashCode visibility:public modality:OPEN <> ($this:kotlin.Any) returnType:kotlin.Int [fake_override] - overridden: - public open fun hashCode (): kotlin.Int declared in kotlin.Any - $this: VALUE_PARAMETER name: type:kotlin.Any - FUN FAKE_OVERRIDE name:toString visibility:public modality:OPEN <> ($this:kotlin.Any) returnType:kotlin.String [fake_override] - overridden: - public open fun toString (): kotlin.String declared in kotlin.Any - $this: VALUE_PARAMETER name: type:kotlin.Any + $this: GET_VAR ': . declared in .' type=. origin=null + FUN DEFAULT_PROPERTY_ACCESSOR name: visibility:public modality:FINAL <> ($this:.MyEnum.Z) returnType:kotlin.Function0 + correspondingProperty: PROPERTY name:aLambda visibility:public modality:FINAL [val] + $this: VALUE_PARAMETER name: type:.MyEnum.Z + BLOCK_BODY + RETURN type=kotlin.Nothing from='public final fun (): kotlin.Function0 declared in .MyEnum.Z' + GET_FIELD 'FIELD PROPERTY_BACKING_FIELD name:aLambda type:kotlin.Function0 visibility:private [final]' type=kotlin.Function0 origin=null + receiver: GET_VAR ': .MyEnum.Z declared in .MyEnum.Z.' type=.MyEnum.Z origin=null + PROPERTY name:anObject visibility:public modality:FINAL [val] + FIELD PROPERTY_BACKING_FIELD name:anObject type:. visibility:private [final] + EXPRESSION_BODY + BLOCK type=.MyEnum.Z.anObject. origin=OBJECT_LITERAL + CLASS OBJECT name: modality:FINAL visibility:local superTypes:[kotlin.Any] + $this: VALUE_PARAMETER INSTANCE_RECEIVER name: type:.MyEnum.Z.anObject. + CONSTRUCTOR visibility:private <> () returnType:kotlin.Any [primary] + BLOCK_BODY + DELEGATING_CONSTRUCTOR_CALL 'public constructor () [primary] declared in kotlin.Any' + INSTANCE_INITIALIZER_CALL classDescriptor='CLASS OBJECT name: modality:FINAL visibility:local superTypes:[kotlin.Any]' + ANONYMOUS_INITIALIZER isStatic=false + BLOCK_BODY + SET_FIELD 'FIELD PROPERTY_BACKING_FIELD name:counter type:kotlin.Int visibility:private' type=kotlin.Unit origin=null + receiver: GET_VAR ': . declared in .' type=. origin=null + value: CONST Int type=kotlin.Int value=1 + CALL 'public final fun foo (): kotlin.Unit declared in .MyEnum.Z' type=kotlin.Unit origin=null + $this: GET_VAR ': . declared in .' type=. origin=null + FUN name:test visibility:public modality:FINAL <> ($this:.MyEnum.Z.anObject.) returnType:kotlin.Unit + $this: VALUE_PARAMETER name: type:.MyEnum.Z.anObject. + BLOCK_BODY + SET_FIELD 'FIELD PROPERTY_BACKING_FIELD name:counter type:kotlin.Int visibility:private' type=kotlin.Unit origin=null + receiver: GET_VAR ': . declared in .' type=. origin=null + value: CONST Int type=kotlin.Int value=1 + CALL 'public final fun foo (): kotlin.Unit declared in .MyEnum.Z' type=kotlin.Unit origin=null + $this: GET_VAR ': . declared in .' type=. origin=null + CONSTRUCTOR_CALL 'private constructor () [primary] declared in .MyEnum.Z.anObject.' type=.MyEnum.Z.anObject. origin=null + FUN DEFAULT_PROPERTY_ACCESSOR name: visibility:public modality:FINAL <> ($this:.MyEnum.Z) returnType:. + correspondingProperty: PROPERTY name:anObject visibility:public modality:FINAL [val] + $this: VALUE_PARAMETER name: type:.MyEnum.Z + BLOCK_BODY + RETURN type=kotlin.Nothing from='public final fun (): . declared in .MyEnum.Z' + GET_FIELD 'FIELD PROPERTY_BACKING_FIELD name:anObject type:. visibility:private [final]' type=. origin=null + receiver: GET_VAR ': .MyEnum.Z declared in .MyEnum.Z.' type=.MyEnum.Z origin=null FUN name:values visibility:public modality:FINAL <> ($this:.MyEnum) returnType:kotlin.Array<.MyEnum> $this: VALUE_PARAMETER name: type:.MyEnum BLOCK_BODY