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