Skip to content

Commit

Permalink
Add getClassType API to Definition[T <: Class]. (#3877) (#3879)
Browse files Browse the repository at this point in the history
This allows users to get a ClassType for use in other Property types,
which is especially useful when constructing aggregate Property types
of a specific Class.

(cherry picked from commit 8a02fb3)

Co-authored-by: Mike Urbach <mike.urbach@sifive.com>
  • Loading branch information
mergify[bot] and mikeurbach authored Feb 27, 2024
1 parent 9622f5c commit 52d7246
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
26 changes: 24 additions & 2 deletions core/src/main/scala/chisel3/properties/Class.scala
Original file line number Diff line number Diff line change
Expand Up @@ -199,13 +199,33 @@ object Class {
def getPropertyType: Property[ClassType] = {
// Get the BaseModule for the Class this is a definition of.
val baseModule = definition.getInnerDataContext.getOrElse(
throwException("Internal Error! Class instance did not have an associated BaseModule.")
throwException(
s"Internal Error! Class instance did not have an associated BaseModule for definition ${definition}."
)
)

// Get a Property[ClassType] type from the Class name.
val classType = ClassType.unsafeGetClassTypeByName(baseModule.name)
Property[classType.Type]()
}

/** Get a ClassType type from a Definition[Class].
*
* This is useful when a ClassType type is needed for other Property types.
*
* This method is safe, and should be used over unsafeGetClassTypeByName when possible.
*/
def getClassType: ClassType = {
// Get the BaseModule for the Class this is a definition of.
val baseModule = definition.getInnerDataContext.getOrElse(
throwException(
s"Internal Error! Class instance did not have an associated BaseModule for definition ${definition}."
)
)

// Get a ClassType from the Class name.
ClassType.unsafeGetClassTypeByName(baseModule.name)
}
}

implicit class ClassInstanceOps[T <: Class](instance: Instance[T]) {
Expand All @@ -218,7 +238,9 @@ object Class {
def getPropertyReference: Property[ClassType] = {
// Get the BaseModule from the Instance.
val baseModule = instance.getInnerDataContext.getOrElse(
throwException("Internal Error! Class instance did not have an associated BaseModule.")
throwException(
s"Internal Error! Class instance did not have an associated BaseModule for instance ${instance}."
)
)

// Get a StaticObject for bookkeeping.
Expand Down
10 changes: 10 additions & 0 deletions src/test/scala/chiselTests/properties/ClassSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -181,4 +181,14 @@ class ClassSpec extends ChiselFlatSpec with MatchesAndOmits {
"propassign obj2.in, obj1.out"
)()
}

it should "support getting a ClassType from a Class Definition" in {
class Test extends Class {}

ChiselStage.emitCHIRRTL(new RawModule {
val definition = Definition(new Test)
val classType = definition.getClassType
classType.name should equal("Test")
})
}
}

0 comments on commit 52d7246

Please sign in to comment.