Minor: get rid of some compiler warnings in kotlin-serialization-compiler-plugin
This commit is contained in:
+15
-13
@@ -26,9 +26,11 @@ class SerializableCompanionIrGenerator(
|
||||
get() = _table
|
||||
|
||||
companion object {
|
||||
fun generate(irClass: IrClass,
|
||||
context: BackendContext,
|
||||
bindingContext: BindingContext) {
|
||||
fun generate(
|
||||
irClass: IrClass,
|
||||
context: BackendContext,
|
||||
bindingContext: BindingContext
|
||||
) {
|
||||
val companionDescriptor = irClass.descriptor
|
||||
val serializableClass = getSerializableClassDescriptorByCompanion(companionDescriptor) ?: return
|
||||
if (serializableClass.isInternalSerializable)
|
||||
@@ -37,15 +39,15 @@ class SerializableCompanionIrGenerator(
|
||||
}
|
||||
|
||||
override fun generateSerializerGetter(methodDescriptor: FunctionDescriptor) =
|
||||
irClass.contributeFunction(methodDescriptor, fromStubs = true) { getter ->
|
||||
val serializer = serializableDescriptor.classSerializer?.toClassDescriptor!!
|
||||
val expr = if (serializer.kind == ClassKind.OBJECT) {
|
||||
irGetObject(serializer)
|
||||
} else {
|
||||
val ctor = compilerContext.externalSymbols.referenceConstructor(serializer.unsubstitutedPrimaryConstructor!!)
|
||||
val args: List<IrExpression> = emptyList() // todo
|
||||
irInvoke(null, ctor, *args.toTypedArray())
|
||||
irClass.contributeFunction(methodDescriptor, fromStubs = true) { _ ->
|
||||
val serializer = serializableDescriptor.classSerializer?.toClassDescriptor!!
|
||||
val expr = if (serializer.kind == ClassKind.OBJECT) {
|
||||
irGetObject(serializer)
|
||||
} else {
|
||||
val ctor = compilerContext.externalSymbols.referenceConstructor(serializer.unsubstitutedPrimaryConstructor!!)
|
||||
val args: List<IrExpression> = emptyList() // todo
|
||||
irInvoke(null, ctor, *args.toTypedArray())
|
||||
}
|
||||
+irReturn(expr)
|
||||
}
|
||||
+irReturn(expr)
|
||||
}
|
||||
}
|
||||
+2
-3
@@ -131,7 +131,7 @@ class SerializerIrGenerator(val irClass: IrClass, override val compilerContext:
|
||||
compilerContext.externalSymbols.referenceClass(module.getClassFromInternalSerializationPackage(SpecialBuiltins.nullableSerializer))
|
||||
if (serializerClass == null) {
|
||||
if (genericIndex == null) return null
|
||||
return TODO("Saved serializer for generic argument")
|
||||
TODO("Saved serializer for generic argument")
|
||||
}
|
||||
if (serializerClass.kind == ClassKind.OBJECT) {
|
||||
return irGetObject(serializerClass)
|
||||
@@ -144,8 +144,7 @@ class SerializerIrGenerator(val irClass: IrClass, override val compilerContext:
|
||||
// todo: smth better than constructors[0] ??
|
||||
if (it.type.isMarkedNullable) irInvoke(null, nullableSerClass.constructors.toList()[0], expr) else expr
|
||||
}
|
||||
if (serializerClass.classId == referenceArraySerializerId)
|
||||
args = TODO("reference array serializer")
|
||||
if (serializerClass.classId == referenceArraySerializerId) TODO("reference array serializer")
|
||||
val serializable = getSerializableClassDescriptorBySerializer(serializerClass)
|
||||
val ctor = if (serializable?.declaredTypeParameters?.isNotEmpty() == true) {
|
||||
KSerializerDescriptorResolver.createTypedSerializerConstructorDescriptor(serializerClass, serializableDescriptor)
|
||||
|
||||
+1
@@ -55,6 +55,7 @@ class SerializableJsTranslator(
|
||||
|
||||
val f = context.buildFunction(constructorDescriptor) { jsFun, context ->
|
||||
val thiz = jsFun.scope.declareName(Namer.ANOTHER_THIS_PARAMETER_NAME).makeRef()
|
||||
@Suppress("NAME_SHADOWING")
|
||||
val context = context.innerContextWithAliased(serializableDescriptor.thisAsReceiverParameter, thiz)
|
||||
|
||||
+JsVars(
|
||||
|
||||
+3
-3
@@ -236,9 +236,9 @@ class SerializerJsTranslator(descriptor: ClassDescriptor,
|
||||
}
|
||||
val inputVar = JsNameRef(jsFun.scope.declareFreshName("input"))
|
||||
val readBeginF = decoderClass.getFuncDesc(CallingConventions.begin).single()
|
||||
val call = JsInvocation(JsNameRef(context.getNameForDescriptor(readBeginF), JsNameRef(jsFun.parameters[0].name)),
|
||||
serialClassDescRef, JsArrayLiteral(typeParams))
|
||||
+JsVars(JsVars.JsVar(inputVar.name, call))
|
||||
val readBeginCall = JsInvocation(JsNameRef(context.getNameForDescriptor(readBeginF), JsNameRef(jsFun.parameters[0].name)),
|
||||
serialClassDescRef, JsArrayLiteral(typeParams))
|
||||
+JsVars(JsVars.JsVar(inputVar.name, readBeginCall))
|
||||
|
||||
// while(true) {
|
||||
val loop = JsLabel(jsFun.scope.declareFreshName("loopLabel"))
|
||||
|
||||
+3
-4
@@ -28,7 +28,6 @@ import org.jetbrains.kotlin.resolve.BindingContext
|
||||
import org.jetbrains.kotlin.resolve.DescriptorUtils
|
||||
import org.jetbrains.kotlin.resolve.descriptorUtil.getSuperClassOrAny
|
||||
import org.jetbrains.kotlin.resolve.descriptorUtil.module
|
||||
import org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmMethodSignature
|
||||
import org.jetbrains.kotlinx.serialization.compiler.backend.common.SerializableCodegen
|
||||
import org.jetbrains.kotlinx.serialization.compiler.backend.common.anonymousInitializers
|
||||
import org.jetbrains.kotlinx.serialization.compiler.backend.common.bodyPropertiesDescriptorsMap
|
||||
@@ -62,14 +61,14 @@ class SerializableCodegenImpl(
|
||||
private val SerializableProperty.asmType get() = classCodegen.typeMapper.mapType(this.type)
|
||||
|
||||
override fun generateInternalConstructor(constructorDescriptor: ClassConstructorDescriptor) {
|
||||
classCodegen.generateMethod(constructorDescriptor, { sig, expr -> doGenerateConstructorImpl(expr) })
|
||||
classCodegen.generateMethod(constructorDescriptor) { _, expr -> doGenerateConstructorImpl(expr) }
|
||||
}
|
||||
|
||||
override fun generateWriteSelfMethod(methodDescriptor: FunctionDescriptor) {
|
||||
classCodegen.generateMethod(methodDescriptor, { sig, expr -> doGenerateWriteSelf(expr, sig) })
|
||||
classCodegen.generateMethod(methodDescriptor) { _, expr -> doGenerateWriteSelf(expr) }
|
||||
}
|
||||
|
||||
private fun InstructionAdapter.doGenerateWriteSelf(exprCodegen: ExpressionCodegen, signature: JvmMethodSignature) {
|
||||
private fun InstructionAdapter.doGenerateWriteSelf(exprCodegen: ExpressionCodegen) {
|
||||
val thisI = 0
|
||||
val outputI = 1
|
||||
val serialDescI = 2
|
||||
|
||||
+2
-2
@@ -35,9 +35,9 @@ class SerializableCompanionCodegenImpl(private val codegen: ImplementationBodyCo
|
||||
|
||||
override fun generateSerializerGetter(methodDescriptor: FunctionDescriptor) {
|
||||
val serial = serializableDescriptor.classSerializer?.toClassDescriptor ?: return
|
||||
codegen.generateMethod(methodDescriptor) {sig, expr ->
|
||||
codegen.generateMethod(methodDescriptor) { _, _ ->
|
||||
stackValueSerializerInstance(codegen, serializableDescriptor.module, serializableDescriptor.defaultType, serial, this, null) {
|
||||
load(it+1, kSerializerType)
|
||||
load(it + 1, kSerializerType)
|
||||
}
|
||||
areturn(kSerializerType)
|
||||
}
|
||||
|
||||
+15
-11
@@ -49,11 +49,13 @@ class SerializerCodegenImpl(
|
||||
|
||||
override fun generateGenericFieldsAndConstructor(typedConstructorDescriptor: ConstructorDescriptor) {
|
||||
serializableDescriptor.declaredTypeParameters.forEachIndexed { i, _ ->
|
||||
codegen.v.newField(OtherOrigin(codegen.myClass.psiOrParent), ACC_PRIVATE or ACC_SYNTHETIC,
|
||||
"$typeArgPrefix$i", kSerializerType.descriptor, null, null )
|
||||
codegen.v.newField(
|
||||
OtherOrigin(codegen.myClass.psiOrParent), ACC_PRIVATE or ACC_SYNTHETIC,
|
||||
"$typeArgPrefix$i", kSerializerType.descriptor, null, null
|
||||
)
|
||||
}
|
||||
|
||||
codegen.generateMethod(typedConstructorDescriptor) { sig, exprCodegen ->
|
||||
codegen.generateMethod(typedConstructorDescriptor) { _, _ ->
|
||||
load(0, serializerAsmType)
|
||||
invokespecial("java/lang/Object", "<init>", "()V", false)
|
||||
serializableDescriptor.declaredTypeParameters.forEachIndexed { i, _ ->
|
||||
@@ -143,7 +145,7 @@ class SerializerCodegenImpl(
|
||||
}
|
||||
|
||||
override fun generateSerializableClassProperty(property: PropertyDescriptor) {
|
||||
codegen.generateMethod(property.getter!!) { signature, expressionCodegen ->
|
||||
codegen.generateMethod(property.getter!!) { _, _ ->
|
||||
getstatic(serializerAsmType.internalName, serialDescField, descType.descriptor)
|
||||
areturn(descType)
|
||||
}
|
||||
@@ -224,7 +226,7 @@ class SerializerCodegenImpl(
|
||||
override fun generateLoad(
|
||||
function: FunctionDescriptor
|
||||
) {
|
||||
codegen.generateMethod(function) { signature, expressionCodegen ->
|
||||
codegen.generateMethod(function) { _, expressionCodegen ->
|
||||
// fun load(input: KInput): T
|
||||
val inputVar = 1
|
||||
val descVar = 2
|
||||
@@ -309,13 +311,15 @@ class SerializerCodegenImpl(
|
||||
}
|
||||
|
||||
fun produceCall(update: Boolean) {
|
||||
invokeinterface(kInputType.internalName,
|
||||
invokeinterface(
|
||||
kInputType.internalName,
|
||||
(if (update) CallingConventions.update else CallingConventions.decode) + sti.elementMethodPrefix + (if (useSerializer) "Serializable" else "") + CallingConventions.elementPostfix,
|
||||
"(" + descType.descriptor + "I" +
|
||||
(if (useSerializer) kSerialLoaderType.descriptor else "")
|
||||
+ (if (unknownSer) AsmTypes.K_CLASS_TYPE.descriptor else "")
|
||||
+ (if (update) sti.type.descriptor else "")
|
||||
+ ")" + (if (sti.unit) "V" else sti.type.descriptor))
|
||||
"(" + descType.descriptor + "I" +
|
||||
(if (useSerializer) kSerialLoaderType.descriptor else "")
|
||||
+ (if (unknownSer) AsmTypes.K_CLASS_TYPE.descriptor else "")
|
||||
+ (if (update) sti.type.descriptor else "")
|
||||
+ ")" + (if (sti.unit) "V" else sti.type.descriptor)
|
||||
)
|
||||
}
|
||||
|
||||
if (useSerializer) {
|
||||
|
||||
+2
-2
@@ -77,7 +77,7 @@ class SerializationResolveExtension : SyntheticResolveExtension {
|
||||
result: MutableCollection<SimpleFunctionDescriptor>
|
||||
) {
|
||||
KSerializerDescriptorResolver.generateSerializerMethods(thisDescriptor, fromSupertypes, name, result)
|
||||
KSerializerDescriptorResolver.generateCompanionObjectMethods(thisDescriptor, fromSupertypes, name, result)
|
||||
KSerializerDescriptorResolver.generateCompanionObjectMethods(thisDescriptor, name, result)
|
||||
}
|
||||
|
||||
override fun generateSyntheticProperties(
|
||||
@@ -87,7 +87,7 @@ class SerializationResolveExtension : SyntheticResolveExtension {
|
||||
fromSupertypes: ArrayList<PropertyDescriptor>,
|
||||
result: MutableSet<PropertyDescriptor>
|
||||
) {
|
||||
KSerializerDescriptorResolver.generateDescriptorsForAnnotationImpl(thisDescriptor, name, fromSupertypes, result)
|
||||
KSerializerDescriptorResolver.generateDescriptorsForAnnotationImpl(thisDescriptor, fromSupertypes, result)
|
||||
KSerializerDescriptorResolver.generateSerializerProperties(thisDescriptor, fromSupertypes, name, result)
|
||||
}
|
||||
}
|
||||
+143
-91
@@ -34,8 +34,8 @@ import org.jetbrains.kotlin.types.TypeProjectionImpl
|
||||
import org.jetbrains.kotlin.types.Variance
|
||||
import org.jetbrains.kotlin.types.typeUtil.createProjection
|
||||
import org.jetbrains.kotlin.types.typeUtil.makeNullable
|
||||
import org.jetbrains.kotlinx.serialization.compiler.resolve.SerialEntityNames.SERIALIZER_CLASS_NAME
|
||||
import org.jetbrains.kotlinx.serialization.compiler.resolve.SerialEntityNames.IMPL_NAME
|
||||
import org.jetbrains.kotlinx.serialization.compiler.resolve.SerialEntityNames.SERIALIZER_CLASS_NAME
|
||||
import org.jetbrains.kotlinx.serialization.compiler.resolve.SerialEntityNames.typeArgPrefix
|
||||
import org.jetbrains.kotlinx.serialization.compiler.resolve.SerializationAnnotations.serialInfoFqName
|
||||
import java.util.*
|
||||
@@ -61,7 +61,11 @@ object KSerializerDescriptorResolver {
|
||||
}
|
||||
}
|
||||
|
||||
fun addSerialInfoImplClass(interfaceDesc: ClassDescriptor, declarationProvider: ClassMemberDeclarationProvider, ctx: LazyClassContext): ClassDescriptor {
|
||||
fun addSerialInfoImplClass(
|
||||
interfaceDesc: ClassDescriptor,
|
||||
declarationProvider: ClassMemberDeclarationProvider,
|
||||
ctx: LazyClassContext
|
||||
): ClassDescriptor {
|
||||
val interfaceDecl = declarationProvider.correspondingClassOrObject!!
|
||||
val scope = ctx.declarationScopeProvider.getResolutionScopeForDeclaration(declarationProvider.ownerInfo!!.scopeAnchor)
|
||||
|
||||
@@ -69,17 +73,19 @@ object KSerializerDescriptorResolver {
|
||||
// if there is some properties, there will be a public synthetic constructor at the codegen phase
|
||||
val primaryCtorVisibility = if (props.isEmpty()) Visibilities.PUBLIC else Visibilities.PRIVATE
|
||||
|
||||
val descriptor = SyntheticClassOrObjectDescriptor(ctx,
|
||||
interfaceDecl,
|
||||
interfaceDesc,
|
||||
IMPL_NAME,
|
||||
interfaceDesc.source,
|
||||
scope,
|
||||
Modality.FINAL,
|
||||
Visibilities.PUBLIC,
|
||||
primaryCtorVisibility,
|
||||
ClassKind.CLASS,
|
||||
false)
|
||||
val descriptor = SyntheticClassOrObjectDescriptor(
|
||||
ctx,
|
||||
interfaceDecl,
|
||||
interfaceDesc,
|
||||
IMPL_NAME,
|
||||
interfaceDesc.source,
|
||||
scope,
|
||||
Modality.FINAL,
|
||||
Visibilities.PUBLIC,
|
||||
primaryCtorVisibility,
|
||||
ClassKind.CLASS,
|
||||
false
|
||||
)
|
||||
descriptor.initialize()
|
||||
return descriptor
|
||||
}
|
||||
@@ -111,20 +117,22 @@ object KSerializerDescriptorResolver {
|
||||
return serializerDescriptor
|
||||
}
|
||||
|
||||
fun generateSerializerProperties(thisDescriptor: ClassDescriptor,
|
||||
fromSupertypes: ArrayList<PropertyDescriptor>,
|
||||
name: Name,
|
||||
result: MutableSet<PropertyDescriptor>) {
|
||||
fun generateSerializerProperties(
|
||||
thisDescriptor: ClassDescriptor,
|
||||
fromSupertypes: ArrayList<PropertyDescriptor>,
|
||||
name: Name,
|
||||
result: MutableSet<PropertyDescriptor>
|
||||
) {
|
||||
val classDescriptor = getSerializableClassDescriptorBySerializer(thisDescriptor) ?: return
|
||||
if (name == SerialEntityNames.SERIAL_DESC_FIELD_NAME && result.none(thisDescriptor::checkSerializableClassPropertyResult) &&
|
||||
fromSupertypes.none { thisDescriptor.checkSerializableClassPropertyResult(it) && it.modality == Modality.FINAL })
|
||||
fromSupertypes.none { thisDescriptor.checkSerializableClassPropertyResult(it) && it.modality == Modality.FINAL }
|
||||
)
|
||||
result.add(createSerializableClassPropertyDescriptor(thisDescriptor, classDescriptor))
|
||||
|
||||
}
|
||||
|
||||
fun generateCompanionObjectMethods(
|
||||
thisDescriptor: ClassDescriptor,
|
||||
fromSupertypes: List<SimpleFunctionDescriptor>,
|
||||
name: Name,
|
||||
result: MutableCollection<SimpleFunctionDescriptor>
|
||||
) {
|
||||
@@ -135,61 +143,70 @@ object KSerializerDescriptorResolver {
|
||||
}
|
||||
}
|
||||
|
||||
fun generateSerializerMethods(thisDescriptor: ClassDescriptor,
|
||||
fromSupertypes: List<SimpleFunctionDescriptor>,
|
||||
name: Name,
|
||||
result: MutableCollection<SimpleFunctionDescriptor>) {
|
||||
fun generateSerializerMethods(
|
||||
thisDescriptor: ClassDescriptor,
|
||||
fromSupertypes: List<SimpleFunctionDescriptor>,
|
||||
name: Name,
|
||||
result: MutableCollection<SimpleFunctionDescriptor>
|
||||
) {
|
||||
val classDescriptor = getSerializableClassDescriptorBySerializer(thisDescriptor) ?: return
|
||||
|
||||
fun shouldAddSerializerFunction(checkParameters: (FunctionDescriptor) -> Boolean): Boolean {
|
||||
// Add 'save' / 'load' iff there is no such declared member AND there is no such final member in supertypes
|
||||
return result.none(checkParameters) &&
|
||||
fromSupertypes.none { checkParameters(it) && it.modality == Modality.FINAL }
|
||||
fromSupertypes.none { checkParameters(it) && it.modality == Modality.FINAL }
|
||||
}
|
||||
|
||||
if (name == SerialEntityNames.SAVE_NAME &&
|
||||
shouldAddSerializerFunction { classDescriptor.checkSaveMethodParameters(it.valueParameters) }) {
|
||||
result.add(createSaveFunctionDescriptor(thisDescriptor, classDescriptor))
|
||||
shouldAddSerializerFunction { classDescriptor.checkSaveMethodParameters(it.valueParameters) }
|
||||
) {
|
||||
result.add(createSaveFunctionDescriptor(thisDescriptor))
|
||||
}
|
||||
|
||||
if (name == SerialEntityNames.LOAD_NAME &&
|
||||
shouldAddSerializerFunction { classDescriptor.checkLoadMethodParameters(it.valueParameters) }) {
|
||||
result.add(createLoadFunctionDescriptor(thisDescriptor, classDescriptor))
|
||||
shouldAddSerializerFunction { classDescriptor.checkLoadMethodParameters(it.valueParameters) }
|
||||
) {
|
||||
result.add(createLoadFunctionDescriptor(thisDescriptor))
|
||||
}
|
||||
}
|
||||
|
||||
fun createSerializableClassPropertyDescriptor(companionDescriptor: ClassDescriptor, classDescriptor: ClassDescriptor): PropertyDescriptor =
|
||||
doCreateSerializerProperty(companionDescriptor, classDescriptor, SerialEntityNames.SERIAL_DESC_FIELD_NAME)
|
||||
fun createSerializableClassPropertyDescriptor(
|
||||
companionDescriptor: ClassDescriptor,
|
||||
classDescriptor: ClassDescriptor
|
||||
): PropertyDescriptor =
|
||||
doCreateSerializerProperty(companionDescriptor, classDescriptor, SerialEntityNames.SERIAL_DESC_FIELD_NAME)
|
||||
|
||||
fun createSaveFunctionDescriptor(companionDescriptor: ClassDescriptor, classDescriptor: ClassDescriptor): SimpleFunctionDescriptor =
|
||||
doCreateSerializerFunction(companionDescriptor, classDescriptor, SerialEntityNames.SAVE_NAME)
|
||||
fun createSaveFunctionDescriptor(companionDescriptor: ClassDescriptor): SimpleFunctionDescriptor =
|
||||
doCreateSerializerFunction(companionDescriptor, SerialEntityNames.SAVE_NAME)
|
||||
|
||||
fun createLoadFunctionDescriptor(companionDescriptor: ClassDescriptor, classDescriptor: ClassDescriptor): SimpleFunctionDescriptor =
|
||||
doCreateSerializerFunction(companionDescriptor, classDescriptor, SerialEntityNames.LOAD_NAME)
|
||||
fun createLoadFunctionDescriptor(companionDescriptor: ClassDescriptor): SimpleFunctionDescriptor =
|
||||
doCreateSerializerFunction(companionDescriptor, SerialEntityNames.LOAD_NAME)
|
||||
|
||||
private fun doCreateSerializerProperty(
|
||||
companionDescriptor: ClassDescriptor,
|
||||
classDescriptor: ClassDescriptor,
|
||||
name: Name
|
||||
companionDescriptor: ClassDescriptor,
|
||||
classDescriptor: ClassDescriptor,
|
||||
name: Name
|
||||
): PropertyDescriptor {
|
||||
val typeParam = listOf(createProjection(classDescriptor.defaultType, Variance.INVARIANT, null))
|
||||
val propertyFromSerializer = companionDescriptor.getKSerializerDescriptor().getMemberScope(typeParam)
|
||||
.getContributedVariables(name, NoLookupLocation.FROM_BUILTINS).single()
|
||||
.getContributedVariables(name, NoLookupLocation.FROM_BUILTINS).single()
|
||||
|
||||
val propertyDescriptor = PropertyDescriptorImpl.create(
|
||||
companionDescriptor, Annotations.EMPTY, Modality.OPEN, Visibilities.PUBLIC, false, name,
|
||||
CallableMemberDescriptor.Kind.SYNTHESIZED, companionDescriptor.source, false, false, false, false, false, false
|
||||
companionDescriptor, Annotations.EMPTY, Modality.OPEN, Visibilities.PUBLIC, false, name,
|
||||
CallableMemberDescriptor.Kind.SYNTHESIZED, companionDescriptor.source, false, false, false, false, false, false
|
||||
)
|
||||
|
||||
val extensionReceiverParameter: ReceiverParameterDescriptor? = null // kludge to disambiguate call
|
||||
propertyDescriptor.setType(propertyFromSerializer.type,
|
||||
propertyFromSerializer.typeParameters,
|
||||
companionDescriptor.thisAsReceiverParameter,
|
||||
extensionReceiverParameter)
|
||||
propertyDescriptor.setType(
|
||||
propertyFromSerializer.type,
|
||||
propertyFromSerializer.typeParameters,
|
||||
companionDescriptor.thisAsReceiverParameter,
|
||||
extensionReceiverParameter
|
||||
)
|
||||
|
||||
val propertyGetter = PropertyGetterDescriptorImpl(
|
||||
propertyDescriptor, Annotations.EMPTY, Modality.OPEN, Visibilities.PUBLIC, false, false, false,
|
||||
CallableMemberDescriptor.Kind.SYNTHESIZED, null, companionDescriptor.source
|
||||
propertyDescriptor, Annotations.EMPTY, Modality.OPEN, Visibilities.PUBLIC, false, false, false,
|
||||
CallableMemberDescriptor.Kind.SYNTHESIZED, null, companionDescriptor.source
|
||||
)
|
||||
|
||||
propertyGetter.initialize(propertyFromSerializer.type)
|
||||
@@ -200,45 +217,44 @@ object KSerializerDescriptorResolver {
|
||||
}
|
||||
|
||||
private fun doCreateSerializerFunction(
|
||||
companionDescriptor: ClassDescriptor,
|
||||
classDescriptor: ClassDescriptor,
|
||||
name: Name
|
||||
companionDescriptor: ClassDescriptor,
|
||||
name: Name
|
||||
): SimpleFunctionDescriptor {
|
||||
val functionDescriptor = SimpleFunctionDescriptorImpl.create(
|
||||
companionDescriptor, Annotations.EMPTY, name, CallableMemberDescriptor.Kind.SYNTHESIZED, companionDescriptor.source
|
||||
companionDescriptor, Annotations.EMPTY, name, CallableMemberDescriptor.Kind.SYNTHESIZED, companionDescriptor.source
|
||||
)
|
||||
|
||||
val serializableClassOnImplSite = extractKSerializerArgumentFromImplementation(companionDescriptor)
|
||||
?: throw AssertionError("Serializer does not implement ${SerialEntityNames.KSERIALIZER_CLASS}??")
|
||||
?: throw AssertionError("Serializer does not implement ${SerialEntityNames.KSERIALIZER_CLASS}??")
|
||||
|
||||
val typeParam = listOf(createProjection(serializableClassOnImplSite, Variance.INVARIANT, null))
|
||||
val functionFromSerializer = companionDescriptor.getKSerializerDescriptor().getMemberScope(typeParam)
|
||||
.getContributedFunctions(name, NoLookupLocation.FROM_BUILTINS).single()
|
||||
.getContributedFunctions(name, NoLookupLocation.FROM_BUILTINS).single()
|
||||
|
||||
functionDescriptor.initialize(
|
||||
null,
|
||||
companionDescriptor.thisAsReceiverParameter,
|
||||
functionFromSerializer.typeParameters,
|
||||
functionFromSerializer.valueParameters.map { it.copy(functionDescriptor, it.name, it.index) },
|
||||
functionFromSerializer.returnType,
|
||||
Modality.OPEN,
|
||||
Visibilities.PUBLIC
|
||||
null,
|
||||
companionDescriptor.thisAsReceiverParameter,
|
||||
functionFromSerializer.typeParameters,
|
||||
functionFromSerializer.valueParameters.map { it.copy(functionDescriptor, it.name, it.index) },
|
||||
functionFromSerializer.returnType,
|
||||
Modality.OPEN,
|
||||
Visibilities.PUBLIC
|
||||
)
|
||||
|
||||
return functionDescriptor
|
||||
}
|
||||
|
||||
fun createLoadConstructorDescriptor(
|
||||
classDescriptor: ClassDescriptor,
|
||||
bindingContext: BindingContext
|
||||
classDescriptor: ClassDescriptor,
|
||||
bindingContext: BindingContext
|
||||
): ClassConstructorDescriptor {
|
||||
if (!classDescriptor.isInternalSerializable) throw IllegalArgumentException()
|
||||
|
||||
val functionDescriptor = ClassConstructorDescriptorImpl.createSynthesized(
|
||||
classDescriptor,
|
||||
Annotations.EMPTY,
|
||||
false,
|
||||
classDescriptor.source
|
||||
classDescriptor,
|
||||
Annotations.EMPTY,
|
||||
false,
|
||||
classDescriptor.source
|
||||
)
|
||||
|
||||
val markerDesc = classDescriptor.getKSerializerConstructorMarker()
|
||||
@@ -247,18 +263,30 @@ object KSerializerDescriptorResolver {
|
||||
val parameterDescsAsProps = SerializableProperties(classDescriptor, bindingContext).serializableProperties.map { it.descriptor }
|
||||
var i = 0
|
||||
val consParams = mutableListOf<ValueParameterDescriptor>()
|
||||
consParams.add(ValueParameterDescriptorImpl(functionDescriptor, null, i++, Annotations.EMPTY, Name.identifier("seen"), functionDescriptor.builtIns.intType, false,
|
||||
false, false, null, functionDescriptor.source))
|
||||
consParams.add(
|
||||
ValueParameterDescriptorImpl(
|
||||
functionDescriptor, null, i++, Annotations.EMPTY, Name.identifier("seen"), functionDescriptor.builtIns.intType, false,
|
||||
false, false, null, functionDescriptor.source
|
||||
)
|
||||
)
|
||||
for (prop in parameterDescsAsProps) {
|
||||
consParams.add(ValueParameterDescriptorImpl(functionDescriptor, null, i++, prop.annotations, prop.name, prop.type.makeNullableIfNotPrimitive(), false, false,
|
||||
false, null, functionDescriptor.source))
|
||||
consParams.add(
|
||||
ValueParameterDescriptorImpl(
|
||||
functionDescriptor, null, i++, prop.annotations, prop.name, prop.type.makeNullableIfNotPrimitive(), false, false,
|
||||
false, null, functionDescriptor.source
|
||||
)
|
||||
)
|
||||
}
|
||||
consParams.add(ValueParameterDescriptorImpl(functionDescriptor, null, i++, Annotations.EMPTY, SerialEntityNames.dummyParamName, markerType, false,
|
||||
false, false, null, functionDescriptor.source))
|
||||
consParams.add(
|
||||
ValueParameterDescriptorImpl(
|
||||
functionDescriptor, null, i, Annotations.EMPTY, SerialEntityNames.dummyParamName, markerType, false,
|
||||
false, false, null, functionDescriptor.source
|
||||
)
|
||||
)
|
||||
|
||||
functionDescriptor.initialize(
|
||||
consParams,
|
||||
Visibilities.PUBLIC
|
||||
consParams,
|
||||
Visibilities.PUBLIC
|
||||
)
|
||||
|
||||
functionDescriptor.returnType = classDescriptor.defaultType
|
||||
@@ -312,7 +340,7 @@ object KSerializerDescriptorResolver {
|
||||
val typeArgs = mutableListOf<TypeParameterDescriptor>()
|
||||
var i = 0
|
||||
|
||||
serializableClass.declaredTypeParameters.forEach { it ->
|
||||
serializableClass.declaredTypeParameters.forEach { _ ->
|
||||
val targ = TypeParameterDescriptorImpl.createWithDefaultBound(
|
||||
f, Annotations.EMPTY, false, Variance.INVARIANT,
|
||||
Name.identifier("T$i"), i
|
||||
@@ -356,12 +384,19 @@ object KSerializerDescriptorResolver {
|
||||
else this.makeNullable()
|
||||
|
||||
fun createWriteSelfFunctionDescriptor(thisClass: ClassDescriptor): FunctionDescriptor {
|
||||
val f = SimpleFunctionDescriptorImpl.create(thisClass, Annotations.EMPTY, SerialEntityNames.WRITE_SELF_NAME, CallableMemberDescriptor.Kind.SYNTHESIZED, thisClass.source)
|
||||
val f = SimpleFunctionDescriptorImpl.create(
|
||||
thisClass,
|
||||
Annotations.EMPTY,
|
||||
SerialEntityNames.WRITE_SELF_NAME,
|
||||
CallableMemberDescriptor.Kind.SYNTHESIZED,
|
||||
thisClass.source
|
||||
)
|
||||
val returnType = f.builtIns.unitType
|
||||
|
||||
val args = mutableListOf<ValueParameterDescriptor>()
|
||||
var i = 0
|
||||
args.add(ValueParameterDescriptorImpl(
|
||||
args.add(
|
||||
ValueParameterDescriptorImpl(
|
||||
f,
|
||||
null,
|
||||
i++,
|
||||
@@ -372,10 +407,12 @@ object KSerializerDescriptorResolver {
|
||||
false,
|
||||
false,
|
||||
null,
|
||||
f.source)
|
||||
f.source
|
||||
)
|
||||
)
|
||||
|
||||
args.add(ValueParameterDescriptorImpl(
|
||||
args.add(
|
||||
ValueParameterDescriptorImpl(
|
||||
f,
|
||||
null,
|
||||
i++,
|
||||
@@ -386,7 +423,8 @@ object KSerializerDescriptorResolver {
|
||||
false,
|
||||
false,
|
||||
null,
|
||||
f.source)
|
||||
f.source
|
||||
)
|
||||
)
|
||||
|
||||
val kSerialClassDesc = thisClass.getClassFromSerializationPackage(SerialEntityNames.KSERIALIZER_CLASS)
|
||||
@@ -396,37 +434,51 @@ object KSerializerDescriptorResolver {
|
||||
val kSerialClass = KotlinTypeFactory.simpleNotNullType(Annotations.EMPTY, kSerialClassDesc, listOf(typeArgument))
|
||||
|
||||
|
||||
args.add(ValueParameterDescriptorImpl(
|
||||
args.add(
|
||||
ValueParameterDescriptorImpl(
|
||||
f,
|
||||
null,
|
||||
i++,
|
||||
Annotations.EMPTY,
|
||||
Name.identifier("$typeArgPrefix${i-3}"),
|
||||
Name.identifier("$typeArgPrefix${i - 3}"),
|
||||
kSerialClass,
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
null,
|
||||
f.source
|
||||
))
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
f.initialize(
|
||||
null,
|
||||
thisClass.thisAsReceiverParameter,
|
||||
emptyList(),
|
||||
args,
|
||||
returnType,
|
||||
Modality.OPEN,
|
||||
Visibilities.PUBLIC
|
||||
null,
|
||||
thisClass.thisAsReceiverParameter,
|
||||
emptyList(),
|
||||
args,
|
||||
returnType,
|
||||
Modality.OPEN,
|
||||
Visibilities.PUBLIC
|
||||
)
|
||||
|
||||
return f
|
||||
}
|
||||
|
||||
fun generateDescriptorsForAnnotationImpl(thisDescriptor: ClassDescriptor, name: Name, fromSupertypes: List<PropertyDescriptor>, result: MutableCollection<PropertyDescriptor>) {
|
||||
fun generateDescriptorsForAnnotationImpl(
|
||||
thisDescriptor: ClassDescriptor,
|
||||
fromSupertypes: List<PropertyDescriptor>,
|
||||
result: MutableCollection<PropertyDescriptor>
|
||||
) {
|
||||
if (isSerialInfoImpl(thisDescriptor)) {
|
||||
result.add(fromSupertypes[0].copy(thisDescriptor, Modality.FINAL, Visibilities.PUBLIC, CallableMemberDescriptor.Kind.SYNTHESIZED, true) as PropertyDescriptor)
|
||||
result.add(
|
||||
fromSupertypes[0].copy(
|
||||
thisDescriptor,
|
||||
Modality.FINAL,
|
||||
Visibilities.PUBLIC,
|
||||
CallableMemberDescriptor.Kind.SYNTHESIZED,
|
||||
true
|
||||
) as PropertyDescriptor
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user