diff --git a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/phaser/CompilerPhase.kt b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/phaser/CompilerPhase.kt index 425fc586261..4cfb622c06d 100644 --- a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/phaser/CompilerPhase.kt +++ b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/phaser/CompilerPhase.kt @@ -179,4 +179,60 @@ class NamedCompilerPhase( } -typealias SameTypeNamedCompilerPhase = NamedCompilerPhase \ No newline at end of file +typealias SameTypeNamedCompilerPhase = NamedCompilerPhase + +/** + * [AbstractNamedCompilerPhase] with different [Input] and [Output] types (unlike [SameTypeNamedCompilerPhase]). + * Preffered when data should be explicitly passed between phases. + * Actively used in a new dynamic Kotlin/Native driver. + */ +abstract class SimpleNamedCompilerPhase( + name: String, + description: String, + prerequisite: Set> = emptySet(), + preconditions: Set> = emptySet(), + postconditions: Set> = emptySet(), + private val preactions: Set> = emptySet(), + private val postactions: Set> = emptySet(), + nlevels: Int = 0, +) : AbstractNamedCompilerPhase( + name, + description, + prerequisite, + preconditions, + postconditions, + nlevels, +) { + final override fun phaseBody(phaseConfig: PhaseConfigurationService, phaserState: PhaserState, context: Context, input: Input): Output = + phaseBody(context, input) + + abstract fun phaseBody(context: Context, input: Input): Output + + override fun changePhaserStateType(phaserState: PhaserState): PhaserState = + phaserState.changePhaserStateType() + + override fun runBefore(phaseConfig: PhaseConfigurationService, phaserState: PhaserState, context: Context, input: Input) { + val state = ActionState(phaseConfig, this, phaserState.phaseCount, BeforeOrAfter.BEFORE) + for (action in preactions) action(state, input, context) + + if (phaseConfig.checkConditions) { + for (pre in preconditions) pre(input) + } + } + + override fun runAfter(phaseConfig: PhaseConfigurationService, phaserState: PhaserState, context: Context, output: Output) { + val state = ActionState(phaseConfig, this, phaserState.phaseCount, BeforeOrAfter.AFTER) + for (action in postactions) action(state, output, context) + + if (phaseConfig.checkConditions) { + for (post in postconditions) post(output) + for (post in stickyPostconditions) post(output) + if (phaseConfig.checkStickyConditions) { + for (post in phaserState.stickyPostconditions) post(output) + } + } + } + + override fun getNamedSubphases(startDepth: Int): List>> = + listOf(startDepth to this) +} \ No newline at end of file