https://kotlinlang.org logo
#compiler
Title
# compiler
j

Johannes Barop

11/27/2023, 9:57 PM
👋 I'm currently porting my K1 compiler plugin to K2. I want to add a new function to a class. I replaced the
SyntheticResolveExtension
with a
FirDeclarationGenerationExtension
to add the signature. The Fir phase runs without exception, however the method is missing during Ir phase.
Copy code
override fun generateFunctions(
    callableId: CallableId,
    context: MemberGenerationContext?
  ): List<FirNamedFunctionSymbol> {
    val newFunction = buildSimpleFunction {
      moduleData = session.moduleData
      name = callableId.callableName
      dispatchReceiverType = context?.owner?.defaultType()
      symbol = FirNamedFunctionSymbol(callableId)
      origin = context!!.owner.origin // TODO
      status = FirResolvedDeclarationStatusImpl(Visibilities.Public, Modality.OPEN, EffectiveVisibility.Public)
      returnTypeRef = buildResolvedTypeRef { type = session.builtinTypes.unitType.type }
    }

    return listOf(newFunction.symbol)
  }
During class lowering I want to add the function body. However the method is missing in
irClass.functions
. Any ideas why?
a

Artem Kobzar

11/30/2023, 8:21 AM
It's possible that it's because you miss the body for the function. You can define it right there with FIR, or add to your plugin an IR extension and add the body on the IR side.
j

Johannes Barop

12/04/2023, 7:21 PM
Hm, it works when I use
createMemberFunction
. Dunno what was missing with the other approach
6 Views