From 1e497ce39fd161c681ee64c0a4a882859eb8850b Mon Sep 17 00:00:00 2001 From: Jack Koenig Date: Wed, 10 Jun 2020 12:47:44 -0700 Subject: 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>--- src/main/scala/firrtl/ir/IR.scala | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) (limited to 'src/main') 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 -- cgit v1.2.3