summaryrefslogtreecommitdiff
path: root/chiselFrontend/src/main/scala/chisel3/core/Module.scala
diff options
context:
space:
mode:
authorJim Lawson2016-07-25 13:37:53 -0700
committerJim Lawson2016-07-25 13:37:53 -0700
commit3624751e2e63ba9f107c795529edfe48cf8340b2 (patch)
tree951deec27b8a75d9d9c0eec0aee6fa08f80f9ae0 /chiselFrontend/src/main/scala/chisel3/core/Module.scala
parent50518f43cbd9c783633714a26ecdb0f2f18a1142 (diff)
parent54cd58cbb435170dd2ed67dafe1cb1d769a799e8 (diff)
Merge branch 'master' into sdtwigg_connectwrap_renamechisel3
Diffstat (limited to 'chiselFrontend/src/main/scala/chisel3/core/Module.scala')
-rw-r--r--chiselFrontend/src/main/scala/chisel3/core/Module.scala32
1 files changed, 27 insertions, 5 deletions
diff --git a/chiselFrontend/src/main/scala/chisel3/core/Module.scala b/chiselFrontend/src/main/scala/chisel3/core/Module.scala
index e8474e39..a593f539 100644
--- a/chiselFrontend/src/main/scala/chisel3/core/Module.scala
+++ b/chiselFrontend/src/main/scala/chisel3/core/Module.scala
@@ -2,7 +2,7 @@
package chisel3.core
-import scala.collection.mutable.{ArrayBuffer, HashSet}
+import scala.collection.mutable.ArrayBuffer
import scala.language.experimental.macros
import chisel3.internal._
@@ -136,13 +136,35 @@ extends HasId {
}
// Suggest names to nodes using runtime reflection
- val valNames = HashSet[String](getClass.getDeclaredFields.map(_.getName):_*)
+ def getValNames(c: Class[_]): Set[String] = {
+ if (c == classOf[Module]) Set()
+ else getValNames(c.getSuperclass) ++ c.getDeclaredFields.map(_.getName)
+ }
+ val valNames = getValNames(this.getClass)
def isPublicVal(m: java.lang.reflect.Method) =
m.getParameterTypes.isEmpty && valNames.contains(m.getName)
+
+ /** Recursively suggests names to supported "container" classes
+ * Arbitrary nestings of supported classes are allowed so long as the
+ * innermost element is of type HasId
+ * Currently supported:
+ * - Iterable
+ * - Option
+ * (Note that Map is Iterable[Tuple2[_,_]] and thus excluded)
+ */
+ def nameRecursively(prefix: String, nameMe: Any): Unit =
+ nameMe match {
+ case (id: HasId) => id.suggestName(prefix)
+ case Some(elt) => nameRecursively(prefix, elt)
+ case (iter: Iterable[_]) if iter.hasDefiniteSize =>
+ for ((elt, i) <- iter.zipWithIndex) {
+ nameRecursively(s"${prefix}_${i}", elt)
+ }
+ case _ => // Do nothing
+ }
val methods = getClass.getMethods.sortWith(_.getName > _.getName)
- for (m <- methods; if isPublicVal(m)) m.invoke(this) match {
- case (id: HasId) => id.suggestName(m.getName)
- case _ =>
+ for (m <- methods if isPublicVal(m)) {
+ nameRecursively(m.getName, m.invoke(this))
}
// For Module instances we haven't named, suggest the name of the Module