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,
parentCodegen instanceof ImplementationBodyCodegen
? ((ImplementationBodyCodegen) parentCodegen).serializer
: DescriptorSerializer.createTopLevel(extension),
: DescriptorSerializer.createTopLevel(extension, null),
state.getProject()
);
@@ -127,7 +127,7 @@ public class PackagePartCodegen extends MemberCodegen<KtFile> {
List<MemberDescriptor> members = CodegenUtil.getMemberDescriptorsToGenerate(codegen.element, codegen.bindingContext);
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);
extension.serializeJvmPackage(builder, packagePartType);
@@ -138,7 +138,7 @@ open class MetadataSerializer(
private val extension = createSerializerExtension()
fun run() {
val serializer = DescriptorSerializer.createTopLevel(extension)
val serializer = DescriptorSerializer.createTopLevel(extension, project)
serializeClasses(classes, serializer, project)
serializeMembers(members, serializer)
serializeStringTable()
@@ -40,7 +40,7 @@ class DescriptorMetadataSerializer(
is DescriptorMetadataSource.Script -> DescriptorSerializer.create(
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)
else -> null
}
@@ -56,7 +56,7 @@ abstract class KlibMetadataSerializer(
)
return SerializerContext(
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.memberKind
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.immediateSupertypes
import org.jetbrains.kotlin.types.typeUtil.replaceAnnotations
import java.util.*
class DescriptorSerializer private constructor(
@@ -46,7 +48,8 @@ class DescriptorSerializer private constructor(
val typeTable: MutableTypeTable,
private val versionRequirementTable: MutableVersionRequirementTable?,
private val serializeTypeTableToFunction: Boolean,
val plugins: List<DescriptorSerializerPlugin> = emptyList()
val plugins: List<DescriptorSerializerPlugin> = emptyList(),
val typeAttributeTranslators: TypeAttributeTranslators? = null
) {
private val contractSerializer = ContractSerializer()
@@ -60,7 +63,7 @@ class DescriptorSerializer private constructor(
private fun createChildSerializer(descriptor: DeclarationDescriptor): DescriptorSerializer =
DescriptorSerializer(
descriptor, Interner(typeParameters), extension, typeTable, versionRequirementTable,
serializeTypeTableToFunction = false
serializeTypeTableToFunction = false, typeAttributeTranslators = typeAttributeTranslators
)
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
}
@@ -859,9 +866,10 @@ class DescriptorSerializer private constructor(
companion object {
@JvmStatic
fun createTopLevel(extension: SerializerExtension): DescriptorSerializer =
fun createTopLevel(extension: SerializerExtension, project: Project? = null): DescriptorSerializer =
DescriptorSerializer(
null, Interner(), extension, MutableTypeTable(), MutableVersionRequirementTable(), serializeTypeTableToFunction = false
null, Interner(), extension, MutableTypeTable(), MutableVersionRequirementTable(), serializeTypeTableToFunction = false,
typeAttributeTranslators = project?.let { TypeAttributeTranslatorExtension.createTranslators(it) }
)
@JvmStatic
@@ -883,6 +891,7 @@ class DescriptorSerializer private constructor(
else
createTopLevel(extension)
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
// serializing outer classes before nested classes.
@@ -895,7 +904,8 @@ class DescriptorSerializer private constructor(
if (container is ClassDescriptor && !isVersionRequirementTableWrittenCorrectly(extension.metadataVersion))
parent.versionRequirementTable else MutableVersionRequirementTable(),
serializeTypeTableToFunction = false,
plugins
plugins,
typeAttributeTranslators
)
for (typeParameter in descriptor.declaredTypeParameters) {
serializer.typeParameters.intern(typeParameter)
@@ -104,7 +104,7 @@ class 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 =
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.SimpleTypeMarker
import org.jetbrains.kotlin.types.model.TypeArgumentListMarker
import org.jetbrains.kotlin.types.typeUtil.builtIns
/**
* [KotlinType] has only two direct subclasses: [WrappedType] and [UnwrappedType].
@@ -54,7 +53,7 @@ sealed class KotlinType : Annotated, KotlinTypeMarker {
abstract val memberScope: MemberScope
abstract val attributes: TypeAttributes
override val annotations: Annotations
get() = attributes.toAnnotations()
get() = attributes.toDefaultAnnotations()
abstract fun unwrap(): UnwrappedType
@@ -165,7 +165,7 @@ object KotlinTypeFactory {
if (annotations.isEmpty())
it
else
SimpleTypeWithAttributes(it, annotations.toAttributes())
SimpleTypeWithAttributes(it, annotations.toDefaultAttributes())
}
@JvmStatic
@@ -209,7 +209,7 @@ object KotlinTypeFactory {
if (annotations.isEmpty())
it
else
SimpleTypeWithAttributes(it, annotations.toAttributes())
SimpleTypeWithAttributes(it, annotations.toDefaultAttributes())
}
@JvmStatic
@@ -113,12 +113,12 @@ class TypeAttributes private constructor(attributes: List<TypeAttribute<*>>) : A
get() = Companion
}
fun TypeAttributes.toAnnotations(): Annotations =
fun TypeAttributes.toDefaultAnnotations(): Annotations =
DefaultTypeAttributesTranslator.toAnnotations(this)
fun Annotations.toAttributes(): TypeAttributes = DefaultTypeAttributesTranslator.toAttributes(this)
fun Annotations.toDefaultAttributes(): TypeAttributes = DefaultTypeAttributesTranslator.toAttributes(this)
fun TypeAttributes.replaceAnnotations(newAnnotations: Annotations): TypeAttributes {
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)
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 =
NewCapturedType(captureStatus, constructor, lowerType, newAttributes, isMarkedNullable)