summaryrefslogtreecommitdiff
path: root/chiselFrontend/src/main/scala/chisel3/core/StrongEnum.scala
diff options
context:
space:
mode:
authorRichard Lin2019-01-22 15:06:08 -0800
committerGitHub2019-01-22 15:06:08 -0800
commitb39bc25ff76762d93300975e533ec3bd4f119d1c (patch)
tree38a8219d1c8ef6c61fd667a3f46a189091e2ea20 /chiselFrontend/src/main/scala/chisel3/core/StrongEnum.scala
parent40f4d3edf93cf48294bd6555a1deaa07571b2d3d (diff)
Define Data .toString (#985)
toString on Data subtypes will now print the type and optionally binding information including literals and IO names as feasible.
Diffstat (limited to 'chiselFrontend/src/main/scala/chisel3/core/StrongEnum.scala')
-rw-r--r--chiselFrontend/src/main/scala/chisel3/core/StrongEnum.scala17
1 files changed, 17 insertions, 0 deletions
diff --git a/chiselFrontend/src/main/scala/chisel3/core/StrongEnum.scala b/chiselFrontend/src/main/scala/chisel3/core/StrongEnum.scala
index 889e9b11..8feb0541 100644
--- a/chiselFrontend/src/main/scala/chisel3/core/StrongEnum.scala
+++ b/chiselFrontend/src/main/scala/chisel3/core/StrongEnum.scala
@@ -33,6 +33,19 @@ import EnumAnnotations._
abstract class EnumType(private val factory: EnumFactory, selfAnnotating: Boolean = false) extends Element {
+ override def toString: String = {
+ val bindingString = litOption match {
+ case Some(value) => factory.nameOfValue(value) match {
+ case Some(name) => s"($value=$name)"
+ case None => s"($value=(invalid))"
+ }
+ case _ => bindingToString
+ }
+ // Use getSimpleName instead of enumTypeName because for debugging purposes the fully qualified name isn't
+ // necessary (compared to for the Enum annotation), and it's more consistent with Bundle printing.
+ s"${factory.getClass.getSimpleName.init}$bindingString"
+ }
+
override def cloneType: this.type = factory().asInstanceOf[this.type]
private[core] def compop(sourceInfo: SourceInfo, op: PrimOp, other: EnumType): Bool = {
@@ -151,6 +164,10 @@ abstract class EnumFactory {
def all: Seq[Type] = enumInstances
+ private[chisel3] def nameOfValue(id: BigInt): Option[String] = {
+ enum_records.find(_.inst.litValue() == id).map(_.name)
+ }
+
protected def Value: Type = macro EnumMacros.ValImpl
protected def Value(id: UInt): Type = macro EnumMacros.ValCustomImpl