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

org.jetbrains.kotlin.backend.common.phaser.PhaseBuilders.kt Maven / Gradle / Ivy

There is a newer version: 2.1.0-RC
Show newest version
/*
 * Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
 * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
 */

package org.jetbrains.kotlin.backend.common.phaser

import org.jetbrains.kotlin.backend.common.CommonBackendContext
import org.jetbrains.kotlin.backend.common.FileLoweringPass
import org.jetbrains.kotlin.backend.common.LoggingContext
import org.jetbrains.kotlin.backend.common.ModuleLoweringPass
import org.jetbrains.kotlin.ir.declarations.IrFile
import org.jetbrains.kotlin.ir.declarations.IrModuleFragment

// Phase composition.
private class CompositePhase(
    val phases: List>
) : CompilerPhase {

    override fun invoke(phaseConfig: PhaseConfigurationService, phaserState: PhaserState, context: Context, input: Input): Output {
        @Suppress("UNCHECKED_CAST") var currentState = phaserState as PhaserState
        var result = phases.first().invoke(phaseConfig, currentState, context, input)
        for ((previous, next) in phases.zip(phases.drop(1))) {
            if (next !is SameTypeCompilerPhase<*, *>) {
                // Discard `stickyPostconditions`, they are useless since data type is changing.
                currentState = currentState.changePhaserStateType()
            }
            currentState.stickyPostconditions.addAll(previous.stickyPostconditions)
            result = next.invoke(phaseConfig, currentState, context, result)
        }
        @Suppress("UNCHECKED_CAST")
        return result as Output
    }

    override fun getNamedSubphases(startDepth: Int): List>> =
        phases.flatMap { it.getNamedSubphases(startDepth) }

    override val stickyPostconditions get() = phases.last().stickyPostconditions
}

@Suppress("UNCHECKED_CAST")
infix fun  CompilerPhase.then(
    other: CompilerPhase
): CompilerPhase {
    val unsafeThis = this as CompilerPhase
    val unsafeOther = other as CompilerPhase
    return CompositePhase(if (this is CompositePhase) phases + unsafeOther else listOf(unsafeThis, unsafeOther))
}

fun  createSimpleNamedCompilerPhase(
    name: String,
    description: String,
    preactions: Set> = emptySet(),
    postactions: Set> = emptySet(),
    prerequisite: Set> = emptySet(),
    outputIfNotEnabled: (PhaseConfigurationService, PhaserState, Context, Input) -> Output,
    op: (Context, Input) -> Output
): SimpleNamedCompilerPhase = object : SimpleNamedCompilerPhase(
    name,
    description,
    preactions = preactions,
    postactions = postactions.map { f ->
        fun(actionState: ActionState, data: Pair, context: Context) = f(actionState, data.second, context)
    }.toSet(),
    prerequisite = prerequisite,
) {
    override fun outputIfNotEnabled(phaseConfig: PhaseConfigurationService, phaserState: PhaserState, context: Context, input: Input): Output =
        outputIfNotEnabled(phaseConfig, phaserState, context, input)

    override fun phaseBody(context: Context, input: Input): Output =
        op(context, input)
}

fun  createSimpleNamedCompilerPhase(
    name: String,
    description: String,
    preactions: Set> = emptySet(),
    postactions: Set> = emptySet(),
    prerequisite: Set> = emptySet(),
    op: (Context, Input) -> Unit
): SimpleNamedCompilerPhase = object : SimpleNamedCompilerPhase(
    name,
    description,
    preactions = preactions,
    postactions = postactions.map { f ->
        fun(actionState: ActionState, data: Pair, context: Context) = f(actionState, data.first, context)
    }.toSet(),
    prerequisite = prerequisite,
) {
    override fun outputIfNotEnabled(phaseConfig: PhaseConfigurationService, phaserState: PhaserState, context: Context, input: Input) {}

    override fun phaseBody(context: Context, input: Input): Unit =
        op(context, input)
}

private val defaultConditions = setOf(defaultDumper, validationAction)

fun  makeIrFilePhase(
    lowering: (Context) -> FileLoweringPass,
    name: String,
    description: String,
    prerequisite: Set> = emptySet(),
    preconditions: Set> = emptySet(),
    postconditions: Set> = emptySet(),
): SimpleNamedCompilerPhase =
    createSimpleNamedCompilerPhase(
        name = name,
        description = description,
        preactions = defaultConditions + preconditions,
        postactions = defaultConditions + postconditions,
        prerequisite = prerequisite,
        outputIfNotEnabled = { _, _, _, irFile -> irFile },
        op = { context, irFile ->
            lowering(context).lower(irFile)
            irFile
        },
    )

fun  makeIrModulePhase(
    lowering: (Context) -> ModuleLoweringPass,
    name: String,
    description: String,
    prerequisite: Set> = emptySet(),
    preconditions: Set> = emptySet(),
    postconditions: Set> = emptySet(),
): SimpleNamedCompilerPhase =
    createSimpleNamedCompilerPhase(
        name = name,
        description = description,
        preactions = defaultConditions + preconditions,
        postactions = defaultConditions + postconditions,
        prerequisite = prerequisite,
        outputIfNotEnabled = { _, _, _, irModule -> irModule },
        op = { context, irModule ->
            lowering(context).lower(irModule)
            irModule
        },
    )




© 2015 - 2024 Weber Informatics LLC | Privacy Policy