All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.mamoe.kjbb.compiler.extensions.BridgeIrGenerationExtension.kt Maven / Gradle / Ivy

Go to download

Kotlin compiler plugin for generating blocking bridges for calling suspend functions from Java with minimal effort

The newest version!
package net.mamoe.kjbb.compiler.extensions

import net.mamoe.kjbb.compiler.backend.ir.JvmBlockingBridgeClassLoweringPass
import net.mamoe.kjbb.compiler.backend.ir.JvmBlockingBridgeFileLoweringPass
import org.jetbrains.kotlin.backend.common.ClassLoweringPass
import org.jetbrains.kotlin.backend.common.FileLoweringPass
import org.jetbrains.kotlin.backend.common.extensions.IrGenerationExtension
import org.jetbrains.kotlin.backend.common.extensions.IrPluginContext
import org.jetbrains.kotlin.ir.IrElement
import org.jetbrains.kotlin.ir.declarations.IrClass
import org.jetbrains.kotlin.ir.declarations.IrFile
import org.jetbrains.kotlin.ir.declarations.IrModuleFragment
import org.jetbrains.kotlin.ir.visitors.IrElementVisitorVoid
import org.jetbrains.kotlin.ir.visitors.acceptChildrenVoid
import org.jetbrains.kotlin.ir.visitors.acceptVoid

/**
 * For IR backend.
 */
// @AutoService(IrGenerationExtension::class)
open class JvmBlockingBridgeIrGenerationExtension(
    private val ext: IBridgeConfiguration,
) : IrGenerationExtension {
    override fun generate(moduleFragment: IrModuleFragment, pluginContext: IrPluginContext) {
        for (file in moduleFragment.files) {
            JvmBlockingBridgeClassLoweringPass(pluginContext, ext).runOnFileInOrder(file)
            JvmBlockingBridgeFileLoweringPass(pluginContext, ext).runOnFileInOrder(file)
        }
    }
}

internal fun ClassLoweringPass.runOnFileInOrder(irFile: IrFile) {
    irFile.acceptVoid(object : IrElementVisitorVoid {
        override fun visitElement(element: IrElement) {
            element.acceptChildrenVoid(this)
        }

        override fun visitClass(declaration: IrClass) {
            lower(declaration) // lower bridge before lowering suspend
        }
    })
}

internal fun FileLoweringPass.runOnFileInOrder(irFile: IrFile) {
    irFile.acceptVoid(object : IrElementVisitorVoid {
        override fun visitElement(element: IrElement) {
            element.acceptChildrenVoid(this)
        }

        override fun visitFile(declaration: IrFile) {
            lower(declaration)
        }
    })
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy