Refactor calls to beginStructure
typeParams argument has been removed
This commit is contained in:
+13
-16
@@ -13,7 +13,10 @@ import org.jetbrains.kotlin.descriptors.*
|
||||
import org.jetbrains.kotlin.ir.builders.*
|
||||
import org.jetbrains.kotlin.ir.declarations.*
|
||||
import org.jetbrains.kotlin.ir.expressions.*
|
||||
import org.jetbrains.kotlin.ir.expressions.impl.*
|
||||
import org.jetbrains.kotlin.ir.expressions.impl.IrBranchImpl
|
||||
import org.jetbrains.kotlin.ir.expressions.impl.IrConstImpl
|
||||
import org.jetbrains.kotlin.ir.expressions.impl.IrDelegatingConstructorCallImpl
|
||||
import org.jetbrains.kotlin.ir.expressions.impl.IrGetValueImpl
|
||||
import org.jetbrains.kotlin.ir.symbols.IrConstructorSymbol
|
||||
import org.jetbrains.kotlin.ir.symbols.IrFunctionSymbol
|
||||
import org.jetbrains.kotlin.ir.types.*
|
||||
@@ -195,8 +198,8 @@ open class SerializerIrGenerator(val irClass: IrClass, final override val compil
|
||||
/* Already implemented in .generateSerialClassDesc ? */
|
||||
}
|
||||
|
||||
protected fun ClassDescriptor.referenceMethod(methodName: String) =
|
||||
getFuncDesc(methodName).single().let { compilerContext.symbolTable.referenceFunction(it) }
|
||||
protected inline fun ClassDescriptor.referenceMethod(methodName: String, predicate: (FunctionDescriptor) -> Boolean = { true }) =
|
||||
getFuncDesc(methodName).single(predicate).let { compilerContext.symbolTable.referenceFunction(it) }
|
||||
|
||||
override fun generateSave(function: FunctionDescriptor) = irClass.contributeFunction(function) { saveFunc ->
|
||||
|
||||
@@ -209,22 +212,17 @@ open class SerializerIrGenerator(val irClass: IrClass, final override val compil
|
||||
IrGetValueImpl(startOffset, endOffset, saveFunc.dispatchReceiverParameter!!.symbol)
|
||||
|
||||
val kOutputClass = serializerDescriptor.getClassFromSerializationPackage(STRUCTURE_ENCODER_CLASS)
|
||||
val kOutputSmallClass = serializerDescriptor.getClassFromSerializationPackage(ENCODER_CLASS)
|
||||
val encoderClass = serializerDescriptor.getClassFromSerializationPackage(ENCODER_CLASS)
|
||||
|
||||
val descriptorGetterSymbol = compilerContext.symbolTable.referenceFunction(anySerialDescProperty?.getter!!) //???
|
||||
|
||||
val localSerialDesc = irTemporary(irGet(descriptorGetterSymbol.owner.returnType, irThis(), descriptorGetterSymbol), "desc")
|
||||
|
||||
// fun beginStructure(desc: SerialDescriptor, vararg typeParams: KSerializer<*>): StructureEncoder
|
||||
val beginFunc = kOutputSmallClass.referenceMethod(CallingConventions.begin) // todo: retrieve from actual encoder instead
|
||||
val beginFunc = encoderClass.referenceMethod(CallingConventions.begin) { it.valueParameters.size == 1 }
|
||||
|
||||
val call = irCall(beginFunc, type = beginFunc.descriptor.returnType!!.toIrType()).mapValueParametersIndexed { i, parameterDescriptor ->
|
||||
if (i == 0) irGet(localSerialDesc) else IrVarargImpl(
|
||||
startOffset,
|
||||
endOffset,
|
||||
parameterDescriptor.type.toIrType(),
|
||||
parameterDescriptor.varargElementType!!.toIrType()
|
||||
)
|
||||
val call = irCall(beginFunc, type = beginFunc.descriptor.returnType!!.toIrType()).mapValueParametersIndexed { _, _ ->
|
||||
irGet(localSerialDesc)
|
||||
}
|
||||
// can it be done in more concise way? e.g. additional builder function?
|
||||
call.dispatchReceiver = irGet(saveFunc.valueParameters[0])
|
||||
@@ -327,7 +325,7 @@ open class SerializerIrGenerator(val irClass: IrClass, final override val compil
|
||||
fun IrVariable.get() = irGet(this)
|
||||
|
||||
val inputClass = serializerDescriptor.getClassFromSerializationPackage(STRUCTURE_DECODER_CLASS)
|
||||
val inputSmallClass = serializerDescriptor.getClassFromSerializationPackage(DECODER_CLASS)
|
||||
val decoderClass = serializerDescriptor.getClassFromSerializationPackage(DECODER_CLASS)
|
||||
val descriptorGetterSymbol = compilerContext.symbolTable.referenceFunction(anySerialDescProperty?.getter!!) //???
|
||||
val localSerialDesc = irTemporary(irGet(descriptorGetterSymbol.owner.returnType, irThis(), descriptorGetterSymbol), "desc")
|
||||
|
||||
@@ -352,12 +350,11 @@ open class SerializerIrGenerator(val irClass: IrClass, final override val compil
|
||||
}
|
||||
|
||||
//input = input.beginStructure(...)
|
||||
val beginFunc = inputSmallClass.referenceMethod(CallingConventions.begin)
|
||||
val beginFunc = decoderClass.referenceMethod(CallingConventions.begin) { it.valueParameters.size == 1 }
|
||||
val call = irInvoke(
|
||||
irGet(loadFunc.valueParameters[0]),
|
||||
beginFunc,
|
||||
irGet(localSerialDesc),
|
||||
irEmptyVararg(beginFunc.descriptor.valueParameters[1])
|
||||
irGet(localSerialDesc)
|
||||
)
|
||||
val localInput = irTemporary(call, "input")
|
||||
|
||||
|
||||
+4
-11
@@ -158,7 +158,7 @@ open class SerializerJsTranslator(
|
||||
val encoderClass = serializerDescriptor.getClassFromSerializationPackage(SerialEntityNames.ENCODER_CLASS)
|
||||
val kOutputClass = serializerDescriptor.getClassFromSerializationPackage(SerialEntityNames.STRUCTURE_ENCODER_CLASS)
|
||||
val wBeginFunc = ctx.getNameForDescriptor(
|
||||
encoderClass.getFuncDesc(CallingConventions.begin).single { it.valueParameters.size == 2 })
|
||||
encoderClass.getFuncDesc(CallingConventions.begin).single { it.valueParameters.size == 1 })
|
||||
val serialClassDescRef = JsNameRef(context.getNameForDescriptor(anySerialDescProperty!!), JsThisRef())
|
||||
|
||||
val serializableSource = ((serializableDescriptor.findPsi() as? KtPureClassOrObject)
|
||||
@@ -167,13 +167,9 @@ open class SerializerJsTranslator(
|
||||
context.buildInitializersRemapping(serializableSource, serializableDescriptor.getSuperClassNotAny())
|
||||
|
||||
// output.writeBegin(desc, [])
|
||||
val typeParams = serializableDescriptor.declaredTypeParameters.mapIndexed { idx, _ ->
|
||||
JsNameRef(context.scope().declareName("$typeArgPrefix$idx"), JsThisRef())
|
||||
}
|
||||
val call = JsInvocation(
|
||||
JsNameRef(wBeginFunc, JsNameRef(jsFun.parameters[0].name)),
|
||||
serialClassDescRef,
|
||||
JsArrayLiteral(typeParams)
|
||||
serialClassDescRef
|
||||
)
|
||||
val objRef = JsNameRef(jsFun.parameters[1].name)
|
||||
// output = output.writeBegin...
|
||||
@@ -259,14 +255,11 @@ open class SerializerJsTranslator(
|
||||
+JsVars(localProps.map { JsVars.JsVar(it.name) }, true)
|
||||
|
||||
//input = input.readBegin(...)
|
||||
val typeParams = serializableDescriptor.declaredTypeParameters.mapIndexed { idx, _ ->
|
||||
JsNameRef(context.scope().declareName("$typeArgPrefix$idx"), JsThisRef())
|
||||
}
|
||||
val inputVar = JsNameRef(jsFun.scope.declareFreshName("input"))
|
||||
val readBeginF = decoderClass.getFuncDesc(CallingConventions.begin).single()
|
||||
val readBeginF = decoderClass.getFuncDesc(CallingConventions.begin).single { it.valueParameters.size == 1 }
|
||||
val readBeginCall = JsInvocation(
|
||||
JsNameRef(context.getNameForDescriptor(readBeginF), JsNameRef(jsFun.parameters[0].name)),
|
||||
serialClassDescRef, JsArrayLiteral(typeParams)
|
||||
serialClassDescRef
|
||||
)
|
||||
+JsVars(JsVars.JsVar(inputVar.name, readBeginCall))
|
||||
|
||||
|
||||
+2
-4
@@ -231,10 +231,9 @@ open class SerializerCodegenImpl(
|
||||
// output = output.writeBegin(classDesc, new KSerializer[0])
|
||||
load(outputVar, encoderType)
|
||||
load(descVar, descType)
|
||||
genArrayOfTypeParametersSerializers()
|
||||
invokeinterface(
|
||||
encoderType.internalName, CallingConventions.begin,
|
||||
"(" + descType.descriptor + kSerializerArrayType.descriptor +
|
||||
"(" + descType.descriptor +
|
||||
")" + kOutputType.descriptor
|
||||
)
|
||||
store(outputVar, kOutputType)
|
||||
@@ -322,10 +321,9 @@ open class SerializerCodegenImpl(
|
||||
// input = input.readBegin(classDesc, new KSerializer[0])
|
||||
load(inputVar, decoderType)
|
||||
load(descVar, descType)
|
||||
genArrayOfTypeParametersSerializers()
|
||||
invokeinterface(
|
||||
decoderType.internalName, CallingConventions.begin,
|
||||
"(" + descType.descriptor + kSerializerArrayType.descriptor +
|
||||
"(" + descType.descriptor +
|
||||
")" + kInputType.descriptor
|
||||
)
|
||||
store(inputVar, kInputType)
|
||||
|
||||
Reference in New Issue
Block a user