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
|
// types.h -- Go frontend types. -*- C++ -*-
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
#ifndef GO_TYPES_H
#define GO_TYPES_H
#include <ostream>
#include "go-linemap.h"
#include "escape.h"
class Gogo;
class Package;
class Variable;
class Traverse;
class Typed_identifier;
class Typed_identifier_list;
class Integer_type;
class Float_type;
class Complex_type;
class String_type;
class Function_type;
class Backend_function_type;
class Struct_field;
class Struct_field_list;
class Struct_type;
class Pointer_type;
class Array_type;
class Map_type;
class Channel_type;
class Interface_type;
class Named_type;
class Forward_declaration_type;
class Method;
class Methods;
class Type_hash_identical;
class Type_identical;
class Expression;
class Expression_list;
class Call_expression;
class Field_reference_expression;
class Bound_method_expression;
class Bindings;
class Named_object;
class Function;
class Translate_context;
class Export;
class Import;
class Backend_name;
class Btype;
class Bexpression;
class Bvariable;
// Type codes used in type descriptors. These must match the values
// in libgo/runtime/go-type.h. They also match the values in the gc
// compiler in src/cmd/gc/reflect.c and src/pkg/runtime/type.go,
// although this is not required.
static const int RUNTIME_TYPE_KIND_BOOL = 1;
static const int RUNTIME_TYPE_KIND_INT = 2;
static const int RUNTIME_TYPE_KIND_INT8 = 3;
static const int RUNTIME_TYPE_KIND_INT16 = 4;
static const int RUNTIME_TYPE_KIND_INT32 = 5;
static const int RUNTIME_TYPE_KIND_INT64 = 6;
static const int RUNTIME_TYPE_KIND_UINT = 7;
static const int RUNTIME_TYPE_KIND_UINT8 = 8;
static const int RUNTIME_TYPE_KIND_UINT16 = 9;
static const int RUNTIME_TYPE_KIND_UINT32 = 10;
static const int RUNTIME_TYPE_KIND_UINT64 = 11;
static const int RUNTIME_TYPE_KIND_UINTPTR = 12;
static const int RUNTIME_TYPE_KIND_FLOAT32 = 13;
static const int RUNTIME_TYPE_KIND_FLOAT64 = 14;
static const int RUNTIME_TYPE_KIND_COMPLEX64 = 15;
static const int RUNTIME_TYPE_KIND_COMPLEX128 = 16;
static const int RUNTIME_TYPE_KIND_ARRAY = 17;
static const int RUNTIME_TYPE_KIND_CHAN = 18;
static const int RUNTIME_TYPE_KIND_FUNC = 19;
static const int RUNTIME_TYPE_KIND_INTERFACE = 20;
static const int RUNTIME_TYPE_KIND_MAP = 21;
static const int RUNTIME_TYPE_KIND_PTR = 22;
static const int RUNTIME_TYPE_KIND_SLICE = 23;
static const int RUNTIME_TYPE_KIND_STRING = 24;
static const int RUNTIME_TYPE_KIND_STRUCT = 25;
static const int RUNTIME_TYPE_KIND_UNSAFE_POINTER = 26;
static const int RUNTIME_TYPE_KIND_DIRECT_IFACE = (1 << 5);
static const int RUNTIME_TYPE_KIND_GC_PROG = (1 << 6);
static const int RUNTIME_TYPE_KIND_NO_POINTERS = (1 << 7);
// To build the complete list of methods for a named type we need to
// gather all methods from anonymous fields. Those methods may
// require an arbitrary set of indirections and field offsets. There
// is also the possibility of ambiguous methods, which we could ignore
// except that we want to give a better error message for that case.
// This is a base class. There are two types of methods: named
// methods, and methods which are inherited from an anonymous field of
// interface type.
class Method
{
public:
// For methods in anonymous types we need to know the sequence of
// field references used to extract the pointer to pass to the
// method. Since each method for a particular anonymous field will
// have the sequence of field indexes, and since the indexes can be
// shared going down the chain, we use a manually managed linked
// list. The first entry in the list is the field index for the
// last field, the one passed to the method.
struct Field_indexes
{
const Field_indexes* next;
unsigned int field_index;
};
virtual ~Method()
{ }
// Get the list of field indexes.
const Field_indexes*
field_indexes() const
{ return this->field_indexes_; }
// Get the depth.
unsigned int
depth() const
{ return this->depth_; }
// Return whether this is a value method--a method which does not
// require a pointer expression.
bool
is_value_method() const
{ return this->is_value_method_; }
// Return whether we need a stub method--this is true if we can't
// just pass the main object to the method.
bool
needs_stub_method() const
{ return this->needs_stub_method_; }
// Return whether this is an ambiguous method name.
bool
is_ambiguous() const
{ return this->is_ambiguous_; }
// Note that this method is ambiguous.
void
set_is_ambiguous()
{ this->is_ambiguous_ = true; }
// Return the type of the method.
Function_type*
type() const
{ return this->do_type(); }
// Return the location of the method receiver.
Location
receiver_location() const
{ return this->do_receiver_location(); }
// Return an expression which binds this method to EXPR. This is
// something which can be used with a function call.
Expression*
bind_method(Expression* expr, Location location) const;
// Return the named object for this method. This may only be called
// after methods are finalized.
Named_object*
named_object() const;
// Get the stub object.
Named_object*
stub_object() const
{
go_assert(this->stub_ != NULL);
return this->stub_;
}
// Set the stub object.
void
set_stub_object(Named_object* no)
{
go_assert(this->stub_ == NULL);
this->stub_ = no;
}
// Get the direct interface method stub object.
Named_object*
iface_stub_object() const
{
go_assert(this->iface_stub_ != NULL);
return this->iface_stub_;
}
// Set the direct interface method stub object.
void
set_iface_stub_object(Named_object* no)
{
go_assert(this->iface_stub_ == NULL);
this->iface_stub_ = no;
}
// Return true if this method should not participate in any
// interfaces.
bool
nointerface() const
{ return this->do_nointerface(); }
protected:
// These objects are only built by the child classes.
Method(const Field_indexes* field_indexes, unsigned int depth,
bool is_value_method, bool needs_stub_method)
: field_indexes_(field_indexes), depth_(depth), stub_(NULL), iface_stub_(NULL),
is_value_method_(is_value_method), needs_stub_method_(needs_stub_method),
is_ambiguous_(false)
{ }
// The named object for this method.
virtual Named_object*
do_named_object() const = 0;
// The type of the method.
virtual Function_type*
do_type() const = 0;
// Return the location of the method receiver.
virtual Location
do_receiver_location() const = 0;
// Bind a method to an object.
virtual Expression*
do_bind_method(Expression* expr, Location location) const = 0;
// Return whether this method should not participate in interfaces.
virtual bool
do_nointerface() const = 0;
private:
// The sequence of field indexes used for this method. If this is
// NULL, then the method is defined for the current type.
const Field_indexes* field_indexes_;
// The depth at which this method was found.
unsigned int depth_;
// If a stub method is required, this is its object. This is only
// set after stub methods are built in finalize_methods.
Named_object* stub_;
// Stub object for direct interface type. This is only set after
// stub methods are built in finalize_methods.
Named_object* iface_stub_;
// Whether this is a value method--a method that does not require a
// pointer.
bool is_value_method_;
// Whether a stub method is required.
bool needs_stub_method_;
// Whether this method is ambiguous.
bool is_ambiguous_;
};
// A named method. This is what you get with a method declaration,
// either directly on the type, or inherited from some anonymous
// embedded field.
class Named_method : public Method
{
public:
Named_method(Named_object* named_object, const Field_indexes* field_indexes,
unsigned int depth, bool is_value_method,
bool needs_stub_method)
: Method(field_indexes, depth, is_value_method, needs_stub_method),
named_object_(named_object)
{ }
protected:
// Get the Named_object for the method.
Named_object*
do_named_object() const
{ return this->named_object_; }
// The type of the method.
Function_type*
do_type() const;
// Return the location of the method receiver.
Location
do_receiver_location() const;
// Bind a method to an object.
Expression*
do_bind_method(Expression* expr, Location location) const;
// Return whether this method should not participate in interfaces.
bool
do_nointerface() const;
private:
// The method itself. For a method which needs a stub, this starts
// out as the underlying method, and is later replaced with the stub
// method.
Named_object* named_object_;
};
// An interface method. This is used when an interface appears as an
// anonymous field in a named struct.
class Interface_method : public Method
{
public:
Interface_method(const std::string& name, Location location,
Function_type* fntype, const Field_indexes* field_indexes,
unsigned int depth)
: Method(field_indexes, depth, true, true),
name_(name), location_(location), fntype_(fntype)
{ }
protected:
// Get the Named_object for the method. This should never be
// called, as we always create a stub.
Named_object*
do_named_object() const
{ go_unreachable(); }
// The type of the method.
Function_type*
do_type() const
{ return this->fntype_; }
// Return the location of the method receiver.
Location
do_receiver_location() const
{ return this->location_; }
// Bind a method to an object.
Expression*
do_bind_method(Expression* expr, Location location) const;
// Return whether this method should not participate in interfaces.
bool
do_nointerface() const
{ return false; }
private:
// The name of the interface method to call.
std::string name_;
// The location of the definition of the interface method.
Location location_;
// The type of the interface method.
Function_type* fntype_;
};
// A mapping from method name to Method. This is a wrapper around a
// hash table.
class Methods
{
private:
typedef Unordered_map(std::string, Method*) Method_map;
public:
typedef Method_map::const_iterator const_iterator;
Methods()
: methods_()
{ }
// Insert a new method. Returns true if it was inserted, false if
// it was overidden or ambiguous.
bool
insert(const std::string& name, Method* m);
// The number of (unambiguous) methods.
size_t
count() const;
// Iterate.
const_iterator
begin() const
{ return this->methods_.begin(); }
const_iterator
end() const
{ return this->methods_.end(); }
// Lookup.
const_iterator
find(const std::string& name) const
{ return this->methods_.find(name); }
bool
empty() const
{ return this->methods_.empty(); }
private:
Method_map methods_;
};
// The base class for all types.
class Type
{
public:
// The types of types.
enum Type_classification
{
TYPE_ERROR,
TYPE_VOID,
TYPE_BOOLEAN,
TYPE_INTEGER,
TYPE_FLOAT,
TYPE_COMPLEX,
TYPE_STRING,
TYPE_SINK,
TYPE_FUNCTION,
TYPE_POINTER,
TYPE_NIL,
TYPE_CALL_MULTIPLE_RESULT,
TYPE_STRUCT,
TYPE_ARRAY,
TYPE_MAP,
TYPE_CHANNEL,
TYPE_INTERFACE,
TYPE_NAMED,
TYPE_FORWARD
};
virtual ~Type();
// Creators.
static Type*
make_error_type();
static Type*
make_void_type();
// Get the unnamed bool type.
static Type*
make_boolean_type();
// Get the named type "bool".
static Named_type*
lookup_bool_type();
// Make the named type "bool".
static Named_type*
make_named_bool_type();
// Make an abstract integer type.
static Integer_type*
make_abstract_integer_type();
// Make an abstract type for a character constant.
static Integer_type*
make_abstract_character_type();
// Make a named integer type with a specified size.
// RUNTIME_TYPE_KIND is the code to use in reflection information,
// to distinguish int and int32.
static Named_type*
make_integer_type(const char* name, bool is_unsigned, int bits,
int runtime_type_kind);
// Make a named integer type alias. This is used for byte and rune.
static Named_type*
make_integer_type_alias(const char* name, Named_type* real_type);
// Look up a named integer type.
static Named_type*
lookup_integer_type(const char* name);
// Make an abstract floating point type.
static Float_type*
make_abstract_float_type();
// Make a named floating point type with a specific size.
// RUNTIME_TYPE_KIND is the code to use in reflection information,
// to distinguish float and float32.
static Named_type*
make_float_type(const char* name, int bits, int runtime_type_kind);
// Look up a named float type.
static Named_type*
lookup_float_type(const char* name);
// Make an abstract complex type.
static Complex_type*
make_abstract_complex_type();
// Make a named complex type with a specific size.
// RUNTIME_TYPE_KIND is the code to use in reflection information,
// to distinguish complex and complex64.
static Named_type*
make_complex_type(const char* name, int bits, int runtime_type_kind);
// Look up a named complex type.
static Named_type*
lookup_complex_type(const char* name);
// Get the unnamed string type.
static Type*
make_string_type();
// Get the named type "string".
static Named_type*
lookup_string_type();
// Make the named type "string".
static Named_type*
make_named_string_type();
static Type*
make_sink_type();
static Function_type*
make_function_type(Typed_identifier* receiver,
Typed_identifier_list* parameters,
Typed_identifier_list* results,
Location);
static Backend_function_type*
make_backend_function_type(Typed_identifier* receiver,
Typed_identifier_list* parameters,
Typed_identifier_list* results,
Location);
static Pointer_type*
make_pointer_type(Type*);
static void
finish_pointer_types(Gogo* gogo);
static Type*
make_nil_type();
static Type*
make_call_multiple_result_type(Call_expression*);
static Struct_type*
make_struct_type(Struct_field_list* fields, Location);
static Array_type*
make_array_type(Type* element_type, Expression* length);
static Map_type*
make_map_type(Type* key_type, Type* value_type, Location);
static Channel_type*
make_channel_type(bool send, bool receive, Type*);
static Interface_type*
make_interface_type(Typed_identifier_list* methods, Location);
static Interface_type*
make_empty_interface_type(Location);
static Type*
make_type_descriptor_type();
static Type*
make_type_descriptor_ptr_type();
static Named_type*
make_named_type(Named_object*, Type*, Location);
static Type*
make_forward_declaration(Named_object*);
// Make a builtin struct type from a list of fields.
static Struct_type*
make_builtin_struct_type(int nfields, ...);
// Make a builtin named type.
static Named_type*
make_builtin_named_type(const char* name, Type* type);
// Traverse a type.
static int
traverse(Type*, Traverse*);
// Verify the type. This is called after parsing, and verifies that
// types are complete and meet the language requirements. This
// returns false if the type is invalid and we should not continue
// traversing it.
bool
verify()
{ return this->do_verify(); }
// Bit flags to pass to are_identical and friends.
// Treat error types as their own distinct type. Sometimes we
// ignore error types--treat them as identical to every other
// type--to avoid cascading errors.
static const int COMPARE_ERRORS = 1;
// Compare struct field tags when comparing structs. We ignore
// struct field tags for purposes of type conversion.
static const int COMPARE_TAGS = 2;
// Compare aliases: treat an alias to T as distinct from T.
static const int COMPARE_ALIASES = 4;
// When comparing interface types compare the interface embedding heirarchy,
// if any, rather than only comparing method sets. Useful primarily when
// exporting types.
static const int COMPARE_EMBEDDED_INTERFACES = 8;
// Return true if two types are identical. If this returns false,
// and REASON is not NULL, it may set *REASON.
static bool
are_identical(const Type* lhs, const Type* rhs, int flags,
std::string* reason);
// Return true if two types are compatible for use in a binary
// operation, other than a shift, comparison, or channel send. This
// is an equivalence relation.
static bool
are_compatible_for_binop(const Type* t1, const Type* t2);
// Return true if two types are compatible for use with the
// comparison operator. IS_EQUALITY_OP is true if this is an
// equality comparison, false if it is an ordered comparison. This
// is an equivalence relation. If this returns false, and REASON is
// not NULL, it sets *REASON.
static bool
are_compatible_for_comparison(bool is_equality_op, const Type *t1,
const Type *t2, std::string* reason);
// Return true if a type is comparable with itself. This is true of
// most types, but false for, e.g., function types.
bool
is_comparable() const
{ return Type::are_compatible_for_comparison(true, this, this, NULL); }
// Return true if a value with type RHS is assignable to a variable
// with type LHS. This is not an equivalence relation. If this
// returns false, and REASON is not NULL, it sets *REASON.
static bool
are_assignable(const Type* lhs, const Type* rhs, std::string* reason);
// Return true if a value with type RHS may be converted to type
// LHS. If this returns false, and REASON is not NULL, it sets
// *REASON.
static bool
are_convertible(const Type* lhs, const Type* rhs, std::string* reason);
// Return true if values of this type can be compared using an
// identity function which gets nothing but a pointer to the value
// and a size.
bool
compare_is_identity(Gogo* gogo)
{ return this->do_compare_is_identity(gogo); }
// Return whether values of this type are reflexive: if a comparison
// of a value with itself always returns true.
bool
is_reflexive()
{ return this->do_is_reflexive(); }
// Return whether values of this, when used as a key in map,
// requires the key to be updated when an assignment is made.
bool
needs_key_update()
{ return this->do_needs_key_update(); }
// Return whether the hash function of this type might panic. This
// is only called for types used as a key in a map type.
bool
hash_might_panic()
{ return this->do_hash_might_panic(); }
// Whether the type is permitted in the heap.
bool
in_heap() const
{ return this->do_in_heap(); }
// Return a hash code for this type for the method hash table.
// Types which are equivalent according to are_identical will have
// the same hash code.
unsigned int
hash_for_method(Gogo*, int) const;
// Return the type classification.
Type_classification
classification() const
{ return this->classification_; }
// Return the base type for this type. This looks through forward
// declarations and names. Using this with a forward declaration
// which has not been defined will return an error type.
Type*
base();
const Type*
base() const;
// Return the type skipping defined forward declarations. If this
// type is a forward declaration which has not been defined, it will
// return the Forward_declaration_type. This differs from base() in
// that it will return a Named_type, and for a
// Forward_declaration_type which is not defined it will return that
// type rather than an error type.
Type*
forwarded();
const Type*
forwarded() const;
// Return the type skipping any alias definitions and any defined
// forward declarations. This is like forwarded, but also
// recursively expands alias definitions to the aliased type.
Type*
unalias();
const Type*
unalias() const;
// Return true if this is a basic type: a type which is not composed
// of other types, and is not void.
bool
is_basic_type() const;
// Return true if this is an abstract type--an integer, floating
// point, or complex type whose size has not been determined.
bool
is_abstract() const;
// Return a non-abstract version of an abstract type.
Type*
make_non_abstract_type();
// Return true if this type is or contains a pointer. This
// determines whether the garbage collector needs to look at a value
// of this type.
bool
has_pointer() const
{ return this->do_has_pointer(); }
// Return true if this is the error type. This returns false for a
// type which is not defined, as it is called by the parser before
// all types are defined.
bool
is_error_type() const;
// Return true if this is the error type or if the type is
// undefined. If the type is undefined, this will give an error.
// This should only be called after parsing is complete.
bool
is_error() const
{ return this->base()->is_error_type(); }
// Return true if this is a void type.
bool
is_void_type() const
{ return this->classification_ == TYPE_VOID; }
// If this is an integer type, return the Integer_type. Otherwise,
// return NULL. This is a controlled dynamic_cast.
Integer_type*
integer_type()
{ return this->convert<Integer_type, TYPE_INTEGER>(); }
const Integer_type*
integer_type() const
{ return this->convert<const Integer_type, TYPE_INTEGER>(); }
// If this is a floating point type, return the Float_type.
// Otherwise, return NULL. This is a controlled dynamic_cast.
Float_type*
float_type()
{ return this->convert<Float_type, TYPE_FLOAT>(); }
const Float_type*
float_type() const
{ return this->convert<const Float_type, TYPE_FLOAT>(); }
// If this is a complex type, return the Complex_type. Otherwise,
// return NULL.
Complex_type*
complex_type()
{ return this->convert<Complex_type, TYPE_COMPLEX>(); }
const Complex_type*
complex_type() const
{ return this->convert<const Complex_type, TYPE_COMPLEX>(); }
// Return whether this is a numeric type.
bool
is_numeric_type() const
{
Type_classification tc = this->base()->classification_;
return tc == TYPE_INTEGER || tc == TYPE_FLOAT || tc == TYPE_COMPLEX;
}
// Return true if this is a boolean type.
bool
is_boolean_type() const
{ return this->base()->classification_ == TYPE_BOOLEAN; }
// Return true if this is an abstract boolean type.
bool
is_abstract_boolean_type() const
{ return this->classification_ == TYPE_BOOLEAN; }
// Return true if this is a string type.
bool
is_string_type() const
{ return this->base()->classification_ == TYPE_STRING; }
// Return true if this is an abstract string type.
bool
is_abstract_string_type() const
{ return this->classification_ == TYPE_STRING; }
// Return true if this is the sink type. This is the type of the
// blank identifier _.
bool
is_sink_type() const
{ return this->base()->classification_ == TYPE_SINK; }
// If this is a function type, return it. Otherwise, return NULL.
Function_type*
function_type()
{ return this->convert<Function_type, TYPE_FUNCTION>(); }
const Function_type*
function_type() const
{ return this->convert<const Function_type, TYPE_FUNCTION>(); }
// If this is a pointer type, return the type to which it points.
// Otherwise, return NULL.
Type*
points_to() const;
// If this is a pointer type, return the type to which it points.
// Otherwise, return the type itself.
Type*
deref()
{
Type* pt = this->points_to();
return pt != NULL ? pt : this;
}
const Type*
deref() const
{
const Type* pt = this->points_to();
return pt != NULL ? pt : this;
}
// Return true if this is the nil type. We don't use base() here,
// because this can be called during parse, and there is no way to
// name the nil type anyhow.
bool
is_nil_type() const
{ return this->classification_ == TYPE_NIL; }
// Return true if this is the predeclared constant nil being used as
// a type. This is what the parser produces for type switches which
// use "case nil".
bool
is_nil_constant_as_type() const;
// Return true if this is the return type of a function which
// returns multiple values.
bool
is_call_multiple_result_type() const
{ return this->base()->classification_ == TYPE_CALL_MULTIPLE_RESULT; }
// If this is a struct type, return it. Otherwise, return NULL.
Struct_type*
struct_type()
{ return this->convert<Struct_type, TYPE_STRUCT>(); }
const Struct_type*
struct_type() const
{ return this->convert<const Struct_type, TYPE_STRUCT>(); }
// If this is an array type, return it. Otherwise, return NULL.
Array_type*
array_type()
{ return this->convert<Array_type, TYPE_ARRAY>(); }
const Array_type*
array_type() const
{ return this->convert<const Array_type, TYPE_ARRAY>(); }
// Return whether if this is a slice type.
bool
is_slice_type() const;
// If this is a map type, return it. Otherwise, return NULL.
Map_type*
map_type()
{ return this->convert<Map_type, TYPE_MAP>(); }
const Map_type*
map_type() const
{ return this->convert<const Map_type, TYPE_MAP>(); }
// If this is a channel type, return it. Otherwise, return NULL.
Channel_type*
channel_type()
{ return this->convert<Channel_type, TYPE_CHANNEL>(); }
const Channel_type*
channel_type() const
{ return this->convert<const Channel_type, TYPE_CHANNEL>(); }
// If this is an interface type, return it. Otherwise, return NULL.
Interface_type*
interface_type()
{ return this->convert<Interface_type, TYPE_INTERFACE>(); }
const Interface_type*
interface_type() const
{ return this->convert<const Interface_type, TYPE_INTERFACE>(); }
// If this is a named type, return it. Otherwise, return NULL.
Named_type*
named_type();
const Named_type*
named_type() const;
// If this is a forward declaration, return it. Otherwise, return
// NULL.
Forward_declaration_type*
forward_declaration_type()
{ return this->convert_no_base<Forward_declaration_type, TYPE_FORWARD>(); }
const Forward_declaration_type*
forward_declaration_type() const
{
return this->convert_no_base<const Forward_declaration_type,
TYPE_FORWARD>();
}
// Return true if this type is not yet defined.
bool
is_undefined() const;
// Return true if this is the unsafe.pointer type. We currently
// represent that as pointer-to-void.
bool
is_unsafe_pointer_type() const
{ return this->points_to() != NULL && this->points_to()->is_void_type(); }
// Return whether this type is stored directly in an interface's
// data word.
bool
is_direct_iface_type() const;
// Return a version of this type with any expressions copied, but
// only if copying the expressions will affect the size of the type.
// If there are no such expressions in the type (expressions can
// only occur in array types), just return the same type. If any
// expressions can not affect the size of the type, just return the
// same type.
Type*
copy_expressions();
// Look for field or method NAME for TYPE. Return an expression for
// it, bound to EXPR.
static Expression*
bind_field_or_method(Gogo*, const Type* type, Expression* expr,
const std::string& name, Location);
// Return true if NAME is an unexported field or method of TYPE.
static bool
is_unexported_field_or_method(Gogo*, const Type*, const std::string&,
std::vector<const Named_type*>*);
// Convert the builtin named types.
static void
convert_builtin_named_types(Gogo*);
// Return the backend representation of this type.
Btype*
get_backend(Gogo*);
// Return a placeholder for the backend representation of the type.
// This will return a type of the correct size, but for which some
// of the fields may still need to be completed.
Btype*
get_backend_placeholder(Gogo*);
// Finish the backend representation of a placeholder.
void
finish_backend(Gogo*, Btype*);
// Build a type descriptor entry for this type. Return a pointer to
// it. The location is the location which causes us to need the
// entry.
Bexpression*
type_descriptor_pointer(Gogo* gogo, Location);
// Build the Garbage Collection symbol for this type. Return a pointer to it.
Bexpression*
gc_symbol_pointer(Gogo* gogo);
// Return whether this type needs a garbage collection program.
// Sets *PTRSIZE and *PTRDATA.
bool
needs_gcprog(Gogo*, int64_t* ptrsize, int64_t* ptrdata);
// Return a ptrmask variable for this type.
Bvariable*
gc_ptrmask_var(Gogo*, int64_t ptrsize, int64_t ptrdata);
// Return the type reflection string for this type.
std::string
reflection(Gogo*) const;
// Add the backend name for the type to BNAME. This will add one or
// two name components. Identical types should have the same
// backend name.
void
backend_name(Gogo*, Backend_name* bname) const;
// If the size of the type can be determined, set *PSIZE to the size
// in bytes and return true. Otherwise, return false. This queries
// the backend.
bool
backend_type_size(Gogo*, int64_t* psize);
// If the alignment of the type can be determined, set *PALIGN to
// the alignment in bytes and return true. Otherwise, return false.
bool
backend_type_align(Gogo*, int64_t* palign);
// If the alignment of a struct field of this type can be
// determined, set *PALIGN to the alignment in bytes and return
// true. Otherwise, return false.
bool
backend_type_field_align(Gogo*, int64_t* palign);
// Determine the ptrdata size for the backend version of this type:
// the length of the prefix of the type that can contain a pointer
// value. If it can be determined, set *PPTRDATA to the value in
// bytes and return true. Otherwise, return false.
bool
backend_type_ptrdata(Gogo*, int64_t* pptrdata);
// Determine the ptrdata size that we are going to set in the type
// descriptor. This is normally the same as backend_type_ptrdata,
// but differs if we use a gcprog for an array. The arguments and
// results are as for backend_type_ptrdata.
bool
descriptor_ptrdata(Gogo*, int64_t* pptrdata);
// Whether the backend size is known.
bool
is_backend_type_size_known(Gogo*);
// Return whether the type needs specially built type functions.
bool
needs_specific_type_functions(Gogo*);
// Get the equality function for a type. Returns NULL if the type
// is not comparable.
Named_object*
equal_function(Gogo*, Named_type* name, Function_type* equal_fntype);
// Get the hash function for a type. Returns NULL if the type is
// not comparable.
Named_object*
hash_function(Gogo*, Function_type* hash_fntype);
// Write the equal function for a type.
void
write_equal_function(Gogo*, Named_type*, int64_t size,
const Backend_name*, Function_type* equal_fntype);
// Write the hash function for a type.
void
write_hash_function(Gogo*, int64_t size, const Backend_name*,
Function_type* hash_fntype);
// Return the alignment required by the memequalN function.
static int64_t memequal_align(Gogo*, int size);
// Export the type.
void
export_type(Export* exp) const
{ this->do_export(exp); }
// Import a type.
static Type*
import_type(Import*);
protected:
Type(Type_classification);
// Functions implemented by the child class.
// Traverse the subtypes.
virtual int
do_traverse(Traverse*);
// Verify the type.
virtual bool
do_verify()
{ return true; }
virtual bool
do_has_pointer() const
{ return false; }
virtual bool
do_compare_is_identity(Gogo*) = 0;
virtual bool
do_is_reflexive()
{ return true; }
virtual bool
do_needs_key_update()
{ return false; }
virtual bool
do_hash_might_panic()
{ return false; }
virtual bool
do_in_heap() const
{ return true; }
virtual unsigned int
do_hash_for_method(Gogo*, int) const;
virtual Btype*
do_get_backend(Gogo*) = 0;
virtual Expression*
do_type_descriptor(Gogo*, Named_type* name) = 0;
virtual void
do_reflection(Gogo*, std::string*) const = 0;
virtual void
do_mangled_name(Gogo*, std::string*, bool*) const = 0;
virtual void
do_export(Export*) const;
// For children to call when they detect that they are in error.
void
set_is_error();
// Return whether a method expects a pointer as the receiver.
static bool
method_expects_pointer(const Named_object*);
// Finalize the methods for a type.
static void
finalize_methods(Gogo*, const Type*, Location, Methods**);
// Return a method from a set of methods.
static Method*
method_function(const Methods*, const std::string& name,
bool* is_ambiguous);
// A mapping from interfaces to the associated interface method
// tables for this type. This maps to a decl.
typedef Unordered_map_hash(Interface_type*, Expression*, Type_hash_identical,
Type_identical) Interface_method_tables;
// Return a pointer to the interface method table for TYPE for the
// interface INTERFACE.
static Expression*
interface_method_table(Type* type,
Interface_type *interface, bool is_pointer,
Interface_method_tables** method_tables,
Interface_method_tables** pointer_tables);
// Return a composite literal for the type descriptor entry for a
// type.
static Expression*
type_descriptor(Gogo*, Type*);
// Return a composite literal for the type descriptor entry for
// TYPE, using NAME as the name of the type.
static Expression*
named_type_descriptor(Gogo*, Type* type, Named_type* name);
// Return a composite literal for a plain type descriptor for this
// type with the given kind and name.
Expression*
plain_type_descriptor(Gogo*, int runtime_type_kind, Named_type* name);
// Build a composite literal for the basic type descriptor.
Expression*
type_descriptor_constructor(Gogo*, int runtime_type_kind, Named_type*,
const Methods*, bool only_value_methods);
// For the benefit of child class reflection string generation.
void
append_reflection(const Type* type, Gogo* gogo, std::string* ret) const
{ type->do_reflection(gogo, ret); }
// For the benefit of child class mangling.
void
append_mangled_name(const Type* type, Gogo* gogo, std::string* ret,
bool *is_non_identifier) const
{ type->do_mangled_name(gogo, ret, is_non_identifier); }
// Return the backend representation for the underlying type of a
// named type.
static Btype*
get_named_base_btype(Gogo* gogo, Type* base_type)
{ return base_type->get_btype_without_hash(gogo); }
private:
// Convert to the desired type classification, or return NULL. This
// is a controlled dynamic_cast.
template<typename Type_class, Type_classification type_classification>
Type_class*
convert()
{
Type* base = this->base();
return (base->classification_ == type_classification
? static_cast<Type_class*>(base)
: NULL);
}
template<typename Type_class, Type_classification type_classification>
const Type_class*
convert() const
{
const Type* base = this->base();
return (base->classification_ == type_classification
? static_cast<Type_class*>(base)
: NULL);
}
template<typename Type_class, Type_classification type_classification>
Type_class*
convert_no_base()
{
return (this->classification_ == type_classification
? static_cast<Type_class*>(this)
: NULL);
}
template<typename Type_class, Type_classification type_classification>
const Type_class*
convert_no_base() const
{
return (this->classification_ == type_classification
? static_cast<Type_class*>(this)
: NULL);
}
// Map unnamed types to type descriptor decls.
typedef Unordered_map_hash(const Type*, Bvariable*, Type_hash_identical,
Type_identical) Type_descriptor_vars;
static Type_descriptor_vars type_descriptor_vars;
// Build the type descriptor variable for this type.
void
make_type_descriptor_var(Gogo*);
// Map unnamed types to type descriptor decls.
typedef Unordered_map_hash(const Type*, Bvariable*, Type_hash_identical,
Type_identical) GC_symbol_vars;
static GC_symbol_vars gc_symbol_vars;
// Map ptrmask symbol names to the ptrmask variable.
typedef Unordered_map(std::string, Bvariable*) GC_gcbits_vars;
static GC_gcbits_vars gc_gcbits_vars;
// Build the GC symbol for this type.
void
make_gc_symbol_var(Gogo*);
// Return true if the type descriptor for this type should be
// defined in some other package. If NAME is not NULL, it is the
// name of this type. If this returns true it sets *PACKAGE to the
// package where the type descriptor is defined.
bool
type_descriptor_defined_elsewhere(Named_type* name, const Package** package);
// Make a composite literal for the garbage collection program for
// this type.
Expression*
gcprog_constructor(Gogo*, int64_t ptrsize, int64_t ptrdata);
// Build the hash function for a type that needs specific functions.
Named_object*
build_hash_function(Gogo*, int64_t size, Function_type* hash_fntype);
// Build the equal function for a type that needs specific functions.
Named_object*
build_equal_function(Gogo*, Named_type*, int64_t size,
Function_type* equal_fntype);
void
write_identity_hash(Gogo*, int64_t size);
void
write_identity_equal(Gogo*, int64_t size);
void
write_named_equal(Gogo*, Named_type*);
// Build a composite literal for the uncommon type information.
Expression*
uncommon_type_constructor(Gogo*, Type* uncommon_type,
Named_type*, const Methods*,
bool only_value_methods) const;
// Build a composite literal for the methods.
Expression*
methods_constructor(Gogo*, Type* methods_type, const Methods*,
bool only_value_methods) const;
// Build a composite literal for one method.
Expression*
method_constructor(Gogo*, Type* method_type, const std::string& name,
const Method*, bool only_value_methods) const;
// Add all methods for TYPE to the list of methods for THIS.
static void
add_methods_for_type(const Type* type, const Method::Field_indexes*,
unsigned int depth, bool, bool,
std::vector<const Named_type*>*,
Methods*);
static void
add_local_methods_for_type(const Named_type* type,
const Method::Field_indexes*,
unsigned int depth, bool, bool, Methods*);
static void
add_embedded_methods_for_type(const Type* type,
const Method::Field_indexes*,
unsigned int depth, bool, bool,
std::vector<const Named_type*>*,
Methods*);
static void
add_interface_methods_for_type(const Type* type,
const Method::Field_indexes*,
unsigned int depth, Methods*);
// Build stub methods for a type.
static void
build_stub_methods(Gogo*, const Type* type, const Methods* methods,
Location);
static void
build_one_stub_method(Gogo*, Method*, const char* receiver_name,
const Typed_identifier_list*, bool is_varargs,
Location);
// Build direct interface stub methods for a type.
static void
build_direct_iface_stub_methods(Gogo*, const Type*, Methods*, Location);
static void
build_one_iface_stub_method(Gogo*, Method*, const char*,
const Typed_identifier_list*,
bool, Location);
static Expression*
apply_field_indexes(Expression*, const Method::Field_indexes*,
Location);
// Look for a field or method named NAME in TYPE.
static bool
find_field_or_method(const Type* type, const std::string& name,
bool receiver_can_be_pointer,
std::vector<const Named_type*>*, int* level,
bool* is_method, bool* found_pointer_method,
std::string* ambig1, std::string* ambig2);
// Helper function for is_direct_iface_type, to prevent infinite
// recursion.
bool
is_direct_iface_type_helper(Unordered_set(const Type*)*) const;
// Get the backend representation for a type without looking in the
// hash table for identical types.
Btype*
get_btype_without_hash(Gogo*);
// A backend type that may be a placeholder.
struct Type_btype_entry
{
Btype *btype;
bool is_placeholder;
};
// A mapping from Type to Btype*, used to ensure that the backend
// representation of identical types is identical. This is only
// used for unnamed types.
typedef Unordered_map_hash(const Type*, Type_btype_entry,
Type_hash_identical, Type_identical) Type_btypes;
static Type_btypes type_btypes;
// A list of builtin named types.
static std::vector<Named_type*> named_builtin_types;
// A map from types that need a specific hash or equality function
// to the hash or equality function.
typedef Unordered_map_hash(const Type*, Named_object*, Type_hash_identical,
Type_identical) Type_function;
static Type_function type_hash_functions_table;
static Type_function type_equal_functions_table;
// Cache for reusing existing pointer types; maps from pointed-to-type
// to pointer type.
typedef Unordered_map(Type*, Pointer_type*) Pointer_type_table;
static Pointer_type_table pointer_types;
// List of placeholder pointer types.
static std::vector<Type*> placeholder_pointers;
// The type classification.
Type_classification classification_;
// The backend representation of the type, once it has been
// determined.
Btype* btype_;
// The type descriptor for this type. This starts out as NULL and
// is filled in as needed.
Bvariable* type_descriptor_var_;
// The GC symbol for this type. This starts out as NULL and
// is filled in as needed.
Bvariable* gc_symbol_var_;
};
// Type hash table operations, treating aliases as identical to the
// types that they alias.
class Type_hash_identical
{
public:
unsigned int
operator()(const Type* type) const
{
return type->hash_for_method(NULL,
Type::COMPARE_ERRORS | Type::COMPARE_TAGS);
}
};
class Type_identical
{
public:
bool
operator()(const Type* t1, const Type* t2) const
{
return Type::are_identical(t1, t2,
Type::COMPARE_ERRORS | Type::COMPARE_TAGS,
NULL);
}
};
// An identifier with a type.
class Typed_identifier
{
public:
Typed_identifier(const std::string& name, Type* type,
Location location)
: name_(name), type_(type), location_(location), note_(NULL)
{ }
// Get the name.
const std::string&
name() const
{ return this->name_; }
// Get the type.
Type*
type() const
{ return this->type_; }
// Return the location where the name was seen. This is not always
// meaningful.
Location
location() const
{ return this->location_; }
// Set the type--sometimes we see the identifier before the type.
void
set_type(Type* type)
{
go_assert(this->type_ == NULL || type->is_error_type());
this->type_ = type;
}
// Get the escape note.
std::string*
note() const
{ return this->note_; }
// Set the escape note.
void
set_note(const std::string& note)
{
if (this->note_ != NULL)
go_assert(*this->note_ == note);
else
this->note_ = new std::string(note);
}
private:
// Identifier name.
std::string name_;
// Type.
Type* type_;
// The location where the name was seen.
Location location_;
// Escape note for this typed identifier. Used when importing and exporting
// functions.
std::string* note_;
};
// A list of Typed_identifiers.
class Typed_identifier_list
{
public:
Typed_identifier_list()
: entries_()
{ }
// Whether the list is empty.
bool
empty() const
{ return this->entries_.empty(); }
// Return the number of entries in the list.
size_t
size() const
{ return this->entries_.size(); }
// Add an entry to the end of the list.
void
push_back(const Typed_identifier& td)
{ this->entries_.push_back(td); }
// Remove an entry from the end of the list.
void
pop_back()
{ this->entries_.pop_back(); }
// Set the type of entry I to TYPE.
void
set_type(size_t i, Type* type)
{
go_assert(i < this->entries_.size());
this->entries_[i].set_type(type);
}
// Sort the entries by name.
void
sort_by_name();
// Traverse types.
int
traverse(Traverse*) const;
// Return the first and last elements.
Typed_identifier&
front()
{ return this->entries_.front(); }
const Typed_identifier&
front() const
{ return this->entries_.front(); }
Typed_identifier&
back()
{ return this->entries_.back(); }
const Typed_identifier&
back() const
{ return this->entries_.back(); }
Typed_identifier&
at(size_t i)
{ return this->entries_.at(i); }
const Typed_identifier&
at(size_t i) const
{ return this->entries_.at(i); }
void
set(size_t i, const Typed_identifier& t)
{ this->entries_.at(i) = t; }
void
resize(size_t c)
{
go_assert(c <= this->entries_.size());
this->entries_.resize(c, Typed_identifier("", NULL,
Linemap::unknown_location()));
}
void
reserve(size_t c)
{ this->entries_.reserve(c); }
// Iterators.
typedef std::vector<Typed_identifier>::iterator iterator;
typedef std::vector<Typed_identifier>::const_iterator const_iterator;
iterator
begin()
{ return this->entries_.begin(); }
const_iterator
begin() const
{ return this->entries_.begin(); }
iterator
end()
{ return this->entries_.end(); }
const_iterator
end() const
{ return this->entries_.end(); }
// Return a copy of this list. This returns an independent copy of
// the vector, but does not copy the types.
Typed_identifier_list*
copy() const;
private:
std::vector<Typed_identifier> entries_;
};
// A type used to indicate a parsing error. This exists to simplify
// later error detection.
class Error_type : public Type
{
public:
Error_type()
: Type(TYPE_ERROR)
{ }
protected:
bool
do_compare_is_identity(Gogo*)
{ return false; }
Btype*
do_get_backend(Gogo* gogo);
Expression*
do_type_descriptor(Gogo*, Named_type*);
void
do_reflection(Gogo*, std::string*) const;
void
do_mangled_name(Gogo*, std::string*, bool*) const;
};
// The void type.
class Void_type : public Type
{
public:
Void_type()
: Type(TYPE_VOID)
{ }
protected:
bool
do_compare_is_identity(Gogo*)
{ return false; }
Btype*
do_get_backend(Gogo* gogo);
Expression*
do_type_descriptor(Gogo*, Named_type*)
{ go_unreachable(); }
void
do_reflection(Gogo*, std::string*) const
{ }
void
do_mangled_name(Gogo*, std::string*, bool*) const;
};
// The boolean type.
class Boolean_type : public Type
{
public:
Boolean_type()
: Type(TYPE_BOOLEAN)
{ }
protected:
bool
do_compare_is_identity(Gogo*)
{ return true; }
Btype*
do_get_backend(Gogo* gogo);
Expression*
do_type_descriptor(Gogo*, Named_type* name);
// We should not be asked for the reflection string of a basic type.
void
do_reflection(Gogo*, std::string* ret) const
{ ret->append("bool"); }
void
do_mangled_name(Gogo*, std::string*, bool*) const;
};
// The type of an integer.
class Integer_type : public Type
{
public:
// Create a new integer type.
static Named_type*
create_integer_type(const char* name, bool is_unsigned, int bits,
int runtime_type_kind);
// Look up an existing integer type.
static Named_type*
lookup_integer_type(const char* name);
// Create an abstract integer type.
static Integer_type*
create_abstract_integer_type();
// Create an abstract character type.
static Integer_type*
create_abstract_character_type();
// Create an alias to an integer type.
static Named_type*
create_integer_type_alias(const char* name, Named_type* real_type);
// Whether this is an abstract integer type.
bool
is_abstract() const
{ return this->is_abstract_; }
// Whether this is an unsigned type.
bool
is_unsigned() const
{ return this->is_unsigned_; }
// The number of bits.
int
bits() const
{ return this->bits_; }
// Whether this type is the same as T.
bool
is_identical(const Integer_type* t) const;
// Whether this is the type "byte" or another name for "byte".
bool
is_byte() const
{ return this->is_byte_; }
// Mark this as the "byte" type.
void
set_is_byte()
{ this->is_byte_ = true; }
// Whether this is the type "rune" or another name for "rune".
bool
is_rune() const
{ return this->is_rune_; }
// Mark this as the "rune" type.
void
set_is_rune()
{ this->is_rune_ = true; }
protected:
bool
do_compare_is_identity(Gogo*)
{ return true; }
unsigned int
do_hash_for_method(Gogo*, int) const;
Btype*
do_get_backend(Gogo*);
Expression*
do_type_descriptor(Gogo*, Named_type*);
void
do_reflection(Gogo*, std::string*) const;
void
do_mangled_name(Gogo*, std::string*, bool*) const;
private:
Integer_type(bool is_abstract, bool is_unsigned, int bits,
int runtime_type_kind)
: Type(TYPE_INTEGER),
is_abstract_(is_abstract), is_unsigned_(is_unsigned), is_byte_(false),
is_rune_(false), bits_(bits), runtime_type_kind_(runtime_type_kind)
{ }
// Map names of integer types to the types themselves.
typedef std::map<std::string, Named_type*> Named_integer_types;
static Named_integer_types named_integer_types;
// True if this is an abstract type.
bool is_abstract_;
// True if this is an unsigned type.
bool is_unsigned_;
// True if this is the byte type.
bool is_byte_;
// True if this is the rune type.
bool is_rune_;
// The number of bits.
int bits_;
// The runtime type code used in the type descriptor for this type.
int runtime_type_kind_;
};
// The type of a floating point number.
class Float_type : public Type
{
public:
// Create a new float type.
static Named_type*
create_float_type(const char* name, int bits, int runtime_type_kind);
// Look up an existing float type.
static Named_type*
lookup_float_type(const char* name);
// Create an abstract float type.
static Float_type*
create_abstract_float_type();
// Whether this is an abstract float type.
bool
is_abstract() const
{ return this->is_abstract_; }
// The number of bits.
int
bits() const
{ return this->bits_; }
// Whether this type is the same as T.
bool
is_identical(const Float_type* t) const;
protected:
bool
do_compare_is_identity(Gogo*)
{ return false; }
bool
do_is_reflexive()
{ return false; }
// Distinction between +0 and -0 requires a key update.
bool
do_needs_key_update()
{ return true; }
unsigned int
do_hash_for_method(Gogo*, int) const;
Btype*
do_get_backend(Gogo*);
Expression*
do_type_descriptor(Gogo*, Named_type*);
void
do_reflection(Gogo*, std::string*) const;
void
do_mangled_name(Gogo*, std::string*, bool*) const;
private:
Float_type(bool is_abstract, int bits, int runtime_type_kind)
: Type(TYPE_FLOAT),
is_abstract_(is_abstract), bits_(bits),
runtime_type_kind_(runtime_type_kind)
{ }
// Map names of float types to the types themselves.
typedef std::map<std::string, Named_type*> Named_float_types;
static Named_float_types named_float_types;
// True if this is an abstract type.
bool is_abstract_;
// The number of bits in the floating point value.
int bits_;
// The runtime type code used in the type descriptor for this type.
int runtime_type_kind_;
};
// The type of a complex number.
class Complex_type : public Type
{
public:
// Create a new complex type.
static Named_type*
create_complex_type(const char* name, int bits, int runtime_type_kind);
// Look up an existing complex type.
static Named_type*
lookup_complex_type(const char* name);
// Create an abstract complex type.
static Complex_type*
create_abstract_complex_type();
// Whether this is an abstract complex type.
bool
is_abstract() const
{ return this->is_abstract_; }
// The number of bits: 64 or 128.
int bits() const
{ return this->bits_; }
// Whether this type is the same as T.
bool
is_identical(const Complex_type* t) const;
protected:
bool
do_compare_is_identity(Gogo*)
{ return false; }
bool
do_is_reflexive()
{ return false; }
// Distinction between +0 and -0 requires a key update.
bool
do_needs_key_update()
{ return true; }
unsigned int
do_hash_for_method(Gogo*, int) const;
Btype*
do_get_backend(Gogo*);
Expression*
do_type_descriptor(Gogo*, Named_type*);
void
do_reflection(Gogo*, std::string*) const;
void
do_mangled_name(Gogo*, std::string*, bool*) const;
private:
Complex_type(bool is_abstract, int bits, int runtime_type_kind)
: Type(TYPE_COMPLEX),
is_abstract_(is_abstract), bits_(bits),
runtime_type_kind_(runtime_type_kind)
{ }
// Map names of complex types to the types themselves.
typedef std::map<std::string, Named_type*> Named_complex_types;
static Named_complex_types named_complex_types;
// True if this is an abstract type.
bool is_abstract_;
// The number of bits in the complex value--64 or 128.
int bits_;
// The runtime type code used in the type descriptor for this type.
int runtime_type_kind_;
};
// The type of a string.
class String_type : public Type
{
public:
String_type()
: Type(TYPE_STRING)
{ }
protected:
bool
do_has_pointer() const
{ return true; }
bool
do_compare_is_identity(Gogo*)
{ return false; }
// New string might have a smaller backing store.
bool
do_needs_key_update()
{ return true; }
Btype*
do_get_backend(Gogo*);
Expression*
do_type_descriptor(Gogo*, Named_type*);
void
do_reflection(Gogo*, std::string*) const;
void
do_mangled_name(Gogo*, std::string*, bool*) const;
private:
// The named string type.
static Named_type* string_type_;
};
// The type of a function.
class Function_type : public Type
{
public:
Function_type(Typed_identifier* receiver, Typed_identifier_list* parameters,
Typed_identifier_list* results, Location location)
: Type(TYPE_FUNCTION),
receiver_(receiver), parameters_(parameters), results_(results),
location_(location), is_varargs_(false), is_builtin_(false),
fnbtype_(NULL), is_tagged_(false)
{ }
// Get the receiver.
const Typed_identifier*
receiver() const
{ return this->receiver_; }
// Add an escape note for the receiver.
void
add_receiver_note(int encoding)
{ this->receiver_->set_note(Escape_note::make_tag(encoding)); }
// Get the return names and types.
const Typed_identifier_list*
results() const
{ return this->results_; }
// Get the parameter names and types.
const Typed_identifier_list*
parameters() const
{ return this->parameters_; }
// Add an escape note for the ith parameter.
void
add_parameter_note(int index, int encoding)
{ this->parameters_->at(index).set_note(Escape_note::make_tag(encoding)); }
// Whether this function has been tagged during escape analysis.
bool
is_tagged() const
{ return this->is_tagged_; }
// Mark this function as tagged after analyzing its escape.
void
set_is_tagged()
{ this->is_tagged_ = true; }
// Whether this is a varargs function.
bool
is_varargs() const
{ return this->is_varargs_; }
// Whether this is a builtin function.
bool
is_builtin() const
{ return this->is_builtin_; }
// The location where this type was defined.
Location
location() const
{ return this->location_; }
// Return whether this is a method type.
bool
is_method() const
{ return this->receiver_ != NULL; }
// Whether T is a valid redeclaration of this type. This is called
// when a function is declared more than once.
bool
is_valid_redeclaration(const Function_type* t, std::string*) const;
// Whether this type is the same as T.
bool
is_identical(const Function_type* t, bool ignore_receiver, int flags,
std::string*) const;
// Record that this is a varargs function.
void
set_is_varargs()
{ this->is_varargs_ = true; }
// Record that this is a builtin function.
void
set_is_builtin()
{ this->is_builtin_ = true; }
// Import a function type.
static Function_type*
do_import(Import*);
// Return a copy of this type without a receiver. This is only
// valid for a method type.
Function_type*
copy_without_receiver() const;
// Return a copy of this type with a receiver. This is used when an
// interface method is attached to a named or struct type.
Function_type*
copy_with_receiver(Type*) const;
// Return a copy of this type with the receiver treated as the first
// parameter. If WANT_POINTER_RECEIVER is true, the receiver is
// forced to be a pointer.
Function_type*
copy_with_receiver_as_param(bool want_pointer_receiver) const;
// Return a copy of this type ignoring any receiver and using dummy
// names for all parameters. This is used for thunks for method
// values.
Function_type*
copy_with_names() const;
static Type*
make_function_type_descriptor_type();
// Return the backend representation of this function type. This is used
// as the real type of a backend function declaration or defintion.
Btype*
get_backend_fntype(Gogo*);
// Return whether this is a Backend_function_type.
virtual bool
is_backend_function_type() const
{ return false; }
protected:
int
do_traverse(Traverse*);
// A function descriptor may be allocated on the heap.
bool
do_has_pointer() const
{ return true; }
bool
do_compare_is_identity(Gogo*)
{ return false; }
unsigned int
do_hash_for_method(Gogo*, int) const;
Btype*
do_get_backend(Gogo*);
Expression*
do_type_descriptor(Gogo*, Named_type*);
void
do_reflection(Gogo*, std::string*) const;
void
do_mangled_name(Gogo*, std::string*, bool*) const;
void
do_export(Export*) const;
private:
Expression*
type_descriptor_params(Type*, const Typed_identifier*,
const Typed_identifier_list*);
// A mapping from a list of result types to a backend struct type.
class Results_hash
{
public:
unsigned int
operator()(const Typed_identifier_list*) const;
};
class Results_equal
{
public:
bool
operator()(const Typed_identifier_list*,
const Typed_identifier_list*) const;
};
typedef Unordered_map_hash(Typed_identifier_list*, Btype*,
Results_hash, Results_equal) Results_structs;
static Results_structs results_structs;
// The receiver name and type. This will be NULL for a normal
// function, non-NULL for a method.
Typed_identifier* receiver_;
// The parameter names and types.
Typed_identifier_list* parameters_;
// The result names and types. This will be NULL if no result was
// specified.
Typed_identifier_list* results_;
// The location where this type was defined. This exists solely to
// give a location for the fields of the struct if this function
// returns multiple values.
Location location_;
// Whether this function takes a variable number of arguments.
bool is_varargs_;
// Whether this is a special builtin function which can not simply
// be called. This is used for len, cap, etc.
bool is_builtin_;
// The backend representation of this type for backend function
// declarations and definitions.
Btype* fnbtype_;
// Whether this function has been analyzed by escape analysis. If this is
// TRUE, this function type's parameters contain a summary of the analysis.
bool is_tagged_;
};
// The type of a function's backend representation.
class Backend_function_type : public Function_type
{
public:
Backend_function_type(Typed_identifier* receiver,
Typed_identifier_list* parameters,
Typed_identifier_list* results, Location location)
: Function_type(receiver, parameters, results, location)
{ }
// Return whether this is a Backend_function_type. This overrides
// Function_type::is_backend_function_type.
bool
is_backend_function_type() const
{ return true; }
protected:
Btype*
do_get_backend(Gogo* gogo)
{ return this->get_backend_fntype(gogo); }
};
// The type of a pointer.
class Pointer_type : public Type
{
public:
Pointer_type(Type* to_type)
: Type(TYPE_POINTER),
to_type_(to_type)
{}
Type*
points_to() const
{ return this->to_type_; }
// Import a pointer type.
static Pointer_type*
do_import(Import*);
static Type*
make_pointer_type_descriptor_type();
protected:
int
do_traverse(Traverse*);
bool
do_verify()
{ return this->to_type_->verify(); }
bool
do_has_pointer() const
{ return true; }
bool
do_compare_is_identity(Gogo*)
{ return true; }
unsigned int
do_hash_for_method(Gogo*, int) const;
Btype*
do_get_backend(Gogo*);
Expression*
do_type_descriptor(Gogo*, Named_type*);
void
do_reflection(Gogo*, std::string*) const;
void
do_mangled_name(Gogo*, std::string*, bool*) const;
void
do_export(Export*) const;
private:
// The type to which this type points.
Type* to_type_;
};
// The nil type. We use a special type for nil because it is not the
// same as any other type. In C term nil has type void*, but there is
// no such type in Go.
class Nil_type : public Type
{
public:
Nil_type()
: Type(TYPE_NIL)
{ }
protected:
bool
do_compare_is_identity(Gogo*)
{ return false; }
Btype*
do_get_backend(Gogo* gogo);
Expression*
do_type_descriptor(Gogo*, Named_type*)
{ go_unreachable(); }
void
do_reflection(Gogo*, std::string*) const
{ go_unreachable(); }
void
do_mangled_name(Gogo*, std::string*, bool*) const;
};
// The type of a field in a struct.
class Struct_field
{
public:
explicit Struct_field(const Typed_identifier& typed_identifier)
: typed_identifier_(typed_identifier), tag_(NULL), is_imported_(false)
{ }
// The field name.
const std::string&
field_name() const;
// Return whether this struct field is named NAME.
bool
is_field_name(const std::string& name) const;
// Return whether this struct field is an unexported field named NAME.
bool
is_unexported_field_name(Gogo*, const std::string& name) const;
// Return whether this struct field is an embedded built-in type.
bool
is_embedded_builtin(Gogo*) const;
// The field type.
Type*
type() const
{ return this->typed_identifier_.type(); }
// The field location.
Location
location() const
{ return this->typed_identifier_.location(); }
// Whether the field has a tag.
bool
has_tag() const
{ return this->tag_ != NULL; }
// The tag.
const std::string&
tag() const
{
go_assert(this->tag_ != NULL);
return *this->tag_;
}
// Whether this is an anonymous field.
bool
is_anonymous() const
{ return this->typed_identifier_.name().empty(); }
// Set the tag. FIXME: This is never freed.
void
set_tag(const std::string& tag)
{ this->tag_ = new std::string(tag); }
// Record that this field is defined in an imported struct.
void
set_is_imported()
{ this->is_imported_ = true; }
// Set the type. This is only used in error cases.
void
set_type(Type* type)
{ this->typed_identifier_.set_type(type); }
private:
// The field name, type, and location.
Typed_identifier typed_identifier_;
// The field tag. This is NULL if the field has no tag.
std::string* tag_;
// Whether this field is defined in an imported struct.
bool is_imported_;
};
// A list of struct fields.
class Struct_field_list
{
public:
Struct_field_list()
: entries_()
{ }
// Whether the list is empty.
bool
empty() const
{ return this->entries_.empty(); }
// Return the number of entries.
size_t
size() const
{ return this->entries_.size(); }
// Add an entry to the end of the list.
void
push_back(const Struct_field& sf)
{ this->entries_.push_back(sf); }
// Index into the list.
const Struct_field&
at(size_t i) const
{ return this->entries_.at(i); }
// Last entry in list.
Struct_field&
back()
{ return this->entries_.back(); }
// Iterators.
typedef std::vector<Struct_field>::iterator iterator;
typedef std::vector<Struct_field>::const_iterator const_iterator;
iterator
begin()
{ return this->entries_.begin(); }
const_iterator
begin() const
{ return this->entries_.begin(); }
iterator
end()
{ return this->entries_.end(); }
const_iterator
end() const
{ return this->entries_.end(); }
private:
std::vector<Struct_field> entries_;
};
// The type of a struct.
class Struct_type : public Type
{
public:
Struct_type(Struct_field_list* fields, Location location)
: Type(TYPE_STRUCT),
fields_(fields), location_(location), all_methods_(NULL),
is_struct_incomparable_(false), has_padding_(false)
{ }
// Return the field NAME. This only looks at local fields, not at
// embedded types. If the field is found, and PINDEX is not NULL,
// this sets *PINDEX to the field index. If the field is not found,
// this returns NULL.
const Struct_field*
find_local_field(const std::string& name, unsigned int *pindex) const;
// Return the field number INDEX.
const Struct_field*
field(unsigned int index) const
{ return &this->fields_->at(index); }
// Get the struct fields.
const Struct_field_list*
fields() const
{ return this->fields_; }
// Return the number of fields.
size_t
field_count() const
{ return this->fields_->size(); }
// Location of struct definition.
Location
location() const
{ return this->location_; }
// Push a new field onto the end of the struct. This is used when
// building a closure variable.
void
push_field(const Struct_field& sf)
{ this->fields_->push_back(sf); }
// Return an expression referring to field NAME in STRUCT_EXPR, or
// NULL if there is no field with that name.
Field_reference_expression*
field_reference(Expression* struct_expr, const std::string& name,
Location) const;
// Return the total number of fields, including embedded fields.
// This is the number of values that can appear in a conversion to
// this type.
unsigned int
total_field_count() const;
// Whether this type is identical with T.
bool
is_identical(const Struct_type* t, int) const;
// Return whether NAME is a local field which is not exported. This
// is only used for better error reporting.
bool
is_unexported_local_field(Gogo*, const std::string& name) const;
// If this is an unnamed struct, build the complete list of methods,
// including those from anonymous fields, and build methods stubs if
// needed.
void
finalize_methods(Gogo*);
// Return whether this type has any methods. This should only be
// called after the finalize_methods pass.
bool
has_any_methods() const
{ return this->all_methods_ != NULL; }
// Return the methods for this type. This should only be called
// after the finalize_methods pass.
const Methods*
methods() const
{ return this->all_methods_; }
// Return the method to use for NAME. This returns NULL if there is
// no such method or if the method is ambiguous. When it returns
// NULL, this sets *IS_AMBIGUOUS if the method name is ambiguous.
Method*
method_function(const std::string& name, bool* is_ambiguous) const;
// Return a pointer to the interface method table for this type for
// the interface INTERFACE. If IS_POINTER is true, set the type
// descriptor to a pointer to this type, otherwise set it to this
// type.
Expression*
interface_method_table(Interface_type* interface, bool is_pointer);
// Traverse just the field types of a struct type.
int
traverse_field_types(Traverse* traverse)
{ return this->do_traverse(traverse); }
// If the offset of field INDEX in the backend implementation can be
// determined, set *POFFSET to the offset in bytes and return true.
// Otherwise, return false.
bool
backend_field_offset(Gogo*, unsigned int index, int64_t* poffset);
// Finish the backend representation of all the fields.
void
finish_backend_fields(Gogo*);
// Import a struct type.
static Struct_type*
do_import(Import*);
static Type*
make_struct_type_descriptor_type();
// Return whether this is a generated struct that is not comparable.
bool
is_struct_incomparable() const
{ return this->is_struct_incomparable_; }
// Record that this is a generated struct that is not comparable.
void
set_is_struct_incomparable()
{ this->is_struct_incomparable_ = true; }
// Return whether this struct's backend type has padding, due to
// trailing zero-sized field.
bool
has_padding() const
{ return this->has_padding_; }
// Record that this struct's backend type has padding.
void
set_has_padding()
{ this->has_padding_ = true; }
// Write the hash function for this type.
void
write_hash_function(Gogo*, Function_type*);
// Write the equality function for this type.
void
write_equal_function(Gogo*, Named_type*);
// Whether we can write this type to a C header file, to implement
// -fgo-c-header.
bool
can_write_to_c_header(std::vector<const Named_object*>*,
std::vector<const Named_object*>*) const;
// Write this type to a C header file, to implement -fgo-c-header.
void
write_to_c_header(std::ostream&) const;
protected:
int
do_traverse(Traverse*);
bool
do_verify();
bool
do_has_pointer() const;
bool
do_compare_is_identity(Gogo*);
bool
do_is_reflexive();
bool
do_needs_key_update();
bool
do_hash_might_panic();
bool
do_in_heap() const;
unsigned int
do_hash_for_method(Gogo*, int) const;
Btype*
do_get_backend(Gogo*);
Expression*
do_type_descriptor(Gogo*, Named_type*);
void
do_reflection(Gogo*, std::string*) const;
void
do_mangled_name(Gogo*, std::string*, bool*) const;
void
do_export(Export*) const;
private:
bool
can_write_type_to_c_header(const Type*,
std::vector<const Named_object*>*,
std::vector<const Named_object*>*) const;
void
write_field_to_c_header(std::ostream&, const std::string&, const Type*) const;
// Used to merge method sets of identical unnamed structs.
typedef Unordered_map_hash(Struct_type*, Struct_type*, Type_hash_identical,
Type_identical) Identical_structs;
static Identical_structs identical_structs;
// Used to manage method tables for identical unnamed structs.
typedef std::pair<Interface_method_tables*, Interface_method_tables*>
Struct_method_table_pair;
typedef Unordered_map_hash(Struct_type*, Struct_method_table_pair*,
Type_hash_identical, Type_identical)
Struct_method_tables;
static Struct_method_tables struct_method_tables;
// Used to avoid infinite loops in field_reference_depth.
struct Saw_named_type
{
Saw_named_type* next;
Named_type* nt;
};
Field_reference_expression*
field_reference_depth(Expression* struct_expr, const std::string& name,
Location, Saw_named_type*,
unsigned int* depth) const;
// The fields of the struct.
Struct_field_list* fields_;
// The place where the struct was declared.
Location location_;
// If this struct is unnamed, a list of methods.
Methods* all_methods_;
// True if this is a generated struct that is not considered to be
// comparable.
bool is_struct_incomparable_;
// True if this struct's backend type has padding, due to trailing
// zero-sized field.
bool has_padding_;
};
// The type of an array.
class Array_type : public Type
{
public:
Array_type(Type* element_type, Expression* length)
: Type(TYPE_ARRAY),
element_type_(element_type), length_(length), blength_(NULL),
issued_length_error_(false), is_array_incomparable_(false)
{ }
// Return the element type.
Type*
element_type() const
{ return this->element_type_; }
// Return the length. This will return NULL for a slice.
Expression*
length() const
{ return this->length_; }
// Store the length as an int64_t into *PLEN. Return false if the
// length can not be determined. This will assert if called for a
// slice.
bool
int_length(int64_t* plen) const;
// Whether this type is identical with T.
bool
is_identical(const Array_type* t, int) const;
// Return an expression for the pointer to the values in an array.
Expression*
get_value_pointer(Gogo*, Expression* array, bool is_lvalue) const;
// Return an expression for the length of an array with this type.
Expression*
get_length(Gogo*, Expression* array) const;
// Return an expression for the capacity of an array with this type.
Expression*
get_capacity(Gogo*, Expression* array) const;
// Import an array type.
static Array_type*
do_import(Import*);
// Return the backend representation of the element type.
Btype*
get_backend_element(Gogo*, bool use_placeholder);
// Return the backend representation of the length.
Bexpression*
get_backend_length(Gogo*);
// Finish the backend representation of the element type.
void
finish_backend_element(Gogo*);
static Type*
make_array_type_descriptor_type();
static Type*
make_slice_type_descriptor_type();
// Return whether this is a generated array that is not comparable.
bool
is_array_incomparable() const
{ return this->is_array_incomparable_; }
// Record that this is a generated array that is not comparable.
void
set_is_array_incomparable()
{ this->is_array_incomparable_ = true; }
// Write the hash function for this type.
void
write_hash_function(Gogo*, Function_type*);
// Write the equality function for this type.
void
write_equal_function(Gogo*, Named_type*);
protected:
int
do_traverse(Traverse* traverse);
bool
do_verify();
bool
do_has_pointer() const;
bool
do_compare_is_identity(Gogo*);
bool
do_is_reflexive()
{
return this->length_ != NULL && this->element_type_->is_reflexive();
}
bool
do_needs_key_update()
{ return this->element_type_->needs_key_update(); }
bool
do_hash_might_panic()
{ return this->length_ != NULL && this->element_type_->hash_might_panic(); }
bool
do_in_heap() const
{ return this->length_ == NULL || this->element_type_->in_heap(); }
unsigned int
do_hash_for_method(Gogo*, int) const;
Btype*
do_get_backend(Gogo*);
Expression*
do_type_descriptor(Gogo*, Named_type*);
void
do_reflection(Gogo*, std::string*) const;
void
do_mangled_name(Gogo*, std::string*, bool*) const;
void
do_export(Export*) const;
private:
bool
verify_length();
Expression*
array_type_descriptor(Gogo*, Named_type*);
Expression*
slice_type_descriptor(Gogo*, Named_type*);
// The type of elements of the array.
Type* element_type_;
// The number of elements. This may be NULL.
Expression* length_;
// The backend representation of the length.
// We only want to compute this once.
Bexpression* blength_;
// Whether or not an invalid length error has been issued for this type,
// to avoid knock-on errors.
mutable bool issued_length_error_;
// True if this is a generated array that is not considered to be
// comparable.
bool is_array_incomparable_;
};
// The type of a map.
class Map_type : public Type
{
public:
Map_type(Type* key_type, Type* val_type, Location location)
: Type(TYPE_MAP),
key_type_(key_type), val_type_(val_type), hmap_type_(NULL),
bucket_type_(NULL), hiter_type_(NULL), location_(location)
{ }
// Return the key type.
Type*
key_type() const
{ return this->key_type_; }
// Return the value type.
Type*
val_type() const
{ return this->val_type_; }
// Return the type used for an iteration over this map.
Type*
hiter_type(Gogo*);
// If this map requires the "fat" functions, returns the pointer to
// pass as the zero value to those functions. Otherwise, in the
// normal case, returns NULL.
Expression*
fat_zero_value(Gogo*);
// Map algorithm to use for this map type. We may use specialized
// fast map routines for certain key types.
enum Map_alg
{
// 32-bit key.
MAP_ALG_FAST32,
// 32-bit pointer key.
MAP_ALG_FAST32PTR,
// 64-bit key.
MAP_ALG_FAST64,
// 64-bit pointer key.
MAP_ALG_FAST64PTR,
// String key.
MAP_ALG_FASTSTR,
// Anything else.
MAP_ALG_SLOW,
};
Map_alg
algorithm(Gogo*);
// Return whether VAR is the map zero value.
static bool
is_zero_value(Variable* var);
// Return the backend representation of the map zero value.
static Bvariable*
backend_zero_value(Gogo*);
// Whether this type is identical with T.
bool
is_identical(const Map_type* t, int) const;
// Import a map type.
static Map_type*
do_import(Import*);
static Type*
make_map_type_descriptor_type();
// This must be in sync with libgo/go/runtime/map.go.
static const int bucket_size = 8;
protected:
int
do_traverse(Traverse*);
bool
do_verify();
bool
do_has_pointer() const
{ return true; }
bool
do_compare_is_identity(Gogo*)
{ return false; }
bool
do_is_reflexive()
{
return this->key_type_->is_reflexive() && this->val_type_->is_reflexive();
}
unsigned int
do_hash_for_method(Gogo*, int) const;
Btype*
do_get_backend(Gogo*);
Expression*
do_type_descriptor(Gogo*, Named_type*);
void
do_reflection(Gogo*, std::string*) const;
void
do_mangled_name(Gogo*, std::string*, bool*) const;
void
do_export(Export*) const;
private:
// These must be in sync with libgo/go/runtime/map.go.
static const int max_key_size = 128;
static const int max_val_size = 128;
static const int max_zero_size = 1024;
// Maps with value types larger than max_zero_size require passing a
// zero value pointer to the map functions.
// The zero value variable.
static Named_object* zero_value;
// The current size of the zero value.
static int64_t zero_value_size;
// The current alignment of the zero value.
static int64_t zero_value_align;
Type*
bucket_type(Gogo*, int64_t, int64_t);
Type*
hmap_type(Type*);
// The key type.
Type* key_type_;
// The value type.
Type* val_type_;
// The hashmap type. At run time a map is represented as a pointer
// to this type.
Type* hmap_type_;
// The bucket type, the type used to hold keys and values at run time.
Type* bucket_type_;
// The iterator type.
Type* hiter_type_;
// Where the type was defined.
Location location_;
};
// The type of a channel.
class Channel_type : public Type
{
public:
Channel_type(bool may_send, bool may_receive, Type* element_type)
: Type(TYPE_CHANNEL),
may_send_(may_send), may_receive_(may_receive),
element_type_(element_type)
{ go_assert(may_send || may_receive); }
// Whether this channel can send data.
bool
may_send() const
{ return this->may_send_; }
// Whether this channel can receive data.
bool
may_receive() const
{ return this->may_receive_; }
// The type of the values that may be sent on this channel. This is
// NULL if any type may be sent.
Type*
element_type() const
{ return this->element_type_; }
// Whether this type is identical with T.
bool
is_identical(const Channel_type* t, int) const;
// Import a channel type.
static Channel_type*
do_import(Import*);
static Type*
make_chan_type_descriptor_type();
static Type*
select_case_type();
protected:
int
do_traverse(Traverse* traverse)
{ return Type::traverse(this->element_type_, traverse); }
bool
do_verify();
bool
do_has_pointer() const
{ return true; }
bool
do_compare_is_identity(Gogo*)
{ return true; }
unsigned int
do_hash_for_method(Gogo*, int) const;
Btype*
do_get_backend(Gogo*);
Expression*
do_type_descriptor(Gogo*, Named_type*);
void
do_reflection(Gogo*, std::string*) const;
void
do_mangled_name(Gogo*, std::string*, bool*) const;
void
do_export(Export*) const;
private:
// Whether this channel can send data.
bool may_send_;
// Whether this channel can receive data.
bool may_receive_;
// The types of elements which may be sent on this channel. If this
// is NULL, it means that any type may be sent.
Type* element_type_;
};
// An interface type.
class Interface_type : public Type
{
public:
Interface_type(Typed_identifier_list* methods, Location location)
: Type(TYPE_INTERFACE),
parse_methods_(methods), all_methods_(NULL), location_(location),
package_(NULL), interface_btype_(NULL), bmethods_(NULL),
assume_identical_(NULL), methods_are_finalized_(false),
bmethods_is_placeholder_(false), seen_(false)
{ go_assert(methods == NULL || !methods->empty()); }
// The location where the interface type was defined.
Location
location() const
{ return this->location_; }
// The package where the interface type was defined. Returns NULL
// for the package currently being compiled.
Package*
package() const
{ return this->package_; }
// Return whether this is an empty interface.
bool
is_empty() const
{
go_assert(this->methods_are_finalized_);
return this->all_methods_ == NULL;
}
// Return the list of locally defined methods. This will return NULL
// for an empty interface. Embedded interfaces will appear in this
// list as an entry with no name.
const Typed_identifier_list*
local_methods() const
{ return this->parse_methods_; }
// Return the list of all methods. This will return NULL for an
// empty interface.
const Typed_identifier_list*
methods() const;
// Return the number of methods.
size_t
method_count() const;
// Return the method NAME, or NULL.
const Typed_identifier*
find_method(const std::string& name) const;
// Return the zero-based index of method NAME.
size_t
method_index(const std::string& name) const;
// Finalize the methods. This sets all_methods_. This handles
// interface inheritance.
void
finalize_methods();
// Return true if T implements this interface. If this returns
// false, and REASON is not NULL, it sets *REASON to the reason that
// it fails.
bool
implements_interface(const Type* t, std::string* reason) const;
// Whether this type is identical with T. REASON is as in
// implements_interface.
bool
is_identical(const Interface_type* t, int) const;
// Whether we can assign T to this type. is_identical is known to
// be false.
bool
is_compatible_for_assign(const Interface_type*, std::string* reason) const;
// Return whether NAME is a method which is not exported. This is
// only used for better error reporting.
bool
is_unexported_method(Gogo*, const std::string& name) const;
// Import an interface type.
static Interface_type*
do_import(Import*);
// Make a struct for an empty interface type.
static Btype*
get_backend_empty_interface_type(Gogo*);
// Get a pointer to the backend representation of the method table.
Btype*
get_backend_methods(Gogo*);
// Return a placeholder for the backend representation of the
// pointer to the method table.
Btype*
get_backend_methods_placeholder(Gogo*);
// Finish the backend representation of the method types.
void
finish_backend_methods(Gogo*);
static Type*
make_interface_type_descriptor_type();
// Return whether methods are finalized for this interface.
bool
methods_are_finalized() const
{ return this->methods_are_finalized_; }
// Sort embedded interfaces by name. Needed when we are preparing
// to emit types into the export data.
void
sort_embedded()
{
if (parse_methods_ != NULL)
parse_methods_->sort_by_name();
}
protected:
int
do_traverse(Traverse*);
bool
do_has_pointer() const
{ return true; }
bool
do_compare_is_identity(Gogo*)
{ return false; }
// Not reflexive if it contains a float.
bool
do_is_reflexive()
{ return false; }
// Distinction between +0 and -0 requires a key update if it
// contains a float.
bool
do_needs_key_update()
{ return true; }
// Hashing an unhashable type stored in an interface might panic.
bool
do_hash_might_panic()
{ return true; }
unsigned int
do_hash_for_method(Gogo*, int) const;
Btype*
do_get_backend(Gogo*);
Expression*
do_type_descriptor(Gogo*, Named_type*);
void
do_reflection(Gogo*, std::string*) const;
void
do_mangled_name(Gogo*, std::string*, bool*) const;
void
do_export(Export*) const;
private:
// This type guards against infinite recursion when comparing
// interface types. We keep a list of interface types assumed to be
// identical during comparison. We just keep the list on the stack.
// This permits us to compare cases like
// type I1 interface { F() interface{I1} }
// type I2 interface { F() interface{I2} }
struct Assume_identical
{
Assume_identical* next;
const Interface_type* t1;
const Interface_type* t2;
};
bool
assume_identical(const Interface_type*, const Interface_type*) const;
struct Bmethods_map_entry
{
Btype *btype;
bool is_placeholder;
};
// A mapping from Interface_type to the backend type of its bmethods_,
// used to ensure that the backend representation of identical types
// is identical.
typedef Unordered_map_hash(const Interface_type*, Bmethods_map_entry,
Type_hash_identical, Type_identical) Bmethods_map;
static Bmethods_map bmethods_map;
// The list of methods associated with the interface from the
// parser. This will be NULL for the empty interface. This may
// include unnamed interface types.
Typed_identifier_list* parse_methods_;
// The list of all methods associated with the interface. This
// expands any interface types listed in methods_. It is set by
// finalize_methods. This will be NULL for the empty interface.
Typed_identifier_list* all_methods_;
// The location where the interface was defined.
Location location_;
// The package where the interface was defined. This is NULL for
// the package being compiled.
Package* package_;
// The backend representation of this type during backend conversion.
Btype* interface_btype_;
// The backend representation of the pointer to the method table.
Btype* bmethods_;
// A list of interface types assumed to be identical during
// interface comparison.
mutable Assume_identical* assume_identical_;
// Whether the methods have been finalized.
bool methods_are_finalized_;
// Whether the bmethods_ field is a placeholder.
bool bmethods_is_placeholder_;
// Used to avoid endless recursion in do_mangled_name.
mutable bool seen_;
};
// The value we keep for a named type. This lets us get the right
// name when we convert to backend. Note that we don't actually keep
// the name here; the name is in the Named_object which points to
// this. This object exists to hold a unique backend representation for
// the type.
class Named_type : public Type
{
public:
Named_type(Named_object* named_object, Type* type, Location location)
: Type(TYPE_NAMED),
named_object_(named_object), in_function_(NULL), in_function_index_(0),
type_(type), local_methods_(NULL), all_methods_(NULL),
interface_method_tables_(NULL), pointer_interface_method_tables_(NULL),
location_(location), named_btype_(NULL), dependencies_(),
is_alias_(false), is_visible_(true), is_error_(false), in_heap_(true),
is_placeholder_(false), is_converted_(false), is_verified_(false),
seen_(false), seen_in_compare_is_identity_(false),
seen_in_get_backend_(false), seen_alias_(false)
{ }
// Return the associated Named_object. This holds the actual name.
Named_object*
named_object()
{ return this->named_object_; }
const Named_object*
named_object() const
{ return this->named_object_; }
// Set the Named_object. This is used when we see a type
// declaration followed by a type.
void
set_named_object(Named_object* no)
{ this->named_object_ = no; }
// Whether this is an alias (type T1 = T2) rather than an ordinary
// named type (type T1 T2).
bool
is_alias() const
{ return this->is_alias_; }
// Record that this type is an alias.
void
set_is_alias()
{ this->is_alias_ = true; }
// Mark this type as not permitted in the heap.
void
set_not_in_heap()
{ this->in_heap_ = false; }
// Return the function in which this type is defined. This will
// return NULL for a type defined in global scope.
const Named_object*
in_function(unsigned int *pindex) const
{
*pindex = this->in_function_index_;
return this->in_function_;
}
// Set the function in which this type is defined.
void
set_in_function(Named_object* f, unsigned int index)
{
this->in_function_ = f;
this->in_function_index_ = index;
}
// Return the name of the type.
const std::string&
name() const;
// Return the name of the type for an error message. The difference
// is that if the type is defined in a different package, this will
// return PACKAGE.NAME.
std::string
message_name() const;
// Return the underlying type.
Type*
real_type()
{ return this->type_; }
const Type*
real_type() const
{ return this->type_; }
// Return the location.
Location
location() const
{ return this->location_; }
// Whether this type is visible. This only matters when parsing.
bool
is_visible() const
{ return this->is_visible_; }
// Mark this type as visible.
void
set_is_visible()
{ this->is_visible_ = true; }
// Mark this type as invisible.
void
clear_is_visible()
{ this->is_visible_ = false; }
// Whether this is a builtin type.
bool
is_builtin() const
{ return Linemap::is_predeclared_location(this->location_); }
// Whether this named type is valid. A recursive named type is invalid.
bool
is_valid() const
{ return !this->is_error_; }
// Return the base type for this type.
Type*
named_base();
const Type*
named_base() const;
// Return whether this is an error type.
bool
is_named_error_type() const;
// Return whether this type is comparable. If REASON is not NULL,
// set *REASON when returning false.
bool
named_type_is_comparable(std::string* reason) const;
// Add a method to this type.
Named_object*
add_method(const std::string& name, Function*);
// Add a method declaration to this type.
Named_object*
add_method_declaration(const std::string& name, Package* package,
Function_type* type, Location location);
// Add an existing method--one defined before the type itself was
// defined--to a type.
void
add_existing_method(Named_object*);
// Look up a local method.
Named_object*
find_local_method(const std::string& name) const;
// Return the list of local methods.
const Bindings*
local_methods() const;
// Build the complete list of methods, including those from
// anonymous fields, and build method stubs if needed.
void
finalize_methods(Gogo*);
// Return whether this type has any methods. This should only be
// called after the finalize_methods pass.
bool
has_any_methods() const;
// Return the methods for this type. This should only be called
// after the finalized_methods pass.
const Methods*
methods() const;
// Return the method to use for NAME. This returns NULL if there is
// no such method or if the method is ambiguous. When it returns
// NULL, this sets *IS_AMBIGUOUS if the method name is ambiguous.
Method*
method_function(const std::string& name, bool *is_ambiguous) const;
// Return whether NAME is a known field or method which is not
// exported. This is only used for better error reporting.
bool
is_unexported_local_method(Gogo*, const std::string& name) const;
// Return a pointer to the interface method table for this type for
// the interface INTERFACE. If IS_POINTER is true, set the type
// descriptor to a pointer to this type, otherwise set it to this
// type.
Expression*
interface_method_table(Interface_type* interface, bool is_pointer);
// Note that a type must be converted to the backend representation
// before we convert this type.
void
add_dependency(Named_type* nt)
{ this->dependencies_.push_back(nt); }
// Return true if the size and alignment of the backend
// representation of this type is known. This is always true after
// types have been converted, but may be false beforehand.
bool
is_named_backend_type_size_known() const
{ return this->named_btype_ != NULL && !this->is_placeholder_; }
// Add to the reflection string as for Type::append_reflection, but
// if USE_ALIAS use the alias name rather than the alias target.
void
append_reflection_type_name(Gogo*, bool use_alias, std::string*) const;
// Append the symbol type name as for Type::append_mangled_name,
// but if USE_ALIAS use the alias name rather than the alias target.
void
append_symbol_type_name(Gogo*, bool use_alias, std::string*,
bool* is_non_identifier) const;
// Import a named type.
static void
import_named_type(Import*, Named_type**);
// Initial conversion to backend representation.
void
convert(Gogo*);
protected:
int
do_traverse(Traverse* traverse)
{ return Type::traverse(this->type_, traverse); }
bool
do_verify();
bool
do_has_pointer() const;
bool
do_compare_is_identity(Gogo*);
bool
do_is_reflexive();
bool
do_needs_key_update();
bool
do_in_heap() const;
unsigned int
do_hash_for_method(Gogo*, int) const;
Btype*
do_get_backend(Gogo*);
Expression*
do_type_descriptor(Gogo*, Named_type*);
void
do_reflection(Gogo*, std::string*) const;
void
do_mangled_name(Gogo*, std::string*, bool*) const;
void
do_export(Export*) const;
private:
// Create the placeholder during conversion.
void
create_placeholder(Gogo*);
// A pointer back to the Named_object for this type.
Named_object* named_object_;
// If this type is defined in a function, a pointer back to the
// function in which it is defined.
Named_object* in_function_;
// The index of this type in IN_FUNCTION_.
unsigned int in_function_index_;
// The actual type.
Type* type_;
// The list of methods defined for this type. Any named type can
// have methods.
Bindings* local_methods_;
// The full list of methods for this type, including methods
// declared for anonymous fields.
Methods* all_methods_;
// A mapping from interfaces to the associated interface method
// tables for this type.
Interface_method_tables* interface_method_tables_;
// A mapping from interfaces to the associated interface method
// tables for pointers to this type.
Interface_method_tables* pointer_interface_method_tables_;
// The location where this type was defined.
Location location_;
// The backend representation of this type during backend
// conversion. This is used to avoid endless recursion when a named
// type refers to itself.
Btype* named_btype_;
// A list of types which must be converted to the backend
// representation before this type can be converted. This is for
// cases like
// type S1 { p *S2 }
// type S2 { s S1 }
// where we can't convert S2 to the backend representation unless we
// have converted S1.
std::vector<Named_type*> dependencies_;
// Whether this is an alias type.
bool is_alias_;
// Whether this type is visible. This is false if this type was
// created because it was referenced by an imported object, but the
// type itself was not exported. This will always be true for types
// created in the current package.
bool is_visible_;
// Whether this type is erroneous.
bool is_error_;
// Whether this type is permitted in the heap. This is true by
// default, false if there is a magic //go:notinheap comment.
bool in_heap_;
// Whether the current value of named_btype_ is a placeholder for
// which the final size of the type is not known.
bool is_placeholder_;
// Whether this type has been converted to the backend
// representation. Implies that is_placeholder_ is false.
bool is_converted_;
// Whether this type has been verified.
bool is_verified_;
// In a recursive operation such as has_pointer, this flag is used
// to prevent infinite recursion when a type refers to itself. This
// is mutable because it is always reset to false when the function
// exits.
mutable bool seen_;
// Like seen_, but used only by do_compare_is_identity.
bool seen_in_compare_is_identity_;
// Like seen_, but used only by do_get_backend.
bool seen_in_get_backend_;
// Like seen_, but used when resolving aliases.
mutable bool seen_alias_;
};
// A forward declaration. This handles a type which has been declared
// but not defined.
class Forward_declaration_type : public Type
{
public:
Forward_declaration_type(Named_object* named_object);
// The named object associated with this type declaration. This
// will be resolved.
Named_object*
named_object();
const Named_object*
named_object() const;
// Return the name of the type.
const std::string&
name() const;
// Return the type to which this points. Give an error if the type
// has not yet been defined.
Type*
real_type();
const Type*
real_type() const;
// Whether the base type has been defined.
bool
is_defined() const;
// Add a method to this type.
Named_object*
add_method(const std::string& name, Function*);
// Add a method declaration to this type.
Named_object*
add_method_declaration(const std::string& name, Package*, Function_type*,
Location);
// Add an already created object as a method to this type.
void
add_existing_method(Named_object*);
protected:
int
do_traverse(Traverse* traverse);
bool
do_verify();
bool
do_has_pointer() const
{ return this->real_type()->has_pointer(); }
bool
do_compare_is_identity(Gogo* gogo)
{ return this->real_type()->compare_is_identity(gogo); }
bool
do_is_reflexive()
{ return this->real_type()->is_reflexive(); }
bool
do_needs_key_update()
{ return this->real_type()->needs_key_update(); }
bool
do_in_heap() const
{ return this->real_type()->in_heap(); }
unsigned int
do_hash_for_method(Gogo* gogo, int flags) const
{ return this->real_type()->hash_for_method(gogo, flags); }
Btype*
do_get_backend(Gogo* gogo);
Expression*
do_type_descriptor(Gogo*, Named_type*);
void
do_reflection(Gogo*, std::string*) const;
void
do_mangled_name(Gogo*, std::string*, bool*) const;
void
do_export(Export*) const;
private:
// Issue a warning about a use of an undefined type.
void
warn() const;
// The type declaration.
Named_object* named_object_;
// Whether we have issued a warning about this type.
mutable bool warned_;
};
// The Type_context struct describes what we expect for the type of an
// expression.
struct Type_context
{
// The exact type we expect, if known. This may be NULL.
Type* type;
// Whether an abstract type is permitted.
bool may_be_abstract;
// Constructors.
Type_context()
: type(NULL), may_be_abstract(false)
{ }
Type_context(Type* a_type, bool a_may_be_abstract)
: type(a_type), may_be_abstract(a_may_be_abstract)
{ }
};
#endif // !defined(GO_TYPES_H)
|