diff --git a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/descriptors/SharedVariablesManager.kt b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/descriptors/SharedVariablesManager.kt index f4ddab287c2..484506171e1 100644 --- a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/descriptors/SharedVariablesManager.kt +++ b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/descriptors/SharedVariablesManager.kt @@ -38,162 +38,174 @@ import org.jetbrains.kotlin.types.* class JvmSharedVariablesManager(val builtIns: KotlinBuiltIns) : SharedVariablesManager { private val kotlinJvmInternalPackage = KnownPackageFragmentDescriptor(builtIns.builtInsModule, FqName("kotlin.jvm.internal")) - private val refNamespaceClass = KnownClassDescriptor.createClass(Name.identifier("Ref"), kotlinJvmInternalPackage, listOf(builtIns.anyType)) + private val refNamespaceClass = + KnownClassDescriptor.createClass(Name.identifier("Ref"), kotlinJvmInternalPackage, listOf(builtIns.anyType)) private class PrimitiveRefDescriptorsProvider(type: KotlinType, refClass: ClassDescriptor) { val refType: KotlinType = refClass.defaultType val refConstructor: ClassConstructorDescriptor = - ClassConstructorDescriptorImpl.create(refClass, Annotations.EMPTY, true, SourceElement.NO_SOURCE).apply { - initialize(emptyList(), Visibilities.PUBLIC, emptyList()) - returnType = refType - } + ClassConstructorDescriptorImpl.create(refClass, Annotations.EMPTY, true, SourceElement.NO_SOURCE).apply { + initialize(emptyList(), Visibilities.PUBLIC, emptyList()) + returnType = refType + } val elementField: PropertyDescriptor = - PropertyDescriptorImpl.create( - refClass, Annotations.EMPTY, Modality.FINAL, Visibilities.PUBLIC, true, - Name.identifier("element"), CallableMemberDescriptor.Kind.DECLARATION, SourceElement.NO_SOURCE, - /* lateInit = */ false, /* isConst = */ false, /* isExpect = */ false, /* isActual = */ false, - /* isExternal = */ false, /* isDelegated = */ false - ).initialize(type, dispatchReceiverParameter = refClass.thisAsReceiverParameter) + PropertyDescriptorImpl.create( + refClass, Annotations.EMPTY, Modality.FINAL, Visibilities.PUBLIC, true, + Name.identifier("element"), CallableMemberDescriptor.Kind.DECLARATION, SourceElement.NO_SOURCE, + /* lateInit = */ false, /* isConst = */ false, /* isExpect = */ false, /* isActual = */ false, + /* isExternal = */ false, /* isDelegated = */ false + ).initialize(type, dispatchReceiverParameter = refClass.thisAsReceiverParameter) } private val primitiveRefDescriptorProviders: Map = - PrimitiveType.values().associate { - val type = builtIns.getPrimitiveKotlinType(it) + PrimitiveType.values().associate { + val type = builtIns.getPrimitiveKotlinType(it) - val refClassName = Name.identifier(it.typeName.asString() + "Ref") - val refClass = KnownClassDescriptor.createClass(refClassName, refNamespaceClass, listOf(builtIns.anyType)) + val refClassName = Name.identifier(it.typeName.asString() + "Ref") + val refClass = KnownClassDescriptor.createClass(refClassName, refNamespaceClass, listOf(builtIns.anyType)) - it to PrimitiveRefDescriptorsProvider(type, refClass) - } + it to PrimitiveRefDescriptorsProvider(type, refClass) + } private inner class ObjectRefDescriptorsProvider { val genericRefClass: ClassDescriptor = - KnownClassDescriptor.createClassWithTypeParameters( - Name.identifier("ObjectRef"), refNamespaceClass, listOf(builtIns.anyType), listOf(Name.identifier("T")) - ) + KnownClassDescriptor.createClassWithTypeParameters( + Name.identifier("ObjectRef"), refNamespaceClass, listOf(builtIns.anyType), listOf(Name.identifier("T")) + ) val genericRefConstructor: ClassConstructorDescriptor = - ClassConstructorDescriptorImpl.create(genericRefClass, Annotations.EMPTY, true, SourceElement.NO_SOURCE).apply { - initialize(emptyList(), Visibilities.PUBLIC) - val typeParameter = typeParameters[0] - val typeParameterType = KotlinTypeFactory.simpleTypeWithNonTrivialMemberScope(Annotations.EMPTY, typeParameter.typeConstructor, listOf(), false, MemberScope.Empty) - returnType = KotlinTypeFactory.simpleNotNullType(Annotations.EMPTY, genericRefClass, listOf(TypeProjectionImpl(Variance.INVARIANT, typeParameterType))) - } + ClassConstructorDescriptorImpl.create(genericRefClass, Annotations.EMPTY, true, SourceElement.NO_SOURCE).apply { + initialize(emptyList(), Visibilities.PUBLIC) + val typeParameter = typeParameters[0] + val typeParameterType = KotlinTypeFactory.simpleTypeWithNonTrivialMemberScope( + Annotations.EMPTY, + typeParameter.typeConstructor, + listOf(), + false, + MemberScope.Empty + ) + returnType = KotlinTypeFactory.simpleNotNullType( + Annotations.EMPTY, + genericRefClass, + listOf(TypeProjectionImpl(Variance.INVARIANT, typeParameterType)) + ) + } val constructorTypeParameter: TypeParameterDescriptor = - genericRefConstructor.typeParameters[0] + genericRefConstructor.typeParameters[0] fun getSubstitutedRefConstructor(valueType: KotlinType): ClassConstructorDescriptor = - genericRefConstructor.substitute(TypeSubstitutor.create( - mapOf(constructorTypeParameter.typeConstructor to TypeProjectionImpl(Variance.INVARIANT, valueType)) - ))!! + genericRefConstructor.substitute( + TypeSubstitutor.create( + mapOf(constructorTypeParameter.typeConstructor to TypeProjectionImpl(Variance.INVARIANT, valueType)) + ) + )!! val genericElementField: PropertyDescriptor = - PropertyDescriptorImpl.create( - genericRefClass, Annotations.EMPTY, Modality.FINAL, Visibilities.PUBLIC, true, - Name.identifier("element"), CallableMemberDescriptor.Kind.DECLARATION, SourceElement.NO_SOURCE, - /* lateInit = */ false, /* isConst = */ false, /* isExpect = */ false, /* isActual = */ false, - /* isExternal = */ false, /* isDelegated = */ false - ).initialize( - type = builtIns.anyType, - dispatchReceiverParameter = genericRefClass.thisAsReceiverParameter - ) + PropertyDescriptorImpl.create( + genericRefClass, Annotations.EMPTY, Modality.FINAL, Visibilities.PUBLIC, true, + Name.identifier("element"), CallableMemberDescriptor.Kind.DECLARATION, SourceElement.NO_SOURCE, + /* lateInit = */ false, /* isConst = */ false, /* isExpect = */ false, /* isActual = */ false, + /* isExternal = */ false, /* isDelegated = */ false + ).initialize( + type = builtIns.anyType, + dispatchReceiverParameter = genericRefClass.thisAsReceiverParameter + ) fun getRefType(valueType: KotlinType) = - KotlinTypeFactory.simpleNotNullType(Annotations.EMPTY, genericRefClass, listOf(TypeProjectionImpl(Variance.INVARIANT, valueType))) + KotlinTypeFactory.simpleNotNullType( + Annotations.EMPTY, + genericRefClass, + listOf(TypeProjectionImpl(Variance.INVARIANT, valueType)) + ) } private val objectRefDescriptorsProvider = ObjectRefDescriptorsProvider() override fun createSharedVariableDescriptor(variableDescriptor: VariableDescriptor): VariableDescriptor = - LocalVariableDescriptor( - variableDescriptor.containingDeclaration, variableDescriptor.annotations, variableDescriptor.name, - getSharedVariableType(variableDescriptor.type), - false, false, variableDescriptor.isLateInit, variableDescriptor.source - ) + LocalVariableDescriptor( + variableDescriptor.containingDeclaration, variableDescriptor.annotations, variableDescriptor.name, + getSharedVariableType(variableDescriptor.type), + false, false, variableDescriptor.isLateInit, variableDescriptor.source + ) override fun defineSharedValue(sharedVariableDescriptor: VariableDescriptor, originalDeclaration: IrVariable): IrStatement { val valueType = originalDeclaration.descriptor.type val primitiveRefDescriptorsProvider = primitiveRefDescriptorProviders[getPrimitiveType(valueType)] val refConstructor = - primitiveRefDescriptorsProvider?.refConstructor ?: - objectRefDescriptorsProvider.getSubstitutedRefConstructor(valueType) + primitiveRefDescriptorsProvider?.refConstructor ?: objectRefDescriptorsProvider.getSubstitutedRefConstructor(valueType) val refConstructorTypeArguments = - if (primitiveRefDescriptorsProvider != null) null - else mapOf(objectRefDescriptorsProvider.constructorTypeParameter to valueType) + if (primitiveRefDescriptorsProvider != null) null + else mapOf(objectRefDescriptorsProvider.constructorTypeParameter to valueType) val elementPropertyDescriptor = - primitiveRefDescriptorsProvider?.elementField ?: - objectRefDescriptorsProvider.genericElementField + primitiveRefDescriptorsProvider?.elementField ?: objectRefDescriptorsProvider.genericElementField val refConstructorCall = IrCallImpl( - originalDeclaration.startOffset, originalDeclaration.endOffset, - refConstructor, refConstructorTypeArguments + originalDeclaration.startOffset, originalDeclaration.endOffset, + refConstructor, refConstructorTypeArguments ) val sharedVariableDeclaration = IrVariableImpl( - originalDeclaration.startOffset, originalDeclaration.endOffset, originalDeclaration.origin, - sharedVariableDescriptor, refConstructorCall + originalDeclaration.startOffset, originalDeclaration.endOffset, originalDeclaration.origin, + sharedVariableDescriptor, refConstructorCall ) - val initializer = originalDeclaration.initializer ?: - return sharedVariableDeclaration + val initializer = originalDeclaration.initializer ?: return sharedVariableDeclaration val sharedVariableInitialization = IrSetFieldImpl( - initializer.startOffset, initializer.endOffset, - elementPropertyDescriptor, - IrGetValueImpl(initializer.startOffset, initializer.endOffset, sharedVariableDescriptor), - initializer + initializer.startOffset, initializer.endOffset, + elementPropertyDescriptor, + IrGetValueImpl(initializer.startOffset, initializer.endOffset, sharedVariableDescriptor), + initializer ) return IrCompositeImpl( - originalDeclaration.startOffset, originalDeclaration.endOffset, builtIns.unitType, null, - listOf(sharedVariableDeclaration, sharedVariableInitialization) + originalDeclaration.startOffset, originalDeclaration.endOffset, builtIns.unitType, null, + listOf(sharedVariableDeclaration, sharedVariableInitialization) ) } private fun getElementFieldDescriptor(valueType: KotlinType): PropertyDescriptor { val primitiveRefDescriptorsProvider = primitiveRefDescriptorProviders[getPrimitiveType(valueType)] - return primitiveRefDescriptorsProvider?.elementField ?: - objectRefDescriptorsProvider.genericElementField + return primitiveRefDescriptorsProvider?.elementField ?: objectRefDescriptorsProvider.genericElementField } override fun getSharedValue(sharedVariableDescriptor: VariableDescriptor, originalGet: IrGetValue): IrExpression = - IrGetFieldImpl( - originalGet.startOffset, originalGet.endOffset, - getElementFieldDescriptor(originalGet.descriptor.type), - IrGetValueImpl(originalGet.startOffset, originalGet.endOffset, sharedVariableDescriptor), - originalGet.origin - ) + IrGetFieldImpl( + originalGet.startOffset, originalGet.endOffset, + getElementFieldDescriptor(originalGet.descriptor.type), + IrGetValueImpl(originalGet.startOffset, originalGet.endOffset, sharedVariableDescriptor), + originalGet.origin + ) override fun setSharedValue(sharedVariableDescriptor: VariableDescriptor, originalSet: IrSetVariable): IrExpression = - IrSetFieldImpl( - originalSet.startOffset, originalSet.endOffset, - getElementFieldDescriptor(originalSet.descriptor.type), - IrGetValueImpl(originalSet.startOffset, originalSet.endOffset, sharedVariableDescriptor), - originalSet.value, - originalSet.origin - ) + IrSetFieldImpl( + originalSet.startOffset, originalSet.endOffset, + getElementFieldDescriptor(originalSet.descriptor.type), + IrGetValueImpl(originalSet.startOffset, originalSet.endOffset, sharedVariableDescriptor), + originalSet.value, + originalSet.origin + ) private fun getSharedVariableType(valueType: KotlinType): KotlinType = - primitiveRefDescriptorProviders[getPrimitiveType(valueType)]?.refType ?: - objectRefDescriptorsProvider.getRefType(valueType) + primitiveRefDescriptorProviders[getPrimitiveType(valueType)]?.refType ?: objectRefDescriptorsProvider.getRefType(valueType) private fun getPrimitiveType(type: KotlinType): PrimitiveType? = - when { - KotlinBuiltIns.isBoolean(type) -> PrimitiveType.BOOLEAN - KotlinBuiltIns.isChar(type) -> PrimitiveType.CHAR - KotlinBuiltIns.isByte(type) -> PrimitiveType.BYTE - KotlinBuiltIns.isShort(type) -> PrimitiveType.SHORT - KotlinBuiltIns.isInt(type) -> PrimitiveType.INT - KotlinBuiltIns.isLong(type) -> PrimitiveType.LONG - KotlinBuiltIns.isFloat(type) -> PrimitiveType.FLOAT - KotlinBuiltIns.isDouble(type) -> PrimitiveType.DOUBLE - else -> null - } + when { + KotlinBuiltIns.isBoolean(type) -> PrimitiveType.BOOLEAN + KotlinBuiltIns.isChar(type) -> PrimitiveType.CHAR + KotlinBuiltIns.isByte(type) -> PrimitiveType.BYTE + KotlinBuiltIns.isShort(type) -> PrimitiveType.SHORT + KotlinBuiltIns.isInt(type) -> PrimitiveType.INT + KotlinBuiltIns.isLong(type) -> PrimitiveType.LONG + KotlinBuiltIns.isFloat(type) -> PrimitiveType.FLOAT + KotlinBuiltIns.isDouble(type) -> PrimitiveType.DOUBLE + else -> null + } } diff --git a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/EnumClassLowering.kt b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/EnumClassLowering.kt index 5a15d91ce5b..5bba012af49 100644 --- a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/EnumClassLowering.kt +++ b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/EnumClassLowering.kt @@ -118,28 +118,28 @@ class EnumClassLowering(val context: JvmBackendContext) : ClassLoweringPass { val constructorDescriptor = enumConstructor.descriptor val loweredConstructorDescriptor = lowerEnumConstructor(constructorDescriptor) return IrConstructorImpl( - enumConstructor.startOffset, enumConstructor.endOffset, enumConstructor.origin, - loweredConstructorDescriptor, - enumConstructor.body!! // will be transformed later + enumConstructor.startOffset, enumConstructor.endOffset, enumConstructor.origin, + loweredConstructorDescriptor, + enumConstructor.body!! // will be transformed later ) } private fun lowerEnumConstructor(constructorDescriptor: ClassConstructorDescriptor): ClassConstructorDescriptor { val loweredConstructorDescriptor = ClassConstructorDescriptorImpl.createSynthesized( - constructorDescriptor.containingDeclaration, - constructorDescriptor.annotations, - constructorDescriptor.isPrimary, - constructorDescriptor.source + constructorDescriptor.containingDeclaration, + constructorDescriptor.annotations, + constructorDescriptor.isPrimary, + constructorDescriptor.source ) val valueParameters = - listOf( - loweredConstructorDescriptor.createValueParameter(0, "name", context.builtIns.stringType), - loweredConstructorDescriptor.createValueParameter(1, "ordinal", context.builtIns.intType) - ) + - constructorDescriptor.valueParameters.map { - lowerConstructorValueParameter(loweredConstructorDescriptor, it) - } + listOf( + loweredConstructorDescriptor.createValueParameter(0, "name", context.builtIns.stringType), + loweredConstructorDescriptor.createValueParameter(1, "ordinal", context.builtIns.intType) + ) + + constructorDescriptor.valueParameters.map { + lowerConstructorValueParameter(loweredConstructorDescriptor, it) + } loweredConstructorDescriptor.initialize(valueParameters, Visibilities.PROTECTED) loweredConstructorDescriptor.returnType = constructorDescriptor.returnType @@ -150,13 +150,13 @@ class EnumClassLowering(val context: JvmBackendContext) : ClassLoweringPass { } private fun lowerConstructorValueParameter( - loweredConstructorDescriptor: ClassConstructorDescriptor, - valueParameterDescriptor: ValueParameterDescriptor + loweredConstructorDescriptor: ClassConstructorDescriptor, + valueParameterDescriptor: ValueParameterDescriptor ): ValueParameterDescriptor { val loweredValueParameterDescriptor = valueParameterDescriptor.copy( - loweredConstructorDescriptor, - valueParameterDescriptor.name, - valueParameterDescriptor.index + 2 + loweredConstructorDescriptor, + valueParameterDescriptor.name, + valueParameterDescriptor.index + 2 ) loweredEnumConstructorParameters[valueParameterDescriptor] = loweredValueParameterDescriptor return loweredValueParameterDescriptor @@ -165,10 +165,11 @@ class EnumClassLowering(val context: JvmBackendContext) : ClassLoweringPass { private fun lowerEnumEntries() { irClass.declarations.transformFlat { declaration -> if (declaration is IrEnumEntry) { - listOfNotNull(createFieldForEnumEntry(declaration), - lowerEnumEntryClass(declaration.correspondingClass)) - } - else null + listOfNotNull( + createFieldForEnumEntry(declaration), + lowerEnumEntryClass(declaration.correspondingClass) + ) + } else null } } @@ -188,9 +189,9 @@ class EnumClassLowering(val context: JvmBackendContext) : ClassLoweringPass { val enumEntryInitializer = enumEntry.initializerExpression!! return IrFieldImpl( - enumEntry.startOffset, enumEntry.endOffset, JvmLoweredDeclarationOrigin.FIELD_FOR_ENUM_ENTRY, - fieldPropertyDescriptor, - IrExpressionBodyImpl(enumEntryInitializer) + enumEntry.startOffset, enumEntry.endOffset, JvmLoweredDeclarationOrigin.FIELD_FOR_ENUM_ENTRY, + fieldPropertyDescriptor, + IrExpressionBodyImpl(enumEntryInitializer) ) } @@ -204,14 +205,14 @@ class EnumClassLowering(val context: JvmBackendContext) : ClassLoweringPass { } private fun findFunctionDescriptorForMemberWithSyntheticBodyKind(kind: IrSyntheticBodyKind): FunctionDescriptor = - irClass.declarations - .first { - it is IrFunction && + irClass.declarations + .first { + it is IrFunction && it.body.let { body -> body is IrSyntheticBody && body.kind == kind } - } - .descriptor as FunctionDescriptor + } + .descriptor as FunctionDescriptor private fun createSyntheticValuesFieldDeclaration(): IrFieldImpl { @@ -220,26 +221,28 @@ class EnumClassLowering(val context: JvmBackendContext) : ClassLoweringPass { val irValuesInitializer = createSyntheticValuesFieldInitializerExpression() - return IrFieldImpl(UNDEFINED_OFFSET, UNDEFINED_OFFSET, JvmLoweredDeclarationOrigin.FIELD_FOR_ENUM_VALUES, - valuesFieldDescriptor, - IrExpressionBodyImpl(UNDEFINED_OFFSET, UNDEFINED_OFFSET, irValuesInitializer)) + return IrFieldImpl( + UNDEFINED_OFFSET, UNDEFINED_OFFSET, JvmLoweredDeclarationOrigin.FIELD_FOR_ENUM_VALUES, + valuesFieldDescriptor, + IrExpressionBodyImpl(UNDEFINED_OFFSET, UNDEFINED_OFFSET, irValuesInitializer) + ) } private fun createSyntheticValuesFieldInitializerExpression(): IrExpression = - createArrayOfExpression( - irClass.descriptor.defaultType, - enumEntryFields.map { fieldDescriptor -> - IrGetFieldImpl(UNDEFINED_OFFSET, UNDEFINED_OFFSET, fieldDescriptor) - }) + createArrayOfExpression( + irClass.descriptor.defaultType, + enumEntryFields.map { fieldDescriptor -> + IrGetFieldImpl(UNDEFINED_OFFSET, UNDEFINED_OFFSET, fieldDescriptor) + }) private fun createSyntheticValuesFieldDescriptor(valuesArrayType: SimpleType): PropertyDescriptorImpl { return JvmPropertyDescriptorImpl.createStaticVal( - Name.identifier("\$VALUES"), - valuesArrayType, - irClass.descriptor, - Annotations.EMPTY, - Modality.FINAL, Visibilities.PRIVATE, Opcodes.ACC_SYNTHETIC, - irClass.descriptor.source + Name.identifier("\$VALUES"), + valuesArrayType, + irClass.descriptor, + Annotations.EMPTY, + Modality.FINAL, Visibilities.PRIVATE, Opcodes.ACC_SYNTHETIC, + irClass.descriptor.source ) } @@ -248,7 +251,7 @@ class EnumClassLowering(val context: JvmBackendContext) : ClassLoweringPass { } private inner class InEnumClassConstructor(val enumClassConstructor: ClassConstructorDescriptor) : - EnumConstructorCallTransformer { + EnumConstructorCallTransformer { override fun transform(enumConstructorCall: IrEnumConstructorCall): IrExpression { val startOffset = enumConstructorCall.startOffset val endOffset = enumConstructorCall.endOffset @@ -327,17 +330,21 @@ class EnumClassLowering(val context: JvmBackendContext) : ClassLoweringPass { throw AssertionError("Unexpected delegating constructor call within enum entry: $enumEntry") } - abstract fun createConstructorCall(startOffset: Int, endOffset: Int, loweredConstructor: ClassConstructorDescriptor): IrMemberAccessExpression + abstract fun createConstructorCall( + startOffset: Int, + endOffset: Int, + loweredConstructor: ClassConstructorDescriptor + ): IrMemberAccessExpression } private inner class InEnumEntryClassConstructor(enumEntry: ClassDescriptor) : InEnumEntry(enumEntry) { override fun createConstructorCall(startOffset: Int, endOffset: Int, loweredConstructor: ClassConstructorDescriptor) = - IrDelegatingConstructorCallImpl(startOffset, endOffset, loweredConstructor) + IrDelegatingConstructorCallImpl(startOffset, endOffset, loweredConstructor) } private inner class InEnumEntryInitializer(enumEntry: ClassDescriptor) : InEnumEntry(enumEntry) { override fun createConstructorCall(startOffset: Int, endOffset: Int, loweredConstructor: ClassConstructorDescriptor) = - IrCallImpl(startOffset, endOffset, loweredConstructor) + IrCallImpl(startOffset, endOffset, loweredConstructor) } private inner class EnumClassBodyTransformer : IrElementTransformerVoid() { @@ -350,7 +357,7 @@ class EnumClassLowering(val context: JvmBackendContext) : ClassLoweringPass { return declaration } - assert(enumConstructorCallTransformer == null) { "Nested enum entry initialization:\n${declaration.dump()}"} + assert(enumConstructorCallTransformer == null) { "Nested enum entry initialization:\n${declaration.dump()}" } enumConstructorCallTransformer = InEnumEntryInitializer(enumEntry) @@ -369,11 +376,10 @@ class EnumClassLowering(val context: JvmBackendContext) : ClassLoweringPass { val previous = enumConstructorCallTransformer if (containingClass.kind == ClassKind.ENUM_ENTRY) { - assert(enumConstructorCallTransformer == null) { "Nested enum entry initialization:\n${declaration.dump()}"} + assert(enumConstructorCallTransformer == null) { "Nested enum entry initialization:\n${declaration.dump()}" } enumConstructorCallTransformer = InEnumEntryClassConstructor(containingClass) - } - else if (containingClass.kind == ClassKind.ENUM_CLASS) { - assert(enumConstructorCallTransformer == null) { "Nested enum entry initialization:\n${declaration.dump()}"} + } else if (containingClass.kind == ClassKind.ENUM_CLASS) { + assert(enumConstructorCallTransformer == null) { "Nested enum entry initialization:\n${declaration.dump()}" } enumConstructorCallTransformer = InEnumClassConstructor(constructorDescriptor) } @@ -387,9 +393,10 @@ class EnumClassLowering(val context: JvmBackendContext) : ClassLoweringPass { override fun visitEnumConstructorCall(expression: IrEnumConstructorCall): IrExpression { expression.transformChildrenVoid(this) - val callTransformer = enumConstructorCallTransformer ?: - throw AssertionError("Enum constructor call outside of enum entry initialization or enum class constructor:\n" + - irClass.dump()) + val callTransformer = enumConstructorCallTransformer ?: throw AssertionError( + "Enum constructor call outside of enum entry initialization or enum class constructor:\n" + + irClass.dump() + ) return callTransformer.transform(expression) } @@ -398,9 +405,10 @@ class EnumClassLowering(val context: JvmBackendContext) : ClassLoweringPass { expression.transformChildrenVoid(this) if (expression.descriptor.containingDeclaration.kind == ClassKind.ENUM_CLASS) { - val callTransformer = enumConstructorCallTransformer ?: - throw AssertionError("Enum constructor call outside of enum entry initialization or enum class constructor:\n" + - irClass.dump()) + val callTransformer = enumConstructorCallTransformer ?: throw AssertionError( + "Enum constructor call outside of enum entry initialization or enum class constructor:\n" + + irClass.dump() + ) return callTransformer.transform(expression) } @@ -412,8 +420,7 @@ class EnumClassLowering(val context: JvmBackendContext) : ClassLoweringPass { val loweredParameter = loweredEnumConstructorParameters[expression.descriptor] return if (loweredParameter != null) { IrGetValueImpl(expression.startOffset, expression.endOffset, loweredParameter, expression.origin) - } - else { + } else { expression } } @@ -436,13 +443,15 @@ class EnumClassLowering(val context: JvmBackendContext) : ClassLoweringPass { val typeSubstitutor = TypeSubstitutor.create(mapOf(typeParameterT.typeConstructor to TypeProjectionImpl(enumClassType))) val substitutedValueOf = unsubstitutedValueOf.substitute(typeSubstitutor)!! - val irValueOfCall = IrCallImpl(UNDEFINED_OFFSET, UNDEFINED_OFFSET, substitutedValueOf, mapOf(typeParameterT to enumClassType)) + val irValueOfCall = + IrCallImpl(UNDEFINED_OFFSET, UNDEFINED_OFFSET, substitutedValueOf, mapOf(typeParameterT to enumClassType)) irValueOfCall.putValueArgument( - 0, IrGetValueImpl(UNDEFINED_OFFSET, UNDEFINED_OFFSET, valueOfFunctionDescriptor.valueParameters[0])) + 0, IrGetValueImpl(UNDEFINED_OFFSET, UNDEFINED_OFFSET, valueOfFunctionDescriptor.valueParameters[0]) + ) return IrBlockBodyImpl( - UNDEFINED_OFFSET, UNDEFINED_OFFSET, - listOf(IrReturnImpl(UNDEFINED_OFFSET, UNDEFINED_OFFSET, valueOfFunctionDescriptor, irValueOfCall)) + UNDEFINED_OFFSET, UNDEFINED_OFFSET, + listOf(IrReturnImpl(UNDEFINED_OFFSET, UNDEFINED_OFFSET, valueOfFunctionDescriptor, irValueOfCall)) ) } @@ -454,8 +463,8 @@ class EnumClassLowering(val context: JvmBackendContext) : ClassLoweringPass { } return IrBlockBodyImpl( - UNDEFINED_OFFSET, UNDEFINED_OFFSET, - listOf(IrReturnImpl(UNDEFINED_OFFSET, UNDEFINED_OFFSET, valuesFunctionDescriptor, irCloneValues)) + UNDEFINED_OFFSET, UNDEFINED_OFFSET, + listOf(IrReturnImpl(UNDEFINED_OFFSET, UNDEFINED_OFFSET, valuesFunctionDescriptor, irCloneValues)) ) } } diff --git a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/ObjectClassLowering.kt b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/ObjectClassLowering.kt index 3366d61b003..04b7c7a0bf9 100644 --- a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/ObjectClassLowering.kt +++ b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/ObjectClassLowering.kt @@ -55,19 +55,25 @@ class ObjectClassLowering(val context: JvmBackendContext) : IrElementTransformer val publicInstanceDescriptor = context.descriptorsFactory.getFieldDescriptorForObjectInstance(irClass.descriptor) - val constructor = irClass.descriptor.unsubstitutedPrimaryConstructor ?: - throw AssertionError("Object should have a primary constructor: ${irClass.descriptor}") + val constructor = irClass.descriptor.unsubstitutedPrimaryConstructor + ?: throw AssertionError("Object should have a primary constructor: ${irClass.descriptor}") val publicInstanceOwner = if (irClass.descriptor.isCompanionObject) parentScope!!.irElement as IrDeclarationContainer else irClass if (isCompanionObjectInInterfaceNotIntrinsic(irClass.descriptor)) { // TODO rename to $$INSTANCE - val privateInstance = publicInstanceDescriptor.copy(irClass.descriptor, Modality.FINAL, Visibilities.PROTECTED/*TODO package local*/, CallableMemberDescriptor.Kind.SYNTHESIZED, false) as PropertyDescriptor + val privateInstance = publicInstanceDescriptor.copy( + irClass.descriptor, + Modality.FINAL, + Visibilities.PROTECTED/*TODO package local*/, + CallableMemberDescriptor.Kind.SYNTHESIZED, + false + ) as PropertyDescriptor privateInstance.name val field = createInstanceFieldWithInitializer(privateInstance, constructor, irClass) createFieldWithCustomInitializer( - publicInstanceDescriptor, - IrGetFieldImpl(UNDEFINED_OFFSET, UNDEFINED_OFFSET, field.symbol), - publicInstanceOwner + publicInstanceDescriptor, + IrGetFieldImpl(UNDEFINED_OFFSET, UNDEFINED_OFFSET, field.symbol), + publicInstanceOwner ) } else { createInstanceFieldWithInitializer(publicInstanceDescriptor, constructor, publicInstanceOwner) @@ -75,22 +81,26 @@ class ObjectClassLowering(val context: JvmBackendContext) : IrElementTransformer } private fun createInstanceFieldWithInitializer( - instanceFieldDescriptor: PropertyDescriptor, - constructor: ClassConstructorDescriptor, - instanceOwner: IrDeclarationContainer + instanceFieldDescriptor: PropertyDescriptor, + constructor: ClassConstructorDescriptor, + instanceOwner: IrDeclarationContainer ): IrField = - createFieldWithCustomInitializer(instanceFieldDescriptor, IrCallImpl(UNDEFINED_OFFSET, UNDEFINED_OFFSET, constructor), instanceOwner) + createFieldWithCustomInitializer( + instanceFieldDescriptor, + IrCallImpl(UNDEFINED_OFFSET, UNDEFINED_OFFSET, constructor), + instanceOwner + ) private fun createFieldWithCustomInitializer( - instanceFieldDescriptor: PropertyDescriptor, - instanceInitializer: IrExpression, - instanceOwner: IrDeclarationContainer + instanceFieldDescriptor: PropertyDescriptor, + instanceInitializer: IrExpression, + instanceOwner: IrDeclarationContainer ): IrField = - IrFieldImpl( - UNDEFINED_OFFSET, UNDEFINED_OFFSET, JvmLoweredDeclarationOrigin.FIELD_FOR_OBJECT_INSTANCE, - instanceFieldDescriptor, - IrExpressionBodyImpl(UNDEFINED_OFFSET, UNDEFINED_OFFSET, instanceInitializer) - ).also { - pendingTransformations.add { instanceOwner.declarations.add(it) } - } + IrFieldImpl( + UNDEFINED_OFFSET, UNDEFINED_OFFSET, JvmLoweredDeclarationOrigin.FIELD_FOR_OBJECT_INSTANCE, + instanceFieldDescriptor, + IrExpressionBodyImpl(UNDEFINED_OFFSET, UNDEFINED_OFFSET, instanceInitializer) + ).also { + pendingTransformations.add { instanceOwner.declarations.add(it) } + } } \ No newline at end of file