Rework flags in kotlinx.metadata:

Introduce extensions for KmNodes to work with various modifiers instead of old Flags.SOMETHING.invoke(flags).
Provide Visibility, Modality, and Kind enums.
Introduce KmPropertyAccessorAttributes to replace KmProperty.getterFlags and KmProperty.setterFlags.
Deprecate nodes constructors which take flags but not flags itself (yet).
Adapt KotlinP to changes.

#KT-59440 Fixed
This commit is contained in:
Leonid Startsev
2023-03-24 20:50:57 +01:00
committed by Space Team
parent ecc1f9fb0b
commit 1a1cbcb321
17 changed files with 1344 additions and 207 deletions
@@ -7,6 +7,9 @@ package org.jetbrains.kotlin.commonizer.metadata
import gnu.trove.THashMap
import kotlinx.metadata.*
import kotlinx.metadata.Modality as KmModality
import kotlinx.metadata.Visibility as KmVisibility
import kotlinx.metadata.ClassKind as KmClassKind
import kotlinx.metadata.klib.annotations
import kotlinx.metadata.klib.compileTimeValue
import kotlinx.metadata.klib.getterAnnotations
@@ -126,9 +126,8 @@ internal fun linkSealedClassesWithSubclasses(packageName: CirPackageName, classC
internal fun CirClassConstructor.serializeConstructor(
context: CirTreeSerializationContext
): KmConstructor = KmConstructor(
flags = classConstructorFlags()
).also { constructor ->
): KmConstructor = KmConstructor().also { constructor ->
constructor.flags = classConstructorFlags()
annotations.mapTo(constructor.annotations) { it.serializeAnnotation() }
// TODO: nowhere to write constructor type parameters
valueParameters.mapTo(constructor.valueParameters) { it.serializeValueParameter(context) }
@@ -137,9 +136,9 @@ internal fun CirClassConstructor.serializeConstructor(
internal fun CirTypeAlias.serializeTypeAlias(
context: CirTreeSerializationContext
): KmTypeAlias = KmTypeAlias(
flags = typeAliasFlags(),
name = name.name
).also { typeAlias ->
typeAlias.flags = typeAliasFlags()
annotations.mapTo(typeAlias.annotations) { it.serializeAnnotation() }
typeParameters.serializeTypeParameters(context, output = typeAlias.typeParameters)
typeAlias.underlyingType = underlyingType.serializeType(context, expansion = ONLY_ABBREVIATIONS)
@@ -148,12 +147,10 @@ internal fun CirTypeAlias.serializeTypeAlias(
internal fun CirProperty.serializeProperty(
context: CirTreeSerializationContext,
): KmProperty = KmProperty(
flags = propertyFlags(isExpect = context.isCommon && !isLiftedUp),
name = name.name,
getterFlags = getter?.propertyAccessorFlags(this, this) ?: NO_FLAGS,
setterFlags = setter?.let { setter -> setter.propertyAccessorFlags(setter, this) } ?: NO_FLAGS
).also { property ->
): KmProperty = KmProperty(name = name.name).also { property ->
property.flags = propertyFlags(isExpect = context.isCommon && !isLiftedUp)
property.getterFlags = getter?.propertyAccessorFlags(this, this) ?: NO_FLAGS
property.setterFlags = setter?.let { setter -> setter.propertyAccessorFlags(setter, this) } ?: NO_FLAGS
annotations.mapTo(property.annotations) { it.serializeAnnotation() }
getter?.annotations?.mapTo(property.getterAnnotations) { it.serializeAnnotation() }
setter?.annotations?.mapTo(property.setterAnnotations) { it.serializeAnnotation() }
@@ -181,9 +178,9 @@ internal fun CirProperty.serializeProperty(
internal fun CirFunction.serializeFunction(
context: CirTreeSerializationContext,
): KmFunction = KmFunction(
flags = functionFlags(isExpect = context.isCommon && kind != CallableMemberDescriptor.Kind.SYNTHESIZED),
name = name.name
).also { function ->
function.flags = functionFlags(isExpect = context.isCommon && kind != CallableMemberDescriptor.Kind.SYNTHESIZED)
annotations.mapTo(function.annotations) { it.serializeAnnotation() }
typeParameters.serializeTypeParameters(context, output = function.typeParameters)
valueParameters.mapTo(function.valueParameters) { it.serializeValueParameter(context) }
@@ -242,9 +239,9 @@ private fun CirConstantValue.serializeConstantValue(): KmAnnotationArgument? = w
private fun CirValueParameter.serializeValueParameter(
context: CirTreeSerializationContext
): KmValueParameter = KmValueParameter(
flags = valueParameterFlags(),
name = name.name
).also { parameter ->
parameter.flags = valueParameterFlags()
annotations.mapTo(parameter.annotations) { it.serializeAnnotation() }
parameter.type = returnType.serializeType(context)
varargElementType?.let { varargElementType ->
@@ -258,11 +255,11 @@ private fun List<CirTypeParameter>.serializeTypeParameters(
) {
mapIndexedTo(output) { index, cirTypeParameter ->
KmTypeParameter(
flags = cirTypeParameter.typeParameterFlags(),
name = cirTypeParameter.name.name,
id = context.typeParameterIndexOffset + index,
variance = cirTypeParameter.variance.serializeVariance()
).also { parameter ->
parameter.flags = cirTypeParameter.typeParameterFlags()
cirTypeParameter.upperBounds.mapTo(parameter.upperBounds) { it.serializeType(context) }
cirTypeParameter.annotations.mapTo(parameter.annotations) { it.serializeAnnotation() }
}
@@ -287,14 +284,16 @@ private fun CirType.serializeType(
}
private fun CirTypeParameterType.serializeTypeParameterType(): KmType =
KmType(typeFlags()).also { type ->
KmType().also { type ->
applyTypeFlagsTo(type)
type.classifier = KmClassifier.TypeParameter(index)
}
private fun CirClassType.serializeClassType(
context: CirTreeSerializationContext,
expansion: TypeAliasExpansion = FOR_TOP_LEVEL_TYPE
): KmType = KmType(typeFlags()).also { type ->
): KmType = KmType().also { type ->
applyTypeFlagsTo(type)
type.classifier = KmClassifier.Class(classifierId.toString())
arguments.mapTo(type.arguments) { it.serializeArgument(context, expansion) }
outerType?.let { type.outerType = it.serializeClassType(context, expansion) }
@@ -318,7 +317,7 @@ private fun CirTypeAliasType.serializeAbbreviationType(
context: CirTreeSerializationContext,
expansion: TypeAliasExpansion
): KmType {
val abbreviationType = KmType(typeFlags())
val abbreviationType = KmType().also { applyTypeFlagsTo(it) }
abbreviationType.classifier = KmClassifier.TypeAlias(classifierId.toString())
arguments.mapTo(abbreviationType.arguments) { it.serializeArgument(context, expansion) }
return abbreviationType
@@ -5,9 +5,7 @@
package org.jetbrains.kotlin.commonizer.metadata
import kotlinx.metadata.Flag
import kotlinx.metadata.Flags
import kotlinx.metadata.flagsOf
import kotlinx.metadata.*
import org.jetbrains.kotlin.commonizer.cir.*
import org.jetbrains.kotlin.descriptors.CallableMemberDescriptor
import org.jetbrains.kotlin.descriptors.ClassKind
@@ -59,11 +57,13 @@ internal fun CirClassConstructor.classConstructorFlags(): Flags =
Flag.Constructor.HAS_NON_STABLE_PARAMETER_NAMES.takeIf { !hasStableParameterNames }
)
internal fun CirType.typeFlags(): Flags =
flagsOfNotNull(
nullableFlag,
//Flag.Type.IS_SUSPEND.takeIf { false }
)
internal fun CirType.applyTypeFlagsTo(type: KmType) {
type.isNullable = when (this) {
is CirSimpleType -> isMarkedNullable
is CirFlexibleType -> lowerBound.isMarkedNullable
}
//Flag.Type.IS_SUSPEND.takeIf { false }
}
internal fun CirTypeParameter.typeParameterFlags(): Flags =
flagsOfNotNull(
@@ -172,14 +172,4 @@ private inline val CirProperty.modifiersFlags: Flags
Flag.Property.IS_LATEINIT.takeIf { isLateInit },
)
private inline val CirType.nullableFlag: Flag?
get() {
val isNullable = when (this) {
is CirSimpleType -> isMarkedNullable
is CirFlexibleType -> lowerBound.isMarkedNullable
}
return if (isNullable) Flag.Type.IS_NULLABLE else null
}
private fun flagsOfNotNull(vararg flags: Flag?): Flags = flagsOf(*listOfNotNull(*flags).toTypedArray())