aboutsummaryrefslogtreecommitdiff
path: root/src/main/scala/firrtl/annotations/Target.scala
diff options
context:
space:
mode:
authorchick2020-08-14 19:47:53 -0700
committerJack Koenig2020-08-14 19:47:53 -0700
commit6fc742bfaf5ee508a34189400a1a7dbffe3f1cac (patch)
tree2ed103ee80b0fba613c88a66af854ae9952610ce /src/main/scala/firrtl/annotations/Target.scala
parentb516293f703c4de86397862fee1897aded2ae140 (diff)
All of src/ formatted with scalafmt
Diffstat (limited to 'src/main/scala/firrtl/annotations/Target.scala')
-rw-r--r--src/main/scala/firrtl/annotations/Target.scala291
1 files changed, 153 insertions, 138 deletions
diff --git a/src/main/scala/firrtl/annotations/Target.scala b/src/main/scala/firrtl/annotations/Target.scala
index 4d1cdc2f..afde84dc 100644
--- a/src/main/scala/firrtl/annotations/Target.scala
+++ b/src/main/scala/firrtl/annotations/Target.scala
@@ -4,7 +4,7 @@ package firrtl
package annotations
import firrtl.ir.{Field => _, _}
-import firrtl.Utils.{sub_type, field_type}
+import firrtl.Utils.{field_type, sub_type}
import AnnotationUtils.{toExp, validComponentName, validModuleName}
import TargetToken._
@@ -29,27 +29,29 @@ sealed trait Target extends Named {
def tokens: Seq[TargetToken]
/** @return Returns a new [[GenericTarget]] with new values */
- def modify(circuitOpt: Option[String] = circuitOpt,
- moduleOpt: Option[String] = moduleOpt,
- tokens: Seq[TargetToken] = tokens): GenericTarget = GenericTarget(circuitOpt, moduleOpt, tokens.toVector)
+ def modify(
+ circuitOpt: Option[String] = circuitOpt,
+ moduleOpt: Option[String] = moduleOpt,
+ tokens: Seq[TargetToken] = tokens
+ ): GenericTarget = GenericTarget(circuitOpt, moduleOpt, tokens.toVector)
/** @return Human-readable serialization */
def serialize: String = {
val circuitString = "~" + circuitOpt.getOrElse("???")
val moduleString = "|" + moduleOpt.getOrElse("???")
val tokensString = tokens.map {
- case Ref(r) => s">$r"
- case Instance(i) => s"/$i"
- case OfModule(o) => s":$o"
+ case Ref(r) => s">$r"
+ case Instance(i) => s"/$i"
+ case OfModule(o) => s":$o"
case TargetToken.Field(f) => s".$f"
- case Index(v) => s"[$v]"
- case Clock => s"@clock"
- case Reset => s"@reset"
- case Init => s"@init"
+ case Index(v) => s"[$v]"
+ case Clock => s"@clock"
+ case Reset => s"@reset"
+ case Init => s"@init"
}.mkString("")
- if(moduleOpt.isEmpty && tokens.isEmpty) {
+ if (moduleOpt.isEmpty && tokens.isEmpty) {
circuitString
- } else if(tokens.isEmpty) {
+ } else if (tokens.isEmpty) {
circuitString + moduleString
} else {
circuitString + moduleString + tokensString
@@ -64,24 +66,23 @@ sealed trait Target extends Named {
val moduleString = s"""\n$tab└── module ${moduleOpt.getOrElse("???")}:"""
var depth = 4
val tokenString = tokens.map {
- case Ref(r) => val rx = s"""\n$tab${" "*depth}└── $r"""; depth += 4; rx
- case Instance(i) => val ix = s"""\n$tab${" "*depth}└── inst $i """; ix
+ case Ref(r) => val rx = s"""\n$tab${" " * depth}└── $r"""; depth += 4; rx
+ case Instance(i) => val ix = s"""\n$tab${" " * depth}└── inst $i """; ix
case OfModule(o) => val ox = s"of $o:"; depth += 4; ox
- case Field(f) => s".$f"
- case Index(v) => s"[$v]"
- case Clock => s"@clock"
- case Reset => s"@reset"
- case Init => s"@init"
+ case Field(f) => s".$f"
+ case Index(v) => s"[$v]"
+ case Clock => s"@clock"
+ case Reset => s"@reset"
+ case Init => s"@init"
}.mkString("")
(moduleOpt.isEmpty, tokens.isEmpty) match {
case (true, true) => circuitString
- case (_, true) => circuitString + moduleString
- case (_, _) => circuitString + moduleString + tokenString
+ case (_, true) => circuitString + moduleString
+ case (_, _) => circuitString + moduleString + tokenString
}
}
-
/** @return Converts this [[Target]] into a [[GenericTarget]] */
def toGenericTarget: GenericTarget = GenericTarget(circuitOpt, moduleOpt, tokens.toVector)
@@ -113,13 +114,13 @@ sealed trait Target extends Named {
object Target {
def asTarget(m: ModuleTarget)(e: Expression): ReferenceTarget = e match {
case r: ir.Reference => m.ref(r.name)
- case s: ir.SubIndex => asTarget(m)(s.expr).index(s.value)
- case s: ir.SubField => asTarget(m)(s.expr).field(s.name)
+ case s: ir.SubIndex => asTarget(m)(s.expr).index(s.value)
+ case s: ir.SubField => asTarget(m)(s.expr).field(s.name)
case s: ir.SubAccess => asTarget(m)(s.expr).field("@" + s.index.serialize)
- case d: DoPrim => m.ref("@" + d.serialize)
- case d: Mux => m.ref("@" + d.serialize)
- case d: ValidIf => m.ref("@" + d.serialize)
- case d: Literal => m.ref("@" + d.serialize)
+ case d: DoPrim => m.ref("@" + d.serialize)
+ case d: Mux => m.ref("@" + d.serialize)
+ case d: ValidIf => m.ref("@" + d.serialize)
+ case d: Literal => m.ref("@" + d.serialize)
case other => sys.error(s"Unsupported: $other")
}
@@ -131,14 +132,14 @@ object Target {
case class NamedException(message: String) extends Exception(message)
- implicit def convertCircuitTarget2CircuitName(c: CircuitTarget): CircuitName = c.toNamed
- implicit def convertModuleTarget2ModuleName(c: ModuleTarget): ModuleName = c.toNamed
- implicit def convertIsComponent2ComponentName(c: IsComponent): ComponentName = c.toNamed
- implicit def convertTarget2Named(c: Target): Named = c.toNamed
- implicit def convertCircuitName2CircuitTarget(c: CircuitName): CircuitTarget = c.toTarget
- implicit def convertModuleName2ModuleTarget(c: ModuleName): ModuleTarget = c.toTarget
+ implicit def convertCircuitTarget2CircuitName(c: CircuitTarget): CircuitName = c.toNamed
+ implicit def convertModuleTarget2ModuleName(c: ModuleTarget): ModuleName = c.toNamed
+ implicit def convertIsComponent2ComponentName(c: IsComponent): ComponentName = c.toNamed
+ implicit def convertTarget2Named(c: Target): Named = c.toNamed
+ implicit def convertCircuitName2CircuitTarget(c: CircuitName): CircuitTarget = c.toTarget
+ implicit def convertModuleName2ModuleTarget(c: ModuleName): ModuleTarget = c.toTarget
implicit def convertComponentName2ReferenceTarget(c: ComponentName): ReferenceTarget = c.toTarget
- implicit def convertNamed2Target(n: Named): CompleteTarget = n.toTarget
+ implicit def convertNamed2Target(n: Named): CompleteTarget = n.toTarget
/** Converts [[ComponentName]]'s name into TargetTokens
* @param name
@@ -148,7 +149,7 @@ object Target {
val tokens = AnnotationUtils.tokenize(name)
val subComps = mutable.ArrayBuffer[TargetToken]()
subComps += Ref(tokens.head)
- if(tokens.tail.nonEmpty) {
+ if (tokens.tail.nonEmpty) {
tokens.tail.zip(tokens.tail.tail).foreach {
case (".", value: String) => subComps += Field(value)
case ("[", value: String) => subComps += Index(value.toInt)
@@ -163,31 +164,33 @@ object Target {
* @param keywords
* @return
*/
- def isOnly(seq: Seq[TargetToken], keywords:String*): Boolean = {
- seq.map(_.is(keywords:_*)).foldLeft(false)(_ || _) && keywords.nonEmpty
+ def isOnly(seq: Seq[TargetToken], keywords: String*): Boolean = {
+ seq.map(_.is(keywords: _*)).foldLeft(false)(_ || _) && keywords.nonEmpty
}
/** @return [[Target]] from human-readable serialization */
def deserialize(s: String): Target = {
val regex = """(?=[~|>/:.\[@])"""
- s.split(regex).foldLeft(GenericTarget(None, None, Vector.empty)) { (t, tokenString) =>
- val value = tokenString.tail
- tokenString(0) match {
- case '~' if t.circuitOpt.isEmpty && t.moduleOpt.isEmpty && t.tokens.isEmpty =>
- if(value == "???") t else t.copy(circuitOpt = Some(value))
- case '|' if t.moduleOpt.isEmpty && t.tokens.isEmpty =>
- if(value == "???") t else t.copy(moduleOpt = Some(value))
- case '/' => t.add(Instance(value))
- case ':' => t.add(OfModule(value))
- case '>' => t.add(Ref(value))
- case '.' => t.add(Field(value))
- case '[' if value.dropRight(1).toInt >= 0 => t.add(Index(value.dropRight(1).toInt))
- case '@' if value == "clock" => t.add(Clock)
- case '@' if value == "init" => t.add(Init)
- case '@' if value == "reset" => t.add(Reset)
- case other => throw NamedException(s"Cannot deserialize Target: $s")
+ s.split(regex)
+ .foldLeft(GenericTarget(None, None, Vector.empty)) { (t, tokenString) =>
+ val value = tokenString.tail
+ tokenString(0) match {
+ case '~' if t.circuitOpt.isEmpty && t.moduleOpt.isEmpty && t.tokens.isEmpty =>
+ if (value == "???") t else t.copy(circuitOpt = Some(value))
+ case '|' if t.moduleOpt.isEmpty && t.tokens.isEmpty =>
+ if (value == "???") t else t.copy(moduleOpt = Some(value))
+ case '/' => t.add(Instance(value))
+ case ':' => t.add(OfModule(value))
+ case '>' => t.add(Ref(value))
+ case '.' => t.add(Field(value))
+ case '[' if value.dropRight(1).toInt >= 0 => t.add(Index(value.dropRight(1).toInt))
+ case '@' if value == "clock" => t.add(Clock)
+ case '@' if value == "init" => t.add(Init)
+ case '@' if value == "reset" => t.add(Reset)
+ case other => throw NamedException(s"Cannot deserialize Target: $s")
+ }
}
- }.tryToComplete
+ .tryToComplete
}
/** Returns the module that a [[Target]] "refers" to.
@@ -217,14 +220,16 @@ object Target {
def getReferenceTarget(t: Target): Target = {
(t.toGenericTarget match {
case t: GenericTarget if t.isLegal =>
- val newTokens = t.tokens.reverse.dropWhile({
- case x: Field => true
- case x: Index => true
- case Clock => true
- case Init => true
- case Reset => true
- case other => false
- }).reverse
+ val newTokens = t.tokens.reverse
+ .dropWhile({
+ case x: Field => true
+ case x: Index => true
+ case Clock => true
+ case Init => true
+ case Reset => true
+ case other => false
+ })
+ .reverse
GenericTarget(t.circuitOpt, t.moduleOpt, newTokens)
case other => sys.error(s"Can't make $other pathless!")
}).tryToComplete
@@ -236,9 +241,8 @@ object Target {
* @param moduleOpt Optional module name
* @param tokens [[TargetToken]]s to represent the target in a circuit and module
*/
-case class GenericTarget(circuitOpt: Option[String],
- moduleOpt: Option[String],
- tokens: Vector[TargetToken]) extends Target {
+case class GenericTarget(circuitOpt: Option[String], moduleOpt: Option[String], tokens: Vector[TargetToken])
+ extends Target {
override def toGenericTarget: GenericTarget = this
@@ -252,11 +256,12 @@ case class GenericTarget(circuitOpt: Option[String],
override def toTarget: CompleteTarget = getComplete.get
override def getComplete: Option[CompleteTarget] = {
- if(!isComplete) None else {
+ if (!isComplete) None
+ else {
val target = this match {
- case GenericTarget(Some(c), None, Vector()) => CircuitTarget(c)
- case GenericTarget(Some(c), Some(m), Vector()) => ModuleTarget(c, m)
- case GenericTarget(Some(c), Some(m), Ref(r) +: component) => ReferenceTarget(c, m, Nil, r, component)
+ case GenericTarget(Some(c), None, Vector()) => CircuitTarget(c)
+ case GenericTarget(Some(c), Some(m), Vector()) => ModuleTarget(c, m)
+ case GenericTarget(Some(c), Some(m), Ref(r) +: component) => ReferenceTarget(c, m, Nil, r, component)
case GenericTarget(Some(c), Some(m), Instance(i) +: OfModule(o) +: Vector()) => InstanceTarget(c, m, Nil, i, o)
case GenericTarget(Some(c), Some(m), component) =>
val path = getPath.getOrElse(Nil)
@@ -271,7 +276,7 @@ case class GenericTarget(circuitOpt: Option[String],
override def isLocal: Boolean = !(getPath.nonEmpty && getPath.get.nonEmpty)
- def path: Vector[(Instance, OfModule)] = if(isComplete){
+ def path: Vector[(Instance, OfModule)] = if (isComplete) {
tokens.zip(tokens.tail).collect {
case (i: Instance, o: OfModule) => (i, o)
}
@@ -280,9 +285,9 @@ case class GenericTarget(circuitOpt: Option[String],
/** If complete, return this [[GenericTarget]]'s path
* @return
*/
- def getPath: Option[Seq[(Instance, OfModule)]] = if(isComplete) {
- val allInstOfs = tokens.grouped(2).collect { case Seq(i: Instance, o:OfModule) => (i, o)}.toSeq
- if(tokens.nonEmpty && tokens.last.isInstanceOf[OfModule]) Some(allInstOfs.dropRight(1)) else Some(allInstOfs)
+ def getPath: Option[Seq[(Instance, OfModule)]] = if (isComplete) {
+ val allInstOfs = tokens.grouped(2).collect { case Seq(i: Instance, o: OfModule) => (i, o) }.toSeq
+ if (tokens.nonEmpty && tokens.last.isInstanceOf[OfModule]) Some(allInstOfs.dropRight(1)) else Some(allInstOfs)
} else {
None
}
@@ -290,7 +295,7 @@ case class GenericTarget(circuitOpt: Option[String],
/** If complete and a reference, return the reference and subcomponents
* @return
*/
- def getRef: Option[(String, Seq[TargetToken])] = if(isComplete) {
+ def getRef: Option[(String, Seq[TargetToken])] = if (isComplete) {
val (optRef, comps) = tokens.foldLeft((None: Option[String], Vector.empty[TargetToken])) {
case ((None, v), Ref(r)) => (Some(r), v)
case ((r: Some[String], comps), c) => (r, comps :+ c)
@@ -304,7 +309,7 @@ case class GenericTarget(circuitOpt: Option[String],
/** If complete and an instance target, return the instance and ofmodule
* @return
*/
- def getInstanceOf: Option[(String, String)] = if(isComplete) {
+ def getInstanceOf: Option[(String, String)] = if (isComplete) {
tokens.grouped(2).foldLeft(None: Option[(String, String)]) {
case (instOf, Seq(i: Instance, o: OfModule)) => Some((i.value, o.value))
case (instOf, _) => None
@@ -328,14 +333,14 @@ case class GenericTarget(circuitOpt: Option[String],
*/
def add(token: TargetToken): GenericTarget = {
token match {
- case _: Instance => requireLast(true, "inst", "of")
- case _: OfModule => requireLast(false, "inst")
- case _: Ref => requireLast(true, "inst", "of")
- case _: Field => requireLast(true, "ref", "[]", ".", "init", "clock", "reset")
- case _: Index => requireLast(true, "ref", "[]", ".", "init", "clock", "reset")
- case Init => requireLast(true, "ref", "[]", ".", "init", "clock", "reset")
- case Clock => requireLast(true, "ref", "[]", ".", "init", "clock", "reset")
- case Reset => requireLast(true, "ref", "[]", ".", "init", "clock", "reset")
+ case _: Instance => requireLast(true, "inst", "of")
+ case _: OfModule => requireLast(false, "inst")
+ case _: Ref => requireLast(true, "inst", "of")
+ case _: Field => requireLast(true, "ref", "[]", ".", "init", "clock", "reset")
+ case _: Index => requireLast(true, "ref", "[]", ".", "init", "clock", "reset")
+ case Init => requireLast(true, "ref", "[]", ".", "init", "clock", "reset")
+ case Clock => requireLast(true, "ref", "[]", ".", "init", "clock", "reset")
+ case Reset => requireLast(true, "ref", "[]", ".", "init", "clock", "reset")
}
this.copy(tokens = tokens :+ token)
}
@@ -345,7 +350,7 @@ case class GenericTarget(circuitOpt: Option[String],
/** Optionally tries to append token to tokens, fails return is not a legal Target */
def optAdd(token: TargetToken): Option[Target] = {
- try{
+ try {
Some(add(token))
} catch {
case _: IllegalArgumentException => None
@@ -358,7 +363,7 @@ case class GenericTarget(circuitOpt: Option[String],
def isLegal: Boolean = {
try {
var comp: GenericTarget = this.copy(tokens = Vector.empty)
- for(token <- tokens) {
+ for (token <- tokens) {
comp = comp.add(token)
}
true
@@ -374,19 +379,18 @@ case class GenericTarget(circuitOpt: Option[String],
def isComplete: Boolean = {
isLegal && (isCircuitTarget || isModuleTarget || (isComponentTarget && tokens.tails.forall {
case Instance(_) +: OfModule(_) +: tail => true
- case Instance(_) +: x +: tail => false
- case x +: OfModule(_) +: tail => false
- case _ => true
- } ))
+ case Instance(_) +: x +: tail => false
+ case x +: OfModule(_) +: tail => false
+ case _ => true
+ }))
}
-
- def isCircuitTarget: Boolean = circuitOpt.nonEmpty && moduleOpt.isEmpty && tokens.isEmpty
- def isModuleTarget: Boolean = circuitOpt.nonEmpty && moduleOpt.nonEmpty && tokens.isEmpty
+ def isCircuitTarget: Boolean = circuitOpt.nonEmpty && moduleOpt.isEmpty && tokens.isEmpty
+ def isModuleTarget: Boolean = circuitOpt.nonEmpty && moduleOpt.nonEmpty && tokens.isEmpty
def isComponentTarget: Boolean = circuitOpt.nonEmpty && moduleOpt.nonEmpty && tokens.nonEmpty
lazy val (parentModule: Option[String], astModule: Option[String]) = path match {
- case Seq() => (None, moduleOpt)
+ case Seq() => (None, moduleOpt)
case Seq((i, OfModule(o))) => (moduleOpt, Some(o))
case seq if seq.size > 1 =>
val reversed = seq.reverse
@@ -421,7 +425,6 @@ trait CompleteTarget extends Target {
override def toString: String = serialize
}
-
/** A member of a FIRRTL Circuit (e.g. cannot point to a CircuitTarget)
* Concrete Subclasses are: [[ModuleTarget]], [[InstanceTarget]], and [[ReferenceTarget]]
*/
@@ -456,10 +459,12 @@ trait IsMember extends CompleteTarget {
/** @return List of local Instance Targets refering to each instance/ofModule in this member's path */
def pathAsTargets: Seq[InstanceTarget] = {
- path.foldLeft((module, Vector.empty[InstanceTarget])) {
- case ((m, vec), (Instance(i), OfModule(o))) =>
- (o, vec :+ InstanceTarget(circuit, m, Nil, i, o))
- }._2
+ path
+ .foldLeft((module, Vector.empty[InstanceTarget])) {
+ case ((m, vec), (Instance(i), OfModule(o))) =>
+ (o, vec :+ InstanceTarget(circuit, m, Nil, i, o))
+ }
+ ._2
}
/** Resets this target to have a new path
@@ -469,7 +474,7 @@ trait IsMember extends CompleteTarget {
def setPathTarget(newPath: IsModule): CompleteTarget
/** @return The [[ModuleTarget]] of the module that directly contains this component */
- def encapsulatingModule: String = if(path.isEmpty) module else path.last._2.value
+ def encapsulatingModule: String = if (path.isEmpty) module else path.last._2.value
def encapsulatingModuleTarget: ModuleTarget = ModuleTarget(circuit, encapsulatingModule)
@@ -492,6 +497,7 @@ trait IsModule extends IsMember {
/** A component of a FIRRTL Module (e.g. cannot point to a CircuitTarget or ModuleTarget)
*/
trait IsComponent extends IsMember {
+
/** Removes n levels of instance hierarchy
*
* Example: n=1, transforms (Top, A)/b:B/c:C -> (Top, B)/c:C
@@ -501,13 +507,13 @@ trait IsComponent extends IsMember {
def stripHierarchy(n: Int): IsMember
override def toNamed: ComponentName = {
- if(isLocal){
+ if (isLocal) {
val mn = ModuleName(module, CircuitName(circuit))
- Seq(tokens:_*) match {
+ Seq(tokens: _*) match {
case Seq(Ref(name)) => ComponentName(name, mn)
case Ref(_) :: tail if Target.isOnly(tail, ".", "[]") =>
- val name = tokens.foldLeft(""){
- case ("", Ref(name)) => name
+ val name = tokens.foldLeft("") {
+ case ("", Ref(name)) => name
case (string, Field(value)) => s"$string.$value"
case (string, Index(value)) => s"$string[$value]"
}
@@ -524,7 +530,8 @@ trait IsComponent extends IsMember {
}
override def pathTarget: IsModule = {
- if(path.isEmpty) moduleTarget else {
+ if (path.isEmpty) moduleTarget
+ else {
val (i, o) = path.last
InstanceTarget(circuit, module, path.dropRight(1), i.value, o.value)
}
@@ -535,7 +542,6 @@ trait IsComponent extends IsMember {
override def isLocal = path.isEmpty
}
-
/** Target pointing to a FIRRTL [[firrtl.ir.Circuit]]
* @param circuit Name of a FIRRTL circuit
*/
@@ -577,7 +583,8 @@ case class ModuleTarget(circuit: String, module: String) extends IsModule {
override def targetParent: CircuitTarget = CircuitTarget(circuit)
- override def addHierarchy(root: String, instance: String): InstanceTarget = InstanceTarget(circuit, root, Nil, instance, module)
+ override def addHierarchy(root: String, instance: String): InstanceTarget =
+ InstanceTarget(circuit, root, Nil, instance, module)
override def ref(value: String): ReferenceTarget = ReferenceTarget(circuit, module, Nil, value, Nil)
@@ -613,11 +620,13 @@ case class ModuleTarget(circuit: String, module: String) extends IsModule {
* @param ref Name of component
* @param component Subcomponent of this reference, e.g. field or index
*/
-case class ReferenceTarget(circuit: String,
- module: String,
- override val path: Seq[(Instance, OfModule)],
- ref: String,
- component: Seq[TargetToken]) extends IsComponent {
+case class ReferenceTarget(
+ circuit: String,
+ module: String,
+ override val path: Seq[(Instance, OfModule)],
+ ref: String,
+ component: Seq[TargetToken])
+ extends IsComponent {
/** @param value Index value of this target
* @return A new [[ReferenceTarget]] to the specified index of this [[ReferenceTarget]]
@@ -648,7 +657,7 @@ case class ReferenceTarget(circuit: String,
baseType
} else {
val headType = tokens.head match {
- case Index(idx) => sub_type(baseType)
+ case Index(idx) => sub_type(baseType)
case Field(field) => field_type(baseType, field)
case _: Ref => baseType
}
@@ -662,7 +671,8 @@ case class ReferenceTarget(circuit: String,
override def targetParent: CompleteTarget = component match {
case Nil =>
- if(path.isEmpty) moduleTarget else {
+ if (path.isEmpty) moduleTarget
+ else {
val (i, o) = path.last
InstanceTarget(circuit, module, path.dropRight(1), i.value, o.value)
}
@@ -676,7 +686,8 @@ case class ReferenceTarget(circuit: String,
override def stripHierarchy(n: Int): ReferenceTarget = {
require(path.size >= n, s"Cannot strip $n levels of hierarchy from $this")
- if(n == 0) this else {
+ if (n == 0) this
+ else {
val newModule = path(n - 1)._2.value
ReferenceTarget(circuit, newModule, path.drop(n), ref, component)
}
@@ -700,15 +711,15 @@ case class ReferenceTarget(circuit: String,
def leafSubTargets(tpe: firrtl.ir.Type): Seq[ReferenceTarget] = tpe match {
case _: firrtl.ir.GroundType => Vector(this)
case firrtl.ir.VectorType(t, size) => (0 until size).flatMap { i => index(i).leafSubTargets(t) }
- case firrtl.ir.BundleType(fields) => fields.flatMap { f => field(f.name).leafSubTargets(f.tpe)}
- case other => sys.error(s"Error! Unexpected type $other")
+ case firrtl.ir.BundleType(fields) => fields.flatMap { f => field(f.name).leafSubTargets(f.tpe) }
+ case other => sys.error(s"Error! Unexpected type $other")
}
def allSubTargets(tpe: firrtl.ir.Type): Seq[ReferenceTarget] = tpe match {
case _: firrtl.ir.GroundType => Vector(this)
case firrtl.ir.VectorType(t, size) => this +: (0 until size).flatMap { i => index(i).allSubTargets(t) }
- case firrtl.ir.BundleType(fields) => this +: fields.flatMap { f => field(f.name).allSubTargets(f.tpe)}
- case other => sys.error(s"Error! Unexpected type $other")
+ case firrtl.ir.BundleType(fields) => this +: fields.flatMap { f => field(f.name).allSubTargets(f.tpe) }
+ case other => sys.error(s"Error! Unexpected type $other")
}
override def leafModule: String = encapsulatingModule
@@ -721,11 +732,14 @@ case class ReferenceTarget(circuit: String,
* @param instance Name of the instance
* @param ofModule Name of the instance's module
*/
-case class InstanceTarget(circuit: String,
- module: String,
- override val path: Seq[(Instance, OfModule)],
- instance: String,
- ofModule: String) extends IsModule with IsComponent {
+case class InstanceTarget(
+ circuit: String,
+ module: String,
+ override val path: Seq[(Instance, OfModule)],
+ instance: String,
+ ofModule: String)
+ extends IsModule
+ with IsComponent {
/** @return a [[ReferenceTarget]] referring to this declaration of this instance */
def asReference: ReferenceTarget = ReferenceTarget(circuit, module, path, instance, Nil)
@@ -744,7 +758,8 @@ case class InstanceTarget(circuit: String,
override def moduleOpt: Option[String] = Some(module)
override def targetParent: IsModule = {
- if(isLocal) ModuleTarget(circuit, module) else {
+ if (isLocal) ModuleTarget(circuit, module)
+ else {
val (newInstance, newOfModule) = path.last
InstanceTarget(circuit, module, path.dropRight(1), newInstance.value, newOfModule.value)
}
@@ -759,8 +774,9 @@ case class InstanceTarget(circuit: String,
override def stripHierarchy(n: Int): IsModule = {
require(path.size + 1 >= n, s"Cannot strip $n levels of hierarchy from $this")
- if(n == 0) this else {
- if(path.size < n){
+ if (n == 0) this
+ else {
+ if (path.size < n) {
ModuleTarget(circuit, ofModule)
} else {
val newModule = path(n - 1)._2.value
@@ -769,7 +785,7 @@ case class InstanceTarget(circuit: String,
}
}
- override def asPath: Seq[(Instance, OfModule)] = path :+( (Instance(instance), OfModule(ofModule)) )
+ override def asPath: Seq[(Instance, OfModule)] = path :+ ((Instance(instance), OfModule(ofModule)))
override def pathlessTarget: InstanceTarget = InstanceTarget(circuit, encapsulatingModule, Nil, instance, ofModule)
@@ -781,33 +797,32 @@ case class InstanceTarget(circuit: String,
override def leafModule: String = ofModule
}
-
/** Named classes associate an annotation with a component in a Firrtl circuit */
sealed trait Named {
def serialize: String
- def toTarget: CompleteTarget
+ def toTarget: CompleteTarget
}
final case class CircuitName(name: String) extends Named {
- if(!validModuleName(name)) throw AnnotationException(s"Illegal circuit name: $name")
+ if (!validModuleName(name)) throw AnnotationException(s"Illegal circuit name: $name")
def serialize: String = name
- def toTarget: CircuitTarget = CircuitTarget(name)
+ def toTarget: CircuitTarget = CircuitTarget(name)
}
final case class ModuleName(name: String, circuit: CircuitName) extends Named {
- if(!validModuleName(name)) throw AnnotationException(s"Illegal module name: $name")
+ if (!validModuleName(name)) throw AnnotationException(s"Illegal module name: $name")
def serialize: String = circuit.serialize + "." + name
- def toTarget: ModuleTarget = ModuleTarget(circuit.name, name)
+ def toTarget: ModuleTarget = ModuleTarget(circuit.name, name)
}
final case class ComponentName(name: String, module: ModuleName) extends Named {
- if(!validComponentName(name)) throw AnnotationException(s"Illegal component name: $name")
- def expr: Expression = toExp(name)
+ if (!validComponentName(name)) throw AnnotationException(s"Illegal component name: $name")
+ def expr: Expression = toExp(name)
def serialize: String = module.serialize + "." + name
def toTarget: ReferenceTarget = {
Target.toTargetTokens(name).toList match {
case Ref(r) :: components => ReferenceTarget(module.circuit.name, module.name, Nil, r, components)
- case other => throw Target.NamedException(s"Cannot convert $this into [[ReferenceTarget]]: $other")
+ case other => throw Target.NamedException(s"Cannot convert $this into [[ReferenceTarget]]: $other")
}
}
}