区分内联约束中的本地数据成员和 child-class 数据成员
Distinguishing between local data member and child-class data member in an inline constraint
我有一个 class 和一个 rand
数据成员 i
。这个 class (child
) 是 class parent
的成员,它还有一个数据成员 i
。我想将子 class 中 i
的值限制为与父 class 中 i
的值相同。我想做类似的事情:
c.randomize with {i==this.i;};
但是 this.i
似乎没有引用父 class 的 i
数据成员。 (为什么?)
我能做到:
function void f;
int dummy = i;
c.randomize with {i==dummy;};
endfunction
或者这个:
function void f;
c.randomize with {i==m.blk.p.i;}; // yuck!
endfunction
但想知道是否有更好的(内置的,非 hacky)区分这两个 i
的方法。
MCVE:
class child;
rand int i;
endclass
class parent;
child c = new;
int i=1;
function void f;
c.randomize with {i==this.i;};
endfunction
endclass
module m;
initial begin : blk
parent p = new;
p.f;
$display("%p", p);
end
endmodule
你想要{i==local::i}
。请参阅 1800-2017 LRM
的第 18.7.1 节
this.i
没有按照您的预期执行的原因是这两个规则的组合:
- 所有 class 方法,包括内置的
randomize
方法,都有一个内置的 this
参数。所以 c.method(args)
实际上是 method(args, c)
并且 this
成为 method
的局部变量,设置为 c
的值
with
子句中的标识符在调用 randomize()
. 时尝试在本地搜索之前首先绑定到被随机化的范围
所以 i
和 this.i
指的是同一个 class 变量,就像你写
class A;
bit i;
function void method;
i = 1;
this.i = 2;
endfunction
endclass
我有一个 class 和一个 rand
数据成员 i
。这个 class (child
) 是 class parent
的成员,它还有一个数据成员 i
。我想将子 class 中 i
的值限制为与父 class 中 i
的值相同。我想做类似的事情:
c.randomize with {i==this.i;};
但是 this.i
似乎没有引用父 class 的 i
数据成员。 (为什么?)
我能做到:
function void f;
int dummy = i;
c.randomize with {i==dummy;};
endfunction
或者这个:
function void f;
c.randomize with {i==m.blk.p.i;}; // yuck!
endfunction
但想知道是否有更好的(内置的,非 hacky)区分这两个 i
的方法。
MCVE:
class child;
rand int i;
endclass
class parent;
child c = new;
int i=1;
function void f;
c.randomize with {i==this.i;};
endfunction
endclass
module m;
initial begin : blk
parent p = new;
p.f;
$display("%p", p);
end
endmodule
你想要{i==local::i}
。请参阅 1800-2017 LRM
this.i
没有按照您的预期执行的原因是这两个规则的组合:
- 所有 class 方法,包括内置的
randomize
方法,都有一个内置的this
参数。所以c.method(args)
实际上是method(args, c)
并且this
成为method
的局部变量,设置为c
的值
with
子句中的标识符在调用randomize()
. 时尝试在本地搜索之前首先绑定到被随机化的范围
所以 i
和 this.i
指的是同一个 class 变量,就像你写
class A;
bit i;
function void method;
i = 1;
this.i = 2;
endfunction
endclass