Add support for attribute serialization.

This commit is contained in:
Irene Dea
2021-10-14 17:56:05 -07:00
committed by Dmitriy Novozhilov
parent f2a351367c
commit d444978ebf
11 changed files with 29 additions and 20 deletions
@@ -128,7 +128,7 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen {
descriptor, extension, descriptor, extension,
parentCodegen instanceof ImplementationBodyCodegen parentCodegen instanceof ImplementationBodyCodegen
? ((ImplementationBodyCodegen) parentCodegen).serializer ? ((ImplementationBodyCodegen) parentCodegen).serializer
: DescriptorSerializer.createTopLevel(extension), : DescriptorSerializer.createTopLevel(extension, null),
state.getProject() state.getProject()
); );
@@ -127,7 +127,7 @@ public class PackagePartCodegen extends MemberCodegen<KtFile> {
List<MemberDescriptor> members = CodegenUtil.getMemberDescriptorsToGenerate(codegen.element, codegen.bindingContext); List<MemberDescriptor> members = CodegenUtil.getMemberDescriptorsToGenerate(codegen.element, codegen.bindingContext);
JvmSerializerExtension extension = new JvmSerializerExtension(codegen.v.getSerializationBindings(), codegen.state); JvmSerializerExtension extension = new JvmSerializerExtension(codegen.v.getSerializationBindings(), codegen.state);
DescriptorSerializer serializer = DescriptorSerializer.createTopLevel(extension); DescriptorSerializer serializer = DescriptorSerializer.createTopLevel(extension, null);
ProtoBuf.Package.Builder builder = serializer.packagePartProto(codegen.element.getPackageFqName(), members); ProtoBuf.Package.Builder builder = serializer.packagePartProto(codegen.element.getPackageFqName(), members);
extension.serializeJvmPackage(builder, packagePartType); extension.serializeJvmPackage(builder, packagePartType);
@@ -138,7 +138,7 @@ open class MetadataSerializer(
private val extension = createSerializerExtension() private val extension = createSerializerExtension()
fun run() { fun run() {
val serializer = DescriptorSerializer.createTopLevel(extension) val serializer = DescriptorSerializer.createTopLevel(extension, project)
serializeClasses(classes, serializer, project) serializeClasses(classes, serializer, project)
serializeMembers(members, serializer) serializeMembers(members, serializer)
serializeStringTable() serializeStringTable()
@@ -40,7 +40,7 @@ class DescriptorMetadataSerializer(
is DescriptorMetadataSource.Script -> DescriptorSerializer.create( is DescriptorMetadataSource.Script -> DescriptorSerializer.create(
metadata.descriptor, serializerExtension, (parent as? DescriptorMetadataSerializer)?.serializer, context.state.project metadata.descriptor, serializerExtension, (parent as? DescriptorMetadataSerializer)?.serializer, context.state.project
) )
is DescriptorMetadataSource.File -> DescriptorSerializer.createTopLevel(serializerExtension) is DescriptorMetadataSource.File -> DescriptorSerializer.createTopLevel(serializerExtension, context.state.project)
is DescriptorMetadataSource.Function -> DescriptorSerializer.createForLambda(serializerExtension) is DescriptorMetadataSource.Function -> DescriptorSerializer.createForLambda(serializerExtension)
else -> null else -> null
} }
@@ -56,7 +56,7 @@ abstract class KlibMetadataSerializer(
) )
return SerializerContext( return SerializerContext(
extension, extension,
DescriptorSerializer.createTopLevel(extension) DescriptorSerializer.createTopLevel(extension, project)
) )
} }
@@ -35,8 +35,10 @@ import org.jetbrains.kotlin.serialization.deserialization.ProtoEnumFlags
import org.jetbrains.kotlin.serialization.deserialization.descriptorVisibility import org.jetbrains.kotlin.serialization.deserialization.descriptorVisibility
import org.jetbrains.kotlin.serialization.deserialization.memberKind import org.jetbrains.kotlin.serialization.deserialization.memberKind
import org.jetbrains.kotlin.types.* import org.jetbrains.kotlin.types.*
import org.jetbrains.kotlin.types.extensions.TypeAttributeTranslators
import org.jetbrains.kotlin.types.typeUtil.contains import org.jetbrains.kotlin.types.typeUtil.contains
import org.jetbrains.kotlin.types.typeUtil.immediateSupertypes import org.jetbrains.kotlin.types.typeUtil.immediateSupertypes
import org.jetbrains.kotlin.types.typeUtil.replaceAnnotations
import java.util.* import java.util.*
class DescriptorSerializer private constructor( class DescriptorSerializer private constructor(
@@ -46,7 +48,8 @@ class DescriptorSerializer private constructor(
val typeTable: MutableTypeTable, val typeTable: MutableTypeTable,
private val versionRequirementTable: MutableVersionRequirementTable?, private val versionRequirementTable: MutableVersionRequirementTable?,
private val serializeTypeTableToFunction: Boolean, private val serializeTypeTableToFunction: Boolean,
val plugins: List<DescriptorSerializerPlugin> = emptyList() val plugins: List<DescriptorSerializerPlugin> = emptyList(),
val typeAttributeTranslators: TypeAttributeTranslators? = null
) { ) {
private val contractSerializer = ContractSerializer() private val contractSerializer = ContractSerializer()
@@ -60,7 +63,7 @@ class DescriptorSerializer private constructor(
private fun createChildSerializer(descriptor: DeclarationDescriptor): DescriptorSerializer = private fun createChildSerializer(descriptor: DeclarationDescriptor): DescriptorSerializer =
DescriptorSerializer( DescriptorSerializer(
descriptor, Interner(typeParameters), extension, typeTable, versionRequirementTable, descriptor, Interner(typeParameters), extension, typeTable, versionRequirementTable,
serializeTypeTableToFunction = false serializeTypeTableToFunction = false, typeAttributeTranslators = typeAttributeTranslators
) )
val stringTable: DescriptorAwareStringTable val stringTable: DescriptorAwareStringTable
@@ -698,7 +701,11 @@ class DescriptorSerializer private constructor(
} }
} }
extension.serializeType(type, builder) val typeWithUpdatedAnnotations = typeAttributeTranslators?.let {
type.replaceAnnotations(it.toAnnotations(type.attributes))
} ?: type
extension.serializeType(typeWithUpdatedAnnotations, builder)
return builder return builder
} }
@@ -859,9 +866,10 @@ class DescriptorSerializer private constructor(
companion object { companion object {
@JvmStatic @JvmStatic
fun createTopLevel(extension: SerializerExtension): DescriptorSerializer = fun createTopLevel(extension: SerializerExtension, project: Project? = null): DescriptorSerializer =
DescriptorSerializer( DescriptorSerializer(
null, Interner(), extension, MutableTypeTable(), MutableVersionRequirementTable(), serializeTypeTableToFunction = false null, Interner(), extension, MutableTypeTable(), MutableVersionRequirementTable(), serializeTypeTableToFunction = false,
typeAttributeTranslators = project?.let { TypeAttributeTranslatorExtension.createTranslators(it) }
) )
@JvmStatic @JvmStatic
@@ -883,6 +891,7 @@ class DescriptorSerializer private constructor(
else else
createTopLevel(extension) createTopLevel(extension)
val plugins = project?.let { DescriptorSerializerPlugin.getInstances(it) }.orEmpty() val plugins = project?.let { DescriptorSerializerPlugin.getInstances(it) }.orEmpty()
val typeAttributeTranslators = project?.let { TypeAttributeTranslators(project) }
// Calculate type parameter ids for the outer class beforehand, as it would've had happened if we were always // Calculate type parameter ids for the outer class beforehand, as it would've had happened if we were always
// serializing outer classes before nested classes. // serializing outer classes before nested classes.
@@ -895,7 +904,8 @@ class DescriptorSerializer private constructor(
if (container is ClassDescriptor && !isVersionRequirementTableWrittenCorrectly(extension.metadataVersion)) if (container is ClassDescriptor && !isVersionRequirementTableWrittenCorrectly(extension.metadataVersion))
parent.versionRequirementTable else MutableVersionRequirementTable(), parent.versionRequirementTable else MutableVersionRequirementTable(),
serializeTypeTableToFunction = false, serializeTypeTableToFunction = false,
plugins plugins,
typeAttributeTranslators
) )
for (typeParameter in descriptor.declaredTypeParameters) { for (typeParameter in descriptor.declaredTypeParameters) {
serializer.typeParameters.intern(typeParameter) serializer.typeParameters.intern(typeParameter)
@@ -104,7 +104,7 @@ class CapturedType(
} }
override fun replaceAnnotations(newAnnotations: Annotations): CapturedType = override fun replaceAnnotations(newAnnotations: Annotations): CapturedType =
CapturedType(typeProjection, constructor, isMarkedNullable, newAnnotations.toAttributes()) CapturedType(typeProjection, constructor, isMarkedNullable, newAnnotations.toDefaultAttributes())
override fun replaceAttributes(newAttributes: TypeAttributes): SimpleType = override fun replaceAttributes(newAttributes: TypeAttributes): SimpleType =
CapturedType(typeProjection, constructor, isMarkedNullable, newAttributes) CapturedType(typeProjection, constructor, isMarkedNullable, newAttributes)
@@ -27,7 +27,6 @@ import org.jetbrains.kotlin.types.model.FlexibleTypeMarker
import org.jetbrains.kotlin.types.model.KotlinTypeMarker import org.jetbrains.kotlin.types.model.KotlinTypeMarker
import org.jetbrains.kotlin.types.model.SimpleTypeMarker import org.jetbrains.kotlin.types.model.SimpleTypeMarker
import org.jetbrains.kotlin.types.model.TypeArgumentListMarker import org.jetbrains.kotlin.types.model.TypeArgumentListMarker
import org.jetbrains.kotlin.types.typeUtil.builtIns
/** /**
* [KotlinType] has only two direct subclasses: [WrappedType] and [UnwrappedType]. * [KotlinType] has only two direct subclasses: [WrappedType] and [UnwrappedType].
@@ -54,7 +53,7 @@ sealed class KotlinType : Annotated, KotlinTypeMarker {
abstract val memberScope: MemberScope abstract val memberScope: MemberScope
abstract val attributes: TypeAttributes abstract val attributes: TypeAttributes
override val annotations: Annotations override val annotations: Annotations
get() = attributes.toAnnotations() get() = attributes.toDefaultAnnotations()
abstract fun unwrap(): UnwrappedType abstract fun unwrap(): UnwrappedType
@@ -165,7 +165,7 @@ object KotlinTypeFactory {
if (annotations.isEmpty()) if (annotations.isEmpty())
it it
else else
SimpleTypeWithAttributes(it, annotations.toAttributes()) SimpleTypeWithAttributes(it, annotations.toDefaultAttributes())
} }
@JvmStatic @JvmStatic
@@ -209,7 +209,7 @@ object KotlinTypeFactory {
if (annotations.isEmpty()) if (annotations.isEmpty())
it it
else else
SimpleTypeWithAttributes(it, annotations.toAttributes()) SimpleTypeWithAttributes(it, annotations.toDefaultAttributes())
} }
@JvmStatic @JvmStatic
@@ -113,12 +113,12 @@ class TypeAttributes private constructor(attributes: List<TypeAttribute<*>>) : A
get() = Companion get() = Companion
} }
fun TypeAttributes.toAnnotations(): Annotations = fun TypeAttributes.toDefaultAnnotations(): Annotations =
DefaultTypeAttributesTranslator.toAnnotations(this) DefaultTypeAttributesTranslator.toAnnotations(this)
fun Annotations.toAttributes(): TypeAttributes = DefaultTypeAttributesTranslator.toAttributes(this) fun Annotations.toDefaultAttributes(): TypeAttributes = DefaultTypeAttributesTranslator.toAttributes(this)
fun TypeAttributes.replaceAnnotations(newAnnotations: Annotations): TypeAttributes { fun TypeAttributes.replaceAnnotations(newAnnotations: Annotations): TypeAttributes {
val withoutCustom = (custom?.let { this.remove(it) } ?: this) val withoutCustom = (custom?.let { this.remove(it) } ?: this)
return withoutCustom.add(newAnnotations.toAttributes()) return withoutCustom.add(newAnnotations.toDefaultAttributes())
} }
@@ -213,7 +213,7 @@ class NewCapturedType(
get() = ErrorUtils.createErrorScope("No member resolution should be done on captured type!", true) get() = ErrorUtils.createErrorScope("No member resolution should be done on captured type!", true)
override fun replaceAnnotations(newAnnotations: Annotations) = override fun replaceAnnotations(newAnnotations: Annotations) =
NewCapturedType(captureStatus, constructor, lowerType, newAnnotations.toAttributes(), isMarkedNullable) NewCapturedType(captureStatus, constructor, lowerType, newAnnotations.toDefaultAttributes(), isMarkedNullable)
override fun replaceAttributes(newAttributes: TypeAttributes): SimpleType = override fun replaceAttributes(newAttributes: TypeAttributes): SimpleType =
NewCapturedType(captureStatus, constructor, lowerType, newAttributes, isMarkedNullable) NewCapturedType(captureStatus, constructor, lowerType, newAttributes, isMarkedNullable)