@@ -1500,11 +1500,69 @@ bool AstNode::simplify(bool const_fold, int stage, int width_hint, bool sign_hin
1500
1500
}
1501
1501
break ;
1502
1502
1503
+ case AST_CAST_SIZE: {
1504
+ int width = 1 ;
1505
+ AstNode *node;
1506
+ AstNode *child = children[0 ];
1507
+
1508
+ if (child->type == AST_WIRE) {
1509
+ if (child->children .size () == 0 ) {
1510
+ // Base type (e.g., int)
1511
+ width = child->range_left - child->range_right +1 ;
1512
+ node = mkconst_int (width, child->is_signed );
1513
+ } else {
1514
+ // User defined type
1515
+ log_assert (child->children [0 ]->type == AST_WIRETYPE);
1516
+
1517
+ const std::string &type_name = child->children [0 ]->str ;
1518
+ if (!current_scope.count (type_name))
1519
+ input_error (" Unknown identifier `%s' used as type name\n " , type_name.c_str ());
1520
+ AstNode *resolved_type_node = current_scope.at (type_name);
1521
+ if (resolved_type_node->type != AST_TYPEDEF)
1522
+ input_error (" `%s' does not name a type\n " , type_name.c_str ());
1523
+ log_assert (resolved_type_node->children .size () == 1 );
1524
+ AstNode *template_node = resolved_type_node->children [0 ];
1525
+
1526
+ // Ensure typedef itself is fully simplified
1527
+ while (template_node->simplify (const_fold, stage, width_hint, sign_hint)) {};
1528
+
1529
+ switch (template_node->type )
1530
+ {
1531
+ case AST_WIRE: {
1532
+ if (template_node->children .size () > 0 && template_node->children [0 ]->type == AST_RANGE)
1533
+ width = range_width (this , template_node->children [0 ]);
1534
+ child->delete_children ();
1535
+ node = mkconst_int (width, true );
1536
+ break ;
1537
+ }
1538
+
1539
+ case AST_STRUCT:
1540
+ case AST_UNION: {
1541
+ child->delete_children ();
1542
+ width = size_packed_struct (template_node, 0 );
1543
+ node = mkconst_int (width, false );
1544
+ break ;
1545
+ }
1546
+
1547
+ default :
1548
+ log_error (" Don't know how to translate static cast of type %s\n " , type2str (template_node->type ).c_str ());
1549
+ }
1550
+ }
1551
+
1552
+ delete child;
1553
+ children.erase (children.begin ());
1554
+ children.insert (children.begin (), node);
1555
+ }
1556
+
1557
+ detect_width_simple = true ;
1558
+ children_are_self_determined = true ;
1559
+ break ;
1560
+ }
1561
+
1503
1562
case AST_TO_BITS:
1504
1563
case AST_TO_SIGNED:
1505
1564
case AST_TO_UNSIGNED:
1506
1565
case AST_SELFSZ:
1507
- case AST_CAST_SIZE:
1508
1566
case AST_CONCAT:
1509
1567
case AST_REPLICATE:
1510
1568
case AST_REDUCE_AND:
0 commit comments