Add error on calling static function without object (#4962).

This commit is contained in:
Wilson Snyder 2024-03-11 18:23:55 -04:00
parent c92ced4ac7
commit 49022c3e73
7 changed files with 72 additions and 29 deletions

View File

@ -20,6 +20,7 @@ Verilator 5.023 devel
**Minor:**
* Add DFG 'regularize' pass, and improve variable removal (#4937). [Geza Lore]
* Add error on calling static function without object (#4962).
* Support public packed struct / union (#860) (#4878). [Kefa Chen]
* Change installation to be relocatable (#4927). [Geza Lore]
* Fix __Vlip undefined error in --freloop (#4824). [Justin Yao Du]

View File

@ -5793,6 +5793,12 @@ class WidthVisitor final : public VNVisitor {
}
UASSERT_OBJ(nodep->taskp(), nodep, "Unlinked");
if (nodep->didWidth()) return;
if ((nodep->taskp()->classMethod() && !nodep->taskp()->isStatic())
&& !VN_IS(m_procedurep, InitialAutomatic)
&& (!m_ftaskp || !m_ftaskp->classMethod() || m_ftaskp->isStatic())) {
nodep->v3error("Cannot call non-static member function "
<< nodep->prettyNameQ() << " without object (IEEE 1800-2023 8.10)");
}
userIterate(nodep->taskp(), nullptr);
// And do the arguments to the task/function too
processFTaskRefArgs(nodep);

View File

@ -1,7 +0,0 @@
%Error: t/t_class_member_bad3.v:16:12: Static access to non-static member variable 'member'
16 | Foo::member = 1;
| ^~~~~~
%Error: t/t_class_member_bad3.v:17:12: Static access to non-static task/function 'method'
17 | Foo::method();
| ^~~~~~
%Error: Exiting due to

View File

@ -1,19 +0,0 @@
// DESCRIPTION: Verilator: Verilog Test module
//
// This file ONLY is placed under the Creative Commons Public Domain, for
// any use, without warranty, 2023 by Antmicro Ltd.
// SPDX-License-Identifier: CC0-1.0
class Foo;
int member;
task method; endtask
endclass
module t;
initial begin
Foo foo = new;
Foo::member = 1;
Foo::method();
end
endmodule

View File

@ -0,0 +1,13 @@
%Error: t/t_class_misstatic_bad.v:31:7: Cannot call non-static member function 'nonstatic' without object (IEEE 1800-2023 8.10)
: ... note: In instance 't'
31 | nonstatic();
| ^~~~~~~~~
%Error: t/t_class_misstatic_bad.v:38:12: Cannot call non-static member function 'nonstatic' without object (IEEE 1800-2023 8.10)
: ... note: In instance 't'
38 | Cls::nonstatic();
| ^~~~~~~~~
%Error: t/t_class_misstatic_bad.v:44:12: Cannot call non-static member function 'nonstatic' without object (IEEE 1800-2023 8.10)
: ... note: In instance 't'
44 | Cls::nonstatic();
| ^~~~~~~~~
%Error: Exiting due to

View File

@ -2,7 +2,7 @@
if (!$::Driver) { use FindBin; exec("$FindBin::Bin/bootstrap.pl", @ARGV, $0); die; }
# DESCRIPTION: Verilator: Verilog Test driver/expect definition
#
# Copyright 2023 by Wilson Snyder. This program is free software; you
# Copyright 2010 by Wilson Snyder. This program is free software; you
# can redistribute it and/or modify it under the terms of either the GNU
# Lesser General Public License Version 3 or the Perl Artistic License
# Version 2.0.
@ -11,8 +11,8 @@ if (!$::Driver) { use FindBin; exec("$FindBin::Bin/bootstrap.pl", @ARGV, $0); di
scenarios(linter => 1);
lint(
# fails => 1, # TODO bug4077
# expect_filename => $Self->{golden_filename},
fails => 1,
expect_filename => $Self->{golden_filename},
);
ok(1);

View File

@ -0,0 +1,49 @@
// DESCRIPTION: Verilator: Verilog Test module
//
// This file ONLY is placed under the Creative Commons Public Domain, for
// any use, without warranty, 2024 by Wilson Snyder.
// SPDX-License-Identifier: CC0-1.0
class Cls;
int m_field = get_ok();
function int get_ok();
return 1;
endfunction
function void nonstatic();
endfunction
static function void isst();
endfunction
endclass
class Bar;
function void bar();
Cls::nonstatic(); // <--- bad static ref
Cls::isst();
endfunction
endclass
class Extends extends Cls;
function void ok();
nonstatic();
isst();
endfunction
static function extstatic();
nonstatic(); // <--- bad static ref
isst();
endfunction
endclass
module t;
function nonclassfunc();
Cls::nonstatic(); // <--- bad static ref
Cls::isst();
endfunction
initial begin
Bar obj = new();
obj.bar();
Cls::nonstatic(); // <--- bad static ref
Cls::isst();
Extends::isst();
$stop;
end
endmodule