class Base ;
virtual function void DISP ( const input int A ) ;
$display("In Base");
endfunction
endclass
class Ext extends Base ;
virtual function void DISP ( const input int A ) ;
$display("In Ext");
endfunction
endclass
Ext e ;
Base b ;
int a = 10 ;
initial begin
e = new();
b = e;
b.DISP(a);
end
I see following error :: The keyword ‘const’ must be followed by ‘ref’ in formal argument lists.
IN SV LRM Section 13.3 I do see syntax :: tf_port_direction ::= port_direction | const ref . So this tells that legal port direction could be input / output / input / ref / constant ref .
Now the reason I ask the syntax is during user_subscriber classes when I use ::
// In class user_subs extends uvm_subscriber # ( trans ) ;
function void write ( const input trans t ) ;
....
I get same error . I want const input so that transactions are not modified by any of the subscriber class
A const ref argument is a guarantee to the caller that you will not modify the actual argument passed to the function. You cannot modify the actual argument of an input to a function because it’s copied upon calling the function and not copied when it returns.
This would not help you if even if it were legal because the argument is only a handle to the transaction. With const ref, you cannot modify the handle, but you can still write to object members that handle references.
In UVM, we use the MO-COW principle - Manual Object Clone On Write.
SV LRM 13.5.2 says :: " When the formal argument is declared as const ref , the subroutine cannot alter the variable , and an attempt to do so shall generate a compile error " .
I am confused by what it means by “cannot alter the variable” when we talk about objects .
I understand that we don’t discuss simulator issues here but I see different results on simulators .
Those which flash error do no allow to change the contents of the object ( All flash an error on usage of const ref and using new() on the object passed as argument )
function void DISP ( cons ref trans t ) ;
// t = new() ; // Error
t.a = 100 ; // Modifying contents of object has conflicting Outputs on Simulators
.....
endfunction
In your code, t is a class variable that holds a handle. That handle is a reference to an object. The object is not a variable, it’s a container to a group of member variables and methods.
On simulators that claim it should be an error when writing to a member variable, run the example below. You will see that it’s a pointless error since it’s so easy to get around it.
class trans;
int a;
endclass
module top;
function automatic void f(const ref trans tx);
trans rx = tx;
// tx.a = 10; // is legal according to the LRM
rx.a=10;
endfunction
trans h;
initial begin
h = new;
f(h);
$display(h.a);
end
endmodule