summaryrefslogtreecommitdiff
path: root/chiselFrontend/src/main/scala/chisel3/core/StrongEnum.scala
diff options
context:
space:
mode:
authorJim Lawson2019-03-18 12:17:33 -0700
committerGitHub2019-03-18 12:17:33 -0700
commit2c449c5d6e23dcbb60e8c64cab6b6f4ba6ae313f (patch)
tree3daffa8eb0f57faf31d3977700be38f5be31e59a /chiselFrontend/src/main/scala/chisel3/core/StrongEnum.scala
parentcfb2f08db9d9df121a82f138dd71297dbcea66cc (diff)
Split #974 into two PRs - scalastyle updates (#1037)
* Update style warnings now that subprojects are aggregated. Use "scalastyle-test-config.xml" for scalastyle config in tests. Enable "_" in method names and accept method names ending in "_=". Re-sync scalastyle-test-config.xml with scalastyle-config.xml * Remove bogus tests that crept in with git add * Add missing import.
Diffstat (limited to 'chiselFrontend/src/main/scala/chisel3/core/StrongEnum.scala')
-rw-r--r--chiselFrontend/src/main/scala/chisel3/core/StrongEnum.scala35
1 files changed, 22 insertions, 13 deletions
diff --git a/chiselFrontend/src/main/scala/chisel3/core/StrongEnum.scala b/chiselFrontend/src/main/scala/chisel3/core/StrongEnum.scala
index 8feb0541..f9414901 100644
--- a/chiselFrontend/src/main/scala/chisel3/core/StrongEnum.scala
+++ b/chiselFrontend/src/main/scala/chisel3/core/StrongEnum.scala
@@ -16,11 +16,11 @@ import firrtl.annotations._
object EnumAnnotations {
case class EnumComponentAnnotation(target: Named, enumTypeName: String) extends SingleTargetAnnotation[Named] {
- def duplicate(n: Named) = this.copy(target = n)
+ def duplicate(n: Named): EnumComponentAnnotation = this.copy(target = n)
}
case class EnumComponentChiselAnnotation(target: InstanceId, enumTypeName: String) extends ChiselAnnotation {
- def toFirrtl = EnumComponentAnnotation(target.toNamed, enumTypeName)
+ def toFirrtl: EnumComponentAnnotation = EnumComponentAnnotation(target.toNamed, enumTypeName)
}
case class EnumDefAnnotation(enumTypeName: String, definition: Map[String, BigInt]) extends NoTargetAnnotation
@@ -52,8 +52,9 @@ abstract class EnumType(private val factory: EnumFactory, selfAnnotating: Boolea
requireIsHardware(this, "bits operated on")
requireIsHardware(other, "bits operated on")
- if(!this.typeEquivalent(other))
+ if(!this.typeEquivalent(other)) {
throwException(s"Enum types are not equivalent: ${this.enumTypeName}, ${other.enumTypeName}")
+ }
pushOp(DefPrim(sourceInfo, Bool(), op, this.ref, other.ref))
}
@@ -75,12 +76,14 @@ abstract class EnumType(private val factory: EnumFactory, selfAnnotating: Boolea
final def > (that: EnumType): Bool = macro SourceInfoTransform.thatArg
final def >= (that: EnumType): Bool = macro SourceInfoTransform.thatArg
+ // scalastyle:off line.size.limit method.name
def do_=== (that: EnumType)(implicit sourceInfo: SourceInfo, compileOptions: CompileOptions): Bool = compop(sourceInfo, EqualOp, that)
def do_=/= (that: EnumType)(implicit sourceInfo: SourceInfo, compileOptions: CompileOptions): Bool = compop(sourceInfo, NotEqualOp, that)
def do_< (that: EnumType)(implicit sourceInfo: SourceInfo, compileOptions: CompileOptions): Bool = compop(sourceInfo, LessOp, that)
def do_> (that: EnumType)(implicit sourceInfo: SourceInfo, compileOptions: CompileOptions): Bool = compop(sourceInfo, GreaterOp, that)
def do_<= (that: EnumType)(implicit sourceInfo: SourceInfo, compileOptions: CompileOptions): Bool = compop(sourceInfo, LessEqOp, that)
def do_>= (that: EnumType)(implicit sourceInfo: SourceInfo, compileOptions: CompileOptions): Bool = compop(sourceInfo, GreaterEqOp, that)
+ // scalastyle:on line.size.limit method.name
override def do_asUInt(implicit sourceInfo: SourceInfo, compileOptions: CompileOptions): UInt =
pushOp(DefPrim(sourceInfo, UInt(width), AsUIntOp, ref))
@@ -99,10 +102,11 @@ abstract class EnumType(private val factory: EnumFactory, selfAnnotating: Boolea
if (litOption.isDefined) {
val index = factory.all.indexOf(this)
- if (index < factory.all.length-1)
- factory.all(index+1).asInstanceOf[this.type]
- else
+ if (index < factory.all.length-1) {
+ factory.all(index + 1).asInstanceOf[this.type]
+ } else {
factory.all.head.asInstanceOf[this.type]
+ }
} else {
val enums_with_nexts = factory.all zip (factory.all.tail :+ factory.all.head)
val next_enum = SeqUtils.priorityMux(enums_with_nexts.map { case (e,n) => (this === e, n) } )
@@ -168,8 +172,8 @@ abstract class EnumFactory {
enum_records.find(_.inst.litValue() == id).map(_.name)
}
- protected def Value: Type = macro EnumMacros.ValImpl
- protected def Value(id: UInt): Type = macro EnumMacros.ValCustomImpl
+ protected def Value: Type = macro EnumMacros.ValImpl // scalastyle:off method.name
+ protected def Value(id: UInt): Type = macro EnumMacros.ValCustomImpl // scalastyle:off method.name
protected def do_Value(names: Seq[String]): Type = {
val result = new Type
@@ -189,10 +193,12 @@ abstract class EnumFactory {
protected def do_Value(names: Seq[String], id: UInt): Type = {
// TODO: These throw ExceptionInInitializerError which can be confusing to the user. Get rid of the error, and just
// throw an exception
- if (id.litOption.isEmpty)
+ if (id.litOption.isEmpty) {
throwException(s"$enumTypeName defined with a non-literal type")
- if (id.litValue() < this.id)
+ }
+ if (id.litValue() < this.id) {
throwException(s"Enums must be strictly increasing: $enumTypeName")
+ }
this.id = id.litValue()
do_Value(names)
@@ -201,6 +207,7 @@ abstract class EnumFactory {
def apply(): Type = new Type
def apply(n: UInt)(implicit sourceInfo: SourceInfo, connectionCompileOptions: CompileOptions): Type = {
+ // scalastyle:off line.size.limit
if (n.litOption.isDefined) {
val result = enumInstances.find(_.litValue == n.litValue)
@@ -223,17 +230,18 @@ abstract class EnumFactory {
result
}
}
+ // scalastyle:on line.size.limit
}
private[core] object EnumMacros {
- def ValImpl(c: Context) : c.Tree = {
+ def ValImpl(c: Context) : c.Tree = { // scalastyle:off method.name
import c.universe._
val names = getNames(c)
q"""this.do_Value(Seq(..$names))"""
}
- def ValCustomImpl(c: Context)(id: c.Expr[UInt]) = {
+ def ValCustomImpl(c: Context)(id: c.Expr[UInt]): c.universe.Tree = { // scalastyle:off method.name
import c.universe._
val names = getNames(c)
q"""this.do_Value(Seq(..$names), $id)"""
@@ -249,8 +257,9 @@ private[core] object EnumMacros {
if rhs.pos == c.macroApplication.pos => name.decoded
}
- if (names.isEmpty)
+ if (names.isEmpty) {
c.abort(c.enclosingPosition, "Value cannot be called without assigning to an enum")
+ }
names
}