From e0edb49f7a8c43881462ea04eec28262129f6c3f Mon Sep 17 00:00:00 2001 From: Yilou Wang Date: Fri, 20 Feb 2026 16:53:34 +0100 Subject: [PATCH] Support 'this' keyword inside inline randomize() with {} constraint blocks (#7102) (#7113) --- src/V3LinkDot.cpp | 3 ++ test_regress/t/t_randomize_this_inline.py | 21 ++++++++ test_regress/t/t_randomize_this_inline.v | 62 +++++++++++++++++++++++ 3 files changed, 86 insertions(+) create mode 100755 test_regress/t/t_randomize_this_inline.py create mode 100644 test_regress/t/t_randomize_this_inline.v diff --git a/src/V3LinkDot.cpp b/src/V3LinkDot.cpp index e831c9a76..2c22bf1a2 100644 --- a/src/V3LinkDot.cpp +++ b/src/V3LinkDot.cpp @@ -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; diff --git a/test_regress/t/t_randomize_this_inline.py b/test_regress/t/t_randomize_this_inline.py new file mode 100755 index 000000000..db1adb3f9 --- /dev/null +++ b/test_regress/t/t_randomize_this_inline.py @@ -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() diff --git a/test_regress/t/t_randomize_this_inline.v b/test_regress/t/t_randomize_this_inline.v new file mode 100644 index 000000000..e3e808a69 --- /dev/null +++ b/test_regress/t/t_randomize_this_inline.v @@ -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