aboutsummaryrefslogtreecommitdiff
path: root/src/main
diff options
context:
space:
mode:
Diffstat (limited to 'src/main')
-rw-r--r--src/main/scala/firrtl/ir/IR.scala25
1 files changed, 23 insertions, 2 deletions
diff --git a/src/main/scala/firrtl/ir/IR.scala b/src/main/scala/firrtl/ir/IR.scala
index af3709cd..a47a4cea 100644
--- a/src/main/scala/firrtl/ir/IR.scala
+++ b/src/main/scala/firrtl/ir/IR.scala
@@ -421,8 +421,29 @@ object Block {
}
case class Block(stmts: Seq[Statement]) extends Statement {
- def serialize: String = stmts map (_.serialize) mkString "\n"
- def mapStmt(f: Statement => Statement): Statement = Block(stmts map f)
+ def serialize: String = {
+ val res = stmts.view.map(_.serialize).mkString("\n")
+ if (res.nonEmpty) res else EmptyStmt.serialize
+ }
+ def mapStmt(f: Statement => Statement): Statement = {
+ val res = new scala.collection.mutable.ArrayBuffer[Statement]()
+ var its = stmts.iterator :: Nil
+ while (its.nonEmpty) {
+ val it = its.head
+ if (it.hasNext) {
+ it.next() match {
+ case EmptyStmt => // flatten out
+ case b: Block =>
+ its = b.stmts.iterator :: its
+ case other =>
+ res.append(f(other))
+ }
+ } else {
+ its = its.tail
+ }
+ }
+ Block(res)
+ }
def mapExpr(f: Expression => Expression): Statement = this
def mapType(f: Type => Type): Statement = this
def mapString(f: String => String): Statement = this