When working with SystemVerilog, one common issue that can arise is the compiler error due to using a class variable before the class itself is declared. This happens when two classes reference each other, creating a circular dependency. The classic dilemma of whether the chicken or the egg came first becomes relevant in this case. If one class references another before it is declared, the compiler can’t process the reference properly.
Example of the Problem
Here’s an example illustrating this issue:
class ABC;
DEF def; // Error: DEF has not been declared yet
endclass
class DEF;
ABC abc;
endclass
In this case, class ABC references DEF before it has been declared, causing the compiler to throw an error like this:
ncvlog: *E,NOIPRT (typedef-class.sv,2|5): Unrecognized declaration 'DEF' could be an unsupported keyword, a spelling mistake or missing instance port list '()' [SystemVerilog].
The error occurs because the DEF class has not been declared when the ABC class tries to use it.
Solution: Use typedef for Forward Declarations
To resolve this issue, you can use the typedef keyword to inform the compiler that the class DEF will be declared later. By using typedef, you can tell the compiler to expect a class definition at a later point in the code. Here’s how you can fix the problem:
typedef class DEF; // Inform the compiler that DEF will be defined later
class ABC;
DEF def; // Okay: Compiler knows that DEF will be defined later
endclass
class DEF;
ABC abc;
endclass
In this solution, typedef class DEF; acts as a forward declaration, signaling to the compiler that the class DEF will be defined later in the code. This allows the ABC class to use DEF as a type without causing an error.
Typedef Without Explicit class Keyword
In some cases, you don’t need to explicitly mention class in the typedef statement. Here’s an alternative syntax:
typedef DEF; // Legal: DEF is declared as a class type implicitly
class ABC;
DEF def;
endclass
In this example, typedef DEF; tells the compiler that DEF is a class, and the compiler will later resolve it to the actual class definition.
Using typedef with Parameterized Classes
You can also use typedef with parameterized classes. This approach is helpful when you have classes with parameters, allowing you to define the parameters in a more flexible way.
Here’s an example using a parameterized class with typedef:
typedef XYZ;
module top;
XYZ #(8'h3f, real) xyz0; // Positional parameter override
XYZ #(.ADDR(8'h60), .T(real)) xyz1; // Named parameter override
endmodule
class XYZ #(parameter ADDR = 8'h00, type T = int);
endclass
In this case, typedef XYZ; allows the module to refer to XYZ as a parameterized type. The module top can use XYZ with different parameter values, both positional and named.
Summary
The typedef keyword in SystemVerilog serves an important role in resolving issues when one class refers to another before it is declared. By using typedef, you inform the compiler of a class’s future existence, allowing you to avoid circular references and errors in your code. You can also use typedef with parameterized classes to add more flexibility to your design.
| Scenario | Code Example | Explanation |
|---|---|---|
| Forward declaration of a class | typedef class DEF; | Informs the compiler that DEF will be defined later. |
Typedef without class keyword | typedef DEF; | A shorthand way of declaring DEF as a class type. |
Using typedef with parameterized classes | typedef XYZ; | Allows the use of parameterized classes with different parameter values. |
Key Points
- Use
typedefwhen a class needs to be referenced before its full definition. typedef classprovides a forward declaration.- You can also use
typedefwith parameterized classes for more flexibility.
By understanding and using typedef correctly, you can avoid common class declaration errors and make your SystemVerilog code more modular and easier to manage.

