Create separate type arguments for writeSelf function, since it is static now.
Fixes kapt issue https://github.com/Kotlin/kotlinx.serialization/issues/429
This commit is contained in:
+87
-82
@@ -370,6 +370,52 @@ object KSerializerDescriptorResolver {
|
|||||||
return constrDesc
|
return constrDesc
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates free type parameters T0, T1, ... for given serializable class
|
||||||
|
* Returns [T0, T1, ...] and [KSerializer<T0>, KSerializer<T1>,...]
|
||||||
|
*/
|
||||||
|
private fun createKSerializerParamsForEachGenericArgument(
|
||||||
|
parentFunction: FunctionDescriptor,
|
||||||
|
serializableClass: ClassDescriptor,
|
||||||
|
actualArgsOffset: Int = 0
|
||||||
|
): Pair<List<TypeParameterDescriptor>, List<ValueParameterDescriptor>> {
|
||||||
|
val serializerClass = serializableClass.getClassFromSerializationPackage(SerialEntityNames.KSERIALIZER_CLASS)
|
||||||
|
val args = mutableListOf<ValueParameterDescriptor>()
|
||||||
|
val typeArgs = mutableListOf<TypeParameterDescriptor>()
|
||||||
|
var i = 0
|
||||||
|
|
||||||
|
serializableClass.declaredTypeParameters.forEach { _ ->
|
||||||
|
val targ = TypeParameterDescriptorImpl.createWithDefaultBound(
|
||||||
|
parentFunction, Annotations.EMPTY, false, Variance.INVARIANT,
|
||||||
|
Name.identifier("T$i"), i
|
||||||
|
)
|
||||||
|
|
||||||
|
val pType =
|
||||||
|
KotlinTypeFactory.simpleNotNullType(Annotations.EMPTY, serializerClass, listOf(TypeProjectionImpl(targ.defaultType)))
|
||||||
|
|
||||||
|
args.add(
|
||||||
|
ValueParameterDescriptorImpl(
|
||||||
|
containingDeclaration = parentFunction,
|
||||||
|
original = null,
|
||||||
|
index = actualArgsOffset + i,
|
||||||
|
annotations = Annotations.EMPTY,
|
||||||
|
name = Name.identifier("$typeArgPrefix$i"),
|
||||||
|
outType = pType,
|
||||||
|
declaresDefaultValue = false,
|
||||||
|
isCrossinline = false,
|
||||||
|
isNoinline = false,
|
||||||
|
varargElementType = null,
|
||||||
|
source = parentFunction.source
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
typeArgs.add(targ)
|
||||||
|
i++
|
||||||
|
}
|
||||||
|
|
||||||
|
return typeArgs to args
|
||||||
|
}
|
||||||
|
|
||||||
private fun createSerializerGetterDescriptor(thisClass: ClassDescriptor, serializableClass: ClassDescriptor): SimpleFunctionDescriptor {
|
private fun createSerializerGetterDescriptor(thisClass: ClassDescriptor, serializableClass: ClassDescriptor): SimpleFunctionDescriptor {
|
||||||
val f = SimpleFunctionDescriptorImpl.create(
|
val f = SimpleFunctionDescriptorImpl.create(
|
||||||
thisClass,
|
thisClass,
|
||||||
@@ -380,38 +426,7 @@ object KSerializerDescriptorResolver {
|
|||||||
)
|
)
|
||||||
val serializerClass = thisClass.getClassFromSerializationPackage(SerialEntityNames.KSERIALIZER_CLASS)
|
val serializerClass = thisClass.getClassFromSerializationPackage(SerialEntityNames.KSERIALIZER_CLASS)
|
||||||
|
|
||||||
val args = mutableListOf<ValueParameterDescriptor>()
|
val (typeArgs, args) = createKSerializerParamsForEachGenericArgument(f, serializableClass)
|
||||||
val typeArgs = mutableListOf<TypeParameterDescriptor>()
|
|
||||||
var i = 0
|
|
||||||
|
|
||||||
serializableClass.declaredTypeParameters.forEach { _ ->
|
|
||||||
val targ = TypeParameterDescriptorImpl.createWithDefaultBound(
|
|
||||||
f, Annotations.EMPTY, false, Variance.INVARIANT,
|
|
||||||
Name.identifier("T$i"), i
|
|
||||||
)
|
|
||||||
|
|
||||||
val pType =
|
|
||||||
KotlinTypeFactory.simpleNotNullType(Annotations.EMPTY, serializerClass, listOf(TypeProjectionImpl(targ.defaultType)))
|
|
||||||
|
|
||||||
args.add(
|
|
||||||
ValueParameterDescriptorImpl(
|
|
||||||
f,
|
|
||||||
null,
|
|
||||||
i,
|
|
||||||
Annotations.EMPTY,
|
|
||||||
Name.identifier("$typeArgPrefix$i"),
|
|
||||||
pType,
|
|
||||||
false,
|
|
||||||
false,
|
|
||||||
false,
|
|
||||||
null,
|
|
||||||
f.source
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
typeArgs.add(targ)
|
|
||||||
i++
|
|
||||||
}
|
|
||||||
|
|
||||||
val newSerializableType =
|
val newSerializableType =
|
||||||
KotlinTypeFactory.simpleNotNullType(Annotations.EMPTY, serializableClass, typeArgs.map { TypeProjectionImpl(it.defaultType) })
|
KotlinTypeFactory.simpleNotNullType(Annotations.EMPTY, serializableClass, typeArgs.map { TypeProjectionImpl(it.defaultType) })
|
||||||
@@ -446,79 +461,69 @@ object KSerializerDescriptorResolver {
|
|||||||
)
|
)
|
||||||
val returnType = f.builtIns.unitType
|
val returnType = f.builtIns.unitType
|
||||||
|
|
||||||
|
val (typeArgs, argsKSer) = createKSerializerParamsForEachGenericArgument(f, thisClass, actualArgsOffset = 3)
|
||||||
|
|
||||||
val args = mutableListOf<ValueParameterDescriptor>()
|
val args = mutableListOf<ValueParameterDescriptor>()
|
||||||
var i = 0
|
|
||||||
// object
|
// object
|
||||||
|
val objectType =
|
||||||
|
KotlinTypeFactory.simpleNotNullType(Annotations.EMPTY, thisClass, typeArgs.map { TypeProjectionImpl(it.defaultType) })
|
||||||
args.add(
|
args.add(
|
||||||
ValueParameterDescriptorImpl(
|
ValueParameterDescriptorImpl(
|
||||||
f, null, i++, Annotations.EMPTY, Name.identifier("self"), thisClass.defaultType, false,
|
containingDeclaration = f,
|
||||||
false, false, null, f.source
|
original = null,
|
||||||
|
index = 0,
|
||||||
|
annotations = Annotations.EMPTY,
|
||||||
|
name = Name.identifier("self"),
|
||||||
|
outType = objectType,
|
||||||
|
declaresDefaultValue = false,
|
||||||
|
isCrossinline = false,
|
||||||
|
isNoinline = false,
|
||||||
|
varargElementType = null,
|
||||||
|
source = f.source
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
// encoder
|
// encoder
|
||||||
args.add(
|
args.add(
|
||||||
ValueParameterDescriptorImpl(
|
ValueParameterDescriptorImpl(
|
||||||
f,
|
containingDeclaration = f,
|
||||||
null,
|
original = null,
|
||||||
i++,
|
index = 1,
|
||||||
Annotations.EMPTY,
|
annotations = Annotations.EMPTY,
|
||||||
Name.identifier("output"),
|
name = Name.identifier("output"),
|
||||||
thisClass.getClassFromSerializationPackage(SerialEntityNames.STRUCTURE_ENCODER_CLASS).toSimpleType(false),
|
outType = thisClass.getClassFromSerializationPackage(SerialEntityNames.STRUCTURE_ENCODER_CLASS).toSimpleType(false),
|
||||||
false,
|
declaresDefaultValue = false,
|
||||||
false,
|
isCrossinline = false,
|
||||||
false,
|
isNoinline = false,
|
||||||
null,
|
varargElementType = null,
|
||||||
f.source
|
source = f.source
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
//descriptor
|
//descriptor
|
||||||
args.add(
|
args.add(
|
||||||
ValueParameterDescriptorImpl(
|
ValueParameterDescriptorImpl(
|
||||||
f,
|
containingDeclaration = f,
|
||||||
null,
|
original = null,
|
||||||
i++,
|
index = 2,
|
||||||
Annotations.EMPTY,
|
annotations = Annotations.EMPTY,
|
||||||
Name.identifier("serialDesc"),
|
name = Name.identifier("serialDesc"),
|
||||||
thisClass.getClassFromSerializationPackage(SerialEntityNames.SERIAL_DESCRIPTOR_CLASS).toSimpleType(false),
|
outType = thisClass.getClassFromSerializationPackage(SerialEntityNames.SERIAL_DESCRIPTOR_CLASS).toSimpleType(false),
|
||||||
false,
|
declaresDefaultValue = false,
|
||||||
false,
|
isCrossinline = false,
|
||||||
false,
|
isNoinline = false,
|
||||||
null,
|
varargElementType = null,
|
||||||
f.source
|
source = f.source
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
val kSerialClassDesc = thisClass.getClassFromSerializationPackage(SerialEntityNames.KSERIALIZER_CLASS)
|
args.addAll(argsKSer)
|
||||||
|
|
||||||
// type parameters serialziers
|
|
||||||
thisClass.declaredTypeParameters.forEach {
|
|
||||||
val typeArgument = TypeProjectionImpl(it.defaultType)
|
|
||||||
val kSerialClass = KotlinTypeFactory.simpleNotNullType(Annotations.EMPTY, kSerialClassDesc, listOf(typeArgument))
|
|
||||||
|
|
||||||
|
|
||||||
args.add(
|
|
||||||
ValueParameterDescriptorImpl(
|
|
||||||
f,
|
|
||||||
null,
|
|
||||||
i++,
|
|
||||||
Annotations.EMPTY,
|
|
||||||
Name.identifier("$typeArgPrefix${i - 3}"),
|
|
||||||
kSerialClass,
|
|
||||||
false,
|
|
||||||
false,
|
|
||||||
false,
|
|
||||||
null,
|
|
||||||
f.source
|
|
||||||
)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
f.initialize(
|
f.initialize(
|
||||||
null,
|
null,
|
||||||
thisClass.thisAsReceiverParameter,
|
thisClass.thisAsReceiverParameter,
|
||||||
emptyList(),
|
typeArgs,
|
||||||
args,
|
args,
|
||||||
returnType,
|
returnType,
|
||||||
Modality.FINAL,
|
Modality.FINAL,
|
||||||
|
|||||||
Reference in New Issue
Block a user