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
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
|
// Copyright (C) 2020-2024 Free Software Foundation, Inc.
// This file is part of GCC.
// GCC is free software; you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free
// Software Foundation; either version 3, or (at your option) any later
// version.
// GCC is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
// for more details.
// You should have received a copy of the GNU General Public License
// along with GCC; see the file COPYING3. If not see
// <http://www.gnu.org/licenses/>.
#ifndef RUST_AST_ITEM_H
#define RUST_AST_ITEM_H
#include "rust-ast.h"
#include "rust-hir-map.h"
#include "rust-mapping-common.h"
#include "rust-path.h"
#include "rust-common.h"
#include "rust-expr.h"
namespace Rust {
namespace AST {
// forward decls
class TypePath;
// TODO: inline?
/*struct AbiName {
std::string abi_name;
// Technically is meant to be STRING_LITERAL
public:
// Returns whether abi name is empty, i.e. doesn't exist.
bool is_empty() const {
return abi_name.empty();
}
AbiName(std::string name) : abi_name(std::move(name)) {}
// Empty AbiName constructor
AbiName() {}
};*/
// A type generic parameter (as opposed to a lifetime generic parameter)
class TypeParam : public GenericParam
{
// bool has_outer_attribute;
// std::unique_ptr<Attribute> outer_attr;
Attribute outer_attr;
Identifier type_representation;
// bool has_type_param_bounds;
// TypeParamBounds type_param_bounds;
std::vector<std::unique_ptr<TypeParamBound>>
type_param_bounds; // inlined form
// bool has_type;
std::unique_ptr<Type> type;
location_t locus;
public:
Identifier get_type_representation () const { return type_representation; }
// Returns whether the type of the type param has been specified.
bool has_type () const { return type != nullptr; }
// Returns whether the type param has type param bounds.
bool has_type_param_bounds () const { return !type_param_bounds.empty (); }
// Returns whether the type param has an outer attribute.
bool has_outer_attribute () const { return !outer_attr.is_empty (); }
Attribute &get_outer_attribute () { return outer_attr; }
TypeParam (Identifier type_representation, location_t locus = UNDEF_LOCATION,
std::vector<std::unique_ptr<TypeParamBound>> type_param_bounds
= std::vector<std::unique_ptr<TypeParamBound>> (),
std::unique_ptr<Type> type = nullptr,
Attribute outer_attr = Attribute::create_empty ())
: GenericParam (Analysis::Mappings::get ()->get_next_node_id ()),
outer_attr (std::move (outer_attr)),
type_representation (std::move (type_representation)),
type_param_bounds (std::move (type_param_bounds)),
type (std::move (type)), locus (locus)
{}
// Copy constructor uses clone
TypeParam (TypeParam const &other)
: GenericParam (other.node_id), outer_attr (other.outer_attr),
type_representation (other.type_representation), locus (other.locus)
{
// guard to prevent null pointer dereference
if (other.type != nullptr)
type = other.type->clone_type ();
type_param_bounds.reserve (other.type_param_bounds.size ());
for (const auto &e : other.type_param_bounds)
type_param_bounds.push_back (e->clone_type_param_bound ());
}
// Overloaded assignment operator to clone
TypeParam &operator= (TypeParam const &other)
{
type_representation = other.type_representation;
outer_attr = other.outer_attr;
locus = other.locus;
node_id = other.node_id;
// guard to prevent null pointer dereference
if (other.type != nullptr)
type = other.type->clone_type ();
else
type = nullptr;
type_param_bounds.reserve (other.type_param_bounds.size ());
for (const auto &e : other.type_param_bounds)
type_param_bounds.push_back (e->clone_type_param_bound ());
return *this;
}
// move constructors
TypeParam (TypeParam &&other) = default;
TypeParam &operator= (TypeParam &&other) = default;
std::string as_string () const override;
location_t get_locus () const override final { return locus; }
Kind get_kind () const override final { return Kind::Type; }
void accept_vis (ASTVisitor &vis) override;
// TODO: is this better? Or is a "vis_block" better?
std::unique_ptr<Type> &get_type ()
{
rust_assert (type != nullptr);
return type;
}
// TODO: mutable getter seems kinda dodgy
std::vector<std::unique_ptr<TypeParamBound>> &get_type_param_bounds ()
{
return type_param_bounds;
}
const std::vector<std::unique_ptr<TypeParamBound>> &
get_type_param_bounds () const
{
return type_param_bounds;
}
protected:
// Clone function implementation as virtual method
TypeParam *clone_generic_param_impl () const override
{
return new TypeParam (*this);
}
};
/* "where" clause item base. Abstract - use LifetimeWhereClauseItem,
* TypeBoundWhereClauseItem */
class WhereClauseItem
{
public:
virtual ~WhereClauseItem () {}
// Unique pointer custom clone function
std::unique_ptr<WhereClauseItem> clone_where_clause_item () const
{
return std::unique_ptr<WhereClauseItem> (clone_where_clause_item_impl ());
}
virtual std::string as_string () const = 0;
virtual void accept_vis (ASTVisitor &vis) = 0;
virtual NodeId get_node_id () const = 0;
protected:
// Clone function implementation as pure virtual method
virtual WhereClauseItem *clone_where_clause_item_impl () const = 0;
};
// A lifetime where clause item
class LifetimeWhereClauseItem : public WhereClauseItem
{
Lifetime lifetime;
std::vector<Lifetime> lifetime_bounds;
location_t locus;
NodeId node_id;
public:
LifetimeWhereClauseItem (Lifetime lifetime,
std::vector<Lifetime> lifetime_bounds,
location_t locus)
: lifetime (std::move (lifetime)),
lifetime_bounds (std::move (lifetime_bounds)), locus (locus),
node_id (Analysis::Mappings::get ()->get_next_node_id ())
{}
std::string as_string () const override;
void accept_vis (ASTVisitor &vis) override;
NodeId get_node_id () const override final { return node_id; }
Lifetime &get_lifetime () { return lifetime; }
std::vector<Lifetime> &get_lifetime_bounds () { return lifetime_bounds; }
location_t get_locus () const { return locus; }
protected:
// Clone function implementation as (not pure) virtual method
LifetimeWhereClauseItem *clone_where_clause_item_impl () const override
{
return new LifetimeWhereClauseItem (*this);
}
};
// A type bound where clause item
class TypeBoundWhereClauseItem : public WhereClauseItem
{
std::vector<LifetimeParam> for_lifetimes;
std::unique_ptr<Type> bound_type;
std::vector<std::unique_ptr<TypeParamBound>> type_param_bounds;
NodeId node_id;
location_t locus;
public:
// Returns whether the item has ForLifetimes
bool has_for_lifetimes () const { return !for_lifetimes.empty (); }
std::vector<LifetimeParam> &get_for_lifetimes () { return for_lifetimes; }
// Returns whether the item has type param bounds
bool has_type_param_bounds () const { return !type_param_bounds.empty (); }
TypeBoundWhereClauseItem (
std::vector<LifetimeParam> for_lifetimes, std::unique_ptr<Type> bound_type,
std::vector<std::unique_ptr<TypeParamBound>> type_param_bounds,
location_t locus)
: for_lifetimes (std::move (for_lifetimes)),
bound_type (std::move (bound_type)),
type_param_bounds (std::move (type_param_bounds)),
node_id (Analysis::Mappings::get ()->get_next_node_id ()), locus (locus)
{}
// Copy constructor requires clone
TypeBoundWhereClauseItem (TypeBoundWhereClauseItem const &other)
: for_lifetimes (other.for_lifetimes),
bound_type (other.bound_type->clone_type ())
{
node_id = other.node_id;
type_param_bounds.reserve (other.type_param_bounds.size ());
for (const auto &e : other.type_param_bounds)
type_param_bounds.push_back (e->clone_type_param_bound ());
}
// Overload assignment operator to clone
TypeBoundWhereClauseItem &operator= (TypeBoundWhereClauseItem const &other)
{
node_id = other.node_id;
for_lifetimes = other.for_lifetimes;
bound_type = other.bound_type->clone_type ();
type_param_bounds.reserve (other.type_param_bounds.size ());
for (const auto &e : other.type_param_bounds)
type_param_bounds.push_back (e->clone_type_param_bound ());
return *this;
}
// move constructors
TypeBoundWhereClauseItem (TypeBoundWhereClauseItem &&other) = default;
TypeBoundWhereClauseItem &operator= (TypeBoundWhereClauseItem &&other)
= default;
std::string as_string () const override;
void accept_vis (ASTVisitor &vis) override;
std::unique_ptr<Type> &get_type ()
{
rust_assert (bound_type != nullptr);
return bound_type;
}
// TODO: this mutable getter seems really dodgy. Think up better way.
std::vector<std::unique_ptr<TypeParamBound>> &get_type_param_bounds ()
{
return type_param_bounds;
}
const std::vector<std::unique_ptr<TypeParamBound>> &
get_type_param_bounds () const
{
return type_param_bounds;
}
NodeId get_node_id () const override final { return node_id; }
location_t get_locus () const { return locus; }
protected:
// Clone function implementation as (not pure) virtual method
TypeBoundWhereClauseItem *clone_where_clause_item_impl () const override
{
return new TypeBoundWhereClauseItem (*this);
}
};
// A where clause
class WhereClause
{
std::vector<std::unique_ptr<WhereClauseItem>> where_clause_items;
NodeId node_id;
public:
WhereClause (std::vector<std::unique_ptr<WhereClauseItem>> where_clause_items)
: where_clause_items (std::move (where_clause_items)),
node_id (Analysis::Mappings::get ()->get_next_node_id ())
{}
// copy constructor with vector clone
WhereClause (WhereClause const &other)
{
node_id = other.node_id;
where_clause_items.reserve (other.where_clause_items.size ());
for (const auto &e : other.where_clause_items)
where_clause_items.push_back (e->clone_where_clause_item ());
}
// overloaded assignment operator with vector clone
WhereClause &operator= (WhereClause const &other)
{
node_id = other.node_id;
where_clause_items.reserve (other.where_clause_items.size ());
for (const auto &e : other.where_clause_items)
where_clause_items.push_back (e->clone_where_clause_item ());
return *this;
}
// move constructors
WhereClause (WhereClause &&other) = default;
WhereClause &operator= (WhereClause &&other) = default;
// Creates a WhereClause with no items.
static WhereClause create_empty ()
{
return WhereClause (std::vector<std::unique_ptr<WhereClauseItem>> ());
}
// Returns whether the WhereClause has no items.
bool is_empty () const { return where_clause_items.empty (); }
std::string as_string () const;
NodeId get_node_id () const { return node_id; }
// TODO: this mutable getter seems kinda dodgy
std::vector<std::unique_ptr<WhereClauseItem>> &get_items ()
{
return where_clause_items;
}
const std::vector<std::unique_ptr<WhereClauseItem>> &get_items () const
{
return where_clause_items;
}
};
// Abstract class Param
class Param : public Visitable
{
public:
Param (std::vector<Attribute> outer_attrs, location_t locus)
: outer_attrs (std::move (outer_attrs)), locus (locus),
node_id (Analysis::Mappings::get ()->get_next_node_id ())
{}
virtual ~Param () = default;
std::unique_ptr<Param> clone_param () const
{
return std::unique_ptr<Param> (clone_param_impl ());
}
virtual bool is_variadic () const { return false; }
virtual bool is_self () const { return false; }
NodeId get_node_id () const { return node_id; }
location_t get_locus () const { return locus; }
std::vector<Attribute> get_outer_attrs () const { return outer_attrs; }
std::vector<Attribute> &get_outer_attrs () { return outer_attrs; }
virtual Param *clone_param_impl () const = 0;
virtual std::string as_string () const = 0;
protected:
std::vector<Attribute> outer_attrs;
location_t locus;
NodeId node_id;
};
// A self parameter in a method
class SelfParam : public Param
{
bool has_ref;
bool is_mut;
// bool has_lifetime; // only possible if also ref
Lifetime lifetime;
// bool has_type; // only possible if not ref
std::unique_ptr<Type> type;
// Unrestricted constructor used for error state
SelfParam (Lifetime lifetime, bool has_ref, bool is_mut, Type *type)
: Param ({}, UNDEF_LOCATION), has_ref (has_ref), is_mut (is_mut),
lifetime (std::move (lifetime)), type (type)
{}
// this is ok as no outside classes can ever call this
// TODO: self param can have outer attributes
public:
// Returns whether the self-param has a type field.
bool has_type () const { return type != nullptr; }
// Returns whether the self-param has a valid lifetime.
bool has_lifetime () const { return !lifetime.is_error (); }
// Returns whether the self-param is in an error state.
bool is_error () const
{
return (has_type () && has_lifetime ()) || (has_lifetime () && !has_ref);
// not having either is not an error
}
// Creates an error state self-param.
static SelfParam create_error ()
{
// cannot have no ref but have a lifetime at the same time
return SelfParam (Lifetime (Lifetime::STATIC), false, false, nullptr);
}
// Type-based self parameter (not ref, no lifetime)
SelfParam (std::unique_ptr<Type> type, bool is_mut, location_t locus)
: Param ({}, locus), has_ref (false), is_mut (is_mut),
lifetime (Lifetime::error ()), type (std::move (type))
{}
// Lifetime-based self parameter (is ref, no type)
SelfParam (Lifetime lifetime, bool is_mut, location_t locus)
: Param ({}, locus), has_ref (true), is_mut (is_mut),
lifetime (std::move (lifetime))
{}
// Copy constructor requires clone
SelfParam (SelfParam const &other)
: Param (other.get_outer_attrs (), other.get_locus ()),
has_ref (other.has_ref), is_mut (other.is_mut), lifetime (other.lifetime)
{
node_id = other.node_id;
if (other.type != nullptr)
type = other.type->clone_type ();
}
// Overload assignment operator to use clone
SelfParam &operator= (SelfParam const &other)
{
is_mut = other.is_mut;
has_ref = other.has_ref;
lifetime = other.lifetime;
locus = other.locus;
node_id = other.node_id;
outer_attrs = other.outer_attrs;
if (other.type != nullptr)
type = other.type->clone_type ();
else
type = nullptr;
return *this;
}
// move constructors
SelfParam (SelfParam &&other) = default;
SelfParam &operator= (SelfParam &&other) = default;
std::string as_string () const override;
location_t get_locus () const { return locus; }
bool is_self () const override { return true; }
bool get_has_ref () const { return has_ref; };
bool get_is_mut () const { return is_mut; }
Lifetime get_lifetime () const { return lifetime; }
Lifetime &get_lifetime () { return lifetime; }
NodeId get_node_id () const { return node_id; }
// TODO: is this better? Or is a "vis_block" better?
std::unique_ptr<Type> &get_type ()
{
rust_assert (has_type ());
return type;
}
void accept_vis (ASTVisitor &vis) override;
SelfParam *clone_param_impl () const override
{
return new SelfParam (*this);
}
};
// Qualifiers for function, i.e. const, unsafe, extern etc.
class FunctionQualifiers
{
Async async_status;
Const const_status;
Unsafety unsafe_status;
bool has_extern;
std::string extern_abi;
location_t locus;
public:
FunctionQualifiers (location_t locus, Async async_status, Const const_status,
Unsafety unsafe_status, bool has_extern = false,
std::string extern_abi = std::string ())
: async_status (async_status), const_status (const_status),
unsafe_status (unsafe_status), has_extern (has_extern),
extern_abi (std::move (extern_abi)), locus (locus)
{
if (!this->extern_abi.empty ())
{
// having extern is required; not having it is an implementation error
rust_assert (has_extern);
}
}
std::string as_string () const;
bool is_unsafe () const { return unsafe_status == Unsafety::Unsafe; }
bool is_extern () const { return has_extern; }
bool is_const () const { return const_status == Const::Yes; }
bool is_async () const { return async_status == Async::Yes; }
std::string get_extern_abi () const { return extern_abi; }
bool has_abi () const { return !extern_abi.empty (); }
Const get_const_status () const { return const_status; }
Async get_async_status () const { return async_status; }
location_t get_locus () const { return locus; }
};
class VariadicParam : public Param
{
std::unique_ptr<Pattern> param_name;
public:
VariadicParam (std::unique_ptr<Pattern> param_name,
std::vector<Attribute> outer_attrs, location_t locus)
: Param (std::move (outer_attrs), std::move (locus)),
param_name (std::move (param_name))
{}
VariadicParam (std::vector<Attribute> outer_attrs, location_t locus)
: Param (std::move (outer_attrs), std::move (locus)), param_name (nullptr)
{}
VariadicParam (VariadicParam const &other)
: Param (other.get_outer_attrs (), other.locus)
{
if (other.param_name != nullptr)
param_name = other.param_name->clone_pattern ();
}
VariadicParam &operator= (VariadicParam const &other)
{
outer_attrs = other.outer_attrs;
locus = other.locus;
node_id = other.node_id;
if (other.param_name != nullptr)
param_name = other.param_name->clone_pattern ();
else
param_name = nullptr;
return *this;
}
bool is_variadic () const override { return true; }
VariadicParam *clone_param_impl () const override
{
return new VariadicParam (*this);
}
std::unique_ptr<Pattern> &get_pattern ()
{
rust_assert (param_name != nullptr);
return param_name;
}
const std::unique_ptr<Pattern> &get_pattern () const
{
rust_assert (param_name != nullptr);
return param_name;
}
bool has_pattern () const { return param_name != nullptr; }
void accept_vis (ASTVisitor &vis) override;
std::string as_string () const override;
};
// A function parameter
class FunctionParam : public Param
{
std::unique_ptr<Pattern> param_name;
std::unique_ptr<Type> type;
public:
FunctionParam (std::unique_ptr<Pattern> param_name,
std::unique_ptr<Type> param_type,
std::vector<Attribute> outer_attrs, location_t locus)
: Param (std::move (outer_attrs), locus),
param_name (std::move (param_name)), type (std::move (param_type))
{}
// Copy constructor uses clone
FunctionParam (FunctionParam const &other)
: Param (other.get_outer_attrs (), other.locus)
{
// guard to prevent nullptr dereference
if (other.param_name != nullptr)
param_name = other.param_name->clone_pattern ();
if (other.type != nullptr)
type = other.type->clone_type ();
}
// Overload assignment operator to use clone
FunctionParam &operator= (FunctionParam const &other)
{
locus = other.locus;
node_id = other.node_id;
// guard to prevent nullptr dereference
if (other.param_name != nullptr)
param_name = other.param_name->clone_pattern ();
else
param_name = nullptr;
if (other.type != nullptr)
type = other.type->clone_type ();
else
type = nullptr;
return *this;
}
// move constructors
FunctionParam (FunctionParam &&other) = default;
FunctionParam &operator= (FunctionParam &&other) = default;
// Returns whether FunctionParam is in an invalid state.
bool is_error () const { return param_name == nullptr || type == nullptr; }
// Creates an error FunctionParam.
static FunctionParam create_error ()
{
return FunctionParam (nullptr, nullptr, {}, UNDEF_LOCATION);
}
std::string as_string () const override;
// TODO: seems kinda dodgy. Think of better way.
std::vector<Attribute> &get_outer_attrs () { return outer_attrs; }
const std::vector<Attribute> &get_outer_attrs () const { return outer_attrs; }
// TODO: is this better? Or is a "vis_block" better?
std::unique_ptr<Pattern> &get_pattern ()
{
rust_assert (param_name != nullptr);
return param_name;
}
bool has_name () const { return param_name != nullptr; }
// TODO: is this better? Or is a "vis_block" better?
std::unique_ptr<Type> &get_type ()
{
rust_assert (type != nullptr);
return type;
}
FunctionParam *clone_param_impl () const override
{
return new FunctionParam (*this);
}
void accept_vis (ASTVisitor &vis) override;
};
// Rust module item - abstract base class
class Module : public VisItem
{
public:
// Type of the current module. A module can be either loaded or unloaded,
// meaning that the items of the module can already be present or not. For
// example, the following module would be loaded: `mod foo { fn bar() {} }`.
// However, the module would be unloaded if it refers to an external file (i.e
// `mod foo;`) and then become loaded upon expansion.
enum ModuleKind
{
LOADED,
UNLOADED,
};
Identifier get_name () const { return module_name; }
AST::Kind get_ast_kind () const override { return AST::Kind::MODULE; }
private:
Identifier module_name;
location_t locus;
ModuleKind kind;
Unsafety safety;
// Name of the file including the module
std::string outer_filename;
// bool has_inner_attrs;
std::vector<Attribute> inner_attrs;
// bool has_items;
std::vector<std::unique_ptr<Item>> items;
// Names of including inline modules (immediate parent is last in the list)
std::vector<std::string> module_scope;
// Filename the module refers to. Empty string on LOADED modules or if an
// error occured when dealing with UNLOADED modules
std::string module_file;
void clone_items (const std::vector<std::unique_ptr<Item>> &other_items)
{
items.reserve (other_items.size ());
for (const auto &e : other_items)
items.push_back (e->clone_item ());
}
public:
// Returns whether the module has items in its body.
bool has_items () const { return !items.empty (); }
// Returns whether the module has any inner attributes.
bool has_inner_attrs () const { return !inner_attrs.empty (); }
// Unloaded module constructor
Module (Identifier module_name, Visibility visibility,
std::vector<Attribute> outer_attrs, location_t locus, Unsafety safety,
std::string outer_filename, std::vector<std::string> module_scope)
: VisItem (std::move (visibility), std::move (outer_attrs)),
module_name (module_name), locus (locus), kind (ModuleKind::UNLOADED),
safety (safety), outer_filename (outer_filename),
inner_attrs (std::vector<Attribute> ()),
items (std::vector<std::unique_ptr<Item>> ()),
module_scope (std::move (module_scope))
{}
// Loaded module constructor, with items
Module (Identifier name, location_t locus,
std::vector<std::unique_ptr<Item>> items,
Visibility visibility = Visibility::create_error (),
Unsafety safety = Unsafety::Normal,
std::vector<Attribute> inner_attrs = std::vector<Attribute> (),
std::vector<Attribute> outer_attrs = std::vector<Attribute> ())
: VisItem (std::move (visibility), std::move (outer_attrs)),
module_name (name), locus (locus), kind (ModuleKind::LOADED),
safety (safety), outer_filename (std::string ()),
inner_attrs (std::move (inner_attrs)), items (std::move (items))
{}
// Copy constructor with vector clone
Module (Module const &other)
: VisItem (other), module_name (other.module_name), locus (other.locus),
kind (other.kind), safety (other.safety), inner_attrs (other.inner_attrs),
module_scope (other.module_scope)
{
// We need to check whether we are copying a loaded module or an unloaded
// one. In the second case, clear the `items` vector.
if (other.kind == LOADED)
clone_items (other.items);
else
items.clear ();
}
// Overloaded assignment operator with vector clone
Module &operator= (Module const &other)
{
VisItem::operator= (other);
module_name = other.module_name;
locus = other.locus;
kind = other.kind;
inner_attrs = other.inner_attrs;
module_scope = other.module_scope;
// Likewise, we need to clear the `items` vector in case the other module is
// unloaded
if (kind == LOADED)
clone_items (other.items);
else
items.clear ();
return *this;
}
// Search for the filename associated with an external module, storing it in
// module_file
void process_file_path ();
// Load the items contained in an external module
void load_items ();
void accept_vis (ASTVisitor &vis) override;
/* Override that runs the function recursively on all items contained within
* the module. */
void add_crate_name (std::vector<std::string> &names) const override;
// Returns the kind of the module
enum ModuleKind get_kind () const { return kind; }
Unsafety get_unsafety () const { return safety; }
// TODO: think of better way to do this - mutable getter seems dodgy
const std::vector<Attribute> &get_inner_attrs () const { return inner_attrs; }
std::vector<Attribute> &get_inner_attrs () { return inner_attrs; }
const std::vector<std::unique_ptr<Item>> &get_items () const { return items; }
std::vector<std::unique_ptr<Item>> &get_items () { return items; }
std::vector<std::unique_ptr<AST::Item>> take_items ()
{
return std::move (items);
}
void set_items (std::vector<std::unique_ptr<AST::Item>> &&new_items)
{
items = std::move (new_items);
}
// move constructors
Module (Module &&other) = default;
Module &operator= (Module &&other) = default;
std::string as_string () const override;
location_t get_locus () const override final { return locus; }
// Invalid if name is empty, so base stripping on that.
void mark_for_strip () override { module_name = {""}; }
bool is_marked_for_strip () const override { return module_name.empty (); }
protected:
/* Use covariance to implement clone function as returning this object
* rather than base */
Module *clone_item_impl () const override { return new Module (*this); }
};
// Rust extern crate declaration AST node
class ExternCrate : public VisItem
{
// this is either an identifier or "self", with self parsed to string
std::string referenced_crate;
// bool has_as_clause;
// AsClause as_clause;
// this is either an identifier or "_", with _ parsed to string
std::string as_clause_name;
location_t locus;
/* e.g.
"extern crate foo as _"
"extern crate foo"
"extern crate std as cool_std" */
public:
std::string as_string () const override;
// Returns whether extern crate declaration has an as clause.
bool has_as_clause () const { return !as_clause_name.empty (); }
/* Returns whether extern crate declaration references the current crate
* (i.e. self). */
bool references_self () const { return referenced_crate == "self"; }
// Constructor
ExternCrate (std::string referenced_crate, Visibility visibility,
std::vector<Attribute> outer_attrs, location_t locus,
std::string as_clause_name = std::string ())
: VisItem (std::move (visibility), std::move (outer_attrs)),
referenced_crate (std::move (referenced_crate)),
as_clause_name (std::move (as_clause_name)), locus (locus)
{}
location_t get_locus () const override final { return locus; }
void accept_vis (ASTVisitor &vis) override;
const std::string &get_referenced_crate () const { return referenced_crate; }
const std::string &get_as_clause () const { return as_clause_name; }
// Override that adds extern crate name in decl to passed list of names.
void add_crate_name (std::vector<std::string> &names) const override
{
names.push_back (referenced_crate);
}
// Invalid if crate name is empty, so base stripping on that.
void mark_for_strip () override { referenced_crate = ""; }
bool is_marked_for_strip () const override
{
return referenced_crate.empty ();
}
protected:
/* Use covariance to implement clone function as returning this object
* rather than base */
ExternCrate *clone_item_impl () const override
{
return new ExternCrate (*this);
}
};
// The path-ish thing referred to in a use declaration - abstract base class
class UseTree
{
location_t locus;
NodeId node_id;
public:
enum Kind
{
Glob,
Rebind,
List,
};
virtual ~UseTree () {}
// Overload assignment operator to clone
UseTree &operator= (UseTree const &other)
{
locus = other.locus;
return *this;
}
UseTree (const UseTree &other) = default;
// move constructors
UseTree (UseTree &&other) = default;
UseTree &operator= (UseTree &&other) = default;
// Unique pointer custom clone function
std::unique_ptr<UseTree> clone_use_tree () const
{
return std::unique_ptr<UseTree> (clone_use_tree_impl ());
}
virtual std::string as_string () const = 0;
virtual Kind get_kind () const = 0;
location_t get_locus () const { return locus; }
NodeId get_node_id () const { return node_id; }
virtual void accept_vis (ASTVisitor &vis) = 0;
protected:
// Clone function implementation as pure virtual method
virtual UseTree *clone_use_tree_impl () const = 0;
UseTree (location_t locus)
: locus (locus), node_id (Analysis::Mappings::get ()->get_next_node_id ())
{}
};
// Use tree with a glob (wildcard) operator
class UseTreeGlob : public UseTree
{
public:
enum PathType
{
NO_PATH,
GLOBAL,
PATH_PREFIXED
};
private:
PathType glob_type;
SimplePath path;
public:
UseTreeGlob (PathType glob_type, SimplePath path, location_t locus)
: UseTree (locus), glob_type (glob_type), path (std::move (path))
{
if (this->glob_type != PATH_PREFIXED)
{
// compiler implementation error if there is a path with a
// non-path-prefixed use tree glob
rust_assert (!has_path ());
}
// TODO: do path-prefixed paths also have to have a path? If so, have an
// assert for that too.
}
/* Returns whether has path. Should be made redundant by PathType
* PATH_PREFIXED. */
bool has_path () const { return !path.is_empty (); }
std::string as_string () const override;
void accept_vis (ASTVisitor &vis) override;
PathType get_glob_type () { return glob_type; }
Kind get_kind () const override { return Glob; }
SimplePath get_path () const
{
rust_assert (has_path ());
return path;
}
SimplePath &get_path () { return path; }
/* TODO: find way to ensure only PATH_PREFIXED glob_type has path - factory
* methods? */
protected:
/* Use covariance to implement clone function as returning this object
* rather than base */
UseTreeGlob *clone_use_tree_impl () const override
{
return new UseTreeGlob (*this);
}
};
// Use tree with a list of paths with a common prefix
class UseTreeList : public UseTree
{
public:
enum PathType
{
NO_PATH,
GLOBAL,
PATH_PREFIXED
};
private:
PathType path_type;
SimplePath path;
std::vector<std::unique_ptr<UseTree>> trees;
public:
UseTreeList (PathType path_type, SimplePath path,
std::vector<std::unique_ptr<UseTree>> trees, location_t locus)
: UseTree (locus), path_type (path_type), path (std::move (path)),
trees (std::move (trees))
{
if (this->path_type != PATH_PREFIXED)
{
// compiler implementation error if there is a path with a
// non-path-prefixed use tree glob
rust_assert (!has_path ());
}
// TODO: do path-prefixed paths also have to have a path? If so, have an
// assert for that too.
}
// copy constructor with vector clone
UseTreeList (UseTreeList const &other)
: UseTree (other), path_type (other.path_type), path (other.path)
{
trees.reserve (other.trees.size ());
for (const auto &e : other.trees)
trees.push_back (e->clone_use_tree ());
}
// overloaded assignment operator with vector clone
UseTreeList &operator= (UseTreeList const &other)
{
UseTree::operator= (other);
path_type = other.path_type;
path = other.path;
trees.reserve (other.trees.size ());
for (const auto &e : other.trees)
trees.push_back (e->clone_use_tree ());
return *this;
}
// move constructors
UseTreeList (UseTreeList &&other) = default;
UseTreeList &operator= (UseTreeList &&other) = default;
// Returns whether has path. Should be made redundant by path_type.
bool has_path () const { return !path.is_empty (); }
// Returns whether has inner tree elements.
bool has_trees () const { return !trees.empty (); }
std::string as_string () const override;
PathType get_path_type () { return path_type; }
void accept_vis (ASTVisitor &vis) override;
Kind get_kind () const override { return List; }
SimplePath get_path () const
{
rust_assert (has_path ());
return path;
}
SimplePath &get_path () { return path; }
std::vector<std::unique_ptr<UseTree>> &get_trees () { return trees; }
const std::vector<std::unique_ptr<UseTree>> &get_trees () const
{
return trees;
}
// TODO: find way to ensure only PATH_PREFIXED path_type has path - factory
// methods?
protected:
/* Use covariance to implement clone function as returning this object
* rather than base */
UseTreeList *clone_use_tree_impl () const override
{
return new UseTreeList (*this);
}
};
// Use tree where it rebinds the module name as something else
class UseTreeRebind : public UseTree
{
public:
enum NewBindType
{
NONE,
IDENTIFIER,
WILDCARD
};
private:
SimplePath path;
NewBindType bind_type;
Identifier identifier; // only if NewBindType is IDENTIFIER
public:
UseTreeRebind (NewBindType bind_type, SimplePath path, location_t locus,
Identifier identifier = std::string ())
: UseTree (locus), path (std::move (path)), bind_type (bind_type),
identifier (std::move (identifier))
{}
// Returns whether has path (this should always be true).
bool has_path () const { return !path.is_empty (); }
// Returns whether has identifier (or, rather, is allowed to).
bool has_identifier () const { return bind_type == IDENTIFIER; }
std::string as_string () const override;
NewBindType get_new_bind_type () { return bind_type; }
void accept_vis (ASTVisitor &vis) override;
Kind get_kind () const override { return Rebind; }
const SimplePath &get_path () const
{
rust_assert (has_path ());
return path;
}
SimplePath &get_path () { return path; }
const Identifier &get_identifier () const
{
rust_assert (has_identifier ());
return identifier;
}
// TODO: find way to ensure only PATH_PREFIXED path_type has path - factory
// methods?
protected:
/* Use covariance to implement clone function as returning this object
* rather than base */
virtual UseTreeRebind *clone_use_tree_impl () const override
{
return new UseTreeRebind (*this);
}
};
// Rust use declaration (i.e. for modules) AST node
class UseDeclaration : public VisItem
{
std::unique_ptr<UseTree> use_tree;
location_t locus;
public:
std::string as_string () const override;
UseDeclaration (std::unique_ptr<UseTree> use_tree, Visibility visibility,
std::vector<Attribute> outer_attrs, location_t locus)
: VisItem (std::move (visibility), std::move (outer_attrs)),
use_tree (std::move (use_tree)), locus (locus)
{}
// Copy constructor with clone
UseDeclaration (UseDeclaration const &other)
: VisItem (other), locus (other.locus)
{
// guard to prevent null dereference (only required if error state)
if (other.use_tree != nullptr)
use_tree = other.use_tree->clone_use_tree ();
}
// Overloaded assignment operator to clone
UseDeclaration &operator= (UseDeclaration const &other)
{
VisItem::operator= (other);
// visibility = other.visibility->clone_visibility();
// outer_attrs = other.outer_attrs;
locus = other.locus;
// guard to prevent null dereference (only required if error state)
if (other.use_tree != nullptr)
use_tree = other.use_tree->clone_use_tree ();
else
use_tree = nullptr;
return *this;
}
// move constructors
UseDeclaration (UseDeclaration &&other) = default;
UseDeclaration &operator= (UseDeclaration &&other) = default;
location_t get_locus () const override final { return locus; }
std::unique_ptr<UseTree> &get_tree () { return use_tree; }
const std::unique_ptr<UseTree> &get_tree () const { return use_tree; }
void accept_vis (ASTVisitor &vis) override;
// Invalid if use tree is null, so base stripping on that.
void mark_for_strip () override { use_tree = nullptr; }
bool is_marked_for_strip () const override { return use_tree == nullptr; }
protected:
/* Use covariance to implement clone function as returning this object
* rather than base */
UseDeclaration *clone_item_impl () const override
{
return new UseDeclaration (*this);
}
};
class LetStmt;
// Rust function declaration AST node
class Function : public VisItem, public TraitImplItem
{
FunctionQualifiers qualifiers;
Identifier function_name;
std::vector<std::unique_ptr<GenericParam>> generic_params;
std::vector<std::unique_ptr<Param>> function_params;
std::unique_ptr<Type> return_type;
WhereClause where_clause;
tl::optional<std::unique_ptr<BlockExpr>> function_body;
location_t locus;
bool is_default;
public:
std::string as_string () const override;
// Returns whether function has generic parameters.
bool has_generics () const { return !generic_params.empty (); }
// Returns whether function has regular parameters.
bool has_function_params () const { return !function_params.empty (); }
// Returns whether function has return type - if not, it is void.
bool has_return_type () const { return return_type != nullptr; }
// Returns whether function has a where clause.
bool has_where_clause () const { return !where_clause.is_empty (); }
bool has_self_param () const
{
return function_params.size () > 0 && function_params[0]->is_self ();
}
bool has_body () const { return function_body.has_value (); }
// Mega-constructor with all possible fields
Function (Identifier function_name, FunctionQualifiers qualifiers,
std::vector<std::unique_ptr<GenericParam>> generic_params,
std::vector<std::unique_ptr<Param>> function_params,
std::unique_ptr<Type> return_type, WhereClause where_clause,
tl::optional<std::unique_ptr<BlockExpr>> function_body,
Visibility vis, std::vector<Attribute> outer_attrs,
location_t locus, bool is_default = false)
: VisItem (std::move (vis), std::move (outer_attrs)),
qualifiers (std::move (qualifiers)),
function_name (std::move (function_name)),
generic_params (std::move (generic_params)),
function_params (std::move (function_params)),
return_type (std::move (return_type)),
where_clause (std::move (where_clause)),
function_body (std::move (function_body)), locus (locus),
is_default (is_default)
{}
// TODO: add constructor with less fields
// Copy constructor with clone
Function (Function const &other);
// Overloaded assignment operator to clone
Function &operator= (Function const &other);
// move constructors
Function (Function &&other) = default;
Function &operator= (Function &&other) = default;
location_t get_locus () const override final { return locus; }
void accept_vis (ASTVisitor &vis) override;
bool is_variadic () const
{
return function_params.size () != 0
&& function_params.back ()->is_variadic ();
}
// Invalid if block is null, so base stripping on that.
void mark_for_strip () override { function_body = nullptr; }
bool is_marked_for_strip () const override
{
return function_body == nullptr;
}
std::vector<std::unique_ptr<Param>> &get_function_params ()
{
return function_params;
}
const std::vector<std::unique_ptr<Param>> &get_function_params () const
{
return function_params;
}
std::vector<std::unique_ptr<GenericParam>> &get_generic_params ()
{
return generic_params;
}
const std::vector<std::unique_ptr<GenericParam>> &get_generic_params () const
{
return generic_params;
}
// TODO: is this better? Or is a "vis_block" better?
tl::optional<std::unique_ptr<BlockExpr>> &get_definition ()
{
return function_body;
}
const FunctionQualifiers &get_qualifiers () const { return qualifiers; }
FunctionQualifiers &get_qualifiers () { return qualifiers; }
Identifier get_function_name () const { return function_name; }
// TODO: is this better? Or is a "vis_block" better?
WhereClause &get_where_clause () { return where_clause; }
// TODO: is this better? Or is a "vis_block" better?
std::unique_ptr<Type> &get_return_type ()
{
rust_assert (has_return_type ());
return return_type;
}
std::unique_ptr<Param> &get_self_param ()
{
rust_assert (has_self_param ());
return function_params[0];
}
const std::unique_ptr<Param> &get_self_param () const
{
rust_assert (has_self_param ());
return function_params[0];
}
protected:
/* Use covariance to implement clone function as returning this object
* rather than base */
Function *clone_item_impl () const override { return new Function (*this); }
/* Use covariance to implement clone function as returning this object
* rather than base */
Function *clone_associated_item_impl () const override
{
return new Function (*this);
}
};
// Rust type alias (i.e. typedef) AST node
class TypeAlias : public VisItem, public TraitImplItem
{
Identifier new_type_name;
// bool has_generics;
// Generics generic_params;
std::vector<std::unique_ptr<GenericParam>> generic_params; // inlined
// bool has_where_clause;
WhereClause where_clause;
std::unique_ptr<Type> existing_type;
location_t locus;
public:
std::string as_string () const override;
// Returns whether type alias has generic parameters.
bool has_generics () const { return !generic_params.empty (); }
// Returns whether type alias has a where clause.
bool has_where_clause () const { return !where_clause.is_empty (); }
// Mega-constructor with all possible fields
TypeAlias (Identifier new_type_name,
std::vector<std::unique_ptr<GenericParam>> generic_params,
WhereClause where_clause, std::unique_ptr<Type> existing_type,
Visibility vis, std::vector<Attribute> outer_attrs,
location_t locus)
: VisItem (std::move (vis), std::move (outer_attrs)),
new_type_name (std::move (new_type_name)),
generic_params (std::move (generic_params)),
where_clause (std::move (where_clause)),
existing_type (std::move (existing_type)), locus (locus)
{}
// Copy constructor
TypeAlias (TypeAlias const &other)
: VisItem (other), new_type_name (other.new_type_name),
where_clause (other.where_clause), locus (other.locus)
{
// guard to prevent null dereference (only required if error state)
if (other.existing_type != nullptr)
existing_type = other.existing_type->clone_type ();
generic_params.reserve (other.generic_params.size ());
for (const auto &e : other.generic_params)
generic_params.push_back (e->clone_generic_param ());
}
// Overloaded assignment operator to clone
TypeAlias &operator= (TypeAlias const &other)
{
VisItem::operator= (other);
new_type_name = other.new_type_name;
where_clause = other.where_clause;
// visibility = other.visibility->clone_visibility();
// outer_attrs = other.outer_attrs;
locus = other.locus;
// guard to prevent null dereference (only required if error state)
if (other.existing_type != nullptr)
existing_type = other.existing_type->clone_type ();
else
existing_type = nullptr;
generic_params.reserve (other.generic_params.size ());
for (const auto &e : other.generic_params)
generic_params.push_back (e->clone_generic_param ());
return *this;
}
// move constructors
TypeAlias (TypeAlias &&other) = default;
TypeAlias &operator= (TypeAlias &&other) = default;
location_t get_locus () const override final { return locus; }
void accept_vis (ASTVisitor &vis) override;
// Invalid if existing type is null, so base stripping on that.
void mark_for_strip () override { existing_type = nullptr; }
bool is_marked_for_strip () const override
{
return existing_type == nullptr;
}
std::vector<std::unique_ptr<GenericParam>> &get_generic_params ()
{
return generic_params;
}
const std::vector<std::unique_ptr<GenericParam>> &get_generic_params () const
{
return generic_params;
}
// TODO: is this better? Or is a "vis_block" better?
WhereClause &get_where_clause () { return where_clause; }
// TODO: is this better? Or is a "vis_block" better?
std::unique_ptr<Type> &get_type_aliased ()
{
rust_assert (existing_type != nullptr);
return existing_type;
}
Identifier get_new_type_name () const { return new_type_name; }
protected:
/* Use covariance to implement clone function as returning this object
* rather than base */
TypeAlias *clone_item_impl () const override { return new TypeAlias (*this); }
/* Use covariance to implement clone function as returning this object
* rather than base */
TypeAlias *clone_associated_item_impl () const override
{
return new TypeAlias (*this);
}
};
// Rust base struct declaration AST node - abstract base class
class Struct : public VisItem
{
protected:
// protected to enable access by derived classes - allows better as_string
Identifier struct_name;
// bool has_generics;
// Generics generic_params;
std::vector<std::unique_ptr<GenericParam>> generic_params; // inlined
// bool has_where_clause;
WhereClause where_clause;
private:
location_t locus;
public:
// Returns whether struct has generic parameters.
bool has_generics () const { return !generic_params.empty (); }
// Returns whether struct has a where clause.
bool has_where_clause () const { return !where_clause.is_empty (); }
location_t get_locus () const override final { return locus; }
// Invalid if name is empty, so base stripping on that.
void mark_for_strip () override { struct_name = {""}; }
bool is_marked_for_strip () const override { return struct_name.empty (); }
Identifier get_struct_name () const { return struct_name; }
std::vector<std::unique_ptr<GenericParam>> &get_generic_params ()
{
return generic_params;
}
const std::vector<std::unique_ptr<GenericParam>> &get_generic_params () const
{
return generic_params;
}
// TODO: is this better? Or is a "vis_block" better?
WhereClause &get_where_clause () { return where_clause; }
Identifier get_identifier () const { return struct_name; }
protected:
Struct (Identifier struct_name,
std::vector<std::unique_ptr<GenericParam>> generic_params,
WhereClause where_clause, Visibility vis, location_t locus,
std::vector<Attribute> outer_attrs = std::vector<Attribute> ())
: VisItem (std::move (vis), std::move (outer_attrs)),
struct_name (std::move (struct_name)),
generic_params (std::move (generic_params)),
where_clause (std::move (where_clause)), locus (locus)
{}
// Copy constructor with vector clone
Struct (Struct const &other)
: VisItem (other), struct_name (other.struct_name),
where_clause (other.where_clause), locus (other.locus)
{
generic_params.reserve (other.generic_params.size ());
for (const auto &e : other.generic_params)
generic_params.push_back (e->clone_generic_param ());
}
// Overloaded assignment operator with vector clone
Struct &operator= (Struct const &other)
{
VisItem::operator= (other);
struct_name = other.struct_name;
where_clause = other.where_clause;
locus = other.locus;
generic_params.reserve (other.generic_params.size ());
for (const auto &e : other.generic_params)
generic_params.push_back (e->clone_generic_param ());
return *this;
}
// move constructors
Struct (Struct &&other) = default;
Struct &operator= (Struct &&other) = default;
};
// A single field in a struct
class StructField
{
// bool has_outer_attributes;
std::vector<Attribute> outer_attrs;
// bool has_visibility;
Visibility visibility;
Identifier field_name;
std::unique_ptr<Type> field_type;
NodeId node_id;
location_t locus;
public:
// Returns whether struct field has any outer attributes.
bool has_outer_attributes () const { return !outer_attrs.empty (); }
// Returns whether struct field has a non-private (non-default) visibility.
bool has_visibility () const { return !visibility.is_error (); }
StructField (Identifier field_name, std::unique_ptr<Type> field_type,
Visibility vis, location_t locus,
std::vector<Attribute> outer_attrs = std::vector<Attribute> ())
: outer_attrs (std::move (outer_attrs)), visibility (std::move (vis)),
field_name (std::move (field_name)), field_type (std::move (field_type)),
node_id (Analysis::Mappings::get ()->get_next_node_id ()), locus (locus)
{}
// Copy constructor
StructField (StructField const &other)
: outer_attrs (other.outer_attrs), visibility (other.visibility),
field_name (other.field_name), node_id (other.node_id),
locus (other.locus)
{
// guard to prevent null dereference
if (other.field_type != nullptr)
field_type = other.field_type->clone_type ();
}
~StructField () = default;
// Overloaded assignment operator to clone
StructField &operator= (StructField const &other)
{
field_name = other.field_name;
visibility = other.visibility;
outer_attrs = other.outer_attrs;
node_id = other.node_id;
// guard to prevent null dereference
if (other.field_type != nullptr)
field_type = other.field_type->clone_type ();
else
field_type = nullptr;
return *this;
}
// move constructors
StructField (StructField &&other) = default;
StructField &operator= (StructField &&other) = default;
// Returns whether struct field is in an error state.
bool is_error () const
{
return field_name.empty () && field_type == nullptr;
// this should really be an or since neither are allowed
}
// Creates an error state struct field.
static StructField create_error ()
{
return StructField (std::string (""), nullptr, Visibility::create_error (),
UNDEF_LOCATION);
}
std::string as_string () const;
// TODO: this mutable getter seems really dodgy. Think up better way.
std::vector<Attribute> &get_outer_attrs () { return outer_attrs; }
const std::vector<Attribute> &get_outer_attrs () const { return outer_attrs; }
Identifier get_field_name () const { return field_name; }
location_t get_locus () const { return locus; }
// TODO: is this better? Or is a "vis_block" better?
std::unique_ptr<Type> &get_field_type ()
{
rust_assert (field_type != nullptr);
return field_type;
}
Visibility &get_visibility () { return visibility; }
const Visibility &get_visibility () const { return visibility; }
NodeId get_node_id () const { return node_id; }
};
// Rust struct declaration with true struct type AST node
class StructStruct : public Struct
{
std::vector<StructField> fields;
bool is_unit;
public:
std::string as_string () const override;
// Mega-constructor with all possible fields
StructStruct (std::vector<StructField> fields, Identifier struct_name,
std::vector<std::unique_ptr<GenericParam>> generic_params,
WhereClause where_clause, bool is_unit, Visibility vis,
std::vector<Attribute> outer_attrs, location_t locus)
: Struct (std::move (struct_name), std::move (generic_params),
std::move (where_clause), std::move (vis), locus,
std::move (outer_attrs)),
fields (std::move (fields)), is_unit (is_unit)
{}
// Unit struct constructor
StructStruct (Identifier struct_name,
std::vector<std::unique_ptr<GenericParam>> generic_params,
WhereClause where_clause, Visibility vis,
std::vector<Attribute> outer_attrs, location_t locus)
: Struct (std::move (struct_name), std::move (generic_params),
std::move (where_clause), std::move (vis), locus,
std::move (outer_attrs)),
is_unit (true)
{}
/* Returns whether the struct is a unit struct - struct defined without
* fields. This is important because it also means an implicit constant of its
* type is defined. */
bool is_unit_struct () const { return is_unit; }
void accept_vis (ASTVisitor &vis) override;
// TODO: this mutable getter seems really dodgy. Think up better way.
std::vector<StructField> &get_fields () { return fields; }
const std::vector<StructField> &get_fields () const { return fields; }
protected:
/* Use covariance to implement clone function as returning this object
* rather than base */
StructStruct *clone_item_impl () const override
{
return new StructStruct (*this);
}
};
// A single field in a tuple
class TupleField
{
// bool has_outer_attributes;
std::vector<Attribute> outer_attrs;
// bool has_visibility;
Visibility visibility;
std::unique_ptr<Type> field_type;
NodeId node_id;
location_t locus;
public:
// Returns whether tuple field has outer attributes.
bool has_outer_attributes () const { return !outer_attrs.empty (); }
/* Returns whether tuple field has a non-default visibility (i.e. a public
* one) */
bool has_visibility () const { return !visibility.is_error (); }
// Complete constructor
TupleField (std::unique_ptr<Type> field_type, Visibility vis,
location_t locus,
std::vector<Attribute> outer_attrs = std::vector<Attribute> ())
: outer_attrs (std::move (outer_attrs)), visibility (std::move (vis)),
field_type (std::move (field_type)),
node_id (Analysis::Mappings::get ()->get_next_node_id ()), locus (locus)
{}
// Copy constructor with clone
TupleField (TupleField const &other)
: outer_attrs (other.outer_attrs), visibility (other.visibility),
node_id (other.node_id), locus (other.locus)
{
// guard to prevent null dereference (only required if error)
if (other.field_type != nullptr)
field_type = other.field_type->clone_type ();
}
~TupleField () = default;
// Overloaded assignment operator to clone
TupleField &operator= (TupleField const &other)
{
visibility = other.visibility;
outer_attrs = other.outer_attrs;
node_id = other.node_id;
locus = other.locus;
// guard to prevent null dereference (only required if error)
if (other.field_type != nullptr)
field_type = other.field_type->clone_type ();
else
field_type = nullptr;
return *this;
}
// move constructors
TupleField (TupleField &&other) = default;
TupleField &operator= (TupleField &&other) = default;
// Returns whether tuple field is in an error state.
bool is_error () const { return field_type == nullptr; }
// Creates an error state tuple field.
static TupleField create_error ()
{
return TupleField (nullptr, Visibility::create_error (), UNDEF_LOCATION);
}
std::string as_string () const;
NodeId get_node_id () const { return node_id; }
Visibility &get_visibility () { return visibility; }
const Visibility &get_visibility () const { return visibility; }
location_t get_locus () const { return locus; }
// TODO: this mutable getter seems really dodgy. Think up better way.
std::vector<Attribute> &get_outer_attrs () { return outer_attrs; }
const std::vector<Attribute> &get_outer_attrs () const { return outer_attrs; }
// TODO: is this better? Or is a "vis_block" better?
std::unique_ptr<Type> &get_field_type ()
{
rust_assert (field_type != nullptr);
return field_type;
}
};
// Rust tuple declared using struct keyword AST node
class TupleStruct : public Struct
{
std::vector<TupleField> fields;
public:
std::string as_string () const override;
// Mega-constructor with all possible fields
TupleStruct (std::vector<TupleField> fields, Identifier struct_name,
std::vector<std::unique_ptr<GenericParam>> generic_params,
WhereClause where_clause, Visibility vis,
std::vector<Attribute> outer_attrs, location_t locus)
: Struct (std::move (struct_name), std::move (generic_params),
std::move (where_clause), std::move (vis), locus,
std::move (outer_attrs)),
fields (std::move (fields))
{}
void accept_vis (ASTVisitor &vis) override;
// TODO: this mutable getter seems really dodgy. Think up better way.
std::vector<TupleField> &get_fields () { return fields; }
const std::vector<TupleField> &get_fields () const { return fields; }
protected:
/* Use covariance to implement clone function as returning this object
* rather than base */
TupleStruct *clone_item_impl () const override
{
return new TupleStruct (*this);
}
};
/* An item used in an "enum" tagged union - not abstract: base represents a
* name-only enum. EnumItems (variants) syntactically allow a Visibility
* annotation. */
class EnumItem : public VisItem
{
Identifier variant_name;
location_t locus;
public:
virtual ~EnumItem () {}
EnumItem (Identifier variant_name, Visibility vis,
std::vector<Attribute> outer_attrs, location_t locus)
: VisItem (std::move (vis), std::move (outer_attrs)),
variant_name (std::move (variant_name)), locus (locus)
{}
// Unique pointer custom clone function
std::unique_ptr<EnumItem> clone_enum_item () const
{
return std::unique_ptr<EnumItem> (clone_item_impl ());
}
virtual std::string as_string () const override;
// not pure virtual as not abstract
virtual void accept_vis (ASTVisitor &vis) override;
location_t get_locus () const override { return locus; }
Identifier get_identifier () const { return variant_name; }
// Based on idea that name is never empty.
void mark_for_strip () override { variant_name = {""}; }
bool is_marked_for_strip () const override { return variant_name.empty (); }
protected:
EnumItem *clone_item_impl () const override { return new EnumItem (*this); }
};
// A tuple item used in an "enum" tagged union
class EnumItemTuple : public EnumItem
{
// bool has_tuple_fields;
std::vector<TupleField> tuple_fields;
public:
// Returns whether tuple enum item has tuple fields.
bool has_tuple_fields () const { return !tuple_fields.empty (); }
EnumItemTuple (Identifier variant_name, Visibility vis,
std::vector<TupleField> tuple_fields,
std::vector<Attribute> outer_attrs, location_t locus)
: EnumItem (std::move (variant_name), std::move (vis),
std::move (outer_attrs), locus),
tuple_fields (std::move (tuple_fields))
{}
std::string as_string () const override;
void accept_vis (ASTVisitor &vis) override;
// TODO: this mutable getter seems really dodgy. Think up better way.
std::vector<TupleField> &get_tuple_fields () { return tuple_fields; }
const std::vector<TupleField> &get_tuple_fields () const
{
return tuple_fields;
}
protected:
// Clone function implementation as (not pure) virtual method
EnumItemTuple *clone_item_impl () const override
{
return new EnumItemTuple (*this);
}
};
// A struct item used in an "enum" tagged union
class EnumItemStruct : public EnumItem
{
// bool has_struct_fields;
std::vector<StructField> struct_fields;
public:
// Returns whether struct enum item has struct fields.
bool has_struct_fields () const { return !struct_fields.empty (); }
EnumItemStruct (Identifier variant_name, Visibility vis,
std::vector<StructField> struct_fields,
std::vector<Attribute> outer_attrs, location_t locus)
: EnumItem (std::move (variant_name), std::move (vis),
std::move (outer_attrs), locus),
struct_fields (std::move (struct_fields))
{}
std::string as_string () const override;
void accept_vis (ASTVisitor &vis) override;
// TODO: this mutable getter seems really dodgy. Think up better way.
std::vector<StructField> &get_struct_fields () { return struct_fields; }
const std::vector<StructField> &get_struct_fields () const
{
return struct_fields;
}
protected:
// Clone function implementation as (not pure) virtual method
EnumItemStruct *clone_item_impl () const override
{
return new EnumItemStruct (*this);
}
};
// A discriminant (numbered enum) item used in an "enum" tagged union
class EnumItemDiscriminant : public EnumItem
{
std::unique_ptr<Expr> expression;
public:
EnumItemDiscriminant (Identifier variant_name, Visibility vis,
std::unique_ptr<Expr> expr,
std::vector<Attribute> outer_attrs, location_t locus)
: EnumItem (std::move (variant_name), std::move (vis),
std::move (outer_attrs), locus),
expression (std::move (expr))
{}
// Copy constructor with clone
EnumItemDiscriminant (EnumItemDiscriminant const &other)
: EnumItem (other), expression (other.expression->clone_expr ())
{}
// Overloaded assignment operator to clone
EnumItemDiscriminant &operator= (EnumItemDiscriminant const &other)
{
EnumItem::operator= (other);
expression = other.expression->clone_expr ();
// variant_name = other.variant_name;
// outer_attrs = other.outer_attrs;
return *this;
}
// move constructors
EnumItemDiscriminant (EnumItemDiscriminant &&other) = default;
EnumItemDiscriminant &operator= (EnumItemDiscriminant &&other) = default;
std::string as_string () const override;
void accept_vis (ASTVisitor &vis) override;
// TODO: is this better? Or is a "vis_block" better?
std::unique_ptr<Expr> &get_expr ()
{
rust_assert (expression != nullptr);
return expression;
}
protected:
// Clone function implementation as (not pure) virtual method
EnumItemDiscriminant *clone_item_impl () const override
{
return new EnumItemDiscriminant (*this);
}
};
// AST node for Rust "enum" - tagged union
class Enum : public VisItem
{
Identifier enum_name;
// bool has_generics;
// Generics generic_params;
std::vector<std::unique_ptr<GenericParam>> generic_params; // inlined
// bool has_where_clause;
WhereClause where_clause;
std::vector<std::unique_ptr<EnumItem>> items;
location_t locus;
public:
std::string as_string () const override;
// Returns whether "enum" has generic parameters.
bool has_generics () const { return !generic_params.empty (); }
// Returns whether "enum" has a where clause.
bool has_where_clause () const { return !where_clause.is_empty (); }
/* Returns whether enum is a "zero-variant" (no possible variant) enum,
* which cannot be instantiated. */
bool is_zero_variant () const { return items.empty (); }
// Mega-constructor
Enum (Identifier enum_name, Visibility vis,
std::vector<std::unique_ptr<GenericParam>> generic_params,
WhereClause where_clause, std::vector<std::unique_ptr<EnumItem>> items,
std::vector<Attribute> outer_attrs, location_t locus)
: VisItem (std::move (vis), std::move (outer_attrs)),
enum_name (std::move (enum_name)),
generic_params (std::move (generic_params)),
where_clause (std::move (where_clause)), items (std::move (items)),
locus (locus)
{}
// TODO: constructor with less arguments
// Copy constructor with vector clone
Enum (Enum const &other)
: VisItem (other), enum_name (other.enum_name),
where_clause (other.where_clause), locus (other.locus)
{
generic_params.reserve (other.generic_params.size ());
for (const auto &e : other.generic_params)
generic_params.push_back (e->clone_generic_param ());
items.reserve (other.items.size ());
for (const auto &e : other.items)
items.push_back (e->clone_enum_item ());
}
// Overloaded assignment operator with vector clone
Enum &operator= (Enum const &other)
{
VisItem::operator= (other);
enum_name = other.enum_name;
where_clause = other.where_clause;
locus = other.locus;
generic_params.reserve (other.generic_params.size ());
for (const auto &e : other.generic_params)
generic_params.push_back (e->clone_generic_param ());
items.reserve (other.items.size ());
for (const auto &e : other.items)
items.push_back (e->clone_enum_item ());
return *this;
}
// Move constructors
Enum (Enum &&other) = default;
Enum &operator= (Enum &&other) = default;
location_t get_locus () const override final { return locus; }
void accept_vis (ASTVisitor &vis) override;
Identifier get_identifier () const { return enum_name; }
// Invalid if name is empty, so base stripping on that.
void mark_for_strip () override { enum_name = {""}; }
bool is_marked_for_strip () const override { return enum_name.empty (); }
// TODO: this mutable getter seems really dodgy. Think up better way.
std::vector<std::unique_ptr<EnumItem>> &get_variants () { return items; }
const std::vector<std::unique_ptr<EnumItem>> &get_variants () const
{
return items;
}
std::vector<std::unique_ptr<GenericParam>> &get_generic_params ()
{
return generic_params;
}
const std::vector<std::unique_ptr<GenericParam>> &get_generic_params () const
{
return generic_params;
}
// TODO: is this better? Or is a "vis_block" better?
WhereClause &get_where_clause () { return where_clause; }
protected:
/* Use covariance to implement clone function as returning this object
* rather than base */
Enum *clone_item_impl () const override { return new Enum (*this); }
};
// Rust untagged union used for C compat AST node
class Union : public VisItem
{
Identifier union_name;
// bool has_generics;
// Generics generic_params;
std::vector<std::unique_ptr<GenericParam>> generic_params; // inlined
// bool has_where_clause;
WhereClause where_clause;
std::vector<StructField> variants;
location_t locus;
public:
std::string as_string () const override;
// Returns whether union has generic params.
bool has_generics () const { return !generic_params.empty (); }
// Returns whether union has where clause.
bool has_where_clause () const { return !where_clause.is_empty (); }
Union (Identifier union_name, Visibility vis,
std::vector<std::unique_ptr<GenericParam>> generic_params,
WhereClause where_clause, std::vector<StructField> variants,
std::vector<Attribute> outer_attrs, location_t locus)
: VisItem (std::move (vis), std::move (outer_attrs)),
union_name (std::move (union_name)),
generic_params (std::move (generic_params)),
where_clause (std::move (where_clause)), variants (std::move (variants)),
locus (locus)
{}
// copy constructor with vector clone
Union (Union const &other)
: VisItem (other), union_name (other.union_name),
where_clause (other.where_clause), variants (other.variants),
locus (other.locus)
{
generic_params.reserve (other.generic_params.size ());
for (const auto &e : other.generic_params)
generic_params.push_back (e->clone_generic_param ());
}
// overloaded assignment operator with vector clone
Union &operator= (Union const &other)
{
VisItem::operator= (other);
union_name = other.union_name;
where_clause = other.where_clause;
variants = other.variants;
locus = other.locus;
generic_params.reserve (other.generic_params.size ());
for (const auto &e : other.generic_params)
generic_params.push_back (e->clone_generic_param ());
return *this;
}
// move constructors
Union (Union &&other) = default;
Union &operator= (Union &&other) = default;
location_t get_locus () const override final { return locus; }
void accept_vis (ASTVisitor &vis) override;
// Invalid if name is empty, so base stripping on that.
void mark_for_strip () override { union_name = {""}; }
bool is_marked_for_strip () const override { return union_name.empty (); }
// TODO: this mutable getter seems really dodgy. Think up better way.
std::vector<StructField> &get_variants () { return variants; }
const std::vector<StructField> &get_variants () const { return variants; }
std::vector<std::unique_ptr<GenericParam>> &get_generic_params ()
{
return generic_params;
}
const std::vector<std::unique_ptr<GenericParam>> &get_generic_params () const
{
return generic_params;
}
// TODO: is this better? Or is a "vis_block" better?
WhereClause &get_where_clause () { return where_clause; }
Identifier get_identifier () const { return union_name; }
protected:
/* Use covariance to implement clone function as returning this object
* rather than base */
Union *clone_item_impl () const override { return new Union (*this); }
};
/* "Constant item" AST node - used for constant, compile-time expressions
* within module scope (like constexpr) */
class ConstantItem : public VisItem, public TraitImplItem
{
// either has an identifier or "_" - maybe handle in identifier?
// bool identifier_is_underscore;
// if no identifier declared, identifier will be "_"
std::string identifier;
std::unique_ptr<Type> type;
std::unique_ptr<Expr> const_expr;
location_t locus;
public:
std::string as_string () const override;
ConstantItem (std::string ident, Visibility vis, std::unique_ptr<Type> type,
std::unique_ptr<Expr> const_expr,
std::vector<Attribute> outer_attrs, location_t locus)
: VisItem (std::move (vis), std::move (outer_attrs)),
identifier (std::move (ident)), type (std::move (type)),
const_expr (std::move (const_expr)), locus (locus)
{}
ConstantItem (std::string ident, Visibility vis, std::unique_ptr<Type> type,
std::vector<Attribute> outer_attrs, location_t locus)
: VisItem (std::move (vis), std::move (outer_attrs)),
identifier (std::move (ident)), type (std::move (type)),
const_expr (nullptr), locus (locus)
{}
ConstantItem (ConstantItem const &other)
: VisItem (other), identifier (other.identifier), locus (other.locus)
{
// guard to prevent null dereference (only required if error state)
if (other.type != nullptr)
type = other.type->clone_type ();
if (other.const_expr != nullptr)
const_expr = other.const_expr->clone_expr ();
}
// Overload assignment operator to clone
ConstantItem &operator= (ConstantItem const &other)
{
VisItem::operator= (other);
identifier = other.identifier;
locus = other.locus;
// guard to prevent null dereference (only required if error state)
if (other.type != nullptr)
type = other.type->clone_type ();
else
type = nullptr;
if (other.const_expr != nullptr)
const_expr = other.const_expr->clone_expr ();
else
const_expr = nullptr;
return *this;
}
// move constructors
ConstantItem (ConstantItem &&other) = default;
ConstantItem &operator= (ConstantItem &&other) = default;
/* Returns whether constant item is an "unnamed" (wildcard underscore used
* as identifier) constant. */
bool is_unnamed () const { return identifier == "_"; }
location_t get_locus () const override final { return locus; }
void accept_vis (ASTVisitor &vis) override;
// Invalid if type or expression are null, so base stripping on that.
void mark_for_strip () override
{
type = nullptr;
const_expr = nullptr;
}
bool is_marked_for_strip () const override
{
return type == nullptr && const_expr == nullptr;
}
bool has_expr () { return const_expr != nullptr; }
// TODO: is this better? Or is a "vis_block" better?
std::unique_ptr<Expr> &get_expr ()
{
rust_assert (const_expr != nullptr);
return const_expr;
}
// TODO: is this better? Or is a "vis_block" better?
std::unique_ptr<Type> &get_type ()
{
rust_assert (type != nullptr);
return type;
}
std::string get_identifier () const { return identifier; }
protected:
/* Use covariance to implement clone function as returning this object
* rather than base */
ConstantItem *clone_item_impl () const override
{
return new ConstantItem (*this);
}
/* Use covariance to implement clone function as returning this object
* rather than base */
ConstantItem *clone_associated_item_impl () const override
{
return new ConstantItem (*this);
}
};
/* Static item AST node - items within module scope with fixed storage
* duration? */
class StaticItem : public VisItem
{
bool has_mut;
Identifier name;
std::unique_ptr<Type> type;
std::unique_ptr<Expr> expr;
location_t locus;
public:
std::string as_string () const override;
StaticItem (Identifier name, bool is_mut, std::unique_ptr<Type> type,
std::unique_ptr<Expr> expr, Visibility vis,
std::vector<Attribute> outer_attrs, location_t locus)
: VisItem (std::move (vis), std::move (outer_attrs)), has_mut (is_mut),
name (std::move (name)), type (std::move (type)), expr (std::move (expr)),
locus (locus)
{}
// Copy constructor with clone
StaticItem (StaticItem const &other)
: VisItem (other), has_mut (other.has_mut), name (other.name),
locus (other.locus)
{
// guard to prevent null dereference (only required if error state)
if (other.type != nullptr)
type = other.type->clone_type ();
if (other.expr != nullptr)
expr = other.expr->clone_expr ();
}
// Overloaded assignment operator to clone
StaticItem &operator= (StaticItem const &other)
{
VisItem::operator= (other);
name = other.name;
has_mut = other.has_mut;
locus = other.locus;
// guard to prevent null dereference (only required if error state)
if (other.type != nullptr)
type = other.type->clone_type ();
else
type = nullptr;
if (other.expr != nullptr)
expr = other.expr->clone_expr ();
else
expr = nullptr;
return *this;
}
// move constructors
StaticItem (StaticItem &&other) = default;
StaticItem &operator= (StaticItem &&other) = default;
location_t get_locus () const override final { return locus; }
void accept_vis (ASTVisitor &vis) override;
// Invalid if type or expression are null, so base stripping on that.
void mark_for_strip () override
{
type = nullptr;
expr = nullptr;
}
bool is_marked_for_strip () const override
{
return type == nullptr && expr == nullptr;
}
bool has_expr () { return expr != nullptr; }
// TODO: is this better? Or is a "vis_block" better?
std::unique_ptr<Expr> &get_expr ()
{
rust_assert (expr != nullptr);
return expr;
}
// TODO: is this better? Or is a "vis_block" better?
std::unique_ptr<Type> &get_type ()
{
rust_assert (type != nullptr);
return type;
}
bool is_mutable () const { return has_mut; }
Identifier get_identifier () const { return name; }
protected:
/* Use covariance to implement clone function as returning this object
* rather than base */
StaticItem *clone_item_impl () const override
{
return new StaticItem (*this);
}
};
// Function declaration in traits
class TraitFunctionDecl
{
// TODO: delete and replace with Function decl item? no as no body in this.
FunctionQualifiers qualifiers;
Identifier function_name;
// bool has_generics;
// Generics generic_params;
std::vector<std::unique_ptr<GenericParam>> generic_params; // inlined
// bool has_params;
// FunctionParams function_params;
std::vector<std::unique_ptr<Param>> function_params; // inlined
// bool has_return_type;
std::unique_ptr<Type> return_type;
// bool has_where_clause;
WhereClause where_clause;
// should this store location info?
public:
// Returns whether function decl has generic parameters.
bool has_generics () const { return !generic_params.empty (); }
// Returns whether function decl has regular parameters.
bool has_params () const { return !function_params.empty (); }
// Returns whether function has return type (otherwise is void).
bool has_return_type () const { return return_type != nullptr; }
// Returns whether function has a where clause.
bool has_where_clause () const { return !where_clause.is_empty (); }
Identifier get_identifier () const { return function_name; }
// Mega-constructor
TraitFunctionDecl (Identifier function_name, FunctionQualifiers qualifiers,
std::vector<std::unique_ptr<GenericParam>> generic_params,
std::vector<std::unique_ptr<Param>> function_params,
std::unique_ptr<Type> return_type,
WhereClause where_clause)
: qualifiers (std::move (qualifiers)),
function_name (std::move (function_name)),
generic_params (std::move (generic_params)),
function_params (std::move (function_params)),
return_type (std::move (return_type)),
where_clause (std::move (where_clause))
{}
// Copy constructor with clone
TraitFunctionDecl (TraitFunctionDecl const &other)
: qualifiers (other.qualifiers), function_name (other.function_name),
where_clause (other.where_clause)
{
// guard to prevent nullptr dereference
if (other.return_type != nullptr)
return_type = other.return_type->clone_type ();
generic_params.reserve (other.generic_params.size ());
for (const auto &e : other.generic_params)
generic_params.push_back (e->clone_generic_param ());
function_params.reserve (other.function_params.size ());
for (const auto &e : other.function_params)
function_params.push_back (e->clone_param ());
}
~TraitFunctionDecl () = default;
// Overloaded assignment operator with clone
TraitFunctionDecl &operator= (TraitFunctionDecl const &other)
{
function_name = other.function_name;
qualifiers = other.qualifiers;
where_clause = other.where_clause;
// guard to prevent nullptr dereference
if (other.return_type != nullptr)
return_type = other.return_type->clone_type ();
else
return_type = nullptr;
generic_params.reserve (other.generic_params.size ());
for (const auto &e : other.generic_params)
generic_params.push_back (e->clone_generic_param ());
function_params.reserve (other.function_params.size ());
for (const auto &e : other.function_params)
function_params.push_back (e->clone_param ());
return *this;
}
// move constructors
TraitFunctionDecl (TraitFunctionDecl &&other) = default;
TraitFunctionDecl &operator= (TraitFunctionDecl &&other) = default;
std::string as_string () const;
// Invalid if function name is empty, so base stripping on that.
void mark_for_strip () { function_name = {""}; }
bool is_marked_for_strip () const { return function_name.empty (); }
// TODO: this mutable getter seems really dodgy. Think up better way.
std::vector<std::unique_ptr<Param>> &get_function_params ()
{
return function_params;
}
const std::vector<std::unique_ptr<Param>> &get_function_params () const
{
return function_params;
}
std::vector<std::unique_ptr<GenericParam>> &get_generic_params ()
{
return generic_params;
}
const std::vector<std::unique_ptr<GenericParam>> &get_generic_params () const
{
return generic_params;
}
// TODO: is this better? Or is a "vis_block" better?
std::unique_ptr<Type> &get_return_type () { return return_type; }
// TODO: is this better? Or is a "vis_block" better?
WhereClause &get_where_clause () { return where_clause; }
FunctionQualifiers get_qualifiers () const { return qualifiers; }
FunctionQualifiers &get_qualifiers () { return qualifiers; }
};
// Actual trait item function declaration within traits
class TraitItemFunc : public TraitItem
{
std::vector<Attribute> outer_attrs;
TraitFunctionDecl decl;
std::unique_ptr<BlockExpr> block_expr;
public:
// Returns whether function has a definition or is just a declaration.
bool has_definition () const { return block_expr != nullptr; }
TraitItemFunc (TraitFunctionDecl decl, std::unique_ptr<BlockExpr> block_expr,
std::vector<Attribute> outer_attrs, location_t locus)
: TraitItem (locus), outer_attrs (std::move (outer_attrs)),
decl (std::move (decl)), block_expr (std::move (block_expr))
{}
// Copy constructor with clone
TraitItemFunc (TraitItemFunc const &other);
// Overloaded assignment operator to clone
TraitItemFunc &operator= (TraitItemFunc const &other);
// move constructors
TraitItemFunc (TraitItemFunc &&other) = default;
TraitItemFunc &operator= (TraitItemFunc &&other) = default;
std::string as_string () const override;
void accept_vis (ASTVisitor &vis) override;
// Invalid if trait decl is empty, so base stripping on that.
void mark_for_strip () override { decl.mark_for_strip (); }
bool is_marked_for_strip () const override
{
return decl.is_marked_for_strip ();
}
// TODO: this mutable getter seems really dodgy. Think up better way.
std::vector<Attribute> &get_outer_attrs () { return outer_attrs; }
const std::vector<Attribute> &get_outer_attrs () const { return outer_attrs; }
// TODO: is this better? Or is a "vis_block" better?
std::unique_ptr<BlockExpr> &get_definition () { return block_expr; }
// TODO: is this better? Or is a "vis_block" better?
TraitFunctionDecl &get_trait_function_decl ()
{
// TODO: maybe only allow access if not marked for strip?
return decl;
}
protected:
// Clone function implementation as (not pure) virtual method
TraitItemFunc *clone_associated_item_impl () const override
{
return new TraitItemFunc (*this);
}
};
// Method declaration within traits
class TraitMethodDecl
{
// TODO: delete and replace with Function decl item? no as no body.
FunctionQualifiers qualifiers;
Identifier function_name;
// bool has_generics;
// Generics generic_params;
std::vector<std::unique_ptr<GenericParam>> generic_params; // inlined
// bool has_params;
// FunctionParams function_params;
std::vector<std::unique_ptr<Param>> function_params; // inlined
// bool has_return_type;
std::unique_ptr<Type> return_type;
// bool has_where_clause;
WhereClause where_clause;
// should this store location info?
public:
// Returns whether method decl has generic parameters.
bool has_generics () const { return !generic_params.empty (); }
// Returns whether method decl has regular parameters.
bool has_params () const { return !function_params.empty (); }
// Returns whether method has return type (otherwise is void).
bool has_return_type () const { return return_type != nullptr; }
// Returns whether method has a where clause.
bool has_where_clause () const { return !where_clause.is_empty (); }
Identifier get_identifier () const { return function_name; }
// Mega-constructor
TraitMethodDecl (Identifier function_name, FunctionQualifiers qualifiers,
std::vector<std::unique_ptr<GenericParam>> generic_params,
std::vector<std::unique_ptr<Param>> function_params,
std::unique_ptr<Type> return_type, WhereClause where_clause)
: qualifiers (std::move (qualifiers)),
function_name (std::move (function_name)),
generic_params (std::move (generic_params)),
function_params (std::move (function_params)),
return_type (std::move (return_type)),
where_clause (std::move (where_clause))
{}
// Copy constructor with clone
TraitMethodDecl (TraitMethodDecl const &other)
: qualifiers (other.qualifiers), function_name (other.function_name),
where_clause (other.where_clause)
{
// guard to prevent nullptr dereference
if (other.return_type != nullptr)
return_type = other.return_type->clone_type ();
generic_params.reserve (other.generic_params.size ());
for (const auto &e : other.generic_params)
generic_params.push_back (e->clone_generic_param ());
function_params.reserve (other.function_params.size ());
for (const auto &e : other.function_params)
function_params.push_back (e->clone_param ());
}
~TraitMethodDecl () = default;
// Overloaded assignment operator with clone
TraitMethodDecl &operator= (TraitMethodDecl const &other)
{
function_name = other.function_name;
qualifiers = other.qualifiers;
where_clause = other.where_clause;
// guard to prevent nullptr dereference
if (other.return_type != nullptr)
return_type = other.return_type->clone_type ();
else
return_type = nullptr;
generic_params.reserve (other.generic_params.size ());
for (const auto &e : other.generic_params)
generic_params.push_back (e->clone_generic_param ());
function_params.reserve (other.function_params.size ());
for (const auto &e : other.function_params)
function_params.push_back (e->clone_param ());
return *this;
}
// move constructors
TraitMethodDecl (TraitMethodDecl &&other) = default;
TraitMethodDecl &operator= (TraitMethodDecl &&other) = default;
std::string as_string () const;
// Invalid if method name is empty, so base stripping on that.
void mark_for_strip () { function_name = {""}; }
bool is_marked_for_strip () const { return function_name.empty (); }
// TODO: this mutable getter seems really dodgy. Think up better way.
std::vector<std::unique_ptr<Param>> &get_function_params ()
{
return function_params;
}
const std::vector<std::unique_ptr<Param>> &get_function_params () const
{
return function_params;
}
std::vector<std::unique_ptr<GenericParam>> &get_generic_params ()
{
return generic_params;
}
const std::vector<std::unique_ptr<GenericParam>> &get_generic_params () const
{
return generic_params;
}
// TODO: is this better? Or is a "vis_block" better?
std::unique_ptr<Type> &get_return_type () { return return_type; }
// TODO: is this better? Or is a "vis_block" better?
WhereClause &get_where_clause () { return where_clause; }
bool has_self () const
{
return !function_params.empty () && function_params[0]->is_self ();
}
std::unique_ptr<Param> &get_self_param ()
{
rust_assert (has_self ());
return function_params[0];
}
const std::unique_ptr<Param> &get_self_param () const
{
rust_assert (has_self ());
return function_params[0];
}
FunctionQualifiers get_qualifiers () const { return qualifiers; }
FunctionQualifiers &get_qualifiers () { return qualifiers; }
};
// Actual trait item method declaration within traits
class TraitItemMethod : public TraitItem
{
std::vector<Attribute> outer_attrs;
TraitMethodDecl decl;
std::unique_ptr<BlockExpr> block_expr;
public:
// Returns whether method has a definition or is just a declaration.
bool has_definition () const { return block_expr != nullptr; }
TraitItemMethod (TraitMethodDecl decl, std::unique_ptr<BlockExpr> block_expr,
std::vector<Attribute> outer_attrs, location_t locus)
: TraitItem (locus), outer_attrs (std::move (outer_attrs)),
decl (std::move (decl)), block_expr (std::move (block_expr))
{}
// Copy constructor with clone
TraitItemMethod (TraitItemMethod const &other);
// Overloaded assignment operator to clone
TraitItemMethod &operator= (TraitItemMethod const &other);
// move constructors
TraitItemMethod (TraitItemMethod &&other) = default;
TraitItemMethod &operator= (TraitItemMethod &&other) = default;
std::string as_string () const override;
void accept_vis (ASTVisitor &vis) override;
// Invalid if trait decl is empty, so base stripping on that.
void mark_for_strip () override { decl.mark_for_strip (); }
bool is_marked_for_strip () const override
{
return decl.is_marked_for_strip ();
}
// TODO: this mutable getter seems really dodgy. Think up better way.
std::vector<Attribute> &get_outer_attrs () { return outer_attrs; }
const std::vector<Attribute> &get_outer_attrs () const { return outer_attrs; }
// TODO: is this better? Or is a "vis_block" better?
TraitMethodDecl &get_trait_method_decl ()
{
// TODO: maybe only allow access if not marked for strip?
return decl;
}
// TODO: is this better? Or is a "vis_block" better?
std::unique_ptr<BlockExpr> &get_definition () { return block_expr; }
protected:
// Clone function implementation as (not pure) virtual method
TraitItemMethod *clone_associated_item_impl () const override
{
return new TraitItemMethod (*this);
}
};
// Constant item within traits
class TraitItemConst : public TraitItem
{
std::vector<Attribute> outer_attrs;
Identifier name;
std::unique_ptr<Type> type;
// bool has_expression;
std::unique_ptr<Expr> expr;
public:
// Whether the constant item has an associated expression.
bool has_expression () const { return expr != nullptr; }
TraitItemConst (Identifier name, std::unique_ptr<Type> type,
std::unique_ptr<Expr> expr,
std::vector<Attribute> outer_attrs, location_t locus)
: TraitItem (locus), outer_attrs (std::move (outer_attrs)),
name (std::move (name)), type (std::move (type)), expr (std::move (expr))
{}
// Copy constructor with clones
TraitItemConst (TraitItemConst const &other)
: TraitItem (other.locus), outer_attrs (other.outer_attrs),
name (other.name)
{
node_id = other.node_id;
// guard to prevent null dereference
if (other.expr != nullptr)
expr = other.expr->clone_expr ();
// guard to prevent null dereference (only for error state)
if (other.type != nullptr)
type = other.type->clone_type ();
}
// Overloaded assignment operator to clone
TraitItemConst &operator= (TraitItemConst const &other)
{
TraitItem::operator= (other);
outer_attrs = other.outer_attrs;
name = other.name;
locus = other.locus;
node_id = other.node_id;
// guard to prevent null dereference
if (other.expr != nullptr)
expr = other.expr->clone_expr ();
else
expr = nullptr;
// guard to prevent null dereference (only for error state)
if (other.type != nullptr)
type = other.type->clone_type ();
else
type = nullptr;
return *this;
}
// move constructors
TraitItemConst (TraitItemConst &&other) = default;
TraitItemConst &operator= (TraitItemConst &&other) = default;
std::string as_string () const override;
location_t get_locus () const override { return locus; }
void accept_vis (ASTVisitor &vis) override;
// Invalid if type is null, so base stripping on that.
void mark_for_strip () override { type = nullptr; }
bool is_marked_for_strip () const override { return type == nullptr; }
// TODO: this mutable getter seems really dodgy. Think up better way.
std::vector<Attribute> &get_outer_attrs () { return outer_attrs; }
const std::vector<Attribute> &get_outer_attrs () const { return outer_attrs; }
bool has_expr () const { return expr != nullptr; }
// TODO: is this better? Or is a "vis_block" better?
std::unique_ptr<Expr> &get_expr ()
{
rust_assert (has_expr ());
return expr;
}
// TODO: is this better? Or is a "vis_block" better?
std::unique_ptr<Type> &get_type ()
{
rust_assert (type != nullptr);
return type;
}
Identifier get_identifier () const { return name; }
protected:
// Clone function implementation as (not pure) virtual method
TraitItemConst *clone_associated_item_impl () const override
{
return new TraitItemConst (*this);
}
};
// Type items within traits
class TraitItemType : public TraitItem
{
std::vector<Attribute> outer_attrs;
Identifier name;
// bool has_type_param_bounds;
// TypeParamBounds type_param_bounds;
std::vector<std::unique_ptr<TypeParamBound>>
type_param_bounds; // inlined form
public:
// Returns whether trait item type has type param bounds.
bool has_type_param_bounds () const { return !type_param_bounds.empty (); }
TraitItemType (Identifier name,
std::vector<std::unique_ptr<TypeParamBound>> type_param_bounds,
std::vector<Attribute> outer_attrs, Visibility vis,
location_t locus)
: TraitItem (vis, locus), outer_attrs (std::move (outer_attrs)),
name (std::move (name)), type_param_bounds (std::move (type_param_bounds))
{}
// Copy constructor with vector clone
TraitItemType (TraitItemType const &other)
: TraitItem (other.locus), outer_attrs (other.outer_attrs),
name (other.name)
{
node_id = other.node_id;
type_param_bounds.reserve (other.type_param_bounds.size ());
for (const auto &e : other.type_param_bounds)
type_param_bounds.push_back (e->clone_type_param_bound ());
}
// Overloaded assignment operator with vector clone
TraitItemType &operator= (TraitItemType const &other)
{
TraitItem::operator= (other);
outer_attrs = other.outer_attrs;
name = other.name;
locus = other.locus;
node_id = other.node_id;
type_param_bounds.reserve (other.type_param_bounds.size ());
for (const auto &e : other.type_param_bounds)
type_param_bounds.push_back (e->clone_type_param_bound ());
return *this;
}
// default move constructors
TraitItemType (TraitItemType &&other) = default;
TraitItemType &operator= (TraitItemType &&other) = default;
std::string as_string () const override;
void accept_vis (ASTVisitor &vis) override;
// Invalid if name is empty, so base stripping on that.
void mark_for_strip () override { name = {""}; }
bool is_marked_for_strip () const override { return name.empty (); }
// TODO: this mutable getter seems really dodgy. Think up better way.
std::vector<Attribute> &get_outer_attrs () { return outer_attrs; }
const std::vector<Attribute> &get_outer_attrs () const { return outer_attrs; }
// TODO: mutable getter seems kinda dodgy
std::vector<std::unique_ptr<TypeParamBound>> &get_type_param_bounds ()
{
return type_param_bounds;
}
const std::vector<std::unique_ptr<TypeParamBound>> &
get_type_param_bounds () const
{
return type_param_bounds;
}
Identifier get_identifier () const { return name; }
protected:
// Clone function implementation as (not pure) virtual method
TraitItemType *clone_associated_item_impl () const override
{
return new TraitItemType (*this);
}
};
// Rust trait item declaration AST node
class Trait : public VisItem
{
bool has_unsafe;
bool has_auto;
Identifier name;
std::vector<std::unique_ptr<GenericParam>> generic_params;
std::vector<std::unique_ptr<TypeParamBound>> type_param_bounds;
WhereClause where_clause;
std::vector<Attribute> inner_attrs;
std::vector<std::unique_ptr<TraitItem>> trait_items;
location_t locus;
public:
std::string as_string () const override;
// Returns whether trait has generic parameters.
bool has_generics () const { return !generic_params.empty (); }
// Returns whether trait has type parameter bounds.
bool has_type_param_bounds () const { return !type_param_bounds.empty (); }
// Returns whether trait has where clause.
bool has_where_clause () const { return !where_clause.is_empty (); }
// Returns whether trait has trait items.
bool has_trait_items () const { return !trait_items.empty (); }
// Returns whether trait has inner attributes.
bool has_inner_attrs () const { return !inner_attrs.empty (); }
Identifier get_identifier () const { return name; }
bool is_unsafe () const { return has_unsafe; }
bool is_auto () const { return has_auto; }
// Mega-constructor
Trait (Identifier name, bool is_unsafe, bool is_auto,
std::vector<std::unique_ptr<GenericParam>> generic_params,
std::vector<std::unique_ptr<TypeParamBound>> type_param_bounds,
WhereClause where_clause,
std::vector<std::unique_ptr<TraitItem>> trait_items, Visibility vis,
std::vector<Attribute> outer_attrs, std::vector<Attribute> inner_attrs,
location_t locus)
: VisItem (std::move (vis), std::move (outer_attrs)),
has_unsafe (is_unsafe), has_auto (is_auto), name (std::move (name)),
generic_params (std::move (generic_params)),
type_param_bounds (std::move (type_param_bounds)),
where_clause (std::move (where_clause)),
inner_attrs (std::move (inner_attrs)),
trait_items (std::move (trait_items)), locus (locus)
{}
// Copy constructor with vector clone
Trait (Trait const &other)
: VisItem (other), has_unsafe (other.has_unsafe), has_auto (other.has_auto),
name (other.name), where_clause (other.where_clause),
inner_attrs (other.inner_attrs), locus (other.locus)
{
generic_params.reserve (other.generic_params.size ());
for (const auto &e : other.generic_params)
generic_params.push_back (e->clone_generic_param ());
type_param_bounds.reserve (other.type_param_bounds.size ());
for (const auto &e : other.type_param_bounds)
type_param_bounds.push_back (e->clone_type_param_bound ());
trait_items.reserve (other.trait_items.size ());
for (const auto &e : other.trait_items)
trait_items.push_back (e->clone_trait_item ());
}
// Overloaded assignment operator with vector clone
Trait &operator= (Trait const &other)
{
VisItem::operator= (other);
name = other.name;
has_unsafe = other.has_unsafe;
has_auto = other.has_auto;
where_clause = other.where_clause;
inner_attrs = other.inner_attrs;
locus = other.locus;
generic_params.reserve (other.generic_params.size ());
for (const auto &e : other.generic_params)
generic_params.push_back (e->clone_generic_param ());
type_param_bounds.reserve (other.type_param_bounds.size ());
for (const auto &e : other.type_param_bounds)
type_param_bounds.push_back (e->clone_type_param_bound ());
trait_items.reserve (other.trait_items.size ());
for (const auto &e : other.trait_items)
trait_items.push_back (e->clone_trait_item ());
return *this;
}
// default move constructors
Trait (Trait &&other) = default;
Trait &operator= (Trait &&other) = default;
location_t get_locus () const override final { return locus; }
void accept_vis (ASTVisitor &vis) override;
// Invalid if trait name is empty, so base stripping on that.
void mark_for_strip () override { name = {""}; }
bool is_marked_for_strip () const override { return name.empty (); }
// TODO: think of better way to do this
const std::vector<Attribute> &get_inner_attrs () const { return inner_attrs; }
std::vector<Attribute> &get_inner_attrs () { return inner_attrs; }
const std::vector<std::unique_ptr<TraitItem>> &get_trait_items () const
{
return trait_items;
}
std::vector<std::unique_ptr<TraitItem>> &get_trait_items ()
{
return trait_items;
}
std::vector<std::unique_ptr<GenericParam>> &get_generic_params ()
{
return generic_params;
}
const std::vector<std::unique_ptr<GenericParam>> &get_generic_params () const
{
return generic_params;
}
std::vector<std::unique_ptr<TypeParamBound>> &get_type_param_bounds ()
{
return type_param_bounds;
}
const std::vector<std::unique_ptr<TypeParamBound>> &
get_type_param_bounds () const
{
return type_param_bounds;
}
WhereClause &get_where_clause () { return where_clause; }
void insert_implict_self (std::unique_ptr<AST::GenericParam> &¶m)
{
std::vector<std::unique_ptr<GenericParam>> new_list;
new_list.reserve (generic_params.size () + 1);
new_list.push_back (std::move (param));
for (auto &p : generic_params)
{
new_list.push_back (std::move (p));
}
generic_params = std::move (new_list);
}
protected:
/* Use covariance to implement clone function as returning this object
* rather than base */
Trait *clone_item_impl () const override { return new Trait (*this); }
};
// Implementation item declaration AST node - abstract base class
class Impl : public VisItem
{
// must be protected to allow subclasses to access them properly
protected:
// bool has_generics;
// Generics generic_params;
std::vector<std::unique_ptr<GenericParam>> generic_params; // inlined
std::unique_ptr<Type> trait_type;
// bool has_where_clause;
WhereClause where_clause;
// bool has_inner_attrs;
std::vector<Attribute> inner_attrs;
private:
// doesn't really need to be protected as write access probably not needed
location_t locus;
public:
// Returns whether impl has generic parameters.
bool has_generics () const { return !generic_params.empty (); }
// Returns whether impl has where clause.
bool has_where_clause () const { return !where_clause.is_empty (); }
// Returns whether impl has inner attributes.
bool has_inner_attrs () const { return !inner_attrs.empty (); }
location_t get_locus () const override final { return locus; }
// Invalid if trait type is null, so base stripping on that.
void mark_for_strip () override { trait_type = nullptr; }
bool is_marked_for_strip () const override { return trait_type == nullptr; }
// TODO: think of better way to do this
const std::vector<Attribute> &get_inner_attrs () const { return inner_attrs; }
std::vector<Attribute> &get_inner_attrs () { return inner_attrs; }
std::vector<std::unique_ptr<GenericParam>> &get_generic_params ()
{
return generic_params;
}
const std::vector<std::unique_ptr<GenericParam>> &get_generic_params () const
{
return generic_params;
}
// TODO: is this better? Or is a "vis_block" better?
WhereClause &get_where_clause () { return where_clause; }
// TODO: is this better? Or is a "vis_block" better?
std::unique_ptr<Type> &get_type ()
{
rust_assert (trait_type != nullptr);
return trait_type;
}
protected:
// Mega-constructor
Impl (std::vector<std::unique_ptr<GenericParam>> generic_params,
std::unique_ptr<Type> trait_type, WhereClause where_clause,
Visibility vis, std::vector<Attribute> inner_attrs,
std::vector<Attribute> outer_attrs, location_t locus)
: VisItem (std::move (vis), std::move (outer_attrs)),
generic_params (std::move (generic_params)),
trait_type (std::move (trait_type)),
where_clause (std::move (where_clause)),
inner_attrs (std::move (inner_attrs)), locus (locus)
{}
// Copy constructor
Impl (Impl const &other)
: VisItem (other), where_clause (other.where_clause),
inner_attrs (other.inner_attrs), locus (other.locus)
{
// guard to prevent null dereference (only required if error state)
if (other.trait_type != nullptr)
trait_type = other.trait_type->clone_type ();
generic_params.reserve (other.generic_params.size ());
for (const auto &e : other.generic_params)
generic_params.push_back (e->clone_generic_param ());
}
// Assignment operator overload with cloning
Impl &operator= (Impl const &other)
{
VisItem::operator= (other);
where_clause = other.where_clause;
inner_attrs = other.inner_attrs;
locus = other.locus;
// guard to prevent null dereference (only required if error state)
if (other.trait_type != nullptr)
trait_type = other.trait_type->clone_type ();
else
trait_type = nullptr;
generic_params.reserve (other.generic_params.size ());
for (const auto &e : other.generic_params)
generic_params.push_back (e->clone_generic_param ());
return *this;
}
// move constructors
Impl (Impl &&other) = default;
Impl &operator= (Impl &&other) = default;
};
// Regular "impl foo" impl block declaration AST node
class InherentImpl : public Impl
{
// bool has_impl_items;
std::vector<std::unique_ptr<AssociatedItem>> impl_items;
public:
std::string as_string () const override;
// Returns whether inherent impl block has inherent impl items.
bool has_impl_items () const { return !impl_items.empty (); }
// Mega-constructor
InherentImpl (std::vector<std::unique_ptr<AssociatedItem>> impl_items,
std::vector<std::unique_ptr<GenericParam>> generic_params,
std::unique_ptr<Type> trait_type, WhereClause where_clause,
Visibility vis, std::vector<Attribute> inner_attrs,
std::vector<Attribute> outer_attrs, location_t locus)
: Impl (std::move (generic_params), std::move (trait_type),
std::move (where_clause), std::move (vis), std::move (inner_attrs),
std::move (outer_attrs), locus),
impl_items (std::move (impl_items))
{}
// Copy constructor with vector clone
InherentImpl (InherentImpl const &other) : Impl (other)
{
impl_items.reserve (other.impl_items.size ());
for (const auto &e : other.impl_items)
impl_items.push_back (e->clone_associated_item ());
}
// Overloaded assignment operator with vector clone
InherentImpl &operator= (InherentImpl const &other)
{
Impl::operator= (other);
impl_items.reserve (other.impl_items.size ());
for (const auto &e : other.impl_items)
impl_items.push_back (e->clone_associated_item ());
return *this;
}
// default move constructors
InherentImpl (InherentImpl &&other) = default;
InherentImpl &operator= (InherentImpl &&other) = default;
void accept_vis (ASTVisitor &vis) override;
// TODO: think of better way to do this
const std::vector<std::unique_ptr<AssociatedItem>> &get_impl_items () const
{
return impl_items;
}
std::vector<std::unique_ptr<AssociatedItem>> &get_impl_items ()
{
return impl_items;
}
protected:
/* Use covariance to implement clone function as returning this object
* rather than base */
InherentImpl *clone_item_impl () const override
{
return new InherentImpl (*this);
}
};
// The "impl footrait for foo" impl block declaration AST node
class TraitImpl : public Impl
{
bool has_unsafe;
bool has_exclam;
TypePath trait_path;
// bool has_impl_items;
std::vector<std::unique_ptr<TraitImplItem>> impl_items;
public:
std::string as_string () const override;
// Returns whether trait impl has impl items.
bool has_impl_items () const { return !impl_items.empty (); }
// Mega-constructor
TraitImpl (TypePath trait_path, bool is_unsafe, bool has_exclam,
std::vector<std::unique_ptr<TraitImplItem>> impl_items,
std::vector<std::unique_ptr<GenericParam>> generic_params,
std::unique_ptr<Type> trait_type, WhereClause where_clause,
Visibility vis, std::vector<Attribute> inner_attrs,
std::vector<Attribute> outer_attrs, location_t locus)
: Impl (std::move (generic_params), std::move (trait_type),
std::move (where_clause), std::move (vis), std::move (inner_attrs),
std::move (outer_attrs), locus),
has_unsafe (is_unsafe), has_exclam (has_exclam),
trait_path (std::move (trait_path)), impl_items (std::move (impl_items))
{}
// Copy constructor with vector clone
TraitImpl (TraitImpl const &other)
: Impl (other), has_unsafe (other.has_unsafe),
has_exclam (other.has_exclam), trait_path (other.trait_path)
{
impl_items.reserve (other.impl_items.size ());
for (const auto &e : other.impl_items)
impl_items.push_back (e->clone_trait_impl_item ());
}
// Overloaded assignment operator with vector clone
TraitImpl &operator= (TraitImpl const &other)
{
Impl::operator= (other);
trait_path = other.trait_path;
has_unsafe = other.has_unsafe;
has_exclam = other.has_exclam;
impl_items.reserve (other.impl_items.size ());
for (const auto &e : other.impl_items)
impl_items.push_back (e->clone_trait_impl_item ());
return *this;
}
// move constructors
TraitImpl (TraitImpl &&other) = default;
TraitImpl &operator= (TraitImpl &&other) = default;
void accept_vis (ASTVisitor &vis) override;
bool is_unsafe () const { return has_unsafe; };
bool is_exclam () const { return has_exclam; }
// TODO: think of better way to do this
const std::vector<std::unique_ptr<TraitImplItem>> &get_impl_items () const
{
return impl_items;
}
std::vector<std::unique_ptr<TraitImplItem>> &get_impl_items ()
{
return impl_items;
}
// TODO: is this better? Or is a "vis_block" better?
TypePath &get_trait_path ()
{
// TODO: assert that trait path is not empty?
return trait_path;
}
protected:
/* Use covariance to implement clone function as returning this object
* rather than base */
TraitImpl *clone_item_impl () const override { return new TraitImpl (*this); }
};
#if 0
// Abstract base class for an item used inside an extern block
class ExternalItem
{
// bool has_outer_attrs;
std::vector<Attribute> outer_attrs;
// bool has_visibility;
Visibility visibility;
Identifier item_name;
location_t locus;
public:
virtual ~ExternalItem () {}
/* TODO: spec syntax rules state that "MacroInvocationSemi" can be used as
* ExternalItem, but text body isn't so clear. Adding MacroInvocationSemi
* support would require a lot of refactoring. */
// Returns whether item has outer attributes.
bool has_outer_attrs () const { return !outer_attrs.empty (); }
// Returns whether item has non-default visibility.
bool has_visibility () const { return !visibility.is_error (); }
// Unique pointer custom clone function
std::unique_ptr<ExternalItem> clone_external_item () const
{
return std::unique_ptr<ExternalItem> (clone_external_item_impl ());
}
virtual std::string as_string () const;
location_t get_locus () const override final { return locus; }
virtual void accept_vis (ASTVisitor &vis) = 0;
// TODO: make virtual? Would be more flexible.
// Based on idea that name should never be empty.
void mark_for_strip () { item_name = ""; };
bool is_marked_for_strip () const { return item_name.empty (); };
protected:
ExternalItem (Identifier item_name, Visibility vis,
std::vector<Attribute> outer_attrs, location_t locus)
: outer_attrs (std::move (outer_attrs)), visibility (std::move (vis)),
item_name (std::move (item_name)), locus (locus)
{}
// Copy constructor
ExternalItem (ExternalItem const &other)
: outer_attrs (other.outer_attrs), visibility (other.visibility),
item_name (other.item_name), locus (other.locus)
{}
// Overloaded assignment operator to clone
ExternalItem &operator= (ExternalItem const &other)
{
item_name = other.item_name;
visibility = other.visibility;
outer_attrs = other.outer_attrs;
locus = other.locus;
return *this;
}
// move constructors
ExternalItem (ExternalItem &&other) = default;
ExternalItem &operator= (ExternalItem &&other) = default;
// Clone function implementation as pure virtual method
virtual ExternalItem *clone_external_item_impl () const = 0;
// possibly make this public if required
std::string get_item_name () const { return item_name; }
};
#endif
// A foreign type defined outside the current crate.
// https://rust-lang.github.io/rfcs/1861-extern-types.html
class ExternalTypeItem : public ExternalItem
{
std::vector<Attribute> outer_attrs;
Visibility visibility;
Identifier item_name;
location_t locus;
bool marked_for_strip;
public:
ExternalTypeItem (Identifier item_name, Visibility vis,
std::vector<Attribute> outer_attrs, location_t locus)
: ExternalItem (), outer_attrs (std::move (outer_attrs)), visibility (vis),
item_name (std::move (item_name)), locus (locus), marked_for_strip (false)
{}
ExternalTypeItem (ExternalTypeItem const &other)
: outer_attrs (other.outer_attrs), visibility (other.visibility),
item_name (other.item_name), locus (other.locus),
marked_for_strip (other.marked_for_strip)
{
node_id = other.node_id;
}
ExternalTypeItem &operator= (ExternalTypeItem const &other)
{
node_id = other.node_id;
outer_attrs = other.outer_attrs;
visibility = other.visibility;
item_name = other.item_name;
locus = other.locus;
marked_for_strip = other.marked_for_strip;
return *this;
}
// move constructors
ExternalTypeItem (ExternalTypeItem &&other) = default;
ExternalTypeItem &operator= (ExternalTypeItem &&other) = default;
std::string as_string () const override;
void accept_vis (ASTVisitor &vis) override;
// Returns whether item has outer attributes.
bool has_outer_attrs () const { return !outer_attrs.empty (); }
// Returns whether item has non-default visibility.
bool has_visibility () const { return !visibility.is_error (); }
location_t get_locus () const { return locus; }
void mark_for_strip () override { marked_for_strip = true; };
bool is_marked_for_strip () const override { return marked_for_strip; };
// TODO: this mutable getter seems really dodgy. Think up better way.
std::vector<Attribute> &get_outer_attrs () { return outer_attrs; }
const std::vector<Attribute> &get_outer_attrs () const { return outer_attrs; }
Identifier get_identifier () const { return item_name; }
Visibility &get_visibility () { return visibility; }
const Visibility &get_visibility () const { return visibility; }
protected:
/* Use covariance to implement clone function as returning this object
* rather than base */
ExternalTypeItem *clone_external_item_impl () const override
{
return new ExternalTypeItem (*this);
}
};
// A static item used in an extern block
class ExternalStaticItem : public ExternalItem
{
// bool has_outer_attrs;
std::vector<Attribute> outer_attrs;
// bool has_visibility;
Visibility visibility;
Identifier item_name;
location_t locus;
bool has_mut;
std::unique_ptr<Type> item_type;
public:
ExternalStaticItem (Identifier item_name, std::unique_ptr<Type> item_type,
bool is_mut, Visibility vis,
std::vector<Attribute> outer_attrs, location_t locus)
: ExternalItem (), outer_attrs (std::move (outer_attrs)),
visibility (std::move (vis)), item_name (std::move (item_name)),
locus (locus), has_mut (is_mut), item_type (std::move (item_type))
{}
// Copy constructor
ExternalStaticItem (ExternalStaticItem const &other)
: outer_attrs (other.outer_attrs), visibility (other.visibility),
item_name (other.item_name), locus (other.locus), has_mut (other.has_mut)
{
node_id = other.node_id;
// guard to prevent null dereference (only required if error state)
if (other.item_type != nullptr)
item_type = other.item_type->clone_type ();
}
// Overloaded assignment operator to clone
ExternalStaticItem &operator= (ExternalStaticItem const &other)
{
node_id = other.node_id;
outer_attrs = other.outer_attrs;
visibility = other.visibility;
item_name = other.item_name;
locus = other.locus;
has_mut = other.has_mut;
// guard to prevent null dereference (only required if error state)
if (other.item_type != nullptr)
item_type = other.item_type->clone_type ();
else
item_type = nullptr;
return *this;
}
// move constructors
ExternalStaticItem (ExternalStaticItem &&other) = default;
ExternalStaticItem &operator= (ExternalStaticItem &&other) = default;
std::string as_string () const override;
void accept_vis (ASTVisitor &vis) override;
// Returns whether item has outer attributes.
bool has_outer_attrs () const { return !outer_attrs.empty (); }
// Returns whether item has non-default visibility.
bool has_visibility () const { return !visibility.is_error (); }
location_t get_locus () const { return locus; }
// Based on idea that type should never be null.
void mark_for_strip () override { item_type = nullptr; };
bool is_marked_for_strip () const override { return item_type == nullptr; };
// TODO: this mutable getter seems really dodgy. Think up better way.
std::vector<Attribute> &get_outer_attrs () { return outer_attrs; }
const std::vector<Attribute> &get_outer_attrs () const { return outer_attrs; }
// TODO: is this better? Or is a "vis_block" better?
std::unique_ptr<Type> &get_type ()
{
rust_assert (item_type != nullptr);
return item_type;
}
Identifier get_identifier () const { return item_name; }
Visibility &get_visibility () { return visibility; }
const Visibility &get_visibility () const { return visibility; }
bool is_mut () const { return has_mut; }
protected:
/* Use covariance to implement clone function as returning this object
* rather than base */
ExternalStaticItem *clone_external_item_impl () const override
{
return new ExternalStaticItem (*this);
}
};
// A named function parameter used in external functions
class NamedFunctionParam
{
// bool has_name; // otherwise is _
std::string name;
std::unique_ptr<Type> param_type;
// seemingly new since writing this node
std::vector<Attribute> outer_attrs;
NodeId node_id;
location_t locus;
bool variadic;
public:
/* Returns whether the named function parameter has a name (i.e. name is not
* '_'). */
bool has_name () const { return name != "_" && name != ""; }
bool has_outer_attrs () const { return !outer_attrs.empty (); }
// Returns whether the named function parameter is in an error state.
bool is_error () const
{
// also if identifier is "" but that is probably more costly to compute
return param_type == nullptr && !variadic;
}
bool is_variadic () const { return variadic; }
std::string get_name () const { return name; }
location_t get_locus () { return locus; }
// Creates an error state named function parameter.
static NamedFunctionParam create_error ()
{
return NamedFunctionParam ("", nullptr, {}, UNDEF_LOCATION);
}
NamedFunctionParam (std::string name, std::unique_ptr<Type> param_type,
std::vector<Attribute> outer_attrs, location_t locus)
: name (std::move (name)), param_type (std::move (param_type)),
outer_attrs (std::move (outer_attrs)),
node_id (Analysis::Mappings::get ()->get_next_node_id ()), locus (locus),
variadic (false)
{}
NamedFunctionParam (std::string name, std::vector<Attribute> outer_attrs,
location_t locus)
: name (std::move (name)), param_type (nullptr),
outer_attrs (std::move (outer_attrs)),
node_id (Analysis::Mappings::get ()->get_next_node_id ()), locus (locus),
variadic (true)
{}
NamedFunctionParam (std::vector<Attribute> outer_attrs, location_t locus)
: name (""), param_type (nullptr), outer_attrs (std::move (outer_attrs)),
node_id (Analysis::Mappings::get ()->get_next_node_id ()), locus (locus),
variadic (true)
{}
// Copy constructor
NamedFunctionParam (NamedFunctionParam const &other)
: name (other.name), outer_attrs (other.outer_attrs),
variadic (other.variadic)
{
node_id = other.node_id;
// guard to prevent null dereference (only required if error state)
if (other.param_type != nullptr)
param_type = other.param_type->clone_type ();
else
param_type = nullptr;
}
~NamedFunctionParam () = default;
// Overloaded assignment operator to clone
NamedFunctionParam &operator= (NamedFunctionParam const &other)
{
node_id = other.node_id;
name = other.name;
// has_name = other.has_name;
outer_attrs = other.outer_attrs;
// guard to prevent null dereference (only required if error state)
if (other.param_type != nullptr)
param_type = other.param_type->clone_type ();
else
param_type = nullptr;
return *this;
}
// move constructors
NamedFunctionParam (NamedFunctionParam &&other) = default;
NamedFunctionParam &operator= (NamedFunctionParam &&other) = default;
std::string as_string () const;
// Based on idea that nane should never be empty.
void mark_for_strip () { param_type = nullptr; };
bool is_marked_for_strip () const { return is_error (); };
// TODO: this mutable getter seems really dodgy. Think up better way.
std::vector<Attribute> &get_outer_attrs () { return outer_attrs; }
const std::vector<Attribute> &get_outer_attrs () const { return outer_attrs; }
// TODO: is this better? Or is a "vis_block" better?
std::unique_ptr<Type> &get_type ()
{
rust_assert (param_type != nullptr);
return param_type;
}
NodeId get_node_id () const { return node_id; }
};
// A function item used in an extern block
class ExternalFunctionItem : public ExternalItem
{
// bool has_outer_attrs;
std::vector<Attribute> outer_attrs;
// bool has_visibility;
Visibility visibility;
Identifier item_name;
location_t locus;
// bool has_generics;
// Generics generic_params;
std::vector<std::unique_ptr<GenericParam>> generic_params; // inlined
// bool has_return_type;
// FunctionReturnType return_type;
std::unique_ptr<Type> return_type; // inlined
// bool has_where_clause;
WhereClause where_clause;
std::vector<NamedFunctionParam> function_params;
public:
// Returns whether item has generic parameters.
bool has_generics () const { return !generic_params.empty (); }
// Returns whether item has a return type (otherwise void).
bool has_return_type () const { return return_type != nullptr; }
// Returns whether item has a where clause.
bool has_where_clause () const { return !where_clause.is_empty (); }
// Returns whether item has outer attributes.
bool has_outer_attrs () const { return !outer_attrs.empty (); }
// Returns whether item has non-default visibility.
bool has_visibility () const { return !visibility.is_error (); }
// Returns whether item has variadic parameters.
bool is_variadic () const
{
return function_params.size () != 0
&& function_params.back ().is_variadic ();
}
location_t get_locus () const { return locus; }
Visibility &get_visibility () { return visibility; }
const Visibility &get_visibility () const { return visibility; }
ExternalFunctionItem (
Identifier item_name,
std::vector<std::unique_ptr<GenericParam>> generic_params,
std::unique_ptr<Type> return_type, WhereClause where_clause,
std::vector<NamedFunctionParam> function_params, Visibility vis,
std::vector<Attribute> outer_attrs, location_t locus)
: ExternalItem (), outer_attrs (std::move (outer_attrs)),
visibility (std::move (vis)), item_name (std::move (item_name)),
locus (locus), generic_params (std::move (generic_params)),
return_type (std::move (return_type)),
where_clause (std::move (where_clause)),
function_params (std::move (function_params))
{
// TODO: assert that if has variadic outer attrs, then has_variadics is
// true?
}
// Copy constructor with clone
ExternalFunctionItem (ExternalFunctionItem const &other)
: outer_attrs (other.outer_attrs), visibility (other.visibility),
item_name (other.item_name), locus (other.locus),
where_clause (other.where_clause), function_params (other.function_params)
{
node_id = other.node_id;
// guard to prevent null pointer dereference
if (other.return_type != nullptr)
return_type = other.return_type->clone_type ();
generic_params.reserve (other.generic_params.size ());
for (const auto &e : other.generic_params)
generic_params.push_back (e->clone_generic_param ());
}
// Overloaded assignment operator with clone
ExternalFunctionItem &operator= (ExternalFunctionItem const &other)
{
outer_attrs = other.outer_attrs;
visibility = other.visibility;
item_name = other.item_name;
locus = other.locus;
where_clause = other.where_clause;
function_params = other.function_params;
node_id = other.node_id;
// guard to prevent null pointer dereference
if (other.return_type != nullptr)
return_type = other.return_type->clone_type ();
else
return_type = nullptr;
generic_params.reserve (other.generic_params.size ());
for (const auto &e : other.generic_params)
generic_params.push_back (e->clone_generic_param ());
return *this;
}
// move constructors
ExternalFunctionItem (ExternalFunctionItem &&other) = default;
ExternalFunctionItem &operator= (ExternalFunctionItem &&other) = default;
std::string as_string () const override;
void accept_vis (ASTVisitor &vis) override;
// Based on idea that nane should never be empty.
void mark_for_strip () override { item_name = {""}; };
bool is_marked_for_strip () const override { return item_name.empty (); };
// TODO: this mutable getter seems really dodgy. Think up better way.
std::vector<Attribute> &get_outer_attrs () { return outer_attrs; }
const std::vector<Attribute> &get_outer_attrs () const { return outer_attrs; }
std::vector<NamedFunctionParam> &get_function_params ()
{
return function_params;
}
const std::vector<NamedFunctionParam> &get_function_params () const
{
return function_params;
}
std::vector<std::unique_ptr<GenericParam>> &get_generic_params ()
{
return generic_params;
}
const std::vector<std::unique_ptr<GenericParam>> &get_generic_params () const
{
return generic_params;
}
// TODO: is this better? Or is a "vis_block" better?
WhereClause &get_where_clause () { return where_clause; }
// TODO: is this better? Or is a "vis_block" better?
std::unique_ptr<Type> &get_return_type ()
{
rust_assert (has_return_type ());
return return_type;
}
Identifier get_identifier () const { return item_name; };
protected:
/* Use covariance to implement clone function as returning this object
* rather than base */
ExternalFunctionItem *clone_external_item_impl () const override
{
return new ExternalFunctionItem (*this);
}
};
// An extern block AST node
class ExternBlock : public VisItem
{
// bool has_abi;
std::string abi;
// bool has_inner_attrs;
std::vector<Attribute> inner_attrs;
// bool has_extern_items;
std::vector<std::unique_ptr<ExternalItem>> extern_items;
location_t locus;
// TODO: find another way to store this to save memory?
bool marked_for_strip = false;
public:
std::string as_string () const override;
// Returns whether extern block has inner attributes.
bool has_inner_attrs () const { return !inner_attrs.empty (); }
// Returns whether extern block has extern items.
bool has_extern_items () const { return !extern_items.empty (); }
// Returns whether extern block has ABI name.
bool has_abi () const { return !abi.empty (); }
std::string get_abi () const { return abi; }
ExternBlock (std::string abi,
std::vector<std::unique_ptr<ExternalItem>> extern_items,
Visibility vis, std::vector<Attribute> inner_attrs,
std::vector<Attribute> outer_attrs, location_t locus)
: VisItem (std::move (vis), std::move (outer_attrs)), abi (std::move (abi)),
inner_attrs (std::move (inner_attrs)),
extern_items (std::move (extern_items)), locus (locus)
{}
// Copy constructor with vector clone
ExternBlock (ExternBlock const &other)
: VisItem (other), abi (other.abi), inner_attrs (other.inner_attrs),
locus (other.locus), marked_for_strip (other.marked_for_strip)
{
extern_items.reserve (other.extern_items.size ());
for (const auto &e : other.extern_items)
extern_items.push_back (e->clone_external_item ());
}
// Overloaded assignment operator with vector clone
ExternBlock &operator= (ExternBlock const &other)
{
VisItem::operator= (other);
abi = other.abi;
inner_attrs = other.inner_attrs;
locus = other.locus;
marked_for_strip = other.marked_for_strip;
extern_items.reserve (other.extern_items.size ());
for (const auto &e : other.extern_items)
extern_items.push_back (e->clone_external_item ());
return *this;
}
// move constructors
ExternBlock (ExternBlock &&other) = default;
ExternBlock &operator= (ExternBlock &&other) = default;
location_t get_locus () const override final { return locus; }
void accept_vis (ASTVisitor &vis) override;
// Can't think of any invalid invariants, so store boolean.
void mark_for_strip () override { marked_for_strip = true; }
bool is_marked_for_strip () const override { return marked_for_strip; }
// TODO: think of better way to do this
const std::vector<std::unique_ptr<ExternalItem>> &get_extern_items () const
{
return extern_items;
}
std::vector<std::unique_ptr<ExternalItem>> &get_extern_items ()
{
return extern_items;
}
// TODO: think of better way to do this
const std::vector<Attribute> &get_inner_attrs () const { return inner_attrs; }
std::vector<Attribute> &get_inner_attrs () { return inner_attrs; }
protected:
/* Use covariance to implement clone function as returning this object
* rather than base */
ExternBlock *clone_item_impl () const override
{
return new ExternBlock (*this);
}
};
class MacroRulesDefinition;
} // namespace AST
} // namespace Rust
#endif
|