Reputation: 11
I'm not familiar with uvm, but trying to understand and studying.
I found the below code when I leaning the UVM.
typedef class driver;
typedef class monitor;
class env; driver d0;
monitor mon0;
function init_drvr ();
d0 = new (); // initialize endfunction
function init_mon ();
mon0 = new (); // initialize endfunction endclass
endfunction
endclass
Especially
typedef class driver;
typedef class monitor;
Probably it seems like declare something, but why those typedef is in there?
Would you please let me know why do we use
typedef class driver;
typedef class monitor;
and how to understand this grammar?
Upvotes: 0
Views: 2689
Reputation: 42738
It is rare that you would need typedef class name
in SystemVerilog. Most programming languages require that identifiers used as type names be declared before they can be referenced syntactically. One place that occurs is if you have cyclical class references
class X;
Y has_a_Y;
endclass
class Y;
X has_a_X;
endclass
In order to compile the code for class X
, class Y
must be declared. If you change the compile order of the classes, then X
becomes unknown. So we use what is called a forward typedef
typedef class Y;
class X;
Y has_a_Y;
endclass
Now class X
compiles as long as class Y
gets defined before closing the current scope.
However, the UVM strongly discourages this kind of coding as these dependencies make the code less reusable.
Sometimes people use a forward typedef even when there are no cyclical dependencies because they are too lazy to compile their code in the correct dependency order.
Upvotes: 1
Reputation: 913
This is called a forward declaration and is a feature of most object-oriented language compilers. It is used to declare an identifier that is not yet defined.
Basically, what you are telling the compiler is that there is a class called monitor
and driver
that is defined elsewhere in the compilation scope. This will be resolved at run-time.
For more info, refer to the below article: (it is in C++ but the concept applies)
https://www.learncpp.com/cpp-tutorial/17-forward-declarations/
Upvotes: 1