Are class object handles passed by reference when used are class method arguments?

Are class object handles always passed by reference when used are class method arguments?
Or does one need to qualify the formal argument as ref, or perhaps inout?

Concrete example I am working on: I have class function

function int to_gp(uvm_tlm_generic_payload gp)

which maps my (transaction) class attributes to generic payload.

I will generally want to call super.to_gp(gp) where my class extends from some base class that already implements to_gp for its base class attributes.
Let’s assume I do a gp = new in the to_gp function of that base class, then set the gp object’s data and data_length etc. I would now expect that in the derived class the new gp object survives the supr.to_gp call so that I can access its data and other contents. But I get a null object access.

I have tried with ref but run into some issues from which I have yet to conclude what;s going on, and I am just trying now inout but that seems counter intuitive.

I am firing off this question since I actually would like to know from an expert - I did not readily find an answer in the LRM.

Thanks - Hans

In reply to HvdS:

Hans, See this response as well as the link it contains.

In reply to dave_59:

Hi Dave,
From the response and embedded link I get that I should not need to use ref for my case as I described. Interestingly though, when not using it I get a null object, when using it, it appears to work. Aside, this is with VCS.

In the other response it does state that you almost never need to use ref with class object handles. So I thought to. Any my use case does not seem to be a case for which this is an exception, right?

So still puzzled about the behavior I am seeing.

Hans

In reply to HvdS:

Hard to answer without seeing more code, but if you are constructing a class handle inside a function that you want to pass out as a function argument, use an output direction, not a ref.

The links no longer work

There’s a problem with the forum migration a few years ago. Instead of clicking directly on the link, you need to open it in a different tab.

No the class object handles are not always passed by reference. Because they themselves are reference variables. They hold the reference of the objects allocated. everytime new() is called a handle which either initially points to null or another object, will start pointing to the newly dynamically constructed object.

So declaring a handle without ref in the function argument means, a handle which has an unique address always pointing to null untill the method was called upon with the object a handle pointing was passed. when you call new() on that handle inside the function, will only reflect within the function and not to the handle which was passed.Class object handles are not always passed by reference. A class handle is itself a reference variable, it stores a reference to a dynamically allocated object.

When new() is called, the handle is made to point to the newly constructed object. If the handle was previously null, it starts pointing to the new object; if it was already pointing to another object, it is updated to point to the new object instead.

When a class handle is passed to a function without ref, the handle itself is passed by value. In other words, the function receives its own local handle variable, containing a copy of the reference stored in the caller’s handle.

Therefore, if the caller’s handle is null, the function’s local handle initially also contains null. If the function then executes: handle = new();

the new() operation changes only the local handle inside the function. The caller’s handle is not changed, because the two handles are separate variables.

However, if the handle is passed using ref, the function receives a reference to the actual handle variable itself. Therefore, when the function executes: handle = new(); the caller’s handle is also updated to point to the newly allocated object.

It would’ve helped to show some code that goes along with your illustration.

There’s still no need to use a ref in a function calls. SystemVerilog also has output and inout argument directions.

I agree your point Dave.

Code for the illustration:

class transfer;
  int a;

  function void func1(transfer xfer);
    $display("before reconstruction @ func1: xfer.a = %0d", xfer.a);
    xfer   = new();
    xfer.a = 20;
    $display("after reconstruction @ func1: xfer.a = %0d", xfer.a);
  endfunction: func1

  function void func2(ref transfer xfer);
    $display("before reconstruction @ func2: xfer.a = %0d", xfer.a);
    xfer   = new();
    xfer.a = 30;
    $display("after  reconstruction @ func2: xfer.a = %0d", xfer.a);
    $display("after  reconstruction @ active obj: xfer.a = %0d", a); // Notable bahavior: a or this.a
  endfunction: func2

endclass: transfer

module     tb_top();

   transfer xfer;

  initial begin
    xfer   = new();
    xfer   = new();
    xfer.a = 10;

    xfer.func1(xfer);
    $display("after func1: xfer.a = %0d", xfer.a);
    $display();
    xfer.func2(xfer); 
    $display("after func2: xfer.a = %0d", xfer.a);
  end
endmodule: tb_top

Output:

# before reconstruction @ func1: xfer.a = 10
# after reconstruction @ func1: xfer.a = 20
# after func1: xfer.a = 10
#  
# before reconstruction @ func2: xfer.a = 10
# after  reconstruction @ func2: xfer.a = 30
# after  reconstruction @ active obj: xfer.a = 10
# after func2: xfer.a = 30

Also The notable behavior is a good example for the statement:

“SV automatically de-allocates objects that are no longer in use”

Since the function was called by the caller object initially we can see the constructed-active object still being alive.