summaryrefslogtreecommitdiff
path: root/core/src/main/scala/chisel3/internal/plugin/package.scala
blob: 9b9b41cd2658c97fdc33d1d36d2e7eecd4882b17 (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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// SPDX-License-Identifier: Apache-2.0

package chisel3.internal

package object plugin {

  // The actual implementation
  private def _autoNameRecursively[T <: Any](prevId: Long, name: String, nameMe: T): T = {
    chisel3.internal.Builder.nameRecursively(
      name,
      nameMe,
      (id: chisel3.internal.HasId, n: String) => {
        // Name override only if result was created in this scope
        if (id._id > prevId) {
          id.forceAutoSeed(n)
        }
      }
    )
    nameMe
  }

  /** Used by Chisel's compiler plugin to automatically name signals
    * DO NOT USE in your normal Chisel code!!!
    *
    * @param name The name to use
    * @param nameMe The thing to be named
    * @tparam T The type of the thing to be named
    * @return The thing, but now named
    */
  def autoNameRecursively[T <: Any](name: String)(nameMe: => T): T = {
    // The _id of the most recently constructed HasId
    val prevId = Builder.idGen.value
    val result = nameMe
    _autoNameRecursively(prevId, name, result)
  }

  /** Used by Chisel's compiler plugin to automatically name signals
    * DO NOT USE in your normal Chisel code!!!
    *
    * @param names The names to use corresponding to interesting fields of the Product
    * @param nameMe The [[Product]] to be named
    * @tparam T The type of the thing to be named
    * @return The thing, but with each member named
    */
  def autoNameRecursivelyProduct[T <: Product](names: List[Option[String]])(nameMe: => T): T = {
    // The _id of the most recently constructed HasId
    val prevId = Builder.idGen.value
    val result = nameMe
    for ((name, t) <- names.iterator.zip(result.productIterator) if name.nonEmpty) {
      _autoNameRecursively(prevId, name.get, t)
    }
    result
  }
}