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