If we adding a new `IrAnonymousInitializer` to `Ir...
# compiler
i
If we adding a new
IrAnonymousInitializer
to
IrClass
, why do we need to call
ReferenceSymbolTable.withReferenceScope
? Can we ignore this step, or what does it do?
Copy code
// IrBuilderWithPluginContext.kt
// from kotlinx-serialization.backend

fun IrClass.addAnonymousInit(body: IrBlockBodyBuilder.() -> Unit) {
    val anonymousInit = this.run {
        val symbol = IrAnonymousInitializerSymbolImpl(symbol)
        this.factory.createAnonymousInitializer(startOffset, endOffset, SERIALIZATION_PLUGIN_ORIGIN, symbol).also {
            it.parent = this
            declarations.add(it)
        }
    }

    anonymousInit.buildWithScope { initIrBody ->
        initIrBody.body =
            DeclarationIrBuilder(
                compilerContext,
                initIrBody.symbol,
                initIrBody.startOffset,
                initIrBody.endOffset
            ).irBlockBody(body = body)
    }
}

fun <T : IrDeclaration> T.buildWithScope(builder: (T) -> Unit): T =
    also { irDeclaration ->
        compilerContext.symbolTable.withReferenceScope(irDeclaration) {
            builder(irDeclaration)
        }
    }