Skip to content

Packages

Packages separate aliases, constants, enumerations, structs, and unions into distinct namespaces - definitions cannot otherwise exist outside of a package.

Example

The Packtype definition can either use a Python dataclass style or the Packtype custom grammar:

1
2
3
4
5
6
import packtype

@packtype.package()
class MyPackage:
    """Description of what purpose this package serves"""
    ...
1
2
3
4
package my_package {
    "Description of what purpose this package serves"
    // ...
}

As rendered to SystemVerilog:

1
2
3
4
5
package my_package;

// ...attached definitions...

endpackage : my_package

Imports

Types and constants defined in one package may reference types and constants defined in another package and Packtype will track which entities are local and foreign.

When using the Python dataclass style syntax, packages may be referenced in the same fashion that normal imports operate:

package_a.py
1
2
3
4
5
6
import packtype
from packtype import Constant

@packtype.package()
class PackageA:
    VALUE_A : Constant[8] = 0x12
package_b.py
1
2
3
4
5
6
7
8
import packtype
from packtype import Constant

from package_a import PackageA

@packtype.package()
class PackageB:
    VALUE_B : Constant[8] = PackageA.VALUE_A + 3

When using the Packtype custom grammar, packages may be referenced using the import keyword:

package_a.pt
1
2
3
package package_a {
    VALUE_A : constant[8] = 0x12
}
package_b.pt
1
2
3
4
package package_b {
    import package_a::VALUE_A
    VALUE_B : constant[8] = VALUE_A + 3
}

Note

When using the Packtype custom grammar, all packages that are referenced will need to be provided to Packtype in order for imports to be resolved

Python Decorators

Python package definitions expose a set of decorators for declaring attached types:

  • @<PACKAGE>.enum() - used for declaring enumerations
  • @<PACKAGE>.struct() - used for declaring packed data structures
  • @<PACKAGE>.union() - used for declaring packed unions

Helper Properties and Methods

Package definitions expose a collection of helper functions for accessing members of the package:

  • <PACKAGE>._pt_foreign() - function returning the ordered set of types referenced by other packages in fields of structs or unions;
  • <PACKAGE>._pt_fields - property that returns the dictionary of definitions attached to the package where key is the field instance, value is the field name;
  • <PACKAGE>._pt_constants - property that returns an iterable of all constants attached to the package, with each entry being a tuple of name and instance;
  • <PACKAGE>._pt_scalars - property that returns an iterable of all scalars attached to the package, with each entry being a tuple of name and instance;
  • <PACKAGE>._pt_aliases - property that returns an iterable of all type aliases attached to the package, with each entry being a tuple of name and instance;
  • <PACKAGE>._pt_enums - property that returns an iterable of all enumerations attached to the package, with each entry being a tuple of name and instance;
  • <PACKAGE>._pt_structs - property that returns an iterable of all structs attached to the package, with each entry being a tuple of name and instance;
  • <PACKAGE>._pt_unions - property that returns an iterable of all unions attached to the package, with each entry being a tuple of name and instance;
  • <PACKAGE>._pt_structs_and_unions - property that returns an iterable of all structs and unions attached to the package maintaining their order of declaration, with each entry being a tuple of name and instance;
  • <PACKAGE>._pt_lookup() - function that returns the name of a package field given its instance.