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

问题描述 投票:-1回答:1

我在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语法?或者如果不是:这背后的原因是什么?

system-verilog
1个回答
2
投票

这是非法语法。必须在任何操作之前声明任务或函数中的所有变量。请参阅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

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

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

  super.foo();
endfunction
© www.soinside.com 2019 - 2024. All rights reserved.