Skip to content

Commit 1437556

Browse files
authored
Merge pull request #1203 from larsclausen/cast-to-real
Reject invalid casts to real
2 parents 30123f8 + eb90bcf commit 1437556

10 files changed

+86
-1
lines changed

elab_expr.cc

+14-1
Original file line numberDiff line numberDiff line change
@@ -3706,7 +3706,20 @@ NetExpr* PECastType::elaborate_expr(Design*des, NetScope*scope,
37063706

37073707
NetExpr*tmp = 0;
37083708
if (dynamic_cast<const netreal_t*>(target_type_)) {
3709-
return cast_to_real(sub);
3709+
switch (sub->expr_type()) {
3710+
case IVL_VT_REAL:
3711+
return sub;
3712+
case IVL_VT_LOGIC:
3713+
case IVL_VT_BOOL:
3714+
return cast_to_real(sub);
3715+
default:
3716+
break;
3717+
}
3718+
cerr << get_fileline() << " error: Expression of type `"
3719+
<< sub->expr_type() << "` can not be cast to target type `real`."
3720+
<< endl;
3721+
des->errors++;
3722+
return nullptr;
37103723
} else if (dynamic_cast<const netstring_t*>(target_type_)) {
37113724
if (base_->expr_type() == IVL_VT_STRING)
37123725
return sub; // no conversion

ivtest/ivltests/cast_real_invalid1.v

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Check that casting a string to real generates an error.
2+
3+
module test;
4+
5+
real r;
6+
string s;
7+
8+
initial begin
9+
r = real'(s); // Error: Cast from string to real not allowed
10+
end
11+
12+
endmodule

ivtest/ivltests/cast_real_invalid2.v

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Check that casting a array to real generates an error.
2+
3+
module test;
4+
5+
real r;
6+
real a[10];
7+
8+
initial begin
9+
r = real'(a); // Error: Cast from array to real not allowed
10+
end
11+
12+
endmodule

ivtest/ivltests/cast_real_invalid3.v

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Check that casting a queue to real generates an error.
2+
3+
module test;
4+
5+
real r;
6+
real q[$];
7+
8+
initial begin
9+
r = real'(q); // Error: Cast from queue to real not allowed
10+
end
11+
12+
endmodule

ivtest/ivltests/cast_real_invalid4.v

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Check that casting a dynamic array to real generates an error.
2+
3+
module test;
4+
5+
real r;
6+
real d[];
7+
8+
initial begin
9+
r = real'(d); // Error: Cast from dynamic array to real not allowed
10+
end
11+
12+
endmodule

ivtest/regress-vvp.list

+4
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ case3 vvp_tests/case3.json
7272
casex_synth vvp_tests/casex_synth.json
7373
cast_int_ams vvp_tests/cast_int_ams.json
7474
cast_int_ams-vlog95 vvp_tests/cast_int_ams-vlog95.json
75+
cast_real_invalid1 vvp_tests/cast_real_invalid1.json
76+
cast_real_invalid2 vvp_tests/cast_real_invalid2.json
77+
cast_real_invalid3 vvp_tests/cast_real_invalid3.json
78+
cast_real_invalid4 vvp_tests/cast_real_invalid4.json
7579
comment1 vvp_tests/comment1.json
7680
constfunc4_ams vvp_tests/constfunc4_ams.json
7781
constfunc4_ams-vlog95 vvp_tests/constfunc4_ams-vlog95.json
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"type" : "CE",
3+
"source" : "cast_real_invalid1.v",
4+
"iverilog-args" : [ "-g2005-sv" ]
5+
}
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"type" : "CE",
3+
"source" : "cast_real_invalid2.v",
4+
"iverilog-args" : [ "-g2005-sv" ]
5+
}
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"type" : "CE",
3+
"source" : "cast_real_invalid3.v",
4+
"iverilog-args" : [ "-g2005-sv" ]
5+
}
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"type" : "CE",
3+
"source" : "cast_real_invalid4.v",
4+
"iverilog-args" : [ "-g2005-sv" ]
5+
}

0 commit comments

Comments
 (0)