From ddad3034dab3b8f20d40dde443819de1881f6e68 Mon Sep 17 00:00:00 2001 From: Alexander Udalov Date: Mon, 18 Mar 2019 18:54:11 +0100 Subject: [PATCH] Extract createPhaseConfig from PhaseConfig To remove dependency of PhaseConfig on CLI-dependent concepts such as CompilerConfiguration --- .../backend/common/phaser/PhaseConfig.kt | 90 +++++++++++-------- .../ir/backend/js/JsIrBackendContext.kt | 3 +- .../kotlin/backend/jvm/JvmBackendContext.kt | 3 +- 3 files changed, 56 insertions(+), 40 deletions(-) diff --git a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/phaser/PhaseConfig.kt b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/phaser/PhaseConfig.kt index 2b3d7b9c0a7..4b54b76dc60 100644 --- a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/phaser/PhaseConfig.kt +++ b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/phaser/PhaseConfig.kt @@ -9,44 +9,69 @@ import org.jetbrains.kotlin.config.CommonConfigurationKeys import org.jetbrains.kotlin.config.CompilerConfiguration import org.jetbrains.kotlin.config.CompilerConfigurationKey -class PhaseConfig(private val compoundPhase: CompilerPhase<*, *, *>, config: CompilerConfiguration) { - +fun createPhaseConfig(compoundPhase: CompilerPhase<*, *, *>, config: CompilerConfiguration): PhaseConfig { val phases = compoundPhase.getNamedSubphases().fold(mutableMapOf()) { acc, (_, phase) -> check(phase.name !in acc) { "Duplicate phase name '${phase.name}'"} acc[phase.name] = phase acc } - private val enabledMut = computeEnabled(config).toMutableSet() - val enabled: Set get() = enabledMut + val enabled = computeEnabled(phases, config).toMutableSet() + val verbose = phaseSetFromConfiguration(phases, config, CommonConfigurationKeys.VERBOSE_PHASES) - val verbose = phaseSetFromConfiguration(config, CommonConfigurationKeys.VERBOSE_PHASES) - val toDumpStateBefore: Set - - val toDumpStateAfter: Set - val toValidateStateBefore: Set - - val toValidateStateAfter: Set - - init { - with(CommonConfigurationKeys) { - val beforeDumpSet = phaseSetFromConfiguration(config, PHASES_TO_DUMP_STATE_BEFORE) - val afterDumpSet = phaseSetFromConfiguration(config, PHASES_TO_DUMP_STATE_AFTER) - val bothDumpSet = phaseSetFromConfiguration(config, PHASES_TO_DUMP_STATE) - toDumpStateBefore = beforeDumpSet + bothDumpSet - toDumpStateAfter = afterDumpSet + bothDumpSet - val beforeValidateSet = phaseSetFromConfiguration(config, PHASES_TO_VALIDATE_BEFORE) - val afterValidateSet = phaseSetFromConfiguration(config, PHASES_TO_VALIDATE_AFTER) - val bothValidateSet = phaseSetFromConfiguration(config, PHASES_TO_VALIDATE) - toValidateStateBefore = beforeValidateSet + bothValidateSet - toValidateStateAfter = afterValidateSet + bothValidateSet - } - } + val beforeDumpSet = phaseSetFromConfiguration(phases, config, CommonConfigurationKeys.PHASES_TO_DUMP_STATE_BEFORE) + val afterDumpSet = phaseSetFromConfiguration(phases, config, CommonConfigurationKeys.PHASES_TO_DUMP_STATE_AFTER) + val bothDumpSet = phaseSetFromConfiguration(phases, config, CommonConfigurationKeys.PHASES_TO_DUMP_STATE) + val toDumpStateBefore = beforeDumpSet + bothDumpSet + val toDumpStateAfter = afterDumpSet + bothDumpSet + val beforeValidateSet = phaseSetFromConfiguration(phases, config, CommonConfigurationKeys.PHASES_TO_VALIDATE_BEFORE) + val afterValidateSet = phaseSetFromConfiguration(phases, config, CommonConfigurationKeys.PHASES_TO_VALIDATE_AFTER) + val bothValidateSet = phaseSetFromConfiguration(phases, config, CommonConfigurationKeys.PHASES_TO_VALIDATE) + val toValidateStateBefore = beforeValidateSet + bothValidateSet + val toValidateStateAfter = afterValidateSet + bothValidateSet val needProfiling = config.getBoolean(CommonConfigurationKeys.PROFILE_PHASES) val checkConditions = config.getBoolean(CommonConfigurationKeys.CHECK_PHASE_CONDITIONS) val checkStickyConditions = config.getBoolean(CommonConfigurationKeys.CHECK_STICKY_CONDITIONS) + return PhaseConfig(compoundPhase, phases, enabled, verbose, toDumpStateBefore, toDumpStateAfter, toValidateStateBefore, toValidateStateAfter, needProfiling, checkConditions, checkStickyConditions) +} + +private fun computeEnabled( + phases: MutableMap, + config: CompilerConfiguration +): Set { + val disabledPhases = phaseSetFromConfiguration(phases, config, CommonConfigurationKeys.DISABLED_PHASES) + return phases.values.toSet() - disabledPhases +} + +private fun phaseSetFromConfiguration( + phases: MutableMap, + config: CompilerConfiguration, + key: CompilerConfigurationKey> +): Set { + val phaseNames = config.get(key) ?: emptySet() + if ("ALL" in phaseNames) return phases.values.toSet() + return phaseNames.map { phases[it]!! }.toSet() +} + +class PhaseConfig( + private val compoundPhase: CompilerPhase<*, *, *>, + private val phases: MutableMap, + enabled: MutableSet, + val verbose: Set, + val toDumpStateBefore: Set, + val toDumpStateAfter: Set, + val toValidateStateBefore: Set, + val toValidateStateAfter: Set, + val needProfiling: Boolean, + val checkConditions: Boolean, + val checkStickyConditions: Boolean +) { + private val enabledMut = enabled + + val enabled: Set get() = enabledMut + fun known(name: String): String { if (phases[name] == null) { error("Unknown phase: $name. Use -Xlist-phases to see the list of phases.") @@ -63,18 +88,6 @@ class PhaseConfig(private val compoundPhase: CompilerPhase<*, *, *>, config: Com } } - private fun computeEnabled(config: CompilerConfiguration) = - with(CommonConfigurationKeys) { - val disabledPhases = phaseSetFromConfiguration(config, DISABLED_PHASES) - phases.values.toSet() - disabledPhases - } - - private fun phaseSetFromConfiguration(config: CompilerConfiguration, key: CompilerConfigurationKey>): Set { - val phaseNames = config.get(key) ?: emptySet() - if ("ALL" in phaseNames) return phases.values.toSet() - return phaseNames.map { phases[it]!! }.toSet() - } - fun enable(phase: AnyNamedPhase) { enabledMut.add(phase) } @@ -82,6 +95,7 @@ class PhaseConfig(private val compoundPhase: CompilerPhase<*, *, *>, config: Com fun disable(phase: AnyNamedPhase) { enabledMut.remove(phase) } + fun switch(phase: AnyNamedPhase, onOff: Boolean) { if (onOff) { enable(phase) diff --git a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/JsIrBackendContext.kt b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/JsIrBackendContext.kt index 74185deba6f..d0f1a52af70 100644 --- a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/JsIrBackendContext.kt +++ b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/JsIrBackendContext.kt @@ -11,6 +11,7 @@ import org.jetbrains.kotlin.backend.common.descriptors.KnownPackageFragmentDescr import org.jetbrains.kotlin.backend.common.ir.Ir import org.jetbrains.kotlin.backend.common.ir.Symbols import org.jetbrains.kotlin.backend.common.phaser.PhaseConfig +import org.jetbrains.kotlin.backend.common.phaser.createPhaseConfig import org.jetbrains.kotlin.backend.js.JsDeclarationFactory import org.jetbrains.kotlin.builtins.PrimitiveType import org.jetbrains.kotlin.config.CompilerConfiguration @@ -49,7 +50,7 @@ class JsIrBackendContext( override val builtIns = module.builtIns - val phaseConfig = PhaseConfig(jsPhases, configuration) + val phaseConfig = createPhaseConfig(jsPhases, configuration) override var inVerbosePhase: Boolean = false val externalNestedClasses = mutableListOf() diff --git a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/JvmBackendContext.kt b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/JvmBackendContext.kt index 2454fe498f3..ba48c3adac4 100644 --- a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/JvmBackendContext.kt +++ b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/JvmBackendContext.kt @@ -9,6 +9,7 @@ import org.jetbrains.kotlin.backend.common.CommonBackendContext import org.jetbrains.kotlin.backend.common.ir.Ir import org.jetbrains.kotlin.backend.common.ir.Symbols import org.jetbrains.kotlin.backend.common.phaser.PhaseConfig +import org.jetbrains.kotlin.backend.common.phaser.createPhaseConfig import org.jetbrains.kotlin.backend.jvm.descriptors.JvmDeclarationFactory import org.jetbrains.kotlin.backend.jvm.descriptors.JvmSharedVariablesManager import org.jetbrains.kotlin.codegen.state.GenerationState @@ -38,7 +39,7 @@ class JvmBackendContext( override val ir = JvmIr(irModuleFragment, symbolTable) - val phaseConfig = PhaseConfig(jvmPhases, state.configuration) + val phaseConfig = createPhaseConfig(jvmPhases, state.configuration) override var inVerbosePhase: Boolean = false override val configuration get() = state.configuration