aboutsummaryrefslogtreecommitdiff
path: root/src/main
diff options
context:
space:
mode:
authorJack Koenig2020-06-10 12:47:44 -0700
committerGitHub2020-06-10 19:47:44 +0000
commit1e497ce39fd161c681ee64c0a4a882859eb8850b (patch)
treea0d02dce28ca6c15d1b372be109cd71dd424d2d8 /src/main
parenta7fe69ba40823215ed52ccb1895bd33e25eaed60 (diff)
Build ArrayBuffers in Block.mapStmt (#1669)
* Build ArrayBuffers in Block.mapStmt * Have empty Block serialize as "skip" The FIRRTL parser requires at least one indented line in each module. Sometimes tests emit and parse modules with no contents; this ensures there's always at least a "skip" in empty modules. Also fix tests that expected certain skips * Use var List as stack in Block.mapStmt impl This replaces Iterator concatenation. In Scala 2.11, RHS recursion on Iterators is not stack safe. This seems to have been fixed in 2.12 by Scala PR 5033. Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
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