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
|
/* d-codegen.cc -- Code generation and routines for manipulation of GCC trees.
Copyright (C) 2006-2019 Free Software Foundation, Inc.
GCC is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
GCC is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING3. If not see
<http://www.gnu.org/licenses/>. */
#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "dmd/aggregate.h"
#include "dmd/ctfe.h"
#include "dmd/declaration.h"
#include "dmd/identifier.h"
#include "dmd/target.h"
#include "dmd/template.h"
#include "tree.h"
#include "tree-iterator.h"
#include "fold-const.h"
#include "diagnostic.h"
#include "langhooks.h"
#include "target.h"
#include "stringpool.h"
#include "varasm.h"
#include "stor-layout.h"
#include "attribs.h"
#include "function.h"
#include "d-tree.h"
/* Return the GCC location for the D frontend location LOC. */
location_t
make_location_t (const Loc& loc)
{
location_t gcc_location = input_location;
if (loc.filename)
{
linemap_add (line_table, LC_ENTER, 0, loc.filename, loc.linnum);
linemap_line_start (line_table, loc.linnum, 0);
gcc_location = linemap_position_for_column (line_table, loc.charnum);
linemap_add (line_table, LC_LEAVE, 0, NULL, 0);
}
return gcc_location;
}
/* Return the DECL_CONTEXT for symbol DSYM. */
tree
d_decl_context (Dsymbol *dsym)
{
Dsymbol *parent = dsym;
Declaration *decl = dsym->isDeclaration ();
while ((parent = parent->toParent ()))
{
/* We've reached the top-level module namespace.
Set DECL_CONTEXT as the NAMESPACE_DECL of the enclosing module,
but only for extern(D) symbols. */
if (parent->isModule ())
{
if (decl != NULL && decl->linkage != LINKd)
return NULL_TREE;
return build_import_decl (parent);
}
/* Declarations marked as 'static' or '__gshared' are never
part of any context except at module level. */
if (decl != NULL && decl->isDataseg ())
continue;
/* Nested functions. */
FuncDeclaration *fd = parent->isFuncDeclaration ();
if (fd != NULL)
return get_symbol_decl (fd);
/* Methods of classes or structs. */
AggregateDeclaration *ad = parent->isAggregateDeclaration ();
if (ad != NULL)
{
tree context = build_ctype (ad->type);
/* Want the underlying RECORD_TYPE. */
if (ad->isClassDeclaration ())
context = TREE_TYPE (context);
return context;
}
/* Instantiated types are given the context of their template. */
TemplateInstance *ti = parent->isTemplateInstance ();
if (ti != NULL && decl == NULL)
parent = ti->tempdecl;
}
return NULL_TREE;
}
/* Return a copy of record TYPE but safe to modify in any way. */
tree
copy_aggregate_type (tree type)
{
tree newtype = build_distinct_type_copy (type);
TYPE_FIELDS (newtype) = copy_list (TYPE_FIELDS (type));
for (tree f = TYPE_FIELDS (newtype); f; f = DECL_CHAIN (f))
DECL_FIELD_CONTEXT (f) = newtype;
return newtype;
}
/* Return TRUE if declaration DECL is a reference type. */
bool
declaration_reference_p (Declaration *decl)
{
Type *tb = decl->type->toBasetype ();
/* Declaration is a reference type. */
if (tb->ty == Treference || decl->storage_class & (STCout | STCref))
return true;
return false;
}
/* Returns the real type for declaration DECL. */
tree
declaration_type (Declaration *decl)
{
/* Lazy declarations are converted to delegates. */
if (decl->storage_class & STClazy)
{
TypeFunction *tf = TypeFunction::create (NULL, decl->type, false, LINKd);
TypeDelegate *t = TypeDelegate::create (tf);
return build_ctype (t->merge2 ());
}
/* Static array va_list have array->pointer conversions applied. */
if (decl->isParameter () && valist_array_p (decl->type))
{
Type *valist = decl->type->nextOf ()->pointerTo ();
valist = valist->castMod (decl->type->mod);
return build_ctype (valist);
}
tree type = build_ctype (decl->type);
/* Parameter is passed by reference. */
if (declaration_reference_p (decl))
return build_reference_type (type);
/* The 'this' parameter is always const. */
if (decl->isThisDeclaration ())
return insert_type_modifiers (type, MODconst);
return type;
}
/* These should match the Declaration versions above
Return TRUE if parameter ARG is a reference type. */
bool
argument_reference_p (Parameter *arg)
{
Type *tb = arg->type->toBasetype ();
/* Parameter is a reference type. */
if (tb->ty == Treference || arg->storageClass & (STCout | STCref))
return true;
tree type = build_ctype (arg->type);
if (TREE_ADDRESSABLE (type))
return true;
return false;
}
/* Returns the real type for parameter ARG. */
tree
type_passed_as (Parameter *arg)
{
/* Lazy parameters are converted to delegates. */
if (arg->storageClass & STClazy)
{
TypeFunction *tf = TypeFunction::create (NULL, arg->type, false, LINKd);
TypeDelegate *t = TypeDelegate::create (tf);
return build_ctype (t->merge2 ());
}
/* Static array va_list have array->pointer conversions applied. */
if (valist_array_p (arg->type))
{
Type *valist = arg->type->nextOf ()->pointerTo ();
valist = valist->castMod (arg->type->mod);
return build_ctype (valist);
}
tree type = build_ctype (arg->type);
/* Parameter is passed by reference. */
if (argument_reference_p (arg))
return build_reference_type (type);
return type;
}
/* Build INTEGER_CST of type TYPE with the value VALUE. */
tree
build_integer_cst (dinteger_t value, tree type)
{
/* The type is error_mark_node, we can't do anything. */
if (error_operand_p (type))
return type;
return build_int_cst_type (type, value);
}
/* Build REAL_CST of type TOTYPE with the value VALUE. */
tree
build_float_cst (const real_t& value, Type *totype)
{
real_t new_value;
TypeBasic *tb = totype->isTypeBasic ();
gcc_assert (tb != NULL);
tree type_node = build_ctype (tb);
real_convert (&new_value.rv (), TYPE_MODE (type_node), &value.rv ());
return build_real (type_node, new_value.rv ());
}
/* Returns the .length component from the D dynamic array EXP. */
tree
d_array_length (tree exp)
{
if (error_operand_p (exp))
return exp;
gcc_assert (TYPE_DYNAMIC_ARRAY (TREE_TYPE (exp)));
/* Get the back-end type for the array and pick out the array
length field (assumed to be the first field). */
tree len_field = TYPE_FIELDS (TREE_TYPE (exp));
return component_ref (exp, len_field);
}
/* Returns the .ptr component from the D dynamic array EXP. */
tree
d_array_ptr (tree exp)
{
if (error_operand_p (exp))
return exp;
gcc_assert (TYPE_DYNAMIC_ARRAY (TREE_TYPE (exp)));
/* Get the back-end type for the array and pick out the array
data pointer field (assumed to be the second field). */
tree ptr_field = TREE_CHAIN (TYPE_FIELDS (TREE_TYPE (exp)));
return component_ref (exp, ptr_field);
}
/* Returns a constructor for D dynamic array type TYPE of .length LEN
and .ptr pointing to DATA. */
tree
d_array_value (tree type, tree len, tree data)
{
tree len_field, ptr_field;
vec<constructor_elt, va_gc> *ce = NULL;
gcc_assert (TYPE_DYNAMIC_ARRAY (type));
len_field = TYPE_FIELDS (type);
ptr_field = TREE_CHAIN (len_field);
len = convert (TREE_TYPE (len_field), len);
data = convert (TREE_TYPE (ptr_field), data);
CONSTRUCTOR_APPEND_ELT (ce, len_field, len);
CONSTRUCTOR_APPEND_ELT (ce, ptr_field, data);
return build_constructor (type, ce);
}
/* Returns value representing the array length of expression EXP.
TYPE could be a dynamic or static array. */
tree
get_array_length (tree exp, Type *type)
{
Type *tb = type->toBasetype ();
switch (tb->ty)
{
case Tsarray:
return size_int (((TypeSArray *) tb)->dim->toUInteger ());
case Tarray:
return d_array_length (exp);
default:
error ("can't determine the length of a %qs", type->toChars ());
return error_mark_node;
}
}
/* Create BINFO for a ClassDeclaration's inheritance tree.
InterfaceDeclaration's are not included. */
tree
build_class_binfo (tree super, ClassDeclaration *cd)
{
tree binfo = make_tree_binfo (1);
tree ctype = build_ctype (cd->type);
/* Want RECORD_TYPE, not POINTER_TYPE. */
BINFO_TYPE (binfo) = TREE_TYPE (ctype);
BINFO_INHERITANCE_CHAIN (binfo) = super;
BINFO_OFFSET (binfo) = integer_zero_node;
if (cd->baseClass)
BINFO_BASE_APPEND (binfo, build_class_binfo (binfo, cd->baseClass));
return binfo;
}
/* Create BINFO for an InterfaceDeclaration's inheritance tree.
In order to access all inherited methods in the debugger,
the entire tree must be described.
This function makes assumptions about interface layout. */
tree
build_interface_binfo (tree super, ClassDeclaration *cd, unsigned& offset)
{
tree binfo = make_tree_binfo (cd->baseclasses->dim);
tree ctype = build_ctype (cd->type);
/* Want RECORD_TYPE, not POINTER_TYPE. */
BINFO_TYPE (binfo) = TREE_TYPE (ctype);
BINFO_INHERITANCE_CHAIN (binfo) = super;
BINFO_OFFSET (binfo) = size_int (offset * Target::ptrsize);
BINFO_VIRTUAL_P (binfo) = 1;
for (size_t i = 0; i < cd->baseclasses->dim; i++, offset++)
{
BaseClass *bc = (*cd->baseclasses)[i];
BINFO_BASE_APPEND (binfo, build_interface_binfo (binfo, bc->sym, offset));
}
return binfo;
}
/* Returns the .funcptr component from the D delegate EXP. */
tree
delegate_method (tree exp)
{
/* Get the back-end type for the delegate and pick out the funcptr field
(assumed to be the second field). */
gcc_assert (TYPE_DELEGATE (TREE_TYPE (exp)));
tree method_field = TREE_CHAIN (TYPE_FIELDS (TREE_TYPE (exp)));
return component_ref (exp, method_field);
}
/* Returns the .object component from the delegate EXP. */
tree
delegate_object (tree exp)
{
/* Get the back-end type for the delegate and pick out the object field
(assumed to be the first field). */
gcc_assert (TYPE_DELEGATE (TREE_TYPE (exp)));
tree obj_field = TYPE_FIELDS (TREE_TYPE (exp));
return component_ref (exp, obj_field);
}
/* Build a delegate literal of type TYPE whose pointer function is
METHOD, and hidden object is OBJECT. */
tree
build_delegate_cst (tree method, tree object, Type *type)
{
tree ctor = make_node (CONSTRUCTOR);
tree ctype;
Type *tb = type->toBasetype ();
if (tb->ty == Tdelegate)
ctype = build_ctype (type);
else
{
/* Convert a function method into an anonymous delegate. */
ctype = make_struct_type ("delegate()", 2,
get_identifier ("object"), TREE_TYPE (object),
get_identifier ("func"), TREE_TYPE (method));
TYPE_DELEGATE (ctype) = 1;
}
vec<constructor_elt, va_gc> *ce = NULL;
CONSTRUCTOR_APPEND_ELT (ce, TYPE_FIELDS (ctype), object);
CONSTRUCTOR_APPEND_ELT (ce, TREE_CHAIN (TYPE_FIELDS (ctype)), method);
CONSTRUCTOR_ELTS (ctor) = ce;
TREE_TYPE (ctor) = ctype;
return ctor;
}
/* Builds a temporary tree to store the CALLEE and OBJECT
of a method call expression of type TYPE. */
tree
build_method_call (tree callee, tree object, Type *type)
{
tree t = build_delegate_cst (callee, object, type);
METHOD_CALL_EXPR (t) = 1;
return t;
}
/* Extract callee and object from T and return in to CALLEE and OBJECT. */
void
extract_from_method_call (tree t, tree& callee, tree& object)
{
gcc_assert (METHOD_CALL_EXPR (t));
object = CONSTRUCTOR_ELT (t, 0)->value;
callee = CONSTRUCTOR_ELT (t, 1)->value;
}
/* Build a typeof(null) constant of type TYPE. Handles certain special case
conversions, where the underlying type is an aggregate with a nullable
interior pointer. */
tree
build_typeof_null_value (Type *type)
{
Type *tb = type->toBasetype ();
tree value;
/* For dynamic arrays, set length and pointer fields to zero. */
if (tb->ty == Tarray)
value = d_array_value (build_ctype (type), size_int (0), null_pointer_node);
/* For associative arrays, set the pointer field to null. */
else if (tb->ty == Taarray)
{
tree ctype = build_ctype (type);
gcc_assert (TYPE_ASSOCIATIVE_ARRAY (ctype));
value = build_constructor_single (ctype, TYPE_FIELDS (ctype),
null_pointer_node);
}
/* For delegates, set the frame and function pointer fields to null. */
else if (tb->ty == Tdelegate)
value = build_delegate_cst (null_pointer_node, null_pointer_node, type);
/* Simple zero constant for all other types. */
else
value = build_zero_cst (build_ctype (type));
TREE_CONSTANT (value) = 1;
return value;
}
/* Build a dereference into the virtual table for OBJECT to retrieve
a function pointer of type FNTYPE at position INDEX. */
tree
build_vindex_ref (tree object, tree fntype, size_t index)
{
/* The vtable is the first field. Interface methods are also in the class's
vtable, so we don't need to convert from a class to an interface. */
tree result = build_deref (object);
result = component_ref (result, TYPE_FIELDS (TREE_TYPE (result)));
gcc_assert (POINTER_TYPE_P (fntype));
return build_memref (fntype, result, size_int (Target::ptrsize * index));
}
/* Return TRUE if EXP is a valid lvalue. Lvalue references cannot be
made into temporaries, otherwise any assignments will be lost. */
static bool
lvalue_p (tree exp)
{
const enum tree_code code = TREE_CODE (exp);
switch (code)
{
case SAVE_EXPR:
return false;
case ARRAY_REF:
case INDIRECT_REF:
case VAR_DECL:
case PARM_DECL:
case RESULT_DECL:
return !FUNC_OR_METHOD_TYPE_P (TREE_TYPE (exp));
case IMAGPART_EXPR:
case REALPART_EXPR:
case COMPONENT_REF:
CASE_CONVERT:
return lvalue_p (TREE_OPERAND (exp, 0));
case COND_EXPR:
return (lvalue_p (TREE_OPERAND (exp, 1)
? TREE_OPERAND (exp, 1)
: TREE_OPERAND (exp, 0))
&& lvalue_p (TREE_OPERAND (exp, 2)));
case TARGET_EXPR:
return true;
case COMPOUND_EXPR:
return lvalue_p (TREE_OPERAND (exp, 1));
default:
return false;
}
}
/* Create a SAVE_EXPR if EXP might have unwanted side effects if referenced
more than once in an expression. */
tree
d_save_expr (tree exp)
{
if (TREE_SIDE_EFFECTS (exp))
{
if (lvalue_p (exp))
return stabilize_reference (exp);
return save_expr (exp);
}
return exp;
}
/* VALUEP is an expression we want to pre-evaluate or perform a computation on.
The expression returned by this function is the part whose value we don't
care about, storing the value in VALUEP. Callers must ensure that the
returned expression is evaluated before VALUEP. */
tree
stabilize_expr (tree *valuep)
{
tree expr = *valuep;
const enum tree_code code = TREE_CODE (expr);
tree lhs;
tree rhs;
switch (code)
{
case COMPOUND_EXPR:
/* Given ((e1, ...), eN):
Store the last RHS 'eN' expression in VALUEP. */
lhs = TREE_OPERAND (expr, 0);
rhs = TREE_OPERAND (expr, 1);
lhs = compound_expr (lhs, stabilize_expr (&rhs));
*valuep = rhs;
return lhs;
default:
return NULL_TREE;
}
}
/* Return a TARGET_EXPR, initializing the DECL with EXP. */
tree
build_target_expr (tree decl, tree exp)
{
tree type = TREE_TYPE (decl);
tree result = build4 (TARGET_EXPR, type, decl, exp, NULL_TREE, NULL_TREE);
if (EXPR_HAS_LOCATION (exp))
SET_EXPR_LOCATION (result, EXPR_LOCATION (exp));
/* If decl must always reside in memory. */
if (TREE_ADDRESSABLE (type))
d_mark_addressable (decl);
/* Always set TREE_SIDE_EFFECTS so that expand_expr does not ignore the
TARGET_EXPR. If there really turn out to be no side effects, then the
optimizer should be able to remove it. */
TREE_SIDE_EFFECTS (result) = 1;
return result;
}
/* Like the above function, but initializes a new temporary. */
tree
force_target_expr (tree exp)
{
tree decl = create_temporary_var (TREE_TYPE (exp));
return build_target_expr (decl, exp);
}
/* Returns the address of the expression EXP. */
tree
build_address (tree exp)
{
if (error_operand_p (exp))
return exp;
tree ptrtype;
tree type = TREE_TYPE (exp);
if (TREE_CODE (exp) == STRING_CST)
{
/* Just convert string literals (char[]) to C-style strings (char *),
otherwise the latter method (char[]*) causes conversion problems
during gimplification. */
ptrtype = build_pointer_type (TREE_TYPE (type));
}
else if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (va_list_type_node)
&& TREE_CODE (TYPE_MAIN_VARIANT (type)) == ARRAY_TYPE)
{
/* Special case for va_list, allow arrays to decay to a pointer. */
ptrtype = build_pointer_type (TREE_TYPE (type));
}
else
ptrtype = build_pointer_type (type);
/* Maybe rewrite: &(e1, e2) => (e1, &e2). */
tree init = stabilize_expr (&exp);
/* Can't take the address of a manifest constant, instead use its value. */
if (TREE_CODE (exp) == CONST_DECL)
exp = DECL_INITIAL (exp);
/* Some expression lowering may request an address of a compile-time constant.
Make sure it is assigned to a location we can reference. */
if (CONSTANT_CLASS_P (exp) && TREE_CODE (exp) != STRING_CST)
exp = force_target_expr (exp);
d_mark_addressable (exp);
exp = build_fold_addr_expr_with_type_loc (input_location, exp, ptrtype);
if (TREE_CODE (exp) == ADDR_EXPR)
TREE_NO_TRAMPOLINE (exp) = 1;
return compound_expr (init, exp);
}
/* Mark EXP saying that we need to be able to take the
address of it; it should not be allocated in a register. */
tree
d_mark_addressable (tree exp)
{
switch (TREE_CODE (exp))
{
case ADDR_EXPR:
case COMPONENT_REF:
case ARRAY_REF:
case REALPART_EXPR:
case IMAGPART_EXPR:
d_mark_addressable (TREE_OPERAND (exp, 0));
break;
case PARM_DECL:
case VAR_DECL:
case RESULT_DECL:
case CONST_DECL:
case FUNCTION_DECL:
TREE_ADDRESSABLE (exp) = 1;
break;
case CONSTRUCTOR:
TREE_ADDRESSABLE (exp) = 1;
break;
case TARGET_EXPR:
TREE_ADDRESSABLE (exp) = 1;
d_mark_addressable (TREE_OPERAND (exp, 0));
break;
default:
break;
}
return exp;
}
/* Mark EXP as "used" in the program for the benefit of
-Wunused warning purposes. */
tree
d_mark_used (tree exp)
{
switch (TREE_CODE (exp))
{
case VAR_DECL:
case CONST_DECL:
case PARM_DECL:
case RESULT_DECL:
case FUNCTION_DECL:
TREE_USED (exp) = 1;
break;
case ARRAY_REF:
case COMPONENT_REF:
case MODIFY_EXPR:
case REALPART_EXPR:
case IMAGPART_EXPR:
case NOP_EXPR:
case CONVERT_EXPR:
case ADDR_EXPR:
d_mark_used (TREE_OPERAND (exp, 0));
break;
case COMPOUND_EXPR:
d_mark_used (TREE_OPERAND (exp, 0));
d_mark_used (TREE_OPERAND (exp, 1));
break;
default:
break;
}
return exp;
}
/* Mark EXP as read, not just set, for set but not used -Wunused
warning purposes. */
tree
d_mark_read (tree exp)
{
switch (TREE_CODE (exp))
{
case VAR_DECL:
case PARM_DECL:
TREE_USED (exp) = 1;
DECL_READ_P (exp) = 1;
break;
case ARRAY_REF:
case COMPONENT_REF:
case MODIFY_EXPR:
case REALPART_EXPR:
case IMAGPART_EXPR:
case NOP_EXPR:
case CONVERT_EXPR:
case ADDR_EXPR:
d_mark_read (TREE_OPERAND (exp, 0));
break;
case COMPOUND_EXPR:
d_mark_read (TREE_OPERAND (exp, 1));
break;
default:
break;
}
return exp;
}
/* Return TRUE if the struct SD is suitable for comparison using memcmp.
This is because we don't guarantee that padding is zero-initialized for
a stack variable, so we can't use memcmp to compare struct values. */
bool
identity_compare_p (StructDeclaration *sd)
{
if (sd->isUnionDeclaration ())
return true;
unsigned offset = 0;
for (size_t i = 0; i < sd->fields.dim; i++)
{
VarDeclaration *vd = sd->fields[i];
Type *tb = vd->type->toBasetype ();
/* Check inner data structures. */
if (tb->ty == Tstruct)
{
TypeStruct *ts = (TypeStruct *) tb;
if (!identity_compare_p (ts->sym))
return false;
}
/* Check for types that may have padding. */
if ((tb->ty == Tcomplex80 || tb->ty == Tfloat80 || tb->ty == Timaginary80)
&& Target::realpad != 0)
return false;
if (offset <= vd->offset)
{
/* There's a hole in the struct. */
if (offset != vd->offset)
return false;
offset += vd->type->size ();
}
}
/* Any trailing padding may not be zero. */
if (offset < sd->structsize)
return false;
return true;
}
/* Build a floating-point identity comparison between T1 and T2, ignoring any
excessive padding in the type. CODE is EQ_EXPR or NE_EXPR comparison. */
tree
build_float_identity (tree_code code, tree t1, tree t2)
{
tree tmemcmp = builtin_decl_explicit (BUILT_IN_MEMCMP);
tree size = size_int (TYPE_PRECISION (TREE_TYPE (t1)) / BITS_PER_UNIT);
tree result = build_call_expr (tmemcmp, 3, build_address (t1),
build_address (t2), size);
return build_boolop (code, result, integer_zero_node);
}
/* Lower a field-by-field equality expression between T1 and T2 of type SD.
CODE is the EQ_EXPR or NE_EXPR comparison. */
static tree
lower_struct_comparison (tree_code code, StructDeclaration *sd,
tree t1, tree t2)
{
tree_code tcode = (code == EQ_EXPR) ? TRUTH_ANDIF_EXPR : TRUTH_ORIF_EXPR;
tree tmemcmp = NULL_TREE;
/* We can skip the compare if the structs are empty. */
if (sd->fields.dim == 0)
{
tmemcmp = build_boolop (code, integer_zero_node, integer_zero_node);
if (TREE_SIDE_EFFECTS (t2))
tmemcmp = compound_expr (t2, tmemcmp);
if (TREE_SIDE_EFFECTS (t1))
tmemcmp = compound_expr (t1, tmemcmp);
return tmemcmp;
}
/* Let back-end take care of union comparisons. */
if (sd->isUnionDeclaration ())
{
tmemcmp = build_call_expr (builtin_decl_explicit (BUILT_IN_MEMCMP), 3,
build_address (t1), build_address (t2),
size_int (sd->structsize));
return build_boolop (code, tmemcmp, integer_zero_node);
}
for (size_t i = 0; i < sd->fields.dim; i++)
{
VarDeclaration *vd = sd->fields[i];
Type *type = vd->type->toBasetype ();
tree sfield = get_symbol_decl (vd);
tree t1ref = component_ref (t1, sfield);
tree t2ref = component_ref (t2, sfield);
tree tcmp;
if (type->ty == Tstruct)
{
/* Compare inner data structures. */
StructDeclaration *decl = ((TypeStruct *) type)->sym;
tcmp = lower_struct_comparison (code, decl, t1ref, t2ref);
}
else if (type->ty != Tvector && type->isintegral ())
{
/* Integer comparison, no special handling required. */
tcmp = build_boolop (code, t1ref, t2ref);
}
else if (type->ty != Tvector && type->isfloating ())
{
/* Floating-point comparison, don't compare padding in type. */
if (!type->iscomplex ())
tcmp = build_float_identity (code, t1ref, t2ref);
else
{
tree req = build_float_identity (code, real_part (t1ref),
real_part (t2ref));
tree ieq = build_float_identity (code, imaginary_part (t1ref),
imaginary_part (t2ref));
tcmp = build_boolop (tcode, req, ieq);
}
}
else
{
tree stype = build_ctype (type);
opt_scalar_int_mode mode = int_mode_for_mode (TYPE_MODE (stype));
if (mode.exists ())
{
/* Compare field bits as their corresponding integer type.
*((T*) &t1) == *((T*) &t2) */
tree tmode = lang_hooks.types.type_for_mode (mode.require (), 1);
if (tmode == NULL_TREE)
tmode = make_unsigned_type (GET_MODE_BITSIZE (mode.require ()));
t1ref = build_vconvert (tmode, t1ref);
t2ref = build_vconvert (tmode, t2ref);
tcmp = build_boolop (code, t1ref, t2ref);
}
else
{
/* Simple memcmp between types. */
tcmp = build_call_expr (builtin_decl_explicit (BUILT_IN_MEMCMP),
3, build_address (t1ref),
build_address (t2ref),
TYPE_SIZE_UNIT (stype));
tcmp = build_boolop (code, tcmp, integer_zero_node);
}
}
tmemcmp = (tmemcmp) ? build_boolop (tcode, tmemcmp, tcmp) : tcmp;
}
return tmemcmp;
}
/* Build an equality expression between two RECORD_TYPES T1 and T2 of type SD.
If possible, use memcmp, otherwise field-by-field comparison is done.
CODE is the EQ_EXPR or NE_EXPR comparison. */
tree
build_struct_comparison (tree_code code, StructDeclaration *sd,
tree t1, tree t2)
{
/* We can skip the compare if the structs are empty. */
if (sd->fields.dim == 0)
{
tree exp = build_boolop (code, integer_zero_node, integer_zero_node);
if (TREE_SIDE_EFFECTS (t2))
exp = compound_expr (t2, exp);
if (TREE_SIDE_EFFECTS (t1))
exp = compound_expr (t1, exp);
return exp;
}
/* Make temporaries to prevent multiple evaluations. */
tree t1init = stabilize_expr (&t1);
tree t2init = stabilize_expr (&t2);
tree result;
t1 = d_save_expr (t1);
t2 = d_save_expr (t2);
/* Bitwise comparison of structs not returned in memory may not work
due to data holes loosing its zero padding upon return.
As a heuristic, small structs are not compared using memcmp either. */
if (TYPE_MODE (TREE_TYPE (t1)) != BLKmode || !identity_compare_p (sd))
result = lower_struct_comparison (code, sd, t1, t2);
else
{
/* Do bit compare of structs. */
tree size = size_int (sd->structsize);
tree tmemcmp = build_call_expr (builtin_decl_explicit (BUILT_IN_MEMCMP),
3, build_address (t1),
build_address (t2), size);
result = build_boolop (code, tmemcmp, integer_zero_node);
}
return compound_expr (compound_expr (t1init, t2init), result);
}
/* Build an equality expression between two ARRAY_TYPES of size LENGTH.
The pointer references are T1 and T2, and the element type is SD.
CODE is the EQ_EXPR or NE_EXPR comparison. */
tree
build_array_struct_comparison (tree_code code, StructDeclaration *sd,
tree length, tree t1, tree t2)
{
tree_code tcode = (code == EQ_EXPR) ? TRUTH_ANDIF_EXPR : TRUTH_ORIF_EXPR;
/* Build temporary for the result of the comparison.
Initialize as either 0 or 1 depending on operation. */
tree result = build_local_temp (d_bool_type);
tree init = build_boolop (code, integer_zero_node, integer_zero_node);
add_stmt (build_assign (INIT_EXPR, result, init));
/* Cast pointer-to-array to pointer-to-struct. */
tree ptrtype = build_ctype (sd->type->pointerTo ());
tree lentype = TREE_TYPE (length);
push_binding_level (level_block);
push_stmt_list ();
/* Build temporary locals for length and pointers. */
tree t = build_local_temp (size_type_node);
add_stmt (build_assign (INIT_EXPR, t, length));
length = t;
t = build_local_temp (ptrtype);
add_stmt (build_assign (INIT_EXPR, t, d_convert (ptrtype, t1)));
t1 = t;
t = build_local_temp (ptrtype);
add_stmt (build_assign (INIT_EXPR, t, d_convert (ptrtype, t2)));
t2 = t;
/* Build loop for comparing each element. */
push_stmt_list ();
/* Exit logic for the loop.
if (length == 0 || result OP 0) break; */
t = build_boolop (EQ_EXPR, length, d_convert (lentype, integer_zero_node));
t = build_boolop (TRUTH_ORIF_EXPR, t, build_boolop (code, result,
boolean_false_node));
t = build1 (EXIT_EXPR, void_type_node, t);
add_stmt (t);
/* Do comparison, caching the value.
result = result OP (*t1 == *t2); */
t = build_struct_comparison (code, sd, build_deref (t1), build_deref (t2));
t = build_boolop (tcode, result, t);
t = modify_expr (result, t);
add_stmt (t);
/* Move both pointers to next element position.
t1++, t2++; */
tree size = d_convert (ptrtype, TYPE_SIZE_UNIT (TREE_TYPE (ptrtype)));
t = build2 (POSTINCREMENT_EXPR, ptrtype, t1, size);
add_stmt (t);
t = build2 (POSTINCREMENT_EXPR, ptrtype, t2, size);
add_stmt (t);
/* Decrease loop counter.
length -= 1; */
t = build2 (POSTDECREMENT_EXPR, lentype, length,
d_convert (lentype, integer_one_node));
add_stmt (t);
/* Pop statements and finish loop. */
tree body = pop_stmt_list ();
add_stmt (build1 (LOOP_EXPR, void_type_node, body));
/* Wrap it up into a bind expression. */
tree stmt_list = pop_stmt_list ();
tree block = pop_binding_level ();
body = build3 (BIND_EXPR, void_type_node,
BLOCK_VARS (block), stmt_list, block);
return compound_expr (body, result);
}
/* Create an anonymous field of type ubyte[T] at OFFSET to fill
the alignment hole between OFFSET and FIELDPOS. */
static tree
build_alignment_field (tree type, HOST_WIDE_INT offset, HOST_WIDE_INT fieldpos)
{
tree atype = make_array_type (Type::tuns8, fieldpos - offset);
tree field = create_field_decl (atype, NULL, 1, 1);
SET_DECL_OFFSET_ALIGN (field, TYPE_ALIGN (atype));
DECL_FIELD_OFFSET (field) = size_int (offset);
DECL_FIELD_BIT_OFFSET (field) = bitsize_zero_node;
DECL_FIELD_CONTEXT (field) = type;
DECL_PADDING_P (field) = 1;
layout_decl (field, 0);
return field;
}
/* Build a constructor for a variable of aggregate type TYPE using the
initializer INIT, an ordered flat list of fields and values provided
by the frontend. The returned constructor should be a value that
matches the layout of TYPE. */
tree
build_struct_literal (tree type, vec<constructor_elt, va_gc> *init)
{
/* If the initializer was empty, use default zero initialization. */
if (vec_safe_is_empty (init))
return build_constructor (type, NULL);
vec<constructor_elt, va_gc> *ve = NULL;
HOST_WIDE_INT offset = 0;
bool constant_p = true;
bool fillholes = true;
bool finished = false;
/* Filling alignment holes this only applies to structs. */
if (TREE_CODE (type) != RECORD_TYPE
|| CLASS_TYPE_P (type) || TYPE_PACKED (type))
fillholes = false;
/* Walk through each field, matching our initializer list. */
for (tree field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
{
bool is_initialized = false;
tree value;
if (DECL_NAME (field) == NULL_TREE
&& RECORD_OR_UNION_TYPE_P (TREE_TYPE (field))
&& ANON_AGGR_TYPE_P (TREE_TYPE (field)))
{
/* Search all nesting aggregates, if nothing is found, then
this will return an empty initializer to fill the hole. */
value = build_struct_literal (TREE_TYPE (field), init);
if (!initializer_zerop (value))
is_initialized = true;
}
else
{
/* Search for the value to initialize the next field. Once found,
pop it from the init list so we don't look at it again. */
unsigned HOST_WIDE_INT idx;
tree index;
FOR_EACH_CONSTRUCTOR_ELT (init, idx, index, value)
{
/* If the index is NULL, then just assign it to the next field.
This comes from layout_typeinfo(), which generates a flat
list of values that we must shape into the record type. */
if (index == field || index == NULL_TREE)
{
init->ordered_remove (idx);
if (!finished)
is_initialized = true;
break;
}
}
}
if (is_initialized)
{
HOST_WIDE_INT fieldpos = int_byte_position (field);
gcc_assert (value != NULL_TREE);
/* Insert anonymous fields in the constructor for padding out
alignment holes in-place between fields. */
if (fillholes && offset < fieldpos)
{
tree pfield = build_alignment_field (type, offset, fieldpos);
tree pvalue = build_zero_cst (TREE_TYPE (pfield));
CONSTRUCTOR_APPEND_ELT (ve, pfield, pvalue);
}
/* Must not initialize fields that overlap. */
if (fieldpos < offset)
{
/* Find the nearest user defined type and field. */
tree vtype = type;
while (ANON_AGGR_TYPE_P (vtype))
vtype = TYPE_CONTEXT (vtype);
tree vfield = field;
if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (vfield))
&& ANON_AGGR_TYPE_P (TREE_TYPE (vfield)))
vfield = TYPE_FIELDS (TREE_TYPE (vfield));
/* Must not generate errors for compiler generated fields. */
gcc_assert (TYPE_NAME (vtype) && DECL_NAME (vfield));
error ("overlapping initializer for field %qT.%qD",
TYPE_NAME (vtype), DECL_NAME (vfield));
}
if (!TREE_CONSTANT (value))
constant_p = false;
CONSTRUCTOR_APPEND_ELT (ve, field, value);
/* For unions, only the first field is initialized, any other field
initializers found for this union are drained and ignored. */
if (TREE_CODE (type) == UNION_TYPE)
finished = true;
}
/* Move offset to the next position in the struct. */
if (TREE_CODE (type) == RECORD_TYPE)
{
offset = int_byte_position (field)
+ int_size_in_bytes (TREE_TYPE (field));
}
/* If all initializers have been assigned, there's nothing else to do. */
if (vec_safe_is_empty (init))
break;
}
/* Finally pad out the end of the record. */
if (fillholes && offset < int_size_in_bytes (type))
{
tree pfield = build_alignment_field (type, offset,
int_size_in_bytes (type));
tree pvalue = build_zero_cst (TREE_TYPE (pfield));
CONSTRUCTOR_APPEND_ELT (ve, pfield, pvalue);
}
/* Ensure that we have consumed all values. */
gcc_assert (vec_safe_is_empty (init) || ANON_AGGR_TYPE_P (type));
tree ctor = build_constructor (type, ve);
if (constant_p)
TREE_CONSTANT (ctor) = 1;
return ctor;
}
/* Given the TYPE of an anonymous field inside T, return the
FIELD_DECL for the field. If not found return NULL_TREE.
Because anonymous types can nest, we must also search all
anonymous fields that are directly reachable. */
static tree
lookup_anon_field (tree t, tree type)
{
t = TYPE_MAIN_VARIANT (t);
for (tree field = TYPE_FIELDS (t); field; field = DECL_CHAIN (field))
{
if (DECL_NAME (field) == NULL_TREE)
{
/* If we find it directly, return the field. */
if (type == TYPE_MAIN_VARIANT (TREE_TYPE (field)))
return field;
/* Otherwise, it could be nested, search harder. */
if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (field))
&& ANON_AGGR_TYPE_P (TREE_TYPE (field)))
{
tree subfield = lookup_anon_field (TREE_TYPE (field), type);
if (subfield)
return subfield;
}
}
}
return NULL_TREE;
}
/* Builds OBJECT.FIELD component reference. */
tree
component_ref (tree object, tree field)
{
if (error_operand_p (object) || error_operand_p (field))
return error_mark_node;
gcc_assert (TREE_CODE (field) == FIELD_DECL);
/* Maybe rewrite: (e1, e2).field => (e1, e2.field) */
tree init = stabilize_expr (&object);
/* If the FIELD is from an anonymous aggregate, generate a reference
to the anonymous data member, and recur to find FIELD. */
if (ANON_AGGR_TYPE_P (DECL_CONTEXT (field)))
{
tree anonymous_field = lookup_anon_field (TREE_TYPE (object),
DECL_CONTEXT (field));
object = component_ref (object, anonymous_field);
}
tree result = fold_build3_loc (input_location, COMPONENT_REF,
TREE_TYPE (field), object, field, NULL_TREE);
return compound_expr (init, result);
}
/* Build an assignment expression of lvalue LHS from value RHS.
CODE is the code for a binary operator that we use to combine
the old value of LHS with RHS to get the new value. */
tree
build_assign (tree_code code, tree lhs, tree rhs)
{
tree init = stabilize_expr (&lhs);
init = compound_expr (init, stabilize_expr (&rhs));
/* If initializing the LHS using a function that returns via NRVO. */
if (code == INIT_EXPR && TREE_CODE (rhs) == CALL_EXPR
&& AGGREGATE_TYPE_P (TREE_TYPE (rhs))
&& aggregate_value_p (TREE_TYPE (rhs), rhs))
{
/* Mark as addressable here, which should ensure the return slot is the
address of the LHS expression, taken care of by back-end. */
d_mark_addressable (lhs);
CALL_EXPR_RETURN_SLOT_OPT (rhs) = true;
}
/* The LHS assignment replaces the temporary in TARGET_EXPR_SLOT. */
if (TREE_CODE (rhs) == TARGET_EXPR)
{
/* If CODE is not INIT_EXPR, can't initialize LHS directly,
since that would cause the LHS to be constructed twice.
So we force the TARGET_EXPR to be expanded without a target. */
if (code != INIT_EXPR)
rhs = compound_expr (rhs, TARGET_EXPR_SLOT (rhs));
else
{
d_mark_addressable (lhs);
rhs = TARGET_EXPR_INITIAL (rhs);
}
}
tree result = fold_build2_loc (input_location, code,
TREE_TYPE (lhs), lhs, rhs);
return compound_expr (init, result);
}
/* Build an assignment expression of lvalue LHS from value RHS. */
tree
modify_expr (tree lhs, tree rhs)
{
return build_assign (MODIFY_EXPR, lhs, rhs);
}
/* Return EXP represented as TYPE. */
tree
build_nop (tree type, tree exp)
{
if (error_operand_p (exp))
return exp;
/* Maybe rewrite: cast(TYPE)(e1, e2) => (e1, cast(TYPE) e2) */
tree init = stabilize_expr (&exp);
exp = fold_build1_loc (input_location, NOP_EXPR, type, exp);
return compound_expr (init, exp);
}
/* Return EXP to be viewed as being another type TYPE. Same as build_nop,
except that EXP is type-punned, rather than a straight-forward cast. */
tree
build_vconvert (tree type, tree exp)
{
/* Building *(cast(TYPE *)&e1) directly rather then using VIEW_CONVERT_EXPR
makes sure this works for vector-to-array viewing, or if EXP ends up being
used as the LHS of a MODIFY_EXPR. */
return indirect_ref (type, build_address (exp));
}
/* Maybe warn about ARG being an address that can never be null. */
static void
warn_for_null_address (tree arg)
{
if (TREE_CODE (arg) == ADDR_EXPR
&& decl_with_nonnull_addr_p (TREE_OPERAND (arg, 0)))
warning (OPT_Waddress,
"the address of %qD will never be %<null%>",
TREE_OPERAND (arg, 0));
}
/* Build a boolean ARG0 op ARG1 expression. */
tree
build_boolop (tree_code code, tree arg0, tree arg1)
{
/* Aggregate comparisons may get lowered to a call to builtin memcmp,
so need to remove all side effects incase its address is taken. */
if (AGGREGATE_TYPE_P (TREE_TYPE (arg0)))
arg0 = d_save_expr (arg0);
if (AGGREGATE_TYPE_P (TREE_TYPE (arg1)))
arg1 = d_save_expr (arg1);
if (VECTOR_TYPE_P (TREE_TYPE (arg0)) && VECTOR_TYPE_P (TREE_TYPE (arg1)))
{
/* Build a vector comparison.
VEC_COND_EXPR <e1 op e2, { -1, -1, -1, -1 }, { 0, 0, 0, 0 }>; */
tree type = TREE_TYPE (arg0);
tree cmptype = build_same_sized_truth_vector_type (type);
tree cmp = fold_build2_loc (input_location, code, cmptype, arg0, arg1);
return fold_build3_loc (input_location, VEC_COND_EXPR, type, cmp,
build_minus_one_cst (type),
build_zero_cst (type));
}
if (code == EQ_EXPR || code == NE_EXPR)
{
/* Check if comparing the address of a variable to null. */
if (POINTER_TYPE_P (TREE_TYPE (arg0)) && integer_zerop (arg1))
warn_for_null_address (arg0);
if (POINTER_TYPE_P (TREE_TYPE (arg1)) && integer_zerop (arg0))
warn_for_null_address (arg1);
}
return fold_build2_loc (input_location, code, d_bool_type,
arg0, d_convert (TREE_TYPE (arg0), arg1));
}
/* Return a COND_EXPR. ARG0, ARG1, and ARG2 are the three
arguments to the conditional expression. */
tree
build_condition (tree type, tree arg0, tree arg1, tree arg2)
{
if (arg1 == void_node)
arg1 = build_empty_stmt (input_location);
if (arg2 == void_node)
arg2 = build_empty_stmt (input_location);
return fold_build3_loc (input_location, COND_EXPR,
type, arg0, arg1, arg2);
}
tree
build_vcondition (tree arg0, tree arg1, tree arg2)
{
return build_condition (void_type_node, arg0, arg1, arg2);
}
/* Build a compound expr to join ARG0 and ARG1 together. */
tree
compound_expr (tree arg0, tree arg1)
{
if (arg1 == NULL_TREE)
return arg0;
if (arg0 == NULL_TREE || !TREE_SIDE_EFFECTS (arg0))
return arg1;
if (TREE_CODE (arg1) == TARGET_EXPR)
{
/* If the rhs is a TARGET_EXPR, then build the compound expression
inside the target_expr's initializer. This helps the compiler
to eliminate unnecessary temporaries. */
tree init = compound_expr (arg0, TARGET_EXPR_INITIAL (arg1));
TARGET_EXPR_INITIAL (arg1) = init;
return arg1;
}
return fold_build2_loc (input_location, COMPOUND_EXPR,
TREE_TYPE (arg1), arg0, arg1);
}
/* Build a return expression. */
tree
return_expr (tree ret)
{
return fold_build1_loc (input_location, RETURN_EXPR,
void_type_node, ret);
}
/* Return the product of ARG0 and ARG1 as a size_type_node. */
tree
size_mult_expr (tree arg0, tree arg1)
{
return fold_build2_loc (input_location, MULT_EXPR, size_type_node,
d_convert (size_type_node, arg0),
d_convert (size_type_node, arg1));
}
/* Return the real part of CE, which should be a complex expression. */
tree
real_part (tree ce)
{
return fold_build1_loc (input_location, REALPART_EXPR,
TREE_TYPE (TREE_TYPE (ce)), ce);
}
/* Return the imaginary part of CE, which should be a complex expression. */
tree
imaginary_part (tree ce)
{
return fold_build1_loc (input_location, IMAGPART_EXPR,
TREE_TYPE (TREE_TYPE (ce)), ce);
}
/* Build a complex expression of type TYPE using RE and IM. */
tree
complex_expr (tree type, tree re, tree im)
{
return fold_build2_loc (input_location, COMPLEX_EXPR,
type, re, im);
}
/* Cast EXP (which should be a pointer) to TYPE* and then indirect.
The back-end requires this cast in many cases. */
tree
indirect_ref (tree type, tree exp)
{
if (error_operand_p (exp))
return exp;
/* Maybe rewrite: *(e1, e2) => (e1, *e2) */
tree init = stabilize_expr (&exp);
if (TREE_CODE (TREE_TYPE (exp)) == REFERENCE_TYPE)
exp = fold_build1 (INDIRECT_REF, type, exp);
else
{
exp = build_nop (build_pointer_type (type), exp);
exp = build_deref (exp);
}
return compound_expr (init, exp);
}
/* Returns indirect reference of EXP, which must be a pointer type. */
tree
build_deref (tree exp)
{
if (error_operand_p (exp))
return exp;
/* Maybe rewrite: *(e1, e2) => (e1, *e2) */
tree init = stabilize_expr (&exp);
gcc_assert (POINTER_TYPE_P (TREE_TYPE (exp)));
if (TREE_CODE (exp) == ADDR_EXPR)
exp = TREE_OPERAND (exp, 0);
else
exp = build_fold_indirect_ref (exp);
return compound_expr (init, exp);
}
/* Builds pointer offset expression PTR[INDEX]. */
tree
build_array_index (tree ptr, tree index)
{
if (error_operand_p (ptr) || error_operand_p (index))
return error_mark_node;
tree ptr_type = TREE_TYPE (ptr);
tree target_type = TREE_TYPE (ptr_type);
tree type = lang_hooks.types.type_for_size (TYPE_PRECISION (sizetype),
TYPE_UNSIGNED (sizetype));
/* Array element size. */
tree size_exp = size_in_bytes (target_type);
if (integer_zerop (size_exp))
{
/* Test for array of void. */
if (TYPE_MODE (target_type) == TYPE_MODE (void_type_node))
index = fold_convert (type, index);
else
{
/* Should catch this earlier. */
error ("invalid use of incomplete type %qD", TYPE_NAME (target_type));
ptr_type = error_mark_node;
}
}
else if (integer_onep (size_exp))
{
/* Array of bytes -- No need to multiply. */
index = fold_convert (type, index);
}
else
{
index = d_convert (type, index);
index = fold_build2 (MULT_EXPR, TREE_TYPE (index),
index, d_convert (TREE_TYPE (index), size_exp));
index = fold_convert (type, index);
}
if (integer_zerop (index))
return ptr;
return fold_build2 (POINTER_PLUS_EXPR, ptr_type, ptr, index);
}
/* Builds pointer offset expression *(PTR OP OFFSET)
OP could be a plus or minus expression. */
tree
build_offset_op (tree_code op, tree ptr, tree offset)
{
gcc_assert (op == MINUS_EXPR || op == PLUS_EXPR);
tree type = lang_hooks.types.type_for_size (TYPE_PRECISION (sizetype),
TYPE_UNSIGNED (sizetype));
offset = fold_convert (type, offset);
if (op == MINUS_EXPR)
offset = fold_build1 (NEGATE_EXPR, type, offset);
return fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (ptr), ptr, offset);
}
/* Builds pointer offset expression *(PTR + OFFSET). */
tree
build_offset (tree ptr, tree offset)
{
return build_offset_op (PLUS_EXPR, ptr, offset);
}
tree
build_memref (tree type, tree ptr, tree offset)
{
return fold_build2 (MEM_REF, type, ptr, fold_convert (type, offset));
}
/* Create a tree node to set multiple elements to a single value. */
tree
build_array_set (tree ptr, tree length, tree value)
{
tree ptrtype = TREE_TYPE (ptr);
tree lentype = TREE_TYPE (length);
push_binding_level (level_block);
push_stmt_list ();
/* Build temporary locals for length and ptr, and maybe value. */
tree t = build_local_temp (size_type_node);
add_stmt (build_assign (INIT_EXPR, t, length));
length = t;
t = build_local_temp (ptrtype);
add_stmt (build_assign (INIT_EXPR, t, ptr));
ptr = t;
if (TREE_SIDE_EFFECTS (value))
{
t = build_local_temp (TREE_TYPE (value));
add_stmt (build_assign (INIT_EXPR, t, value));
value = t;
}
/* Build loop to initialize { .length=length, .ptr=ptr } with value. */
push_stmt_list ();
/* Exit logic for the loop.
if (length == 0) break; */
t = build_boolop (EQ_EXPR, length, d_convert (lentype, integer_zero_node));
t = build1 (EXIT_EXPR, void_type_node, t);
add_stmt (t);
/* Assign value to the current pointer position.
*ptr = value; */
t = modify_expr (build_deref (ptr), value);
add_stmt (t);
/* Move pointer to next element position.
ptr++; */
tree size = TYPE_SIZE_UNIT (TREE_TYPE (ptrtype));
t = build2 (POSTINCREMENT_EXPR, ptrtype, ptr, d_convert (ptrtype, size));
add_stmt (t);
/* Decrease loop counter.
length -= 1; */
t = build2 (POSTDECREMENT_EXPR, lentype, length,
d_convert (lentype, integer_one_node));
add_stmt (t);
/* Pop statements and finish loop. */
tree loop_body = pop_stmt_list ();
add_stmt (build1 (LOOP_EXPR, void_type_node, loop_body));
/* Wrap it up into a bind expression. */
tree stmt_list = pop_stmt_list ();
tree block = pop_binding_level ();
return build3 (BIND_EXPR, void_type_node,
BLOCK_VARS (block), stmt_list, block);
}
/* Build an array of type TYPE where all the elements are VAL. */
tree
build_array_from_val (Type *type, tree val)
{
gcc_assert (type->ty == Tsarray);
tree etype = build_ctype (type->nextOf ());
/* Initializing a multidimensional array. */
if (TREE_CODE (etype) == ARRAY_TYPE && TREE_TYPE (val) != etype)
val = build_array_from_val (type->nextOf (), val);
size_t dims = ((TypeSArray *) type)->dim->toInteger ();
vec<constructor_elt, va_gc> *elms = NULL;
vec_safe_reserve (elms, dims);
val = d_convert (etype, val);
for (size_t i = 0; i < dims; i++)
CONSTRUCTOR_APPEND_ELT (elms, size_int (i), val);
return build_constructor (build_ctype (type), elms);
}
/* Implicitly converts void* T to byte* as D allows { void[] a; &a[3]; } */
tree
void_okay_p (tree t)
{
tree type = TREE_TYPE (t);
if (VOID_TYPE_P (TREE_TYPE (type)))
{
tree totype = build_ctype (Type::tuns8->pointerTo ());
return fold_convert (totype, t);
}
return t;
}
/* Builds a bounds condition checking that INDEX is between 0 and LEN.
The condition returns the INDEX if true, or throws a RangeError.
If INCLUSIVE, we allow INDEX == LEN to return true also. */
tree
build_bounds_condition (const Loc& loc, tree index, tree len, bool inclusive)
{
if (!array_bounds_check ())
return index;
/* Prevent multiple evaluations of the index. */
index = d_save_expr (index);
/* Generate INDEX >= LEN && throw RangeError.
No need to check whether INDEX >= 0 as the front-end should
have already taken care of implicit casts to unsigned. */
tree condition = fold_build2 (inclusive ? GT_EXPR : GE_EXPR,
d_bool_type, index, len);
tree boundserr = d_assert_call (loc, LIBCALL_ARRAY_BOUNDS);
return build_condition (TREE_TYPE (index), condition, boundserr, index);
}
/* Returns TRUE if array bounds checking code generation is turned on. */
bool
array_bounds_check (void)
{
FuncDeclaration *fd;
switch (global.params.useArrayBounds)
{
case BOUNDSCHECKoff:
return false;
case BOUNDSCHECKon:
return true;
case BOUNDSCHECKsafeonly:
/* For D2 safe functions only. */
fd = d_function_chain->function;
if (fd && fd->type->ty == Tfunction)
{
TypeFunction *tf = (TypeFunction *) fd->type;
if (tf->trust == TRUSTsafe)
return true;
}
return false;
default:
gcc_unreachable ();
}
}
/* Return an undeclared local temporary of type TYPE
for use with BIND_EXPR. */
tree
create_temporary_var (tree type)
{
tree decl = build_decl (input_location, VAR_DECL, NULL_TREE, type);
DECL_CONTEXT (decl) = current_function_decl;
DECL_ARTIFICIAL (decl) = 1;
DECL_IGNORED_P (decl) = 1;
layout_decl (decl, 0);
return decl;
}
/* Return an undeclared local temporary OUT_VAR initialized
with result of expression EXP. */
tree
maybe_temporary_var (tree exp, tree *out_var)
{
tree t = exp;
/* Get the base component. */
while (TREE_CODE (t) == COMPONENT_REF)
t = TREE_OPERAND (t, 0);
if (!DECL_P (t) && !REFERENCE_CLASS_P (t))
{
*out_var = create_temporary_var (TREE_TYPE (exp));
DECL_INITIAL (*out_var) = exp;
return *out_var;
}
else
{
*out_var = NULL_TREE;
return exp;
}
}
/* Builds a BIND_EXPR around BODY for the variables VAR_CHAIN. */
tree
bind_expr (tree var_chain, tree body)
{
/* Only handles one var. */
gcc_assert (TREE_CHAIN (var_chain) == NULL_TREE);
if (DECL_INITIAL (var_chain))
{
tree ini = build_assign (INIT_EXPR, var_chain, DECL_INITIAL (var_chain));
DECL_INITIAL (var_chain) = NULL_TREE;
body = compound_expr (ini, body);
}
return d_save_expr (build3 (BIND_EXPR, TREE_TYPE (body),
var_chain, body, NULL_TREE));
}
/* Returns the TypeFunction class for Type T.
Assumes T is already ->toBasetype(). */
TypeFunction *
get_function_type (Type *t)
{
TypeFunction *tf = NULL;
if (t->ty == Tpointer)
t = t->nextOf ()->toBasetype ();
if (t->ty == Tfunction)
tf = (TypeFunction *) t;
else if (t->ty == Tdelegate)
tf = (TypeFunction *) ((TypeDelegate *) t)->next;
return tf;
}
/* Returns TRUE if CALLEE is a plain nested function outside the scope of
CALLER. In which case, CALLEE is being called through an alias that was
passed to CALLER. */
bool
call_by_alias_p (FuncDeclaration *caller, FuncDeclaration *callee)
{
if (!callee->isNested ())
return false;
if (caller->toParent () == callee->toParent ())
return false;
Dsymbol *dsym = callee;
while (dsym)
{
if (dsym->isTemplateInstance ())
return false;
else if (dsym->isFuncDeclaration () == caller)
return false;
dsym = dsym->toParent ();
}
return true;
}
/* Entry point for call routines. Builds a function call to FD.
OBJECT is the 'this' reference passed and ARGS are the arguments to FD. */
tree
d_build_call_expr (FuncDeclaration *fd, tree object, Expressions *arguments)
{
return d_build_call (get_function_type (fd->type),
build_address (get_symbol_decl (fd)), object, arguments);
}
/* Builds a CALL_EXPR of type TF to CALLABLE. OBJECT holds the 'this' pointer,
ARGUMENTS are evaluated in left to right order, saved and promoted
before passing. */
tree
d_build_call (TypeFunction *tf, tree callable, tree object,
Expressions *arguments)
{
tree ctype = TREE_TYPE (callable);
tree callee = callable;
if (POINTER_TYPE_P (ctype))
ctype = TREE_TYPE (ctype);
else
callee = build_address (callable);
gcc_assert (FUNC_OR_METHOD_TYPE_P (ctype));
gcc_assert (tf != NULL);
gcc_assert (tf->ty == Tfunction);
if (TREE_CODE (ctype) != FUNCTION_TYPE && object == NULL_TREE)
{
/* Front-end apparently doesn't check this. */
if (TREE_CODE (callable) == FUNCTION_DECL)
{
error ("need %<this%> to access member %qE", DECL_NAME (callable));
return error_mark_node;
}
/* Probably an internal error. */
gcc_unreachable ();
}
/* Build the argument list for the call. */
vec<tree, va_gc> *args = NULL;
tree saved_args = NULL_TREE;
/* If this is a delegate call or a nested function being called as
a delegate, the object should not be NULL. */
if (object != NULL_TREE)
vec_safe_push (args, object);
if (arguments)
{
/* First pass, evaluated expanded tuples in function arguments. */
for (size_t i = 0; i < arguments->dim; ++i)
{
Lagain:
Expression *arg = (*arguments)[i];
gcc_assert (arg->op != TOKtuple);
if (arg->op == TOKcomma)
{
CommaExp *ce = (CommaExp *) arg;
tree tce = build_expr (ce->e1);
saved_args = compound_expr (saved_args, tce);
(*arguments)[i] = ce->e2;
goto Lagain;
}
}
size_t nparams = Parameter::dim (tf->parameters);
/* if _arguments[] is the first argument. */
size_t varargs = (tf->linkage == LINKd && tf->varargs == 1);
/* Assumes arguments->dim <= formal_args->dim if (!tf->varargs). */
for (size_t i = 0; i < arguments->dim; ++i)
{
Expression *arg = (*arguments)[i];
tree targ = build_expr (arg);
if (i - varargs < nparams && i >= varargs)
{
/* Actual arguments for declared formal arguments. */
Parameter *parg = Parameter::getNth (tf->parameters, i - varargs);
targ = convert_for_argument (targ, parg);
}
/* Don't pass empty aggregates by value. */
if (empty_aggregate_p (TREE_TYPE (targ)) && !TREE_ADDRESSABLE (targ)
&& TREE_CODE (targ) != CONSTRUCTOR)
{
tree t = build_constructor (TREE_TYPE (targ), NULL);
targ = build2 (COMPOUND_EXPR, TREE_TYPE (t), targ, t);
}
vec_safe_push (args, targ);
}
}
/* Evaluate the callee before calling it. */
if (TREE_SIDE_EFFECTS (callee))
{
callee = d_save_expr (callee);
saved_args = compound_expr (callee, saved_args);
}
tree result = build_call_vec (TREE_TYPE (ctype), callee, args);
/* Enforce left to right evaluation. */
if (tf->linkage == LINKd)
CALL_EXPR_ARGS_ORDERED (result) = 1;
result = maybe_expand_intrinsic (result);
/* Return the value in a temporary slot so that it can be evaluated
multiple times by the caller. */
if (TREE_CODE (result) == CALL_EXPR
&& AGGREGATE_TYPE_P (TREE_TYPE (result))
&& TREE_ADDRESSABLE (TREE_TYPE (result)))
{
CALL_EXPR_RETURN_SLOT_OPT (result) = true;
result = force_target_expr (result);
}
return compound_expr (saved_args, result);
}
/* Builds a call to AssertError or AssertErrorMsg. */
tree
d_assert_call (const Loc& loc, libcall_fn libcall, tree msg)
{
tree file;
tree line = size_int (loc.linnum);
/* File location is passed as a D string. */
if (loc.filename)
{
unsigned len = strlen (loc.filename);
tree str = build_string (len, loc.filename);
TREE_TYPE (str) = make_array_type (Type::tchar, len);
file = d_array_value (build_ctype (Type::tchar->arrayOf ()),
size_int (len), build_address (str));
}
else
file = null_array_node;
if (msg != NULL)
return build_libcall (libcall, Type::tvoid, 3, msg, file, line);
else
return build_libcall (libcall, Type::tvoid, 2, file, line);
}
/* Build and return the correct call to fmod depending on TYPE.
ARG0 and ARG1 are the arguments pass to the function. */
tree
build_float_modulus (tree type, tree arg0, tree arg1)
{
tree fmodfn = NULL_TREE;
tree basetype = type;
if (COMPLEX_FLOAT_TYPE_P (basetype))
basetype = TREE_TYPE (basetype);
if (TYPE_MAIN_VARIANT (basetype) == double_type_node
|| TYPE_MAIN_VARIANT (basetype) == idouble_type_node)
fmodfn = builtin_decl_explicit (BUILT_IN_FMOD);
else if (TYPE_MAIN_VARIANT (basetype) == float_type_node
|| TYPE_MAIN_VARIANT (basetype) == ifloat_type_node)
fmodfn = builtin_decl_explicit (BUILT_IN_FMODF);
else if (TYPE_MAIN_VARIANT (basetype) == long_double_type_node
|| TYPE_MAIN_VARIANT (basetype) == ireal_type_node)
fmodfn = builtin_decl_explicit (BUILT_IN_FMODL);
if (!fmodfn)
{
error ("tried to perform floating-point modulo division on %qT", type);
return error_mark_node;
}
if (COMPLEX_FLOAT_TYPE_P (type))
{
tree re = build_call_expr (fmodfn, 2, real_part (arg0), arg1);
tree im = build_call_expr (fmodfn, 2, imaginary_part (arg0), arg1);
return complex_expr (type, re, im);
}
if (SCALAR_FLOAT_TYPE_P (type))
return build_call_expr (fmodfn, 2, arg0, arg1);
/* Should have caught this above. */
gcc_unreachable ();
}
/* Build a function type whose first argument is a pointer to BASETYPE,
which is to be used for the 'vthis' context parameter for TYPE.
The base type may be a record for member functions, or a void for
nested functions and delegates. */
tree
build_vthis_function (tree basetype, tree type)
{
gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
tree argtypes = tree_cons (NULL_TREE, build_pointer_type (basetype),
TYPE_ARG_TYPES (type));
tree fntype = build_function_type (TREE_TYPE (type), argtypes);
if (RECORD_OR_UNION_TYPE_P (basetype))
TYPE_METHOD_BASETYPE (fntype) = TYPE_MAIN_VARIANT (basetype);
else
gcc_assert (VOID_TYPE_P (basetype));
return fntype;
}
/* If SYM is a nested function, return the static chain to be
used when calling that function from the current function.
If SYM is a nested class or struct, return the static chain
to be used when creating an instance of the class from CFUN. */
tree
get_frame_for_symbol (Dsymbol *sym)
{
FuncDeclaration *thisfd
= d_function_chain ? d_function_chain->function : NULL;
FuncDeclaration *fd = sym->isFuncDeclaration ();
FuncDeclaration *fdparent = NULL;
FuncDeclaration *fdoverride = NULL;
if (fd != NULL)
{
/* Check that the nested function is properly defined. */
if (!fd->fbody)
{
/* Should instead error on line that references 'fd'. */
error_at (make_location_t (fd->loc), "nested function missing body");
return null_pointer_node;
}
fdparent = fd->toParent2 ()->isFuncDeclaration ();
/* Special case for __ensure and __require. */
if ((fd->ident == Identifier::idPool ("__ensure")
|| fd->ident == Identifier::idPool ("__require"))
&& fdparent != thisfd)
{
fdoverride = fdparent;
fdparent = thisfd;
}
}
else
{
/* It's a class (or struct). NewExp codegen has already determined its
outer scope is not another class, so it must be a function. */
while (sym && !sym->isFuncDeclaration ())
sym = sym->toParent2 ();
fdparent = (FuncDeclaration *) sym;
}
gcc_assert (fdparent != NULL);
if (thisfd != fdparent)
{
/* If no frame pointer for this function. */
if (!thisfd->vthis)
{
error_at (make_location_t (sym->loc),
"is a nested function and cannot be accessed from %qs",
thisfd->toChars ());
return null_pointer_node;
}
/* Make sure we can get the frame pointer to the outer function.
Go up each nesting level until we find the enclosing function. */
Dsymbol *dsym = thisfd;
while (fd != dsym)
{
/* Check if enclosing function is a function. */
FuncDeclaration *fd = dsym->isFuncDeclaration ();
if (fd != NULL)
{
if (fdparent == fd->toParent2 ())
break;
gcc_assert (fd->isNested () || fd->vthis);
dsym = dsym->toParent2 ();
continue;
}
/* Check if enclosed by an aggregate. That means the current
function must be a member function of that aggregate. */
AggregateDeclaration *ad = dsym->isAggregateDeclaration ();
if (ad == NULL)
goto Lnoframe;
if (ad->isClassDeclaration () && fdparent == ad->toParent2 ())
break;
if (ad->isStructDeclaration () && fdparent == ad->toParent2 ())
break;
if (!ad->isNested () || !ad->vthis)
{
Lnoframe:
error_at (make_location_t (thisfd->loc),
"cannot get frame pointer to %qs",
sym->toPrettyChars ());
return null_pointer_node;
}
dsym = dsym->toParent2 ();
}
}
tree ffo = get_frameinfo (fdparent);
if (FRAMEINFO_CREATES_FRAME (ffo) || FRAMEINFO_STATIC_CHAIN (ffo))
{
tree frame_ref = get_framedecl (thisfd, fdparent);
/* If 'thisfd' is a derived member function, then 'fdparent' is the
overridden member function in the base class. Even if there's a
closure environment, we should give the original stack data as the
nested function frame. */
if (fdoverride)
{
ClassDeclaration *cdo = fdoverride->isThis ()->isClassDeclaration ();
ClassDeclaration *cd = thisfd->isThis ()->isClassDeclaration ();
gcc_assert (cdo && cd);
int offset;
if (cdo->isBaseOf (cd, &offset) && offset != 0)
{
/* Generate a new frame to pass to the overriden function that
has the 'this' pointer adjusted. */
gcc_assert (offset != OFFSET_RUNTIME);
tree type = FRAMEINFO_TYPE (get_frameinfo (fdoverride));
tree fields = TYPE_FIELDS (type);
/* The 'this' field comes immediately after the '__chain'. */
tree thisfield = chain_index (1, fields);
vec<constructor_elt, va_gc> *ve = NULL;
tree framefields = TYPE_FIELDS (FRAMEINFO_TYPE (ffo));
frame_ref = build_deref (frame_ref);
for (tree field = fields; field; field = DECL_CHAIN (field))
{
tree value = component_ref (frame_ref, framefields);
if (field == thisfield)
value = build_offset (value, size_int (offset));
CONSTRUCTOR_APPEND_ELT (ve, field, value);
framefields = DECL_CHAIN (framefields);
}
frame_ref = build_address (build_constructor (type, ve));
}
}
return frame_ref;
}
return null_pointer_node;
}
/* Return the parent function of a nested class CD. */
static FuncDeclaration *
d_nested_class (ClassDeclaration *cd)
{
FuncDeclaration *fd = NULL;
while (cd && cd->isNested ())
{
Dsymbol *dsym = cd->toParent2 ();
if ((fd = dsym->isFuncDeclaration ()))
return fd;
else
cd = dsym->isClassDeclaration ();
}
return NULL;
}
/* Return the parent function of a nested struct SD. */
static FuncDeclaration *
d_nested_struct (StructDeclaration *sd)
{
FuncDeclaration *fd = NULL;
while (sd && sd->isNested ())
{
Dsymbol *dsym = sd->toParent2 ();
if ((fd = dsym->isFuncDeclaration ()))
return fd;
else
sd = dsym->isStructDeclaration ();
}
return NULL;
}
/* Starting from the current function FD, try to find a suitable value of
'this' in nested function instances. A suitable 'this' value is an
instance of OCD or a class that has OCD as a base. */
static tree
find_this_tree (ClassDeclaration *ocd)
{
FuncDeclaration *fd = d_function_chain ? d_function_chain->function : NULL;
while (fd)
{
AggregateDeclaration *ad = fd->isThis ();
ClassDeclaration *cd = ad ? ad->isClassDeclaration () : NULL;
if (cd != NULL)
{
if (ocd == cd)
return get_decl_tree (fd->vthis);
else if (ocd->isBaseOf (cd, NULL))
return convert_expr (get_decl_tree (fd->vthis),
cd->type, ocd->type);
fd = d_nested_class (cd);
}
else
{
if (fd->isNested ())
{
fd = fd->toParent2 ()->isFuncDeclaration ();
continue;
}
fd = NULL;
}
}
return NULL_TREE;
}
/* Retrieve the outer class/struct 'this' value of DECL from
the current function. */
tree
build_vthis (AggregateDeclaration *decl)
{
ClassDeclaration *cd = decl->isClassDeclaration ();
StructDeclaration *sd = decl->isStructDeclaration ();
/* If an aggregate nested in a function has no methods and there are no
other nested functions, any static chain created here will never be
translated. Use a null pointer for the link in this case. */
tree vthis_value = null_pointer_node;
if (cd != NULL || sd != NULL)
{
Dsymbol *outer = decl->toParent2 ();
/* If the parent is a templated struct, the outer context is instead
the enclosing symbol of where the instantiation happened. */
if (outer->isStructDeclaration ())
{
gcc_assert (outer->parent && outer->parent->isTemplateInstance ());
outer = ((TemplateInstance *) outer->parent)->enclosing;
}
/* For outer classes, get a suitable 'this' value.
For outer functions, get a suitable frame/closure pointer. */
ClassDeclaration *cdo = outer->isClassDeclaration ();
FuncDeclaration *fdo = outer->isFuncDeclaration ();
if (cdo)
{
vthis_value = find_this_tree (cdo);
gcc_assert (vthis_value != NULL_TREE);
}
else if (fdo)
{
tree ffo = get_frameinfo (fdo);
if (FRAMEINFO_CREATES_FRAME (ffo) || FRAMEINFO_STATIC_CHAIN (ffo)
|| fdo->hasNestedFrameRefs ())
vthis_value = get_frame_for_symbol (decl);
else if (cd != NULL)
{
/* Classes nested in methods are allowed to access any outer
class fields, use the function chain in this case. */
if (fdo->vthis && fdo->vthis->type != Type::tvoidptr)
vthis_value = get_decl_tree (fdo->vthis);
}
}
else
gcc_unreachable ();
}
return vthis_value;
}
/* Build the RECORD_TYPE that describes the function frame or closure type for
the function FD. FFI is the tree holding all frame information. */
static tree
build_frame_type (tree ffi, FuncDeclaration *fd)
{
if (FRAMEINFO_TYPE (ffi))
return FRAMEINFO_TYPE (ffi);
tree frame_rec_type = make_node (RECORD_TYPE);
char *name = concat (FRAMEINFO_IS_CLOSURE (ffi) ? "CLOSURE." : "FRAME.",
fd->toPrettyChars (), NULL);
TYPE_NAME (frame_rec_type) = get_identifier (name);
free (name);
tree fields = NULL_TREE;
/* Function is a member or nested, so must have field for outer context. */
if (fd->vthis)
{
tree ptr_field = build_decl (BUILTINS_LOCATION, FIELD_DECL,
get_identifier ("__chain"), ptr_type_node);
DECL_FIELD_CONTEXT (ptr_field) = frame_rec_type;
fields = chainon (NULL_TREE, ptr_field);
DECL_NONADDRESSABLE_P (ptr_field) = 1;
}
/* The __ensure and __require are called directly, so never make the outer
functions closure, but nevertheless could still be referencing parameters
of the calling function non-locally. So we add all parameters with nested
refs to the function frame, this should also mean overriding methods will
have the same frame layout when inheriting a contract. */
if ((global.params.useIn && fd->frequire)
|| (global.params.useOut && fd->fensure))
{
if (fd->parameters)
{
for (size_t i = 0; fd->parameters && i < fd->parameters->dim; i++)
{
VarDeclaration *v = (*fd->parameters)[i];
/* Remove if already in closureVars so can push to front. */
for (size_t j = i; j < fd->closureVars.dim; j++)
{
Dsymbol *s = fd->closureVars[j];
if (s == v)
{
fd->closureVars.remove (j);
break;
}
}
fd->closureVars.insert (i, v);
}
}
/* Also add hidden 'this' to outer context. */
if (fd->vthis)
{
for (size_t i = 0; i < fd->closureVars.dim; i++)
{
Dsymbol *s = fd->closureVars[i];
if (s == fd->vthis)
{
fd->closureVars.remove (i);
break;
}
}
fd->closureVars.insert (0, fd->vthis);
}
}
for (size_t i = 0; i < fd->closureVars.dim; i++)
{
VarDeclaration *v = fd->closureVars[i];
tree vsym = get_symbol_decl (v);
tree ident = v->ident
? get_identifier (v->ident->toChars ()) : NULL_TREE;
tree field = build_decl (make_location_t (v->loc), FIELD_DECL, ident,
TREE_TYPE (vsym));
SET_DECL_LANG_FRAME_FIELD (vsym, field);
DECL_FIELD_CONTEXT (field) = frame_rec_type;
fields = chainon (fields, field);
TREE_USED (vsym) = 1;
TREE_ADDRESSABLE (field) = TREE_ADDRESSABLE (vsym);
DECL_NONADDRESSABLE_P (field) = !TREE_ADDRESSABLE (vsym);
TREE_THIS_VOLATILE (field) = TREE_THIS_VOLATILE (vsym);
/* Can't do nrvo if the variable is put in a frame. */
if (fd->nrvo_can && fd->nrvo_var == v)
fd->nrvo_can = 0;
if (FRAMEINFO_IS_CLOSURE (ffi))
{
/* Because the value needs to survive the end of the scope. */
if ((v->edtor && (v->storage_class & STCparameter))
|| v->needsScopeDtor ())
error_at (make_location_t (v->loc),
"has scoped destruction, cannot build closure");
}
}
TYPE_FIELDS (frame_rec_type) = fields;
TYPE_READONLY (frame_rec_type) = 1;
layout_type (frame_rec_type);
d_keep (frame_rec_type);
return frame_rec_type;
}
/* Closures are implemented by taking the local variables that
need to survive the scope of the function, and copying them
into a GC allocated chuck of memory. That chunk, called the
closure here, is inserted into the linked list of stack
frames instead of the usual stack frame.
If a closure is not required, but FD still needs a frame to lower
nested refs, then instead build custom static chain decl on stack. */
void
build_closure (FuncDeclaration *fd)
{
tree ffi = get_frameinfo (fd);
if (!FRAMEINFO_CREATES_FRAME (ffi))
return;
tree type = FRAMEINFO_TYPE (ffi);
gcc_assert (COMPLETE_TYPE_P (type));
tree decl, decl_ref;
if (FRAMEINFO_IS_CLOSURE (ffi))
{
decl = build_local_temp (build_pointer_type (type));
DECL_NAME (decl) = get_identifier ("__closptr");
decl_ref = build_deref (decl);
/* Allocate memory for closure. */
tree arg = convert (build_ctype (Type::tsize_t), TYPE_SIZE_UNIT (type));
tree init = build_libcall (LIBCALL_ALLOCMEMORY, Type::tvoidptr, 1, arg);
tree init_exp = build_assign (INIT_EXPR, decl,
build_nop (TREE_TYPE (decl), init));
add_stmt (init_exp);
}
else
{
decl = build_local_temp (type);
DECL_NAME (decl) = get_identifier ("__frame");
decl_ref = decl;
}
/* Set the first entry to the parent closure/frame, if any. */
if (fd->vthis)
{
tree chain_field = component_ref (decl_ref, TYPE_FIELDS (type));
tree chain_expr = modify_expr (chain_field,
d_function_chain->static_chain);
add_stmt (chain_expr);
}
/* Copy parameters that are referenced nonlocally. */
for (size_t i = 0; i < fd->closureVars.dim; i++)
{
VarDeclaration *v = fd->closureVars[i];
if (!v->isParameter ())
continue;
tree vsym = get_symbol_decl (v);
tree field = component_ref (decl_ref, DECL_LANG_FRAME_FIELD (vsym));
tree expr = modify_expr (field, vsym);
add_stmt (expr);
}
if (!FRAMEINFO_IS_CLOSURE (ffi))
decl = build_address (decl);
d_function_chain->static_chain = decl;
}
/* Return the frame of FD. This could be a static chain or a closure
passed via the hidden 'this' pointer. */
tree
get_frameinfo (FuncDeclaration *fd)
{
tree fds = get_symbol_decl (fd);
if (DECL_LANG_FRAMEINFO (fds))
return DECL_LANG_FRAMEINFO (fds);
tree ffi = make_node (FUNCFRAME_INFO);
DECL_LANG_FRAMEINFO (fds) = ffi;
if (fd->needsClosure ())
{
/* Set-up a closure frame, this will be allocated on the heap. */
FRAMEINFO_CREATES_FRAME (ffi) = 1;
FRAMEINFO_IS_CLOSURE (ffi) = 1;
}
else if (fd->hasNestedFrameRefs ())
{
/* Functions with nested refs must create a static frame for local
variables to be referenced from. */
FRAMEINFO_CREATES_FRAME (ffi) = 1;
}
else
{
/* For nested functions, default to creating a frame. Even if there are
no fields to populate the frame, create it anyway, as this will be
used as the record type instead of `void*` for the this parameter. */
if (fd->vthis && fd->vthis->type == Type::tvoidptr)
FRAMEINFO_CREATES_FRAME (ffi) = 1;
/* In checkNestedReference, references from contracts are not added to the
closureVars array, so assume all parameters referenced. */
if ((global.params.useIn && fd->frequire)
|| (global.params.useOut && fd->fensure))
FRAMEINFO_CREATES_FRAME (ffi) = 1;
/* If however `fd` is nested (deeply) in a function that creates a
closure, then `fd` instead inherits that closure via hidden vthis
pointer, and doesn't create a stack frame at all. */
FuncDeclaration *ff = fd;
while (ff)
{
tree ffo = get_frameinfo (ff);
if (ff != fd && FRAMEINFO_CREATES_FRAME (ffo))
{
gcc_assert (FRAMEINFO_TYPE (ffo));
FRAMEINFO_CREATES_FRAME (ffi) = 0;
FRAMEINFO_STATIC_CHAIN (ffi) = 1;
FRAMEINFO_IS_CLOSURE (ffi) = FRAMEINFO_IS_CLOSURE (ffo);
gcc_assert (COMPLETE_TYPE_P (FRAMEINFO_TYPE (ffo)));
FRAMEINFO_TYPE (ffi) = FRAMEINFO_TYPE (ffo);
break;
}
/* Stop looking if no frame pointer for this function. */
if (ff->vthis == NULL)
break;
AggregateDeclaration *ad = ff->isThis ();
if (ad && ad->isNested ())
{
while (ad->isNested ())
{
Dsymbol *d = ad->toParent2 ();
ad = d->isAggregateDeclaration ();
ff = d->isFuncDeclaration ();
if (ad == NULL)
break;
}
}
else
ff = ff->toParent2 ()->isFuncDeclaration ();
}
}
/* Build type now as may be referenced from another module. */
if (FRAMEINFO_CREATES_FRAME (ffi))
FRAMEINFO_TYPE (ffi) = build_frame_type (ffi, fd);
return ffi;
}
/* Return a pointer to the frame/closure block of OUTER
so can be accessed from the function INNER. */
tree
get_framedecl (FuncDeclaration *inner, FuncDeclaration *outer)
{
tree result = d_function_chain->static_chain;
FuncDeclaration *fd = inner;
while (fd && fd != outer)
{
AggregateDeclaration *ad;
ClassDeclaration *cd;
StructDeclaration *sd;
/* Parent frame link is the first field. */
if (FRAMEINFO_CREATES_FRAME (get_frameinfo (fd)))
result = indirect_ref (ptr_type_node, result);
if (fd->isNested ())
fd = fd->toParent2 ()->isFuncDeclaration ();
/* The frame/closure record always points to the outer function's
frame, even if there are intervening nested classes or structs.
So, we can just skip over these. */
else if ((ad = fd->isThis ()) && (cd = ad->isClassDeclaration ()))
fd = d_nested_class (cd);
else if ((ad = fd->isThis ()) && (sd = ad->isStructDeclaration ()))
fd = d_nested_struct (sd);
else
break;
}
/* Go get our frame record. */
gcc_assert (fd == outer);
tree frame_type = FRAMEINFO_TYPE (get_frameinfo (outer));
if (frame_type != NULL_TREE)
{
result = build_nop (build_pointer_type (frame_type), result);
return result;
}
else
{
error_at (make_location_t (inner->loc),
"forward reference to frame of %qs", outer->toChars ());
return null_pointer_node;
}
}
|