summaryrefslogtreecommitdiff
path: root/src/main/scala/Chisel/Namespace.scala
blob: 9d867bdd241b62d08f50ef6edcc2d5be3779d562 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package Chisel

abstract class Namespace {
  final def name(n: String): String = {
    if (nameExists(n)) name(rename(n))
    else {
      names += n
      n
    }
  }

  def nameExists(n: String): Boolean

  protected final def nameExistsHere(n: String): Boolean =
    names contains n

  private def rename(n: String): String = {
    i += 1
    s"${n}_${i}"
  }

  private var i = 0L
  protected val names = collection.mutable.HashSet[String]()
}

class RootNamespace(initialNames: String*) extends Namespace {
  names ++= initialNames
  def nameExists(n: String) = nameExistsHere(n)
}

class ChildNamespace(parent: Namespace) extends Namespace {
  def nameExists(n: String) = nameExistsHere(n) || parent.nameExists(n)
}

class FIRRTLNamespace extends RootNamespace("mem", "node", "wire", "reg", "inst")