Skip to content

Commit 2333827

Browse files
authored
Improve naming of anonymous/class-in-function Modules (bp #1224) (#1249)
* Better anonymous and class-in-function desiredName This changes the desired name of a Module to provide non-numeric naming for anonymous Modules and Modules defined inside function bodies. Signed-off-by: Schuyler Eldridge <schuyler.eldridge@ibm.com> (cherry picked from commit a69d79f) * Tests for anonymous/class-in-module desiredName Signed-off-by: Schuyler Eldridge <schuyler.eldridge@ibm.com> (cherry picked from commit 77d2455)
1 parent 6862513 commit 2333827

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

chiselFrontend/src/main/scala/chisel3/Module.scala

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -218,10 +218,32 @@ package experimental {
218218
//
219219
// Chisel Internals
220220
//
221-
/** Desired name of this module. Override this to give this module a custom, perhaps parametric,
222-
* name.
221+
222+
/** The desired name of this module (which will be used in generated FIRRTL IR or Verilog).
223+
*
224+
* The name of a module approximates the behavior of the Java Reflection [[`getSimpleName` method
225+
* https://docs.oracle.com/javase/8/docs/api/java/lang/Class.html#getSimpleName--]] with some modifications:
226+
*
227+
* - Anonymous modules will get an `"_Anon"` tag
228+
* - Modules defined in functions will use their class name and not a numeric name
229+
*
230+
* @note If you want a custom or parametric name, override this method.
223231
*/
224-
def desiredName: String = this.getClass.getName.split("\\.|\\$").last
232+
def desiredName: String = {
233+
/* The default module name is derived from the Java reflection derived class name. */
234+
val baseName = this.getClass.getName
235+
236+
/* A sequence of string filters applied to the name */
237+
val filters: Seq[String => String] = Seq(
238+
((a: String) => raw"\$$+anon".r.replaceAllIn(a, "_Anon")) // Merge the "$$anon" name with previous name
239+
)
240+
241+
filters
242+
.foldLeft(baseName){ case (str, filter) => filter(str) } // 1. Apply filters to baseName
243+
.split("\\.|\\$") // 2. Split string at '.' or '$'
244+
.filterNot(_.forall(_.isDigit)) // 3. Drop purely numeric names
245+
.last // 4. Use the last name
246+
}
225247

226248
/** Legalized name of this module. */
227249
final lazy val name = try {

src/test/scala/chiselTests/Module.scala

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,4 +158,19 @@ class ModuleSpec extends ChiselPropSpec {
158158
(the [Exception] thrownBy (Driver.elaborate(() => new NullModuleWrapper)))
159159
.getMessage should include ("desiredName of chiselTests.NullModuleWrapper is null")
160160
}
161+
property("The name of a module in a function should be sane") {
162+
def foo = {
163+
class Foo1 extends RawModule {
164+
assert(name == "Foo1")
165+
}
166+
new Foo1
167+
}
168+
Driver.elaborate(() => foo)
169+
}
170+
property("The name of an anonymous module should include '_Anon'") {
171+
trait Foo { this: RawModule =>
172+
assert(name.contains("_Anon"))
173+
}
174+
Driver.elaborate(() => new RawModule with Foo)
175+
}
161176
}

0 commit comments

Comments
 (0)