1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
|
/* expr.cc -- Lower D frontend expressions to GCC trees.
Copyright (C) 2015-2021 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/expression.h"
#include "dmd/identifier.h"
#include "dmd/init.h"
#include "dmd/module.h"
#include "dmd/mtype.h"
#include "dmd/template.h"
#include "tree.h"
#include "fold-const.h"
#include "diagnostic.h"
#include "langhooks.h"
#include "tm.h"
#include "function.h"
#include "toplev.h"
#include "varasm.h"
#include "predict.h"
#include "stor-layout.h"
#include "d-tree.h"
/* Determine if type T is a struct that has a postblit. */
static bool
needs_postblit (Type *t)
{
t = t->baseElemOf ();
if (TypeStruct *ts = t->isTypeStruct ())
{
if (ts->sym->postblit)
return true;
}
return false;
}
/* Determine if type T is a struct that has a destructor. */
static bool
needs_dtor (Type *t)
{
t = t->baseElemOf ();
if (TypeStruct *ts = t->isTypeStruct ())
{
if (ts->sym->dtor)
return true;
}
return false;
}
/* Determine if expression E is a suitable lvalue. */
static bool
lvalue_p (Expression *e)
{
SliceExp *se = e->isSliceExp ();
if (se != NULL && se->e1->isLvalue ())
return true;
CastExp *ce = e->isCastExp ();
if (ce != NULL && ce->e1->isLvalue ())
return true;
return (e->op != TOKslice && e->isLvalue ());
}
/* Build an expression of code CODE, data type TYPE, and operands ARG0 and
ARG1. Perform relevant conversions needed for correct code operations. */
static tree
binary_op (tree_code code, tree type, tree arg0, tree arg1)
{
tree t0 = TREE_TYPE (arg0);
tree t1 = TREE_TYPE (arg1);
tree ret = NULL_TREE;
bool unsignedp = TYPE_UNSIGNED (t0) || TYPE_UNSIGNED (t1);
/* Deal with float mod expressions immediately. */
if (code == FLOAT_MOD_EXPR)
return build_float_modulus (type, arg0, arg1);
if (POINTER_TYPE_P (t0) && INTEGRAL_TYPE_P (t1))
return build_nop (type, build_offset_op (code, arg0, arg1));
if (INTEGRAL_TYPE_P (t0) && POINTER_TYPE_P (t1))
return build_nop (type, build_offset_op (code, arg1, arg0));
if (POINTER_TYPE_P (t0) && POINTER_TYPE_P (t1))
{
gcc_assert (code == MINUS_EXPR);
tree ptrtype = lang_hooks.types.type_for_mode (ptr_mode, 0);
/* POINTER_DIFF_EXPR requires a signed integer type of the same size as
pointers. If some platform cannot provide that, or has a larger
ptrdiff_type to support differences larger than half the address
space, cast the pointers to some larger integer type and do the
computations in that type. */
if (TYPE_PRECISION (ptrtype) > TYPE_PRECISION (t0))
ret = fold_build2 (MINUS_EXPR, ptrtype,
d_convert (ptrtype, arg0),
d_convert (ptrtype, arg1));
else
ret = fold_build2 (POINTER_DIFF_EXPR, ptrtype, arg0, arg1);
}
else if (INTEGRAL_TYPE_P (type) && (TYPE_UNSIGNED (type) != unsignedp))
{
tree inttype = (unsignedp)
? d_unsigned_type (type) : d_signed_type (type);
ret = fold_build2 (code, inttype, arg0, arg1);
}
else
{
/* If the operation needs excess precision. */
tree eptype = excess_precision_type (type);
if (eptype != NULL_TREE)
{
arg0 = d_convert (eptype, arg0);
arg1 = d_convert (eptype, arg1);
}
else
{
/* Front-end does not do this conversion and GCC does not
always do it right. */
if (COMPLEX_FLOAT_TYPE_P (t0) && !COMPLEX_FLOAT_TYPE_P (t1))
arg1 = d_convert (t0, arg1);
else if (COMPLEX_FLOAT_TYPE_P (t1) && !COMPLEX_FLOAT_TYPE_P (t0))
arg0 = d_convert (t1, arg0);
eptype = type;
}
ret = build2 (code, eptype, arg0, arg1);
}
return d_convert (type, ret);
}
/* Build a binary expression of code CODE, assigning the result into E1. */
static tree
binop_assignment (tree_code code, Expression *e1, Expression *e2)
{
/* Skip casts for lhs assignment. */
Expression *e1b = e1;
while (e1b->op == TOKcast)
{
CastExp *ce = e1b->isCastExp ();
gcc_assert (same_type_p (ce->type, ce->to));
e1b = ce->e1;
}
/* Stabilize LHS for assignment. */
tree lhs = build_expr (e1b);
tree lexpr = stabilize_expr (&lhs);
/* The LHS expression could be an assignment, to which its operation gets
lost during gimplification. */
if (TREE_CODE (lhs) == MODIFY_EXPR)
{
/* If LHS has side effects, call stabilize_reference on it, so it can
be evaluated multiple times. */
if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
lhs = build_assign (MODIFY_EXPR,
stabilize_reference (TREE_OPERAND (lhs, 0)),
TREE_OPERAND (lhs, 1));
lexpr = compound_expr (lexpr, lhs);
lhs = TREE_OPERAND (lhs, 0);
}
lhs = stabilize_reference (lhs);
/* Save RHS, to ensure that the expression is evaluated before LHS. */
tree rhs = build_expr (e2);
tree rexpr = d_save_expr (rhs);
rhs = binary_op (code, build_ctype (e1->type),
convert_expr (lhs, e1b->type, e1->type), rexpr);
if (TREE_SIDE_EFFECTS (rhs))
rhs = compound_expr (rexpr, rhs);
tree expr = modify_expr (lhs, convert_expr (rhs, e1->type, e1b->type));
return compound_expr (lexpr, expr);
}
/* Implements the visitor interface to build the GCC trees of all Expression
AST classes emitted from the D Front-end.
All visit methods accept one parameter E, which holds the frontend AST
of the expression to compile. They also don't return any value, instead
generated code is cached in RESULT_ and returned from the caller. */
class ExprVisitor : public Visitor
{
using Visitor::visit;
tree result_;
bool constp_;
bool literalp_;
public:
ExprVisitor (bool constp, bool literalp)
{
this->result_ = NULL_TREE;
this->constp_ = constp;
this->literalp_ = literalp;
}
tree result (void)
{
return this->result_;
}
/* Visitor interfaces, each Expression class should have
overridden the default. */
void visit (Expression *)
{
gcc_unreachable ();
}
/* Build a conditional expression. If either the second or third
expression is void, then the resulting type is void. Otherwise
they are implicitly converted to a common type. */
void visit (CondExp *e)
{
tree cond = convert_for_condition (build_expr (e->econd),
e->econd->type);
tree t1 = build_expr (e->e1);
tree t2 = build_expr (e->e2);
if (e->type->ty != Tvoid)
{
t1 = convert_expr (t1, e->e1->type, e->type);
t2 = convert_expr (t2, e->e2->type, e->type);
}
this->result_ = build_condition (build_ctype (e->type), cond, t1, t2);
}
/* Build an identity comparison expression. Operands go through the
usual conversions to bring them to a common type before comparison.
The result type is bool. */
void visit (IdentityExp *e)
{
tree_code code = (e->op == TOKidentity) ? EQ_EXPR : NE_EXPR;
Type *tb1 = e->e1->type->toBasetype ();
Type *tb2 = e->e2->type->toBasetype ();
if ((tb1->ty == Tsarray || tb1->ty == Tarray)
&& (tb2->ty == Tsarray || tb2->ty == Tarray))
{
/* For static and dynamic arrays, identity is defined as referring to
the same array elements and the same number of elements. */
tree t1 = d_array_convert (e->e1);
tree t2 = d_array_convert (e->e2);
this->result_ = d_convert (build_ctype (e->type),
build_boolop (code, t1, t2));
}
else if (tb1->isfloating () && tb1->ty != Tvector)
{
/* For floating-point values, identity is defined as the bits in the
operands being identical. */
tree t1 = d_save_expr (build_expr (e->e1));
tree t2 = d_save_expr (build_expr (e->e2));
if (!tb1->iscomplex ())
this->result_ = build_float_identity (code, t1, t2);
else
{
/* Compare the real and imaginary parts separately. */
tree req = build_float_identity (code, real_part (t1),
real_part (t2));
tree ieq = build_float_identity (code, imaginary_part (t1),
imaginary_part (t2));
if (code == EQ_EXPR)
this->result_ = build_boolop (TRUTH_ANDIF_EXPR, req, ieq);
else
this->result_ = build_boolop (TRUTH_ORIF_EXPR, req, ieq);
}
}
else if (TypeStruct *ts = tb1->isTypeStruct ())
{
/* For struct objects, identity is defined as bits in operands being
identical also. Alignment holes in structs are ignored. */
tree t1 = build_expr (e->e1);
tree t2 = build_expr (e->e2);
gcc_assert (same_type_p (tb1, tb2));
this->result_ = build_struct_comparison (code, ts->sym, t1, t2);
}
else
{
/* For operands of other types, identity is defined as being the
same as equality expressions. */
tree t1 = build_expr (e->e1);
tree t2 = build_expr (e->e2);
this->result_ = d_convert (build_ctype (e->type),
build_boolop (code, t1, t2));
}
}
/* Build an equality expression, which compare the two operands for either
equality or inequality. Operands go through the usual conversions to bring
them to a common type before comparison. The result type is bool. */
void visit (EqualExp *e)
{
Type *tb1 = e->e1->type->toBasetype ();
Type *tb2 = e->e2->type->toBasetype ();
tree_code code = (e->op == TOKequal) ? EQ_EXPR : NE_EXPR;
if ((tb1->ty == Tsarray || tb1->ty == Tarray)
&& (tb2->ty == Tsarray || tb2->ty == Tarray))
{
/* For static and dynamic arrays, equality is defined as the lengths of
the arrays matching, and all the elements are equal. */
Type *t1elem = tb1->nextOf ()->toBasetype ();
Type *t2elem = tb1->nextOf ()->toBasetype ();
/* Check if comparisons of arrays can be optimized using memcmp.
This will inline EQ expressions as:
e1.length == e2.length && memcmp(e1.ptr, e2.ptr, size) == 0;
Or when generating a NE expression:
e1.length != e2.length || memcmp(e1.ptr, e2.ptr, size) != 0; */
if ((t1elem->isintegral () || t1elem->ty == Tvoid
|| (t1elem->ty == Tstruct && !t1elem->isTypeStruct ()->sym->xeq))
&& t1elem->ty == t2elem->ty)
{
tree t1 = d_array_convert (e->e1);
tree t2 = d_array_convert (e->e2);
tree result;
/* Make temporaries to prevent multiple evaluations. */
tree t1saved = d_save_expr (t1);
tree t2saved = d_save_expr (t2);
/* Length of arrays, for comparisons done before calling memcmp. */
tree t1len = d_array_length (t1saved);
tree t2len = d_array_length (t2saved);
/* Reference to array data. */
tree t1ptr = d_array_ptr (t1saved);
tree t2ptr = d_array_ptr (t2saved);
/* Compare arrays using memcmp if possible, otherwise for structs,
each field is compared inline. */
if (t1elem->ty != Tstruct
|| identity_compare_p (t1elem->isTypeStruct ()->sym))
{
tree size = size_mult_expr (t1len, size_int (t1elem->size ()));
result = build_memcmp_call (t1ptr, t2ptr, size);
result = build_boolop (code, result, integer_zero_node);
}
else
{
StructDeclaration *sd = t1elem->isTypeStruct ()->sym;
result = build_array_struct_comparison (code, sd, t1len,
t1ptr, t2ptr);
}
/* Check array length first before passing to memcmp.
For equality expressions, this becomes:
(e1.length == 0 || memcmp);
Otherwise for inequality:
(e1.length != 0 && memcmp); */
tree tsizecmp = build_boolop (code, t1len, size_zero_node);
if (e->op == TOKequal)
result = build_boolop (TRUTH_ORIF_EXPR, tsizecmp, result);
else
result = build_boolop (TRUTH_ANDIF_EXPR, tsizecmp, result);
/* Finally, check if lengths of both arrays match if dynamic.
The frontend should have already guaranteed that static arrays
have same size. */
if (tb1->ty == Tsarray && tb2->ty == Tsarray)
gcc_assert (tb1->size () == tb2->size ());
else
{
tree tlencmp = build_boolop (code, t1len, t2len);
if (e->op == TOKequal)
result = build_boolop (TRUTH_ANDIF_EXPR, tlencmp, result);
else
result = build_boolop (TRUTH_ORIF_EXPR, tlencmp, result);
}
/* Ensure left-to-right order of evaluation. */
if (TREE_SIDE_EFFECTS (t2))
result = compound_expr (t2saved, result);
if (TREE_SIDE_EFFECTS (t1))
result = compound_expr (t1saved, result);
this->result_ = result;
}
else
{
/* Use _adEq2() to compare each element. */
Type *t1array = t1elem->arrayOf ();
tree result = build_libcall (LIBCALL_ADEQ2, e->type, 3,
d_array_convert (e->e1),
d_array_convert (e->e2),
build_typeinfo (e->loc, t1array));
if (e->op == TOKnotequal)
result = build1 (TRUTH_NOT_EXPR, build_ctype (e->type), result);
this->result_ = result;
}
}
else if (TypeStruct *ts = tb1->isTypeStruct ())
{
/* Equality for struct objects means the logical product of all
equality results of the corresponding object fields. */
tree t1 = build_expr (e->e1);
tree t2 = build_expr (e->e2);
gcc_assert (same_type_p (tb1, tb2));
this->result_ = build_struct_comparison (code, ts->sym, t1, t2);
}
else if (tb1->ty == Taarray && tb2->ty == Taarray)
{
/* Use _aaEqual() for associative arrays. */
tree result = build_libcall (LIBCALL_AAEQUAL, e->type, 3,
build_typeinfo (e->loc, tb1),
build_expr (e->e1),
build_expr (e->e2));
if (e->op == TOKnotequal)
result = build1 (TRUTH_NOT_EXPR, build_ctype (e->type), result);
this->result_ = result;
}
else
{
/* For operands of other types, equality is defined as the bit pattern
of the type matches exactly. */
tree t1 = build_expr (e->e1);
tree t2 = build_expr (e->e2);
this->result_ = d_convert (build_ctype (e->type),
build_boolop (code, t1, t2));
}
}
/* Build an `in' expression. This is a condition to see if an element
exists in an associative array. The result is a pointer to the
element, or null if false. */
void visit (InExp *e)
{
Type *tb2 = e->e2->type->toBasetype ();
Type *tkey = tb2->isTypeAArray ()->index->toBasetype ();
tree key = convert_expr (build_expr (e->e1), e->e1->type, tkey);
/* Build a call to _aaInX(). */
this->result_ = build_libcall (LIBCALL_AAINX, e->type, 3,
build_expr (e->e2),
build_typeinfo (e->loc, tkey),
build_address (key));
}
/* Build a relational expression. The result type is bool. */
void visit (CmpExp *e)
{
Type *tb1 = e->e1->type->toBasetype ();
Type *tb2 = e->e2->type->toBasetype ();
tree result;
tree_code code;
switch (e->op)
{
case TOKle:
code = LE_EXPR;
break;
case TOKlt:
code = LT_EXPR;
break;
case TOKge:
code = GE_EXPR;
break;
case TOKgt:
code = GT_EXPR;
break;
default:
gcc_unreachable ();
}
if ((tb1->ty == Tsarray || tb1->ty == Tarray)
&& (tb2->ty == Tsarray || tb2->ty == Tarray))
{
/* For static and dynamic arrays, the result of the relational op is
the result of the operator applied to the first non-equal element
of the array. If two arrays compare equal, but are of different
lengths, the shorter array compares as less than the longer. */
Type *telem = tb1->nextOf ()->toBasetype ();
tree call = build_libcall (LIBCALL_ADCMP2, Type::tint32, 3,
d_array_convert (e->e1),
d_array_convert (e->e2),
build_typeinfo (e->loc, telem->arrayOf ()));
result = build_boolop (code, call, integer_zero_node);
this->result_ = d_convert (build_ctype (e->type), result);
return;
}
/* Simple comparison. */
result = build_boolop (code, build_expr (e->e1), build_expr (e->e2));
this->result_ = d_convert (build_ctype (e->type), result);
}
/* Build a logical `and if' or `or if' expression. If the right operand
expression is void, then the resulting type is void. Otherwise the
result is bool. */
void visit (LogicalExp *e)
{
tree_code code = (e->op == TOKandand) ? TRUTH_ANDIF_EXPR : TRUTH_ORIF_EXPR;
if (e->e2->type->toBasetype ()->ty != Tvoid)
{
tree t1 = build_expr (e->e1);
tree t2 = build_expr (e->e2);
t1 = convert_for_condition (t1, e->e1->type);
t2 = convert_for_condition (t2, e->e2->type);
this->result_ = d_convert (build_ctype (e->type),
build_boolop (code, t1, t2));
}
else
{
tree t1 = convert_for_condition (build_expr (e->e1), e->e1->type);
tree t2 = build_expr_dtor (e->e2);
/* Invert condition for logical or if expression. */
if (e->op == TOKoror)
t1 = build1 (TRUTH_NOT_EXPR, d_bool_type, t1);
this->result_ = build_condition (build_ctype (e->type),
t1, t2, void_node);
}
}
/* Build a binary operand expression. Operands go through usual arithmetic
conversions to bring them to a common type before evaluating. */
void visit (BinExp *e)
{
tree_code code;
switch (e->op)
{
case TOKadd:
case TOKmin:
if ((e->e1->type->isreal () && e->e2->type->isimaginary ())
|| (e->e1->type->isimaginary () && e->e2->type->isreal ()))
{
/* If the result is complex, then we can shortcut binary_op.
Frontend should have already validated types and sizes. */
tree t1 = build_expr (e->e1);
tree t2 = build_expr (e->e2);
if (e->op == TOKmin)
t2 = build1 (NEGATE_EXPR, TREE_TYPE (t2), t2);
if (e->e1->type->isreal ())
this->result_ = complex_expr (build_ctype (e->type), t1, t2);
else
this->result_ = complex_expr (build_ctype (e->type), t2, t1);
return;
}
else
code = (e->op == TOKadd)
? PLUS_EXPR : MINUS_EXPR;
break;
case TOKmul:
code = MULT_EXPR;
break;
case TOKdiv:
/* Determine if the div expression is a lowered pointer diff operation.
The front-end rewrites `(p1 - p2)' into `(p1 - p2) / stride'. */
if (MinExp *me = e->e1->isMinExp ())
{
if (me->e1->type->ty == Tpointer && me->e2->type->ty == Tpointer
&& e->e2->op == TOKint64)
{
code = EXACT_DIV_EXPR;
break;
}
}
code = e->e1->type->isintegral ()
? TRUNC_DIV_EXPR : RDIV_EXPR;
break;
case TOKmod:
code = e->e1->type->isfloating ()
? FLOAT_MOD_EXPR : TRUNC_MOD_EXPR;
break;
case TOKand:
code = BIT_AND_EXPR;
break;
case TOKor:
code = BIT_IOR_EXPR;
break;
case TOKxor:
code = BIT_XOR_EXPR;
break;
case TOKshl:
code = LSHIFT_EXPR;
break;
case TOKshr:
code = RSHIFT_EXPR;
break;
case TOKushr:
code = UNSIGNED_RSHIFT_EXPR;
break;
default:
gcc_unreachable ();
}
this->result_ = binary_op (code, build_ctype (e->type),
build_expr (e->e1), build_expr (e->e2));
}
/* Build a concat expression, which concatenates two or more arrays of the
same type, producing a dynamic array with the result. If one operand
is an element type, that element is converted to an array of length 1. */
void visit (CatExp *e)
{
Type *tb1 = e->e1->type->toBasetype ();
Type *tb2 = e->e2->type->toBasetype ();
Type *etype;
if (tb1->ty == Tarray || tb1->ty == Tsarray)
etype = tb1->nextOf ();
else
etype = tb2->nextOf ();
tree result;
if (e->e1->op == TOKcat)
{
/* Flatten multiple concatenations to an array.
So the expression ((a ~ b) ~ c) becomes [a, b, c] */
int ndims = 2;
for (Expression *ex = e->e1; ex->op == TOKcat;)
{
if (ex->op == TOKcat)
{
ex = ex->isCatExp ()->e1;
ndims++;
}
}
/* Store all concatenation args to a temporary byte[][ndims] array. */
Type *targselem = Type::tint8->arrayOf ();
tree var = build_local_temp (make_array_type (targselem, ndims));
/* Loop through each concatenation from right to left. */
vec <constructor_elt, va_gc> *elms = NULL;
CatExp *ce = e;
int dim = ndims - 1;
for (Expression *oe = ce->e2; oe != NULL;
(ce->e1->op != TOKcat
? (oe = ce->e1)
: (ce = ce->e1->isCatExp (), oe = ce->e2)))
{
tree arg = d_array_convert (etype, oe);
tree index = size_int (dim);
CONSTRUCTOR_APPEND_ELT (elms, index, d_save_expr (arg));
/* Finished pushing all arrays. */
if (oe == ce->e1)
break;
dim -= 1;
}
/* Check there is no logic bug in constructing byte[][] of arrays. */
gcc_assert (dim == 0);
tree init = build_constructor (TREE_TYPE (var), elms);
var = compound_expr (modify_expr (var, init), var);
tree arrs = d_array_value (build_ctype (targselem->arrayOf ()),
size_int (ndims), build_address (var));
result = build_libcall (LIBCALL_ARRAYCATNTX, e->type, 2,
build_typeinfo (e->loc, e->type), arrs);
}
else
{
/* Handle single concatenation (a ~ b). */
result = build_libcall (LIBCALL_ARRAYCATT, e->type, 3,
build_typeinfo (e->loc, e->type),
d_array_convert (etype, e->e1),
d_array_convert (etype, e->e2));
}
this->result_ = result;
}
/* Build an assignment operator expression. The right operand is implicitly
converted to the type of the left operand, and assigned to it. */
void visit (BinAssignExp *e)
{
tree_code code;
Expression *e1b = e->e1;
switch (e->op)
{
case TOKaddass:
code = PLUS_EXPR;
break;
case TOKminass:
code = MINUS_EXPR;
break;
case TOKmulass:
code = MULT_EXPR;
break;
case TOKdivass:
code = e->e1->type->isintegral ()
? TRUNC_DIV_EXPR : RDIV_EXPR;
break;
case TOKmodass:
code = e->e1->type->isfloating ()
? FLOAT_MOD_EXPR : TRUNC_MOD_EXPR;
break;
case TOKandass:
code = BIT_AND_EXPR;
break;
case TOKorass:
code = BIT_IOR_EXPR;
break;
case TOKxorass:
code = BIT_XOR_EXPR;
break;
case TOKpowass:
gcc_unreachable ();
case TOKshlass:
code = LSHIFT_EXPR;
break;
case TOKshrass:
case TOKushrass:
/* Use the original lhs type before it was promoted. The left operand
of `>>>=' does not undergo integral promotions before shifting.
Strip off casts just incase anyway. */
while (e1b->op == TOKcast)
{
CastExp *ce = e1b->isCastExp ();
gcc_assert (same_type_p (ce->type, ce->to));
e1b = ce->e1;
}
code = (e->op == TOKshrass) ? RSHIFT_EXPR : UNSIGNED_RSHIFT_EXPR;
break;
default:
gcc_unreachable ();
}
tree exp = binop_assignment (code, e1b, e->e2);
this->result_ = convert_expr (exp, e1b->type, e->type);
}
/* Build a concat assignment expression. The right operand is appended
to the left operand. */
void visit (CatAssignExp *e)
{
Type *tb1 = e->e1->type->toBasetype ();
Type *tb2 = e->e2->type->toBasetype ();
Type *etype = tb1->nextOf ()->toBasetype ();
/* Save the address of `e1', so it can be evaluated first.
As all D run-time library functions for concat assignments update `e1'
in-place and then return its value, the saved address can also be used as
the result of this expression as well. */
tree lhs = build_expr (e->e1);
tree lexpr = stabilize_expr (&lhs);
tree ptr = d_save_expr (build_address (lhs));
tree result = NULL_TREE;
if (tb1->ty == Tarray && tb2->ty == Tdchar
&& (etype->ty == Tchar || etype->ty == Twchar))
{
/* Append a dchar to a char[] or wchar[]:
The assignment is handled by the D run-time library, so only
need to call `_d_arrayappend[cw]d(&e1, e2)' */
libcall_fn libcall = (etype->ty == Tchar)
? LIBCALL_ARRAYAPPENDCD : LIBCALL_ARRAYAPPENDWD;
result = build_libcall (libcall, e->type, 2,
ptr, build_expr (e->e2));
}
else
{
gcc_assert (tb1->ty == Tarray || tb2->ty == Tsarray);
if ((tb2->ty == Tarray || tb2->ty == Tsarray)
&& same_type_p (etype, tb2->nextOf ()->toBasetype ()))
{
/* Append an array to another array:
The assignment is handled by the D run-time library, so only
need to call `_d_arrayappendT(ti, &e1, e2)' */
result = build_libcall (LIBCALL_ARRAYAPPENDT, e->type, 3,
build_typeinfo (e->loc, e->type),
ptr, d_array_convert (e->e2));
}
else if (same_type_p (etype, tb2))
{
/* Append an element to an array:
The assignment is generated inline, so need to handle temporaries
here, and ensure that they are evaluated in the correct order.
The generated code should end up being equivalent to:
_d_arrayappendcTX(ti, &e1, 1)[e1.length - 1] = e2
*/
tree callexp = build_libcall (LIBCALL_ARRAYAPPENDCTX, e->type, 3,
build_typeinfo (e->loc, e->type),
ptr, size_one_node);
callexp = d_save_expr (callexp);
/* Assign e2 to last element. */
tree offexp = d_array_length (callexp);
offexp = build2 (MINUS_EXPR, TREE_TYPE (offexp),
offexp, size_one_node);
tree ptrexp = d_array_ptr (callexp);
ptrexp = void_okay_p (ptrexp);
ptrexp = build_array_index (ptrexp, offexp);
/* Evaluate expression before appending. */
tree rhs = build_expr (e->e2);
tree rexpr = stabilize_expr (&rhs);
if (TREE_CODE (rhs) == CALL_EXPR)
rhs = force_target_expr (rhs);
result = modify_expr (build_deref (ptrexp), rhs);
result = compound_expr (rexpr, result);
}
else
gcc_unreachable ();
}
/* Construct in order: ptr = &e1, _d_arrayappend(ptr, e2), *ptr; */
result = compound_expr (compound_expr (lexpr, ptr), result);
this->result_ = compound_expr (result, build_deref (ptr));
}
/* Build an assignment expression. The right operand is implicitly
converted to the type of the left operand, and assigned to it. */
void visit (AssignExp *e)
{
/* First, handle special assignment semantics. */
/* Look for array.length = n; */
if (e->e1->op == TOKarraylength)
{
/* Assignment to an array's length property; resize the array. */
ArrayLengthExp *ale = e->e1->isArrayLengthExp ();
tree newlength = convert_expr (build_expr (e->e2), e->e2->type,
Type::tsize_t);
tree ptr = build_address (build_expr (ale->e1));
/* Don't want the basetype for the element type. */
Type *etype = ale->e1->type->toBasetype ()->nextOf ();
libcall_fn libcall = etype->isZeroInit ()
? LIBCALL_ARRAYSETLENGTHT : LIBCALL_ARRAYSETLENGTHIT;
tree result = build_libcall (libcall, ale->e1->type, 3,
build_typeinfo (ale->loc, ale->e1->type),
newlength, ptr);
this->result_ = d_array_length (result);
return;
}
/* Look for array[] = n; */
if (e->e1->op == TOKslice)
{
SliceExp *se = e->e1->isSliceExp ();
Type *stype = se->e1->type->toBasetype ();
Type *etype = stype->nextOf ()->toBasetype ();
/* Determine if we need to run postblit or dtor. */
bool postblit = needs_postblit (etype) && lvalue_p (e->e2);
bool destructor = needs_dtor (etype);
if (e->memset & blockAssign)
{
/* Set a range of elements to one value. */
tree t1 = d_save_expr (build_expr (e->e1));
tree t2 = build_expr (e->e2);
tree result;
if ((postblit || destructor) && e->op != TOKblit)
{
libcall_fn libcall = (e->op == TOKconstruct)
? LIBCALL_ARRAYSETCTOR : LIBCALL_ARRAYSETASSIGN;
/* So we can call postblits on const/immutable objects. */
Type *tm = etype->unSharedOf ()->mutableOf ();
tree ti = build_typeinfo (e->loc, tm);
tree result = build_libcall (libcall, Type::tvoid, 4,
d_array_ptr (t1),
build_address (t2),
d_array_length (t1), ti);
this->result_ = compound_expr (result, t1);
return;
}
if (integer_zerop (t2))
{
tree size = size_mult_expr (d_array_length (t1),
size_int (etype->size ()));
result = build_memset_call (d_array_ptr (t1), size);
}
else
result = build_array_set (d_array_ptr (t1),
d_array_length (t1), t2);
this->result_ = compound_expr (result, t1);
}
else
{
/* Perform a memcpy operation. */
gcc_assert (e->e2->type->ty != Tpointer);
if (!postblit && !destructor)
{
tree t1 = d_save_expr (d_array_convert (e->e1));
tree t2 = d_save_expr (d_array_convert (e->e2));
/* References to array data. */
tree t1ptr = d_array_ptr (t1);
tree t1len = d_array_length (t1);
tree t2ptr = d_array_ptr (t2);
/* Generate: memcpy(to, from, size) */
tree size = size_mult_expr (t1len, size_int (etype->size ()));
tree result = build_memcpy_call (t1ptr, t2ptr, size);
/* Insert check that array lengths match and do not overlap. */
if (array_bounds_check ())
{
/* tlencmp = (t1len == t2len) */
tree t2len = d_array_length (t2);
tree tlencmp = build_boolop (EQ_EXPR, t1len, t2len);
/* toverlap = (t1ptr + size <= t2ptr
|| t2ptr + size <= t1ptr) */
tree t1ptrcmp = build_boolop (LE_EXPR,
build_offset (t1ptr, size),
t2ptr);
tree t2ptrcmp = build_boolop (LE_EXPR,
build_offset (t2ptr, size),
t1ptr);
tree toverlap = build_boolop (TRUTH_ORIF_EXPR, t1ptrcmp,
t2ptrcmp);
/* (tlencmp && toverlap) ? memcpy() : _d_arraybounds() */
tree tassert = build_array_bounds_call (e->loc);
tree tboundscheck = build_boolop (TRUTH_ANDIF_EXPR,
tlencmp, toverlap);
result = build_condition (void_type_node, tboundscheck,
result, tassert);
}
this->result_ = compound_expr (result, t1);
}
else if ((postblit || destructor) && e->op != TOKblit)
{
/* Generate: _d_arrayassign(ti, from, to)
or: _d_arrayctor(ti, from, to) */
libcall_fn libcall = (e->op == TOKconstruct)
? LIBCALL_ARRAYCTOR : LIBCALL_ARRAYASSIGN;
this->result_ = build_libcall (libcall, e->type, 3,
build_typeinfo (e->loc, etype),
d_array_convert (e->e2),
d_array_convert (e->e1));
}
else
{
/* Generate: _d_arraycopy() */
this->result_ = build_libcall (LIBCALL_ARRAYCOPY, e->type, 3,
size_int (etype->size ()),
d_array_convert (e->e2),
d_array_convert (e->e1));
}
}
return;
}
/* Look for reference initializations. */
if (e->memset & referenceInit)
{
gcc_assert (e->op == TOKconstruct || e->op == TOKblit);
gcc_assert (e->e1->op == TOKvar);
Declaration *decl = e->e1->isVarExp ()->var;
if (decl->storage_class & (STCout | STCref))
{
tree t2 = convert_for_assignment (build_expr (e->e2),
e->e2->type, e->e1->type);
tree t1 = build_expr (e->e1);
/* Want reference to lhs, not indirect ref. */
t1 = TREE_OPERAND (t1, 0);
t2 = build_address (t2);
this->result_ = indirect_ref (build_ctype (e->type),
build_assign (INIT_EXPR, t1, t2));
return;
}
}
/* Other types of assignments that may require post construction. */
Type *tb1 = e->e1->type->toBasetype ();
tree_code modifycode = (e->op == TOKconstruct) ? INIT_EXPR : MODIFY_EXPR;
/* Look for struct assignment. */
if (tb1->ty == Tstruct)
{
tree t1 = build_expr (e->e1);
tree t2 = convert_for_assignment (build_expr (e->e2, false, true),
e->e2->type, e->e1->type);
StructDeclaration *sd = tb1->isTypeStruct ()->sym;
/* Look for struct = 0. */
if (e->e2->op == TOKint64)
{
/* Use memset to fill struct. */
gcc_assert (e->op == TOKblit);
tree result = build_memset_call (t1);
/* Maybe set-up hidden pointer to outer scope context. */
if (sd->isNested ())
{
tree field = get_symbol_decl (sd->vthis);
tree value = build_vthis (sd);
tree vthis_exp = modify_expr (component_ref (t1, field), value);
result = compound_expr (result, vthis_exp);
}
this->result_ = compound_expr (result, t1);
}
else
{
/* Simple struct literal assignment. */
tree init = NULL_TREE;
/* Fill any alignment holes in the struct using memset. */
if ((e->op == TOKconstruct
|| (e->e2->op == TOKstructliteral && e->op == TOKblit))
&& (sd->isUnionDeclaration () || !identity_compare_p (sd)))
{
t1 = stabilize_reference (t1);
init = build_memset_call (t1);
}
/* Elide generating assignment if init is all zeroes. */
if (init != NULL_TREE && initializer_zerop (t2))
this->result_ = compound_expr (init, t1);
else
{
tree result = build_assign (modifycode, t1, t2);
this->result_ = compound_expr (init, result);
}
}
return;
}
/* Look for static array assignment. */
if (tb1->ty == Tsarray)
{
/* Look for array = 0. */
if (e->e2->op == TOKint64)
{
/* Use memset to fill the array. */
gcc_assert (e->op == TOKblit);
this->result_ = build_memset_call (build_expr (e->e1));
return;
}
Type *etype = tb1->nextOf ();
gcc_assert (e->e2->type->toBasetype ()->ty == Tsarray);
/* Determine if we need to run postblit. */
bool postblit = needs_postblit (etype);
bool destructor = needs_dtor (etype);
bool lvalue = lvalue_p (e->e2);
/* Optimize static array assignment with array literal. Even if the
elements in rhs are all rvalues and don't have to call postblits,
this assignment should call dtors on old assigned elements. */
if ((!postblit && !destructor)
|| (e->op == TOKconstruct && e->e2->op == TOKarrayliteral)
|| (e->op == TOKconstruct && !lvalue && postblit)
|| (e->op == TOKblit || e->e1->type->size () == 0))
{
tree t1 = build_expr (e->e1);
tree t2 = convert_for_assignment (build_expr (e->e2),
e->e2->type, e->e1->type);
this->result_ = build_assign (modifycode, t1, t2);
return;
}
Type *arrtype = (e->type->ty == Tsarray) ? etype->arrayOf () : e->type;
tree result;
if (e->op == TOKconstruct)
{
/* Generate: _d_arrayctor(ti, from, to) */
result = build_libcall (LIBCALL_ARRAYCTOR, arrtype, 3,
build_typeinfo (e->loc, etype),
d_array_convert (e->e2),
d_array_convert (e->e1));
}
else
{
/* Generate: _d_arrayassign_l()
or: _d_arrayassign_r() */
libcall_fn libcall = (lvalue)
? LIBCALL_ARRAYASSIGN_L : LIBCALL_ARRAYASSIGN_R;
tree elembuf = build_local_temp (build_ctype (etype));
result = build_libcall (libcall, arrtype, 4,
build_typeinfo (e->loc, etype),
d_array_convert (e->e2),
d_array_convert (e->e1),
build_address (elembuf));
}
/* Cast the libcall result back to a static array. */
if (e->type->ty == Tsarray)
result = indirect_ref (build_ctype (e->type),
d_array_ptr (result));
this->result_ = result;
return;
}
/* Simple assignment. */
tree t1 = build_expr (e->e1);
tree t2 = convert_for_assignment (build_expr (e->e2),
e->e2->type, e->e1->type);
this->result_ = build_assign (modifycode, t1, t2);
}
/* Build a postfix expression. */
void visit (PostExp *e)
{
tree result;
if (e->op == TOKplusplus)
{
result = build2 (POSTINCREMENT_EXPR, build_ctype (e->type),
build_expr (e->e1), build_expr (e->e2));
}
else if (e->op == TOKminusminus)
{
result = build2 (POSTDECREMENT_EXPR, build_ctype (e->type),
build_expr (e->e1), build_expr (e->e2));
}
else
gcc_unreachable ();
TREE_SIDE_EFFECTS (result) = 1;
this->result_ = result;
}
/* Build an index expression. */
void visit (IndexExp *e)
{
Type *tb1 = e->e1->type->toBasetype ();
if (tb1->ty == Taarray)
{
/* Get the key for the associative array. */
Type *tkey = tb1->isTypeAArray ()->index->toBasetype ();
tree key = convert_expr (build_expr (e->e2), e->e2->type, tkey);
libcall_fn libcall;
tree tinfo, ptr;
if (e->modifiable)
{
libcall = LIBCALL_AAGETY;
ptr = build_address (build_expr (e->e1));
tinfo = build_typeinfo (e->loc, tb1->unSharedOf ()->mutableOf ());
}
else
{
libcall = LIBCALL_AAGETRVALUEX;
ptr = build_expr (e->e1);
tinfo = build_typeinfo (e->loc, tkey);
}
/* Index the associative array. */
tree result = build_libcall (libcall, e->type->pointerTo (), 4,
ptr, tinfo,
size_int (tb1->nextOf ()->size ()),
build_address (key));
if (!e->indexIsInBounds && array_bounds_check ())
{
tree tassert = build_array_bounds_call (e->loc);
result = d_save_expr (result);
result = build_condition (TREE_TYPE (result),
d_truthvalue_conversion (result),
result, tassert);
}
this->result_ = indirect_ref (build_ctype (e->type), result);
}
else
{
/* Get the data pointer and length for static and dynamic arrays. */
tree array = d_save_expr (build_expr (e->e1));
tree ptr = convert_expr (array, tb1, tb1->nextOf ()->pointerTo ());
tree length = NULL_TREE;
if (tb1->ty != Tpointer)
length = get_array_length (array, tb1);
else
gcc_assert (e->lengthVar == NULL);
/* The __dollar variable just becomes a placeholder for the
actual length. */
if (e->lengthVar)
e->lengthVar->csym = length;
/* Generate the index. */
tree index = build_expr (e->e2);
/* If it's a static array and the index is constant, the front end has
already checked the bounds. */
if (tb1->ty != Tpointer && !e->indexIsInBounds)
index = build_bounds_condition (e->e2->loc, index, length, false);
/* Index the .ptr. */
ptr = void_okay_p (ptr);
this->result_ = indirect_ref (TREE_TYPE (TREE_TYPE (ptr)),
build_array_index (ptr, index));
}
}
/* Build a comma expression. The type is the type of the right operand. */
void visit (CommaExp *e)
{
tree t1 = build_expr (e->e1);
tree t2 = build_expr (e->e2);
tree type = e->type ? build_ctype (e->type) : void_type_node;
this->result_ = build2 (COMPOUND_EXPR, type, t1, t2);
}
/* Build an array length expression. Returns the number of elements
in the array. The result is of type size_t. */
void visit (ArrayLengthExp *e)
{
if (e->e1->type->toBasetype ()->ty == Tarray)
this->result_ = d_array_length (build_expr (e->e1));
else
{
/* Static arrays have already been handled by the front-end. */
error ("unexpected type for array length: %qs", e->type->toChars ());
this->result_ = error_mark_node;
}
}
/* Build a delegate pointer expression. This will return the frame
pointer value as a type void*. */
void visit (DelegatePtrExp *e)
{
tree t1 = build_expr (e->e1);
this->result_ = delegate_object (t1);
}
/* Build a delegate function pointer expression. This will return the
function pointer value as a function type. */
void visit (DelegateFuncptrExp *e)
{
tree t1 = build_expr (e->e1);
this->result_ = delegate_method (t1);
}
/* Build a slice expression. */
void visit (SliceExp *e)
{
Type *tb = e->type->toBasetype ();
Type *tb1 = e->e1->type->toBasetype ();
gcc_assert (tb->ty == Tarray || tb->ty == Tsarray);
/* Use convert-to-dynamic-array code if possible. */
if (!e->lwr)
{
tree result = build_expr (e->e1);
if (e->e1->type->toBasetype ()->ty == Tsarray)
result = convert_expr (result, e->e1->type, e->type);
this->result_ = result;
return;
}
else
gcc_assert (e->upr != NULL);
/* Get the data pointer and length for static and dynamic arrays. */
tree array = d_save_expr (build_expr (e->e1));
tree ptr = convert_expr (array, tb1, tb1->nextOf ()->pointerTo ());
tree length = NULL_TREE;
/* Our array is already a SAVE_EXPR if necessary, so we don't make length
a SAVE_EXPR which is, at most, a COMPONENT_REF on top of array. */
if (tb1->ty != Tpointer)
length = get_array_length (array, tb1);
else
gcc_assert (e->lengthVar == NULL);
/* The __dollar variable just becomes a placeholder for the
actual length. */
if (e->lengthVar)
e->lengthVar->csym = length;
/* Generate upper and lower bounds. */
tree lwr_tree = d_save_expr (build_expr (e->lwr));
tree upr_tree = d_save_expr (build_expr (e->upr));
/* If the upper bound has any side effects, then the lower bound should be
copied to a temporary always. */
if (TREE_CODE (upr_tree) == SAVE_EXPR && TREE_CODE (lwr_tree) != SAVE_EXPR)
lwr_tree = save_expr (lwr_tree);
/* Adjust the .ptr offset. */
if (!integer_zerop (lwr_tree))
{
tree ptrtype = TREE_TYPE (ptr);
ptr = build_array_index (void_okay_p (ptr), lwr_tree);
ptr = build_nop (ptrtype, ptr);
}
else
lwr_tree = NULL_TREE;
/* Nothing more to do for static arrays, their bounds checking has been
done at compile-time. */
if (tb->ty == Tsarray)
{
this->result_ = indirect_ref (build_ctype (e->type), ptr);
return;
}
else
gcc_assert (tb->ty == Tarray);
/* Generate bounds checking code. */
tree newlength;
if (!e->upperIsInBounds)
{
if (length)
{
newlength = build_bounds_condition (e->upr->loc, upr_tree,
length, true);
}
else
{
/* Still need to check bounds lwr <= upr for pointers. */
gcc_assert (tb1->ty == Tpointer);
newlength = upr_tree;
}
}
else
newlength = upr_tree;
if (lwr_tree)
{
/* Enforces lwr <= upr. No need to check lwr <= length as
we've already ensured that upr <= length. */
if (!e->lowerIsLessThanUpper)
{
tree cond = build_bounds_condition (e->lwr->loc, lwr_tree,
upr_tree, true);
/* When bounds checking is off, the index value is
returned directly. */
if (cond != lwr_tree)
newlength = compound_expr (cond, newlength);
}
/* Need to ensure lwr always gets evaluated first, as it may be a
function call. Generates (lwr, upr) - lwr. */
newlength = fold_build2 (MINUS_EXPR, TREE_TYPE (newlength),
compound_expr (lwr_tree, newlength), lwr_tree);
}
tree result = d_array_value (build_ctype (e->type), newlength, ptr);
this->result_ = compound_expr (array, result);
}
/* Build a cast expression, which converts the given unary expression to the
type of result. */
void visit (CastExp *e)
{
Type *ebtype = e->e1->type->toBasetype ();
Type *tbtype = e->to->toBasetype ();
tree result = build_expr (e->e1, this->constp_, this->literalp_);
/* Just evaluate e1 if it has any side effects. */
if (tbtype->ty == Tvoid)
this->result_ = build_nop (build_ctype (tbtype), result);
else
this->result_ = convert_for_rvalue (result, ebtype, tbtype);
}
/* Build a delete expression. */
void visit (DeleteExp *e)
{
tree t1 = build_expr (e->e1);
Type *tb1 = e->e1->type->toBasetype ();
if (tb1->ty == Tclass)
{
/* For class object references, if there is a destructor for that class,
the destructor is called for the object instance. */
libcall_fn libcall;
if (e->e1->op == TOKvar)
{
VarDeclaration *v = e->e1->isVarExp ()->var->isVarDeclaration ();
if (v && v->onstack)
{
libcall = tb1->isClassHandle ()->isInterfaceDeclaration ()
? LIBCALL_CALLINTERFACEFINALIZER : LIBCALL_CALLFINALIZER;
this->result_ = build_libcall (libcall, Type::tvoid, 1, t1);
return;
}
}
/* Otherwise, the garbage collector is called to immediately free the
memory allocated for the class instance. */
libcall = tb1->isClassHandle ()->isInterfaceDeclaration ()
? LIBCALL_DELINTERFACE : LIBCALL_DELCLASS;
t1 = build_address (t1);
this->result_ = build_libcall (libcall, Type::tvoid, 1, t1);
}
else if (tb1->ty == Tarray)
{
/* For dynamic arrays, the garbage collector is called to immediately
release the memory. */
Type *telem = tb1->nextOf ()->baseElemOf ();
tree ti = null_pointer_node;
if (TypeStruct *ts = telem->isTypeStruct ())
{
/* Might need to run destructor on array contents. */
if (ts->sym->dtor)
ti = build_typeinfo (e->loc, tb1->nextOf ());
}
/* Generate: _delarray_t (&t1, ti); */
this->result_ = build_libcall (LIBCALL_DELARRAYT, Type::tvoid, 2,
build_address (t1), ti);
}
else if (tb1->ty == Tpointer)
{
/* For pointers to a struct instance, if the struct has overloaded
operator delete, then that operator is called. */
t1 = build_address (t1);
Type *tnext = tb1->isTypePointer ()->next->toBasetype ();
if (TypeStruct *ts = tnext->isTypeStruct ())
{
if (ts->sym->dtor)
{
tree ti = build_typeinfo (e->loc, tnext);
this->result_ = build_libcall (LIBCALL_DELSTRUCT, Type::tvoid,
2, t1, ti);
return;
}
}
/* Otherwise, the garbage collector is called to immediately free the
memory allocated for the pointer. */
this->result_ = build_libcall (LIBCALL_DELMEMORY, Type::tvoid, 1, t1);
}
else
{
error ("don%'t know how to delete %qs", e->e1->toChars ());
this->result_ = error_mark_node;
}
}
/* Build a remove expression, which removes a particular key from an
associative array. */
void visit (RemoveExp *e)
{
/* Check that the array is actually an associative array. */
if (e->e1->type->toBasetype ()->ty == Taarray)
{
Type *tb = e->e1->type->toBasetype ();
Type *tkey = tb->isTypeAArray ()->index->toBasetype ();
tree index = convert_expr (build_expr (e->e2), e->e2->type, tkey);
this->result_ = build_libcall (LIBCALL_AADELX, Type::tbool, 3,
build_expr (e->e1),
build_typeinfo (e->loc, tkey),
build_address (index));
}
else
{
error ("%qs is not an associative array", e->e1->toChars ());
this->result_ = error_mark_node;
}
}
/* Build an unary not expression. */
void visit (NotExp *e)
{
tree result = convert_for_condition (build_expr (e->e1), e->e1->type);
/* Need to convert to boolean type or this will fail. */
result = fold_build1 (TRUTH_NOT_EXPR, d_bool_type, result);
this->result_ = d_convert (build_ctype (e->type), result);
}
/* Build a compliment expression, where all the bits in the value are
complemented. Note: unlike in C, the usual integral promotions
are not performed prior to the complement operation. */
void visit (ComExp *e)
{
TY ty1 = e->e1->type->toBasetype ()->ty;
gcc_assert (ty1 != Tarray && ty1 != Tsarray);
this->result_ = fold_build1 (BIT_NOT_EXPR, build_ctype (e->type),
build_expr (e->e1));
}
/* Build an unary negation expression. */
void visit (NegExp *e)
{
TY ty1 = e->e1->type->toBasetype ()->ty;
gcc_assert (ty1 != Tarray && ty1 != Tsarray);
tree type = build_ctype (e->type);
tree expr = build_expr (e->e1);
/* If the operation needs excess precision. */
tree eptype = excess_precision_type (type);
if (eptype != NULL_TREE)
expr = d_convert (eptype, expr);
else
eptype = type;
tree ret = fold_build1 (NEGATE_EXPR, eptype, expr);
this->result_ = d_convert (type, ret);
}
/* Build a pointer index expression. */
void visit (PtrExp *e)
{
Type *tnext = NULL;
size_t offset;
tree result;
if (e->e1->op == TOKadd)
{
AddExp *ae = e->e1->isAddExp ();
if (ae->e1->op == TOKaddress
&& ae->e2->isConst () && ae->e2->type->isintegral ())
{
Expression *ex = ae->e1->isAddrExp ()->e1;
tnext = ex->type->toBasetype ();
result = build_expr (ex);
offset = ae->e2->toUInteger ();
}
}
else if (e->e1->op == TOKsymoff)
{
SymOffExp *se = e->e1->isSymOffExp ();
if (!declaration_reference_p (se->var))
{
tnext = se->var->type->toBasetype ();
result = get_decl_tree (se->var);
offset = se->offset;
}
}
/* Produce better code by converting *(#record + n) to
COMPONENT_REFERENCE. Otherwise, the variable will always be
allocated in memory because its address is taken. */
if (tnext && tnext->ty == Tstruct)
{
StructDeclaration *sd = tnext->isTypeStruct ()->sym;
for (size_t i = 0; i < sd->fields.length; i++)
{
VarDeclaration *field = sd->fields[i];
if (field->offset == offset
&& same_type_p (field->type, e->type))
{
/* Catch errors, backend will ICE otherwise. */
if (error_operand_p (result))
this->result_ = result;
else
{
result = component_ref (result, get_symbol_decl (field));
this->result_ = result;
}
return;
}
else if (field->offset > offset)
break;
}
}
this->result_ = indirect_ref (build_ctype (e->type), build_expr (e->e1));
}
/* Build an unary address expression. */
void visit (AddrExp *e)
{
tree type = build_ctype (e->type);
tree exp;
/* The frontend optimizer can convert const symbol into a struct literal.
Taking the address of a struct literal is otherwise illegal. */
if (e->e1->op == TOKstructliteral)
{
StructLiteralExp *sle = e->e1->isStructLiteralExp ()->origin;
gcc_assert (sle != NULL);
/* Build the reference symbol, the decl is built first as the
initializer may have recursive references. */
if (!sle->sym)
{
sle->sym = build_artificial_decl (build_ctype (sle->type),
NULL_TREE, "S");
DECL_INITIAL (sle->sym) = build_expr (sle, true);
d_pushdecl (sle->sym);
rest_of_decl_compilation (sle->sym, 1, 0);
}
exp = sle->sym;
}
else
exp = build_expr (e->e1, this->constp_, this->literalp_);
TREE_CONSTANT (exp) = 0;
this->result_ = d_convert (type, build_address (exp));
}
/* Build a function call expression. */
void visit (CallExp *e)
{
Type *tb = e->e1->type->toBasetype ();
Expression *e1b = e->e1;
tree callee = NULL_TREE;
tree object = NULL_TREE;
tree cleanup = NULL_TREE;
tree returnvalue = NULL_TREE;
TypeFunction *tf = NULL;
/* Calls to delegates can sometimes look like this. */
if (e1b->op == TOKcomma)
{
e1b = e1b->isCommaExp ()->e2;
gcc_assert (e1b->op == TOKvar);
Declaration *var = e1b->isVarExp ()->var;
gcc_assert (var->isFuncDeclaration () && !var->needThis ());
}
if (e1b->op == TOKdotvar && tb->ty != Tdelegate)
{
DotVarExp *dve = e1b->isDotVarExp ();
/* Don't modify the static initializer for struct literals. */
if (dve->e1->op == TOKstructliteral)
{
StructLiteralExp *sle = dve->e1->isStructLiteralExp ();
sle->useStaticInit = false;
}
FuncDeclaration *fd = dve->var->isFuncDeclaration ();
if (fd != NULL)
{
/* Get the correct callee from the DotVarExp object. */
tree fndecl = get_symbol_decl (fd);
AggregateDeclaration *ad = fd->isThis ();
/* Static method; ignore the object instance. */
if (!ad)
callee = build_address (fndecl);
else
{
tree thisexp = build_expr (dve->e1);
/* When constructing temporaries, if the constructor throws,
then the object is destructed even though it is not a fully
constructed object yet. And so this call will need to be
moved inside the TARGET_EXPR_INITIAL slot. */
if (fd->isCtorDeclaration ()
&& TREE_CODE (thisexp) == COMPOUND_EXPR
&& TREE_CODE (TREE_OPERAND (thisexp, 0)) == TARGET_EXPR
&& TARGET_EXPR_CLEANUP (TREE_OPERAND (thisexp, 0)))
{
cleanup = TREE_OPERAND (thisexp, 0);
thisexp = TREE_OPERAND (thisexp, 1);
}
if (TREE_CODE (thisexp) == CONSTRUCTOR)
thisexp = force_target_expr (thisexp);
/* Want reference to `this' object. */
if (!POINTER_TYPE_P (TREE_TYPE (thisexp)))
thisexp = build_address (thisexp);
/* Make the callee a virtual call. */
if (fd->isVirtual () && !fd->isFinalFunc () && !e->directcall)
{
tree fntype = build_pointer_type (TREE_TYPE (fndecl));
tree thistype = build_ctype (ad->handleType ());
thisexp = build_nop (thistype, d_save_expr (thisexp));
fndecl = build_vindex_ref (thisexp, fntype, fd->vtblIndex);
}
else
fndecl = build_address (fndecl);
/* C++ constructors return void, even though front-end semantic
treats them as implicitly returning `this'. Set returnvalue
to override the result of this expression. */
if (fd->isCtorDeclaration () && fd->linkage == LINKcpp)
{
thisexp = d_save_expr (thisexp);
returnvalue = thisexp;
}
callee = build_method_call (fndecl, thisexp, fd->type);
}
}
}
if (callee == NULL_TREE)
callee = build_expr (e1b);
if (METHOD_CALL_EXPR (callee))
{
/* This could be a delegate expression (TY == Tdelegate), but not
actually a delegate variable. */
if (e1b->op == TOKdotvar)
{
/* This gets the true function type, getting the function type
from e1->type can sometimes be incorrect, such as when calling
a `ref' return function. */
tf = get_function_type (e1b->isDotVarExp ()->var->type);
}
else
tf = get_function_type (tb);
extract_from_method_call (callee, callee, object);
}
else if (tb->ty == Tdelegate)
{
/* Delegate call, extract .object and .funcptr from var. */
callee = d_save_expr (callee);
tf = get_function_type (tb);
object = delegate_object (callee);
callee = delegate_method (callee);
}
else if (e1b->op == TOKvar)
{
FuncDeclaration *fd = e1b->isVarExp ()->var->isFuncDeclaration ();
gcc_assert (fd != NULL);
tf = get_function_type (fd->type);
if (fd->isNested ())
{
/* Maybe re-evaluate symbol storage treating `fd' as public. */
if (call_by_alias_p (d_function_chain->function, fd))
TREE_PUBLIC (callee) = 1;
object = get_frame_for_symbol (fd);
}
else if (fd->needThis ())
{
error_at (make_location_t (e1b->loc),
"need %<this%> to access member %qs", fd->toChars ());
/* Continue compiling... */
object = null_pointer_node;
}
}
else
{
/* Normal direct function call. */
tf = get_function_type (tb);
}
gcc_assert (tf != NULL);
/* Now we have the type, callee and maybe object reference,
build the call expression. */
tree exp = d_build_call (tf, callee, object, e->arguments);
if (returnvalue != NULL_TREE)
exp = compound_expr (exp, returnvalue);
if (tf->isref)
exp = build_deref (exp);
/* Some library calls are defined to return a generic type.
this->type is the real type we want to return. */
if (e->type->isTypeBasic ())
exp = d_convert (build_ctype (e->type), exp);
/* If this call was found to be a constructor for a temporary with a
cleanup, then move the call inside the TARGET_EXPR. */
if (cleanup != NULL_TREE)
{
tree init = TARGET_EXPR_INITIAL (cleanup);
TARGET_EXPR_INITIAL (cleanup) = compound_expr (init, exp);
exp = cleanup;
}
this->result_ = exp;
}
/* Build a delegate expression. */
void visit (DelegateExp *e)
{
if (e->func->semanticRun == PASSsemantic3done)
{
/* Add the function as nested function if it belongs to this module.
ie: it is a member of this module, or it is a template instance. */
Dsymbol *owner = e->func->toParent ();
while (!owner->isTemplateInstance () && owner->toParent ())
owner = owner->toParent ();
if (owner->isTemplateInstance () || owner == d_function_chain->module)
build_decl_tree (e->func);
}
tree fndecl;
tree object;
if (e->func->isNested ())
{
if (e->e1->op == TOKnull)
object = build_expr (e->e1);
else
object = get_frame_for_symbol (e->func);
fndecl = build_address (get_symbol_decl (e->func));
}
else
{
if (!e->func->isThis ())
{
error ("delegates are only for non-static functions");
this->result_ = error_mark_node;
return;
}
object = build_expr (e->e1);
/* Want reference to `this' object. */
if (e->e1->type->ty != Tclass && e->e1->type->ty != Tpointer)
object = build_address (object);
/* Object reference could be the outer `this' field of a class or
closure of type `void*'. Cast it to the right type. */
if (e->e1->type->ty == Tclass)
object = d_convert (build_ctype (e->e1->type), object);
fndecl = get_symbol_decl (e->func);
/* Get pointer to function out of the virtual table. */
if (e->func->isVirtual () && !e->func->isFinalFunc ()
&& e->e1->op != TOKsuper && e->e1->op != TOKdottype)
{
tree fntype = build_pointer_type (TREE_TYPE (fndecl));
object = d_save_expr (object);
fndecl = build_vindex_ref (object, fntype, e->func->vtblIndex);
}
else
fndecl = build_address (fndecl);
}
this->result_ = build_method_call (fndecl, object, e->type);
}
/* Build a type component expression. */
void visit (DotTypeExp *e)
{
/* Just a pass through to underlying expression. */
this->result_ = build_expr (e->e1);
}
/* Build a component reference expression. */
void visit (DotVarExp *e)
{
VarDeclaration *vd = e->var->isVarDeclaration ();
/* This could also be a function, but relying on that being taken
care of by the visitor interface for CallExp. */
if (vd != NULL)
{
if (!vd->isField ())
this->result_ = get_decl_tree (vd);
else
{
tree object = build_expr (e->e1);
if (e->e1->type->toBasetype ()->ty != Tstruct)
object = build_deref (object);
this->result_ = component_ref (object, get_symbol_decl (vd));
}
}
else
{
error ("%qs is not a field, but a %qs",
e->var->toChars (), e->var->kind ());
this->result_ = error_mark_node;
}
}
/* Build an assert expression, used to declare conditions that must hold at
that a given point in the program. */
void visit (AssertExp *e)
{
Type *tb1 = e->e1->type->toBasetype ();
tree arg = build_expr (e->e1);
tree tmsg = NULL_TREE;
tree assert_pass = void_node;
tree assert_fail;
if (global.params.useAssert == CHECKENABLEon
&& global.params.checkAction == CHECKACTION_D)
{
/* Generate: ((bool) e1 ? (void)0 : _d_assert (...))
or: (e1 != null ? e1._invariant() : _d_assert (...)) */
bool unittest_p = d_function_chain->function->isUnitTestDeclaration ();
libcall_fn libcall;
if (e->msg)
{
tmsg = build_expr_dtor (e->msg);
libcall = unittest_p ? LIBCALL_UNITTEST_MSG : LIBCALL_ASSERT_MSG;
}
else
libcall = unittest_p ? LIBCALL_UNITTEST : LIBCALL_ASSERT;
/* Build a call to _d_assert(). */
assert_fail = d_assert_call (e->loc, libcall, tmsg);
if (global.params.useInvariants == CHECKENABLEon)
{
/* If the condition is a D class or struct object with an invariant,
call it if the condition result is true. */
if (tb1->ty == Tclass)
{
ClassDeclaration *cd = tb1->isClassHandle ();
if (!cd->isInterfaceDeclaration () && !cd->isCPPclass ())
{
arg = d_save_expr (arg);
assert_pass = build_libcall (LIBCALL_INVARIANT,
Type::tvoid, 1, arg);
}
}
else if (tb1->ty == Tpointer && tb1->nextOf ()->ty == Tstruct)
{
StructDeclaration *sd = tb1->nextOf ()->isTypeStruct ()->sym;
if (sd->inv != NULL)
{
Expressions args;
arg = d_save_expr (arg);
assert_pass = d_build_call_expr (sd->inv, arg, &args);
}
}
}
}
else if (global.params.useAssert == CHECKENABLEon
&& global.params.checkAction == CHECKACTION_C)
{
/* Generate: __builtin_trap() */
tree fn = builtin_decl_explicit (BUILT_IN_TRAP);
assert_fail = build_call_expr (fn, 0);
}
else
{
/* Assert contracts are turned off, if the contract condition has no
side effects can still use it as a predicate for the optimizer. */
if (TREE_SIDE_EFFECTS (arg))
{
this->result_ = void_node;
return;
}
assert_fail = build_predict_expr (PRED_NORETURN, NOT_TAKEN);
}
/* Build condition that we are asserting in this contract. */
tree condition = convert_for_condition (arg, e->e1->type);
/* We expect the condition to always be true, as what happens if an assert
contract is false is undefined behavior. */
tree fn = builtin_decl_explicit (BUILT_IN_EXPECT);
tree arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
tree pred_type = TREE_VALUE (arg_types);
tree expected_type = TREE_VALUE (TREE_CHAIN (arg_types));
condition = build_call_expr (fn, 2, d_convert (pred_type, condition),
build_int_cst (expected_type, 1));
condition = d_truthvalue_conversion (condition);
this->result_ = build_vcondition (condition, assert_pass, assert_fail);
}
/* Build a declaration expression. */
void visit (DeclarationExp *e)
{
/* Compile the declaration. */
push_stmt_list ();
build_decl_tree (e->declaration);
tree result = pop_stmt_list ();
/* Construction of an array for typesafe-variadic function arguments
can cause an empty STMT_LIST here. This can causes problems
during gimplification. */
if (TREE_CODE (result) == STATEMENT_LIST && !STATEMENT_LIST_HEAD (result))
result = build_empty_stmt (input_location);
this->result_ = result;
}
/* Build a typeid expression. Returns an instance of class TypeInfo
corresponding to. */
void visit (TypeidExp *e)
{
if (Type *tid = isType (e->obj))
{
tree ti = build_typeinfo (e->loc, tid);
/* If the typeinfo is at an offset. */
if (tid->vtinfo->offset)
ti = build_offset (ti, size_int (tid->vtinfo->offset));
this->result_ = build_nop (build_ctype (e->type), ti);
}
else if (Expression *tid = isExpression (e->obj))
{
Type *type = tid->type->toBasetype ();
assert (type->ty == Tclass);
/* Generate **classptr to get the classinfo. */
tree ci = build_expr (tid);
ci = indirect_ref (ptr_type_node, ci);
ci = indirect_ref (ptr_type_node, ci);
/* Add extra indirection for interfaces. */
if (type->isTypeClass ()->sym->isInterfaceDeclaration ())
ci = indirect_ref (ptr_type_node, ci);
this->result_ = build_nop (build_ctype (e->type), ci);
}
else
gcc_unreachable ();
}
/* Build a function/lambda expression. */
void visit (FuncExp *e)
{
Type *ftype = e->type->toBasetype ();
/* This check is for lambda's, remove `vthis' as function isn't nested. */
if (e->fd->tok == TOKreserved && ftype->ty == Tpointer)
{
e->fd->tok = TOKfunction;
e->fd->vthis = NULL;
}
/* Compile the function literal body. */
build_decl_tree (e->fd);
/* If nested, this will be a trampoline. */
if (e->fd->isNested ())
{
tree func = build_address (get_symbol_decl (e->fd));
tree object;
if (this->constp_)
{
/* Static delegate variables have no context pointer. */
object = null_pointer_node;
this->result_ = build_method_call (func, object, e->fd->type);
TREE_CONSTANT (this->result_) = 1;
}
else
{
object = get_frame_for_symbol (e->fd);
this->result_ = build_method_call (func, object, e->fd->type);
}
}
else
{
this->result_ = build_nop (build_ctype (e->type),
build_address (get_symbol_decl (e->fd)));
}
}
/* Build a halt expression. */
void visit (HaltExp *)
{
/* Should we use trap() or abort()? */
tree ttrap = builtin_decl_explicit (BUILT_IN_TRAP);
this->result_ = build_call_expr (ttrap, 0);
}
/* Build a symbol pointer offset expression. */
void visit (SymOffExp *e)
{
/* Build the address and offset of the symbol. */
size_t soffset = e->isSymOffExp ()->offset;
tree result = get_decl_tree (e->var);
TREE_USED (result) = 1;
if (declaration_reference_p (e->var))
gcc_assert (POINTER_TYPE_P (TREE_TYPE (result)));
else
result = build_address (result);
if (!soffset)
result = d_convert (build_ctype (e->type), result);
else
{
tree offset = size_int (soffset);
result = build_nop (build_ctype (e->type),
build_offset (result, offset));
}
this->result_ = result;
}
/* Build a variable expression. */
void visit (VarExp *e)
{
if (e->var->needThis ())
{
error ("need %<this%> to access member %qs", e->var->ident->toChars ());
this->result_ = error_mark_node;
return;
}
else if (e->var->ident == Identifier::idPool ("__ctfe"))
{
/* __ctfe is always false at run-time. */
this->result_ = integer_zero_node;
return;
}
/* This check is same as is done in FuncExp for lambdas. */
FuncLiteralDeclaration *fld = e->var->isFuncLiteralDeclaration ();
if (fld != NULL)
{
if (fld->tok == TOKreserved)
{
fld->tok = TOKfunction;
fld->vthis = NULL;
}
/* Compiler the function literal body. */
build_decl_tree (fld);
}
if (this->constp_)
{
/* Want the initializer, not the expression. */
VarDeclaration *var = e->var->isVarDeclaration ();
SymbolDeclaration *sd = e->var->isSymbolDeclaration ();
tree init = NULL_TREE;
if (var && (var->isConst () || var->isImmutable ())
&& e->type->toBasetype ()->ty != Tsarray && var->_init)
{
if (var->inuse)
error_at (make_location_t (e->loc), "recursive reference %qs",
e->toChars ());
else
{
var->inuse++;
init = build_expr (initializerToExpression (var->_init), true);
var->inuse--;
}
}
else if (sd && sd->dsym)
init = layout_struct_initializer (sd->dsym);
else
error_at (make_location_t (e->loc), "non-constant expression %qs",
e->toChars ());
if (init != NULL_TREE)
this->result_ = init;
else
this->result_ = error_mark_node;
}
else
{
tree result = get_decl_tree (e->var);
TREE_USED (result) = 1;
/* For variables that are references - currently only out/inout
arguments; objects don't count - evaluating the variable means
we want what it refers to. */
if (declaration_reference_p (e->var))
result = indirect_ref (build_ctype (e->var->type), result);
this->result_ = result;
}
}
/* Build a this variable expression. */
void visit (ThisExp *e)
{
FuncDeclaration *fd = d_function_chain ? d_function_chain->function : NULL;
tree result = NULL_TREE;
if (e->var)
result = get_decl_tree (e->var);
else
{
gcc_assert (fd && fd->vthis);
result = get_decl_tree (fd->vthis);
}
if (e->type->ty == Tstruct)
result = build_deref (result);
this->result_ = result;
}
/* Build a new expression, which allocates memory either on the garbage
collected heap or by using a class or struct specific allocator. */
void visit (NewExp *e)
{
Type *tb = e->type->toBasetype ();
tree result;
if (e->allocator)
gcc_assert (e->newargs);
if (tb->ty == Tclass)
{
/* Allocating a new class. */
tb = e->newtype->toBasetype ();
ClassDeclaration *cd = tb->isTypeClass ()->sym;
tree type = build_ctype (tb);
tree setup_exp = NULL_TREE;
tree new_call;
if (e->onstack)
{
/* If being used as an initializer for a local variable with scope
storage class, then the instance is allocated on the stack
rather than the heap or using the class specific allocator. */
tree var = build_local_temp (TREE_TYPE (type));
new_call = build_nop (type, build_address (var));
setup_exp = modify_expr (var, aggregate_initializer_decl (cd));
}
else if (e->allocator)
{
/* Call class allocator, and copy the initializer into memory. */
new_call = d_build_call_expr (e->allocator, NULL_TREE, e->newargs);
new_call = d_save_expr (new_call);
new_call = build_nop (type, new_call);
setup_exp = modify_expr (build_deref (new_call),
aggregate_initializer_decl (cd));
}
else
{
/* Generate: _d_newclass() */
tree arg = build_address (get_classinfo_decl (cd));
new_call = build_libcall (LIBCALL_NEWCLASS, tb, 1, arg);
}
/* Set the context pointer for nested classes. */
if (cd->isNested ())
{
tree field = get_symbol_decl (cd->vthis);
tree value = NULL_TREE;
if (e->thisexp)
{
ClassDeclaration *tcd = e->thisexp->type->isClassHandle ();
Dsymbol *outer = cd->toParent2 ();
int offset = 0;
value = build_expr (e->thisexp);
if (outer != tcd)
{
ClassDeclaration *ocd = outer->isClassDeclaration ();
gcc_assert (ocd->isBaseOf (tcd, &offset));
/* Could just add offset... */
value = convert_expr (value, e->thisexp->type, ocd->type);
}
}
else
value = build_vthis (cd);
if (value != NULL_TREE)
{
/* Generate: (new())->vthis = this; */
new_call = d_save_expr (new_call);
field = component_ref (build_deref (new_call), field);
setup_exp = compound_expr (setup_exp,
modify_expr (field, value));
}
}
new_call = compound_expr (setup_exp, new_call);
/* Call the class constructor. */
if (e->member)
result = d_build_call_expr (e->member, new_call, e->arguments);
else
result = new_call;
if (e->argprefix)
result = compound_expr (build_expr (e->argprefix), result);
}
else if (tb->ty == Tpointer && tb->nextOf ()->toBasetype ()->ty == Tstruct)
{
/* Allocating memory for a new struct. */
Type *htype = e->newtype->toBasetype ();
gcc_assert (!e->onstack);
TypeStruct *stype = htype->isTypeStruct ();
StructDeclaration *sd = stype->sym;
tree new_call;
/* Cannot new an opaque struct. */
if (sd->size (e->loc) == 0)
{
this->result_ = d_convert (build_ctype (e->type),
integer_zero_node);
return;
}
if (e->allocator)
{
/* Call struct allocator. */
new_call = d_build_call_expr (e->allocator, NULL_TREE, e->newargs);
new_call = build_nop (build_ctype (tb), new_call);
}
else
{
/* Generate: _d_newitemT() */
libcall_fn libcall = htype->isZeroInit ()
? LIBCALL_NEWITEMT : LIBCALL_NEWITEMIT;
tree arg = build_typeinfo (e->loc, e->newtype);
new_call = build_libcall (libcall, tb, 1, arg);
}
if (e->member || !e->arguments)
{
/* Set the context pointer for nested structs. */
if (sd->isNested ())
{
tree value = build_vthis (sd);
tree field = get_symbol_decl (sd->vthis);
tree type = build_ctype (stype);
new_call = d_save_expr (new_call);
field = component_ref (indirect_ref (type, new_call), field);
new_call = compound_expr (modify_expr (field, value), new_call);
}
/* Call the struct constructor. */
if (e->member)
result = d_build_call_expr (e->member, new_call, e->arguments);
else
result = new_call;
}
else
{
/* If we have a user supplied initializer, then set-up with a
struct literal. */
if (e->arguments != NULL && sd->fields.length != 0)
{
StructLiteralExp *se = StructLiteralExp::create (e->loc, sd,
e->arguments,
htype);
new_call = d_save_expr (new_call);
se->type = sd->type;
se->sym = new_call;
result = compound_expr (build_expr (se), new_call);
}
else
result = new_call;
}
if (e->argprefix)
result = compound_expr (build_expr (e->argprefix), result);
}
else if (tb->ty == Tarray)
{
/* Allocating memory for a new D array. */
tb = e->newtype->toBasetype ();
TypeDArray *tarray = tb->isTypeDArray ();
gcc_assert (!e->allocator);
gcc_assert (e->arguments && e->arguments->length >= 1);
if (e->arguments->length == 1)
{
/* Single dimension array allocations. */
Expression *arg = (*e->arguments)[0];
if (tarray->next->size () == 0)
{
/* Array element size is unknown. */
this->result_ = d_array_value (build_ctype (e->type),
size_int (0), null_pointer_node);
return;
}
libcall_fn libcall = tarray->next->isZeroInit ()
? LIBCALL_NEWARRAYT : LIBCALL_NEWARRAYIT;
result = build_libcall (libcall, tb, 2,
build_typeinfo (e->loc, e->type),
build_expr (arg));
}
else
{
/* Multidimensional array allocations. */
tree tarray = make_array_type (Type::tsize_t, e->arguments->length);
tree var = build_local_temp (tarray);
vec <constructor_elt, va_gc> *elms = NULL;
/* Get the base element type for the array, generating the
initializer for the dims parameter along the way. */
Type *telem = e->newtype->toBasetype ();
for (size_t i = 0; i < e->arguments->length; i++)
{
Expression *arg = (*e->arguments)[i];
CONSTRUCTOR_APPEND_ELT (elms, size_int (i), build_expr (arg));
gcc_assert (telem->ty == Tarray);
telem = telem->toBasetype ()->nextOf ();
gcc_assert (telem);
}
/* Initialize the temporary. */
tree init = modify_expr (var, build_constructor (tarray, elms));
var = compound_expr (init, var);
/* Generate: _d_newarraymTX(ti, dims)
or: _d_newarraymiTX(ti, dims) */
libcall_fn libcall = telem->isZeroInit ()
? LIBCALL_NEWARRAYMTX : LIBCALL_NEWARRAYMITX;
tree tinfo = build_typeinfo (e->loc, e->type);
tree dims = d_array_value (build_ctype (Type::tsize_t->arrayOf ()),
size_int (e->arguments->length),
build_address (var));
result = build_libcall (libcall, tb, 2, tinfo, dims);
}
if (e->argprefix)
result = compound_expr (build_expr (e->argprefix), result);
}
else if (tb->ty == Tpointer)
{
/* Allocating memory for a new pointer. */
TypePointer *tpointer = tb->isTypePointer ();
if (tpointer->next->size () == 0)
{
/* Pointer element size is unknown. */
this->result_ = d_convert (build_ctype (e->type),
integer_zero_node);
return;
}
libcall_fn libcall = tpointer->next->isZeroInit (e->loc)
? LIBCALL_NEWITEMT : LIBCALL_NEWITEMIT;
tree arg = build_typeinfo (e->loc, e->newtype);
result = build_libcall (libcall, tb, 1, arg);
if (e->arguments && e->arguments->length == 1)
{
result = d_save_expr (result);
tree init = modify_expr (build_deref (result),
build_expr ((*e->arguments)[0]));
result = compound_expr (init, result);
}
if (e->argprefix)
result = compound_expr (build_expr (e->argprefix), result);
}
else
gcc_unreachable ();
this->result_ = convert_expr (result, tb, e->type);
}
/* Build an integer literal. */
void visit (IntegerExp *e)
{
tree ctype = build_ctype (e->type->toBasetype ());
this->result_ = build_integer_cst (e->value, ctype);
}
/* Build a floating-point literal. */
void visit (RealExp *e)
{
this->result_ = build_float_cst (e->value, e->type->toBasetype ());
}
/* Build a complex literal. */
void visit (ComplexExp *e)
{
Type *tnext;
switch (e->type->toBasetype ()->ty)
{
case Tcomplex32:
tnext = (TypeBasic *) Type::tfloat32;
break;
case Tcomplex64:
tnext = (TypeBasic *) Type::tfloat64;
break;
case Tcomplex80:
tnext = (TypeBasic *) Type::tfloat80;
break;
default:
gcc_unreachable ();
}
this->result_ = build_complex (build_ctype (e->type),
build_float_cst (creall (e->value), tnext),
build_float_cst (cimagl (e->value), tnext));
}
/* Build a string literal, all strings are null terminated except for
static arrays. */
void visit (StringExp *e)
{
Type *tb = e->type->toBasetype ();
tree type = build_ctype (e->type);
if (tb->ty == Tsarray)
{
/* Turn the string into a constructor for the static array. */
vec <constructor_elt, va_gc> *elms = NULL;
vec_safe_reserve (elms, e->len);
tree etype = TREE_TYPE (type);
for (size_t i = 0; i < e->len; i++)
{
tree value = build_integer_cst (e->charAt (i), etype);
CONSTRUCTOR_APPEND_ELT (elms, size_int (i), value);
}
tree ctor = build_constructor (type, elms);
TREE_CONSTANT (ctor) = 1;
this->result_ = ctor;
}
else
{
/* Copy the string contents to a null terminated string. */
dinteger_t length = (e->len * e->sz);
char *string = XALLOCAVEC (char, length + 1);
memcpy (string, e->string, length);
string[length] = '\0';
/* String value and type includes the null terminator. */
tree value = build_string (length, string);
TREE_TYPE (value) = make_array_type (tb->nextOf (), length + 1);
value = build_address (value);
if (tb->ty == Tarray)
value = d_array_value (type, size_int (e->len), value);
TREE_CONSTANT (value) = 1;
this->result_ = d_convert (type, value);
}
}
/* Build a tuple literal. Just an argument list that may have
side effects that need evaluation. */
void visit (TupleExp *e)
{
tree result = NULL_TREE;
if (e->e0)
result = build_expr (e->e0, this->constp_, true);
for (size_t i = 0; i < e->exps->length; ++i)
{
Expression *exp = (*e->exps)[i];
result = compound_expr (result, build_expr (exp, this->constp_, true));
}
if (result == NULL_TREE)
result = void_node;
this->result_ = result;
}
/* Build an array literal. The common type of the all elements is taken to
be the type of the array element, and all elements are implicitly
converted to that type. */
void visit (ArrayLiteralExp *e)
{
Type *tb = e->type->toBasetype ();
/* Implicitly convert void[n] to ubyte[n]. */
if (tb->ty == Tsarray && tb->nextOf ()->toBasetype ()->ty == Tvoid)
tb = Type::tuns8->sarrayOf (tb->isTypeSArray ()->dim->toUInteger ());
gcc_assert (tb->ty == Tarray || tb->ty == Tsarray || tb->ty == Tpointer);
/* Handle empty array literals. */
if (e->elements->length == 0)
{
if (tb->ty == Tarray)
this->result_ = d_array_value (build_ctype (e->type),
size_int (0), null_pointer_node);
else
this->result_ = build_constructor (make_array_type (tb->nextOf (), 0),
NULL);
return;
}
/* Build an expression that assigns the expressions in ELEMENTS to
a constructor. */
vec <constructor_elt, va_gc> *elms = NULL;
vec_safe_reserve (elms, e->elements->length);
bool constant_p = true;
tree saved_elems = NULL_TREE;
Type *etype = tb->nextOf ();
tree satype = make_array_type (etype, e->elements->length);
for (size_t i = 0; i < e->elements->length; i++)
{
Expression *expr = e->getElement (i);
tree value = build_expr (expr, this->constp_, true);
/* Only append nonzero values, the backend will zero out the rest
of the constructor as we don't set CONSTRUCTOR_NO_CLEARING. */
if (!initializer_zerop (value))
{
if (!TREE_CONSTANT (value))
constant_p = false;
/* Split construction of values out of the constructor if there
may be side effects. */
tree init = stabilize_expr (&value);
if (init != NULL_TREE)
saved_elems = compound_expr (saved_elems, init);
CONSTRUCTOR_APPEND_ELT (elms, size_int (i),
convert_expr (value, expr->type, etype));
}
}
/* Now return the constructor as the correct type. For static arrays there
is nothing else to do. For dynamic arrays, return a two field struct.
For pointers, return the address. */
tree ctor = build_constructor (satype, elms);
tree type = build_ctype (e->type);
/* Nothing else to do for static arrays. */
if (tb->ty == Tsarray || this->constp_)
{
/* Can't take the address of the constructor, so create an anonymous
static symbol, and then refer to it. */
if (tb->ty != Tsarray)
{
tree decl = build_artificial_decl (TREE_TYPE (ctor), ctor, "A");
ctor = build_address (decl);
if (tb->ty == Tarray)
ctor = d_array_value (type, size_int (e->elements->length), ctor);
d_pushdecl (decl);
rest_of_decl_compilation (decl, 1, 0);
}
/* If the array literal is readonly or static. */
if (constant_p)
TREE_CONSTANT (ctor) = 1;
if (constant_p && initializer_constant_valid_p (ctor, TREE_TYPE (ctor)))
TREE_STATIC (ctor) = 1;
/* Use memset to fill any alignment holes in the array. */
if (!this->constp_ && !this->literalp_)
{
TypeStruct *ts = etype->baseElemOf ()->isTypeStruct ();
if (ts != NULL && (!identity_compare_p (ts->sym)
|| ts->sym->isUnionDeclaration ()))
{
tree var = build_local_temp (TREE_TYPE (ctor));
tree init = build_memset_call (var);
/* Evaluate memset() first, then any saved elements. */
saved_elems = compound_expr (init, saved_elems);
ctor = compound_expr (modify_expr (var, ctor), var);
}
}
this->result_ = compound_expr (saved_elems, d_convert (type, ctor));
}
else
{
/* Allocate space on the memory managed heap. */
tree mem = build_libcall (LIBCALL_ARRAYLITERALTX,
etype->pointerTo (), 2,
build_typeinfo (e->loc, etype->arrayOf ()),
size_int (e->elements->length));
mem = d_save_expr (mem);
/* Now copy the constructor into memory. */
tree size = size_mult_expr (size_int (e->elements->length),
size_int (tb->nextOf ()->size ()));
tree result = build_memcpy_call (mem, build_address (ctor), size);
/* Return the array pointed to by MEM. */
result = compound_expr (result, mem);
if (tb->ty == Tarray)
result = d_array_value (type, size_int (e->elements->length), result);
this->result_ = compound_expr (saved_elems, result);
}
}
/* Build an associative array literal. The common type of the all keys is
taken to be the key type, and common type of all values the value type.
All keys and values are then implicitly converted as needed. */
void visit (AssocArrayLiteralExp *e)
{
/* Want the mutable type for typeinfo reference. */
Type *tb = e->type->toBasetype ()->mutableOf ();
/* Handle empty assoc array literals. */
TypeAArray *ta = tb->isTypeAArray ();
if (e->keys->length == 0)
{
this->result_ = build_constructor (build_ctype (ta), NULL);
return;
}
/* Build an expression that assigns all expressions in KEYS
to a constructor. */
tree akeys = build_array_from_exprs (ta->index->sarrayOf (e->keys->length),
e->keys, this->constp_);
tree init = stabilize_expr (&akeys);
/* Do the same with all expressions in VALUES. */
tree avals = build_array_from_exprs (ta->next->sarrayOf (e->values->length),
e->values, this->constp_);
init = compound_expr (init, stabilize_expr (&avals));
/* Generate: _d_assocarrayliteralTX (ti, keys, vals); */
tree keys = d_array_value (build_ctype (ta->index->arrayOf ()),
size_int (e->keys->length),
build_address (akeys));
tree vals = d_array_value (build_ctype (ta->next->arrayOf ()),
size_int (e->values->length),
build_address (avals));
tree mem = build_libcall (LIBCALL_ASSOCARRAYLITERALTX, Type::tvoidptr, 3,
build_typeinfo (e->loc, ta), keys, vals);
/* Return an associative array pointed to by MEM. */
tree aatype = build_ctype (ta);
vec <constructor_elt, va_gc> *ce = NULL;
CONSTRUCTOR_APPEND_ELT (ce, TYPE_FIELDS (aatype), mem);
tree result = build_nop (build_ctype (e->type),
build_constructor (aatype, ce));
this->result_ = compound_expr (init, result);
}
/* Build a struct literal. */
void visit (StructLiteralExp *e)
{
/* Handle empty struct literals. */
if (e->elements == NULL || e->sd->fields.length == 0)
{
this->result_ = build_constructor (build_ctype (e->type), NULL);
return;
}
/* Building sinit trees are delayed until after frontend semantic
processing has complete. Build the static initializer now. */
if (e->useStaticInit && !this->constp_)
{
tree init = aggregate_initializer_decl (e->sd);
/* If initializing a symbol, don't forget to set it. */
if (e->sym != NULL)
{
tree var = build_deref (e->sym);
init = compound_expr (modify_expr (var, init), var);
}
this->result_ = init;
return;
}
/* Build a constructor that assigns the expressions in ELEMENTS
at each field index that has been filled in. */
vec <constructor_elt, va_gc> *ve = NULL;
tree saved_elems = NULL_TREE;
/* CTFE may fill the hidden pointer by NullExp. */
gcc_assert (e->elements->length <= e->sd->fields.length);
Type *tb = e->type->toBasetype ();
gcc_assert (tb->ty == Tstruct);
for (size_t i = 0; i < e->elements->length; i++)
{
Expression *exp = (*e->elements)[i];
if (!exp)
continue;
VarDeclaration *field = e->sd->fields[i];
Type *type = exp->type->toBasetype ();
Type *ftype = field->type->toBasetype ();
tree value = NULL_TREE;
if (ftype->ty == Tsarray && !same_type_p (type, ftype))
{
/* Initialize a static array with a single element. */
tree elem = build_expr (exp, this->constp_, true);
saved_elems = compound_expr (saved_elems, stabilize_expr (&elem));
elem = d_save_expr (elem);
if (initializer_zerop (elem))
value = build_constructor (build_ctype (ftype), NULL);
else
value = build_array_from_val (ftype, elem);
}
else
{
value = convert_expr (build_expr (exp, this->constp_, true),
exp->type, field->type);
}
/* Split construction of values out of the constructor. */
saved_elems = compound_expr (saved_elems, stabilize_expr (&value));
CONSTRUCTOR_APPEND_ELT (ve, get_symbol_decl (field), value);
}
/* Maybe setup hidden pointer to outer scope context. */
if (e->sd->isNested () && e->elements->length != e->sd->fields.length
&& this->constp_ == false)
{
tree field = get_symbol_decl (e->sd->vthis);
tree value = build_vthis (e->sd);
CONSTRUCTOR_APPEND_ELT (ve, field, value);
gcc_assert (e->useStaticInit == false);
}
/* Build a constructor in the correct shape of the aggregate type. */
tree ctor = build_struct_literal (build_ctype (e->type), ve);
/* Nothing more to do for constant literals. */
if (this->constp_)
{
/* If the struct literal is a valid for static data. */
if (TREE_CONSTANT (ctor)
&& initializer_constant_valid_p (ctor, TREE_TYPE (ctor)))
TREE_STATIC (ctor) = 1;
this->result_ = compound_expr (saved_elems, ctor);
return;
}
/* Construct the struct literal for run-time. */
if (e->sym != NULL)
{
/* Store the result in a symbol to initialize the literal. */
tree var = build_deref (e->sym);
ctor = compound_expr (modify_expr (var, ctor), var);
}
else if (!this->literalp_)
{
/* Use memset to fill any alignment holes in the object. */
if (!identity_compare_p (e->sd) || e->sd->isUnionDeclaration ())
{
tree var = build_local_temp (TREE_TYPE (ctor));
tree init = build_memset_call (var);
/* Evaluate memset() first, then any saved element constructors. */
saved_elems = compound_expr (init, saved_elems);
ctor = compound_expr (modify_expr (var, ctor), var);
}
}
this->result_ = compound_expr (saved_elems, ctor);
}
/* Build a null literal. */
void visit (NullExp *e)
{
this->result_ = build_typeof_null_value (e->type);
}
/* Build a vector literal. */
void visit (VectorExp *e)
{
tree type = build_ctype (e->type);
/* First handle array literal expressions. */
if (e->e1->op == TOKarrayliteral)
{
ArrayLiteralExp *ale = e->e1->isArrayLiteralExp ();
vec <constructor_elt, va_gc> *elms = NULL;
bool constant_p = true;
vec_safe_reserve (elms, ale->elements->length);
for (size_t i = 0; i < ale->elements->length; i++)
{
Expression *expr = ale->getElement (i);
tree value = d_convert (TREE_TYPE (type),
build_expr (expr, this->constp_, true));
if (!CONSTANT_CLASS_P (value))
constant_p = false;
CONSTRUCTOR_APPEND_ELT (elms, size_int (i), value);
}
/* Build a VECTOR_CST from a constant vector constructor. */
if (constant_p)
this->result_ = build_vector_from_ctor (type, elms);
else
this->result_ = build_constructor (type, elms);
}
else
{
/* Build constructor from single value. */
tree value = d_convert (TREE_TYPE (type),
build_expr (e->e1, this->constp_, true));
this->result_ = build_vector_from_val (type, value);
}
}
/* Build a static array representation of a vector expression. */
void visit (VectorArrayExp *e)
{
this->result_ = convert_expr (build_expr (e->e1, this->constp_, true),
e->e1->type, e->type);
}
/* Build a static class literal, return its reference. */
void visit (ClassReferenceExp *e)
{
/* The result of build_new_class_expr is a RECORD_TYPE, we want
the reference. */
tree var = build_address (build_new_class_expr (e));
/* If the type of this literal is an interface, the we must add the
interface offset to symbol. */
if (this->constp_)
{
TypeClass *tc = e->type->toBasetype ()->isTypeClass ();
InterfaceDeclaration *to = tc->sym->isInterfaceDeclaration ();
if (to != NULL)
{
ClassDeclaration *from = e->originalClass ();
int offset = 0;
gcc_assert (to->isBaseOf (from, &offset) != 0);
if (offset != 0)
var = build_offset (var, size_int (offset));
}
}
this->result_ = var;
}
/* These expressions are mainly just a placeholders in the frontend.
We shouldn't see them here. */
void visit (ScopeExp *e)
{
error_at (make_location_t (e->loc), "%qs is not an expression",
e->toChars ());
this->result_ = error_mark_node;
}
void visit (TypeExp *e)
{
error_at (make_location_t (e->loc), "type %qs is not an expression",
e->toChars ());
this->result_ = error_mark_node;
}
};
/* Main entry point for ExprVisitor interface to generate code for
the Expression AST class E. If CONST_P is true, then E is a
constant expression. If LITERAL_P is true, then E is a value used
in the initialization of another literal. */
tree
build_expr (Expression *e, bool const_p, bool literal_p)
{
ExprVisitor v = ExprVisitor (const_p, literal_p);
location_t saved_location = input_location;
input_location = make_location_t (e->loc);
e->accept (&v);
tree expr = v.result ();
input_location = saved_location;
/* Check if initializer expression is valid constant. */
if (const_p && !initializer_constant_valid_p (expr, TREE_TYPE (expr)))
{
error_at (make_location_t (e->loc), "non-constant expression %qs",
e->toChars ());
return error_mark_node;
}
return expr;
}
/* Same as build_expr, but also calls destructors on any temporaries. */
tree
build_expr_dtor (Expression *e)
{
/* Codegen can be improved by determining if no exceptions can be thrown
between the ctor and dtor, and eliminating the ctor and dtor. */
size_t saved_vars = vec_safe_length (d_function_chain->vars_in_scope);
tree result = build_expr (e);
if (saved_vars != vec_safe_length (d_function_chain->vars_in_scope))
{
result = fold_build_cleanup_point_expr (TREE_TYPE (result), result);
vec_safe_truncate (d_function_chain->vars_in_scope, saved_vars);
}
return result;
}
/* Same as build_expr_dtor, but handles the result of E as a return value. */
tree
build_return_dtor (Expression *e, Type *type, TypeFunction *tf)
{
size_t saved_vars = vec_safe_length (d_function_chain->vars_in_scope);
tree result = build_expr (e);
/* Convert for initializing the DECL_RESULT. */
if (tf->isref)
{
/* If we are returning a reference, take the address. */
result = convert_expr (result, e->type, type);
result = build_address (result);
}
else
result = convert_for_rvalue (result, e->type, type);
/* The decl to store the return expression. */
tree decl = DECL_RESULT (cfun->decl);
/* Split comma expressions, so that the result is returned directly. */
tree expr = stabilize_expr (&result);
result = build_assign (INIT_EXPR, decl, result);
result = compound_expr (expr, return_expr (result));
/* May nest the return expression inside the try/finally expression. */
if (saved_vars != vec_safe_length (d_function_chain->vars_in_scope))
{
result = fold_build_cleanup_point_expr (TREE_TYPE (result), result);
vec_safe_truncate (d_function_chain->vars_in_scope, saved_vars);
}
return result;
}
|