Facebook/LinkedIn login is now deprecated, please disconnect our access to your social profile.
User dashboard under chipverify.com/connect will be deprecated from Oct 1, 2020, Read More.
Let us contribute to a cleaner Earth, Go Green Updated: May 31, 2020

Sometimes the compiler errors out because of a class variable being used before the declaration of the class itself. For example, if two classes need a handle to each other, the classic puzzle of whether chicken or egg came first pops up. This is because the compiler processes the first class where it finds a reference to the second class being that which hasn't been declared yet.

  
  
class ABC;
	DEF 	def; 	// Error: DEF has not been declared yet
endclass

class DEF;
	ABC 	abc;
endclass

  

Compilation Error

file: typdef-class.sv
   DEF def;
     |
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].

In such cases you have to provide a forward declaration for the second class using typedef keyword. When the compiler see a typedef class, it will know that a definition for the class will be found later in the same file.

Usage

  
  
typedef class DEF;  // Inform compiler that DEF might be 
                    // used before actual class definition

class ABC;
	DEF 	def;      // Okay: Compiler knows that DEF 
	                // declaration will come later
endclass

class DEF;
	ABC 	abc;
endclass

  

By using a typedef DEF is declared to be of type class which is later proved to be the same. It is not necessary to specify that DEF is of type class in the typedef statement.

  
  
typedef DEF; 		// Legal

class ABC;
	DEF def;
endclass

  

Using typedef with parameterized classes

A typedef can also be used on classes with a parameterized port list as shown below.

  
  
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

  

You consent to our cookies if you continue to use our website. To know more about cookies, see our privacy policy. I accept cookies from this site.

Agree