IR: refactor PhaseBuilders a little
- use named classes to improve stacktraces - reorder parameters to make the code shorter - use explicit types to improve IDE resolve in usages
This commit is contained in:
+49
-77
@@ -58,16 +58,9 @@ fun <Context : CommonBackendContext> namedIrModulePhase(
|
|||||||
stickyPostconditions: Set<Checker<IrModuleFragment>> = lower.stickyPostconditions,
|
stickyPostconditions: Set<Checker<IrModuleFragment>> = lower.stickyPostconditions,
|
||||||
actions: Set<Action<IrModuleFragment, Context>> = setOf(defaultDumper, validationAction),
|
actions: Set<Action<IrModuleFragment, Context>> = setOf(defaultDumper, validationAction),
|
||||||
nlevels: Int = 1
|
nlevels: Int = 1
|
||||||
) = NamedCompilerPhase(
|
): NamedCompilerPhase<Context, IrModuleFragment> =
|
||||||
name,
|
NamedCompilerPhase(
|
||||||
description,
|
name, description, prerequisite, lower, preconditions, postconditions, stickyPostconditions, actions, nlevels
|
||||||
prerequisite,
|
|
||||||
lower,
|
|
||||||
preconditions,
|
|
||||||
postconditions,
|
|
||||||
stickyPostconditions,
|
|
||||||
actions,
|
|
||||||
nlevels
|
|
||||||
)
|
)
|
||||||
|
|
||||||
fun <Context : CommonBackendContext> namedIrFilePhase(
|
fun <Context : CommonBackendContext> namedIrFilePhase(
|
||||||
@@ -80,16 +73,9 @@ fun <Context : CommonBackendContext> namedIrFilePhase(
|
|||||||
stickyPostconditions: Set<Checker<IrFile>> = lower.stickyPostconditions,
|
stickyPostconditions: Set<Checker<IrFile>> = lower.stickyPostconditions,
|
||||||
actions: Set<Action<IrFile, Context>> = setOf(defaultDumper, validationAction),
|
actions: Set<Action<IrFile, Context>> = setOf(defaultDumper, validationAction),
|
||||||
nlevels: Int = 1
|
nlevels: Int = 1
|
||||||
) = NamedCompilerPhase(
|
): NamedCompilerPhase<Context, IrFile> =
|
||||||
name,
|
NamedCompilerPhase(
|
||||||
description,
|
name, description, prerequisite, lower, preconditions, postconditions, stickyPostconditions, actions, nlevels
|
||||||
prerequisite,
|
|
||||||
lower,
|
|
||||||
preconditions,
|
|
||||||
postconditions,
|
|
||||||
stickyPostconditions,
|
|
||||||
actions,
|
|
||||||
nlevels
|
|
||||||
)
|
)
|
||||||
|
|
||||||
fun <Context : CommonBackendContext, Element : IrElement> makeCustomPhase(
|
fun <Context : CommonBackendContext, Element : IrElement> makeCustomPhase(
|
||||||
@@ -102,27 +88,19 @@ fun <Context : CommonBackendContext, Element : IrElement> makeCustomPhase(
|
|||||||
stickyPostconditions: Set<Checker<Element>> = emptySet(),
|
stickyPostconditions: Set<Checker<Element>> = emptySet(),
|
||||||
actions: Set<Action<Element, Context>> = setOf(defaultDumper, validationAction),
|
actions: Set<Action<Element, Context>> = setOf(defaultDumper, validationAction),
|
||||||
nlevels: Int = 1
|
nlevels: Int = 1
|
||||||
) = NamedCompilerPhase(
|
): NamedCompilerPhase<Context, Element> =
|
||||||
name,
|
NamedCompilerPhase(
|
||||||
description,
|
name, description, prerequisite, CustomPhaseAdapter(op), preconditions, postconditions, stickyPostconditions, actions, nlevels,
|
||||||
prerequisite,
|
)
|
||||||
preconditions = preconditions,
|
|
||||||
postconditions = postconditions,
|
private class CustomPhaseAdapter<Context : CommonBackendContext, Element>(
|
||||||
stickyPostconditions = stickyPostconditions,
|
private val op: (Context, Element) -> Unit
|
||||||
actions = actions,
|
) : SameTypeCompilerPhase<Context, Element> {
|
||||||
nlevels = nlevels,
|
override fun invoke(phaseConfig: PhaseConfig, phaserState: PhaserState<Element>, context: Context, input: Element): Element {
|
||||||
lower = object : SameTypeCompilerPhase<Context, Element> {
|
|
||||||
override fun invoke(
|
|
||||||
phaseConfig: PhaseConfig,
|
|
||||||
phaserState: PhaserState<Element>,
|
|
||||||
context: Context,
|
|
||||||
input: Element
|
|
||||||
): Element {
|
|
||||||
op(context, input)
|
op(context, input)
|
||||||
return input
|
return input
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
)
|
|
||||||
|
|
||||||
fun <Context : CommonBackendContext> namedUnitPhase(
|
fun <Context : CommonBackendContext> namedUnitPhase(
|
||||||
name: String,
|
name: String,
|
||||||
@@ -130,10 +108,9 @@ fun <Context : CommonBackendContext> namedUnitPhase(
|
|||||||
prerequisite: Set<NamedCompilerPhase<Context, *>> = emptySet(),
|
prerequisite: Set<NamedCompilerPhase<Context, *>> = emptySet(),
|
||||||
nlevels: Int = 1,
|
nlevels: Int = 1,
|
||||||
lower: CompilerPhase<Context, Unit, Unit>
|
lower: CompilerPhase<Context, Unit, Unit>
|
||||||
) = NamedCompilerPhase(
|
): NamedCompilerPhase<Context, Unit> =
|
||||||
name, description, prerequisite,
|
NamedCompilerPhase(
|
||||||
lower = lower,
|
name, description, prerequisite, lower, nlevels = nlevels
|
||||||
nlevels = nlevels
|
|
||||||
)
|
)
|
||||||
|
|
||||||
@Suppress("unused") // Used in kotlin-native
|
@Suppress("unused") // Used in kotlin-native
|
||||||
@@ -142,7 +119,7 @@ fun <Context : CommonBackendContext> namedOpUnitPhase(
|
|||||||
description: String,
|
description: String,
|
||||||
prerequisite: Set<NamedCompilerPhase<Context, *>>,
|
prerequisite: Set<NamedCompilerPhase<Context, *>>,
|
||||||
op: Context.() -> Unit
|
op: Context.() -> Unit
|
||||||
) = namedUnitPhase(
|
): NamedCompilerPhase<Context, Unit> = namedUnitPhase(
|
||||||
name, description, prerequisite,
|
name, description, prerequisite,
|
||||||
nlevels = 0,
|
nlevels = 0,
|
||||||
lower = object : SameTypeCompilerPhase<Context, Unit> {
|
lower = object : SameTypeCompilerPhase<Context, Unit> {
|
||||||
@@ -161,19 +138,17 @@ fun <Context : CommonBackendContext> performByIrFile(
|
|||||||
stickyPostconditions: Set<Checker<IrModuleFragment>> = emptySet(),
|
stickyPostconditions: Set<Checker<IrModuleFragment>> = emptySet(),
|
||||||
actions: Set<Action<IrModuleFragment, Context>> = setOf(defaultDumper),
|
actions: Set<Action<IrModuleFragment, Context>> = setOf(defaultDumper),
|
||||||
lower: CompilerPhase<Context, IrFile, IrFile>
|
lower: CompilerPhase<Context, IrFile, IrFile>
|
||||||
) = namedIrModulePhase(
|
): NamedCompilerPhase<Context, IrModuleFragment> =
|
||||||
name, description, prerequisite,
|
namedIrModulePhase(
|
||||||
preconditions = preconditions,
|
name, description, prerequisite, PerformByIrFilePhase(lower), preconditions, postconditions, stickyPostconditions, actions,
|
||||||
postconditions = postconditions,
|
|
||||||
stickyPostconditions = stickyPostconditions,
|
|
||||||
actions = actions,
|
|
||||||
nlevels = 1,
|
nlevels = 1,
|
||||||
lower = object : SameTypeCompilerPhase<Context, IrModuleFragment> {
|
)
|
||||||
|
|
||||||
|
private class PerformByIrFilePhase<Context : CommonBackendContext>(
|
||||||
|
private val lower: CompilerPhase<Context, IrFile, IrFile>
|
||||||
|
) : SameTypeCompilerPhase<Context, IrModuleFragment> {
|
||||||
override fun invoke(
|
override fun invoke(
|
||||||
phaseConfig: PhaseConfig,
|
phaseConfig: PhaseConfig, phaserState: PhaserState<IrModuleFragment>, context: Context, input: IrModuleFragment
|
||||||
phaserState: PhaserState<IrModuleFragment>,
|
|
||||||
context: Context,
|
|
||||||
input: IrModuleFragment
|
|
||||||
): IrModuleFragment {
|
): IrModuleFragment {
|
||||||
for (irFile in input.files) {
|
for (irFile in input.files) {
|
||||||
try {
|
try {
|
||||||
@@ -189,7 +164,6 @@ fun <Context : CommonBackendContext> performByIrFile(
|
|||||||
|
|
||||||
override fun getNamedSubphases(startDepth: Int) = lower.getNamedSubphases(startDepth)
|
override fun getNamedSubphases(startDepth: Int) = lower.getNamedSubphases(startDepth)
|
||||||
}
|
}
|
||||||
)
|
|
||||||
|
|
||||||
fun <Context : CommonBackendContext> makeIrFilePhase(
|
fun <Context : CommonBackendContext> makeIrFilePhase(
|
||||||
lowering: (Context) -> FileLoweringPass,
|
lowering: (Context) -> FileLoweringPass,
|
||||||
@@ -200,20 +174,20 @@ fun <Context : CommonBackendContext> makeIrFilePhase(
|
|||||||
postconditions: Set<Checker<IrFile>> = emptySet(),
|
postconditions: Set<Checker<IrFile>> = emptySet(),
|
||||||
stickyPostconditions: Set<Checker<IrFile>> = emptySet(),
|
stickyPostconditions: Set<Checker<IrFile>> = emptySet(),
|
||||||
actions: Set<Action<IrFile, Context>> = setOf(defaultDumper, validationAction)
|
actions: Set<Action<IrFile, Context>> = setOf(defaultDumper, validationAction)
|
||||||
) = namedIrFilePhase(
|
): NamedCompilerPhase<Context, IrFile> =
|
||||||
name, description, prerequisite,
|
namedIrFilePhase(
|
||||||
preconditions = preconditions,
|
name, description, prerequisite, FileLoweringPhaseAdapter(lowering), preconditions, postconditions, stickyPostconditions, actions,
|
||||||
postconditions = postconditions,
|
|
||||||
stickyPostconditions = stickyPostconditions,
|
|
||||||
actions = actions,
|
|
||||||
nlevels = 0,
|
nlevels = 0,
|
||||||
lower = object : SameTypeCompilerPhase<Context, IrFile> {
|
)
|
||||||
|
|
||||||
|
private class FileLoweringPhaseAdapter<Context : CommonBackendContext>(
|
||||||
|
private val lowering: (Context) -> FileLoweringPass
|
||||||
|
) : SameTypeCompilerPhase<Context, IrFile> {
|
||||||
override fun invoke(phaseConfig: PhaseConfig, phaserState: PhaserState<IrFile>, context: Context, input: IrFile): IrFile {
|
override fun invoke(phaseConfig: PhaseConfig, phaserState: PhaserState<IrFile>, context: Context, input: IrFile): IrFile {
|
||||||
lowering(context).lower(input)
|
lowering(context).lower(input)
|
||||||
return input
|
return input
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
)
|
|
||||||
|
|
||||||
fun <Context : CommonBackendContext> makeIrModulePhase(
|
fun <Context : CommonBackendContext> makeIrModulePhase(
|
||||||
lowering: (Context) -> FileLoweringPass,
|
lowering: (Context) -> FileLoweringPass,
|
||||||
@@ -224,39 +198,37 @@ fun <Context : CommonBackendContext> makeIrModulePhase(
|
|||||||
postconditions: Set<Checker<IrModuleFragment>> = emptySet(),
|
postconditions: Set<Checker<IrModuleFragment>> = emptySet(),
|
||||||
stickyPostconditions: Set<Checker<IrModuleFragment>> = emptySet(),
|
stickyPostconditions: Set<Checker<IrModuleFragment>> = emptySet(),
|
||||||
actions: Set<Action<IrModuleFragment, Context>> = setOf(defaultDumper, validationAction)
|
actions: Set<Action<IrModuleFragment, Context>> = setOf(defaultDumper, validationAction)
|
||||||
) = namedIrModulePhase(
|
): NamedCompilerPhase<Context, IrModuleFragment> =
|
||||||
name, description, prerequisite,
|
namedIrModulePhase(
|
||||||
preconditions = preconditions,
|
name, description, prerequisite, ModuleLoweringPhaseAdapter(lowering), preconditions, postconditions, stickyPostconditions, actions,
|
||||||
postconditions = postconditions,
|
|
||||||
stickyPostconditions = stickyPostconditions,
|
|
||||||
actions = actions,
|
|
||||||
nlevels = 0,
|
nlevels = 0,
|
||||||
lower = object : SameTypeCompilerPhase<Context, IrModuleFragment> {
|
)
|
||||||
|
|
||||||
|
private class ModuleLoweringPhaseAdapter<Context : CommonBackendContext>(
|
||||||
|
private val lowering: (Context) -> FileLoweringPass
|
||||||
|
) : SameTypeCompilerPhase<Context, IrModuleFragment> {
|
||||||
override fun invoke(
|
override fun invoke(
|
||||||
phaseConfig: PhaseConfig,
|
phaseConfig: PhaseConfig, phaserState: PhaserState<IrModuleFragment>, context: Context, input: IrModuleFragment
|
||||||
phaserState: PhaserState<IrModuleFragment>,
|
|
||||||
context: Context,
|
|
||||||
input: IrModuleFragment
|
|
||||||
): IrModuleFragment {
|
): IrModuleFragment {
|
||||||
lowering(context).lower(input)
|
lowering(context).lower(input)
|
||||||
return input
|
return input
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
)
|
|
||||||
|
|
||||||
@Suppress("unused") // Used in kotlin-native
|
@Suppress("unused") // Used in kotlin-native
|
||||||
fun <Context : CommonBackendContext, Input> unitSink() = object : CompilerPhase<Context, Input, Unit> {
|
fun <Context : CommonBackendContext, Input> unitSink(): CompilerPhase<Context, Input, Unit> =
|
||||||
|
object : CompilerPhase<Context, Input, Unit> {
|
||||||
override fun invoke(phaseConfig: PhaseConfig, phaserState: PhaserState<Input>, context: Context, input: Input) {}
|
override fun invoke(phaseConfig: PhaseConfig, phaserState: PhaserState<Input>, context: Context, input: Input) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Intermediate phases to change the object of transformations
|
// Intermediate phases to change the object of transformations
|
||||||
@Suppress("unused") // Used in kotlin-native
|
@Suppress("unused") // Used in kotlin-native
|
||||||
fun <Context : CommonBackendContext, OldData, NewData> takeFromContext(op: (Context) -> NewData) =
|
fun <Context : CommonBackendContext, OldData, NewData> takeFromContext(op: (Context) -> NewData): CompilerPhase<Context, OldData, NewData> =
|
||||||
object : CompilerPhase<Context, OldData, NewData> {
|
object : CompilerPhase<Context, OldData, NewData> {
|
||||||
override fun invoke(phaseConfig: PhaseConfig, phaserState: PhaserState<OldData>, context: Context, input: OldData) = op(context)
|
override fun invoke(phaseConfig: PhaseConfig, phaserState: PhaserState<OldData>, context: Context, input: OldData) = op(context)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun <Context : CommonBackendContext, OldData, NewData> transform(op: (OldData) -> NewData) =
|
fun <Context : CommonBackendContext, OldData, NewData> transform(op: (OldData) -> NewData): CompilerPhase<Context, OldData, NewData> =
|
||||||
object : CompilerPhase<Context, OldData, NewData> {
|
object : CompilerPhase<Context, OldData, NewData> {
|
||||||
override fun invoke(phaseConfig: PhaseConfig, phaserState: PhaserState<OldData>, context: Context, input: OldData) = op(input)
|
override fun invoke(phaseConfig: PhaseConfig, phaserState: PhaserState<OldData>, context: Context, input: OldData) = op(input)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user