diff --git a/compiler/fir/fir-serialization/src/org/jetbrains/kotlin/fir/serialization/FirElementSerializer.kt b/compiler/fir/fir-serialization/src/org/jetbrains/kotlin/fir/serialization/FirElementSerializer.kt index e02b1416533..6d795cda45f 100644 --- a/compiler/fir/fir-serialization/src/org/jetbrains/kotlin/fir/serialization/FirElementSerializer.kt +++ b/compiler/fir/fir-serialization/src/org/jetbrains/kotlin/fir/serialization/FirElementSerializer.kt @@ -37,6 +37,7 @@ import org.jetbrains.kotlin.metadata.serialization.Interner import org.jetbrains.kotlin.metadata.serialization.MutableTypeTable import org.jetbrains.kotlin.metadata.serialization.MutableVersionRequirementTable import org.jetbrains.kotlin.name.ClassId +import org.jetbrains.kotlin.name.FqName import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.resolve.RequireKotlinConstants import org.jetbrains.kotlin.resolve.constants.ConstantValue @@ -57,6 +58,31 @@ class FirElementSerializer private constructor( ) { private val contractSerializer = FirContractSerializer() + fun packagePartProto(packageFqName: FqName, file: FirFile): ProtoBuf.Package.Builder { + val builder = ProtoBuf.Package.newBuilder() + + for (declaration in file.declarations) { + when (declaration) { + is FirProperty -> propertyProto(declaration)?.let { builder.addProperty(it) } + is FirSimpleFunction -> functionProto(declaration)?.let { builder.addFunction(it) } + } + } + + val typeTableProto = typeTable.serialize() + if (typeTableProto != null) { + builder.typeTable = typeTableProto + } + + val versionRequirementTableProto = versionRequirementTable?.serialize() + if (versionRequirementTableProto != null) { + builder.versionRequirementTable = versionRequirementTableProto + } + + extension.serializePackage(packageFqName, builder) + + return builder + } + fun classProto(irClass: IrClass): ProtoBuf.Class.Builder { val klass = (irClass.metadata as FirMetadataSource.Class).klass val builder = ProtoBuf.Class.newBuilder() diff --git a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrVisitor.kt b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrVisitor.kt index 699f7010c55..92561a475a6 100644 --- a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrVisitor.kt +++ b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrVisitor.kt @@ -37,6 +37,7 @@ import org.jetbrains.kotlin.ir.builders.constFalse import org.jetbrains.kotlin.ir.builders.constTrue import org.jetbrains.kotlin.ir.builders.elseBranch import org.jetbrains.kotlin.ir.declarations.* +import org.jetbrains.kotlin.ir.declarations.impl.IrFileImpl import org.jetbrains.kotlin.ir.expressions.* import org.jetbrains.kotlin.ir.expressions.impl.* import org.jetbrains.kotlin.ir.types.IrType @@ -81,6 +82,8 @@ class Fir2IrVisitor( annotations = file.annotations.mapNotNull { it.accept(this@Fir2IrVisitor, data) as? IrConstructorCall } + + (this as IrFileImpl).metadata = FirMetadataSource.File(file, components.session, declarations.map { it.descriptor }) } } diff --git a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/FirMetadataSource.kt b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/FirMetadataSource.kt index a5305c3a378..bdfc2b77776 100644 --- a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/FirMetadataSource.kt +++ b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/FirMetadataSource.kt @@ -5,6 +5,7 @@ package org.jetbrains.kotlin.fir.backend +import org.jetbrains.kotlin.descriptors.DeclarationDescriptor import org.jetbrains.kotlin.descriptors.FunctionDescriptor import org.jetbrains.kotlin.descriptors.PropertyDescriptor import org.jetbrains.kotlin.fir.FirSession @@ -16,6 +17,10 @@ interface FirMetadataSource : MetadataSource { val session: FirSession + class File( + val file: FirFile, override val session: FirSession, descriptors: List + ) : MetadataSource.File(descriptors), FirMetadataSource + class Class( val klass: FirClass<*>, descriptor: WrappedClassDescriptor ) : MetadataSource.Class(descriptor), FirMetadataSource { diff --git a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/codegen/FirBasedClassCodegen.kt b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/codegen/FirBasedClassCodegen.kt index ef7db0d003e..5f1828c09f6 100644 --- a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/codegen/FirBasedClassCodegen.kt +++ b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/codegen/FirBasedClassCodegen.kt @@ -9,7 +9,6 @@ import org.jetbrains.kotlin.backend.jvm.JvmBackendContext import org.jetbrains.kotlin.backend.jvm.lower.MultifileFacadeFileEntry import org.jetbrains.kotlin.codegen.* import org.jetbrains.kotlin.codegen.binding.CodegenBinding -import org.jetbrains.kotlin.codegen.serialization.JvmSerializationBindings import org.jetbrains.kotlin.fir.backend.FirMetadataSource import org.jetbrains.kotlin.fir.backend.jvm.FirJvmSerializerExtension import org.jetbrains.kotlin.fir.declarations.FirAnonymousFunction @@ -21,10 +20,7 @@ import org.jetbrains.kotlin.fir.symbols.impl.FirAnonymousFunctionSymbol import org.jetbrains.kotlin.ir.declarations.IrClass import org.jetbrains.kotlin.ir.declarations.IrField import org.jetbrains.kotlin.ir.declarations.IrFunction -import org.jetbrains.kotlin.ir.util.dump -import org.jetbrains.kotlin.ir.util.fqNameWhenAvailable -import org.jetbrains.kotlin.ir.util.isFileClass -import org.jetbrains.kotlin.ir.util.render +import org.jetbrains.kotlin.ir.util.* import org.jetbrains.kotlin.load.java.JvmAbi import org.jetbrains.kotlin.load.java.JvmAnnotationNames import org.jetbrains.kotlin.load.kotlin.header.KotlinClassHeader @@ -46,6 +42,7 @@ class FirBasedClassCodegen internal constructor( is FirMetadataSource.Class -> FirElementSerializer.create( metadata.klass, serializerExtension, (parentClassCodegen as? FirBasedClassCodegen)?.serializer ) + is FirMetadataSource.File -> FirElementSerializer.createTopLevel(session, serializerExtension) is FirMetadataSource.Function -> FirElementSerializer.createForLambda(session, serializerExtension) else -> null } @@ -89,6 +86,26 @@ class FirBasedClassCodegen internal constructor( "JvmPackageName is not supported for classes: ${irClass.render()}" } } + is FirMetadataSource.File -> { + val packageFqName = irClass.getPackageFragment()!!.fqName + val packageProto = serializer!!.packagePartProto(packageFqName, metadata.file) + + serializerExtension.serializeJvmPackage(packageProto, type) + + val facadeClassName = context.multifileFacadeForPart[irClass.attributeOwnerId] + val kind = if (facadeClassName != null) KotlinClassHeader.Kind.MULTIFILE_CLASS_PART else KotlinClassHeader.Kind.FILE_FACADE + writeKotlinMetadata(visitor, state, kind, extraFlags) { av -> + AsmUtil.writeAnnotationData(av, packageProto.build(), serializer.stringTable as JvmStringTable) + + if (facadeClassName != null) { + av.visit(JvmAnnotationNames.METADATA_MULTIFILE_CLASS_NAME_FIELD_NAME, facadeClassName.internalName) + } + + if (irClass in context.classNameOverride) { + av.visit(JvmAnnotationNames.METADATA_PACKAGE_NAME_FIELD_NAME, irClass.fqNameWhenAvailable!!.parent().asString()) + } + } + } is FirMetadataSource.Function -> { val fakeAnonymousFunction = metadata.function.copyToFreeAnonymousFunction() val functionProto = serializer!!.functionProto(fakeAnonymousFunction)?.build() diff --git a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/declarations/MetadataSource.kt b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/declarations/MetadataSource.kt index 09da9f1dc26..42f340b90b8 100644 --- a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/declarations/MetadataSource.kt +++ b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/declarations/MetadataSource.kt @@ -13,7 +13,7 @@ import org.jetbrains.kotlin.descriptors.PropertyDescriptor interface MetadataSource { open class Class(val descriptor: ClassDescriptor) : MetadataSource - class File(val descriptors: List) : MetadataSource + open class File(val descriptors: List) : MetadataSource open class Function(val descriptor: FunctionDescriptor) : MetadataSource diff --git a/compiler/testData/codegen/box/delegatedProperty/stackOverflowOnCallFromGetValue.kt b/compiler/testData/codegen/box/delegatedProperty/stackOverflowOnCallFromGetValue.kt index cc974dc1831..f46a3f6deea 100644 --- a/compiler/testData/codegen/box/delegatedProperty/stackOverflowOnCallFromGetValue.kt +++ b/compiler/testData/codegen/box/delegatedProperty/stackOverflowOnCallFromGetValue.kt @@ -1,4 +1,3 @@ -// IGNORE_BACKEND_FIR: JVM_IR // TARGET_BACKEND: JVM // WITH_REFLECT diff --git a/compiler/testData/codegen/box/delegatedProperty/useKPropertyLater.kt b/compiler/testData/codegen/box/delegatedProperty/useKPropertyLater.kt index 7726b2352ec..096575f1e0a 100644 --- a/compiler/testData/codegen/box/delegatedProperty/useKPropertyLater.kt +++ b/compiler/testData/codegen/box/delegatedProperty/useKPropertyLater.kt @@ -1,4 +1,3 @@ -// IGNORE_BACKEND_FIR: JVM_IR // WITH_REFLECT // KJS_WITH_FULL_RUNTIME diff --git a/compiler/testData/codegen/box/delegatedProperty/useReflectionOnKProperty.kt b/compiler/testData/codegen/box/delegatedProperty/useReflectionOnKProperty.kt index 021d5763714..15069d1c6de 100644 --- a/compiler/testData/codegen/box/delegatedProperty/useReflectionOnKProperty.kt +++ b/compiler/testData/codegen/box/delegatedProperty/useReflectionOnKProperty.kt @@ -1,4 +1,3 @@ -// IGNORE_BACKEND_FIR: JVM_IR // IGNORE_BACKEND: JS_IR // TODO: muted automatically, investigate should it be ran for JS or not // IGNORE_BACKEND: JS, NATIVE diff --git a/compiler/testData/codegen/box/inlineClasses/functionNameMangling/genericSignatureOfFunctionWithMangledName.kt b/compiler/testData/codegen/box/inlineClasses/functionNameMangling/genericSignatureOfFunctionWithMangledName.kt index b6a4c48451d..2880d7ee38d 100644 --- a/compiler/testData/codegen/box/inlineClasses/functionNameMangling/genericSignatureOfFunctionWithMangledName.kt +++ b/compiler/testData/codegen/box/inlineClasses/functionNameMangling/genericSignatureOfFunctionWithMangledName.kt @@ -1,5 +1,4 @@ // !LANGUAGE: +InlineClasses -// IGNORE_BACKEND_FIR: JVM_IR // TARGET_BACKEND: JVM // WITH_REFLECT diff --git a/compiler/testData/codegen/box/multiplatform/starImportOfExpectEnumWithActualTypeAlias.kt b/compiler/testData/codegen/box/multiplatform/starImportOfExpectEnumWithActualTypeAlias.kt index 6e0a9432e15..7188b314310 100644 --- a/compiler/testData/codegen/box/multiplatform/starImportOfExpectEnumWithActualTypeAlias.kt +++ b/compiler/testData/codegen/box/multiplatform/starImportOfExpectEnumWithActualTypeAlias.kt @@ -1,5 +1,4 @@ // !LANGUAGE: +MultiPlatformProjects -// IGNORE_BACKEND_FIR: JVM_IR // IGNORE_BACKEND: JS // IGNORE_BACKEND: JS_IR diff --git a/compiler/testData/codegen/box/reflection/annotations/genericExtensionProperty.kt b/compiler/testData/codegen/box/reflection/annotations/genericExtensionProperty.kt index 10e6cbbb0ea..ba05011d5b5 100644 --- a/compiler/testData/codegen/box/reflection/annotations/genericExtensionProperty.kt +++ b/compiler/testData/codegen/box/reflection/annotations/genericExtensionProperty.kt @@ -1,4 +1,3 @@ -// IGNORE_BACKEND_FIR: JVM_IR // TARGET_BACKEND: JVM // WITH_REFLECT diff --git a/compiler/testData/codegen/box/reflection/annotations/propertyWithoutBackingField.kt b/compiler/testData/codegen/box/reflection/annotations/propertyWithoutBackingField.kt index 38b6d692684..63b920ea1a9 100644 --- a/compiler/testData/codegen/box/reflection/annotations/propertyWithoutBackingField.kt +++ b/compiler/testData/codegen/box/reflection/annotations/propertyWithoutBackingField.kt @@ -1,4 +1,3 @@ -// IGNORE_BACKEND_FIR: JVM_IR // IGNORE_BACKEND: JS_IR // TODO: muted automatically, investigate should it be ran for JS or not // IGNORE_BACKEND: JS, NATIVE diff --git a/compiler/testData/codegen/box/reflection/annotations/simpleValAnnotation.kt b/compiler/testData/codegen/box/reflection/annotations/simpleValAnnotation.kt index 2e60c8ecd03..709f4ecee6b 100644 --- a/compiler/testData/codegen/box/reflection/annotations/simpleValAnnotation.kt +++ b/compiler/testData/codegen/box/reflection/annotations/simpleValAnnotation.kt @@ -1,4 +1,3 @@ -// IGNORE_BACKEND_FIR: JVM_IR // IGNORE_BACKEND: JS_IR // TODO: muted automatically, investigate should it be ran for JS or not // IGNORE_BACKEND: JS, NATIVE diff --git a/compiler/testData/codegen/box/reflection/callBy/boundExtensionPropertyAcessor.kt b/compiler/testData/codegen/box/reflection/callBy/boundExtensionPropertyAcessor.kt index 95b33d3f365..7a44fb572df 100644 --- a/compiler/testData/codegen/box/reflection/callBy/boundExtensionPropertyAcessor.kt +++ b/compiler/testData/codegen/box/reflection/callBy/boundExtensionPropertyAcessor.kt @@ -1,4 +1,3 @@ -// IGNORE_BACKEND_FIR: JVM_IR // IGNORE_BACKEND: JS_IR // TODO: muted automatically, investigate should it be ran for JS or not // IGNORE_BACKEND: JS, NATIVE diff --git a/compiler/testData/codegen/box/reflection/methodsFromAny/extensionPropertyReceiverToString.kt b/compiler/testData/codegen/box/reflection/methodsFromAny/extensionPropertyReceiverToString.kt index e207467bcbf..e330044f23f 100644 --- a/compiler/testData/codegen/box/reflection/methodsFromAny/extensionPropertyReceiverToString.kt +++ b/compiler/testData/codegen/box/reflection/methodsFromAny/extensionPropertyReceiverToString.kt @@ -1,4 +1,3 @@ -// IGNORE_BACKEND_FIR: JVM_IR // IGNORE_BACKEND: JS_IR // TODO: muted automatically, investigate should it be ran for JS or not // IGNORE_BACKEND: JS, NATIVE diff --git a/compiler/testData/codegen/box/reflection/methodsFromAny/functionToString.kt b/compiler/testData/codegen/box/reflection/methodsFromAny/functionToString.kt index b76cb15e012..a817ef22cca 100644 --- a/compiler/testData/codegen/box/reflection/methodsFromAny/functionToString.kt +++ b/compiler/testData/codegen/box/reflection/methodsFromAny/functionToString.kt @@ -1,4 +1,3 @@ -// IGNORE_BACKEND_FIR: JVM_IR // IGNORE_BACKEND: JS_IR // TODO: muted automatically, investigate should it be ran for JS or not // IGNORE_BACKEND: JS, NATIVE diff --git a/compiler/testData/codegen/box/reflection/methodsFromAny/propertyToString.kt b/compiler/testData/codegen/box/reflection/methodsFromAny/propertyToString.kt index 161778af2de..21f49f193eb 100644 --- a/compiler/testData/codegen/box/reflection/methodsFromAny/propertyToString.kt +++ b/compiler/testData/codegen/box/reflection/methodsFromAny/propertyToString.kt @@ -1,4 +1,3 @@ -// IGNORE_BACKEND_FIR: JVM_IR // IGNORE_BACKEND: JS_IR // TODO: muted automatically, investigate should it be ran for JS or not // IGNORE_BACKEND: JS, NATIVE diff --git a/compiler/testData/codegen/box/reflection/modifiers/properties.kt b/compiler/testData/codegen/box/reflection/modifiers/properties.kt index d84c3a8178b..0157c34957c 100644 --- a/compiler/testData/codegen/box/reflection/modifiers/properties.kt +++ b/compiler/testData/codegen/box/reflection/modifiers/properties.kt @@ -1,4 +1,3 @@ -// IGNORE_BACKEND_FIR: JVM_IR // IGNORE_BACKEND: JS_IR // TODO: muted automatically, investigate should it be ran for JS or not // IGNORE_BACKEND: JS, NATIVE diff --git a/compiler/testData/codegen/box/reflection/parameters/propertySetter.kt b/compiler/testData/codegen/box/reflection/parameters/propertySetter.kt index 5956a228650..8d79dc5565c 100644 --- a/compiler/testData/codegen/box/reflection/parameters/propertySetter.kt +++ b/compiler/testData/codegen/box/reflection/parameters/propertySetter.kt @@ -1,4 +1,3 @@ -// IGNORE_BACKEND_FIR: JVM_IR // IGNORE_BACKEND: JS_IR // TODO: muted automatically, investigate should it be ran for JS or not // IGNORE_BACKEND: JS, NATIVE diff --git a/compiler/testData/codegen/box/reflection/properties/getDelegate/notDelegatedProperty.kt b/compiler/testData/codegen/box/reflection/properties/getDelegate/notDelegatedProperty.kt index 23c7647d075..83ff07b2f12 100644 --- a/compiler/testData/codegen/box/reflection/properties/getDelegate/notDelegatedProperty.kt +++ b/compiler/testData/codegen/box/reflection/properties/getDelegate/notDelegatedProperty.kt @@ -1,4 +1,3 @@ -// IGNORE_BACKEND_FIR: JVM_IR // TARGET_BACKEND: JVM // WITH_REFLECT diff --git a/compiler/testData/codegen/box/typealias/privateInKlib.kt b/compiler/testData/codegen/box/typealias/privateInKlib.kt index addad8fca58..fc9e1658ca3 100644 --- a/compiler/testData/codegen/box/typealias/privateInKlib.kt +++ b/compiler/testData/codegen/box/typealias/privateInKlib.kt @@ -1,4 +1,3 @@ -// IGNORE_BACKEND_FIR: JVM_IR // MODULE: lib // FILE: f1.kt