org.jetbrains.kotlin.backend.common.phaser.PhaseConfig.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
fun CompilerPhase<*, *, *>.toPhaseMap(): MutableMap =
getNamedSubphases().fold(mutableMapOf()) { acc, (_, phase) ->
check(phase.name !in acc) { "Duplicate phase name '${phase.name}'" }
acc[phase.name] = phase
acc
}
class PhaseConfig(
private val compoundPhase: CompilerPhase<*, *, *>,
private val phases: MutableMap = compoundPhase.toPhaseMap(),
enabled: MutableSet = phases.values.toMutableSet(),
val verbose: Set = emptySet(),
val toDumpStateBefore: Set = emptySet(),
val toDumpStateAfter: Set = emptySet(),
val dumpToDirectory: String? = null,
val dumpOnlyFqName: String? = null,
val toValidateStateBefore: Set = emptySet(),
val toValidateStateAfter: Set = emptySet(),
val namesOfElementsExcludedFromDumping: Set = emptySet(),
val needProfiling: Boolean = false,
val checkConditions: Boolean = false,
val checkStickyConditions: Boolean = false
) {
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.")
}
return name
}
fun list() {
compoundPhase.getNamedSubphases().forEach { (depth, phase) ->
val enabled = if (phase in enabled) "(Enabled)" else ""
val verbose = if (phase in verbose) "(Verbose)" else ""
println(String.format("%1$-50s %2$-50s %3$-10s", "${" ".repeat(depth)}${phase.name}:", phase.description, "$enabled $verbose"))
}
}
fun enable(phase: AnyNamedPhase) {
enabledMut.add(phase)
}
fun disable(phase: AnyNamedPhase) {
enabledMut.remove(phase)
}
fun switch(phase: AnyNamedPhase, onOff: Boolean) {
if (onOff) {
enable(phase)
} else {
disable(phase)
}
}
}