Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reject invalid casts to real #1203

Merged
merged 2 commits into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion elab_expr.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3706,7 +3706,20 @@ NetExpr* PECastType::elaborate_expr(Design*des, NetScope*scope,

NetExpr*tmp = 0;
if (dynamic_cast<const netreal_t*>(target_type_)) {
return cast_to_real(sub);
switch (sub->expr_type()) {
case IVL_VT_REAL:
return sub;
case IVL_VT_LOGIC:
case IVL_VT_BOOL:
return cast_to_real(sub);
default:
break;
}
cerr << get_fileline() << " error: Expression of type `"
<< sub->expr_type() << "` can not be cast to target type `real`."
<< endl;
des->errors++;
return nullptr;
} else if (dynamic_cast<const netstring_t*>(target_type_)) {
if (base_->expr_type() == IVL_VT_STRING)
return sub; // no conversion
Expand Down
12 changes: 12 additions & 0 deletions ivtest/ivltests/cast_real_invalid1.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Check that casting a string to real generates an error.

module test;

real r;
string s;

initial begin
r = real'(s); // Error: Cast from string to real not allowed
end

endmodule
12 changes: 12 additions & 0 deletions ivtest/ivltests/cast_real_invalid2.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Check that casting a array to real generates an error.

module test;

real r;
real a[10];

initial begin
r = real'(a); // Error: Cast from array to real not allowed
end

endmodule
12 changes: 12 additions & 0 deletions ivtest/ivltests/cast_real_invalid3.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Check that casting a queue to real generates an error.

module test;

real r;
real q[$];

initial begin
r = real'(q); // Error: Cast from queue to real not allowed
end

endmodule
12 changes: 12 additions & 0 deletions ivtest/ivltests/cast_real_invalid4.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Check that casting a dynamic array to real generates an error.

module test;

real r;
real d[];

initial begin
r = real'(d); // Error: Cast from dynamic array to real not allowed
end

endmodule
4 changes: 4 additions & 0 deletions ivtest/regress-vvp.list
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ case3 vvp_tests/case3.json
casex_synth vvp_tests/casex_synth.json
cast_int_ams vvp_tests/cast_int_ams.json
cast_int_ams-vlog95 vvp_tests/cast_int_ams-vlog95.json
cast_real_invalid1 vvp_tests/cast_real_invalid1.json
cast_real_invalid2 vvp_tests/cast_real_invalid2.json
cast_real_invalid3 vvp_tests/cast_real_invalid3.json
cast_real_invalid4 vvp_tests/cast_real_invalid4.json
comment1 vvp_tests/comment1.json
constfunc4_ams vvp_tests/constfunc4_ams.json
constfunc4_ams-vlog95 vvp_tests/constfunc4_ams-vlog95.json
Expand Down
5 changes: 5 additions & 0 deletions ivtest/vvp_tests/cast_real_invalid1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"type" : "CE",
"source" : "cast_real_invalid1.v",
"iverilog-args" : [ "-g2005-sv" ]
}
5 changes: 5 additions & 0 deletions ivtest/vvp_tests/cast_real_invalid2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"type" : "CE",
"source" : "cast_real_invalid2.v",
"iverilog-args" : [ "-g2005-sv" ]
}
5 changes: 5 additions & 0 deletions ivtest/vvp_tests/cast_real_invalid3.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"type" : "CE",
"source" : "cast_real_invalid3.v",
"iverilog-args" : [ "-g2005-sv" ]
}
5 changes: 5 additions & 0 deletions ivtest/vvp_tests/cast_real_invalid4.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"type" : "CE",
"source" : "cast_real_invalid4.v",
"iverilog-args" : [ "-g2005-sv" ]
}