Add declarations from serialization plugin to FIR metadata declarations provider.

These declarations should not be visible to users (and therefore are not added to FIR),
but plugin itself can reference them in already compiled serializable classes,
and therefore they should be available in metadata:

- synthetic deserialization constructor
- static write$Self function

See also:
^KT-55885
This commit is contained in:
Leonid Startsev
2023-02-15 18:47:37 +01:00
committed by Space Team
parent 8953b25c5b
commit c564dd973b
8 changed files with 215 additions and 19 deletions
@@ -12,6 +12,8 @@ dependencies {
compileOnly(project(":compiler:backend.jvm.codegen"))
compileOnly(project(":compiler:backend.jvm.lower"))
compileOnly(project(":compiler:ir.tree"))
compileOnly(project(":compiler:fir:fir2ir"))
compileOnly(project(":compiler:fir:tree"))
compileOnly(project(":js:js.frontend"))
compileOnly(project(":js:js.translator"))
compileOnly(project(":kotlin-util-klib-metadata"))
@@ -6,6 +6,9 @@
package org.jetbrains.kotlinx.serialization.compiler.backend.ir
import org.jetbrains.kotlin.descriptors.DescriptorVisibilities
import org.jetbrains.kotlin.fir.declarations.impl.FirDefaultPropertyGetter
import org.jetbrains.kotlin.fir.declarations.impl.FirPrimaryConstructor
import org.jetbrains.kotlin.fir.lazy.Fir2IrLazyProperty
import org.jetbrains.kotlin.ir.ObsoleteDescriptorBasedAPI
import org.jetbrains.kotlin.ir.declarations.IrClass
import org.jetbrains.kotlin.ir.declarations.IrDeclarationOrigin
@@ -41,6 +44,41 @@ class IrSerializableProperties(
override val serializableStandaloneProperties: List<IrSerializableProperty>
) : ISerializableProperties<IrSerializableProperty>
/**
* This function checks if a deserialized property declares default value and has backing field.
*
* Returns (declaresDefaultValue, hasBackingField) boolean pair. Returns (false, false) for properties from current module.
*/
@OptIn(ObsoleteDescriptorBasedAPI::class)
fun IrProperty.analyzeIfFromAnotherModule(): Pair<Boolean, Boolean> {
return if (descriptor is DeserializedPropertyDescriptor) {
// IrLazyProperty does not deserialize backing fields correctly, so we should fall back to info from descriptor.
// DeserializedPropertyDescriptor can be encountered only after K1, so it is safe to check it.
val hasDefault = descriptor.declaresDefaultValue()
hasDefault to (descriptor.backingField != null || hasDefault)
} else if (this is Fir2IrLazyProperty) {
// Fir2IrLazyProperty after K2 correctly deserializes information about backing field.
// However, nor Fir2IrLazyProp nor deserialized FirProperty do not store default value (initializer expression) for property,
// so we either should find corresponding constructor parameter and check its default, or rely on less strict check for default getter.
// Comments are copied from PropertyDescriptor.declaresDefaultValue() as it has similar logic.
val hasBackingField = backingField != null
val matchingPrimaryConstructorParam = containingClass?.declarations?.filterIsInstance<FirPrimaryConstructor>()
?.singleOrNull()?.valueParameters?.find { it.name == this.name }
if (matchingPrimaryConstructorParam != null) {
// If property is a constructor parameter, check parameter default value
// (serializable classes always have parameters-as-properties, so no name clash here)
(matchingPrimaryConstructorParam.defaultValue != null) to hasBackingField
} else {
// If it is a body property, then it is likely to have initializer when getter is not specified
// note this approach is not working well if we have smth like `get() = field`, but such cases on cross-module boundaries
// should be very marginal. If we want to solve them, we need to add protobuf metadata extension.
(fir.getter is FirDefaultPropertyGetter) to hasBackingField
}
} else {
false to false
}
}
/**
* typeReplacement should be populated from FakeOverrides and is used when we want to determine the type for property
* accounting for generic substitutions performed in subclasses:
@@ -83,11 +121,7 @@ internal fun serializablePropertiesForIrBackend(
.filter(::isPropSerializable)
.map {
val isConstructorParameterWithDefault = primaryParamsAsProps[it] ?: false
// FIXME: workaround because IrLazyProperty doesn't deserialize information about backing fields. Fallback to descriptor won't work with FIR.
val isPropertyFromAnotherModuleDeclaresDefaultValue =
it.descriptor is DeserializedPropertyDescriptor && it.descriptor.declaresDefaultValue()
val isPropertyWithBackingFieldFromAnotherModule =
it.descriptor is DeserializedPropertyDescriptor && (it.descriptor.backingField != null || isPropertyFromAnotherModuleDeclaresDefaultValue)
val (isPropertyFromAnotherModuleDeclaresDefaultValue, isPropertyWithBackingFieldFromAnotherModule) = it.analyzeIfFromAnotherModule()
IrSerializableProperty(
it,
isConstructorParameterWithDefault,
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
@@ -39,10 +39,7 @@ class SerializableIrGenerator(
protected val properties = serializablePropertiesForIrBackend(irClass)
private val serialDescriptorClass = compilerContext.referenceClass(
ClassId(
SerializationPackages.descriptorsPackageFqName,
Name.identifier(SerialEntityNames.SERIAL_DESCRIPTOR_CLASS)
)
SerializationRuntimeClassIds.descriptorClassId
)!!.owner
private val serialDescriptorImplClass = compilerContext.referenceClass(