[Serialization] Introduce utility for generation of IR annotation calls

This commit is contained in:
Dmitriy Novozhilov
2023-11-28 17:51:41 +02:00
parent c70c8f927e
commit 51dcd7c076
2 changed files with 11 additions and 16 deletions
@@ -9,6 +9,7 @@ import org.jetbrains.kotlin.backend.common.lower.DeclarationIrBuilder
import org.jetbrains.kotlin.backend.common.lower.createIrBuilder import org.jetbrains.kotlin.backend.common.lower.createIrBuilder
import org.jetbrains.kotlin.builtins.KotlinBuiltIns import org.jetbrains.kotlin.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.descriptors.* import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.ir.IrElement
import org.jetbrains.kotlin.ir.ObsoleteDescriptorBasedAPI import org.jetbrains.kotlin.ir.ObsoleteDescriptorBasedAPI
import org.jetbrains.kotlin.ir.builders.* import org.jetbrains.kotlin.ir.builders.*
import org.jetbrains.kotlin.ir.builders.declarations.* import org.jetbrains.kotlin.ir.builders.declarations.*
@@ -141,6 +142,13 @@ interface IrBuilderWithPluginContext {
return prop return prop
} }
fun IrElement.createAnnotationCallWithoutArgs(annotationSymbol: IrClassSymbol): IrConstructorCall {
val annotationCtor = annotationSymbol.constructors.single { it.owner.isPrimary }
val annotationType = annotationSymbol.defaultType
return IrConstructorCallImpl.fromSymbolOwner(startOffset, endOffset, annotationType, annotationCtor)
}
fun IrClass.addValPropertyWithJvmField( fun IrClass.addValPropertyWithJvmField(
type: IrType, type: IrType,
name: Name, name: Name,
@@ -153,11 +161,7 @@ interface IrBuilderWithPluginContext {
val resultExpression = addAndGetLastExpression(initializerBuilder) val resultExpression = addAndGetLastExpression(initializerBuilder)
+irSetField(irGet(thisReceiver!!), field, resultExpression) +irSetField(irGet(thisReceiver!!), field, resultExpression)
} }
field.annotations += createAnnotationCallWithoutArgs(compilerContext.jvmFieldClassSymbol)
val annotationCtor = compilerContext.jvmFieldClassSymbol.constructors.single { it.owner.isPrimary }
val annotationType = compilerContext.jvmFieldClassSymbol.defaultType
field.annotations += IrConstructorCallImpl.fromSymbolOwner(startOffset, endOffset, annotationType, annotationCtor)
} }
} }
@@ -177,11 +181,7 @@ interface IrBuilderWithPluginContext {
field.endOffset field.endOffset
) )
field.initializer = IrExpressionBodyImpl(builder.initializer()) field.initializer = IrExpressionBodyImpl(builder.initializer())
field.annotations += createAnnotationCallWithoutArgs(compilerContext.jvmFieldClassSymbol)
val annotationCtor = compilerContext.jvmFieldClassSymbol.constructors.single { it.owner.isPrimary }
val annotationType = compilerContext.jvmFieldClassSymbol.defaultType
field.annotations += IrConstructorCallImpl.fromSymbolOwner(startOffset, endOffset, annotationType, annotationCtor)
} }
} }
@@ -87,12 +87,7 @@ class SerializableCompanionIrGenerator(
} }
val annotationClass = compilerContext.referenceClass(SerializationAnnotations.namedCompanionClassId) ?: return val annotationClass = compilerContext.referenceClass(SerializationAnnotations.namedCompanionClassId) ?: return
val annotationCall = irClass.createAnnotationCallWithoutArgs(annotationClass)
val annotationCtor = annotationClass.constructors.single { it.owner.isPrimary }
val annotationType = annotationClass.defaultType
val annotationCall = IrConstructorCallImpl.fromSymbolOwner(irClass.startOffset, irClass.endOffset, annotationType, annotationCtor)
compilerContext.metadataDeclarationRegistrar.addMetadataVisibleAnnotationsToElement(irClass, annotationCall) compilerContext.metadataDeclarationRegistrar.addMetadataVisibleAnnotationsToElement(irClass, annotationCall)
} }