From cd3dece7266a6bd0c88133d2e0b10dc078192f94 Mon Sep 17 00:00:00 2001 From: Sergey Bogolepov Date: Fri, 14 Oct 2022 15:44:10 +0300 Subject: [PATCH] Introduce SimpleNamedCompilerPhase Dynamic Kotlin/Native compiler driver relies on a more "pure" approach when data is explicitly passed between different phases. SameTypeNamedCompilerPhase is not suitable for this purpose, so we need a new type for that. --- .../backend/common/phaser/CompilerPhase.kt | 58 ++++++++++++++++++- 1 file changed, 57 insertions(+), 1 deletion(-) 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