Hey! I'm unsure whether this is the correct channe...
# compiler
r
Hey! I'm unsure whether this is the correct channel for this, but I'll just ask anyway. Is there a way to turn a kotlin source file to IR / FIR? I've seen https://github.com/google/Kotlin-FirViewer but it's archived. XY here: Making a compiler plugin and I'm unsure about the code structure I need to generate
o
If you're writing a compiler plugin, you probably have a derivative of
IrGenerationExtension
and know how to invoke it. In that case, you can use the
dump()
function like so:
Copy code
import org.jetbrains.kotlin.backend.common.IrElementTransformerVoidWithContext
import org.jetbrains.kotlin.backend.common.extensions.IrGenerationExtension
import org.jetbrains.kotlin.backend.common.extensions.IrPluginContext
import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity
import org.jetbrains.kotlin.cli.common.messages.MessageCollector
import org.jetbrains.kotlin.config.CommonConfigurationKeys
import org.jetbrains.kotlin.config.CompilerConfiguration
import org.jetbrains.kotlin.ir.declarations.IrFile
import org.jetbrains.kotlin.ir.declarations.IrModuleFragment
import org.jetbrains.kotlin.ir.util.dump

class CompilerPluginIrGenerationExtension(private val compilerConfiguration: CompilerConfiguration) :
    IrGenerationExtension {
    override fun generate(moduleFragment: IrModuleFragment, pluginContext: IrPluginContext) {
        moduleFragment.transform(
            ModuleTransformer(
                compilerConfiguration.get(CommonConfigurationKeys.MESSAGE_COLLECTOR_KEY, MessageCollector.NONE)
            ),
            null
        )
    }
}

private class ModuleTransformer(private val messageCollector: MessageCollector) :
    IrElementTransformerVoidWithContext() {
    override fun visitFileNew(declaration: IrFile): IrFile {
        val result = super.visitFileNew(declaration)
        messageCollector.report(CompilerMessageSeverity.WARNING, declaration.dump())
        return result
    }
}
🙏 2
r
Thank you!
👍 1
j
r
This looks incredibly useful as well! Thanks you two 🙂
i
There is also
-Xprint-ir
compiler argument, but only for Kotlin/Native. It will print how IR is looking on all different compilation stages.
thank you color 1
o
That sounds interesting, as the FIR plugin requires IDE/K2 mode, which other plugins (Ktlint) do not yet support. Would be ideal if I could put that into some command line invocation callable by the IDE for a single (scratch) source file.
j
> That sounds interesting, as the FIR plugin requires IDE/K2 mode, which other plugins (Ktlint) do not yet support. Would be ideal if I could put that into some command line invocation callable by the IDE for a single (scratch) source file. Even if you don't have K2 enabled on the IDE, FIR reports will be shown in the CLI if you are using Kotlin 2+
o
You mean the plugin's reports? Doesn't load for me if IDE/K2 mode is disabled.
j
But I am not sure if scratch file outputs are using Kotlin 2 or not
The plugin is used on CLI even if it is disabled on the IDE. If you add an error check in FIR, running
./gradlew assemble
will crash with that error on CLI.
Codegen and so on will work too. Even if you see errors on the IDE due your plugin not being used there, it will compile if you run the Gradle task
o
Are we talking about the same "FIR Tree" IDE plugin mentioned above?
j
Ah, sorry I misunderstood you. I thought you were talking about FIR not working on CLI not about the plugin
👍 1