Support 'this' keyword inside inline randomize() with {} constraint blocks (#7102) (#7113)

This commit is contained in:
Yilou Wang 2026-02-20 16:53:34 +01:00 committed by GitHub
parent 52a6de29cb
commit e0edb49f7a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 86 additions and 0 deletions

View File

@ -3705,6 +3705,9 @@ class LinkDotResolveVisitor final : public VNVisitor {
if (VN_IS(nodep->lhsp(), ParseRef) && nodep->lhsp()->name() == "this") {
VSymEnt* classSymp = getThisClassSymp();
// In 'randomize() with { this.member }', 'this' refers to randomized
// object, not the calling class (IEEE 1800-2023 18.7)
if (m_randSymp && m_inWith) classSymp = m_randSymp;
if (!classSymp) {
nodep->v3error("'this' used outside class (IEEE 1800-2023 8.11)");
m_ds.m_dotErr = true;

View File

@ -0,0 +1,21 @@
#!/usr/bin/env python3
# DESCRIPTION: Verilator: Verilog Test driver/expect definition
#
# 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-FileCopyrightText: 2026 Wilson Snyder
# SPDX-License-Identifier: LGPL-3.0-only OR Artistic-2.0
import vltest_bootstrap
test.scenarios('simulator')
if not test.have_solver:
test.skip("No constraint solver installed")
test.compile()
test.execute()
test.passes()

View File

@ -0,0 +1,62 @@
// DESCRIPTION: Verilator: Verilog Test module
//
// This file ONLY is placed under the Creative Commons Public Domain.
// SPDX-FileCopyrightText: 2026 PlanV GmbH
// SPDX-License-Identifier: CC0-1.0
// verilog_format: off
`define stop $stop
`define checkd(gotv,expv) do if ((gotv) !== (expv)) begin $write("%%Error: %s:%0d: got=%0d exp=%0d\n", `__FILE__,`__LINE__, (gotv), (expv)); `stop; end while(0);
`define check_range(gotv,minv,maxv) do if ((gotv) < (minv) || (gotv) > (maxv)) begin $write("%%Error: %s:%0d: got=%0d exp=%0d-%0d\n", `__FILE__,`__LINE__, (gotv), (minv), (maxv)); `stop; end while(0);
// verilog_format: on
// Test: 'this' keyword inside inline randomize() with {} constraint blocks.
// 'this' should refer to the object being randomized (IEEE 1800-2023 18.7).
class DataItem;
rand bit [7:0] value;
rand bit [7:0] limit;
constraint default_con { limit inside {[8'd50:8'd200]}; }
endclass
// Test 4: 'this' in inline constraint called from another class method.
// 'this' must bind to the randomized object, not the calling class.
class Caller;
rand bit [7:0] own_value;
function int do_rand(DataItem item);
return item.randomize() with { this.value > 8'd30; this.value < 8'd40; };
endfunction
endclass
module t;
initial begin
automatic DataItem item = new();
automatic Caller caller = new();
automatic int rand_ok;
// Test 1: 'this.member' in inline constraint from module-level code
rand_ok = item.randomize() with { this.value > 8'd10; this.value < 8'd50; };
`checkd(rand_ok, 1)
`check_range(item.value, 11, 49)
`check_range(item.limit, 50, 200)
// Test 2: multiple 'this.member' references
rand_ok = item.randomize() with { this.value > 8'd20; this.value < 8'd30; };
`checkd(rand_ok, 1)
`check_range(item.value, 21, 29)
// Test 3: mix of 'this.member' and unqualified member
rand_ok = item.randomize() with { this.value > 8'd5; this.value < 8'd100; limit > 8'd150; };
`checkd(rand_ok, 1)
`check_range(item.value, 6, 99)
`check_range(item.limit, 151, 200)
// Test 4: 'this' binds to randomized object, not calling class
rand_ok = caller.do_rand(item);
`checkd(rand_ok, 1)
`check_range(item.value, 31, 39)
$write("*-* All Finished *-*\n");
$finish;
end
endmodule