aboutsummaryrefslogtreecommitdiff
path: root/gcc/d/dmd/dsymbolsem.c
blob: 5d5c9fca7691b192c7050fd08935022c32cc65a5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486

/* Compiler implementation of the D programming language
 * Copyright (C) 1999-2021 by The D Language Foundation, All Rights Reserved
 * written by Walter Bright
 * http://www.digitalmars.com
 * Distributed under the Boost Software License, Version 1.0.
 * http://www.boost.org/LICENSE_1_0.txt
 */

#include "root/dsystem.h"
#include "root/aav.h"

#include "dsymbol.h"
#include "aggregate.h"
#include "aliasthis.h"
#include "attrib.h"
#include "cond.h"
#include "declaration.h"
#include "enum.h"
#include "errors.h"
#include "hdrgen.h"
#include "id.h"
#include "import.h"
#include "init.h"
#include "mars.h"
#include "module.h"
#include "nspace.h"
#include "objc.h"
#include "parse.h"
#include "scope.h"
#include "statement.h"
#include "staticassert.h"
#include "target.h"
#include "template.h"
#include "utf.h"
#include "version.h"
#include "visitor.h"

bool allowsContractWithoutBody(FuncDeclaration *funcdecl);
bool checkFrameAccess(Loc loc, Scope *sc, AggregateDeclaration *ad, size_t istart = 0);
VarDeclaration *copyToTemp(StorageClass stc, const char *name, Expression *e);
Initializer *inferType(Initializer *init, Scope *sc);
void MODtoBuffer(OutBuffer *buf, MOD mod);
bool reliesOnTident(Type *t, TemplateParameters *tparams = NULL, size_t iStart = 0);
Objc *objc();

static unsigned setMangleOverride(Dsymbol *s, char *sym)
{
    AttribDeclaration *ad = s->isAttribDeclaration();

    if (ad)
    {
        Dsymbols *decls = ad->include(NULL);
        unsigned nestedCount = 0;

        if (decls && decls->length)
            for (size_t i = 0; i < decls->length; ++i)
                nestedCount += setMangleOverride((*decls)[i], sym);

        return nestedCount;
    }
    else if (s->isFuncDeclaration() || s->isVarDeclaration())
    {
        s->isDeclaration()->mangleOverride = sym;
        return 1;
    }
    else
        return 0;
}

/**********************************
 * Decide if attributes for this function can be inferred from examining
 * the function body.
 * Returns:
 *  true if can
 */
static bool canInferAttributes(FuncDeclaration *fd, Scope *sc)
{
    if (!fd->fbody)
        return false;

    if (fd->isVirtualMethod())
        return false;               // since they may be overridden

    if (sc->func &&
        /********** this is for backwards compatibility for the moment ********/
        (!fd->isMember() || (sc->func->isSafeBypassingInference() && !fd->isInstantiated())))
        return true;

    if (fd->isFuncLiteralDeclaration() ||               // externs are not possible with literals
        (fd->storage_class & STCinference) ||           // do attribute inference
        (fd->inferRetType && !fd->isCtorDeclaration()))
        return true;

    if (fd->isInstantiated())
    {
        TemplateInstance *ti = fd->parent->isTemplateInstance();
        if (ti == NULL || ti->isTemplateMixin() || ti->tempdecl->ident == fd->ident)
            return true;
    }

    return false;
}

/*****************************************
 * Initialize for inferring the attributes of this function.
 */
static void initInferAttributes(FuncDeclaration *fd)
{
    //printf("initInferAttributes() for %s\n", toPrettyChars());
    TypeFunction *tf = fd->type->toTypeFunction();
    if (tf->purity == PUREimpure) // purity not specified
        fd->flags |= FUNCFLAGpurityInprocess;

    if (tf->trust == TRUSTdefault)
        fd->flags |= FUNCFLAGsafetyInprocess;

    if (!tf->isnothrow)
        fd->flags |= FUNCFLAGnothrowInprocess;

    if (!tf->isnogc)
        fd->flags |= FUNCFLAGnogcInprocess;

    if (!fd->isVirtual() || fd->introducing)
        fd->flags |= FUNCFLAGreturnInprocess;

    // Initialize for inferring STCscope
    if (global.params.vsafe)
        fd->flags |= FUNCFLAGinferScope;
}

static void badObjectDotD(ClassDeclaration *cd)
{
    cd->error("missing or corrupt object.d");
    fatal();
}

/* Bugzilla 12078, 12143 and 15733:
 * While resolving base classes and interfaces, a base may refer
 * the member of this derived class. In that time, if all bases of
 * this class can  be determined, we can go forward the semantc process
 * beyond the Lancestorsdone. To do the recursive semantic analysis,
 * temporarily set and unset `_scope` around exp().
 */
static Type *resolveBase(ClassDeclaration *cd, Scope *sc, Scope *&scx, Type *type)
{
    if (!scx)
    {
        scx = sc->copy();
        scx->setNoFree();
    }
    cd->_scope = scx;
    Type *t = typeSemantic(type, cd->loc, sc);
    cd->_scope = NULL;
    return t;
}

static void resolveBase(ClassDeclaration *cd, Scope *sc, Scope *&scx, ClassDeclaration *sym)
{
    if (!scx)
    {
        scx = sc->copy();
        scx->setNoFree();
    }
    cd->_scope = scx;
    dsymbolSemantic(sym, NULL);
    cd->_scope = NULL;
}

class DsymbolSemanticVisitor : public Visitor
{
public:
    Scope *sc;

    DsymbolSemanticVisitor(Scope *sc)
    {
        this->sc = sc;
    }

    void visit(Dsymbol *dsym)
    {
        dsym->error("%p has no semantic routine", dsym);
    }

    void visit(ScopeDsymbol *) { }
    void visit(Declaration *) { }

    void visit(AliasThis *dsym)
    {
        if (dsym->semanticRun != PASSinit)
            return;

        if (dsym->_scope)
        {
            sc = dsym->_scope;
            dsym->_scope = NULL;
        }

        if (!sc)
            return;

        dsym->semanticRun = PASSsemantic;

        Dsymbol *p = sc->parent->pastMixin();
        AggregateDeclaration *ad = p->isAggregateDeclaration();
        if (!ad)
        {
            error(dsym->loc, "alias this can only be a member of aggregate, not %s %s",
                p->kind(), p->toChars());
            return;
        }

        assert(ad->members);
        Dsymbol *s = ad->search(dsym->loc, dsym->ident);
        if (!s)
        {
            s = sc->search(dsym->loc, dsym->ident, NULL);
            if (s)
                error(dsym->loc, "%s is not a member of %s", s->toChars(), ad->toChars());
            else
                error(dsym->loc, "undefined identifier %s", dsym->ident->toChars());
            return;
        }
        else if (ad->aliasthis && s != ad->aliasthis)
        {
            error(dsym->loc, "there can be only one alias this");
            return;
        }

        if (ad->type->ty == Tstruct && ((TypeStruct *)ad->type)->sym != ad)
        {
            AggregateDeclaration *ad2 = ((TypeStruct *)ad->type)->sym;
            assert(ad2->type == Type::terror);
            ad->aliasthis = ad2->aliasthis;
            return;
        }

        /* disable the alias this conversion so the implicit conversion check
         * doesn't use it.
         */
        ad->aliasthis = NULL;

        Dsymbol *sx = s;
        if (sx->isAliasDeclaration())
            sx = sx->toAlias();
        Declaration *d = sx->isDeclaration();
        if (d && !d->isTupleDeclaration())
        {
            Type *t = d->type;
            assert(t);
            if (ad->type->implicitConvTo(t) > MATCHnomatch)
            {
                error(dsym->loc, "alias this is not reachable as %s already converts to %s", ad->toChars(), t->toChars());
            }
        }

        ad->aliasthis = s;
        dsym->semanticRun = PASSsemanticdone;
    }

    void visit(AliasDeclaration *dsym)
    {
        if (dsym->semanticRun >= PASSsemanticdone)
            return;
        assert(dsym->semanticRun <= PASSsemantic);

        dsym->storage_class |= sc->stc & STCdeprecated;
        dsym->protection = sc->protection;
        dsym->userAttribDecl = sc->userAttribDecl;

        if (!sc->func && dsym->inNonRoot())
            return;

        aliasSemantic(dsym, sc);
    }

    void visit(VarDeclaration *dsym)
    {
        //if (dsym->semanticRun > PASSinit)
        //    return;
        //dsym->semanticRun = PASSsemantic;

        if (dsym->semanticRun >= PASSsemanticdone)
            return;

        Scope *scx = NULL;
        if (dsym->_scope)
        {
            sc = dsym->_scope;
            scx = sc;
            dsym->_scope = NULL;
        }

        if (!sc)
            return;

        dsym->semanticRun = PASSsemantic;

        /* Pick up storage classes from context, but except synchronized,
         * override, abstract, and final.
         */
        dsym->storage_class |= (sc->stc & ~(STCsynchronized | STCoverride | STCabstract | STCfinal));
        if (dsym->storage_class & STCextern && dsym->_init)
            dsym->error("extern symbols cannot have initializers");

        dsym->userAttribDecl = sc->userAttribDecl;

        AggregateDeclaration *ad = dsym->isThis();
        if (ad)
            dsym->storage_class |= ad->storage_class & STC_TYPECTOR;

        /* If auto type inference, do the inference
         */
        int inferred = 0;
        if (!dsym->type)
        {
            dsym->inuse++;

            // Infering the type requires running semantic,
            // so mark the scope as ctfe if required
            bool needctfe = (dsym->storage_class & (STCmanifest | STCstatic)) != 0;
            if (needctfe) sc = sc->startCTFE();

            //printf("inferring type for %s with init %s\n", dsym->toChars(), dsym->_init->toChars());
            dsym->_init = inferType(dsym->_init, sc);
            dsym->type = initializerToExpression(dsym->_init)->type;

            if (needctfe) sc = sc->endCTFE();

            dsym->inuse--;
            inferred = 1;

            /* This is a kludge to support the existing syntax for RAII
             * declarations.
             */
            dsym->storage_class &= ~STCauto;
            dsym->originalType = dsym->type->syntaxCopy();
        }
        else
        {
            if (!dsym->originalType)
                dsym->originalType = dsym->type->syntaxCopy();

            /* Prefix function attributes of variable declaration can affect
             * its type:
             *      pure nothrow void function() fp;
             *      static assert(is(typeof(fp) == void function() pure nothrow));
             */
            Scope *sc2 = sc->push();
            sc2->stc |= (dsym->storage_class & STC_FUNCATTR);
            dsym->inuse++;
            dsym->type = typeSemantic(dsym->type, dsym->loc, sc2);
            dsym->inuse--;
            sc2->pop();
        }
        //printf(" semantic type = %s\n", dsym->type ? dsym->type->toChars() : "null");
        if (dsym->type->ty == Terror)
            dsym->errors = true;

        dsym->type->checkDeprecated(dsym->loc, sc);
        dsym->linkage = sc->linkage;
        dsym->parent = sc->parent;
        //printf("this = %p, parent = %p, '%s'\n", dsym, dsym->parent, dsym->parent->toChars());
        dsym->protection = sc->protection;

        /* If scope's alignment is the default, use the type's alignment,
         * otherwise the scope overrrides.
         */
        dsym->alignment = sc->alignment();
        if (dsym->alignment == STRUCTALIGN_DEFAULT)
            dsym->alignment = dsym->type->alignment();          // use type's alignment

        //printf("sc->stc = %x\n", sc->stc);
        //printf("storage_class = x%x\n", dsym->storage_class);

        if (global.params.vcomplex)
            dsym->type->checkComplexTransition(dsym->loc);

        // Calculate type size + safety checks
        if (sc->func && !sc->intypeof)
        {
            if ((dsym->storage_class & STCgshared) && !dsym->isMember())
            {
                if (sc->func->setUnsafe())
                    dsym->error("__gshared not allowed in safe functions; use shared");
            }
        }

        Dsymbol *parent = dsym->toParent();

        Type *tb = dsym->type->toBasetype();
        Type *tbn = tb->baseElemOf();
        if (tb->ty == Tvoid && !(dsym->storage_class & STClazy))
        {
            if (inferred)
            {
                dsym->error("type %s is inferred from initializer %s, and variables cannot be of type void",
                    dsym->type->toChars(), dsym->_init->toChars());
            }
            else
                dsym->error("variables cannot be of type void");
            dsym->type = Type::terror;
            tb = dsym->type;
        }
        if (tb->ty == Tfunction)
        {
            dsym->error("cannot be declared to be a function");
            dsym->type = Type::terror;
            tb = dsym->type;
        }
        if (tb->ty == Tstruct)
        {
            TypeStruct *ts = (TypeStruct *)tb;
            if (!ts->sym->members)
            {
                dsym->error("no definition of struct %s", ts->toChars());
            }
        }
        if ((dsym->storage_class & STCauto) && !inferred)
            dsym->error("storage class `auto` has no effect if type is not inferred, did you mean `scope`?");

        if (tb->ty == Ttuple)
        {
            /* Instead, declare variables for each of the tuple elements
             * and add those.
             */
            TypeTuple *tt = (TypeTuple *)tb;
            size_t nelems = Parameter::dim(tt->arguments);
            Expression *ie = (dsym->_init && !dsym->_init->isVoidInitializer()) ? initializerToExpression(dsym->_init) : NULL;
            if (ie)
                ie = expressionSemantic(ie, sc);

            if (nelems > 0 && ie)
            {
                Expressions *iexps = new Expressions();
                iexps->push(ie);

                Expressions *exps = new Expressions();

                for (size_t pos = 0; pos < iexps->length; pos++)
                {
                Lexpand1:
                    Expression *e = (*iexps)[pos];
                    Parameter *arg = Parameter::getNth(tt->arguments, pos);
                    arg->type = typeSemantic(arg->type, dsym->loc, sc);
                    //printf("[%d] iexps->length = %d, ", pos, iexps->length);
                    //printf("e = (%s %s, %s), ", Token::tochars[e->op], e->toChars(), e->type->toChars());
                    //printf("arg = (%s, %s)\n", arg->toChars(), arg->type->toChars());

                    if (e != ie)
                    {
                    if (iexps->length > nelems)
                        goto Lnomatch;
                    if (e->type->implicitConvTo(arg->type))
                        continue;
                    }

                    if (e->op == TOKtuple)
                    {
                        TupleExp *te = (TupleExp *)e;
                        if (iexps->length - 1 + te->exps->length > nelems)
                            goto Lnomatch;

                        iexps->remove(pos);
                        iexps->insert(pos, te->exps);
                        (*iexps)[pos] = Expression::combine(te->e0, (*iexps)[pos]);
                        goto Lexpand1;
                    }
                    else if (isAliasThisTuple(e))
                    {
                        VarDeclaration *v = copyToTemp(0, "__tup", e);
                        dsymbolSemantic(v, sc);
                        VarExp *ve = new VarExp(dsym->loc, v);
                        ve->type = e->type;

                        exps->setDim(1);
                        (*exps)[0] = ve;
                        expandAliasThisTuples(exps, 0);

                        for (size_t u = 0; u < exps->length ; u++)
                        {
                        Lexpand2:
                            Expression *ee = (*exps)[u];
                            arg = Parameter::getNth(tt->arguments, pos + u);
                            arg->type = typeSemantic(arg->type, dsym->loc, sc);
                            //printf("[%d+%d] exps->length = %d, ", pos, u, exps->length);
                            //printf("ee = (%s %s, %s), ", Token::tochars[ee->op], ee->toChars(), ee->type->toChars());
                            //printf("arg = (%s, %s)\n", arg->toChars(), arg->type->toChars());

                            size_t iexps_dim = iexps->length - 1 + exps->length;
                            if (iexps_dim > nelems)
                                goto Lnomatch;
                            if (ee->type->implicitConvTo(arg->type))
                                continue;

                            if (expandAliasThisTuples(exps, u) != -1)
                                goto Lexpand2;
                        }

                        if ((*exps)[0] != ve)
                        {
                            Expression *e0 = (*exps)[0];
                            (*exps)[0] = new CommaExp(dsym->loc, new DeclarationExp(dsym->loc, v), e0);
                            (*exps)[0]->type = e0->type;

                            iexps->remove(pos);
                            iexps->insert(pos, exps);
                            goto Lexpand1;
                        }
                    }
                }
                if (iexps->length < nelems)
                    goto Lnomatch;

                ie = new TupleExp(dsym->_init->loc, iexps);
            }
    Lnomatch:

            if (ie && ie->op == TOKtuple)
            {
                TupleExp *te = (TupleExp *)ie;
                size_t tedim = te->exps->length;
                if (tedim != nelems)
                {
                    error(dsym->loc, "tuple of %d elements cannot be assigned to tuple of %d elements", (int)tedim, (int)nelems);
                    for (size_t u = tedim; u < nelems; u++) // fill dummy expression
                        te->exps->push(new ErrorExp());
                }
            }

            Objects *exps = new Objects();
            exps->setDim(nelems);
            for (size_t i = 0; i < nelems; i++)
            {
                Parameter *arg = Parameter::getNth(tt->arguments, i);

                OutBuffer buf;
                buf.printf("__%s_field_%llu", dsym->ident->toChars(), (ulonglong)i);
                const char *name = buf.extractChars();
                Identifier *id = Identifier::idPool(name);

                Initializer *ti;
                if (ie)
                {
                    Expression *einit = ie;
                    if (ie->op == TOKtuple)
                    {
                        TupleExp *te = (TupleExp *)ie;
                        einit = (*te->exps)[i];
                        if (i == 0)
                            einit = Expression::combine(te->e0, einit);
                    }
                    ti = new ExpInitializer(einit->loc, einit);
                }
                else
                    ti = dsym->_init ? dsym->_init->syntaxCopy() : NULL;

                VarDeclaration *v = new VarDeclaration(dsym->loc, arg->type, id, ti);
                v->storage_class |= STCtemp | dsym->storage_class;
                if (arg->storageClass & STCparameter)
                    v->storage_class |= arg->storageClass;
                //printf("declaring field %s of type %s\n", v->toChars(), v->type->toChars());
                dsymbolSemantic(v, sc);

                if (sc->scopesym)
                {
                    //printf("adding %s to %s\n", v->toChars(), sc->scopesym->toChars());
                    if (sc->scopesym->members)
                        sc->scopesym->members->push(v);
                }

                Expression *e = new DsymbolExp(dsym->loc, v);
                (*exps)[i] = e;
            }
            TupleDeclaration *v2 = new TupleDeclaration(dsym->loc, dsym->ident, exps);
            v2->parent = dsym->parent;
            v2->isexp = true;
            dsym->aliassym = v2;
            dsym->semanticRun = PASSsemanticdone;
            return;
        }

        /* Storage class can modify the type
         */
        dsym->type = dsym->type->addStorageClass(dsym->storage_class);

        /* Adjust storage class to reflect type
         */
        if (dsym->type->isConst())
        {
            dsym->storage_class |= STCconst;
            if (dsym->type->isShared())
                dsym->storage_class |= STCshared;
        }
        else if (dsym->type->isImmutable())
            dsym->storage_class |= STCimmutable;
        else if (dsym->type->isShared())
            dsym->storage_class |= STCshared;
        else if (dsym->type->isWild())
            dsym->storage_class |= STCwild;

        if (StorageClass stc = dsym->storage_class & (STCsynchronized | STCoverride | STCabstract | STCfinal))
        {
            if (stc == STCfinal)
                dsym->error("cannot be final, perhaps you meant const?");
            else
            {
                OutBuffer buf;
                stcToBuffer(&buf, stc);
                dsym->error("cannot be %s", buf.peekChars());
            }
            dsym->storage_class &= ~stc;  // strip off
        }

        if (dsym->storage_class & STCscope)
        {
            StorageClass stc = dsym->storage_class & (STCstatic | STCextern | STCmanifest | STCtls | STCgshared);
            if (stc)
            {
                OutBuffer buf;
                stcToBuffer(&buf, stc);
                dsym->error("cannot be `scope` and `%s`", buf.peekChars());
            }
            else if (dsym->isMember())
            {
                dsym->error("field cannot be `scope`");
            }
            else if (!dsym->type->hasPointers())
            {
                dsym->storage_class &= ~STCscope;     // silently ignore; may occur in generic code
            }
        }

        if (dsym->storage_class & (STCstatic | STCextern | STCmanifest | STCtemplateparameter | STCtls | STCgshared | STCctfe))
        {
        }
        else
        {
            AggregateDeclaration *aad = parent->isAggregateDeclaration();
            if (aad)
            {
                if (global.params.vfield &&
                    dsym->storage_class & (STCconst | STCimmutable) && dsym->_init && !dsym->_init->isVoidInitializer())
                {
                    const char *s = (dsym->storage_class & STCimmutable) ? "immutable" : "const";
                    message(dsym->loc, "`%s.%s` is `%s` field", ad->toPrettyChars(), dsym->toChars(), s);
                }
                dsym->storage_class |= STCfield;
                if (tbn->ty == Tstruct && ((TypeStruct *)tbn)->sym->noDefaultCtor)
                {
                    if (!dsym->isThisDeclaration() && !dsym->_init)
                        aad->noDefaultCtor = true;
                }
            }

            InterfaceDeclaration *id = parent->isInterfaceDeclaration();
            if (id)
            {
                dsym->error("field not allowed in interface");
            }
            else if (aad && aad->sizeok == SIZEOKdone)
            {
                dsym->error("cannot be further field because it will change the determined %s size", aad->toChars());
            }

            /* Templates cannot add fields to aggregates
             */
            TemplateInstance *ti = parent->isTemplateInstance();
            if (ti)
            {
                // Take care of nested templates
                while (1)
                {
                    TemplateInstance *ti2 = ti->tempdecl->parent->isTemplateInstance();
                    if (!ti2)
                        break;
                    ti = ti2;
                }

                // If it's a member template
                AggregateDeclaration *ad2 = ti->tempdecl->isMember();
                if (ad2 && dsym->storage_class != STCundefined)
                {
                    dsym->error("cannot use template to add field to aggregate `%s`", ad2->toChars());
                }
            }
        }

        if ((dsym->storage_class & (STCref | STCparameter | STCforeach | STCtemp | STCresult)) == STCref && dsym->ident != Id::This)
        {
            dsym->error("only parameters or foreach declarations can be ref");
        }

        if (dsym->type->hasWild())
        {
            if (dsym->storage_class & (STCstatic | STCextern | STCtls | STCgshared | STCmanifest | STCfield) ||
                dsym->isDataseg()
                )
            {
                dsym->error("only parameters or stack based variables can be inout");
            }
            FuncDeclaration *func = sc->func;
            if (func)
            {
                if (func->fes)
                    func = func->fes->func;
                bool isWild = false;
                for (FuncDeclaration *fd = func; fd; fd = fd->toParent2()->isFuncDeclaration())
                {
                    if (((TypeFunction *)fd->type)->iswild)
                    {
                        isWild = true;
                        break;
                    }
                }
                if (!isWild)
                {
                    dsym->error("inout variables can only be declared inside inout functions");
                }
            }
        }

        if (!(dsym->storage_class & (STCctfe | STCref | STCresult)) && tbn->ty == Tstruct &&
            ((TypeStruct *)tbn)->sym->noDefaultCtor)
        {
            if (!dsym->_init)
            {
                if (dsym->isField())
                {
                    /* For fields, we'll check the constructor later to make sure it is initialized
                     */
                    dsym->storage_class |= STCnodefaultctor;
                }
                else if (dsym->storage_class & STCparameter)
                    ;
                else
                    dsym->error("default construction is disabled for type %s", dsym->type->toChars());
            }
        }

        FuncDeclaration *fd = parent->isFuncDeclaration();
        if (dsym->type->isscope() && !(dsym->storage_class & STCnodtor))
        {
            if (dsym->storage_class & (STCfield | STCout | STCref | STCstatic | STCmanifest | STCtls | STCgshared) || !fd)
            {
                dsym->error("globals, statics, fields, manifest constants, ref and out parameters cannot be scope");
            }

            if (!(dsym->storage_class & STCscope))
            {
                if (!(dsym->storage_class & STCparameter) && dsym->ident != Id::withSym)
                    dsym->error("reference to scope class must be scope");
            }
        }

        // Calculate type size + safety checks
        if (sc->func && !sc->intypeof)
        {
            if (dsym->_init && dsym->_init->isVoidInitializer() && dsym->type->hasPointers()) // get type size
            {
                if (sc->func->setUnsafe())
                    dsym->error("void initializers for pointers not allowed in safe functions");
            }
            else if (!dsym->_init &&
                     !(dsym->storage_class & (STCstatic | STCextern | STCtls | STCgshared | STCmanifest | STCfield | STCparameter)) &&
                     dsym->type->hasVoidInitPointers())
            {
                if (sc->func->setUnsafe())
                    dsym->error("void initializers for pointers not allowed in safe functions");
            }
        }

        if (!dsym->_init && !fd)
        {
            // If not mutable, initializable by constructor only
            dsym->storage_class |= STCctorinit;
        }

        if (dsym->_init)
            dsym->storage_class |= STCinit;     // remember we had an explicit initializer
        else if (dsym->storage_class & STCmanifest)
            dsym->error("manifest constants must have initializers");

        bool isBlit = false;
        d_uns64 sz = 0;
        if (!dsym->_init && !sc->inunion && !(dsym->storage_class & (STCstatic | STCgshared | STCextern)) && fd &&
            (!(dsym->storage_class & (STCfield | STCin | STCforeach | STCparameter | STCresult))
             || (dsym->storage_class & STCout)) &&
            (sz = dsym->type->size()) != 0)
        {
            // Provide a default initializer
            //printf("Providing default initializer for '%s'\n", dsym->toChars());
            if (sz == SIZE_INVALID && dsym->type->ty != Terror)
                dsym->error("size of type %s is invalid", dsym->type->toChars());

            Type *tv = dsym->type;
            while (tv->ty == Tsarray)    // Don't skip Tenum
                tv = tv->nextOf();
            if (tv->needsNested())
            {
                /* Nested struct requires valid enclosing frame pointer.
                 * In StructLiteralExp::toElem(), it's calculated.
                 */
                assert(tv->toBasetype()->ty == Tstruct);
                checkFrameAccess(dsym->loc, sc, ((TypeStruct *)tbn)->sym);

                Expression *e = tv->defaultInitLiteral(dsym->loc);
                e = new BlitExp(dsym->loc, new VarExp(dsym->loc, dsym), e);
                e = expressionSemantic(e, sc);
                dsym->_init = new ExpInitializer(dsym->loc, e);
                goto Ldtor;
            }
            if (tv->ty == Tstruct && ((TypeStruct *)tv)->sym->zeroInit == 1)
            {
                /* If a struct is all zeros, as a special case
                 * set it's initializer to the integer 0.
                 * In AssignExp::toElem(), we check for this and issue
                 * a memset() to initialize the struct.
                 * Must do same check in interpreter.
                 */
                Expression *e = new IntegerExp(dsym->loc, 0, Type::tint32);
                e = new BlitExp(dsym->loc, new VarExp(dsym->loc, dsym), e);
                e->type = dsym->type;         // don't type check this, it would fail
                dsym->_init = new ExpInitializer(dsym->loc, e);
                goto Ldtor;
            }
            if (dsym->type->baseElemOf()->ty == Tvoid)
            {
                dsym->error("%s does not have a default initializer", dsym->type->toChars());
            }
            else if (Expression *e = dsym->type->defaultInit(dsym->loc))
            {
                dsym->_init = new ExpInitializer(dsym->loc, e);
            }
            // Default initializer is always a blit
            isBlit = true;
        }

        if (dsym->_init)
        {
            sc = sc->push();
            sc->stc &= ~(STC_TYPECTOR | STCpure | STCnothrow | STCnogc | STCref | STCdisable);

            ExpInitializer *ei = dsym->_init->isExpInitializer();
            if (ei)     // Bugzilla 13424: Preset the required type to fail in FuncLiteralDeclaration::semantic3
                ei->exp = inferType(ei->exp, dsym->type);

            // If inside function, there is no semantic3() call
            if (sc->func || sc->intypeof == 1)
            {
                // If local variable, use AssignExp to handle all the various
                // possibilities.
                if (fd &&
                    !(dsym->storage_class & (STCmanifest | STCstatic | STCtls | STCgshared | STCextern)) &&
                    !dsym->_init->isVoidInitializer())
                {
                    //printf("fd = '%s', var = '%s'\n", fd->toChars(), dsym->toChars());
                    if (!ei)
                    {
                        ArrayInitializer *ai = dsym->_init->isArrayInitializer();
                        Expression *e;
                        if (ai && tb->ty == Taarray)
                            e = ai->toAssocArrayLiteral();
                        else
                            e = initializerToExpression(dsym->_init);
                        if (!e)
                        {
                            // Run semantic, but don't need to interpret
                            dsym->_init = initializerSemantic(dsym->_init, sc, dsym->type, INITnointerpret);
                            e = initializerToExpression(dsym->_init);
                            if (!e)
                            {
                                dsym->error("is not a static and cannot have static initializer");
                                e = new ErrorExp();
                            }
                        }
                        ei = new ExpInitializer(dsym->_init->loc, e);
                        dsym->_init = ei;
                    }

                    Expression *exp = ei->exp;
                    Expression *e1 = new VarExp(dsym->loc, dsym);
                    if (isBlit)
                        exp = new BlitExp(dsym->loc, e1, exp);
                    else
                        exp = new ConstructExp(dsym->loc, e1, exp);
                    dsym->canassign++;
                    exp = expressionSemantic(exp, sc);
                    dsym->canassign--;
                    exp = exp->optimize(WANTvalue);

                    if (exp->op == TOKerror)
                    {
                        dsym->_init = new ErrorInitializer();
                        ei = NULL;
                    }
                    else
                        ei->exp = exp;

                    if (ei && dsym->isScope())
                    {
                        Expression *ex = ei->exp;
                        while (ex->op == TOKcomma)
                            ex = ((CommaExp *)ex)->e2;
                        if (ex->op == TOKblit || ex->op == TOKconstruct)
                            ex = ((AssignExp *)ex)->e2;
                        if (ex->op == TOKnew)
                        {
                            // See if initializer is a NewExp that can be allocated on the stack
                            NewExp *ne = (NewExp *)ex;
                            if (dsym->type->toBasetype()->ty == Tclass)
                            {
                                if (ne->newargs && ne->newargs->length > 1)
                                {
                                    dsym->mynew = true;
                                }
                                else
                                {
                                    ne->onstack = true;
                                    dsym->onstack = true;
                                }
                            }
                        }
                        else if (ex->op == TOKfunction)
                        {
                            // or a delegate that doesn't escape a reference to the function
                            FuncDeclaration *f = ((FuncExp *)ex)->fd;
                            f->tookAddressOf--;
                        }
                    }
                }
                else
                {
                    // Bugzilla 14166: Don't run CTFE for the temporary variables inside typeof
                    dsym->_init = initializerSemantic(dsym->_init, sc, dsym->type, sc->intypeof == 1 ? INITnointerpret : INITinterpret);
                }
            }
            else if (parent->isAggregateDeclaration())
            {
                dsym->_scope = scx ? scx : sc->copy();
                dsym->_scope->setNoFree();
            }
            else if (dsym->storage_class & (STCconst | STCimmutable | STCmanifest) ||
                     dsym->type->isConst() || dsym->type->isImmutable())
            {
                /* Because we may need the results of a const declaration in a
                 * subsequent type, such as an array dimension, before semantic2()
                 * gets ordinarily run, try to run semantic2() now.
                 * Ignore failure.
                 */

                if (!inferred)
                {
                    unsigned errors = global.errors;
                    dsym->inuse++;
                    if (ei)
                    {
                        Expression *exp = ei->exp->syntaxCopy();

                        bool needctfe = dsym->isDataseg() || (dsym->storage_class & STCmanifest);
                        if (needctfe) sc = sc->startCTFE();
                        exp = expressionSemantic(exp, sc);
                        exp = resolveProperties(sc, exp);
                        if (needctfe) sc = sc->endCTFE();

                        Type *tb2 = dsym->type->toBasetype();
                        Type *ti = exp->type->toBasetype();

                        /* The problem is the following code:
                         *  struct CopyTest {
                         *     double x;
                         *     this(double a) { x = a * 10.0;}
                         *     this(this) { x += 2.0; }
                         *  }
                         *  const CopyTest z = CopyTest(5.3);  // ok
                         *  const CopyTest w = z;              // not ok, postblit not run
                         *  static assert(w.x == 55.0);
                         * because the postblit doesn't get run on the initialization of w.
                         */
                        if (ti->ty == Tstruct)
                        {
                            StructDeclaration *sd = ((TypeStruct *)ti)->sym;
                            /* Look to see if initializer involves a copy constructor
                             * (which implies a postblit)
                             */
                             // there is a copy constructor
                             // and exp is the same struct
                            if (sd->postblit &&
                                tb2->toDsymbol(NULL) == sd)
                            {
                                // The only allowable initializer is a (non-copy) constructor
                                if (exp->isLvalue())
                                    dsym->error("of type struct %s uses this(this), which is not allowed in static initialization", tb2->toChars());
                            }
                        }
                        ei->exp = exp;
                    }
                    dsym->_init = initializerSemantic(dsym->_init, sc, dsym->type, INITinterpret);
                    dsym->inuse--;
                    if (global.errors > errors)
                    {
                        dsym->_init = new ErrorInitializer();
                        dsym->type = Type::terror;
                    }
                }
                else
                {
                    dsym->_scope = scx ? scx : sc->copy();
                    dsym->_scope->setNoFree();
                }
            }
            sc = sc->pop();
        }

    Ldtor:
        /* Build code to execute destruction, if necessary
         */
        dsym->edtor = dsym->callScopeDtor(sc);
        if (dsym->edtor)
        {
            if (sc->func && dsym->storage_class & (STCstatic | STCgshared))
                dsym->edtor = expressionSemantic(dsym->edtor, sc->_module->_scope);
            else
                dsym->edtor = expressionSemantic(dsym->edtor, sc);

    #if 0 // currently disabled because of std.stdio.stdin, stdout and stderr
            if (dsym->isDataseg() && !(dsym->storage_class & STCextern))
                dsym->error("static storage variables cannot have destructors");
    #endif
        }

        dsym->semanticRun = PASSsemanticdone;

        if (dsym->type->toBasetype()->ty == Terror)
            dsym->errors = true;

        if (sc->scopesym && !sc->scopesym->isAggregateDeclaration())
        {
            for (ScopeDsymbol *sym = sc->scopesym; sym && dsym->endlinnum == 0;
                 sym = sym->parent ? sym->parent->isScopeDsymbol() : NULL)
                dsym->endlinnum = sym->endlinnum;
        }
    }

    void visit(TypeInfoDeclaration *dsym)
    {
        assert(dsym->linkage == LINKc);
    }

    void visit(Import *imp)
    {
        //printf("Import::semantic('%s') %s\n", toPrettyChars(), imp->id->toChars());
        if (imp->semanticRun > PASSinit)
            return;

        if (imp->_scope)
        {
            sc = imp->_scope;
            imp->_scope = NULL;
        }
        if (!sc)
            return;

        imp->semanticRun = PASSsemantic;

        // Load if not already done so
        if (!imp->mod)
        {
            imp->load(sc);
            if (imp->mod)
                imp->mod->importAll(NULL);
        }

        if (imp->mod)
        {
            // Modules need a list of each imported module
            //printf("%s imports %s\n", sc->_module->toChars(), imp->mod->toChars());
            sc->_module->aimports.push(imp->mod);

            if (sc->explicitProtection)
                imp->protection = sc->protection;

            if (!imp->aliasId && !imp->names.length) // neither a selective nor a renamed import
            {
                ScopeDsymbol *scopesym = NULL;
                if (sc->explicitProtection)
                    imp->protection = sc->protection.kind;
                for (Scope *scd = sc; scd; scd = scd->enclosing)
                {
                    if (!scd->scopesym)
                        continue;
                    scopesym = scd->scopesym;
                    break;
                }

                if (!imp->isstatic)
                {
                    scopesym->importScope(imp->mod, imp->protection);
                }

                // Mark the imported packages as accessible from the current
                // scope. This access check is necessary when using FQN b/c
                // we're using a single global package tree. See Bugzilla 313.
                if (imp->packages)
                {
                    // import a.b.c.d;
                    Package *p = imp->pkg; // a
                    scopesym->addAccessiblePackage(p, imp->protection);
                    for (size_t i = 1; i < imp->packages->length; i++) // [b, c]
                    {
                        Identifier *id = (*imp->packages)[i];
                        p = (Package *) p->symtab->lookup(id);
                        scopesym->addAccessiblePackage(p, imp->protection);
                    }
                }
                scopesym->addAccessiblePackage(imp->mod, imp->protection); // d
            }

            dsymbolSemantic(imp->mod, NULL);

            if (imp->mod->needmoduleinfo)
            {
                //printf("module4 %s because of %s\n", sc->_module->toChars(), imp->mod->toChars());
                sc->_module->needmoduleinfo = 1;
            }

            sc = sc->push(imp->mod);
            sc->protection = imp->protection;
            for (size_t i = 0; i < imp->aliasdecls.length; i++)
            {
                AliasDeclaration *ad = imp->aliasdecls[i];
                //printf("\tImport %s alias %s = %s, scope = %p\n", toPrettyChars(), imp->aliases[i]->toChars(), imp->names[i]->toChars(), ad->_scope);
                if (imp->mod->search(imp->loc, imp->names[i]))
                {
                    dsymbolSemantic(ad, sc);
                    // If the import declaration is in non-root module,
                    // analysis of the aliased symbol is deferred.
                    // Therefore, don't see the ad->aliassym or ad->type here.
                }
                else
                {
                    Dsymbol *s = imp->mod->search_correct(imp->names[i]);
                    if (s)
                        imp->mod->error(imp->loc, "import `%s` not found, did you mean %s `%s`?", imp->names[i]->toChars(), s->kind(), s->toChars());
                    else
                        imp->mod->error(imp->loc, "import `%s` not found", imp->names[i]->toChars());
                    ad->type = Type::terror;
                }
            }
            sc = sc->pop();
        }

        imp->semanticRun = PASSsemanticdone;

        // object self-imports itself, so skip that (Bugzilla 7547)
        // don't list pseudo modules __entrypoint.d, __main.d (Bugzilla 11117, 11164)
        if (global.params.moduleDeps != NULL &&
            !(imp->id == Id::object && sc->_module->ident == Id::object) &&
            sc->_module->ident != Id::entrypoint &&
            strcmp(sc->_module->ident->toChars(), "__main") != 0)
        {
            /* The grammar of the file is:
             *      ImportDeclaration
             *          ::= BasicImportDeclaration [ " : " ImportBindList ] [ " -> "
             *      ModuleAliasIdentifier ] "\n"
             *
             *      BasicImportDeclaration
             *          ::= ModuleFullyQualifiedName " (" FilePath ") : " Protection|"string"
             *              " [ " static" ] : " ModuleFullyQualifiedName " (" FilePath ")"
             *
             *      FilePath
             *          - any string with '(', ')' and '\' escaped with the '\' character
             */

            OutBuffer *ob = global.params.moduleDeps;
            Module* imod = sc->instantiatingModule();
            if (!global.params.moduleDepsFile.length)
                ob->writestring("depsImport ");
            ob->writestring(imod->toPrettyChars());
            ob->writestring(" (");
            escapePath(ob,  imod->srcfile->toChars());
            ob->writestring(") : ");

            // use protection instead of sc->protection because it couldn't be
            // resolved yet, see the comment above
            protectionToBuffer(ob, imp->protection);
            ob->writeByte(' ');
            if (imp->isstatic)
            {
                stcToBuffer(ob, STCstatic);
                ob->writeByte(' ');
            }
            ob->writestring(": ");

            if (imp->packages)
            {
                for (size_t i = 0; i < imp->packages->length; i++)
                {
                    Identifier *pid = (*imp->packages)[i];
                    ob->printf("%s.", pid->toChars());
                }
            }

            ob->writestring(imp->id->toChars());
            ob->writestring(" (");
            if (imp->mod)
                escapePath(ob, imp->mod->srcfile->toChars());
            else
                ob->writestring("???");
            ob->writeByte(')');

            for (size_t i = 0; i < imp->names.length; i++)
            {
                if (i == 0)
                    ob->writeByte(':');
                else
                    ob->writeByte(',');

                Identifier *name = imp->names[i];
                Identifier *alias = imp->aliases[i];

                if (!alias)
                {
                    ob->printf("%s", name->toChars());
                    alias = name;
                }
                else
                    ob->printf("%s=%s", alias->toChars(), name->toChars());
            }

            if (imp->aliasId)
                    ob->printf(" -> %s", imp->aliasId->toChars());

            ob->writenl();
        }

        //printf("-Import::semantic('%s'), pkg = %p\n", imp->toChars(), imp->pkg);
    }

    void attribSemantic(AttribDeclaration *ad)
    {
        if (ad->semanticRun != PASSinit)
            return;
        ad->semanticRun = PASSsemantic;
        Dsymbols *d = ad->include(sc);
        //printf("\tAttribDeclaration::semantic '%s', d = %p\n",toChars(), d);
        if (d)
        {
            Scope *sc2 = ad->newScope(sc);
            bool errors = false;
            for (size_t i = 0; i < d->length; i++)
            {
                Dsymbol *s = (*d)[i];
                dsymbolSemantic(s, sc2);
                errors |= s->errors;
            }
            ad->errors |= errors;
            if (sc2 != sc)
                sc2->pop();
        }
        ad->semanticRun = PASSsemanticdone;
    }

    void visit(AttribDeclaration *atd)
    {
        attribSemantic(atd);
    }

    void visit(AnonDeclaration *scd)
    {
        //printf("\tAnonDeclaration::semantic %s %p\n", isunion ? "union" : "struct", scd);
        assert(sc->parent);
        Dsymbol *p = sc->parent->pastMixin();
        AggregateDeclaration *ad = p->isAggregateDeclaration();
        if (!ad)
        {
            error(scd->loc, "%s can only be a part of an aggregate, not %s %s",
                scd->kind(), p->kind(), p->toChars());
            scd->errors = true;
            return;
        }

        if (scd->decl)
        {
            sc = sc->push();
            sc->stc &= ~(STCauto | STCscope | STCstatic | STCtls | STCgshared);
            sc->inunion = scd->isunion;
            sc->flags = 0;

            for (size_t i = 0; i < scd->decl->length; i++)
            {
                Dsymbol *s = (*scd->decl)[i];
                dsymbolSemantic(s, sc);
            }
            sc = sc->pop();
        }
    }

    void visit(PragmaDeclaration *pd)
    {
        // Should be merged with PragmaStatement
        //printf("\tPragmaDeclaration::semantic '%s'\n",toChars());
        if (pd->ident == Id::msg)
        {
            if (pd->args)
            {
                for (size_t i = 0; i < pd->args->length; i++)
                {
                    Expression *e = (*pd->args)[i];

                    sc = sc->startCTFE();
                    e = expressionSemantic(e, sc);
                    e = resolveProperties(sc, e);
                    sc = sc->endCTFE();

                    // pragma(msg) is allowed to contain types as well as expressions
                    e = ctfeInterpretForPragmaMsg(e);
                    if (e->op == TOKerror)
                    {
                        errorSupplemental(pd->loc, "while evaluating pragma(msg, %s)", (*pd->args)[i]->toChars());
                        return;
                    }
                    StringExp *se = e->toStringExp();
                    if (se)
                    {
                        se = se->toUTF8(sc);
                        fprintf(stderr, "%.*s", (int)se->len, (char *)se->string);
                    }
                    else
                        fprintf(stderr, "%s", e->toChars());
                }
                fprintf(stderr, "\n");
            }
            goto Lnodecl;
        }
        else if (pd->ident == Id::lib)
        {
            if (!pd->args || pd->args->length != 1)
                pd->error("string expected for library name");
            else
            {
                StringExp *se = semanticString(sc, (*pd->args)[0], "library name");
                if (!se)
                    goto Lnodecl;
                (*pd->args)[0] = se;

                char *name = (char *)mem.xmalloc(se->len + 1);
                memcpy(name, se->string, se->len);
                name[se->len] = 0;
                if (global.params.verbose)
                    message("library   %s", name);
                if (global.params.moduleDeps && !global.params.moduleDepsFile.length)
                {
                    OutBuffer *ob = global.params.moduleDeps;
                    Module *imod = sc->instantiatingModule();
                    ob->writestring("depsLib ");
                    ob->writestring(imod->toPrettyChars());
                    ob->writestring(" (");
                    escapePath(ob, imod->srcfile->toChars());
                    ob->writestring(") : ");
                    ob->writestring((char *) name);
                    ob->writenl();
                }
                mem.xfree(name);
            }
            goto Lnodecl;
        }
        else if (pd->ident == Id::startaddress)
        {
            if (!pd->args || pd->args->length != 1)
                pd->error("function name expected for start address");
            else
            {
                /* Bugzilla 11980:
                 * resolveProperties and ctfeInterpret call are not necessary.
                 */
                Expression *e = (*pd->args)[0];

                sc = sc->startCTFE();
                e = expressionSemantic(e, sc);
                sc = sc->endCTFE();

                (*pd->args)[0] = e;
                Dsymbol *sa = getDsymbol(e);
                if (!sa || !sa->isFuncDeclaration())
                    pd->error("function name expected for start address, not `%s`", e->toChars());
            }
            goto Lnodecl;
        }
        else if (pd->ident == Id::Pinline)
        {
            goto Ldecl;
        }
        else if (pd->ident == Id::mangle)
        {
            if (!pd->args)
                pd->args = new Expressions();
            if (pd->args->length != 1)
            {
                pd->error("string expected for mangled name");
                pd->args->setDim(1);
                (*pd->args)[0] = new ErrorExp();    // error recovery
                goto Ldecl;
            }

            StringExp *se = semanticString(sc, (*pd->args)[0], "mangled name");
            if (!se)
                goto Ldecl;
            (*pd->args)[0] = se; // Will be used for later

            if (!se->len)
            {
                pd->error("zero-length string not allowed for mangled name");
                goto Ldecl;
            }
            if (se->sz != 1)
            {
                pd->error("mangled name characters can only be of type char");
                goto Ldecl;
            }

            /* Note: D language specification should not have any assumption about backend
             * implementation. Ideally pragma(mangle) can accept a string of any content.
             *
             * Therefore, this validation is compiler implementation specific.
             */
            for (size_t i = 0; i < se->len; )
            {
                utf8_t *p = (utf8_t *)se->string;
                dchar_t c = p[i];
                if (c < 0x80)
                {
                    if ((c >= 'A' && c <= 'Z') ||
                        (c >= 'a' && c <= 'z') ||
                        (c >= '0' && c <= '9') ||
                        (c != 0 && strchr("$%().:?@[]_", c)))
                    {
                        ++i;
                        continue;
                    }
                    else
                    {
                        pd->error("char 0x%02x not allowed in mangled name", c);
                        break;
                    }
                }

                if (const char* msg = utf_decodeChar((utf8_t *)se->string, se->len, &i, &c))
                {
                    pd->error("%s", msg);
                    break;
                }

                if (!isUniAlpha(c))
                {
                    pd->error("char 0x%04x not allowed in mangled name", c);
                    break;
                }
            }
        }
        else if (global.params.ignoreUnsupportedPragmas)
        {
            if (global.params.verbose)
            {
                /* Print unrecognized pragmas
                 */
                OutBuffer buf;
                buf.writestring(pd->ident->toChars());
                if (pd->args)
                {
                    for (size_t i = 0; i < pd->args->length; i++)
                    {
                        Expression *e = (*pd->args)[i];

                        sc = sc->startCTFE();
                        e = expressionSemantic(e, sc);
                        e = resolveProperties(sc, e);
                        sc = sc->endCTFE();

                        e = e->ctfeInterpret();
                        if (i == 0)
                            buf.writestring(" (");
                        else
                            buf.writeByte(',');
                        buf.writestring(e->toChars());
                    }
                    if (pd->args->length)
                        buf.writeByte(')');
                }
                message("pragma    %s", buf.peekChars());
            }
            goto Lnodecl;
        }
        else
            error(pd->loc, "unrecognized pragma(%s)", pd->ident->toChars());

    Ldecl:
        if (pd->decl)
        {
            Scope *sc2 = pd->newScope(sc);

            for (size_t i = 0; i < pd->decl->length; i++)
            {
                Dsymbol *s = (*pd->decl)[i];

                dsymbolSemantic(s, sc2);

                if (pd->ident == Id::mangle)
                {
                    assert(pd->args && pd->args->length == 1);
                    if (StringExp *se = (*pd->args)[0]->toStringExp())
                    {
                        char *name = (char *)mem.xmalloc(se->len + 1);
                        memcpy(name, se->string, se->len);
                        name[se->len] = 0;

                        unsigned cnt = setMangleOverride(s, name);
                        if (cnt > 1)
                            pd->error("can only apply to a single declaration");
                    }
                }
            }

            if (sc2 != sc)
                sc2->pop();
        }
        return;

    Lnodecl:
        if (pd->decl)
        {
            pd->error("pragma is missing closing `;`");
            goto Ldecl; // do them anyway, to avoid segfaults.
        }
    }

    void visit(StaticIfDeclaration *sid)
    {
        attribSemantic(sid);
    }

    void visit(StaticForeachDeclaration *sfd)
    {
        attribSemantic(sfd);
    }

    Dsymbols *compileIt(CompileDeclaration *cd)
    {
        //printf("CompileDeclaration::compileIt(loc = %d) %s\n", cd->loc.linnum, cd->exp->toChars());
        StringExp *se = semanticString(sc, cd->exp, "argument to mixin");
        if (!se)
            return NULL;
        se = se->toUTF8(sc);

        unsigned errors = global.errors;
        Parser p(cd->loc, sc->_module, (utf8_t *)se->string, se->len, 0);
        p.nextToken();

        Dsymbols *d = p.parseDeclDefs(0);
        if (global.errors != errors)
            return NULL;

        if (p.token.value != TOKeof)
        {
            cd->exp->error("incomplete mixin declaration (%s)", se->toChars());
            return NULL;
        }
        return d;
    }

    void visit(CompileDeclaration *cd)
    {
        //printf("CompileDeclaration::semantic()\n");
        if (!cd->compiled)
        {
            cd->decl = compileIt(cd);
            cd->AttribDeclaration::addMember(sc, cd->scopesym);
            cd->compiled = true;

            if (cd->_scope && cd->decl)
            {
                for (size_t i = 0; i < cd->decl->length; i++)
                {
                    Dsymbol *s = (*cd->decl)[i];
                    s->setScope(cd->_scope);
                }
            }
        }
        attribSemantic(cd);
    }

    void visit(UserAttributeDeclaration *uad)
    {
        //printf("UserAttributeDeclaration::semantic() %p\n", this);
        if (uad->decl && !uad->_scope)
            uad->Dsymbol::setScope(sc);  // for function local symbols

        attribSemantic(uad);
    }

    void visit(StaticAssert *sa)
    {
            if (sa->semanticRun < PASSsemanticdone)
                sa->semanticRun = PASSsemanticdone;
    }

    void visit(DebugSymbol *ds)
    {
        //printf("DebugSymbol::semantic() %s\n", ds->toChars());
        if (ds->semanticRun < PASSsemanticdone)
            ds->semanticRun = PASSsemanticdone;
    }

    void visit(VersionSymbol *vs)
    {
        if (vs->semanticRun < PASSsemanticdone)
            vs->semanticRun = PASSsemanticdone;
    }

    void visit(Package *pkg)
    {
        if (pkg->semanticRun < PASSsemanticdone)
            pkg->semanticRun = PASSsemanticdone;
    }

    void visit(Module *m)
    {
        if (m->semanticRun != PASSinit)
            return;

        //printf("+Module::semantic(this = %p, '%s'): parent = %p\n", this, m->toChars(), parent);
        m->semanticRun = PASSsemantic;

        // Note that modules get their own scope, from scratch.
        // This is so regardless of where in the syntax a module
        // gets imported, it is unaffected by context.
        Scope *sc = m->_scope;                  // see if already got one from importAll()
        if (!sc)
        {
            Scope::createGlobal(m);      // create root scope
        }

        //printf("Module = %p, linkage = %d\n", sc->scopesym, sc->linkage);

        // Pass 1 semantic routines: do public side of the definition
        for (size_t i = 0; i < m->members->length; i++)
        {
            Dsymbol *s = (*m->members)[i];

            //printf("\tModule('%s'): '%s'.semantic()\n", m->toChars(), s->toChars());
            dsymbolSemantic(s, sc);
            m->runDeferredSemantic();
        }

        if (m->userAttribDecl)
        {
            dsymbolSemantic(m->userAttribDecl, sc);
        }

        if (!m->_scope)
        {
            sc = sc->pop();
            sc->pop();              // 2 pops because Scope::createGlobal() created 2
        }
        m->semanticRun = PASSsemanticdone;
        //printf("-Module::semantic(this = %p, '%s'): parent = %p\n", m, m->toChars(), parent);
    }

    void visit(EnumDeclaration *ed)
    {
        //printf("EnumDeclaration::semantic(sd = %p, '%s') %s\n", sc->scopesym, sc->scopesym->toChars(), ed->toChars());
        //printf("EnumDeclaration::semantic() %p %s\n", ed, ed->toChars());
        if (ed->semanticRun >= PASSsemanticdone)
            return;             // semantic() already completed
        if (ed->semanticRun == PASSsemantic)
        {
            assert(ed->memtype);
            error(ed->loc, "circular reference to enum base type %s", ed->memtype->toChars());
            ed->errors = true;
            ed->semanticRun = PASSsemanticdone;
            return;
        }
        unsigned dprogress_save = Module::dprogress;

        Scope *scx = NULL;
        if (ed->_scope)
        {
            sc = ed->_scope;
            scx = ed->_scope;            // save so we don't make redundant copies
            ed->_scope = NULL;
        }

        if (!sc)
            return;

        ed->parent = sc->parent;
        ed->type = typeSemantic(ed->type, ed->loc, sc);

        ed->protection = sc->protection;
        if (sc->stc & STCdeprecated)
            ed->isdeprecated = true;
        ed->userAttribDecl = sc->userAttribDecl;

        ed->semanticRun = PASSsemantic;

        if (!ed->members && !ed->memtype)               // enum ident;
        {
            ed->semanticRun = PASSsemanticdone;
            return;
        }

        if (!ed->symtab)
            ed->symtab = new DsymbolTable();

        /* The separate, and distinct, cases are:
         *  1. enum { ... }
         *  2. enum : memtype { ... }
         *  3. enum ident { ... }
         *  4. enum ident : memtype { ... }
         *  5. enum ident : memtype;
         *  6. enum ident;
         */

        if (ed->memtype)
        {
            ed->memtype = typeSemantic(ed->memtype, ed->loc, sc);

            /* Check to see if memtype is forward referenced
             */
            if (ed->memtype->ty == Tenum)
            {
                EnumDeclaration *sym = (EnumDeclaration *)ed->memtype->toDsymbol(sc);
                if (!sym->memtype || !sym->members || !sym->symtab || sym->_scope)
                {
                    // memtype is forward referenced, so try again later
                    ed->_scope = scx ? scx : sc->copy();
                    ed->_scope->setNoFree();
                    ed->_scope->_module->addDeferredSemantic(ed);
                    Module::dprogress = dprogress_save;
                    //printf("\tdeferring %s\n", ed->toChars());
                    ed->semanticRun = PASSinit;
                    return;
                }
            }
            if (ed->memtype->ty == Tvoid)
            {
                ed->error("base type must not be void");
                ed->memtype = Type::terror;
            }
            if (ed->memtype->ty == Terror)
            {
                ed->errors = true;
                if (ed->members)
                {
                    for (size_t i = 0; i < ed->members->length; i++)
                    {
                        Dsymbol *s = (*ed->members)[i];
                        s->errors = true;               // poison all the members
                    }
                }
                ed->semanticRun = PASSsemanticdone;
                return;
            }
        }

        ed->semanticRun = PASSsemanticdone;

        if (!ed->members)               // enum ident : memtype;
            return;

        if (ed->members->length == 0)
        {
            ed->error("enum %s must have at least one member", ed->toChars());
            ed->errors = true;
            return;
        }

        Module::dprogress++;

        Scope *sce;
        if (ed->isAnonymous())
            sce = sc;
        else
        {
            sce = sc->push(ed);
            sce->parent = ed;
        }
        sce = sce->startCTFE();
        sce->setNoFree();                   // needed for getMaxMinValue()

        /* Each enum member gets the sce scope
         */
        for (size_t i = 0; i < ed->members->length; i++)
        {
            EnumMember *em = (*ed->members)[i]->isEnumMember();
            if (em)
                em->_scope = sce;
        }

        if (!ed->added)
        {
            /* addMember() is not called when the EnumDeclaration appears as a function statement,
             * so we have to do what addMember() does and install the enum members in the right symbol
             * table
             */
            ScopeDsymbol *scopesym = NULL;
            if (ed->isAnonymous())
            {
                /* Anonymous enum members get added to enclosing scope.
                 */
                for (Scope *sct = sce; 1; sct = sct->enclosing)
                {
                    assert(sct);
                    if (sct->scopesym)
                    {
                        scopesym = sct->scopesym;
                        if (!sct->scopesym->symtab)
                            sct->scopesym->symtab = new DsymbolTable();
                        break;
                    }
                }
            }
            else
            {
                // Otherwise enum members are in the EnumDeclaration's symbol table
                scopesym = ed;
            }

            for (size_t i = 0; i < ed->members->length; i++)
            {
                EnumMember *em = (*ed->members)[i]->isEnumMember();
                if (em)
                {
                    em->ed = ed;
                    em->addMember(sc, scopesym);
                }
            }
        }

        for (size_t i = 0; i < ed->members->length; i++)
        {
            EnumMember *em = (*ed->members)[i]->isEnumMember();
            if (em)
                dsymbolSemantic(em, em->_scope);
        }
        //printf("defaultval = %lld\n", defaultval);

        //if (defaultval) printf("defaultval: %s %s\n", defaultval->toChars(), defaultval->type->toChars());
        //printf("members = %s\n", ed->members->toChars());
    }

    void visit(EnumMember *em)
    {
        //printf("EnumMember::semantic() %s\n", em->toChars());
        if (em->errors || em->semanticRun >= PASSsemanticdone)
            return;
        if (em->semanticRun == PASSsemantic)
        {
            em->error("circular reference to enum member");
        Lerrors:
            em->errors = true;
            em->semanticRun = PASSsemanticdone;
            return;
        }
        assert(em->ed);

        dsymbolSemantic(em->ed, sc);
        if (em->ed->errors)
            goto Lerrors;

        if (em->errors || em->semanticRun >= PASSsemanticdone)
            return;

        if (em->_scope)
            sc = em->_scope;
        if (!sc)
            return;

        em->semanticRun = PASSsemantic;

        em->protection = em->ed->isAnonymous() ? em->ed->protection : Prot(Prot::public_);
        em->linkage = LINKd;
        em->storage_class |= STCmanifest;

        // https://issues.dlang.org/show_bug.cgi?id=9701
        if (em->ed->isAnonymous())
        {
            if (em->userAttribDecl)
                em->userAttribDecl->userAttribDecl = em->ed->userAttribDecl;
            else
                em->userAttribDecl = em->ed->userAttribDecl;
        }

        // The first enum member is special
        bool first = (em == (*em->ed->members)[0]);

        if (em->origType)
        {
            em->origType = typeSemantic(em->origType, em->loc, sc);
            em->type = em->origType;
            assert(em->value());          // "type id;" is not a valid enum member declaration
        }

        if (em->value())
        {
            Expression *e = em->value();
            assert(e->dyncast() == DYNCAST_EXPRESSION);
            e = expressionSemantic(e, sc);
            e = resolveProperties(sc, e);
            e = e->ctfeInterpret();
            if (e->op == TOKerror)
                goto Lerrors;
            if (first && !em->ed->memtype && !em->ed->isAnonymous())
            {
                em->ed->memtype = e->type;
                if (em->ed->memtype->ty == Terror)
                {
                    em->ed->errors = true;
                    goto Lerrors;
                }
                if (em->ed->memtype->ty != Terror)
                {
                    /* Bugzilla 11746: All of named enum members should have same type
                     * with the first member. If the following members were referenced
                     * during the first member semantic, their types should be unified.
                     */
                    for (size_t i = 0; i < em->ed->members->length; i++)
                    {
                        EnumMember *enm = (*em->ed->members)[i]->isEnumMember();
                        if (!enm || enm == em || enm->semanticRun < PASSsemanticdone || enm->origType)
                            continue;

                        //printf("[%d] enm = %s, enm->semanticRun = %d\n", i, enm->toChars(), enm->semanticRun);
                        Expression *ev = enm->value();
                        ev = ev->implicitCastTo(sc, em->ed->memtype);
                        ev = ev->ctfeInterpret();
                        ev = ev->castTo(sc, em->ed->type);
                        if (ev->op == TOKerror)
                            em->ed->errors = true;
                        enm->value() = ev;
                    }
                    if (em->ed->errors)
                    {
                        em->ed->memtype = Type::terror;
                        goto Lerrors;
                    }
                }
            }

            if (em->ed->memtype && !em->origType)
            {
                e = e->implicitCastTo(sc, em->ed->memtype);
                e = e->ctfeInterpret();

                // save origValue for better json output
                em->origValue = e;

                if (!em->ed->isAnonymous())
                {
                    e = e->castTo(sc, em->ed->type);
                    e = e->ctfeInterpret();
                }
            }
            else if (em->origType)
            {
                e = e->implicitCastTo(sc, em->origType);
                e = e->ctfeInterpret();
                assert(em->ed->isAnonymous());

                // save origValue for better json output
                em->origValue = e;
            }
            em->value() = e;
        }
        else if (first)
        {
            Type *t;
            if (em->ed->memtype)
                t = em->ed->memtype;
            else
            {
                t = Type::tint32;
                if (!em->ed->isAnonymous())
                    em->ed->memtype = t;
            }
            Expression *e = new IntegerExp(em->loc, 0, Type::tint32);
            e = e->implicitCastTo(sc, t);
            e = e->ctfeInterpret();

            // save origValue for better json output
            em->origValue = e;

            if (!em->ed->isAnonymous())
            {
                e = e->castTo(sc, em->ed->type);
                e = e->ctfeInterpret();
            }
            em->value() = e;
        }
        else
        {
            /* Find the previous enum member,
             * and set this to be the previous value + 1
             */
            EnumMember *emprev = NULL;
            for (size_t i = 0; i < em->ed->members->length; i++)
            {
                EnumMember *enm = (*em->ed->members)[i]->isEnumMember();
                if (enm)
                {
                    if (enm == em)
                        break;
                    emprev = enm;
                }
            }
            assert(emprev);
            if (emprev->semanticRun < PASSsemanticdone)    // if forward reference
                dsymbolSemantic(emprev, emprev->_scope);    // resolve it
            if (emprev->errors)
                goto Lerrors;

            Expression *eprev = emprev->value();
            Type *tprev = eprev->type->equals(em->ed->type) ? em->ed->memtype : eprev->type;

            Expression *emax = tprev->getProperty(em->ed->loc, Id::max, 0);
            emax = expressionSemantic(emax, sc);
            emax = emax->ctfeInterpret();

            // Set value to (eprev + 1).
            // But first check that (eprev != emax)
            assert(eprev);
            Expression *e = new EqualExp(TOKequal, em->loc, eprev, emax);
            e = expressionSemantic(e, sc);
            e = e->ctfeInterpret();
            if (e->toInteger())
            {
                em->error("initialization with (%s.%s + 1) causes overflow for type `%s`", emprev->ed->toChars(), emprev->toChars(), em->ed->type->toBasetype()->toChars());
                goto Lerrors;
            }

            // Now set e to (eprev + 1)
            e = new AddExp(em->loc, eprev, new IntegerExp(em->loc, 1, Type::tint32));
            e = expressionSemantic(e, sc);
            e = e->castTo(sc, eprev->type);
            e = e->ctfeInterpret();

            // save origValue (without cast) for better json output
            if (e->op != TOKerror)  // avoid duplicate diagnostics
            {
                assert(emprev->origValue);
                em->origValue = new AddExp(em->loc, emprev->origValue, new IntegerExp(em->loc, 1, Type::tint32));
                em->origValue = expressionSemantic(em->origValue, sc);
                em->origValue = em->origValue->ctfeInterpret();
            }

            if (e->op == TOKerror)
                goto Lerrors;
            if (e->type->isfloating())
            {
                // Check that e != eprev (not always true for floats)
                Expression *etest = new EqualExp(TOKequal, em->loc, e, eprev);
                etest = expressionSemantic(etest, sc);
                etest = etest->ctfeInterpret();
                if (etest->toInteger())
                {
                    em->error("has inexact value, due to loss of precision");
                    goto Lerrors;
                }
            }
            em->value() = e;
        }
        if (!em->origType)
            em->type = em->value()->type;

        assert(em->origValue);
        em->semanticRun = PASSsemanticdone;
    }

    void visit(TemplateDeclaration *tempdecl)
    {
        if (tempdecl->semanticRun != PASSinit)
            return;         // semantic() already run

        // Remember templates defined in module object that we need to know about
        if (sc->_module && sc->_module->ident == Id::object)
        {
            if (tempdecl->ident == Id::RTInfo)
                Type::rtinfo = tempdecl;
        }

        /* Remember Scope for later instantiations, but make
         * a copy since attributes can change.
         */
        if (!tempdecl->_scope)
        {
            tempdecl->_scope = sc->copy();
            tempdecl->_scope->setNoFree();
        }

        tempdecl->semanticRun = PASSsemantic;

        tempdecl->parent = sc->parent;
        tempdecl->protection = sc->protection;
        tempdecl->isstatic = tempdecl->toParent()->isModule() || (tempdecl->_scope->stc & STCstatic);

        if (!tempdecl->isstatic)
        {
            if (AggregateDeclaration *ad = tempdecl->parent->pastMixin()->isAggregateDeclaration())
                ad->makeNested();
        }

        // Set up scope for parameters
        ScopeDsymbol *paramsym = new ScopeDsymbol();
        paramsym->parent = tempdecl->parent;
        Scope *paramscope = sc->push(paramsym);
        paramscope->stc = 0;

        if (global.params.doDocComments)
        {
            tempdecl->origParameters = new TemplateParameters();
            tempdecl->origParameters->setDim(tempdecl->parameters->length);
            for (size_t i = 0; i < tempdecl->parameters->length; i++)
            {
                TemplateParameter *tp = (*tempdecl->parameters)[i];
                (*tempdecl->origParameters)[i] = tp->syntaxCopy();
            }
        }

        for (size_t i = 0; i < tempdecl->parameters->length; i++)
        {
            TemplateParameter *tp = (*tempdecl->parameters)[i];

            if (!tp->declareParameter(paramscope))
            {
                error(tp->loc, "parameter `%s` multiply defined", tp->ident->toChars());
                tempdecl->errors = true;
            }
            if (!tpsemantic(tp, paramscope, tempdecl->parameters))
            {
                tempdecl->errors = true;
            }
            if (i + 1 != tempdecl->parameters->length && tp->isTemplateTupleParameter())
            {
                tempdecl->error("template tuple parameter must be last one");
                tempdecl->errors = true;
            }
        }

        /* Calculate TemplateParameter::dependent
         */
        TemplateParameters tparams;
        tparams.setDim(1);
        for (size_t i = 0; i < tempdecl->parameters->length; i++)
        {
            TemplateParameter *tp = (*tempdecl->parameters)[i];
            tparams[0] = tp;

            for (size_t j = 0; j < tempdecl->parameters->length; j++)
            {
                // Skip cases like: X(T : T)
                if (i == j)
                    continue;

                if (TemplateTypeParameter *ttp = (*tempdecl->parameters)[j]->isTemplateTypeParameter())
                {
                    if (reliesOnTident(ttp->specType, &tparams))
                        tp->dependent = true;
                }
                else if (TemplateAliasParameter *tap = (*tempdecl->parameters)[j]->isTemplateAliasParameter())
                {
                    if (reliesOnTident(tap->specType, &tparams) ||
                        reliesOnTident(isType(tap->specAlias), &tparams))
                    {
                        tp->dependent = true;
                    }
                }
            }
        }

        paramscope->pop();

        // Compute again
        tempdecl->onemember = NULL;
        if (tempdecl->members)
        {
            Dsymbol *s;
            if (Dsymbol::oneMembers(tempdecl->members, &s, tempdecl->ident) && s)
            {
                tempdecl->onemember = s;
                s->parent = tempdecl;
            }
        }

        /* BUG: should check:
         *  o no virtual functions or non-static data members of classes
         */
        tempdecl->semanticRun = PASSsemanticdone;
    }

    void visit(TemplateInstance *ti)
    {
        templateInstanceSemantic(ti, sc, NULL);
    }

    void visit(TemplateMixin *tm)
    {
        if (tm->semanticRun != PASSinit)
        {
            // When a class/struct contains mixin members, and is done over
            // because of forward references, never reach here so semanticRun
            // has been reset to PASSinit.
            return;
        }
        tm->semanticRun = PASSsemantic;

        Scope *scx = NULL;
        if (tm->_scope)
        {
            sc = tm->_scope;
            scx = tm->_scope;            // save so we don't make redundant copies
            tm->_scope = NULL;
        }

        /* Run semantic on each argument, place results in tiargs[],
         * then find best match template with tiargs
         */
        if (!tm->findTempDecl(sc) ||
            !tm->semanticTiargs(sc) ||
            !tm->findBestMatch(sc, NULL))
        {
            if (tm->semanticRun == PASSinit)    // forward reference had occured
            {
                //printf("forward reference - deferring\n");
                tm->_scope = scx ? scx : sc->copy();
                tm->_scope->setNoFree();
                tm->_scope->_module->addDeferredSemantic(tm);
                return;
            }

            tm->inst = tm;
            tm->errors = true;
            return;         // error recovery
        }
        TemplateDeclaration *tempdecl = tm->tempdecl->isTemplateDeclaration();
        assert(tempdecl);

        if (!tm->ident)
        {
            /* Assign scope local unique identifier, as same as lambdas.
             */
            const char *s = "__mixin";

            if (FuncDeclaration *func = sc->parent->isFuncDeclaration())
            {
                tm->symtab = func->localsymtab;
                if (tm->symtab)
                {
                    // Inside template constraint, symtab is not set yet.
                    goto L1;
                }
            }
            else
            {
                tm->symtab = sc->parent->isScopeDsymbol()->symtab;
            L1:
                assert(tm->symtab);
                int num = (int)dmd_aaLen(tm->symtab->tab) + 1;
                tm->ident = Identifier::generateId(s, num);
                tm->symtab->insert(tm);
            }
        }

        tm->inst = tm;
        tm->parent = sc->parent;

        /* Detect recursive mixin instantiations.
         */
        for (Dsymbol *s = tm->parent; s; s = s->parent)
        {
            //printf("\ts = '%s'\n", s->toChars());
            TemplateMixin *tmix = s->isTemplateMixin();
            if (!tmix || tempdecl != tmix->tempdecl)
                continue;

            /* Different argument list lengths happen with variadic args
             */
            if (tm->tiargs->length != tmix->tiargs->length)
                continue;

            for (size_t i = 0; i < tm->tiargs->length; i++)
            {
                RootObject *o = (*tm->tiargs)[i];
                Type *ta = isType(o);
                Expression *ea = isExpression(o);
                Dsymbol *sa = isDsymbol(o);
                RootObject *tmo = (*tmix->tiargs)[i];
                if (ta)
                {
                    Type *tmta = isType(tmo);
                    if (!tmta)
                        goto Lcontinue;
                    if (!ta->equals(tmta))
                        goto Lcontinue;
                }
                else if (ea)
                {
                    Expression *tme = isExpression(tmo);
                    if (!tme || !ea->equals(tme))
                        goto Lcontinue;
                }
                else if (sa)
                {
                    Dsymbol *tmsa = isDsymbol(tmo);
                    if (sa != tmsa)
                        goto Lcontinue;
                }
                else
                    assert(0);
            }
            tm->error("recursive mixin instantiation");
            return;

        Lcontinue:
            continue;
        }

        // Copy the syntax trees from the TemplateDeclaration
        tm->members = Dsymbol::arraySyntaxCopy(tempdecl->members);
        if (!tm->members)
            return;

        tm->symtab = new DsymbolTable();

        for (Scope *sce = sc; 1; sce = sce->enclosing)
        {
            ScopeDsymbol *sds = (ScopeDsymbol *)sce->scopesym;
            if (sds)
            {
                sds->importScope(tm, Prot(Prot::public_));
                break;
            }
        }

        Scope *scy = sc->push(tm);
        scy->parent = tm;

        tm->argsym = new ScopeDsymbol();
        tm->argsym->parent = scy->parent;
        Scope *argscope = scy->push(tm->argsym);

        unsigned errorsave = global.errors;

        // Declare each template parameter as an alias for the argument type
        tm->declareParameters(argscope);

        // Add members to enclosing scope, as well as this scope
        for (size_t i = 0; i < tm->members->length; i++)
        {
            Dsymbol *s = (*tm->members)[i];
            s->addMember(argscope, tm);
            //printf("sc->parent = %p, sc->scopesym = %p\n", sc->parent, sc->scopesym);
            //printf("s->parent = %s\n", s->parent->toChars());
        }

        // Do semantic() analysis on template instance members
        Scope *sc2 = argscope->push(tm);
        //size_t deferred_dim = Module::deferred.length;

        static int nest;
        //printf("%d\n", nest);
        if (++nest > global.recursionLimit)
        {
            global.gag = 0;                 // ensure error message gets printed
            tm->error("recursive expansion");
            fatal();
        }

        for (size_t i = 0; i < tm->members->length; i++)
        {
            Dsymbol *s = (*tm->members)[i];
            s->setScope(sc2);
        }

        for (size_t i = 0; i < tm->members->length; i++)
        {
            Dsymbol *s = (*tm->members)[i];
            s->importAll(sc2);
        }

        for (size_t i = 0; i < tm->members->length; i++)
        {
            Dsymbol *s = (*tm->members)[i];
            dsymbolSemantic(s, sc2);
        }

        nest--;

        /* In DeclDefs scope, TemplateMixin does not have to handle deferred symbols.
         * Because the members would already call Module::addDeferredSemantic() for themselves.
         * See Struct, Class, Interface, and EnumDeclaration::semantic().
         */
        //if (!sc->func && Module::deferred.length > deferred_dim) {}

        AggregateDeclaration *ad = tm->toParent()->isAggregateDeclaration();
        if (sc->func && !ad)
        {
            semantic2(tm, sc2);
            semantic3(tm, sc2);
        }

        // Give additional context info if error occurred during instantiation
        if (global.errors != errorsave)
        {
            tm->error("error instantiating");
            tm->errors = true;
        }

        sc2->pop();
        argscope->pop();
        scy->pop();
    }

    void visit(Nspace *ns)
    {
        if (ns->semanticRun != PASSinit)
            return;
        if (ns->_scope)
        {
            sc = ns->_scope;
            ns->_scope = NULL;
        }
        if (!sc)
            return;

        ns->semanticRun = PASSsemantic;
        ns->parent = sc->parent;
        if (ns->members)
        {
            assert(sc);
            sc = sc->push(ns);
            sc->linkage = LINKcpp;          // note that namespaces imply C++ linkage
            sc->parent = ns;

            for (size_t i = 0; i < ns->members->length; i++)
            {
                Dsymbol *s = (*ns->members)[i];
                s->importAll(sc);
            }

            for (size_t i = 0; i < ns->members->length; i++)
            {
                Dsymbol *s = (*ns->members)[i];
                dsymbolSemantic(s, sc);
            }
            sc->pop();
        }
        ns->semanticRun = PASSsemanticdone;
    }

    void funcDeclarationSemantic(FuncDeclaration *funcdecl)
    {
        TypeFunction *f;
        AggregateDeclaration *ad;
        InterfaceDeclaration *id;

        if (funcdecl->semanticRun != PASSinit && funcdecl->isFuncLiteralDeclaration())
        {
            /* Member functions that have return types that are
             * forward references can have semantic() run more than
             * once on them.
             * See test\interface2.d, test20
             */
            return;
        }

        if (funcdecl->semanticRun >= PASSsemanticdone)
            return;
        assert(funcdecl->semanticRun <= PASSsemantic);
        funcdecl->semanticRun = PASSsemantic;

        if (funcdecl->_scope)
        {
            sc = funcdecl->_scope;
            funcdecl->_scope = NULL;
        }

        if (!sc || funcdecl->errors)
            return;

        funcdecl->parent = sc->parent;
        Dsymbol *parent = funcdecl->toParent();

        funcdecl->foverrides.setDim(0);       // reset in case semantic() is being retried for this function

        funcdecl->storage_class |= sc->stc & ~STCref;
        ad = funcdecl->isThis();
        // Don't nest structs b/c of generated methods which should not access the outer scopes.
        // https://issues.dlang.org/show_bug.cgi?id=16627
        if (ad && !funcdecl->generated)
        {
            funcdecl->storage_class |= ad->storage_class & (STC_TYPECTOR | STCsynchronized);
            ad->makeNested();
        }
        if (sc->func)
            funcdecl->storage_class |= sc->func->storage_class & STCdisable;
        // Remove prefix storage classes silently.
        if ((funcdecl->storage_class & STC_TYPECTOR) && !(ad || funcdecl->isNested()))
            funcdecl->storage_class &= ~STC_TYPECTOR;

        //printf("function storage_class = x%llx, sc->stc = x%llx, %x\n", funcdecl->storage_class, sc->stc, Declaration::isFinal());

        FuncLiteralDeclaration *fld = funcdecl->isFuncLiteralDeclaration();
        if (fld && fld->treq)
        {
            Type *treq = fld->treq;
            assert(treq->nextOf()->ty == Tfunction);
            if (treq->ty == Tdelegate)
                fld->tok = TOKdelegate;
            else if (treq->ty == Tpointer && treq->nextOf()->ty == Tfunction)
                fld->tok = TOKfunction;
            else
                assert(0);
            funcdecl->linkage = treq->nextOf()->toTypeFunction()->linkage;
        }
        else
            funcdecl->linkage = sc->linkage;
        funcdecl->inlining = sc->inlining;
        funcdecl->protection = sc->protection;
        funcdecl->userAttribDecl = sc->userAttribDecl;

        if (!funcdecl->originalType)
            funcdecl->originalType = funcdecl->type->syntaxCopy();
        if (funcdecl->type->ty != Tfunction)
        {
            if (funcdecl->type->ty != Terror)
            {
                funcdecl->error("%s must be a function instead of %s", funcdecl->toChars(), funcdecl->type->toChars());
                funcdecl->type = Type::terror;
            }
            funcdecl->errors = true;
            return;
        }
        if (!funcdecl->type->deco)
        {
            sc = sc->push();
            sc->stc |= funcdecl->storage_class & (STCdisable | STCdeprecated);  // forward to function type
            TypeFunction *tf = funcdecl->type->toTypeFunction();

            if (sc->func)
            {
                /* If the nesting parent is pure without inference,
                 * then this function defaults to pure too.
                 *
                 *  auto foo() pure {
                 *    auto bar() {}     // become a weak purity funciton
                 *    class C {         // nested class
                 *      auto baz() {}   // become a weak purity funciton
                 *    }
                 *
                 *    static auto boo() {}   // typed as impure
                 *    // Even though, boo cannot call any impure functions.
                 *    // See also Expression::checkPurity().
                 *  }
                 */
                if (tf->purity == PUREimpure && (funcdecl->isNested() || funcdecl->isThis()))
                {
                    FuncDeclaration *fd = NULL;
                    for (Dsymbol *p = funcdecl->toParent2(); p; p = p->toParent2())
                    {
                        if (AggregateDeclaration *adx = p->isAggregateDeclaration())
                        {
                            if (adx->isNested())
                                continue;
                            break;
                        }
                        if ((fd = p->isFuncDeclaration()) != NULL)
                            break;
                    }

                    /* If the parent's purity is inferred, then this function's purity needs
                     * to be inferred first.
                     */
                    if (fd && fd->isPureBypassingInference() >= PUREweak &&
                        !funcdecl->isInstantiated())
                    {
                        tf->purity = PUREfwdref;            // default to pure
                    }
                }
            }

            if (tf->isref)      sc->stc |= STCref;
            if (tf->isscope)    sc->stc |= STCscope;
            if (tf->isnothrow)  sc->stc |= STCnothrow;
            if (tf->isnogc)     sc->stc |= STCnogc;
            if (tf->isproperty) sc->stc |= STCproperty;
            if (tf->purity == PUREfwdref)   sc->stc |= STCpure;
            if (tf->trust != TRUSTdefault)
                sc->stc &= ~(STCsafe | STCsystem | STCtrusted);
            if (tf->trust == TRUSTsafe)     sc->stc |= STCsafe;
            if (tf->trust == TRUSTsystem)   sc->stc |= STCsystem;
            if (tf->trust == TRUSTtrusted)  sc->stc |= STCtrusted;

            if (funcdecl->isCtorDeclaration())
            {
                sc->flags |= SCOPEctor;

                Type *tret = ad->handleType();
                assert(tret);
                tret = tret->addStorageClass(funcdecl->storage_class | sc->stc);
                tret = tret->addMod(funcdecl->type->mod);
                tf->next = tret;

                if (ad->isStructDeclaration())
                    sc->stc |= STCref;
            }

            // 'return' on a non-static class member function implies 'scope' as well
            if (ad && ad->isClassDeclaration() && (tf->isreturn || sc->stc & STCreturn) && !(sc->stc & STCstatic))
                sc->stc |= STCscope;

            // If 'this' has no pointers, remove 'scope' as it has no meaning
            if (sc->stc & STCscope && ad && ad->isStructDeclaration() && !ad->type->hasPointers())
            {
                sc->stc &= ~STCscope;
                tf->isscope = false;
            }

            sc->linkage = funcdecl->linkage;

            if (!tf->isNaked() && !(funcdecl->isThis() || funcdecl->isNested()))
            {
                OutBuffer buf;
                MODtoBuffer(&buf, tf->mod);
                funcdecl->error("without `this` cannot be %s", buf.peekChars());
                tf->mod = 0;    // remove qualifiers
            }

            /* Apply const, immutable, wild and shared storage class
             * to the function type. Do this before type semantic.
             */
            StorageClass stc = funcdecl->storage_class;
            if (funcdecl->type->isImmutable())
                stc |= STCimmutable;
            if (funcdecl->type->isConst())
                stc |= STCconst;
            if (funcdecl->type->isShared() || funcdecl->storage_class & STCsynchronized)
                stc |= STCshared;
            if (funcdecl->type->isWild())
                stc |= STCwild;
            switch (stc & STC_TYPECTOR)
            {
                case STCimmutable:
                case STCimmutable | STCconst:
                case STCimmutable | STCwild:
                case STCimmutable | STCwild | STCconst:
                case STCimmutable | STCshared:
                case STCimmutable | STCshared | STCconst:
                case STCimmutable | STCshared | STCwild:
                case STCimmutable | STCshared | STCwild | STCconst:
                    // Don't use immutableOf(), as that will do a merge()
                    funcdecl->type = funcdecl->type->makeImmutable();
                    break;

                case STCconst:
                    funcdecl->type = funcdecl->type->makeConst();
                    break;

                case STCwild:
                    funcdecl->type = funcdecl->type->makeWild();
                    break;

                case STCwild | STCconst:
                    funcdecl->type = funcdecl->type->makeWildConst();
                    break;

                case STCshared:
                    funcdecl->type = funcdecl->type->makeShared();
                    break;

                case STCshared | STCconst:
                    funcdecl->type = funcdecl->type->makeSharedConst();
                    break;

                case STCshared | STCwild:
                    funcdecl->type = funcdecl->type->makeSharedWild();
                    break;

                case STCshared | STCwild | STCconst:
                    funcdecl->type = funcdecl->type->makeSharedWildConst();
                    break;

                case 0:
                    break;

                default:
                    assert(0);
            }

            funcdecl->type = typeSemantic(funcdecl->type, funcdecl->loc, sc);
            sc = sc->pop();
        }
        if (funcdecl->type->ty != Tfunction)
        {
            if (funcdecl->type->ty != Terror)
            {
                funcdecl->error("%s must be a function instead of %s", funcdecl->toChars(), funcdecl->type->toChars());
                funcdecl->type = Type::terror;
            }
            funcdecl->errors = true;
            return;
        }
        else
        {
            // Merge back function attributes into 'originalType'.
            // It's used for mangling, ddoc, and json output.
            TypeFunction *tfo = funcdecl->originalType->toTypeFunction();
            TypeFunction *tfx = funcdecl->type->toTypeFunction();
            tfo->mod        = tfx->mod;
            tfo->isscope    = tfx->isscope;
            tfo->isscopeinferred = tfx->isscopeinferred;
            tfo->isref      = tfx->isref;
            tfo->isnothrow  = tfx->isnothrow;
            tfo->isnogc     = tfx->isnogc;
            tfo->isproperty = tfx->isproperty;
            tfo->purity     = tfx->purity;
            tfo->trust      = tfx->trust;

            funcdecl->storage_class &= ~(STC_TYPECTOR | STC_FUNCATTR);
        }

        f = (TypeFunction *)funcdecl->type;

        if ((funcdecl->storage_class & STCauto) && !f->isref && !funcdecl->inferRetType)
            funcdecl->error("storage class `auto` has no effect if return type is not inferred");
        /* Functions can only be 'scope' if they have a 'this'
         */
        if (f->isscope && !funcdecl->isNested() && !ad)
        {
            funcdecl->error("functions cannot be scope");
        }

        if (f->isreturn && !funcdecl->needThis() && !funcdecl->isNested())
        {
            /* Non-static nested functions have a hidden 'this' pointer to which
             * the 'return' applies
             */
            funcdecl->error("static member has no `this` to which `return` can apply");
        }

        if (funcdecl->isAbstract() && !funcdecl->isVirtual())
        {
            const char *sfunc;
            if (funcdecl->isStatic())
                sfunc = "static";
            else if (funcdecl->protection.kind == Prot::private_ || funcdecl->protection.kind == Prot::package_)
                sfunc = protectionToChars(funcdecl->protection.kind);
            else
                sfunc = "non-virtual";
            funcdecl->error("%s functions cannot be abstract", sfunc);
        }

        if (funcdecl->isOverride() && !funcdecl->isVirtual())
        {
            Prot::Kind kind = funcdecl->prot().kind;
            if ((kind == Prot::private_ || kind == Prot::package_) && funcdecl->isMember())
                funcdecl->error("%s method is not virtual and cannot override", protectionToChars(kind));
            else
                funcdecl->error("cannot override a non-virtual function");
        }

        if (funcdecl->isAbstract() && funcdecl->isFinalFunc())
            funcdecl->error("cannot be both final and abstract");

        id = parent->isInterfaceDeclaration();
        if (id)
        {
            funcdecl->storage_class |= STCabstract;

            if (funcdecl->isCtorDeclaration() ||
                funcdecl->isPostBlitDeclaration() ||
                funcdecl->isDtorDeclaration() ||
                funcdecl->isInvariantDeclaration() ||
                funcdecl->isNewDeclaration() || funcdecl->isDelete())
                funcdecl->error("constructors, destructors, postblits, invariants, new and delete functions are not allowed in interface %s", id->toChars());
            if (funcdecl->fbody && funcdecl->isVirtual())
                funcdecl->error("function body only allowed in final functions in interface %s", id->toChars());
        }

        if (UnionDeclaration *ud = parent->isUnionDeclaration())
        {
            if (funcdecl->isPostBlitDeclaration() ||
                funcdecl->isDtorDeclaration() ||
                funcdecl->isInvariantDeclaration())
                funcdecl->error("destructors, postblits and invariants are not allowed in union %s", ud->toChars());
        }

        if (parent->isStructDeclaration())
        {
            if (funcdecl->isCtorDeclaration())
            {
                goto Ldone;
            }
        }

        if (ClassDeclaration *cd = parent->isClassDeclaration())
        {
            if (funcdecl->isCtorDeclaration())
            {
                goto Ldone;
            }

            if (funcdecl->storage_class & STCabstract)
                cd->isabstract = ABSyes;

            // if static function, do not put in vtbl[]
            if (!funcdecl->isVirtual())
            {
                //printf("\tnot virtual\n");
                goto Ldone;
            }
            // Suppress further errors if the return type is an error
            if (funcdecl->type->nextOf() == Type::terror)
                goto Ldone;

            bool may_override = false;
            for (size_t i = 0; i < cd->baseclasses->length; i++)
            {
                BaseClass *b = (*cd->baseclasses)[i];
                ClassDeclaration *cbd = b->type->toBasetype()->isClassHandle();
                if (!cbd)
                    continue;
                for (size_t j = 0; j < cbd->vtbl.length; j++)
                {
                    FuncDeclaration *f2 = cbd->vtbl[j]->isFuncDeclaration();
                    if (!f2 || f2->ident != funcdecl->ident)
                        continue;
                    if (cbd->parent && cbd->parent->isTemplateInstance())
                    {
                        if (!f2->functionSemantic())
                            goto Ldone;
                    }
                    may_override = true;
                }
            }
            if (may_override && funcdecl->type->nextOf() == NULL)
            {
                /* If same name function exists in base class but 'this' is auto return,
                 * cannot find index of base class's vtbl[] to override.
                 */
                funcdecl->error("return type inference is not supported if may override base class function");
            }

            /* Find index of existing function in base class's vtbl[] to override
             * (the index will be the same as in cd's current vtbl[])
             */
            int vi = cd->baseClass ? funcdecl->findVtblIndex((Dsymbols*)&cd->baseClass->vtbl, (int)cd->baseClass->vtbl.length)
                                   : -1;

            bool doesoverride = false;
            switch (vi)
            {
                case -1:
            Lintro:
                    /* Didn't find one, so
                     * This is an 'introducing' function which gets a new
                     * slot in the vtbl[].
                     */

                    // Verify this doesn't override previous final function
                    if (cd->baseClass)
                    {
                        Dsymbol *s = cd->baseClass->search(funcdecl->loc, funcdecl->ident);
                        if (s)
                        {
                            FuncDeclaration *f2 = s->isFuncDeclaration();
                            if (f2)
                            {
                                f2 = f2->overloadExactMatch(funcdecl->type);
                                if (f2 && f2->isFinalFunc() && f2->prot().kind != Prot::private_)
                                    funcdecl->error("cannot override final function %s", f2->toPrettyChars());
                            }
                        }
                    }

                    /* These quirky conditions mimic what VC++ appears to do
                     */
                    if (global.params.mscoff && cd->isCPPclass() &&
                        cd->baseClass && cd->baseClass->vtbl.length)
                    {
                        /* if overriding an interface function, then this is not
                         * introducing and don't put it in the class vtbl[]
                         */
                        funcdecl->interfaceVirtual = funcdecl->overrideInterface();
                        if (funcdecl->interfaceVirtual)
                        {
                            //printf("\tinterface function %s\n", funcdecl->toChars());
                            cd->vtblFinal.push(funcdecl);
                            goto Linterfaces;
                        }
                    }

                    if (funcdecl->isFinalFunc())
                    {
                        // Don't check here, as it may override an interface function
                        //if (funcdecl->isOverride())
                            //funcdecl->error("is marked as override, but does not override any function");
                        cd->vtblFinal.push(funcdecl);
                    }
                    else
                    {
                        //printf("\tintroducing function %s\n", funcdecl->toChars());
                        funcdecl->introducing = 1;
                        if (cd->isCPPclass() && target.cpp.reverseOverloads)
                        {
                            // with dmc, overloaded functions are grouped and in reverse order
                            funcdecl->vtblIndex = (int)cd->vtbl.length;
                            for (int i = 0; i < (int)cd->vtbl.length; i++)
                            {
                                if (cd->vtbl[i]->ident == funcdecl->ident && cd->vtbl[i]->parent == parent)
                                {
                                    funcdecl->vtblIndex = (int)i;
                                    break;
                                }
                            }
                            // shift all existing functions back
                            for (int i = (int)cd->vtbl.length; i > funcdecl->vtblIndex; i--)
                            {
                                FuncDeclaration *fd = cd->vtbl[i-1]->isFuncDeclaration();
                                assert(fd);
                                fd->vtblIndex++;
                            }
                            cd->vtbl.insert(funcdecl->vtblIndex, funcdecl);
                        }
                        else
                        {
                            // Append to end of vtbl[]
                            vi = (int)cd->vtbl.length;
                            cd->vtbl.push(funcdecl);
                            funcdecl->vtblIndex = vi;
                        }
                    }
                    break;

                case -2:
                    // can't determine because of forward references
                    funcdecl->errors = true;
                    return;

                default:
                {
                    FuncDeclaration *fdv = cd->baseClass->vtbl[vi]->isFuncDeclaration();
                    FuncDeclaration *fdc = cd->vtbl[vi]->isFuncDeclaration();
                    // This function is covariant with fdv

                    if (fdc == funcdecl)
                    {
                        doesoverride = true;
                        break;
                    }

                    if (fdc->toParent() == parent)
                    {
                        //printf("vi = %d,\tthis = %p %s %s @ [%s]\n\tfdc  = %p %s %s @ [%s]\n\tfdv  = %p %s %s @ [%s]\n",
                        //        vi, funcdecl, funcdecl->toChars(), funcdecl->type->toChars(), funcdecl->loc.toChars(),
                        //            fdc,  fdc ->toChars(), fdc ->type->toChars(), fdc ->loc.toChars(),
                        //            fdv,  fdv ->toChars(), fdv ->type->toChars(), fdv ->loc.toChars());

                        // fdc overrides fdv exactly, then this introduces new function.
                        if (fdc->type->mod == fdv->type->mod && funcdecl->type->mod != fdv->type->mod)
                            goto Lintro;
                    }

                    // This function overrides fdv
                    if (fdv->isFinalFunc())
                        funcdecl->error("cannot override final function %s", fdv->toPrettyChars());

                    if (!funcdecl->isOverride())
                    {
                        if (fdv->isFuture())
                        {
                            ::deprecation(funcdecl->loc, "@__future base class method %s is being overridden by %s; rename the latter",
                                fdv->toPrettyChars(), funcdecl->toPrettyChars());
                            // Treat 'this' as an introducing function, giving it a separate hierarchy in the vtbl[]
                            goto Lintro;
                        }
                        else
                        {
                            int vi2 = funcdecl->findVtblIndex(&cd->baseClass->vtbl, (int)cd->baseClass->vtbl.length, false);
                            if (vi2 < 0)
                                // https://issues.dlang.org/show_bug.cgi?id=17349
                                ::deprecation(funcdecl->loc, "cannot implicitly override base class method `%s` with `%s`; add `override` attribute",
                                    fdv->toPrettyChars(), funcdecl->toPrettyChars());
                            else
                                error(funcdecl->loc, "implicitly overriding base class method %s with %s deprecated; add `override` attribute",
                                    fdv->toPrettyChars(), funcdecl->toPrettyChars());
                        }
                    }

                    doesoverride = true;
                    if (fdc->toParent() == parent)
                    {
                        // If both are mixins, or both are not, then error.
                        // If either is not, the one that is not overrides the other.
                        bool thismixin = funcdecl->parent->isClassDeclaration() != NULL;
                        bool fdcmixin = fdc->parent->isClassDeclaration() != NULL;
                        if (thismixin == fdcmixin)
                        {
                            funcdecl->error("multiple overrides of same function");
                        }
                        else if (!thismixin)    // fdc overrides fdv
                        {
                            // this doesn't override any function
                            break;
                        }
                    }
                    cd->vtbl[vi] = funcdecl;
                    funcdecl->vtblIndex = vi;

                    /* Remember which functions this overrides
                     */
                    funcdecl->foverrides.push(fdv);

                    /* This works by whenever this function is called,
                     * it actually returns tintro, which gets dynamically
                     * cast to type. But we know that tintro is a base
                     * of type, so we could optimize it by not doing a
                     * dynamic cast, but just subtracting the isBaseOf()
                     * offset if the value is != null.
                     */

                    if (fdv->tintro)
                        funcdecl->tintro = fdv->tintro;
                    else if (!funcdecl->type->equals(fdv->type))
                    {
                        /* Only need to have a tintro if the vptr
                         * offsets differ
                         */
                        int offset;
                        if (fdv->type->nextOf()->isBaseOf(funcdecl->type->nextOf(), &offset))
                        {
                            funcdecl->tintro = fdv->type;
                        }
                    }
                    break;
                }
            }

            /* Go through all the interface bases.
             * If this function is covariant with any members of those interface
             * functions, set the tintro.
             */
        Linterfaces:
            for (size_t i = 0; i < cd->interfaces.length; i++)
            {
                BaseClass *b = cd->interfaces.ptr[i];
                vi = funcdecl->findVtblIndex((Dsymbols *)&b->sym->vtbl, (int)b->sym->vtbl.length);
                switch (vi)
                {
                    case -1:
                        break;

                    case -2:
                        // can't determine because of forward references
                        funcdecl->errors = true;
                        return;

                    default:
                    {
                        FuncDeclaration *fdv = (FuncDeclaration *)b->sym->vtbl[vi];
                        Type *ti = NULL;

                        /* Remember which functions this overrides
                         */
                        funcdecl->foverrides.push(fdv);

                        /* Should we really require 'override' when implementing
                         * an interface function?
                         */
                        //if (!funcdecl->isOverride())
                            //warning(funcdecl->loc, "overrides base class function %s, but is not marked with `override`", fdv->toPrettyChars());

                        if (fdv->tintro)
                            ti = fdv->tintro;
                        else if (!funcdecl->type->equals(fdv->type))
                        {
                            /* Only need to have a tintro if the vptr
                             * offsets differ
                             */
                            int offset;
                            if (fdv->type->nextOf()->isBaseOf(funcdecl->type->nextOf(), &offset))
                            {
                                ti = fdv->type;
                            }
                        }
                        if (ti)
                        {
                            if (funcdecl->tintro)
                            {
                                if (!funcdecl->tintro->nextOf()->equals(ti->nextOf()) &&
                                    !funcdecl->tintro->nextOf()->isBaseOf(ti->nextOf(), NULL) &&
                                    !ti->nextOf()->isBaseOf(funcdecl->tintro->nextOf(), NULL))
                                {
                                    funcdecl->error("incompatible covariant types %s and %s", funcdecl->tintro->toChars(), ti->toChars());
                                }
                            }
                            funcdecl->tintro = ti;
                        }
                        goto L2;
                    }
                }
            }

            if (!doesoverride && funcdecl->isOverride() && (funcdecl->type->nextOf() || !may_override))
            {
                BaseClass *bc = NULL;
                Dsymbol *s = NULL;
                for (size_t i = 0; i < cd->baseclasses->length; i++)
                {
                    bc = (*cd->baseclasses)[i];
                    s = bc->sym->search_correct(funcdecl->ident);
                    if (s) break;
                }

                if (s)
                    funcdecl->error("does not override any function, did you mean to override `%s%s`?",
                        bc->sym->isCPPclass() ? "extern (C++) " : "", s->toPrettyChars());
                else
                    funcdecl->error("does not override any function");
            }

        L2: ;

            /* Go through all the interface bases.
             * Disallow overriding any final functions in the interface(s).
             */
            for (size_t i = 0; i < cd->interfaces.length; i++)
            {
                BaseClass *b = cd->interfaces.ptr[i];
                if (b->sym)
                {
                    Dsymbol *s = search_function(b->sym, funcdecl->ident);
                    if (s)
                    {
                        FuncDeclaration *f2 = s->isFuncDeclaration();
                        if (f2)
                        {
                            f2 = f2->overloadExactMatch(funcdecl->type);
                            if (f2 && f2->isFinalFunc() && f2->prot().kind != Prot::private_)
                                funcdecl->error("cannot override final function %s.%s", b->sym->toChars(), f2->toPrettyChars());
                        }
                    }
                }
            }

            if (funcdecl->isOverride())
            {
                if (funcdecl->storage_class & STCdisable)
                    funcdecl->deprecation("overridden functions cannot be annotated @disable");
                if (funcdecl->isDeprecated())
                    funcdecl->deprecation("deprecated functions cannot be annotated @disable");
            }
        }
        else if (funcdecl->isOverride() && !parent->isTemplateInstance())
            funcdecl->error("override only applies to class member functions");

        // Reflect this->type to f because it could be changed by findVtblIndex
        f = funcdecl->type->toTypeFunction();

    Ldone:
        /* Contracts can only appear without a body when they are virtual interface functions
         */
        if (!funcdecl->fbody && !allowsContractWithoutBody(funcdecl))
            funcdecl->error("in and out contracts can only appear without a body when they are virtual interface functions or abstract");

        /* Do not allow template instances to add virtual functions
         * to a class.
         */
        if (funcdecl->isVirtual())
        {
            TemplateInstance *ti = parent->isTemplateInstance();
            if (ti)
            {
                // Take care of nested templates
                while (1)
                {
                    TemplateInstance *ti2 = ti->tempdecl->parent->isTemplateInstance();
                    if (!ti2)
                        break;
                    ti = ti2;
                }

                // If it's a member template
                ClassDeclaration *cd = ti->tempdecl->isClassMember();
                if (cd)
                {
                    funcdecl->error("cannot use template to add virtual function to class `%s`", cd->toChars());
                }
            }
        }

        if (funcdecl->isMain())
            funcdecl->checkDmain();       // Check main() parameters and return type

        /* Purity and safety can be inferred for some functions by examining
         * the function body.
         */
        if (canInferAttributes(funcdecl, sc))
            initInferAttributes(funcdecl);

        Module::dprogress++;
        funcdecl->semanticRun = PASSsemanticdone;

        /* Save scope for possible later use (if we need the
         * function internals)
         */
        funcdecl->_scope = sc->copy();
        funcdecl->_scope->setNoFree();

        static bool printedMain = false;  // semantic might run more than once
        if (global.params.verbose && !printedMain)
        {
            const char *type = funcdecl->isMain() ? "main" : funcdecl->isWinMain() ? "winmain" : funcdecl->isDllMain() ? "dllmain" : (const char *)NULL;
            Module *mod = sc->_module;

            if (type && mod)
            {
                printedMain = true;
                const char *name = mod->srcfile->toChars();
                const char *path = FileName::searchPath(global.path, name, true);
                message("entry     %-10s\t%s", type, path ? path : name);
            }
        }

        if (funcdecl->fbody && funcdecl->isMain() && sc->_module->isRoot())
            Compiler::genCmain(sc);

        assert(funcdecl->type->ty != Terror || funcdecl->errors);

        // semantic for parameters' UDAs
        const size_t nparams = f->parameterList.length();
        for (size_t i = 0; i < nparams; i++)
        {
            Parameter *param = f->parameterList[i];
            if (param && param->userAttribDecl)
                dsymbolSemantic(param->userAttribDecl, sc);
        }
    }

    // Do the semantic analysis on the external interface to the function.
    void visit(FuncDeclaration *funcdecl)
    {
        funcDeclarationSemantic(funcdecl);
    }

    void visit(CtorDeclaration *ctd)
    {
        //printf("CtorDeclaration::semantic() %s\n", ctd->toChars());
        if (ctd->semanticRun >= PASSsemanticdone)
            return;
        if (ctd->_scope)
        {
            sc = ctd->_scope;
            ctd->_scope = NULL;
        }

        ctd->parent = sc->parent;
        Dsymbol *p = ctd->toParent2();
        AggregateDeclaration *ad = p->isAggregateDeclaration();
        if (!ad)
        {
            error(ctd->loc, "constructor can only be a member of aggregate, not %s %s",
                p->kind(), p->toChars());
            ctd->type = Type::terror;
            ctd->errors = true;
            return;
        }

        sc = sc->push();
        sc->stc &= ~STCstatic;              // not a static constructor
        sc->flags |= SCOPEctor;

        funcDeclarationSemantic(ctd);

        sc->pop();

        if (ctd->errors)
            return;

        TypeFunction *tf = ctd->type->toTypeFunction();

        /* See if it's the default constructor
         * But, template constructor should not become a default constructor.
         */
        if (ad && (!ctd->parent->isTemplateInstance() || ctd->parent->isTemplateMixin()))
        {
            const size_t dim = tf->parameterList.length();

            if (StructDeclaration *sd = ad->isStructDeclaration())
            {
                if (dim == 0 && tf->parameterList.varargs == VARARGnone) // empty default ctor w/o any varargs
                {
                    if (ctd->fbody || !(ctd->storage_class & STCdisable) || dim)
                    {
                        ctd->error("default constructor for structs only allowed "
                            "with @disable, no body, and no parameters");
                        ctd->storage_class |= STCdisable;
                        ctd->fbody = NULL;
                    }
                    sd->noDefaultCtor = true;
                }
                else if (dim == 0 && tf->parameterList.varargs) // allow varargs only ctor
                {
                }
                else if (dim && tf->parameterList[0]->defaultArg)
                {
                    // if the first parameter has a default argument, then the rest does as well
                    if (ctd->storage_class & STCdisable)
                    {
                        ctd->deprecation("@disable'd constructor cannot have default "
                                         "arguments for all parameters.");
                        deprecationSupplemental(ctd->loc, "Use @disable this(); if you want to disable default initialization.");
                    }
                    else
                        ctd->deprecation("all parameters have default arguments, "
                                         "but structs cannot have default constructors.");
                }

            }
            else if (dim == 0 && tf->parameterList.varargs == VARARGnone)
            {
                ad->defaultCtor = ctd;
            }
        }
    }

    void visit(PostBlitDeclaration *pbd)
    {
        //printf("PostBlitDeclaration::semantic() %s\n", pbd->toChars());
        //printf("ident: %s, %s, %p, %p\n", pbd->ident->toChars(), Id::dtor->toChars(), pbd->ident, Id::dtor);
        //printf("stc = x%llx\n", sc->stc);
        if (pbd->semanticRun >= PASSsemanticdone)
            return;
        if (pbd->_scope)
        {
            sc = pbd->_scope;
            pbd->_scope = NULL;
        }

        pbd->parent = sc->parent;
        Dsymbol *p = pbd->toParent2();
        StructDeclaration *ad = p->isStructDeclaration();
        if (!ad)
        {
            error(pbd->loc, "postblit can only be a member of struct/union, not %s %s",
                p->kind(), p->toChars());
            pbd->type = Type::terror;
            pbd->errors = true;
            return;
        }
        if (pbd->ident == Id::postblit && pbd->semanticRun < PASSsemantic)
            ad->postblits.push(pbd);
        if (!pbd->type)
            pbd->type = new TypeFunction(ParameterList(), Type::tvoid, LINKd, pbd->storage_class);

        sc = sc->push();
        sc->stc &= ~STCstatic;              // not static
        sc->linkage = LINKd;

        funcDeclarationSemantic(pbd);

        sc->pop();
    }

    void visit(DtorDeclaration *dd)
    {
        //printf("DtorDeclaration::semantic() %s\n", dd->toChars());
        //printf("ident: %s, %s, %p, %p\n", dd->ident->toChars(), Id::dtor->toChars(), dd->ident, Id::dtor);
        if (dd->semanticRun >= PASSsemanticdone)
            return;
        if (dd->_scope)
        {
            sc = dd->_scope;
            dd->_scope = NULL;
        }

        dd->parent = sc->parent;
        Dsymbol *p = dd->toParent2();
        AggregateDeclaration *ad = p->isAggregateDeclaration();
        if (!ad)
        {
            error(dd->loc, "destructor can only be a member of aggregate, not %s %s",
                p->kind(), p->toChars());
            dd->type = Type::terror;
            dd->errors = true;
            return;
        }
        if (dd->ident == Id::dtor && dd->semanticRun < PASSsemantic)
            ad->dtors.push(dd);
        if (!dd->type)
            dd->type = new TypeFunction(ParameterList(), Type::tvoid, LINKd, dd->storage_class);

        sc = sc->push();
        sc->stc &= ~STCstatic;              // not a static destructor
        if (sc->linkage != LINKcpp)
            sc->linkage = LINKd;

        funcDeclarationSemantic(dd);

        sc->pop();
    }

    void visit(StaticCtorDeclaration *scd)
    {
        //printf("StaticCtorDeclaration::semantic()\n");
        if (scd->semanticRun >= PASSsemanticdone)
            return;
        if (scd->_scope)
        {
            sc = scd->_scope;
            scd->_scope = NULL;
        }

        scd->parent = sc->parent;
        Dsymbol *p = scd->parent->pastMixin();
        if (!p->isScopeDsymbol())
        {
            const char *s = (scd->isSharedStaticCtorDeclaration() ? "shared " : "");
            error(scd->loc, "%sstatic constructor can only be member of module/aggregate/template, not %s %s",
                s, p->kind(), p->toChars());
            scd->type = Type::terror;
            scd->errors = true;
            return;
        }
        if (!scd->type)
            scd->type = new TypeFunction(ParameterList(), Type::tvoid, LINKd, scd->storage_class);

        /* If the static ctor appears within a template instantiation,
         * it could get called multiple times by the module constructors
         * for different modules. Thus, protect it with a gate.
         */
        if (scd->isInstantiated() && scd->semanticRun < PASSsemantic)
        {
            /* Add this prefix to the function:
             *      static int gate;
             *      if (++gate != 1) return;
             * Note that this is not thread safe; should not have threads
             * during static construction.
             */
            VarDeclaration *v = new VarDeclaration(Loc(), Type::tint32, Id::gate, NULL);
            v->storage_class = STCtemp | (scd->isSharedStaticCtorDeclaration() ? STCstatic : STCtls);
            Statements *sa = new Statements();
            Statement *s = new ExpStatement(Loc(), v);
            sa->push(s);
            Expression *e = new IdentifierExp(Loc(), v->ident);
            e = new AddAssignExp(Loc(), e, new IntegerExp(1));
            e = new EqualExp(TOKnotequal, Loc(), e, new IntegerExp(1));
            s = new IfStatement(Loc(), NULL, e, new ReturnStatement(Loc(), NULL), NULL, Loc());
            sa->push(s);
            if (scd->fbody)
                sa->push(scd->fbody);
            scd->fbody = new CompoundStatement(Loc(), sa);
        }

        funcDeclarationSemantic(scd);

        // We're going to need ModuleInfo
        Module *m = scd->getModule();
        if (!m)
            m = sc->_module;
        if (m)
        {
            m->needmoduleinfo = 1;
            //printf("module1 %s needs moduleinfo\n", m->toChars());
        }
    }

    void visit(StaticDtorDeclaration *sdd)
    {
        if (sdd->semanticRun >= PASSsemanticdone)
            return;
        if (sdd->_scope)
        {
            sc = sdd->_scope;
            sdd->_scope = NULL;
        }

        sdd->parent = sc->parent;
        Dsymbol *p = sdd->parent->pastMixin();
        if (!p->isScopeDsymbol())
        {
            const char *s = (sdd->isSharedStaticDtorDeclaration() ? "shared " : "");
            error(sdd->loc, "%sstatic destructor can only be member of module/aggregate/template, not %s %s",
                s, p->kind(), p->toChars());
            sdd->type = Type::terror;
            sdd->errors = true;
            return;
        }
        if (!sdd->type)
            sdd->type = new TypeFunction(ParameterList(), Type::tvoid, LINKd, sdd->storage_class);

        /* If the static ctor appears within a template instantiation,
         * it could get called multiple times by the module constructors
         * for different modules. Thus, protect it with a gate.
         */
        if (sdd->isInstantiated() && sdd->semanticRun < PASSsemantic)
        {
            /* Add this prefix to the function:
             *      static int gate;
             *      if (--gate != 0) return;
             * Increment gate during constructor execution.
             * Note that this is not thread safe; should not have threads
             * during static destruction.
             */
            VarDeclaration *v = new VarDeclaration(Loc(), Type::tint32, Id::gate, NULL);
            v->storage_class = STCtemp | (sdd->isSharedStaticDtorDeclaration() ? STCstatic : STCtls);
            Statements *sa = new Statements();
            Statement *s = new ExpStatement(Loc(), v);
            sa->push(s);
            Expression *e = new IdentifierExp(Loc(), v->ident);
            e = new AddAssignExp(Loc(), e, new IntegerExp(-1));
            e = new EqualExp(TOKnotequal, Loc(), e, new IntegerExp(0));
            s = new IfStatement(Loc(), NULL, e, new ReturnStatement(Loc(), NULL), NULL, Loc());
            sa->push(s);
            if (sdd->fbody)
                sa->push(sdd->fbody);
            sdd->fbody = new CompoundStatement(Loc(), sa);
            sdd->vgate = v;
        }

        funcDeclarationSemantic(sdd);

        // We're going to need ModuleInfo
        Module *m = sdd->getModule();
        if (!m)
            m = sc->_module;
        if (m)
        {
            m->needmoduleinfo = 1;
            //printf("module2 %s needs moduleinfo\n", m->toChars());
        }
    }

    void visit(InvariantDeclaration *invd)
    {
        if (invd->semanticRun >= PASSsemanticdone)
            return;
        if (invd->_scope)
        {
            sc = invd->_scope;
            invd->_scope = NULL;
        }

        invd->parent = sc->parent;
        Dsymbol *p = invd->parent->pastMixin();
        AggregateDeclaration *ad = p->isAggregateDeclaration();
        if (!ad)
        {
            error(invd->loc, "invariant can only be a member of aggregate, not %s %s",
                p->kind(), p->toChars());
            invd->type = Type::terror;
            invd->errors = true;
            return;
        }
        if (invd->ident != Id::classInvariant &&
            invd->semanticRun < PASSsemantic &&
            !ad->isUnionDeclaration()           // users are on their own with union fields
           )
            ad->invs.push(invd);
        if (!invd->type)
            invd->type = new TypeFunction(ParameterList(), Type::tvoid, LINKd, invd->storage_class);

        sc = sc->push();
        sc->stc &= ~STCstatic;              // not a static invariant
        sc->stc |= STCconst;                // invariant() is always const
        sc->flags = (sc->flags & ~SCOPEcontract) | SCOPEinvariant;
        sc->linkage = LINKd;

        funcDeclarationSemantic(invd);

        sc->pop();
    }

    void visit(UnitTestDeclaration *utd)
    {
        if (utd->semanticRun >= PASSsemanticdone)
            return;
        if (utd->_scope)
        {
            sc = utd->_scope;
            utd->_scope = NULL;
        }

        utd->protection = sc->protection;

        utd->parent = sc->parent;
        Dsymbol *p = utd->parent->pastMixin();
        if (!p->isScopeDsymbol())
        {
            error(utd->loc, "unittest can only be a member of module/aggregate/template, not %s %s",
                p->kind(), p->toChars());
            utd->type = Type::terror;
            utd->errors = true;
            return;
        }

        if (global.params.useUnitTests)
        {
            if (!utd->type)
                utd->type = new TypeFunction(ParameterList(), Type::tvoid, LINKd, utd->storage_class);
            Scope *sc2 = sc->push();
            sc2->linkage = LINKd;
            funcDeclarationSemantic(utd);
            sc2->pop();
        }
    }

    void visit(NewDeclaration *nd)
    {
        //printf("NewDeclaration::semantic()\n");
        if (nd->semanticRun >= PASSsemanticdone)
            return;
        if (nd->_scope)
        {
            sc = nd->_scope;
            nd->_scope = NULL;
        }

        nd->parent = sc->parent;
        Dsymbol *p = nd->parent->pastMixin();
        if (!p->isAggregateDeclaration())
        {
            error(nd->loc, "allocator can only be a member of aggregate, not %s %s",
                p->kind(), p->toChars());
            nd->type = Type::terror;
            nd->errors = true;
            return;
        }
        Type *tret = Type::tvoid->pointerTo();
        if (!nd->type)
            nd->type = new TypeFunction(ParameterList(nd->parameters, nd->varargs), tret, LINKd, nd->storage_class);

        nd->type = typeSemantic(nd->type, nd->loc, sc);

        // Check that there is at least one argument of type size_t
        TypeFunction *tf = nd->type->toTypeFunction();
        if (tf->parameterList.length() < 1)
        {
            nd->error("at least one argument of type size_t expected");
        }
        else
        {
            Parameter *fparam = tf->parameterList[0];
            if (!fparam->type->equals(Type::tsize_t))
                nd->error("first argument must be type size_t, not %s", fparam->type->toChars());
        }

        funcDeclarationSemantic(nd);
    }

    void visit(DeleteDeclaration *deld)
    {
        //printf("DeleteDeclaration::semantic()\n");
        if (deld->semanticRun >= PASSsemanticdone)
            return;
        if (deld->_scope)
        {
            sc = deld->_scope;
            deld->_scope = NULL;
        }

        deld->parent = sc->parent;
        Dsymbol *p = deld->parent->pastMixin();
        if (!p->isAggregateDeclaration())
        {
            error(deld->loc, "deallocator can only be a member of aggregate, not %s %s",
                p->kind(), p->toChars());
            deld->type = Type::terror;
            deld->errors = true;
            return;
        }
        if (!deld->type)
            deld->type = new TypeFunction(ParameterList(deld->parameters), Type::tvoid, LINKd, deld->storage_class);

        deld->type = typeSemantic(deld->type, deld->loc, sc);

        // Check that there is only one argument of type void*
        TypeFunction *tf = deld->type->toTypeFunction();
        if (tf->parameterList.length() != 1)
        {
            deld->error("one argument of type void* expected");
        }
        else
        {
            Parameter *fparam = tf->parameterList[0];
            if (!fparam->type->equals(Type::tvoid->pointerTo()))
                deld->error("one argument of type void* expected, not %s", fparam->type->toChars());
        }

        funcDeclarationSemantic(deld);
    }

    void visit(StructDeclaration *sd)
    {
        //printf("StructDeclaration::semantic(this=%p, %s '%s', sizeok = %d)\n", sd, sd->parent->toChars(), sd->toChars(), sizeok);

        //static int count; if (++count == 20) halt();

        if (sd->semanticRun >= PASSsemanticdone)
            return;
        unsigned errors = global.errors;

        //printf("+StructDeclaration::semantic(this=%p, %s '%s', sizeok = %d)\n", sd, sd->parent->toChars(), sd->toChars(), sizeok);
        Scope *scx = NULL;
        if (sd->_scope)
        {
            sc = sd->_scope;
            scx = sd->_scope;            // save so we don't make redundant copies
            sd->_scope = NULL;
        }

        if (!sd->parent)
        {
            assert(sc->parent && sc->func);
            sd->parent = sc->parent;
        }
        assert(sd->parent && !sd->isAnonymous());

        if (sd->errors)
            sd->type = Type::terror;
        if (sd->semanticRun == PASSinit)
            sd->type = sd->type->addSTC(sc->stc | sd->storage_class);
        sd->type = typeSemantic(sd->type, sd->loc, sc);

        if (sd->type->ty == Tstruct && ((TypeStruct *)sd->type)->sym != sd)
        {
            TemplateInstance *ti = ((TypeStruct *)sd->type)->sym->isInstantiated();
            if (ti && isError(ti))
                ((TypeStruct *)sd->type)->sym = sd;
        }

        // Ungag errors when not speculative
        Ungag ungag = sd->ungagSpeculative();

        if (sd->semanticRun == PASSinit)
        {
            sd->protection = sc->protection;

            sd->alignment = sc->alignment();

            sd->storage_class |= sc->stc;
            if (sd->storage_class & STCdeprecated)
                sd->isdeprecated = true;
            if (sd->storage_class & STCabstract)
                sd->error("structs, unions cannot be abstract");
            sd->userAttribDecl = sc->userAttribDecl;

            if (sc->linkage == LINKcpp)
                sd->classKind = ClassKind::cpp;
        }
        else if (sd->symtab && !scx)
        {
            return;
        }
        sd->semanticRun = PASSsemantic;

        if (!sd->members)               // if opaque declaration
        {
            sd->semanticRun = PASSsemanticdone;
            return;
        }
        if (!sd->symtab)
        {
            sd->symtab = new DsymbolTable();

            for (size_t i = 0; i < sd->members->length; i++)
            {
                Dsymbol *s = (*sd->members)[i];
                //printf("adding member '%s' to '%s'\n", s->toChars(), sd->toChars());
                s->addMember(sc, sd);
            }
        }

        Scope *sc2 = sd->newScope(sc);

        /* Set scope so if there are forward references, we still might be able to
         * resolve individual members like enums.
         */
        for (size_t i = 0; i < sd->members->length; i++)
        {
            Dsymbol *s = (*sd->members)[i];
            //printf("struct: setScope %s %s\n", s->kind(), s->toChars());
            s->setScope(sc2);
        }

        for (size_t i = 0; i < sd->members->length; i++)
        {
            Dsymbol *s = (*sd->members)[i];
            s->importAll(sc2);
        }

        for (size_t i = 0; i < sd->members->length; i++)
        {
            Dsymbol *s = (*sd->members)[i];
            dsymbolSemantic(s, sc2);
        }

        if (!sd->determineFields())
        {
            assert(sd->type->ty == Terror);
            sc2->pop();
            sd->semanticRun = PASSsemanticdone;
            return;
        }

        /* Following special member functions creation needs semantic analysis
         * completion of sub-structs in each field types. For example, buildDtor
         * needs to check existence of elaborate dtor in type of each fields.
         * See the case in compilable/test14838.d
         */
        for (size_t i = 0; i < sd->fields.length; i++)
        {
            VarDeclaration *v = sd->fields[i];
            Type *tb = v->type->baseElemOf();
            if (tb->ty != Tstruct)
                continue;
            StructDeclaration *sdec = ((TypeStruct *)tb)->sym;
            if (sdec->semanticRun >= PASSsemanticdone)
                continue;

            sc2->pop();

            sd->_scope = scx ? scx : sc->copy();
            sd->_scope->setNoFree();
            sd->_scope->_module->addDeferredSemantic(sd);

            //printf("\tdeferring %s\n", sd->toChars());
            return;
        }

        /* Look for special member functions.
         */
        sd->aggNew =       (NewDeclaration *)sd->search(Loc(), Id::classNew);
        sd->aggDelete = (DeleteDeclaration *)sd->search(Loc(), Id::classDelete);

        // Look for the constructor
        sd->ctor = sd->searchCtor();

        sd->dtor = buildDtor(sd, sc2);
        sd->postblit = buildPostBlit(sd, sc2);

        buildOpAssign(sd, sc2);
        buildOpEquals(sd, sc2);

        if (global.params.useTypeInfo && Type::dtypeinfo)  // these functions are used for TypeInfo
        {
            sd->xeq = buildXopEquals(sd, sc2);
            sd->xcmp = buildXopCmp(sd, sc2);
            sd->xhash = buildXtoHash(sd, sc2);
        }

        sd->inv = buildInv(sd, sc2);

        Module::dprogress++;
        sd->semanticRun = PASSsemanticdone;
        //printf("-StructDeclaration::semantic(this=%p, '%s')\n", sd, sd->toChars());

        sc2->pop();

        if (sd->ctor)
        {
            Dsymbol *scall = sd->search(Loc(), Id::call);
            if (scall)
            {
                unsigned xerrors = global.startGagging();
                sc = sc->push();
                sc->tinst = NULL;
                sc->minst = NULL;
                FuncDeclaration *fcall = resolveFuncCall(sd->loc, sc, scall, NULL, NULL, NULL, 1);
                sc = sc->pop();
                global.endGagging(xerrors);

                if (fcall && fcall->isStatic())
                {
                    sd->error(fcall->loc, "`static opCall` is hidden by constructors and can never be called");
                    errorSupplemental(fcall->loc, "Please use a factory method instead, or replace all constructors with `static opCall`.");
                }
            }
        }

        if (sd->type->ty == Tstruct && ((TypeStruct *)sd->type)->sym != sd)
        {
            // https://issues.dlang.org/show_bug.cgi?id=19024
            StructDeclaration *sym = ((TypeStruct *)sd->type)->sym;
            sd->error("already exists at %s. Perhaps in another function with the same name?", sym->loc.toChars());
        }

        if (global.errors != errors)
        {
            // The type is no good.
            sd->type = Type::terror;
            sd->errors = true;
            if (sd->deferred)
                sd->deferred->errors = true;
        }

        if (sd->deferred && !global.gag)
        {
            semantic2(sd->deferred, sc);
            semantic3(sd->deferred, sc);
        }
    }

    void interfaceSemantic(ClassDeclaration *cd)
    {
        cd->vtblInterfaces = new BaseClasses();
        cd->vtblInterfaces->reserve(cd->interfaces.length);

        for (size_t i = 0; i < cd->interfaces.length; i++)
        {
            BaseClass *b = cd->interfaces.ptr[i];
            cd->vtblInterfaces->push(b);
            b->copyBaseInterfaces(cd->vtblInterfaces);
        }
    }

    void visit(ClassDeclaration *cldec)
    {
        //printf("ClassDeclaration::semantic(%s), type = %p, sizeok = %d, this = %p\n", cldec->toChars(), cldec->type, sizeok, cldec);
        //printf("\tparent = %p, '%s'\n", sc->parent, sc->parent ? sc->parent->toChars() : "");
        //printf("sc->stc = %x\n", sc->stc);

        //{ static int n;  if (++n == 20) *(char*)0=0; }

        if (cldec->semanticRun >= PASSsemanticdone)
            return;
        unsigned errors = global.errors;

        //printf("+ClassDeclaration::semantic(%s), type = %p, sizeok = %d, this = %p\n", cldec->toChars(), cldec->type, sizeok, cldec);

        Scope *scx = NULL;
        if (cldec->_scope)
        {
            sc = cldec->_scope;
            scx = cldec->_scope;            // save so we don't make redundant copies
            cldec->_scope = NULL;
        }

        if (!cldec->parent)
        {
            assert(sc->parent);
            cldec->parent = sc->parent;
        }

        if (cldec->errors)
            cldec->type = Type::terror;
        cldec->type = typeSemantic(cldec->type, cldec->loc, sc);

        if (cldec->type->ty == Tclass && ((TypeClass *)cldec->type)->sym != cldec)
        {
            TemplateInstance *ti = ((TypeClass *)cldec->type)->sym->isInstantiated();
            if (ti && isError(ti))
                ((TypeClass *)cldec->type)->sym = cldec;
        }

        // Ungag errors when not speculative
        Ungag ungag = cldec->ungagSpeculative();

        if (cldec->semanticRun == PASSinit)
        {
            cldec->protection = sc->protection;

            cldec->storage_class |= sc->stc;
            if (cldec->storage_class & STCdeprecated)
                cldec->isdeprecated = true;
            if (cldec->storage_class & STCauto)
                cldec->error("storage class `auto` is invalid when declaring a class, did you mean to use `scope`?");
            if (cldec->storage_class & STCscope)
                cldec->isscope = true;
            if (cldec->storage_class & STCabstract)
                cldec->isabstract = ABSyes;

            cldec->userAttribDecl = sc->userAttribDecl;

            if (sc->linkage == LINKcpp)
                cldec->classKind = ClassKind::cpp;
            if (sc->linkage == LINKobjc)
                objc()->setObjc(cldec);
        }
        else if (cldec->symtab && !scx)
        {
            return;
        }
        cldec->semanticRun = PASSsemantic;

        if (cldec->baseok < BASEOKdone)
        {
            cldec->baseok = BASEOKin;

            // Expand any tuples in baseclasses[]
            for (size_t i = 0; i < cldec->baseclasses->length; )
            {
                BaseClass *b = (*cldec->baseclasses)[i];
                b->type = resolveBase(cldec, sc, scx, b->type);

                Type *tb = b->type->toBasetype();
                if (tb->ty == Ttuple)
                {
                    TypeTuple *tup = (TypeTuple *)tb;
                    cldec->baseclasses->remove(i);
                    size_t dim = Parameter::dim(tup->arguments);
                    for (size_t j = 0; j < dim; j++)
                    {
                        Parameter *arg = Parameter::getNth(tup->arguments, j);
                        b = new BaseClass(arg->type);
                        cldec->baseclasses->insert(i + j, b);
                    }
                }
                else
                    i++;
            }

            if (cldec->baseok >= BASEOKdone)
            {
                //printf("%s already semantic analyzed, semanticRun = %d\n", cldec->toChars(), cldec->semanticRun);
                if (cldec->semanticRun >= PASSsemanticdone)
                    return;
                goto Lancestorsdone;
            }

            // See if there's a base class as first in baseclasses[]
            if (cldec->baseclasses->length)
            {
                BaseClass *b = (*cldec->baseclasses)[0];
                Type *tb = b->type->toBasetype();
                TypeClass *tc = (tb->ty == Tclass) ? (TypeClass *)tb : NULL;
                if (!tc)
                {
                    if (b->type != Type::terror)
                        cldec->error("base type must be class or interface, not %s", b->type->toChars());
                    cldec->baseclasses->remove(0);
                    goto L7;
                }

                if (tc->sym->isDeprecated())
                {
                    if (!cldec->isDeprecated())
                    {
                        // Deriving from deprecated class makes this one deprecated too
                        cldec->isdeprecated = true;

                        tc->checkDeprecated(cldec->loc, sc);
                    }
                }

                if (tc->sym->isInterfaceDeclaration())
                    goto L7;

                for (ClassDeclaration *cdb = tc->sym; cdb; cdb = cdb->baseClass)
                {
                    if (cdb == cldec)
                    {
                        cldec->error("circular inheritance");
                        cldec->baseclasses->remove(0);
                        goto L7;
                    }
                }

                /* Bugzilla 11034: Essentially, class inheritance hierarchy
                 * and instance size of each classes are orthogonal information.
                 * Therefore, even if tc->sym->sizeof == SIZEOKnone,
                 * we need to set baseClass field for class covariance check.
                 */
                cldec->baseClass = tc->sym;
                b->sym = cldec->baseClass;

                if (tc->sym->baseok < BASEOKdone)
                    resolveBase(cldec, sc, scx, tc->sym); // Try to resolve forward reference
                if (tc->sym->baseok < BASEOKdone)
                {
                    //printf("\ttry later, forward reference of base class %s\n", tc->sym->toChars());
                    if (tc->sym->_scope)
                        tc->sym->_scope->_module->addDeferredSemantic(tc->sym);
                    cldec->baseok = BASEOKnone;
                }
             L7: ;
            }

            // Treat the remaining entries in baseclasses as interfaces
            // Check for errors, handle forward references
            for (size_t i = (cldec->baseClass ? 1 : 0); i < cldec->baseclasses->length; )
            {
                BaseClass *b = (*cldec->baseclasses)[i];
                Type *tb = b->type->toBasetype();
                TypeClass *tc = (tb->ty == Tclass) ? (TypeClass *)tb : NULL;
                if (!tc || !tc->sym->isInterfaceDeclaration())
                {
                    if (b->type != Type::terror)
                        cldec->error("base type must be interface, not %s", b->type->toChars());
                    cldec->baseclasses->remove(i);
                    continue;
                }

                // Check for duplicate interfaces
                for (size_t j = (cldec->baseClass ? 1 : 0); j < i; j++)
                {
                    BaseClass *b2 = (*cldec->baseclasses)[j];
                    if (b2->sym == tc->sym)
                    {
                        cldec->error("inherits from duplicate interface %s", b2->sym->toChars());
                        cldec->baseclasses->remove(i);
                        continue;
                    }
                }

                if (tc->sym->isDeprecated())
                {
                    if (!cldec->isDeprecated())
                    {
                        // Deriving from deprecated class makes this one deprecated too
                        cldec->isdeprecated = true;

                        tc->checkDeprecated(cldec->loc, sc);
                    }
                }

                b->sym = tc->sym;

                if (tc->sym->baseok < BASEOKdone)
                    resolveBase(cldec, sc, scx, tc->sym); // Try to resolve forward reference
                if (tc->sym->baseok < BASEOKdone)
                {
                    //printf("\ttry later, forward reference of base %s\n", tc->sym->toChars());
                    if (tc->sym->_scope)
                        tc->sym->_scope->_module->addDeferredSemantic(tc->sym);
                    cldec->baseok = BASEOKnone;
                }
                i++;
            }
            if (cldec->baseok == BASEOKnone)
            {
                // Forward referencee of one or more bases, try again later
                cldec->_scope = scx ? scx : sc->copy();
                cldec->_scope->setNoFree();
                cldec->_scope->_module->addDeferredSemantic(cldec);
                //printf("\tL%d semantic('%s') failed due to forward references\n", __LINE__, cldec->toChars());
                return;
            }
            cldec->baseok = BASEOKdone;

            // If no base class, and this is not an Object, use Object as base class
            if (!cldec->baseClass && cldec->ident != Id::Object && !cldec->isCPPclass())
            {
                if (!ClassDeclaration::object || ClassDeclaration::object->errors)
                    badObjectDotD(cldec);

                Type *t = ClassDeclaration::object->type;
                t = typeSemantic(t, cldec->loc, sc)->toBasetype();
                if (t->ty == Terror)
                    badObjectDotD(cldec);
                assert(t->ty == Tclass);
                TypeClass *tc = (TypeClass *)t;

                BaseClass *b = new BaseClass(tc);
                cldec->baseclasses->shift(b);

                cldec->baseClass = tc->sym;
                assert(!cldec->baseClass->isInterfaceDeclaration());
                b->sym = cldec->baseClass;
            }
            if (cldec->baseClass)
            {
                if (cldec->baseClass->storage_class & STCfinal)
                    cldec->error("cannot inherit from final class %s", cldec->baseClass->toChars());

                // Inherit properties from base class
                if (cldec->baseClass->isCOMclass())
                    cldec->com = true;
                if (cldec->baseClass->isCPPclass())
                    cldec->classKind = ClassKind::cpp;
                if (cldec->baseClass->isscope)
                    cldec->isscope = true;
                cldec->enclosing = cldec->baseClass->enclosing;
                cldec->storage_class |= cldec->baseClass->storage_class & STC_TYPECTOR;
            }

            cldec->interfaces.length = cldec->baseclasses->length - (cldec->baseClass ? 1 : 0);
            cldec->interfaces.ptr = cldec->baseclasses->tdata() + (cldec->baseClass ? 1 : 0);

            for (size_t i = 0; i < cldec->interfaces.length; i++)
            {
                BaseClass *b = cldec->interfaces.ptr[i];
                // If this is an interface, and it derives from a COM interface,
                // then this is a COM interface too.
                if (b->sym->isCOMinterface())
                    cldec->com = true;
                if (cldec->isCPPclass() && !b->sym->isCPPinterface())
                {
                    error(cldec->loc, "C++ class `%s` cannot implement D interface `%s`",
                        cldec->toPrettyChars(), b->sym->toPrettyChars());
                }
            }

            interfaceSemantic(cldec);
        }
    Lancestorsdone:
        //printf("\tClassDeclaration::semantic(%s) baseok = %d\n", cldec->toChars(), cldec->baseok);

        if (!cldec->members)               // if opaque declaration
        {
            cldec->semanticRun = PASSsemanticdone;
            return;
        }
        if (!cldec->symtab)
        {
            cldec->symtab = new DsymbolTable();

            /* Bugzilla 12152: The semantic analysis of base classes should be finished
             * before the members semantic analysis of this class, in order to determine
             * vtbl in this class. However if a base class refers the member of this class,
             * it can be resolved as a normal forward reference.
             * Call addMember() and setScope() to make this class members visible from the base classes.
             */
            for (size_t i = 0; i < cldec->members->length; i++)
            {
                Dsymbol *s = (*cldec->members)[i];
                s->addMember(sc, cldec);
            }

            Scope *sc2 = cldec->newScope(sc);

            /* Set scope so if there are forward references, we still might be able to
             * resolve individual members like enums.
             */
            for (size_t i = 0; i < cldec->members->length; i++)
            {
                Dsymbol *s = (*cldec->members)[i];
                //printf("[%d] setScope %s %s, sc2 = %p\n", i, s->kind(), s->toChars(), sc2);
                s->setScope(sc2);
            }

            sc2->pop();
        }

        for (size_t i = 0; i < cldec->baseclasses->length; i++)
        {
            BaseClass *b = (*cldec->baseclasses)[i];
            Type *tb = b->type->toBasetype();
            assert(tb->ty == Tclass);
            TypeClass *tc = (TypeClass *)tb;

            if (tc->sym->semanticRun < PASSsemanticdone)
            {
                // Forward referencee of one or more bases, try again later
                cldec->_scope = scx ? scx : sc->copy();
                cldec->_scope->setNoFree();
                if (tc->sym->_scope)
                    tc->sym->_scope->_module->addDeferredSemantic(tc->sym);
                cldec->_scope->_module->addDeferredSemantic(cldec);
                //printf("\tL%d semantic('%s') failed due to forward references\n", __LINE__, cldec->toChars());
                return;
            }
        }

        if (cldec->baseok == BASEOKdone)
        {
            cldec->baseok = BASEOKsemanticdone;

            // initialize vtbl
            if (cldec->baseClass)
            {
                if (cldec->isCPPclass() && cldec->baseClass->vtbl.length == 0)
                {
                    cldec->error("C++ base class %s needs at least one virtual function", cldec->baseClass->toChars());
                }

                // Copy vtbl[] from base class
                cldec->vtbl.setDim(cldec->baseClass->vtbl.length);
                memcpy(cldec->vtbl.tdata(), cldec->baseClass->vtbl.tdata(), sizeof(void *) * cldec->vtbl.length);

                cldec->vthis = cldec->baseClass->vthis;
            }
            else
            {
                // No base class, so this is the root of the class hierarchy
                cldec->vtbl.setDim(0);
                if (cldec->vtblOffset())
                    cldec->vtbl.push(cldec);            // leave room for classinfo as first member
            }

            /* If this is a nested class, add the hidden 'this'
             * member which is a pointer to the enclosing scope.
             */
            if (cldec->vthis)              // if inheriting from nested class
            {
                // Use the base class's 'this' member
                if (cldec->storage_class & STCstatic)
                    cldec->error("static class cannot inherit from nested class %s", cldec->baseClass->toChars());
                if (cldec->toParent2() != cldec->baseClass->toParent2() &&
                    (!cldec->toParent2() ||
                     !cldec->baseClass->toParent2()->getType() ||
                     !cldec->baseClass->toParent2()->getType()->isBaseOf(cldec->toParent2()->getType(), NULL)))
                {
                    if (cldec->toParent2())
                    {
                        cldec->error("is nested within %s, but super class %s is nested within %s",
                            cldec->toParent2()->toChars(),
                            cldec->baseClass->toChars(),
                            cldec->baseClass->toParent2()->toChars());
                    }
                    else
                    {
                        cldec->error("is not nested, but super class %s is nested within %s",
                            cldec->baseClass->toChars(),
                            cldec->baseClass->toParent2()->toChars());
                    }
                    cldec->enclosing = NULL;
                }
            }
            else
                cldec->makeNested();
        }

        Scope *sc2 = cldec->newScope(sc);

        for (size_t i = 0; i < cldec->members->length; i++)
        {
            Dsymbol *s = (*cldec->members)[i];
            s->importAll(sc2);
        }

        // Note that members.length can grow due to tuple expansion during semantic()
        for (size_t i = 0; i < cldec->members->length; i++)
        {
            Dsymbol *s = (*cldec->members)[i];
            dsymbolSemantic(s, sc2);
        }

        if (!cldec->determineFields())
        {
            assert(cldec->type == Type::terror);
            sc2->pop();
            return;
        }

        /* Following special member functions creation needs semantic analysis
         * completion of sub-structs in each field types.
         */
        for (size_t i = 0; i < cldec->fields.length; i++)
        {
            VarDeclaration *v = cldec->fields[i];
            Type *tb = v->type->baseElemOf();
            if (tb->ty != Tstruct)
                continue;
            StructDeclaration *sd = ((TypeStruct *)tb)->sym;
            if (sd->semanticRun >= PASSsemanticdone)
                continue;

            sc2->pop();

            cldec->_scope = scx ? scx : sc->copy();
            cldec->_scope->setNoFree();
            cldec->_scope->_module->addDeferredSemantic(cldec);
            //printf("\tdeferring %s\n", cldec->toChars());
            return;
        }

        /* Look for special member functions.
         * They must be in this class, not in a base class.
         */

        // Can be in base class
        cldec->aggNew    =    (NewDeclaration *)cldec->search(Loc(), Id::classNew);
        cldec->aggDelete = (DeleteDeclaration *)cldec->search(Loc(), Id::classDelete);

        // Look for the constructor
        cldec->ctor = cldec->searchCtor();

        if (!cldec->ctor && cldec->noDefaultCtor)
        {
            // A class object is always created by constructor, so this check is legitimate.
            for (size_t i = 0; i < cldec->fields.length; i++)
            {
                VarDeclaration *v = cldec->fields[i];
                if (v->storage_class & STCnodefaultctor)
                    error(v->loc, "field %s must be initialized in constructor", v->toChars());
            }
        }

        // If this class has no constructor, but base class has a default
        // ctor, create a constructor:
        //    this() { }
        if (!cldec->ctor && cldec->baseClass && cldec->baseClass->ctor)
        {
            FuncDeclaration *fd = resolveFuncCall(cldec->loc, sc2, cldec->baseClass->ctor, NULL, cldec->type, NULL, 1);
            if (!fd) // try shared base ctor instead
                fd = resolveFuncCall(cldec->loc, sc2, cldec->baseClass->ctor, NULL, cldec->type->sharedOf(), NULL, 1);
            if (fd && !fd->errors)
            {
                //printf("Creating default this(){} for class %s\n", cldec->toChars());
                TypeFunction *btf = fd->type->toTypeFunction();
                TypeFunction *tf = new TypeFunction(ParameterList(), NULL, LINKd, fd->storage_class);
                tf->mod = btf->mod;
                tf->purity = btf->purity;
                tf->isnothrow = btf->isnothrow;
                tf->isnogc = btf->isnogc;
                tf->trust = btf->trust;

                CtorDeclaration *ctor = new CtorDeclaration(cldec->loc, Loc(), 0, tf);
                ctor->fbody = new CompoundStatement(Loc(), new Statements());

                cldec->members->push(ctor);
                ctor->addMember(sc, cldec);
                dsymbolSemantic(ctor, sc2);

                cldec->ctor = ctor;
                cldec->defaultCtor = ctor;
            }
            else
            {
                cldec->error("cannot implicitly generate a default ctor when base class %s is missing a default ctor",
                    cldec->baseClass->toPrettyChars());
            }
        }

        cldec->dtor = buildDtor(cldec, sc2);

        if (FuncDeclaration *f = hasIdentityOpAssign(cldec, sc2))
        {
            if (!(f->storage_class & STCdisable))
                cldec->error(f->loc, "identity assignment operator overload is illegal");
        }

        cldec->inv = buildInv(cldec, sc2);

        Module::dprogress++;
        cldec->semanticRun = PASSsemanticdone;
        //printf("-ClassDeclaration.semantic(%s), type = %p\n", cldec->toChars(), cldec->type);
        //members.print();

        sc2->pop();

        if (cldec->type->ty == Tclass && ((TypeClass *)cldec->type)->sym != cldec)
        {
            // https://issues.dlang.org/show_bug.cgi?id=17492
            ClassDeclaration *cd = ((TypeClass *)cldec->type)->sym;
            cldec->error("already exists at %s. Perhaps in another function with the same name?", cd->loc.toChars());
        }

        if (global.errors != errors)
        {
            // The type is no good.
            cldec->type = Type::terror;
            cldec->errors = true;
            if (cldec->deferred)
                cldec->deferred->errors = true;
        }

        // Verify fields of a synchronized class are not public
        if (cldec->storage_class & STCsynchronized)
        {
            for (size_t i = 0; i < cldec->fields.length; i++)
            {
                VarDeclaration *vd = cldec->fields[i];
                if (!vd->isThisDeclaration() &&
                    !vd->prot().isMoreRestrictiveThan(Prot(Prot::public_)))
                {
                    vd->error("Field members of a synchronized class cannot be %s",
                        protectionToChars(vd->prot().kind));
                }
            }
        }

        if (cldec->deferred && !global.gag)
        {
            semantic2(cldec->deferred, sc);
            semantic3(cldec->deferred, sc);
        }
        //printf("-ClassDeclaration::semantic(%s), type = %p, sizeok = %d, this = %p\n", cldec->toChars(), cldec->type, sizeok, cldec);
    }

    void visit(InterfaceDeclaration *idec)
    {
        //printf("InterfaceDeclaration::semantic(%s), type = %p\n", idec->toChars(), idec->type);
        if (idec->semanticRun >= PASSsemanticdone)
            return;
        unsigned errors = global.errors;

        //printf("+InterfaceDeclaration.semantic(%s), type = %p\n", idec->toChars(), idec->type);

        Scope *scx = NULL;
        if (idec->_scope)
        {
            sc = idec->_scope;
            scx = idec->_scope;            // save so we don't make redundant copies
            idec->_scope = NULL;
        }

        if (!idec->parent)
        {
            assert(sc->parent && sc->func);
            idec->parent = sc->parent;
        }
        assert(idec->parent && !idec->isAnonymous());

        if (idec->errors)
            idec->type = Type::terror;
        idec->type = typeSemantic(idec->type, idec->loc, sc);

        if (idec->type->ty == Tclass && ((TypeClass *)idec->type)->sym != idec)
        {
            TemplateInstance *ti = ((TypeClass *)idec->type)->sym->isInstantiated();
            if (ti && isError(ti))
                ((TypeClass *)idec->type)->sym = idec;
        }

        // Ungag errors when not speculative
        Ungag ungag = idec->ungagSpeculative();

        if (idec->semanticRun == PASSinit)
        {
            idec->protection = sc->protection;

            idec->storage_class |= sc->stc;
            if (idec->storage_class & STCdeprecated)
                idec->isdeprecated = true;

            idec->userAttribDecl = sc->userAttribDecl;
        }
        else if (idec->symtab)
        {
            if (idec->sizeok == SIZEOKdone || !scx)
            {
                idec->semanticRun = PASSsemanticdone;
                return;
            }
        }
        idec->semanticRun = PASSsemantic;

        if (idec->baseok < BASEOKdone)
        {
            idec->baseok = BASEOKin;

            // Expand any tuples in baseclasses[]
            for (size_t i = 0; i < idec->baseclasses->length; )
            {
                BaseClass *b = (*idec->baseclasses)[i];
                b->type = resolveBase(idec, sc, scx, b->type);

                Type *tb = b->type->toBasetype();
                if (tb->ty == Ttuple)
                {
                    TypeTuple *tup = (TypeTuple *)tb;
                    idec->baseclasses->remove(i);
                    size_t dim = Parameter::dim(tup->arguments);
                    for (size_t j = 0; j < dim; j++)
                    {
                        Parameter *arg = Parameter::getNth(tup->arguments, j);
                        b = new BaseClass(arg->type);
                        idec->baseclasses->insert(i + j, b);
                    }
                }
                else
                    i++;
            }

            if (idec->baseok >= BASEOKdone)
            {
                //printf("%s already semantic analyzed, semanticRun = %d\n", idec->toChars(), idec->semanticRun);
                if (idec->semanticRun >= PASSsemanticdone)
                    return;
                goto Lancestorsdone;
            }

            if (!idec->baseclasses->length && sc->linkage == LINKcpp)
                idec->classKind = ClassKind::cpp;
            if (sc->linkage == LINKobjc)
                objc()->setObjc(idec);

            // Check for errors, handle forward references
            for (size_t i = 0; i < idec->baseclasses->length; )
            {
                BaseClass *b = (*idec->baseclasses)[i];
                Type *tb = b->type->toBasetype();
                TypeClass *tc = (tb->ty == Tclass) ? (TypeClass *)tb : NULL;
                if (!tc || !tc->sym->isInterfaceDeclaration())
                {
                    if (b->type != Type::terror)
                        idec->error("base type must be interface, not %s", b->type->toChars());
                    idec->baseclasses->remove(i);
                    continue;
                }

                // Check for duplicate interfaces
                for (size_t j = 0; j < i; j++)
                {
                    BaseClass *b2 = (*idec->baseclasses)[j];
                    if (b2->sym == tc->sym)
                    {
                        idec->error("inherits from duplicate interface %s", b2->sym->toChars());
                        idec->baseclasses->remove(i);
                        continue;
                    }
                }

                if (tc->sym == idec || idec->isBaseOf2(tc->sym))
                {
                    idec->error("circular inheritance of interface");
                    idec->baseclasses->remove(i);
                    continue;
                }

                if (tc->sym->isDeprecated())
                {
                    if (!idec->isDeprecated())
                    {
                        // Deriving from deprecated class makes this one deprecated too
                        idec->isdeprecated = true;

                        tc->checkDeprecated(idec->loc, sc);
                    }
                }

                b->sym = tc->sym;

                if (tc->sym->baseok < BASEOKdone)
                    resolveBase(idec, sc, scx, tc->sym); // Try to resolve forward reference
                if (tc->sym->baseok < BASEOKdone)
                {
                    //printf("\ttry later, forward reference of base %s\n", tc->sym->toChars());
                    if (tc->sym->_scope)
                        tc->sym->_scope->_module->addDeferredSemantic(tc->sym);
                    idec->baseok = BASEOKnone;
                }
                i++;
            }
            if (idec->baseok == BASEOKnone)
            {
                // Forward referencee of one or more bases, try again later
                idec->_scope = scx ? scx : sc->copy();
                idec->_scope->setNoFree();
                idec->_scope->_module->addDeferredSemantic(idec);
                return;
            }
            idec->baseok = BASEOKdone;

            idec->interfaces.length = idec->baseclasses->length;
            idec->interfaces.ptr = idec->baseclasses->tdata();

            for (size_t i = 0; i < idec->interfaces.length; i++)
            {
                BaseClass *b = idec->interfaces.ptr[i];
                // If this is an interface, and it derives from a COM interface,
                // then this is a COM interface too.
                if (b->sym->isCOMinterface())
                    idec->com = true;
                if (b->sym->isCPPinterface())
                    idec->classKind = ClassKind::cpp;
            }

            interfaceSemantic(idec);
        }
    Lancestorsdone:

        if (!idec->members)               // if opaque declaration
        {
            idec->semanticRun = PASSsemanticdone;
            return;
        }
        if (!idec->symtab)
            idec->symtab = new DsymbolTable();

        for (size_t i = 0; i < idec->baseclasses->length; i++)
        {
            BaseClass *b = (*idec->baseclasses)[i];
            Type *tb = b->type->toBasetype();
            assert(tb->ty == Tclass);
            TypeClass *tc = (TypeClass *)tb;

            if (tc->sym->semanticRun < PASSsemanticdone)
            {
                // Forward referencee of one or more bases, try again later
                idec->_scope = scx ? scx : sc->copy();
                idec->_scope->setNoFree();
                if (tc->sym->_scope)
                    tc->sym->_scope->_module->addDeferredSemantic(tc->sym);
                idec->_scope->_module->addDeferredSemantic(idec);
                return;
            }
        }

        if (idec->baseok == BASEOKdone)
        {
            idec->baseok = BASEOKsemanticdone;

            // initialize vtbl
            if (idec->vtblOffset())
                idec->vtbl.push(idec);                // leave room at vtbl[0] for classinfo

            // Cat together the vtbl[]'s from base cldec->interfaces
            for (size_t i = 0; i < idec->interfaces.length; i++)
            {
                BaseClass *b = idec->interfaces.ptr[i];

                // Skip if b has already appeared
                for (size_t k = 0; k < i; k++)
                {
                    if (b == idec->interfaces.ptr[k])
                        goto Lcontinue;
                }

                // Copy vtbl[] from base class
                if (b->sym->vtblOffset())
                {
                    size_t d = b->sym->vtbl.length;
                    if (d > 1)
                    {
                        idec->vtbl.reserve(d - 1);
                        for (size_t j = 1; j < d; j++)
                            idec->vtbl.push(b->sym->vtbl[j]);
                    }
                }
                else
                {
                    idec->vtbl.append(&b->sym->vtbl);
                }

              Lcontinue:
                ;
            }
        }

        for (size_t i = 0; i < idec->members->length; i++)
        {
            Dsymbol *s = (*idec->members)[i];
            s->addMember(sc, idec);
        }

        Scope *sc2 = idec->newScope(sc);

        /* Set scope so if there are forward references, we still might be able to
         * resolve individual members like enums.
         */
        for (size_t i = 0; i < idec->members->length; i++)
        {
            Dsymbol *s = (*idec->members)[i];
            //printf("setScope %s %s\n", s->kind(), s->toChars());
            s->setScope(sc2);
        }

        for (size_t i = 0; i < idec->members->length; i++)
        {
            Dsymbol *s = (*idec->members)[i];
            s->importAll(sc2);
        }

        for (size_t i = 0; i < idec->members->length; i++)
        {
            Dsymbol *s = (*idec->members)[i];
            dsymbolSemantic(s, sc2);
        }

        Module::dprogress++;
        idec->semanticRun = PASSsemanticdone;
        //printf("-InterfaceDeclaration.semantic(%s), type = %p\n", idec->toChars(), idec->type);
        //members->print();

        sc2->pop();

        if (global.errors != errors)
        {
            // The type is no good.
            idec->type = Type::terror;
        }

        assert(idec->type->ty != Tclass || ((TypeClass *)idec->type)->sym == idec);
    }
};

void templateInstanceSemantic(TemplateInstance *tempinst, Scope *sc, Expressions *fargs)
{
    //printf("[%s] TemplateInstance::semantic('%s', this=%p, gag = %d, sc = %p)\n", tempinst->loc.toChars(), tempinst->toChars(), tempinst, global.gag, sc);
    if (tempinst->inst)           // if semantic() was already run
    {
        return;
    }
    if (tempinst->semanticRun != PASSinit)
    {
        Ungag ungag(global.gag);
        if (!tempinst->gagged)
            global.gag = 0;
        tempinst->error(tempinst->loc, "recursive template expansion");
        if (tempinst->gagged)
            tempinst->semanticRun = PASSinit;
        else
            tempinst->inst = tempinst;
        tempinst->errors = true;
        return;
    }

    // Get the enclosing template instance from the scope tinst
    tempinst->tinst = sc->tinst;

    // Get the instantiating module from the scope minst
    tempinst->minst = sc->minst;
    // Bugzilla 10920: If the enclosing function is non-root symbol,
    // this instance should be speculative.
    if (!tempinst->tinst && sc->func && sc->func->inNonRoot())
    {
        tempinst->minst = NULL;
    }

    tempinst->gagged = (global.gag > 0);

    tempinst->semanticRun = PASSsemantic;

    /* Find template declaration first,
     * then run semantic on each argument (place results in tiargs[]),
     * last find most specialized template from overload list/set.
     */
    if (!tempinst->findTempDecl(sc, NULL) ||
        !tempinst->semanticTiargs(sc) ||
        !tempinst->findBestMatch(sc, fargs))
    {
Lerror:
        if (tempinst->gagged)
        {
            // Bugzilla 13220: Rollback status for later semantic re-running.
            tempinst->semanticRun = PASSinit;
        }
        else
            tempinst->inst = tempinst;
        tempinst->errors = true;
        return;
    }
    TemplateDeclaration *tempdecl = tempinst->tempdecl->isTemplateDeclaration();
    assert(tempdecl);

    // If tempdecl is a mixin, disallow it
    if (tempdecl->ismixin)
    {
        tempinst->error("mixin templates are not regular templates");
        goto Lerror;
    }

    tempinst->hasNestedArgs(tempinst->tiargs, tempdecl->isstatic);
    if (tempinst->errors)
        goto Lerror;

    /* See if there is an existing TemplateInstantiation that already
     * implements the typeargs. If so, just refer to that one instead.
     */
    tempinst->inst = tempdecl->findExistingInstance(tempinst, fargs);
    TemplateInstance *errinst = NULL;
    if (!tempinst->inst)
    {
        // So, we need to implement 'this' instance.
    }
    else if (tempinst->inst->gagged && !tempinst->gagged && tempinst->inst->errors)
    {
        // If the first instantiation had failed, re-run semantic,
        // so that error messages are shown.
        errinst = tempinst->inst;
    }
    else
    {
        // It's a match
        tempinst->parent = tempinst->inst->parent;
        tempinst->errors = tempinst->inst->errors;

        // If both this and the previous instantiation were gagged,
        // use the number of errors that happened last time.
        global.errors += tempinst->errors;
        global.gaggedErrors += tempinst->errors;

        // If the first instantiation was gagged, but this is not:
        if (tempinst->inst->gagged)
        {
            // It had succeeded, mark it is a non-gagged instantiation,
            // and reuse it.
            tempinst->inst->gagged = tempinst->gagged;
        }

        tempinst->tnext = tempinst->inst->tnext;
        tempinst->inst->tnext = tempinst;

        /* A module can have explicit template instance and its alias
         * in module scope (e,g, `alias Base64 = Base64Impl!('+', '/');`).
         * If the first instantiation 'inst' had happened in non-root module,
         * compiler can assume that its instantiated code would be included
         * in the separately compiled obj/lib file (e.g. phobos.lib).
         *
         * However, if 'this' second instantiation happened in root module,
         * compiler might need to invoke its codegen (Bugzilla 2500 & 2644).
         * But whole import graph is not determined until all semantic pass finished,
         * so 'inst' should conservatively finish the semantic3 pass for the codegen.
         */
        if (tempinst->minst && tempinst->minst->isRoot() && !(tempinst->inst->minst && tempinst->inst->minst->isRoot()))
        {
            /* Swap the position of 'inst' and 'this' in the instantiation graph.
             * Then, the primary instance `inst` will be changed to a root instance,
             * along with all members of `inst` having their scopes updated.
             *
             * Before:
             *  non-root -> A!() -> B!()[inst] -> C!() { members[non-root] }
             *                      |
             *  root     -> D!() -> B!()[this]
             *
             * After:
             *  non-root -> A!() -> B!()[this]
             *                      |
             *  root     -> D!() -> B!()[inst] -> C!() { members[root] }
             */
            Module *mi = tempinst->minst;
            TemplateInstance *ti = tempinst->tinst;
            tempinst->minst = tempinst->inst->minst;
            tempinst->tinst = tempinst->inst->tinst;
            tempinst->inst->minst = mi;
            tempinst->inst->tinst = ti;

            /* https://issues.dlang.org/show_bug.cgi?id=21299
               `minst` has been updated on the primary instance `inst` so it is
               now coming from a root module, however all Dsymbol `inst.members`
               of the instance still have their `_scope.minst` pointing at the
               original non-root module. We must now propagate `minst` to all
               members so that forward referenced dependencies that get
               instantiated will also be appended to the root module, otherwise
               there will be undefined references at link-time.  */
            class InstMemberWalker : public Visitor
            {
            public:
                TemplateInstance *inst;

                InstMemberWalker(TemplateInstance *inst)
                    : inst(inst) { }

                void visit(Dsymbol *d)
                {
                    if (d->_scope)
                        d->_scope->minst = inst->minst;
                }

                void visit(ScopeDsymbol *sds)
                {
                    if (!sds->members)
                        return;
                    for (size_t i = 0; i < sds->members->length; i++)
                    {
                        Dsymbol *s = (*sds->members)[i];
                        s->accept(this);
                    }
                    visit((Dsymbol *)sds);
                }

                void visit(AttribDeclaration *ad)
                {
                    Dsymbols *d = ad->include(NULL);
                    if (!d)
                        return;
                    for (size_t i = 0; i < d->length; i++)
                    {
                        Dsymbol *s = (*d)[i];
                        s->accept(this);
                    }
                    visit((Dsymbol *)ad);
                }

                void visit(ConditionalDeclaration *cd)
                {
                    if (cd->condition->inc)
                        visit((AttribDeclaration *)cd);
                    else
                        visit((Dsymbol *)cd);
                }
            };
            InstMemberWalker v(tempinst->inst);
            tempinst->inst->accept(&v);

            if (tempinst->minst)  // if inst was not speculative
            {
                /* Add 'inst' once again to the root module members[], then the
                 * instance members will get codegen chances.
                 */
                tempinst->inst->appendToModuleMember();
            }
        }

        return;
    }
    unsigned errorsave = global.errors;

    tempinst->inst = tempinst;
    tempinst->parent = tempinst->enclosing ? tempinst->enclosing : tempdecl->parent;
    //printf("parent = '%s'\n", tempinst->parent->kind());

    TemplateInstance *tempdecl_instance_idx = tempdecl->addInstance(tempinst);

    //getIdent();

    // Store the place we added it to in target_symbol_list(_idx) so we can
    // remove it later if we encounter an error.
    Dsymbols *target_symbol_list = tempinst->appendToModuleMember();
    size_t target_symbol_list_idx = target_symbol_list ? target_symbol_list->length - 1 : 0;

    // Copy the syntax trees from the TemplateDeclaration
    tempinst->members = Dsymbol::arraySyntaxCopy(tempdecl->members);

    // resolve TemplateThisParameter
    for (size_t i = 0; i < tempdecl->parameters->length; i++)
    {
        if ((*tempdecl->parameters)[i]->isTemplateThisParameter() == NULL)
            continue;
        Type *t = isType((*tempinst->tiargs)[i]);
        assert(t);
        if (StorageClass stc = ModToStc(t->mod))
        {
            //printf("t = %s, stc = x%llx\n", t->toChars(), stc);
            Dsymbols *s = new Dsymbols();
            s->push(new StorageClassDeclaration(stc, tempinst->members));
            tempinst->members = s;
        }
        break;
    }

    // Create our own scope for the template parameters
    Scope *scope = tempdecl->_scope;
    if (tempdecl->semanticRun == PASSinit)
    {
        tempinst->error("template instantiation %s forward references template declaration %s", tempinst->toChars(), tempdecl->toChars());
        return;
    }

    tempinst->argsym = new ScopeDsymbol();
    tempinst->argsym->parent = scope->parent;
    scope = scope->push(tempinst->argsym);
    scope->tinst = tempinst;
    scope->minst = tempinst->minst;
    //scope->stc = 0;

    // Declare each template parameter as an alias for the argument type
    Scope *paramscope = scope->push();
    paramscope->stc = 0;
    paramscope->protection = Prot(Prot::public_);  // Bugzilla 14169: template parameters should be public
    tempinst->declareParameters(paramscope);
    paramscope->pop();

    // Add members of template instance to template instance symbol table
//    tempinst->parent = scope->scopesym;
    tempinst->symtab = new DsymbolTable();
    for (size_t i = 0; i < tempinst->members->length; i++)
    {
        Dsymbol *s = (*tempinst->members)[i];
        s->addMember(scope, tempinst);
    }

    /* See if there is only one member of template instance, and that
     * member has the same name as the template instance.
     * If so, this template instance becomes an alias for that member.
     */
    //printf("members->length = %d\n", tempinst->members->length);
    if (tempinst->members->length)
    {
        Dsymbol *s;
        if (Dsymbol::oneMembers(tempinst->members, &s, tempdecl->ident) && s)
        {
            //printf("tempdecl->ident = %s, s = '%s'\n", tempdecl->ident->toChars(), s->kind(), s->toPrettyChars());
            //printf("setting aliasdecl\n");
            tempinst->aliasdecl = s;
        }
    }

    /* If function template declaration
     */
    if (fargs && tempinst->aliasdecl)
    {
        FuncDeclaration *fd = tempinst->aliasdecl->isFuncDeclaration();
        if (fd)
        {
            /* Transmit fargs to type so that TypeFunction::semantic() can
             * resolve any "auto ref" storage classes.
             */
            TypeFunction *tf = (TypeFunction *)fd->type;
            if (tf && tf->ty == Tfunction)
                tf->fargs = fargs;
        }
    }

    // Do semantic() analysis on template instance members
    Scope *sc2;
    sc2 = scope->push(tempinst);
    //printf("enclosing = %d, sc->parent = %s\n", tempinst->enclosing, sc->parent->toChars());
    sc2->parent = tempinst;
    sc2->tinst = tempinst;
    sc2->minst = tempinst->minst;

    tempinst->tryExpandMembers(sc2);

    tempinst->semanticRun = PASSsemanticdone;

    /* ConditionalDeclaration may introduce eponymous declaration,
     * so we should find it once again after semantic.
     */
    if (tempinst->members->length)
    {
        Dsymbol *s;
        if (Dsymbol::oneMembers(tempinst->members, &s, tempdecl->ident) && s)
        {
            if (!tempinst->aliasdecl || tempinst->aliasdecl != s)
            {
                //printf("tempdecl->ident = %s, s = '%s'\n", tempdecl->ident->toChars(), s->kind(), s->toPrettyChars());
                //printf("setting aliasdecl 2\n");
                tempinst->aliasdecl = s;
            }
        }
    }

    if (global.errors != errorsave)
        goto Laftersemantic;

    /* If any of the instantiation members didn't get semantic() run
     * on them due to forward references, we cannot run semantic2()
     * or semantic3() yet.
     */
    {
    bool found_deferred_ad = false;
    for (size_t i = 0; i < Module::deferred.length; i++)
    {
        Dsymbol *sd = Module::deferred[i];
        AggregateDeclaration *ad = sd->isAggregateDeclaration();
        if (ad && ad->parent && ad->parent->isTemplateInstance())
        {
            //printf("deferred template aggregate: %s %s\n",
            //        sd->parent->toChars(), sd->toChars());
            found_deferred_ad = true;
            if (ad->parent == tempinst)
            {
                ad->deferred = tempinst;
                break;
            }
        }
    }
    if (found_deferred_ad || Module::deferred.length)
        goto Laftersemantic;
    }

    /* The problem is when to parse the initializer for a variable.
     * Perhaps VarDeclaration::semantic() should do it like it does
     * for initializers inside a function.
     */
    //if (sc->parent->isFuncDeclaration())
    {
        /* BUG 782: this has problems if the classes this depends on
         * are forward referenced. Find a way to defer semantic()
         * on this template.
         */
        semantic2(tempinst, sc2);
    }
    if (global.errors != errorsave)
        goto Laftersemantic;

    if ((sc->func || (sc->flags & SCOPEfullinst)) && !tempinst->tinst)
    {
        /* If a template is instantiated inside function, the whole instantiation
         * should be done at that position. But, immediate running semantic3 of
         * dependent templates may cause unresolved forward reference (Bugzilla 9050).
         * To avoid the issue, don't run semantic3 until semantic and semantic2 done.
         */
        TemplateInstances deferred;
        tempinst->deferred = &deferred;

        //printf("Run semantic3 on %s\n", tempinst->toChars());
        tempinst->trySemantic3(sc2);

        for (size_t i = 0; i < deferred.length; i++)
        {
            //printf("+ run deferred semantic3 on %s\n", deferred[i]->toChars());
            semantic3(deferred[i], NULL);
        }

        tempinst->deferred = NULL;
    }
    else if (tempinst->tinst)
    {
        bool doSemantic3 = false;
        if (sc->func && tempinst->aliasdecl && tempinst->aliasdecl->toAlias()->isFuncDeclaration())
        {
            /* Template function instantiation should run semantic3 immediately
             * for attribute inference.
             */
            tempinst->trySemantic3(sc2);
        }
        else if (sc->func)
        {
            /* A lambda function in template arguments might capture the
             * instantiated scope context. For the correct context inference,
             * all instantiated functions should run the semantic3 immediately.
             * See also compilable/test14973.d
             */
            for (size_t i = 0; i < tempinst->tdtypes.length; i++)
            {
                RootObject *oarg = tempinst->tdtypes[i];
                Dsymbol *s = getDsymbol(oarg);
                if (!s)
                    continue;

                if (TemplateDeclaration *td = s->isTemplateDeclaration())
                {
                    if (!td->literal)
                        continue;
                    assert(td->members && td->members->length == 1);
                    s = (*td->members)[0];
                }
                if (FuncLiteralDeclaration *fld = s->isFuncLiteralDeclaration())
                {
                    if (fld->tok == TOKreserved)
                    {
                        doSemantic3 = true;
                        break;
                    }
                }
            }
            //printf("[%s] %s doSemantic3 = %d\n", tempinst->loc.toChars(), tempinst->toChars(), doSemantic3);
        }
        if (doSemantic3)
            tempinst->trySemantic3(sc2);

        TemplateInstance *ti = tempinst->tinst;
        int nest = 0;
        while (ti && !ti->deferred && ti->tinst)
        {
            ti = ti->tinst;
            if (++nest > global.recursionLimit)
            {
                global.gag = 0;            // ensure error message gets printed
                tempinst->error("recursive expansion");
                fatal();
            }
        }
        if (ti && ti->deferred)
        {
            //printf("deferred semantic3 of %p %s, ti = %s, ti->deferred = %p\n", tempinst, tempinst->toChars(), ti->toChars());
            for (size_t i = 0; ; i++)
            {
                if (i == ti->deferred->length)
                {
                    ti->deferred->push(tempinst);
                    break;
                }
                if ((*ti->deferred)[i] == tempinst)
                    break;
            }
        }
    }

    if (tempinst->aliasdecl)
    {
        /* Bugzilla 13816: AliasDeclaration tries to resolve forward reference
         * twice (See inuse check in AliasDeclaration::toAlias()). It's
         * necessary to resolve mutual references of instantiated symbols, but
         * it will left a true recursive alias in tuple declaration - an
         * AliasDeclaration A refers TupleDeclaration B, and B contains A
         * in its elements.  To correctly make it an error, we strictly need to
         * resolve the alias of eponymous member.
         */
        tempinst->aliasdecl = tempinst->aliasdecl->toAlias2();
    }

  Laftersemantic:
    sc2->pop();

    scope->pop();

    // Give additional context info if error occurred during instantiation
    if (global.errors != errorsave)
    {
        if (!tempinst->errors)
        {
            if (!tempdecl->literal)
                tempinst->error(tempinst->loc, "error instantiating");
            if (tempinst->tinst)
                tempinst->tinst->printInstantiationTrace();
        }
        tempinst->errors = true;
        if (tempinst->gagged)
        {
            // Errors are gagged, so remove the template instance from the
            // instance/symbol lists we added it to and reset our state to
            // finish clean and so we can try to instantiate it again later
            // (see bugzilla 4302 and 6602).
            tempdecl->removeInstance(tempdecl_instance_idx);
            if (target_symbol_list)
            {
                // Because we added 'this' in the last position above, we
                // should be able to remove it without messing other indices up.
                assert((*target_symbol_list)[target_symbol_list_idx] == tempinst);
                target_symbol_list->remove(target_symbol_list_idx);
                tempinst->memberOf = NULL;                    // no longer a member
            }
            tempinst->semanticRun = PASSinit;
            tempinst->inst = NULL;
            tempinst->symtab = NULL;
        }
    }
    else if (errinst)
    {
        /* Bugzilla 14541: If the previous gagged instance had failed by
         * circular references, currrent "error reproduction instantiation"
         * might succeed, because of the difference of instantiated context.
         * On such case, the cached error instance needs to be overridden by the
         * succeeded instance.
         */
        //printf("replaceInstance()\n");
        TemplateInstances *tinstances = (TemplateInstances *)dmd_aaGetRvalue((AA *)tempdecl->instances, (void *)tempinst->hash);
        assert(tinstances);
        for (size_t i = 0; i < tinstances->length; i++)
        {
            TemplateInstance *ti = (*tinstances)[i];
            if (ti == errinst)
            {
                (*tinstances)[i] = tempinst;     // override
                break;
            }
        }
    }
}

// function used to perform semantic on AliasDeclaration
void aliasSemantic(AliasDeclaration *ds, Scope *sc)
{
    //printf("AliasDeclaration::semantic() %s\n", ds->toChars());

    // as AliasDeclaration::semantic, in case we're called first.
    // see https://issues.dlang.org/show_bug.cgi?id=21001
    ds->storage_class |= sc->stc & STCdeprecated;
    ds->protection = sc->protection;
    ds->userAttribDecl = sc->userAttribDecl;

    // TypeTraits needs to know if it's located in an AliasDeclaration
    sc->flags |= SCOPEalias;

    if (ds->aliassym)
    {
        FuncDeclaration *fd = ds->aliassym->isFuncLiteralDeclaration();
        TemplateDeclaration *td = ds->aliassym->isTemplateDeclaration();
        if (fd || (td && td->literal))
        {
            if (fd && fd->semanticRun >= PASSsemanticdone)
            {
                sc->flags &= ~SCOPEalias;
                return;
            }

            Expression *e = new FuncExp(ds->loc, ds->aliassym);
            e = expressionSemantic(e, sc);
            if (e->op == TOKfunction)
            {
                FuncExp *fe = (FuncExp *)e;
                ds->aliassym = fe->td ? (Dsymbol *)fe->td : fe->fd;
            }
            else
            {
                ds->aliassym = NULL;
                ds->type = Type::terror;
            }
            sc->flags &= ~SCOPEalias;
            return;
        }

        if (ds->aliassym->isTemplateInstance())
            dsymbolSemantic(ds->aliassym, sc);
        sc->flags &= ~SCOPEalias;
        return;
    }
    ds->inuse = 1;

    // Given:
    //  alias foo.bar.abc def;
    // it is not knowable from the syntax whether this is an alias
    // for a type or an alias for a symbol. It is up to the semantic()
    // pass to distinguish.
    // If it is a type, then type is set and getType() will return that
    // type. If it is a symbol, then aliassym is set and type is NULL -
    // toAlias() will return aliasssym.

    unsigned int errors = global.errors;
    Type *oldtype = ds->type;

    // Ungag errors when not instantiated DeclDefs scope alias
    Ungag ungag(global.gag);
    //printf("%s parent = %s, gag = %d, instantiated = %d\n", ds->toChars(), ds->parent, global.gag, ds->isInstantiated());
    if (ds->parent && global.gag && !ds->isInstantiated() && !ds->toParent2()->isFuncDeclaration())
    {
        //printf("%s type = %s\n", ds->toPrettyChars(), ds->type->toChars());
        global.gag = 0;
    }

    /* This section is needed because Type::resolve() will:
     *   const x = 3;
     *   alias y = x;
     * try to convert identifier x to 3.
     */
    Dsymbol *s = ds->type->toDsymbol(sc);
    if (errors != global.errors)
    {
        s = NULL;
        ds->type = Type::terror;
    }
    if (s && s == ds)
    {
        ds->error("cannot resolve");
        s = NULL;
        ds->type = Type::terror;
    }
    if (!s || !s->isEnumMember())
    {
        Type *t;
        Expression *e;
        Scope *sc2 = sc;
        if (ds->storage_class & (STCref | STCnothrow | STCnogc | STCpure | STCdisable))
        {
            // For 'ref' to be attached to function types, and picked
            // up by Type::resolve(), it has to go into sc.
            sc2 = sc->push();
            sc2->stc |= ds->storage_class & (STCref | STCnothrow | STCnogc | STCpure | STCshared | STCdisable);
        }
        ds->type = ds->type->addSTC(ds->storage_class);
        ds->type->resolve(ds->loc, sc2, &e, &t, &s);
        if (sc2 != sc)
            sc2->pop();

        if (e)  // Try to convert Expression to Dsymbol
        {
            s = getDsymbol(e);
            if (!s)
            {
                if (e->op != TOKerror)
                    ds->error("cannot alias an expression %s", e->toChars());
                t = Type::terror;
            }
        }
        ds->type = t;
    }
    if (s == ds)
    {
        assert(global.errors);
        ds->type = Type::terror;
        s = NULL;
    }
    if (!s) // it's a type alias
    {
        //printf("alias %s resolved to type %s\n", ds->toChars(), ds->type->toChars());
        ds->type = typeSemantic(ds->type, ds->loc, sc);
        ds->aliassym = NULL;
    }
    else    // it's a symbolic alias
    {
        //printf("alias %s resolved to %s %s\n", ds->toChars(), s->kind(), s->toChars());
        ds->type = NULL;
        ds->aliassym = s;
    }
    if (global.gag && errors != global.errors)
    {
        ds->type = oldtype;
        ds->aliassym = NULL;
    }
    ds->inuse = 0;
    ds->semanticRun = PASSsemanticdone;

    if (Dsymbol *sx = ds->overnext)
    {
        ds->overnext = NULL;

        if (!ds->overloadInsert(sx))
            ScopeDsymbol::multiplyDefined(Loc(), sx, ds);
    }
    sc->flags &= ~SCOPEalias;
}


/*************************************
 * Does semantic analysis on the public face of declarations.
 */
void dsymbolSemantic(Dsymbol *dsym, Scope *sc)
{
    DsymbolSemanticVisitor v(sc);
    dsym->accept(&v);
}