@@ -94,6 +94,21 @@ void verilog_typecheck_exprt::propagate_type(
94
94
exprt &expr,
95
95
const typet &type)
96
96
{
97
+ auto &verilog_dest_type = type.get (ID_C_verilog_type);
98
+ if (verilog_dest_type == ID_verilog_enum)
99
+ {
100
+ // IEEE 1800-2017 6.19.3: "a variable of type enum cannot be directly
101
+ // assigned a value that lies outside the enumeration set unless an
102
+ // explicit cast is used"
103
+ if (
104
+ expr.type ().get (ID_C_verilog_type) != ID_verilog_enum ||
105
+ expr.type ().get (ID_C_identifier) != type.get (ID_C_identifier))
106
+ {
107
+ throw errort ().with_location (expr.source_location ())
108
+ << " assignment to enum requires enum of the same type" ;
109
+ }
110
+ }
111
+
97
112
if (expr.type ()==type)
98
113
return ;
99
114
@@ -193,7 +208,7 @@ void verilog_typecheck_exprt::propagate_type(
193
208
}
194
209
}
195
210
196
- typecast (expr, type);
211
+ implicit_typecast (expr, type);
197
212
}
198
213
199
214
/* ******************************************************************\
@@ -1436,7 +1451,7 @@ bool verilog_typecheck_exprt::is_constant_expression(
1436
1451
1437
1452
/* ******************************************************************\
1438
1453
1439
- Function: verilog_typecheck_exprt::typecast
1454
+ Function: verilog_typecheck_exprt::implicit_typecast
1440
1455
1441
1456
Inputs:
1442
1457
@@ -1446,13 +1461,28 @@ Function: verilog_typecheck_exprt::typecast
1446
1461
1447
1462
\*******************************************************************/
1448
1463
1449
- void verilog_typecheck_exprt::typecast (
1464
+ void verilog_typecheck_exprt::implicit_typecast (
1450
1465
exprt &expr,
1451
1466
const typet &dest_type)
1452
1467
{
1453
1468
if (dest_type.id ()==irep_idt ())
1454
1469
return ;
1455
1470
1471
+ auto &verilog_dest_type = dest_type.get (ID_C_verilog_type);
1472
+ if (verilog_dest_type == ID_verilog_enum)
1473
+ {
1474
+ // IEEE 1800-2017 6.19.3: "a variable of type enum cannot be directly
1475
+ // assigned a value that lies outside the enumeration set unless an
1476
+ // explicit cast is used"
1477
+ if (
1478
+ expr.type ().get (ID_C_verilog_type) != ID_verilog_enum ||
1479
+ expr.type ().get (ID_C_identifier) != dest_type.get (ID_C_identifier))
1480
+ {
1481
+ throw errort ().with_location (expr.source_location ())
1482
+ << " assignment to enum requires enum of the same type" ;
1483
+ }
1484
+ }
1485
+
1456
1486
if (expr.type ()==dest_type)
1457
1487
return ;
1458
1488
@@ -1800,7 +1830,8 @@ exprt verilog_typecheck_exprt::convert_unary_expr(unary_exprt expr)
1800
1830
}
1801
1831
else if (expr.id () == ID_typecast)
1802
1832
{
1803
- // System Verilog has got type'(expr).
1833
+ // System Verilog has got type'(expr). This is an explicit
1834
+ // type cast.
1804
1835
expr.type () = convert_type (expr.type ());
1805
1836
convert_expr (expr.op ());
1806
1837
}
0 commit comments