Johannes Barop
11/21/2022, 12:32 PMshikasd
11/21/2022, 1:33 PMirCall
and put dispatchReceiver
property to point to instance. (similar for extension functions, just use extensionReceiver
there)shikasd
11/21/2022, 1:35 PMreferenceFunctions
works this wayraulraja
11/21/2022, 3:02 PMIrGetObjectValueImpl
If it's a class and you need to contruct an instance first IrConstructorCallImpl
Johannes Barop
11/21/2022, 4:04 PMoverride fun lower(irClass: IrClass) {
val propertyToClose: IrProperty = /* ... */
val propertyClassSymbol: IrClassSymbol = pluginContext.findClassOf(propertyToClose)
val propertyClass: IrClass = propertyClassSymbol.owner
val propertyCloseFunction: IrSimpleFunction = propertyClass.functions.single { it.name == closeName }
closeFunction.body = DeclarationIrBuilder(pluginContext, closeFunction.symbol).irBlockBody {
+irCall(propertyCloseFunction).apply {
dispatchReceiver = irGetField(irClass.thisReceiver /* Type mismatch: IrValueParameter vs IrExpression? */, propertyToClose.backingField!!)
}
}
How do I get the instance of the property?natario1
11/21/2022, 5:45 PMirGet
around irClass.thisReceiverJohannes Barop
11/21/2022, 7:33 PMNo mapping for symbol: VALUE_PARAMETER INSTANCE_RECEIVER
.
This is the `dump()`of the generated IR:
CALL 'public open fun close (): kotlin.Unit declared in <root>.ExternalResource' type=kotlin.Unit origin=null
$this: GET_FIELD 'FIELD PROPERTY_BACKING_FIELD name:firstResource type:<root>.ExternalResource visibility:private [final]' type=<root>.ExternalResource origin=null
receiver: GET_VAR '<this>: <root>.ResourceManager declared in <root>.ResourceManager' type=<root>.ResourceManager origin=null
This is what i ant to generate:
CALL 'public open fun close (): kotlin.Unit declared in <root>.ExternalResource' type=kotlin.Unit origin=null
$this: CALL 'private final fun <get-firstResource> (): <root>.ExternalResource declared in <root>.ResourceManager' type=<root>.ExternalResource origin=GET_PROPERTY
$this: GET_VAR '<this>: <root>.ResourceManager declared in <root>.ResourceManager.close' type=<root>.ResourceManager origin=null
natario1
11/21/2022, 7:46 PMcloseFunction.dispatchReceiverParameter
IIRC. Assuming closeFunction is a member of the managerJohannes Barop
11/21/2022, 9:21 PMirGet(irClass.thisReceiver!!)
it works with irGet(thisCloseFunction.dispatchReceiverParameter!!)
Full snippet:
override fun lower(irClass: IrClass) {
/* ... */
val propertyToClose: IrProperty = /* ... */
val propertyClassSymbol: IrClassSymbol = pluginContext.findClassOf(propertyToClose)
val propertyClass: IrClass = propertyClassSymbol.owner
val propertyCloseFunction: IrSimpleFunction =
propertyClass.functions.single { it.name == closeName }
thisCloseFunction.body = DeclarationIrBuilder(pluginContext, thisCloseFunction.symbol).irBlockBody {
+irCall(propertyCloseFunction).apply {
dispatchReceiver = irCall(propertyToClose.getter!!).apply {
dispatchReceiver = irGet(thisCloseFunction.dispatchReceiverParameter!!)
}
}
}
}
Thanks for the help and nudges in the right direction 🙂Johannes Barop
11/22/2022, 2:21 PM