1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
8238
8239
8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8306
8307
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
8322
8323
8324
8325
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
8357
8358
8359
8360
8361
8362
8363
8364
8365
8366
8367
8368
8369
8370
8371
8372
8373
8374
8375
8376
8377
8378
8379
8380
8381
8382
8383
8384
8385
8386
8387
8388
8389
8390
8391
8392
8393
8394
8395
8396
8397
8398
8399
8400
8401
8402
8403
8404
8405
8406
8407
8408
8409
8410
8411
8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
8435
8436
8437
8438
8439
8440
8441
8442
8443
8444
8445
8446
8447
8448
8449
8450
8451
8452
8453
8454
8455
8456
8457
8458
8459
8460
8461
8462
8463
8464
8465
8466
8467
8468
8469
8470
8471
8472
8473
8474
8475
8476
8477
8478
8479
8480
8481
8482
8483
8484
8485
8486
8487
8488
8489
8490
8491
8492
8493
8494
8495
8496
8497
8498
8499
8500
8501
8502
8503
8504
8505
8506
8507
8508
8509
8510
8511
8512
8513
8514
8515
8516
8517
8518
8519
8520
8521
8522
8523
8524
8525
8526
8527
8528
8529
8530
8531
8532
8533
8534
8535
8536
8537
8538
8539
8540
8541
8542
8543
8544
8545
8546
8547
8548
8549
8550
8551
8552
8553
8554
8555
8556
8557
8558
8559
8560
8561
8562
8563
8564
8565
8566
8567
8568
8569
8570
8571
8572
8573
8574
8575
8576
8577
8578
8579
8580
8581
8582
8583
8584
8585
8586
8587
8588
8589
8590
8591
8592
8593
8594
8595
8596
8597
8598
8599
8600
8601
8602
8603
8604
8605
8606
8607
8608
8609
8610
8611
8612
8613
8614
8615
8616
8617
8618
8619
8620
8621
8622
8623
8624
8625
8626
8627
8628
8629
8630
8631
8632
8633
8634
8635
8636
8637
8638
8639
8640
8641
8642
8643
8644
8645
8646
8647
8648
8649
8650
8651
8652
8653
8654
8655
8656
8657
8658
8659
8660
8661
8662
8663
8664
8665
8666
8667
8668
8669
8670
8671
8672
8673
8674
8675
8676
8677
8678
8679
8680
8681
8682
8683
8684
8685
8686
8687
8688
8689
8690
8691
8692
8693
8694
8695
8696
8697
8698
8699
8700
8701
8702
8703
8704
8705
8706
8707
8708
8709
8710
8711
8712
8713
8714
8715
8716
8717
8718
8719
8720
8721
8722
8723
8724
8725
8726
8727
8728
8729
8730
8731
8732
8733
8734
8735
8736
8737
8738
8739
8740
8741
8742
8743
8744
8745
8746
8747
8748
8749
8750
8751
8752
8753
8754
8755
8756
8757
8758
8759
8760
8761
8762
8763
8764
8765
8766
8767
8768
8769
8770
8771
8772
8773
8774
8775
8776
8777
8778
8779
8780
8781
8782
8783
8784
8785
8786
8787
8788
8789
8790
8791
8792
8793
8794
8795
8796
8797
8798
8799
8800
8801
8802
8803
8804
8805
8806
8807
8808
8809
8810
8811
8812
8813
8814
8815
8816
8817
8818
8819
8820
8821
8822
8823
8824
8825
8826
8827
8828
8829
8830
8831
8832
8833
8834
8835
8836
8837
8838
8839
8840
8841
8842
8843
8844
8845
8846
8847
8848
8849
8850
8851
8852
8853
8854
8855
8856
8857
8858
8859
8860
8861
8862
8863
8864
8865
8866
8867
8868
8869
8870
8871
8872
8873
8874
8875
8876
8877
8878
8879
8880
8881
8882
8883
8884
8885
8886
8887
8888
8889
8890
8891
8892
8893
8894
8895
8896
8897
8898
8899
8900
8901
8902
8903
8904
8905
8906
8907
8908
8909
8910
8911
8912
8913
8914
8915
8916
8917
8918
8919
8920
8921
8922
8923
8924
8925
8926
8927
8928
8929
8930
8931
8932
8933
8934
8935
8936
8937
8938
8939
8940
8941
8942
8943
8944
8945
8946
8947
8948
8949
8950
8951
8952
8953
8954
8955
8956
8957
8958
8959
8960
8961
8962
8963
8964
8965
8966
8967
8968
8969
8970
8971
8972
8973
8974
8975
8976
8977
8978
8979
8980
8981
8982
8983
8984
8985
8986
8987
8988
8989
8990
8991
8992
8993
8994
8995
8996
8997
8998
8999
9000
9001
9002
9003
9004
9005
9006
9007
9008
9009
9010
9011
9012
9013
9014
9015
9016
9017
9018
9019
9020
9021
9022
9023
9024
9025
9026
9027
9028
9029
9030
9031
9032
9033
9034
9035
9036
9037
9038
9039
9040
9041
9042
9043
9044
9045
9046
9047
9048
9049
9050
9051
9052
9053
9054
9055
9056
9057
9058
9059
9060
9061
9062
9063
9064
9065
9066
9067
9068
9069
9070
9071
9072
9073
9074
9075
9076
9077
9078
9079
9080
9081
9082
9083
9084
9085
9086
9087
9088
9089
9090
9091
9092
9093
9094
9095
9096
9097
9098
9099
9100
9101
9102
9103
9104
9105
9106
9107
9108
9109
9110
9111
9112
9113
9114
9115
9116
9117
9118
9119
9120
9121
9122
9123
9124
9125
9126
9127
9128
9129
9130
9131
9132
9133
9134
9135
9136
9137
9138
9139
9140
9141
9142
9143
9144
9145
9146
9147
9148
9149
9150
9151
9152
9153
9154
9155
9156
9157
9158
9159
9160
9161
9162
9163
9164
9165
9166
9167
9168
9169
9170
9171
9172
9173
9174
9175
9176
9177
9178
9179
9180
9181
9182
9183
9184
9185
9186
9187
9188
9189
9190
9191
9192
9193
9194
9195
9196
9197
9198
9199
9200
9201
9202
9203
9204
9205
9206
9207
9208
9209
9210
9211
9212
9213
9214
9215
9216
9217
9218
9219
9220
9221
9222
9223
9224
9225
9226
9227
9228
9229
9230
9231
9232
9233
9234
9235
9236
9237
9238
9239
9240
9241
9242
9243
9244
9245
9246
9247
9248
9249
9250
9251
9252
9253
9254
9255
9256
9257
9258
9259
9260
9261
9262
9263
9264
9265
9266
9267
9268
9269
9270
9271
9272
9273
9274
9275
9276
9277
9278
9279
9280
9281
9282
9283
9284
9285
9286
9287
9288
9289
9290
9291
9292
9293
9294
9295
9296
9297
9298
9299
9300
9301
9302
9303
9304
9305
9306
9307
9308
9309
9310
9311
9312
9313
9314
9315
9316
9317
9318
9319
9320
9321
9322
9323
9324
9325
9326
9327
9328
9329
9330
9331
9332
9333
9334
9335
9336
9337
9338
9339
9340
9341
9342
9343
9344
9345
9346
9347
9348
9349
9350
9351
9352
9353
9354
9355
9356
9357
9358
9359
9360
9361
9362
9363
9364
9365
9366
9367
9368
9369
9370
9371
9372
9373
9374
9375
9376
9377
9378
9379
9380
9381
9382
9383
9384
9385
9386
9387
9388
9389
9390
9391
9392
9393
9394
9395
9396
9397
9398
9399
9400
9401
9402
9403
9404
9405
9406
9407
9408
9409
9410
9411
9412
9413
9414
9415
9416
9417
9418
9419
9420
9421
9422
9423
9424
9425
9426
9427
9428
9429
9430
9431
9432
9433
9434
9435
9436
9437
9438
9439
9440
9441
9442
9443
9444
9445
9446
9447
9448
9449
9450
9451
9452
9453
9454
9455
9456
9457
9458
9459
9460
9461
9462
9463
9464
9465
9466
9467
9468
9469
9470
9471
9472
9473
9474
9475
9476
9477
9478
9479
9480
9481
9482
9483
9484
9485
9486
9487
9488
9489
9490
9491
9492
9493
9494
9495
9496
9497
9498
9499
9500
9501
9502
9503
9504
9505
9506
9507
9508
9509
9510
9511
9512
9513
9514
9515
9516
9517
9518
9519
9520
9521
9522
9523
9524
9525
9526
9527
9528
9529
9530
9531
9532
9533
9534
9535
9536
9537
9538
9539
9540
9541
9542
9543
9544
9545
9546
9547
9548
9549
9550
9551
9552
9553
9554
9555
9556
9557
9558
9559
9560
9561
9562
9563
9564
9565
9566
9567
9568
9569
9570
9571
9572
9573
9574
9575
9576
9577
9578
9579
9580
9581
9582
9583
9584
9585
9586
9587
9588
9589
9590
9591
9592
9593
9594
9595
9596
9597
9598
9599
9600
9601
9602
9603
9604
9605
9606
9607
9608
9609
9610
9611
9612
9613
9614
9615
9616
9617
9618
9619
9620
9621
9622
9623
9624
9625
9626
9627
9628
9629
9630
9631
9632
9633
9634
9635
9636
9637
9638
9639
9640
9641
9642
9643
9644
9645
9646
9647
9648
9649
9650
9651
9652
9653
9654
9655
9656
9657
9658
9659
9660
9661
9662
9663
9664
9665
9666
9667
9668
9669
9670
9671
9672
9673
9674
9675
9676
9677
9678
9679
9680
9681
9682
9683
9684
9685
9686
9687
9688
9689
9690
9691
9692
9693
9694
9695
9696
9697
9698
9699
9700
9701
9702
9703
9704
9705
9706
9707
9708
9709
9710
9711
9712
9713
9714
9715
9716
9717
9718
9719
9720
9721
9722
9723
9724
9725
9726
9727
9728
9729
9730
9731
9732
9733
9734
9735
9736
9737
9738
9739
9740
9741
9742
9743
9744
9745
9746
9747
9748
9749
9750
9751
9752
9753
9754
9755
9756
9757
9758
9759
9760
9761
9762
9763
9764
9765
9766
9767
9768
9769
9770
9771
9772
9773
9774
9775
9776
9777
9778
9779
9780
9781
9782
9783
9784
9785
9786
9787
9788
9789
9790
9791
9792
9793
9794
9795
9796
9797
9798
9799
9800
9801
9802
9803
9804
9805
9806
9807
9808
9809
9810
9811
9812
9813
9814
9815
9816
9817
9818
9819
9820
9821
9822
9823
9824
9825
9826
9827
9828
9829
9830
9831
9832
9833
9834
9835
9836
9837
9838
9839
9840
9841
9842
9843
9844
9845
9846
9847
9848
9849
9850
9851
9852
9853
9854
9855
9856
9857
9858
9859
9860
9861
9862
9863
9864
9865
9866
9867
9868
9869
9870
9871
9872
9873
9874
9875
9876
9877
9878
9879
9880
9881
9882
9883
9884
9885
9886
9887
9888
9889
9890
9891
9892
9893
9894
9895
9896
9897
9898
9899
9900
9901
9902
9903
9904
9905
9906
9907
9908
9909
9910
9911
9912
9913
9914
9915
9916
9917
9918
9919
9920
9921
9922
9923
9924
9925
9926
9927
9928
9929
9930
9931
9932
9933
9934
9935
9936
9937
9938
9939
9940
9941
9942
9943
9944
9945
9946
9947
9948
9949
9950
9951
9952
9953
9954
9955
9956
9957
9958
9959
9960
9961
9962
9963
9964
9965
9966
9967
9968
9969
9970
9971
9972
9973
9974
9975
9976
9977
9978
9979
9980
9981
9982
9983
9984
9985
9986
9987
9988
9989
9990
9991
9992
9993
9994
9995
9996
9997
9998
9999
10000
10001
10002
10003
10004
10005
10006
10007
10008
10009
10010
10011
10012
10013
10014
10015
10016
10017
10018
10019
10020
10021
10022
10023
10024
10025
10026
10027
10028
10029
10030
10031
10032
10033
10034
10035
10036
10037
10038
10039
10040
10041
10042
10043
10044
10045
10046
10047
10048
10049
10050
10051
10052
10053
10054
10055
10056
10057
10058
10059
10060
10061
10062
10063
10064
10065
10066
10067
10068
10069
10070
10071
10072
10073
10074
10075
10076
10077
10078
10079
10080
10081
10082
10083
10084
10085
10086
10087
10088
10089
10090
10091
10092
10093
10094
10095
10096
10097
10098
10099
10100
10101
10102
10103
10104
10105
10106
10107
10108
10109
10110
10111
10112
10113
10114
10115
10116
10117
10118
10119
10120
10121
10122
10123
10124
10125
10126
10127
10128
10129
10130
10131
10132
10133
10134
10135
10136
10137
10138
10139
10140
10141
10142
10143
10144
10145
10146
10147
10148
10149
10150
10151
10152
10153
10154
10155
10156
10157
10158
10159
10160
10161
10162
10163
10164
10165
10166
10167
10168
10169
10170
10171
10172
10173
10174
10175
10176
10177
10178
10179
10180
10181
10182
10183
10184
10185
10186
10187
10188
10189
10190
10191
10192
10193
10194
10195
10196
10197
10198
10199
10200
10201
10202
10203
10204
10205
10206
10207
10208
10209
10210
10211
10212
10213
10214
10215
10216
10217
10218
10219
10220
10221
10222
10223
10224
10225
10226
10227
10228
10229
10230
10231
10232
10233
10234
10235
10236
10237
10238
10239
10240
10241
10242
10243
10244
10245
10246
10247
10248
10249
10250
10251
10252
10253
10254
10255
10256
10257
10258
10259
10260
10261
10262
10263
10264
10265
10266
10267
10268
10269
10270
10271
10272
10273
10274
10275
10276
10277
10278
10279
10280
10281
10282
10283
10284
10285
10286
10287
10288
10289
10290
10291
10292
10293
10294
10295
10296
10297
10298
10299
10300
10301
10302
10303
10304
10305
10306
10307
10308
10309
10310
10311
10312
10313
10314
10315
10316
10317
10318
10319
10320
10321
10322
10323
10324
10325
10326
10327
10328
10329
10330
10331
10332
10333
10334
10335
10336
10337
10338
10339
10340
10341
10342
10343
10344
10345
10346
10347
10348
10349
10350
10351
10352
10353
10354
10355
10356
10357
10358
10359
10360
10361
10362
10363
10364
10365
10366
10367
10368
10369
10370
10371
10372
10373
10374
10375
10376
10377
10378
10379
10380
10381
10382
10383
10384
10385
10386
10387
10388
10389
10390
10391
10392
10393
10394
10395
10396
10397
10398
10399
10400
10401
10402
10403
10404
10405
10406
10407
10408
10409
10410
10411
10412
10413
10414
10415
10416
10417
10418
10419
10420
10421
10422
10423
10424
10425
10426
10427
10428
10429
10430
10431
10432
10433
10434
10435
10436
10437
10438
10439
10440
10441
10442
10443
10444
10445
10446
10447
10448
10449
10450
10451
10452
10453
10454
10455
10456
10457
10458
10459
10460
10461
10462
10463
10464
10465
10466
10467
10468
10469
10470
10471
10472
10473
10474
10475
10476
10477
10478
10479
10480
10481
10482
10483
10484
10485
10486
10487
10488
10489
10490
10491
10492
10493
10494
10495
10496
10497
10498
10499
10500
10501
10502
10503
10504
10505
10506
10507
10508
10509
10510
10511
10512
10513
10514
10515
10516
10517
10518
10519
10520
10521
10522
10523
10524
10525
10526
10527
10528
10529
10530
10531
10532
10533
10534
10535
10536
10537
10538
10539
10540
10541
10542
10543
10544
10545
10546
10547
10548
10549
10550
10551
10552
10553
10554
10555
10556
10557
10558
10559
10560
10561
10562
10563
10564
10565
10566
10567
10568
10569
10570
10571
10572
10573
10574
10575
10576
10577
10578
10579
10580
10581
10582
10583
10584
10585
10586
10587
10588
10589
10590
10591
10592
10593
10594
10595
10596
10597
10598
10599
10600
10601
10602
10603
10604
10605
10606
10607
10608
10609
10610
10611
10612
10613
10614
10615
10616
10617
10618
10619
10620
10621
10622
10623
10624
10625
10626
10627
10628
10629
10630
10631
10632
10633
10634
10635
10636
10637
10638
10639
10640
10641
10642
10643
10644
10645
10646
10647
10648
10649
10650
10651
10652
10653
10654
10655
10656
10657
10658
10659
10660
10661
10662
10663
10664
10665
10666
10667
10668
10669
10670
10671
10672
10673
10674
10675
10676
10677
10678
10679
10680
10681
10682
10683
10684
10685
10686
10687
10688
10689
10690
10691
10692
10693
10694
10695
10696
10697
10698
10699
10700
10701
10702
10703
10704
10705
10706
10707
10708
10709
10710
10711
10712
10713
10714
10715
10716
10717
10718
10719
10720
10721
10722
10723
10724
10725
10726
10727
10728
10729
10730
10731
10732
10733
10734
10735
10736
10737
10738
10739
10740
10741
10742
10743
10744
10745
10746
10747
10748
10749
10750
10751
10752
10753
10754
10755
10756
10757
10758
10759
10760
10761
10762
10763
10764
10765
10766
10767
10768
10769
10770
10771
10772
10773
10774
10775
10776
10777
10778
10779
10780
10781
10782
10783
10784
10785
10786
10787
10788
10789
10790
10791
10792
10793
10794
10795
10796
10797
10798
10799
10800
10801
10802
10803
10804
10805
10806
10807
10808
10809
10810
10811
10812
10813
10814
10815
10816
10817
10818
10819
10820
10821
10822
10823
10824
10825
10826
10827
10828
10829
10830
10831
10832
10833
10834
10835
10836
10837
10838
10839
10840
10841
10842
10843
10844
10845
10846
10847
10848
10849
10850
10851
10852
10853
10854
10855
10856
10857
10858
10859
10860
10861
10862
10863
10864
10865
10866
10867
10868
10869
10870
10871
10872
10873
10874
10875
10876
10877
10878
10879
10880
10881
10882
10883
10884
10885
10886
10887
10888
10889
10890
10891
10892
10893
10894
10895
10896
10897
10898
10899
10900
10901
10902
10903
10904
10905
10906
10907
10908
10909
10910
10911
10912
10913
10914
10915
10916
10917
10918
10919
10920
10921
10922
10923
10924
10925
10926
10927
10928
10929
10930
10931
10932
10933
10934
10935
10936
10937
10938
10939
10940
10941
10942
10943
10944
10945
10946
10947
10948
10949
10950
10951
10952
10953
10954
10955
10956
10957
10958
10959
10960
10961
10962
10963
10964
10965
10966
10967
10968
10969
10970
10971
10972
10973
10974
10975
10976
10977
10978
10979
10980
10981
10982
10983
10984
10985
10986
10987
10988
10989
10990
10991
10992
10993
10994
10995
10996
10997
10998
10999
11000
11001
11002
11003
11004
11005
11006
11007
11008
11009
11010
11011
11012
11013
11014
11015
11016
11017
11018
11019
11020
11021
11022
11023
11024
11025
11026
11027
11028
11029
11030
11031
11032
11033
11034
11035
11036
11037
11038
11039
11040
11041
11042
11043
11044
11045
11046
11047
11048
11049
11050
11051
11052
11053
11054
11055
11056
11057
11058
11059
11060
11061
11062
11063
11064
11065
11066
11067
11068
11069
11070
11071
11072
11073
11074
11075
11076
11077
11078
11079
11080
11081
11082
11083
11084
11085
11086
11087
11088
11089
11090
11091
11092
11093
11094
11095
11096
11097
11098
11099
11100
11101
11102
11103
11104
11105
11106
11107
11108
11109
11110
11111
11112
11113
11114
11115
11116
11117
11118
11119
11120
11121
11122
11123
11124
11125
11126
11127
11128
11129
11130
11131
11132
11133
11134
11135
11136
11137
11138
11139
11140
11141
11142
11143
11144
11145
11146
11147
11148
11149
11150
11151
11152
11153
11154
11155
11156
11157
11158
11159
11160
11161
11162
11163
11164
11165
11166
11167
11168
11169
11170
11171
11172
11173
11174
11175
11176
11177
11178
11179
11180
11181
11182
11183
11184
11185
11186
11187
11188
11189
11190
11191
11192
11193
11194
11195
11196
11197
11198
11199
11200
11201
11202
11203
11204
11205
11206
11207
11208
11209
11210
11211
11212
11213
11214
11215
11216
11217
11218
11219
11220
11221
11222
11223
11224
11225
11226
11227
11228
11229
11230
11231
11232
11233
11234
11235
11236
11237
11238
11239
11240
11241
11242
11243
11244
11245
11246
11247
11248
11249
11250
11251
11252
11253
11254
11255
11256
11257
11258
11259
11260
11261
11262
11263
11264
11265
11266
11267
11268
11269
11270
11271
11272
11273
11274
11275
11276
11277
11278
11279
11280
11281
11282
11283
11284
11285
11286
11287
11288
11289
11290
11291
11292
11293
11294
11295
11296
11297
11298
11299
11300
11301
11302
11303
11304
11305
11306
11307
11308
11309
11310
11311
11312
11313
11314
11315
11316
11317
11318
11319
11320
11321
11322
11323
11324
11325
11326
11327
11328
11329
11330
11331
11332
11333
11334
11335
11336
11337
11338
11339
11340
11341
11342
11343
11344
11345
11346
11347
11348
11349
11350
11351
11352
11353
11354
11355
11356
11357
11358
11359
11360
11361
11362
11363
11364
11365
11366
11367
11368
11369
11370
11371
11372
11373
11374
11375
11376
11377
11378
11379
11380
11381
11382
11383
11384
11385
11386
11387
11388
11389
11390
11391
11392
11393
11394
11395
11396
11397
11398
11399
11400
11401
11402
11403
11404
11405
11406
11407
11408
11409
11410
11411
11412
11413
11414
11415
11416
11417
11418
11419
11420
11421
11422
11423
11424
11425
11426
11427
11428
11429
11430
11431
11432
11433
11434
11435
11436
11437
11438
11439
11440
11441
11442
11443
11444
11445
11446
11447
11448
11449
11450
11451
11452
11453
11454
11455
11456
11457
11458
11459
11460
11461
11462
11463
11464
11465
11466
11467
11468
11469
11470
11471
11472
11473
11474
11475
11476
11477
11478
11479
11480
11481
11482
11483
11484
11485
11486
11487
11488
11489
11490
11491
11492
11493
11494
11495
11496
11497
11498
11499
11500
11501
11502
11503
11504
11505
11506
11507
11508
11509
11510
11511
11512
11513
11514
11515
11516
11517
11518
11519
11520
11521
11522
11523
11524
11525
11526
11527
11528
11529
11530
11531
11532
11533
11534
11535
11536
11537
11538
11539
11540
11541
11542
11543
11544
11545
11546
11547
11548
11549
11550
11551
11552
11553
11554
11555
11556
11557
11558
11559
11560
11561
11562
11563
11564
11565
11566
11567
11568
11569
11570
11571
11572
11573
11574
11575
11576
11577
11578
11579
11580
11581
11582
11583
11584
11585
11586
11587
11588
11589
11590
11591
11592
11593
11594
11595
11596
11597
11598
11599
11600
11601
11602
11603
11604
11605
11606
11607
11608
11609
11610
11611
11612
11613
11614
11615
11616
11617
11618
11619
11620
11621
11622
11623
11624
11625
11626
11627
11628
11629
11630
11631
11632
11633
11634
11635
11636
11637
11638
11639
11640
11641
11642
11643
11644
11645
11646
11647
11648
11649
11650
11651
11652
11653
11654
11655
11656
11657
11658
11659
11660
11661
11662
11663
11664
11665
11666
11667
11668
11669
11670
11671
11672
11673
11674
11675
11676
11677
11678
11679
11680
11681
11682
11683
11684
11685
11686
11687
11688
11689
11690
11691
11692
11693
11694
11695
11696
11697
11698
11699
11700
11701
11702
11703
11704
11705
11706
11707
11708
11709
11710
11711
11712
11713
11714
11715
11716
11717
11718
11719
11720
11721
11722
11723
11724
11725
11726
11727
11728
11729
11730
11731
11732
11733
11734
11735
11736
11737
11738
11739
11740
11741
11742
11743
11744
11745
11746
11747
11748
11749
11750
11751
11752
11753
11754
11755
11756
11757
11758
11759
11760
11761
11762
11763
11764
11765
11766
11767
11768
11769
11770
11771
11772
11773
11774
11775
11776
11777
11778
11779
11780
11781
11782
11783
11784
11785
11786
11787
11788
11789
11790
11791
11792
11793
11794
11795
11796
11797
11798
11799
11800
11801
11802
11803
11804
11805
11806
11807
11808
11809
11810
11811
11812
11813
11814
11815
11816
11817
11818
11819
11820
11821
11822
11823
11824
11825
11826
11827
11828
11829
11830
11831
11832
11833
11834
11835
11836
11837
11838
11839
11840
11841
11842
11843
11844
11845
11846
11847
11848
11849
11850
11851
11852
11853
11854
11855
11856
11857
11858
11859
11860
11861
11862
11863
11864
11865
11866
11867
11868
11869
11870
11871
11872
11873
11874
11875
11876
11877
11878
11879
11880
11881
11882
11883
11884
11885
11886
11887
11888
11889
11890
11891
11892
11893
11894
11895
11896
11897
11898
11899
11900
11901
11902
11903
11904
11905
11906
11907
11908
11909
11910
11911
11912
11913
11914
11915
11916
11917
11918
11919
11920
11921
11922
11923
11924
11925
11926
11927
11928
11929
11930
11931
11932
11933
11934
11935
11936
11937
|
/* Build expressions with type checking for C++ compiler.
Copyright (C) 1987-2023 Free Software Foundation, Inc.
Hacked by Michael Tiemann (tiemann@cygnus.com)
This file is part of GCC.
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/>. */
/* This file is part of the C++ front end.
It contains routines to build C++ expressions given their operands,
including computing the types of the result, C and C++ specific error
checks, and some optimization. */
#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "target.h"
#include "cp-tree.h"
#include "stor-layout.h"
#include "varasm.h"
#include "intl.h"
#include "convert.h"
#include "c-family/c-objc.h"
#include "c-family/c-ubsan.h"
#include "gcc-rich-location.h"
#include "stringpool.h"
#include "attribs.h"
#include "asan.h"
#include "gimplify.h"
static tree cp_build_addr_expr_strict (tree, tsubst_flags_t);
static tree cp_build_function_call (tree, tree, tsubst_flags_t);
static tree pfn_from_ptrmemfunc (tree);
static tree delta_from_ptrmemfunc (tree);
static tree convert_for_assignment (tree, tree, impl_conv_rhs, tree, int,
tsubst_flags_t, int);
static tree cp_pointer_int_sum (location_t, enum tree_code, tree, tree,
tsubst_flags_t);
static tree rationalize_conditional_expr (enum tree_code, tree,
tsubst_flags_t);
static bool comp_ptr_ttypes_real (tree, tree, int);
static bool comp_except_types (tree, tree, bool);
static bool comp_array_types (const_tree, const_tree, compare_bounds_t, bool);
static tree pointer_diff (location_t, tree, tree, tree, tsubst_flags_t, tree *);
static tree get_delta_difference (tree, tree, bool, bool, tsubst_flags_t);
static void casts_away_constness_r (tree *, tree *, tsubst_flags_t);
static bool casts_away_constness (tree, tree, tsubst_flags_t);
static bool maybe_warn_about_returning_address_of_local (tree, location_t = UNKNOWN_LOCATION);
static void error_args_num (location_t, tree, bool);
static int convert_arguments (tree, vec<tree, va_gc> **, tree, int,
tsubst_flags_t);
static bool is_std_move_p (tree);
static bool is_std_forward_p (tree);
/* Do `exp = require_complete_type (exp);' to make sure exp
does not have an incomplete type. (That includes void types.)
Returns error_mark_node if the VALUE does not have
complete type when this function returns. */
tree
require_complete_type (tree value,
tsubst_flags_t complain /* = tf_warning_or_error */)
{
tree type;
if (processing_template_decl || value == error_mark_node)
return value;
if (TREE_CODE (value) == OVERLOAD)
type = unknown_type_node;
else
type = TREE_TYPE (value);
if (type == error_mark_node)
return error_mark_node;
/* First, detect a valid value with a complete type. */
if (COMPLETE_TYPE_P (type))
return value;
if (complete_type_or_maybe_complain (type, value, complain))
return value;
else
return error_mark_node;
}
/* Try to complete TYPE, if it is incomplete. For example, if TYPE is
a template instantiation, do the instantiation. Returns TYPE,
whether or not it could be completed, unless something goes
horribly wrong, in which case the error_mark_node is returned. */
tree
complete_type (tree type)
{
if (type == NULL_TREE)
/* Rather than crash, we return something sure to cause an error
at some point. */
return error_mark_node;
if (type == error_mark_node || COMPLETE_TYPE_P (type))
;
else if (TREE_CODE (type) == ARRAY_TYPE)
{
tree t = complete_type (TREE_TYPE (type));
unsigned int needs_constructing, has_nontrivial_dtor;
if (COMPLETE_TYPE_P (t) && !dependent_type_p (type))
layout_type (type);
needs_constructing
= TYPE_NEEDS_CONSTRUCTING (TYPE_MAIN_VARIANT (t));
has_nontrivial_dtor
= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TYPE_MAIN_VARIANT (t));
for (t = TYPE_MAIN_VARIANT (type); t; t = TYPE_NEXT_VARIANT (t))
{
TYPE_NEEDS_CONSTRUCTING (t) = needs_constructing;
TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = has_nontrivial_dtor;
}
}
else if (CLASS_TYPE_P (type))
{
if (modules_p ())
/* TYPE could be a class member we've not loaded the definition of. */
lazy_load_pendings (TYPE_NAME (TYPE_MAIN_VARIANT (type)));
if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
instantiate_class_template (TYPE_MAIN_VARIANT (type));
}
return type;
}
/* Like complete_type, but issue an error if the TYPE cannot be completed.
VALUE is used for informative diagnostics.
Returns NULL_TREE if the type cannot be made complete. */
tree
complete_type_or_maybe_complain (tree type, tree value, tsubst_flags_t complain)
{
type = complete_type (type);
if (type == error_mark_node)
/* We already issued an error. */
return NULL_TREE;
else if (!COMPLETE_TYPE_P (type))
{
if (complain & tf_error)
cxx_incomplete_type_diagnostic (value, type, DK_ERROR);
note_failed_type_completion_for_satisfaction (type);
return NULL_TREE;
}
else
return type;
}
tree
complete_type_or_else (tree type, tree value)
{
return complete_type_or_maybe_complain (type, value, tf_warning_or_error);
}
/* Return the common type of two parameter lists.
We assume that comptypes has already been done and returned 1;
if that isn't so, this may crash.
As an optimization, free the space we allocate if the parameter
lists are already common. */
static tree
commonparms (tree p1, tree p2)
{
tree oldargs = p1, newargs, n;
int i, len;
int any_change = 0;
len = list_length (p1);
newargs = tree_last (p1);
if (newargs == void_list_node)
i = 1;
else
{
i = 0;
newargs = 0;
}
for (; i < len; i++)
newargs = tree_cons (NULL_TREE, NULL_TREE, newargs);
n = newargs;
for (i = 0; p1;
p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2), n = TREE_CHAIN (n), i++)
{
if (TREE_PURPOSE (p1) && !TREE_PURPOSE (p2))
{
TREE_PURPOSE (n) = TREE_PURPOSE (p1);
any_change = 1;
}
else if (! TREE_PURPOSE (p1))
{
if (TREE_PURPOSE (p2))
{
TREE_PURPOSE (n) = TREE_PURPOSE (p2);
any_change = 1;
}
}
else
{
if (simple_cst_equal (TREE_PURPOSE (p1), TREE_PURPOSE (p2)) != 1)
any_change = 1;
TREE_PURPOSE (n) = TREE_PURPOSE (p2);
}
if (TREE_VALUE (p1) != TREE_VALUE (p2))
{
any_change = 1;
TREE_VALUE (n) = merge_types (TREE_VALUE (p1), TREE_VALUE (p2));
}
else
TREE_VALUE (n) = TREE_VALUE (p1);
}
if (! any_change)
return oldargs;
return newargs;
}
/* Given a type, perhaps copied for a typedef,
find the "original" version of it. */
static tree
original_type (tree t)
{
int quals = cp_type_quals (t);
while (t != error_mark_node
&& TYPE_NAME (t) != NULL_TREE)
{
tree x = TYPE_NAME (t);
if (TREE_CODE (x) != TYPE_DECL)
break;
x = DECL_ORIGINAL_TYPE (x);
if (x == NULL_TREE)
break;
t = x;
}
return cp_build_qualified_type (t, quals);
}
/* Merge the attributes of type OTHER_TYPE into the attributes of type TYPE
and return a variant of TYPE with the merged attributes. */
static tree
merge_type_attributes_from (tree type, tree other_type)
{
tree attrs = targetm.merge_type_attributes (type, other_type);
attrs = restrict_type_identity_attributes_to (attrs, TYPE_ATTRIBUTES (type));
return cp_build_type_attribute_variant (type, attrs);
}
/* Compare floating point conversion ranks and subranks of T1 and T2
types. If T1 and T2 have unordered conversion ranks, return 3.
If T1 has greater conversion rank than T2, return 2.
If T2 has greater conversion rank than T1, return -2.
If T1 has equal conversion rank as T2, return -1, 0 or 1 depending
on if T1 has smaller, equal or greater conversion subrank than
T2. */
int
cp_compare_floating_point_conversion_ranks (tree t1, tree t2)
{
tree mv1 = TYPE_MAIN_VARIANT (t1);
tree mv2 = TYPE_MAIN_VARIANT (t2);
int extended1 = 0;
int extended2 = 0;
if (mv1 == mv2)
return 0;
for (int i = 0; i < NUM_FLOATN_NX_TYPES; ++i)
{
if (mv1 == FLOATN_NX_TYPE_NODE (i))
extended1 = i + 1;
if (mv2 == FLOATN_NX_TYPE_NODE (i))
extended2 = i + 1;
}
if (mv1 == bfloat16_type_node)
extended1 = true;
if (mv2 == bfloat16_type_node)
extended2 = true;
if (extended2 && !extended1)
{
int ret = cp_compare_floating_point_conversion_ranks (t2, t1);
return ret == 3 ? 3 : -ret;
}
const struct real_format *fmt1 = REAL_MODE_FORMAT (TYPE_MODE (t1));
const struct real_format *fmt2 = REAL_MODE_FORMAT (TYPE_MODE (t2));
gcc_assert (fmt1->b == 2 && fmt2->b == 2);
/* For {ibm,mips}_extended_format formats, the type has variable
precision up to ~2150 bits when the first double is around maximum
representable double and second double is subnormal minimum.
So, e.g. for __ibm128 vs. std::float128_t, they have unordered
ranks. */
int p1 = (MODE_COMPOSITE_P (TYPE_MODE (t1))
? fmt1->emax - fmt1->emin + fmt1->p - 1 : fmt1->p);
int p2 = (MODE_COMPOSITE_P (TYPE_MODE (t2))
? fmt2->emax - fmt2->emin + fmt2->p - 1 : fmt2->p);
/* The rank of a floating point type T is greater than the rank of
any floating-point type whose set of values is a proper subset
of the set of values of T. */
if ((p1 > p2 && fmt1->emax >= fmt2->emax)
|| (p1 == p2 && fmt1->emax > fmt2->emax))
return 2;
if ((p1 < p2 && fmt1->emax <= fmt2->emax)
|| (p1 == p2 && fmt1->emax < fmt2->emax))
return -2;
if ((p1 > p2 && fmt1->emax < fmt2->emax)
|| (p1 < p2 && fmt1->emax > fmt2->emax))
return 3;
if (!extended1 && !extended2)
{
/* The rank of long double is greater than the rank of double, which
is greater than the rank of float. */
if (t1 == long_double_type_node)
return 2;
else if (t2 == long_double_type_node)
return -2;
if (t1 == double_type_node)
return 2;
else if (t2 == double_type_node)
return -2;
if (t1 == float_type_node)
return 2;
else if (t2 == float_type_node)
return -2;
return 0;
}
/* Two extended floating-point types with the same set of values have equal
ranks. */
if (extended1 && extended2)
{
if ((extended1 <= NUM_FLOATN_TYPES) == (extended2 <= NUM_FLOATN_TYPES))
{
/* Prefer higher extendedN value. */
if (extended1 > extended2)
return 1;
else if (extended1 < extended2)
return -1;
else
return 0;
}
else if (extended1 <= NUM_FLOATN_TYPES)
/* Prefer _FloatN type over _FloatMx type. */
return 1;
else if (extended2 <= NUM_FLOATN_TYPES)
return -1;
else
return 0;
}
/* gcc_assert (extended1 && !extended2); */
tree *p;
int cnt = 0;
for (p = &float_type_node; p <= &long_double_type_node; ++p)
{
const struct real_format *fmt3 = REAL_MODE_FORMAT (TYPE_MODE (*p));
gcc_assert (fmt3->b == 2);
int p3 = (MODE_COMPOSITE_P (TYPE_MODE (*p))
? fmt3->emax - fmt3->emin + fmt3->p - 1 : fmt3->p);
if (p1 == p3 && fmt1->emax == fmt3->emax)
++cnt;
}
/* An extended floating-point type with the same set of values
as exactly one cv-unqualified standard floating-point type
has a rank equal to the rank of that standard floating-point
type.
An extended floating-point type with the same set of values
as more than one cv-unqualified standard floating-point type
has a rank equal to the rank of double.
Thus, if the latter is true and t2 is long double, t2
has higher rank. */
if (cnt > 1 && mv2 == long_double_type_node)
return -2;
/* Otherwise, they have equal rank, but extended types
(other than std::bfloat16_t) have higher subrank.
std::bfloat16_t shouldn't have equal rank to any standard
floating point type. */
return 1;
}
/* Return the common type for two arithmetic types T1 and T2 under the
usual arithmetic conversions. The default conversions have already
been applied, and enumerated types converted to their compatible
integer types. */
static tree
cp_common_type (tree t1, tree t2)
{
enum tree_code code1 = TREE_CODE (t1);
enum tree_code code2 = TREE_CODE (t2);
tree attributes;
int i;
/* In what follows, we slightly generalize the rules given in [expr] so
as to deal with `long long' and `complex'. First, merge the
attributes. */
attributes = (*targetm.merge_type_attributes) (t1, t2);
if (SCOPED_ENUM_P (t1) || SCOPED_ENUM_P (t2))
{
if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
return build_type_attribute_variant (t1, attributes);
else
return NULL_TREE;
}
/* FIXME: Attributes. */
gcc_assert (ARITHMETIC_TYPE_P (t1)
|| VECTOR_TYPE_P (t1)
|| UNSCOPED_ENUM_P (t1));
gcc_assert (ARITHMETIC_TYPE_P (t2)
|| VECTOR_TYPE_P (t2)
|| UNSCOPED_ENUM_P (t2));
/* If one type is complex, form the common type of the non-complex
components, then make that complex. Use T1 or T2 if it is the
required type. */
if (code1 == COMPLEX_TYPE || code2 == COMPLEX_TYPE)
{
tree subtype1 = code1 == COMPLEX_TYPE ? TREE_TYPE (t1) : t1;
tree subtype2 = code2 == COMPLEX_TYPE ? TREE_TYPE (t2) : t2;
tree subtype
= type_after_usual_arithmetic_conversions (subtype1, subtype2);
if (subtype == error_mark_node)
return subtype;
if (code1 == COMPLEX_TYPE && TREE_TYPE (t1) == subtype)
return build_type_attribute_variant (t1, attributes);
else if (code2 == COMPLEX_TYPE && TREE_TYPE (t2) == subtype)
return build_type_attribute_variant (t2, attributes);
else
return build_type_attribute_variant (build_complex_type (subtype),
attributes);
}
if (code1 == VECTOR_TYPE)
{
/* When we get here we should have two vectors of the same size.
Just prefer the unsigned one if present. */
if (TYPE_UNSIGNED (t1))
return merge_type_attributes_from (t1, t2);
else
return merge_type_attributes_from (t2, t1);
}
/* If only one is real, use it as the result. */
if (code1 == REAL_TYPE && code2 != REAL_TYPE)
return build_type_attribute_variant (t1, attributes);
if (code2 == REAL_TYPE && code1 != REAL_TYPE)
return build_type_attribute_variant (t2, attributes);
if (code1 == REAL_TYPE
&& (extended_float_type_p (t1) || extended_float_type_p (t2)))
{
tree mv1 = TYPE_MAIN_VARIANT (t1);
tree mv2 = TYPE_MAIN_VARIANT (t2);
if (mv1 == mv2)
return build_type_attribute_variant (t1, attributes);
int cmpret = cp_compare_floating_point_conversion_ranks (mv1, mv2);
if (cmpret == 3)
return error_mark_node;
else if (cmpret >= 0)
return build_type_attribute_variant (t1, attributes);
else
return build_type_attribute_variant (t2, attributes);
}
/* Both real or both integers; use the one with greater precision. */
if (TYPE_PRECISION (t1) > TYPE_PRECISION (t2))
return build_type_attribute_variant (t1, attributes);
else if (TYPE_PRECISION (t2) > TYPE_PRECISION (t1))
return build_type_attribute_variant (t2, attributes);
/* The types are the same; no need to do anything fancy. */
if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
return build_type_attribute_variant (t1, attributes);
if (code1 != REAL_TYPE)
{
/* If one is unsigned long long, then convert the other to unsigned
long long. */
if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_unsigned_type_node)
|| same_type_p (TYPE_MAIN_VARIANT (t2), long_long_unsigned_type_node))
return build_type_attribute_variant (long_long_unsigned_type_node,
attributes);
/* If one is a long long, and the other is an unsigned long, and
long long can represent all the values of an unsigned long, then
convert to a long long. Otherwise, convert to an unsigned long
long. Otherwise, if either operand is long long, convert the
other to long long.
Since we're here, we know the TYPE_PRECISION is the same;
therefore converting to long long cannot represent all the values
of an unsigned long, so we choose unsigned long long in that
case. */
if (same_type_p (TYPE_MAIN_VARIANT (t1), long_long_integer_type_node)
|| same_type_p (TYPE_MAIN_VARIANT (t2), long_long_integer_type_node))
{
tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
? long_long_unsigned_type_node
: long_long_integer_type_node);
return build_type_attribute_variant (t, attributes);
}
/* Go through the same procedure, but for longs. */
if (same_type_p (TYPE_MAIN_VARIANT (t1), long_unsigned_type_node)
|| same_type_p (TYPE_MAIN_VARIANT (t2), long_unsigned_type_node))
return build_type_attribute_variant (long_unsigned_type_node,
attributes);
if (same_type_p (TYPE_MAIN_VARIANT (t1), long_integer_type_node)
|| same_type_p (TYPE_MAIN_VARIANT (t2), long_integer_type_node))
{
tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
? long_unsigned_type_node : long_integer_type_node);
return build_type_attribute_variant (t, attributes);
}
/* For __intN types, either the type is __int128 (and is lower
priority than the types checked above, but higher than other
128-bit types) or it's known to not be the same size as other
types (enforced in toplev.cc). Prefer the unsigned type. */
for (i = 0; i < NUM_INT_N_ENTS; i ++)
{
if (int_n_enabled_p [i]
&& (same_type_p (TYPE_MAIN_VARIANT (t1), int_n_trees[i].signed_type)
|| same_type_p (TYPE_MAIN_VARIANT (t2), int_n_trees[i].signed_type)
|| same_type_p (TYPE_MAIN_VARIANT (t1), int_n_trees[i].unsigned_type)
|| same_type_p (TYPE_MAIN_VARIANT (t2), int_n_trees[i].unsigned_type)))
{
tree t = ((TYPE_UNSIGNED (t1) || TYPE_UNSIGNED (t2))
? int_n_trees[i].unsigned_type
: int_n_trees[i].signed_type);
return build_type_attribute_variant (t, attributes);
}
}
/* Otherwise prefer the unsigned one. */
if (TYPE_UNSIGNED (t1))
return build_type_attribute_variant (t1, attributes);
else
return build_type_attribute_variant (t2, attributes);
}
else
{
if (same_type_p (TYPE_MAIN_VARIANT (t1), long_double_type_node)
|| same_type_p (TYPE_MAIN_VARIANT (t2), long_double_type_node))
return build_type_attribute_variant (long_double_type_node,
attributes);
if (same_type_p (TYPE_MAIN_VARIANT (t1), double_type_node)
|| same_type_p (TYPE_MAIN_VARIANT (t2), double_type_node))
return build_type_attribute_variant (double_type_node,
attributes);
if (same_type_p (TYPE_MAIN_VARIANT (t1), float_type_node)
|| same_type_p (TYPE_MAIN_VARIANT (t2), float_type_node))
return build_type_attribute_variant (float_type_node,
attributes);
/* Two floating-point types whose TYPE_MAIN_VARIANTs are none of
the standard C++ floating-point types. Logic earlier in this
function has already eliminated the possibility that
TYPE_PRECISION (t2) != TYPE_PRECISION (t1), so there's no
compelling reason to choose one or the other. */
return build_type_attribute_variant (t1, attributes);
}
}
/* T1 and T2 are arithmetic or enumeration types. Return the type
that will result from the "usual arithmetic conversions" on T1 and
T2 as described in [expr]. */
tree
type_after_usual_arithmetic_conversions (tree t1, tree t2)
{
gcc_assert (ARITHMETIC_TYPE_P (t1)
|| VECTOR_TYPE_P (t1)
|| UNSCOPED_ENUM_P (t1));
gcc_assert (ARITHMETIC_TYPE_P (t2)
|| VECTOR_TYPE_P (t2)
|| UNSCOPED_ENUM_P (t2));
/* Perform the integral promotions. We do not promote real types here. */
if (INTEGRAL_OR_ENUMERATION_TYPE_P (t1)
&& INTEGRAL_OR_ENUMERATION_TYPE_P (t2))
{
t1 = type_promotes_to (t1);
t2 = type_promotes_to (t2);
}
return cp_common_type (t1, t2);
}
static void
composite_pointer_error (const op_location_t &location,
diagnostic_t kind, tree t1, tree t2,
composite_pointer_operation operation)
{
switch (operation)
{
case CPO_COMPARISON:
emit_diagnostic (kind, location, 0,
"comparison between "
"distinct pointer types %qT and %qT lacks a cast",
t1, t2);
break;
case CPO_CONVERSION:
emit_diagnostic (kind, location, 0,
"conversion between "
"distinct pointer types %qT and %qT lacks a cast",
t1, t2);
break;
case CPO_CONDITIONAL_EXPR:
emit_diagnostic (kind, location, 0,
"conditional expression between "
"distinct pointer types %qT and %qT lacks a cast",
t1, t2);
break;
default:
gcc_unreachable ();
}
}
/* Subroutine of composite_pointer_type to implement the recursive
case. See that function for documentation of the parameters. And ADD_CONST
is used to track adding "const" where needed. */
static tree
composite_pointer_type_r (const op_location_t &location,
tree t1, tree t2, bool *add_const,
composite_pointer_operation operation,
tsubst_flags_t complain)
{
tree pointee1;
tree pointee2;
tree result_type;
tree attributes;
/* Determine the types pointed to by T1 and T2. */
if (TYPE_PTR_P (t1))
{
pointee1 = TREE_TYPE (t1);
pointee2 = TREE_TYPE (t2);
}
else
{
pointee1 = TYPE_PTRMEM_POINTED_TO_TYPE (t1);
pointee2 = TYPE_PTRMEM_POINTED_TO_TYPE (t2);
}
/* [expr.type]
If T1 and T2 are similar types, the result is the cv-combined type of
T1 and T2. */
if (same_type_ignoring_top_level_qualifiers_p (pointee1, pointee2))
result_type = pointee1;
else if ((TYPE_PTR_P (pointee1) && TYPE_PTR_P (pointee2))
|| (TYPE_PTRMEM_P (pointee1) && TYPE_PTRMEM_P (pointee2)))
{
result_type = composite_pointer_type_r (location, pointee1, pointee2,
add_const, operation, complain);
if (result_type == error_mark_node)
return error_mark_node;
}
else
{
if (complain & tf_error)
composite_pointer_error (location, DK_PERMERROR,
t1, t2, operation);
else
return error_mark_node;
result_type = void_type_node;
}
const int q1 = cp_type_quals (pointee1);
const int q2 = cp_type_quals (pointee2);
const int quals = q1 | q2;
result_type = cp_build_qualified_type (result_type,
(quals | (*add_const
? TYPE_QUAL_CONST
: TYPE_UNQUALIFIED)));
/* The cv-combined type can add "const" as per [conv.qual]/3.3 (except for
the TLQ). The reason is that both T1 and T2 can then be converted to the
cv-combined type of T1 and T2. */
if (quals != q1 || quals != q2)
*add_const = true;
/* If the original types were pointers to members, so is the
result. */
if (TYPE_PTRMEM_P (t1))
{
if (!same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1),
TYPE_PTRMEM_CLASS_TYPE (t2)))
{
if (complain & tf_error)
composite_pointer_error (location, DK_PERMERROR,
t1, t2, operation);
else
return error_mark_node;
}
result_type = build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1),
result_type);
}
else
result_type = build_pointer_type (result_type);
/* Merge the attributes. */
attributes = (*targetm.merge_type_attributes) (t1, t2);
return build_type_attribute_variant (result_type, attributes);
}
/* Return the composite pointer type (see [expr.type]) for T1 and T2.
ARG1 and ARG2 are the values with those types. The OPERATION is to
describe the operation between the pointer types,
in case an error occurs.
This routine also implements the computation of a common type for
pointers-to-members as per [expr.eq]. */
tree
composite_pointer_type (const op_location_t &location,
tree t1, tree t2, tree arg1, tree arg2,
composite_pointer_operation operation,
tsubst_flags_t complain)
{
tree class1;
tree class2;
/* [expr.type]
If one operand is a null pointer constant, the composite pointer
type is the type of the other operand. */
if (null_ptr_cst_p (arg1))
return t2;
if (null_ptr_cst_p (arg2))
return t1;
/* We have:
[expr.type]
If one of the operands has type "pointer to cv1 void", then
the other has type "pointer to cv2 T", and the composite pointer
type is "pointer to cv12 void", where cv12 is the union of cv1
and cv2.
If either type is a pointer to void, make sure it is T1. */
if (TYPE_PTR_P (t2) && VOID_TYPE_P (TREE_TYPE (t2)))
std::swap (t1, t2);
/* Now, if T1 is a pointer to void, merge the qualifiers. */
if (TYPE_PTR_P (t1) && VOID_TYPE_P (TREE_TYPE (t1)))
{
tree attributes;
tree result_type;
if (TYPE_PTRFN_P (t2))
{
if (complain & tf_error)
{
switch (operation)
{
case CPO_COMPARISON:
pedwarn (location, OPT_Wpedantic,
"ISO C++ forbids comparison between pointer "
"of type %<void *%> and pointer-to-function");
break;
case CPO_CONVERSION:
pedwarn (location, OPT_Wpedantic,
"ISO C++ forbids conversion between pointer "
"of type %<void *%> and pointer-to-function");
break;
case CPO_CONDITIONAL_EXPR:
pedwarn (location, OPT_Wpedantic,
"ISO C++ forbids conditional expression between "
"pointer of type %<void *%> and "
"pointer-to-function");
break;
default:
gcc_unreachable ();
}
}
else
return error_mark_node;
}
result_type
= cp_build_qualified_type (void_type_node,
(cp_type_quals (TREE_TYPE (t1))
| cp_type_quals (TREE_TYPE (t2))));
result_type = build_pointer_type (result_type);
/* Merge the attributes. */
attributes = (*targetm.merge_type_attributes) (t1, t2);
return build_type_attribute_variant (result_type, attributes);
}
if (c_dialect_objc () && TYPE_PTR_P (t1)
&& TYPE_PTR_P (t2))
{
if (objc_have_common_type (t1, t2, -3, NULL_TREE))
return objc_common_type (t1, t2);
}
/* if T1 or T2 is "pointer to noexcept function" and the other type is
"pointer to function", where the function types are otherwise the same,
"pointer to function" */
if (fnptr_conv_p (t1, t2))
return t1;
if (fnptr_conv_p (t2, t1))
return t2;
/* [expr.eq] permits the application of a pointer conversion to
bring the pointers to a common type. */
if (TYPE_PTR_P (t1) && TYPE_PTR_P (t2)
&& CLASS_TYPE_P (TREE_TYPE (t1))
&& CLASS_TYPE_P (TREE_TYPE (t2))
&& !same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (t1),
TREE_TYPE (t2)))
{
class1 = TREE_TYPE (t1);
class2 = TREE_TYPE (t2);
if (DERIVED_FROM_P (class1, class2))
t2 = (build_pointer_type
(cp_build_qualified_type (class1, cp_type_quals (class2))));
else if (DERIVED_FROM_P (class2, class1))
t1 = (build_pointer_type
(cp_build_qualified_type (class2, cp_type_quals (class1))));
else
{
if (complain & tf_error)
composite_pointer_error (location, DK_ERROR, t1, t2, operation);
return error_mark_node;
}
}
/* [expr.eq] permits the application of a pointer-to-member
conversion to change the class type of one of the types. */
else if (TYPE_PTRMEM_P (t1)
&& !same_type_p (TYPE_PTRMEM_CLASS_TYPE (t1),
TYPE_PTRMEM_CLASS_TYPE (t2)))
{
class1 = TYPE_PTRMEM_CLASS_TYPE (t1);
class2 = TYPE_PTRMEM_CLASS_TYPE (t2);
if (DERIVED_FROM_P (class1, class2))
t1 = build_ptrmem_type (class2, TYPE_PTRMEM_POINTED_TO_TYPE (t1));
else if (DERIVED_FROM_P (class2, class1))
t2 = build_ptrmem_type (class1, TYPE_PTRMEM_POINTED_TO_TYPE (t2));
else
{
if (complain & tf_error)
switch (operation)
{
case CPO_COMPARISON:
error_at (location, "comparison between distinct "
"pointer-to-member types %qT and %qT lacks a cast",
t1, t2);
break;
case CPO_CONVERSION:
error_at (location, "conversion between distinct "
"pointer-to-member types %qT and %qT lacks a cast",
t1, t2);
break;
case CPO_CONDITIONAL_EXPR:
error_at (location, "conditional expression between distinct "
"pointer-to-member types %qT and %qT lacks a cast",
t1, t2);
break;
default:
gcc_unreachable ();
}
return error_mark_node;
}
}
bool add_const = false;
return composite_pointer_type_r (location, t1, t2, &add_const, operation,
complain);
}
/* Return the merged type of two types.
We assume that comptypes has already been done and returned 1;
if that isn't so, this may crash.
This just combines attributes and default arguments; any other
differences would cause the two types to compare unalike. */
tree
merge_types (tree t1, tree t2)
{
enum tree_code code1;
enum tree_code code2;
tree attributes;
/* Save time if the two types are the same. */
if (t1 == t2)
return t1;
if (original_type (t1) == original_type (t2))
return t1;
/* If one type is nonsense, use the other. */
if (t1 == error_mark_node)
return t2;
if (t2 == error_mark_node)
return t1;
/* Handle merging an auto redeclaration with a previous deduced
return type. */
if (is_auto (t1))
return t2;
/* Merge the attributes. */
attributes = (*targetm.merge_type_attributes) (t1, t2);
if (TYPE_PTRMEMFUNC_P (t1))
t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
if (TYPE_PTRMEMFUNC_P (t2))
t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
code1 = TREE_CODE (t1);
code2 = TREE_CODE (t2);
if (code1 != code2)
{
gcc_assert (code1 == TYPENAME_TYPE || code2 == TYPENAME_TYPE);
if (code1 == TYPENAME_TYPE)
{
t1 = resolve_typename_type (t1, /*only_current_p=*/true);
code1 = TREE_CODE (t1);
}
else
{
t2 = resolve_typename_type (t2, /*only_current_p=*/true);
code2 = TREE_CODE (t2);
}
}
switch (code1)
{
case POINTER_TYPE:
case REFERENCE_TYPE:
/* For two pointers, do this recursively on the target type. */
{
tree target = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
int quals = cp_type_quals (t1);
if (code1 == POINTER_TYPE)
{
t1 = build_pointer_type (target);
if (TREE_CODE (target) == METHOD_TYPE)
t1 = build_ptrmemfunc_type (t1);
}
else
t1 = cp_build_reference_type (target, TYPE_REF_IS_RVALUE (t1));
t1 = build_type_attribute_variant (t1, attributes);
t1 = cp_build_qualified_type (t1, quals);
return t1;
}
case OFFSET_TYPE:
{
int quals;
tree pointee;
quals = cp_type_quals (t1);
pointee = merge_types (TYPE_PTRMEM_POINTED_TO_TYPE (t1),
TYPE_PTRMEM_POINTED_TO_TYPE (t2));
t1 = build_ptrmem_type (TYPE_PTRMEM_CLASS_TYPE (t1),
pointee);
t1 = cp_build_qualified_type (t1, quals);
break;
}
case ARRAY_TYPE:
{
tree elt = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
/* Save space: see if the result is identical to one of the args. */
if (elt == TREE_TYPE (t1) && TYPE_DOMAIN (t1))
return build_type_attribute_variant (t1, attributes);
if (elt == TREE_TYPE (t2) && TYPE_DOMAIN (t2))
return build_type_attribute_variant (t2, attributes);
/* Merge the element types, and have a size if either arg has one. */
t1 = build_cplus_array_type
(elt, TYPE_DOMAIN (TYPE_DOMAIN (t1) ? t1 : t2));
break;
}
case FUNCTION_TYPE:
/* Function types: prefer the one that specified arg types.
If both do, merge the arg types. Also merge the return types. */
{
tree valtype = merge_types (TREE_TYPE (t1), TREE_TYPE (t2));
tree p1 = TYPE_ARG_TYPES (t1);
tree p2 = TYPE_ARG_TYPES (t2);
tree parms;
/* Save space: see if the result is identical to one of the args. */
if (valtype == TREE_TYPE (t1) && ! p2)
return cp_build_type_attribute_variant (t1, attributes);
if (valtype == TREE_TYPE (t2) && ! p1)
return cp_build_type_attribute_variant (t2, attributes);
/* Simple way if one arg fails to specify argument types. */
if (p1 == NULL_TREE || TREE_VALUE (p1) == void_type_node)
parms = p2;
else if (p2 == NULL_TREE || TREE_VALUE (p2) == void_type_node)
parms = p1;
else
parms = commonparms (p1, p2);
cp_cv_quals quals = type_memfn_quals (t1);
cp_ref_qualifier rqual = type_memfn_rqual (t1);
gcc_assert (quals == type_memfn_quals (t2));
gcc_assert (rqual == type_memfn_rqual (t2));
tree rval = build_function_type (valtype, parms);
rval = apply_memfn_quals (rval, quals);
tree raises = merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1),
TYPE_RAISES_EXCEPTIONS (t2));
bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t1);
t1 = build_cp_fntype_variant (rval, rqual, raises, late_return_type_p);
break;
}
case METHOD_TYPE:
{
/* Get this value the long way, since TYPE_METHOD_BASETYPE
is just the main variant of this. */
tree basetype = class_of_this_parm (t2);
tree raises = merge_exception_specifiers (TYPE_RAISES_EXCEPTIONS (t1),
TYPE_RAISES_EXCEPTIONS (t2));
cp_ref_qualifier rqual = type_memfn_rqual (t1);
tree t3;
bool late_return_type_1_p = TYPE_HAS_LATE_RETURN_TYPE (t1);
/* If this was a member function type, get back to the
original type of type member function (i.e., without
the class instance variable up front. */
t1 = build_function_type (TREE_TYPE (t1),
TREE_CHAIN (TYPE_ARG_TYPES (t1)));
t2 = build_function_type (TREE_TYPE (t2),
TREE_CHAIN (TYPE_ARG_TYPES (t2)));
t3 = merge_types (t1, t2);
t3 = build_method_type_directly (basetype, TREE_TYPE (t3),
TYPE_ARG_TYPES (t3));
t1 = build_cp_fntype_variant (t3, rqual, raises, late_return_type_1_p);
break;
}
case TYPENAME_TYPE:
/* There is no need to merge attributes into a TYPENAME_TYPE.
When the type is instantiated it will have whatever
attributes result from the instantiation. */
return t1;
default:;
if (attribute_list_equal (TYPE_ATTRIBUTES (t1), attributes))
return t1;
else if (attribute_list_equal (TYPE_ATTRIBUTES (t2), attributes))
return t2;
break;
}
return cp_build_type_attribute_variant (t1, attributes);
}
/* Return the ARRAY_TYPE type without its domain. */
tree
strip_array_domain (tree type)
{
tree t2;
gcc_assert (TREE_CODE (type) == ARRAY_TYPE);
if (TYPE_DOMAIN (type) == NULL_TREE)
return type;
t2 = build_cplus_array_type (TREE_TYPE (type), NULL_TREE);
return cp_build_type_attribute_variant (t2, TYPE_ATTRIBUTES (type));
}
/* Wrapper around cp_common_type that is used by c-common.cc and other
front end optimizations that remove promotions.
Return the common type for two arithmetic types T1 and T2 under the
usual arithmetic conversions. The default conversions have already
been applied, and enumerated types converted to their compatible
integer types. */
tree
common_type (tree t1, tree t2)
{
/* If one type is nonsense, use the other */
if (t1 == error_mark_node)
return t2;
if (t2 == error_mark_node)
return t1;
return cp_common_type (t1, t2);
}
/* Return the common type of two pointer types T1 and T2. This is the
type for the result of most arithmetic operations if the operands
have the given two types.
We assume that comp_target_types has already been done and returned
nonzero; if that isn't so, this may crash. */
tree
common_pointer_type (tree t1, tree t2)
{
gcc_assert ((TYPE_PTR_P (t1) && TYPE_PTR_P (t2))
|| (TYPE_PTRDATAMEM_P (t1) && TYPE_PTRDATAMEM_P (t2))
|| (TYPE_PTRMEMFUNC_P (t1) && TYPE_PTRMEMFUNC_P (t2)));
return composite_pointer_type (input_location, t1, t2,
error_mark_node, error_mark_node,
CPO_CONVERSION, tf_warning_or_error);
}
/* Compare two exception specifier types for exactness or subsetness, if
allowed. Returns false for mismatch, true for match (same, or
derived and !exact).
[except.spec] "If a class X ... objects of class X or any class publicly
and unambiguously derived from X. Similarly, if a pointer type Y * ...
exceptions of type Y * or that are pointers to any type publicly and
unambiguously derived from Y. Otherwise a function only allows exceptions
that have the same type ..."
This does not mention cv qualifiers and is different to what throw
[except.throw] and catch [except.catch] will do. They will ignore the
top level cv qualifiers, and allow qualifiers in the pointer to class
example.
We implement the letter of the standard. */
static bool
comp_except_types (tree a, tree b, bool exact)
{
if (same_type_p (a, b))
return true;
else if (!exact)
{
if (cp_type_quals (a) || cp_type_quals (b))
return false;
if (TYPE_PTR_P (a) && TYPE_PTR_P (b))
{
a = TREE_TYPE (a);
b = TREE_TYPE (b);
if (cp_type_quals (a) || cp_type_quals (b))
return false;
}
if (TREE_CODE (a) != RECORD_TYPE
|| TREE_CODE (b) != RECORD_TYPE)
return false;
if (publicly_uniquely_derived_p (a, b))
return true;
}
return false;
}
/* Return true if TYPE1 and TYPE2 are equivalent exception specifiers.
If EXACT is ce_derived, T2 can be stricter than T1 (according to 15.4/5).
If EXACT is ce_type, the C++17 type compatibility rules apply.
If EXACT is ce_normal, the compatibility rules in 15.4/3 apply.
If EXACT is ce_exact, the specs must be exactly the same. Exception lists
are unordered, but we've already filtered out duplicates. Most lists will
be in order, we should try to make use of that. */
bool
comp_except_specs (const_tree t1, const_tree t2, int exact)
{
const_tree probe;
const_tree base;
int length = 0;
if (t1 == t2)
return true;
/* First handle noexcept. */
if (exact < ce_exact)
{
if (exact == ce_type
&& (canonical_eh_spec (CONST_CAST_TREE (t1))
== canonical_eh_spec (CONST_CAST_TREE (t2))))
return true;
/* noexcept(false) is compatible with no exception-specification,
and less strict than any spec. */
if (t1 == noexcept_false_spec)
return t2 == NULL_TREE || exact == ce_derived;
/* Even a derived noexcept(false) is compatible with no
exception-specification. */
if (t2 == noexcept_false_spec)
return t1 == NULL_TREE;
/* Otherwise, if we aren't looking for an exact match, noexcept is
equivalent to throw(). */
if (t1 == noexcept_true_spec)
t1 = empty_except_spec;
if (t2 == noexcept_true_spec)
t2 = empty_except_spec;
}
/* If any noexcept is left, it is only comparable to itself;
either we're looking for an exact match or we're redeclaring a
template with dependent noexcept. */
if ((t1 && TREE_PURPOSE (t1))
|| (t2 && TREE_PURPOSE (t2)))
return (t1 && t2
&& cp_tree_equal (TREE_PURPOSE (t1), TREE_PURPOSE (t2)));
if (t1 == NULL_TREE) /* T1 is ... */
return t2 == NULL_TREE || exact == ce_derived;
if (!TREE_VALUE (t1)) /* t1 is EMPTY */
return t2 != NULL_TREE && !TREE_VALUE (t2);
if (t2 == NULL_TREE) /* T2 is ... */
return false;
if (TREE_VALUE (t1) && !TREE_VALUE (t2)) /* T2 is EMPTY, T1 is not */
return exact == ce_derived;
/* Neither set is ... or EMPTY, make sure each part of T2 is in T1.
Count how many we find, to determine exactness. For exact matching and
ordered T1, T2, this is an O(n) operation, otherwise its worst case is
O(nm). */
for (base = t1; t2 != NULL_TREE; t2 = TREE_CHAIN (t2))
{
for (probe = base; probe != NULL_TREE; probe = TREE_CHAIN (probe))
{
tree a = TREE_VALUE (probe);
tree b = TREE_VALUE (t2);
if (comp_except_types (a, b, exact))
{
if (probe == base && exact > ce_derived)
base = TREE_CHAIN (probe);
length++;
break;
}
}
if (probe == NULL_TREE)
return false;
}
return exact == ce_derived || base == NULL_TREE || length == list_length (t1);
}
/* Compare the array types T1 and T2. CB says how we should behave when
comparing array bounds: bounds_none doesn't allow dimensionless arrays,
bounds_either says than any array can be [], bounds_first means that
onlt T1 can be an array with unknown bounds. STRICT is true if
qualifiers must match when comparing the types of the array elements. */
static bool
comp_array_types (const_tree t1, const_tree t2, compare_bounds_t cb,
bool strict)
{
tree d1;
tree d2;
tree max1, max2;
if (t1 == t2)
return true;
/* The type of the array elements must be the same. */
if (strict
? !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2))
: !similar_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
return false;
d1 = TYPE_DOMAIN (t1);
d2 = TYPE_DOMAIN (t2);
if (d1 == d2)
return true;
/* If one of the arrays is dimensionless, and the other has a
dimension, they are of different types. However, it is valid to
write:
extern int a[];
int a[3];
by [basic.link]:
declarations for an array object can specify
array types that differ by the presence or absence of a major
array bound (_dcl.array_). */
if (!d1 && d2)
return cb >= bounds_either;
else if (d1 && !d2)
return cb == bounds_either;
/* Check that the dimensions are the same. */
if (!cp_tree_equal (TYPE_MIN_VALUE (d1), TYPE_MIN_VALUE (d2)))
return false;
max1 = TYPE_MAX_VALUE (d1);
max2 = TYPE_MAX_VALUE (d2);
if (!cp_tree_equal (max1, max2))
return false;
return true;
}
/* Compare the relative position of T1 and T2 into their respective
template parameter list.
T1 and T2 must be template parameter types.
Return TRUE if T1 and T2 have the same position, FALSE otherwise. */
static bool
comp_template_parms_position (tree t1, tree t2)
{
tree index1, index2;
gcc_assert (t1 && t2
&& TREE_CODE (t1) == TREE_CODE (t2)
&& (TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM
|| TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM
|| TREE_CODE (t1) == TEMPLATE_TYPE_PARM));
index1 = TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t1));
index2 = TEMPLATE_TYPE_PARM_INDEX (TYPE_MAIN_VARIANT (t2));
/* Then compare their relative position. */
if (TEMPLATE_PARM_IDX (index1) != TEMPLATE_PARM_IDX (index2)
|| TEMPLATE_PARM_LEVEL (index1) != TEMPLATE_PARM_LEVEL (index2)
|| (TEMPLATE_PARM_PARAMETER_PACK (index1)
!= TEMPLATE_PARM_PARAMETER_PACK (index2)))
return false;
/* In C++14 we can end up comparing 'auto' to a normal template
parameter. Don't confuse them. */
if (cxx_dialect >= cxx14 && (is_auto (t1) || is_auto (t2)))
return TYPE_IDENTIFIER (t1) == TYPE_IDENTIFIER (t2);
return true;
}
/* Heuristic check if two parameter types can be considered ABI-equivalent. */
static bool
cxx_safe_arg_type_equiv_p (tree t1, tree t2)
{
t1 = TYPE_MAIN_VARIANT (t1);
t2 = TYPE_MAIN_VARIANT (t2);
if (TYPE_PTR_P (t1)
&& TYPE_PTR_P (t2))
return true;
/* The signedness of the parameter matters only when an integral
type smaller than int is promoted to int, otherwise only the
precision of the parameter matters.
This check should make sure that the callee does not see
undefined values in argument registers. */
if (INTEGRAL_TYPE_P (t1)
&& INTEGRAL_TYPE_P (t2)
&& TYPE_PRECISION (t1) == TYPE_PRECISION (t2)
&& (TYPE_UNSIGNED (t1) == TYPE_UNSIGNED (t2)
|| !targetm.calls.promote_prototypes (NULL_TREE)
|| TYPE_PRECISION (t1) >= TYPE_PRECISION (integer_type_node)))
return true;
return same_type_p (t1, t2);
}
/* Check if a type cast between two function types can be considered safe. */
static bool
cxx_safe_function_type_cast_p (tree t1, tree t2)
{
if (TREE_TYPE (t1) == void_type_node &&
TYPE_ARG_TYPES (t1) == void_list_node)
return true;
if (TREE_TYPE (t2) == void_type_node &&
TYPE_ARG_TYPES (t2) == void_list_node)
return true;
if (!cxx_safe_arg_type_equiv_p (TREE_TYPE (t1), TREE_TYPE (t2)))
return false;
for (t1 = TYPE_ARG_TYPES (t1), t2 = TYPE_ARG_TYPES (t2);
t1 && t2;
t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
if (!cxx_safe_arg_type_equiv_p (TREE_VALUE (t1), TREE_VALUE (t2)))
return false;
return true;
}
/* Subroutine in comptypes. */
static bool
structural_comptypes (tree t1, tree t2, int strict)
{
/* Both should be types that are not obviously the same. */
gcc_checking_assert (t1 != t2 && TYPE_P (t1) && TYPE_P (t2));
/* Suppress typename resolution under spec_hasher::equal in place of calling
push_to_top_level there. */
if (!comparing_specializations)
{
/* TYPENAME_TYPEs should be resolved if the qualifying scope is the
current instantiation. */
if (TREE_CODE (t1) == TYPENAME_TYPE)
t1 = resolve_typename_type (t1, /*only_current_p=*/true);
if (TREE_CODE (t2) == TYPENAME_TYPE)
t2 = resolve_typename_type (t2, /*only_current_p=*/true);
}
if (TYPE_PTRMEMFUNC_P (t1))
t1 = TYPE_PTRMEMFUNC_FN_TYPE (t1);
if (TYPE_PTRMEMFUNC_P (t2))
t2 = TYPE_PTRMEMFUNC_FN_TYPE (t2);
/* Different classes of types can't be compatible. */
if (TREE_CODE (t1) != TREE_CODE (t2))
return false;
/* Qualifiers must match. For array types, we will check when we
recur on the array element types. */
if (TREE_CODE (t1) != ARRAY_TYPE
&& cp_type_quals (t1) != cp_type_quals (t2))
return false;
if (TREE_CODE (t1) == FUNCTION_TYPE
&& type_memfn_quals (t1) != type_memfn_quals (t2))
return false;
/* Need to check this before TYPE_MAIN_VARIANT.
FIXME function qualifiers should really change the main variant. */
if (FUNC_OR_METHOD_TYPE_P (t1))
{
if (type_memfn_rqual (t1) != type_memfn_rqual (t2))
return false;
if (flag_noexcept_type
&& !comp_except_specs (TYPE_RAISES_EXCEPTIONS (t1),
TYPE_RAISES_EXCEPTIONS (t2),
ce_type))
return false;
}
/* Allow for two different type nodes which have essentially the same
definition. Note that we already checked for equality of the type
qualifiers (just above). */
if (TREE_CODE (t1) != ARRAY_TYPE
&& TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
goto check_alias;
/* Compare the types. Return false on known not-same. Break on not
known. Never return true from this switch -- you'll break
specialization comparison. */
switch (TREE_CODE (t1))
{
case VOID_TYPE:
case BOOLEAN_TYPE:
/* All void and bool types are the same. */
break;
case OPAQUE_TYPE:
case INTEGER_TYPE:
case FIXED_POINT_TYPE:
case REAL_TYPE:
/* With these nodes, we can't determine type equivalence by
looking at what is stored in the nodes themselves, because
two nodes might have different TYPE_MAIN_VARIANTs but still
represent the same type. For example, wchar_t and int could
have the same properties (TYPE_PRECISION, TYPE_MIN_VALUE,
TYPE_MAX_VALUE, etc.), but have different TYPE_MAIN_VARIANTs
and are distinct types. On the other hand, int and the
following typedef
typedef int INT __attribute((may_alias));
have identical properties, different TYPE_MAIN_VARIANTs, but
represent the same type. The canonical type system keeps
track of equivalence in this case, so we fall back on it. */
if (TYPE_CANONICAL (t1) != TYPE_CANONICAL (t2))
return false;
/* We don't need or want the attribute comparison. */
goto check_alias;
case TEMPLATE_TEMPLATE_PARM:
case BOUND_TEMPLATE_TEMPLATE_PARM:
if (!comp_template_parms_position (t1, t2))
return false;
if (!comp_template_parms
(DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t1)),
DECL_TEMPLATE_PARMS (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t2))))
return false;
if (TREE_CODE (t1) == TEMPLATE_TEMPLATE_PARM)
break;
/* Don't check inheritance. */
strict = COMPARE_STRICT;
/* Fall through. */
case RECORD_TYPE:
case UNION_TYPE:
if (TYPE_TEMPLATE_INFO (t1) && TYPE_TEMPLATE_INFO (t2)
&& (TYPE_TI_TEMPLATE (t1) == TYPE_TI_TEMPLATE (t2)
|| TREE_CODE (t1) == BOUND_TEMPLATE_TEMPLATE_PARM)
&& comp_template_args (TYPE_TI_ARGS (t1), TYPE_TI_ARGS (t2)))
break;
if ((strict & COMPARE_BASE) && DERIVED_FROM_P (t1, t2))
break;
else if ((strict & COMPARE_DERIVED) && DERIVED_FROM_P (t2, t1))
break;
return false;
case OFFSET_TYPE:
if (!comptypes (TYPE_OFFSET_BASETYPE (t1), TYPE_OFFSET_BASETYPE (t2),
strict & ~COMPARE_REDECLARATION))
return false;
if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
return false;
break;
case REFERENCE_TYPE:
if (TYPE_REF_IS_RVALUE (t1) != TYPE_REF_IS_RVALUE (t2))
return false;
/* fall through to checks for pointer types */
gcc_fallthrough ();
case POINTER_TYPE:
if (TYPE_MODE (t1) != TYPE_MODE (t2)
|| !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
return false;
break;
case METHOD_TYPE:
case FUNCTION_TYPE:
/* Exception specs and memfn_rquals were checked above. */
if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
return false;
if (!compparms (TYPE_ARG_TYPES (t1), TYPE_ARG_TYPES (t2)))
return false;
break;
case ARRAY_TYPE:
/* Target types must match incl. qualifiers. */
if (!comp_array_types (t1, t2, ((strict & COMPARE_REDECLARATION)
? bounds_either : bounds_none),
/*strict=*/true))
return false;
break;
case TEMPLATE_TYPE_PARM:
/* If T1 and T2 don't have the same relative position in their
template parameters set, they can't be equal. */
if (!comp_template_parms_position (t1, t2))
return false;
/* If T1 and T2 don't represent the same class template deduction,
they aren't equal. */
if (CLASS_PLACEHOLDER_TEMPLATE (t1)
!= CLASS_PLACEHOLDER_TEMPLATE (t2))
return false;
/* Constrained 'auto's are distinct from parms that don't have the same
constraints. */
if (!equivalent_placeholder_constraints (t1, t2))
return false;
break;
case TYPENAME_TYPE:
if (!cp_tree_equal (TYPENAME_TYPE_FULLNAME (t1),
TYPENAME_TYPE_FULLNAME (t2)))
return false;
/* Qualifiers don't matter on scopes. */
if (!same_type_ignoring_top_level_qualifiers_p (TYPE_CONTEXT (t1),
TYPE_CONTEXT (t2)))
return false;
break;
case UNBOUND_CLASS_TEMPLATE:
if (!cp_tree_equal (TYPE_IDENTIFIER (t1), TYPE_IDENTIFIER (t2)))
return false;
if (!same_type_p (TYPE_CONTEXT (t1), TYPE_CONTEXT (t2)))
return false;
break;
case COMPLEX_TYPE:
if (!same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
return false;
break;
case VECTOR_TYPE:
if (gnu_vector_type_p (t1) != gnu_vector_type_p (t2)
|| maybe_ne (TYPE_VECTOR_SUBPARTS (t1), TYPE_VECTOR_SUBPARTS (t2))
|| !same_type_p (TREE_TYPE (t1), TREE_TYPE (t2)))
return false;
break;
case TYPE_PACK_EXPANSION:
return (same_type_p (PACK_EXPANSION_PATTERN (t1),
PACK_EXPANSION_PATTERN (t2))
&& comp_template_args (PACK_EXPANSION_EXTRA_ARGS (t1),
PACK_EXPANSION_EXTRA_ARGS (t2)));
case DECLTYPE_TYPE:
if (DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t1)
!= DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t2))
return false;
if (DECLTYPE_FOR_LAMBDA_CAPTURE (t1) != DECLTYPE_FOR_LAMBDA_CAPTURE (t2))
return false;
if (DECLTYPE_FOR_LAMBDA_PROXY (t1) != DECLTYPE_FOR_LAMBDA_PROXY (t2))
return false;
if (!cp_tree_equal (DECLTYPE_TYPE_EXPR (t1), DECLTYPE_TYPE_EXPR (t2)))
return false;
break;
case TRAIT_TYPE:
if (TRAIT_TYPE_KIND (t1) != TRAIT_TYPE_KIND (t2))
return false;
if (!cp_tree_equal (TRAIT_TYPE_TYPE1 (t1), TRAIT_TYPE_TYPE1 (t2))
|| !cp_tree_equal (TRAIT_TYPE_TYPE2 (t1), TRAIT_TYPE_TYPE2 (t2)))
return false;
break;
case TYPEOF_TYPE:
if (!cp_tree_equal (TYPEOF_TYPE_EXPR (t1), TYPEOF_TYPE_EXPR (t2)))
return false;
break;
default:
return false;
}
/* If we get here, we know that from a target independent POV the
types are the same. Make sure the target attributes are also
the same. */
if (!comp_type_attributes (t1, t2))
return false;
check_alias:
if (comparing_dependent_aliases)
{
/* Don't treat an alias template specialization with dependent
arguments as equivalent to its underlying type when used as a
template argument; we need them to be distinct so that we
substitute into the specialization arguments at instantiation
time. And aliases can't be equivalent without being ==, so
we don't need to look any deeper. */
++processing_template_decl;
tree dep1 = dependent_alias_template_spec_p (t1, nt_transparent);
tree dep2 = dependent_alias_template_spec_p (t2, nt_transparent);
--processing_template_decl;
if ((dep1 || dep2) && dep1 != dep2)
return false;
}
return true;
}
/* Return true if T1 and T2 are related as allowed by STRICT. STRICT
is a bitwise-or of the COMPARE_* flags. */
bool
comptypes (tree t1, tree t2, int strict)
{
gcc_checking_assert (t1 && t2);
/* TYPE_ARGUMENT_PACKS are not really types. */
gcc_checking_assert (TREE_CODE (t1) != TYPE_ARGUMENT_PACK
&& TREE_CODE (t2) != TYPE_ARGUMENT_PACK);
if (t1 == t2)
return true;
/* Suppress errors caused by previously reported errors. */
if (t1 == error_mark_node || t2 == error_mark_node)
return false;
if (strict == COMPARE_STRICT)
{
if (TYPE_STRUCTURAL_EQUALITY_P (t1) || TYPE_STRUCTURAL_EQUALITY_P (t2))
/* At least one of the types requires structural equality, so
perform a deep check. */
return structural_comptypes (t1, t2, strict);
if (flag_checking && param_use_canonical_types)
{
bool result = structural_comptypes (t1, t2, strict);
if (result && TYPE_CANONICAL (t1) != TYPE_CANONICAL (t2))
/* The two types are structurally equivalent, but their
canonical types were different. This is a failure of the
canonical type propagation code.*/
internal_error
("canonical types differ for identical types %qT and %qT",
t1, t2);
else if (!result && TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2))
/* Two types are structurally different, but the canonical
types are the same. This means we were over-eager in
assigning canonical types. */
internal_error
("same canonical type node for different types %qT and %qT",
t1, t2);
return result;
}
if (!flag_checking && param_use_canonical_types)
return TYPE_CANONICAL (t1) == TYPE_CANONICAL (t2);
else
return structural_comptypes (t1, t2, strict);
}
else if (strict == COMPARE_STRUCTURAL)
return structural_comptypes (t1, t2, COMPARE_STRICT);
else
return structural_comptypes (t1, t2, strict);
}
/* Returns nonzero iff TYPE1 and TYPE2 are the same type, ignoring
top-level qualifiers. */
bool
same_type_ignoring_top_level_qualifiers_p (tree type1, tree type2)
{
if (type1 == error_mark_node || type2 == error_mark_node)
return false;
if (type1 == type2)
return true;
type1 = cp_build_qualified_type (type1, TYPE_UNQUALIFIED);
type2 = cp_build_qualified_type (type2, TYPE_UNQUALIFIED);
return same_type_p (type1, type2);
}
/* Returns nonzero iff TYPE1 and TYPE2 are similar, as per [conv.qual]. */
bool
similar_type_p (tree type1, tree type2)
{
if (type1 == error_mark_node || type2 == error_mark_node)
return false;
/* Informally, two types are similar if, ignoring top-level cv-qualification:
* they are the same type; or
* they are both pointers, and the pointed-to types are similar; or
* they are both pointers to member of the same class, and the types of
the pointed-to members are similar; or
* they are both arrays of the same size or both arrays of unknown bound,
and the array element types are similar. */
if (same_type_ignoring_top_level_qualifiers_p (type1, type2))
return true;
if ((TYPE_PTR_P (type1) && TYPE_PTR_P (type2))
|| (TYPE_PTRDATAMEM_P (type1) && TYPE_PTRDATAMEM_P (type2))
|| (TREE_CODE (type1) == ARRAY_TYPE && TREE_CODE (type2) == ARRAY_TYPE))
return comp_ptr_ttypes_const (type1, type2, bounds_either);
return false;
}
/* Helper function for layout_compatible_type_p and
is_corresponding_member_aggr. Advance to next members (NULL if
no further ones) and return true if those members are still part of
the common initial sequence. */
bool
next_common_initial_sequence (tree &memb1, tree &memb2)
{
while (memb1)
{
if (TREE_CODE (memb1) != FIELD_DECL
|| (DECL_FIELD_IS_BASE (memb1) && is_empty_field (memb1)))
{
memb1 = DECL_CHAIN (memb1);
continue;
}
if (DECL_FIELD_IS_BASE (memb1))
{
memb1 = TYPE_FIELDS (TREE_TYPE (memb1));
continue;
}
break;
}
while (memb2)
{
if (TREE_CODE (memb2) != FIELD_DECL
|| (DECL_FIELD_IS_BASE (memb2) && is_empty_field (memb2)))
{
memb2 = DECL_CHAIN (memb2);
continue;
}
if (DECL_FIELD_IS_BASE (memb2))
{
memb2 = TYPE_FIELDS (TREE_TYPE (memb2));
continue;
}
break;
}
if (memb1 == NULL_TREE && memb2 == NULL_TREE)
return true;
if (memb1 == NULL_TREE || memb2 == NULL_TREE)
return false;
if (DECL_BIT_FIELD_TYPE (memb1))
{
if (!DECL_BIT_FIELD_TYPE (memb2))
return false;
if (!layout_compatible_type_p (DECL_BIT_FIELD_TYPE (memb1),
DECL_BIT_FIELD_TYPE (memb2)))
return false;
if (TYPE_PRECISION (TREE_TYPE (memb1))
!= TYPE_PRECISION (TREE_TYPE (memb2)))
return false;
}
else if (DECL_BIT_FIELD_TYPE (memb2))
return false;
else if (!layout_compatible_type_p (TREE_TYPE (memb1), TREE_TYPE (memb2)))
return false;
if ((!lookup_attribute ("no_unique_address", DECL_ATTRIBUTES (memb1)))
!= !lookup_attribute ("no_unique_address", DECL_ATTRIBUTES (memb2)))
return false;
if (DECL_ALIGN (memb1) != DECL_ALIGN (memb2))
return false;
if (!tree_int_cst_equal (bit_position (memb1), bit_position (memb2)))
return false;
return true;
}
/* Return true if TYPE1 and TYPE2 are layout-compatible types. */
bool
layout_compatible_type_p (tree type1, tree type2)
{
if (type1 == error_mark_node || type2 == error_mark_node)
return false;
if (type1 == type2)
return true;
if (TREE_CODE (type1) != TREE_CODE (type2))
return false;
type1 = cp_build_qualified_type (type1, TYPE_UNQUALIFIED);
type2 = cp_build_qualified_type (type2, TYPE_UNQUALIFIED);
if (TREE_CODE (type1) == ENUMERAL_TYPE)
return (tree_int_cst_equal (TYPE_SIZE (type1), TYPE_SIZE (type2))
&& same_type_p (finish_underlying_type (type1),
finish_underlying_type (type2)));
if (CLASS_TYPE_P (type1)
&& std_layout_type_p (type1)
&& std_layout_type_p (type2)
&& tree_int_cst_equal (TYPE_SIZE (type1), TYPE_SIZE (type2)))
{
tree field1 = TYPE_FIELDS (type1);
tree field2 = TYPE_FIELDS (type2);
if (TREE_CODE (type1) == RECORD_TYPE)
{
while (1)
{
if (!next_common_initial_sequence (field1, field2))
return false;
if (field1 == NULL_TREE)
return true;
field1 = DECL_CHAIN (field1);
field2 = DECL_CHAIN (field2);
}
}
/* Otherwise both types must be union types.
The standard says:
"Two standard-layout unions are layout-compatible if they have
the same number of non-static data members and corresponding
non-static data members (in any order) have layout-compatible
types."
but the code anticipates that bitfield vs. non-bitfield,
different bitfield widths or presence/absence of
[[no_unique_address]] should be checked as well. */
auto_vec<tree, 16> vec;
unsigned int count = 0;
for (; field1; field1 = DECL_CHAIN (field1))
if (TREE_CODE (field1) == FIELD_DECL)
count++;
for (; field2; field2 = DECL_CHAIN (field2))
if (TREE_CODE (field2) == FIELD_DECL)
vec.safe_push (field2);
/* Discussions on core lean towards treating multiple union fields
of the same type as the same field, so this might need changing
in the future. */
if (count != vec.length ())
return false;
for (field1 = TYPE_FIELDS (type1); field1; field1 = DECL_CHAIN (field1))
{
if (TREE_CODE (field1) != FIELD_DECL)
continue;
unsigned int j;
tree t1 = DECL_BIT_FIELD_TYPE (field1);
if (t1 == NULL_TREE)
t1 = TREE_TYPE (field1);
FOR_EACH_VEC_ELT (vec, j, field2)
{
tree t2 = DECL_BIT_FIELD_TYPE (field2);
if (t2 == NULL_TREE)
t2 = TREE_TYPE (field2);
if (DECL_BIT_FIELD_TYPE (field1))
{
if (!DECL_BIT_FIELD_TYPE (field2))
continue;
if (TYPE_PRECISION (TREE_TYPE (field1))
!= TYPE_PRECISION (TREE_TYPE (field2)))
continue;
}
else if (DECL_BIT_FIELD_TYPE (field2))
continue;
if (!layout_compatible_type_p (t1, t2))
continue;
if ((!lookup_attribute ("no_unique_address",
DECL_ATTRIBUTES (field1)))
!= !lookup_attribute ("no_unique_address",
DECL_ATTRIBUTES (field2)))
continue;
break;
}
if (j == vec.length ())
return false;
vec.unordered_remove (j);
}
return true;
}
return same_type_p (type1, type2);
}
/* Returns 1 if TYPE1 is at least as qualified as TYPE2. */
bool
at_least_as_qualified_p (const_tree type1, const_tree type2)
{
int q1 = cp_type_quals (type1);
int q2 = cp_type_quals (type2);
/* All qualifiers for TYPE2 must also appear in TYPE1. */
return (q1 & q2) == q2;
}
/* Returns 1 if TYPE1 is more cv-qualified than TYPE2, -1 if TYPE2 is
more cv-qualified that TYPE1, and 0 otherwise. */
int
comp_cv_qualification (int q1, int q2)
{
if (q1 == q2)
return 0;
if ((q1 & q2) == q2)
return 1;
else if ((q1 & q2) == q1)
return -1;
return 0;
}
int
comp_cv_qualification (const_tree type1, const_tree type2)
{
int q1 = cp_type_quals (type1);
int q2 = cp_type_quals (type2);
return comp_cv_qualification (q1, q2);
}
/* Returns 1 if the cv-qualification signature of TYPE1 is a proper
subset of the cv-qualification signature of TYPE2, and the types
are similar. Returns -1 if the other way 'round, and 0 otherwise. */
int
comp_cv_qual_signature (tree type1, tree type2)
{
if (comp_ptr_ttypes_real (type2, type1, -1))
return 1;
else if (comp_ptr_ttypes_real (type1, type2, -1))
return -1;
else
return 0;
}
/* Subroutines of `comptypes'. */
/* Return true if two parameter type lists PARMS1 and PARMS2 are
equivalent in the sense that functions with those parameter types
can have equivalent types. The two lists must be equivalent,
element by element. */
bool
compparms (const_tree parms1, const_tree parms2)
{
const_tree t1, t2;
/* An unspecified parmlist matches any specified parmlist
whose argument types don't need default promotions. */
for (t1 = parms1, t2 = parms2;
t1 || t2;
t1 = TREE_CHAIN (t1), t2 = TREE_CHAIN (t2))
{
/* If one parmlist is shorter than the other,
they fail to match. */
if (!t1 || !t2)
return false;
if (!same_type_p (TREE_VALUE (t1), TREE_VALUE (t2)))
return false;
}
return true;
}
/* Process a sizeof or alignof expression where the operand is a type.
STD_ALIGNOF indicates whether an alignof has C++11 (minimum alignment)
or GNU (preferred alignment) semantics; it is ignored if OP is
SIZEOF_EXPR. */
tree
cxx_sizeof_or_alignof_type (location_t loc, tree type, enum tree_code op,
bool std_alignof, bool complain)
{
gcc_assert (op == SIZEOF_EXPR || op == ALIGNOF_EXPR);
if (type == error_mark_node)
return error_mark_node;
type = non_reference (type);
if (TREE_CODE (type) == METHOD_TYPE)
{
if (complain)
{
pedwarn (loc, OPT_Wpointer_arith,
"invalid application of %qs to a member function",
OVL_OP_INFO (false, op)->name);
return size_one_node;
}
else
return error_mark_node;
}
else if (VOID_TYPE_P (type) && std_alignof)
{
if (complain)
error_at (loc, "invalid application of %qs to a void type",
OVL_OP_INFO (false, op)->name);
return error_mark_node;
}
bool dependent_p = dependent_type_p (type);
if (!dependent_p)
complete_type (type);
if (dependent_p
/* VLA types will have a non-constant size. In the body of an
uninstantiated template, we don't need to try to compute the
value, because the sizeof expression is not an integral
constant expression in that case. And, if we do try to
compute the value, we'll likely end up with SAVE_EXPRs, which
the template substitution machinery does not expect to see. */
|| (processing_template_decl
&& COMPLETE_TYPE_P (type)
&& TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST))
{
tree value = build_min (op, size_type_node, type);
TREE_READONLY (value) = 1;
if (op == ALIGNOF_EXPR && std_alignof)
ALIGNOF_EXPR_STD_P (value) = true;
SET_EXPR_LOCATION (value, loc);
return value;
}
return c_sizeof_or_alignof_type (loc, complete_type (type),
op == SIZEOF_EXPR, std_alignof,
complain);
}
/* Return the size of the type, without producing any warnings for
types whose size cannot be taken. This routine should be used only
in some other routine that has already produced a diagnostic about
using the size of such a type. */
tree
cxx_sizeof_nowarn (tree type)
{
if (TREE_CODE (type) == FUNCTION_TYPE
|| VOID_TYPE_P (type)
|| TREE_CODE (type) == ERROR_MARK)
return size_one_node;
else if (!COMPLETE_TYPE_P (type))
return size_zero_node;
else
return cxx_sizeof_or_alignof_type (input_location, type,
SIZEOF_EXPR, false, false);
}
/* Process a sizeof expression where the operand is an expression. */
static tree
cxx_sizeof_expr (location_t loc, tree e, tsubst_flags_t complain)
{
if (e == error_mark_node)
return error_mark_node;
if (instantiation_dependent_uneval_expression_p (e))
{
e = build_min (SIZEOF_EXPR, size_type_node, e);
TREE_SIDE_EFFECTS (e) = 0;
TREE_READONLY (e) = 1;
SET_EXPR_LOCATION (e, loc);
return e;
}
location_t e_loc = cp_expr_loc_or_loc (e, loc);
STRIP_ANY_LOCATION_WRAPPER (e);
/* To get the size of a static data member declared as an array of
unknown bound, we need to instantiate it. */
if (VAR_P (e)
&& VAR_HAD_UNKNOWN_BOUND (e)
&& DECL_TEMPLATE_INSTANTIATION (e))
instantiate_decl (e, /*defer_ok*/true, /*expl_inst_mem*/false);
if (TREE_CODE (e) == PARM_DECL
&& DECL_ARRAY_PARAMETER_P (e)
&& (complain & tf_warning))
{
auto_diagnostic_group d;
if (warning_at (e_loc, OPT_Wsizeof_array_argument,
"%<sizeof%> on array function parameter %qE "
"will return size of %qT", e, TREE_TYPE (e)))
inform (DECL_SOURCE_LOCATION (e), "declared here");
}
e = mark_type_use (e);
if (bitfield_p (e))
{
if (complain & tf_error)
error_at (e_loc,
"invalid application of %<sizeof%> to a bit-field");
else
return error_mark_node;
e = char_type_node;
}
else if (is_overloaded_fn (e))
{
if (complain & tf_error)
permerror (e_loc, "ISO C++ forbids applying %<sizeof%> to "
"an expression of function type");
else
return error_mark_node;
e = char_type_node;
}
else if (type_unknown_p (e))
{
if (complain & tf_error)
cxx_incomplete_type_error (e_loc, e, TREE_TYPE (e));
else
return error_mark_node;
e = char_type_node;
}
else
e = TREE_TYPE (e);
return cxx_sizeof_or_alignof_type (loc, e, SIZEOF_EXPR, false,
complain & tf_error);
}
/* Implement the __alignof keyword: Return the minimum required
alignment of E, measured in bytes. For VAR_DECL's and
FIELD_DECL's return DECL_ALIGN (which can be set from an
"aligned" __attribute__ specification). STD_ALIGNOF acts
like in cxx_sizeof_or_alignof_type. */
static tree
cxx_alignof_expr (location_t loc, tree e, bool std_alignof,
tsubst_flags_t complain)
{
tree t;
if (e == error_mark_node)
return error_mark_node;
if (processing_template_decl)
{
e = build_min (ALIGNOF_EXPR, size_type_node, e);
TREE_SIDE_EFFECTS (e) = 0;
TREE_READONLY (e) = 1;
SET_EXPR_LOCATION (e, loc);
ALIGNOF_EXPR_STD_P (e) = std_alignof;
return e;
}
location_t e_loc = cp_expr_loc_or_loc (e, loc);
STRIP_ANY_LOCATION_WRAPPER (e);
e = mark_type_use (e);
if (!verify_type_context (loc, TCTX_ALIGNOF, TREE_TYPE (e),
!(complain & tf_error)))
{
if (!(complain & tf_error))
return error_mark_node;
t = size_one_node;
}
else if (VAR_P (e))
t = size_int (DECL_ALIGN_UNIT (e));
else if (bitfield_p (e))
{
if (complain & tf_error)
error_at (e_loc,
"invalid application of %<__alignof%> to a bit-field");
else
return error_mark_node;
t = size_one_node;
}
else if (TREE_CODE (e) == COMPONENT_REF
&& TREE_CODE (TREE_OPERAND (e, 1)) == FIELD_DECL)
t = size_int (DECL_ALIGN_UNIT (TREE_OPERAND (e, 1)));
else if (is_overloaded_fn (e))
{
if (complain & tf_error)
permerror (e_loc, "ISO C++ forbids applying %<__alignof%> to "
"an expression of function type");
else
return error_mark_node;
if (TREE_CODE (e) == FUNCTION_DECL)
t = size_int (DECL_ALIGN_UNIT (e));
else
t = size_one_node;
}
else if (type_unknown_p (e))
{
if (complain & tf_error)
cxx_incomplete_type_error (e_loc, e, TREE_TYPE (e));
else
return error_mark_node;
t = size_one_node;
}
else
return cxx_sizeof_or_alignof_type (loc, TREE_TYPE (e),
ALIGNOF_EXPR, std_alignof,
complain & tf_error);
return fold_convert_loc (loc, size_type_node, t);
}
/* Process a sizeof or alignof expression E with code OP where the operand
is an expression. STD_ALIGNOF acts like in cxx_sizeof_or_alignof_type. */
tree
cxx_sizeof_or_alignof_expr (location_t loc, tree e, enum tree_code op,
bool std_alignof, bool complain)
{
gcc_assert (op == SIZEOF_EXPR || op == ALIGNOF_EXPR);
if (op == SIZEOF_EXPR)
return cxx_sizeof_expr (loc, e, complain? tf_warning_or_error : tf_none);
else
return cxx_alignof_expr (loc, e, std_alignof,
complain? tf_warning_or_error : tf_none);
}
/* Build a representation of an expression 'alignas(E).' Return the
folded integer value of E if it is an integral constant expression
that resolves to a valid alignment. If E depends on a template
parameter, return a syntactic representation tree of kind
ALIGNOF_EXPR. Otherwise, return an error_mark_node if the
expression is ill formed, or NULL_TREE if E is NULL_TREE. */
tree
cxx_alignas_expr (tree e)
{
if (e == NULL_TREE || e == error_mark_node
|| (!TYPE_P (e) && !require_potential_rvalue_constant_expression (e)))
return e;
if (TYPE_P (e))
/* [dcl.align]/3:
When the alignment-specifier is of the form
alignas(type-id), it shall have the same effect as
alignas(alignof(type-id)). */
return cxx_sizeof_or_alignof_type (input_location,
e, ALIGNOF_EXPR,
/*std_alignof=*/true,
/*complain=*/true);
/* If we reach this point, it means the alignas expression if of
the form "alignas(assignment-expression)", so we should follow
what is stated by [dcl.align]/2. */
if (value_dependent_expression_p (e))
/* Leave value-dependent expression alone for now. */
return e;
e = instantiate_non_dependent_expr (e);
e = mark_rvalue_use (e);
/* [dcl.align]/2 says:
the assignment-expression shall be an integral constant
expression. */
if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (e)))
{
error ("%<alignas%> argument has non-integral type %qT", TREE_TYPE (e));
return error_mark_node;
}
return cxx_constant_value (e);
}
/* EXPR is being used in a context that is not a function call.
Enforce:
[expr.ref]
The expression can be used only as the left-hand operand of a
member function call.
[expr.mptr.operator]
If the result of .* or ->* is a function, then that result can be
used only as the operand for the function call operator ().
by issuing an error message if appropriate. Returns true iff EXPR
violates these rules. */
bool
invalid_nonstatic_memfn_p (location_t loc, tree expr, tsubst_flags_t complain)
{
if (expr == NULL_TREE)
return false;
/* Don't enforce this in MS mode. */
if (flag_ms_extensions)
return false;
if (is_overloaded_fn (expr) && !really_overloaded_fn (expr))
expr = get_first_fn (expr);
if (TREE_TYPE (expr)
&& DECL_NONSTATIC_MEMBER_FUNCTION_P (expr))
{
if (complain & tf_error)
{
if (DECL_P (expr))
{
error_at (loc, "invalid use of non-static member function %qD",
expr);
inform (DECL_SOURCE_LOCATION (expr), "declared here");
}
else
error_at (loc, "invalid use of non-static member function of "
"type %qT", TREE_TYPE (expr));
}
return true;
}
return false;
}
/* If EXP is a reference to a bit-field, and the type of EXP does not
match the declared type of the bit-field, return the declared type
of the bit-field. Otherwise, return NULL_TREE. */
tree
is_bitfield_expr_with_lowered_type (const_tree exp)
{
switch (TREE_CODE (exp))
{
case COND_EXPR:
if (!is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1)
? TREE_OPERAND (exp, 1)
: TREE_OPERAND (exp, 0)))
return NULL_TREE;
return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 2));
case COMPOUND_EXPR:
return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 1));
case MODIFY_EXPR:
case SAVE_EXPR:
case UNARY_PLUS_EXPR:
case PREDECREMENT_EXPR:
case PREINCREMENT_EXPR:
case POSTDECREMENT_EXPR:
case POSTINCREMENT_EXPR:
case NEGATE_EXPR:
case NON_LVALUE_EXPR:
case BIT_NOT_EXPR:
return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0));
case COMPONENT_REF:
{
tree field;
field = TREE_OPERAND (exp, 1);
if (TREE_CODE (field) != FIELD_DECL || !DECL_BIT_FIELD_TYPE (field))
return NULL_TREE;
if (same_type_ignoring_top_level_qualifiers_p
(TREE_TYPE (exp), DECL_BIT_FIELD_TYPE (field)))
return NULL_TREE;
return DECL_BIT_FIELD_TYPE (field);
}
case VAR_DECL:
if (DECL_HAS_VALUE_EXPR_P (exp))
return is_bitfield_expr_with_lowered_type (DECL_VALUE_EXPR
(CONST_CAST_TREE (exp)));
return NULL_TREE;
case VIEW_CONVERT_EXPR:
if (location_wrapper_p (exp))
return is_bitfield_expr_with_lowered_type (TREE_OPERAND (exp, 0));
else
return NULL_TREE;
default:
return NULL_TREE;
}
}
/* Like is_bitfield_with_lowered_type, except that if EXP is not a
bitfield with a lowered type, the type of EXP is returned, rather
than NULL_TREE. */
tree
unlowered_expr_type (const_tree exp)
{
tree type;
tree etype = TREE_TYPE (exp);
type = is_bitfield_expr_with_lowered_type (exp);
if (type)
type = cp_build_qualified_type (type, cp_type_quals (etype));
else
type = etype;
return type;
}
/* Perform the conversions in [expr] that apply when an lvalue appears
in an rvalue context: the lvalue-to-rvalue, array-to-pointer, and
function-to-pointer conversions. In addition, bitfield references are
converted to their declared types. Note that this function does not perform
the lvalue-to-rvalue conversion for class types. If you need that conversion
for class types, then you probably need to use force_rvalue.
Although the returned value is being used as an rvalue, this
function does not wrap the returned expression in a
NON_LVALUE_EXPR; the caller is expected to be mindful of the fact
that the return value is no longer an lvalue. */
tree
decay_conversion (tree exp,
tsubst_flags_t complain,
bool reject_builtin /* = true */)
{
tree type;
enum tree_code code;
location_t loc = cp_expr_loc_or_input_loc (exp);
type = TREE_TYPE (exp);
if (type == error_mark_node)
return error_mark_node;
exp = resolve_nondeduced_context_or_error (exp, complain);
code = TREE_CODE (type);
if (error_operand_p (exp))
return error_mark_node;
if (NULLPTR_TYPE_P (type) && !TREE_SIDE_EFFECTS (exp))
{
mark_rvalue_use (exp, loc, reject_builtin);
return nullptr_node;
}
/* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
Leave such NOP_EXPRs, since RHS is being used in non-lvalue context. */
if (code == VOID_TYPE)
{
if (complain & tf_error)
error_at (loc, "void value not ignored as it ought to be");
return error_mark_node;
}
if (invalid_nonstatic_memfn_p (loc, exp, complain))
return error_mark_node;
if (code == FUNCTION_TYPE || is_overloaded_fn (exp))
{
exp = mark_lvalue_use (exp);
if (reject_builtin && reject_gcc_builtin (exp, loc))
return error_mark_node;
return cp_build_addr_expr (exp, complain);
}
if (code == ARRAY_TYPE)
{
tree adr;
tree ptrtype;
exp = mark_lvalue_use (exp);
if (INDIRECT_REF_P (exp))
return build_nop (build_pointer_type (TREE_TYPE (type)),
TREE_OPERAND (exp, 0));
if (TREE_CODE (exp) == COMPOUND_EXPR)
{
tree op1 = decay_conversion (TREE_OPERAND (exp, 1), complain);
if (op1 == error_mark_node)
return error_mark_node;
return build2 (COMPOUND_EXPR, TREE_TYPE (op1),
TREE_OPERAND (exp, 0), op1);
}
if (!obvalue_p (exp)
&& ! (TREE_CODE (exp) == CONSTRUCTOR && TREE_STATIC (exp)))
{
if (complain & tf_error)
error_at (loc, "invalid use of non-lvalue array");
return error_mark_node;
}
ptrtype = build_pointer_type (TREE_TYPE (type));
if (VAR_P (exp))
{
if (!cxx_mark_addressable (exp))
return error_mark_node;
adr = build_nop (ptrtype, build_address (exp));
return adr;
}
/* This way is better for a COMPONENT_REF since it can
simplify the offset for a component. */
adr = cp_build_addr_expr (exp, complain);
return cp_convert (ptrtype, adr, complain);
}
/* Otherwise, it's the lvalue-to-rvalue conversion. */
exp = mark_rvalue_use (exp, loc, reject_builtin);
/* If a bitfield is used in a context where integral promotion
applies, then the caller is expected to have used
default_conversion. That function promotes bitfields correctly
before calling this function. At this point, if we have a
bitfield referenced, we may assume that is not subject to
promotion, and that, therefore, the type of the resulting rvalue
is the declared type of the bitfield. */
exp = convert_bitfield_to_declared_type (exp);
/* We do not call rvalue() here because we do not want to wrap EXP
in a NON_LVALUE_EXPR. */
/* [basic.lval]
Non-class rvalues always have cv-unqualified types. */
type = TREE_TYPE (exp);
if (!CLASS_TYPE_P (type) && cv_qualified_p (type))
exp = build_nop (cv_unqualified (type), exp);
if (!complete_type_or_maybe_complain (type, exp, complain))
return error_mark_node;
return exp;
}
/* Perform preparatory conversions, as part of the "usual arithmetic
conversions". In particular, as per [expr]:
Whenever an lvalue expression appears as an operand of an
operator that expects the rvalue for that operand, the
lvalue-to-rvalue, array-to-pointer, or function-to-pointer
standard conversions are applied to convert the expression to an
rvalue.
In addition, we perform integral promotions here, as those are
applied to both operands to a binary operator before determining
what additional conversions should apply. */
static tree
cp_default_conversion (tree exp, tsubst_flags_t complain)
{
/* Check for target-specific promotions. */
tree promoted_type = targetm.promoted_type (TREE_TYPE (exp));
if (promoted_type)
exp = cp_convert (promoted_type, exp, complain);
/* Perform the integral promotions first so that bitfield
expressions (which may promote to "int", even if the bitfield is
declared "unsigned") are promoted correctly. */
else if (INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (exp)))
exp = cp_perform_integral_promotions (exp, complain);
/* Perform the other conversions. */
exp = decay_conversion (exp, complain);
return exp;
}
/* C version. */
tree
default_conversion (tree exp)
{
return cp_default_conversion (exp, tf_warning_or_error);
}
/* EXPR is an expression with an integral or enumeration type.
Perform the integral promotions in [conv.prom], and return the
converted value. */
tree
cp_perform_integral_promotions (tree expr, tsubst_flags_t complain)
{
tree type;
tree promoted_type;
expr = mark_rvalue_use (expr);
if (error_operand_p (expr))
return error_mark_node;
type = TREE_TYPE (expr);
/* [conv.prom]
A prvalue for an integral bit-field (11.3.9) can be converted to a prvalue
of type int if int can represent all the values of the bit-field;
otherwise, it can be converted to unsigned int if unsigned int can
represent all the values of the bit-field. If the bit-field is larger yet,
no integral promotion applies to it. If the bit-field has an enumerated
type, it is treated as any other value of that type for promotion
purposes. */
tree bitfield_type = is_bitfield_expr_with_lowered_type (expr);
if (bitfield_type
&& (TREE_CODE (bitfield_type) == ENUMERAL_TYPE
|| TYPE_PRECISION (type) > TYPE_PRECISION (integer_type_node)))
type = bitfield_type;
gcc_assert (INTEGRAL_OR_ENUMERATION_TYPE_P (type));
/* Scoped enums don't promote. */
if (SCOPED_ENUM_P (type))
return expr;
promoted_type = type_promotes_to (type);
if (type != promoted_type)
expr = cp_convert (promoted_type, expr, complain);
else if (bitfield_type && bitfield_type != type)
/* Prevent decay_conversion from converting to bitfield_type. */
expr = build_nop (type, expr);
return expr;
}
/* C version. */
tree
perform_integral_promotions (tree expr)
{
return cp_perform_integral_promotions (expr, tf_warning_or_error);
}
/* Returns nonzero iff exp is a STRING_CST or the result of applying
decay_conversion to one. */
int
string_conv_p (const_tree totype, const_tree exp, int warn)
{
tree t;
if (!TYPE_PTR_P (totype))
return 0;
t = TREE_TYPE (totype);
if (!same_type_p (t, char_type_node)
&& !same_type_p (t, char8_type_node)
&& !same_type_p (t, char16_type_node)
&& !same_type_p (t, char32_type_node)
&& !same_type_p (t, wchar_type_node))
return 0;
location_t loc = EXPR_LOC_OR_LOC (exp, input_location);
STRIP_ANY_LOCATION_WRAPPER (exp);
if (TREE_CODE (exp) == STRING_CST)
{
/* Make sure that we don't try to convert between char and wide chars. */
if (!same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (exp))), t))
return 0;
}
else
{
/* Is this a string constant which has decayed to 'const char *'? */
t = build_pointer_type (cp_build_qualified_type (t, TYPE_QUAL_CONST));
if (!same_type_p (TREE_TYPE (exp), t))
return 0;
STRIP_NOPS (exp);
if (TREE_CODE (exp) != ADDR_EXPR
|| TREE_CODE (TREE_OPERAND (exp, 0)) != STRING_CST)
return 0;
}
if (warn)
{
if (cxx_dialect >= cxx11)
pedwarn (loc, OPT_Wwrite_strings,
"ISO C++ forbids converting a string constant to %qT",
totype);
else
warning_at (loc, OPT_Wwrite_strings,
"deprecated conversion from string constant to %qT",
totype);
}
return 1;
}
/* Given a COND_EXPR, MIN_EXPR, or MAX_EXPR in T, return it in a form that we
can, for example, use as an lvalue. This code used to be in
unary_complex_lvalue, but we needed it to deal with `a = (d == c) ? b : c'
expressions, where we're dealing with aggregates. But now it's again only
called from unary_complex_lvalue. The case (in particular) that led to
this was with CODE == ADDR_EXPR, since it's not an lvalue when we'd
get it there. */
static tree
rationalize_conditional_expr (enum tree_code code, tree t,
tsubst_flags_t complain)
{
location_t loc = cp_expr_loc_or_input_loc (t);
/* For MIN_EXPR or MAX_EXPR, fold-const.cc has arranged things so that
the first operand is always the one to be used if both operands
are equal, so we know what conditional expression this used to be. */
if (TREE_CODE (t) == MIN_EXPR || TREE_CODE (t) == MAX_EXPR)
{
tree op0 = TREE_OPERAND (t, 0);
tree op1 = TREE_OPERAND (t, 1);
/* The following code is incorrect if either operand side-effects. */
gcc_assert (!TREE_SIDE_EFFECTS (op0)
&& !TREE_SIDE_EFFECTS (op1));
return
build_conditional_expr (loc,
build_x_binary_op (loc,
(TREE_CODE (t) == MIN_EXPR
? LE_EXPR : GE_EXPR),
op0, TREE_CODE (op0),
op1, TREE_CODE (op1),
NULL_TREE,
/*overload=*/NULL,
complain),
cp_build_unary_op (code, op0, false, complain),
cp_build_unary_op (code, op1, false, complain),
complain);
}
tree op1 = TREE_OPERAND (t, 1);
if (TREE_CODE (op1) != THROW_EXPR)
op1 = cp_build_unary_op (code, op1, false, complain);
tree op2 = TREE_OPERAND (t, 2);
if (TREE_CODE (op2) != THROW_EXPR)
op2 = cp_build_unary_op (code, op2, false, complain);
return
build_conditional_expr (loc, TREE_OPERAND (t, 0), op1, op2, complain);
}
/* Given the TYPE of an anonymous union field inside T, return the
FIELD_DECL for the field. If not found return NULL_TREE. Because
anonymous unions can nest, we must also search all anonymous unions
that are directly reachable. */
tree
lookup_anon_field (tree, tree type)
{
tree field;
type = TYPE_MAIN_VARIANT (type);
field = ANON_AGGR_TYPE_FIELD (type);
gcc_assert (field);
return field;
}
/* Build an expression representing OBJECT.MEMBER. OBJECT is an
expression; MEMBER is a DECL or baselink. If ACCESS_PATH is
non-NULL, it indicates the path to the base used to name MEMBER.
If PRESERVE_REFERENCE is true, the expression returned will have
REFERENCE_TYPE if the MEMBER does. Otherwise, the expression
returned will have the type referred to by the reference.
This function does not perform access control; that is either done
earlier by the parser when the name of MEMBER is resolved to MEMBER
itself, or later when overload resolution selects one of the
functions indicated by MEMBER. */
tree
build_class_member_access_expr (cp_expr object, tree member,
tree access_path, bool preserve_reference,
tsubst_flags_t complain)
{
tree object_type;
tree member_scope;
tree result = NULL_TREE;
tree using_decl = NULL_TREE;
if (error_operand_p (object) || error_operand_p (member))
return error_mark_node;
gcc_assert (DECL_P (member) || BASELINK_P (member));
/* [expr.ref]
The type of the first expression shall be "class object" (of a
complete type). */
object_type = TREE_TYPE (object);
if (!currently_open_class (object_type)
&& !complete_type_or_maybe_complain (object_type, object, complain))
return error_mark_node;
if (!CLASS_TYPE_P (object_type))
{
if (complain & tf_error)
{
if (INDIRECT_TYPE_P (object_type)
&& CLASS_TYPE_P (TREE_TYPE (object_type)))
error ("request for member %qD in %qE, which is of pointer "
"type %qT (maybe you meant to use %<->%> ?)",
member, object.get_value (), object_type);
else
error ("request for member %qD in %qE, which is of non-class "
"type %qT", member, object.get_value (), object_type);
}
return error_mark_node;
}
/* The standard does not seem to actually say that MEMBER must be a
member of OBJECT_TYPE. However, that is clearly what is
intended. */
if (DECL_P (member))
{
member_scope = DECL_CLASS_CONTEXT (member);
if (!mark_used (member, complain) && !(complain & tf_error))
return error_mark_node;
if (TREE_UNAVAILABLE (member))
error_unavailable_use (member, NULL_TREE);
else if (TREE_DEPRECATED (member))
warn_deprecated_use (member, NULL_TREE);
}
else
member_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (member));
/* If MEMBER is from an anonymous aggregate, MEMBER_SCOPE will
presently be the anonymous union. Go outwards until we find a
type related to OBJECT_TYPE. */
while ((ANON_AGGR_TYPE_P (member_scope) || UNSCOPED_ENUM_P (member_scope))
&& !same_type_ignoring_top_level_qualifiers_p (member_scope,
object_type))
member_scope = TYPE_CONTEXT (member_scope);
if (!member_scope || !DERIVED_FROM_P (member_scope, object_type))
{
if (complain & tf_error)
{
if (TREE_CODE (member) == FIELD_DECL)
error ("invalid use of non-static data member %qE", member);
else
error ("%qD is not a member of %qT", member, object_type);
}
return error_mark_node;
}
/* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x' into
`(*(a ? &b : &c)).x', and so on. A COND_EXPR is only an lvalue
in the front end; only _DECLs and _REFs are lvalues in the back end. */
if (tree temp = unary_complex_lvalue (ADDR_EXPR, object))
{
temp = cp_build_fold_indirect_ref (temp);
if (!lvalue_p (object) && lvalue_p (temp))
/* Preserve rvalueness. */
temp = move (temp);
object = temp;
}
/* In [expr.ref], there is an explicit list of the valid choices for
MEMBER. We check for each of those cases here. */
if (VAR_P (member))
{
/* A static data member. */
result = member;
mark_exp_read (object);
if (tree wrap = maybe_get_tls_wrapper_call (result))
/* Replace an evaluated use of the thread_local variable with
a call to its wrapper. */
result = wrap;
/* If OBJECT has side-effects, they are supposed to occur. */
if (TREE_SIDE_EFFECTS (object))
result = build2 (COMPOUND_EXPR, TREE_TYPE (result), object, result);
}
else if (TREE_CODE (member) == FIELD_DECL)
{
/* A non-static data member. */
bool null_object_p;
int type_quals;
tree member_type;
if (INDIRECT_REF_P (object))
null_object_p =
integer_zerop (tree_strip_nop_conversions (TREE_OPERAND (object, 0)));
else
null_object_p = false;
/* Convert OBJECT to the type of MEMBER. */
if (!same_type_p (TYPE_MAIN_VARIANT (object_type),
TYPE_MAIN_VARIANT (member_scope)))
{
tree binfo;
base_kind kind;
/* We didn't complain above about a currently open class, but now we
must: we don't know how to refer to a base member before layout is
complete. But still don't complain in a template. */
if (!cp_unevaluated_operand
&& !dependent_type_p (object_type)
&& !complete_type_or_maybe_complain (object_type, object,
complain))
return error_mark_node;
binfo = lookup_base (access_path ? access_path : object_type,
member_scope, ba_unique, &kind, complain);
if (binfo == error_mark_node)
return error_mark_node;
/* It is invalid to try to get to a virtual base of a
NULL object. The most common cause is invalid use of
offsetof macro. */
if (null_object_p && kind == bk_via_virtual)
{
if (complain & tf_error)
{
error ("invalid access to non-static data member %qD in "
"virtual base of NULL object", member);
}
return error_mark_node;
}
/* Convert to the base. */
object = build_base_path (PLUS_EXPR, object, binfo,
/*nonnull=*/1, complain);
/* If we found the base successfully then we should be able
to convert to it successfully. */
gcc_assert (object != error_mark_node);
}
/* If MEMBER is from an anonymous aggregate, we have converted
OBJECT so that it refers to the class containing the
anonymous union. Generate a reference to the anonymous union
itself, and recur to find MEMBER. */
if (ANON_AGGR_TYPE_P (DECL_CONTEXT (member))
/* When this code is called from build_field_call, the
object already has the type of the anonymous union.
That is because the COMPONENT_REF was already
constructed, and was then disassembled before calling
build_field_call. After the function-call code is
cleaned up, this waste can be eliminated. */
&& (!same_type_ignoring_top_level_qualifiers_p
(TREE_TYPE (object), DECL_CONTEXT (member))))
{
tree anonymous_union;
anonymous_union = lookup_anon_field (TREE_TYPE (object),
DECL_CONTEXT (member));
object = build_class_member_access_expr (object,
anonymous_union,
/*access_path=*/NULL_TREE,
preserve_reference,
complain);
}
/* Compute the type of the field, as described in [expr.ref]. */
type_quals = TYPE_UNQUALIFIED;
member_type = TREE_TYPE (member);
if (!TYPE_REF_P (member_type))
{
type_quals = (cp_type_quals (member_type)
| cp_type_quals (object_type));
/* A field is const (volatile) if the enclosing object, or the
field itself, is const (volatile). But, a mutable field is
not const, even within a const object. */
if (DECL_MUTABLE_P (member))
type_quals &= ~TYPE_QUAL_CONST;
member_type = cp_build_qualified_type (member_type, type_quals);
}
result = build3_loc (input_location, COMPONENT_REF, member_type,
object, member, NULL_TREE);
/* Mark the expression const or volatile, as appropriate. Even
though we've dealt with the type above, we still have to mark the
expression itself. */
if (type_quals & TYPE_QUAL_CONST)
TREE_READONLY (result) = 1;
if (type_quals & TYPE_QUAL_VOLATILE)
TREE_THIS_VOLATILE (result) = 1;
}
else if (BASELINK_P (member))
{
/* The member is a (possibly overloaded) member function. */
tree functions;
tree type;
/* If the MEMBER is exactly one static member function, then we
know the type of the expression. Otherwise, we must wait
until overload resolution has been performed. */
functions = BASELINK_FUNCTIONS (member);
if (TREE_CODE (functions) == FUNCTION_DECL
&& DECL_STATIC_FUNCTION_P (functions))
type = TREE_TYPE (functions);
else
type = unknown_type_node;
/* Note that we do not convert OBJECT to the BASELINK_BINFO
base. That will happen when the function is called. */
result = build3_loc (input_location, COMPONENT_REF, type, object, member,
NULL_TREE);
}
else if (TREE_CODE (member) == CONST_DECL)
{
/* The member is an enumerator. */
result = member;
/* If OBJECT has side-effects, they are supposed to occur. */
if (TREE_SIDE_EFFECTS (object))
result = build2 (COMPOUND_EXPR, TREE_TYPE (result),
object, result);
}
else if ((using_decl = strip_using_decl (member)) != member)
result = build_class_member_access_expr (object,
using_decl,
access_path, preserve_reference,
complain);
else
{
if (complain & tf_error)
error ("invalid use of %qD", member);
return error_mark_node;
}
if (!preserve_reference)
/* [expr.ref]
If E2 is declared to have type "reference to T", then ... the
type of E1.E2 is T. */
result = convert_from_reference (result);
return result;
}
/* Return the destructor denoted by OBJECT.SCOPE::DTOR_NAME, or, if
SCOPE is NULL, by OBJECT.DTOR_NAME, where DTOR_NAME is ~type. */
tree
lookup_destructor (tree object, tree scope, tree dtor_name,
tsubst_flags_t complain)
{
tree object_type = TREE_TYPE (object);
tree dtor_type = TREE_OPERAND (dtor_name, 0);
tree expr;
/* We've already complained about this destructor. */
if (dtor_type == error_mark_node)
return error_mark_node;
if (scope && !check_dtor_name (scope, dtor_type))
{
if (complain & tf_error)
error ("qualified type %qT does not match destructor name ~%qT",
scope, dtor_type);
return error_mark_node;
}
if (is_auto (dtor_type))
dtor_type = object_type;
else if (identifier_p (dtor_type))
{
/* In a template, names we can't find a match for are still accepted
destructor names, and we check them here. */
if (check_dtor_name (object_type, dtor_type))
dtor_type = object_type;
else
{
if (complain & tf_error)
error ("object type %qT does not match destructor name ~%qT",
object_type, dtor_type);
return error_mark_node;
}
}
else if (!DERIVED_FROM_P (dtor_type, TYPE_MAIN_VARIANT (object_type)))
{
if (complain & tf_error)
error ("the type being destroyed is %qT, but the destructor "
"refers to %qT", TYPE_MAIN_VARIANT (object_type), dtor_type);
return error_mark_node;
}
expr = lookup_member (dtor_type, complete_dtor_identifier,
/*protect=*/1, /*want_type=*/false,
tf_warning_or_error);
if (!expr)
{
if (complain & tf_error)
cxx_incomplete_type_error (dtor_name, dtor_type);
return error_mark_node;
}
expr = (adjust_result_of_qualified_name_lookup
(expr, dtor_type, object_type));
if (scope == NULL_TREE)
/* We need to call adjust_result_of_qualified_name_lookup in case the
destructor names a base class, but we unset BASELINK_QUALIFIED_P so
that we still get virtual function binding. */
BASELINK_QUALIFIED_P (expr) = false;
return expr;
}
/* An expression of the form "A::template B" has been resolved to
DECL. Issue a diagnostic if B is not a template or template
specialization. */
void
check_template_keyword (tree decl)
{
/* The standard says:
[temp.names]
If a name prefixed by the keyword template is not a member
template, the program is ill-formed.
DR 228 removed the restriction that the template be a member
template.
DR 96, if accepted would add the further restriction that explicit
template arguments must be provided if the template keyword is
used, but, as of 2005-10-16, that DR is still in "drafting". If
this DR is accepted, then the semantic checks here can be
simplified, as the entity named must in fact be a template
specialization, rather than, as at present, a set of overloaded
functions containing at least one template function. */
if (TREE_CODE (decl) != TEMPLATE_DECL
&& TREE_CODE (decl) != TEMPLATE_ID_EXPR)
{
if (VAR_P (decl))
{
if (DECL_USE_TEMPLATE (decl)
&& PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
;
else
permerror (input_location, "%qD is not a template", decl);
}
else if (!is_overloaded_fn (decl))
permerror (input_location, "%qD is not a template", decl);
else
{
bool found = false;
for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (decl));
!found && iter; ++iter)
{
tree fn = *iter;
if (TREE_CODE (fn) == TEMPLATE_DECL
|| TREE_CODE (fn) == TEMPLATE_ID_EXPR
|| (TREE_CODE (fn) == FUNCTION_DECL
&& DECL_USE_TEMPLATE (fn)
&& PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (fn))))
found = true;
}
if (!found)
permerror (input_location, "%qD is not a template", decl);
}
}
}
/* Record that an access failure occurred on BASETYPE_PATH attempting
to access DECL, where DIAG_DECL should be used for diagnostics. */
void
access_failure_info::record_access_failure (tree basetype_path,
tree decl, tree diag_decl)
{
m_was_inaccessible = true;
m_basetype_path = basetype_path;
m_decl = decl;
m_diag_decl = diag_decl;
}
/* If an access failure was recorded, then attempt to locate an
accessor function for the pertinent field.
Otherwise, return NULL_TREE. */
tree
access_failure_info::get_any_accessor (bool const_p) const
{
if (!was_inaccessible_p ())
return NULL_TREE;
tree accessor
= locate_field_accessor (m_basetype_path, m_diag_decl, const_p);
if (!accessor)
return NULL_TREE;
/* The accessor must itself be accessible for it to be a reasonable
suggestion. */
if (!accessible_p (m_basetype_path, accessor, true))
return NULL_TREE;
return accessor;
}
/* Add a fix-it hint to RICHLOC suggesting the use of ACCESSOR_DECL, by
replacing the primary location in RICHLOC with "accessor()". */
void
access_failure_info::add_fixit_hint (rich_location *richloc,
tree accessor_decl)
{
pretty_printer pp;
pp_string (&pp, IDENTIFIER_POINTER (DECL_NAME (accessor_decl)));
pp_string (&pp, "()");
richloc->add_fixit_replace (pp_formatted_text (&pp));
}
/* If an access failure was recorded, then attempt to locate an
accessor function for the pertinent field, and if one is
available, add a note and fix-it hint suggesting using it. */
void
access_failure_info::maybe_suggest_accessor (bool const_p) const
{
tree accessor = get_any_accessor (const_p);
if (accessor == NULL_TREE)
return;
rich_location richloc (line_table, input_location);
add_fixit_hint (&richloc, accessor);
inform (&richloc, "field %q#D can be accessed via %q#D",
m_diag_decl, accessor);
}
/* Subroutine of finish_class_member_access_expr.
Issue an error about NAME not being a member of ACCESS_PATH (or
OBJECT_TYPE), potentially providing a fix-it hint for misspelled
names. */
static void
complain_about_unrecognized_member (tree access_path, tree name,
tree object_type)
{
/* Attempt to provide a hint about misspelled names. */
tree guessed_id = lookup_member_fuzzy (access_path, name,
/*want_type=*/false);
if (guessed_id == NULL_TREE)
{
/* No hint. */
error ("%q#T has no member named %qE",
TREE_CODE (access_path) == TREE_BINFO
? TREE_TYPE (access_path) : object_type, name);
return;
}
location_t bogus_component_loc = input_location;
gcc_rich_location rich_loc (bogus_component_loc);
/* Check that the guessed name is accessible along access_path. */
access_failure_info afi;
lookup_member (access_path, guessed_id, /*protect=*/1,
/*want_type=*/false, /*complain=*/false,
&afi);
if (afi.was_inaccessible_p ())
{
tree accessor = afi.get_any_accessor (TYPE_READONLY (object_type));
if (accessor)
{
/* The guessed name isn't directly accessible, but can be accessed
via an accessor member function. */
afi.add_fixit_hint (&rich_loc, accessor);
error_at (&rich_loc,
"%q#T has no member named %qE;"
" did you mean %q#D? (accessible via %q#D)",
TREE_CODE (access_path) == TREE_BINFO
? TREE_TYPE (access_path) : object_type,
name, afi.get_diag_decl (), accessor);
}
else
{
/* The guessed name isn't directly accessible, and no accessor
member function could be found. */
error_at (&rich_loc,
"%q#T has no member named %qE;"
" did you mean %q#D? (not accessible from this context)",
TREE_CODE (access_path) == TREE_BINFO
? TREE_TYPE (access_path) : object_type,
name, afi.get_diag_decl ());
complain_about_access (afi.get_decl (), afi.get_diag_decl (),
afi.get_diag_decl (), false, ak_none);
}
}
else
{
/* The guessed name is directly accessible; suggest it. */
rich_loc.add_fixit_misspelled_id (bogus_component_loc,
guessed_id);
error_at (&rich_loc,
"%q#T has no member named %qE;"
" did you mean %qE?",
TREE_CODE (access_path) == TREE_BINFO
? TREE_TYPE (access_path) : object_type,
name, guessed_id);
}
}
/* This function is called by the parser to process a class member
access expression of the form OBJECT.NAME. NAME is a node used by
the parser to represent a name; it is not yet a DECL. It may,
however, be a BASELINK where the BASELINK_FUNCTIONS is a
TEMPLATE_ID_EXPR. Templates must be looked up by the parser, and
there is no reason to do the lookup twice, so the parser keeps the
BASELINK. TEMPLATE_P is true iff NAME was explicitly declared to
be a template via the use of the "A::template B" syntax. */
tree
finish_class_member_access_expr (cp_expr object, tree name, bool template_p,
tsubst_flags_t complain)
{
tree expr;
tree object_type;
tree member;
tree access_path = NULL_TREE;
tree orig_object = object;
tree orig_name = name;
if (object == error_mark_node || name == error_mark_node)
return error_mark_node;
/* If OBJECT is an ObjC class instance, we must obey ObjC access rules. */
if (!objc_is_public (object, name))
return error_mark_node;
object_type = TREE_TYPE (object);
if (processing_template_decl)
{
if (/* If OBJECT is dependent, so is OBJECT.NAME. */
type_dependent_object_expression_p (object)
/* If NAME is "f<args>", where either 'f' or 'args' is
dependent, then the expression is dependent. */
|| (TREE_CODE (name) == TEMPLATE_ID_EXPR
&& dependent_template_id_p (TREE_OPERAND (name, 0),
TREE_OPERAND (name, 1)))
/* If NAME is "T::X" where "T" is dependent, then the
expression is dependent. */
|| (TREE_CODE (name) == SCOPE_REF
&& TYPE_P (TREE_OPERAND (name, 0))
&& dependent_scope_p (TREE_OPERAND (name, 0)))
/* If NAME is operator T where "T" is dependent, we can't
lookup until we instantiate the T. */
|| (TREE_CODE (name) == IDENTIFIER_NODE
&& IDENTIFIER_CONV_OP_P (name)
&& dependent_type_p (TREE_TYPE (name))))
{
dependent:
return build_min_nt_loc (UNKNOWN_LOCATION, COMPONENT_REF,
orig_object, orig_name, NULL_TREE);
}
}
else if (c_dialect_objc ()
&& identifier_p (name)
&& (expr = objc_maybe_build_component_ref (object, name)))
return expr;
/* [expr.ref]
The type of the first expression shall be "class object" (of a
complete type). */
if (!currently_open_class (object_type)
&& !complete_type_or_maybe_complain (object_type, object, complain))
return error_mark_node;
if (!CLASS_TYPE_P (object_type))
{
if (complain & tf_error)
{
if (INDIRECT_TYPE_P (object_type)
&& CLASS_TYPE_P (TREE_TYPE (object_type)))
error ("request for member %qD in %qE, which is of pointer "
"type %qT (maybe you meant to use %<->%> ?)",
name, object.get_value (), object_type);
else
error ("request for member %qD in %qE, which is of non-class "
"type %qT", name, object.get_value (), object_type);
}
return error_mark_node;
}
if (BASELINK_P (name))
/* A member function that has already been looked up. */
member = name;
else
{
bool is_template_id = false;
tree template_args = NULL_TREE;
tree scope = NULL_TREE;
access_path = object_type;
if (TREE_CODE (name) == SCOPE_REF)
{
/* A qualified name. The qualifying class or namespace `S'
has already been looked up; it is either a TYPE or a
NAMESPACE_DECL. */
scope = TREE_OPERAND (name, 0);
name = TREE_OPERAND (name, 1);
/* If SCOPE is a namespace, then the qualified name does not
name a member of OBJECT_TYPE. */
if (TREE_CODE (scope) == NAMESPACE_DECL)
{
if (complain & tf_error)
error ("%<%D::%D%> is not a member of %qT",
scope, name, object_type);
return error_mark_node;
}
}
if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
{
is_template_id = true;
template_args = TREE_OPERAND (name, 1);
name = TREE_OPERAND (name, 0);
if (!identifier_p (name))
name = OVL_NAME (name);
}
if (scope)
{
if (TREE_CODE (scope) == ENUMERAL_TYPE)
{
gcc_assert (!is_template_id);
/* Looking up a member enumerator (c++/56793). */
if (!TYPE_CLASS_SCOPE_P (scope)
|| !DERIVED_FROM_P (TYPE_CONTEXT (scope), object_type))
{
if (complain & tf_error)
error ("%<%D::%D%> is not a member of %qT",
scope, name, object_type);
return error_mark_node;
}
tree val = lookup_enumerator (scope, name);
if (!val)
{
if (complain & tf_error)
error ("%qD is not a member of %qD",
name, scope);
return error_mark_node;
}
if (TREE_SIDE_EFFECTS (object))
val = build2 (COMPOUND_EXPR, TREE_TYPE (val), object, val);
return val;
}
gcc_assert (CLASS_TYPE_P (scope));
gcc_assert (identifier_p (name) || TREE_CODE (name) == BIT_NOT_EXPR);
if (constructor_name_p (name, scope))
{
if (complain & tf_error)
error ("cannot call constructor %<%T::%D%> directly",
scope, name);
return error_mark_node;
}
/* NAME may refer to a static data member, in which case there is
one copy of the data member that is shared by all the objects of
the class. So NAME can be unambiguously referred to even if
there are multiple indirect base classes containing NAME. */
const base_access ba = [scope, name] ()
{
if (identifier_p (name))
{
tree m = lookup_member (scope, name, /*protect=*/0,
/*want_type=*/false, tf_none);
if (!m || shared_member_p (m))
return ba_any;
}
return ba_check;
} ();
/* Find the base of OBJECT_TYPE corresponding to SCOPE. */
access_path = lookup_base (object_type, scope, ba, NULL, complain);
if (access_path == error_mark_node)
return error_mark_node;
if (!access_path)
{
if (any_dependent_bases_p (object_type))
goto dependent;
if (complain & tf_error)
error ("%qT is not a base of %qT", scope, object_type);
return error_mark_node;
}
}
if (TREE_CODE (name) == BIT_NOT_EXPR)
{
if (dependent_type_p (object_type))
/* The destructor isn't declared yet. */
goto dependent;
member = lookup_destructor (object, scope, name, complain);
}
else
{
/* Look up the member. */
access_failure_info afi;
if (processing_template_decl)
/* Even though this class member access expression is at this
point not dependent, the member itself may be dependent, and
we must not potentially push a access check for a dependent
member onto TI_DEFERRED_ACCESS_CHECKS. So don't check access
ahead of time here; we're going to redo this member lookup at
instantiation time anyway. */
push_deferring_access_checks (dk_no_check);
member = lookup_member (access_path, name, /*protect=*/1,
/*want_type=*/false, complain,
&afi);
if (processing_template_decl)
pop_deferring_access_checks ();
afi.maybe_suggest_accessor (TYPE_READONLY (object_type));
if (member == NULL_TREE)
{
if (dependent_type_p (object_type))
/* Try again at instantiation time. */
goto dependent;
if (complain & tf_error)
complain_about_unrecognized_member (access_path, name,
object_type);
return error_mark_node;
}
if (member == error_mark_node)
return error_mark_node;
if (DECL_P (member)
&& any_dependent_type_attributes_p (DECL_ATTRIBUTES (member)))
/* Dependent type attributes on the decl mean that the TREE_TYPE is
wrong, so don't use it. */
goto dependent;
if (TREE_CODE (member) == USING_DECL && DECL_DEPENDENT_P (member))
goto dependent;
}
if (is_template_id)
{
tree templ = member;
if (BASELINK_P (templ))
member = lookup_template_function (templ, template_args);
else if (variable_template_p (templ))
member = (lookup_and_finish_template_variable
(templ, template_args, complain));
else
{
if (complain & tf_error)
error ("%qD is not a member template function", name);
return error_mark_node;
}
}
}
if (TREE_UNAVAILABLE (member))
error_unavailable_use (member, NULL_TREE);
else if (TREE_DEPRECATED (member))
warn_deprecated_use (member, NULL_TREE);
if (template_p)
check_template_keyword (member);
expr = build_class_member_access_expr (object, member, access_path,
/*preserve_reference=*/false,
complain);
if (processing_template_decl && expr != error_mark_node)
{
if (BASELINK_P (member))
{
if (TREE_CODE (orig_name) == SCOPE_REF)
BASELINK_QUALIFIED_P (member) = 1;
orig_name = member;
}
return build_min_non_dep (COMPONENT_REF, expr,
orig_object, orig_name,
NULL_TREE);
}
return expr;
}
/* Build a COMPONENT_REF of OBJECT and MEMBER with the appropriate
type. */
tree
build_simple_component_ref (tree object, tree member)
{
tree type = cp_build_qualified_type (TREE_TYPE (member),
cp_type_quals (TREE_TYPE (object)));
return build3_loc (input_location,
COMPONENT_REF, type,
object, member, NULL_TREE);
}
/* Return an expression for the MEMBER_NAME field in the internal
representation of PTRMEM, a pointer-to-member function. (Each
pointer-to-member function type gets its own RECORD_TYPE so it is
more convenient to access the fields by name than by FIELD_DECL.)
This routine converts the NAME to a FIELD_DECL and then creates the
node for the complete expression. */
tree
build_ptrmemfunc_access_expr (tree ptrmem, tree member_name)
{
tree ptrmem_type;
tree member;
if (TREE_CODE (ptrmem) == CONSTRUCTOR)
{
for (auto &e: CONSTRUCTOR_ELTS (ptrmem))
if (e.index && DECL_P (e.index) && DECL_NAME (e.index) == member_name)
return e.value;
gcc_unreachable ();
}
/* This code is a stripped down version of
build_class_member_access_expr. It does not work to use that
routine directly because it expects the object to be of class
type. */
ptrmem_type = TREE_TYPE (ptrmem);
gcc_assert (TYPE_PTRMEMFUNC_P (ptrmem_type));
for (member = TYPE_FIELDS (ptrmem_type); member;
member = DECL_CHAIN (member))
if (DECL_NAME (member) == member_name)
break;
return build_simple_component_ref (ptrmem, member);
}
/* Return a TREE_LIST of namespace-scope overloads for the given operator,
and for any other relevant operator. */
static tree
op_unqualified_lookup (tree_code code, bool is_assign)
{
tree lookups = NULL_TREE;
if (cxx_dialect >= cxx20 && !is_assign)
{
if (code == NE_EXPR)
{
/* != can get rewritten in terms of ==. */
tree fnname = ovl_op_identifier (false, EQ_EXPR);
if (tree fns = lookup_name (fnname, LOOK_where::BLOCK_NAMESPACE))
lookups = tree_cons (fnname, fns, lookups);
}
else if (code == GT_EXPR || code == LE_EXPR
|| code == LT_EXPR || code == GE_EXPR)
{
/* These can get rewritten in terms of <=>. */
tree fnname = ovl_op_identifier (false, SPACESHIP_EXPR);
if (tree fns = lookup_name (fnname, LOOK_where::BLOCK_NAMESPACE))
lookups = tree_cons (fnname, fns, lookups);
}
}
tree fnname = ovl_op_identifier (is_assign, code);
if (tree fns = lookup_name (fnname, LOOK_where::BLOCK_NAMESPACE))
lookups = tree_cons (fnname, fns, lookups);
if (lookups)
return lookups;
else
return build_tree_list (NULL_TREE, NULL_TREE);
}
/* Create a DEPENDENT_OPERATOR_TYPE for a dependent operator expression of
the given operator. LOOKUPS, if non-NULL, is the result of phase 1
name lookup for the given operator. */
tree
build_dependent_operator_type (tree lookups, tree_code code, bool is_assign)
{
if (lookups)
/* We're partially instantiating a dependent operator expression, and
LOOKUPS is the result of phase 1 name lookup that we performed
earlier at template definition time, so just reuse the corresponding
DEPENDENT_OPERATOR_TYPE. */
return TREE_TYPE (lookups);
/* Otherwise we're processing a dependent operator expression at template
definition time, so perform phase 1 name lookup now. */
lookups = op_unqualified_lookup (code, is_assign);
tree type = cxx_make_type (DEPENDENT_OPERATOR_TYPE);
DEPENDENT_OPERATOR_TYPE_SAVED_LOOKUPS (type) = lookups;
TREE_TYPE (lookups) = type;
return type;
}
/* Given an expression PTR for a pointer, return an expression
for the value pointed to.
ERRORSTRING is the name of the operator to appear in error messages.
This function may need to overload OPERATOR_FNNAME.
Must also handle REFERENCE_TYPEs for C++. */
tree
build_x_indirect_ref (location_t loc, tree expr, ref_operator errorstring,
tree lookups, tsubst_flags_t complain)
{
tree orig_expr = expr;
tree rval;
tree overload = NULL_TREE;
if (processing_template_decl)
{
/* Retain the type if we know the operand is a pointer. */
if (TREE_TYPE (expr) && INDIRECT_TYPE_P (TREE_TYPE (expr)))
{
if (expr == current_class_ptr
|| (TREE_CODE (expr) == NOP_EXPR
&& TREE_OPERAND (expr, 0) == current_class_ptr
&& (same_type_ignoring_top_level_qualifiers_p
(TREE_TYPE (expr), TREE_TYPE (current_class_ptr)))))
return current_class_ref;
return build_min (INDIRECT_REF, TREE_TYPE (TREE_TYPE (expr)), expr);
}
if (type_dependent_expression_p (expr))
{
expr = build_min_nt_loc (loc, INDIRECT_REF, expr);
TREE_TYPE (expr)
= build_dependent_operator_type (lookups, INDIRECT_REF, false);
return expr;
}
}
rval = build_new_op (loc, INDIRECT_REF, LOOKUP_NORMAL, expr,
NULL_TREE, NULL_TREE, lookups,
&overload, complain);
if (!rval)
rval = cp_build_indirect_ref (loc, expr, errorstring, complain);
if (processing_template_decl && rval != error_mark_node)
{
if (overload != NULL_TREE)
return (build_min_non_dep_op_overload
(INDIRECT_REF, rval, overload, orig_expr));
return build_min_non_dep (INDIRECT_REF, rval, orig_expr);
}
else
return rval;
}
/* Like c-family strict_aliasing_warning, but don't warn for dependent
types or expressions. */
static bool
cp_strict_aliasing_warning (location_t loc, tree type, tree expr)
{
if (processing_template_decl)
{
tree e = expr;
STRIP_NOPS (e);
if (dependent_type_p (type) || type_dependent_expression_p (e))
return false;
}
return strict_aliasing_warning (loc, type, expr);
}
/* The implementation of the above, and of indirection implied by other
constructs. If DO_FOLD is true, fold away INDIRECT_REF of ADDR_EXPR. */
static tree
cp_build_indirect_ref_1 (location_t loc, tree ptr, ref_operator errorstring,
tsubst_flags_t complain, bool do_fold)
{
tree pointer, type;
/* RO_NULL should only be used with the folding entry points below, not
cp_build_indirect_ref. */
gcc_checking_assert (errorstring != RO_NULL || do_fold);
if (ptr == current_class_ptr
|| (TREE_CODE (ptr) == NOP_EXPR
&& TREE_OPERAND (ptr, 0) == current_class_ptr
&& (same_type_ignoring_top_level_qualifiers_p
(TREE_TYPE (ptr), TREE_TYPE (current_class_ptr)))))
return current_class_ref;
pointer = (TYPE_REF_P (TREE_TYPE (ptr))
? ptr : decay_conversion (ptr, complain));
if (pointer == error_mark_node)
return error_mark_node;
type = TREE_TYPE (pointer);
if (INDIRECT_TYPE_P (type))
{
/* [expr.unary.op]
If the type of the expression is "pointer to T," the type
of the result is "T." */
tree t = TREE_TYPE (type);
if ((CONVERT_EXPR_P (ptr)
|| TREE_CODE (ptr) == VIEW_CONVERT_EXPR)
&& (!CLASS_TYPE_P (t) || !CLASSTYPE_EMPTY_P (t)))
{
/* If a warning is issued, mark it to avoid duplicates from
the backend. This only needs to be done at
warn_strict_aliasing > 2. */
if (warn_strict_aliasing > 2
&& cp_strict_aliasing_warning (EXPR_LOCATION (ptr),
type, TREE_OPERAND (ptr, 0)))
suppress_warning (ptr, OPT_Wstrict_aliasing);
}
if (VOID_TYPE_P (t))
{
/* A pointer to incomplete type (other than cv void) can be
dereferenced [expr.unary.op]/1 */
if (complain & tf_error)
error_at (loc, "%qT is not a pointer-to-object type", type);
return error_mark_node;
}
else if (do_fold && TREE_CODE (pointer) == ADDR_EXPR
&& same_type_p (t, TREE_TYPE (TREE_OPERAND (pointer, 0))))
/* The POINTER was something like `&x'. We simplify `*&x' to
`x'. */
return TREE_OPERAND (pointer, 0);
else
{
tree ref = build1 (INDIRECT_REF, t, pointer);
/* We *must* set TREE_READONLY when dereferencing a pointer to const,
so that we get the proper error message if the result is used
to assign to. Also, &* is supposed to be a no-op. */
TREE_READONLY (ref) = CP_TYPE_CONST_P (t);
TREE_THIS_VOLATILE (ref) = CP_TYPE_VOLATILE_P (t);
TREE_SIDE_EFFECTS (ref)
= (TREE_THIS_VOLATILE (ref) || TREE_SIDE_EFFECTS (pointer));
return ref;
}
}
else if (!(complain & tf_error))
/* Don't emit any errors; we'll just return ERROR_MARK_NODE later. */
;
/* `pointer' won't be an error_mark_node if we were given a
pointer to member, so it's cool to check for this here. */
else if (TYPE_PTRMEM_P (type))
switch (errorstring)
{
case RO_ARRAY_INDEXING:
error_at (loc,
"invalid use of array indexing on pointer to member");
break;
case RO_UNARY_STAR:
error_at (loc, "invalid use of unary %<*%> on pointer to member");
break;
case RO_IMPLICIT_CONVERSION:
error_at (loc, "invalid use of implicit conversion on pointer "
"to member");
break;
case RO_ARROW_STAR:
error_at (loc, "left hand operand of %<->*%> must be a pointer to "
"class, but is a pointer to member of type %qT", type);
break;
default:
gcc_unreachable ();
}
else if (pointer != error_mark_node)
invalid_indirection_error (loc, type, errorstring);
return error_mark_node;
}
/* Entry point used by c-common, which expects folding. */
tree
build_indirect_ref (location_t loc, tree ptr, ref_operator errorstring)
{
return cp_build_indirect_ref_1 (loc, ptr, errorstring,
tf_warning_or_error, true);
}
/* Entry point used by internal indirection needs that don't correspond to any
syntactic construct. */
tree
cp_build_fold_indirect_ref (tree pointer)
{
return cp_build_indirect_ref_1 (input_location, pointer, RO_NULL,
tf_warning_or_error, true);
}
/* Entry point used by indirection needs that correspond to some syntactic
construct. */
tree
cp_build_indirect_ref (location_t loc, tree ptr, ref_operator errorstring,
tsubst_flags_t complain)
{
return cp_build_indirect_ref_1 (loc, ptr, errorstring, complain, false);
}
/* This handles expressions of the form "a[i]", which denotes
an array reference.
This is logically equivalent in C to *(a+i), but we may do it differently.
If A is a variable or a member, we generate a primitive ARRAY_REF.
This avoids forcing the array out of registers, and can work on
arrays that are not lvalues (for example, members of structures returned
by functions).
If INDEX is of some user-defined type, it must be converted to
integer type. Otherwise, to make a compatible PLUS_EXPR, it
will inherit the type of the array, which will be some pointer type.
LOC is the location to use in building the array reference. */
tree
cp_build_array_ref (location_t loc, tree array, tree idx,
tsubst_flags_t complain)
{
tree ret;
if (idx == 0)
{
if (complain & tf_error)
error_at (loc, "subscript missing in array reference");
return error_mark_node;
}
if (TREE_TYPE (array) == error_mark_node
|| TREE_TYPE (idx) == error_mark_node)
return error_mark_node;
/* If ARRAY is a COMPOUND_EXPR or COND_EXPR, move our reference
inside it. */
switch (TREE_CODE (array))
{
case COMPOUND_EXPR:
{
tree value = cp_build_array_ref (loc, TREE_OPERAND (array, 1), idx,
complain);
ret = build2 (COMPOUND_EXPR, TREE_TYPE (value),
TREE_OPERAND (array, 0), value);
SET_EXPR_LOCATION (ret, loc);
return ret;
}
case COND_EXPR:
ret = build_conditional_expr
(loc, TREE_OPERAND (array, 0),
cp_build_array_ref (loc, TREE_OPERAND (array, 1), idx,
complain),
cp_build_array_ref (loc, TREE_OPERAND (array, 2), idx,
complain),
complain);
protected_set_expr_location (ret, loc);
return ret;
default:
break;
}
bool non_lvalue = convert_vector_to_array_for_subscript (loc, &array, idx);
if (TREE_CODE (TREE_TYPE (array)) == ARRAY_TYPE)
{
tree rval, type;
warn_array_subscript_with_type_char (loc, idx);
if (!INTEGRAL_OR_UNSCOPED_ENUMERATION_TYPE_P (TREE_TYPE (idx)))
{
if (complain & tf_error)
error_at (loc, "array subscript is not an integer");
return error_mark_node;
}
/* Apply integral promotions *after* noticing character types.
(It is unclear why we do these promotions -- the standard
does not say that we should. In fact, the natural thing would
seem to be to convert IDX to ptrdiff_t; we're performing
pointer arithmetic.) */
idx = cp_perform_integral_promotions (idx, complain);
idx = maybe_fold_non_dependent_expr (idx, complain);
/* An array that is indexed by a non-constant
cannot be stored in a register; we must be able to do
address arithmetic on its address.
Likewise an array of elements of variable size. */
if (TREE_CODE (idx) != INTEGER_CST
|| (COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (array)))
&& (TREE_CODE (TYPE_SIZE (TREE_TYPE (TREE_TYPE (array))))
!= INTEGER_CST)))
{
if (!cxx_mark_addressable (array, true))
return error_mark_node;
}
/* An array that is indexed by a constant value which is not within
the array bounds cannot be stored in a register either; because we
would get a crash in store_bit_field/extract_bit_field when trying
to access a non-existent part of the register. */
if (TREE_CODE (idx) == INTEGER_CST
&& TYPE_DOMAIN (TREE_TYPE (array))
&& ! int_fits_type_p (idx, TYPE_DOMAIN (TREE_TYPE (array))))
{
if (!cxx_mark_addressable (array))
return error_mark_node;
}
/* Note in C++ it is valid to subscript a `register' array, since
it is valid to take the address of something with that
storage specification. */
if (extra_warnings)
{
tree foo = array;
while (TREE_CODE (foo) == COMPONENT_REF)
foo = TREE_OPERAND (foo, 0);
if (VAR_P (foo) && DECL_REGISTER (foo)
&& (complain & tf_warning))
warning_at (loc, OPT_Wextra,
"subscripting array declared %<register%>");
}
type = TREE_TYPE (TREE_TYPE (array));
rval = build4 (ARRAY_REF, type, array, idx, NULL_TREE, NULL_TREE);
/* Array ref is const/volatile if the array elements are
or if the array is.. */
TREE_READONLY (rval)
|= (CP_TYPE_CONST_P (type) | TREE_READONLY (array));
TREE_SIDE_EFFECTS (rval)
|= (CP_TYPE_VOLATILE_P (type) | TREE_SIDE_EFFECTS (array));
TREE_THIS_VOLATILE (rval)
|= (CP_TYPE_VOLATILE_P (type) | TREE_THIS_VOLATILE (array));
ret = require_complete_type (rval, complain);
protected_set_expr_location (ret, loc);
if (non_lvalue)
ret = non_lvalue_loc (loc, ret);
return ret;
}
{
tree ar = cp_default_conversion (array, complain);
tree ind = cp_default_conversion (idx, complain);
tree first = NULL_TREE;
if (flag_strong_eval_order == 2 && TREE_SIDE_EFFECTS (ind))
ar = first = save_expr (ar);
/* Put the integer in IND to simplify error checking. */
if (TREE_CODE (TREE_TYPE (ar)) == INTEGER_TYPE)
std::swap (ar, ind);
if (ar == error_mark_node || ind == error_mark_node)
return error_mark_node;
if (!TYPE_PTR_P (TREE_TYPE (ar)))
{
if (complain & tf_error)
error_at (loc, "subscripted value is neither array nor pointer");
return error_mark_node;
}
if (TREE_CODE (TREE_TYPE (ind)) != INTEGER_TYPE)
{
if (complain & tf_error)
error_at (loc, "array subscript is not an integer");
return error_mark_node;
}
warn_array_subscript_with_type_char (loc, idx);
ret = cp_build_binary_op (input_location, PLUS_EXPR, ar, ind, complain);
if (first)
ret = build2_loc (loc, COMPOUND_EXPR, TREE_TYPE (ret), first, ret);
ret = cp_build_indirect_ref (loc, ret, RO_ARRAY_INDEXING, complain);
protected_set_expr_location (ret, loc);
if (non_lvalue)
ret = non_lvalue_loc (loc, ret);
return ret;
}
}
/* Entry point for Obj-C++. */
tree
build_array_ref (location_t loc, tree array, tree idx)
{
return cp_build_array_ref (loc, array, idx, tf_warning_or_error);
}
/* Resolve a pointer to member function. INSTANCE is the object
instance to use, if the member points to a virtual member.
This used to avoid checking for virtual functions if basetype
has no virtual functions, according to an earlier ANSI draft.
With the final ISO C++ rules, such an optimization is
incorrect: A pointer to a derived member can be static_cast
to pointer-to-base-member, as long as the dynamic object
later has the right member. So now we only do this optimization
when we know the dynamic type of the object. */
tree
get_member_function_from_ptrfunc (tree *instance_ptrptr, tree function,
tsubst_flags_t complain)
{
if (TREE_CODE (function) == OFFSET_REF)
function = TREE_OPERAND (function, 1);
if (TYPE_PTRMEMFUNC_P (TREE_TYPE (function)))
{
tree idx, delta, e1, e2, e3, vtbl;
bool nonvirtual;
tree fntype = TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (function));
tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (fntype));
tree instance_ptr = *instance_ptrptr;
tree instance_save_expr = 0;
if (instance_ptr == error_mark_node)
{
if (TREE_CODE (function) == PTRMEM_CST)
{
/* Extracting the function address from a pmf is only
allowed with -Wno-pmf-conversions. It only works for
pmf constants. */
e1 = build_addr_func (PTRMEM_CST_MEMBER (function), complain);
e1 = convert (fntype, e1);
return e1;
}
else
{
if (complain & tf_error)
error ("object missing in use of %qE", function);
return error_mark_node;
}
}
/* True if we know that the dynamic type of the object doesn't have
virtual functions, so we can assume the PFN field is a pointer. */
nonvirtual = (COMPLETE_TYPE_P (basetype)
&& !TYPE_POLYMORPHIC_P (basetype)
&& resolves_to_fixed_type_p (instance_ptr, 0));
/* If we don't really have an object (i.e. in an ill-formed
conversion from PMF to pointer), we can't resolve virtual
functions anyway. */
if (!nonvirtual && is_dummy_object (instance_ptr))
nonvirtual = true;
if (TREE_SIDE_EFFECTS (instance_ptr))
instance_ptr = instance_save_expr = save_expr (instance_ptr);
if (TREE_SIDE_EFFECTS (function))
function = save_expr (function);
/* Start by extracting all the information from the PMF itself. */
e3 = pfn_from_ptrmemfunc (function);
delta = delta_from_ptrmemfunc (function);
idx = build1 (NOP_EXPR, vtable_index_type, e3);
switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
{
int flag_sanitize_save;
case ptrmemfunc_vbit_in_pfn:
e1 = cp_build_binary_op (input_location,
BIT_AND_EXPR, idx, integer_one_node,
complain);
idx = cp_build_binary_op (input_location,
MINUS_EXPR, idx, integer_one_node,
complain);
if (idx == error_mark_node)
return error_mark_node;
break;
case ptrmemfunc_vbit_in_delta:
e1 = cp_build_binary_op (input_location,
BIT_AND_EXPR, delta, integer_one_node,
complain);
/* Don't instrument the RSHIFT_EXPR we're about to create because
we're going to use DELTA number of times, and that wouldn't play
well with SAVE_EXPRs therein. */
flag_sanitize_save = flag_sanitize;
flag_sanitize = 0;
delta = cp_build_binary_op (input_location,
RSHIFT_EXPR, delta, integer_one_node,
complain);
flag_sanitize = flag_sanitize_save;
if (delta == error_mark_node)
return error_mark_node;
break;
default:
gcc_unreachable ();
}
if (e1 == error_mark_node)
return error_mark_node;
/* Convert down to the right base before using the instance. A
special case is that in a pointer to member of class C, C may
be incomplete. In that case, the function will of course be
a member of C, and no conversion is required. In fact,
lookup_base will fail in that case, because incomplete
classes do not have BINFOs. */
if (!same_type_ignoring_top_level_qualifiers_p
(basetype, TREE_TYPE (TREE_TYPE (instance_ptr))))
{
basetype = lookup_base (TREE_TYPE (TREE_TYPE (instance_ptr)),
basetype, ba_check, NULL, complain);
instance_ptr = build_base_path (PLUS_EXPR, instance_ptr, basetype,
1, complain);
if (instance_ptr == error_mark_node)
return error_mark_node;
}
/* ...and then the delta in the PMF. */
instance_ptr = fold_build_pointer_plus (instance_ptr, delta);
/* Hand back the adjusted 'this' argument to our caller. */
*instance_ptrptr = instance_ptr;
if (nonvirtual)
/* Now just return the pointer. */
return e3;
/* Next extract the vtable pointer from the object. */
vtbl = build1 (NOP_EXPR, build_pointer_type (vtbl_ptr_type_node),
instance_ptr);
vtbl = cp_build_fold_indirect_ref (vtbl);
if (vtbl == error_mark_node)
return error_mark_node;
/* Finally, extract the function pointer from the vtable. */
e2 = fold_build_pointer_plus_loc (input_location, vtbl, idx);
e2 = cp_build_fold_indirect_ref (e2);
if (e2 == error_mark_node)
return error_mark_node;
TREE_CONSTANT (e2) = 1;
/* When using function descriptors, the address of the
vtable entry is treated as a function pointer. */
if (TARGET_VTABLE_USES_DESCRIPTORS)
e2 = build1 (NOP_EXPR, TREE_TYPE (e2),
cp_build_addr_expr (e2, complain));
e2 = fold_convert (TREE_TYPE (e3), e2);
e1 = build_conditional_expr (input_location, e1, e2, e3, complain);
if (e1 == error_mark_node)
return error_mark_node;
/* Make sure this doesn't get evaluated first inside one of the
branches of the COND_EXPR. */
if (instance_save_expr)
e1 = build2 (COMPOUND_EXPR, TREE_TYPE (e1),
instance_save_expr, e1);
function = e1;
}
return function;
}
/* Used by the C-common bits. */
tree
build_function_call (location_t /*loc*/,
tree function, tree params)
{
return cp_build_function_call (function, params, tf_warning_or_error);
}
/* Used by the C-common bits. */
tree
build_function_call_vec (location_t /*loc*/, vec<location_t> /*arg_loc*/,
tree function, vec<tree, va_gc> *params,
vec<tree, va_gc> * /*origtypes*/, tree orig_function)
{
vec<tree, va_gc> *orig_params = params;
tree ret = cp_build_function_call_vec (function, ¶ms,
tf_warning_or_error, orig_function);
/* cp_build_function_call_vec can reallocate PARAMS by adding
default arguments. That should never happen here. Verify
that. */
gcc_assert (params == orig_params);
return ret;
}
/* Build a function call using a tree list of arguments. */
static tree
cp_build_function_call (tree function, tree params, tsubst_flags_t complain)
{
tree ret;
releasing_vec vec;
for (; params != NULL_TREE; params = TREE_CHAIN (params))
vec_safe_push (vec, TREE_VALUE (params));
ret = cp_build_function_call_vec (function, &vec, complain);
return ret;
}
/* Build a function call using varargs. */
tree
cp_build_function_call_nary (tree function, tsubst_flags_t complain, ...)
{
va_list args;
tree ret, t;
releasing_vec vec;
va_start (args, complain);
for (t = va_arg (args, tree); t != NULL_TREE; t = va_arg (args, tree))
vec_safe_push (vec, t);
va_end (args);
ret = cp_build_function_call_vec (function, &vec, complain);
return ret;
}
/* Build a function call using a vector of arguments.
If FUNCTION is the result of resolving an overloaded target built-in,
ORIG_FNDECL is the original function decl, otherwise it is null.
PARAMS may be NULL if there are no parameters. This changes the
contents of PARAMS. */
tree
cp_build_function_call_vec (tree function, vec<tree, va_gc> **params,
tsubst_flags_t complain, tree orig_fndecl)
{
tree fntype, fndecl;
int is_method;
tree original = function;
int nargs;
tree *argarray;
tree parm_types;
vec<tree, va_gc> *allocated = NULL;
tree ret;
/* For Objective-C, convert any calls via a cast to OBJC_TYPE_REF
expressions, like those used for ObjC messenger dispatches. */
if (params != NULL && !vec_safe_is_empty (*params))
function = objc_rewrite_function_call (function, (**params)[0]);
/* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
Strip such NOP_EXPRs, since FUNCTION is used in non-lvalue context. */
if (TREE_CODE (function) == NOP_EXPR
&& TREE_TYPE (function) == TREE_TYPE (TREE_OPERAND (function, 0)))
function = TREE_OPERAND (function, 0);
if (TREE_CODE (function) == FUNCTION_DECL)
{
if (!mark_used (function, complain))
return error_mark_node;
fndecl = function;
/* Convert anything with function type to a pointer-to-function. */
if (DECL_MAIN_P (function))
{
if (complain & tf_error)
pedwarn (input_location, OPT_Wpedantic,
"ISO C++ forbids calling %<::main%> from within program");
else
return error_mark_node;
}
function = build_addr_func (function, complain);
}
else
{
fndecl = NULL_TREE;
function = build_addr_func (function, complain);
}
if (function == error_mark_node)
return error_mark_node;
fntype = TREE_TYPE (function);
if (TYPE_PTRMEMFUNC_P (fntype))
{
if (complain & tf_error)
error ("must use %<.*%> or %<->*%> to call pointer-to-member "
"function in %<%E (...)%>, e.g. %<(... ->* %E) (...)%>",
original, original);
return error_mark_node;
}
is_method = (TYPE_PTR_P (fntype)
&& TREE_CODE (TREE_TYPE (fntype)) == METHOD_TYPE);
if (!(TYPE_PTRFN_P (fntype)
|| is_method
|| TREE_CODE (function) == TEMPLATE_ID_EXPR))
{
if (complain & tf_error)
{
if (!flag_diagnostics_show_caret)
error_at (input_location,
"%qE cannot be used as a function", original);
else if (DECL_P (original))
error_at (input_location,
"%qD cannot be used as a function", original);
else
error_at (input_location,
"expression cannot be used as a function");
}
return error_mark_node;
}
/* fntype now gets the type of function pointed to. */
fntype = TREE_TYPE (fntype);
parm_types = TYPE_ARG_TYPES (fntype);
if (params == NULL)
{
allocated = make_tree_vector ();
params = &allocated;
}
nargs = convert_arguments (parm_types, params, fndecl, LOOKUP_NORMAL,
complain);
if (nargs < 0)
return error_mark_node;
argarray = (*params)->address ();
/* Check for errors in format strings and inappropriately
null parameters. */
bool warned_p = check_function_arguments (input_location, fndecl, fntype,
nargs, argarray, NULL);
ret = build_cxx_call (function, nargs, argarray, complain, orig_fndecl);
if (warned_p)
{
tree c = extract_call_expr (ret);
if (TREE_CODE (c) == CALL_EXPR)
suppress_warning (c, OPT_Wnonnull);
}
if (allocated != NULL)
release_tree_vector (allocated);
return ret;
}
/* Subroutine of convert_arguments.
Print an error message about a wrong number of arguments. */
static void
error_args_num (location_t loc, tree fndecl, bool too_many_p)
{
if (fndecl)
{
if (TREE_CODE (TREE_TYPE (fndecl)) == METHOD_TYPE)
{
if (DECL_NAME (fndecl) == NULL_TREE
|| (DECL_NAME (fndecl)
== DECL_NAME (TYPE_NAME (DECL_CONTEXT (fndecl)))))
error_at (loc,
too_many_p
? G_("too many arguments to constructor %q#D")
: G_("too few arguments to constructor %q#D"),
fndecl);
else
error_at (loc,
too_many_p
? G_("too many arguments to member function %q#D")
: G_("too few arguments to member function %q#D"),
fndecl);
}
else
error_at (loc,
too_many_p
? G_("too many arguments to function %q#D")
: G_("too few arguments to function %q#D"),
fndecl);
if (!DECL_IS_UNDECLARED_BUILTIN (fndecl))
inform (DECL_SOURCE_LOCATION (fndecl), "declared here");
}
else
{
if (c_dialect_objc () && objc_message_selector ())
error_at (loc,
too_many_p
? G_("too many arguments to method %q#D")
: G_("too few arguments to method %q#D"),
objc_message_selector ());
else
error_at (loc, too_many_p ? G_("too many arguments to function")
: G_("too few arguments to function"));
}
}
/* Convert the actual parameter expressions in the list VALUES to the
types in the list TYPELIST. The converted expressions are stored
back in the VALUES vector.
If parmdecls is exhausted, or when an element has NULL as its type,
perform the default conversions.
NAME is an IDENTIFIER_NODE or 0. It is used only for error messages.
This is also where warnings about wrong number of args are generated.
Returns the actual number of arguments processed (which might be less
than the length of the vector), or -1 on error.
In C++, unspecified trailing parameters can be filled in with their
default arguments, if such were specified. Do so here. */
static int
convert_arguments (tree typelist, vec<tree, va_gc> **values, tree fndecl,
int flags, tsubst_flags_t complain)
{
tree typetail;
unsigned int i;
/* Argument passing is always copy-initialization. */
flags |= LOOKUP_ONLYCONVERTING;
for (i = 0, typetail = typelist;
i < vec_safe_length (*values);
i++)
{
tree type = typetail ? TREE_VALUE (typetail) : 0;
tree val = (**values)[i];
if (val == error_mark_node || type == error_mark_node)
return -1;
if (type == void_type_node)
{
if (complain & tf_error)
{
error_args_num (input_location, fndecl, /*too_many_p=*/true);
return i;
}
else
return -1;
}
/* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
Strip such NOP_EXPRs, since VAL is used in non-lvalue context. */
if (TREE_CODE (val) == NOP_EXPR
&& TREE_TYPE (val) == TREE_TYPE (TREE_OPERAND (val, 0))
&& (type == 0 || !TYPE_REF_P (type)))
val = TREE_OPERAND (val, 0);
if (type == 0 || !TYPE_REF_P (type))
{
if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE
|| FUNC_OR_METHOD_TYPE_P (TREE_TYPE (val)))
val = decay_conversion (val, complain);
}
if (val == error_mark_node)
return -1;
if (type != 0)
{
/* Formal parm type is specified by a function prototype. */
tree parmval;
if (!COMPLETE_TYPE_P (complete_type (type)))
{
if (complain & tf_error)
{
location_t loc = EXPR_LOC_OR_LOC (val, input_location);
if (fndecl)
{
auto_diagnostic_group d;
error_at (loc,
"parameter %P of %qD has incomplete type %qT",
i, fndecl, type);
inform (get_fndecl_argument_location (fndecl, i),
" declared here");
}
else
error_at (loc, "parameter %P has incomplete type %qT", i,
type);
}
parmval = error_mark_node;
}
else
{
parmval = convert_for_initialization
(NULL_TREE, type, val, flags,
ICR_ARGPASS, fndecl, i, complain);
parmval = convert_for_arg_passing (type, parmval, complain);
}
if (parmval == error_mark_node)
return -1;
(**values)[i] = parmval;
}
else
{
int magic = fndecl ? magic_varargs_p (fndecl) : 0;
if (magic)
{
/* Don't truncate excess precision to the semantic type. */
if (magic == 1 && TREE_CODE (val) == EXCESS_PRECISION_EXPR)
val = TREE_OPERAND (val, 0);
/* Don't do ellipsis conversion for __built_in_constant_p
as this will result in spurious errors for non-trivial
types. */
val = require_complete_type (val, complain);
}
else
val = convert_arg_to_ellipsis (val, complain);
(**values)[i] = val;
}
if (typetail)
typetail = TREE_CHAIN (typetail);
}
if (typetail != 0 && typetail != void_list_node)
{
/* See if there are default arguments that can be used. Because
we hold default arguments in the FUNCTION_TYPE (which is so
wrong), we can see default parameters here from deduced
contexts (and via typeof) for indirect function calls.
Fortunately we know whether we have a function decl to
provide default arguments in a language conformant
manner. */
if (fndecl && TREE_PURPOSE (typetail)
&& TREE_CODE (TREE_PURPOSE (typetail)) != DEFERRED_PARSE)
{
for (; typetail != void_list_node; ++i)
{
/* After DR777, with explicit template args we can end up with a
default argument followed by no default argument. */
if (!TREE_PURPOSE (typetail))
break;
tree parmval
= convert_default_arg (TREE_VALUE (typetail),
TREE_PURPOSE (typetail),
fndecl, i, complain);
if (parmval == error_mark_node)
return -1;
vec_safe_push (*values, parmval);
typetail = TREE_CHAIN (typetail);
/* ends with `...'. */
if (typetail == NULL_TREE)
break;
}
}
if (typetail && typetail != void_list_node)
{
if (complain & tf_error)
error_args_num (input_location, fndecl, /*too_many_p=*/false);
return -1;
}
}
return (int) i;
}
/* Build a binary-operation expression, after performing default
conversions on the operands. CODE is the kind of expression to
build. ARG1 and ARG2 are the arguments. ARG1_CODE and ARG2_CODE
are the tree codes which correspond to ARG1 and ARG2 when issuing
warnings about possibly misplaced parentheses. They may differ
from the TREE_CODE of ARG1 and ARG2 if the parser has done constant
folding (e.g., if the parser sees "a | 1 + 1", it may call this
routine with ARG2 being an INTEGER_CST and ARG2_CODE == PLUS_EXPR).
To avoid issuing any parentheses warnings, pass ARG1_CODE and/or
ARG2_CODE as ERROR_MARK. */
tree
build_x_binary_op (const op_location_t &loc, enum tree_code code, tree arg1,
enum tree_code arg1_code, tree arg2,
enum tree_code arg2_code, tree lookups,
tree *overload_p, tsubst_flags_t complain)
{
tree orig_arg1;
tree orig_arg2;
tree expr;
tree overload = NULL_TREE;
orig_arg1 = arg1;
orig_arg2 = arg2;
if (processing_template_decl)
{
if (type_dependent_expression_p (arg1)
|| type_dependent_expression_p (arg2))
{
expr = build_min_nt_loc (loc, code, arg1, arg2);
TREE_TYPE (expr)
= build_dependent_operator_type (lookups, code, false);
return expr;
}
}
if (code == DOTSTAR_EXPR)
expr = build_m_component_ref (arg1, arg2, complain);
else
expr = build_new_op (loc, code, LOOKUP_NORMAL, arg1, arg2, NULL_TREE,
lookups, &overload, complain);
if (overload_p != NULL)
*overload_p = overload;
/* Check for cases such as x+y<<z which users are likely to
misinterpret. But don't warn about obj << x + y, since that is a
common idiom for I/O. */
if (warn_parentheses
&& (complain & tf_warning)
&& !processing_template_decl
&& !error_operand_p (arg1)
&& !error_operand_p (arg2)
&& (code != LSHIFT_EXPR
|| !CLASS_TYPE_P (TREE_TYPE (arg1))))
warn_about_parentheses (loc, code, arg1_code, orig_arg1,
arg2_code, orig_arg2);
if (processing_template_decl && expr != error_mark_node)
{
if (overload != NULL_TREE)
return (build_min_non_dep_op_overload
(code, expr, overload, orig_arg1, orig_arg2));
return build_min_non_dep (code, expr, orig_arg1, orig_arg2);
}
return expr;
}
/* Build and return an ARRAY_REF expression. */
tree
build_x_array_ref (location_t loc, tree arg1, tree arg2,
tsubst_flags_t complain)
{
tree orig_arg1 = arg1;
tree orig_arg2 = arg2;
tree expr;
tree overload = NULL_TREE;
if (processing_template_decl)
{
if (type_dependent_expression_p (arg1)
|| type_dependent_expression_p (arg2))
return build_min_nt_loc (loc, ARRAY_REF, arg1, arg2,
NULL_TREE, NULL_TREE);
}
expr = build_new_op (loc, ARRAY_REF, LOOKUP_NORMAL, arg1, arg2,
NULL_TREE, NULL_TREE, &overload, complain);
if (processing_template_decl && expr != error_mark_node)
{
if (overload != NULL_TREE)
return (build_min_non_dep_op_overload
(ARRAY_REF, expr, overload, orig_arg1, orig_arg2));
return build_min_non_dep (ARRAY_REF, expr, orig_arg1, orig_arg2,
NULL_TREE, NULL_TREE);
}
return expr;
}
/* Return whether OP is an expression of enum type cast to integer
type. In C++ even unsigned enum types are cast to signed integer
types. We do not want to issue warnings about comparisons between
signed and unsigned types when one of the types is an enum type.
Those warnings are always false positives in practice. */
static bool
enum_cast_to_int (tree op)
{
if (CONVERT_EXPR_P (op)
&& TREE_TYPE (op) == integer_type_node
&& TREE_CODE (TREE_TYPE (TREE_OPERAND (op, 0))) == ENUMERAL_TYPE
&& TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op, 0))))
return true;
/* The cast may have been pushed into a COND_EXPR. */
if (TREE_CODE (op) == COND_EXPR)
return (enum_cast_to_int (TREE_OPERAND (op, 1))
|| enum_cast_to_int (TREE_OPERAND (op, 2)));
return false;
}
/* For the c-common bits. */
tree
build_binary_op (location_t location, enum tree_code code, tree op0, tree op1,
bool /*convert_p*/)
{
return cp_build_binary_op (location, code, op0, op1, tf_warning_or_error);
}
/* Build a vector comparison of ARG0 and ARG1 using CODE opcode
into a value of TYPE type. Comparison is done via VEC_COND_EXPR. */
static tree
build_vec_cmp (tree_code code, tree type,
tree arg0, tree arg1)
{
tree zero_vec = build_zero_cst (type);
tree minus_one_vec = build_minus_one_cst (type);
tree cmp_type = truth_type_for (TREE_TYPE (arg0));
tree cmp = build2 (code, cmp_type, arg0, arg1);
return build3 (VEC_COND_EXPR, type, cmp, minus_one_vec, zero_vec);
}
/* Possibly warn about an address never being NULL. */
static void
warn_for_null_address (location_t location, tree op, tsubst_flags_t complain)
{
/* Prevent warnings issued for macro expansion. */
if (!warn_address
|| (complain & tf_warning) == 0
|| c_inhibit_evaluation_warnings != 0
|| from_macro_expansion_at (location)
|| warning_suppressed_p (op, OPT_Waddress))
return;
tree cop = fold_for_warn (op);
if (TREE_CODE (cop) == NON_LVALUE_EXPR)
/* Unwrap the expression for C++ 98. */
cop = TREE_OPERAND (cop, 0);
if (TREE_CODE (cop) == PTRMEM_CST)
{
/* The address of a nonstatic data member is never null. */
warning_at (location, OPT_Waddress,
"the address %qE will never be NULL",
cop);
return;
}
if (TREE_CODE (cop) == NOP_EXPR)
{
/* Allow casts to intptr_t to suppress the warning. */
tree type = TREE_TYPE (cop);
if (TREE_CODE (type) == INTEGER_TYPE)
return;
STRIP_NOPS (cop);
}
bool warned = false;
if (TREE_CODE (cop) == ADDR_EXPR)
{
cop = TREE_OPERAND (cop, 0);
/* Set to true in the loop below if OP dereferences its operand.
In such a case the ultimate target need not be a decl for
the null [in]equality test to be necessarily constant. */
bool deref = false;
/* Get the outermost array or object, or member. */
while (handled_component_p (cop))
{
if (TREE_CODE (cop) == COMPONENT_REF)
{
/* Get the member (its address is never null). */
cop = TREE_OPERAND (cop, 1);
break;
}
/* Get the outer array/object to refer to in the warning. */
cop = TREE_OPERAND (cop, 0);
deref = true;
}
if ((!deref && !decl_with_nonnull_addr_p (cop))
|| from_macro_expansion_at (location)
|| warning_suppressed_p (cop, OPT_Waddress))
return;
warned = warning_at (location, OPT_Waddress,
"the address of %qD will never be NULL", cop);
op = cop;
}
else if (TREE_CODE (cop) == POINTER_PLUS_EXPR)
{
/* Adding zero to the null pointer is well-defined in C++. When
the offset is unknown (i.e., not a constant) warn anyway since
it's less likely that the pointer operand is null than not. */
tree off = TREE_OPERAND (cop, 1);
if (!integer_zerop (off)
&& !warning_suppressed_p (cop, OPT_Waddress))
{
tree base = TREE_OPERAND (cop, 0);
STRIP_NOPS (base);
if (TYPE_REF_P (TREE_TYPE (base)))
warning_at (location, OPT_Waddress, "the compiler can assume that "
"the address of %qE will never be NULL", base);
else
warning_at (location, OPT_Waddress, "comparing the result of "
"pointer addition %qE and NULL", cop);
}
return;
}
else if (CONVERT_EXPR_P (op)
&& TYPE_REF_P (TREE_TYPE (TREE_OPERAND (op, 0))))
{
STRIP_NOPS (op);
if (TREE_CODE (op) == COMPONENT_REF)
op = TREE_OPERAND (op, 1);
if (DECL_P (op))
warned = warning_at (location, OPT_Waddress,
"the compiler can assume that the address of "
"%qD will never be NULL", op);
}
if (warned && DECL_P (op))
inform (DECL_SOURCE_LOCATION (op), "%qD declared here", op);
}
/* Warn about [expr.arith.conv]/2: If one operand is of enumeration type and
the other operand is of a different enumeration type or a floating-point
type, this behavior is deprecated ([depr.arith.conv.enum]). CODE is the
code of the binary operation, TYPE0 and TYPE1 are the types of the operands,
and LOC is the location for the whole binary expression.
For C++26 this is ill-formed rather than deprecated.
Return true for SFINAE errors.
TODO: Consider combining this with -Wenum-compare in build_new_op_1. */
static bool
do_warn_enum_conversions (location_t loc, enum tree_code code, tree type0,
tree type1, tsubst_flags_t complain)
{
if (TREE_CODE (type0) == ENUMERAL_TYPE
&& TREE_CODE (type1) == ENUMERAL_TYPE
&& TYPE_MAIN_VARIANT (type0) != TYPE_MAIN_VARIANT (type1))
{
if (cxx_dialect >= cxx26)
{
if ((complain & tf_warning_or_error) == 0)
return true;
}
else if ((complain & tf_warning) == 0)
return false;
/* In C++20, -Wdeprecated-enum-enum-conversion is on by default.
Otherwise, warn if -Wenum-conversion is on. */
enum opt_code opt;
if (warn_deprecated_enum_enum_conv)
opt = OPT_Wdeprecated_enum_enum_conversion;
else if (warn_enum_conversion)
opt = OPT_Wenum_conversion;
else
return false;
switch (code)
{
case GT_EXPR:
case LT_EXPR:
case GE_EXPR:
case LE_EXPR:
case EQ_EXPR:
case NE_EXPR:
/* Comparisons are handled by -Wenum-compare. */
return false;
case SPACESHIP_EXPR:
/* This is invalid, don't warn. */
return false;
case BIT_AND_EXPR:
case BIT_IOR_EXPR:
case BIT_XOR_EXPR:
if (cxx_dialect >= cxx26)
pedwarn (loc, opt, "bitwise operation between different "
"enumeration types %qT and %qT", type0, type1);
else
warning_at (loc, opt, "bitwise operation between different "
"enumeration types %qT and %qT is deprecated",
type0, type1);
return false;
default:
if (cxx_dialect >= cxx26)
pedwarn (loc, opt, "arithmetic between different enumeration "
"types %qT and %qT", type0, type1);
else
warning_at (loc, opt, "arithmetic between different enumeration "
"types %qT and %qT is deprecated", type0, type1);
return false;
}
}
else if ((TREE_CODE (type0) == ENUMERAL_TYPE
&& SCALAR_FLOAT_TYPE_P (type1))
|| (SCALAR_FLOAT_TYPE_P (type0)
&& TREE_CODE (type1) == ENUMERAL_TYPE))
{
if (cxx_dialect >= cxx26)
{
if ((complain & tf_warning_or_error) == 0)
return true;
}
else if ((complain & tf_warning) == 0)
return false;
const bool enum_first_p = TREE_CODE (type0) == ENUMERAL_TYPE;
/* In C++20, -Wdeprecated-enum-float-conversion is on by default.
Otherwise, warn if -Wenum-conversion is on. */
enum opt_code opt;
if (warn_deprecated_enum_float_conv)
opt = OPT_Wdeprecated_enum_float_conversion;
else if (warn_enum_conversion)
opt = OPT_Wenum_conversion;
else
return false;
switch (code)
{
case GT_EXPR:
case LT_EXPR:
case GE_EXPR:
case LE_EXPR:
case EQ_EXPR:
case NE_EXPR:
if (enum_first_p && cxx_dialect >= cxx26)
pedwarn (loc, opt, "comparison of enumeration type %qT with "
"floating-point type %qT", type0, type1);
else if (cxx_dialect >= cxx26)
pedwarn (loc, opt, "comparison of floating-point type %qT "
"with enumeration type %qT", type0, type1);
else if (enum_first_p)
warning_at (loc, opt, "comparison of enumeration type %qT with "
"floating-point type %qT is deprecated",
type0, type1);
else
warning_at (loc, opt, "comparison of floating-point type %qT "
"with enumeration type %qT is deprecated",
type0, type1);
return false;
case SPACESHIP_EXPR:
/* This is invalid, don't warn. */
return false;
default:
if (enum_first_p && cxx_dialect >= cxx26)
pedwarn (loc, opt, "arithmetic between enumeration type %qT "
"and floating-point type %qT", type0, type1);
else if (cxx_dialect >= cxx26)
pedwarn (loc, opt, "arithmetic between floating-point type %qT "
"and enumeration type %qT", type0, type1);
else if (enum_first_p)
warning_at (loc, opt, "arithmetic between enumeration type %qT "
"and floating-point type %qT is deprecated",
type0, type1);
else
warning_at (loc, opt, "arithmetic between floating-point type %qT "
"and enumeration type %qT is deprecated",
type0, type1);
return false;
}
}
return false;
}
/* Build a binary-operation expression without default conversions.
CODE is the kind of expression to build.
LOCATION is the location_t of the operator in the source code.
This function differs from `build' in several ways:
the data type of the result is computed and recorded in it,
warnings are generated if arg data types are invalid,
special handling for addition and subtraction of pointers is known,
and some optimization is done (operations on narrow ints
are done in the narrower type when that gives the same result).
Constant folding is also done before the result is returned.
Note that the operands will never have enumeral types
because either they have just had the default conversions performed
or they have both just been converted to some other type in which
the arithmetic is to be done.
C++: must do special pointer arithmetic when implementing
multiple inheritance, and deal with pointer to member functions. */
tree
cp_build_binary_op (const op_location_t &location,
enum tree_code code, tree orig_op0, tree orig_op1,
tsubst_flags_t complain)
{
tree op0, op1;
enum tree_code code0, code1;
tree type0, type1, orig_type0, orig_type1;
const char *invalid_op_diag;
/* Expression code to give to the expression when it is built.
Normally this is CODE, which is what the caller asked for,
but in some special cases we change it. */
enum tree_code resultcode = code;
/* Data type in which the computation is to be performed.
In the simplest cases this is the common type of the arguments. */
tree result_type = NULL_TREE;
/* When the computation is in excess precision, the type of the
final EXCESS_PRECISION_EXPR. */
tree semantic_result_type = NULL;
/* Nonzero means operands have already been type-converted
in whatever way is necessary.
Zero means they need to be converted to RESULT_TYPE. */
int converted = 0;
/* Nonzero means create the expression with this type, rather than
RESULT_TYPE. */
tree build_type = 0;
/* Nonzero means after finally constructing the expression
convert it to this type. */
tree final_type = 0;
tree result;
/* Nonzero if this is an operation like MIN or MAX which can
safely be computed in short if both args are promoted shorts.
Also implies COMMON.
-1 indicates a bitwise operation; this makes a difference
in the exact conditions for when it is safe to do the operation
in a narrower mode. */
int shorten = 0;
/* Nonzero if this is a comparison operation;
if both args are promoted shorts, compare the original shorts.
Also implies COMMON. */
int short_compare = 0;
/* Nonzero if this is a right-shift operation, which can be computed on the
original short and then promoted if the operand is a promoted short. */
int short_shift = 0;
/* Nonzero means set RESULT_TYPE to the common type of the args. */
int common = 0;
/* True if both operands have arithmetic type. */
bool arithmetic_types_p;
/* Remember whether we're doing / or %. */
bool doing_div_or_mod = false;
/* Remember whether we're doing << or >>. */
bool doing_shift = false;
/* Tree holding instrumentation expression. */
tree instrument_expr = NULL_TREE;
/* True means this is an arithmetic operation that may need excess
precision. */
bool may_need_excess_precision;
/* Apply default conversions. */
op0 = resolve_nondeduced_context (orig_op0, complain);
op1 = resolve_nondeduced_context (orig_op1, complain);
if (code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
|| code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
|| code == TRUTH_XOR_EXPR)
{
if (!really_overloaded_fn (op0) && !VOID_TYPE_P (TREE_TYPE (op0)))
op0 = decay_conversion (op0, complain);
if (!really_overloaded_fn (op1) && !VOID_TYPE_P (TREE_TYPE (op1)))
op1 = decay_conversion (op1, complain);
}
else
{
if (!really_overloaded_fn (op0) && !VOID_TYPE_P (TREE_TYPE (op0)))
op0 = cp_default_conversion (op0, complain);
if (!really_overloaded_fn (op1) && !VOID_TYPE_P (TREE_TYPE (op1)))
op1 = cp_default_conversion (op1, complain);
}
/* Strip NON_LVALUE_EXPRs, etc., since we aren't using as an lvalue. */
STRIP_TYPE_NOPS (op0);
STRIP_TYPE_NOPS (op1);
/* DTRT if one side is an overloaded function, but complain about it. */
if (type_unknown_p (op0))
{
tree t = instantiate_type (TREE_TYPE (op1), op0, tf_none);
if (t != error_mark_node)
{
if (complain & tf_error)
permerror (location,
"assuming cast to type %qT from overloaded function",
TREE_TYPE (t));
op0 = t;
}
}
if (type_unknown_p (op1))
{
tree t = instantiate_type (TREE_TYPE (op0), op1, tf_none);
if (t != error_mark_node)
{
if (complain & tf_error)
permerror (location,
"assuming cast to type %qT from overloaded function",
TREE_TYPE (t));
op1 = t;
}
}
orig_type0 = type0 = TREE_TYPE (op0);
orig_type1 = type1 = TREE_TYPE (op1);
tree non_ep_op0 = op0;
tree non_ep_op1 = op1;
/* The expression codes of the data types of the arguments tell us
whether the arguments are integers, floating, pointers, etc. */
code0 = TREE_CODE (type0);
code1 = TREE_CODE (type1);
/* If an error was already reported for one of the arguments,
avoid reporting another error. */
if (code0 == ERROR_MARK || code1 == ERROR_MARK)
return error_mark_node;
if ((invalid_op_diag
= targetm.invalid_binary_op (code, type0, type1)))
{
if (complain & tf_error)
{
if (code0 == REAL_TYPE
&& code1 == REAL_TYPE
&& (extended_float_type_p (type0)
|| extended_float_type_p (type1))
&& cp_compare_floating_point_conversion_ranks (type0,
type1) == 3)
{
rich_location richloc (line_table, location);
binary_op_error (&richloc, code, type0, type1);
}
else
error (invalid_op_diag);
}
return error_mark_node;
}
switch (code)
{
case PLUS_EXPR:
case MINUS_EXPR:
case MULT_EXPR:
case TRUNC_DIV_EXPR:
case CEIL_DIV_EXPR:
case FLOOR_DIV_EXPR:
case ROUND_DIV_EXPR:
case EXACT_DIV_EXPR:
may_need_excess_precision = true;
break;
case EQ_EXPR:
case NE_EXPR:
case LE_EXPR:
case GE_EXPR:
case LT_EXPR:
case GT_EXPR:
case SPACESHIP_EXPR:
/* Excess precision for implicit conversions of integers to
floating point. */
may_need_excess_precision = (ANY_INTEGRAL_TYPE_P (type0)
|| ANY_INTEGRAL_TYPE_P (type1));
break;
default:
may_need_excess_precision = false;
break;
}
if (TREE_CODE (op0) == EXCESS_PRECISION_EXPR)
{
op0 = TREE_OPERAND (op0, 0);
type0 = TREE_TYPE (op0);
}
else if (may_need_excess_precision
&& (code0 == REAL_TYPE || code0 == COMPLEX_TYPE))
if (tree eptype = excess_precision_type (type0))
{
type0 = eptype;
op0 = convert (eptype, op0);
}
if (TREE_CODE (op1) == EXCESS_PRECISION_EXPR)
{
op1 = TREE_OPERAND (op1, 0);
type1 = TREE_TYPE (op1);
}
else if (may_need_excess_precision
&& (code1 == REAL_TYPE || code1 == COMPLEX_TYPE))
if (tree eptype = excess_precision_type (type1))
{
type1 = eptype;
op1 = convert (eptype, op1);
}
/* Issue warnings about peculiar, but valid, uses of NULL. */
if ((null_node_p (orig_op0) || null_node_p (orig_op1))
/* It's reasonable to use pointer values as operands of &&
and ||, so NULL is no exception. */
&& code != TRUTH_ANDIF_EXPR && code != TRUTH_ORIF_EXPR
&& ( /* Both are NULL (or 0) and the operation was not a
comparison or a pointer subtraction. */
(null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1)
&& code != EQ_EXPR && code != NE_EXPR && code != MINUS_EXPR)
/* Or if one of OP0 or OP1 is neither a pointer nor NULL. */
|| (!null_ptr_cst_p (orig_op0)
&& !TYPE_PTR_OR_PTRMEM_P (type0))
|| (!null_ptr_cst_p (orig_op1)
&& !TYPE_PTR_OR_PTRMEM_P (type1)))
&& (complain & tf_warning))
{
location_t loc =
expansion_point_location_if_in_system_header (input_location);
warning_at (loc, OPT_Wpointer_arith, "NULL used in arithmetic");
}
/* In case when one of the operands of the binary operation is
a vector and another is a scalar -- convert scalar to vector. */
if ((gnu_vector_type_p (type0) && code1 != VECTOR_TYPE)
|| (gnu_vector_type_p (type1) && code0 != VECTOR_TYPE))
{
enum stv_conv convert_flag
= scalar_to_vector (location, code, non_ep_op0, non_ep_op1,
complain & tf_error);
switch (convert_flag)
{
case stv_error:
return error_mark_node;
case stv_firstarg:
{
op0 = convert (TREE_TYPE (type1), op0);
op0 = save_expr (op0);
op0 = build_vector_from_val (type1, op0);
orig_type0 = type0 = TREE_TYPE (op0);
code0 = TREE_CODE (type0);
converted = 1;
break;
}
case stv_secondarg:
{
op1 = convert (TREE_TYPE (type0), op1);
op1 = save_expr (op1);
op1 = build_vector_from_val (type0, op1);
orig_type1 = type1 = TREE_TYPE (op1);
code1 = TREE_CODE (type1);
converted = 1;
break;
}
default:
break;
}
}
switch (code)
{
case MINUS_EXPR:
/* Subtraction of two similar pointers.
We must subtract them as integers, then divide by object size. */
if (code0 == POINTER_TYPE && code1 == POINTER_TYPE
&& same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0),
TREE_TYPE (type1)))
{
result = pointer_diff (location, op0, op1,
common_pointer_type (type0, type1), complain,
&instrument_expr);
if (instrument_expr != NULL)
result = build2 (COMPOUND_EXPR, TREE_TYPE (result),
instrument_expr, result);
return result;
}
/* In all other cases except pointer - int, the usual arithmetic
rules apply. */
else if (!(code0 == POINTER_TYPE && code1 == INTEGER_TYPE))
{
common = 1;
break;
}
/* The pointer - int case is just like pointer + int; fall
through. */
gcc_fallthrough ();
case PLUS_EXPR:
if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
&& (code0 == INTEGER_TYPE || code1 == INTEGER_TYPE))
{
tree ptr_operand;
tree int_operand;
ptr_operand = ((code0 == POINTER_TYPE) ? op0 : op1);
int_operand = ((code0 == INTEGER_TYPE) ? op0 : op1);
if (processing_template_decl)
{
result_type = TREE_TYPE (ptr_operand);
break;
}
return cp_pointer_int_sum (location, code,
ptr_operand,
int_operand,
complain);
}
common = 1;
break;
case MULT_EXPR:
common = 1;
break;
case TRUNC_DIV_EXPR:
case CEIL_DIV_EXPR:
case FLOOR_DIV_EXPR:
case ROUND_DIV_EXPR:
case EXACT_DIV_EXPR:
if (TREE_CODE (op0) == SIZEOF_EXPR && TREE_CODE (op1) == SIZEOF_EXPR)
{
tree type0 = TREE_OPERAND (op0, 0);
tree type1 = TREE_OPERAND (op1, 0);
tree first_arg = tree_strip_any_location_wrapper (type0);
if (!TYPE_P (type0))
type0 = TREE_TYPE (type0);
if (!TYPE_P (type1))
type1 = TREE_TYPE (type1);
if (type0
&& INDIRECT_TYPE_P (type0)
&& same_type_p (TREE_TYPE (type0), type1))
{
if (!(TREE_CODE (first_arg) == PARM_DECL
&& DECL_ARRAY_PARAMETER_P (first_arg)
&& warn_sizeof_array_argument)
&& (complain & tf_warning))
{
auto_diagnostic_group d;
if (warning_at (location, OPT_Wsizeof_pointer_div,
"division %<sizeof (%T) / sizeof (%T)%> does "
"not compute the number of array elements",
type0, type1))
if (DECL_P (first_arg))
inform (DECL_SOURCE_LOCATION (first_arg),
"first %<sizeof%> operand was declared here");
}
}
else if (!dependent_type_p (type0)
&& !dependent_type_p (type1)
&& TREE_CODE (type0) == ARRAY_TYPE
&& !char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (type0)))
/* Set by finish_parenthesized_expr. */
&& !warning_suppressed_p (op1, OPT_Wsizeof_array_div)
&& (complain & tf_warning))
maybe_warn_sizeof_array_div (location, first_arg, type0,
op1, non_reference (type1));
}
if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
|| code0 == COMPLEX_TYPE || code0 == VECTOR_TYPE)
&& (code1 == INTEGER_TYPE || code1 == REAL_TYPE
|| code1 == COMPLEX_TYPE || code1 == VECTOR_TYPE))
{
enum tree_code tcode0 = code0, tcode1 = code1;
doing_div_or_mod = true;
warn_for_div_by_zero (location, fold_for_warn (op1));
if (tcode0 == COMPLEX_TYPE || tcode0 == VECTOR_TYPE)
tcode0 = TREE_CODE (TREE_TYPE (TREE_TYPE (op0)));
if (tcode1 == COMPLEX_TYPE || tcode1 == VECTOR_TYPE)
tcode1 = TREE_CODE (TREE_TYPE (TREE_TYPE (op1)));
if (!(tcode0 == INTEGER_TYPE && tcode1 == INTEGER_TYPE))
resultcode = RDIV_EXPR;
else
{
/* When dividing two signed integers, we have to promote to int.
unless we divide by a constant != -1. Note that default
conversion will have been performed on the operands at this
point, so we have to dig out the original type to find out if
it was unsigned. */
tree stripped_op1 = tree_strip_any_location_wrapper (op1);
shorten = may_shorten_divmod (op0, stripped_op1);
}
common = 1;
}
break;
case BIT_AND_EXPR:
case BIT_IOR_EXPR:
case BIT_XOR_EXPR:
if ((code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
|| (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
&& !VECTOR_FLOAT_TYPE_P (type0)
&& !VECTOR_FLOAT_TYPE_P (type1)))
shorten = -1;
break;
case TRUNC_MOD_EXPR:
case FLOOR_MOD_EXPR:
doing_div_or_mod = true;
warn_for_div_by_zero (location, fold_for_warn (op1));
if (code0 == VECTOR_TYPE && code1 == VECTOR_TYPE
&& TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
&& TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE)
common = 1;
else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
{
/* Although it would be tempting to shorten always here, that loses
on some targets, since the modulo instruction is undefined if the
quotient can't be represented in the computation mode. We shorten
only if unsigned or if dividing by something we know != -1. */
tree stripped_op1 = tree_strip_any_location_wrapper (op1);
shorten = may_shorten_divmod (op0, stripped_op1);
common = 1;
}
break;
case TRUTH_ANDIF_EXPR:
case TRUTH_ORIF_EXPR:
case TRUTH_AND_EXPR:
case TRUTH_OR_EXPR:
if (!VECTOR_TYPE_P (type0) && gnu_vector_type_p (type1))
{
if (!COMPARISON_CLASS_P (op1))
op1 = cp_build_binary_op (EXPR_LOCATION (op1), NE_EXPR, op1,
build_zero_cst (type1), complain);
if (code == TRUTH_ANDIF_EXPR)
{
tree z = build_zero_cst (TREE_TYPE (op1));
return build_conditional_expr (location, op0, op1, z, complain);
}
else if (code == TRUTH_ORIF_EXPR)
{
tree m1 = build_all_ones_cst (TREE_TYPE (op1));
return build_conditional_expr (location, op0, m1, op1, complain);
}
else
gcc_unreachable ();
}
if (gnu_vector_type_p (type0)
&& (!VECTOR_TYPE_P (type1) || gnu_vector_type_p (type1)))
{
if (!COMPARISON_CLASS_P (op0))
op0 = cp_build_binary_op (EXPR_LOCATION (op0), NE_EXPR, op0,
build_zero_cst (type0), complain);
if (!VECTOR_TYPE_P (type1))
{
tree m1 = build_all_ones_cst (TREE_TYPE (op0));
tree z = build_zero_cst (TREE_TYPE (op0));
op1 = build_conditional_expr (location, op1, m1, z, complain);
}
else if (!COMPARISON_CLASS_P (op1))
op1 = cp_build_binary_op (EXPR_LOCATION (op1), NE_EXPR, op1,
build_zero_cst (type1), complain);
if (code == TRUTH_ANDIF_EXPR)
code = BIT_AND_EXPR;
else if (code == TRUTH_ORIF_EXPR)
code = BIT_IOR_EXPR;
else
gcc_unreachable ();
return cp_build_binary_op (location, code, op0, op1, complain);
}
result_type = boolean_type_node;
break;
/* Shift operations: result has same type as first operand;
always convert second operand to int.
Also set SHORT_SHIFT if shifting rightward. */
case RSHIFT_EXPR:
if (gnu_vector_type_p (type0)
&& code1 == INTEGER_TYPE
&& TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
{
result_type = type0;
converted = 1;
}
else if (gnu_vector_type_p (type0)
&& gnu_vector_type_p (type1)
&& TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
&& TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
&& known_eq (TYPE_VECTOR_SUBPARTS (type0),
TYPE_VECTOR_SUBPARTS (type1)))
{
result_type = type0;
converted = 1;
}
else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
{
tree const_op1 = fold_for_warn (op1);
if (TREE_CODE (const_op1) != INTEGER_CST)
const_op1 = op1;
result_type = type0;
doing_shift = true;
if (TREE_CODE (const_op1) == INTEGER_CST)
{
if (tree_int_cst_lt (const_op1, integer_zero_node))
{
if ((complain & tf_warning)
&& c_inhibit_evaluation_warnings == 0)
warning_at (location, OPT_Wshift_count_negative,
"right shift count is negative");
}
else
{
if (!integer_zerop (const_op1))
short_shift = 1;
if (compare_tree_int (const_op1, TYPE_PRECISION (type0)) >= 0
&& (complain & tf_warning)
&& c_inhibit_evaluation_warnings == 0)
warning_at (location, OPT_Wshift_count_overflow,
"right shift count >= width of type");
}
}
/* Avoid converting op1 to result_type later. */
converted = 1;
}
break;
case LSHIFT_EXPR:
if (gnu_vector_type_p (type0)
&& code1 == INTEGER_TYPE
&& TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE)
{
result_type = type0;
converted = 1;
}
else if (gnu_vector_type_p (type0)
&& gnu_vector_type_p (type1)
&& TREE_CODE (TREE_TYPE (type0)) == INTEGER_TYPE
&& TREE_CODE (TREE_TYPE (type1)) == INTEGER_TYPE
&& known_eq (TYPE_VECTOR_SUBPARTS (type0),
TYPE_VECTOR_SUBPARTS (type1)))
{
result_type = type0;
converted = 1;
}
else if (code0 == INTEGER_TYPE && code1 == INTEGER_TYPE)
{
tree const_op0 = fold_for_warn (op0);
if (TREE_CODE (const_op0) != INTEGER_CST)
const_op0 = op0;
tree const_op1 = fold_for_warn (op1);
if (TREE_CODE (const_op1) != INTEGER_CST)
const_op1 = op1;
result_type = type0;
doing_shift = true;
if (TREE_CODE (const_op0) == INTEGER_CST
&& tree_int_cst_sgn (const_op0) < 0
&& !TYPE_OVERFLOW_WRAPS (type0)
&& (complain & tf_warning)
&& c_inhibit_evaluation_warnings == 0)
warning_at (location, OPT_Wshift_negative_value,
"left shift of negative value");
if (TREE_CODE (const_op1) == INTEGER_CST)
{
if (tree_int_cst_lt (const_op1, integer_zero_node))
{
if ((complain & tf_warning)
&& c_inhibit_evaluation_warnings == 0)
warning_at (location, OPT_Wshift_count_negative,
"left shift count is negative");
}
else if (compare_tree_int (const_op1,
TYPE_PRECISION (type0)) >= 0)
{
if ((complain & tf_warning)
&& c_inhibit_evaluation_warnings == 0)
warning_at (location, OPT_Wshift_count_overflow,
"left shift count >= width of type");
}
else if (TREE_CODE (const_op0) == INTEGER_CST
&& (complain & tf_warning))
maybe_warn_shift_overflow (location, const_op0, const_op1);
}
/* Avoid converting op1 to result_type later. */
converted = 1;
}
break;
case EQ_EXPR:
case NE_EXPR:
if (gnu_vector_type_p (type0) && gnu_vector_type_p (type1))
goto vector_compare;
if ((complain & tf_warning)
&& c_inhibit_evaluation_warnings == 0
&& (FLOAT_TYPE_P (type0) || FLOAT_TYPE_P (type1)))
warning_at (location, OPT_Wfloat_equal,
"comparing floating-point with %<==%> "
"or %<!=%> is unsafe");
if (complain & tf_warning)
{
tree stripped_orig_op0 = tree_strip_any_location_wrapper (orig_op0);
tree stripped_orig_op1 = tree_strip_any_location_wrapper (orig_op1);
if ((TREE_CODE (stripped_orig_op0) == STRING_CST
&& !integer_zerop (cp_fully_fold (op1)))
|| (TREE_CODE (stripped_orig_op1) == STRING_CST
&& !integer_zerop (cp_fully_fold (op0))))
warning_at (location, OPT_Waddress,
"comparison with string literal results in "
"unspecified behavior");
else if (warn_array_compare
&& TREE_CODE (TREE_TYPE (orig_op0)) == ARRAY_TYPE
&& TREE_CODE (TREE_TYPE (orig_op1)) == ARRAY_TYPE)
do_warn_array_compare (location, code, stripped_orig_op0,
stripped_orig_op1);
}
build_type = boolean_type_node;
if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
|| code0 == COMPLEX_TYPE || code0 == ENUMERAL_TYPE)
&& (code1 == INTEGER_TYPE || code1 == REAL_TYPE
|| code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE))
short_compare = 1;
else if (((code0 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type0))
&& null_ptr_cst_p (orig_op1))
/* Handle, eg, (void*)0 (c++/43906), and more. */
|| (code0 == POINTER_TYPE
&& TYPE_PTR_P (type1) && integer_zerop (op1)))
{
if (TYPE_PTR_P (type1))
result_type = composite_pointer_type (location,
type0, type1, op0, op1,
CPO_COMPARISON, complain);
else
result_type = type0;
if (char_type_p (TREE_TYPE (orig_op1)))
{
auto_diagnostic_group d;
if (warning_at (location, OPT_Wpointer_compare,
"comparison between pointer and zero character "
"constant"))
inform (location,
"did you mean to dereference the pointer?");
}
warn_for_null_address (location, op0, complain);
}
else if (((code1 == POINTER_TYPE || TYPE_PTRDATAMEM_P (type1))
&& null_ptr_cst_p (orig_op0))
/* Handle, eg, (void*)0 (c++/43906), and more. */
|| (code1 == POINTER_TYPE
&& TYPE_PTR_P (type0) && integer_zerop (op0)))
{
if (TYPE_PTR_P (type0))
result_type = composite_pointer_type (location,
type0, type1, op0, op1,
CPO_COMPARISON, complain);
else
result_type = type1;
if (char_type_p (TREE_TYPE (orig_op0)))
{
auto_diagnostic_group d;
if (warning_at (location, OPT_Wpointer_compare,
"comparison between pointer and zero character "
"constant"))
inform (location,
"did you mean to dereference the pointer?");
}
warn_for_null_address (location, op1, complain);
}
else if ((code0 == POINTER_TYPE && code1 == POINTER_TYPE)
|| (TYPE_PTRDATAMEM_P (type0) && TYPE_PTRDATAMEM_P (type1)))
result_type = composite_pointer_type (location,
type0, type1, op0, op1,
CPO_COMPARISON, complain);
else if (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1))
/* One of the operands must be of nullptr_t type. */
result_type = TREE_TYPE (nullptr_node);
else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
{
result_type = type0;
if (complain & tf_error)
permerror (location, "ISO C++ forbids comparison between "
"pointer and integer");
else
return error_mark_node;
}
else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
{
result_type = type1;
if (complain & tf_error)
permerror (location, "ISO C++ forbids comparison between "
"pointer and integer");
else
return error_mark_node;
}
else if (TYPE_PTRMEMFUNC_P (type0) && null_ptr_cst_p (orig_op1))
{
if (TARGET_PTRMEMFUNC_VBIT_LOCATION
== ptrmemfunc_vbit_in_delta)
{
tree pfn0, delta0, e1, e2;
if (TREE_SIDE_EFFECTS (op0))
op0 = cp_save_expr (op0);
pfn0 = pfn_from_ptrmemfunc (op0);
delta0 = delta_from_ptrmemfunc (op0);
{
/* If we will warn below about a null-address compare
involving the orig_op0 ptrmemfunc, we'd likely also
warn about the pfn0's null-address compare, and
that would be redundant, so suppress it. */
warning_sentinel ws (warn_address);
e1 = cp_build_binary_op (location,
EQ_EXPR,
pfn0,
build_zero_cst (TREE_TYPE (pfn0)),
complain);
}
e2 = cp_build_binary_op (location,
BIT_AND_EXPR,
delta0,
integer_one_node,
complain);
if (complain & tf_warning)
maybe_warn_zero_as_null_pointer_constant (op1, input_location);
e2 = cp_build_binary_op (location,
EQ_EXPR, e2, integer_zero_node,
complain);
op0 = cp_build_binary_op (location,
TRUTH_ANDIF_EXPR, e1, e2,
complain);
op1 = cp_convert (TREE_TYPE (op0), integer_one_node, complain);
}
else
{
op0 = build_ptrmemfunc_access_expr (op0, pfn_identifier);
op1 = cp_convert (TREE_TYPE (op0), op1, complain);
}
result_type = TREE_TYPE (op0);
warn_for_null_address (location, orig_op0, complain);
}
else if (TYPE_PTRMEMFUNC_P (type1) && null_ptr_cst_p (orig_op0))
return cp_build_binary_op (location, code, op1, op0, complain);
else if (TYPE_PTRMEMFUNC_P (type0) && TYPE_PTRMEMFUNC_P (type1))
{
tree type;
/* E will be the final comparison. */
tree e;
/* E1 and E2 are for scratch. */
tree e1;
tree e2;
tree pfn0;
tree pfn1;
tree delta0;
tree delta1;
type = composite_pointer_type (location, type0, type1, op0, op1,
CPO_COMPARISON, complain);
if (!same_type_p (TREE_TYPE (op0), type))
op0 = cp_convert_and_check (type, op0, complain);
if (!same_type_p (TREE_TYPE (op1), type))
op1 = cp_convert_and_check (type, op1, complain);
if (op0 == error_mark_node || op1 == error_mark_node)
return error_mark_node;
if (TREE_SIDE_EFFECTS (op0))
op0 = save_expr (op0);
if (TREE_SIDE_EFFECTS (op1))
op1 = save_expr (op1);
pfn0 = pfn_from_ptrmemfunc (op0);
pfn0 = cp_fully_fold (pfn0);
/* Avoid -Waddress warnings (c++/64877). */
if (TREE_CODE (pfn0) == ADDR_EXPR)
suppress_warning (pfn0, OPT_Waddress);
pfn1 = pfn_from_ptrmemfunc (op1);
pfn1 = cp_fully_fold (pfn1);
delta0 = delta_from_ptrmemfunc (op0);
delta1 = delta_from_ptrmemfunc (op1);
if (TARGET_PTRMEMFUNC_VBIT_LOCATION
== ptrmemfunc_vbit_in_delta)
{
/* We generate:
(op0.pfn == op1.pfn
&& ((op0.delta == op1.delta)
|| (!op0.pfn && op0.delta & 1 == 0
&& op1.delta & 1 == 0))
The reason for the `!op0.pfn' bit is that a NULL
pointer-to-member is any member with a zero PFN and
LSB of the DELTA field is 0. */
e1 = cp_build_binary_op (location, BIT_AND_EXPR,
delta0,
integer_one_node,
complain);
e1 = cp_build_binary_op (location,
EQ_EXPR, e1, integer_zero_node,
complain);
e2 = cp_build_binary_op (location, BIT_AND_EXPR,
delta1,
integer_one_node,
complain);
e2 = cp_build_binary_op (location,
EQ_EXPR, e2, integer_zero_node,
complain);
e1 = cp_build_binary_op (location,
TRUTH_ANDIF_EXPR, e2, e1,
complain);
e2 = cp_build_binary_op (location, EQ_EXPR,
pfn0,
build_zero_cst (TREE_TYPE (pfn0)),
complain);
e2 = cp_build_binary_op (location,
TRUTH_ANDIF_EXPR, e2, e1, complain);
e1 = cp_build_binary_op (location,
EQ_EXPR, delta0, delta1, complain);
e1 = cp_build_binary_op (location,
TRUTH_ORIF_EXPR, e1, e2, complain);
}
else
{
/* We generate:
(op0.pfn == op1.pfn
&& (!op0.pfn || op0.delta == op1.delta))
The reason for the `!op0.pfn' bit is that a NULL
pointer-to-member is any member with a zero PFN; the
DELTA field is unspecified. */
e1 = cp_build_binary_op (location,
EQ_EXPR, delta0, delta1, complain);
e2 = cp_build_binary_op (location,
EQ_EXPR,
pfn0,
build_zero_cst (TREE_TYPE (pfn0)),
complain);
e1 = cp_build_binary_op (location,
TRUTH_ORIF_EXPR, e1, e2, complain);
}
e2 = build2 (EQ_EXPR, boolean_type_node, pfn0, pfn1);
e = cp_build_binary_op (location,
TRUTH_ANDIF_EXPR, e2, e1, complain);
if (code == EQ_EXPR)
return e;
return cp_build_binary_op (location,
EQ_EXPR, e, integer_zero_node, complain);
}
else
{
gcc_assert (!TYPE_PTRMEMFUNC_P (type0)
|| !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type0),
type1));
gcc_assert (!TYPE_PTRMEMFUNC_P (type1)
|| !same_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type1),
type0));
}
break;
case MAX_EXPR:
case MIN_EXPR:
if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE)
&& (code1 == INTEGER_TYPE || code1 == REAL_TYPE))
shorten = 1;
else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
result_type = composite_pointer_type (location,
type0, type1, op0, op1,
CPO_COMPARISON, complain);
break;
case LE_EXPR:
case GE_EXPR:
case LT_EXPR:
case GT_EXPR:
case SPACESHIP_EXPR:
if (TREE_CODE (orig_op0) == STRING_CST
|| TREE_CODE (orig_op1) == STRING_CST)
{
if (complain & tf_warning)
warning_at (location, OPT_Waddress,
"comparison with string literal results "
"in unspecified behavior");
}
else if (warn_array_compare
&& TREE_CODE (TREE_TYPE (orig_op0)) == ARRAY_TYPE
&& TREE_CODE (TREE_TYPE (orig_op1)) == ARRAY_TYPE
&& code != SPACESHIP_EXPR
&& (complain & tf_warning))
do_warn_array_compare (location, code,
tree_strip_any_location_wrapper (orig_op0),
tree_strip_any_location_wrapper (orig_op1));
if (gnu_vector_type_p (type0) && gnu_vector_type_p (type1))
{
vector_compare:
tree intt;
if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type0),
TREE_TYPE (type1))
&& !vector_types_compatible_elements_p (type0, type1))
{
if (complain & tf_error)
{
error_at (location, "comparing vectors with different "
"element types");
inform (location, "operand types are %qT and %qT",
type0, type1);
}
return error_mark_node;
}
if (maybe_ne (TYPE_VECTOR_SUBPARTS (type0),
TYPE_VECTOR_SUBPARTS (type1)))
{
if (complain & tf_error)
{
error_at (location, "comparing vectors with different "
"number of elements");
inform (location, "operand types are %qT and %qT",
type0, type1);
}
return error_mark_node;
}
/* It's not precisely specified how the usual arithmetic
conversions apply to the vector types. Here, we use
the unsigned type if one of the operands is signed and
the other one is unsigned. */
if (TYPE_UNSIGNED (type0) != TYPE_UNSIGNED (type1))
{
if (!TYPE_UNSIGNED (type0))
op0 = build1 (VIEW_CONVERT_EXPR, type1, op0);
else
op1 = build1 (VIEW_CONVERT_EXPR, type0, op1);
warning_at (location, OPT_Wsign_compare, "comparison between "
"types %qT and %qT", type0, type1);
}
if (resultcode == SPACESHIP_EXPR)
{
if (complain & tf_error)
sorry_at (location, "three-way comparison of vectors");
return error_mark_node;
}
/* Always construct signed integer vector type. */
intt = c_common_type_for_size
(GET_MODE_BITSIZE (SCALAR_TYPE_MODE (TREE_TYPE (type0))), 0);
if (!intt)
{
if (complain & tf_error)
error_at (location, "could not find an integer type "
"of the same size as %qT", TREE_TYPE (type0));
return error_mark_node;
}
result_type = build_opaque_vector_type (intt,
TYPE_VECTOR_SUBPARTS (type0));
return build_vec_cmp (resultcode, result_type, op0, op1);
}
build_type = boolean_type_node;
if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
|| code0 == ENUMERAL_TYPE)
&& (code1 == INTEGER_TYPE || code1 == REAL_TYPE
|| code1 == ENUMERAL_TYPE))
short_compare = 1;
else if (code0 == POINTER_TYPE && code1 == POINTER_TYPE)
result_type = composite_pointer_type (location,
type0, type1, op0, op1,
CPO_COMPARISON, complain);
else if ((code0 == POINTER_TYPE && null_ptr_cst_p (orig_op1))
|| (code1 == POINTER_TYPE && null_ptr_cst_p (orig_op0))
|| (null_ptr_cst_p (orig_op0) && null_ptr_cst_p (orig_op1)))
{
/* Core Issue 1512 made this ill-formed. */
if (complain & tf_error)
error_at (location, "ordered comparison of pointer with "
"integer zero (%qT and %qT)", type0, type1);
return error_mark_node;
}
else if (code0 == POINTER_TYPE && code1 == INTEGER_TYPE)
{
result_type = type0;
if (complain & tf_error)
permerror (location, "ISO C++ forbids comparison between "
"pointer and integer");
else
return error_mark_node;
}
else if (code0 == INTEGER_TYPE && code1 == POINTER_TYPE)
{
result_type = type1;
if (complain & tf_error)
permerror (location, "ISO C++ forbids comparison between "
"pointer and integer");
else
return error_mark_node;
}
if ((code0 == POINTER_TYPE || code1 == POINTER_TYPE)
&& !processing_template_decl
&& sanitize_flags_p (SANITIZE_POINTER_COMPARE))
{
op0 = save_expr (op0);
op1 = save_expr (op1);
tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_COMPARE);
instrument_expr = build_call_expr_loc (location, tt, 2, op0, op1);
}
break;
case UNORDERED_EXPR:
case ORDERED_EXPR:
case UNLT_EXPR:
case UNLE_EXPR:
case UNGT_EXPR:
case UNGE_EXPR:
case UNEQ_EXPR:
build_type = integer_type_node;
if (code0 != REAL_TYPE || code1 != REAL_TYPE)
{
if (complain & tf_error)
error ("unordered comparison on non-floating-point argument");
return error_mark_node;
}
common = 1;
break;
default:
break;
}
if (((code0 == INTEGER_TYPE || code0 == REAL_TYPE || code0 == COMPLEX_TYPE
|| code0 == ENUMERAL_TYPE)
&& (code1 == INTEGER_TYPE || code1 == REAL_TYPE
|| code1 == COMPLEX_TYPE || code1 == ENUMERAL_TYPE)))
arithmetic_types_p = 1;
else
{
arithmetic_types_p = 0;
/* Vector arithmetic is only allowed when both sides are vectors. */
if (gnu_vector_type_p (type0) && gnu_vector_type_p (type1))
{
if (!tree_int_cst_equal (TYPE_SIZE (type0), TYPE_SIZE (type1))
|| !vector_types_compatible_elements_p (type0, type1))
{
if (complain & tf_error)
{
/* "location" already embeds the locations of the
operands, so we don't need to add them separately
to richloc. */
rich_location richloc (line_table, location);
binary_op_error (&richloc, code, type0, type1);
}
return error_mark_node;
}
arithmetic_types_p = 1;
}
}
/* Determine the RESULT_TYPE, if it is not already known. */
if (!result_type
&& arithmetic_types_p
&& (shorten || common || short_compare))
{
result_type = cp_common_type (type0, type1);
if (result_type == error_mark_node)
{
tree t1 = type0;
tree t2 = type1;
if (TREE_CODE (t1) == COMPLEX_TYPE)
t1 = TREE_TYPE (t1);
if (TREE_CODE (t2) == COMPLEX_TYPE)
t2 = TREE_TYPE (t2);
gcc_checking_assert (TREE_CODE (t1) == REAL_TYPE
&& TREE_CODE (t2) == REAL_TYPE
&& (extended_float_type_p (t1)
|| extended_float_type_p (t2))
&& cp_compare_floating_point_conversion_ranks
(t1, t2) == 3);
if (complain & tf_error)
{
rich_location richloc (line_table, location);
binary_op_error (&richloc, code, type0, type1);
}
return error_mark_node;
}
if (complain & tf_warning)
do_warn_double_promotion (result_type, type0, type1,
"implicit conversion from %qH to %qI "
"to match other operand of binary "
"expression", location);
if (do_warn_enum_conversions (location, code, TREE_TYPE (orig_op0),
TREE_TYPE (orig_op1), complain))
return error_mark_node;
}
if (may_need_excess_precision
&& (orig_type0 != type0 || orig_type1 != type1)
&& build_type == NULL_TREE
&& result_type)
{
gcc_assert (common);
semantic_result_type = cp_common_type (orig_type0, orig_type1);
if (semantic_result_type == error_mark_node)
{
tree t1 = orig_type0;
tree t2 = orig_type1;
if (TREE_CODE (t1) == COMPLEX_TYPE)
t1 = TREE_TYPE (t1);
if (TREE_CODE (t2) == COMPLEX_TYPE)
t2 = TREE_TYPE (t2);
gcc_checking_assert (TREE_CODE (t1) == REAL_TYPE
&& TREE_CODE (t2) == REAL_TYPE
&& (extended_float_type_p (t1)
|| extended_float_type_p (t2))
&& cp_compare_floating_point_conversion_ranks
(t1, t2) == 3);
if (complain & tf_error)
{
rich_location richloc (line_table, location);
binary_op_error (&richloc, code, type0, type1);
}
return error_mark_node;
}
}
if (code == SPACESHIP_EXPR)
{
iloc_sentinel s (location);
tree orig_type0 = TREE_TYPE (orig_op0);
tree_code orig_code0 = TREE_CODE (orig_type0);
tree orig_type1 = TREE_TYPE (orig_op1);
tree_code orig_code1 = TREE_CODE (orig_type1);
if (!result_type || result_type == error_mark_node)
/* Nope. */
result_type = NULL_TREE;
else if ((orig_code0 == BOOLEAN_TYPE) != (orig_code1 == BOOLEAN_TYPE))
/* "If one of the operands is of type bool and the other is not, the
program is ill-formed." */
result_type = NULL_TREE;
else if (code0 == POINTER_TYPE && orig_code0 != POINTER_TYPE
&& code1 == POINTER_TYPE && orig_code1 != POINTER_TYPE)
/* We only do array/function-to-pointer conversion if "at least one of
the operands is of pointer type". */
result_type = NULL_TREE;
else if (TYPE_PTRFN_P (result_type) || NULLPTR_TYPE_P (result_type))
/* <=> no longer supports equality relations. */
result_type = NULL_TREE;
else if (orig_code0 == ENUMERAL_TYPE && orig_code1 == ENUMERAL_TYPE
&& !(same_type_ignoring_top_level_qualifiers_p
(orig_type0, orig_type1)))
/* "If both operands have arithmetic types, or one operand has integral
type and the other operand has unscoped enumeration type, the usual
arithmetic conversions are applied to the operands." So we don't do
arithmetic conversions if the operands both have enumeral type. */
result_type = NULL_TREE;
else if ((orig_code0 == ENUMERAL_TYPE && orig_code1 == REAL_TYPE)
|| (orig_code0 == REAL_TYPE && orig_code1 == ENUMERAL_TYPE))
/* [depr.arith.conv.enum]: Three-way comparisons between such operands
[where one is of enumeration type and the other is of a different
enumeration type or a floating-point type] are ill-formed. */
result_type = NULL_TREE;
if (result_type)
{
build_type = spaceship_type (result_type, complain);
if (build_type == error_mark_node)
return error_mark_node;
}
if (result_type && arithmetic_types_p)
{
/* If a narrowing conversion is required, other than from an integral
type to a floating point type, the program is ill-formed. */
bool ok = true;
if (TREE_CODE (result_type) == REAL_TYPE
&& CP_INTEGRAL_TYPE_P (orig_type0))
/* OK */;
else if (!check_narrowing (result_type, orig_op0, complain))
ok = false;
if (TREE_CODE (result_type) == REAL_TYPE
&& CP_INTEGRAL_TYPE_P (orig_type1))
/* OK */;
else if (!check_narrowing (result_type, orig_op1, complain))
ok = false;
if (!ok && !(complain & tf_error))
return error_mark_node;
}
}
if (!result_type)
{
if (complain & tf_error)
{
binary_op_rich_location richloc (location,
orig_op0, orig_op1, true);
error_at (&richloc,
"invalid operands of types %qT and %qT to binary %qO",
TREE_TYPE (orig_op0), TREE_TYPE (orig_op1), code);
}
return error_mark_node;
}
/* If we're in a template, the only thing we need to know is the
RESULT_TYPE. */
if (processing_template_decl)
{
/* Since the middle-end checks the type when doing a build2, we
need to build the tree in pieces. This built tree will never
get out of the front-end as we replace it when instantiating
the template. */
tree tmp = build2 (resultcode,
build_type ? build_type : result_type,
NULL_TREE, op1);
TREE_OPERAND (tmp, 0) = op0;
if (semantic_result_type)
tmp = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, tmp);
return tmp;
}
/* Remember the original type; RESULT_TYPE might be changed later on
by shorten_binary_op. */
tree orig_type = result_type;
if (arithmetic_types_p)
{
bool first_complex = (code0 == COMPLEX_TYPE);
bool second_complex = (code1 == COMPLEX_TYPE);
int none_complex = (!first_complex && !second_complex);
/* Adapted from patch for c/24581. */
if (first_complex != second_complex
&& (code == PLUS_EXPR
|| code == MINUS_EXPR
|| code == MULT_EXPR
|| (code == TRUNC_DIV_EXPR && first_complex))
&& TREE_CODE (TREE_TYPE (result_type)) == REAL_TYPE
&& flag_signed_zeros)
{
/* An operation on mixed real/complex operands must be
handled specially, but the language-independent code can
more easily optimize the plain complex arithmetic if
-fno-signed-zeros. */
tree real_type = TREE_TYPE (result_type);
tree real, imag;
if (first_complex)
{
if (TREE_TYPE (op0) != result_type)
op0 = cp_convert_and_check (result_type, op0, complain);
if (TREE_TYPE (op1) != real_type)
op1 = cp_convert_and_check (real_type, op1, complain);
}
else
{
if (TREE_TYPE (op0) != real_type)
op0 = cp_convert_and_check (real_type, op0, complain);
if (TREE_TYPE (op1) != result_type)
op1 = cp_convert_and_check (result_type, op1, complain);
}
if (TREE_CODE (op0) == ERROR_MARK || TREE_CODE (op1) == ERROR_MARK)
return error_mark_node;
if (first_complex)
{
op0 = save_expr (op0);
real = cp_build_unary_op (REALPART_EXPR, op0, true, complain);
imag = cp_build_unary_op (IMAGPART_EXPR, op0, true, complain);
switch (code)
{
case MULT_EXPR:
case TRUNC_DIV_EXPR:
op1 = save_expr (op1);
imag = build2 (resultcode, real_type, imag, op1);
/* Fall through. */
case PLUS_EXPR:
case MINUS_EXPR:
real = build2 (resultcode, real_type, real, op1);
break;
default:
gcc_unreachable();
}
}
else
{
op1 = save_expr (op1);
real = cp_build_unary_op (REALPART_EXPR, op1, true, complain);
imag = cp_build_unary_op (IMAGPART_EXPR, op1, true, complain);
switch (code)
{
case MULT_EXPR:
op0 = save_expr (op0);
imag = build2 (resultcode, real_type, op0, imag);
/* Fall through. */
case PLUS_EXPR:
real = build2 (resultcode, real_type, op0, real);
break;
case MINUS_EXPR:
real = build2 (resultcode, real_type, op0, real);
imag = build1 (NEGATE_EXPR, real_type, imag);
break;
default:
gcc_unreachable();
}
}
result = build2 (COMPLEX_EXPR, result_type, real, imag);
if (semantic_result_type)
result = build1 (EXCESS_PRECISION_EXPR, semantic_result_type,
result);
return result;
}
/* For certain operations (which identify themselves by shorten != 0)
if both args were extended from the same smaller type,
do the arithmetic in that type and then extend.
shorten !=0 and !=1 indicates a bitwise operation.
For them, this optimization is safe only if
both args are zero-extended or both are sign-extended.
Otherwise, we might change the result.
E.g., (short)-1 | (unsigned short)-1 is (int)-1
but calculated in (unsigned short) it would be (unsigned short)-1. */
if (shorten && none_complex)
{
final_type = result_type;
result_type = shorten_binary_op (result_type, op0, op1,
shorten == -1);
}
/* Shifts can be shortened if shifting right. */
if (short_shift)
{
int unsigned_arg;
tree arg0 = get_narrower (op0, &unsigned_arg);
/* We're not really warning here but when we set short_shift we
used fold_for_warn to fold the operand. */
tree const_op1 = fold_for_warn (op1);
final_type = result_type;
if (arg0 == op0 && final_type == TREE_TYPE (op0))
unsigned_arg = TYPE_UNSIGNED (TREE_TYPE (op0));
if (TYPE_PRECISION (TREE_TYPE (arg0)) < TYPE_PRECISION (result_type)
&& tree_int_cst_sgn (const_op1) > 0
/* We can shorten only if the shift count is less than the
number of bits in the smaller type size. */
&& compare_tree_int (const_op1,
TYPE_PRECISION (TREE_TYPE (arg0))) < 0
/* We cannot drop an unsigned shift after sign-extension. */
&& (!TYPE_UNSIGNED (final_type) || unsigned_arg))
{
/* Do an unsigned shift if the operand was zero-extended. */
result_type
= c_common_signed_or_unsigned_type (unsigned_arg,
TREE_TYPE (arg0));
/* Convert value-to-be-shifted to that type. */
if (TREE_TYPE (op0) != result_type)
op0 = convert (result_type, op0);
converted = 1;
}
}
/* Comparison operations are shortened too but differently.
They identify themselves by setting short_compare = 1. */
if (short_compare)
{
/* We call shorten_compare only for diagnostics. */
tree xop0 = fold_simple (op0);
tree xop1 = fold_simple (op1);
tree xresult_type = result_type;
enum tree_code xresultcode = resultcode;
shorten_compare (location, &xop0, &xop1, &xresult_type,
&xresultcode);
}
if ((short_compare || code == MIN_EXPR || code == MAX_EXPR)
&& warn_sign_compare
/* Do not warn until the template is instantiated; we cannot
bound the ranges of the arguments until that point. */
&& !processing_template_decl
&& (complain & tf_warning)
&& c_inhibit_evaluation_warnings == 0
/* Even unsigned enum types promote to signed int. We don't
want to issue -Wsign-compare warnings for this case. */
&& !enum_cast_to_int (orig_op0)
&& !enum_cast_to_int (orig_op1))
{
warn_for_sign_compare (location, orig_op0, orig_op1, op0, op1,
result_type, resultcode);
}
}
/* If CONVERTED is zero, both args will be converted to type RESULT_TYPE.
Then the expression will be built.
It will be given type FINAL_TYPE if that is nonzero;
otherwise, it will be given type RESULT_TYPE. */
if (! converted)
{
warning_sentinel w (warn_sign_conversion, short_compare);
if (!same_type_p (TREE_TYPE (op0), result_type))
op0 = cp_convert_and_check (result_type, op0, complain);
if (!same_type_p (TREE_TYPE (op1), result_type))
op1 = cp_convert_and_check (result_type, op1, complain);
if (op0 == error_mark_node || op1 == error_mark_node)
return error_mark_node;
}
if (build_type == NULL_TREE)
build_type = result_type;
if (doing_shift
&& flag_strong_eval_order == 2
&& TREE_SIDE_EFFECTS (op1)
&& !processing_template_decl)
{
/* In C++17, in both op0 << op1 and op0 >> op1 op0 is sequenced before
op1, so if op1 has side-effects, use SAVE_EXPR around op0. */
op0 = cp_save_expr (op0);
instrument_expr = op0;
}
if (sanitize_flags_p ((SANITIZE_SHIFT
| SANITIZE_DIVIDE
| SANITIZE_FLOAT_DIVIDE
| SANITIZE_SI_OVERFLOW))
&& current_function_decl != NULL_TREE
&& !processing_template_decl
&& (doing_div_or_mod || doing_shift))
{
/* OP0 and/or OP1 might have side-effects. */
op0 = cp_save_expr (op0);
op1 = cp_save_expr (op1);
op0 = fold_non_dependent_expr (op0, complain);
op1 = fold_non_dependent_expr (op1, complain);
tree instrument_expr1 = NULL_TREE;
if (doing_div_or_mod
&& sanitize_flags_p (SANITIZE_DIVIDE
| SANITIZE_FLOAT_DIVIDE
| SANITIZE_SI_OVERFLOW))
{
/* For diagnostics we want to use the promoted types without
shorten_binary_op. So convert the arguments to the
original result_type. */
tree cop0 = op0;
tree cop1 = op1;
if (TREE_TYPE (cop0) != orig_type)
cop0 = cp_convert (orig_type, op0, complain);
if (TREE_TYPE (cop1) != orig_type)
cop1 = cp_convert (orig_type, op1, complain);
instrument_expr1 = ubsan_instrument_division (location, cop0, cop1);
}
else if (doing_shift && sanitize_flags_p (SANITIZE_SHIFT))
instrument_expr1 = ubsan_instrument_shift (location, code, op0, op1);
if (instrument_expr != NULL)
instrument_expr = add_stmt_to_compound (instrument_expr,
instrument_expr1);
else
instrument_expr = instrument_expr1;
}
result = build2_loc (location, resultcode, build_type, op0, op1);
if (final_type != 0)
result = cp_convert (final_type, result, complain);
if (instrument_expr != NULL)
result = build2 (COMPOUND_EXPR, TREE_TYPE (result),
instrument_expr, result);
if (resultcode == SPACESHIP_EXPR && !processing_template_decl)
result = get_target_expr (result, complain);
if (semantic_result_type)
result = build1 (EXCESS_PRECISION_EXPR, semantic_result_type, result);
if (!c_inhibit_evaluation_warnings)
{
if (!processing_template_decl)
{
op0 = cp_fully_fold (op0);
/* Only consider the second argument if the first isn't overflowed. */
if (!CONSTANT_CLASS_P (op0) || TREE_OVERFLOW_P (op0))
return result;
op1 = cp_fully_fold (op1);
if (!CONSTANT_CLASS_P (op1) || TREE_OVERFLOW_P (op1))
return result;
}
else if (!CONSTANT_CLASS_P (op0) || !CONSTANT_CLASS_P (op1)
|| TREE_OVERFLOW_P (op0) || TREE_OVERFLOW_P (op1))
return result;
tree result_ovl = fold_build2 (resultcode, build_type, op0, op1);
if (TREE_OVERFLOW_P (result_ovl))
overflow_warning (location, result_ovl);
}
return result;
}
/* Build a VEC_PERM_EXPR.
This is a simple wrapper for c_build_vec_perm_expr. */
tree
build_x_vec_perm_expr (location_t loc,
tree arg0, tree arg1, tree arg2,
tsubst_flags_t complain)
{
tree orig_arg0 = arg0;
tree orig_arg1 = arg1;
tree orig_arg2 = arg2;
if (processing_template_decl)
{
if (type_dependent_expression_p (arg0)
|| type_dependent_expression_p (arg1)
|| type_dependent_expression_p (arg2))
return build_min_nt_loc (loc, VEC_PERM_EXPR, arg0, arg1, arg2);
}
tree exp = c_build_vec_perm_expr (loc, arg0, arg1, arg2, complain & tf_error);
if (processing_template_decl && exp != error_mark_node)
return build_min_non_dep (VEC_PERM_EXPR, exp, orig_arg0,
orig_arg1, orig_arg2);
return exp;
}
/* Build a VEC_PERM_EXPR.
This is a simple wrapper for c_build_shufflevector. */
tree
build_x_shufflevector (location_t loc, vec<tree, va_gc> *args,
tsubst_flags_t complain)
{
tree arg0 = (*args)[0];
tree arg1 = (*args)[1];
if (processing_template_decl)
{
for (unsigned i = 0; i < args->length (); ++i)
if (i <= 1
? type_dependent_expression_p ((*args)[i])
: instantiation_dependent_expression_p ((*args)[i]))
{
tree exp = build_min_nt_call_vec (NULL, args);
CALL_EXPR_IFN (exp) = IFN_SHUFFLEVECTOR;
return exp;
}
}
auto_vec<tree, 16> mask;
for (unsigned i = 2; i < args->length (); ++i)
{
tree idx = fold_non_dependent_expr ((*args)[i], complain);
mask.safe_push (idx);
}
tree exp = c_build_shufflevector (loc, arg0, arg1, mask, complain & tf_error);
if (processing_template_decl && exp != error_mark_node)
{
exp = build_min_non_dep_call_vec (exp, NULL, args);
CALL_EXPR_IFN (exp) = IFN_SHUFFLEVECTOR;
}
return exp;
}
/* Return a tree for the sum or difference (RESULTCODE says which)
of pointer PTROP and integer INTOP. */
static tree
cp_pointer_int_sum (location_t loc, enum tree_code resultcode, tree ptrop,
tree intop, tsubst_flags_t complain)
{
tree res_type = TREE_TYPE (ptrop);
/* pointer_int_sum() uses size_in_bytes() on the TREE_TYPE(res_type)
in certain circumstance (when it's valid to do so). So we need
to make sure it's complete. We don't need to check here, if we
can actually complete it at all, as those checks will be done in
pointer_int_sum() anyway. */
complete_type (TREE_TYPE (res_type));
return pointer_int_sum (loc, resultcode, ptrop,
intop, complain & tf_warning_or_error);
}
/* Return a tree for the difference of pointers OP0 and OP1.
The resulting tree has type int. If POINTER_SUBTRACT sanitization is
enabled, assign to INSTRUMENT_EXPR call to libsanitizer. */
static tree
pointer_diff (location_t loc, tree op0, tree op1, tree ptrtype,
tsubst_flags_t complain, tree *instrument_expr)
{
tree result, inttype;
tree restype = ptrdiff_type_node;
tree target_type = TREE_TYPE (ptrtype);
if (!complete_type_or_maybe_complain (target_type, NULL_TREE, complain))
return error_mark_node;
if (VOID_TYPE_P (target_type))
{
if (complain & tf_error)
permerror (loc, "ISO C++ forbids using pointer of "
"type %<void *%> in subtraction");
else
return error_mark_node;
}
if (TREE_CODE (target_type) == FUNCTION_TYPE)
{
if (complain & tf_error)
permerror (loc, "ISO C++ forbids using pointer to "
"a function in subtraction");
else
return error_mark_node;
}
if (TREE_CODE (target_type) == METHOD_TYPE)
{
if (complain & tf_error)
permerror (loc, "ISO C++ forbids using pointer to "
"a method in subtraction");
else
return error_mark_node;
}
else if (!verify_type_context (loc, TCTX_POINTER_ARITH,
TREE_TYPE (TREE_TYPE (op0)),
!(complain & tf_error))
|| !verify_type_context (loc, TCTX_POINTER_ARITH,
TREE_TYPE (TREE_TYPE (op1)),
!(complain & tf_error)))
return error_mark_node;
/* Determine integer type result of the subtraction. This will usually
be the same as the result type (ptrdiff_t), but may need to be a wider
type if pointers for the address space are wider than ptrdiff_t. */
if (TYPE_PRECISION (restype) < TYPE_PRECISION (TREE_TYPE (op0)))
inttype = c_common_type_for_size (TYPE_PRECISION (TREE_TYPE (op0)), 0);
else
inttype = restype;
if (!processing_template_decl
&& sanitize_flags_p (SANITIZE_POINTER_SUBTRACT))
{
op0 = save_expr (op0);
op1 = save_expr (op1);
tree tt = builtin_decl_explicit (BUILT_IN_ASAN_POINTER_SUBTRACT);
*instrument_expr = build_call_expr_loc (loc, tt, 2, op0, op1);
}
/* First do the subtraction, then build the divide operator
and only convert at the very end.
Do not do default conversions in case restype is a short type. */
/* 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 (inttype) > TYPE_PRECISION (TREE_TYPE (op0)))
op0 = cp_build_binary_op (loc,
MINUS_EXPR,
cp_convert (inttype, op0, complain),
cp_convert (inttype, op1, complain),
complain);
else
op0 = build2_loc (loc, POINTER_DIFF_EXPR, inttype, op0, op1);
/* This generates an error if op1 is a pointer to an incomplete type. */
if (!COMPLETE_TYPE_P (TREE_TYPE (TREE_TYPE (op1))))
{
if (complain & tf_error)
error_at (loc, "invalid use of a pointer to an incomplete type in "
"pointer arithmetic");
else
return error_mark_node;
}
if (pointer_to_zero_sized_aggr_p (TREE_TYPE (op1)))
{
if (complain & tf_error)
error_at (loc, "arithmetic on pointer to an empty aggregate");
else
return error_mark_node;
}
op1 = (TYPE_PTROB_P (ptrtype)
? size_in_bytes_loc (loc, target_type)
: integer_one_node);
/* Do the division. */
result = build2_loc (loc, EXACT_DIV_EXPR, inttype, op0,
cp_convert (inttype, op1, complain));
return cp_convert (restype, result, complain);
}
/* Construct and perhaps optimize a tree representation
for a unary operation. CODE, a tree_code, specifies the operation
and XARG is the operand. */
tree
build_x_unary_op (location_t loc, enum tree_code code, cp_expr xarg,
tree lookups, tsubst_flags_t complain)
{
tree orig_expr = xarg;
tree exp;
int ptrmem = 0;
tree overload = NULL_TREE;
if (processing_template_decl)
{
if (type_dependent_expression_p (xarg))
{
tree e = build_min_nt_loc (loc, code, xarg.get_value (), NULL_TREE);
TREE_TYPE (e) = build_dependent_operator_type (lookups, code, false);
return e;
}
}
exp = NULL_TREE;
/* [expr.unary.op] says:
The address of an object of incomplete type can be taken.
(And is just the ordinary address operator, not an overloaded
"operator &".) However, if the type is a template
specialization, we must complete the type at this point so that
an overloaded "operator &" will be available if required. */
if (code == ADDR_EXPR
&& TREE_CODE (xarg) != TEMPLATE_ID_EXPR
&& ((CLASS_TYPE_P (TREE_TYPE (xarg))
&& !COMPLETE_TYPE_P (complete_type (TREE_TYPE (xarg))))
|| (TREE_CODE (xarg) == OFFSET_REF)))
/* Don't look for a function. */;
else
exp = build_new_op (loc, code, LOOKUP_NORMAL, xarg, NULL_TREE,
NULL_TREE, lookups, &overload, complain);
if (!exp && code == ADDR_EXPR)
{
if (is_overloaded_fn (xarg))
{
tree fn = get_first_fn (xarg);
if (DECL_CONSTRUCTOR_P (fn) || DECL_DESTRUCTOR_P (fn))
{
if (complain & tf_error)
error_at (loc, DECL_CONSTRUCTOR_P (fn)
? G_("taking address of constructor %qD")
: G_("taking address of destructor %qD"),
fn);
return error_mark_node;
}
}
/* A pointer to member-function can be formed only by saying
&X::mf. */
if (!flag_ms_extensions && TREE_CODE (TREE_TYPE (xarg)) == METHOD_TYPE
&& (TREE_CODE (xarg) != OFFSET_REF || !PTRMEM_OK_P (xarg)))
{
if (TREE_CODE (xarg) != OFFSET_REF
|| !TYPE_P (TREE_OPERAND (xarg, 0)))
{
if (complain & tf_error)
{
error_at (loc, "invalid use of %qE to form a "
"pointer-to-member-function", xarg.get_value ());
if (TREE_CODE (xarg) != OFFSET_REF)
inform (loc, " a qualified-id is required");
}
return error_mark_node;
}
else
{
if (complain & tf_error)
error_at (loc, "parentheses around %qE cannot be used to "
"form a pointer-to-member-function",
xarg.get_value ());
else
return error_mark_node;
PTRMEM_OK_P (xarg) = 1;
}
}
if (TREE_CODE (xarg) == OFFSET_REF)
{
ptrmem = PTRMEM_OK_P (xarg);
if (!ptrmem && !flag_ms_extensions
&& TREE_CODE (TREE_TYPE (TREE_OPERAND (xarg, 1))) == METHOD_TYPE)
{
/* A single non-static member, make sure we don't allow a
pointer-to-member. */
xarg = build2 (OFFSET_REF, TREE_TYPE (xarg),
TREE_OPERAND (xarg, 0),
ovl_make (TREE_OPERAND (xarg, 1)));
PTRMEM_OK_P (xarg) = ptrmem;
}
}
exp = cp_build_addr_expr_strict (xarg, complain);
if (TREE_CODE (exp) == PTRMEM_CST)
PTRMEM_CST_LOCATION (exp) = loc;
else
protected_set_expr_location (exp, loc);
}
if (processing_template_decl && exp != error_mark_node)
{
if (overload != NULL_TREE)
return (build_min_non_dep_op_overload
(code, exp, overload, orig_expr, integer_zero_node));
exp = build_min_non_dep (code, exp, orig_expr,
/*For {PRE,POST}{INC,DEC}REMENT_EXPR*/NULL_TREE);
}
if (TREE_CODE (exp) == ADDR_EXPR)
PTRMEM_OK_P (exp) = ptrmem;
return exp;
}
/* Construct and perhaps optimize a tree representation
for __builtin_addressof operation. ARG specifies the operand. */
tree
cp_build_addressof (location_t loc, tree arg, tsubst_flags_t complain)
{
tree orig_expr = arg;
if (processing_template_decl)
{
if (type_dependent_expression_p (arg))
return build_min_nt_loc (loc, ADDRESSOF_EXPR, arg, NULL_TREE);
}
tree exp = cp_build_addr_expr_strict (arg, complain);
if (processing_template_decl && exp != error_mark_node)
exp = build_min_non_dep (ADDRESSOF_EXPR, exp, orig_expr, NULL_TREE);
return exp;
}
/* Like c_common_truthvalue_conversion, but handle pointer-to-member
constants, where a null value is represented by an INTEGER_CST of
-1. */
tree
cp_truthvalue_conversion (tree expr, tsubst_flags_t complain)
{
tree type = TREE_TYPE (expr);
location_t loc = cp_expr_loc_or_input_loc (expr);
if (TYPE_PTR_OR_PTRMEM_P (type)
/* Avoid ICE on invalid use of non-static member function. */
|| TREE_CODE (expr) == FUNCTION_DECL)
return cp_build_binary_op (loc, NE_EXPR, expr, nullptr_node, complain);
else
return c_common_truthvalue_conversion (loc, expr);
}
/* Returns EXPR contextually converted to bool. */
tree
contextual_conv_bool (tree expr, tsubst_flags_t complain)
{
return perform_implicit_conversion_flags (boolean_type_node, expr,
complain, LOOKUP_NORMAL);
}
/* Just like cp_truthvalue_conversion, but we want a CLEANUP_POINT_EXPR. This
is a low-level function; most callers should use maybe_convert_cond. */
tree
condition_conversion (tree expr)
{
tree t = contextual_conv_bool (expr, tf_warning_or_error);
if (!processing_template_decl)
t = fold_build_cleanup_point_expr (boolean_type_node, t);
return t;
}
/* Returns the address of T. This function will fold away
ADDR_EXPR of INDIRECT_REF. This is only for low-level usage;
most places should use cp_build_addr_expr instead. */
tree
build_address (tree t)
{
if (error_operand_p (t) || !cxx_mark_addressable (t))
return error_mark_node;
gcc_checking_assert (TREE_CODE (t) != CONSTRUCTOR
|| processing_template_decl);
t = build_fold_addr_expr_loc (EXPR_LOCATION (t), t);
if (TREE_CODE (t) != ADDR_EXPR)
t = rvalue (t);
return t;
}
/* Return a NOP_EXPR converting EXPR to TYPE. */
tree
build_nop (tree type, tree expr)
{
if (type == error_mark_node || error_operand_p (expr))
return expr;
return build1_loc (EXPR_LOCATION (expr), NOP_EXPR, type, expr);
}
/* Take the address of ARG, whatever that means under C++ semantics.
If STRICT_LVALUE is true, require an lvalue; otherwise, allow xvalues
and class rvalues as well.
Nothing should call this function directly; instead, callers should use
cp_build_addr_expr or cp_build_addr_expr_strict. */
static tree
cp_build_addr_expr_1 (tree arg, bool strict_lvalue, tsubst_flags_t complain)
{
tree argtype;
tree val;
if (!arg || error_operand_p (arg))
return error_mark_node;
arg = mark_lvalue_use (arg);
if (error_operand_p (arg))
return error_mark_node;
argtype = lvalue_type (arg);
location_t loc = cp_expr_loc_or_input_loc (arg);
gcc_assert (!(identifier_p (arg) && IDENTIFIER_ANY_OP_P (arg)));
if (TREE_CODE (arg) == COMPONENT_REF && type_unknown_p (arg)
&& !really_overloaded_fn (arg))
{
/* They're trying to take the address of a unique non-static
member function. This is ill-formed (except in MS-land),
but let's try to DTRT.
Note: We only handle unique functions here because we don't
want to complain if there's a static overload; non-unique
cases will be handled by instantiate_type. But we need to
handle this case here to allow casts on the resulting PMF.
We could defer this in non-MS mode, but it's easier to give
a useful error here. */
/* Inside constant member functions, the `this' pointer
contains an extra const qualifier. TYPE_MAIN_VARIANT
is used here to remove this const from the diagnostics
and the created OFFSET_REF. */
tree base = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (arg, 0)));
tree fn = get_first_fn (TREE_OPERAND (arg, 1));
if (!mark_used (fn, complain) && !(complain & tf_error))
return error_mark_node;
if (! flag_ms_extensions)
{
tree name = DECL_NAME (fn);
if (!(complain & tf_error))
return error_mark_node;
else if (current_class_type
&& TREE_OPERAND (arg, 0) == current_class_ref)
/* An expression like &memfn. */
permerror (loc,
"ISO C++ forbids taking the address of an unqualified"
" or parenthesized non-static member function to form"
" a pointer to member function. Say %<&%T::%D%>",
base, name);
else
permerror (loc,
"ISO C++ forbids taking the address of a bound member"
" function to form a pointer to member function."
" Say %<&%T::%D%>",
base, name);
}
arg = build_offset_ref (base, fn, /*address_p=*/true, complain);
}
/* Uninstantiated types are all functions. Taking the
address of a function is a no-op, so just return the
argument. */
if (type_unknown_p (arg))
return build1 (ADDR_EXPR, unknown_type_node, arg);
if (TREE_CODE (arg) == OFFSET_REF)
/* We want a pointer to member; bypass all the code for actually taking
the address of something. */
goto offset_ref;
/* Anything not already handled and not a true memory reference
is an error. */
if (!FUNC_OR_METHOD_TYPE_P (argtype))
{
cp_lvalue_kind kind = lvalue_kind (arg);
if (kind == clk_none)
{
if (complain & tf_error)
lvalue_error (loc, lv_addressof);
return error_mark_node;
}
if (strict_lvalue && (kind & (clk_rvalueref|clk_class)))
{
if (!(complain & tf_error))
return error_mark_node;
/* Make this a permerror because we used to accept it. */
permerror (loc, "taking address of rvalue");
}
}
if (TYPE_REF_P (argtype))
{
tree type = build_pointer_type (TREE_TYPE (argtype));
arg = build1 (CONVERT_EXPR, type, arg);
return arg;
}
else if (pedantic && DECL_MAIN_P (tree_strip_any_location_wrapper (arg)))
{
/* ARM $3.4 */
/* Apparently a lot of autoconf scripts for C++ packages do this,
so only complain if -Wpedantic. */
if (complain & (flag_pedantic_errors ? tf_error : tf_warning))
pedwarn (loc, OPT_Wpedantic,
"ISO C++ forbids taking address of function %<::main%>");
else if (flag_pedantic_errors)
return error_mark_node;
}
/* Let &* cancel out to simplify resulting code. */
if (INDIRECT_REF_P (arg))
{
arg = TREE_OPERAND (arg, 0);
if (TYPE_REF_P (TREE_TYPE (arg)))
{
tree type = build_pointer_type (TREE_TYPE (TREE_TYPE (arg)));
arg = build1 (CONVERT_EXPR, type, arg);
}
else
/* Don't let this be an lvalue. */
arg = rvalue (arg);
return arg;
}
/* Handle complex lvalues (when permitted)
by reduction to simpler cases. */
val = unary_complex_lvalue (ADDR_EXPR, arg);
if (val != 0)
return val;
switch (TREE_CODE (arg))
{
CASE_CONVERT:
case FLOAT_EXPR:
case FIX_TRUNC_EXPR:
/* We should have handled this above in the lvalue_kind check. */
gcc_unreachable ();
break;
case BASELINK:
arg = BASELINK_FUNCTIONS (arg);
/* Fall through. */
case OVERLOAD:
arg = OVL_FIRST (arg);
break;
case OFFSET_REF:
offset_ref:
/* Turn a reference to a non-static data member into a
pointer-to-member. */
{
tree type;
tree t;
gcc_assert (PTRMEM_OK_P (arg));
t = TREE_OPERAND (arg, 1);
if (TYPE_REF_P (TREE_TYPE (t)))
{
if (complain & tf_error)
error_at (loc,
"cannot create pointer to reference member %qD", t);
return error_mark_node;
}
/* Forming a pointer-to-member is a use of non-pure-virtual fns. */
if (TREE_CODE (t) == FUNCTION_DECL
&& !DECL_PURE_VIRTUAL_P (t)
&& !mark_used (t, complain) && !(complain & tf_error))
return error_mark_node;
type = build_ptrmem_type (context_for_name_lookup (t),
TREE_TYPE (t));
t = make_ptrmem_cst (type, t);
return t;
}
default:
break;
}
if (argtype != error_mark_node)
argtype = build_pointer_type (argtype);
if (bitfield_p (arg))
{
if (complain & tf_error)
error_at (loc, "attempt to take address of bit-field");
return error_mark_node;
}
/* In a template, we are processing a non-dependent expression
so we can just form an ADDR_EXPR with the correct type. */
if (processing_template_decl || TREE_CODE (arg) != COMPONENT_REF)
{
if (!mark_single_function (arg, complain))
return error_mark_node;
val = build_address (arg);
if (TREE_CODE (arg) == OFFSET_REF)
PTRMEM_OK_P (val) = PTRMEM_OK_P (arg);
}
else if (BASELINK_P (TREE_OPERAND (arg, 1)))
{
tree fn = BASELINK_FUNCTIONS (TREE_OPERAND (arg, 1));
/* We can only get here with a single static member
function. */
gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
&& DECL_STATIC_FUNCTION_P (fn));
if (!mark_used (fn, complain) && !(complain & tf_error))
return error_mark_node;
val = build_address (fn);
if (TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
/* Do not lose object's side effects. */
val = build2 (COMPOUND_EXPR, TREE_TYPE (val),
TREE_OPERAND (arg, 0), val);
}
else
{
tree object = TREE_OPERAND (arg, 0);
tree field = TREE_OPERAND (arg, 1);
gcc_assert (same_type_ignoring_top_level_qualifiers_p
(TREE_TYPE (object), decl_type_context (field)));
val = build_address (arg);
}
if (TYPE_PTR_P (argtype)
&& TREE_CODE (TREE_TYPE (argtype)) == METHOD_TYPE)
{
build_ptrmemfunc_type (argtype);
val = build_ptrmemfunc (argtype, val, 0,
/*c_cast_p=*/false,
complain);
}
/* Ensure we have EXPR_LOCATION set for possible later diagnostics. */
if (TREE_CODE (val) == ADDR_EXPR
&& TREE_CODE (TREE_OPERAND (val, 0)) == FUNCTION_DECL)
SET_EXPR_LOCATION (val, input_location);
return val;
}
/* Take the address of ARG if it has one, even if it's an rvalue. */
tree
cp_build_addr_expr (tree arg, tsubst_flags_t complain)
{
return cp_build_addr_expr_1 (arg, 0, complain);
}
/* Take the address of ARG, but only if it's an lvalue. */
static tree
cp_build_addr_expr_strict (tree arg, tsubst_flags_t complain)
{
return cp_build_addr_expr_1 (arg, 1, complain);
}
/* C++: Must handle pointers to members.
Perhaps type instantiation should be extended to handle conversion
from aggregates to types we don't yet know we want? (Or are those
cases typically errors which should be reported?)
NOCONVERT suppresses the default promotions (such as from short to int). */
tree
cp_build_unary_op (enum tree_code code, tree xarg, bool noconvert,
tsubst_flags_t complain)
{
/* No default_conversion here. It causes trouble for ADDR_EXPR. */
tree arg = xarg;
location_t location = cp_expr_loc_or_input_loc (arg);
tree argtype = 0;
tree eptype = NULL_TREE;
const char *errstring = NULL;
tree val;
const char *invalid_op_diag;
if (!arg || error_operand_p (arg))
return error_mark_node;
arg = resolve_nondeduced_context (arg, complain);
if ((invalid_op_diag
= targetm.invalid_unary_op ((code == UNARY_PLUS_EXPR
? CONVERT_EXPR
: code),
TREE_TYPE (arg))))
{
if (complain & tf_error)
error (invalid_op_diag);
return error_mark_node;
}
if (TREE_CODE (arg) == EXCESS_PRECISION_EXPR)
{
eptype = TREE_TYPE (arg);
arg = TREE_OPERAND (arg, 0);
}
switch (code)
{
case UNARY_PLUS_EXPR:
case NEGATE_EXPR:
{
int flags = WANT_ARITH | WANT_ENUM;
/* Unary plus (but not unary minus) is allowed on pointers. */
if (code == UNARY_PLUS_EXPR)
flags |= WANT_POINTER;
arg = build_expr_type_conversion (flags, arg, true);
if (!arg)
errstring = (code == NEGATE_EXPR
? _("wrong type argument to unary minus")
: _("wrong type argument to unary plus"));
else
{
if (!noconvert && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (arg)))
arg = cp_perform_integral_promotions (arg, complain);
/* Make sure the result is not an lvalue: a unary plus or minus
expression is always a rvalue. */
arg = rvalue (arg);
}
}
break;
case BIT_NOT_EXPR:
if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
{
code = CONJ_EXPR;
if (!noconvert)
{
arg = cp_default_conversion (arg, complain);
if (arg == error_mark_node)
return error_mark_node;
}
}
else if (!(arg = build_expr_type_conversion (WANT_INT | WANT_ENUM
| WANT_VECTOR_OR_COMPLEX,
arg, true)))
errstring = _("wrong type argument to bit-complement");
else if (!noconvert && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (arg)))
{
/* Warn if the expression has boolean value. */
if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE
&& (complain & tf_warning)
&& warning_at (location, OPT_Wbool_operation,
"%<~%> on an expression of type %<bool%>"))
inform (location, "did you mean to use logical not (%<!%>)?");
arg = cp_perform_integral_promotions (arg, complain);
}
else if (!noconvert && VECTOR_TYPE_P (TREE_TYPE (arg)))
arg = mark_rvalue_use (arg);
break;
case ABS_EXPR:
if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
errstring = _("wrong type argument to abs");
else if (!noconvert)
{
arg = cp_default_conversion (arg, complain);
if (arg == error_mark_node)
return error_mark_node;
}
break;
case CONJ_EXPR:
/* Conjugating a real value is a no-op, but allow it anyway. */
if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_ENUM, arg, true)))
errstring = _("wrong type argument to conjugation");
else if (!noconvert)
{
arg = cp_default_conversion (arg, complain);
if (arg == error_mark_node)
return error_mark_node;
}
break;
case TRUTH_NOT_EXPR:
if (gnu_vector_type_p (TREE_TYPE (arg)))
return cp_build_binary_op (input_location, EQ_EXPR, arg,
build_zero_cst (TREE_TYPE (arg)), complain);
arg = perform_implicit_conversion (boolean_type_node, arg,
complain);
if (arg != error_mark_node)
{
if (processing_template_decl)
return build1_loc (location, TRUTH_NOT_EXPR, boolean_type_node, arg);
val = invert_truthvalue_loc (location, arg);
if (obvalue_p (val))
val = non_lvalue_loc (location, val);
return val;
}
errstring = _("in argument to unary !");
break;
case NOP_EXPR:
break;
case REALPART_EXPR:
case IMAGPART_EXPR:
val = build_real_imag_expr (input_location, code, arg);
if (eptype && TREE_CODE (eptype) == COMPLEX_EXPR)
val = build1_loc (input_location, EXCESS_PRECISION_EXPR,
TREE_TYPE (eptype), val);
return val;
case PREINCREMENT_EXPR:
case POSTINCREMENT_EXPR:
case PREDECREMENT_EXPR:
case POSTDECREMENT_EXPR:
/* Handle complex lvalues (when permitted)
by reduction to simpler cases. */
val = unary_complex_lvalue (code, arg);
if (val != 0)
goto return_build_unary_op;
arg = mark_lvalue_use (arg);
/* Increment or decrement the real part of the value,
and don't change the imaginary part. */
if (TREE_CODE (TREE_TYPE (arg)) == COMPLEX_TYPE)
{
tree real, imag;
arg = cp_stabilize_reference (arg);
real = cp_build_unary_op (REALPART_EXPR, arg, true, complain);
imag = cp_build_unary_op (IMAGPART_EXPR, arg, true, complain);
real = cp_build_unary_op (code, real, true, complain);
if (real == error_mark_node || imag == error_mark_node)
return error_mark_node;
val = build2 (COMPLEX_EXPR, TREE_TYPE (arg), real, imag);
goto return_build_unary_op;
}
/* Report invalid types. */
if (!(arg = build_expr_type_conversion (WANT_ARITH | WANT_POINTER,
arg, true)))
{
if (code == PREINCREMENT_EXPR)
errstring = _("no pre-increment operator for type");
else if (code == POSTINCREMENT_EXPR)
errstring = _("no post-increment operator for type");
else if (code == PREDECREMENT_EXPR)
errstring = _("no pre-decrement operator for type");
else
errstring = _("no post-decrement operator for type");
break;
}
else if (arg == error_mark_node)
return error_mark_node;
/* Report something read-only. */
if (CP_TYPE_CONST_P (TREE_TYPE (arg))
|| TREE_READONLY (arg))
{
if (complain & tf_error)
cxx_readonly_error (location, arg,
((code == PREINCREMENT_EXPR
|| code == POSTINCREMENT_EXPR)
? lv_increment : lv_decrement));
else
return error_mark_node;
}
{
tree inc;
tree declared_type = unlowered_expr_type (arg);
argtype = TREE_TYPE (arg);
/* ARM $5.2.5 last annotation says this should be forbidden. */
if (TREE_CODE (argtype) == ENUMERAL_TYPE)
{
if (complain & tf_error)
permerror (location, (code == PREINCREMENT_EXPR
|| code == POSTINCREMENT_EXPR)
? G_("ISO C++ forbids incrementing an enum")
: G_("ISO C++ forbids decrementing an enum"));
else
return error_mark_node;
}
/* Compute the increment. */
if (TYPE_PTR_P (argtype))
{
tree type = complete_type (TREE_TYPE (argtype));
if (!COMPLETE_OR_VOID_TYPE_P (type))
{
if (complain & tf_error)
error_at (location, ((code == PREINCREMENT_EXPR
|| code == POSTINCREMENT_EXPR))
? G_("cannot increment a pointer to incomplete "
"type %qT")
: G_("cannot decrement a pointer to incomplete "
"type %qT"),
TREE_TYPE (argtype));
else
return error_mark_node;
}
else if (!TYPE_PTROB_P (argtype))
{
if (complain & tf_error)
pedwarn (location, OPT_Wpointer_arith,
(code == PREINCREMENT_EXPR
|| code == POSTINCREMENT_EXPR)
? G_("ISO C++ forbids incrementing a pointer "
"of type %qT")
: G_("ISO C++ forbids decrementing a pointer "
"of type %qT"),
argtype);
else
return error_mark_node;
}
else if (!verify_type_context (location, TCTX_POINTER_ARITH,
TREE_TYPE (argtype),
!(complain & tf_error)))
return error_mark_node;
inc = cxx_sizeof_nowarn (TREE_TYPE (argtype));
}
else
inc = VECTOR_TYPE_P (argtype)
? build_one_cst (argtype)
: integer_one_node;
inc = cp_convert (argtype, inc, complain);
/* If 'arg' is an Objective-C PROPERTY_REF expression, then we
need to ask Objective-C to build the increment or decrement
expression for it. */
if (objc_is_property_ref (arg))
return objc_build_incr_expr_for_property_ref (input_location, code,
arg, inc);
/* Complain about anything else that is not a true lvalue. */
if (!lvalue_or_else (arg, ((code == PREINCREMENT_EXPR
|| code == POSTINCREMENT_EXPR)
? lv_increment : lv_decrement),
complain))
return error_mark_node;
/* [depr.volatile.type] "Postfix ++ and -- expressions and
prefix ++ and -- expressions of volatile-qualified arithmetic
and pointer types are deprecated." */
if ((TREE_THIS_VOLATILE (arg) || CP_TYPE_VOLATILE_P (TREE_TYPE (arg)))
&& (complain & tf_warning))
warning_at (location, OPT_Wvolatile,
"%qs expression of %<volatile%>-qualified type is "
"deprecated",
((code == PREINCREMENT_EXPR
|| code == POSTINCREMENT_EXPR)
? "++" : "--"));
/* Forbid using -- or ++ in C++17 on `bool'. */
if (TREE_CODE (declared_type) == BOOLEAN_TYPE)
{
if (code == POSTDECREMENT_EXPR || code == PREDECREMENT_EXPR)
{
if (complain & tf_error)
error_at (location,
"use of an operand of type %qT in %<operator--%> "
"is forbidden", boolean_type_node);
return error_mark_node;
}
else
{
if (cxx_dialect >= cxx17)
{
if (complain & tf_error)
error_at (location,
"use of an operand of type %qT in "
"%<operator++%> is forbidden in C++17",
boolean_type_node);
return error_mark_node;
}
/* Otherwise, [depr.incr.bool] says this is deprecated. */
else if (complain & tf_warning)
warning_at (location, OPT_Wdeprecated,
"use of an operand of type %qT "
"in %<operator++%> is deprecated",
boolean_type_node);
}
val = boolean_increment (code, arg);
}
else if (code == POSTINCREMENT_EXPR || code == POSTDECREMENT_EXPR)
/* An rvalue has no cv-qualifiers. */
val = build2 (code, cv_unqualified (TREE_TYPE (arg)), arg, inc);
else
val = build2 (code, TREE_TYPE (arg), arg, inc);
TREE_SIDE_EFFECTS (val) = 1;
goto return_build_unary_op;
}
case ADDR_EXPR:
/* Note that this operation never does default_conversion
regardless of NOCONVERT. */
return cp_build_addr_expr (arg, complain);
default:
break;
}
if (!errstring)
{
if (argtype == 0)
argtype = TREE_TYPE (arg);
val = build1 (code, argtype, arg);
return_build_unary_op:
if (eptype)
val = build1 (EXCESS_PRECISION_EXPR, eptype, val);
return val;
}
if (complain & tf_error)
error_at (location, "%s", errstring);
return error_mark_node;
}
/* Hook for the c-common bits that build a unary op. */
tree
build_unary_op (location_t /*location*/,
enum tree_code code, tree xarg, bool noconvert)
{
return cp_build_unary_op (code, xarg, noconvert, tf_warning_or_error);
}
/* Adjust LVALUE, an MODIFY_EXPR, PREINCREMENT_EXPR or PREDECREMENT_EXPR,
so that it is a valid lvalue even for GENERIC by replacing
(lhs = rhs) with ((lhs = rhs), lhs)
(--lhs) with ((--lhs), lhs)
(++lhs) with ((++lhs), lhs)
and if lhs has side-effects, calling cp_stabilize_reference on it, so
that it can be evaluated multiple times. */
tree
genericize_compound_lvalue (tree lvalue)
{
if (TREE_SIDE_EFFECTS (TREE_OPERAND (lvalue, 0)))
lvalue = build2 (TREE_CODE (lvalue), TREE_TYPE (lvalue),
cp_stabilize_reference (TREE_OPERAND (lvalue, 0)),
TREE_OPERAND (lvalue, 1));
return build2 (COMPOUND_EXPR, TREE_TYPE (TREE_OPERAND (lvalue, 0)),
lvalue, TREE_OPERAND (lvalue, 0));
}
/* Apply unary lvalue-demanding operator CODE to the expression ARG
for certain kinds of expressions which are not really lvalues
but which we can accept as lvalues.
If ARG is not a kind of expression we can handle, return
NULL_TREE. */
tree
unary_complex_lvalue (enum tree_code code, tree arg)
{
/* Inside a template, making these kinds of adjustments is
pointless; we are only concerned with the type of the
expression. */
if (processing_template_decl)
return NULL_TREE;
/* Handle (a, b) used as an "lvalue". */
if (TREE_CODE (arg) == COMPOUND_EXPR)
{
tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 1), false,
tf_warning_or_error);
return build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
TREE_OPERAND (arg, 0), real_result);
}
/* Handle (a ? b : c) used as an "lvalue". */
if (TREE_CODE (arg) == COND_EXPR
|| TREE_CODE (arg) == MIN_EXPR || TREE_CODE (arg) == MAX_EXPR)
return rationalize_conditional_expr (code, arg, tf_warning_or_error);
/* Handle (a = b), (++a), and (--a) used as an "lvalue". */
if (TREE_CODE (arg) == MODIFY_EXPR
|| TREE_CODE (arg) == PREINCREMENT_EXPR
|| TREE_CODE (arg) == PREDECREMENT_EXPR)
return unary_complex_lvalue (code, genericize_compound_lvalue (arg));
if (code != ADDR_EXPR)
return NULL_TREE;
/* Handle (a = b) used as an "lvalue" for `&'. */
if (TREE_CODE (arg) == MODIFY_EXPR
|| TREE_CODE (arg) == INIT_EXPR)
{
tree real_result = cp_build_unary_op (code, TREE_OPERAND (arg, 0), false,
tf_warning_or_error);
arg = build2 (COMPOUND_EXPR, TREE_TYPE (real_result),
arg, real_result);
suppress_warning (arg /* What warning? */);
return arg;
}
if (FUNC_OR_METHOD_TYPE_P (TREE_TYPE (arg))
|| TREE_CODE (arg) == OFFSET_REF)
return NULL_TREE;
/* We permit compiler to make function calls returning
objects of aggregate type look like lvalues. */
{
tree targ = arg;
if (TREE_CODE (targ) == SAVE_EXPR)
targ = TREE_OPERAND (targ, 0);
if (TREE_CODE (targ) == CALL_EXPR && MAYBE_CLASS_TYPE_P (TREE_TYPE (targ)))
{
if (TREE_CODE (arg) == SAVE_EXPR)
targ = arg;
else
targ = build_cplus_new (TREE_TYPE (arg), arg, tf_warning_or_error);
return build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (arg)), targ);
}
if (TREE_CODE (arg) == SAVE_EXPR && INDIRECT_REF_P (targ))
return build3 (SAVE_EXPR, build_pointer_type (TREE_TYPE (arg)),
TREE_OPERAND (targ, 0), current_function_decl, NULL);
}
/* Don't let anything else be handled specially. */
return NULL_TREE;
}
/* Mark EXP saying that we need to be able to take the
address of it; it should not be allocated in a register.
Value is true if successful. ARRAY_REF_P is true if this
is for ARRAY_REF construction - in that case we don't want
to look through VIEW_CONVERT_EXPR from VECTOR_TYPE to ARRAY_TYPE,
it is fine to use ARRAY_REFs for vector subscripts on vector
register variables.
C++: we do not allow `current_class_ptr' to be addressable. */
bool
cxx_mark_addressable (tree exp, bool array_ref_p)
{
tree x = exp;
while (1)
switch (TREE_CODE (x))
{
case VIEW_CONVERT_EXPR:
if (array_ref_p
&& TREE_CODE (TREE_TYPE (x)) == ARRAY_TYPE
&& VECTOR_TYPE_P (TREE_TYPE (TREE_OPERAND (x, 0))))
return true;
x = TREE_OPERAND (x, 0);
break;
case COMPONENT_REF:
if (bitfield_p (x))
error ("attempt to take address of bit-field");
/* FALLTHRU */
case ADDR_EXPR:
case ARRAY_REF:
case REALPART_EXPR:
case IMAGPART_EXPR:
x = TREE_OPERAND (x, 0);
break;
case PARM_DECL:
if (x == current_class_ptr)
{
error ("cannot take the address of %<this%>, which is an rvalue expression");
TREE_ADDRESSABLE (x) = 1; /* so compiler doesn't die later. */
return true;
}
/* Fall through. */
case VAR_DECL:
/* Caller should not be trying to mark initialized
constant fields addressable. */
gcc_assert (DECL_LANG_SPECIFIC (x) == 0
|| DECL_IN_AGGR_P (x) == 0
|| TREE_STATIC (x)
|| DECL_EXTERNAL (x));
/* Fall through. */
case RESULT_DECL:
if (DECL_REGISTER (x) && !TREE_ADDRESSABLE (x)
&& !DECL_ARTIFICIAL (x))
{
if (VAR_P (x) && DECL_HARD_REGISTER (x))
{
error
("address of explicit register variable %qD requested", x);
return false;
}
else if (extra_warnings)
warning
(OPT_Wextra, "address requested for %qD, which is declared %<register%>", x);
}
TREE_ADDRESSABLE (x) = 1;
return true;
case CONST_DECL:
case FUNCTION_DECL:
TREE_ADDRESSABLE (x) = 1;
return true;
case CONSTRUCTOR:
TREE_ADDRESSABLE (x) = 1;
return true;
case TARGET_EXPR:
TREE_ADDRESSABLE (x) = 1;
cxx_mark_addressable (TREE_OPERAND (x, 0));
return true;
default:
return true;
}
}
/* Build and return a conditional expression IFEXP ? OP1 : OP2. */
tree
build_x_conditional_expr (location_t loc, tree ifexp, tree op1, tree op2,
tsubst_flags_t complain)
{
tree orig_ifexp = ifexp;
tree orig_op1 = op1;
tree orig_op2 = op2;
tree expr;
if (processing_template_decl)
{
/* The standard says that the expression is type-dependent if
IFEXP is type-dependent, even though the eventual type of the
expression doesn't dependent on IFEXP. */
if (type_dependent_expression_p (ifexp)
/* As a GNU extension, the middle operand may be omitted. */
|| (op1 && type_dependent_expression_p (op1))
|| type_dependent_expression_p (op2))
return build_min_nt_loc (loc, COND_EXPR, ifexp, op1, op2);
}
expr = build_conditional_expr (loc, ifexp, op1, op2, complain);
if (processing_template_decl && expr != error_mark_node)
{
tree min = build_min_non_dep (COND_EXPR, expr,
orig_ifexp, orig_op1, orig_op2);
expr = convert_from_reference (min);
}
return expr;
}
/* Given a list of expressions, return a compound expression
that performs them all and returns the value of the last of them. */
tree
build_x_compound_expr_from_list (tree list, expr_list_kind exp,
tsubst_flags_t complain)
{
tree expr = TREE_VALUE (list);
if (BRACE_ENCLOSED_INITIALIZER_P (expr)
&& !CONSTRUCTOR_IS_DIRECT_INIT (expr))
{
if (complain & tf_error)
pedwarn (cp_expr_loc_or_input_loc (expr), 0,
"list-initializer for non-class type must not "
"be parenthesized");
else
return error_mark_node;
}
if (TREE_CHAIN (list))
{
if (complain & tf_error)
switch (exp)
{
case ELK_INIT:
permerror (input_location, "expression list treated as compound "
"expression in initializer");
break;
case ELK_MEM_INIT:
permerror (input_location, "expression list treated as compound "
"expression in mem-initializer");
break;
case ELK_FUNC_CAST:
permerror (input_location, "expression list treated as compound "
"expression in functional cast");
break;
default:
gcc_unreachable ();
}
else
return error_mark_node;
for (list = TREE_CHAIN (list); list; list = TREE_CHAIN (list))
expr = build_x_compound_expr (EXPR_LOCATION (TREE_VALUE (list)),
expr, TREE_VALUE (list), NULL_TREE,
complain);
}
return expr;
}
/* Like build_x_compound_expr_from_list, but using a VEC. */
tree
build_x_compound_expr_from_vec (vec<tree, va_gc> *vec, const char *msg,
tsubst_flags_t complain)
{
if (vec_safe_is_empty (vec))
return NULL_TREE;
else if (vec->length () == 1)
return (*vec)[0];
else
{
tree expr;
unsigned int ix;
tree t;
if (msg != NULL)
{
if (complain & tf_error)
permerror (input_location,
"%s expression list treated as compound expression",
msg);
else
return error_mark_node;
}
expr = (*vec)[0];
for (ix = 1; vec->iterate (ix, &t); ++ix)
expr = build_x_compound_expr (EXPR_LOCATION (t), expr,
t, NULL_TREE, complain);
return expr;
}
}
/* Handle overloading of the ',' operator when needed. */
tree
build_x_compound_expr (location_t loc, tree op1, tree op2,
tree lookups, tsubst_flags_t complain)
{
tree result;
tree orig_op1 = op1;
tree orig_op2 = op2;
tree overload = NULL_TREE;
if (processing_template_decl)
{
if (type_dependent_expression_p (op1)
|| type_dependent_expression_p (op2))
{
result = build_min_nt_loc (loc, COMPOUND_EXPR, op1, op2);
TREE_TYPE (result)
= build_dependent_operator_type (lookups, COMPOUND_EXPR, false);
return result;
}
}
result = build_new_op (loc, COMPOUND_EXPR, LOOKUP_NORMAL, op1, op2,
NULL_TREE, lookups, &overload, complain);
if (!result)
result = cp_build_compound_expr (op1, op2, complain);
if (processing_template_decl && result != error_mark_node)
{
if (overload != NULL_TREE)
return (build_min_non_dep_op_overload
(COMPOUND_EXPR, result, overload, orig_op1, orig_op2));
return build_min_non_dep (COMPOUND_EXPR, result, orig_op1, orig_op2);
}
return result;
}
/* Like cp_build_compound_expr, but for the c-common bits. */
tree
build_compound_expr (location_t /*loc*/, tree lhs, tree rhs)
{
return cp_build_compound_expr (lhs, rhs, tf_warning_or_error);
}
/* Build a compound expression. */
tree
cp_build_compound_expr (tree lhs, tree rhs, tsubst_flags_t complain)
{
lhs = convert_to_void (lhs, ICV_LEFT_OF_COMMA, complain);
if (lhs == error_mark_node || rhs == error_mark_node)
return error_mark_node;
if (TREE_CODE (lhs) == EXCESS_PRECISION_EXPR)
lhs = TREE_OPERAND (lhs, 0);
tree eptype = NULL_TREE;
if (TREE_CODE (rhs) == EXCESS_PRECISION_EXPR)
{
eptype = TREE_TYPE (rhs);
rhs = TREE_OPERAND (rhs, 0);
}
if (TREE_CODE (rhs) == TARGET_EXPR)
{
/* If the rhs is a TARGET_EXPR, then build the compound
expression inside the target_expr's initializer. This
helps the compiler to eliminate unnecessary temporaries. */
tree init = TREE_OPERAND (rhs, 1);
init = build2 (COMPOUND_EXPR, TREE_TYPE (init), lhs, init);
TREE_OPERAND (rhs, 1) = init;
if (eptype)
rhs = build1 (EXCESS_PRECISION_EXPR, eptype, rhs);
return rhs;
}
if (type_unknown_p (rhs))
{
if (complain & tf_error)
error_at (cp_expr_loc_or_input_loc (rhs),
"no context to resolve type of %qE", rhs);
return error_mark_node;
}
tree ret = build2 (COMPOUND_EXPR, TREE_TYPE (rhs), lhs, rhs);
if (eptype)
ret = build1 (EXCESS_PRECISION_EXPR, eptype, ret);
return ret;
}
/* Issue a diagnostic message if casting from SRC_TYPE to DEST_TYPE
casts away constness. CAST gives the type of cast. Returns true
if the cast is ill-formed, false if it is well-formed.
??? This function warns for casting away any qualifier not just
const. We would like to specify exactly what qualifiers are casted
away.
*/
static bool
check_for_casting_away_constness (location_t loc, tree src_type,
tree dest_type, enum tree_code cast,
tsubst_flags_t complain)
{
/* C-style casts are allowed to cast away constness. With
WARN_CAST_QUAL, we still want to issue a warning. */
if (cast == CAST_EXPR && !warn_cast_qual)
return false;
if (!casts_away_constness (src_type, dest_type, complain))
return false;
switch (cast)
{
case CAST_EXPR:
if (complain & tf_warning)
warning_at (loc, OPT_Wcast_qual,
"cast from type %qT to type %qT casts away qualifiers",
src_type, dest_type);
return false;
case STATIC_CAST_EXPR:
if (complain & tf_error)
error_at (loc, "%<static_cast%> from type %qT to type %qT casts "
"away qualifiers",
src_type, dest_type);
return true;
case REINTERPRET_CAST_EXPR:
if (complain & tf_error)
error_at (loc, "%<reinterpret_cast%> from type %qT to type %qT "
"casts away qualifiers",
src_type, dest_type);
return true;
default:
gcc_unreachable();
}
}
/* Warns if the cast from expression EXPR to type TYPE is useless. */
void
maybe_warn_about_useless_cast (location_t loc, tree type, tree expr,
tsubst_flags_t complain)
{
if (warn_useless_cast
&& complain & tf_warning)
{
if (TYPE_REF_P (type)
? ((TYPE_REF_IS_RVALUE (type)
? xvalue_p (expr) : lvalue_p (expr))
&& same_type_p (TREE_TYPE (expr), TREE_TYPE (type)))
/* Don't warn when converting a class object to a non-reference type,
because that's a common way to create a temporary. */
: (!glvalue_p (expr) && same_type_p (TREE_TYPE (expr), type)))
warning_at (loc, OPT_Wuseless_cast,
"useless cast to type %q#T", type);
}
}
/* Warns if the cast ignores cv-qualifiers on TYPE. */
static void
maybe_warn_about_cast_ignoring_quals (location_t loc, tree type,
tsubst_flags_t complain)
{
if (warn_ignored_qualifiers
&& complain & tf_warning
&& !CLASS_TYPE_P (type)
&& (cp_type_quals (type) & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE)))
warning_at (loc, OPT_Wignored_qualifiers,
"type qualifiers ignored on cast result type");
}
/* Convert EXPR (an expression with pointer-to-member type) to TYPE
(another pointer-to-member type in the same hierarchy) and return
the converted expression. If ALLOW_INVERSE_P is permitted, a
pointer-to-derived may be converted to pointer-to-base; otherwise,
only the other direction is permitted. If C_CAST_P is true, this
conversion is taking place as part of a C-style cast. */
tree
convert_ptrmem (tree type, tree expr, bool allow_inverse_p,
bool c_cast_p, tsubst_flags_t complain)
{
if (same_type_p (type, TREE_TYPE (expr)))
return expr;
if (TYPE_PTRDATAMEM_P (type))
{
tree obase = TYPE_PTRMEM_CLASS_TYPE (TREE_TYPE (expr));
tree nbase = TYPE_PTRMEM_CLASS_TYPE (type);
tree delta = (get_delta_difference
(obase, nbase,
allow_inverse_p, c_cast_p, complain));
if (delta == error_mark_node)
return error_mark_node;
if (!same_type_p (obase, nbase))
{
if (TREE_CODE (expr) == PTRMEM_CST)
expr = cplus_expand_constant (expr);
tree cond = cp_build_binary_op (input_location, EQ_EXPR, expr,
build_int_cst (TREE_TYPE (expr), -1),
complain);
tree op1 = build_nop (ptrdiff_type_node, expr);
tree op2 = cp_build_binary_op (input_location, PLUS_EXPR, op1, delta,
complain);
expr = fold_build3_loc (input_location,
COND_EXPR, ptrdiff_type_node, cond, op1, op2);
}
return build_nop (type, expr);
}
else
return build_ptrmemfunc (TYPE_PTRMEMFUNC_FN_TYPE (type), expr,
allow_inverse_p, c_cast_p, complain);
}
/* Perform a static_cast from EXPR to TYPE. When C_CAST_P is true,
this static_cast is being attempted as one of the possible casts
allowed by a C-style cast. (In that case, accessibility of base
classes is not considered, and it is OK to cast away
constness.) Return the result of the cast. *VALID_P is set to
indicate whether or not the cast was valid. */
static tree
build_static_cast_1 (location_t loc, tree type, tree expr, bool c_cast_p,
bool *valid_p, tsubst_flags_t complain)
{
tree intype;
tree result;
cp_lvalue_kind clk;
/* Assume the cast is valid. */
*valid_p = true;
intype = unlowered_expr_type (expr);
/* Save casted types in the function's used types hash table. */
used_types_insert (type);
/* A prvalue of non-class type is cv-unqualified. */
if (!CLASS_TYPE_P (type))
type = cv_unqualified (type);
/* [expr.static.cast]
An lvalue of type "cv1 B", where B is a class type, can be cast
to type "reference to cv2 D", where D is a class derived (clause
_class.derived_) from B, if a valid standard conversion from
"pointer to D" to "pointer to B" exists (_conv.ptr_), cv2 is the
same cv-qualification as, or greater cv-qualification than, cv1,
and B is not a virtual base class of D. */
/* We check this case before checking the validity of "TYPE t =
EXPR;" below because for this case:
struct B {};
struct D : public B { D(const B&); };
extern B& b;
void f() { static_cast<const D&>(b); }
we want to avoid constructing a new D. The standard is not
completely clear about this issue, but our interpretation is
consistent with other compilers. */
if (TYPE_REF_P (type)
&& CLASS_TYPE_P (TREE_TYPE (type))
&& CLASS_TYPE_P (intype)
&& (TYPE_REF_IS_RVALUE (type) || lvalue_p (expr))
&& DERIVED_FROM_P (intype, TREE_TYPE (type))
&& can_convert (build_pointer_type (TYPE_MAIN_VARIANT (intype)),
build_pointer_type (TYPE_MAIN_VARIANT
(TREE_TYPE (type))),
complain)
&& (c_cast_p
|| at_least_as_qualified_p (TREE_TYPE (type), intype)))
{
tree base;
if (processing_template_decl)
return expr;
/* There is a standard conversion from "D*" to "B*" even if "B"
is ambiguous or inaccessible. If this is really a
static_cast, then we check both for inaccessibility and
ambiguity. However, if this is a static_cast being performed
because the user wrote a C-style cast, then accessibility is
not considered. */
base = lookup_base (TREE_TYPE (type), intype,
c_cast_p ? ba_unique : ba_check,
NULL, complain);
expr = cp_build_addr_expr (expr, complain);
if (sanitize_flags_p (SANITIZE_VPTR))
{
tree ubsan_check
= cp_ubsan_maybe_instrument_downcast (loc, type,
intype, expr);
if (ubsan_check)
expr = ubsan_check;
}
/* Convert from "B*" to "D*". This function will check that "B"
is not a virtual base of "D". Even if we don't have a guarantee
that expr is NULL, if the static_cast is to a reference type,
it is UB if it would be NULL, so omit the non-NULL check. */
expr = build_base_path (MINUS_EXPR, expr, base,
/*nonnull=*/flag_delete_null_pointer_checks,
complain);
/* Convert the pointer to a reference -- but then remember that
there are no expressions with reference type in C++.
We call rvalue so that there's an actual tree code
(NON_LVALUE_EXPR) for the static_cast; otherwise, if the operand
is a variable with the same type, the conversion would get folded
away, leaving just the variable and causing lvalue_kind to give
the wrong answer. */
expr = cp_fold_convert (type, expr);
/* When -fsanitize=null, make sure to diagnose reference binding to
NULL even when the reference is converted to pointer later on. */
if (sanitize_flags_p (SANITIZE_NULL)
&& TREE_CODE (expr) == COND_EXPR
&& TREE_OPERAND (expr, 2)
&& TREE_CODE (TREE_OPERAND (expr, 2)) == INTEGER_CST
&& TREE_TYPE (TREE_OPERAND (expr, 2)) == type)
ubsan_maybe_instrument_reference (&TREE_OPERAND (expr, 2));
return convert_from_reference (rvalue (expr));
}
/* "A glvalue of type cv1 T1 can be cast to type rvalue reference to
cv2 T2 if cv2 T2 is reference-compatible with cv1 T1 (8.5.3)." */
if (TYPE_REF_P (type)
&& TYPE_REF_IS_RVALUE (type)
&& (clk = real_lvalue_p (expr))
&& reference_compatible_p (TREE_TYPE (type), intype)
&& (c_cast_p || at_least_as_qualified_p (TREE_TYPE (type), intype)))
{
if (processing_template_decl)
return expr;
if (clk == clk_ordinary)
{
/* Handle the (non-bit-field) lvalue case here by casting to
lvalue reference and then changing it to an rvalue reference.
Casting an xvalue to rvalue reference will be handled by the
main code path. */
tree lref = cp_build_reference_type (TREE_TYPE (type), false);
result = (perform_direct_initialization_if_possible
(lref, expr, c_cast_p, complain));
result = build1 (NON_LVALUE_EXPR, type, result);
return convert_from_reference (result);
}
else
/* For a bit-field or packed field, bind to a temporary. */
expr = rvalue (expr);
}
/* Resolve overloaded address here rather than once in
implicit_conversion and again in the inverse code below. */
if (TYPE_PTRMEMFUNC_P (type) && type_unknown_p (expr))
{
expr = instantiate_type (type, expr, complain);
intype = TREE_TYPE (expr);
}
/* [expr.static.cast]
Any expression can be explicitly converted to type cv void. */
if (VOID_TYPE_P (type))
{
if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
expr = TREE_OPERAND (expr, 0);
return convert_to_void (expr, ICV_CAST, complain);
}
/* [class.abstract]
An abstract class shall not be used ... as the type of an explicit
conversion. */
if (abstract_virtuals_error (ACU_CAST, type, complain))
return error_mark_node;
/* [expr.static.cast]
An expression e can be explicitly converted to a type T using a
static_cast of the form static_cast<T>(e) if the declaration T
t(e);" is well-formed, for some invented temporary variable
t. */
result = perform_direct_initialization_if_possible (type, expr,
c_cast_p, complain);
/* P1975 allows static_cast<Aggr>(42), as well as static_cast<T[5]>(42),
which initialize the first element of the aggregate. We need to handle
the array case specifically. */
if (result == NULL_TREE
&& cxx_dialect >= cxx20
&& TREE_CODE (type) == ARRAY_TYPE)
{
/* Create { EXPR } and perform direct-initialization from it. */
tree e = build_constructor_single (init_list_type_node, NULL_TREE, expr);
CONSTRUCTOR_IS_DIRECT_INIT (e) = true;
CONSTRUCTOR_IS_PAREN_INIT (e) = true;
result = perform_direct_initialization_if_possible (type, e, c_cast_p,
complain);
}
if (result)
{
if (processing_template_decl)
return expr;
result = convert_from_reference (result);
/* [expr.static.cast]
If T is a reference type, the result is an lvalue; otherwise,
the result is an rvalue. */
if (!TYPE_REF_P (type))
{
result = rvalue (result);
if (result == expr && SCALAR_TYPE_P (type))
/* Leave some record of the cast. */
result = build_nop (type, expr);
}
return result;
}
/* [expr.static.cast]
The inverse of any standard conversion sequence (clause _conv_),
other than the lvalue-to-rvalue (_conv.lval_), array-to-pointer
(_conv.array_), function-to-pointer (_conv.func_), and boolean
(_conv.bool_) conversions, can be performed explicitly using
static_cast subject to the restriction that the explicit
conversion does not cast away constness (_expr.const.cast_), and
the following additional rules for specific cases: */
/* For reference, the conversions not excluded are: integral
promotions, floating-point promotion, integral conversions,
floating-point conversions, floating-integral conversions,
pointer conversions, and pointer to member conversions. */
/* DR 128
A value of integral _or enumeration_ type can be explicitly
converted to an enumeration type. */
/* The effect of all that is that any conversion between any two
types which are integral, floating, or enumeration types can be
performed. */
if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type)
|| SCALAR_FLOAT_TYPE_P (type))
&& (INTEGRAL_OR_ENUMERATION_TYPE_P (intype)
|| SCALAR_FLOAT_TYPE_P (intype)))
{
if (processing_template_decl)
return expr;
if (TREE_CODE (expr) == EXCESS_PRECISION_EXPR)
expr = TREE_OPERAND (expr, 0);
/* [expr.static.cast]: "If the value is not a bit-field, the result
refers to the object or the specified base class subobject thereof;
otherwise, the lvalue-to-rvalue conversion is applied to the
bit-field and the resulting prvalue is used as the operand of the
static_cast." There are no prvalue bit-fields; the l-to-r conversion
will give us an object of the underlying type of the bit-field. */
expr = decay_conversion (expr, complain);
return ocp_convert (type, expr, CONV_C_CAST, LOOKUP_NORMAL, complain);
}
if (TYPE_PTR_P (type) && TYPE_PTR_P (intype)
&& CLASS_TYPE_P (TREE_TYPE (type))
&& CLASS_TYPE_P (TREE_TYPE (intype))
&& can_convert (build_pointer_type (TYPE_MAIN_VARIANT
(TREE_TYPE (intype))),
build_pointer_type (TYPE_MAIN_VARIANT
(TREE_TYPE (type))),
complain))
{
tree base;
if (processing_template_decl)
return expr;
if (!c_cast_p
&& check_for_casting_away_constness (loc, intype, type,
STATIC_CAST_EXPR,
complain))
return error_mark_node;
base = lookup_base (TREE_TYPE (type), TREE_TYPE (intype),
c_cast_p ? ba_unique : ba_check,
NULL, complain);
expr = build_base_path (MINUS_EXPR, expr, base, /*nonnull=*/false,
complain);
if (sanitize_flags_p (SANITIZE_VPTR))
{
tree ubsan_check
= cp_ubsan_maybe_instrument_downcast (loc, type,
intype, expr);
if (ubsan_check)
expr = ubsan_check;
}
return cp_fold_convert (type, expr);
}
if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype))
|| (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype)))
{
tree c1;
tree c2;
tree t1;
tree t2;
c1 = TYPE_PTRMEM_CLASS_TYPE (intype);
c2 = TYPE_PTRMEM_CLASS_TYPE (type);
if (TYPE_PTRDATAMEM_P (type))
{
t1 = (build_ptrmem_type
(c1,
TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (intype))));
t2 = (build_ptrmem_type
(c2,
TYPE_MAIN_VARIANT (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
}
else
{
t1 = intype;
t2 = type;
}
if (can_convert (t1, t2, complain) || can_convert (t2, t1, complain))
{
if (!c_cast_p
&& check_for_casting_away_constness (loc, intype, type,
STATIC_CAST_EXPR,
complain))
return error_mark_node;
if (processing_template_decl)
return expr;
return convert_ptrmem (type, expr, /*allow_inverse_p=*/1,
c_cast_p, complain);
}
}
/* [expr.static.cast]
An rvalue of type "pointer to cv void" can be explicitly
converted to a pointer to object type. A value of type pointer
to object converted to "pointer to cv void" and back to the
original pointer type will have its original value. */
if (TYPE_PTR_P (intype)
&& VOID_TYPE_P (TREE_TYPE (intype))
&& TYPE_PTROB_P (type))
{
if (!c_cast_p
&& check_for_casting_away_constness (loc, intype, type,
STATIC_CAST_EXPR,
complain))
return error_mark_node;
if (processing_template_decl)
return expr;
return build_nop (type, expr);
}
*valid_p = false;
return error_mark_node;
}
/* Return an expression representing static_cast<TYPE>(EXPR). */
tree
build_static_cast (location_t loc, tree type, tree oexpr,
tsubst_flags_t complain)
{
tree expr = oexpr;
tree result;
bool valid_p;
if (type == error_mark_node || expr == error_mark_node)
return error_mark_node;
bool dependent = (dependent_type_p (type)
|| type_dependent_expression_p (expr));
if (dependent)
{
tmpl:
expr = build_min (STATIC_CAST_EXPR, type, oexpr);
/* We don't know if it will or will not have side effects. */
TREE_SIDE_EFFECTS (expr) = 1;
result = convert_from_reference (expr);
protected_set_expr_location (result, loc);
return result;
}
/* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
if (!TYPE_REF_P (type)
&& TREE_CODE (expr) == NOP_EXPR
&& TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
expr = TREE_OPERAND (expr, 0);
result = build_static_cast_1 (loc, type, expr, /*c_cast_p=*/false,
&valid_p, complain);
if (valid_p)
{
if (result != error_mark_node)
{
maybe_warn_about_useless_cast (loc, type, expr, complain);
maybe_warn_about_cast_ignoring_quals (loc, type, complain);
}
if (processing_template_decl)
goto tmpl;
protected_set_expr_location (result, loc);
return result;
}
if (complain & tf_error)
{
error_at (loc, "invalid %<static_cast%> from type %qT to type %qT",
TREE_TYPE (expr), type);
if ((TYPE_PTR_P (type) || TYPE_REF_P (type))
&& CLASS_TYPE_P (TREE_TYPE (type))
&& !COMPLETE_TYPE_P (TREE_TYPE (type)))
inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (TREE_TYPE (type))),
"class type %qT is incomplete", TREE_TYPE (type));
tree expr_type = TREE_TYPE (expr);
if (TYPE_PTR_P (expr_type))
expr_type = TREE_TYPE (expr_type);
if (CLASS_TYPE_P (expr_type) && !COMPLETE_TYPE_P (expr_type))
inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (expr_type)),
"class type %qT is incomplete", expr_type);
}
return error_mark_node;
}
/* EXPR is an expression with member function or pointer-to-member
function type. TYPE is a pointer type. Converting EXPR to TYPE is
not permitted by ISO C++, but we accept it in some modes. If we
are not in one of those modes, issue a diagnostic. Return the
converted expression. */
tree
convert_member_func_to_ptr (tree type, tree expr, tsubst_flags_t complain)
{
tree intype;
tree decl;
intype = TREE_TYPE (expr);
gcc_assert (TYPE_PTRMEMFUNC_P (intype)
|| TREE_CODE (intype) == METHOD_TYPE);
if (!(complain & tf_warning_or_error))
return error_mark_node;
location_t loc = cp_expr_loc_or_input_loc (expr);
if (pedantic || warn_pmf2ptr)
pedwarn (loc, pedantic ? OPT_Wpedantic : OPT_Wpmf_conversions,
"converting from %qH to %qI", intype, type);
STRIP_ANY_LOCATION_WRAPPER (expr);
if (TREE_CODE (intype) == METHOD_TYPE)
expr = build_addr_func (expr, complain);
else if (TREE_CODE (expr) == PTRMEM_CST)
expr = build_address (PTRMEM_CST_MEMBER (expr));
else
{
decl = maybe_dummy_object (TYPE_PTRMEM_CLASS_TYPE (intype), 0);
decl = build_address (decl);
expr = get_member_function_from_ptrfunc (&decl, expr, complain);
}
if (expr == error_mark_node)
return error_mark_node;
expr = build_nop (type, expr);
SET_EXPR_LOCATION (expr, loc);
return expr;
}
/* Build a NOP_EXPR to TYPE, but mark it as a reinterpret_cast so that
constexpr evaluation knows to reject it. */
static tree
build_nop_reinterpret (tree type, tree expr)
{
tree ret = build_nop (type, expr);
if (ret != expr)
REINTERPRET_CAST_P (ret) = true;
return ret;
}
/* Return a representation for a reinterpret_cast from EXPR to TYPE.
If C_CAST_P is true, this reinterpret cast is being done as part of
a C-style cast. If VALID_P is non-NULL, *VALID_P is set to
indicate whether or not reinterpret_cast was valid. */
static tree
build_reinterpret_cast_1 (location_t loc, tree type, tree expr,
bool c_cast_p, bool *valid_p,
tsubst_flags_t complain)
{
tree intype;
/* Assume the cast is invalid. */
if (valid_p)
*valid_p = true;
if (type == error_mark_node || error_operand_p (expr))
return error_mark_node;
intype = TREE_TYPE (expr);
/* Save casted types in the function's used types hash table. */
used_types_insert (type);
/* A prvalue of non-class type is cv-unqualified. */
if (!CLASS_TYPE_P (type))
type = cv_unqualified (type);
/* [expr.reinterpret.cast]
A glvalue of type T1, designating an object x, can be cast to the type
"reference to T2" if an expression of type "pointer to T1" can be
explicitly converted to the type "pointer to T2" using a reinterpret_cast.
The result is that of *reinterpret_cast<T2 *>(p) where p is a pointer to x
of type "pointer to T1". No temporary is created, no copy is made, and no
constructors (11.4.4) or conversion functions (11.4.7) are called. */
if (TYPE_REF_P (type))
{
if (!glvalue_p (expr))
{
if (complain & tf_error)
error_at (loc, "invalid cast of a prvalue expression of type "
"%qT to type %qT",
intype, type);
return error_mark_node;
}
/* Warn about a reinterpret_cast from "A*" to "B&" if "A" and
"B" are related class types; the reinterpret_cast does not
adjust the pointer. */
if (TYPE_PTR_P (intype)
&& (complain & tf_warning)
&& (comptypes (TREE_TYPE (intype), TREE_TYPE (type),
COMPARE_BASE | COMPARE_DERIVED)))
warning_at (loc, 0, "casting %qT to %qT does not dereference pointer",
intype, type);
expr = cp_build_addr_expr (expr, complain);
if (warn_strict_aliasing > 2)
cp_strict_aliasing_warning (EXPR_LOCATION (expr), type, expr);
if (expr != error_mark_node)
expr = build_reinterpret_cast_1
(loc, build_pointer_type (TREE_TYPE (type)), expr, c_cast_p,
valid_p, complain);
if (expr != error_mark_node)
/* cp_build_indirect_ref isn't right for rvalue refs. */
expr = convert_from_reference (fold_convert (type, expr));
return expr;
}
/* As a G++ extension, we consider conversions from member
functions, and pointers to member functions to
pointer-to-function and pointer-to-void types. If
-Wno-pmf-conversions has not been specified,
convert_member_func_to_ptr will issue an error message. */
if ((TYPE_PTRMEMFUNC_P (intype)
|| TREE_CODE (intype) == METHOD_TYPE)
&& TYPE_PTR_P (type)
&& (TREE_CODE (TREE_TYPE (type)) == FUNCTION_TYPE
|| VOID_TYPE_P (TREE_TYPE (type))))
return convert_member_func_to_ptr (type, expr, complain);
/* If the cast is not to a reference type, the lvalue-to-rvalue,
array-to-pointer, and function-to-pointer conversions are
performed. */
expr = decay_conversion (expr, complain);
/* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
if (TREE_CODE (expr) == NOP_EXPR
&& TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
expr = TREE_OPERAND (expr, 0);
if (error_operand_p (expr))
return error_mark_node;
intype = TREE_TYPE (expr);
/* [expr.reinterpret.cast]
A pointer can be converted to any integral type large enough to
hold it. ... A value of type std::nullptr_t can be converted to
an integral type; the conversion has the same meaning and
validity as a conversion of (void*)0 to the integral type. */
if (CP_INTEGRAL_TYPE_P (type)
&& (TYPE_PTR_P (intype) || NULLPTR_TYPE_P (intype)))
{
if (TYPE_PRECISION (type) < TYPE_PRECISION (intype))
{
if (complain & tf_error)
permerror (loc, "cast from %qH to %qI loses precision",
intype, type);
else
return error_mark_node;
}
if (NULLPTR_TYPE_P (intype))
return build_int_cst (type, 0);
}
/* [expr.reinterpret.cast]
A value of integral or enumeration type can be explicitly
converted to a pointer. */
else if (TYPE_PTR_P (type) && INTEGRAL_OR_ENUMERATION_TYPE_P (intype))
/* OK */
;
else if ((INTEGRAL_OR_ENUMERATION_TYPE_P (type)
|| TYPE_PTR_OR_PTRMEM_P (type))
&& same_type_p (type, intype))
/* DR 799 */
return rvalue (expr);
else if (TYPE_PTRFN_P (type) && TYPE_PTRFN_P (intype))
{
if ((complain & tf_warning)
&& !cxx_safe_function_type_cast_p (TREE_TYPE (type),
TREE_TYPE (intype)))
warning_at (loc, OPT_Wcast_function_type,
"cast between incompatible function types"
" from %qH to %qI", intype, type);
return build_nop_reinterpret (type, expr);
}
else if (TYPE_PTRMEMFUNC_P (type) && TYPE_PTRMEMFUNC_P (intype))
{
if ((complain & tf_warning)
&& !cxx_safe_function_type_cast_p
(TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE_RAW (type)),
TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE_RAW (intype))))
warning_at (loc, OPT_Wcast_function_type,
"cast between incompatible pointer to member types"
" from %qH to %qI", intype, type);
return build_nop_reinterpret (type, expr);
}
else if ((TYPE_PTRDATAMEM_P (type) && TYPE_PTRDATAMEM_P (intype))
|| (TYPE_PTROBV_P (type) && TYPE_PTROBV_P (intype)))
{
if (!c_cast_p
&& check_for_casting_away_constness (loc, intype, type,
REINTERPRET_CAST_EXPR,
complain))
return error_mark_node;
/* Warn about possible alignment problems. */
if ((STRICT_ALIGNMENT || warn_cast_align == 2)
&& (complain & tf_warning)
&& !VOID_TYPE_P (type)
&& TREE_CODE (TREE_TYPE (intype)) != FUNCTION_TYPE
&& COMPLETE_TYPE_P (TREE_TYPE (type))
&& COMPLETE_TYPE_P (TREE_TYPE (intype))
&& min_align_of_type (TREE_TYPE (type))
> min_align_of_type (TREE_TYPE (intype)))
warning_at (loc, OPT_Wcast_align, "cast from %qH to %qI "
"increases required alignment of target type",
intype, type);
if (warn_strict_aliasing <= 2)
/* strict_aliasing_warning STRIP_NOPs its expr. */
cp_strict_aliasing_warning (EXPR_LOCATION (expr), type, expr);
return build_nop_reinterpret (type, expr);
}
else if ((TYPE_PTRFN_P (type) && TYPE_PTROBV_P (intype))
|| (TYPE_PTRFN_P (intype) && TYPE_PTROBV_P (type)))
{
if (complain & tf_warning)
/* C++11 5.2.10 p8 says that "Converting a function pointer to an
object pointer type or vice versa is conditionally-supported." */
warning_at (loc, OPT_Wconditionally_supported,
"casting between pointer-to-function and "
"pointer-to-object is conditionally-supported");
return build_nop_reinterpret (type, expr);
}
else if (gnu_vector_type_p (type) && scalarish_type_p (intype))
return convert_to_vector (type, rvalue (expr));
else if (gnu_vector_type_p (intype)
&& INTEGRAL_OR_ENUMERATION_TYPE_P (type))
return convert_to_integer_nofold (type, expr);
else
{
if (valid_p)
*valid_p = false;
if (complain & tf_error)
error_at (loc, "invalid cast from type %qT to type %qT",
intype, type);
return error_mark_node;
}
expr = cp_convert (type, expr, complain);
if (TREE_CODE (expr) == NOP_EXPR)
/* Mark any nop_expr that created as a reintepret_cast. */
REINTERPRET_CAST_P (expr) = true;
return expr;
}
tree
build_reinterpret_cast (location_t loc, tree type, tree expr,
tsubst_flags_t complain)
{
tree r;
if (type == error_mark_node || expr == error_mark_node)
return error_mark_node;
if (processing_template_decl)
{
tree t = build_min (REINTERPRET_CAST_EXPR, type, expr);
if (!TREE_SIDE_EFFECTS (t)
&& type_dependent_expression_p (expr))
/* There might turn out to be side effects inside expr. */
TREE_SIDE_EFFECTS (t) = 1;
r = convert_from_reference (t);
protected_set_expr_location (r, loc);
return r;
}
r = build_reinterpret_cast_1 (loc, type, expr, /*c_cast_p=*/false,
/*valid_p=*/NULL, complain);
if (r != error_mark_node)
{
maybe_warn_about_useless_cast (loc, type, expr, complain);
maybe_warn_about_cast_ignoring_quals (loc, type, complain);
}
protected_set_expr_location (r, loc);
return r;
}
/* Perform a const_cast from EXPR to TYPE. If the cast is valid,
return an appropriate expression. Otherwise, return
error_mark_node. If the cast is not valid, and COMPLAIN is true,
then a diagnostic will be issued. If VALID_P is non-NULL, we are
performing a C-style cast, its value upon return will indicate
whether or not the conversion succeeded. */
static tree
build_const_cast_1 (location_t loc, tree dst_type, tree expr,
tsubst_flags_t complain, bool *valid_p)
{
tree src_type;
tree reference_type;
/* Callers are responsible for handling error_mark_node as a
destination type. */
gcc_assert (dst_type != error_mark_node);
/* In a template, callers should be building syntactic
representations of casts, not using this machinery. */
gcc_assert (!processing_template_decl);
/* Assume the conversion is invalid. */
if (valid_p)
*valid_p = false;
if (!INDIRECT_TYPE_P (dst_type) && !TYPE_PTRDATAMEM_P (dst_type))
{
if (complain & tf_error)
error_at (loc, "invalid use of %<const_cast%> with type %qT, "
"which is not a pointer, reference, "
"nor a pointer-to-data-member type", dst_type);
return error_mark_node;
}
if (TREE_CODE (TREE_TYPE (dst_type)) == FUNCTION_TYPE)
{
if (complain & tf_error)
error_at (loc, "invalid use of %<const_cast%> with type %qT, "
"which is a pointer or reference to a function type",
dst_type);
return error_mark_node;
}
/* A prvalue of non-class type is cv-unqualified. */
dst_type = cv_unqualified (dst_type);
/* Save casted types in the function's used types hash table. */
used_types_insert (dst_type);
src_type = TREE_TYPE (expr);
/* Expressions do not really have reference types. */
if (TYPE_REF_P (src_type))
src_type = TREE_TYPE (src_type);
/* [expr.const.cast]
For two object types T1 and T2, if a pointer to T1 can be explicitly
converted to the type "pointer to T2" using a const_cast, then the
following conversions can also be made:
-- an lvalue of type T1 can be explicitly converted to an lvalue of
type T2 using the cast const_cast<T2&>;
-- a glvalue of type T1 can be explicitly converted to an xvalue of
type T2 using the cast const_cast<T2&&>; and
-- if T1 is a class type, a prvalue of type T1 can be explicitly
converted to an xvalue of type T2 using the cast const_cast<T2&&>. */
if (TYPE_REF_P (dst_type))
{
reference_type = dst_type;
if (!TYPE_REF_IS_RVALUE (dst_type)
? lvalue_p (expr)
: obvalue_p (expr))
/* OK. */;
else
{
if (complain & tf_error)
error_at (loc, "invalid %<const_cast%> of an rvalue of type %qT "
"to type %qT",
src_type, dst_type);
return error_mark_node;
}
dst_type = build_pointer_type (TREE_TYPE (dst_type));
src_type = build_pointer_type (src_type);
}
else
{
reference_type = NULL_TREE;
/* If the destination type is not a reference type, the
lvalue-to-rvalue, array-to-pointer, and function-to-pointer
conversions are performed. */
src_type = type_decays_to (src_type);
if (src_type == error_mark_node)
return error_mark_node;
}
if (TYPE_PTR_P (src_type) || TYPE_PTRDATAMEM_P (src_type))
{
if (comp_ptr_ttypes_const (dst_type, src_type, bounds_none))
{
if (valid_p)
{
*valid_p = true;
/* This cast is actually a C-style cast. Issue a warning if
the user is making a potentially unsafe cast. */
check_for_casting_away_constness (loc, src_type, dst_type,
CAST_EXPR, complain);
/* ??? comp_ptr_ttypes_const ignores TYPE_ALIGN. */
if ((STRICT_ALIGNMENT || warn_cast_align == 2)
&& (complain & tf_warning)
&& min_align_of_type (TREE_TYPE (dst_type))
> min_align_of_type (TREE_TYPE (src_type)))
warning_at (loc, OPT_Wcast_align, "cast from %qH to %qI "
"increases required alignment of target type",
src_type, dst_type);
}
if (reference_type)
{
expr = cp_build_addr_expr (expr, complain);
if (expr == error_mark_node)
return error_mark_node;
expr = build_nop (reference_type, expr);
return convert_from_reference (expr);
}
else
{
expr = decay_conversion (expr, complain);
if (expr == error_mark_node)
return error_mark_node;
/* build_c_cast puts on a NOP_EXPR to make the result not an
lvalue. Strip such NOP_EXPRs if VALUE is being used in
non-lvalue context. */
if (TREE_CODE (expr) == NOP_EXPR
&& TREE_TYPE (expr) == TREE_TYPE (TREE_OPERAND (expr, 0)))
expr = TREE_OPERAND (expr, 0);
return build_nop (dst_type, expr);
}
}
else if (valid_p
&& !at_least_as_qualified_p (TREE_TYPE (dst_type),
TREE_TYPE (src_type)))
check_for_casting_away_constness (loc, src_type, dst_type,
CAST_EXPR, complain);
}
if (complain & tf_error)
error_at (loc, "invalid %<const_cast%> from type %qT to type %qT",
src_type, dst_type);
return error_mark_node;
}
tree
build_const_cast (location_t loc, tree type, tree expr,
tsubst_flags_t complain)
{
tree r;
if (type == error_mark_node || error_operand_p (expr))
return error_mark_node;
if (processing_template_decl)
{
tree t = build_min (CONST_CAST_EXPR, type, expr);
if (!TREE_SIDE_EFFECTS (t)
&& type_dependent_expression_p (expr))
/* There might turn out to be side effects inside expr. */
TREE_SIDE_EFFECTS (t) = 1;
r = convert_from_reference (t);
protected_set_expr_location (r, loc);
return r;
}
r = build_const_cast_1 (loc, type, expr, complain, /*valid_p=*/NULL);
if (r != error_mark_node)
{
maybe_warn_about_useless_cast (loc, type, expr, complain);
maybe_warn_about_cast_ignoring_quals (loc, type, complain);
}
protected_set_expr_location (r, loc);
return r;
}
/* Like cp_build_c_cast, but for the c-common bits. */
tree
build_c_cast (location_t loc, tree type, tree expr)
{
return cp_build_c_cast (loc, type, expr, tf_warning_or_error);
}
/* Like the "build_c_cast" used for c-common, but using cp_expr to
preserve location information even for tree nodes that don't
support it. */
cp_expr
build_c_cast (location_t loc, tree type, cp_expr expr)
{
cp_expr result = cp_build_c_cast (loc, type, expr, tf_warning_or_error);
result.set_location (loc);
return result;
}
/* Build an expression representing an explicit C-style cast to type
TYPE of expression EXPR. */
tree
cp_build_c_cast (location_t loc, tree type, tree expr,
tsubst_flags_t complain)
{
tree value = expr;
tree result;
bool valid_p;
if (type == error_mark_node || error_operand_p (expr))
return error_mark_node;
if (processing_template_decl)
{
tree t = build_min (CAST_EXPR, type,
tree_cons (NULL_TREE, value, NULL_TREE));
/* We don't know if it will or will not have side effects. */
TREE_SIDE_EFFECTS (t) = 1;
return convert_from_reference (t);
}
/* Casts to a (pointer to a) specific ObjC class (or 'id' or
'Class') should always be retained, because this information aids
in method lookup. */
if (objc_is_object_ptr (type)
&& objc_is_object_ptr (TREE_TYPE (expr)))
return build_nop (type, expr);
/* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
Strip such NOP_EXPRs if VALUE is being used in non-lvalue context. */
if (!TYPE_REF_P (type)
&& TREE_CODE (value) == NOP_EXPR
&& TREE_TYPE (value) == TREE_TYPE (TREE_OPERAND (value, 0)))
value = TREE_OPERAND (value, 0);
if (TREE_CODE (type) == ARRAY_TYPE)
{
/* Allow casting from T1* to T2[] because Cfront allows it.
NIHCL uses it. It is not valid ISO C++ however. */
if (TYPE_PTR_P (TREE_TYPE (expr)))
{
if (complain & tf_error)
permerror (loc, "ISO C++ forbids casting to an array type %qT",
type);
else
return error_mark_node;
type = build_pointer_type (TREE_TYPE (type));
}
else
{
if (complain & tf_error)
error_at (loc, "ISO C++ forbids casting to an array type %qT",
type);
return error_mark_node;
}
}
if (FUNC_OR_METHOD_TYPE_P (type))
{
if (complain & tf_error)
error_at (loc, "invalid cast to function type %qT", type);
return error_mark_node;
}
if (TYPE_PTR_P (type)
&& TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE
/* Casting to an integer of smaller size is an error detected elsewhere. */
&& TYPE_PRECISION (type) > TYPE_PRECISION (TREE_TYPE (value))
/* Don't warn about converting any constant. */
&& !TREE_CONSTANT (value))
warning_at (loc, OPT_Wint_to_pointer_cast,
"cast to pointer from integer of different size");
/* A C-style cast can be a const_cast. */
result = build_const_cast_1 (loc, type, value, complain & tf_warning,
&valid_p);
if (valid_p)
{
if (result != error_mark_node)
{
maybe_warn_about_useless_cast (loc, type, value, complain);
maybe_warn_about_cast_ignoring_quals (loc, type, complain);
}
else if (complain & tf_error)
build_const_cast_1 (loc, type, value, tf_error, &valid_p);
return result;
}
/* Or a static cast. */
result = build_static_cast_1 (loc, type, value, /*c_cast_p=*/true,
&valid_p, complain);
/* Or a reinterpret_cast. */
if (!valid_p)
result = build_reinterpret_cast_1 (loc, type, value, /*c_cast_p=*/true,
&valid_p, complain);
/* The static_cast or reinterpret_cast may be followed by a
const_cast. */
if (valid_p
/* A valid cast may result in errors if, for example, a
conversion to an ambiguous base class is required. */
&& !error_operand_p (result))
{
tree result_type;
maybe_warn_about_useless_cast (loc, type, value, complain);
maybe_warn_about_cast_ignoring_quals (loc, type, complain);
/* Non-class rvalues always have cv-unqualified type. */
if (!CLASS_TYPE_P (type))
type = TYPE_MAIN_VARIANT (type);
result_type = TREE_TYPE (result);
if (!CLASS_TYPE_P (result_type) && !TYPE_REF_P (type))
result_type = TYPE_MAIN_VARIANT (result_type);
/* If the type of RESULT does not match TYPE, perform a
const_cast to make it match. If the static_cast or
reinterpret_cast succeeded, we will differ by at most
cv-qualification, so the follow-on const_cast is guaranteed
to succeed. */
if (!same_type_p (non_reference (type), non_reference (result_type)))
{
result = build_const_cast_1 (loc, type, result, tf_none, &valid_p);
gcc_assert (valid_p);
}
return result;
}
return error_mark_node;
}
/* Warn when a value is moved to itself with std::move. LHS is the target,
RHS may be the std::move call, and LOC is the location of the whole
assignment. */
static void
maybe_warn_self_move (location_t loc, tree lhs, tree rhs)
{
if (!warn_self_move)
return;
/* C++98 doesn't know move. */
if (cxx_dialect < cxx11)
return;
if (processing_template_decl)
return;
if (!REFERENCE_REF_P (rhs)
|| TREE_CODE (TREE_OPERAND (rhs, 0)) != CALL_EXPR)
return;
tree fn = TREE_OPERAND (rhs, 0);
if (!is_std_move_p (fn))
return;
/* Just a little helper to strip * and various NOPs. */
auto extract_op = [] (tree &op) {
STRIP_NOPS (op);
while (INDIRECT_REF_P (op))
op = TREE_OPERAND (op, 0);
op = maybe_undo_parenthesized_ref (op);
STRIP_ANY_LOCATION_WRAPPER (op);
};
tree arg = CALL_EXPR_ARG (fn, 0);
extract_op (arg);
if (TREE_CODE (arg) == ADDR_EXPR)
arg = TREE_OPERAND (arg, 0);
tree type = TREE_TYPE (lhs);
tree orig_lhs = lhs;
extract_op (lhs);
if (cp_tree_equal (lhs, arg))
{
auto_diagnostic_group d;
if (warning_at (loc, OPT_Wself_move,
"moving %qE of type %qT to itself", orig_lhs, type))
inform (loc, "remove %<std::move%> call");
}
}
/* For use from the C common bits. */
tree
build_modify_expr (location_t location,
tree lhs, tree /*lhs_origtype*/,
enum tree_code modifycode,
location_t /*rhs_location*/, tree rhs,
tree /*rhs_origtype*/)
{
return cp_build_modify_expr (location, lhs, modifycode, rhs,
tf_warning_or_error);
}
/* Build an assignment expression of lvalue LHS from value RHS.
MODIFYCODE is the code for a binary operator that we use
to combine the old value of LHS with RHS to get the new value.
Or else MODIFYCODE is NOP_EXPR meaning do a simple assignment.
C++: If MODIFYCODE is INIT_EXPR, then leave references unbashed. */
tree
cp_build_modify_expr (location_t loc, tree lhs, enum tree_code modifycode,
tree rhs, tsubst_flags_t complain)
{
lhs = mark_lvalue_use_nonread (lhs);
tree result = NULL_TREE;
tree newrhs = rhs;
tree lhstype = TREE_TYPE (lhs);
tree olhs = lhs;
tree olhstype = lhstype;
bool plain_assign = (modifycode == NOP_EXPR);
bool compound_side_effects_p = false;
tree preeval = NULL_TREE;
/* Avoid duplicate error messages from operands that had errors. */
if (error_operand_p (lhs) || error_operand_p (rhs))
return error_mark_node;
while (TREE_CODE (lhs) == COMPOUND_EXPR)
{
if (TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0)))
compound_side_effects_p = true;
lhs = TREE_OPERAND (lhs, 1);
}
/* Handle control structure constructs used as "lvalues". Note that we
leave COMPOUND_EXPR on the LHS because it is sequenced after the RHS. */
switch (TREE_CODE (lhs))
{
/* Handle --foo = 5; as these are valid constructs in C++. */
case PREDECREMENT_EXPR:
case PREINCREMENT_EXPR:
if (compound_side_effects_p)
newrhs = rhs = stabilize_expr (rhs, &preeval);
lhs = genericize_compound_lvalue (lhs);
maybe_add_compound:
/* If we had (bar, --foo) = 5; or (bar, (baz, --foo)) = 5;
and looked through the COMPOUND_EXPRs, readd them now around
the resulting lhs. */
if (TREE_CODE (olhs) == COMPOUND_EXPR)
{
lhs = build2 (COMPOUND_EXPR, lhstype, TREE_OPERAND (olhs, 0), lhs);
tree *ptr = &TREE_OPERAND (lhs, 1);
for (olhs = TREE_OPERAND (olhs, 1);
TREE_CODE (olhs) == COMPOUND_EXPR;
olhs = TREE_OPERAND (olhs, 1))
{
*ptr = build2 (COMPOUND_EXPR, lhstype,
TREE_OPERAND (olhs, 0), *ptr);
ptr = &TREE_OPERAND (*ptr, 1);
}
}
break;
case MODIFY_EXPR:
if (compound_side_effects_p)
newrhs = rhs = stabilize_expr (rhs, &preeval);
lhs = genericize_compound_lvalue (lhs);
goto maybe_add_compound;
case MIN_EXPR:
case MAX_EXPR:
/* MIN_EXPR and MAX_EXPR are currently only permitted as lvalues,
when neither operand has side-effects. */
if (!lvalue_or_else (lhs, lv_assign, complain))
return error_mark_node;
gcc_assert (!TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 0))
&& !TREE_SIDE_EFFECTS (TREE_OPERAND (lhs, 1)));
lhs = build3 (COND_EXPR, TREE_TYPE (lhs),
build2 (TREE_CODE (lhs) == MIN_EXPR ? LE_EXPR : GE_EXPR,
boolean_type_node,
TREE_OPERAND (lhs, 0),
TREE_OPERAND (lhs, 1)),
TREE_OPERAND (lhs, 0),
TREE_OPERAND (lhs, 1));
gcc_fallthrough ();
/* Handle (a ? b : c) used as an "lvalue". */
case COND_EXPR:
{
/* Produce (a ? (b = rhs) : (c = rhs))
except that the RHS goes through a save-expr
so the code to compute it is only emitted once. */
if (VOID_TYPE_P (TREE_TYPE (rhs)))
{
if (complain & tf_error)
error_at (cp_expr_loc_or_loc (rhs, loc),
"void value not ignored as it ought to be");
return error_mark_node;
}
rhs = stabilize_expr (rhs, &preeval);
/* Check this here to avoid odd errors when trying to convert
a throw to the type of the COND_EXPR. */
if (!lvalue_or_else (lhs, lv_assign, complain))
return error_mark_node;
tree op1 = TREE_OPERAND (lhs, 1);
if (TREE_CODE (op1) != THROW_EXPR)
op1 = cp_build_modify_expr (loc, op1, modifycode, rhs, complain);
/* When sanitizing undefined behavior, even when rhs doesn't need
stabilization at this point, the sanitization might add extra
SAVE_EXPRs in there and so make sure there is no tree sharing
in the rhs, otherwise those SAVE_EXPRs will have initialization
only in one of the two branches. */
if (sanitize_flags_p (SANITIZE_UNDEFINED
| SANITIZE_UNDEFINED_NONDEFAULT))
rhs = unshare_expr (rhs);
tree op2 = TREE_OPERAND (lhs, 2);
if (TREE_CODE (op2) != THROW_EXPR)
op2 = cp_build_modify_expr (loc, op2, modifycode, rhs, complain);
tree cond = build_conditional_expr (input_location,
TREE_OPERAND (lhs, 0), op1, op2,
complain);
if (cond == error_mark_node)
return cond;
/* If we had (e, (a ? b : c)) = d; or (e, (f, (a ? b : c))) = d;
and looked through the COMPOUND_EXPRs, readd them now around
the resulting cond before adding the preevaluated rhs. */
if (TREE_CODE (olhs) == COMPOUND_EXPR)
{
cond = build2 (COMPOUND_EXPR, TREE_TYPE (cond),
TREE_OPERAND (olhs, 0), cond);
tree *ptr = &TREE_OPERAND (cond, 1);
for (olhs = TREE_OPERAND (olhs, 1);
TREE_CODE (olhs) == COMPOUND_EXPR;
olhs = TREE_OPERAND (olhs, 1))
{
*ptr = build2 (COMPOUND_EXPR, TREE_TYPE (cond),
TREE_OPERAND (olhs, 0), *ptr);
ptr = &TREE_OPERAND (*ptr, 1);
}
}
/* Make sure the code to compute the rhs comes out
before the split. */
result = cond;
goto ret;
}
default:
lhs = olhs;
break;
}
if (modifycode == INIT_EXPR)
{
if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
/* Do the default thing. */;
else if (TREE_CODE (rhs) == CONSTRUCTOR)
{
/* Compound literal. */
if (! same_type_p (TREE_TYPE (rhs), lhstype))
/* Call convert to generate an error; see PR 11063. */
rhs = convert (lhstype, rhs);
result = cp_build_init_expr (lhs, rhs);
TREE_SIDE_EFFECTS (result) = 1;
goto ret;
}
else if (! MAYBE_CLASS_TYPE_P (lhstype))
/* Do the default thing. */;
else
{
releasing_vec rhs_vec = make_tree_vector_single (rhs);
result = build_special_member_call (lhs, complete_ctor_identifier,
&rhs_vec, lhstype, LOOKUP_NORMAL,
complain);
if (result == NULL_TREE)
return error_mark_node;
goto ret;
}
}
else
{
lhs = require_complete_type (lhs, complain);
if (lhs == error_mark_node)
return error_mark_node;
if (modifycode == NOP_EXPR)
{
maybe_warn_self_move (loc, lhs, rhs);
if (c_dialect_objc ())
{
result = objc_maybe_build_modify_expr (lhs, rhs);
if (result)
goto ret;
}
/* `operator=' is not an inheritable operator. */
if (! MAYBE_CLASS_TYPE_P (lhstype))
/* Do the default thing. */;
else
{
result = build_new_op (input_location, MODIFY_EXPR,
LOOKUP_NORMAL, lhs, rhs,
make_node (NOP_EXPR), NULL_TREE,
/*overload=*/NULL, complain);
if (result == NULL_TREE)
return error_mark_node;
goto ret;
}
lhstype = olhstype;
}
else
{
tree init = NULL_TREE;
/* A binary op has been requested. Combine the old LHS
value with the RHS producing the value we should actually
store into the LHS. */
gcc_assert (!((TYPE_REF_P (lhstype)
&& MAYBE_CLASS_TYPE_P (TREE_TYPE (lhstype)))
|| MAYBE_CLASS_TYPE_P (lhstype)));
/* Preevaluate the RHS to make sure its evaluation is complete
before the lvalue-to-rvalue conversion of the LHS:
[expr.ass] With respect to an indeterminately-sequenced
function call, the operation of a compound assignment is a
single evaluation. [ Note: Therefore, a function call shall
not intervene between the lvalue-to-rvalue conversion and the
side effect associated with any single compound assignment
operator. -- end note ] */
lhs = cp_stabilize_reference (lhs);
rhs = decay_conversion (rhs, complain);
if (rhs == error_mark_node)
return error_mark_node;
rhs = stabilize_expr (rhs, &init);
newrhs = cp_build_binary_op (loc, modifycode, lhs, rhs, complain);
if (newrhs == error_mark_node)
{
if (complain & tf_error)
inform (loc, " in evaluation of %<%Q(%#T, %#T)%>",
modifycode, TREE_TYPE (lhs), TREE_TYPE (rhs));
return error_mark_node;
}
if (init)
newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), init, newrhs);
/* Now it looks like a plain assignment. */
modifycode = NOP_EXPR;
if (c_dialect_objc ())
{
result = objc_maybe_build_modify_expr (lhs, newrhs);
if (result)
goto ret;
}
}
gcc_assert (!TYPE_REF_P (lhstype));
gcc_assert (!TYPE_REF_P (TREE_TYPE (newrhs)));
}
/* The left-hand side must be an lvalue. */
if (!lvalue_or_else (lhs, lv_assign, complain))
return error_mark_node;
/* Warn about modifying something that is `const'. Don't warn if
this is initialization. */
if (modifycode != INIT_EXPR
&& (TREE_READONLY (lhs) || CP_TYPE_CONST_P (lhstype)
/* Functions are not modifiable, even though they are
lvalues. */
|| FUNC_OR_METHOD_TYPE_P (TREE_TYPE (lhs))
/* If it's an aggregate and any field is const, then it is
effectively const. */
|| (CLASS_TYPE_P (lhstype)
&& C_TYPE_FIELDS_READONLY (lhstype))))
{
if (complain & tf_error)
cxx_readonly_error (loc, lhs, lv_assign);
return error_mark_node;
}
/* If storing into a structure or union member, it may have been given a
lowered bitfield type. We need to convert to the declared type first,
so retrieve it now. */
olhstype = unlowered_expr_type (lhs);
/* Convert new value to destination type. */
if (TREE_CODE (lhstype) == ARRAY_TYPE)
{
int from_array;
if (BRACE_ENCLOSED_INITIALIZER_P (newrhs))
{
if (modifycode != INIT_EXPR)
{
if (complain & tf_error)
error_at (loc,
"assigning to an array from an initializer list");
return error_mark_node;
}
if (check_array_initializer (lhs, lhstype, newrhs))
return error_mark_node;
newrhs = digest_init (lhstype, newrhs, complain);
if (newrhs == error_mark_node)
return error_mark_node;
}
/* C++11 8.5/17: "If the destination type is an array of characters,
an array of char16_t, an array of char32_t, or an array of wchar_t,
and the initializer is a string literal...". */
else if ((TREE_CODE (tree_strip_any_location_wrapper (newrhs))
== STRING_CST)
&& char_type_p (TREE_TYPE (TYPE_MAIN_VARIANT (lhstype)))
&& modifycode == INIT_EXPR)
{
newrhs = digest_init (lhstype, newrhs, complain);
if (newrhs == error_mark_node)
return error_mark_node;
}
else if (!same_or_base_type_p (TYPE_MAIN_VARIANT (lhstype),
TYPE_MAIN_VARIANT (TREE_TYPE (newrhs))))
{
if (complain & tf_error)
error_at (loc, "incompatible types in assignment of %qT to %qT",
TREE_TYPE (rhs), lhstype);
return error_mark_node;
}
/* Allow array assignment in compiler-generated code. */
else if (DECL_P (lhs) && DECL_ARTIFICIAL (lhs))
/* OK, used by coroutines (co-await-initlist1.C). */;
else if (!current_function_decl
|| !DECL_DEFAULTED_FN (current_function_decl))
{
/* This routine is used for both initialization and assignment.
Make sure the diagnostic message differentiates the context. */
if (complain & tf_error)
{
if (modifycode == INIT_EXPR)
error_at (loc, "array used as initializer");
else
error_at (loc, "invalid array assignment");
}
return error_mark_node;
}
from_array = TREE_CODE (TREE_TYPE (newrhs)) == ARRAY_TYPE
? 1 + (modifycode != INIT_EXPR): 0;
result = build_vec_init (lhs, NULL_TREE, newrhs,
/*explicit_value_init_p=*/false,
from_array, complain);
goto ret;
}
if (modifycode == INIT_EXPR)
/* Calls with INIT_EXPR are all direct-initialization, so don't set
LOOKUP_ONLYCONVERTING. */
newrhs = convert_for_initialization (lhs, olhstype, newrhs, LOOKUP_NORMAL,
ICR_INIT, NULL_TREE, 0,
complain | tf_no_cleanup);
else
newrhs = convert_for_assignment (olhstype, newrhs, ICR_ASSIGN,
NULL_TREE, 0, complain, LOOKUP_IMPLICIT);
if (!same_type_p (lhstype, olhstype))
newrhs = cp_convert_and_check (lhstype, newrhs, complain);
if (modifycode != INIT_EXPR)
{
if (TREE_CODE (newrhs) == CALL_EXPR
&& TYPE_NEEDS_CONSTRUCTING (lhstype))
newrhs = build_cplus_new (lhstype, newrhs, complain);
/* Can't initialize directly from a TARGET_EXPR, since that would
cause the lhs to be constructed twice, and possibly result in
accidental self-initialization. So we force the TARGET_EXPR to be
expanded without a target. */
if (TREE_CODE (newrhs) == TARGET_EXPR)
newrhs = build2 (COMPOUND_EXPR, TREE_TYPE (newrhs), newrhs,
TREE_OPERAND (newrhs, 0));
}
if (newrhs == error_mark_node)
return error_mark_node;
if (c_dialect_objc () && flag_objc_gc)
{
result = objc_generate_write_barrier (lhs, modifycode, newrhs);
if (result)
goto ret;
}
result = build2_loc (loc, modifycode == NOP_EXPR ? MODIFY_EXPR : INIT_EXPR,
lhstype, lhs, newrhs);
if (modifycode == INIT_EXPR)
set_target_expr_eliding (newrhs);
TREE_SIDE_EFFECTS (result) = 1;
if (!plain_assign)
suppress_warning (result, OPT_Wparentheses);
ret:
if (preeval)
result = build2 (COMPOUND_EXPR, TREE_TYPE (result), preeval, result);
return result;
}
cp_expr
build_x_modify_expr (location_t loc, tree lhs, enum tree_code modifycode,
tree rhs, tree lookups, tsubst_flags_t complain)
{
tree orig_lhs = lhs;
tree orig_rhs = rhs;
tree overload = NULL_TREE;
if (lhs == error_mark_node || rhs == error_mark_node)
return cp_expr (error_mark_node, loc);
tree op = build_min (modifycode, void_type_node, NULL_TREE, NULL_TREE);
if (processing_template_decl)
{
if (type_dependent_expression_p (lhs)
|| type_dependent_expression_p (rhs))
{
tree rval = build_min_nt_loc (loc, MODOP_EXPR, lhs, op, rhs);
if (modifycode != NOP_EXPR)
TREE_TYPE (rval)
= build_dependent_operator_type (lookups, modifycode, true);
return rval;
}
}
tree rval;
if (modifycode == NOP_EXPR)
rval = cp_build_modify_expr (loc, lhs, modifycode, rhs, complain);
else
rval = build_new_op (loc, MODIFY_EXPR, LOOKUP_NORMAL,
lhs, rhs, op, lookups, &overload, complain);
if (rval == error_mark_node)
return error_mark_node;
if (processing_template_decl)
{
if (overload != NULL_TREE)
return (build_min_non_dep_op_overload
(MODIFY_EXPR, rval, overload, orig_lhs, orig_rhs));
return (build_min_non_dep
(MODOP_EXPR, rval, orig_lhs, op, orig_rhs));
}
return rval;
}
/* Helper function for get_delta_difference which assumes FROM is a base
class of TO. Returns a delta for the conversion of pointer-to-member
of FROM to pointer-to-member of TO. If the conversion is invalid and
tf_error is not set in COMPLAIN returns error_mark_node, otherwise
returns zero. If FROM is not a base class of TO, returns NULL_TREE.
If C_CAST_P is true, this conversion is taking place as part of a
C-style cast. */
static tree
get_delta_difference_1 (tree from, tree to, bool c_cast_p,
tsubst_flags_t complain)
{
tree binfo;
base_kind kind;
binfo = lookup_base (to, from, c_cast_p ? ba_unique : ba_check,
&kind, complain);
if (binfo == error_mark_node)
{
if (!(complain & tf_error))
return error_mark_node;
inform (input_location, " in pointer to member function conversion");
return size_zero_node;
}
else if (binfo)
{
if (kind != bk_via_virtual)
return BINFO_OFFSET (binfo);
else
/* FROM is a virtual base class of TO. Issue an error or warning
depending on whether or not this is a reinterpret cast. */
{
if (!(complain & tf_error))
return error_mark_node;
error ("pointer to member conversion via virtual base %qT",
BINFO_TYPE (binfo_from_vbase (binfo)));
return size_zero_node;
}
}
else
return NULL_TREE;
}
/* Get difference in deltas for different pointer to member function
types. If the conversion is invalid and tf_error is not set in
COMPLAIN, returns error_mark_node, otherwise returns an integer
constant of type PTRDIFF_TYPE_NODE and its value is zero if the
conversion is invalid. If ALLOW_INVERSE_P is true, then allow reverse
conversions as well. If C_CAST_P is true this conversion is taking
place as part of a C-style cast.
Note that the naming of FROM and TO is kind of backwards; the return
value is what we add to a TO in order to get a FROM. They are named
this way because we call this function to find out how to convert from
a pointer to member of FROM to a pointer to member of TO. */
static tree
get_delta_difference (tree from, tree to,
bool allow_inverse_p,
bool c_cast_p, tsubst_flags_t complain)
{
tree result;
if (same_type_ignoring_top_level_qualifiers_p (from, to))
/* Pointer to member of incomplete class is permitted*/
result = size_zero_node;
else
result = get_delta_difference_1 (from, to, c_cast_p, complain);
if (result == error_mark_node)
return error_mark_node;
if (!result)
{
if (!allow_inverse_p)
{
if (!(complain & tf_error))
return error_mark_node;
error_not_base_type (from, to);
inform (input_location, " in pointer to member conversion");
result = size_zero_node;
}
else
{
result = get_delta_difference_1 (to, from, c_cast_p, complain);
if (result == error_mark_node)
return error_mark_node;
if (result)
result = size_diffop_loc (input_location,
size_zero_node, result);
else
{
if (!(complain & tf_error))
return error_mark_node;
error_not_base_type (from, to);
inform (input_location, " in pointer to member conversion");
result = size_zero_node;
}
}
}
return convert_to_integer (ptrdiff_type_node, result);
}
/* Return a constructor for the pointer-to-member-function TYPE using
the other components as specified. */
tree
build_ptrmemfunc1 (tree type, tree delta, tree pfn)
{
tree u = NULL_TREE;
tree delta_field;
tree pfn_field;
vec<constructor_elt, va_gc> *v;
/* Pull the FIELD_DECLs out of the type. */
pfn_field = TYPE_FIELDS (type);
delta_field = DECL_CHAIN (pfn_field);
/* Make sure DELTA has the type we want. */
delta = convert_and_check (input_location, delta_type_node, delta);
/* Convert to the correct target type if necessary. */
pfn = fold_convert (TREE_TYPE (pfn_field), pfn);
/* Finish creating the initializer. */
vec_alloc (v, 2);
CONSTRUCTOR_APPEND_ELT(v, pfn_field, pfn);
CONSTRUCTOR_APPEND_ELT(v, delta_field, delta);
u = build_constructor (type, v);
TREE_CONSTANT (u) = TREE_CONSTANT (pfn) & TREE_CONSTANT (delta);
TREE_STATIC (u) = (TREE_CONSTANT (u)
&& (initializer_constant_valid_p (pfn, TREE_TYPE (pfn))
!= NULL_TREE)
&& (initializer_constant_valid_p (delta, TREE_TYPE (delta))
!= NULL_TREE));
return u;
}
/* Build a constructor for a pointer to member function. It can be
used to initialize global variables, local variable, or used
as a value in expressions. TYPE is the POINTER to METHOD_TYPE we
want to be.
If FORCE is nonzero, then force this conversion, even if
we would rather not do it. Usually set when using an explicit
cast. A C-style cast is being processed iff C_CAST_P is true.
Return error_mark_node, if something goes wrong. */
tree
build_ptrmemfunc (tree type, tree pfn, int force, bool c_cast_p,
tsubst_flags_t complain)
{
tree fn;
tree pfn_type;
tree to_type;
if (error_operand_p (pfn))
return error_mark_node;
pfn_type = TREE_TYPE (pfn);
to_type = build_ptrmemfunc_type (type);
/* Handle multiple conversions of pointer to member functions. */
if (TYPE_PTRMEMFUNC_P (pfn_type))
{
tree delta = NULL_TREE;
tree npfn = NULL_TREE;
tree n;
if (!force
&& !can_convert_arg (to_type, TREE_TYPE (pfn), pfn,
LOOKUP_NORMAL, complain))
{
if (complain & tf_error)
error ("invalid conversion to type %qT from type %qT",
to_type, pfn_type);
else
return error_mark_node;
}
n = get_delta_difference (TYPE_PTRMEMFUNC_OBJECT_TYPE (pfn_type),
TYPE_PTRMEMFUNC_OBJECT_TYPE (to_type),
force,
c_cast_p, complain);
if (n == error_mark_node)
return error_mark_node;
STRIP_ANY_LOCATION_WRAPPER (pfn);
/* We don't have to do any conversion to convert a
pointer-to-member to its own type. But, we don't want to
just return a PTRMEM_CST if there's an explicit cast; that
cast should make the expression an invalid template argument. */
if (TREE_CODE (pfn) != PTRMEM_CST
&& same_type_p (to_type, pfn_type))
return pfn;
if (TREE_SIDE_EFFECTS (pfn))
pfn = save_expr (pfn);
/* Obtain the function pointer and the current DELTA. */
if (TREE_CODE (pfn) == PTRMEM_CST)
expand_ptrmemfunc_cst (pfn, &delta, &npfn);
else
{
npfn = build_ptrmemfunc_access_expr (pfn, pfn_identifier);
delta = build_ptrmemfunc_access_expr (pfn, delta_identifier);
}
/* Just adjust the DELTA field. */
gcc_assert (same_type_ignoring_top_level_qualifiers_p
(TREE_TYPE (delta), ptrdiff_type_node));
if (!integer_zerop (n))
{
if (TARGET_PTRMEMFUNC_VBIT_LOCATION == ptrmemfunc_vbit_in_delta)
n = cp_build_binary_op (input_location,
LSHIFT_EXPR, n, integer_one_node,
complain);
delta = cp_build_binary_op (input_location,
PLUS_EXPR, delta, n, complain);
}
return build_ptrmemfunc1 (to_type, delta, npfn);
}
/* Handle null pointer to member function conversions. */
if (null_ptr_cst_p (pfn))
{
pfn = cp_build_c_cast (input_location,
TYPE_PTRMEMFUNC_FN_TYPE_RAW (to_type),
pfn, complain);
return build_ptrmemfunc1 (to_type,
integer_zero_node,
pfn);
}
if (type_unknown_p (pfn))
return instantiate_type (type, pfn, complain);
fn = TREE_OPERAND (pfn, 0);
gcc_assert (TREE_CODE (fn) == FUNCTION_DECL
/* In a template, we will have preserved the
OFFSET_REF. */
|| (processing_template_decl && TREE_CODE (fn) == OFFSET_REF));
return make_ptrmem_cst (to_type, fn);
}
/* Return the DELTA, IDX, PFN, and DELTA2 values for the PTRMEM_CST
given by CST.
??? There is no consistency as to the types returned for the above
values. Some code acts as if it were a sizetype and some as if it were
integer_type_node. */
void
expand_ptrmemfunc_cst (tree cst, tree *delta, tree *pfn)
{
tree type = TREE_TYPE (cst);
tree fn = PTRMEM_CST_MEMBER (cst);
tree ptr_class, fn_class;
gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
/* The class that the function belongs to. */
fn_class = DECL_CONTEXT (fn);
/* The class that we're creating a pointer to member of. */
ptr_class = TYPE_PTRMEMFUNC_OBJECT_TYPE (type);
/* First, calculate the adjustment to the function's class. */
*delta = get_delta_difference (fn_class, ptr_class, /*force=*/0,
/*c_cast_p=*/0, tf_warning_or_error);
if (!DECL_VIRTUAL_P (fn))
{
tree t = build_addr_func (fn, tf_warning_or_error);
if (TREE_CODE (t) == ADDR_EXPR)
SET_EXPR_LOCATION (t, PTRMEM_CST_LOCATION (cst));
*pfn = convert (TYPE_PTRMEMFUNC_FN_TYPE (type), t);
}
else
{
/* If we're dealing with a virtual function, we have to adjust 'this'
again, to point to the base which provides the vtable entry for
fn; the call will do the opposite adjustment. */
tree orig_class = DECL_CONTEXT (fn);
tree binfo = binfo_or_else (orig_class, fn_class);
*delta = fold_build2 (PLUS_EXPR, TREE_TYPE (*delta),
*delta, BINFO_OFFSET (binfo));
/* We set PFN to the vtable offset at which the function can be
found, plus one (unless ptrmemfunc_vbit_in_delta, in which
case delta is shifted left, and then incremented). */
*pfn = DECL_VINDEX (fn);
*pfn = fold_build2 (MULT_EXPR, integer_type_node, *pfn,
TYPE_SIZE_UNIT (vtable_entry_type));
switch (TARGET_PTRMEMFUNC_VBIT_LOCATION)
{
case ptrmemfunc_vbit_in_pfn:
*pfn = fold_build2 (PLUS_EXPR, integer_type_node, *pfn,
integer_one_node);
break;
case ptrmemfunc_vbit_in_delta:
*delta = fold_build2 (LSHIFT_EXPR, TREE_TYPE (*delta),
*delta, integer_one_node);
*delta = fold_build2 (PLUS_EXPR, TREE_TYPE (*delta),
*delta, integer_one_node);
break;
default:
gcc_unreachable ();
}
*pfn = fold_convert (TYPE_PTRMEMFUNC_FN_TYPE (type), *pfn);
}
}
/* Return an expression for PFN from the pointer-to-member function
given by T. */
static tree
pfn_from_ptrmemfunc (tree t)
{
if (TREE_CODE (t) == PTRMEM_CST)
{
tree delta;
tree pfn;
expand_ptrmemfunc_cst (t, &delta, &pfn);
if (pfn)
return pfn;
}
return build_ptrmemfunc_access_expr (t, pfn_identifier);
}
/* Return an expression for DELTA from the pointer-to-member function
given by T. */
static tree
delta_from_ptrmemfunc (tree t)
{
if (TREE_CODE (t) == PTRMEM_CST)
{
tree delta;
tree pfn;
expand_ptrmemfunc_cst (t, &delta, &pfn);
if (delta)
return delta;
}
return build_ptrmemfunc_access_expr (t, delta_identifier);
}
/* Convert value RHS to type TYPE as preparation for an assignment to
an lvalue of type TYPE. ERRTYPE indicates what kind of error the
implicit conversion is. If FNDECL is non-NULL, we are doing the
conversion in order to pass the PARMNUMth argument of FNDECL.
If FNDECL is NULL, we are doing the conversion in function pointer
argument passing, conversion in initialization, etc. */
static tree
convert_for_assignment (tree type, tree rhs,
impl_conv_rhs errtype, tree fndecl, int parmnum,
tsubst_flags_t complain, int flags)
{
tree rhstype;
enum tree_code coder;
location_t rhs_loc = cp_expr_loc_or_input_loc (rhs);
bool has_loc = EXPR_LOCATION (rhs) != UNKNOWN_LOCATION;
/* Strip NON_LVALUE_EXPRs since we aren't using as an lvalue,
but preserve location wrappers. */
if (TREE_CODE (rhs) == NON_LVALUE_EXPR
&& !location_wrapper_p (rhs))
rhs = TREE_OPERAND (rhs, 0);
/* Handle [dcl.init.list] direct-list-initialization from
single element of enumeration with a fixed underlying type. */
if (is_direct_enum_init (type, rhs))
{
tree elt = CONSTRUCTOR_ELT (rhs, 0)->value;
if (check_narrowing (ENUM_UNDERLYING_TYPE (type), elt, complain))
{
warning_sentinel w (warn_useless_cast);
warning_sentinel w2 (warn_ignored_qualifiers);
rhs = cp_build_c_cast (rhs_loc, type, elt, complain);
}
else
rhs = error_mark_node;
}
rhstype = TREE_TYPE (rhs);
coder = TREE_CODE (rhstype);
if (VECTOR_TYPE_P (type) && coder == VECTOR_TYPE
&& vector_types_convertible_p (type, rhstype, true))
{
rhs = mark_rvalue_use (rhs);
return convert (type, rhs);
}
if (rhs == error_mark_node || rhstype == error_mark_node)
return error_mark_node;
if (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node)
return error_mark_node;
/* The RHS of an assignment cannot have void type. */
if (coder == VOID_TYPE)
{
if (complain & tf_error)
error_at (rhs_loc, "void value not ignored as it ought to be");
return error_mark_node;
}
if (c_dialect_objc ())
{
int parmno;
tree selector;
tree rname = fndecl;
switch (errtype)
{
case ICR_ASSIGN:
parmno = -1;
break;
case ICR_INIT:
parmno = -2;
break;
default:
selector = objc_message_selector ();
parmno = parmnum;
if (selector && parmno > 1)
{
rname = selector;
parmno -= 1;
}
}
if (objc_compare_types (type, rhstype, parmno, rname))
{
rhs = mark_rvalue_use (rhs);
return convert (type, rhs);
}
}
/* [expr.ass]
The expression is implicitly converted (clause _conv_) to the
cv-unqualified type of the left operand.
We allow bad conversions here because by the time we get to this point
we are committed to doing the conversion. If we end up doing a bad
conversion, convert_like will complain. */
if (!can_convert_arg_bad (type, rhstype, rhs, flags, complain))
{
/* When -Wno-pmf-conversions is use, we just silently allow
conversions from pointers-to-members to plain pointers. If
the conversion doesn't work, cp_convert will complain. */
if (!warn_pmf2ptr
&& TYPE_PTR_P (type)
&& TYPE_PTRMEMFUNC_P (rhstype))
rhs = cp_convert (strip_top_quals (type), rhs, complain);
else
{
if (complain & tf_error)
{
/* If the right-hand side has unknown type, then it is an
overloaded function. Call instantiate_type to get error
messages. */
if (rhstype == unknown_type_node)
{
tree r = instantiate_type (type, rhs, tf_warning_or_error);
/* -fpermissive might allow this; recurse. */
if (!seen_error ())
return convert_for_assignment (type, r, errtype, fndecl,
parmnum, complain, flags);
}
else if (fndecl)
complain_about_bad_argument (rhs_loc,
rhstype, type,
fndecl, parmnum);
else
{
range_label_for_type_mismatch label (rhstype, type);
gcc_rich_location richloc (rhs_loc, has_loc ? &label : NULL);
auto_diagnostic_group d;
switch (errtype)
{
case ICR_DEFAULT_ARGUMENT:
error_at (&richloc,
"cannot convert %qH to %qI in default argument",
rhstype, type);
break;
case ICR_ARGPASS:
error_at (&richloc,
"cannot convert %qH to %qI in argument passing",
rhstype, type);
break;
case ICR_CONVERTING:
error_at (&richloc, "cannot convert %qH to %qI",
rhstype, type);
break;
case ICR_INIT:
error_at (&richloc,
"cannot convert %qH to %qI in initialization",
rhstype, type);
break;
case ICR_RETURN:
error_at (&richloc, "cannot convert %qH to %qI in return",
rhstype, type);
break;
case ICR_ASSIGN:
error_at (&richloc,
"cannot convert %qH to %qI in assignment",
rhstype, type);
break;
default:
gcc_unreachable();
}
}
/* See if we can be more helpful. */
maybe_show_nonconverting_candidate (type, rhstype, rhs, flags);
if (TYPE_PTR_P (rhstype)
&& TYPE_PTR_P (type)
&& CLASS_TYPE_P (TREE_TYPE (rhstype))
&& CLASS_TYPE_P (TREE_TYPE (type))
&& !COMPLETE_TYPE_P (TREE_TYPE (rhstype)))
inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL
(TREE_TYPE (rhstype))),
"class type %qT is incomplete", TREE_TYPE (rhstype));
}
return error_mark_node;
}
}
if (warn_suggest_attribute_format)
{
const enum tree_code codel = TREE_CODE (type);
if ((codel == POINTER_TYPE || codel == REFERENCE_TYPE)
&& coder == codel
&& check_missing_format_attribute (type, rhstype)
&& (complain & tf_warning))
switch (errtype)
{
case ICR_ARGPASS:
case ICR_DEFAULT_ARGUMENT:
if (fndecl)
warning (OPT_Wsuggest_attribute_format,
"parameter %qP of %qD might be a candidate "
"for a format attribute", parmnum, fndecl);
else
warning (OPT_Wsuggest_attribute_format,
"parameter might be a candidate "
"for a format attribute");
break;
case ICR_CONVERTING:
warning (OPT_Wsuggest_attribute_format,
"target of conversion might be a candidate "
"for a format attribute");
break;
case ICR_INIT:
warning (OPT_Wsuggest_attribute_format,
"target of initialization might be a candidate "
"for a format attribute");
break;
case ICR_RETURN:
warning (OPT_Wsuggest_attribute_format,
"return type might be a candidate "
"for a format attribute");
break;
case ICR_ASSIGN:
warning (OPT_Wsuggest_attribute_format,
"left-hand side of assignment might be a candidate "
"for a format attribute");
break;
default:
gcc_unreachable();
}
}
/* If -Wparentheses, warn about a = b = c when a has type bool and b
does not. */
if (TREE_CODE (type) == BOOLEAN_TYPE
&& TREE_CODE (TREE_TYPE (rhs)) != BOOLEAN_TYPE)
maybe_warn_unparenthesized_assignment (rhs, complain);
if (complain & tf_warning)
warn_for_address_of_packed_member (type, rhs);
return perform_implicit_conversion_flags (strip_top_quals (type), rhs,
complain, flags);
}
/* Convert RHS to be of type TYPE.
If EXP is nonzero, it is the target of the initialization.
ERRTYPE indicates what kind of error the implicit conversion is.
Two major differences between the behavior of
`convert_for_assignment' and `convert_for_initialization'
are that references are bashed in the former, while
copied in the latter, and aggregates are assigned in
the former (operator=) while initialized in the
latter (X(X&)).
If using constructor make sure no conversion operator exists, if one does
exist, an ambiguity exists. */
tree
convert_for_initialization (tree exp, tree type, tree rhs, int flags,
impl_conv_rhs errtype, tree fndecl, int parmnum,
tsubst_flags_t complain)
{
enum tree_code codel = TREE_CODE (type);
tree rhstype;
enum tree_code coder;
/* build_c_cast puts on a NOP_EXPR to make the result not an lvalue.
Strip such NOP_EXPRs, since RHS is used in non-lvalue context. */
if (TREE_CODE (rhs) == NOP_EXPR
&& TREE_TYPE (rhs) == TREE_TYPE (TREE_OPERAND (rhs, 0))
&& codel != REFERENCE_TYPE)
rhs = TREE_OPERAND (rhs, 0);
if (type == error_mark_node
|| rhs == error_mark_node
|| (TREE_CODE (rhs) == TREE_LIST && TREE_VALUE (rhs) == error_mark_node))
return error_mark_node;
if (MAYBE_CLASS_TYPE_P (non_reference (type)))
;
else if ((TREE_CODE (TREE_TYPE (rhs)) == ARRAY_TYPE
&& TREE_CODE (type) != ARRAY_TYPE
&& (!TYPE_REF_P (type)
|| TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE))
|| (TREE_CODE (TREE_TYPE (rhs)) == FUNCTION_TYPE
&& !TYPE_REFFN_P (type))
|| TREE_CODE (TREE_TYPE (rhs)) == METHOD_TYPE)
rhs = decay_conversion (rhs, complain);
rhstype = TREE_TYPE (rhs);
coder = TREE_CODE (rhstype);
if (coder == ERROR_MARK)
return error_mark_node;
/* We accept references to incomplete types, so we can
return here before checking if RHS is of complete type. */
if (codel == REFERENCE_TYPE)
{
auto_diagnostic_group d;
/* This should eventually happen in convert_arguments. */
int savew = 0, savee = 0;
if (fndecl)
savew = warningcount + werrorcount, savee = errorcount;
rhs = initialize_reference (type, rhs, flags, complain);
if (fndecl
&& (warningcount + werrorcount > savew || errorcount > savee))
inform (get_fndecl_argument_location (fndecl, parmnum),
"in passing argument %P of %qD", parmnum, fndecl);
return rhs;
}
if (exp != 0)
exp = require_complete_type (exp, complain);
if (exp == error_mark_node)
return error_mark_node;
type = complete_type (type);
if (DIRECT_INIT_EXPR_P (type, rhs))
/* Don't try to do copy-initialization if we already have
direct-initialization. */
return rhs;
if (MAYBE_CLASS_TYPE_P (type))
return perform_implicit_conversion_flags (type, rhs, complain, flags);
return convert_for_assignment (type, rhs, errtype, fndecl, parmnum,
complain, flags);
}
/* If RETVAL is the address of, or a reference to, a local variable or
temporary give an appropriate warning and return true. */
static bool
maybe_warn_about_returning_address_of_local (tree retval, location_t loc)
{
tree valtype = TREE_TYPE (DECL_RESULT (current_function_decl));
tree whats_returned = fold_for_warn (retval);
if (!loc)
loc = cp_expr_loc_or_input_loc (retval);
for (;;)
{
if (TREE_CODE (whats_returned) == COMPOUND_EXPR)
whats_returned = TREE_OPERAND (whats_returned, 1);
else if (CONVERT_EXPR_P (whats_returned)
|| TREE_CODE (whats_returned) == NON_LVALUE_EXPR)
whats_returned = TREE_OPERAND (whats_returned, 0);
else
break;
}
if (TREE_CODE (whats_returned) == TARGET_EXPR
&& is_std_init_list (TREE_TYPE (whats_returned)))
{
tree init = TARGET_EXPR_INITIAL (whats_returned);
if (TREE_CODE (init) == CONSTRUCTOR)
/* Pull out the array address. */
whats_returned = CONSTRUCTOR_ELT (init, 0)->value;
else if (TREE_CODE (init) == INDIRECT_REF)
/* The source of a trivial copy looks like *(T*)&var. */
whats_returned = TREE_OPERAND (init, 0);
else
return false;
STRIP_NOPS (whats_returned);
}
/* As a special case, we handle a call to std::move or std::forward. */
if (TREE_CODE (whats_returned) == CALL_EXPR
&& (is_std_move_p (whats_returned)
|| is_std_forward_p (whats_returned)))
{
tree arg = CALL_EXPR_ARG (whats_returned, 0);
return maybe_warn_about_returning_address_of_local (arg, loc);
}
if (TREE_CODE (whats_returned) != ADDR_EXPR)
return false;
whats_returned = TREE_OPERAND (whats_returned, 0);
while (TREE_CODE (whats_returned) == COMPONENT_REF
|| TREE_CODE (whats_returned) == ARRAY_REF)
whats_returned = TREE_OPERAND (whats_returned, 0);
if (TREE_CODE (whats_returned) == AGGR_INIT_EXPR
|| TREE_CODE (whats_returned) == TARGET_EXPR)
{
if (TYPE_REF_P (valtype))
warning_at (loc, OPT_Wreturn_local_addr,
"returning reference to temporary");
else if (TYPE_PTR_P (valtype))
warning_at (loc, OPT_Wreturn_local_addr,
"returning pointer to temporary");
else if (is_std_init_list (valtype))
warning_at (loc, OPT_Winit_list_lifetime,
"returning temporary %<initializer_list%> does not extend "
"the lifetime of the underlying array");
return true;
}
STRIP_ANY_LOCATION_WRAPPER (whats_returned);
if (DECL_P (whats_returned)
&& DECL_NAME (whats_returned)
&& DECL_FUNCTION_SCOPE_P (whats_returned)
&& !is_capture_proxy (whats_returned)
&& !(TREE_STATIC (whats_returned)
|| TREE_PUBLIC (whats_returned)))
{
if (VAR_P (whats_returned)
&& DECL_DECOMPOSITION_P (whats_returned)
&& DECL_DECOMP_BASE (whats_returned)
&& DECL_HAS_VALUE_EXPR_P (whats_returned))
{
/* When returning address of a structured binding, if the structured
binding is not a reference, continue normally, if it is a
reference, recurse on the initializer of the structured
binding. */
tree base = DECL_DECOMP_BASE (whats_returned);
if (TYPE_REF_P (TREE_TYPE (base)))
{
if (tree init = DECL_INITIAL (base))
return maybe_warn_about_returning_address_of_local (init, loc);
else
return false;
}
}
bool w = false;
auto_diagnostic_group d;
if (TYPE_REF_P (valtype))
w = warning_at (loc, OPT_Wreturn_local_addr,
"reference to local variable %qD returned",
whats_returned);
else if (is_std_init_list (valtype))
w = warning_at (loc, OPT_Winit_list_lifetime,
"returning local %<initializer_list%> variable %qD "
"does not extend the lifetime of the underlying array",
whats_returned);
else if (POINTER_TYPE_P (valtype)
&& TREE_CODE (whats_returned) == LABEL_DECL)
w = warning_at (loc, OPT_Wreturn_local_addr,
"address of label %qD returned",
whats_returned);
else if (POINTER_TYPE_P (valtype))
w = warning_at (loc, OPT_Wreturn_local_addr,
"address of local variable %qD returned",
whats_returned);
if (w)
inform (DECL_SOURCE_LOCATION (whats_returned),
"declared here");
return true;
}
return false;
}
/* Returns true if DECL is in the std namespace. */
bool
decl_in_std_namespace_p (tree decl)
{
while (decl)
{
decl = decl_namespace_context (decl);
if (DECL_NAMESPACE_STD_P (decl))
return true;
/* Allow inline namespaces inside of std namespace, e.g. with
--enable-symvers=gnu-versioned-namespace std::forward would be
actually std::_8::forward. */
if (!DECL_NAMESPACE_INLINE_P (decl))
return false;
decl = CP_DECL_CONTEXT (decl);
}
return false;
}
/* Returns true if FN, a CALL_EXPR, is a call to std::forward. */
static bool
is_std_forward_p (tree fn)
{
/* std::forward only takes one argument. */
if (call_expr_nargs (fn) != 1)
return false;
tree fndecl = cp_get_callee_fndecl_nofold (fn);
if (!decl_in_std_namespace_p (fndecl))
return false;
tree name = DECL_NAME (fndecl);
return name && id_equal (name, "forward");
}
/* Returns true if FN, a CALL_EXPR, is a call to std::move. */
static bool
is_std_move_p (tree fn)
{
/* std::move only takes one argument. */
if (call_expr_nargs (fn) != 1)
return false;
tree fndecl = cp_get_callee_fndecl_nofold (fn);
if (!decl_in_std_namespace_p (fndecl))
return false;
tree name = DECL_NAME (fndecl);
return name && id_equal (name, "move");
}
/* Returns true if RETVAL is a good candidate for the NRVO as per
[class.copy.elision]. FUNCTYPE is the type the function is declared
to return. */
static bool
can_do_nrvo_p (tree retval, tree functype)
{
if (functype == error_mark_node)
return false;
if (retval)
STRIP_ANY_LOCATION_WRAPPER (retval);
tree result = DECL_RESULT (current_function_decl);
return (retval != NULL_TREE
&& !processing_template_decl
/* Must be a local, automatic variable. */
&& VAR_P (retval)
&& DECL_CONTEXT (retval) == current_function_decl
&& !TREE_STATIC (retval)
/* And not a lambda or anonymous union proxy. */
&& !DECL_HAS_VALUE_EXPR_P (retval)
&& (DECL_ALIGN (retval) <= DECL_ALIGN (result))
/* The cv-unqualified type of the returned value must be the
same as the cv-unqualified return type of the
function. */
&& same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (retval)),
TYPE_MAIN_VARIANT (functype))
/* And the returned value must be non-volatile. */
&& !TYPE_VOLATILE (TREE_TYPE (retval)));
}
/* True if we would like to perform NRVO, i.e. can_do_nrvo_p is true and we
would otherwise return in memory. */
static bool
want_nrvo_p (tree retval, tree functype)
{
return (can_do_nrvo_p (retval, functype)
&& aggregate_value_p (functype, current_function_decl));
}
/* Like can_do_nrvo_p, but we check if we're trying to move a class
prvalue. */
static bool
can_elide_copy_prvalue_p (tree retval, tree functype)
{
if (functype == error_mark_node)
return false;
if (retval)
STRIP_ANY_LOCATION_WRAPPER (retval);
return (retval != NULL_TREE
&& !glvalue_p (retval)
&& same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (retval)),
TYPE_MAIN_VARIANT (functype))
&& !TYPE_VOLATILE (TREE_TYPE (retval)));
}
/* If we should treat RETVAL, an expression being returned, as if it were
designated by an rvalue, returns it adjusted accordingly; otherwise, returns
NULL_TREE. See [class.copy.elision]. RETURN_P is true if this is a return
context (rather than throw). */
tree
treat_lvalue_as_rvalue_p (tree expr, bool return_p)
{
if (cxx_dialect == cxx98)
return NULL_TREE;
tree retval = expr;
STRIP_ANY_LOCATION_WRAPPER (retval);
if (REFERENCE_REF_P (retval))
retval = TREE_OPERAND (retval, 0);
/* An implicitly movable entity is a variable of automatic storage duration
that is either a non-volatile object or (C++20) an rvalue reference to a
non-volatile object type. */
if (!(((VAR_P (retval) && !DECL_HAS_VALUE_EXPR_P (retval))
|| TREE_CODE (retval) == PARM_DECL)
&& !TREE_STATIC (retval)
&& !CP_TYPE_VOLATILE_P (non_reference (TREE_TYPE (retval)))
&& (TREE_CODE (TREE_TYPE (retval)) != REFERENCE_TYPE
|| (cxx_dialect >= cxx20
&& TYPE_REF_IS_RVALUE (TREE_TYPE (retval))))))
return NULL_TREE;
/* If the expression in a return or co_return statement is a (possibly
parenthesized) id-expression that names an implicitly movable entity
declared in the body or parameter-declaration-clause of the innermost
enclosing function or lambda-expression, */
if (DECL_CONTEXT (retval) != current_function_decl)
return NULL_TREE;
if (return_p)
{
expr = move (expr);
if (expr == error_mark_node)
return NULL_TREE;
return set_implicit_rvalue_p (expr);
}
/* if the operand of a throw-expression is a (possibly parenthesized)
id-expression that names an implicitly movable entity whose scope does not
extend beyond the compound-statement of the innermost try-block or
function-try-block (if any) whose compound-statement or ctor-initializer
encloses the throw-expression, */
/* C++20 added move on throw of parms. */
if (TREE_CODE (retval) == PARM_DECL && cxx_dialect < cxx20)
return NULL_TREE;
for (cp_binding_level *b = current_binding_level;
; b = b->level_chain)
{
for (tree decl = b->names; decl; decl = TREE_CHAIN (decl))
if (decl == retval)
return set_implicit_rvalue_p (move (expr));
if (b->kind == sk_function_parms || b->kind == sk_try)
return NULL_TREE;
}
}
/* Warn about dubious usage of std::move (in a return statement, if RETURN_P
is true). EXPR is the std::move expression; TYPE is the type of the object
being initialized. */
void
maybe_warn_pessimizing_move (tree expr, tree type, bool return_p)
{
if (!(warn_pessimizing_move || warn_redundant_move))
return;
const location_t loc = cp_expr_loc_or_input_loc (expr);
/* C++98 doesn't know move. */
if (cxx_dialect < cxx11)
return;
/* Wait until instantiation time, since we can't gauge if we should do
the NRVO until then. */
if (processing_template_decl)
return;
/* This is only interesting for class types. */
if (!CLASS_TYPE_P (type))
return;
bool wrapped_p = false;
/* A a = std::move (A()); */
if (TREE_CODE (expr) == TREE_LIST)
{
if (list_length (expr) == 1)
{
expr = TREE_VALUE (expr);
wrapped_p = true;
}
else
return;
}
/* A a = {std::move (A())};
A a{std::move (A())}; */
else if (TREE_CODE (expr) == CONSTRUCTOR)
{
if (CONSTRUCTOR_NELTS (expr) == 1)
{
expr = CONSTRUCTOR_ELT (expr, 0)->value;
wrapped_p = true;
}
else
return;
}
/* First, check if this is a call to std::move. */
if (!REFERENCE_REF_P (expr)
|| TREE_CODE (TREE_OPERAND (expr, 0)) != CALL_EXPR)
return;
tree fn = TREE_OPERAND (expr, 0);
if (!is_std_move_p (fn))
return;
tree arg = CALL_EXPR_ARG (fn, 0);
if (TREE_CODE (arg) != NOP_EXPR)
return;
/* If we're looking at *std::move<T&> ((T &) &arg), do the pessimizing N/RVO
and implicitly-movable warnings. */
if (TREE_CODE (TREE_OPERAND (arg, 0)) == ADDR_EXPR)
{
arg = TREE_OPERAND (arg, 0);
arg = TREE_OPERAND (arg, 0);
arg = convert_from_reference (arg);
if (can_elide_copy_prvalue_p (arg, type))
{
auto_diagnostic_group d;
if (warning_at (loc, OPT_Wpessimizing_move,
"moving a temporary object prevents copy elision"))
inform (loc, "remove %<std::move%> call");
}
/* The rest of the warnings is only relevant for when we are returning
from a function. */
if (!return_p)
return;
tree moved;
/* Warn if we could do copy elision were it not for the move. */
if (can_do_nrvo_p (arg, type))
{
auto_diagnostic_group d;
if (!warning_suppressed_p (expr, OPT_Wpessimizing_move)
&& warning_at (loc, OPT_Wpessimizing_move,
"moving a local object in a return statement "
"prevents copy elision"))
inform (loc, "remove %<std::move%> call");
}
/* Warn if the move is redundant. It is redundant when we would
do maybe-rvalue overload resolution even without std::move. */
else if (warn_redundant_move
/* This doesn't apply for return {std::move (t)};. */
&& !wrapped_p
&& !warning_suppressed_p (expr, OPT_Wredundant_move)
&& (moved = treat_lvalue_as_rvalue_p (arg, /*return*/true)))
{
/* Make sure that overload resolution would actually succeed
if we removed the std::move call. */
tree t = convert_for_initialization (NULL_TREE, type,
moved,
(LOOKUP_NORMAL
| LOOKUP_ONLYCONVERTING),
ICR_RETURN, NULL_TREE, 0,
tf_none);
/* If this worked, implicit rvalue would work, so the call to
std::move is redundant. */
if (t != error_mark_node)
{
auto_diagnostic_group d;
if (warning_at (loc, OPT_Wredundant_move,
"redundant move in return statement"))
inform (loc, "remove %<std::move%> call");
}
}
}
/* Also try to warn about redundant std::move in code such as
T f (const T& t)
{
return std::move(t);
}
for which EXPR will be something like
*std::move<const T&> ((const struct T &) (const struct T *) t)
and where the std::move does nothing if T does not have a T(const T&&)
constructor, because the argument is const. It will not use T(T&&)
because that would mean losing the const. */
else if (warn_redundant_move
&& !warning_suppressed_p (expr, OPT_Wredundant_move)
&& TYPE_REF_P (TREE_TYPE (arg))
&& CP_TYPE_CONST_P (TREE_TYPE (TREE_TYPE (arg))))
{
tree rtype = TREE_TYPE (TREE_TYPE (arg));
if (!same_type_ignoring_top_level_qualifiers_p (rtype, type))
return;
/* Check for the unlikely case there's T(const T&&) (we don't care if
it's deleted). */
for (tree fn : ovl_range (CLASSTYPE_CONSTRUCTORS (rtype)))
if (move_fn_p (fn))
{
tree t = TREE_VALUE (FUNCTION_FIRST_USER_PARMTYPE (fn));
if (UNLIKELY (CP_TYPE_CONST_P (TREE_TYPE (t))))
return;
}
auto_diagnostic_group d;
if (return_p
? warning_at (loc, OPT_Wredundant_move,
"redundant move in return statement")
: warning_at (loc, OPT_Wredundant_move,
"redundant move in initialization"))
inform (loc, "remove %<std::move%> call");
}
}
/* Check that returning RETVAL from the current function is valid.
Return an expression explicitly showing all conversions required to
change RETVAL into the function return type, and to assign it to
the DECL_RESULT for the function. Set *NO_WARNING to true if
code reaches end of non-void function warning shouldn't be issued
on this RETURN_EXPR. Set *DANGLING to true if code returns the
address of a local variable. */
tree
check_return_expr (tree retval, bool *no_warning, bool *dangling)
{
tree result;
/* The type actually returned by the function. */
tree valtype;
/* The type the function is declared to return, or void if
the declared type is incomplete. */
tree functype;
int fn_returns_value_p;
location_t loc = cp_expr_loc_or_input_loc (retval);
*no_warning = false;
*dangling = false;
/* A `volatile' function is one that isn't supposed to return, ever.
(This is a G++ extension, used to get better code for functions
that call the `volatile' function.) */
if (TREE_THIS_VOLATILE (current_function_decl))
warning (0, "function declared %<noreturn%> has a %<return%> statement");
/* Check for various simple errors. */
if (DECL_DESTRUCTOR_P (current_function_decl))
{
if (retval)
error_at (loc, "returning a value from a destructor");
if (targetm.cxx.cdtor_returns_this () && !processing_template_decl)
retval = current_class_ptr;
else
return NULL_TREE;
}
else if (DECL_CONSTRUCTOR_P (current_function_decl))
{
if (in_function_try_handler)
/* If a return statement appears in a handler of the
function-try-block of a constructor, the program is ill-formed. */
error ("cannot return from a handler of a function-try-block of a constructor");
else if (retval)
/* You can't return a value from a constructor. */
error_at (loc, "returning a value from a constructor");
if (targetm.cxx.cdtor_returns_this () && !processing_template_decl)
retval = current_class_ptr;
else
return NULL_TREE;
}
const tree saved_retval = retval;
if (processing_template_decl)
{
current_function_returns_value = 1;
if (check_for_bare_parameter_packs (retval))
return error_mark_node;
/* If one of the types might be void, we can't tell whether we're
returning a value. */
if ((WILDCARD_TYPE_P (TREE_TYPE (DECL_RESULT (current_function_decl)))
&& !FNDECL_USED_AUTO (current_function_decl))
|| (retval != NULL_TREE
&& (TREE_TYPE (retval) == NULL_TREE
|| WILDCARD_TYPE_P (TREE_TYPE (retval)))))
goto dependent;
}
functype = TREE_TYPE (TREE_TYPE (current_function_decl));
/* Deduce auto return type from a return statement. */
if (FNDECL_USED_AUTO (current_function_decl))
{
tree pattern = DECL_SAVED_AUTO_RETURN_TYPE (current_function_decl);
tree auto_node;
tree type;
if (!retval && !is_auto (pattern))
{
/* Give a helpful error message. */
error ("return-statement with no value, in function returning %qT",
pattern);
inform (input_location, "only plain %<auto%> return type can be "
"deduced to %<void%>");
type = error_mark_node;
}
else if (retval && BRACE_ENCLOSED_INITIALIZER_P (retval))
{
error ("returning initializer list");
type = error_mark_node;
}
else
{
if (!retval)
retval = void_node;
auto_node = type_uses_auto (pattern);
type = do_auto_deduction (pattern, retval, auto_node,
tf_warning_or_error, adc_return_type);
}
if (type == error_mark_node)
/* Leave it. */;
else if (functype == pattern)
apply_deduced_return_type (current_function_decl, type);
else if (!same_type_p (type, functype))
{
if (LAMBDA_FUNCTION_P (current_function_decl))
error_at (loc, "inconsistent types %qT and %qT deduced for "
"lambda return type", functype, type);
else
error_at (loc, "inconsistent deduction for auto return type: "
"%qT and then %qT", functype, type);
}
functype = type;
}
result = DECL_RESULT (current_function_decl);
valtype = TREE_TYPE (result);
gcc_assert (valtype != NULL_TREE);
fn_returns_value_p = !VOID_TYPE_P (valtype);
/* Check for a return statement with no return value in a function
that's supposed to return a value. */
if (!retval && fn_returns_value_p)
{
if (functype != error_mark_node)
permerror (input_location, "return-statement with no value, in "
"function returning %qT", valtype);
/* Remember that this function did return. */
current_function_returns_value = 1;
/* And signal caller that TREE_NO_WARNING should be set on the
RETURN_EXPR to avoid control reaches end of non-void function
warnings in tree-cfg.cc. */
*no_warning = true;
}
/* Check for a return statement with a value in a function that
isn't supposed to return a value. */
else if (retval && !fn_returns_value_p)
{
if (VOID_TYPE_P (TREE_TYPE (retval)))
/* You can return a `void' value from a function of `void'
type. In that case, we have to evaluate the expression for
its side-effects. */
finish_expr_stmt (retval);
else if (retval != error_mark_node)
permerror (loc, "return-statement with a value, in function "
"returning %qT", valtype);
current_function_returns_null = 1;
/* There's really no value to return, after all. */
return NULL_TREE;
}
else if (!retval)
/* Remember that this function can sometimes return without a
value. */
current_function_returns_null = 1;
else
/* Remember that this function did return a value. */
current_function_returns_value = 1;
/* Check for erroneous operands -- but after giving ourselves a
chance to provide an error about returning a value from a void
function. */
if (error_operand_p (retval))
{
current_function_return_value = error_mark_node;
return error_mark_node;
}
/* Only operator new(...) throw(), can return NULL [expr.new/13]. */
if (IDENTIFIER_NEW_OP_P (DECL_NAME (current_function_decl))
&& !TYPE_NOTHROW_P (TREE_TYPE (current_function_decl))
&& ! flag_check_new
&& retval && null_ptr_cst_p (retval))
warning (0, "%<operator new%> must not return NULL unless it is "
"declared %<throw()%> (or %<-fcheck-new%> is in effect)");
/* Effective C++ rule 15. See also start_function. */
if (warn_ecpp
&& DECL_NAME (current_function_decl) == assign_op_identifier
&& !type_dependent_expression_p (retval))
{
bool warn = true;
/* The function return type must be a reference to the current
class. */
if (TYPE_REF_P (valtype)
&& same_type_ignoring_top_level_qualifiers_p
(TREE_TYPE (valtype), TREE_TYPE (current_class_ref)))
{
/* Returning '*this' is obviously OK. */
if (retval == current_class_ref)
warn = false;
/* If we are calling a function whose return type is the same of
the current class reference, it is ok. */
else if (INDIRECT_REF_P (retval)
&& TREE_CODE (TREE_OPERAND (retval, 0)) == CALL_EXPR)
warn = false;
}
if (warn)
warning_at (loc, OPT_Weffc__,
"%<operator=%> should return a reference to %<*this%>");
}
if (dependent_type_p (functype)
|| type_dependent_expression_p (retval))
{
dependent:
/* We should not have changed the return value. */
gcc_assert (retval == saved_retval);
/* We don't know if this is an lvalue or rvalue use, but
either way we can mark it as read. */
mark_exp_read (retval);
return retval;
}
/* The fabled Named Return Value optimization, as per [class.copy]/15:
[...] For a function with a class return type, if the expression
in the return statement is the name of a local object, and the cv-
unqualified type of the local object is the same as the function
return type, an implementation is permitted to omit creating the tem-
porary object to hold the function return value [...]
So, if this is a value-returning function that always returns the same
local variable, remember it.
We choose the first suitable variable even if the function sometimes
returns something else, but only if the variable is out of scope at the
other return sites, or else we run the risk of clobbering the variable we
chose if the other returned expression uses the chosen variable somehow.
We don't currently do this if the first return is a non-variable, as it
would be complicated to determine whether an NRV selected later was in
scope at the point of the earlier return. We also don't currently support
multiple variables with non-overlapping scopes (53637).
See finish_function and finalize_nrv for the rest of this optimization. */
tree bare_retval = NULL_TREE;
if (retval)
{
retval = maybe_undo_parenthesized_ref (retval);
bare_retval = tree_strip_any_location_wrapper (retval);
}
bool named_return_value_okay_p = want_nrvo_p (bare_retval, functype);
if (fn_returns_value_p && flag_elide_constructors
&& current_function_return_value != bare_retval)
{
if (named_return_value_okay_p
&& current_function_return_value == NULL_TREE)
current_function_return_value = bare_retval;
else if (current_function_return_value
&& VAR_P (current_function_return_value)
&& DECL_NAME (current_function_return_value)
&& !decl_in_scope_p (current_function_return_value))
{
/* The earlier NRV is out of scope at this point, so it's safe to
leave it alone; the current return can't refer to it. */;
if (named_return_value_okay_p
&& !warning_suppressed_p (current_function_decl, OPT_Wnrvo))
{
warning (OPT_Wnrvo, "not eliding copy on return from %qD",
bare_retval);
suppress_warning (current_function_decl, OPT_Wnrvo);
}
}
else
{
if ((named_return_value_okay_p
|| (current_function_return_value
&& current_function_return_value != error_mark_node))
&& !warning_suppressed_p (current_function_decl, OPT_Wnrvo))
{
warning (OPT_Wnrvo, "not eliding copy on return in %qD",
current_function_decl);
suppress_warning (current_function_decl, OPT_Wnrvo);
}
current_function_return_value = error_mark_node;
}
}
/* We don't need to do any conversions when there's nothing being
returned. */
if (!retval)
return NULL_TREE;
if (!named_return_value_okay_p)
maybe_warn_pessimizing_move (retval, functype, /*return_p*/true);
/* Do any required conversions. */
if (bare_retval == result || DECL_CONSTRUCTOR_P (current_function_decl))
/* No conversions are required. */
;
else
{
int flags = LOOKUP_NORMAL | LOOKUP_ONLYCONVERTING;
/* The functype's return type will have been set to void, if it
was an incomplete type. Just treat this as 'return;' */
if (VOID_TYPE_P (functype))
return error_mark_node;
/* Under C++11 [12.8/32 class.copy], a returned lvalue is sometimes
treated as an rvalue for the purposes of overload resolution to
favor move constructors over copy constructors.
Note that these conditions are similar to, but not as strict as,
the conditions for the named return value optimization. */
bool converted = false;
tree moved;
/* Until C++23, this was only interesting for class type, but in C++23,
we should do the below when we're converting rom/to a class/reference
(a non-scalar type). */
if ((cxx_dialect < cxx23
? CLASS_TYPE_P (functype)
: !SCALAR_TYPE_P (functype) || !SCALAR_TYPE_P (TREE_TYPE (retval)))
&& (moved = treat_lvalue_as_rvalue_p (retval, /*return*/true)))
/* In C++20 and earlier we treat the return value as an rvalue
that can bind to lvalue refs. In C++23, such an expression is just
an xvalue (see reference_binding). */
retval = moved;
/* The call in a (lambda) thunk needs no conversions. */
if (TREE_CODE (retval) == CALL_EXPR
&& call_from_lambda_thunk_p (retval))
converted = true;
/* First convert the value to the function's return type, then
to the type of return value's location to handle the
case that functype is smaller than the valtype. */
if (!converted)
retval = convert_for_initialization
(NULL_TREE, functype, retval, flags, ICR_RETURN, NULL_TREE, 0,
tf_warning_or_error);
retval = convert (valtype, retval);
/* If the conversion failed, treat this just like `return;'. */
if (retval == error_mark_node)
return retval;
/* We can't initialize a register from a AGGR_INIT_EXPR. */
else if (! cfun->returns_struct
&& TREE_CODE (retval) == TARGET_EXPR
&& TREE_CODE (TREE_OPERAND (retval, 1)) == AGGR_INIT_EXPR)
retval = build2 (COMPOUND_EXPR, TREE_TYPE (retval), retval,
TREE_OPERAND (retval, 0));
else if (!processing_template_decl
&& maybe_warn_about_returning_address_of_local (retval, loc)
&& INDIRECT_TYPE_P (valtype))
*dangling = true;
}
/* A naive attempt to reduce the number of -Wdangling-reference false
positives: if we know that this function can return a variable with
static storage duration rather than one of its parameters, suppress
the warning. */
if (warn_dangling_reference
&& TYPE_REF_P (functype)
&& bare_retval
&& VAR_P (bare_retval)
&& TREE_STATIC (bare_retval))
suppress_warning (current_function_decl, OPT_Wdangling_reference);
if (processing_template_decl)
return saved_retval;
/* Actually copy the value returned into the appropriate location. */
if (retval && retval != result)
{
/* If there's a postcondition for a scalar return value, wrap
retval in a call to the postcondition function. */
if (tree post = apply_postcondition_to_return (retval))
retval = post;
retval = cp_build_init_expr (result, retval);
}
if (current_function_return_value == bare_retval)
INIT_EXPR_NRV_P (retval) = true;
if (tree set = maybe_set_retval_sentinel ())
retval = build2 (COMPOUND_EXPR, void_type_node, retval, set);
/* If there's a postcondition for an aggregate return value, call the
postcondition function after the return object is initialized. */
if (tree post = apply_postcondition_to_return (result))
retval = build2 (COMPOUND_EXPR, void_type_node, retval, post);
return retval;
}
/* Returns nonzero if the pointer-type FROM can be converted to the
pointer-type TO via a qualification conversion. If CONSTP is -1,
then we return nonzero if the pointers are similar, and the
cv-qualification signature of FROM is a proper subset of that of TO.
If CONSTP is positive, then all outer pointers have been
const-qualified. */
static bool
comp_ptr_ttypes_real (tree to, tree from, int constp)
{
bool to_more_cv_qualified = false;
bool is_opaque_pointer = false;
for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
{
if (TREE_CODE (to) != TREE_CODE (from))
return false;
if (TREE_CODE (from) == OFFSET_TYPE
&& !same_type_p (TYPE_OFFSET_BASETYPE (from),
TYPE_OFFSET_BASETYPE (to)))
return false;
/* Const and volatile mean something different for function and
array types, so the usual checks are not appropriate. We'll
check the array type elements in further iterations. */
if (!FUNC_OR_METHOD_TYPE_P (to) && TREE_CODE (to) != ARRAY_TYPE)
{
if (!at_least_as_qualified_p (to, from))
return false;
if (!at_least_as_qualified_p (from, to))
{
if (constp == 0)
return false;
to_more_cv_qualified = true;
}
if (constp > 0)
constp &= TYPE_READONLY (to);
}
if (VECTOR_TYPE_P (to))
is_opaque_pointer = vector_targets_convertible_p (to, from);
/* P0388R4 allows a conversion from int[N] to int[] but not the
other way round. When both arrays have bounds but they do
not match, then no conversion is possible. */
if (TREE_CODE (to) == ARRAY_TYPE
&& !comp_array_types (to, from, bounds_first, /*strict=*/false))
return false;
if (!TYPE_PTR_P (to)
&& !TYPE_PTRDATAMEM_P (to)
/* CWG 330 says we need to look through arrays. */
&& TREE_CODE (to) != ARRAY_TYPE)
return ((constp >= 0 || to_more_cv_qualified)
&& (is_opaque_pointer
|| same_type_ignoring_top_level_qualifiers_p (to, from)));
}
}
/* When comparing, say, char ** to char const **, this function takes
the 'char *' and 'char const *'. Do not pass non-pointer/reference
types to this function. */
int
comp_ptr_ttypes (tree to, tree from)
{
return comp_ptr_ttypes_real (to, from, 1);
}
/* Returns true iff FNTYPE is a non-class type that involves
error_mark_node. We can get FUNCTION_TYPE with buried error_mark_node
if a parameter type is ill-formed. */
bool
error_type_p (const_tree type)
{
tree t;
switch (TREE_CODE (type))
{
case ERROR_MARK:
return true;
case POINTER_TYPE:
case REFERENCE_TYPE:
case OFFSET_TYPE:
return error_type_p (TREE_TYPE (type));
case FUNCTION_TYPE:
case METHOD_TYPE:
if (error_type_p (TREE_TYPE (type)))
return true;
for (t = TYPE_ARG_TYPES (type); t; t = TREE_CHAIN (t))
if (error_type_p (TREE_VALUE (t)))
return true;
return false;
case RECORD_TYPE:
if (TYPE_PTRMEMFUNC_P (type))
return error_type_p (TYPE_PTRMEMFUNC_FN_TYPE (type));
return false;
default:
return false;
}
}
/* Returns true if to and from are (possibly multi-level) pointers to the same
type or inheritance-related types, regardless of cv-quals. */
bool
ptr_reasonably_similar (const_tree to, const_tree from)
{
for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
{
/* Any target type is similar enough to void. */
if (VOID_TYPE_P (to))
return !error_type_p (from);
if (VOID_TYPE_P (from))
return !error_type_p (to);
if (TREE_CODE (to) != TREE_CODE (from))
return false;
if (TREE_CODE (from) == OFFSET_TYPE
&& comptypes (TYPE_OFFSET_BASETYPE (to),
TYPE_OFFSET_BASETYPE (from),
COMPARE_BASE | COMPARE_DERIVED))
continue;
if (VECTOR_TYPE_P (to)
&& vector_types_convertible_p (to, from, false))
return true;
if (TREE_CODE (to) == INTEGER_TYPE
&& TYPE_PRECISION (to) == TYPE_PRECISION (from))
return true;
if (TREE_CODE (to) == FUNCTION_TYPE)
return !error_type_p (to) && !error_type_p (from);
if (!TYPE_PTR_P (to))
{
/* When either type is incomplete avoid DERIVED_FROM_P,
which may call complete_type (c++/57942). */
bool b = !COMPLETE_TYPE_P (to) || !COMPLETE_TYPE_P (from);
return comptypes
(TYPE_MAIN_VARIANT (to), TYPE_MAIN_VARIANT (from),
b ? COMPARE_STRICT : COMPARE_BASE | COMPARE_DERIVED);
}
}
}
/* Return true if TO and FROM (both of which are POINTER_TYPEs or
pointer-to-member types) are the same, ignoring cv-qualification at
all levels. CB says how we should behave when comparing array bounds. */
bool
comp_ptr_ttypes_const (tree to, tree from, compare_bounds_t cb)
{
bool is_opaque_pointer = false;
for (; ; to = TREE_TYPE (to), from = TREE_TYPE (from))
{
if (TREE_CODE (to) != TREE_CODE (from))
return false;
if (TREE_CODE (from) == OFFSET_TYPE
&& same_type_p (TYPE_OFFSET_BASETYPE (from),
TYPE_OFFSET_BASETYPE (to)))
continue;
if (VECTOR_TYPE_P (to))
is_opaque_pointer = vector_targets_convertible_p (to, from);
if (TREE_CODE (to) == ARRAY_TYPE
/* Ignore cv-qualification, but if we see e.g. int[3] and int[4],
we must fail. */
&& !comp_array_types (to, from, cb, /*strict=*/false))
return false;
/* CWG 330 says we need to look through arrays. */
if (!TYPE_PTR_P (to) && TREE_CODE (to) != ARRAY_TYPE)
return (is_opaque_pointer
|| same_type_ignoring_top_level_qualifiers_p (to, from));
}
}
/* Returns the type qualifiers for this type, including the qualifiers on the
elements for an array type. */
int
cp_type_quals (const_tree type)
{
int quals;
/* This CONST_CAST is okay because strip_array_types returns its
argument unmodified and we assign it to a const_tree. */
type = strip_array_types (CONST_CAST_TREE (type));
if (type == error_mark_node
/* Quals on a FUNCTION_TYPE are memfn quals. */
|| TREE_CODE (type) == FUNCTION_TYPE)
return TYPE_UNQUALIFIED;
quals = TYPE_QUALS (type);
/* METHOD and REFERENCE_TYPEs should never have quals. */
gcc_assert ((TREE_CODE (type) != METHOD_TYPE
&& !TYPE_REF_P (type))
|| ((quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE))
== TYPE_UNQUALIFIED));
return quals;
}
/* Returns the function-ref-qualifier for TYPE */
cp_ref_qualifier
type_memfn_rqual (const_tree type)
{
gcc_assert (FUNC_OR_METHOD_TYPE_P (type));
if (!FUNCTION_REF_QUALIFIED (type))
return REF_QUAL_NONE;
else if (FUNCTION_RVALUE_QUALIFIED (type))
return REF_QUAL_RVALUE;
else
return REF_QUAL_LVALUE;
}
/* Returns the function-cv-quals for TYPE, which must be a FUNCTION_TYPE or
METHOD_TYPE. */
int
type_memfn_quals (const_tree type)
{
if (TREE_CODE (type) == FUNCTION_TYPE)
return TYPE_QUALS (type);
else if (TREE_CODE (type) == METHOD_TYPE)
return cp_type_quals (class_of_this_parm (type));
else
gcc_unreachable ();
}
/* Returns the FUNCTION_TYPE TYPE with its function-cv-quals changed to
MEMFN_QUALS and its ref-qualifier to RQUAL. */
tree
apply_memfn_quals (tree type, cp_cv_quals memfn_quals, cp_ref_qualifier rqual)
{
/* Could handle METHOD_TYPE here if necessary. */
gcc_assert (TREE_CODE (type) == FUNCTION_TYPE);
if (TYPE_QUALS (type) == memfn_quals
&& type_memfn_rqual (type) == rqual)
return type;
/* This should really have a different TYPE_MAIN_VARIANT, but that gets
complex. */
tree result = build_qualified_type (type, memfn_quals);
return build_ref_qualified_type (result, rqual);
}
/* Returns nonzero if TYPE is const or volatile. */
bool
cv_qualified_p (const_tree type)
{
int quals = cp_type_quals (type);
return (quals & (TYPE_QUAL_CONST|TYPE_QUAL_VOLATILE)) != 0;
}
/* Returns nonzero if the TYPE contains a mutable member. */
bool
cp_has_mutable_p (const_tree type)
{
/* This CONST_CAST is okay because strip_array_types returns its
argument unmodified and we assign it to a const_tree. */
type = strip_array_types (CONST_CAST_TREE(type));
return CLASS_TYPE_P (type) && CLASSTYPE_HAS_MUTABLE (type);
}
/* Set TREE_READONLY and TREE_VOLATILE on DECL as indicated by the
TYPE_QUALS. For a VAR_DECL, this may be an optimistic
approximation. In particular, consider:
int f();
struct S { int i; };
const S s = { f(); }
Here, we will make "s" as TREE_READONLY (because it is declared
"const") -- only to reverse ourselves upon seeing that the
initializer is non-constant. */
void
cp_apply_type_quals_to_decl (int type_quals, tree decl)
{
tree type = TREE_TYPE (decl);
if (type == error_mark_node)
return;
if (TREE_CODE (decl) == TYPE_DECL)
return;
gcc_assert (!(TREE_CODE (type) == FUNCTION_TYPE
&& type_quals != TYPE_UNQUALIFIED));
/* Avoid setting TREE_READONLY incorrectly. */
/* We used to check TYPE_NEEDS_CONSTRUCTING here, but now a constexpr
constructor can produce constant init, so rely on cp_finish_decl to
clear TREE_READONLY if the variable has non-constant init. */
/* If the type has (or might have) a mutable component, that component
might be modified. */
if (TYPE_HAS_MUTABLE_P (type) || !COMPLETE_TYPE_P (type))
type_quals &= ~TYPE_QUAL_CONST;
c_apply_type_quals_to_decl (type_quals, decl);
}
/* Subroutine of casts_away_constness. Make T1 and T2 point at
exemplar types such that casting T1 to T2 is casting away constness
if and only if there is no implicit conversion from T1 to T2. */
static void
casts_away_constness_r (tree *t1, tree *t2, tsubst_flags_t complain)
{
int quals1;
int quals2;
/* [expr.const.cast]
For multi-level pointer to members and multi-level mixed pointers
and pointers to members (conv.qual), the "member" aspect of a
pointer to member level is ignored when determining if a const
cv-qualifier has been cast away. */
/* [expr.const.cast]
For two pointer types:
X1 is T1cv1,1 * ... cv1,N * where T1 is not a pointer type
X2 is T2cv2,1 * ... cv2,M * where T2 is not a pointer type
K is min(N,M)
casting from X1 to X2 casts away constness if, for a non-pointer
type T there does not exist an implicit conversion (clause
_conv_) from:
Tcv1,(N-K+1) * cv1,(N-K+2) * ... cv1,N *
to
Tcv2,(M-K+1) * cv2,(M-K+2) * ... cv2,M *. */
if ((!TYPE_PTR_P (*t1) && !TYPE_PTRDATAMEM_P (*t1))
|| (!TYPE_PTR_P (*t2) && !TYPE_PTRDATAMEM_P (*t2)))
{
*t1 = cp_build_qualified_type (void_type_node,
cp_type_quals (*t1));
*t2 = cp_build_qualified_type (void_type_node,
cp_type_quals (*t2));
return;
}
quals1 = cp_type_quals (*t1);
quals2 = cp_type_quals (*t2);
if (TYPE_PTRDATAMEM_P (*t1))
*t1 = TYPE_PTRMEM_POINTED_TO_TYPE (*t1);
else
*t1 = TREE_TYPE (*t1);
if (TYPE_PTRDATAMEM_P (*t2))
*t2 = TYPE_PTRMEM_POINTED_TO_TYPE (*t2);
else
*t2 = TREE_TYPE (*t2);
casts_away_constness_r (t1, t2, complain);
*t1 = build_pointer_type (*t1);
*t2 = build_pointer_type (*t2);
*t1 = cp_build_qualified_type (*t1, quals1);
*t2 = cp_build_qualified_type (*t2, quals2);
}
/* Returns nonzero if casting from TYPE1 to TYPE2 casts away
constness.
??? This function returns non-zero if casting away qualifiers not
just const. We would like to return to the caller exactly which
qualifiers are casted away to give more accurate diagnostics.
*/
static bool
casts_away_constness (tree t1, tree t2, tsubst_flags_t complain)
{
if (TYPE_REF_P (t2))
{
/* [expr.const.cast]
Casting from an lvalue of type T1 to an lvalue of type T2
using a reference cast casts away constness if a cast from an
rvalue of type "pointer to T1" to the type "pointer to T2"
casts away constness. */
t1 = (TYPE_REF_P (t1) ? TREE_TYPE (t1) : t1);
return casts_away_constness (build_pointer_type (t1),
build_pointer_type (TREE_TYPE (t2)),
complain);
}
if (TYPE_PTRDATAMEM_P (t1) && TYPE_PTRDATAMEM_P (t2))
/* [expr.const.cast]
Casting from an rvalue of type "pointer to data member of X
of type T1" to the type "pointer to data member of Y of type
T2" casts away constness if a cast from an rvalue of type
"pointer to T1" to the type "pointer to T2" casts away
constness. */
return casts_away_constness
(build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t1)),
build_pointer_type (TYPE_PTRMEM_POINTED_TO_TYPE (t2)),
complain);
/* Casting away constness is only something that makes sense for
pointer or reference types. */
if (!TYPE_PTR_P (t1) || !TYPE_PTR_P (t2))
return false;
/* Top-level qualifiers don't matter. */
t1 = TYPE_MAIN_VARIANT (t1);
t2 = TYPE_MAIN_VARIANT (t2);
casts_away_constness_r (&t1, &t2, complain);
if (!can_convert (t2, t1, complain))
return true;
return false;
}
/* If T is a REFERENCE_TYPE return the type to which T refers.
Otherwise, return T itself. */
tree
non_reference (tree t)
{
if (t && TYPE_REF_P (t))
t = TREE_TYPE (t);
return t;
}
/* Return nonzero if REF is an lvalue valid for this language;
otherwise, print an error message and return zero. USE says
how the lvalue is being used and so selects the error message. */
int
lvalue_or_else (tree ref, enum lvalue_use use, tsubst_flags_t complain)
{
cp_lvalue_kind kind = lvalue_kind (ref);
if (kind == clk_none)
{
if (complain & tf_error)
lvalue_error (cp_expr_loc_or_input_loc (ref), use);
return 0;
}
else if (kind & (clk_rvalueref|clk_class))
{
if (!(complain & tf_error))
return 0;
/* Make this a permerror because we used to accept it. */
permerror (cp_expr_loc_or_input_loc (ref),
"using rvalue as lvalue");
}
return 1;
}
/* Return true if a user-defined literal operator is a raw operator. */
bool
check_raw_literal_operator (const_tree decl)
{
tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
tree argtype;
int arity;
bool maybe_raw_p = false;
/* Count the number and type of arguments and check for ellipsis. */
for (argtype = argtypes, arity = 0;
argtype && argtype != void_list_node;
++arity, argtype = TREE_CHAIN (argtype))
{
tree t = TREE_VALUE (argtype);
if (same_type_p (t, const_string_type_node))
maybe_raw_p = true;
}
if (!argtype)
return false; /* Found ellipsis. */
if (!maybe_raw_p || arity != 1)
return false;
return true;
}
/* Return true if a user-defined literal operator has one of the allowed
argument types. */
bool
check_literal_operator_args (const_tree decl,
bool *long_long_unsigned_p, bool *long_double_p)
{
tree argtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
*long_long_unsigned_p = false;
*long_double_p = false;
if (processing_template_decl || processing_specialization)
return argtypes == void_list_node;
else
{
tree argtype;
int arity;
int max_arity = 2;
/* Count the number and type of arguments and check for ellipsis. */
for (argtype = argtypes, arity = 0;
argtype && argtype != void_list_node;
argtype = TREE_CHAIN (argtype))
{
tree t = TREE_VALUE (argtype);
++arity;
if (TYPE_PTR_P (t))
{
bool maybe_raw_p = false;
t = TREE_TYPE (t);
if (cp_type_quals (t) != TYPE_QUAL_CONST)
return false;
t = TYPE_MAIN_VARIANT (t);
if ((maybe_raw_p = same_type_p (t, char_type_node))
|| same_type_p (t, wchar_type_node)
|| same_type_p (t, char8_type_node)
|| same_type_p (t, char16_type_node)
|| same_type_p (t, char32_type_node))
{
argtype = TREE_CHAIN (argtype);
if (!argtype)
return false;
t = TREE_VALUE (argtype);
if (maybe_raw_p && argtype == void_list_node)
return true;
else if (same_type_p (t, size_type_node))
{
++arity;
continue;
}
else
return false;
}
}
else if (same_type_p (t, long_long_unsigned_type_node))
{
max_arity = 1;
*long_long_unsigned_p = true;
}
else if (same_type_p (t, long_double_type_node))
{
max_arity = 1;
*long_double_p = true;
}
else if (same_type_p (t, char_type_node))
max_arity = 1;
else if (same_type_p (t, wchar_type_node))
max_arity = 1;
else if (same_type_p (t, char8_type_node))
max_arity = 1;
else if (same_type_p (t, char16_type_node))
max_arity = 1;
else if (same_type_p (t, char32_type_node))
max_arity = 1;
else
return false;
}
if (!argtype)
return false; /* Found ellipsis. */
if (arity != max_arity)
return false;
return true;
}
}
/* Always returns false since unlike C90, C++ has no concept of implicit
function declarations. */
bool
c_decl_implicit (const_tree)
{
return false;
}
|