Jump to content

passing "this" as an argument to a function


phil

Recommended Posts

Hi:

is there a way to pass "this" (or other means that represent the calling object handle) as an argument to a function, in SystemVerilog?

Here is this unexpected situation:

There is a function: func(class1 obj1, class2 obj2);

inside class1, there is a function foo(), calling func().

Class1::foo();

void' func(this, obj2);

endfunction

But surprisingly to me, it is illegal, because "this" is not a variable, as told by the compiler.

So is there a way to pass the handle of the calling object as an argument to a function? if not, is there a work around?

Thanks

phil

Link to comment
Share on other sites

interesting. it is an error only when func() uses "ref" of obj1.

below is the complete code. please note that function pa() uses "ref A".

Without "ref", the code works. With "ref", the error message is:

pa(this): invalid ref argument usage because actual argument is not a variable. [systemVerilog].

Why "this" can't be used as an argument of a object reference?

package my_pkg;

class A;

parameter bit base_a = 1;

endclass // A

function automatic void pa(ref A tr);

$display("pkg tr.base_a %h", tr.base_a);

endfunction // pa

endpackage // my_pkg

`include "my_pkg.svh"

import my_pkg::*;

class sub_a extends A;

function new(string name="sub_a");

pa(this);

endfunction // new

endclass // aub_a

module try;

sub_a sa;

initial begin

sa = new;

end

endmodule

Link to comment
Share on other sites

First, there is no need to declare tr as a ref argument. tr is already a class variable that references a class object. Use 'input A tr' or simply 'A tr' instead.

What you put as the actual argument to a ref, output, or inout argument must all be "writable". 'this' is not a variable and cannot be written to. The keyword 'this' is a special reference to the object used to invoke the method that 'this' is used from within.

See this link about when you should use ref arguments: https://forum.verificationacademy.com/forum/verification-methodology-discussion-forum/systemverilog-and-other-languages-forum/29572-passing-arguments-reference#comment-29573

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...