From 87916d55490ff04691bc59454086c82ed09646b2 Mon Sep 17 00:00:00 2001 From: Daniel Kasza Date: Mon, 16 Nov 2020 14:21:01 -0500 Subject: Improve source locators for switch statements. (#1669) * Improve source locators for switch statements.--- src/main/scala/chisel3/util/Conditional.scala | 7 ++++--- src/test/scala/chiselTests/SwitchSpec.scala | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/main/scala/chisel3/util/Conditional.scala b/src/main/scala/chisel3/util/Conditional.scala index ca393055..b934f27f 100644 --- a/src/main/scala/chisel3/util/Conditional.scala +++ b/src/main/scala/chisel3/util/Conditional.scala @@ -10,6 +10,7 @@ import scala.language.experimental.macros import scala.reflect.macros.blackbox._ import chisel3._ +import chisel3.internal.sourceinfo.SourceInfo @deprecated("The unless conditional is deprecated, use when(!condition){...} instead", "3.2") object unless { @@ -25,7 +26,7 @@ object unless { * @note DO NOT USE. This API is subject to change without warning. */ class SwitchContext[T <: Element](cond: T, whenContext: Option[WhenContext], lits: Set[BigInt]) { - def is(v: Iterable[T])(block: => Any): SwitchContext[T] = { + def is(v: Iterable[T])(block: => Any)(implicit sourceInfo: SourceInfo, compileOptions: CompileOptions): SwitchContext[T] = { if (!v.isEmpty) { val newLits = v.map { w => require(w.litOption.isDefined, "is condition must be literal") @@ -43,8 +44,8 @@ class SwitchContext[T <: Element](cond: T, whenContext: Option[WhenContext], lit this } } - def is(v: T)(block: => Any): SwitchContext[T] = is(Seq(v))(block) - def is(v: T, vr: T*)(block: => Any): SwitchContext[T] = is(v :: vr.toList)(block) + def is(v: T)(block: => Any)(implicit sourceInfo: SourceInfo, compileOptions: CompileOptions): SwitchContext[T] = is(Seq(v))(block) + def is(v: T, vr: T*)(block: => Any)(implicit sourceInfo: SourceInfo, compileOptions: CompileOptions): SwitchContext[T] = is(v :: vr.toList)(block) } /** Use to specify cases in a [[switch]] block, equivalent to a [[when$ when]] block comparing to diff --git a/src/test/scala/chiselTests/SwitchSpec.scala b/src/test/scala/chiselTests/SwitchSpec.scala index 78e5d59e..12bbb9e7 100644 --- a/src/test/scala/chiselTests/SwitchSpec.scala +++ b/src/test/scala/chiselTests/SwitchSpec.scala @@ -32,4 +32,22 @@ class SwitchSpec extends ChiselFlatSpec with Utils { }) } } + it should "provide useful source locators" in { + val chirrtl = ChiselStage.emitChirrtl(new Module { + val io = IO(new Bundle { + val in = Input(UInt(2.W)) + val out = Output(UInt(2.W)) + }) + + io.out := 0.U + switch (io.in) { + is (0.U) { io.out := 3.U } + is (1.U) { io.out := 0.U } + is (2.U) { io.out := 1.U } + is (3.U) { io.out := 3.U } + } + }) + + chirrtl should not include "Conditional.scala" + } } -- cgit v1.2.3