1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
// SPDX-License-Identifier: Apache-2.0
// Copyright 2026 ZeroDDS Contributors
//! IDL → C99 codegen mode (vendor spec `zerodds-xcdr2-c-1.0`).
//!
//! For each IDL specification, this module emits:
//! - C99 `typedef struct` definitions per `struct` (with a module prefix
//! in the type name, because C99 has no namespaces).
//! - Static `zerodds_typesupport_t` tables with XCDR2 encoder/
//! decoder/key-hash/free function pointers.
//! - Inline body implementations of the encoders/decoders that produce
//! the XCDR2 wire format (XTypes 1.3 §7.4) byte-exactly.
//!
//! ## Scope
//!
//! Supported:
//! - Structs with `@final`/`@appendable`/`@mutable` extensibility.
//! - Primitive types (boolean, octet, short/long/long long + unsigned,
//! float, double).
//! - `string` (unbounded).
//! - `sequence<T>` (unbounded; nested sequences allowed).
//! - Nested modules → type-name prefix with `::` (cross-language
//! convention) and identifier prefix with `_` (C99-conformant).
//! - `@key` members → key-hash routine via `PlainCdr2BeKeyHolder`.
//! - `@id(N)` for @mutable.
//! - **Enums** → `int32_t` alias + prefixed enumerator constants (Bug C).
//! - **Typedefs** → resolved to the aliased type at every reference (Bug C).
//! - **Nested struct** members → inline-embedded by value (Bug C).
//! - **Fixed arrays** (`T x[N][M]…`) → C array, no length prefix (Bug C).
//! - **`@optional`** members → `<name>_present` companion flag + a wire
//! presence boolean (Bug C2; no longer flattened to mandatory).
//! - **Array bound by named constant** (`long v[N]`) → the bound is resolved
//! through the const symbol table (#43, const-array-bound).
//! - **`sequence<T>`** for primitive, string, enum, nested struct and nested
//! `sequence<…>` element types (#43, sequence widening).
//! - **`map<K,V>`** → parallel `keys[]`/`vals[]` arrays + DHEADER/count codec
//! (#43, map).
//! - **`wstring`** → NUL-terminated `uint16_t*`, UTF-16-LE on the wire
//! (XTypes §7.4.4.6) (#43, wstring).
//! - **Discriminated `union`** → C tagged union `struct { D _d; union {…} _u; }`
//! with a switch-on-discriminator encode/decode (#43, union). A top-level
//! union additionally gets a `TypeSupport` so it can be a topic type.
//! - **`typedef`-to-aggregate** (alias of a struct / sequence / map) → resolved
//! to the aliased aggregate at every reference (#43, typedef-to-aggregate).
//! - **typedef ALIAS CHAIN + typedef-to-array** (`typedef A B; typedef long
//! M[3][3];`) → resolved through the chain to the root type; an array-alias
//! contributes its dims at the use site (#43, typedefs).
//! - **`bitmask`** → smallest holder uint typedef (default `@bit_bound(32)` →
//! `uint32_t`) + `<LABEL>` flag-bit constants; **`bitset`** → packed holder
//! uint typedef + per-field SHIFT/MASK accessor macros. Both serialize as
//! their holder integer (XTypes §7.3.1.2) (#43, bitset/bitmask).
//! - **Self-/mutually-recursive types** (self-referential through a sequence,
//! or mutual `@external`) → a heap-indirected C type (pointer-to-tag element)
//! plus a runtime `_write_body`/`_read_body` helper that splices the recursion
//! at RUNTIME (XTypes §7.4.5) (#43, recursion / forward-decl).
//! - **Forward-declared** struct/union (then defined) → aggregate typedefs are
//! emitted in by-value dependency order so a later definition is reachable.
//!
//! Out of scope (errors at the codegen level, honest partial):
//! - `fixed<M,N>` decimal, `any`, `long double` — these legitimately stay out
//! of the C profile (no native C fixed-decimal / variant type).
//! - A genuinely **infinite-size** type (a by-value self/mutual cycle, e.g.
//! `struct Node { Node n; };`) is rejected cleanly — the only legal
//! self-reference is heap-indirected (through a sequence).
//! - `@optional` *inside* a nested struct, nested `@appendable`/`@mutable`
//! struct splice (only the inline `@final` form is wired), nested-aggregate
//! union arms inside a `@mutable` member, and freeing of heap-owning members
//! reached only through a fixed array.
//!
//! ## Invocation
//!
//! ```rust
//! use zerodds_idl::config::ParserConfig;
//! use zerodds_idl_cpp::{generate_c_header, CGenOptions};
//!
//! let ast = zerodds_idl::parse("@final struct Point { long x; long y; };",
//! &ParserConfig::default()).unwrap();
//! let header = generate_c_header(&ast, &CGenOptions::default()).unwrap();
//! assert!(header.contains("typedef struct Point_s"));
//! assert!(header.contains("Point_typesupport"));
//! ```
#![allow(clippy::module_name_repetitions)]
use core::fmt::Write as _;
use std::collections::{BTreeMap, BTreeSet};
use zerodds_idl::ast::{
Annotation, AnnotationParams, BitmaskDecl, BitsetDecl, Case, CaseLabel, ConstExpr,
ConstrTypeDecl, Declarator, Definition, EnumDef, FloatingType, IntegerType, LiteralKind,
ModuleDef, PrimitiveType, ScopedName, Specification, StructDcl, StructDef, SwitchTypeSpec,
TypeDecl, TypeSpec, UnionDcl, UnionDef,
};
use zerodds_idl::semantics::const_eval::{Symbol, SymbolTable, evaluate, evaluate_positive_int};
use crate::error::CppGenError;
/// Codegen-scoped type registry for the C backend (Bug C scope widening).
/// Resolves a `Scoped` member to its referent kind so the emitter can pick the
/// right C type name + XCDR2 codec instead of rejecting all non-flat members.
#[derive(Default)]
struct TypeReg {
/// IDL simple enum name → its C identifier (module-prefixed).
enums: BTreeMap<String, String>,
/// IDL simple enum name → signed wire holder width in BYTES (1/2/4) from
/// `@bit_bound` (XTypes §7.4.5.1): N≤8 → 1, N≤16 → 2, else 4. The C in-memory
/// type stays `int32_t`; only the wire write/read narrows.
enum_bytes: BTreeMap<String, u32>,
/// IDL simple typedef name → the aliased TypeSpec.
typedefs: BTreeMap<String, TypeSpec>,
/// IDL simple typedef name → its declarator array dimensions, for a
/// `typedef long Matrix3[3][3];` style array-alias (#43, typedef-to-array).
/// A reference to such a typedef inherits these dims at the use site.
typedef_arrays: BTreeMap<String, Vec<u64>>,
/// IDL simple bitmask name → (C identifier, holder uint C type). A bitmask
/// serializes as its smallest holder uint (XTypes §7.3.1.2.2, default
/// `@bit_bound(32)` → `uint32_t`).
bitmasks: BTreeMap<String, (String, &'static str)>,
/// IDL simple bitset name → (C identifier, packed holder uint C type). A
/// bitset serializes as one packed integer sized to the sum of its bitfield
/// widths (XTypes §7.3.1.2.1).
bitsets: BTreeMap<String, (String, &'static str)>,
/// IDL simple struct name → its C identifier (module-prefixed) + def.
structs: BTreeMap<String, (String, StructDef)>,
/// IDL simple union name → its C identifier (module-prefixed) + def.
unions: BTreeMap<String, (String, UnionDef)>,
/// Struct/union simple names that participate in a reference cycle
/// (self-referential through a sequence, or mutually recursive). Members of
/// this set are spliced via a generated `_write_body`/`_read_body` helper
/// (runtime recursion) instead of being inline-emitted, which would recurse
/// infinitely at codegen time (XTypes §7.4.5 / Bug G, C backend).
recursive: BTreeSet<String>,
/// Constant symbol table, keyed by BOTH the fully-qualified path
/// (`Mod::Sub::N`) and the simple name (`N`), so an array bound written
/// as a named constant (`long v[N]`) resolves to its literal the way every
/// other backend does (Bug C #43, const-array-bound). Enumerator ordinals
/// are folded in too (case labels / bounds frequently reference them).
consts: SymbolTable,
}
impl TypeReg {
fn build(defs: &[Definition]) -> Self {
let mut r = TypeReg::default();
collect_types(defs, &[], &mut r);
r.compute_recursive();
r
}
/// Compute the set of struct/union names that participate in a reference
/// cycle. An aggregate is recursive if it can reach itself by following
/// member references — directly, or (the only legal self-reference) through
/// a sequence/map element, a union arm, or a typedef alias. Such types must
/// be spliced via a runtime helper, not inline-emitted (codegen would
/// otherwise recurse forever — Bug G for the C backend).
fn compute_recursive(&mut self) {
let names: Vec<String> = self
.structs
.keys()
.chain(self.unions.keys())
.cloned()
.collect();
let mut recursive = BTreeSet::new();
for start in &names {
let mut seen = BTreeSet::new();
if self.reaches(start, start, &mut seen) {
recursive.insert(start.clone());
}
}
self.recursive = recursive;
}
/// True if aggregate `from` can reach `target` by following member /
/// element / arm references. `seen` guards the traversal itself against
/// cycles. zerodds-lint: recursion-depth 256 (bounded by distinct type set)
fn reaches(&self, from: &str, target: &str, seen: &mut BTreeSet<String>) -> bool {
if !seen.insert(from.to_string()) {
return false;
}
let mut refs: Vec<String> = Vec::new();
if let Some((_, sdef)) = self.structs.get(from) {
for m in &sdef.members {
self.collect_aggregate_refs(&m.type_spec, &mut refs);
}
}
if let Some((_, udef)) = self.unions.get(from) {
self.collect_aggregate_refs(&switch_type_spec(&udef.switch_type), &mut refs);
for c in &udef.cases {
self.collect_aggregate_refs(&c.element.type_spec, &mut refs);
}
}
for r in refs {
if r == target {
return true;
}
if self.reaches(&r, target, seen) {
return true;
}
}
false
}
/// Collect the simple names of struct/union aggregates referenced by a
/// type-spec (through sequences, maps, and typedef aliases). Enums, bitsets,
/// bitmasks and primitives terminate (they cannot close a cycle).
/// zerodds-lint: recursion-depth 64 (bounded by AST nesting)
fn collect_aggregate_refs(&self, ts: &TypeSpec, out: &mut Vec<String>) {
let resolved = resolve_alias(self, ts);
match &resolved {
TypeSpec::Scoped(sc) => {
if let Some(last) = scoped_last(sc) {
if self.structs.contains_key(&last) || self.unions.contains_key(&last) {
out.push(last);
}
}
}
TypeSpec::Sequence(seq) => self.collect_aggregate_refs(&seq.elem, out),
TypeSpec::Map(m) => {
self.collect_aggregate_refs(&m.key, out);
self.collect_aggregate_refs(&m.value, out);
}
_ => {}
}
}
fn is_recursive(&self, name: &str) -> bool {
self.recursive.contains(name)
}
}
/// zerodds-lint: recursion-depth 64 (module/type tree; bounded by IDL nesting)
fn collect_types(defs: &[Definition], scope: &[String], r: &mut TypeReg) {
for d in defs {
match d {
Definition::Module(m) => {
let mut s = scope.to_vec();
s.push(m.name.text.clone());
collect_types(&m.definitions, &s, r);
}
Definition::Type(TypeDecl::Constr(ConstrTypeDecl::Enum(e))) => {
r.enums
.insert(e.name.text.clone(), c_identifier(scope, &e.name.text));
let ebound = extract_int_annotation_c(&e.annotations, "bit_bound")
.filter(|&v| (1..=32).contains(&v))
.unwrap_or(32);
let ebytes = if ebound <= 8 {
1
} else if ebound <= 16 {
2
} else {
4
};
r.enum_bytes.insert(e.name.text.clone(), ebytes);
// Register enumerator ordinals (both simple + scoped) so a
// const-expression that references an enumerator (array bound or
// union case label) resolves.
let type_name = scope_join(scope, &e.name.text, "::");
for (i, en) in e.enumerators.iter().enumerate() {
let sym = Symbol::EnumValue {
type_name: type_name.clone(),
value: i32::try_from(i).unwrap_or(0),
};
r.consts.insert(en.name.text.clone(), sym.clone());
r.consts.insert(scope_join(scope, &en.name.text, "::"), sym);
}
}
Definition::Type(TypeDecl::Constr(ConstrTypeDecl::Struct(StructDcl::Def(s)))) => {
r.structs.insert(
s.name.text.clone(),
(c_identifier(scope, &s.name.text), s.clone()),
);
}
Definition::Type(TypeDecl::Constr(ConstrTypeDecl::Union(UnionDcl::Def(u)))) => {
r.unions.insert(
u.name.text.clone(),
(c_identifier(scope, &u.name.text), u.clone()),
);
}
Definition::Type(TypeDecl::Constr(ConstrTypeDecl::Bitmask(b))) => {
let holder = bitmask_holder_c_type(b);
r.bitmasks.insert(
b.name.text.clone(),
(c_identifier(scope, &b.name.text), holder),
);
}
Definition::Type(TypeDecl::Constr(ConstrTypeDecl::Bitset(b))) => {
let holder = bitset_holder_c_type(b);
r.bitsets.insert(
b.name.text.clone(),
(c_identifier(scope, &b.name.text), holder),
);
}
Definition::Type(TypeDecl::Typedef(td)) => {
for decl in &td.declarators {
match decl {
Declarator::Simple(n) => {
r.typedefs.insert(n.text.clone(), td.type_spec.clone());
}
Declarator::Array(arr) => {
// `typedef long Matrix3[3][3];` — the alias carries
// both the element type AND fixed dims; a use site
// inherits the dims (#43, typedef-to-array).
r.typedefs
.insert(arr.name.text.clone(), td.type_spec.clone());
let dims: Vec<u64> = arr
.sizes
.iter()
.map(|sz| const_expr_to_u64_pre(&r.consts, sz).unwrap_or(0))
.collect();
r.typedef_arrays.insert(arr.name.text.clone(), dims);
}
}
}
}
Definition::Const(c) => {
// Resolve the const value against whatever's already collected
// (forward refs to later consts are rare for array bounds, and
// we make a best-effort pass).
if let Ok(v) = evaluate(&c.value, &r.consts) {
r.consts
.insert(c.name.text.clone(), Symbol::Const(v.clone()));
r.consts
.insert(scope_join(scope, &c.name.text, "::"), Symbol::Const(v));
}
}
_ => {}
}
}
}
/// Resolve a TypeSpec through typedef chains to its effective type.
/// zerodds-lint: recursion-depth 16
fn resolve_alias(reg: &TypeReg, ts: &TypeSpec) -> TypeSpec {
let mut cur = ts.clone();
for _ in 0..16 {
let TypeSpec::Scoped(s) = &cur else { break };
let Some(last) = s.parts.last() else { break };
let Some(aliased) = reg.typedefs.get(&last.text).cloned() else {
break;
};
cur = aliased;
}
cur
}
fn scoped_last(s: &ScopedName) -> Option<String> {
s.parts.last().map(|p| p.text.clone())
}
fn unsupported(kind: &'static str) -> CppGenError {
CppGenError::UnsupportedConstruct {
construct: kind.to_string(),
context: None,
}
}
/// Codegen options (parallel to `CppGenOptions`).
#[derive(Debug, Clone, Default)]
pub struct CGenOptions {
/// Optional include-guard name (default: `ZERODDS_GENERATED_H`).
pub include_guard: Option<String>,
/// Optional file-header comment.
pub file_header: Option<String>,
}
/// Produces a complete C99 header from an IDL specification.
///
/// # Errors
/// - [`CppGenError::UnsupportedConstruct`]: out-of-scope IDL constructs that
/// legitimately stay outside the C profile (`fixed<M,N>` decimal, `any`,
/// `long double`), or a genuinely infinite-size by-value self/mutual
/// recursion. Structs, enums, typedefs (incl. alias chains + to-array),
/// bitsets/bitmasks, arrays (literal or const-bound), sequences, maps,
/// `wstring`, discriminated unions, forward declarations and
/// sequence-indirected recursive types are all supported.
pub fn generate_c_header(ast: &Specification, opts: &CGenOptions) -> Result<String, CppGenError> {
let reg = TypeReg::build(&ast.definitions);
let mut ctx = Ctx::new(opts, ®);
ctx.emit_preamble();
// Emit enum typedefs first (referenced by struct fields as int32 aliases).
ctx.emit_enums(&ast.definitions, &[]);
// Bitset/bitmask holder typedefs + bitmask position constants (#43).
ctx.emit_bits(&ast.definitions, &[]);
// Aggregate (struct + union) typedefs in by-value dependency order, so a
// by-value embed sees its referent's complete C type, and a recursive type's
// body helper sees its own typedef (#43, recursion / forward-decl). A
// recursive self-reference is always behind a pointer (sequence element) so
// it does not constrain the order.
ctx.emit_all_aggregate_typedefs(&ast.definitions)?;
// Forward-declare + define the runtime body helpers for recursive types
// (so a self-/mutually-recursive reference becomes a runtime call instead of
// an infinite codegen recursion — Bug G for the C backend).
ctx.emit_recursive_helpers()?;
ctx.walk_definitions(&ast.definitions, &[])?;
ctx.emit_postamble();
Ok(ctx.out)
}
// ============================================================================
// Internals
// ============================================================================
struct Ctx<'a> {
out: String,
opts: &'a CGenOptions,
reg: &'a TypeReg,
/// Nesting depth of collection (sequence/map) element loops, so each loop
/// gets a unique counter variable (`i0`, `i1`, …). Without this, a
/// `sequence<sequence<T>>` reuses `i` and the inner loop shadows the outer
/// index, corrupting addressing (segfault). Bumped around each element body.
coll_depth: u32,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Extensibility {
Final,
Appendable,
Mutable,
}
impl<'a> Ctx<'a> {
fn new(opts: &'a CGenOptions, reg: &'a TypeReg) -> Self {
Self {
out: String::new(),
opts,
reg,
coll_depth: 0,
}
}
/// Emit each IDL enum as a C `enum` + an int32 typedef so struct fields can
/// reference it by name and the codec can treat it as int32 (Spec §7.4.1.4.2).
/// zerodds-lint: recursion-depth 64 (module tree; bounded by IDL nesting)
fn emit_enums(&mut self, defs: &[Definition], scope: &[String]) {
for d in defs {
match d {
Definition::Module(m) => {
let mut s = scope.to_vec();
s.push(m.name.text.clone());
self.emit_enums(&m.definitions, &s);
}
Definition::Type(TypeDecl::Constr(ConstrTypeDecl::Enum(e))) => {
self.emit_enum(e, scope);
}
_ => {}
}
}
}
fn emit_enum(&mut self, e: &EnumDef, scope: &[String]) {
let c_name = c_identifier(scope, &e.name.text);
let _ = writeln!(self.out, "typedef int32_t {c_name}_t;");
for (i, en) in e.enumerators.iter().enumerate() {
// Enumerator constants are module+enum prefixed to stay unique in C's
// flat namespace.
let _ = writeln!(
self.out,
"enum {{ {c_name}_{label} = {i} }};",
label = en.name.text
);
}
let _ = writeln!(self.out);
}
/// Emit each IDL bitset/bitmask as a C holder-integer typedef. A bitmask
/// additionally gets a `<C>_<LABEL>` flag-bit constant per value; a bitset
/// gets `<C>_<field>_SHIFT` / `<C>_<field>_MASK` accessor macros (XTypes
/// §7.3.1.2). The wire form is the holder integer in both cases (#43).
/// zerodds-lint: recursion-depth 64 (module tree; bounded by IDL nesting)
fn emit_bits(&mut self, defs: &[Definition], scope: &[String]) {
for d in defs {
match d {
Definition::Module(m) => {
let mut s = scope.to_vec();
s.push(m.name.text.clone());
self.emit_bits(&m.definitions, &s);
}
Definition::Type(TypeDecl::Constr(ConstrTypeDecl::Bitmask(b))) => {
self.emit_bitmask(b, scope);
}
Definition::Type(TypeDecl::Constr(ConstrTypeDecl::Bitset(b))) => {
self.emit_bitset(b, scope);
}
_ => {}
}
}
}
fn emit_bitmask(&mut self, b: &BitmaskDecl, scope: &[String]) {
let c_name = c_identifier(scope, &b.name.text);
let holder = bitmask_holder_c_type(b);
let _ = writeln!(self.out, "typedef {holder} {c_name}_t;");
let mut next_pos: u32 = 0;
for v in &b.values {
let pos = extract_int_annotation_c(&v.annotations, "position").unwrap_or(next_pos);
next_pos = pos.saturating_add(1);
// Flag bit value (`1 << position`) as a C enum constant — keeps it a
// compile-time constant of the holder width.
let _ = writeln!(
self.out,
"enum {{ {c_name}_{label} = (1u << {pos}) }};",
label = v.name.text
);
}
let _ = writeln!(self.out);
}
fn emit_bitset(&mut self, b: &BitsetDecl, scope: &[String]) {
let c_name = c_identifier(scope, &b.name.text);
let holder = bitset_holder_c_type(b);
let _ = writeln!(self.out, "typedef {holder} {c_name}_t;");
// Per named bitfield: SHIFT (offset) + MASK accessor macros so a caller
// can pack/unpack the field within the holder integer.
let mut offset: u32 = 0;
for f in &b.bitfields {
let width = match &f.spec.width {
ConstExpr::Literal(l) if l.kind == LiteralKind::Integer => {
l.raw.trim().parse::<u32>().unwrap_or(0)
}
_ => 0,
};
if let Some(name) = &f.name {
let mask: u64 = if width >= 64 {
u64::MAX
} else {
(1u64 << width) - 1
};
let _ = writeln!(
self.out,
"enum {{ {c_name}_{field}_SHIFT = {offset} }};",
field = name.text
);
let _ = writeln!(
self.out,
"#define {c_name}_{field}_MASK 0x{mask:X}u",
field = name.text
);
}
offset = offset.saturating_add(width);
}
let _ = writeln!(self.out);
}
/// Emit one IDL union as a C tagged union `typedef struct { D _d; union
/// { ... } _u; }` so struct members can embed it by value and the codec can
/// switch on `_d` (#43, union). Called from the aggregate-typedef pre-pass.
fn emit_union_typedef(&mut self, u: &UnionDef, scope: &[String]) -> Result<(), CppGenError> {
let c_name = c_identifier(scope, &u.name.text);
let disc_ts = switch_type_spec(&u.switch_type);
let disc_c = c_type_for(self.reg, &disc_ts)?;
let _ = writeln!(self.out, "typedef struct {c_name}_s {{");
let _ = writeln!(self.out, " {disc_c} _d;");
let _ = writeln!(self.out, " union {{");
for case in &u.cases {
let field = union_case_field(case);
let c_type = c_type_for(self.reg, &case.element.type_spec)?;
let dims =
effective_array_dims(self.reg, &case.element.type_spec, &case.element.declarator)?;
if dims.is_empty() {
let _ = writeln!(self.out, " {c_type} {field};");
} else {
let suffix: String = dims.iter().map(|n| format!("[{n}]")).collect();
let _ = writeln!(self.out, " {c_type} {field}{suffix};");
}
}
let _ = writeln!(self.out, " }} _u;");
let _ = writeln!(self.out, "}} {c_name}_t;");
let _ = writeln!(self.out);
Ok(())
}
/// Emit encode/decode/free/typesupport for a TOP-LEVEL union (so it can be
/// a topic type). XCDR2 unions are appendable by default → DHEADER wrap.
fn emit_union_typesupport(
&mut self,
u: &UnionDef,
scope: &[String],
) -> Result<(), CppGenError> {
let c_name = c_identifier(scope, &u.name.text);
let dds_name = dds_type_name(scope, &u.name.text);
let ext = extensibility_of(&u.annotations);
let _ = writeln!(
self.out,
"static int {c_name}_encode(const void* sample, uint8_t* out_buf, size_t out_cap, size_t* out_len);"
);
let _ = writeln!(
self.out,
"static int {c_name}_decode(const uint8_t* buf, size_t len, void* out_sample);\nstatic int {c_name}_decode_e(const uint8_t* buf, size_t len, void* out_sample, int zd_be);"
);
let _ = writeln!(
self.out,
"static int {c_name}_decode_repr(const uint8_t* buf, size_t len, uint8_t representation, void* out_sample);"
);
let _ = writeln!(self.out, "static void {c_name}_sample_free(void* sample);");
let _ = writeln!(self.out);
let _ = writeln!(
self.out,
"static const char {c_name}_type_name[] = \"{dds_name}\";"
);
let _ = writeln!(
self.out,
"static const zerodds_typesupport_t {c_name}_typesupport = {{"
);
let _ = writeln!(self.out, " .type_hash = {{0}},");
let _ = writeln!(self.out, " .type_name = {c_name}_type_name,");
let _ = writeln!(self.out, " .is_keyed = 0,");
let _ = writeln!(self.out, " .extensibility = {},", ext.as_u8());
let _ = writeln!(self.out, " ._reserved = {{0}},");
let _ = writeln!(self.out, " .encode = {c_name}_encode,");
let _ = writeln!(self.out, " .decode = {c_name}_decode,");
let _ = writeln!(self.out, " .key_hash = NULL,");
let _ = writeln!(self.out, " .sample_free = {c_name}_sample_free,");
let _ = writeln!(self.out, " .decode_repr = {c_name}_decode_repr,");
let _ = writeln!(self.out, "}};");
let _ = writeln!(self.out);
// ---- encode body ----
let _ = writeln!(
self.out,
"static int {c_name}_encode_repr(const void* sample, uint8_t* out_buf, size_t out_cap, size_t* out_len, int representation);"
);
let _ = writeln!(
self.out,
"static int {c_name}_encode(const void* sample, uint8_t* out_buf, size_t out_cap, size_t* out_len) {{ return {c_name}_encode_repr(sample, out_buf, out_cap, out_len, 1); }}"
);
let _ = writeln!(
self.out,
"static int {c_name}_encode_repr(const void* sample, uint8_t* out_buf, size_t out_cap, size_t* out_len, int representation) {{"
);
let _ = writeln!(
self.out,
" const {c_name}_t* s = (const {c_name}_t*)sample;"
);
let _ = writeln!(self.out, " (void)s;");
let _ = writeln!(
self.out,
" size_t zd_ma = representation ? 4 : 8; (void)zd_ma;"
);
let _ = writeln!(self.out, " uint8_t* w_buf = NULL;");
let _ = writeln!(self.out, " size_t w_len = 0;");
let _ = writeln!(self.out, " size_t w_cap = 0;");
let _ = writeln!(
self.out,
" if (out_buf == NULL && out_cap > 0) goto fail;"
);
// The union's own DHEADER (appendable/mutable) is emitted by emit_union_write,
// so it is identical whether the union is top-level or a nested element.
let _ = ext;
self.emit_union_write("(*s)", u)?;
let _ = writeln!(self.out, " if (out_len) *out_len = w_len;");
let _ = writeln!(
self.out,
" if (out_buf == NULL || out_cap < w_len) {{ free(w_buf); return -13; }}"
);
let _ = writeln!(
self.out,
" if (w_len > 0) memcpy(out_buf, w_buf, w_len);"
);
let _ = writeln!(self.out, " free(w_buf);");
let _ = writeln!(self.out, " return 0;");
let _ = writeln!(self.out, "fail:");
let _ = writeln!(self.out, " free(w_buf);");
let _ = writeln!(self.out, " return -1;");
let _ = writeln!(self.out, "}}");
let _ = writeln!(self.out);
// ---- decode body ----
let _ = writeln!(
self.out,
"static int {c_name}_decode_core(const uint8_t* buf, size_t len, void* out_sample, int zd_be, int representation);"
);
let _ = writeln!(
self.out,
"static int {c_name}_decode(const uint8_t* buf, size_t len, void* out_sample) {{ return {c_name}_decode_core(buf, len, out_sample, 0, 1); }}"
);
let _ = writeln!(
self.out,
"static int {c_name}_decode_e(const uint8_t* buf, size_t len, void* out_sample, int zd_be) {{ return {c_name}_decode_core(buf, len, out_sample, zd_be, 1); }}"
);
let _ = writeln!(
self.out,
"static int {c_name}_decode_repr(const uint8_t* buf, size_t len, uint8_t representation, void* out_sample) {{ return {c_name}_decode_core(buf, len, out_sample, 0, representation ? 1 : 0); }}"
);
let _ = writeln!(
self.out,
"static int {c_name}_decode_core(const uint8_t* buf, size_t len, void* out_sample, int zd_be, int representation) {{"
);
let _ = writeln!(self.out, " {c_name}_t* s = ({c_name}_t*)out_sample;");
let _ = writeln!(self.out, " size_t pos = 0;");
let _ = writeln!(
self.out,
" size_t zd_ma = representation ? 4 : 8; (void)zd_ma;"
);
// The union DHEADER (appendable/mutable) is read by emit_union_read itself.
self.emit_union_read("(*s)", u)?;
let _ = writeln!(self.out, " (void)s;");
let _ = writeln!(self.out, " return 0;");
let _ = writeln!(self.out, "}}");
let _ = writeln!(self.out);
// ---- free body (free heap-owning union arms) ----
let _ = writeln!(
self.out,
"static void {c_name}_sample_free(void* sample) {{"
);
let _ = writeln!(self.out, " if (sample == NULL) return;");
let _ = writeln!(self.out, " {c_name}_t* s = ({c_name}_t*)sample;");
let _ = writeln!(self.out, " (void)s;");
let _ = writeln!(self.out, "}}");
let _ = writeln!(self.out);
Ok(())
}
fn emit_preamble(&mut self) {
let guard = self
.opts
.include_guard
.clone()
.unwrap_or_else(|| "ZERODDS_GENERATED_H".to_string());
if let Some(h) = &self.opts.file_header {
for line in h.lines() {
let _ = writeln!(self.out, "/* {line} */");
}
} else {
let _ = writeln!(
self.out,
"/* Generated by zerodds idl-cpp c_mode. Do not edit. */"
);
}
let _ = writeln!(self.out, "#ifndef {guard}");
let _ = writeln!(self.out, "#define {guard}");
let _ = writeln!(self.out, "#include <stddef.h>");
let _ = writeln!(self.out, "#include <stdint.h>");
let _ = writeln!(self.out, "#include <string.h>");
let _ = writeln!(self.out, "#include <stdlib.h>");
// Bug C2: only `zerodds_xcdr2.h` is needed — it declares
// `zerodds_typesupport_t` and every `zerodds_xcdr2_c_*` helper the
// generated body calls. The cbindgen-generated `zerodds.h` redeclares
// ~6 typed-FFI functions (`zerodds_topic_create_typed`, …) with names
// that conflict with the handwritten prototypes in `zerodds_xcdr2.h`
// (`struct zerodds_ZeroDdsRuntime*` vs `struct zerodds_ZeroDdsRuntime *`
// plus other signature drift), so including both broke
// `gcc -fsyntax-only <gen>.h`. The generated header uses none of the
// `zerodds.h`-only symbols, so it is dropped.
let _ = writeln!(self.out, "#include \"zerodds_xcdr2.h\"");
let _ = writeln!(self.out, "#ifdef __cplusplus");
let _ = writeln!(self.out, "extern \"C\" {{");
let _ = writeln!(self.out, "#endif");
let _ = writeln!(self.out);
// XCDR2 8-byte primitives align to MAXALIGN = min(sizeof, 4) = 4, never
// 8 (OMG DDS-XTypes 1.3 §7.4.1.1.1 / §7.4.3.2.3 INIT MAXALIGN(2)=4 —
// matches the cross-vendor `zerodds-cdr` core, crates/cdr). The shared
// `zerodds_xcdr2.h` `write_u64`/`read_u64` helpers pad to 8 (classic CDR1
// / XCDR1 semantics), so the XCDR2 codec uses these locally-emitted
// align-4 variants for u64/i64/f64 instead.
let _ = writeln!(self.out, "#ifndef ZERODDS_X2_ALIGN4_8BYTE");
let _ = writeln!(self.out, "#define ZERODDS_X2_ALIGN4_8BYTE");
let _ = writeln!(
self.out,
"static inline int zd_x2_write_u64(uint8_t** buf, size_t* len, size_t* cap, uint64_t v) {{"
);
let _ = writeln!(
self.out,
" if (zerodds_xcdr2_c_pad_to(buf, len, cap, 4) != 0) return -1;"
);
let _ = writeln!(
self.out,
" if (zerodds_xcdr2_c_grow(buf, cap, *len + 8) != 0) return -1;"
);
let _ = writeln!(
self.out,
" for (int i = 0; i < 8; ++i) {{ (*buf)[(*len)++] = (uint8_t)((v >> (8 * i)) & 0xFFu); }}"
);
let _ = writeln!(self.out, " return 0;");
let _ = writeln!(self.out, "}}");
let _ = writeln!(
self.out,
"static inline int zd_x2_write_i64(uint8_t** buf, size_t* len, size_t* cap, int64_t v) {{ return zd_x2_write_u64(buf, len, cap, (uint64_t)v); }}"
);
let _ = writeln!(
self.out,
"static inline int zd_x2_write_f64(uint8_t** buf, size_t* len, size_t* cap, double v) {{ uint64_t u; memcpy(&u, &v, sizeof(u)); return zd_x2_write_u64(buf, len, cap, u); }}"
);
let _ = writeln!(
self.out,
"static inline int zd_x2_read_u64(const uint8_t* buf, size_t len, size_t* pos, uint64_t* out, int big_endian) {{"
);
let _ = writeln!(
self.out,
" if (zerodds_xcdr2_c_pad_read(buf, len, pos, 4) != 0) return -1;"
);
let _ = writeln!(self.out, " if (*pos + 8 > len) return -1;");
let _ = writeln!(self.out, " uint64_t v = 0;");
let _ = writeln!(
self.out,
" for (int i = 0; i < 8; ++i) {{ int sh = big_endian ? (8 * (7 - i)) : (8 * i); v |= (uint64_t)buf[*pos + (size_t)i] << sh; }}"
);
let _ = writeln!(self.out, " *pos += 8; *out = v; return 0;");
let _ = writeln!(self.out, "}}");
let _ = writeln!(
self.out,
"static inline int zd_x2_read_i64(const uint8_t* buf, size_t len, size_t* pos, int64_t* out, int big_endian) {{ uint64_t u; int rc = zd_x2_read_u64(buf, len, pos, &u, big_endian); if (rc != 0) return rc; *out = (int64_t)u; return 0; }}"
);
let _ = writeln!(
self.out,
"static inline int zd_x2_read_f64(const uint8_t* buf, size_t len, size_t* pos, double* out, int big_endian) {{ uint64_t u; int rc = zd_x2_read_u64(buf, len, pos, &u, big_endian); if (rc != 0) return rc; memcpy(out, &u, sizeof(*out)); return 0; }}"
);
let _ = writeln!(self.out, "#endif");
let _ = writeln!(self.out);
}
/// Map a primitive write/read helper suffix to the XCDR2-align-4 prefix.
/// The 8-byte primitives (`u64`/`i64`/`f64`) route through the locally
/// emitted `zd_x2_*` helpers (align 4, §7.4.1.1.1); everything else uses the
/// shared `zerodds_xcdr2_c_*` helpers (which already pad to their own size,
/// capped at 4).
fn helper_call_prefix(helper: &str) -> &'static str {
match helper {
"u64" | "i64" | "f64" => "zd_x2_",
_ => "zerodds_xcdr2_c_",
}
}
/// If `name` is a bitmask or bitset, return the XCDR2 primitive helper
/// suffix for its holder integer (`u8`/`u16`/`u32`/`u64`). Both serialize as
/// their unsigned holder integer (XTypes §7.3.1.2).
fn bits_helper(&self, name: &str) -> Option<&'static str> {
let holder = self
.reg
.bitmasks
.get(name)
.or_else(|| self.reg.bitsets.get(name))
.map(|(_, h)| *h)?;
Some(match holder {
"uint8_t" => "u8",
"uint16_t" => "u16",
"uint32_t" => "u32",
_ => "u64",
})
}
fn emit_postamble(&mut self) {
let _ = writeln!(self.out, "#ifdef __cplusplus");
let _ = writeln!(self.out, "}}");
let _ = writeln!(self.out, "#endif");
let _ = writeln!(self.out, "#endif");
}
fn walk_definitions(
&mut self,
defs: &[Definition],
scope: &[String],
) -> Result<(), CppGenError> {
for d in defs {
match d {
Definition::Module(m) => self.walk_module(m, scope)?,
Definition::Type(TypeDecl::Constr(ConstrTypeDecl::Struct(sd))) => {
if let zerodds_idl::ast::StructDcl::Def(def) = sd {
self.emit_struct(def, scope)?;
}
}
// Enums are emitted in the pre-pass (as int32 typedefs);
// bitsets/bitmasks in the pre-pass (as holder-int typedefs);
// typedefs are resolved inline at every reference site. All are
// no-ops here (Bug C / #43: no longer rejected).
Definition::Type(TypeDecl::Constr(ConstrTypeDecl::Enum(_)))
| Definition::Type(TypeDecl::Constr(ConstrTypeDecl::Bitmask(_)))
| Definition::Type(TypeDecl::Constr(ConstrTypeDecl::Bitset(_)))
| Definition::Type(TypeDecl::Typedef(_)) => {}
Definition::Type(TypeDecl::Constr(ConstrTypeDecl::Union(ud))) => {
// The tagged-union typedef is emitted in the pre-pass; a
// top-level union additionally gets a TypeSupport so it can
// be a topic type (#43, union).
if let UnionDcl::Def(def) = ud {
self.emit_union_typesupport(def, scope)?;
}
}
Definition::Type(_) => {
return Err(unsupported("non-struct type"));
}
// Constants/annotations occur at top level.
Definition::Const(_)
| Definition::Annotation(_)
| Definition::TypeId(_)
| Definition::TypePrefix(_)
| Definition::Import(_) => {
// Ignore — no C output needed.
}
_ => {
return Err(unsupported("non-struct definition"));
}
}
}
Ok(())
}
fn walk_module(&mut self, m: &ModuleDef, scope: &[String]) -> Result<(), CppGenError> {
let mut new_scope = scope.to_vec();
new_scope.push(m.name.text.clone());
self.walk_definitions(&m.definitions, &new_scope)
}
/// Emit just the `typedef struct {…} <C>_t;` for a struct. Split out so all
/// struct typedefs can be emitted in a pre-pass — a recursive struct's body
/// helper (and a struct that splices another struct) needs every aggregate's
/// C type already in scope (#43, recursion / forward-decl).
/// zerodds-lint: recursion-depth 64 (member walk; bounded by IDL nesting)
fn emit_struct_typedef(
&mut self,
def: &StructDef,
scope: &[String],
) -> Result<(), CppGenError> {
let c_name = c_identifier(scope, &def.name.text);
let _ = writeln!(self.out, "typedef struct {c_name}_s {{");
for member in &def.members {
let optional = is_optional(&member.annotations);
for decl in &member.declarators {
let m_name = decl.name();
let c_type = c_type_for(self.reg, &member.type_spec)?;
// Fixed array declarator → C array suffix `[N][M]…`. A
// typedef-to-array alias contributes leading dims (#43).
let dims = effective_array_dims(self.reg, &member.type_spec, decl)?;
if dims.is_empty() {
let _ = writeln!(self.out, " {c_type} {field};", field = m_name.text);
} else {
let suffix: String = dims.iter().map(|n| format!("[{n}]")).collect();
let _ = writeln!(
self.out,
" {c_type} {field}{suffix};",
field = m_name.text
);
}
// @optional: a presence companion flag (Bug C2: no longer
// flattened to a mandatory member).
if optional {
let _ = writeln!(
self.out,
" uint8_t {field}_present;",
field = m_name.text
);
}
}
}
if def.members.is_empty() {
// C99 forbids empty structs; dummy padding member.
let _ = writeln!(self.out, " uint8_t _zerodds_empty;");
}
let _ = writeln!(self.out, "}} {c_name}_t;");
let _ = writeln!(self.out);
Ok(())
}
/// Emit ALL aggregate typedefs (structs + unions) in by-value dependency
/// order. A struct/union embedded BY VALUE (not behind a sequence/map
/// pointer) must have its complete C type emitted first; a recursive
/// self-reference is a pointer and imposes no order.
///
/// A *by-value* cycle (e.g. `struct Node { Node n; };`) is a genuinely
/// infinite-size type — the only legal self-reference is heap-indirected
/// (through a sequence). Such a cycle is rejected cleanly here rather than
/// emitting a non-compilable infinite C struct (XTypes §7.4.5, #43).
fn emit_all_aggregate_typedefs(&mut self, defs: &[Definition]) -> Result<(), CppGenError> {
// Gather every aggregate with its scope, keyed by simple name.
let mut order: Vec<(String, Vec<String>, AggDef)> = Vec::new();
collect_aggregates(defs, &[], &mut order);
// Reject infinite-size by-value self/mutual recursion.
for (name, _, agg) in &order {
let mut seen = BTreeSet::new();
if self.by_value_reaches(agg, name, &mut seen) {
return Err(CppGenError::UnsupportedConstruct {
construct: "infinite-size type: a struct/union member references its own \
type by value (a self-reference is only legal heap-indirected, \
e.g. through a sequence)"
.to_string(),
context: Some(name.clone()),
});
}
}
// Topological sort on the by-value dependency edges.
let mut emitted: BTreeSet<String> = BTreeSet::new();
let mut remaining: Vec<(String, Vec<String>, AggDef)> = order;
// Bounded fixpoint: each pass emits at least one node (acyclic by-value
// graph), so at most N passes.
let max_passes = remaining.len() + 1;
for _ in 0..max_passes {
if remaining.is_empty() {
break;
}
let mut progressed = false;
let mut next: Vec<(String, Vec<String>, AggDef)> = Vec::new();
for (name, scope, agg) in remaining.drain(..) {
let deps = self.by_value_agg_deps(&agg);
if deps.iter().all(|d| emitted.contains(d) || d == &name) {
match &agg {
AggDef::Struct(s) => self.emit_struct_typedef(s, &scope)?,
AggDef::Union(u) => self.emit_union_typedef(u, &scope)?,
}
emitted.insert(name);
progressed = true;
} else {
next.push((name, scope, agg));
}
}
remaining = next;
if !progressed {
// Defensive: a residual (should not happen for valid IDL) — emit
// in declaration order rather than dropping types.
for (name, scope, agg) in remaining.drain(..) {
match &agg {
AggDef::Struct(s) => self.emit_struct_typedef(s, &scope)?,
AggDef::Union(u) => self.emit_union_typedef(u, &scope)?,
}
emitted.insert(name);
}
break;
}
}
Ok(())
}
/// The simple names of aggregates a struct/union embeds BY VALUE (members
/// that are directly a struct/union, or a fixed array / typedef-to-aggregate
/// of one). Sequence/map element refs are pointers and excluded.
fn by_value_agg_deps(&self, agg: &AggDef) -> Vec<String> {
let mut out = Vec::new();
let push_ts = |ts: &TypeSpec, out: &mut Vec<String>| {
let resolved = resolve_alias(self.reg, ts);
if let TypeSpec::Scoped(sc) = &resolved {
if let Some(last) = scoped_last(sc) {
if self.reg.structs.contains_key(&last) || self.reg.unions.contains_key(&last) {
out.push(last);
}
}
}
};
match agg {
AggDef::Struct(s) => {
for m in &s.members {
push_ts(&m.type_spec, &mut out);
}
}
AggDef::Union(u) => {
for c in &u.cases {
push_ts(&c.element.type_spec, &mut out);
}
}
}
out
}
/// True if `agg` can reach `target` following ONLY by-value member edges —
/// i.e. `target` is embedded (transitively) as a non-pointer field, which
/// makes the type infinite-size. `seen` guards the walk against cycles.
/// zerodds-lint: recursion-depth 256 (bounded by distinct type set)
fn by_value_reaches(&self, agg: &AggDef, target: &str, seen: &mut BTreeSet<String>) -> bool {
for dep in self.by_value_agg_deps(agg) {
if dep == target {
return true;
}
if !seen.insert(dep.clone()) {
continue;
}
let next = self
.reg
.structs
.get(&dep)
.map(|(_, s)| AggDef::Struct(s.clone()))
.or_else(|| {
self.reg
.unions
.get(&dep)
.map(|(_, u)| AggDef::Union(u.clone()))
});
if let Some(next) = next {
if self.by_value_reaches(&next, target, seen) {
return true;
}
}
}
false
}
/// Forward-declare and define a runtime body helper for every recursive
/// struct/union. A recursive reference (self / mutual) is emitted as a call
/// to `<C>_write_body` / `<C>_read_body`, which threads the SAME growing
/// buffer (`w_buf`/`w_len`/`w_cap`) / read cursor (`pos`) by pointer — so
/// recursion happens at RUNTIME, not at codegen time (Bug G, C backend).
fn emit_recursive_helpers(&mut self) -> Result<(), CppGenError> {
// Collect (c_name, AggDef) for recursive aggregates, in a stable order.
let mut recs: Vec<(String, AggDef)> = Vec::new();
for name in self.reg.recursive.clone() {
if let Some((cn, s)) = self.reg.structs.get(&name).cloned() {
recs.push((cn, AggDef::Struct(s)));
} else if let Some((cn, u)) = self.reg.unions.get(&name).cloned() {
recs.push((cn, AggDef::Union(u)));
}
}
if recs.is_empty() {
return Ok(());
}
// Forward declarations (so mutually recursive bodies can call each other).
for (cn, _) in &recs {
let _ = writeln!(
self.out,
"static int {cn}_write_body(const {cn}_t* v, uint8_t** w_buf_pp, size_t* w_len_pp, size_t* w_cap_pp, int representation);"
);
let _ = writeln!(
self.out,
"static int {cn}_read_body(const uint8_t* buf, size_t len, size_t* pos_pp, {cn}_t* v, int zd_be, int representation);"
);
}
let _ = writeln!(self.out);
// Bodies.
for (cn, agg) in &recs {
self.emit_recursive_write_body(cn, agg)?;
self.emit_recursive_read_body(cn, agg)?;
}
Ok(())
}
/// `<C>_write_body`: the inline write templates, but with `w_buf`/`w_len`/
/// `w_cap` macro-aliased to the caller's buffer cursor (passed by pointer),
/// so a nested recursive call (`&w_buf` == the same `w_buf_pp`) keeps the
/// one shared output buffer. The templates' `goto fail` reaches a local
/// `fail:` returning -1.
fn emit_recursive_write_body(&mut self, cn: &str, agg: &AggDef) -> Result<(), CppGenError> {
let _ = writeln!(
self.out,
"static int {cn}_write_body(const {cn}_t* v, uint8_t** w_buf_pp, size_t* w_len_pp, size_t* w_cap_pp, int representation) {{"
);
let _ = writeln!(self.out, "#define w_buf (*w_buf_pp)");
let _ = writeln!(self.out, "#define w_len (*w_len_pp)");
let _ = writeln!(self.out, "#define w_cap (*w_cap_pp)");
let _ = writeln!(self.out, " const {cn}_t* s = v; (void)s;");
let _ = writeln!(
self.out,
" size_t zd_ma = representation ? 4 : 8; (void)zd_ma;"
);
match agg {
AggDef::Struct(s) => self.emit_struct_body_writes(s)?,
AggDef::Union(u) => self.emit_union_write("(*s)", u)?,
}
let _ = writeln!(self.out, " return 0;");
let _ = writeln!(self.out, "fail:");
let _ = writeln!(self.out, " return -1;");
let _ = writeln!(self.out, "#undef w_buf");
let _ = writeln!(self.out, "#undef w_len");
let _ = writeln!(self.out, "#undef w_cap");
let _ = writeln!(self.out, "}}");
let _ = writeln!(self.out);
Ok(())
}
/// `<C>_read_body`: the inline read templates with `pos` macro-aliased to the
/// caller's read cursor (passed by pointer); templates' `return -7` is the
/// error path.
fn emit_recursive_read_body(&mut self, cn: &str, agg: &AggDef) -> Result<(), CppGenError> {
let _ = writeln!(
self.out,
"static int {cn}_read_body(const uint8_t* buf, size_t len, size_t* pos_pp, {cn}_t* v, int zd_be, int representation) {{"
);
let _ = writeln!(self.out, "#define pos (*pos_pp)");
let _ = writeln!(self.out, " {cn}_t* s = v; (void)s;");
let _ = writeln!(
self.out,
" size_t zd_ma = representation ? 4 : 8; (void)zd_ma;"
);
match agg {
AggDef::Struct(s) => self.emit_struct_body_reads(s)?,
AggDef::Union(u) => self.emit_union_read("(*s)", u)?,
}
let _ = writeln!(self.out, " return 0;");
let _ = writeln!(self.out, "#undef pos");
let _ = writeln!(self.out, "}}");
let _ = writeln!(self.out);
Ok(())
}
fn emit_struct(&mut self, def: &StructDef, scope: &[String]) -> Result<(), CppGenError> {
let ext = extensibility_of(&def.annotations);
let c_name = c_identifier(scope, &def.name.text);
let dds_name = dds_type_name(scope, &def.name.text);
// ---- typedef struct ---- emitted in the aggregate-typedef pre-pass
// (`emit_all_aggregate_typedefs`) so by-value embeds and recursive body
// helpers see complete C types; here we emit only the codec.
// ---- encode/decode/free/key_hash declarations ----
let _ = writeln!(
self.out,
"static int {c_name}_encode(const void* sample, uint8_t* out_buf, size_t out_cap, size_t* out_len);"
);
let _ = writeln!(
self.out,
"static int {c_name}_decode(const uint8_t* buf, size_t len, void* out_sample);\nstatic int {c_name}_decode_e(const uint8_t* buf, size_t len, void* out_sample, int zd_be);"
);
let _ = writeln!(
self.out,
"static int {c_name}_decode_repr(const uint8_t* buf, size_t len, uint8_t representation, void* out_sample);"
);
let _ = writeln!(self.out, "static void {c_name}_sample_free(void* sample);");
let has_key = struct_has_key(def);
if has_key {
let _ = writeln!(
self.out,
"static int {c_name}_key_hash(const void* sample, uint8_t out_hash[16]);"
);
}
let _ = writeln!(self.out);
// ---- typesupport static const ----
let _ = writeln!(
self.out,
"static const char {c_name}_type_name[] = \"{dds_name}\";"
);
let _ = writeln!(
self.out,
"static const zerodds_typesupport_t {c_name}_typesupport = {{"
);
let _ = writeln!(self.out, " .type_hash = {{0}},");
let _ = writeln!(self.out, " .type_name = {c_name}_type_name,");
let _ = writeln!(self.out, " .is_keyed = {},", if has_key { 1 } else { 0 });
let _ = writeln!(self.out, " .extensibility = {},", ext.as_u8());
let _ = writeln!(self.out, " ._reserved = {{0}},");
let _ = writeln!(self.out, " .encode = {c_name}_encode,");
let _ = writeln!(self.out, " .decode = {c_name}_decode,");
if has_key {
let _ = writeln!(self.out, " .key_hash = {c_name}_key_hash,");
} else {
let _ = writeln!(self.out, " .key_hash = NULL,");
}
let _ = writeln!(self.out, " .sample_free = {c_name}_sample_free,");
let _ = writeln!(self.out, " .decode_repr = {c_name}_decode_repr,");
let _ = writeln!(self.out, "}};");
let _ = writeln!(self.out);
// ---- encode body ----
self.emit_encode_body(&c_name, def, ext)?;
// ---- decode body ----
self.emit_decode_body(&c_name, def, ext)?;
// ---- free body ----
self.emit_free_body(&c_name, def);
// ---- key_hash body ----
if has_key {
self.emit_key_hash_body(&c_name, def);
}
Ok(())
}
fn emit_encode_body(
&mut self,
c_name: &str,
def: &StructDef,
ext: Extensibility,
) -> Result<(), CppGenError> {
// `_encode` keeps the XCDR2 default ABI (typesupport .encode pointer);
// `_encode_repr` is the representation-aware body. `representation`:
// 0=XCDR1 (classic CDR, max_align 8, no DHEADER, @mutable=PL_CDR1),
// 1=XCDR2 (DHEADER + EMHEADER). c_mode is little-endian only.
let _ = writeln!(
self.out,
"static int {c_name}_encode_repr(const void* sample, uint8_t* out_buf, size_t out_cap, size_t* out_len, int representation);"
);
let _ = writeln!(
self.out,
"static int {c_name}_encode(const void* sample, uint8_t* out_buf, size_t out_cap, size_t* out_len) {{ return {c_name}_encode_repr(sample, out_buf, out_cap, out_len, 1); }}"
);
let _ = writeln!(
self.out,
"static int {c_name}_encode_repr(const void* sample, uint8_t* out_buf, size_t out_cap, size_t* out_len, int representation) {{"
);
let _ = writeln!(
self.out,
" const {c_name}_t* s = (const {c_name}_t*)sample;"
);
let _ = writeln!(self.out, " (void)s;");
// XCDR1 caps 8-byte primitives at MAXALIGN 8, XCDR2 at 4 (§7.4.1.1.1).
let _ = writeln!(
self.out,
" size_t zd_ma = representation ? 4 : 8; (void)zd_ma;"
);
let _ = writeln!(self.out, " /* Two-pass: grow the buffer, then copy. */");
let _ = writeln!(self.out, " uint8_t* w_buf = NULL;");
let _ = writeln!(self.out, " size_t w_len = 0;");
let _ = writeln!(self.out, " size_t w_cap = 0;");
let _ = writeln!(
self.out,
" if (out_buf == NULL && out_cap > 0) goto fail;"
);
match ext {
Extensibility::Final => {
self.emit_struct_body_writes(def)?;
}
Extensibility::Appendable => {
// XCDR2: DHEADER reserved, body-writes, then length patch.
// XCDR1: NO DHEADER — body starts at offset 0 (max_align 8).
let _ = writeln!(self.out, " size_t dheader_pos = 0; (void)dheader_pos;");
let _ = writeln!(self.out, " if (representation) {{");
let _ = writeln!(
self.out,
" if (zerodds_xcdr2_c_grow(&w_buf, &w_cap, w_len + 4) != 0) goto fail;"
);
let _ = writeln!(self.out, " dheader_pos = w_len; w_len += 4;");
let _ = writeln!(self.out, " }}");
let _ = writeln!(self.out, " size_t body_start = w_len;");
self.emit_struct_body_writes(def)?;
let _ = writeln!(self.out, " if (representation) {{");
let _ = writeln!(
self.out,
" zerodds_xcdr2_c_put_u32_at(w_buf, dheader_pos, (uint32_t)(w_len - body_start));"
);
let _ = writeln!(self.out, " }}");
}
Extensibility::Mutable => {
// XCDR2: DHEADER + EMHEADER per member. XCDR1: PL_CDR1 parameter
// list (no DHEADER), terminated by the PID_LIST_END sentinel.
let _ = writeln!(self.out, " if (representation) {{");
let _ = writeln!(
self.out,
" if (zerodds_xcdr2_c_grow(&w_buf, &w_cap, w_len + 4) != 0) goto fail;"
);
let _ = writeln!(self.out, " size_t dheader_pos = w_len; w_len += 4;");
let _ = writeln!(self.out, " size_t mut_body_start = w_len;");
self.emit_mutable_member_writes(def)?;
let _ = writeln!(
self.out,
" zerodds_xcdr2_c_put_u32_at(w_buf, dheader_pos, (uint32_t)(w_len - mut_body_start));"
);
let _ = writeln!(self.out, " }} else {{");
self.emit_pl_cdr1_member_writes(def)?;
let _ = writeln!(self.out, " }}");
}
}
// Copy the output.
let _ = writeln!(self.out, " if (out_len) *out_len = w_len;");
let _ = writeln!(
self.out,
" if (out_buf == NULL || out_cap < w_len) {{ free(w_buf); return -13; }}"
);
let _ = writeln!(
self.out,
" if (w_len > 0) memcpy(out_buf, w_buf, w_len);"
);
let _ = writeln!(self.out, " free(w_buf);");
let _ = writeln!(self.out, " return 0;");
let _ = writeln!(self.out, "fail:");
let _ = writeln!(self.out, " free(w_buf);");
let _ = writeln!(self.out, " return -1;");
let _ = writeln!(self.out, "}}");
let _ = writeln!(self.out);
Ok(())
}
fn emit_struct_body_writes(&mut self, def: &StructDef) -> Result<(), CppGenError> {
for member in &def.members {
let optional = is_optional(&member.annotations);
for decl in &member.declarators {
let f = &decl.name().text;
let dims = effective_array_dims(self.reg, &member.type_spec, decl)?;
if optional {
// XCDR2 non-mutable optional: a boolean presence flag, then
// the value only if present (XTypes §7.4.3 optional members).
let _ = writeln!(
self.out,
" if (zerodds_xcdr2_c_write_u8(&w_buf, &w_len, &w_cap, s->{f}_present ? 1 : 0) != 0) goto fail;"
);
let _ = writeln!(self.out, " if (s->{f}_present) {{");
self.emit_array_or_scalar_write(&format!("s->{f}"), &member.type_spec, &dims)?;
let _ = writeln!(self.out, " }}");
} else {
self.emit_array_or_scalar_write(&format!("s->{f}"), &member.type_spec, &dims)?;
}
}
}
Ok(())
}
/// Write a member that may be a fixed array (N nested loops over the C array)
/// or a scalar/aggregate. Fixed arrays carry no length prefix (XTypes §7.4.3).
fn emit_array_or_scalar_write(
&mut self,
var: &str,
type_spec: &TypeSpec,
dims: &[u64],
) -> Result<(), CppGenError> {
if dims.is_empty() {
return self.emit_member_write(var, type_spec);
}
// Bug XV-arr: a fixed array gets ONE collection DHEADER only when its
// ELEMENT type is NON-primitive (1-D array of struct/string/sequence). An
// array of a PRIMITIVE element is a PARRAY (XTypes 1.3 §7.4.3.5 rule 8) —
// PLAIN-collection regardless of dimensionality, so it carries NO DHEADER
// even when multi-dimensional (`long grid[2][3]`). Byte-identical to the
// corrected rust golden (grid: 6×i32 tight, NO DHEADER; shape: DHEADER=16).
let needs_dheader = !self.seq_elem_is_primitive(type_spec);
if needs_dheader {
// Own brace scope so two array members in one struct do not collide
// on the `arr_dheader_pos`/`arr_body_start` locals. The collection
// DHEADER exists only under XCDR2 — XCDR1 (classic CDR) has none.
let _ = writeln!(self.out, " {{");
let _ = writeln!(
self.out,
" size_t arr_dheader_pos = 0; (void)arr_dheader_pos;"
);
let _ = writeln!(self.out, " if (representation) {{");
let _ = writeln!(
self.out,
" if (zerodds_xcdr2_c_grow(&w_buf, &w_cap, w_len + 4) != 0) goto fail;"
);
let _ = writeln!(self.out, " arr_dheader_pos = w_len; w_len += 4;");
let _ = writeln!(self.out, " }}");
let _ = writeln!(
self.out,
" size_t arr_body_start = w_len; (void)arr_body_start;"
);
}
let mut acc = var.to_string();
for (d, _n) in dims.iter().enumerate() {
let iv = format!("ai{d}");
let _ = writeln!(
self.out,
" for (uint32_t {iv} = 0; {iv} < {n}; ++{iv}) {{",
n = dims[d]
);
acc = format!("{acc}[{iv}]");
}
self.emit_member_write(&acc, type_spec)?;
for _ in dims {
let _ = writeln!(self.out, " }}");
}
if needs_dheader {
let _ = writeln!(
self.out,
" if (representation) {{ zerodds_xcdr2_c_put_u32_at(w_buf, arr_dheader_pos, (uint32_t)(w_len - arr_body_start)); }}"
);
let _ = writeln!(self.out, " }}"); // close the array DHEADER scope
}
Ok(())
}
/// zerodds-lint: recursion-depth 64 (nested struct members; bounded by IDL)
fn emit_member_write(&mut self, var: &str, type_spec: &TypeSpec) -> Result<(), CppGenError> {
// Resolve typedef aliases to the effective type first (Bug C).
let resolved = resolve_alias(self.reg, type_spec);
match &resolved {
TypeSpec::Primitive(p) => self.emit_primitive_write(var, *p),
TypeSpec::String(st) => {
if st.wide {
return self.emit_wstring_write(var, st.bound.as_ref());
}
// Bounded string<N>: enforce the bound (XTypes §7.4.13.4.2) — a
// string longer than N must be rejected, not silently corrupt.
if let Some(n) = self.eval_bound(st.bound.as_ref()) {
let _ = writeln!(
self.out,
" if (({var}) != NULL && strlen({var}) > {n}u) goto fail;"
);
}
let _ = writeln!(
self.out,
" if (zerodds_xcdr2_c_write_string(&w_buf, &w_len, &w_cap, {var}) != 0) goto fail;"
);
Ok(())
}
TypeSpec::Sequence(seq) => self.emit_sequence_write(var, &seq.elem, seq.bound.as_ref()),
TypeSpec::Map(m) => self.emit_map_write(var, &m.key, &m.value, m.bound.as_ref()),
TypeSpec::Scoped(sc) => {
let last = scoped_last(sc).ok_or_else(|| unsupported("empty scoped name"))?;
if self.reg.enums.contains_key(&last) {
// enum → signed wire holder of its @bit_bound width
// (XTypes §7.4.5.1). u8/u16 carry the byte image of the
// signed value; the int32 case is the spec default.
let bytes = self.reg.enum_bytes.get(&last).copied().unwrap_or(4);
let line = match bytes {
1 => format!(
" if (zerodds_xcdr2_c_write_u8(&w_buf, &w_len, &w_cap, (uint8_t)({var})) != 0) goto fail;"
),
2 => format!(
" if (zerodds_xcdr2_c_write_u16(&w_buf, &w_len, &w_cap, (uint16_t)({var})) != 0) goto fail;"
),
_ => format!(
" if (zerodds_xcdr2_c_write_i32(&w_buf, &w_len, &w_cap, (int32_t)({var})) != 0) goto fail;"
),
};
let _ = writeln!(self.out, "{line}");
return Ok(());
}
// bitmask / bitset → write the holder integer (XTypes §7.3.1.2).
if let Some(helper) = self.bits_helper(&last) {
let prefix = Self::helper_call_prefix(helper);
let _ = writeln!(
self.out,
" if ({prefix}write_{helper}(&w_buf, &w_len, &w_cap, {var}) != 0) goto fail;"
);
return Ok(());
}
if let Some((cn, ndef)) = self.reg.structs.get(&last).cloned() {
// A recursive struct (self-referential through a sequence,
// or mutually recursive) is spliced through its own runtime
// body helper — inlining would recurse forever at codegen
// time (XTypes §7.4.5 / Bug G). Non-recursive structs are
// inline-encoded by value (no DHEADER: @final inline form).
if self.reg.is_recursive(&last) {
// A recursive struct spliced as a member/sequence-element
// is a self-contained value. Under XCDR2 a NON-final
// (@appendable/@mutable) aggregate is DELIMITED — it
// carries its own DHEADER (XTypes §7.4.3.5 / §7.4.4.4),
// exactly as its top-level `_encode` would emit. The
// body-only `_write_body` must therefore be wrapped in a
// DHEADER here, so each `sequence<Tree>` element matches
// the rust golden (per-node DHEADER). @final splices the
// bare body (no DHEADER).
let ext = extensibility_of(&ndef.annotations);
if matches!(ext, Extensibility::Final) {
let _ = writeln!(
self.out,
" if ({cn}_write_body(&({var}), &w_buf, &w_len, &w_cap, representation) != 0) goto fail;"
);
} else {
// Per-node DHEADER under XCDR2 only; XCDR1 splices the
// bare recursive body (no delimiter).
let _ = writeln!(self.out, " {{");
let _ = writeln!(
self.out,
" size_t rec_dheader_pos = 0; (void)rec_dheader_pos;"
);
let _ = writeln!(self.out, " if (representation) {{");
let _ = writeln!(
self.out,
" if (zerodds_xcdr2_c_grow(&w_buf, &w_cap, w_len + 4) != 0) goto fail;"
);
let _ =
writeln!(self.out, " rec_dheader_pos = w_len; w_len += 4;");
let _ = writeln!(self.out, " }}");
let _ = writeln!(
self.out,
" size_t rec_body_start = w_len; (void)rec_body_start;"
);
let _ = writeln!(
self.out,
" if ({cn}_write_body(&({var}), &w_buf, &w_len, &w_cap, representation) != 0) goto fail;"
);
let _ = writeln!(
self.out,
" if (representation) {{ zerodds_xcdr2_c_put_u32_at(w_buf, rec_dheader_pos, (uint32_t)(w_len - rec_body_start)); }}"
);
let _ = writeln!(self.out, " }}");
}
return Ok(());
}
return self.emit_nested_struct_write(var, &ndef);
}
if let Some((cn, udef)) = self.reg.unions.get(&last).cloned() {
if self.reg.is_recursive(&last) {
let _ = writeln!(
self.out,
" if ({cn}_write_body(&({var}), &w_buf, &w_len, &w_cap, representation) != 0) goto fail;"
);
return Ok(());
}
return self.emit_union_write(var, &udef);
}
Err(unsupported("unresolved scoped member (C backend)"))
}
TypeSpec::Fixed(f) => {
// fixed<P,S>: write the (P+2)/2 raw BCD octets (CORBA §9.3.2.7),
// no alignment, no length prefix, endian-independent.
let p = const_expr_to_u64(self.reg, &f.digits)
.ok_or_else(|| unsupported("fixed: non-constant digit count"))?;
let n = (p + 2) / 2;
let _ = writeln!(
self.out,
" for (size_t __fi = 0; __fi < {n}; __fi++) {{ if (zerodds_xcdr2_c_write_u8(&w_buf, &w_len, &w_cap, (uint8_t)(({var}).bcd[__fi])) != 0) goto fail; }}"
);
Ok(())
}
TypeSpec::Any => Err(unsupported("any")),
}
}
/// XCDR2 wstring (§7.4.4.6): uint32 byte-length (= 2*code-units), then the
/// UTF-16-LE code units, no NUL. C side holds a NUL-terminated `uint16_t*`.
fn emit_wstring_write(
&mut self,
var: &str,
bound: Option<&ConstExpr>,
) -> Result<(), CppGenError> {
let _ = writeln!(self.out, " {{");
let _ = writeln!(
self.out,
" const uint16_t* ws_p = (const uint16_t*)({var});"
);
let _ = writeln!(self.out, " uint32_t ws_n = 0;");
let _ = writeln!(self.out, " if (ws_p) while (ws_p[ws_n]) ++ws_n;");
// Bounded wstring<N>: reject a string longer than N code units.
if let Some(n) = self.eval_bound(bound) {
let _ = writeln!(self.out, " if (ws_n > {n}u) goto fail;");
}
let _ = writeln!(
self.out,
" if (zerodds_xcdr2_c_write_u32(&w_buf, &w_len, &w_cap, ws_n * 2u) != 0) goto fail;"
);
let _ = writeln!(
self.out,
" for (uint32_t wi = 0; wi < ws_n; ++wi) {{ if (zerodds_xcdr2_c_write_u16(&w_buf, &w_len, &w_cap, ws_p[wi]) != 0) goto fail; }}"
);
let _ = writeln!(self.out, " }}");
Ok(())
}
/// map<K,V> (§7.4.4.7): XCDR2 serializes a map as a sequence of (K,V)
/// pairs — DHEADER (byte-length) + uint32 entry-count, then each entry's
/// key followed by its value. C side: parallel `keys[]`/`vals[]` arrays.
fn emit_map_write(
&mut self,
var: &str,
key: &TypeSpec,
value: &TypeSpec,
bound: Option<&ConstExpr>,
) -> Result<(), CppGenError> {
let _ = writeln!(self.out, " {{");
// Bounded map<K,V,N>: reject more than N entries.
if let Some(n) = self.eval_bound(bound) {
let _ = writeln!(self.out, " if (({var}).len > {n}u) goto fail;");
}
// The map DHEADER is a uint32 -> 4-align before writing it (a map after a
// sub-4-byte member, e.g. a 2-byte @bit_bound enum, would otherwise land
// the DHEADER unaligned). Matches the rust reference + XCDR2 §7.4.1.
let _ = writeln!(
self.out,
" if (zerodds_xcdr2_c_pad_to(&w_buf, &w_len, &w_cap, 4) != 0) goto fail;"
);
// XCDR2 §7.4.3.5: a map carries a DHEADER only when its (key,value) element
// is NON-primitive. `map<long,long>` (both primitive) omits it — matching
// cdr-core `needs_collection_dheader(.., K::IS_PRIMITIVE && V::IS_PRIMITIVE)`
// and FastDDS/OpenDDS. (Same rule as the primitive-array PARRAY above.)
let map_dh = !(self.seq_elem_is_primitive(key) && self.seq_elem_is_primitive(value));
let _ = writeln!(
self.out,
" size_t map_dheader_pos = 0; (void)map_dheader_pos;"
);
if map_dh {
let _ = writeln!(self.out, " if (representation) {{");
let _ = writeln!(
self.out,
" if (zerodds_xcdr2_c_grow(&w_buf, &w_cap, w_len + 4) != 0) goto fail;"
);
let _ = writeln!(self.out, " map_dheader_pos = w_len; w_len += 4;");
let _ = writeln!(self.out, " }}");
}
let _ = writeln!(
self.out,
" size_t map_body_start = w_len; (void)map_body_start;"
);
let _ = writeln!(
self.out,
" if (zerodds_xcdr2_c_write_u32(&w_buf, &w_len, &w_cap, ({var}).len) != 0) goto fail;"
);
let mv = format!("mi{}", self.coll_depth);
let _ = writeln!(
self.out,
" for (uint32_t {mv} = 0; {mv} < ({var}).len; ++{mv}) {{"
);
self.coll_depth += 1;
let rk = self.emit_member_write(&format!("({var}).keys[{mv}]"), key);
let rv = if rk.is_ok() {
self.emit_member_write(&format!("({var}).vals[{mv}]"), value)
} else {
Ok(())
};
self.coll_depth -= 1;
rk.and(rv)?;
let _ = writeln!(self.out, " }}");
if map_dh {
let _ = writeln!(
self.out,
" if (representation) {{ zerodds_xcdr2_c_put_u32_at(w_buf, map_dheader_pos, (uint32_t)(w_len - map_body_start)); }}"
);
}
let _ = writeln!(self.out, " }}");
Ok(())
}
/// Discriminated union (§7.4.4.5): write the discriminator, then the member
/// selected by it. C side: a tagged union `struct { Disc _d; union {...} _u; }`.
fn emit_union_write(&mut self, var: &str, udef: &UnionDef) -> Result<(), CppGenError> {
// A union honours its extensibility wherever it appears (top-level OR as a
// member / sequence element): @appendable/@mutable carry a 4-aligned uint32
// DHEADER over [disc + branch]; @final does not. Previously only the
// top-level TypeSupport wrapped the DHEADER, so a union *element* (e.g.
// sequence<Sel>) was emitted without one — cross-PSM wire divergence.
let u_dheader = !matches!(extensibility_of(&udef.annotations), Extensibility::Final);
if u_dheader {
let _ = writeln!(self.out, " {{");
let _ = writeln!(
self.out,
" size_t u_dheader_pos = 0; (void)u_dheader_pos;"
);
let _ = writeln!(self.out, " if (representation) {{");
let _ = writeln!(
self.out,
" if (zerodds_xcdr2_c_pad_to(&w_buf, &w_len, &w_cap, 4) != 0) goto fail;"
);
let _ = writeln!(
self.out,
" if (zerodds_xcdr2_c_grow(&w_buf, &w_cap, w_len + 4) != 0) goto fail;"
);
let _ = writeln!(self.out, " u_dheader_pos = w_len; w_len += 4;");
let _ = writeln!(self.out, " }}");
let _ = writeln!(
self.out,
" size_t u_body_start = w_len; (void)u_body_start;"
);
}
// Discriminator.
let disc_ts = switch_type_spec(&udef.switch_type);
self.emit_member_write(&format!("({var})._d"), &disc_ts)?;
let _ = writeln!(self.out, " switch (({var})._d) {{");
let mut default_case: Option<&Case> = None;
for case in &udef.cases {
let field = union_case_field(case);
let mut has_value_label = false;
for label in &case.labels {
match label {
CaseLabel::Value(expr) => {
has_value_label = true;
let lit = self.case_label_literal(expr)?;
let _ = writeln!(self.out, " case {lit}:");
}
CaseLabel::Default => {
default_case = Some(case);
}
}
}
if has_value_label {
self.emit_member_write(&format!("({var})._u.{field}"), &case.element.type_spec)?;
let _ = writeln!(self.out, " break;");
}
}
if let Some(case) = default_case {
let field = union_case_field(case);
let _ = writeln!(self.out, " default:");
self.emit_member_write(&format!("({var})._u.{field}"), &case.element.type_spec)?;
let _ = writeln!(self.out, " break;");
} else {
let _ = writeln!(self.out, " default: break;");
}
let _ = writeln!(self.out, " }}");
if u_dheader {
let _ = writeln!(
self.out,
" if (representation) {{ zerodds_xcdr2_c_put_u32_at(w_buf, u_dheader_pos, (uint32_t)(w_len - u_body_start)); }}"
);
let _ = writeln!(self.out, " }}");
}
Ok(())
}
/// Inline-encode a nested struct's members by value (Plain-CDR2, no DHEADER).
/// zerodds-lint: recursion-depth 64 (bounded by IDL nesting)
fn emit_nested_struct_write(&mut self, var: &str, ndef: &StructDef) -> Result<(), CppGenError> {
// FINDING T1b: a NON-`@final` nested struct is delimited — it carries its
// own leading DHEADER, exactly as its top-level `_encode` would emit
// (XTypes §7.4.3.5 / §7.4.4.4). When such a struct is a @mutable member
// or a sequence element, its DHEADER doubles as the EMHEADER NEXTINT
// (LengthCode 5, picked by `mutable_compact_lc`). `@mutable` additionally
// frames each of its own members with an EMHEADER; `@appendable` splices
// its members tight after the DHEADER. A `@final` nested struct has no
// DHEADER and tight-packs its body (the original inline form).
let ext = extensibility_of(&ndef.annotations);
if !matches!(ext, Extensibility::Final) {
// XCDR2: the non-@final nested struct carries its own DHEADER.
// XCDR1: no DHEADER (classic CDR) — tight-packed body. (A nested
// @mutable struct under XCDR1 would need PL_CDR1 framing; the corpus
// has only @appendable nested structs, which splice tight.)
let _ = writeln!(self.out, " {{");
let _ = writeln!(
self.out,
" size_t nst_dheader_pos = 0; (void)nst_dheader_pos;"
);
let _ = writeln!(self.out, " if (representation) {{");
let _ = writeln!(
self.out,
" if (zerodds_xcdr2_c_grow(&w_buf, &w_cap, w_len + 4) != 0) goto fail;"
);
let _ = writeln!(self.out, " nst_dheader_pos = w_len; w_len += 4;");
let _ = writeln!(self.out, " }}");
let _ = writeln!(
self.out,
" size_t nst_body_start = w_len; (void)nst_body_start;"
);
if matches!(ext, Extensibility::Mutable) {
self.emit_mutable_member_writes_base(ndef, &format!("({var})."))?;
} else {
self.emit_nested_struct_inline_write(var, ndef)?;
}
let _ = writeln!(
self.out,
" if (representation) {{ zerodds_xcdr2_c_put_u32_at(w_buf, nst_dheader_pos, (uint32_t)(w_len - nst_body_start)); }}"
);
let _ = writeln!(self.out, " }}");
return Ok(());
}
self.emit_nested_struct_inline_write(var, ndef)
}
/// Tight-packed (`@final`) inline write of a nested struct's members by
/// value — Plain-CDR2, no DHEADER, no per-member EMHEADER. Also serves as the
/// `@appendable` body (members after the DHEADER frame).
/// zerodds-lint: recursion-depth 64 (bounded by IDL nesting)
fn emit_nested_struct_inline_write(
&mut self,
var: &str,
ndef: &StructDef,
) -> Result<(), CppGenError> {
for member in &ndef.members {
let optional = is_optional(&member.annotations);
for decl in &member.declarators {
let f = &decl.name().text;
let dims = effective_array_dims(self.reg, &member.type_spec, decl)?;
if optional {
// Plain-CDR2 optional (XTypes §7.4.3): a boolean presence
// flag, then the value only if present. The nested struct's
// own typedef carries the `<f>_present` companion (emitted by
// emit_struct), so the field access is symmetric.
let _ = writeln!(
self.out,
" if (zerodds_xcdr2_c_write_u8(&w_buf, &w_len, &w_cap, ({var}).{f}_present ? 1 : 0) != 0) goto fail;"
);
let _ = writeln!(self.out, " if (({var}).{f}_present) {{");
self.emit_array_or_scalar_write(
&format!("({var}).{f}"),
&member.type_spec,
&dims,
)?;
let _ = writeln!(self.out, " }}");
} else {
self.emit_array_or_scalar_write(
&format!("({var}).{f}"),
&member.type_spec,
&dims,
)?;
}
}
}
Ok(())
}
fn emit_primitive_write(&mut self, var: &str, p: PrimitiveType) -> Result<(), CppGenError> {
let helper = match p {
PrimitiveType::Boolean | PrimitiveType::Octet => "u8",
PrimitiveType::Char => "u8",
PrimitiveType::WideChar => "u16",
PrimitiveType::Integer(IntegerType::Short)
| PrimitiveType::Integer(IntegerType::Int16) => "i16",
PrimitiveType::Integer(IntegerType::UShort)
| PrimitiveType::Integer(IntegerType::UInt16) => "u16",
PrimitiveType::Integer(IntegerType::Long)
| PrimitiveType::Integer(IntegerType::Int32) => "i32",
PrimitiveType::Integer(IntegerType::ULong)
| PrimitiveType::Integer(IntegerType::UInt32) => "u32",
PrimitiveType::Integer(IntegerType::LongLong)
| PrimitiveType::Integer(IntegerType::Int64) => "i64",
PrimitiveType::Integer(IntegerType::ULongLong)
| PrimitiveType::Integer(IntegerType::UInt64) => "u64",
PrimitiveType::Integer(IntegerType::Int8) => "i8",
PrimitiveType::Integer(IntegerType::UInt8) => "u8",
PrimitiveType::Floating(FloatingType::Float) => "f32",
PrimitiveType::Floating(FloatingType::Double) => "f64",
PrimitiveType::Floating(FloatingType::LongDouble) => {
return Err(unsupported("long double"));
}
};
if matches!(helper, "u64" | "i64" | "f64") {
// 8-byte primitive: XCDR2 caps alignment at 4 (`zd_x2_*`), XCDR1 uses
// the shared align-8 helper (classic CDR, MAXALIGN 8). `representation`
// is in scope in `_encode_repr` / `_write_body`.
let _ = writeln!(
self.out,
" if (representation) {{ if (zd_x2_write_{helper}(&w_buf, &w_len, &w_cap, {var}) != 0) goto fail; }}"
);
let _ = writeln!(
self.out,
" else {{ if (zerodds_xcdr2_c_write_{helper}(&w_buf, &w_len, &w_cap, {var}) != 0) goto fail; }}"
);
return Ok(());
}
let prefix = Self::helper_call_prefix(helper);
let _ = writeln!(
self.out,
" if ({prefix}write_{helper}(&w_buf, &w_len, &w_cap, {var}) != 0) goto fail;"
);
Ok(())
}
fn emit_sequence_write(
&mut self,
var: &str,
elem: &TypeSpec,
bound: Option<&ConstExpr>,
) -> Result<(), CppGenError> {
// Sequence-Repraesentation in C: `struct { uint32_t len; T* elems; }`.
// XCDR2 §7.4.3.5: non-primitive elements (string, struct, nested
// sequence, …) get a DHEADER (uint32 = byte length of
// [count + elements]) prepended; primitives (incl. enum→int32) do not.
// Cyclone-DDS-verified.
let non_primitive = !self.seq_elem_is_primitive(elem);
// Block-scopes the DHEADER locals (multiple sequences per struct).
let _ = writeln!(self.out, " {{");
// Bounded sequence<T,N>: reject more than N elements (XTypes §7.4.13.4.2)
// — over-bound must error, not silently corrupt the wire.
if let Some(n) = self.eval_bound(bound) {
let _ = writeln!(self.out, " if (({var}).len > {n}u) goto fail;");
}
if non_primitive {
// DHEADER only under XCDR2; XCDR1 (classic CDR) has no collection
// delimiter — just the count + elements.
let _ = writeln!(
self.out,
" size_t seq_dheader_pos = 0; (void)seq_dheader_pos;"
);
let _ = writeln!(self.out, " if (representation) {{");
let _ = writeln!(
self.out,
" if (zerodds_xcdr2_c_grow(&w_buf, &w_cap, w_len + 4) != 0) goto fail;"
);
let _ = writeln!(self.out, " seq_dheader_pos = w_len; w_len += 4;");
let _ = writeln!(self.out, " }}");
let _ = writeln!(
self.out,
" size_t seq_body_start = w_len; (void)seq_body_start;"
);
}
let _ = writeln!(
self.out,
" if (zerodds_xcdr2_c_write_u32(&w_buf, &w_len, &w_cap, ({var}).len) != 0) goto fail;"
);
let iv = format!("i{}", self.coll_depth);
let _ = writeln!(
self.out,
" for (uint32_t {iv} = 0; {iv} < ({var}).len; ++{iv}) {{"
);
// Delegate the element body to the generic member writer so structs,
// enums and nested sequences (sequence<sequence<T>>) all work (#43,
// sequence<non-primitive T>). The element loop counter is depth-scoped
// so a nested sequence does not shadow the outer index.
self.coll_depth += 1;
let r = self.emit_member_write(&format!("({var}).elems[{iv}]"), elem);
self.coll_depth -= 1;
r?;
let _ = writeln!(self.out, " }}");
if non_primitive {
let _ = writeln!(
self.out,
" if (representation) {{ zerodds_xcdr2_c_put_u32_at(w_buf, seq_dheader_pos, (uint32_t)(w_len - seq_body_start)); }}"
);
}
let _ = writeln!(self.out, " }}");
Ok(())
}
fn emit_mutable_member_writes(&mut self, def: &StructDef) -> Result<(), CppGenError> {
self.emit_mutable_member_writes_base(def, "s->")
}
/// Emits the per-member EMHEADER-framed writes of a @mutable struct, with the
/// member accessors prefixed by `base` (`"s->"` for a top-level encode, or
/// `"(<var>)."` when this struct is a nested @mutable member / sequence
/// element). Splits out so a nested @mutable struct reuses the SAME framing.
fn emit_mutable_member_writes_base(
&mut self,
def: &StructDef,
base: &str,
) -> Result<(), CppGenError> {
// XTypes 1.3 §7.3.4.3: `@autoid` defaults to SEQUENTIAL — a @mutable member
// without explicit `@id(N)` takes the next 0-based declaration-order id
// (vendor-confirmed byte-identical to CycloneDDS). Explicit `@id(N)` sets
// the id and resets the counter to N+1. (Previously the C backend rejected
// auto-id @mutable members outright.)
let mut auto_id: u32 = 0;
for member in &def.members {
let id = id_annotation(&member.annotations).unwrap_or(auto_id);
auto_id = id + 1;
let dims_per_decl: Vec<Vec<u64>> = member
.declarators
.iter()
.map(|d| effective_array_dims(self.reg, &member.type_spec, d))
.collect::<Result<_, _>>()?;
let optional = is_optional(&member.annotations);
for (decl, dims) in member.declarators.iter().zip(dims_per_decl.iter()) {
let f = format!("{base}{}", decl.name().text);
let f = f.as_str();
// Bug XV-mut: pick the COMPACT length code (XTypes 1.3 §7.4.3.4.2)
// mirroring the Rust reference (`mutable_member_length_code`):
// * a fixed-size scalar primitive uses LC by wire size
// (1→LC0, 2→LC1, 4→LC2, 8→LC3) — NO NEXTINT, body inline;
// * a string/wstring uses LC5 — its own uint32 length prefix
// doubles as the NEXTINT (no separate NEXTINT);
// * everything else (arrays, sequences, maps, nested aggregates,
// long double) falls back to the universal LC4 + NEXTINT.
let compact_lc = mutable_compact_lc(self.reg, &member.type_spec, dims, optional);
match compact_lc {
Some(lc) => {
// LC0..3 (fixed primitive) and LC5 (string): EMHEADER then
// the body straight into w_buf, NO separate NEXTINT.
let emheader: u32 = (u32::from(lc) << 28) | id;
let _ = writeln!(self.out, " {{");
let _ = writeln!(
self.out,
" if (zerodds_xcdr2_c_write_u32(&w_buf, &w_len, &w_cap, 0x{emheader:08X}u) != 0) goto fail;"
);
self.emit_array_or_scalar_write(f, &member.type_spec, dims)?;
let _ = writeln!(self.out, " }}");
}
None => {
// Universal LC4 (NEXTINT = body byte length), back-patched.
let emheader: u32 = (4u32 << 28) | id;
let _ = writeln!(self.out, " {{");
let _ = writeln!(
self.out,
" if (zerodds_xcdr2_c_write_u32(&w_buf, &w_len, &w_cap, 0x{emheader:08X}u) != 0) goto fail;"
);
let _ = writeln!(
self.out,
" if (zerodds_xcdr2_c_grow(&w_buf, &w_cap, w_len + 4) != 0) goto fail;"
);
let _ = writeln!(self.out, " size_t m_nextint_pos = w_len;");
let _ = writeln!(self.out, " w_len += 4;");
let _ = writeln!(self.out, " size_t m_body_start = w_len;");
self.emit_array_or_scalar_write(f, &member.type_spec, dims)?;
let _ = writeln!(
self.out,
" uint32_t m_body_len = (uint32_t)(w_len - m_body_start);"
);
let _ = writeln!(
self.out,
" zerodds_xcdr2_c_put_u32_at(w_buf, m_nextint_pos, m_body_len);"
);
let _ = writeln!(self.out, " }}");
}
}
}
}
Ok(())
}
/// Emits the @mutable members under XCDR1 as a PL_CDR1 parameter list. The C
/// runtime aligns relative to the buffer start, but a PL_CDR1 member body
/// must align param-relative (origin 0) — so each member body is encoded
/// into a FRESH temp buffer (save/swap of `w_buf`), then spliced through
/// `pl1_write_member` (PID header + body + pad-to-4). Ends with the sentinel.
/// Mirrors `emit_mutable_member_writes_base`'s id assignment.
fn emit_pl_cdr1_member_writes(&mut self, def: &StructDef) -> Result<(), CppGenError> {
let mut auto_id: u32 = 0;
for member in &def.members {
let id = id_annotation(&member.annotations).unwrap_or(auto_id);
auto_id = id + 1;
let dims_per_decl: Vec<Vec<u64>> = member
.declarators
.iter()
.map(|d| effective_array_dims(self.reg, &member.type_spec, d))
.collect::<Result<_, _>>()?;
let optional = is_optional(&member.annotations);
for (decl, dims) in member.declarators.iter().zip(dims_per_decl.iter()) {
let fname = decl.name().text.clone();
let f = format!("s->{fname}");
if optional {
// PL_CDR1 optional: present -> emit the parameter; absent ->
// omit it (no present flag; absence = not in the list).
let _ = writeln!(self.out, " if (s->{fname}_present) {{");
}
let _ = writeln!(self.out, " {{");
// Save the main buffer; redirect w_* to a fresh temp so the body
// is encoded param-relative (origin 0).
let _ = writeln!(
self.out,
" uint8_t* m_buf = w_buf; size_t m_len = w_len; size_t m_cap = w_cap;"
);
let _ = writeln!(self.out, " w_buf = NULL; w_len = 0; w_cap = 0;");
self.emit_array_or_scalar_write(&f, &member.type_spec, dims)?;
// Splice [PID header][temp body][pad] into the saved main buffer.
let _ = writeln!(
self.out,
" if (zerodds_xcdr2_c_pl1_write_member(&m_buf, &m_len, &m_cap, {id}u, w_buf, w_len) != 0) {{ free(w_buf); w_buf = m_buf; w_len = m_len; w_cap = m_cap; goto fail; }}"
);
let _ = writeln!(
self.out,
" free(w_buf); w_buf = m_buf; w_len = m_len; w_cap = m_cap;"
);
let _ = writeln!(self.out, " }}");
if optional {
let _ = writeln!(self.out, " }}");
}
}
}
// PID_LIST_END sentinel terminates the parameter list.
let _ = writeln!(
self.out,
" if (zerodds_xcdr2_c_pl1_sentinel(&w_buf, &w_len, &w_cap) != 0) goto fail;"
);
Ok(())
}
/// Reads a @mutable struct under XCDR1 as a PL_CDR1 parameter list: read each
/// member header, dispatch on its id, and decode the body from the parameter
/// sub-buffer (`buf + body_start`, local pos 0 → param-relative alignment),
/// then advance past the (padded) parameter. Symmetric to
/// `emit_pl_cdr1_member_writes`; mirrors its id assignment.
fn emit_pl_cdr1_member_reads(&mut self, def: &StructDef) -> Result<(), CppGenError> {
let _ = writeln!(self.out, " while (pos + 4 <= len) {{");
let _ = writeln!(
self.out,
" uint32_t pl_id = 0; size_t pl_blen = 0; int pl_end = 0;"
);
let _ = writeln!(
self.out,
" if (zerodds_xcdr2_c_pl1_read_header(buf, len, &pos, &pl_id, &pl_blen, &pl_end, zd_be) != 0) return -7;"
);
let _ = writeln!(self.out, " if (pl_end) break;");
let _ = writeln!(self.out, " if (pos + pl_blen > len) return -7;");
let _ = writeln!(self.out, " size_t pl_body_start = pos;");
let _ = writeln!(self.out, " switch (pl_id) {{");
let mut auto_id: u32 = 0;
for member in &def.members {
let id = id_annotation(&member.annotations).unwrap_or(auto_id);
auto_id = id + 1;
let dims_per_decl: Vec<Vec<u64>> = member
.declarators
.iter()
.map(|d| effective_array_dims(self.reg, &member.type_spec, d))
.collect::<Result<_, _>>()?;
let optional = is_optional(&member.annotations);
for (decl, dims) in member.declarators.iter().zip(dims_per_decl.iter()) {
let fname = decl.name().text.clone();
let f = format!("s->{fname}");
let _ = writeln!(self.out, " case {id}u: {{");
// Redirect the cursor to the parameter body (origin 0).
let _ = writeln!(
self.out,
" const uint8_t* mb = buf; size_t ml = len; size_t mp = pos;"
);
let _ = writeln!(
self.out,
" buf = mb + pl_body_start; len = pl_blen; pos = 0;"
);
if optional {
let _ = writeln!(self.out, " s->{fname}_present = 1;");
}
self.emit_array_or_scalar_read(&f, &member.type_spec, dims)?;
let _ = writeln!(self.out, " buf = mb; len = ml; pos = mp;");
let _ = writeln!(self.out, " break;");
let _ = writeln!(self.out, " }}");
}
}
let _ = writeln!(self.out, " default: break;");
let _ = writeln!(self.out, " }}");
let _ = writeln!(self.out, " pos = pl_body_start + pl_blen;");
// Skip the trailing pad to the next 4-byte boundary.
let _ = writeln!(
self.out,
" {{ size_t pl_pad = (4u - (pl_blen % 4u)) % 4u; pos += pl_pad; if (pos > len) pos = len; }}"
);
let _ = writeln!(self.out, " }}");
Ok(())
}
fn emit_decode_body(
&mut self,
c_name: &str,
def: &StructDef,
ext: Extensibility,
) -> Result<(), CppGenError> {
// The decoder is symmetric to the encoder; builds the Rust-stack-style
// BufferReader via helper inline functions from `zerodds_xcdr2.h`.
// `_decode`/`_decode_e` keep the XCDR2 ABI (typesupport .decode pointer);
// `_decode_repr` is the representation-aware entry (header typedef
// `zerodds_decode_repr_fn`); all funnel into `_decode_core(buf,len,out,
// zd_be,representation)`. representation: 0=XCDR1 (no DHEADER, max_align 8,
// @mutable=PL_CDR1), 1=XCDR2.
let _ = writeln!(
self.out,
"static int {c_name}_decode_core(const uint8_t* buf, size_t len, void* out_sample, int zd_be, int representation);"
);
let _ = writeln!(
self.out,
"static int {c_name}_decode(const uint8_t* buf, size_t len, void* out_sample) {{ return {c_name}_decode_core(buf, len, out_sample, 0, 1); }}"
);
let _ = writeln!(
self.out,
"static int {c_name}_decode_e(const uint8_t* buf, size_t len, void* out_sample, int zd_be) {{ return {c_name}_decode_core(buf, len, out_sample, zd_be, 1); }}"
);
let _ = writeln!(
self.out,
"static int {c_name}_decode_repr(const uint8_t* buf, size_t len, uint8_t representation, void* out_sample) {{ return {c_name}_decode_core(buf, len, out_sample, 0, representation ? 1 : 0); }}"
);
let _ = writeln!(
self.out,
"static int {c_name}_decode_core(const uint8_t* buf, size_t len, void* out_sample, int zd_be, int representation) {{"
);
let _ = writeln!(self.out, " {c_name}_t* s = ({c_name}_t*)out_sample;");
let _ = writeln!(self.out, " size_t pos = 0;");
let _ = writeln!(
self.out,
" size_t zd_ma = representation ? 4 : 8; (void)zd_ma;"
);
match ext {
Extensibility::Final => {
self.emit_struct_body_reads(def)?;
}
Extensibility::Appendable => {
// XCDR2 reads the DHEADER; XCDR1 has none (body runs to `len`).
let _ = writeln!(self.out, " size_t body_end = len;");
let _ = writeln!(self.out, " if (representation) {{");
let _ = writeln!(self.out, " uint32_t dheader = 0;");
let _ = writeln!(
self.out,
" if (zerodds_xcdr2_c_read_u32(buf, len, &pos, &dheader, zd_be) != 0) return -7;"
);
let _ = writeln!(self.out, " body_end = pos + dheader;");
let _ = writeln!(self.out, " if (body_end > len) return -7;");
let _ = writeln!(self.out, " }}");
self.emit_struct_body_reads(def)?;
let _ = writeln!(self.out, " if (representation) pos = body_end;");
}
Extensibility::Mutable => {
let _ = writeln!(self.out, " if (!representation) {{");
self.emit_pl_cdr1_member_reads(def)?;
let _ = writeln!(self.out, " }} else {{");
let _ = writeln!(self.out, " uint32_t dheader = 0;");
let _ = writeln!(
self.out,
" if (zerodds_xcdr2_c_read_u32(buf, len, &pos, &dheader, zd_be) != 0) return -7;"
);
let _ = writeln!(self.out, " size_t body_end = pos + dheader;");
let _ = writeln!(self.out, " if (body_end > len) return -7;");
let _ = writeln!(self.out, " while (pos < body_end) {{");
let _ = writeln!(self.out, " uint32_t emheader = 0;");
let _ = writeln!(
self.out,
" if (zerodds_xcdr2_c_read_u32(buf, len, &pos, &emheader, zd_be) != 0) return -7;"
);
let _ = writeln!(self.out, " uint32_t mid = emheader & 0x0FFFFFFFu;");
let _ = writeln!(self.out, " uint32_t lc = (emheader >> 28) & 0x7u;");
let _ = writeln!(self.out, " uint32_t nextint = 0;");
let _ = writeln!(self.out, " size_t body_len = 0;");
// Bug XV-mut: compact length codes. LC0..3 = fixed 1/2/4/8-byte
// bodies, NO NEXTINT. LC4/6/7 carry a separate NEXTINT (consumed
// here). LC5 = the body's OWN leading uint32 length word doubles as
// the NEXTINT — so it must NOT be consumed separately; we PEEK it
// (read via a scratch position) and leave `pos` at the body start so
// the member reader sees its length prefix intact.
let _ = writeln!(
self.out,
" if (lc == 0) body_len = 1; else if (lc == 1) body_len = 2; else if (lc == 2) body_len = 4; else if (lc == 3) body_len = 8;"
);
let _ = writeln!(self.out, " else if (lc == 5) {{");
let _ = writeln!(self.out, " size_t peek_pos = pos;");
let _ = writeln!(
self.out,
" if (zerodds_xcdr2_c_read_u32(buf, len, &peek_pos, &nextint, zd_be) != 0) return -7;"
);
let _ = writeln!(self.out, " body_len = 4u + (size_t)nextint;");
let _ = writeln!(self.out, " }} else {{");
let _ = writeln!(
self.out,
" if (zerodds_xcdr2_c_read_u32(buf, len, &pos, &nextint, zd_be) != 0) return -7;"
);
let _ = writeln!(self.out, " body_len = nextint;");
let _ = writeln!(self.out, " }}");
let _ = writeln!(
self.out,
" if (pos + body_len > body_end) return -7;"
);
self.emit_mutable_member_dispatch(def)?;
let _ = writeln!(self.out, " }}"); // close while
let _ = writeln!(self.out, " }}"); // close the XCDR2 `else`
}
}
let _ = writeln!(self.out, " (void)s;");
let _ = writeln!(self.out, " return 0;");
let _ = writeln!(self.out, "}}");
let _ = writeln!(self.out);
Ok(())
}
fn emit_struct_body_reads(&mut self, def: &StructDef) -> Result<(), CppGenError> {
for member in &def.members {
let optional = is_optional(&member.annotations);
for decl in &member.declarators {
let f = &decl.name().text;
let dims = effective_array_dims(self.reg, &member.type_spec, decl)?;
if optional {
let _ = writeln!(self.out, " {{");
let _ = writeln!(self.out, " uint8_t present = 0;");
let _ = writeln!(
self.out,
" if (zerodds_xcdr2_c_read_u8(buf, len, &pos, &present, zd_be) != 0) return -7;"
);
let _ = writeln!(self.out, " s->{f}_present = present;");
let _ = writeln!(self.out, " if (present) {{");
self.emit_array_or_scalar_read(&format!("s->{f}"), &member.type_spec, &dims)?;
let _ = writeln!(self.out, " }}");
let _ = writeln!(self.out, " }}");
} else {
self.emit_array_or_scalar_read(&format!("s->{f}"), &member.type_spec, &dims)?;
}
}
}
Ok(())
}
fn emit_array_or_scalar_read(
&mut self,
var: &str,
type_spec: &TypeSpec,
dims: &[u64],
) -> Result<(), CppGenError> {
if dims.is_empty() {
return self.emit_member_read(var, type_spec);
}
// Bug XV-arr: symmetric to the encode — only a NON-primitive-element array
// carries a collection DHEADER. A PARRAY (primitive element, any dims) has
// none, so nothing to read+discard.
let needs_dheader = !self.seq_elem_is_primitive(type_spec);
if needs_dheader {
let _ = writeln!(
self.out,
" if (representation) {{ uint32_t arr_dheader = 0; if (zerodds_xcdr2_c_read_u32(buf, len, &pos, &arr_dheader, zd_be) != 0) return -7; (void)arr_dheader; }}"
);
}
let mut acc = var.to_string();
for (d, _n) in dims.iter().enumerate() {
let iv = format!("ri{d}");
let _ = writeln!(
self.out,
" for (uint32_t {iv} = 0; {iv} < {n}; ++{iv}) {{",
n = dims[d]
);
acc = format!("{acc}[{iv}]");
}
self.emit_member_read(&acc, type_spec)?;
for _ in dims {
let _ = writeln!(self.out, " }}");
}
Ok(())
}
/// zerodds-lint: recursion-depth 64 (nested struct members; bounded by IDL)
fn emit_member_read(&mut self, var: &str, type_spec: &TypeSpec) -> Result<(), CppGenError> {
let resolved = resolve_alias(self.reg, type_spec);
match &resolved {
TypeSpec::Primitive(p) => {
let helper = primitive_helper(*p)?;
if matches!(helper, "u64" | "i64" | "f64") {
// 8-byte primitive: XCDR2 caps alignment at 4 (`zd_x2_*`),
// XCDR1 reads at MAXALIGN 8 via the shared helper.
let _ = writeln!(
self.out,
" if (representation) {{ if (zd_x2_read_{helper}(buf, len, &pos, &({var}), zd_be) != 0) return -7; }}"
);
let _ = writeln!(
self.out,
" else {{ if (zerodds_xcdr2_c_read_{helper}(buf, len, &pos, &({var}), zd_be) != 0) return -7; }}"
);
return Ok(());
}
let prefix = Self::helper_call_prefix(helper);
let _ = writeln!(
self.out,
" if ({prefix}read_{helper}(buf, len, &pos, &({var}), zd_be) != 0) return -7;"
);
Ok(())
}
TypeSpec::String(st) => {
if st.wide {
return self.emit_wstring_read(var);
}
let _ = writeln!(
self.out,
" if (zerodds_xcdr2_c_read_string(buf, len, &pos, &({var}), zd_be) != 0) return -7;"
);
Ok(())
}
TypeSpec::Sequence(seq) => self.emit_sequence_read(var, &seq.elem),
TypeSpec::Map(m) => self.emit_map_read(var, &m.key, &m.value),
TypeSpec::Scoped(sc) => {
let last = scoped_last(sc).ok_or_else(|| unsupported("empty scoped name"))?;
if self.reg.enums.contains_key(&last) {
// Read the @bit_bound-width holder, sign-extend to the int32
// in-memory enum (XTypes §7.4.5.1).
let bytes = self.reg.enum_bytes.get(&last).copied().unwrap_or(4);
let line = match bytes {
1 => format!(
" {{ uint8_t zd_e8 = 0; if (zerodds_xcdr2_c_read_u8(buf, len, &pos, &zd_e8, zd_be) != 0) return -7; ({var}) = (int32_t)(int8_t)zd_e8; }}"
),
2 => format!(
" {{ uint16_t zd_e16 = 0; if (zerodds_xcdr2_c_read_u16(buf, len, &pos, &zd_e16, zd_be) != 0) return -7; ({var}) = (int32_t)(int16_t)zd_e16; }}"
),
_ => format!(
" if (zerodds_xcdr2_c_read_i32(buf, len, &pos, (int32_t*)&({var}), zd_be) != 0) return -7;"
),
};
let _ = writeln!(self.out, "{line}");
return Ok(());
}
// bitmask / bitset → read the holder integer (XTypes §7.3.1.2).
if let Some(helper) = self.bits_helper(&last) {
let prefix = Self::helper_call_prefix(helper);
let _ = writeln!(
self.out,
" if ({prefix}read_{helper}(buf, len, &pos, &({var}), zd_be) != 0) return -7;"
);
return Ok(());
}
if let Some((cn, ndef)) = self.reg.structs.get(&last).cloned() {
if self.reg.is_recursive(&last) {
// Symmetric to the encode: a NON-final recursive struct is
// DHEADER-delimited — read+discard its DHEADER before its
// body. (XTypes §7.4.3.5 / §7.4.4.4.)
let ext = extensibility_of(&ndef.annotations);
if !matches!(ext, Extensibility::Final) {
let _ = writeln!(
self.out,
" if (representation) {{ uint32_t rec_dheader = 0; if (zerodds_xcdr2_c_read_u32(buf, len, &pos, &rec_dheader, zd_be) != 0) return -7; (void)rec_dheader; }}"
);
}
let _ = writeln!(
self.out,
" if ({cn}_read_body(buf, len, &pos, &({var}), zd_be, representation) != 0) return -7;"
);
return Ok(());
}
return self.emit_nested_struct_read(var, &ndef);
}
if let Some((cn, udef)) = self.reg.unions.get(&last).cloned() {
if self.reg.is_recursive(&last) {
let _ = writeln!(
self.out,
" if ({cn}_read_body(buf, len, &pos, &({var}), zd_be, representation) != 0) return -7;"
);
return Ok(());
}
return self.emit_union_read(var, &udef);
}
Err(unsupported("unresolved scoped member read (C backend)"))
}
TypeSpec::Fixed(f) => {
// fixed<P,S>: read the (P+2)/2 raw BCD octets into `.bcd[]`.
let p = const_expr_to_u64(self.reg, &f.digits)
.ok_or_else(|| unsupported("fixed: non-constant digit count"))?;
let n = (p + 2) / 2;
let _ = writeln!(
self.out,
" for (size_t __fi = 0; __fi < {n}; __fi++) {{ uint8_t __fb; if (zerodds_xcdr2_c_read_u8(buf, len, &pos, &__fb, zd_be) != 0) return -7; ({var}).bcd[__fi] = __fb; }}"
);
Ok(())
}
_ => Err(unsupported("complex member read")),
}
}
/// zerodds-lint: recursion-depth 64 (bounded by IDL nesting)
fn emit_nested_struct_read(&mut self, var: &str, ndef: &StructDef) -> Result<(), CppGenError> {
// Symmetric to `emit_nested_struct_write`: a NON-`@final` nested struct is
// DHEADER-delimited. `@mutable` parses the EMHEADER member loop bounded by
// its own DHEADER; `@appendable` reads its members tight after the
// DHEADER; `@final` reads the bare inline body. The depth counter scopes
// the local names so nested @mutable structs don't collide.
let ext = extensibility_of(&ndef.annotations);
if !matches!(ext, Extensibility::Final) {
let d = self.coll_depth;
self.coll_depth += 1;
let r = self.emit_nested_struct_read_delimited(var, ndef, ext, d);
self.coll_depth -= 1;
return r;
}
self.emit_nested_struct_inline_read(var, ndef)
}
/// Reads a DHEADER-delimited nested struct (`@appendable` / `@mutable`). For
/// `@mutable`, runs the EMHEADER member-id dispatch loop bounded by the
/// struct's own DHEADER; for `@appendable`, reads members tight then snaps to
/// the DHEADER end. `depth` scopes the C local names.
fn emit_nested_struct_read_delimited(
&mut self,
var: &str,
ndef: &StructDef,
ext: Extensibility,
depth: u32,
) -> Result<(), CppGenError> {
let _ = writeln!(self.out, " {{");
// XCDR2: read the nested struct's DHEADER (bounds the body). XCDR1: no
// DHEADER — the body runs inline (no separate bound). (A @mutable nested
// struct under XCDR1 would be PL_CDR1; the corpus has only @appendable
// nested structs.)
let _ = writeln!(
self.out,
" size_t nst_end{depth} = len; (void)nst_end{depth};"
);
let _ = writeln!(self.out, " if (representation) {{");
let _ = writeln!(self.out, " uint32_t nst_dheader{depth} = 0;");
let _ = writeln!(
self.out,
" if (zerodds_xcdr2_c_read_u32(buf, len, &pos, &nst_dheader{depth}, zd_be) != 0) return -7;"
);
let _ = writeln!(
self.out,
" nst_end{depth} = pos + nst_dheader{depth};"
);
let _ = writeln!(self.out, " if (nst_end{depth} > len) return -7;");
let _ = writeln!(self.out, " }}");
if matches!(ext, Extensibility::Mutable) {
let _ = writeln!(
self.out,
" while (representation && pos < nst_end{depth}) {{"
);
let _ = writeln!(self.out, " uint32_t emheader{depth} = 0;");
let _ = writeln!(
self.out,
" if (zerodds_xcdr2_c_read_u32(buf, len, &pos, &emheader{depth}, zd_be) != 0) return -7;"
);
let _ = writeln!(
self.out,
" uint32_t mid{depth} = emheader{depth} & 0x0FFFFFFFu;"
);
let _ = writeln!(
self.out,
" uint32_t lc{depth} = (emheader{depth} >> 28) & 0x7u;"
);
let _ = writeln!(self.out, " uint32_t nextint{depth} = 0;");
let _ = writeln!(self.out, " size_t body_len{depth} = 0;");
let _ = writeln!(
self.out,
" if (lc{depth} == 0) body_len{depth} = 1; else if (lc{depth} == 1) body_len{depth} = 2; else if (lc{depth} == 2) body_len{depth} = 4; else if (lc{depth} == 3) body_len{depth} = 8;"
);
let _ = writeln!(self.out, " else if (lc{depth} == 5) {{");
let _ = writeln!(self.out, " size_t peek_pos{depth} = pos;");
let _ = writeln!(
self.out,
" if (zerodds_xcdr2_c_read_u32(buf, len, &peek_pos{depth}, &nextint{depth}, zd_be) != 0) return -7;"
);
let _ = writeln!(
self.out,
" body_len{depth} = 4u + (size_t)nextint{depth};"
);
let _ = writeln!(self.out, " }} else {{");
let _ = writeln!(
self.out,
" if (zerodds_xcdr2_c_read_u32(buf, len, &pos, &nextint{depth}, zd_be) != 0) return -7;"
);
let _ = writeln!(
self.out,
" body_len{depth} = nextint{depth};"
);
let _ = writeln!(self.out, " }}");
let _ = writeln!(
self.out,
" if (pos + body_len{depth} > nst_end{depth}) return -7;"
);
self.emit_mutable_member_dispatch_base(
ndef,
&format!("({var})."),
&format!("mid{depth}"),
&format!("body_len{depth}"),
)?;
let _ = writeln!(self.out, " }}");
} else {
self.emit_nested_struct_inline_read(var, ndef)?;
let _ = writeln!(
self.out,
" if (representation) pos = nst_end{depth};"
);
}
let _ = writeln!(self.out, " }}");
Ok(())
}
/// Tight-packed (`@final`) inline read of a nested struct's members. Also the
/// `@appendable` body (members after the DHEADER).
/// zerodds-lint: recursion-depth 64 (bounded by IDL nesting)
fn emit_nested_struct_inline_read(
&mut self,
var: &str,
ndef: &StructDef,
) -> Result<(), CppGenError> {
for member in &ndef.members {
let optional = is_optional(&member.annotations);
for decl in &member.declarators {
let f = &decl.name().text;
let dims = effective_array_dims(self.reg, &member.type_spec, decl)?;
if optional {
let _ = writeln!(self.out, " {{");
let _ = writeln!(self.out, " uint8_t present = 0;");
let _ = writeln!(
self.out,
" if (zerodds_xcdr2_c_read_u8(buf, len, &pos, &present, zd_be) != 0) return -7;"
);
let _ = writeln!(self.out, " ({var}).{f}_present = present;");
let _ = writeln!(self.out, " if (present) {{");
self.emit_array_or_scalar_read(
&format!("({var}).{f}"),
&member.type_spec,
&dims,
)?;
let _ = writeln!(self.out, " }}");
let _ = writeln!(self.out, " }}");
} else {
self.emit_array_or_scalar_read(
&format!("({var}).{f}"),
&member.type_spec,
&dims,
)?;
}
}
}
Ok(())
}
/// Read an XCDR2 wstring into a freshly-malloc'd NUL-terminated `uint16_t*`.
fn emit_wstring_read(&mut self, var: &str) -> Result<(), CppGenError> {
let _ = writeln!(self.out, " {{");
let _ = writeln!(self.out, " uint32_t ws_bytes = 0;");
let _ = writeln!(
self.out,
" if (zerodds_xcdr2_c_read_u32(buf, len, &pos, &ws_bytes, zd_be) != 0) return -7;"
);
let _ = writeln!(self.out, " uint32_t ws_n = ws_bytes / 2u;");
let _ = writeln!(
self.out,
" uint16_t* ws_p = (uint16_t*)malloc(((size_t)ws_n + 1) * sizeof(uint16_t));"
);
let _ = writeln!(self.out, " if (ws_p == NULL) return -7;");
let _ = writeln!(
self.out,
" for (uint32_t wi = 0; wi < ws_n; ++wi) {{ if (zerodds_xcdr2_c_read_u16(buf, len, &pos, &ws_p[wi], zd_be) != 0) {{ free(ws_p); return -7; }} }}"
);
let _ = writeln!(self.out, " ws_p[ws_n] = 0;");
let _ = writeln!(self.out, " ({var}) = ws_p;");
let _ = writeln!(self.out, " }}");
Ok(())
}
/// Read an XCDR2 map: skip DHEADER, read count, allocate parallel
/// key/value arrays, read each pair.
fn emit_map_read(
&mut self,
var: &str,
key: &TypeSpec,
value: &TypeSpec,
) -> Result<(), CppGenError> {
let kc = c_type_for(self.reg, key)?;
let vc = c_type_for(self.reg, value)?;
let _ = writeln!(self.out, " {{");
// Symmetric to the encoder: the map DHEADER is 4-aligned and present
// ONLY for a non-primitive (key,value) element (XCDR2 §7.4.3.5).
let _ = writeln!(
self.out,
" if (zerodds_xcdr2_c_pad_read(buf, len, &pos, 4) != 0) return -1;"
);
if !(self.seq_elem_is_primitive(key) && self.seq_elem_is_primitive(value)) {
let _ = writeln!(self.out, " if (representation) {{");
let _ = writeln!(self.out, " uint32_t map_dheader = 0;");
let _ = writeln!(
self.out,
" if (zerodds_xcdr2_c_read_u32(buf, len, &pos, &map_dheader, zd_be) != 0) return -7;"
);
let _ = writeln!(self.out, " (void)map_dheader;");
let _ = writeln!(self.out, " }}");
}
let _ = writeln!(self.out, " uint32_t map_len = 0;");
let _ = writeln!(
self.out,
" if (zerodds_xcdr2_c_read_u32(buf, len, &pos, &map_len, zd_be) != 0) return -7;"
);
let _ = writeln!(self.out, " ({var}).len = map_len;");
let _ = writeln!(
self.out,
" ({var}).keys = ({kc}*)calloc(map_len ? map_len : 1, sizeof({kc}));"
);
let _ = writeln!(
self.out,
" ({var}).vals = ({vc}*)calloc(map_len ? map_len : 1, sizeof({vc}));"
);
let _ = writeln!(
self.out,
" if (((({var}).keys == NULL) || (({var}).vals == NULL)) && map_len > 0) return -7;"
);
let mv = format!("mi{}", self.coll_depth);
let _ = writeln!(
self.out,
" for (uint32_t {mv} = 0; {mv} < map_len; ++{mv}) {{"
);
self.coll_depth += 1;
let rk = self.emit_member_read(&format!("({var}).keys[{mv}]"), key);
let rv = if rk.is_ok() {
self.emit_member_read(&format!("({var}).vals[{mv}]"), value)
} else {
Ok(())
};
self.coll_depth -= 1;
rk.and(rv)?;
let _ = writeln!(self.out, " }}");
let _ = writeln!(self.out, " }}");
Ok(())
}
/// Read a discriminated union: read the discriminator, then the selected
/// member into the `_u` arm.
fn emit_union_read(&mut self, var: &str, udef: &UnionDef) -> Result<(), CppGenError> {
// Symmetric to emit_union_write: appendable/mutable unions carry a
// 4-aligned DHEADER over [disc + branch]; @final does not.
let u_dheader = !matches!(extensibility_of(&udef.annotations), Extensibility::Final);
if u_dheader {
// XCDR2: 4-align + the union DHEADER. XCDR1: no DHEADER (the
// discriminator read below does its own alignment).
let _ = writeln!(self.out, " if (representation) {{");
let _ = writeln!(
self.out,
" if (zerodds_xcdr2_c_pad_read(buf, len, &pos, 4) != 0) return -1;"
);
let _ = writeln!(
self.out,
" {{ uint32_t u_dh = 0; if (zerodds_xcdr2_c_read_u32(buf, len, &pos, &u_dh, zd_be) != 0) return -7; (void)u_dh; }}"
);
let _ = writeln!(self.out, " }}");
}
let disc_ts = switch_type_spec(&udef.switch_type);
self.emit_member_read(&format!("({var})._d"), &disc_ts)?;
let _ = writeln!(self.out, " switch (({var})._d) {{");
let mut default_case: Option<&Case> = None;
for case in &udef.cases {
let field = union_case_field(case);
let mut has_value_label = false;
for label in &case.labels {
match label {
CaseLabel::Value(expr) => {
has_value_label = true;
let lit = self.case_label_literal(expr)?;
let _ = writeln!(self.out, " case {lit}:");
}
CaseLabel::Default => {
default_case = Some(case);
}
}
}
if has_value_label {
self.emit_member_read(&format!("({var})._u.{field}"), &case.element.type_spec)?;
let _ = writeln!(self.out, " break;");
}
}
if let Some(case) = default_case {
let field = union_case_field(case);
let _ = writeln!(self.out, " default:");
self.emit_member_read(&format!("({var})._u.{field}"), &case.element.type_spec)?;
let _ = writeln!(self.out, " break;");
} else {
let _ = writeln!(self.out, " default: break;");
}
let _ = writeln!(self.out, " }}");
Ok(())
}
/// Fold a collection bound (`sequence<T,N>` / `string<N>` / `map<K,V,N>`)
/// to its positive integer value, resolving named constants/enumerators via
/// the symbol table. `None` for an unbounded collection.
fn eval_bound(&self, bound: Option<&ConstExpr>) -> Option<u64> {
let expr = bound?;
evaluate(expr, &self.reg.consts)
.ok()
.and_then(|v| v.as_i64())
.filter(|n| *n >= 0)
.map(|n| n as u64)
}
/// Evaluate a union case label to a C-printable integer literal. Handles
/// integer/char/boolean discriminators plus enum constants.
fn case_label_literal(&self, expr: &ConstExpr) -> Result<String, CppGenError> {
// integer / char / boolean / enumerator → integer C `case` value.
// `as_i64` already covers Bool, Char, WChar and Enum ordinals.
if let Ok(v) = evaluate(expr, &self.reg.consts) {
if let Some(i) = v.as_i64() {
return Ok(i.to_string());
}
}
Err(unsupported("non-integer union case label (C backend)"))
}
fn emit_sequence_read(&mut self, var: &str, elem: &TypeSpec) -> Result<(), CppGenError> {
let _ = writeln!(self.out, " {{");
// XCDR2 §7.4.3.5: for non-primitive elements, a DHEADER (uint32 before
// [count + elements]) is present — skip it. enum→int32 is primitive.
if !self.seq_elem_is_primitive(elem) {
let _ = writeln!(self.out, " if (representation) {{");
let _ = writeln!(self.out, " uint32_t seq_dheader = 0;");
let _ = writeln!(
self.out,
" if (zerodds_xcdr2_c_read_u32(buf, len, &pos, &seq_dheader, zd_be) != 0) return -7;"
);
let _ = writeln!(self.out, " (void)seq_dheader;");
let _ = writeln!(self.out, " }}");
}
let _ = writeln!(self.out, " uint32_t seq_len = 0;");
let _ = writeln!(
self.out,
" if (zerodds_xcdr2_c_read_u32(buf, len, &pos, &seq_len, zd_be) != 0) return -7;"
);
let elem_c = c_type_for(self.reg, elem)?;
let _ = writeln!(self.out, " ({var}).len = seq_len;");
let _ = writeln!(
self.out,
" ({var}).elems = ({elem_c}*)calloc(seq_len ? seq_len : 1, sizeof({elem_c}));"
);
let _ = writeln!(
self.out,
" if (({var}).elems == NULL && seq_len > 0) return -7;"
);
let iv = format!("i{}", self.coll_depth);
let _ = writeln!(
self.out,
" for (uint32_t {iv} = 0; {iv} < seq_len; ++{iv}) {{"
);
// Delegate to the generic member reader so structs, enums and nested
// sequences all decode (#43, sequence<non-primitive T>). Depth-scoped
// loop counter avoids inner/outer index shadowing.
self.coll_depth += 1;
let r = self.emit_member_read(&format!("({var}).elems[{iv}]"), elem);
self.coll_depth -= 1;
r?;
let _ = writeln!(self.out, " }}");
let _ = writeln!(self.out, " }}");
Ok(())
}
/// True if a sequence element serializes as a plain primitive (no DHEADER):
/// IDL primitives, and enum members (which become int32). Resolves through
/// typedef aliases first.
fn seq_elem_is_primitive(&self, elem: &TypeSpec) -> bool {
let resolved = resolve_alias(self.reg, elem);
match &resolved {
TypeSpec::Primitive(_) => true,
TypeSpec::Scoped(sc) => scoped_last(sc).is_some_and(|last| {
// enum (→int32), bitmask/bitset (→holder uint) all serialize as
// a plain primitive integer, so no element DHEADER.
self.reg.enums.contains_key(&last)
|| self.reg.bitmasks.contains_key(&last)
|| self.reg.bitsets.contains_key(&last)
}),
_ => false,
}
}
fn emit_mutable_member_dispatch(&mut self, def: &StructDef) -> Result<(), CppGenError> {
self.emit_mutable_member_dispatch_base(def, "s->", "mid", "body_len")
}
/// EMHEADER member-id `switch` for a @mutable decode, with member accessors
/// prefixed by `base` and the member-id / body-length expressions named by
/// `mid_var` / `body_len_var` (so a nested @mutable struct can run its own
/// depth-scoped loop).
fn emit_mutable_member_dispatch_base(
&mut self,
def: &StructDef,
base: &str,
mid_var: &str,
body_len_var: &str,
) -> Result<(), CppGenError> {
let _ = writeln!(self.out, " switch ({mid_var}) {{");
// Sequential auto-id (XTypes §7.3.4.3 default), mirroring the encoder.
let mut auto_id: u32 = 0;
for member in &def.members {
let id = id_annotation(&member.annotations).unwrap_or(auto_id);
auto_id = id + 1;
for decl in &member.declarators {
let f = format!("{base}{}", decl.name().text);
let dims = effective_array_dims(self.reg, &member.type_spec, decl)?;
let _ = writeln!(self.out, " case {id}: {{");
self.emit_array_or_scalar_read(&f, &member.type_spec, &dims)?;
let _ = writeln!(self.out, " break;");
let _ = writeln!(self.out, " }}");
}
}
let _ = writeln!(self.out, " default: pos += {body_len_var}; break;");
let _ = writeln!(self.out, " }}");
Ok(())
}
fn emit_free_body(&mut self, c_name: &str, def: &StructDef) {
let _ = writeln!(
self.out,
"static void {c_name}_sample_free(void* sample) {{"
);
let _ = writeln!(self.out, " if (sample == NULL) return;");
let _ = writeln!(self.out, " {c_name}_t* s = ({c_name}_t*)sample;");
let _ = writeln!(self.out, " (void)s;");
for member in &def.members {
let resolved = resolve_alias(self.reg, &member.type_spec);
for decl in &member.declarators {
let f = &decl.name().text;
let dims =
effective_array_dims(self.reg, &member.type_spec, decl).unwrap_or_default();
if dims.is_empty() {
// Scalar member.
self.emit_free_member(&format!("s->{f}"), &resolved);
} else {
// Heap-owning members reached THROUGH a fixed array (e.g.
// `string names[3]`, `sequence<long> rows[2]`) must still be
// freed per element — previously skipped → leak.
let mut acc = format!("s->{f}");
for (d, n) in dims.iter().enumerate() {
let iv = format!("fi{d}");
let _ = writeln!(
self.out,
" for (uint32_t {iv} = 0; {iv} < {n}; ++{iv}) {{"
);
acc = format!("{acc}[{iv}]");
}
self.emit_free_member(&acc, &resolved);
for _ in &dims {
let _ = writeln!(self.out, " }}");
}
}
}
}
let _ = writeln!(self.out, "}}");
let _ = writeln!(self.out);
}
/// Free the heap-owned payload of a single member instance, reached via the
/// accessor expression `acc` (e.g. `s->name`, `s->names[fi0]`). Resolved
/// type drives the shape; primitives/enums/nested structs hold no heap.
fn emit_free_member(&mut self, acc: &str, resolved: &TypeSpec) {
match resolved {
TypeSpec::String(_) => {
// Both string (char*) and wstring (uint16_t*) are heap.
let _ = writeln!(self.out, " free({acc}); {acc} = NULL;");
}
TypeSpec::Sequence(seq) => {
if matches!(resolve_alias(self.reg, &seq.elem), TypeSpec::String(_)) {
let _ = writeln!(
self.out,
" for (uint32_t fsi = 0; fsi < ({acc}).len; ++fsi) free(({acc}).elems[fsi]);"
);
}
let _ = writeln!(
self.out,
" free(({acc}).elems); ({acc}).elems = NULL; ({acc}).len = 0;"
);
}
TypeSpec::Map(m) => {
if matches!(resolve_alias(self.reg, &m.key), TypeSpec::String(_)) {
let _ = writeln!(
self.out,
" for (uint32_t fki = 0; fki < ({acc}).len; ++fki) free(({acc}).keys[fki]);"
);
}
if matches!(resolve_alias(self.reg, &m.value), TypeSpec::String(_)) {
let _ = writeln!(
self.out,
" for (uint32_t fvi = 0; fvi < ({acc}).len; ++fvi) free(({acc}).vals[fvi]);"
);
}
let _ = writeln!(self.out, " free(({acc}).keys); ({acc}).keys = NULL;");
let _ = writeln!(
self.out,
" free(({acc}).vals); ({acc}).vals = NULL; ({acc}).len = 0;"
);
}
_ => {}
}
}
fn emit_key_hash_body(&mut self, c_name: &str, def: &StructDef) {
// Spec §7.6.8: collects members in PlainCdr2BeKeyHolder, then
// either zero-pad or MD5. We use the XCDR2 C helpers.
let _ = writeln!(
self.out,
"static int {c_name}_key_hash(const void* sample, uint8_t out_hash[16]) {{"
);
let _ = writeln!(
self.out,
" const {c_name}_t* s = (const {c_name}_t*)sample;"
);
let _ = writeln!(self.out, " uint8_t* kh_buf = NULL;");
let _ = writeln!(self.out, " size_t kh_len = 0;");
let _ = writeln!(self.out, " size_t kh_cap = 0;");
for member in &def.members {
if !is_key(&member.annotations) {
continue;
}
for decl in &member.declarators {
let f = &decl.name().text;
match &member.type_spec {
TypeSpec::Primitive(p) => {
let helper = match primitive_helper(*p) {
Ok(h) => h,
Err(_) => continue,
};
let _ = writeln!(
self.out,
" if (zerodds_xcdr2_c_kh_write_{helper}(&kh_buf, &kh_len, &kh_cap, s->{f}) != 0) {{ free(kh_buf); return -1; }}"
);
}
TypeSpec::String(_) => {
let _ = writeln!(
self.out,
" if (zerodds_xcdr2_c_kh_write_string(&kh_buf, &kh_len, &kh_cap, s->{f}) != 0) {{ free(kh_buf); return -1; }}"
);
}
_ => {}
}
}
}
let _ = writeln!(
self.out,
" zerodds_xcdr2_c_compute_key_hash(kh_buf, kh_len, /*max_size_static=*/0, out_hash);"
);
let _ = writeln!(self.out, " free(kh_buf);");
let _ = writeln!(self.out, " return 0;");
let _ = writeln!(self.out, "}}");
let _ = writeln!(self.out);
}
}
// ============================================================================
// Helpers
// ============================================================================
/// An aggregate definition (struct or union) for the typedef-ordering pre-pass.
#[derive(Clone)]
enum AggDef {
Struct(StructDef),
Union(UnionDef),
}
/// Collect every struct/union definition with its module scope, in declaration
/// order, for the aggregate-typedef pre-pass.
/// zerodds-lint: recursion-depth 64 (module tree; bounded by IDL nesting)
fn collect_aggregates(
defs: &[Definition],
scope: &[String],
out: &mut Vec<(String, Vec<String>, AggDef)>,
) {
for d in defs {
match d {
Definition::Module(m) => {
let mut s = scope.to_vec();
s.push(m.name.text.clone());
collect_aggregates(&m.definitions, &s, out);
}
Definition::Type(TypeDecl::Constr(ConstrTypeDecl::Struct(StructDcl::Def(s)))) => {
out.push((
s.name.text.clone(),
scope.to_vec(),
AggDef::Struct(s.clone()),
));
}
Definition::Type(TypeDecl::Constr(ConstrTypeDecl::Union(UnionDcl::Def(u)))) => {
out.push((
u.name.text.clone(),
scope.to_vec(),
AggDef::Union(u.clone()),
));
}
_ => {}
}
}
}
fn dds_type_name(scope: &[String], name: &str) -> String {
if scope.is_empty() {
name.to_string()
} else {
let mut s = scope.join("::");
s.push_str("::");
s.push_str(name);
s
}
}
/// The TypeSpec a union discriminator maps to (so the discriminator can reuse
/// the generic member writer/reader + C type mapping).
fn switch_type_spec(s: &SwitchTypeSpec) -> TypeSpec {
match s {
SwitchTypeSpec::Integer(it) => TypeSpec::Primitive(PrimitiveType::Integer(*it)),
SwitchTypeSpec::Char => TypeSpec::Primitive(PrimitiveType::Char),
SwitchTypeSpec::Boolean => TypeSpec::Primitive(PrimitiveType::Boolean),
SwitchTypeSpec::Octet => TypeSpec::Primitive(PrimitiveType::Octet),
SwitchTypeSpec::Scoped(sn) => TypeSpec::Scoped(sn.clone()),
}
}
/// The C field name for a union case's member (the case's declarator name).
fn union_case_field(case: &Case) -> String {
case.element.declarator.name().text.clone()
}
/// Join a scope + name with the given separator (`::` for IDL paths).
fn scope_join(scope: &[String], name: &str, sep: &str) -> String {
if scope.is_empty() {
name.to_string()
} else {
let mut s = scope.join(sep);
s.push_str(sep);
s.push_str(name);
s
}
}
fn c_identifier(scope: &[String], name: &str) -> String {
if scope.is_empty() {
name.to_string()
} else {
let mut s = scope.join("_");
s.push('_');
s.push_str(name);
s
}
}
impl Extensibility {
fn as_u8(self) -> u8 {
match self {
Self::Final => 0,
Self::Appendable => 1,
Self::Mutable => 2,
}
}
}
fn extensibility_of(annotations: &[Annotation]) -> Extensibility {
for a in annotations {
if let Some(name) = a.name.parts.last() {
match name.text.as_str() {
"final" | "Final" => return Extensibility::Final,
"appendable" | "Appendable" => return Extensibility::Appendable,
"mutable" | "Mutable" => return Extensibility::Mutable,
_ => {}
}
}
}
// Un-annotated default: FINAL. XTypes 1.3 §7.2.2.4.4 leaves the
// extensibility of an un-annotated aggregate implementation-defined; the
// canonical zerodds choice — anchored to the `zerodds-cdr` core and the
// idl-rust reference (whose hardcoded default is Final, see
// tools/idlc/src/default_ext.rs) — is FINAL, matching the rust golden:
// SX2: spec default for an unannotated aggregate is APPENDABLE (§7.3.3.1).
// `--default-extensibility final` opts back to the no-DHEADER form.
Extensibility::Appendable
}
fn is_optional(annotations: &[Annotation]) -> bool {
annotations.iter().any(|a| {
a.name
.parts
.last()
.is_some_and(|p| p.text == "optional" || p.text == "Optional")
})
}
/// XCDR2 wire byte size of a primitive scalar (XTypes 1.3 §7.4.1). Used to pick
/// the compact EMHEADER length code for a `@mutable` member.
fn primitive_wire_bytes(p: PrimitiveType) -> u32 {
match p {
PrimitiveType::Boolean | PrimitiveType::Octet | PrimitiveType::Char => 1,
PrimitiveType::WideChar => 2,
PrimitiveType::Integer(IntegerType::Int8 | IntegerType::UInt8) => 1,
PrimitiveType::Integer(
IntegerType::Short | IntegerType::Int16 | IntegerType::UShort | IntegerType::UInt16,
) => 2,
PrimitiveType::Integer(
IntegerType::Long | IntegerType::Int32 | IntegerType::ULong | IntegerType::UInt32,
) => 4,
PrimitiveType::Integer(
IntegerType::LongLong
| IntegerType::Int64
| IntegerType::ULongLong
| IntegerType::UInt64,
) => 8,
PrimitiveType::Floating(FloatingType::Float) => 4,
PrimitiveType::Floating(FloatingType::Double) => 8,
// long double (16 bytes) is not a compact code → falls back to LC4.
PrimitiveType::Floating(FloatingType::LongDouble) => 16,
}
}
/// Picks the compact XTypes 1.3 §7.4.3.4.2 length code for a `@mutable` member,
/// mirroring the Rust reference `mutable_member_length_code` (Bug XV-mut). Only
/// non-array, non-optional scalar primitives (LC0..3 by wire size) and
/// strings/wstrings (LC5, reusing the leading uint32 length prefix as the
/// NEXTINT) are eligible. Returns `None` for everything else → universal LC4.
fn mutable_compact_lc(
reg: &TypeReg,
type_spec: &TypeSpec,
dims: &[u64],
optional: bool,
) -> Option<u8> {
if !dims.is_empty() || optional {
return None;
}
let resolved = resolve_alias(reg, type_spec);
match &resolved {
TypeSpec::Primitive(p) => match primitive_wire_bytes(*p) {
1 => Some(0),
2 => Some(1),
4 => Some(2),
8 => Some(3),
_ => None, // long double → LC4
},
// FINDING T1b (mirrors idl-rust `mutable_member_length_code` +
// `member_body_has_leading_dheader`): a member whose XCDR2 body begins
// with a 4-byte length word — a string/wstring length prefix, a
// non-primitive sequence/map DHEADER, or a nested @appendable/@mutable
// struct's DHEADER — uses LC5 to REUSE that word as the NEXTINT (no
// redundant NEXTINT), matching CycloneDDS / RTI / FastDDS. A @final
// nested struct (no DHEADER) and a sequence<primitive> (bare element
// count, not a byte length) fall through to the universal LC4.
_ if member_body_has_leading_dheader_c(reg, &resolved) => Some(5),
_ => None,
}
}
/// `true` if `type_spec`'s XCDR2 body begins with a 4-byte length/DHEADER word,
/// making it eligible for EMHEADER LengthCode 5 (the leading word doubles as the
/// NEXTINT). Mirrors idl-rust `type_map::member_body_has_leading_dheader`:
/// * string / wstring — leading uint32 octet-length prefix,
/// * map<K,V> — always a non-primitive aggregate → DHEADER,
/// * sequence<E> with NON-primitive `E` — XCDR2 DHEADER (sequence<primitive>
/// has only a bare element count, not a byte length → stays LC4),
/// * a nested struct (chasing typedef) whose extensibility is @appendable /
/// @mutable — self-delimits with a leading DHEADER; a @final nested struct
/// tight-packs its body (no DHEADER) → LC4.
///
/// zerodds-lint: recursion-depth 16
fn member_body_has_leading_dheader_c(reg: &TypeReg, type_spec: &TypeSpec) -> bool {
match resolve_alias(reg, type_spec) {
TypeSpec::String(_) => true,
TypeSpec::Map(_) => true,
TypeSpec::Sequence(seq) => !seq_elem_is_primitive_reg(reg, &seq.elem),
TypeSpec::Scoped(sc) => scoped_last(&sc)
.and_then(|last| reg.structs.get(&last).cloned())
.is_some_and(|(_, ndef)| {
!matches!(extensibility_of(&ndef.annotations), Extensibility::Final)
}),
_ => false,
}
}
/// Free-function twin of `CSink::seq_elem_is_primitive` (a sequence element is
/// "primitive" — and carries no DHEADER — when it is an IDL primitive, enum,
/// bitmask or bitset). Kept standalone so the length-code picker can run without
/// a `&self` borrow.
fn seq_elem_is_primitive_reg(reg: &TypeReg, elem: &TypeSpec) -> bool {
match resolve_alias(reg, elem) {
TypeSpec::Primitive(_) => true,
TypeSpec::Scoped(sc) => scoped_last(&sc).is_some_and(|last| {
reg.enums.contains_key(&last)
|| reg.bitmasks.contains_key(&last)
|| reg.bitsets.contains_key(&last)
}),
_ => false,
}
}
/// Fixed-array dimensions of a declarator (empty for a simple declarator).
///
/// A bound may be a literal **or a named constant** (`long v[N]`) — the latter
/// is resolved through the const symbol table the way every other backend does
/// (Bug C #43, const-array-bound).
fn array_dims(reg: &TypeReg, decl: &Declarator) -> Result<Vec<u64>, CppGenError> {
match decl {
Declarator::Simple(_) => Ok(Vec::new()),
Declarator::Array(arr) => {
let mut dims = Vec::with_capacity(arr.sizes.len());
for sz in &arr.sizes {
let n = const_expr_to_u64(reg, sz)
.ok_or_else(|| unsupported("non-literal array bound"))?;
dims.push(n);
}
Ok(dims)
}
}
}
/// Effective fixed-array dimensions for a member, combining a typedef-to-array
/// alias's dims with the declarator's own dims. `Matrix3 transform;` where
/// `typedef long Matrix3[3][3];` yields `[3,3]` even though the member's own
/// declarator is simple (#43, typedef-to-array). Alias dims come first
/// (outermost), then the declarator dims, matching C array nesting order.
fn effective_array_dims(
reg: &TypeReg,
type_spec: &TypeSpec,
decl: &Declarator,
) -> Result<Vec<u64>, CppGenError> {
let mut dims = Vec::new();
if let TypeSpec::Scoped(sc) = type_spec {
if let Some(last) = scoped_last(sc) {
if let Some(td) = reg.typedef_arrays.get(&last) {
dims.extend_from_slice(td);
}
}
}
dims.extend(array_dims(reg, decl)?);
Ok(dims)
}
/// Smallest `uintN_t` holder for a bit width (XTypes §7.3.1.2 / Tab. 7.12).
fn holder_uint_c_type(bits: u32) -> &'static str {
match bits {
0..=8 => "uint8_t",
9..=16 => "uint16_t",
17..=32 => "uint32_t",
_ => "uint64_t",
}
}
/// The C holder type for a bitmask: smallest uint that fits `@bit_bound`
/// (default 32). XTypes §7.3.1.2.2 — a bitmask serializes as this integer.
fn bitmask_holder_c_type(b: &BitmaskDecl) -> &'static str {
// Holder width: an explicit `@bit_bound(N)` is authoritative; otherwise the
// spec DEFAULT is @bit_bound=32 (Bug XV-bits, XTypes 1.3 §7.3.1.2.1.6) → a
// UInt32 (4-byte) holder, NOT a width sized to the value count. Byte-identical
// wire to the corrected rust golden (Perm => uint32_t).
let bound = extract_int_annotation_c(&b.annotations, "bit_bound").unwrap_or(32);
holder_uint_c_type(bound)
}
/// The C holder type for a bitset: smallest uint that fits the sum of all
/// bitfield widths. XTypes §7.3.1.2.1 — a bitset serializes as one packed
/// integer (over 64 bits is out of the C/XTypes profile and saturates to u64).
fn bitset_holder_c_type(b: &BitsetDecl) -> &'static str {
let mut total: u32 = 0;
for f in &b.bitfields {
if let ConstExpr::Literal(l) = &f.spec.width {
if l.kind == LiteralKind::Integer {
if let Ok(w) = l.raw.trim().parse::<u32>() {
total = total.saturating_add(w);
}
}
}
}
holder_uint_c_type(total)
}
/// Extract a single integer annotation parameter (`@bit_bound(32)` /
/// `@position(3)`) — used by the bitset/bitmask holder sizing.
fn extract_int_annotation_c(anns: &[Annotation], name: &str) -> Option<u32> {
let a = anns
.iter()
.find(|a| a.name.parts.last().map(|p| p.text.as_str()) == Some(name))?;
if let AnnotationParams::Single(ConstExpr::Literal(l)) = &a.params {
if l.kind == LiteralKind::Integer {
return l.raw.trim().parse::<u32>().ok();
}
}
None
}
/// Best-effort literal/const fold during the collection pre-pass (the const
/// table is still being built, so this only resolves literals + already-seen
/// consts). Used to size a `typedef T A[N];` array-alias.
fn const_expr_to_u64_pre(consts: &SymbolTable, e: &ConstExpr) -> Option<u64> {
if let ConstExpr::Literal(l) = e {
if l.kind == LiteralKind::Integer {
let raw = l.raw.trim();
return if let Some(hex) = raw.strip_prefix("0x").or_else(|| raw.strip_prefix("0X")) {
u64::from_str_radix(hex, 16).ok()
} else {
raw.parse::<u64>().ok()
};
}
}
evaluate_positive_int(e, consts, e.span()).ok()
}
fn const_expr_to_u64(reg: &TypeReg, e: &ConstExpr) -> Option<u64> {
// Literal fast path (also covers hex).
if let ConstExpr::Literal(l) = e {
if l.kind == LiteralKind::Integer {
let raw = l.raw.trim();
let parsed =
if let Some(hex) = raw.strip_prefix("0x").or_else(|| raw.strip_prefix("0X")) {
u64::from_str_radix(hex, 16).ok()
} else {
raw.parse::<u64>().ok()
};
if let Some(v) = parsed {
return Some(v);
}
}
}
// Named-constant / expression path: evaluate against the const symbol table
// (handles `const long N = 4; long v[N];` and arithmetic like `N*2`).
evaluate_positive_int(e, ®.consts, e.span()).ok()
}
fn is_key(annotations: &[Annotation]) -> bool {
annotations.iter().any(|a| {
a.name
.parts
.last()
.is_some_and(|p| p.text == "key" || p.text == "Key")
})
}
fn struct_has_key(def: &StructDef) -> bool {
def.members.iter().any(|m| is_key(&m.annotations))
}
fn id_annotation(annotations: &[Annotation]) -> Option<u32> {
for a in annotations {
let last = a.name.parts.last()?;
if last.text != "id" && last.text != "Id" {
continue;
}
if let AnnotationParams::Single(ConstExpr::Literal(lit)) = &a.params {
if lit.kind == LiteralKind::Integer {
if let Ok(v) = lit.raw.parse::<u32>() {
return Some(v);
}
}
}
}
None
}
/// zerodds-lint: recursion-depth 64 (c_type_for bounded by AST depth)
fn c_type_for(reg: &TypeReg, type_spec: &TypeSpec) -> Result<String, CppGenError> {
let s = match type_spec {
TypeSpec::Scoped(sc) => {
let last = scoped_last(sc).ok_or_else(|| unsupported("empty scoped name"))?;
// enum → int32 alias.
if let Some(cn) = reg.enums.get(&last) {
return Ok(format!("{cn}_t"));
}
// bitmask / bitset → holder-integer typedef.
if let Some((cn, _)) = reg.bitmasks.get(&last) {
return Ok(format!("{cn}_t"));
}
if let Some((cn, _)) = reg.bitsets.get(&last) {
return Ok(format!("{cn}_t"));
}
// typedef → resolve to underlying type. A typedef-to-array alias
// (`typedef long Matrix3[3][3];`) carries the element type here; the
// dims are applied separately at the member declarator via
// `effective_array_dims` (#43, typedef-to-array).
if reg.typedefs.contains_key(&last) {
let resolved = resolve_alias(reg, type_spec);
return c_type_for(reg, &resolved);
}
// nested struct → embed the C struct by value. A RECURSIVE struct
// can only be referenced behind a pointer (sequence element); use
// the struct tag `struct <C>_s*` so the type is valid INSIDE its own
// typedef, before the `<C>_t` alias name exists (#43, recursion).
if let Some((cn, _)) = reg.structs.get(&last) {
if reg.is_recursive(&last) {
return Ok(format!("struct {cn}_s"));
}
return Ok(format!("{cn}_t"));
}
// union → embed the C tagged-union by value (tag form if recursive).
if let Some((cn, _)) = reg.unions.get(&last) {
if reg.is_recursive(&last) {
return Ok(format!("struct {cn}_s"));
}
return Ok(format!("{cn}_t"));
}
return Err(unsupported("unresolved scoped type (C backend)"));
}
TypeSpec::Primitive(p) => match p {
PrimitiveType::Boolean => "uint8_t",
PrimitiveType::Octet => "uint8_t",
PrimitiveType::Char => "char",
PrimitiveType::WideChar => "uint16_t",
PrimitiveType::Integer(IntegerType::Short)
| PrimitiveType::Integer(IntegerType::Int16) => "int16_t",
PrimitiveType::Integer(IntegerType::UShort)
| PrimitiveType::Integer(IntegerType::UInt16) => "uint16_t",
PrimitiveType::Integer(IntegerType::Long)
| PrimitiveType::Integer(IntegerType::Int32) => "int32_t",
PrimitiveType::Integer(IntegerType::ULong)
| PrimitiveType::Integer(IntegerType::UInt32) => "uint32_t",
PrimitiveType::Integer(IntegerType::LongLong)
| PrimitiveType::Integer(IntegerType::Int64) => "int64_t",
PrimitiveType::Integer(IntegerType::ULongLong)
| PrimitiveType::Integer(IntegerType::UInt64) => "uint64_t",
PrimitiveType::Integer(IntegerType::Int8) => "int8_t",
PrimitiveType::Integer(IntegerType::UInt8) => "uint8_t",
PrimitiveType::Floating(FloatingType::Float) => "float",
PrimitiveType::Floating(FloatingType::Double) => "double",
PrimitiveType::Floating(FloatingType::LongDouble) => {
return Err(unsupported("long double"));
}
},
TypeSpec::String(st) => {
if st.wide {
// wstring → NUL-terminated UTF-16 (uint16) buffer (#43, wstring).
"uint16_t*"
} else {
"char*"
}
}
TypeSpec::Sequence(seq) => {
let elem = c_type_for(reg, &seq.elem)?;
return Ok(format!("struct {{ uint32_t len; {elem}* elems; }}"));
}
TypeSpec::Map(m) => {
// map<K,V> → parallel key/value arrays + count (#43, map<K,V>).
let kc = c_type_for(reg, &m.key)?;
let vc = c_type_for(reg, &m.value)?;
return Ok(format!(
"struct {{ uint32_t len; {kc}* keys; {vc}* vals; }}"
));
}
TypeSpec::Fixed(f) => {
// fixed<P,S>: CORBA/GIOP §9.3.2.7 packed BCD, (P+2)/2 raw octets.
// C has no decimal type, so the field IS the BCD byte array; the
// user fills/reads `.bcd[]` (helpers can convert to/from a string).
let p = const_expr_to_u64(reg, &f.digits)
.ok_or_else(|| unsupported("fixed: non-constant digit count"))?;
let n = (p + 2) / 2;
return Ok(format!("struct {{ uint8_t bcd[{n}]; }}"));
}
_ => {
return Err(unsupported("non-primitive type in field"));
}
};
Ok(s.to_string())
}
fn primitive_helper(p: PrimitiveType) -> Result<&'static str, CppGenError> {
Ok(match p {
PrimitiveType::Boolean | PrimitiveType::Octet => "u8",
PrimitiveType::Char => "u8",
PrimitiveType::WideChar => "u16",
PrimitiveType::Integer(IntegerType::Short) | PrimitiveType::Integer(IntegerType::Int16) => {
"i16"
}
PrimitiveType::Integer(IntegerType::UShort)
| PrimitiveType::Integer(IntegerType::UInt16) => "u16",
PrimitiveType::Integer(IntegerType::Long) | PrimitiveType::Integer(IntegerType::Int32) => {
"i32"
}
PrimitiveType::Integer(IntegerType::ULong)
| PrimitiveType::Integer(IntegerType::UInt32) => "u32",
PrimitiveType::Integer(IntegerType::LongLong)
| PrimitiveType::Integer(IntegerType::Int64) => "i64",
PrimitiveType::Integer(IntegerType::ULongLong)
| PrimitiveType::Integer(IntegerType::UInt64) => "u64",
PrimitiveType::Integer(IntegerType::Int8) => "i8",
PrimitiveType::Integer(IntegerType::UInt8) => "u8",
PrimitiveType::Floating(FloatingType::Float) => "f32",
PrimitiveType::Floating(FloatingType::Double) => "f64",
PrimitiveType::Floating(FloatingType::LongDouble) => {
return Err(unsupported("long double"));
}
})
}
#[cfg(test)]
mod tests {
#![allow(clippy::expect_used)]
use super::*;
use zerodds_idl::config::ParserConfig;
fn gen_c(src: &str) -> String {
let ast = zerodds_idl::parse(src, &ParserConfig::default()).expect("parse ok");
generate_c_header(&ast, &CGenOptions::default()).expect("c-gen ok")
}
#[test]
fn empty_final_struct_emits_typedef_and_typesupport() {
let h = gen_c("@final struct Empty {};");
assert!(h.contains("typedef struct Empty_s"));
assert!(h.contains("Empty_typesupport"));
assert!(h.contains(".extensibility = 0"));
}
#[test]
fn primitive_struct_maps_types() {
let h = gen_c("@final struct Point { long x; long y; };");
assert!(h.contains("int32_t x;"));
assert!(h.contains("int32_t y;"));
assert!(h.contains("\"Point\""));
}
#[test]
fn nested_module_yields_scoped_type_name() {
let h = gen_c("module Outer { module Inner { @final struct S { long x; }; }; };");
assert!(h.contains("typedef struct Outer_Inner_S_s"));
assert!(h.contains("\"Outer::Inner::S\""));
}
#[test]
fn appendable_default_when_no_annotation() {
// SX2: un-annotated aggregates default to APPENDABLE (extensibility = 1,
// XTypes 1.3 §7.3.3.1), self-delimited with a DHEADER, matching the
// TypeObject path + FastDDS/Cyclone.
let h = gen_c("struct V { long a; long b; };");
assert!(h.contains(".extensibility = 1"));
}
#[test]
fn mutable_struct_marks_extensibility() {
let h = gen_c("@mutable struct M { @id(1) long a; };");
assert!(h.contains(".extensibility = 2"));
}
#[test]
fn key_member_sets_is_keyed_and_emits_key_hash() {
let h = gen_c("@final struct Sensor { @key long id; double value; };");
assert!(h.contains(".is_keyed = 1"));
assert!(h.contains("Sensor_key_hash"));
}
// ---- Bug C: scope widening (no longer rejected) ----
#[test]
fn enum_member_no_longer_rejected() {
// Previously: `non-struct type` / `nested struct reference` error.
let h = gen_c("enum Color { RED, GREEN, BLUE }; @final struct S { Color c; };");
assert!(h.contains("typedef int32_t Color_t;"));
assert!(h.contains("Color_GREEN = 1"));
// field typed as the int32 alias, encoded as i32.
assert!(h.contains("Color_t c;"));
assert!(h.contains("zerodds_xcdr2_c_write_i32(&w_buf, &w_len, &w_cap, (int32_t)(s->c))"));
assert!(h.contains("zerodds_xcdr2_c_read_i32(buf, len, &pos, (int32_t*)&(s->c), zd_be)"));
}
#[test]
fn typedef_member_resolves_to_underlying() {
let h = gen_c("typedef double Amps; @final struct S { Amps battery; };");
assert!(h.contains("double battery;"));
// 8-byte primitives use the XCDR2 align-4 helper `zd_x2_write_f64`
// (MAXALIGN=min(8,4)=4, §7.4.1.1.1), NOT the shared align-8 `write_f64`.
assert!(h.contains("zd_x2_write_f64(&w_buf, &w_len, &w_cap, s->battery)"));
assert!(h.contains("static inline int zd_x2_write_f64"));
}
#[test]
fn nested_struct_member_inlined() {
let h = gen_c(
"@final struct Point { long x; long y; }; @final struct Line { Point a; Point b; };",
);
assert!(h.contains("Point_t a;"));
assert!(h.contains("Point_t b;"));
// inline-encoded by value (sub-member access through the field).
assert!(h.contains("(s->a).x"));
assert!(h.contains("(s->b).y"));
}
#[test]
fn fixed_array_member_no_length_prefix() {
let h = gen_c("@final struct G { long grid[3]; };");
assert!(h.contains("int32_t grid[3];"));
// a loop over the fixed extent, no u32 length write.
assert!(h.contains("ai0 = 0; ai0 < 3"));
}
// ---- Bug C2: @optional + header conflict ----
#[test]
fn optional_member_gets_presence_flag() {
let h = gen_c("@final struct O { @optional long maybe; };");
assert!(h.contains("int32_t maybe;"));
assert!(h.contains("uint8_t maybe_present;"));
// wire: presence boolean, then the value only if present.
assert!(h.contains("s->maybe_present ? 1 : 0"));
assert!(h.contains("if (s->maybe_present) {"));
}
#[test]
fn header_does_not_include_conflicting_zerodds_h() {
// Bug C2: the cbindgen `zerodds.h` redeclares typed-FFI fns with names
// conflicting with `zerodds_xcdr2.h`; only the latter is needed.
let h = gen_c("@final struct S { long x; };");
assert!(h.contains("#include \"zerodds_xcdr2.h\""));
assert!(!h.contains("#include \"zerodds.h\""));
}
// ---- #43: C-Foundation widening — union / map / wstring / const-bound ----
#[test]
fn union_emits_tagged_union_codec() {
// Unions are now in scope (#43): a tagged-union typedef + TypeSupport.
let h = gen_c("union U switch (long) { case 1: long a; default: long b; };");
assert!(h.contains("typedef struct U_s"));
assert!(h.contains("} _u;"));
assert!(h.contains("U_typesupport"));
// Discriminator is switched on in encode/decode.
assert!(h.contains("._d) {"));
assert!(h.contains("case 1:"));
assert!(h.contains("default:"));
}
#[test]
fn const_array_bound_resolves_to_literal() {
// `long v[N]` with `const long N = 4` resolves the bound (#43).
let h = gen_c("const long N = 4; @final struct A { long v[N]; };");
assert!(
h.contains("int32_t v[4];"),
"named const bound not folded:\n{h}"
);
}
#[test]
fn map_member_emits_parallel_arrays_and_codec() {
let h = gen_c("@final struct M { map<string, long> counters; };");
assert!(h.contains("keys;"));
assert!(h.contains("vals;"));
// DHEADER + count emitted for the map body.
assert!(h.contains("map_dheader_pos"));
}
#[test]
fn wstring_member_maps_to_uint16_ptr() {
let h = gen_c("@final struct W { wstring label; };");
assert!(h.contains("uint16_t* label;"));
// wire: byte-length then uint16 code units.
assert!(h.contains("ws_n * 2u"));
}
#[test]
fn sequence_of_struct_resolves_element_type() {
let h = gen_c("@final struct P { long x; }; @final struct S { sequence<P> ps; };");
assert!(h.contains("P_t* elems; } ps;"));
// element body inline-encodes the struct sub-member.
assert!(h.contains(".elems[i0]).x") || h.contains(".elems[i0])"));
}
#[test]
fn typedef_to_aggregate_resolves_to_struct() {
let h = gen_c(
"@final struct Point { long x; }; typedef Point Position; \
@final struct S { Position p; };",
);
assert!(h.contains("Point_t p;"));
}
// ---- #43: the last four Foundation fixtures (typedefs / bits / recursion
// / forward-decl) ----
#[test]
fn typedef_alias_chain_resolves_to_root() {
let h = gen_c("typedef double A; typedef A B; @final struct S { B v; };");
// The alias-of-alias resolves to the root primitive `double`.
assert!(h.contains("double v;"), "alias chain not resolved:\n{h}");
}
#[test]
fn typedef_to_array_contributes_dims_at_use_site() {
let h = gen_c("typedef long Matrix3[3][3]; @final struct S { Matrix3 m; };");
assert!(
h.contains("int32_t m[3][3];"),
"typedef-to-array dims lost:\n{h}"
);
}
#[test]
fn bitmask_maps_to_holder_uint_and_flag_constants() {
let h = gen_c(
"bitmask Permissions { READ, WRITE, EXECUTE }; @final struct S { Permissions p; };",
);
// No explicit @bit_bound → spec DEFAULT @bit_bound=32 (Bug XV-bits,
// XTypes 1.3 §7.3.1.2.1.6) → a UInt32 holder, NOT a width sized to the
// value count. flag = 1 << position.
assert!(
h.contains("typedef uint32_t Permissions_t;"),
"holder uint:\n{h}"
);
assert!(
h.contains("Permissions_WRITE = (1u << 1)"),
"flag bit:\n{h}"
);
assert!(h.contains("Permissions_t p;"), "member type:\n{h}");
// Serialized as the holder integer (u32), no DHEADER.
assert!(h.contains("zerodds_xcdr2_c_write_u32(&w_buf, &w_len, &w_cap, s->p)"));
}
#[test]
fn bitset_maps_to_packed_holder_and_accessors() {
let h = gen_c(
"bitset Flags { bitfield<3> kind; bitfield<1> active; bitfield<4> priority; }; \
@final struct S { Flags f; };",
);
// 3+1+4 = 8 bits → uint8 holder; SHIFT/MASK accessor per named field.
assert!(
h.contains("typedef uint8_t Flags_t;"),
"packed holder:\n{h}"
);
assert!(h.contains("Flags_active_SHIFT = 3"), "field offset:\n{h}");
assert!(
h.contains("#define Flags_priority_MASK 0xFu"),
"field mask:\n{h}"
);
assert!(h.contains("zerodds_xcdr2_c_write_u8(&w_buf, &w_len, &w_cap, s->f)"));
}
#[test]
fn recursive_type_through_sequence_splices_via_helper() {
let h = gen_c("struct TreeNode { long value; sequence<TreeNode> children; };");
// Self-reference is a pointer-to-tag, spliced through a runtime helper.
assert!(
h.contains("struct TreeNode_s* elems;"),
"pointer-to-tag elem:\n{h}"
);
assert!(
h.contains("static int TreeNode_write_body("),
"write helper:\n{h}"
);
assert!(
h.contains("static int TreeNode_read_body("),
"read helper:\n{h}"
);
assert!(
h.contains("TreeNode_write_body(&((s->children).elems[i0])"),
"recursive splice call:\n{h}"
);
}
#[test]
fn forward_declared_then_defined_struct_and_union_generate() {
// Forward decls then defs; the union embeds the (recursive) struct by
// value, so the struct typedef must precede the union typedef.
let h = gen_c(
"module conf { \
struct Node; union Variant; \
struct Node { long value; sequence<Node> next; }; \
union Variant switch (long) { case 0: long a; case 1: Node n; }; \
};",
);
assert!(
h.contains("typedef struct conf_Node_s"),
"node typedef:\n{h}"
);
assert!(
h.contains("typedef struct conf_Variant_s"),
"variant typedef:\n{h}"
);
// Node typedef must come before Variant typedef (by-value embed order).
let ni = h
.find("typedef struct conf_Node_s")
.expect("Node typedef present");
let vi = h
.find("typedef struct conf_Variant_s")
.expect("Variant typedef present");
assert!(ni < vi, "Node typedef must precede Variant typedef");
}
#[test]
fn direct_by_value_self_membership_is_rejected() {
// `struct Node { Node n; };` is an infinite-size type — rejected cleanly.
let ast = zerodds_idl::parse("struct Node { long v; Node n; };", &ParserConfig::default());
if let Ok(ast) = ast {
assert!(
generate_c_header(&ast, &CGenOptions::default()).is_err(),
"infinite by-value self-membership must be rejected"
);
}
}
#[test]
fn fixed_decimal_is_a_bcd_byte_field() {
// fixed<P,S> now maps to a (P+2)/2-octet BCD byte field (CORBA §9.3.2.7)
// with a raw encode/decode loop. `any` stays out of the C profile.
let ast = zerodds_idl::parse(
"@final struct S { fixed<5,2> price; };",
&ParserConfig::default(),
)
.expect("parse");
let c = generate_c_header(&ast, &CGenOptions::default()).expect("gen");
assert!(
c.contains("uint8_t bcd[3]"),
"fixed<5,2> -> 3-octet BCD field:\n{c}"
);
assert!(
c.contains("zerodds_xcdr2_c_write_u8") && c.contains("zerodds_xcdr2_c_read_u8"),
"fixed must emit a raw BCD write/read loop"
);
}
#[test]
fn any_remains_out_of_scope() {
// `any` (CORBA TypeCode + dynamic value) is NOT in the C profile.
let ast = zerodds_idl::parse("@final struct S { any value; };", &ParserConfig::default())
.expect("parse");
assert!(generate_c_header(&ast, &CGenOptions::default()).is_err());
}
}