org.jetbrains.kotlin.backend.common.phaser.PhaseBuilders.kt Maven / Gradle / Ivy
/*
* 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.lower
import org.jetbrains.kotlin.ir.IrElement
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 makeCustomPhase(
op: (Context, Element) -> Unit,
name: String,
description: String,
prerequisite: Set> = emptySet(),
preconditions: Set> = emptySet(),
postconditions: Set> = emptySet(),
stickyPostconditions: Set> = emptySet(),
actions: Set> = setOf(defaultDumper, validationAction),
nlevels: Int = 1
): SameTypeNamedCompilerPhase =
SameTypeNamedCompilerPhase(
name, description, prerequisite, CustomPhaseAdapter(op), preconditions, postconditions, stickyPostconditions, actions, nlevels,
)
private class CustomPhaseAdapter(
private val op: (Context, Element) -> Unit
) : SameTypeCompilerPhase {
override fun invoke(phaseConfig: PhaseConfigurationService, phaserState: PhaserState, context: Context, input: Element): Element {
op(context, input)
return input
}
}
fun namedUnitPhase(
name: String,
description: String,
prerequisite: Set> = emptySet(),
nlevels: Int = 1,
lower: CompilerPhase
): SameTypeNamedCompilerPhase =
SameTypeNamedCompilerPhase(
name, description, prerequisite, lower, nlevels = nlevels
)
@Suppress("unused") // Used in kotlin-native
fun namedOpUnitPhase(
name: String,
description: String,
prerequisite: Set>,
op: Context.() -> Unit
): SameTypeNamedCompilerPhase = namedUnitPhase(
name, description, prerequisite,
nlevels = 0,
lower = object : SameTypeCompilerPhase {
override fun invoke(phaseConfig: PhaseConfigurationService, phaserState: PhaserState, context: Context, input: Unit) {
context.op()
}
}
)
fun makeIrFilePhase(
lowering: (Context) -> FileLoweringPass,
name: String,
description: String,
prerequisite: Set> = emptySet(),
preconditions: Set> = emptySet(),
postconditions: Set> = emptySet(),
stickyPostconditions: Set> = emptySet(),
actions: Set> = setOf(defaultDumper, validationAction)
): SameTypeNamedCompilerPhase =
SameTypeNamedCompilerPhase(
name, description, prerequisite, FileLoweringPhaseAdapter(lowering), preconditions, postconditions, stickyPostconditions, actions,
nlevels = 0,
)
private class FileLoweringPhaseAdapter(
private val lowering: (Context) -> FileLoweringPass
) : SameTypeCompilerPhase {
override fun invoke(phaseConfig: PhaseConfigurationService, phaserState: PhaserState, context: Context, input: IrFile): IrFile {
lowering(context).lower(input)
return input
}
}
fun makeIrModulePhase(
lowering: (Context) -> FileLoweringPass,
name: String,
description: String,
prerequisite: Set> = emptySet(),
preconditions: Set> = emptySet(),
postconditions: Set> = emptySet(),
stickyPostconditions: Set> = emptySet(),
actions: Set> = setOf(defaultDumper, validationAction)
): SameTypeNamedCompilerPhase =
SameTypeNamedCompilerPhase(
name, description, prerequisite, ModuleLoweringPhaseAdapter(lowering), preconditions, postconditions, stickyPostconditions, actions,
nlevels = 0,
)
private class ModuleLoweringPhaseAdapter(
private val lowering: (Context) -> FileLoweringPass
) : SameTypeCompilerPhase {
override fun invoke(
phaseConfig: PhaseConfigurationService, phaserState: PhaserState, context: Context, input: IrModuleFragment
): IrModuleFragment {
lowering(context).lower(input)
return input
}
}
@Suppress("unused") // Used in kotlin-native
fun unitSink(): CompilerPhase =
object : CompilerPhase {
override fun invoke(phaseConfig: PhaseConfigurationService, phaserState: PhaserState, context: Context, input: Input) {}
}
// Intermediate phases to change the object of transformations
@Suppress("unused") // Used in kotlin-native
fun takeFromContext(op: (Context) -> NewData): CompilerPhase =
object : CompilerPhase {
override fun invoke(phaseConfig: PhaseConfigurationService, phaserState: PhaserState, context: Context, input: OldData) = op(context)
}
fun transform(op: (OldData) -> NewData): CompilerPhase =
object : CompilerPhase {
override fun invoke(phaseConfig: PhaseConfigurationService, phaserState: PhaserState, context: Context, input: OldData) = op(input)
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy