diff --git a/elab_expr.cc b/elab_expr.cc index 682c017ba..8b77b9c76 100644 --- a/elab_expr.cc +++ b/elab_expr.cc @@ -3706,7 +3706,20 @@ NetExpr* PECastType::elaborate_expr(Design*des, NetScope*scope, NetExpr*tmp = 0; if (dynamic_cast(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(target_type_)) { if (base_->expr_type() == IVL_VT_STRING) return sub; // no conversion diff --git a/ivtest/ivltests/cast_real_invalid1.v b/ivtest/ivltests/cast_real_invalid1.v new file mode 100644 index 000000000..64f5e3638 --- /dev/null +++ b/ivtest/ivltests/cast_real_invalid1.v @@ -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 diff --git a/ivtest/ivltests/cast_real_invalid2.v b/ivtest/ivltests/cast_real_invalid2.v new file mode 100644 index 000000000..8e3f10d91 --- /dev/null +++ b/ivtest/ivltests/cast_real_invalid2.v @@ -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 diff --git a/ivtest/ivltests/cast_real_invalid3.v b/ivtest/ivltests/cast_real_invalid3.v new file mode 100644 index 000000000..bbcede088 --- /dev/null +++ b/ivtest/ivltests/cast_real_invalid3.v @@ -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 diff --git a/ivtest/ivltests/cast_real_invalid4.v b/ivtest/ivltests/cast_real_invalid4.v new file mode 100644 index 000000000..d73e14e10 --- /dev/null +++ b/ivtest/ivltests/cast_real_invalid4.v @@ -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 diff --git a/ivtest/regress-vvp.list b/ivtest/regress-vvp.list index 51fa355f8..ad5bb2317 100644 --- a/ivtest/regress-vvp.list +++ b/ivtest/regress-vvp.list @@ -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 diff --git a/ivtest/vvp_tests/cast_real_invalid1.json b/ivtest/vvp_tests/cast_real_invalid1.json new file mode 100644 index 000000000..2de12011f --- /dev/null +++ b/ivtest/vvp_tests/cast_real_invalid1.json @@ -0,0 +1,5 @@ +{ + "type" : "CE", + "source" : "cast_real_invalid1.v", + "iverilog-args" : [ "-g2005-sv" ] +} diff --git a/ivtest/vvp_tests/cast_real_invalid2.json b/ivtest/vvp_tests/cast_real_invalid2.json new file mode 100644 index 000000000..c745a15a9 --- /dev/null +++ b/ivtest/vvp_tests/cast_real_invalid2.json @@ -0,0 +1,5 @@ +{ + "type" : "CE", + "source" : "cast_real_invalid2.v", + "iverilog-args" : [ "-g2005-sv" ] +} diff --git a/ivtest/vvp_tests/cast_real_invalid3.json b/ivtest/vvp_tests/cast_real_invalid3.json new file mode 100644 index 000000000..b86f7cad5 --- /dev/null +++ b/ivtest/vvp_tests/cast_real_invalid3.json @@ -0,0 +1,5 @@ +{ + "type" : "CE", + "source" : "cast_real_invalid3.v", + "iverilog-args" : [ "-g2005-sv" ] +} diff --git a/ivtest/vvp_tests/cast_real_invalid4.json b/ivtest/vvp_tests/cast_real_invalid4.json new file mode 100644 index 000000000..ff7b2992a --- /dev/null +++ b/ivtest/vvp_tests/cast_real_invalid4.json @@ -0,0 +1,5 @@ +{ + "type" : "CE", + "source" : "cast_real_invalid4.v", + "iverilog-args" : [ "-g2005-sv" ] +}