Use IrFactory in kotlin-serialization-compiler

This commit is contained in:
Alexander Udalov
2020-07-28 13:23:25 +02:00
parent 0909894a96
commit 4892737cc9
3 changed files with 13 additions and 15 deletions
@@ -14,7 +14,6 @@ dependencies {
compileOnly(project(":compiler:backend")) compileOnly(project(":compiler:backend"))
compileOnly(project(":compiler:ir.backend.common")) compileOnly(project(":compiler:ir.backend.common"))
compileOnly(project(":compiler:ir.psi2ir")) compileOnly(project(":compiler:ir.psi2ir"))
compileOnly(project(":compiler:ir.tree.impl"))
compileOnly(project(":js:js.frontend")) compileOnly(project(":js:js.frontend"))
compileOnly(project(":js:js.translator")) compileOnly(project(":js:js.translator"))
compileOnly(project(":kotlin-util-klib-metadata")) compileOnly(project(":kotlin-util-klib-metadata"))
@@ -12,7 +12,6 @@ import org.jetbrains.kotlin.descriptors.annotations.Annotations
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.declarations.* import org.jetbrains.kotlin.ir.declarations.*
import org.jetbrains.kotlin.ir.declarations.impl.*
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.*
import org.jetbrains.kotlin.ir.symbols.* import org.jetbrains.kotlin.ir.symbols.*
@@ -236,7 +235,7 @@ interface IrBuilderExtension {
if (declare) { if (declare) {
with(propertyDescriptor) { with(propertyDescriptor) {
IrPropertyImpl( propertyParent.factory.createProperty(
propertyParent.startOffset, propertyParent.endOffset, SERIALIZABLE_PLUGIN_ORIGIN, irPropertySymbol, propertyParent.startOffset, propertyParent.endOffset, SERIALIZABLE_PLUGIN_ORIGIN, irPropertySymbol,
name, visibility, modality, isVar, isConst, isLateInit, isDelegated, isExternal name, visibility, modality, isVar, isConst, isLateInit, isDelegated, isExternal
).also { ).also {
@@ -252,9 +251,9 @@ interface IrBuilderExtension {
correspondingPropertySymbol = irPropertySymbol correspondingPropertySymbol = irPropertySymbol
} }
val fieldSymbol = irProperty.backingField!!.symbol val fieldSymbol = irProperty.backingField!!.symbol
irProperty.getter = propertyDescriptor.getter?.let { generatePropertyAccessor(it, fieldSymbol, declare) } irProperty.getter = propertyDescriptor.getter?.let { generatePropertyAccessor(irProperty, it, fieldSymbol, declare) }
?.apply { parent = propertyParent } ?.apply { parent = propertyParent }
irProperty.setter = propertyDescriptor.setter?.let { generatePropertyAccessor(it, fieldSymbol, declare) } irProperty.setter = propertyDescriptor.setter?.let { generatePropertyAccessor(irProperty, it, fieldSymbol, declare) }
?.apply { parent = propertyParent } ?.apply { parent = propertyParent }
return irProperty return irProperty
} }
@@ -268,7 +267,7 @@ interface IrBuilderExtension {
return with(descriptor) { return with(descriptor) {
// TODO: type parameters // TODO: type parameters
IrFieldImpl( originProperty.factory.createField(
originProperty.startOffset, originProperty.endOffset, SERIALIZABLE_PLUGIN_ORIGIN, fieldSymbol, name, type.toIrType(), originProperty.startOffset, originProperty.endOffset, SERIALIZABLE_PLUGIN_ORIGIN, fieldSymbol, name, type.toIrType(),
visibility, !isVar, isEffectivelyExternal(), dispatchReceiverParameter == null visibility, !isVar, isEffectivelyExternal(), dispatchReceiverParameter == null
) )
@@ -276,6 +275,7 @@ interface IrBuilderExtension {
} }
fun generatePropertyAccessor( fun generatePropertyAccessor(
property: IrProperty,
descriptor: PropertyAccessorDescriptor, descriptor: PropertyAccessorDescriptor,
fieldSymbol: IrFieldSymbol, fieldSymbol: IrFieldSymbol,
declare: Boolean declare: Boolean
@@ -285,7 +285,7 @@ interface IrBuilderExtension {
if (declare) { if (declare) {
with(descriptor) { with(descriptor) {
IrFunctionImpl( property.factory.createFunction(
fieldSymbol.owner.startOffset, fieldSymbol.owner.endOffset, SERIALIZABLE_PLUGIN_ORIGIN, symbol, fieldSymbol.owner.startOffset, fieldSymbol.owner.endOffset, SERIALIZABLE_PLUGIN_ORIGIN, symbol,
name, visibility, modality, returnType!!.toIrType(), name, visibility, modality, returnType!!.toIrType(),
isInline, isExternal, isTailrec, isSuspend, isOperator, isInfix, isExpect isInline, isExternal, isTailrec, isSuspend, isOperator, isInfix, isExpect
@@ -317,7 +317,7 @@ interface IrBuilderExtension {
val startOffset = irAccessor.startOffset val startOffset = irAccessor.startOffset
val endOffset = irAccessor.endOffset val endOffset = irAccessor.endOffset
val irBody = IrBlockBodyImpl(startOffset, endOffset) val irBody = irAccessor.factory.createBlockBody(startOffset, endOffset)
val receiver = generateReceiverExpressionForFieldAccess(irAccessor.dispatchReceiverParameter!!.symbol, property) val receiver = generateReceiverExpressionForFieldAccess(irAccessor.dispatchReceiverParameter!!.symbol, property)
@@ -344,7 +344,7 @@ interface IrBuilderExtension {
val irProperty = irAccessor.correspondingPropertySymbol?.owner ?: error("Expected corresponding property for accessor $setter") val irProperty = irAccessor.correspondingPropertySymbol?.owner ?: error("Expected corresponding property for accessor $setter")
val startOffset = irAccessor.startOffset val startOffset = irAccessor.startOffset
val endOffset = irAccessor.endOffset val endOffset = irAccessor.endOffset
val irBody = IrBlockBodyImpl(startOffset, endOffset) val irBody = irAccessor.factory.createBlockBody(startOffset, endOffset)
val receiver = generateReceiverExpressionForFieldAccess(irAccessor.dispatchReceiverParameter!!.symbol, property) val receiver = generateReceiverExpressionForFieldAccess(irAccessor.dispatchReceiverParameter!!.symbol, property)
@@ -383,7 +383,7 @@ interface IrBuilderExtension {
) { ) {
val function = this val function = this
fun irValueParameter(descriptor: ParameterDescriptor): IrValueParameter = with(descriptor) { fun irValueParameter(descriptor: ParameterDescriptor): IrValueParameter = with(descriptor) {
IrValueParameterImpl( factory.createValueParameter(
function.startOffset, function.endOffset, SERIALIZABLE_PLUGIN_ORIGIN, IrValueParameterSymbolImpl(this), function.startOffset, function.endOffset, SERIALIZABLE_PLUGIN_ORIGIN, IrValueParameterSymbolImpl(this),
name, indexOrMinusOne, type.toIrType(), varargElementType?.toIrType(), isCrossinline, isNoinline name, indexOrMinusOne, type.toIrType(), varargElementType?.toIrType(), isCrossinline, isNoinline
).also { ).also {
@@ -407,7 +407,7 @@ interface IrBuilderExtension {
fun IrFunction.copyTypeParamsFromDescriptor() { fun IrFunction.copyTypeParamsFromDescriptor() {
val newTypeParameters = descriptor.typeParameters.map { val newTypeParameters = descriptor.typeParameters.map {
IrTypeParameterImpl( factory.createTypeParameter(
startOffset, endOffset, startOffset, endOffset,
SERIALIZABLE_PLUGIN_ORIGIN, SERIALIZABLE_PLUGIN_ORIGIN,
IrTypeParameterSymbolImpl(it), IrTypeParameterSymbolImpl(it),
@@ -13,7 +13,6 @@ import org.jetbrains.kotlin.descriptors.*
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.declarations.* import org.jetbrains.kotlin.ir.declarations.*
import org.jetbrains.kotlin.ir.declarations.impl.IrAnonymousInitializerImpl
import org.jetbrains.kotlin.ir.expressions.IrConstructorCall import org.jetbrains.kotlin.ir.expressions.IrConstructorCall
import org.jetbrains.kotlin.ir.expressions.IrExpression import org.jetbrains.kotlin.ir.expressions.IrExpression
import org.jetbrains.kotlin.ir.expressions.impl.IrBranchImpl import org.jetbrains.kotlin.ir.expressions.impl.IrBranchImpl
@@ -75,15 +74,15 @@ open class SerializerIrGenerator(val irClass: IrClass, final override val compil
} }
} }
val annonimousInit = irClass.run { val anonymousInit = irClass.run {
val symbol = IrAnonymousInitializerSymbolImpl(descriptor) val symbol = IrAnonymousInitializerSymbolImpl(descriptor)
IrAnonymousInitializerImpl(startOffset, endOffset, SERIALIZABLE_PLUGIN_ORIGIN, symbol).also { irClass.factory.createAnonymousInitializer(startOffset, endOffset, SERIALIZABLE_PLUGIN_ORIGIN, symbol).also {
it.parent = this it.parent = this
declarations.add(it) declarations.add(it)
} }
} }
annonimousInit.buildWithScope { initIrBody -> anonymousInit.buildWithScope { initIrBody ->
compilerContext.symbolTable.withReferenceScope(initIrBody.descriptor) { compilerContext.symbolTable.withReferenceScope(initIrBody.descriptor) {
initIrBody.body = initIrBody.body =
DeclarationIrBuilder(compilerContext, initIrBody.symbol, initIrBody.startOffset, initIrBody.endOffset).irBlockBody { DeclarationIrBuilder(compilerContext, initIrBody.symbol, initIrBody.startOffset, initIrBody.endOffset).irBlockBody {