aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJack Koenig2017-09-29 15:12:43 -0700
committerGitHub2017-09-29 15:12:43 -0700
commitcd8542d31f20511844c59e08527af73d1e3f6ae1 (patch)
tree1630acbced28e0c1b3e4bbbbc93a07c99b0b1e23 /src
parent34e9944aaf3c1fc76fcaaacc02509f217c0c0d63 (diff)
Namespace - only save suffix for temp names (#667)
This prevents collisions for one prefix (including temp) from incrementing the suffix for other prefixes. Makes names more stable.
Diffstat (limited to 'src')
-rw-r--r--src/main/scala/firrtl/Namespace.scala16
-rw-r--r--src/test/scala/firrtlTests/NamespaceSpec.scala26
2 files changed, 38 insertions, 4 deletions
diff --git a/src/main/scala/firrtl/Namespace.scala b/src/main/scala/firrtl/Namespace.scala
index b3629211..cf8472a6 100644
--- a/src/main/scala/firrtl/Namespace.scala
+++ b/src/main/scala/firrtl/Namespace.scala
@@ -11,7 +11,7 @@ class Namespace private {
private val tempNamePrefix: String = "_GEN"
// Begin with a tempNamePrefix in namespace so we always have a number suffix
private val namespace = mutable.HashSet[String](tempNamePrefix)
- private var n = 0L
+ private var tempN = 0
def tryName(value: String): Boolean = {
val unused = !contains(value)
@@ -21,15 +21,23 @@ class Namespace private {
def contains(value: String): Boolean = namespace.contains(value)
- def newName(value: String): String = {
+ private def newNameIndex(value: String, idx: Int): (String, Int) = {
+ var n = idx
var str = value
while (!tryName(str)) {
str = s"${value}_$n"
n += 1
}
- str
+ (str, n)
+ }
+
+ def newName(value: String): String = newNameIndex(value, 0)._1
+
+ def newTemp: String = {
+ val (name, n) = newNameIndex(tempNamePrefix, tempN)
+ tempN = n
+ name
}
- def newTemp: String = newName(tempNamePrefix)
}
object Namespace {
diff --git a/src/test/scala/firrtlTests/NamespaceSpec.scala b/src/test/scala/firrtlTests/NamespaceSpec.scala
new file mode 100644
index 00000000..8aa29705
--- /dev/null
+++ b/src/test/scala/firrtlTests/NamespaceSpec.scala
@@ -0,0 +1,26 @@
+// See LICENSE for license details.
+
+package firrtlTests
+
+import firrtl.Namespace
+
+class NamespaceSpec extends FirrtlFlatSpec {
+
+ "A Namespace" should "not allow collisions" in {
+ val namespace = Namespace()
+ namespace.newName("foo") should be ("foo")
+ namespace.newName("foo") should be ("foo_0")
+ }
+
+ it should "start temps with a suffix of 0" in {
+ Namespace().newTemp.last should be ('0')
+ }
+
+ it should "handle multiple prefixes with independent suffixes" in {
+ val namespace = Namespace()
+ namespace.newName("foo") should be ("foo")
+ namespace.newName("foo") should be ("foo_0")
+ namespace.newName("bar") should be ("bar")
+ namespace.newName("bar") should be ("bar_0")
+ }
+}