Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add getClassType API to Definition[T <: Class]. (backport #3877) #3879

Merged
merged 1 commit into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")
})
}
}
Loading