is there a way to share arbitrary information betw...
# compiler
z
is there a way to share arbitrary information between FIR and IR? For example - say I want to compute some information in FIR that isn't purely representable in generated code but I want to avoid recomputing in IR later. Serializing it in between is fine too!
t
i've seen plugins do things like put custom annotations on things in the FIR layer and then use them in the IR layer. that might work for what you are doing
z
yeah though that requires being able to serialize the data into an annotation, was hoping for something more flexible
b
While there isn't an "official" way to share information, it is possible to access some FIR elements from IR using the
metadata
property:
Copy code
val IrClass.firClass: FirClass? get() = (metadata as? FirMetadataSource.Class)?.fir
Combine that with the ability to add custom data to FIR declarations, you should be able to store information in FIR and access it from IR:
Copy code
class CustomData(...)
object CustomDataAttribute : FirDeclarationDataKey()

var FirClass.customData: CustomData? by FirDeclarationDataRegistry.data(CustomDataAttribute)
val IrClass.customData: CustomData? get() = (metadata as? FirMetadataSource.Class)?.fir?.customData
🙌 1
h
Are there any plans to add an official api for this use-case?
b
There are no plans to remove FIR-level declaration attributes nor the IR-level metadata property, so I suspect this will become the officially recommended way, but there hasn't been any discussion on the topic. Will this pattern not work for your particular use-case or are you just curious?
h
I did test it yet, it is planned for this week, but I was just curious.