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

Ji Sungbin

03/15/2023, 3:58 AM
Can the Kotlin Compiler Plugin access the IR of functions defined in external modules? More details in the thread.. 🧵
I have written the following code in A and B modules. (both functions are declared at the top level in a file)
Copy code
// A module
public fun Hi() {
    println("hi~")
}

// B module
public fun Bye() {
    println("bye~")
}
Module A has a dependency on module B, and I’ve enabled the Kotlin Compiler Plugin with
MyIrVisitor
.
Copy code
internal class MyIrVisitor : IrElementVisitorVoid {
    override fun visitModuleFragment(declaration: IrModuleFragment) {
        declaration.files.forEach { file ->
            file.accept(this, null)
        }
    }

    override fun visitFile(declaration: IrFile) {
        declaration.declarations.forEach { item ->
            item.accept(this, null)
        }
    }

    override fun visitSimpleFunction(declaration: IrSimpleFunction) {
        logger.warn("[visit] ${declaration.name.asString()}")
        declaration.body?.accept(this, null)
    }

    override fun visitExternalPackageFragment(declaration: IrExternalPackageFragment) {
        declaration.declarations.forEach { item ->
            item.accept(this, null)
        }
    }
}
I expected both
[visit] Hi
and
[visit] Bye
to be printed, but only
[visit] Hi
was actually printed. Am I misunderstanding
visitExternalPackageFragment
?
11 Views