phil Posted February 9, 2013 Report Share Posted February 9, 2013 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 Quote Link to comment Share on other sites More sharing options...
dave_59 Posted February 9, 2013 Report Share Posted February 9, 2013 Can you show the complete definition of foo? Where is the 'function' keyword. Is it a static method? You can only use 'this' inside a non-static method of a class. If it is not static, can you show the actual error message you are seeing? Quote Link to comment Share on other sites More sharing options...
phil Posted February 11, 2013 Author Report Share Posted February 11, 2013 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 Quote Link to comment Share on other sites More sharing options...
dave_59 Posted February 11, 2013 Report Share Posted February 11, 2013 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 Quote Link to comment Share on other sites More sharing options...
phil Posted February 11, 2013 Author Report Share Posted February 11, 2013 Hi Daave: Thanks for the explanation. It makes sense now. --Phil Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.