1

My question is regarding using $cast in SV. If you search for the word cast in the code below, I have on purpose added a '!' to check for unsuccessful casting. In the event of unsuccessful cast, I wanted to see what happens when a call is made to bobsquare.bob(); I am surprised that at time=1ms when call_bob is called with the handle to polygon 'p', the function 'bob' in square class is called to perform the display statements. How can that be possible? I ran with Cadence's irun and debugged with the SV class browser and see that at time=1ms, bobsquare is not even allocated mem space and has NULL as pointer. Thanks!

class figure;

endclass

class polygon extends figure;

  virtual function void draw();
    $display("polygon::draw");
  endfunction

endclass

class square extends polygon;

  virtual function void draw();
    $display("square::draw");
  endfunction

  function void compute_area();
    $display("square::compute_area");
  endfunction

  function void bob();
     $display("square::I am bob");
     $display("%t", $realtime);     
  endfunction

endclass

program top;
  polygon p;
  square s;

  initial begin
    s = new();
    p = new();

     #1ms;
     call_bob(p);
     #1ms;
     call_bob(s);     
  end // initial begin
   task call_bob(figure generic_ref_figure);
      square bobsquare;      
      if (!($cast(bobsquare, generic_ref_figure)))
         bobsquare.bob();
   endtask // call_bob

endprogram
V from QC
  • 11
  • 1
  • 1
    I think your expectations are correct, and it sounds like the simulator is not behaving per the language spec. If you have access to another simulator (VCS or Questa) it would be interested to see what results you get from it. Since you have a test case already, you can submit to Cadence as a support request and get some feedback from them. It could be a tool bug. – dwikle Jul 24 '13 at 01:33

1 Answers1

0

IEEE Std 1800-2012 § 8.4 (& IEEE Std 1800-2005 § 7.4) states:

Accessing non-static members (see 8.9) or virtual methods (see 8.20) via a null object handle is illegal. The result of an illegal access via a null object is indeterminate, and implementations may issue an error.

I was unable to find any reference on what should happen with a default method type. Based on the LRM, it appeares that calling non-virtual methods via a null object handle is legal as long as the method is not calling any non-static members.

Make the method bob virtual to get the null object handle error.

Greg
  • 18,111
  • 5
  • 46
  • 68