aboutsummaryrefslogtreecommitdiff
path: root/gcc/d/dmd/declaration.c
blob: d20f6636445f7a2954704941b08704d9365807ac (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

/* Compiler implementation of the D programming language
 * Copyright (C) 1999-2020 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
 * https://github.com/D-Programming-Language/dmd/blob/master/src/declaration.c
 */

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

#include "errors.h"
#include "init.h"
#include "declaration.h"
#include "attrib.h"
#include "mtype.h"
#include "template.h"
#include "scope.h"
#include "aggregate.h"
#include "module.h"
#include "import.h"
#include "id.h"
#include "expression.h"
#include "statement.h"
#include "ctfe.h"
#include "target.h"
#include "hdrgen.h"

bool checkNestedRef(Dsymbol *s, Dsymbol *p);
VarDeclaration *copyToTemp(StorageClass stc, const char *name, Expression *e);
Expression *semantic(Expression *e, Scope *sc);
Initializer *inferType(Initializer *init, Scope *sc);
Initializer *semantic(Initializer *init, Scope *sc, Type *t, NeedInterpret needInterpret);

/************************************
 * Check to see the aggregate type is nested and its context pointer is
 * accessible from the current scope.
 * Returns true if error occurs.
 */
bool checkFrameAccess(Loc loc, Scope *sc, AggregateDeclaration *ad, size_t iStart = 0)
{
    Dsymbol *sparent = ad->toParent2();
    Dsymbol *s = sc->func;
    if (ad->isNested() && s)
    {
        //printf("ad = %p %s [%s], parent:%p\n", ad, ad->toChars(), ad->loc.toChars(), ad->parent);
        //printf("sparent = %p %s [%s], parent: %s\n", sparent, sparent->toChars(), sparent->loc.toChars(), sparent->parent->toChars());
        if (checkNestedRef(s, sparent))
        {
            error(loc, "cannot access frame pointer of %s", ad->toPrettyChars());
            return true;
        }
    }

    bool result = false;
    for (size_t i = iStart; i < ad->fields.length; i++)
    {
        VarDeclaration *vd = ad->fields[i];
        Type *tb = vd->type->baseElemOf();
        if (tb->ty == Tstruct)
        {
            result |= checkFrameAccess(loc, sc, ((TypeStruct *)tb)->sym);
        }
    }
    return result;
}

/********************************* Declaration ****************************/

Declaration::Declaration(Identifier *id)
    : Dsymbol(id)
{
    type = NULL;
    originalType = NULL;
    storage_class = STCundefined;
    protection = Prot(Prot::undefined);
    linkage = LINKdefault;
    inuse = 0;
    mangleOverride = NULL;
}

void Declaration::semantic(Scope *)
{
}

const char *Declaration::kind() const
{
    return "declaration";
}

d_uns64 Declaration::size(Loc)
{
    assert(type);
    return type->size();
}

bool Declaration::isDelete()
{
    return false;
}

bool Declaration::isDataseg()
{
    return false;
}

bool Declaration::isThreadlocal()
{
    return false;
}

bool Declaration::isCodeseg() const
{
    return false;
}

Prot Declaration::prot()
{
    return protection;
}

/*************************************
 * Check to see if declaration can be modified in this context (sc).
 * Issue error if not.
 */

int Declaration::checkModify(Loc loc, Scope *sc, Type *, Expression *e1, int flag)
{
    VarDeclaration *v = isVarDeclaration();
    if (v && v->canassign)
        return 2;

    if (isParameter() || isResult())
    {
        for (Scope *scx = sc; scx; scx = scx->enclosing)
        {
            if (scx->func == parent && (scx->flags & SCOPEcontract))
            {
                const char *s = isParameter() && parent->ident != Id::ensure ? "parameter" : "result";
                if (!flag) error(loc, "cannot modify %s '%s' in contract", s, toChars());
                return 2;   // do not report type related errors
            }
        }
    }

    if (e1 && e1->op == TOKthis && isField())
    {
        VarDeclaration *vthis = e1->isThisExp()->var;
        for (Scope *scx = sc; scx; scx = scx->enclosing)
        {
            if (scx->func == vthis->parent && (scx->flags & SCOPEcontract))
            {
                if (!flag)
                    error(loc, "cannot modify parameter `this` in contract");
                return 2;   // do not report type related errors
            }
        }
    }

    if (v && (isCtorinit() || isField()))
    {
        // It's only modifiable if inside the right constructor
        if ((storage_class & (STCforeach | STCref)) == (STCforeach | STCref))
            return 2;
        return modifyFieldVar(loc, sc, v, e1) ? 2 : 1;
    }
    return 1;
}

/**
 * Issue an error if an attempt to call a disabled method is made
 *
 * If the declaration is disabled but inside a disabled function,
 * returns `true` but do not issue an error message.
 *
 * Params:
 *   loc = Location information of the call
 *   sc  = Scope in which the call occurs
 *   isAliasedDeclaration = if `true` searches overload set
 *
 * Returns:
 *   `true` if this `Declaration` is `@disable`d, `false` otherwise.
 */
bool Declaration::checkDisabled(Loc loc, Scope *sc, bool isAliasedDeclaration)
{
    if (!(storage_class & STCdisable))
        return false;

    if (sc->func && (sc->func->storage_class & STCdisable))
        return true;

    Dsymbol *p = toParent();
    if (p && isPostBlitDeclaration())
    {
        p->error(loc, "is not copyable because it is annotated with `@disable`");
        return true;
    }

    // if the function is @disabled, maybe there
    // is an overload in the overload set that isn't
    if (isAliasedDeclaration)
    {
        FuncDeclaration *fd = isFuncDeclaration();
        if (fd)
        {
            for (FuncDeclaration *ovl = fd; ovl; ovl = (FuncDeclaration *)ovl->overnext)
                if (!(ovl->storage_class & STCdisable))
                    return false;
        }
    }
    error(loc, "cannot be used because it is annotated with `@disable`");
    return true;
}

Dsymbol *Declaration::search(const Loc &loc, Identifier *ident, int flags)
{
    Dsymbol *s = Dsymbol::search(loc, ident, flags);
    if (!s && type)
    {
        s = type->toDsymbol(_scope);
        if (s)
            s = s->search(loc, ident, flags);
    }
    return s;
}


/********************************* TupleDeclaration ****************************/

TupleDeclaration::TupleDeclaration(Loc loc, Identifier *id, Objects *objects)
    : Declaration(id)
{
    this->loc = loc;
    this->type = NULL;
    this->objects = objects;
    this->isexp = false;
    this->tupletype = NULL;
}

Dsymbol *TupleDeclaration::syntaxCopy(Dsymbol *)
{
    assert(0);
    return NULL;
}

const char *TupleDeclaration::kind() const
{
    return "tuple";
}

Type *TupleDeclaration::getType()
{
    /* If this tuple represents a type, return that type
     */

    //printf("TupleDeclaration::getType() %s\n", toChars());
    if (isexp)
        return NULL;
    if (!tupletype)
    {
        /* It's only a type tuple if all the Object's are types
         */
        for (size_t i = 0; i < objects->length; i++)
        {
            RootObject *o = (*objects)[i];
            if (o->dyncast() != DYNCAST_TYPE)
            {
                //printf("\tnot[%d], %p, %d\n", i, o, o->dyncast());
                return NULL;
            }
        }

        /* We know it's a type tuple, so build the TypeTuple
         */
        Types *types = (Types *)objects;
        Parameters *args = new Parameters();
        args->setDim(objects->length);
        OutBuffer buf;
        int hasdeco = 1;
        for (size_t i = 0; i < types->length; i++)
        {
            Type *t = (*types)[i];
            //printf("type = %s\n", t->toChars());
            Parameter *arg = new Parameter(0, t, NULL, NULL, NULL);
            (*args)[i] = arg;
            if (!t->deco)
                hasdeco = 0;
        }

        tupletype = new TypeTuple(args);
        if (hasdeco)
            return tupletype->semantic(Loc(), NULL);
    }

    return tupletype;
}

Dsymbol *TupleDeclaration::toAlias2()
{
    //printf("TupleDeclaration::toAlias2() '%s' objects = %s\n", toChars(), objects->toChars());

    for (size_t i = 0; i < objects->length; i++)
    {
        RootObject *o = (*objects)[i];
        if (Dsymbol *s = isDsymbol(o))
        {
            s = s->toAlias2();
            (*objects)[i] = s;
        }
    }
    return this;
}

bool TupleDeclaration::needThis()
{
    //printf("TupleDeclaration::needThis(%s)\n", toChars());
    for (size_t i = 0; i < objects->length; i++)
    {
        RootObject *o = (*objects)[i];
        if (o->dyncast() == DYNCAST_EXPRESSION)
        {
            Expression *e = (Expression *)o;
            if (e->op == TOKdsymbol)
            {
                DsymbolExp *ve = (DsymbolExp *)e;
                Declaration *d = ve->s->isDeclaration();
                if (d && d->needThis())
                {
                    return true;
                }
            }
        }
    }
    return false;
}

/********************************* AliasDeclaration ****************************/

AliasDeclaration::AliasDeclaration(Loc loc, Identifier *id, Type *type)
    : Declaration(id)
{
    //printf("AliasDeclaration(id = '%s', type = %p)\n", id->toChars(), type);
    //printf("type = '%s'\n", type->toChars());
    this->loc = loc;
    this->type = type;
    this->aliassym = NULL;
    this->_import = NULL;
    this->overnext = NULL;
    assert(type);
}

AliasDeclaration::AliasDeclaration(Loc loc, Identifier *id, Dsymbol *s)
    : Declaration(id)
{
    //printf("AliasDeclaration(id = '%s', s = %p)\n", id->toChars(), s);
    assert(s != this);
    this->loc = loc;
    this->type = NULL;
    this->aliassym = s;
    this->_import = NULL;
    this->overnext = NULL;
    assert(s);
}

AliasDeclaration *AliasDeclaration::create(Loc loc, Identifier *id, Type *type)
{
    return new AliasDeclaration(loc, id, type);
}

Dsymbol *AliasDeclaration::syntaxCopy(Dsymbol *s)
{
    //printf("AliasDeclaration::syntaxCopy()\n");
    assert(!s);
    AliasDeclaration *sa =
        type ? new AliasDeclaration(loc, ident, type->syntaxCopy())
             : new AliasDeclaration(loc, ident, aliassym->syntaxCopy(NULL));
    sa->storage_class = storage_class;
    return sa;
}

void AliasDeclaration::semantic(Scope *sc)
{
    if (semanticRun >= PASSsemanticdone)
        return;
    assert(semanticRun <= PASSsemantic);

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

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

    aliasSemantic(sc);
}

void AliasDeclaration::aliasSemantic(Scope *sc)
{
    //printf("AliasDeclaration::semantic() %s\n", toChars());

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

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

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

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

        if (aliassym->isTemplateInstance())
            aliassym->semantic(sc);
        sc->flags &= ~SCOPEalias;
        return;
    }
    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 = type;

    // Ungag errors when not instantiated DeclDefs scope alias
    Ungag ungag(global.gag);
    //printf("%s parent = %s, gag = %d, instantiated = %d\n", toChars(), parent, global.gag, isInstantiated());
    if (parent && global.gag && !isInstantiated() && !toParent2()->isFuncDeclaration())
    {
        //printf("%s type = %s\n", toPrettyChars(), 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 = type->toDsymbol(sc);
    if (errors != global.errors)
    {
        s = NULL;
        type = Type::terror;
    }
    if (s && s == this)
    {
        error("cannot resolve");
        s = NULL;
        type = Type::terror;
    }
    if (!s || !s->isEnumMember())
    {
        Type *t;
        Expression *e;
        Scope *sc2 = sc;
        if (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 |= storage_class & (STCref | STCnothrow | STCnogc | STCpure | STCshared | STCdisable);
        }
        type = type->addSTC(storage_class);
        type->resolve(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)
                    error("cannot alias an expression %s", e->toChars());
                t = Type::terror;
            }
        }
        type = t;
    }
    if (s == this)
    {
        assert(global.errors);
        type = Type::terror;
        s = NULL;
    }
    if (!s) // it's a type alias
    {
        //printf("alias %s resolved to type %s\n", toChars(), type->toChars());
        type = type->semantic(loc, sc);
        aliassym = NULL;
    }
    else    // it's a symbolic alias
    {
        //printf("alias %s resolved to %s %s\n", toChars(), s->kind(), s->toChars());
        type = NULL;
        aliassym = s;
    }
    if (global.gag && errors != global.errors)
    {
        type = oldtype;
        aliassym = NULL;
    }
    inuse = 0;
    semanticRun = PASSsemanticdone;

    if (Dsymbol *sx = overnext)
    {
        overnext = NULL;

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

bool AliasDeclaration::overloadInsert(Dsymbol *s)
{
    //printf("[%s] AliasDeclaration::overloadInsert('%s') s = %s %s @ [%s]\n",
    //    loc.toChars(), toChars(), s->kind(), s->toChars(), s->loc.toChars());

    /** Aliases aren't overloadable themselves, but if their Aliasee is
     *  overloadable they are converted to an overloadable Alias (either
     *  FuncAliasDeclaration or OverDeclaration).
     *
     *  This is done by moving the Aliasee into such an overloadable alias
     *  which is then used to replace the existing Aliasee. The original
     *  Alias (_this_) remains a useless shell.
     *
     *  This is a horrible mess. It was probably done to avoid replacing
     *  existing AST nodes and references, but it needs a major
     *  simplification b/c it's too complex to maintain.
     *
     *  A simpler approach might be to merge any colliding symbols into a
     *  simple Overload class (an array) and then later have that resolve
     *  all collisions.
     */
    if (semanticRun >= PASSsemanticdone)
    {
        /* Semantic analysis is already finished, and the aliased entity
         * is not overloadable.
         */
        if (type)
            return false;

        /* When s is added in member scope by static if, mixin("code") or others,
         * aliassym is determined already. See the case in: test/compilable/test61.d
         */
        Dsymbol *sa = aliassym->toAlias();
        if (FuncDeclaration *fd = sa->isFuncDeclaration())
        {
            FuncAliasDeclaration *fa = new FuncAliasDeclaration(ident, fd);
            fa->protection = protection;
            fa->parent = parent;
            aliassym = fa;
            return aliassym->overloadInsert(s);
        }
        if (TemplateDeclaration *td = sa->isTemplateDeclaration())
        {
            OverDeclaration *od = new OverDeclaration(ident, td);
            od->protection = protection;
            od->parent = parent;
            aliassym = od;
            return aliassym->overloadInsert(s);
        }
        if (OverDeclaration *od = sa->isOverDeclaration())
        {
            if (sa->ident != ident || sa->parent != parent)
            {
                od = new OverDeclaration(ident, od);
                od->protection = protection;
                od->parent = parent;
                aliassym = od;
            }
            return od->overloadInsert(s);
        }
        if (OverloadSet *os = sa->isOverloadSet())
        {
            if (sa->ident != ident || sa->parent != parent)
            {
                os = new OverloadSet(ident, os);
                // TODO: protection is lost here b/c OverloadSets have no protection attribute
                // Might no be a practical issue, b/c the code below fails to resolve the overload anyhow.
                // ----
                // module os1;
                // import a, b;
                // private alias merged = foo; // private alias to overload set of a.foo and b.foo
                // ----
                // module os2;
                // import a, b;
                // public alias merged = bar; // public alias to overload set of a.bar and b.bar
                // ----
                // module bug;
                // import os1, os2;
                // void test() { merged(123); } // should only look at os2.merged
                //
                // os.protection = protection;
                os->parent = parent;
                aliassym = os;
            }
            os->push(s);
            return true;
        }
        return false;
    }

    /* Don't know yet what the aliased symbol is, so assume it can
     * be overloaded and check later for correctness.
     */
    if (overnext)
        return overnext->overloadInsert(s);
    if (s == this)
        return true;
    overnext = s;
    return true;
}

const char *AliasDeclaration::kind() const
{
    return "alias";
}

Type *AliasDeclaration::getType()
{
    if (type)
        return type;
    return toAlias()->getType();
}

Dsymbol *AliasDeclaration::toAlias()
{
    //printf("[%s] AliasDeclaration::toAlias('%s', this = %p, aliassym = %p, kind = '%s', inuse = %d)\n",
    //    loc.toChars(), toChars(), this, aliassym, aliassym ? aliassym->kind() : "", inuse);
    assert(this != aliassym);
    //static int count; if (++count == 10) *(char*)0=0;
    if (inuse == 1 && type && _scope)
    {
        inuse = 2;
        unsigned olderrors = global.errors;
        Dsymbol *s = type->toDsymbol(_scope);
        //printf("[%s] type = %s, s = %p, this = %p\n", loc.toChars(), type->toChars(), s, this);
        if (global.errors != olderrors)
            goto Lerr;
        if (s)
        {
            s = s->toAlias();
            if (global.errors != olderrors)
                goto Lerr;
            aliassym = s;
            inuse = 0;
        }
        else
        {
            Type *t = type->semantic(loc, _scope);
            if (t->ty == Terror)
                goto Lerr;
            if (global.errors != olderrors)
                goto Lerr;
            //printf("t = %s\n", t->toChars());
            inuse = 0;
        }
    }
    if (inuse)
    {
        error("recursive alias declaration");

    Lerr:
        // Avoid breaking "recursive alias" state during errors gagged
        if (global.gag)
            return this;

        aliassym = new AliasDeclaration(loc, ident, Type::terror);
        type = Type::terror;
        return aliassym;
    }

    if (semanticRun >= PASSsemanticdone)
    {
        // semantic is already done.

        // Do not see aliassym !is null, because of lambda aliases.

        // Do not see type.deco !is null, even so "alias T = const int;` needs
        // semantic analysis to take the storage class `const` as type qualifier.
    }
    else
    {
        if (_import && _import->_scope)
        {
            /* If this is an internal alias for selective/renamed import,
             * load the module first.
             */
            _import->semantic(NULL);
        }
        if (_scope)
        {
            aliasSemantic(_scope);
        }
    }

    inuse = 1;
    Dsymbol *s = aliassym ? aliassym->toAlias() : this;
    inuse = 0;
    return s;
}

Dsymbol *AliasDeclaration::toAlias2()
{
    if (inuse)
    {
        error("recursive alias declaration");
        return this;
    }
    inuse = 1;
    Dsymbol *s  = aliassym ? aliassym->toAlias2() : this;
    inuse = 0;
    return s;
}

bool AliasDeclaration::isOverloadable()
{
    // assume overloadable until alias is resolved
    return semanticRun < PASSsemanticdone ||
        (aliassym && aliassym->isOverloadable());
}

/****************************** OverDeclaration **************************/

OverDeclaration::OverDeclaration(Identifier *ident, Dsymbol *s, bool hasOverloads)
    : Declaration(ident)
{
    this->overnext = NULL;
    this->aliassym = s;

    this->hasOverloads = hasOverloads;
    if (hasOverloads)
    {
        if (OverDeclaration *od = aliassym->isOverDeclaration())
            this->hasOverloads = od->hasOverloads;
    }
    else
    {
        // for internal use
        assert(!aliassym->isOverDeclaration());
    }
}

const char *OverDeclaration::kind() const
{
    return "overload alias";    // todo
}

void OverDeclaration::semantic(Scope *)
{
}

bool OverDeclaration::equals(RootObject *o)
{
    if (this == o)
        return true;

    Dsymbol *s = isDsymbol(o);
    if (!s)
        return false;

    OverDeclaration *od1 = this;
    if (OverDeclaration *od2 = s->isOverDeclaration())
    {
        return od1->aliassym->equals(od2->aliassym) &&
               od1->hasOverloads == od2->hasOverloads;
    }
    if (aliassym == s)
    {
        if (hasOverloads)
            return true;
        if (FuncDeclaration *fd = s->isFuncDeclaration())
        {
            return fd->isUnique() != NULL;
        }
        if (TemplateDeclaration *td = s->isTemplateDeclaration())
        {
            return td->overnext == NULL;
        }
    }
    return false;
}

bool OverDeclaration::overloadInsert(Dsymbol *s)
{
    //printf("OverDeclaration::overloadInsert('%s') aliassym = %p, overnext = %p\n", s->toChars(), aliassym, overnext);
    if (overnext)
        return overnext->overloadInsert(s);
    if (s == this)
        return true;
    overnext = s;
    return true;
}

Dsymbol *OverDeclaration::toAlias()
{
    return this;
}

bool OverDeclaration::isOverloadable()
{
    return true;
}

Dsymbol *OverDeclaration::isUnique()
{
    if (!hasOverloads)
    {
        if (aliassym->isFuncDeclaration() ||
            aliassym->isTemplateDeclaration())
        {
            return aliassym;
        }
    }

  struct ParamUniqueSym
  {
    static int fp(void *param, Dsymbol *s)
    {
        Dsymbol **ps = (Dsymbol **)param;
        if (*ps)
        {
            *ps = NULL;
            return 1;   // ambiguous, done
        }
        else
        {
            *ps = s;
            return 0;
        }
    }
  };
    Dsymbol *result = NULL;
    overloadApply(aliassym, &result, &ParamUniqueSym::fp);
    return result;
}

/********************************* VarDeclaration ****************************/

VarDeclaration::VarDeclaration(Loc loc, Type *type, Identifier *id, Initializer *init)
    : Declaration(id)
{
    //printf("VarDeclaration('%s')\n", id->toChars());
    assert(id);
    assert(type || init);
    this->type = type;
    this->_init = init;
    this->loc = loc;
    offset = 0;
    isargptr = false;
    alignment = 0;
    ctorinit = 0;
    aliassym = NULL;
    onstack = false;
    mynew = false;
    canassign = 0;
    overlapped = false;
    overlapUnsafe = false;
    doNotInferScope = false;
    isdataseg = 0;
    lastVar = NULL;
    endlinnum = 0;
    ctfeAdrOnStack = -1;
    edtor = NULL;
    range = NULL;

    static unsigned nextSequenceNumber = 0;
    this->sequenceNumber = ++nextSequenceNumber;
}

VarDeclaration *VarDeclaration::create(Loc loc, Type *type, Identifier *id, Initializer *init)
{
    return new VarDeclaration(loc, type, id, init);
}

Dsymbol *VarDeclaration::syntaxCopy(Dsymbol *s)
{
    //printf("VarDeclaration::syntaxCopy(%s)\n", toChars());
    assert(!s);
    VarDeclaration *v = new VarDeclaration(loc,
            type ? type->syntaxCopy() : NULL,
            ident,
            _init ? _init->syntaxCopy() : NULL);
    v->storage_class = storage_class;
    return v;
}


void VarDeclaration::semantic(Scope *sc)
{
//    if (semanticRun > PASSinit)
//      return;
//    semanticRun = PASSsemantic;

    if (semanticRun >= PASSsemanticdone)
        return;

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

    if (!sc)
        return;

    semanticRun = PASSsemantic;

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

    userAttribDecl = sc->userAttribDecl;

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

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

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

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

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

        inuse--;
        inferred = 1;

        /* This is a kludge to support the existing syntax for RAII
         * declarations.
         */
        storage_class &= ~STCauto;
        originalType = type->syntaxCopy();
    }
    else
    {
        if (!originalType)
            originalType = 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 |= (storage_class & STC_FUNCATTR);
        inuse++;
        type = type->semantic(loc, sc2);
        inuse--;
        sc2->pop();
    }
    //printf(" semantic type = %s\n", type ? type->toChars() : "null");
    if (type->ty == Terror)
        errors = true;

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

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

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

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

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

    Dsymbol *parent = toParent();

    Type *tb = type->toBasetype();
    Type *tbn = tb->baseElemOf();
    if (tb->ty == Tvoid && !(storage_class & STClazy))
    {
        if (inferred)
        {
            error("type %s is inferred from initializer %s, and variables cannot be of type void",
                type->toChars(), _init->toChars());
        }
        else
            error("variables cannot be of type void");
        type = Type::terror;
        tb = type;
    }
    if (tb->ty == Tfunction)
    {
        error("cannot be declared to be a function");
        type = Type::terror;
        tb = type;
    }
    if (tb->ty == Tstruct)
    {
        TypeStruct *ts = (TypeStruct *)tb;
        if (!ts->sym->members)
        {
            error("no definition of struct %s", ts->toChars());
        }
    }
    if ((storage_class & STCauto) && !inferred)
        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 = (_init && !_init->isVoidInitializer()) ? initializerToExpression(_init) : NULL;
        if (ie)
            ie = ::semantic(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 = arg->type->semantic(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);
                    v->semantic(sc);
                    VarExp *ve = new VarExp(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 = arg->type->semantic(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(loc, new DeclarationExp(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(_init->loc, iexps);
        }
Lnomatch:

        if (ie && ie->op == TOKtuple)
        {
            TupleExp *te = (TupleExp *)ie;
            size_t tedim = te->exps->length;
            if (tedim != nelems)
            {
                ::error(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", 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 = _init ? _init->syntaxCopy() : NULL;

            VarDeclaration *v = new VarDeclaration(loc, arg->type, id, ti);
            v->storage_class |= STCtemp | storage_class;
            if (arg->storageClass & STCparameter)
                v->storage_class |= arg->storageClass;
            //printf("declaring field %s of type %s\n", v->toChars(), v->type->toChars());
            v->semantic(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(loc, v);
            (*exps)[i] = e;
        }
        TupleDeclaration *v2 = new TupleDeclaration(loc, ident, exps);
        v2->parent = this->parent;
        v2->isexp = true;
        aliassym = v2;
        semanticRun = PASSsemanticdone;
        return;
    }

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

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

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

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

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

        InterfaceDeclaration *id = parent->isInterfaceDeclaration();
        if (id)
        {
            error("field not allowed in interface");
        }
        else if (aad && aad->sizeok == SIZEOKdone)
        {
            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 && storage_class != STCundefined)
            {
                error("cannot use template to add field to aggregate '%s'", ad2->toChars());
            }
        }
    }

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

    if (type->hasWild())
    {
        if (storage_class & (STCstatic | STCextern | STCtls | STCgshared | STCmanifest | STCfield) ||
            isDataseg()
            )
        {
            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)
            {
                error("inout variables can only be declared inside inout functions");
            }
        }
    }

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

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

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

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

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

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

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

        Type *tv = 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(loc, sc, ((TypeStruct *)tbn)->sym);

            Expression *e = tv->defaultInitLiteral(loc);
            e = new BlitExp(loc, new VarExp(loc, this), e);
            e = ::semantic(e, sc);
            _init = new ExpInitializer(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(loc, 0, Type::tint32);
            e = new BlitExp(loc, new VarExp(loc, this), e);
            e->type = type;         // don't type check this, it would fail
            _init = new ExpInitializer(loc, e);
            goto Ldtor;
        }
        if (type->baseElemOf()->ty == Tvoid)
        {
            error("%s does not have a default initializer", type->toChars());
        }
        else if (Expression *e = type->defaultInit(loc))
        {
            _init = new ExpInitializer(loc, e);
        }
        // Default initializer is always a blit
        isBlit = true;
    }

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

        ExpInitializer *ei = _init->isExpInitializer();
        if (ei)     // Bugzilla 13424: Preset the required type to fail in FuncLiteralDeclaration::semantic3
            ei->exp = inferType(ei->exp, 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 &&
                !(storage_class & (STCmanifest | STCstatic | STCtls | STCgshared | STCextern)) &&
                !_init->isVoidInitializer())
            {
                //printf("fd = '%s', var = '%s'\n", fd->toChars(), toChars());
                if (!ei)
                {
                    ArrayInitializer *ai = _init->isArrayInitializer();
                    Expression *e;
                    if (ai && tb->ty == Taarray)
                        e = ai->toAssocArrayLiteral();
                    else
                        e = initializerToExpression(_init);
                    if (!e)
                    {
                        // Run semantic, but don't need to interpret
                        _init = ::semantic(_init, sc, type, INITnointerpret);
                        e = initializerToExpression(_init);
                        if (!e)
                        {
                            error("is not a static and cannot have static initializer");
                            e = new ErrorExp();
                        }
                    }
                    ei = new ExpInitializer(_init->loc, e);
                    _init = ei;
                }

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

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

                if (ei && 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 (type->toBasetype()->ty == Tclass)
                        {
                            if (ne->newargs && ne->newargs->length > 1)
                            {
                                mynew = true;
                            }
                            else
                            {
                                ne->onstack = true;
                                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
                _init = ::semantic(_init, sc, type, sc->intypeof == 1 ? INITnointerpret : INITinterpret);
            }
        }
        else if (parent->isAggregateDeclaration())
        {
            _scope = scx ? scx : sc->copy();
            _scope->setNoFree();
        }
        else if (storage_class & (STCconst | STCimmutable | STCmanifest) ||
                 type->isConst() || 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;
                inuse++;
                if (ei)
                {
                    Expression *exp = ei->exp->syntaxCopy();

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

                    Type *tb2 = 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())
                                error("of type struct %s uses this(this), which is not allowed in static initialization", tb2->toChars());
                        }
                    }
                    ei->exp = exp;
                }
                _init = ::semantic(_init, sc, type, INITinterpret);
                inuse--;
                if (global.errors > errors)
                {
                    _init = new ErrorInitializer();
                    type = Type::terror;
                }
            }
            else
            {
                _scope = scx ? scx : sc->copy();
                _scope->setNoFree();
            }
        }
        sc = sc->pop();
    }

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

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

    semanticRun = PASSsemanticdone;

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

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

void VarDeclaration::semantic2(Scope *sc)
{
    if (semanticRun < PASSsemanticdone && inuse)
        return;

    //printf("VarDeclaration::semantic2('%s')\n", toChars());

    if (_init && !toParent()->isFuncDeclaration())
    {
        inuse++;
        // Bugzilla 14166: Don't run CTFE for the temporary variables inside typeof
        _init = ::semantic(_init, sc, type, sc->intypeof == 1 ? INITnointerpret : INITinterpret);
        inuse--;
    }
    if (_init && storage_class & STCmanifest)
    {
        /* Cannot initializer enums with CTFE classreferences and addresses of struct literals.
         * Scan initializer looking for them. Issue error if found.
         */
        if (ExpInitializer *ei = _init->isExpInitializer())
        {
            struct EnumInitializer
            {
                static bool arrayHasInvalidEnumInitializer(Expressions *elems)
                {
                    for (size_t i = 0; i < elems->length; i++)
                    {
                        Expression *e = (*elems)[i];
                        if (e && hasInvalidEnumInitializer(e))
                            return true;
                    }
                    return false;
                }

                static bool hasInvalidEnumInitializer(Expression *e)
                {
                    if (e->op == TOKclassreference)
                        return true;
                    if (e->op == TOKaddress && ((AddrExp *)e)->e1->op == TOKstructliteral)
                        return true;
                    if (e->op == TOKarrayliteral)
                        return arrayHasInvalidEnumInitializer(((ArrayLiteralExp *)e)->elements);
                    if (e->op == TOKstructliteral)
                        return arrayHasInvalidEnumInitializer(((StructLiteralExp *)e)->elements);
                    if (e->op == TOKassocarrayliteral)
                    {
                        AssocArrayLiteralExp *ae = (AssocArrayLiteralExp *)e;
                        return arrayHasInvalidEnumInitializer(ae->values) ||
                            arrayHasInvalidEnumInitializer(ae->keys);
                    }
                    return false;
                }
            };
            if (EnumInitializer::hasInvalidEnumInitializer(ei->exp))
                error(": Unable to initialize enum with class or pointer to struct. Use static const variable instead.");
        }
    }
    else if (_init && isThreadlocal())
    {
        if ((type->ty == Tclass) && type->isMutable() && !type->isShared())
        {
            ExpInitializer *ei = _init->isExpInitializer();
            if (ei && ei->exp->op == TOKclassreference)
                error("is mutable. Only const or immutable class thread local variable are allowed, not %s", type->toChars());
        }
        else if (type->ty == Tpointer && type->nextOf()->ty == Tstruct && type->nextOf()->isMutable() &&!type->nextOf()->isShared())
        {
            ExpInitializer *ei = _init->isExpInitializer();
            if (ei && ei->exp->op == TOKaddress && ((AddrExp *)ei->exp)->e1->op == TOKstructliteral)
            {
                error("is a pointer to mutable struct. Only pointers to const, immutable or shared struct thread local variable are allowed, not %s", type->toChars());
            }
        }
    }
    semanticRun = PASSsemantic2done;
}

void VarDeclaration::setFieldOffset(AggregateDeclaration *ad, unsigned *poffset, bool isunion)
{
    //printf("VarDeclaration::setFieldOffset(ad = %s) %s\n", ad->toChars(), toChars());

    if (aliassym)
    {
        // If this variable was really a tuple, set the offsets for the tuple fields
        TupleDeclaration *v2 = aliassym->isTupleDeclaration();
        assert(v2);
        for (size_t i = 0; i < v2->objects->length; i++)
        {
            RootObject *o = (*v2->objects)[i];
            assert(o->dyncast() == DYNCAST_EXPRESSION);
            Expression *e = (Expression *)o;
            assert(e->op == TOKdsymbol);
            DsymbolExp *se = (DsymbolExp *)e;
            se->s->setFieldOffset(ad, poffset, isunion);
        }
        return;
    }

    if (!isField())
        return;
    assert(!(storage_class & (STCstatic | STCextern | STCparameter | STCtls)));

    //printf("+VarDeclaration::setFieldOffset(ad = %s) %s\n", ad->toChars(), toChars());

    /* Fields that are tuples appear both as part of TupleDeclarations and
     * as members. That means ignore them if they are already a field.
     */
    if (offset)
    {
        // already a field
        *poffset = ad->structsize;  // Bugzilla 13613
        return;
    }
    for (size_t i = 0; i < ad->fields.length; i++)
    {
        if (ad->fields[i] == this)
        {
            // already a field
            *poffset = ad->structsize;  // Bugzilla 13613
            return;
        }
    }

    // Check for forward referenced types which will fail the size() call
    Type *t = type->toBasetype();
    if (storage_class & STCref)
    {
        // References are the size of a pointer
        t = Type::tvoidptr;
    }
    Type *tv = t->baseElemOf();
    if (tv->ty == Tstruct)
    {
        TypeStruct *ts = (TypeStruct *)tv;
        assert(ts->sym != ad);   // already checked in ad->determineFields()
        if (!ts->sym->determineSize(loc))
        {
            type = Type::terror;
            errors = true;
            return;
        }
    }

    // List in ad->fields. Even if the type is error, it's necessary to avoid
    // pointless error diagnostic "more initializers than fields" on struct literal.
    ad->fields.push(this);

    if (t->ty == Terror)
        return;

    const d_uns64 sz = t->size(loc);
    assert(sz != SIZE_INVALID && sz < UINT32_MAX);
    unsigned memsize = (unsigned)sz;                 // size of member
    unsigned memalignsize = target.fieldalign(t);   // size of member for alignment purposes

    offset = AggregateDeclaration::placeField(poffset, memsize, memalignsize, alignment,
                &ad->structsize, &ad->alignsize, isunion);

    //printf("\t%s: memalignsize = %d\n", toChars(), memalignsize);

    //printf(" addField '%s' to '%s' at offset %d, size = %d\n", toChars(), ad->toChars(), offset, memsize);
}

const char *VarDeclaration::kind() const
{
    return "variable";
}

Dsymbol *VarDeclaration::toAlias()
{
    //printf("VarDeclaration::toAlias('%s', this = %p, aliassym = %p)\n", toChars(), this, aliassym);
    if ((!type || !type->deco) && _scope)
        semantic(_scope);

    assert(this != aliassym);
    Dsymbol *s = aliassym ? aliassym->toAlias() : this;
    return s;
}

AggregateDeclaration *VarDeclaration::isThis()
{
    AggregateDeclaration *ad = NULL;

    if (!(storage_class & (STCstatic | STCextern | STCmanifest | STCtemplateparameter |
                           STCtls | STCgshared | STCctfe)))
    {
        for (Dsymbol *s = this; s; s = s->parent)
        {
            ad = s->isMember();
            if (ad)
                break;
            if (!s->parent || !s->parent->isTemplateMixin()) break;
        }
    }
    return ad;
}

bool VarDeclaration::needThis()
{
    //printf("VarDeclaration::needThis(%s, x%x)\n", toChars(), storage_class);
    return isField();
}

bool VarDeclaration::isExport() const
{
    return protection.kind == Prot::export_;
}

bool VarDeclaration::isImportedSymbol() const
{
    if (protection.kind == Prot::export_ && !_init &&
        (storage_class & STCstatic || parent->isModule()))
        return true;
    return false;
}

/*******************************************
 * Helper function for the expansion of manifest constant.
 */
Expression *VarDeclaration::expandInitializer(Loc loc)
{
    assert((storage_class & STCmanifest) && _init);

    Expression *e = getConstInitializer();
    if (!e)
    {
        ::error(loc, "cannot make expression out of initializer for %s", toChars());
        return new ErrorExp();
    }

    e = e->copy();
    e->loc = loc;    // for better error message
    return e;
}

void VarDeclaration::checkCtorConstInit()
{
#if 0 /* doesn't work if more than one static ctor */
    if (ctorinit == 0 && isCtorinit() && !isField())
        error("missing initializer in static constructor for const variable");
#endif
}

bool lambdaCheckForNestedRef(Expression *e, Scope *sc);

/************************************
 * Check to see if this variable is actually in an enclosing function
 * rather than the current one.
 * Returns true if error occurs.
 */
bool VarDeclaration::checkNestedReference(Scope *sc, Loc loc)
{
    //printf("VarDeclaration::checkNestedReference() %s\n", toChars());
    if (sc->intypeof == 1 || (sc->flags & SCOPEctfe))
        return false;
    if (!parent || parent == sc->parent)
        return false;
    if (isDataseg() || (storage_class & STCmanifest))
        return false;

    // The current function
    FuncDeclaration *fdthis = sc->parent->isFuncDeclaration();
    if (!fdthis)
        return false;   // out of function scope

    Dsymbol *p = toParent2();

    // Function literals from fdthis to p must be delegates
    checkNestedRef(fdthis, p);

    // The function that this variable is in
    FuncDeclaration *fdv = p->isFuncDeclaration();
    if (!fdv || fdv == fdthis)
        return false;

    // Add fdthis to nestedrefs[] if not already there
    if (!nestedrefs.contains(fdthis))
        nestedrefs.push(fdthis);

    /* __require and __ensure will always get called directly,
     * so they never make outer functions closure.
     */
    if (fdthis->ident == Id::require || fdthis->ident == Id::ensure)
        return false;

    //printf("\tfdv = %s\n", fdv->toChars());
    //printf("\tfdthis = %s\n", fdthis->toChars());
    if (loc.filename)
    {
        int lv = fdthis->getLevel(loc, sc, fdv);
        if (lv == -2)   // error
            return true;
    }

    // Add this to fdv->closureVars[] if not already there
    if (!sc->intypeof && !(sc->flags & SCOPEcompile))
    {
        if (!fdv->closureVars.contains(this))
            fdv->closureVars.push(this);
    }

    //printf("fdthis is %s\n", fdthis->toChars());
    //printf("var %s in function %s is nested ref\n", toChars(), fdv->toChars());
    // __dollar creates problems because it isn't a real variable Bugzilla 3326
    if (ident == Id::dollar)
    {
        ::error(loc, "cannnot use $ inside a function literal");
        return true;
    }

    if (ident == Id::withSym)       // Bugzilla 1759
    {
        ExpInitializer *ez = _init->isExpInitializer();
        assert(ez);
        Expression *e = ez->exp;
        if (e->op == TOKconstruct || e->op == TOKblit)
            e = ((AssignExp *)e)->e2;
        return lambdaCheckForNestedRef(e, sc);
    }

    return false;
}

/*******************************************
 * If variable has a constant expression initializer, get it.
 * Otherwise, return NULL.
 */

Expression *VarDeclaration::getConstInitializer(bool needFullType)
{
    assert(type && _init);

    // Ungag errors when not speculative
    unsigned oldgag = global.gag;
    if (global.gag)
    {
        Dsymbol *sym = toParent()->isAggregateDeclaration();
        if (sym && !sym->isSpeculative())
            global.gag = 0;
    }

    if (_scope)
    {
        inuse++;
        _init = ::semantic(_init, _scope, type, INITinterpret);
        _scope = NULL;
        inuse--;
    }
    Expression *e = initializerToExpression(_init, needFullType ? type : NULL);

    global.gag = oldgag;
    return e;
}

/*************************************
 * Return true if we can take the address of this variable.
 */

bool VarDeclaration::canTakeAddressOf()
{
    return !(storage_class & STCmanifest);
}


/*******************************
 * Does symbol go into data segment?
 * Includes extern variables.
 */

bool VarDeclaration::isDataseg()
{
    if (isdataseg == 0) // the value is not cached
    {
        isdataseg = 2; // The Variables does not go into the datasegment

        if (!canTakeAddressOf())
        {
            return false;
        }

        Dsymbol *parent = toParent();
        if (!parent && !(storage_class & STCstatic))
        {
            error("forward referenced");
            type = Type::terror;
        }
        else if (storage_class & (STCstatic | STCextern | STCtls | STCgshared) ||
                 parent->isModule() || parent->isTemplateInstance() || parent->isNspace())
        {
            assert(!isParameter() && !isResult());
            isdataseg = 1; // It is in the DataSegment
        }
    }

    return (isdataseg == 1);
}

/************************************
 * Does symbol go into thread local storage?
 */

bool VarDeclaration::isThreadlocal()
{
    //printf("VarDeclaration::isThreadlocal(%p, '%s')\n", this, toChars());
    /* Data defaults to being thread-local. It is not thread-local
     * if it is immutable, const or shared.
     */
    bool i = isDataseg() &&
        !(storage_class & (STCimmutable | STCconst | STCshared | STCgshared));
    //printf("\treturn %d\n", i);
    return i;
}

/********************************************
 * Can variable be read and written by CTFE?
 */

bool VarDeclaration::isCTFE()
{
    return (storage_class & STCctfe) != 0; // || !isDataseg();
}

bool VarDeclaration::isOverlappedWith(VarDeclaration *v)
{
    const d_uns64 vsz = v->type->size();
    const d_uns64 tsz = type->size();
    assert(vsz != SIZE_INVALID && tsz != SIZE_INVALID);
    return offset < v->offset + vsz &&
        v->offset < offset + tsz;
}

bool VarDeclaration::hasPointers()
{
    //printf("VarDeclaration::hasPointers() %s, ty = %d\n", toChars(), type->ty);
    return (!isDataseg() && type->hasPointers());
}

/******************************************
 * Return true if variable needs to call the destructor.
 */

bool VarDeclaration::needsScopeDtor()
{
    //printf("VarDeclaration::needsScopeDtor() %s\n", toChars());
    return edtor && !(storage_class & STCnodtor);
}


/******************************************
 * If a variable has a scope destructor call, return call for it.
 * Otherwise, return NULL.
 */

Expression *VarDeclaration::callScopeDtor(Scope *)
{
    //printf("VarDeclaration::callScopeDtor() %s\n", toChars());

    // Destruction of STCfield's is handled by buildDtor()
    if (storage_class & (STCnodtor | STCref | STCout | STCfield))
    {
        return NULL;
    }

    Expression *e = NULL;

    // Destructors for structs and arrays of structs
    Type *tv = type->baseElemOf();
    if (tv->ty == Tstruct)
    {
        StructDeclaration *sd = ((TypeStruct *)tv)->sym;
        if (!sd->dtor || sd->errors)
           return NULL;

        const d_uns64 sz = type->size();
        assert(sz != SIZE_INVALID);
        if (!sz)
            return NULL;

        if (type->toBasetype()->ty == Tstruct)
        {
            // v.__xdtor()
            e = new VarExp(loc, this);

            /* This is a hack so we can call destructors on const/immutable objects.
             * Need to add things like "const ~this()" and "immutable ~this()" to
             * fix properly.
             */
            e->type = e->type->mutableOf();

            // Enable calling destructors on shared objects.
            // The destructor is always a single, non-overloaded function,
            // and must serve both shared and non-shared objects.
            e->type = e->type->unSharedOf();

            e = new DotVarExp(loc, e, sd->dtor, false);
            e = new CallExp(loc, e);
        }
        else
        {
            // __ArrayDtor(v[0 .. n])
            e = new VarExp(loc, this);

            const d_uns64 sdsz = sd->type->size();
            assert(sdsz != SIZE_INVALID && sdsz != 0);
            const d_uns64 n = sz / sdsz;
            e = new SliceExp(loc, e, new IntegerExp(loc, 0, Type::tsize_t),
                                     new IntegerExp(loc, n, Type::tsize_t));
            // Prevent redundant bounds check
            ((SliceExp *)e)->upperIsInBounds = true;
            ((SliceExp *)e)->lowerIsLessThanUpper = true;

            // This is a hack so we can call destructors on const/immutable objects.
            e->type = sd->type->arrayOf();

            e = new CallExp(loc, new IdentifierExp(loc, Id::__ArrayDtor), e);
        }
        return e;
    }

    // Destructors for classes
    if (storage_class & (STCauto | STCscope) && !(storage_class & STCparameter))
    {
        for (ClassDeclaration *cd = type->isClassHandle();
             cd;
             cd = cd->baseClass)
        {
            /* We can do better if there's a way with onstack
             * classes to determine if there's no way the monitor
             * could be set.
             */
            //if (cd->isInterfaceDeclaration())
                //error("interface %s cannot be scope", cd->toChars());

            // Destroying C++ scope classes crashes currently. Since C++ class dtors are not currently supported, simply do not run dtors for them.
            // See https://issues.dlang.org/show_bug.cgi?id=13182
            if (cd->isCPPclass())
            {
                break;
            }
            if (mynew || onstack) // if any destructors
            {
                // delete this;
                Expression *ec;

                ec = new VarExp(loc, this);
                e = new DeleteExp(loc, ec, true);
                e->type = Type::tvoid;
                break;
            }
        }
    }
    return e;
}

/**********************************
 * Determine if `this` has a lifetime that lasts past
 * the destruction of `v`
 * Params:
 *  v = variable to test against
 * Returns:
 *  true if it does
 */
bool VarDeclaration::enclosesLifetimeOf(VarDeclaration *v) const
{
    return sequenceNumber < v->sequenceNumber;
}

/******************************************
 */

void ObjectNotFound(Identifier *id)
{
    Type::error(Loc(), "%s not found. object.d may be incorrectly installed or corrupt.", id->toChars());
    fatal();
}

/******************************** SymbolDeclaration ********************************/

SymbolDeclaration::SymbolDeclaration(Loc loc, StructDeclaration *dsym)
        : Declaration(dsym->ident)
{
    this->loc = loc;
    this->dsym = dsym;
    storage_class |= STCconst;
}

/********************************* TypeInfoDeclaration ****************************/

TypeInfoDeclaration::TypeInfoDeclaration(Type *tinfo)
    : VarDeclaration(Loc(), Type::dtypeinfo->type, tinfo->getTypeInfoIdent(), NULL)
{
    this->tinfo = tinfo;
    storage_class = STCstatic | STCgshared;
    protection = Prot(Prot::public_);
    linkage = LINKc;
    alignment = target.ptrsize;
}

TypeInfoDeclaration *TypeInfoDeclaration::create(Type *tinfo)
{
    return new TypeInfoDeclaration(tinfo);
}

Dsymbol *TypeInfoDeclaration::syntaxCopy(Dsymbol *)
{
    assert(0);          // should never be produced by syntax
    return NULL;
}

void TypeInfoDeclaration::semantic(Scope *)
{
    assert(linkage == LINKc);
}

const char *TypeInfoDeclaration::toChars()
{
    //printf("TypeInfoDeclaration::toChars() tinfo = %s\n", tinfo->toChars());
    OutBuffer buf;
    buf.writestring("typeid(");
    buf.writestring(tinfo->toChars());
    buf.writeByte(')');
    return buf.extractChars();
}

/***************************** TypeInfoConstDeclaration **********************/

TypeInfoConstDeclaration::TypeInfoConstDeclaration(Type *tinfo)
    : TypeInfoDeclaration(tinfo)
{
    if (!Type::typeinfoconst)
    {
        ObjectNotFound(Id::TypeInfo_Const);
    }
    type = Type::typeinfoconst->type;
}

TypeInfoConstDeclaration *TypeInfoConstDeclaration::create(Type *tinfo)
{
    return new TypeInfoConstDeclaration(tinfo);
}

/***************************** TypeInfoInvariantDeclaration **********************/

TypeInfoInvariantDeclaration::TypeInfoInvariantDeclaration(Type *tinfo)
    : TypeInfoDeclaration(tinfo)
{
    if (!Type::typeinfoinvariant)
    {
        ObjectNotFound(Id::TypeInfo_Invariant);
    }
    type = Type::typeinfoinvariant->type;
}

TypeInfoInvariantDeclaration *TypeInfoInvariantDeclaration::create(Type *tinfo)
{
    return new TypeInfoInvariantDeclaration(tinfo);
}

/***************************** TypeInfoSharedDeclaration **********************/

TypeInfoSharedDeclaration::TypeInfoSharedDeclaration(Type *tinfo)
    : TypeInfoDeclaration(tinfo)
{
    if (!Type::typeinfoshared)
    {
        ObjectNotFound(Id::TypeInfo_Shared);
    }
    type = Type::typeinfoshared->type;
}

TypeInfoSharedDeclaration *TypeInfoSharedDeclaration::create(Type *tinfo)
{
    return new TypeInfoSharedDeclaration(tinfo);
}

/***************************** TypeInfoWildDeclaration **********************/

TypeInfoWildDeclaration::TypeInfoWildDeclaration(Type *tinfo)
    : TypeInfoDeclaration(tinfo)
{
    if (!Type::typeinfowild)
    {
        ObjectNotFound(Id::TypeInfo_Wild);
    }
    type = Type::typeinfowild->type;
}

TypeInfoWildDeclaration *TypeInfoWildDeclaration::create(Type *tinfo)
{
    return new TypeInfoWildDeclaration(tinfo);
}

/***************************** TypeInfoStructDeclaration **********************/

TypeInfoStructDeclaration::TypeInfoStructDeclaration(Type *tinfo)
    : TypeInfoDeclaration(tinfo)
{
    if (!Type::typeinfostruct)
    {
        ObjectNotFound(Id::TypeInfo_Struct);
    }
    type = Type::typeinfostruct->type;
}

TypeInfoStructDeclaration *TypeInfoStructDeclaration::create(Type *tinfo)
{
    return new TypeInfoStructDeclaration(tinfo);
}

/***************************** TypeInfoClassDeclaration ***********************/

TypeInfoClassDeclaration::TypeInfoClassDeclaration(Type *tinfo)
    : TypeInfoDeclaration(tinfo)
{
    if (!Type::typeinfoclass)
    {
        ObjectNotFound(Id::TypeInfo_Class);
    }
    type = Type::typeinfoclass->type;
}

TypeInfoClassDeclaration *TypeInfoClassDeclaration::create(Type *tinfo)
{
    return new TypeInfoClassDeclaration(tinfo);
}

/***************************** TypeInfoInterfaceDeclaration *******************/

TypeInfoInterfaceDeclaration::TypeInfoInterfaceDeclaration(Type *tinfo)
    : TypeInfoDeclaration(tinfo)
{
    if (!Type::typeinfointerface)
    {
        ObjectNotFound(Id::TypeInfo_Interface);
    }
    type = Type::typeinfointerface->type;
}

TypeInfoInterfaceDeclaration *TypeInfoInterfaceDeclaration::create(Type *tinfo)
{
    return new TypeInfoInterfaceDeclaration(tinfo);
}

/***************************** TypeInfoPointerDeclaration *********************/

TypeInfoPointerDeclaration::TypeInfoPointerDeclaration(Type *tinfo)
    : TypeInfoDeclaration(tinfo)
{
    if (!Type::typeinfopointer)
    {
        ObjectNotFound(Id::TypeInfo_Pointer);
    }
    type = Type::typeinfopointer->type;
}

TypeInfoPointerDeclaration *TypeInfoPointerDeclaration::create(Type *tinfo)
{
    return new TypeInfoPointerDeclaration(tinfo);
}

/***************************** TypeInfoArrayDeclaration ***********************/

TypeInfoArrayDeclaration::TypeInfoArrayDeclaration(Type *tinfo)
    : TypeInfoDeclaration(tinfo)
{
    if (!Type::typeinfoarray)
    {
        ObjectNotFound(Id::TypeInfo_Array);
    }
    type = Type::typeinfoarray->type;
}

TypeInfoArrayDeclaration *TypeInfoArrayDeclaration::create(Type *tinfo)
{
    return new TypeInfoArrayDeclaration(tinfo);
}

/***************************** TypeInfoStaticArrayDeclaration *****************/

TypeInfoStaticArrayDeclaration::TypeInfoStaticArrayDeclaration(Type *tinfo)
    : TypeInfoDeclaration(tinfo)
{
    if (!Type::typeinfostaticarray)
    {
        ObjectNotFound(Id::TypeInfo_StaticArray);
    }
    type = Type::typeinfostaticarray->type;
}

TypeInfoStaticArrayDeclaration *TypeInfoStaticArrayDeclaration::create(Type *tinfo)
{
    return new TypeInfoStaticArrayDeclaration(tinfo);
}

/***************************** TypeInfoAssociativeArrayDeclaration ************/

TypeInfoAssociativeArrayDeclaration::TypeInfoAssociativeArrayDeclaration(Type *tinfo)
    : TypeInfoDeclaration(tinfo)
{
    if (!Type::typeinfoassociativearray)
    {
        ObjectNotFound(Id::TypeInfo_AssociativeArray);
    }
    type = Type::typeinfoassociativearray->type;
}

TypeInfoAssociativeArrayDeclaration *TypeInfoAssociativeArrayDeclaration::create(Type *tinfo)
{
    return new TypeInfoAssociativeArrayDeclaration(tinfo);
}

/***************************** TypeInfoVectorDeclaration ***********************/

TypeInfoVectorDeclaration::TypeInfoVectorDeclaration(Type *tinfo)
    : TypeInfoDeclaration(tinfo)
{
    if (!Type::typeinfovector)
    {
        ObjectNotFound(Id::TypeInfo_Vector);
    }
    type = Type::typeinfovector->type;
}

TypeInfoVectorDeclaration *TypeInfoVectorDeclaration::create(Type *tinfo)
{
    return new TypeInfoVectorDeclaration(tinfo);
}

/***************************** TypeInfoEnumDeclaration ***********************/

TypeInfoEnumDeclaration::TypeInfoEnumDeclaration(Type *tinfo)
    : TypeInfoDeclaration(tinfo)
{
    if (!Type::typeinfoenum)
    {
        ObjectNotFound(Id::TypeInfo_Enum);
    }
    type = Type::typeinfoenum->type;
}

TypeInfoEnumDeclaration *TypeInfoEnumDeclaration::create(Type *tinfo)
{
    return new TypeInfoEnumDeclaration(tinfo);
}

/***************************** TypeInfoFunctionDeclaration ********************/

TypeInfoFunctionDeclaration::TypeInfoFunctionDeclaration(Type *tinfo)
    : TypeInfoDeclaration(tinfo)
{
    if (!Type::typeinfofunction)
    {
        ObjectNotFound(Id::TypeInfo_Function);
    }
    type = Type::typeinfofunction->type;
}

TypeInfoFunctionDeclaration *TypeInfoFunctionDeclaration::create(Type *tinfo)
{
    return new TypeInfoFunctionDeclaration(tinfo);
}

/***************************** TypeInfoDelegateDeclaration ********************/

TypeInfoDelegateDeclaration::TypeInfoDelegateDeclaration(Type *tinfo)
    : TypeInfoDeclaration(tinfo)
{
    if (!Type::typeinfodelegate)
    {
        ObjectNotFound(Id::TypeInfo_Delegate);
    }
    type = Type::typeinfodelegate->type;
}

TypeInfoDelegateDeclaration *TypeInfoDelegateDeclaration::create(Type *tinfo)
{
    return new TypeInfoDelegateDeclaration(tinfo);
}

/***************************** TypeInfoTupleDeclaration **********************/

TypeInfoTupleDeclaration::TypeInfoTupleDeclaration(Type *tinfo)
    : TypeInfoDeclaration(tinfo)
{
    if (!Type::typeinfotypelist)
    {
        ObjectNotFound(Id::TypeInfo_Tuple);
    }
    type = Type::typeinfotypelist->type;
}

TypeInfoTupleDeclaration *TypeInfoTupleDeclaration::create(Type *tinfo)
{
    return new TypeInfoTupleDeclaration(tinfo);
}

/********************************* ThisDeclaration ****************************/

// For the "this" parameter to member functions

ThisDeclaration::ThisDeclaration(Loc loc, Type *t)
   : VarDeclaration(loc, t, Id::This, NULL)
{
    storage_class |= STCnodtor;
}

Dsymbol *ThisDeclaration::syntaxCopy(Dsymbol *)
{
    assert(0);          // should never be produced by syntax
    return NULL;
}