diff options
| author | Schuyler Eldridge | 2019-11-02 14:50:29 +0800 |
|---|---|---|
| committer | GitHub | 2019-11-02 14:50:29 +0800 |
| commit | dc7c4c619c677cc6a563bd27dbf55ba26f4c9f95 (patch) | |
| tree | bc3584e856a3336f8f91d110d949f26e0a44604c | |
| parent | 2e891a4e2a638a00cb836736c72eb87673dffba3 (diff) | |
| parent | 77d2455a5a9cd1e3baad460d1bf5ce950b58788a (diff) | |
Merge pull request #1224 from freechipsproject/issue-1223
Improve naming of anonymous/class-in-function Modules
| -rw-r--r-- | chiselFrontend/src/main/scala/chisel3/Module.scala | 28 | ||||
| -rw-r--r-- | src/test/scala/chiselTests/Module.scala | 15 |
2 files changed, 40 insertions, 3 deletions
diff --git a/chiselFrontend/src/main/scala/chisel3/Module.scala b/chiselFrontend/src/main/scala/chisel3/Module.scala index f88d637f..5642840f 100644 --- a/chiselFrontend/src/main/scala/chisel3/Module.scala +++ b/chiselFrontend/src/main/scala/chisel3/Module.scala @@ -218,10 +218,32 @@ package experimental { // // Chisel Internals // - /** Desired name of this module. Override this to give this module a custom, perhaps parametric, - * name. + + /** The desired name of this module (which will be used in generated FIRRTL IR or Verilog). + * + * The name of a module approximates the behavior of the Java Reflection [[`getSimpleName` method + * https://docs.oracle.com/javase/8/docs/api/java/lang/Class.html#getSimpleName--]] with some modifications: + * + * - Anonymous modules will get an `"_Anon"` tag + * - Modules defined in functions will use their class name and not a numeric name + * + * @note If you want a custom or parametric name, override this method. */ - def desiredName: String = this.getClass.getName.split("\\.|\\$").last + def desiredName: String = { + /* The default module name is derived from the Java reflection derived class name. */ + val baseName = this.getClass.getName + + /* A sequence of string filters applied to the name */ + val filters: Seq[String => String] = Seq( + ((a: String) => raw"\$$+anon".r.replaceAllIn(a, "_Anon")) // Merge the "$$anon" name with previous name + ) + + filters + .foldLeft(baseName){ case (str, filter) => filter(str) } // 1. Apply filters to baseName + .split("\\.|\\$") // 2. Split string at '.' or '$' + .filterNot(_.forall(_.isDigit)) // 3. Drop purely numeric names + .last // 4. Use the last name + } /** Legalized name of this module. */ final lazy val name = try { diff --git a/src/test/scala/chiselTests/Module.scala b/src/test/scala/chiselTests/Module.scala index f730d08b..d8cae510 100644 --- a/src/test/scala/chiselTests/Module.scala +++ b/src/test/scala/chiselTests/Module.scala @@ -158,4 +158,19 @@ class ModuleSpec extends ChiselPropSpec { (the [Exception] thrownBy (Driver.elaborate(() => new NullModuleWrapper))) .getMessage should include ("desiredName of chiselTests.NullModuleWrapper is null") } + property("The name of a module in a function should be sane") { + def foo = { + class Foo1 extends RawModule { + assert(name == "Foo1") + } + new Foo1 + } + Driver.elaborate(() => foo) + } + property("The name of an anonymous module should include '_Anon'") { + trait Foo { this: RawModule => + assert(name.contains("_Anon")) + } + Driver.elaborate(() => new RawModule with Foo) + } } |
