Fix referencing module variables above classes (#6304)

Signed-off-by: Artur Bieniek <abieniek@internships.antmicro.com>
This commit is contained in:
Artur Bieniek 2025-08-18 14:51:25 +02:00 committed by GitHub
parent 9dc31e4556
commit 53c59e7ac7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 64 additions and 3 deletions

View File

@ -68,9 +68,18 @@ class ScopeVisitor final : public VNVisitor {
UASSERT_OBJ(it2 != m_packageScopes.end(), nodep, "Can't locate package scope");
scopep = it2->second;
}
const auto it3 = m_varScopes.find(std::make_pair(nodep->varp(), scopep));
UASSERT_OBJ(it3 != m_varScopes.end(), nodep, "Can't locate varref scope");
AstVarScope* const varscp = it3->second;
// Search up the scope hierarchy for the variable
AstVarScope* varscp = nullptr;
AstScope* searchScopep = scopep;
while (searchScopep) {
const auto it3 = m_varScopes.find(std::make_pair(nodep->varp(), searchScopep));
if (it3 != m_varScopes.end()) {
varscp = it3->second;
break;
}
searchScopep = searchScopep->aboveScopep();
}
UASSERT_OBJ(varscp, nodep, "Can't locate varref scope");
nodep->varScopep(varscp);
}
}

View File

@ -0,0 +1,18 @@
#!/usr/bin/env python3
# DESCRIPTION: Verilator: Verilog Test driver/expect definition
#
# Copyright 2025 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.
# SPDX-License-Identifier: LGPL-3.0-only OR Artistic-2.0
import vltest_bootstrap
test.scenarios('simulator')
test.compile()
test.execute()
test.passes()

View File

@ -0,0 +1,34 @@
// DESCRIPTION: Verilator: Verilog Test module
//
// This file ONLY is placed under the Creative Commons Public Domain, for
// any use, without warranty, 2025 by Antmicro.
// SPDX-License-Identifier: CC0-1.0
`define stop $stop
`define checkh(gotv,expv) do if ((gotv) !== (expv)) begin $write("%%Error: %s:%0d: got='h%x exp='h%x\n", `__FILE__,`__LINE__, (gotv), (expv)); `stop; end while(0);
module m();
class c;
static function void fstatic();
`checkh(v, 42);
v++;
endfunction
function void fnonstatic();
`checkh(v, 43);
v++;
endfunction
endclass
c classinst;
int v;
initial begin
v=42;
`checkh(v, 42);
c::fstatic();
classinst = new();
classinst.fnonstatic();
`checkh(v, 44);
$finish;
end
endmodule