Systemverilog 不允许在调用 super.foo() 后声明变量?

Systemverilog doesn't allow variable declarations after call to super.foo()?

我 运行 在 DVT 上使用 SystemVerilog 遇到了一个奇怪的问题。有问题的代码片段看起来像这样:

class parent;     

   int A;
   function void foo();
      A = 5;
   endfunction

endclass


class childA extends parent;

   function void foo();
      bit test_one; //Does not flag as a syntax error.
      super.foo();
      bit test_two; //Flags as error: Expected endfunction, found bit.
   endfunction      //Subsequently: Expected endclass, found endfunction

endclass            //And lastly: Unexpected token: Endclass

据我所知,使用 super 调用任何隐藏的父函数都是合法的。但这种行为让我感到困惑。有人能告诉我这是否是合法的 SV 语法吗?或者如果不是:这背后的原因是什么?

这是非法语法。任务或函数中的所有变量必须在任何操作之前声明。请参阅 IEEE Std 1800-2012 § 13 任务和函数(子例程)

合法语法为:

function void foo();
  bit test_one;
  bit test_two;
  super.foo();
endfunction

唯一的例外是 begin-end 块,在这种情况下,可以在任何操作之前在 begin-end 块的顶部声明变量(但你可以嵌套 begin-end 块)。然而,这确实限制了范围访问并且可能不太可读。所以这不是一个好习惯

function void foo();
  bit test_one;
  super.foo();
  begin
    bit test_two; // only in scope within this begin-end
    begin
     bit test_three; // not the same 'test_three' as below, different scope
    end
    begin
     bit test_three; // not the same 'test_three' as above, different scope
    end
    // both 'test_three's are out of scope
  end
  // 'test_two' both 'test_three's are out of scope
endfunction

一般的最佳做法是始终在顶部声明变量。我更喜欢在变量声明和操作之间添加空 space 视觉分隔符;使阅读和修改苦行僧更容易。

function void foo();
  bit test_one;
  bit test_two;

  super.foo();
endfunction