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
|
/* Processing rules for constraints.
Copyright (C) 2013-2016 Free Software Foundation, Inc.
Contributed by Andrew Sutton (andrew.n.sutton@gmail.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/>. */
#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "tm.h"
#include "hash-set.h"
#include "machmode.h"
#include "vec.h"
#include "double-int.h"
#include "input.h"
#include "alias.h"
#include "symtab.h"
#include "wide-int.h"
#include "inchash.h"
#include "tree.h"
#include "stringpool.h"
#include "attribs.h"
#include "intl.h"
#include "flags.h"
#include "cp-tree.h"
#include "c-family/c-common.h"
#include "c-family/c-objc.h"
#include "cp-objcp-common.h"
#include "tree-inline.h"
#include "decl.h"
#include "toplev.h"
#include "type-utils.h"
/*---------------------------------------------------------------------------
Operations on constraints
---------------------------------------------------------------------------*/
/* Returns true if C is a constraint tree code. Note that ERROR_MARK
is a valid constraint. */
static inline bool
constraint_p (tree_code c)
{
return (PRED_CONSTR <= c && c <= DISJ_CONSTR) || c == ERROR_MARK;
}
/* Returns true if T is a constraint. Note that error_mark_node
is a valid constraint. */
bool
constraint_p (tree t)
{
return constraint_p (TREE_CODE (t));
}
/* Make a predicate constraint from the given expression. */
tree
make_predicate_constraint (tree expr)
{
return build_nt (PRED_CONSTR, expr);
}
/* Returns the conjunction of two constraints A and B. Note that
conjoining a non-null constraint with NULL_TREE is an identity
operation. That is, for non-null A,
conjoin_constraints(a, NULL_TREE) == a
and
conjoin_constraints (NULL_TREE, a) == a
If both A and B are NULL_TREE, the result is also NULL_TREE. */
tree
conjoin_constraints (tree a, tree b)
{
gcc_assert (a ? constraint_p (a) : true);
gcc_assert (b ? constraint_p (b) : true);
if (a)
return b ? build_nt (CONJ_CONSTR, a, b) : a;
else if (b)
return b;
else
return NULL_TREE;
}
/* Transform the vector of expressions in the T into a conjunction
of requirements. T must be a TREE_VEC. */
tree
conjoin_constraints (tree t)
{
gcc_assert (TREE_CODE (t) == TREE_VEC);
tree r = NULL_TREE;
for (int i = 0; i < TREE_VEC_LENGTH (t); ++i)
r = conjoin_constraints (r, TREE_VEC_ELT (t, i));
return r;
}
/* Returns true if T is a call expression to a function
concept. */
bool
function_concept_check_p (tree t)
{
gcc_assert (TREE_CODE (t) == CALL_EXPR);
tree fn = CALL_EXPR_FN (t);
if (TREE_CODE (fn) == TEMPLATE_ID_EXPR
&& TREE_CODE (TREE_OPERAND (fn, 0)) == OVERLOAD)
{
tree f1 = get_first_fn (fn);
if (TREE_CODE (f1) == TEMPLATE_DECL
&& DECL_DECLARED_CONCEPT_P (DECL_TEMPLATE_RESULT (f1)))
return true;
}
return false;
}
/*---------------------------------------------------------------------------
Resolution of qualified concept names
---------------------------------------------------------------------------*/
/* This facility is used to resolve constraint checks from
requirement expressions. A constraint check is a call to
a function template declared with the keyword 'concept'.
The result of resolution is a pair (a TREE_LIST) whose value
is the matched declaration, and whose purpose contains the
coerced template arguments that can be substituted into the
call. */
// Given an overload set OVL, try to find a unique definition that can be
// instantiated by the template arguments ARGS.
//
// This function is not called for arbitrary call expressions. In particular,
// the call expression must be written with explicit template arguments
// and no function arguments. For example:
//
// f<T, U>()
//
// If a single match is found, this returns a TREE_LIST whose VALUE
// is the constraint function (not the template), and its PURPOSE is
// the complete set of arguments substituted into the parameter list.
static tree
resolve_constraint_check (tree ovl, tree args)
{
tree cands = NULL_TREE;
for (tree p = ovl; p != NULL_TREE; p = OVL_NEXT (p))
{
// Get the next template overload.
tree tmpl = OVL_CURRENT (p);
if (TREE_CODE (tmpl) != TEMPLATE_DECL)
continue;
// Don't try to deduce checks for non-concepts. We often
// end up trying to resolve constraints in functional casts
// as part of a postfix-expression. We can save time and
// headaches by not instantiating those declarations.
//
// NOTE: This masks a potential error, caused by instantiating
// non-deduced contexts using placeholder arguments.
tree fn = DECL_TEMPLATE_RESULT (tmpl);
if (DECL_ARGUMENTS (fn))
continue;
if (!DECL_DECLARED_CONCEPT_P (fn))
continue;
// Remember the candidate if we can deduce a substitution.
++processing_template_decl;
tree parms = TREE_VALUE (DECL_TEMPLATE_PARMS (tmpl));
if (tree subst = coerce_template_parms (parms, args, tmpl))
if (subst != error_mark_node)
cands = tree_cons (subst, fn, cands);
--processing_template_decl;
}
// If we didn't find a unique candidate, then this is
// not a constraint check.
if (!cands || TREE_CHAIN (cands))
return NULL_TREE;
return cands;
}
// Determine if the the call expression CALL is a constraint check, and
// return the concept declaration and arguments being checked. If CALL
// does not denote a constraint check, return NULL.
tree
resolve_constraint_check (tree call)
{
gcc_assert (TREE_CODE (call) == CALL_EXPR);
// A constraint check must be only a template-id expression. If
// it's a call to a base-link, its function(s) should be a
// template-id expression. If this is not a template-id, then it
// cannot be a concept-check.
tree target = CALL_EXPR_FN (call);
if (BASELINK_P (target))
target = BASELINK_FUNCTIONS (target);
if (TREE_CODE (target) != TEMPLATE_ID_EXPR)
return NULL_TREE;
// Get the overload set and template arguments and try to
// resolve the target.
tree ovl = TREE_OPERAND (target, 0);
/* This is a function call of a variable concept... ill-formed. */
if (TREE_CODE (ovl) == TEMPLATE_DECL)
{
error_at (location_of (call),
"function call of variable concept %qE", call);
return error_mark_node;
}
tree args = TREE_OPERAND (target, 1);
return resolve_constraint_check (ovl, args);
}
/* Returns a pair containing the checked variable concept
and its associated prototype parameter. The result
is a TREE_LIST whose TREE_VALUE is the variable concept
and whose TREE_PURPOSE is the prototype parameter. */
tree
resolve_variable_concept_check (tree id)
{
tree tmpl = TREE_OPERAND (id, 0);
tree args = TREE_OPERAND (id, 1);
if (!variable_concept_p (tmpl))
return NULL_TREE;
/* Make sure that we have the right parameters before
assuming that it works. Note that failing to deduce
will result in diagnostics. */
tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
tree result = coerce_template_parms (parms, args, tmpl);
if (result != error_mark_node)
{
tree decl = DECL_TEMPLATE_RESULT (tmpl);
return build_tree_list (result, decl);
}
else
return NULL_TREE;
}
/* Given a call expression or template-id expression to
a concept EXPR possibly including a wildcard, deduce
the concept being checked and the prototype parameter.
Returns true if the constraint and prototype can be
deduced and false otherwise. Note that the CHECK and
PROTO arguments are set to NULL_TREE if this returns
false. */
bool
deduce_constrained_parameter (tree expr, tree& check, tree& proto)
{
tree info = NULL_TREE;
if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
info = resolve_variable_concept_check (expr);
else if (TREE_CODE (expr) == CALL_EXPR)
info = resolve_constraint_check (expr);
else
gcc_unreachable ();
if (info && info != error_mark_node)
{
check = TREE_VALUE (info);
tree arg = TREE_VEC_ELT (TREE_PURPOSE (info), 0);
if (ARGUMENT_PACK_P (arg))
arg = TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg), 0);
proto = TREE_TYPE (arg);
return true;
}
check = proto = NULL_TREE;
return false;
}
// Given a call expression or template-id expression to a concept, EXPR,
// deduce the concept being checked and return the template arguments.
// Returns NULL_TREE if deduction fails.
static tree
deduce_concept_introduction (tree expr)
{
tree info = NULL_TREE;
if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
info = resolve_variable_concept_check (expr);
else if (TREE_CODE (expr) == CALL_EXPR)
info = resolve_constraint_check (expr);
else
gcc_unreachable ();
if (info && info != error_mark_node)
return TREE_PURPOSE (info);
return NULL_TREE;
}
namespace {
/*---------------------------------------------------------------------------
Lifting of concept definitions
---------------------------------------------------------------------------*/
/* Part of constraint normalization. Whenever we find a reference to
a variable concept or a call to a function concept, we lift or
inline that concept's definition into the constraint. This ensures
that constraints are always checked in the immediate instantiation
context. */
tree lift_expression (tree);
/* If the tree T has operands, then lift any concepts out of them. */
tree
lift_operands (tree t)
{
if (int n = tree_operand_length (t))
{
t = copy_node (t);
for (int i = 0; i < n; ++i)
TREE_OPERAND (t, i) = lift_expression (TREE_OPERAND (t, i));
}
return t;
}
/* Recursively lift all operands of the function call. Also, check
that the call target is not accidentally a variable concept
since that's ill-formed. */
tree
lift_function_call (tree t)
{
gcc_assert (TREE_CODE (t) == CALL_EXPR);
gcc_assert (!VAR_P (CALL_EXPR_FN (t)));
return lift_operands (t);
}
/* Inline a function (concept) definition by substituting
ARGS into its body. */
tree
lift_function_definition (tree fn, tree args)
{
/* Extract the body of the function minus the return expression. */
tree body = DECL_SAVED_TREE (fn);
if (!body)
return error_mark_node;
if (TREE_CODE (body) == BIND_EXPR)
body = BIND_EXPR_BODY (body);
if (TREE_CODE (body) != RETURN_EXPR)
return error_mark_node;
body = TREE_OPERAND (body, 0);
/* Substitute template arguments to produce our inline expression. */
tree result = tsubst_expr (body, args, tf_none, NULL_TREE, false);
if (result == error_mark_node)
return error_mark_node;
return lift_expression (result);
}
/* Inline a reference to a function concept. */
tree
lift_call_expression (tree t)
{
/* Try to resolve this function call as a concept. If not, then
it can be returned as-is. */
tree check = resolve_constraint_check (t);
if (!check)
return lift_function_call (t);
if (check == error_mark_node)
return error_mark_node;
tree fn = TREE_VALUE (check);
tree args = TREE_PURPOSE (check);
return lift_function_definition (fn, args);
}
tree
lift_variable_initializer (tree var, tree args)
{
/* Extract the body from the variable initializer. */
tree init = DECL_INITIAL (var);
if (!init)
return error_mark_node;
/* Substitute the arguments to form our new inline expression. */
tree result = tsubst_expr (init, args, tf_none, NULL_TREE, false);
if (result == error_mark_node)
return error_mark_node;
return lift_expression (result);
}
/* Determine if a template-id is a variable concept and inline. */
tree
lift_template_id (tree t)
{
if (tree info = resolve_variable_concept_check (t))
{
tree decl = TREE_VALUE (info);
tree args = TREE_PURPOSE (info);
return lift_variable_initializer (decl, args);
}
/* Check that we didn't refer to a function concept like
a variable.
TODO: Add a note on how to fix this. */
tree tmpl = TREE_OPERAND (t, 0);
if (TREE_CODE (tmpl) == OVERLOAD)
{
tree fn = OVL_FUNCTION (tmpl);
if (TREE_CODE (fn) == TEMPLATE_DECL
&& DECL_DECLARED_CONCEPT_P (DECL_TEMPLATE_RESULT (fn)))
{
error_at (location_of (t),
"invalid reference to function concept %qD", fn);
return error_mark_node;
}
}
return t;
}
/* Lift any constraints appearing in a nested requirement of
a requires-expression. */
tree
lift_requires_expression (tree t)
{
tree parms = TREE_OPERAND (t, 0);
tree reqs = TREE_OPERAND (t, 1);
tree result = NULL_TREE;
for (; reqs != NULL_TREE; reqs = TREE_CHAIN (reqs))
{
tree req = TREE_VALUE (reqs);
if (TREE_CODE (req) == NESTED_REQ)
{
tree expr = lift_expression (TREE_OPERAND (req, 0));
req = finish_nested_requirement (expr);
}
result = tree_cons (NULL_TREE, req, result);
}
return finish_requires_expr (parms, result);
}
/* Inline references to specializations of concepts. */
tree
lift_expression (tree t)
{
if (t == NULL_TREE)
return NULL_TREE;
if (t == error_mark_node)
return error_mark_node;
/* Concepts can be referred to by call or variable. All other
nodes are preserved. */
switch (TREE_CODE (t))
{
case CALL_EXPR:
return lift_call_expression (t);
case TEMPLATE_ID_EXPR:
return lift_template_id (t);
case REQUIRES_EXPR:
return lift_requires_expression (t);
case EXPR_PACK_EXPANSION:
/* Use copy_node rather than make_pack_expansion so that
PACK_EXPANSION_PARAMETER_PACKS stays the same. */
t = copy_node (t);
SET_PACK_EXPANSION_PATTERN
(t, lift_expression (PACK_EXPANSION_PATTERN (t)));
return t;
case TREE_LIST:
{
t = copy_node (t);
TREE_VALUE (t) = lift_expression (TREE_VALUE (t));
TREE_CHAIN (t) = lift_expression (TREE_CHAIN (t));
return t;
}
default:
return lift_operands (t);
}
}
/*---------------------------------------------------------------------------
Transformation of expressions into constraints
---------------------------------------------------------------------------*/
/* Part of constraint normalization. The following functions rewrite
expressions as constraints. */
tree transform_expression (tree);
/* Check that the logical-or or logical-and expression does
not result in a call to a user-defined user-defined operator
(temp.constr.op). Returns true if the logical operator is
admissible and false otherwise. */
bool
check_logical_expr (tree t)
{
/* We can't do much for type dependent expressions. */
if (type_dependent_expression_p (t))
return true;
/* Resolve the logical operator. Note that template processing is
disabled so we get the actual call or target expression back.
not_processing_template_sentinel sentinel.
TODO: This check is actually subsumed by the requirement that
constraint operands have type bool. I'm not sure we need it
unless we allow conversions. */
tree arg1 = TREE_OPERAND (t, 0);
tree arg2 = TREE_OPERAND (t, 1);
tree ovl = NULL_TREE;
tree expr = build_x_binary_op (EXPR_LOC_OR_LOC (arg2, input_location),
TREE_CODE (t),
arg1, TREE_CODE (arg1),
arg2, TREE_CODE (arg2),
&ovl,
tf_none);
if (TREE_CODE (expr) != TREE_CODE (t))
{
error ("user-defined operator %qs in constraint %q+E",
operator_name_info[TREE_CODE (t)].name, t);
return false;
}
return true;
}
/* Transform a logical-or or logical-and expression into either
a conjunction or disjunction. */
tree
xform_logical (tree t, tree_code c)
{
if (!check_logical_expr (t))
return error_mark_node;
tree t0 = transform_expression (TREE_OPERAND (t, 0));
tree t1 = transform_expression (TREE_OPERAND (t, 1));
return build_nt (c, t0, t1);
}
/* A simple requirement T introduces an expression constraint
for its expression. */
inline tree
xform_simple_requirement (tree t)
{
return build_nt (EXPR_CONSTR, TREE_OPERAND (t, 0));
}
/* A type requirement T introduce a type constraint for its type. */
inline tree
xform_type_requirement (tree t)
{
return build_nt (TYPE_CONSTR, TREE_OPERAND (t, 0));
}
/* A compound requirement T introduces a conjunction of constraints
depending on its form. The conjunction always includes an
expression constraint for the expression of the requirement.
If a trailing return type was specified, the conjunction includes
either an implicit conversion constraint or an argument deduction
constraint. If the noexcept specifier is present, the conjunction
includes an exception constraint. */
tree
xform_compound_requirement (tree t)
{
tree expr = TREE_OPERAND (t, 0);
tree constr = build_nt (EXPR_CONSTR, TREE_OPERAND (t, 0));
/* If a type is given, append an implicit conversion or
argument deduction constraint. */
if (tree type = TREE_OPERAND (t, 1))
{
tree type_constr;
/* TODO: We should be extracting a list of auto nodes
from type_uses_auto, not a single node */
if (tree placeholder = type_uses_auto (type))
type_constr = build_nt (DEDUCT_CONSTR, expr, type, placeholder);
else
type_constr = build_nt (ICONV_CONSTR, expr, type);
constr = conjoin_constraints (constr, type_constr);
}
/* If noexcept is present, append an exception constraint. */
if (COMPOUND_REQ_NOEXCEPT_P (t))
{
tree except = build_nt (EXCEPT_CONSTR, expr);
constr = conjoin_constraints (constr, except);
}
return constr;
}
/* A nested requirement T introduces a conjunction of constraints
corresponding to its constraint-expression.
If the result of transforming T is error_mark_node, the resulting
constraint is a predicate constraint whose operand is also
error_mark_node. This preserves the constraint structure, but
will guarantee that the constraint is never satisfied. */
inline tree
xform_nested_requirement (tree t)
{
return transform_expression (TREE_OPERAND (t, 0));
}
/* Transform a requirement T into one or more constraints. */
tree
xform_requirement (tree t)
{
switch (TREE_CODE (t))
{
case SIMPLE_REQ:
return xform_simple_requirement (t);
case TYPE_REQ:
return xform_type_requirement (t);
case COMPOUND_REQ:
return xform_compound_requirement (t);
case NESTED_REQ:
return xform_nested_requirement (t);
default:
gcc_unreachable ();
}
return error_mark_node;
}
/* Transform a sequence of requirements into a conjunction of
constraints. */
tree
xform_requirements (tree t)
{
tree result = NULL_TREE;
for (; t; t = TREE_CHAIN (t))
{
tree constr = xform_requirement (TREE_VALUE (t));
result = conjoin_constraints (result, constr);
}
return result;
}
/* Transform a requires-expression into a parameterized constraint. */
tree
xform_requires_expr (tree t)
{
tree operand = xform_requirements (TREE_OPERAND (t, 1));
if (tree parms = TREE_OPERAND (t, 0))
return build_nt (PARM_CONSTR, parms, operand);
else
return operand;
}
/* Transform an expression into an atomic predicate constraint.
After substitution, the expression of a predicate constraint
shall have type bool (temp.constr.pred). For non-type-dependent
expressions, we can check that now. */
tree
xform_atomic (tree t)
{
if (TREE_TYPE (t) && !type_dependent_expression_p (t))
{
tree type = cv_unqualified (TREE_TYPE (t));
if (!same_type_p (type, boolean_type_node))
{
error ("predicate constraint %q+E does not have type %<bool%>", t);
return error_mark_node;
}
}
return build_nt (PRED_CONSTR, t);
}
/* Push down the pack expansion EXP into the leaves of the constraint PAT. */
tree
push_down_pack_expansion (tree exp, tree pat)
{
switch (TREE_CODE (pat))
{
case CONJ_CONSTR:
case DISJ_CONSTR:
{
pat = copy_node (pat);
TREE_OPERAND (pat, 0)
= push_down_pack_expansion (exp, TREE_OPERAND (pat, 0));
TREE_OPERAND (pat, 1)
= push_down_pack_expansion (exp, TREE_OPERAND (pat, 1));
return pat;
}
default:
{
exp = copy_node (exp);
SET_PACK_EXPANSION_PATTERN (exp, pat);
return exp;
}
}
}
/* Transform a pack expansion into a constraint. First we transform the
pattern of the pack expansion, then we push the pack expansion down into the
leaves of the constraint so that partial ordering will work. */
tree
xform_pack_expansion (tree t)
{
tree pat = transform_expression (PACK_EXPANSION_PATTERN (t));
return push_down_pack_expansion (t, pat);
}
/* Transform an expression into a constraint. */
tree
xform_expr (tree t)
{
switch (TREE_CODE (t))
{
case TRUTH_ANDIF_EXPR:
return xform_logical (t, CONJ_CONSTR);
case TRUTH_ORIF_EXPR:
return xform_logical (t, DISJ_CONSTR);
case REQUIRES_EXPR:
return xform_requires_expr (t);
case BIND_EXPR:
return transform_expression (BIND_EXPR_BODY (t));
case EXPR_PACK_EXPANSION:
return xform_pack_expansion (t);
default:
/* All other constraints are atomic. */
return xform_atomic (t);
}
}
/* Transform a statement into an expression. */
tree
xform_stmt (tree t)
{
switch (TREE_CODE (t))
{
case RETURN_EXPR:
return transform_expression (TREE_OPERAND (t, 0));
default:
gcc_unreachable ();
}
return error_mark_node;
}
/* Reduction rules for the declaration T. */
tree
xform_decl (tree t)
{
switch (TREE_CODE (t))
{
case VAR_DECL:
return xform_atomic (t);
default:
gcc_unreachable ();
}
return error_mark_node;
}
/* Transform a lifted expression into a constraint. This either
returns a constraint, or it returns error_mark_node when
a constraint cannot be formed. */
tree
transform_expression (tree t)
{
if (!t)
return NULL_TREE;
if (t == error_mark_node)
return error_mark_node;
switch (TREE_CODE_CLASS (TREE_CODE (t)))
{
case tcc_unary:
case tcc_binary:
case tcc_expression:
case tcc_vl_exp:
return xform_expr (t);
case tcc_statement:
return xform_stmt (t);
case tcc_declaration:
return xform_decl (t);
case tcc_exceptional:
case tcc_constant:
case tcc_reference:
case tcc_comparison:
/* These are all atomic predicate constraints. */
return xform_atomic (t);
default:
/* Unhandled node kind. */
gcc_unreachable ();
}
return error_mark_node;
}
/*---------------------------------------------------------------------------
Constraint normalization
---------------------------------------------------------------------------*/
tree normalize_constraint (tree);
/* The normal form of the disjunction T0 /\ T1 is the conjunction
of the normal form of T0 and the normal form of T1. */
inline tree
normalize_conjunction (tree t)
{
tree t0 = normalize_constraint (TREE_OPERAND (t, 0));
tree t1 = normalize_constraint (TREE_OPERAND (t, 1));
return build_nt (CONJ_CONSTR, t0, t1);
}
/* The normal form of the disjunction T0 \/ T1 is the disjunction
of the normal form of T0 and the normal form of T1. */
inline tree
normalize_disjunction (tree t)
{
tree t0 = normalize_constraint (TREE_OPERAND (t, 0));
tree t1 = normalize_constraint (TREE_OPERAND (t, 1));
return build_nt (DISJ_CONSTR, t0, t1);
}
/* A predicate constraint is normalized in two stages. First all
references specializations of concepts are replaced by their
substituted definitions. Then, the resulting expression is
transformed into a constraint by transforming && expressions
into conjunctions and || into disjunctions. */
tree
normalize_predicate_constraint (tree t)
{
++processing_template_decl;
tree expr = PRED_CONSTR_EXPR (t);
tree lifted = lift_expression (expr);
tree constr = transform_expression (lifted);
--processing_template_decl;
return constr;
}
/* The normal form of a parameterized constraint is the normal
form of its operand. */
tree
normalize_parameterized_constraint (tree t)
{
tree parms = PARM_CONSTR_PARMS (t);
tree operand = normalize_constraint (PARM_CONSTR_OPERAND (t));
return build_nt (PARM_CONSTR, parms, operand);
}
/* Normalize the constraint T by reducing it so that it is
comprised of only conjunctions and disjunctions of atomic
constraints. */
tree
normalize_constraint (tree t)
{
if (!t)
return NULL_TREE;
if (t == error_mark_node)
return t;
switch (TREE_CODE (t))
{
case CONJ_CONSTR:
return normalize_conjunction (t);
case DISJ_CONSTR:
return normalize_disjunction (t);
case PRED_CONSTR:
return normalize_predicate_constraint (t);
case PARM_CONSTR:
return normalize_parameterized_constraint (t);
case EXPR_CONSTR:
case TYPE_CONSTR:
case ICONV_CONSTR:
case DEDUCT_CONSTR:
case EXCEPT_CONSTR:
/* These constraints are defined to be atomic. */
return t;
default:
/* CONSTR was not a constraint. */
gcc_unreachable();
}
return error_mark_node;
}
} /* namespace */
// -------------------------------------------------------------------------- //
// Constraint Semantic Processing
//
// The following functions are called by the parser and substitution rules
// to create and evaluate constraint-related nodes.
// The constraints associated with the current template parameters.
tree
current_template_constraints (void)
{
if (!current_template_parms)
return NULL_TREE;
tree tmpl_constr = TEMPLATE_PARM_CONSTRAINTS (current_template_parms);
return build_constraints (tmpl_constr, NULL_TREE);
}
// If the recently parsed TYPE declares or defines a template or template
// specialization, get its corresponding constraints from the current
// template parameters and bind them to TYPE's declaration.
tree
associate_classtype_constraints (tree type)
{
if (!type || type == error_mark_node || TREE_CODE (type) != RECORD_TYPE)
return type;
// An explicit class template specialization has no template
// parameters.
if (!current_template_parms)
return type;
if (CLASSTYPE_IS_TEMPLATE (type) || CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
{
tree decl = TYPE_STUB_DECL (type);
tree ci = current_template_constraints ();
// An implicitly instantiated member template declaration already
// has associated constraints. If it is defined outside of its
// class, then we need match these constraints against those of
// original declaration.
if (tree orig_ci = get_constraints (decl))
{
if (!equivalent_constraints (ci, orig_ci))
{
// FIXME: Improve diagnostics.
error ("%qT does not match any declaration", type);
return error_mark_node;
}
return type;
}
set_constraints (decl, ci);
}
return type;
}
namespace {
// Create an empty constraint info block.
inline tree_constraint_info*
build_constraint_info ()
{
return (tree_constraint_info *)make_node (CONSTRAINT_INFO);
}
} // namespace
/* Build a constraint-info object that contains the associated constraints
of a declaration. This also includes the declaration's template
requirements (TREQS) and any trailing requirements for a function
declarator (DREQS). Note that both TREQS and DREQS must be constraints.
If the declaration has neither template nor declaration requirements
this returns NULL_TREE, indicating an unconstrained declaration. */
tree
build_constraints (tree tmpl_reqs, tree decl_reqs)
{
gcc_assert (tmpl_reqs ? constraint_p (tmpl_reqs) : true);
gcc_assert (decl_reqs ? constraint_p (decl_reqs) : true);
if (!tmpl_reqs && !decl_reqs)
return NULL_TREE;
tree_constraint_info* ci = build_constraint_info ();
ci->template_reqs = tmpl_reqs;
ci->declarator_reqs = decl_reqs;
ci->associated_constr = conjoin_constraints (tmpl_reqs, decl_reqs);
++processing_template_decl;
ci->normalized_constr = normalize_constraint (ci->associated_constr);
--processing_template_decl;
ci->assumptions = decompose_assumptions (ci->normalized_constr);
return (tree)ci;
}
namespace {
/* Returns true if any of the arguments in the template
argument list is a wildcard or wildcard pack. */
bool
contains_wildcard_p (tree args)
{
for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
{
tree arg = TREE_VEC_ELT (args, i);
if (TREE_CODE (arg) == WILDCARD_DECL)
return true;
}
return false;
}
/* Build a new call expression, but don't actually generate
a new function call. We just want the tree, not the
semantics. */
inline tree
build_call_check (tree id)
{
++processing_template_decl;
vec<tree, va_gc> *fargs = make_tree_vector();
tree call = finish_call_expr (id, &fargs, false, false, tf_none);
release_tree_vector (fargs);
--processing_template_decl;
return call;
}
/* Build an expression that will check a variable concept. If any
argument contains a wildcard, don't try to finish the variable
template because we can't substitute into a non-existent
declaration. */
tree
build_variable_check (tree id)
{
gcc_assert (TREE_CODE (id) == TEMPLATE_ID_EXPR);
if (contains_wildcard_p (TREE_OPERAND (id, 1)))
return id;
++processing_template_decl;
tree var = finish_template_variable (id);
--processing_template_decl;
return var;
}
/* Construct a sequence of template arguments by prepending
ARG to REST. Either ARG or REST may be null. */
tree
build_concept_check_arguments (tree arg, tree rest)
{
gcc_assert (rest ? TREE_CODE (rest) == TREE_VEC : true);
tree args;
if (arg)
{
int n = rest ? TREE_VEC_LENGTH (rest) : 0;
args = make_tree_vec (n + 1);
TREE_VEC_ELT (args, 0) = arg;
if (rest)
for (int i = 0; i < n; ++i)
TREE_VEC_ELT (args, i + 1) = TREE_VEC_ELT (rest, i);
int def = rest ? GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (rest) : 0;
SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args, def + 1);
}
else
{
gcc_assert (rest != NULL_TREE);
args = rest;
}
return args;
}
} // namespace
/* Construct an expression that checks the concept given by
TARGET. The TARGET must be:
- an OVERLOAD referring to one or more function concepts
- a BASELINK referring to an overload set of the above, or
- a TEMPLTATE_DECL referring to a variable concept.
ARG and REST are the explicit template arguments for the
eventual concept check. */
tree
build_concept_check (tree target, tree arg, tree rest)
{
tree args = build_concept_check_arguments (arg, rest);
if (variable_template_p (target))
return build_variable_check (lookup_template_variable (target, args));
else
return build_call_check (lookup_template_function (target, args));
}
/* Returns a TYPE_DECL that contains sufficient information to
build a template parameter of the same kind as PROTO and
constrained by the concept declaration CNC. Note that PROTO
is the first template parameter of CNC.
If specified, ARGS provides additional arguments to the
constraint check. */
tree
build_constrained_parameter (tree cnc, tree proto, tree args)
{
tree name = DECL_NAME (cnc);
tree type = TREE_TYPE (proto);
tree decl = build_decl (input_location, TYPE_DECL, name, type);
CONSTRAINED_PARM_PROTOTYPE (decl) = proto;
CONSTRAINED_PARM_CONCEPT (decl) = cnc;
CONSTRAINED_PARM_EXTRA_ARGS (decl) = args;
return decl;
}
/* Create a constraint expression for the given DECL that
evaluates the requirements specified by CONSTR, a TYPE_DECL
that contains all the information necessary to build the
requirements (see finish_concept_name for the layout of
that TYPE_DECL).
Note that the constraints are neither reduced nor decomposed.
That is done only after the requires clause has been parsed
(or not). */
tree
finish_shorthand_constraint (tree decl, tree constr)
{
/* No requirements means no constraints. */
if (!constr)
return NULL_TREE;
tree proto = CONSTRAINED_PARM_PROTOTYPE (constr);
tree con = CONSTRAINED_PARM_CONCEPT (constr);
tree args = CONSTRAINED_PARM_EXTRA_ARGS (constr);
/* If the parameter declaration is variadic, but the concept
is not then we need to apply the concept to every element
in the pack. */
bool is_proto_pack = template_parameter_pack_p (proto);
bool is_decl_pack = template_parameter_pack_p (decl);
bool apply_to_all_p = is_decl_pack && !is_proto_pack;
/* Get the argument and overload used for the requirement
and adjust it if we're going to expand later. */
tree arg = template_parm_to_arg (build_tree_list (NULL_TREE, decl));
if (apply_to_all_p)
arg = PACK_EXPANSION_PATTERN (TREE_VEC_ELT (ARGUMENT_PACK_ARGS (arg), 0));
/* Build the concept check. If it the constraint needs to be
applied to all elements of the parameter pack, then make
the constraint an expansion. */
tree check;
tree tmpl = DECL_TI_TEMPLATE (con);
if (TREE_CODE (con) == VAR_DECL)
{
check = build_concept_check (tmpl, arg, args);
}
else
{
tree ovl = build_overload (tmpl, NULL_TREE);
check = build_concept_check (ovl, arg, args);
}
/* Make the check a pack expansion if needed.
FIXME: We should be making a fold expression. */
if (apply_to_all_p)
{
check = make_pack_expansion (check);
TREE_TYPE (check) = boolean_type_node;
}
return make_predicate_constraint (check);
}
/* Returns a conjunction of shorthand requirements for the template
parameter list PARMS. Note that the requirements are stored in
the TYPE of each tree node. */
tree
get_shorthand_constraints (tree parms)
{
tree result = NULL_TREE;
parms = INNERMOST_TEMPLATE_PARMS (parms);
for (int i = 0; i < TREE_VEC_LENGTH (parms); ++i)
{
tree parm = TREE_VEC_ELT (parms, i);
tree constr = TEMPLATE_PARM_CONSTRAINTS (parm);
result = conjoin_constraints (result, constr);
}
return result;
}
// Returns and chains a new parameter for PARAMETER_LIST which will conform
// to the prototype given by SRC_PARM. The new parameter will have its
// identifier and location set according to IDENT and PARM_LOC respectively.
static tree
process_introduction_parm (tree parameter_list, tree src_parm)
{
// If we have a pack, we should have a single pack argument which is the
// placeholder we want to look at.
bool is_parameter_pack = ARGUMENT_PACK_P (src_parm);
if (is_parameter_pack)
src_parm = TREE_VEC_ELT (ARGUMENT_PACK_ARGS (src_parm), 0);
// At this point we should have a wildcard, but we want to
// grab the associated decl from it. Also grab the stored
// identifier and location that should be chained to it in
// a PARM_DECL.
gcc_assert (TREE_CODE (src_parm) == WILDCARD_DECL);
tree ident = DECL_NAME (src_parm);
location_t parm_loc = DECL_SOURCE_LOCATION (src_parm);
// If we expect a pack and the deduced template is not a pack, or if the
// template is using a pack and we didn't declare a pack, throw an error.
if (is_parameter_pack != WILDCARD_PACK_P (src_parm))
{
error_at (parm_loc, "cannot match pack for introduced parameter");
tree err_parm = build_tree_list (error_mark_node, error_mark_node);
return chainon (parameter_list, err_parm);
}
src_parm = TREE_TYPE (src_parm);
tree parm;
bool is_non_type;
if (TREE_CODE (src_parm) == TYPE_DECL)
{
is_non_type = false;
parm = finish_template_type_parm (class_type_node, ident);
}
else if (TREE_CODE (src_parm) == TEMPLATE_DECL)
{
is_non_type = false;
begin_template_parm_list ();
current_template_parms = DECL_TEMPLATE_PARMS (src_parm);
end_template_parm_list ();
parm = finish_template_template_parm (class_type_node, ident);
}
else
{
is_non_type = true;
// Since we don't have a declarator, so we can copy the source
// parameter and change the name and eventually the location.
parm = copy_decl (src_parm);
DECL_NAME (parm) = ident;
}
// Wrap in a TREE_LIST for process_template_parm. Introductions do not
// retain the defaults from the source template.
parm = build_tree_list (NULL_TREE, parm);
return process_template_parm (parameter_list, parm_loc, parm,
is_non_type, is_parameter_pack);
}
/* Associates a constraint check to the current template based
on the introduction parameters. INTRO_LIST must be a TREE_VEC
of WILDCARD_DECLs containing a chained PARM_DECL which
contains the identifier as well as the source location.
TMPL_DECL is the decl for the concept being used. If we
take a concept, C, this will form a check in the form of
C<INTRO_LIST> filling in any extra arguments needed by the
defaults deduced.
Returns NULL_TREE if no concept could be matched and
error_mark_node if an error occurred when matching. */
tree
finish_template_introduction (tree tmpl_decl, tree intro_list)
{
/* Deduce the concept check. */
tree expr = build_concept_check (tmpl_decl, NULL_TREE, intro_list);
if (expr == error_mark_node)
return NULL_TREE;
tree parms = deduce_concept_introduction (expr);
if (!parms)
return NULL_TREE;
/* Build template parameter scope for introduction. */
tree parm_list = NULL_TREE;
begin_template_parm_list ();
int nargs = MIN (TREE_VEC_LENGTH (parms), TREE_VEC_LENGTH (intro_list));
for (int n = 0; n < nargs; ++n)
parm_list = process_introduction_parm (parm_list, TREE_VEC_ELT (parms, n));
parm_list = end_template_parm_list (parm_list);
for (int i = 0; i < TREE_VEC_LENGTH (parm_list); ++i)
if (TREE_VALUE (TREE_VEC_ELT (parm_list, i)) == error_mark_node)
{
end_template_decl ();
return error_mark_node;
}
/* Build a concept check for our constraint. */
tree check_args = make_tree_vec (TREE_VEC_LENGTH (parms));
int n = 0;
for (; n < TREE_VEC_LENGTH (parm_list); ++n)
{
tree parm = TREE_VEC_ELT (parm_list, n);
TREE_VEC_ELT (check_args, n) = template_parm_to_arg (parm);
}
SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (check_args, n);
/* If the template expects more parameters we should be able
to use the defaults from our deduced concept. */
for (; n < TREE_VEC_LENGTH (parms); ++n)
TREE_VEC_ELT (check_args, n) = TREE_VEC_ELT (parms, n);
/* Associate the constraint. */
tree check = build_concept_check (tmpl_decl, NULL_TREE, check_args);
tree constr = make_predicate_constraint (check);
TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = constr;
return parm_list;
}
/* Given the predicate constraint T from a constrained-type-specifier, extract
its TMPL and ARGS. FIXME why do we need two different forms of
constrained-type-specifier? */
void
placeholder_extract_concept_and_args (tree t, tree &tmpl, tree &args)
{
if (TREE_CODE (t) == TYPE_DECL)
{
/* A constrained parameter. */
tmpl = DECL_TI_TEMPLATE (CONSTRAINED_PARM_CONCEPT (t));
args = CONSTRAINED_PARM_EXTRA_ARGS (t);
return;
}
gcc_assert (TREE_CODE (t) == PRED_CONSTR);
t = PRED_CONSTR_EXPR (t);
gcc_assert (TREE_CODE (t) == CALL_EXPR
|| TREE_CODE (t) == TEMPLATE_ID_EXPR
|| VAR_P (t));
if (TREE_CODE (t) == CALL_EXPR)
t = CALL_EXPR_FN (t);
if (TREE_CODE (t) == TEMPLATE_ID_EXPR)
{
tmpl = TREE_OPERAND (t, 0);
if (TREE_CODE (tmpl) == OVERLOAD)
{
gcc_assert (OVL_CHAIN (tmpl) == NULL_TREE);
tmpl = OVL_FUNCTION (tmpl);
}
args = TREE_OPERAND (t, 1);
}
else if (DECL_P (t))
{
tmpl = DECL_TI_TEMPLATE (t);
args = DECL_TI_ARGS (t);
}
else
gcc_unreachable ();
}
/* Returns true iff the placeholders C1 and C2 are equivalent. C1
and C2 can be either PRED_CONSTR_EXPR or TEMPLATE_TYPE_PARM. */
bool
equivalent_placeholder_constraints (tree c1, tree c2)
{
if (c1 && TREE_CODE (c1) == TEMPLATE_TYPE_PARM)
/* A constrained auto. */
c1 = PLACEHOLDER_TYPE_CONSTRAINTS (c1);
if (c2 && TREE_CODE (c2) == TEMPLATE_TYPE_PARM)
c2 = PLACEHOLDER_TYPE_CONSTRAINTS (c2);
if (c1 == c2)
return true;
if (!c1 || !c2)
return false;
tree t1, t2, a1, a2;
placeholder_extract_concept_and_args (c1, t1, a1);
placeholder_extract_concept_and_args (c2, t2, a2);
if (t1 != t2)
return false;
/* Skip the first argument to avoid infinite recursion on the
placeholder auto itself. */
bool skip1 = (TREE_CODE (c1) == PRED_CONSTR);
bool skip2 = (TREE_CODE (c2) == PRED_CONSTR);
int len1 = (a1 ? TREE_VEC_LENGTH (a1) : 0) - skip1;
int len2 = (a2 ? TREE_VEC_LENGTH (a2) : 0) - skip2;
if (len1 != len2)
return false;
for (int i = 0; i < len1; ++i)
if (!cp_tree_equal (TREE_VEC_ELT (a1, i + skip1),
TREE_VEC_ELT (a2, i + skip2)))
return false;
return true;
}
/* Return a hash value for the placeholder PRED_CONSTR C. */
hashval_t
hash_placeholder_constraint (tree c)
{
tree t, a;
placeholder_extract_concept_and_args (c, t, a);
/* Like hash_tmpl_and_args, but skip the first argument. */
hashval_t val = iterative_hash_object (DECL_UID (t), 0);
for (int i = TREE_VEC_LENGTH (a)-1; i > 0; --i)
val = iterative_hash_template_arg (TREE_VEC_ELT (a, i), val);
return val;
}
/*---------------------------------------------------------------------------
Constraint substitution
---------------------------------------------------------------------------*/
/* The following functions implement substitution rules for constraints.
Substitution without checking constraints happens only in the
instantiation of class templates. For example:
template<C1 T> struct S {
void f(T) requires C2<T>;
void g(T) requires T::value;
};
S<int> s; // error instantiating S<int>::g(T)
When we instantiate S, we substitute into its member declarations,
including their constraints. However, those constraints are not
checked. Substituting int into C2<T> yields C2<int>, and substituting
into T::value yields a substitution failure, making the program
ill-formed.
Note that we only ever substitute into the associated constraints
of a declaration. That is, substitution is defined only for predicate
constraints and conjunctions. */
/* Substitute into the predicate constraints. Returns error_mark_node
if the substitution into the expression fails. */
tree
tsubst_predicate_constraint (tree t, tree args,
tsubst_flags_t complain, tree in_decl)
{
tree expr = PRED_CONSTR_EXPR (t);
++processing_template_decl;
tree result = tsubst_expr (expr, args, complain, in_decl, false);
--processing_template_decl;
return build_nt (PRED_CONSTR, result);
}
/* Substitute into the conjunction of constraints. Returns
error_mark_node if substitution into either operand fails. */
tree
tsubst_conjunction (tree t, tree args,
tsubst_flags_t complain, tree in_decl)
{
tree t0 = TREE_OPERAND (t, 0);
tree r0 = tsubst_constraint (t0, args, complain, in_decl);
tree t1 = TREE_OPERAND (t, 1);
tree r1 = tsubst_constraint (t1, args, complain, in_decl);
return build_nt (CONJ_CONSTR, r0, r1);
}
/* Substitute ARGS into the constraint T. */
tree
tsubst_constraint (tree t, tree args, tsubst_flags_t complain, tree in_decl)
{
if (t == NULL_TREE)
return t;
if (TREE_CODE (t) == CONJ_CONSTR)
return tsubst_conjunction (t, args, complain, in_decl);
else if (TREE_CODE (t) == PRED_CONSTR)
return tsubst_predicate_constraint (t, args, complain, in_decl);
else
gcc_unreachable ();
return error_mark_node;
}
namespace {
/* A subroutine of tsubst_constraint_variables. Register local
specializations for each of parameter in PARMS and its
corresponding substituted constraint variable in VARS.
Returns VARS. */
tree
declare_constraint_vars (tree parms, tree vars)
{
tree s = vars;
for (tree t = parms; t; t = DECL_CHAIN (t))
{
if (DECL_PACK_P (t))
{
tree pack = extract_fnparm_pack (t, &s);
register_local_specialization (pack, t);
}
else
{
register_local_specialization (s, t);
s = DECL_CHAIN (s);
}
}
return vars;
}
/* A subroutine of tsubst_parameterized_constraint. Substitute ARGS
into the parameter list T, producing a sequence of constraint
variables, declared in the current scope.
Note that the caller must establish a local specialization stack
prior to calling this function since this substitution will
declare the substituted parameters. */
tree
tsubst_constraint_variables (tree t, tree args,
tsubst_flags_t complain, tree in_decl)
{
/* Clear cp_unevaluated_operand across tsubst so that we get a proper chain
of PARM_DECLs. */
int saved_unevaluated_operand = cp_unevaluated_operand;
cp_unevaluated_operand = 0;
tree vars = tsubst (t, args, complain, in_decl);
cp_unevaluated_operand = saved_unevaluated_operand;
if (vars == error_mark_node)
return error_mark_node;
return declare_constraint_vars (t, vars);
}
/* Substitute ARGS into the simple requirement T. Note that
substitution may result in an ill-formed expression without
causing the program to be ill-formed. In such cases, the
requirement wraps an error_mark_node. */
inline tree
tsubst_simple_requirement (tree t, tree args,
tsubst_flags_t complain, tree in_decl)
{
++processing_template_decl;
tree expr = tsubst_expr (TREE_OPERAND (t, 0), args, complain, in_decl, false);
--processing_template_decl;
return finish_simple_requirement (expr);
}
/* Substitute ARGS into the type requirement T. Note that
substitution may result in an ill-formed type without
causing the program to be ill-formed. In such cases, the
requirement wraps an error_mark_node. */
inline tree
tsubst_type_requirement (tree t, tree args,
tsubst_flags_t complain, tree in_decl)
{
++processing_template_decl;
tree type = tsubst (TREE_OPERAND (t, 0), args, complain, in_decl);
--processing_template_decl;
return finish_type_requirement (type);
}
/* Substitute args into the compound requirement T. If substituting
into either the expression or the type fails, the corresponding
operands in the resulting node will be error_mark_node. This
preserves a requirement for the purpose of partial ordering, but
it will never be satisfied. */
tree
tsubst_compound_requirement (tree t, tree args,
tsubst_flags_t complain, tree in_decl)
{
++processing_template_decl;
tree expr = tsubst_expr (TREE_OPERAND (t, 0), args, complain, in_decl, false);
tree type = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
--processing_template_decl;
bool noexcept_p = COMPOUND_REQ_NOEXCEPT_P (t);
return finish_compound_requirement (expr, type, noexcept_p);
}
/* Substitute ARGS into the nested requirement T. */
tree
tsubst_nested_requirement (tree t, tree args,
tsubst_flags_t complain, tree in_decl)
{
++processing_template_decl;
tree expr = tsubst_expr (TREE_OPERAND (t, 0), args, complain, in_decl, false);
--processing_template_decl;
return finish_nested_requirement (expr);
}
inline tree
tsubst_requirement (tree t, tree args, tsubst_flags_t complain, tree in_decl)
{
switch (TREE_CODE (t))
{
case SIMPLE_REQ:
return tsubst_simple_requirement (t, args, complain, in_decl);
case TYPE_REQ:
return tsubst_type_requirement (t, args, complain, in_decl);
case COMPOUND_REQ:
return tsubst_compound_requirement (t, args, complain, in_decl);
case NESTED_REQ:
return tsubst_nested_requirement (t, args, complain, in_decl);
default:
gcc_unreachable ();
}
return error_mark_node;
}
/* Substitute ARGS into the list of requirements T. Note that
substitution failures here result in ill-formed programs. */
tree
tsubst_requirement_body (tree t, tree args,
tsubst_flags_t complain, tree in_decl)
{
tree r = NULL_TREE;
while (t)
{
tree e = tsubst_requirement (TREE_VALUE (t), args, complain, in_decl);
if (e == error_mark_node)
return error_mark_node;
r = tree_cons (NULL_TREE, e, r);
t = TREE_CHAIN (t);
}
return r;
}
} /* namespace */
/* Substitute ARGS into the requires expression T. Note that this
results in the re-declaration of local parameters when
substituting through the parameter list. If either substitution
fails, the program is ill-formed. */
tree
tsubst_requires_expr (tree t, tree args,
tsubst_flags_t complain, tree in_decl)
{
local_specialization_stack stack;
tree parms = TREE_OPERAND (t, 0);
if (parms)
{
parms = tsubst_constraint_variables (parms, args, complain, in_decl);
if (parms == error_mark_node)
return error_mark_node;
}
tree reqs = TREE_OPERAND (t, 1);
reqs = tsubst_requirement_body (reqs, args, complain, in_decl);
if (reqs == error_mark_node)
return error_mark_node;
return finish_requires_expr (parms, reqs);
}
/* Substitute ARGS into the constraint information CI, producing a new
constraint record. */
tree
tsubst_constraint_info (tree t, tree args,
tsubst_flags_t complain, tree in_decl)
{
if (!t || t == error_mark_node || !check_constraint_info (t))
return NULL_TREE;
tree tmpl_constr = NULL_TREE;
if (tree r = CI_TEMPLATE_REQS (t))
tmpl_constr = tsubst_constraint (r, args, complain, in_decl);
tree decl_constr = NULL_TREE;
if (tree r = CI_DECLARATOR_REQS (t))
decl_constr = tsubst_constraint (r, args, complain, in_decl);
return build_constraints (tmpl_constr, decl_constr);
}
/*---------------------------------------------------------------------------
Constraint satisfaction
---------------------------------------------------------------------------*/
/* The following functions determine if a constraint, when
substituting template arguments, is satisfied. For convenience,
satisfaction reduces a constraint to either true or false (and
nothing else). */
namespace {
tree satisfy_constraint_1 (tree, tree, tsubst_flags_t, tree);
/* Check the constraint pack expansion. */
tree
satisfy_pack_expansion (tree t, tree args,
tsubst_flags_t complain, tree in_decl)
{
/* Get the vector of satisfaction results.
gen_elem_of_pack_expansion_instantiation will check that each element of
the expansion is satisfied. */
tree exprs = tsubst_pack_expansion (t, args, complain, in_decl);
if (exprs == error_mark_node)
return boolean_false_node;
int n = TREE_VEC_LENGTH (exprs);
for (int i = 0; i < n; ++i)
if (TREE_VEC_ELT (exprs, i) != boolean_true_node)
return boolean_false_node;
return boolean_true_node;
}
/* A predicate constraint is satisfied if its expression evaluates
to true. If substitution into that node fails, the constraint
is not satisfied ([temp.constr.pred]).
Note that a predicate constraint is a constraint expression
of type bool. If neither of those are true, the program is
ill-formed; they are not SFINAE'able errors. */
tree
satisfy_predicate_constraint (tree t, tree args,
tsubst_flags_t complain, tree in_decl)
{
tree original = TREE_OPERAND (t, 0);
/* We should never have a naked pack expansion in a predicate constraint. */
gcc_assert (TREE_CODE (original) != EXPR_PACK_EXPANSION);
tree expr = tsubst_expr (original, args, complain, in_decl, false);
if (expr == error_mark_node)
return boolean_false_node;
/* A predicate constraint shall have type bool. In some
cases, substitution gives us const-qualified bool, which
is also acceptable. */
tree type = cv_unqualified (TREE_TYPE (expr));
if (!same_type_p (type, boolean_type_node))
{
error_at (EXPR_LOC_OR_LOC (expr, input_location),
"constraint %qE does not have type %qT",
expr, boolean_type_node);
return boolean_false_node;
}
tree value = cxx_constant_value (expr);
return value;
}
/* Check an expression constraint. The constraint is satisfied if
substitution succeeds ([temp.constr.expr]).
Note that the expression is unevaluated. */
tree
satisfy_expression_constraint (tree t, tree args,
tsubst_flags_t complain, tree in_decl)
{
cp_unevaluated guard;
deferring_access_check_sentinel deferring;
tree expr = EXPR_CONSTR_EXPR (t);
tree check = tsubst_expr (expr, args, complain, in_decl, false);
if (check == error_mark_node)
return boolean_false_node;
if (!perform_deferred_access_checks (tf_none))
return boolean_false_node;
return boolean_true_node;
}
/* Check a type constraint. The constraint is satisfied if
substitution succeeds. */
inline tree
satisfy_type_constraint (tree t, tree args,
tsubst_flags_t complain, tree in_decl)
{
deferring_access_check_sentinel deferring;
tree type = TYPE_CONSTR_TYPE (t);
gcc_assert (TYPE_P (type) || type == error_mark_node);
tree check = tsubst (type, args, complain, in_decl);
if (error_operand_p (check))
return boolean_false_node;
if (!perform_deferred_access_checks (complain))
return boolean_false_node;
return boolean_true_node;
}
/* Check an implicit conversion constraint. */
tree
satisfy_implicit_conversion_constraint (tree t, tree args,
tsubst_flags_t complain, tree in_decl)
{
/* Don't tsubst as if we're processing a template. If we try
to we can end up generating template-like expressions
(e.g., modop-exprs) that aren't properly typed. */
tree expr =
tsubst_expr (ICONV_CONSTR_EXPR (t), args, complain, in_decl, false);
if (expr == error_mark_node)
return boolean_false_node;
/* Get the transformed target type. */
tree type = tsubst (ICONV_CONSTR_TYPE (t), args, complain, in_decl);
if (type == error_mark_node)
return boolean_false_node;
/* Attempt the conversion as a direct initialization
of the form TYPE <unspecified> = EXPR. */
tree conv =
perform_direct_initialization_if_possible (type, expr, false, complain);
if (conv == NULL_TREE || conv == error_mark_node)
return boolean_false_node;
else
return boolean_true_node;
}
/* Check an argument deduction constraint. */
tree
satisfy_argument_deduction_constraint (tree t, tree args,
tsubst_flags_t complain, tree in_decl)
{
/* Substitute through the expression. */
tree expr = DEDUCT_CONSTR_EXPR (t);
tree init = tsubst_expr (expr, args, complain, in_decl, false);
if (expr == error_mark_node)
return boolean_false_node;
/* Perform auto or decltype(auto) deduction to get the result. */
tree pattern = DEDUCT_CONSTR_PATTERN (t);
tree placeholder = DEDUCT_CONSTR_PLACEHOLDER (t);
tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (placeholder);
tree type_canonical = TYPE_CANONICAL (placeholder);
PLACEHOLDER_TYPE_CONSTRAINTS (placeholder)
= tsubst_constraint (constr, args, complain|tf_partial, in_decl);
TYPE_CANONICAL (placeholder) = NULL_TREE;
tree type = do_auto_deduction (pattern, init, placeholder,
complain, adc_requirement);
PLACEHOLDER_TYPE_CONSTRAINTS (placeholder) = constr;
TYPE_CANONICAL (placeholder) = type_canonical;
if (type == error_mark_node)
return boolean_false_node;
return boolean_true_node;
}
/* Check an exception constraint. An exception constraint for an
expression e is satisfied when noexcept(e) is true. */
tree
satisfy_exception_constraint (tree t, tree args,
tsubst_flags_t complain, tree in_decl)
{
tree expr = EXCEPT_CONSTR_EXPR (t);
tree check = tsubst_expr (expr, args, complain, in_decl, false);
if (check == error_mark_node)
return boolean_false_node;
if (expr_noexcept_p (check, complain))
return boolean_true_node;
else
return boolean_false_node;
}
/* Check a parameterized constraint. */
tree
satisfy_parameterized_constraint (tree t, tree args,
tsubst_flags_t complain, tree in_decl)
{
local_specialization_stack stack;
tree parms = PARM_CONSTR_PARMS (t);
tree vars = tsubst_constraint_variables (parms, args, complain, in_decl);
if (vars == error_mark_node)
return boolean_false_node;
tree constr = PARM_CONSTR_OPERAND (t);
return satisfy_constraint_1 (constr, args, complain, in_decl);
}
/* Check that the conjunction of constraints is satisfied. Note
that if left operand is not satisfied, the right operand
is not checked.
FIXME: Check that this wouldn't result in a user-defined
operator. Note that this error is partially diagnosed in
satisfy_predicate_constraint. It would be nice to diagnose
the overload, but I don't think it's strictly necessary. */
tree
satisfy_conjunction (tree t, tree args, tsubst_flags_t complain, tree in_decl)
{
tree t0 = satisfy_constraint_1 (TREE_OPERAND (t, 0), args, complain, in_decl);
if (t0 == boolean_false_node)
return t0;
tree t1 = satisfy_constraint_1 (TREE_OPERAND (t, 1), args, complain, in_decl);
if (t1 == boolean_false_node)
return t1;
return boolean_true_node;
}
/* Check that the disjunction of constraints is satisfied. Note
that if the left operand is satisfied, the right operand is not
checked. */
tree
satisfy_disjunction (tree t, tree args, tsubst_flags_t complain, tree in_decl)
{
tree t0 = satisfy_constraint_1 (TREE_OPERAND (t, 0), args, complain, in_decl);
if (t0 == boolean_true_node)
return boolean_true_node;
tree t1 = satisfy_constraint_1 (TREE_OPERAND (t, 1), args, complain, in_decl);
if (t1 == boolean_true_node)
return boolean_true_node;
return boolean_false_node;
}
/* Dispatch to an appropriate satisfaction routine depending on the
tree code of T. */
tree
satisfy_constraint_1 (tree t, tree args, tsubst_flags_t complain, tree in_decl)
{
gcc_assert (!processing_template_decl);
if (!t)
return boolean_false_node;
if (t == error_mark_node)
return boolean_false_node;
switch (TREE_CODE (t))
{
case PRED_CONSTR:
return satisfy_predicate_constraint (t, args, complain, in_decl);
case EXPR_CONSTR:
return satisfy_expression_constraint (t, args, complain, in_decl);
case TYPE_CONSTR:
return satisfy_type_constraint (t, args, complain, in_decl);
case ICONV_CONSTR:
return satisfy_implicit_conversion_constraint (t, args, complain, in_decl);
case DEDUCT_CONSTR:
return satisfy_argument_deduction_constraint (t, args, complain, in_decl);
case EXCEPT_CONSTR:
return satisfy_exception_constraint (t, args, complain, in_decl);
case PARM_CONSTR:
return satisfy_parameterized_constraint (t, args, complain, in_decl);
case CONJ_CONSTR:
return satisfy_conjunction (t, args, complain, in_decl);
case DISJ_CONSTR:
return satisfy_disjunction (t, args, complain, in_decl);
case EXPR_PACK_EXPANSION:
return satisfy_pack_expansion (t, args, complain, in_decl);
default:
gcc_unreachable ();
}
return boolean_false_node;
}
/* Check that the constraint is satisfied, according to the rules
for that constraint. Note that each satisfy_* function returns
true or false, depending on whether it is satisfied or not. */
tree
satisfy_constraint (tree t, tree args)
{
/* Turn off template processing. Constraint satisfaction only applies
to non-dependent terms, so we want full checking here. */
processing_template_decl_sentinel sentinel (true);
/* Avoid early exit in tsubst and tsubst_copy from null args; since earlier
substitution was done with processing_template_decl forced on, there will
be expressions that still need semantic processing, possibly buried in
decltype or a template argument. */
if (args == NULL_TREE)
args = make_tree_vec (1);
return satisfy_constraint_1 (t, args, tf_none, NULL_TREE);
}
/* Check the associated constraints in CI against the given
ARGS, returning true when the constraints are satisfied
and false otherwise. */
tree
satisfy_associated_constraints (tree ci, tree args)
{
/* If there are no constraints then this is trivially satisfied. */
if (!ci)
return boolean_true_node;
/* If any arguments depend on template parameters, we can't
check constraints. */
if (args && uses_template_parms (args))
return boolean_true_node;
/* Invalid requirements cannot be satisfied. */
if (!valid_constraints_p (ci))
return boolean_false_node;
return satisfy_constraint (CI_NORMALIZED_CONSTRAINTS (ci), args);
}
} /* namespace */
/* Evaluate the given constraint, returning boolean_true_node
if the constraint is satisfied and boolean_false_node
otherwise. */
tree
evaluate_constraints (tree constr, tree args)
{
gcc_assert (constraint_p (constr));
return satisfy_constraint (normalize_constraint (constr), args);
}
/* Evaluate the function concept FN by substituting its own args
into its definition and evaluating that as the result. Returns
boolean_true_node if the constraints are satisfied and
boolean_false_node otherwise. */
tree
evaluate_function_concept (tree fn, tree args)
{
++processing_template_decl;
/* We lift using DECL_TI_ARGS because we want to delay producing
non-dependent expressions until we're doing satisfaction. We can't just
go without any substitution because we need to lower the level of 'auto's
in type deduction constraints. */
tree constr = transform_expression (lift_function_definition
(fn, DECL_TI_ARGS (fn)));
--processing_template_decl;
return satisfy_constraint (constr, args);
}
/* Evaluate the variable concept VAR by substituting its own args into
its initializer and checking the resulting constraint. Returns
boolean_true_node if the constraints are satisfied and
boolean_false_node otherwise. */
tree
evaluate_variable_concept (tree decl, tree args)
{
++processing_template_decl;
tree constr = transform_expression (lift_variable_initializer
(decl, DECL_TI_ARGS (decl)));
--processing_template_decl;
return satisfy_constraint (constr, args);
}
/* Evaluate the given expression as if it were a predicate
constraint. Returns boolean_true_node if the constraint
is satisfied and boolean_false_node otherwise. */
tree
evaluate_constraint_expression (tree expr, tree args)
{
++processing_template_decl;
tree constr = transform_expression (lift_expression (expr));
--processing_template_decl;
return satisfy_constraint (constr, args);
}
/* Returns true if the DECL's constraints are satisfied.
This is used in cases where a declaration is formed but
before it is used (e.g., overload resolution). */
bool
constraints_satisfied_p (tree decl)
{
/* Get the constraints to check for satisfaction. This depends
on whether we're looking at a template specialization or not. */
tree ci;
tree args = NULL_TREE;
if (tree ti = DECL_TEMPLATE_INFO (decl))
{
ci = get_constraints (TI_TEMPLATE (ti));
args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (ti));
}
else
{
ci = get_constraints (decl);
}
tree eval = satisfy_associated_constraints (ci, args);
return eval == boolean_true_node;
}
/* Returns true if the constraints are satisfied by ARGS.
Here, T can be either a constraint or a constrained
declaration. */
bool
constraints_satisfied_p (tree t, tree args)
{
tree eval;
if (constraint_p (t))
eval = evaluate_constraints (t, args);
else
eval = satisfy_associated_constraints (get_constraints (t), args);
return eval == boolean_true_node;
}
namespace
{
/* Normalize EXPR and determine if the resulting constraint is
satisfied by ARGS. Returns true if and only if the constraint
is satisfied. This is used extensively by diagnostics to
determine causes for failure. */
inline bool
constraint_expression_satisfied_p (tree expr, tree args)
{
return evaluate_constraint_expression (expr, args) == boolean_true_node;
}
} /* namespace */
/*---------------------------------------------------------------------------
Semantic analysis of requires-expressions
---------------------------------------------------------------------------*/
/* Finish a requires expression for the given PARMS (possibly
null) and the non-empty sequence of requirements. */
tree
finish_requires_expr (tree parms, tree reqs)
{
/* Modify the declared parameters by removing their context
so they don't refer to the enclosing scope and explicitly
indicating that they are constraint variables. */
for (tree parm = parms; parm; parm = DECL_CHAIN (parm))
{
DECL_CONTEXT (parm) = NULL_TREE;
CONSTRAINT_VAR_P (parm) = true;
}
/* Build the node. */
tree r = build_min (REQUIRES_EXPR, boolean_type_node, parms, reqs);
TREE_SIDE_EFFECTS (r) = false;
TREE_CONSTANT (r) = true;
return r;
}
/* Construct a requirement for the validity of EXPR. */
tree
finish_simple_requirement (tree expr)
{
return build_nt (SIMPLE_REQ, expr);
}
/* Construct a requirement for the validity of TYPE. */
tree
finish_type_requirement (tree type)
{
return build_nt (TYPE_REQ, type);
}
/* Construct a requirement for the validity of EXPR, along with
its properties. if TYPE is non-null, then it specifies either
an implicit conversion or argument deduction constraint,
depending on whether any placeholders occur in the type name.
NOEXCEPT_P is true iff the noexcept keyword was specified. */
tree
finish_compound_requirement (tree expr, tree type, bool noexcept_p)
{
tree req = build_nt (COMPOUND_REQ, expr, type);
COMPOUND_REQ_NOEXCEPT_P (req) = noexcept_p;
return req;
}
/* Finish a nested requirement. */
tree
finish_nested_requirement (tree expr)
{
return build_nt (NESTED_REQ, expr);
}
// Check that FN satisfies the structural requirements of a
// function concept definition.
tree
check_function_concept (tree fn)
{
// Check that the function is comprised of only a single
// return statement.
tree body = DECL_SAVED_TREE (fn);
if (TREE_CODE (body) == BIND_EXPR)
body = BIND_EXPR_BODY (body);
// Sometimes a function call results in the creation of clean up
// points. Allow these to be preserved in the body of the
// constraint, as we might actually need them for some constexpr
// evaluations.
if (TREE_CODE (body) == CLEANUP_POINT_EXPR)
body = TREE_OPERAND (body, 0);
/* Check that the definition is written correctly. */
if (TREE_CODE (body) != RETURN_EXPR)
{
location_t loc = DECL_SOURCE_LOCATION (fn);
if (TREE_CODE (body) == STATEMENT_LIST && !STATEMENT_LIST_HEAD (body))
error_at (loc, "definition of concept %qD is empty", fn);
else
error_at (loc, "definition of concept %qD has multiple statements", fn);
}
return NULL_TREE;
}
// Check that a constrained friend declaration function declaration,
// FN, is admissible. This is the case only when the declaration depends
// on template parameters and does not declare a specialization.
void
check_constrained_friend (tree fn, tree reqs)
{
if (fn == error_mark_node)
return;
gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
// If there are not constraints, this cannot be an error.
if (!reqs)
return;
// Constrained friend functions that don't depend on template
// arguments are effectively meaningless.
if (!uses_template_parms (TREE_TYPE (fn)))
{
error_at (location_of (fn),
"constrained friend does not depend on template parameters");
return;
}
}
/*---------------------------------------------------------------------------
Equivalence of constraints
---------------------------------------------------------------------------*/
/* Returns true when A and B are equivalent constraints. */
bool
equivalent_constraints (tree a, tree b)
{
gcc_assert (!a || TREE_CODE (a) == CONSTRAINT_INFO);
gcc_assert (!b || TREE_CODE (b) == CONSTRAINT_INFO);
return cp_tree_equal (a, b);
}
/* Returns true if the template declarations A and B have equivalent
constraints. This is the case when A's constraints subsume B's and
when B's also constrain A's. */
bool
equivalently_constrained (tree d1, tree d2)
{
gcc_assert (TREE_CODE (d1) == TREE_CODE (d2));
return equivalent_constraints (get_constraints (d1), get_constraints (d2));
}
/*---------------------------------------------------------------------------
Partial ordering of constraints
---------------------------------------------------------------------------*/
/* Returns true when the the constraints in A subsume those in B. */
bool
subsumes_constraints (tree a, tree b)
{
gcc_assert (!a || TREE_CODE (a) == CONSTRAINT_INFO);
gcc_assert (!b || TREE_CODE (b) == CONSTRAINT_INFO);
return subsumes (a, b);
}
/* Returns true when the the constraints in A subsume those in B, but
the constraints in B do not subsume the constraints in A. */
bool
strictly_subsumes (tree a, tree b)
{
return subsumes (a, b) && !subsumes (b, a);
}
/* Determines which of the declarations, A or B, is more constrained.
That is, which declaration's constraints subsume but are not subsumed
by the other's?
Returns 1 if A is more constrained than B, -1 if B is more constrained
than A, and 0 otherwise. */
int
more_constrained (tree d1, tree d2)
{
tree c1 = get_constraints (d1);
tree c2 = get_constraints (d2);
int winner = 0;
if (subsumes_constraints (c1, c2))
++winner;
if (subsumes_constraints (c2, c1))
--winner;
return winner;
}
/* Returns true if D1 is at least as constrained as D2. That is, the
associated constraints of D1 subsume those of D2, or both declarations
are unconstrained. */
bool
at_least_as_constrained (tree d1, tree d2)
{
tree c1 = get_constraints (d1);
tree c2 = get_constraints (d2);
return subsumes_constraints (c1, c2);
}
/*---------------------------------------------------------------------------
Constraint diagnostics
---------------------------------------------------------------------------*/
/* The diagnosis of constraints performs a combination of
normalization and satisfaction testing. We recursively
walk through the conjunction (or disjunctions) of associated
constraints, testing each sub-expression in turn.
We currently restrict diagnostics to just the top-level
conjunctions within the associated constraints. A fully
recursive walk is possible, but it can generate a lot
of errors. */
namespace {
void diagnose_expression (location_t, tree, tree);
void diagnose_constraint (location_t, tree, tree);
/* Diagnose a conjunction of constraints. */
void
diagnose_logical_operation (location_t loc, tree t, tree args)
{
diagnose_expression (loc, TREE_OPERAND (t, 0), args);
diagnose_expression (loc, TREE_OPERAND (t, 0), args);
}
/* Determine if the trait expression T is satisfied by ARGS.
Emit a precise diagnostic if it is not. */
void
diagnose_trait_expression (location_t loc, tree t, tree args)
{
if (constraint_expression_satisfied_p (t, args))
return;
/* Rebuild the trait expression so we can diagnose the
specific failure. */
++processing_template_decl;
tree expr = tsubst_expr (t, args, tf_none, NULL_TREE, false);
--processing_template_decl;
tree t1 = TRAIT_EXPR_TYPE1 (expr);
tree t2 = TRAIT_EXPR_TYPE2 (expr);
switch (TRAIT_EXPR_KIND (t))
{
case CPTK_HAS_NOTHROW_ASSIGN:
inform (loc, " %qT is not nothrow copy assignable", t1);
break;
case CPTK_HAS_NOTHROW_CONSTRUCTOR:
inform (loc, " %qT is not nothrow default constructible", t1);
break;
case CPTK_HAS_NOTHROW_COPY:
inform (loc, " %qT is not nothrow copy constructible", t1);
break;
case CPTK_HAS_TRIVIAL_ASSIGN:
inform (loc, " %qT is not trivially copy assignable", t1);
break;
case CPTK_HAS_TRIVIAL_CONSTRUCTOR:
inform (loc, " %qT is not trivially default constructible", t1);
break;
case CPTK_HAS_TRIVIAL_COPY:
inform (loc, " %qT is not trivially copy constructible", t1);
break;
case CPTK_HAS_TRIVIAL_DESTRUCTOR:
inform (loc, " %qT is not trivially destructible", t1);
break;
case CPTK_HAS_VIRTUAL_DESTRUCTOR:
inform (loc, " %qT does not have a virtual destructor", t1);
break;
case CPTK_IS_ABSTRACT:
inform (loc, " %qT is not an abstract class", t1);
break;
case CPTK_IS_BASE_OF:
inform (loc, " %qT is not a base of %qT", t1, t2);
break;
case CPTK_IS_CLASS:
inform (loc, " %qT is not a class", t1);
break;
case CPTK_IS_EMPTY:
inform (loc, " %qT is not an empty class", t1);
break;
case CPTK_IS_ENUM:
inform (loc, " %qT is not an enum", t1);
break;
case CPTK_IS_FINAL:
inform (loc, " %qT is not a final class", t1);
break;
case CPTK_IS_LITERAL_TYPE:
inform (loc, " %qT is not a literal type", t1);
break;
case CPTK_IS_POD:
inform (loc, " %qT is not a POD type", t1);
break;
case CPTK_IS_POLYMORPHIC:
inform (loc, " %qT is not a polymorphic type", t1);
break;
case CPTK_IS_SAME_AS:
inform (loc, " %qT is not the same as %qT", t1, t2);
break;
case CPTK_IS_STD_LAYOUT:
inform (loc, " %qT is not an standard layout type", t1);
break;
case CPTK_IS_TRIVIAL:
inform (loc, " %qT is not a trivial type", t1);
break;
case CPTK_IS_UNION:
inform (loc, " %qT is not a union", t1);
break;
default:
gcc_unreachable ();
}
}
/* Determine if the call expression T, when normalized as a constraint,
is satisfied by ARGS.
TODO: If T is refers to a concept, We could recursively analyze
its definition to identify the exact failure, but that could
emit a *lot* of error messages (defeating the purpose of
improved diagnostics). Consider adding a flag to control the
depth of diagnostics. */
void
diagnose_call_expression (location_t loc, tree t, tree args)
{
if (constraint_expression_satisfied_p (t, args))
return;
/* Rebuild the expression for the purpose of diagnostics. */
++processing_template_decl;
tree expr = tsubst_expr (t, args, tf_none, NULL_TREE, false);
--processing_template_decl;
/* If the function call is known to be a concept check, then
diagnose it differently (i.e., we may recurse). */
if (resolve_constraint_check (t))
inform (loc, " concept %qE was not satisfied", expr);
else
inform (loc, " %qE evaluated to false", expr);
}
/* Determine if the template-id T, when normalized as a constraint
is satisfied by ARGS. */
void
diagnose_template_id (location_t loc, tree t, tree args)
{
/* Check for invalid template-ids. */
if (!variable_template_p (TREE_OPERAND (t, 0)))
{
inform (loc, " invalid constraint %qE", t);
return;
}
if (constraint_expression_satisfied_p (t, args))
return;
/* Rebuild the expression for the purpose of diagnostics. */
++processing_template_decl;
tree expr = tsubst_expr (t, args, tf_none, NULL_TREE, false);
--processing_template_decl;
tree var = DECL_TEMPLATE_RESULT (TREE_OPERAND (t, 0));
if (DECL_DECLARED_CONCEPT_P (var))
inform (loc, " concept %qE was not satisfied", expr);
else
inform (loc, " %qE evaluated to false", expr);
}
/* Determine if the requires-expression, when normalized as a
constraint is satisfied by ARGS.
TODO: Build sets of expressions, types, and constraints
based on the requirements in T and emit specific diagnostics
for those. */
void
diagnose_requires_expression (location_t loc, tree t, tree args)
{
if (constraint_expression_satisfied_p (t, args))
return;
inform (loc, "requirements not satisfied");
}
void
diagnose_pack_expansion (location_t loc, tree t, tree args)
{
if (constraint_expression_satisfied_p (t, args))
return;
/* Make sure that we don't have naked packs that we don't expect. */
if (!same_type_p (TREE_TYPE (t), boolean_type_node))
{
inform (loc, "invalid pack expansion in constraint %qE", t);
return;
}
inform (loc, " in the expansion of %qE", t);
/* Get the vector of expanded arguments. Note that n must not
be 0 since this constraint is not satisfied. */
++processing_template_decl;
tree exprs = tsubst_pack_expansion (t, args, tf_none, NULL_TREE);
--processing_template_decl;
if (exprs == error_mark_node)
{
/* TODO: This error message could be better. */
inform (loc, " substitution failure occurred during expansion");
return;
}
/* Check each expanded constraint separately. */
int n = TREE_VEC_LENGTH (exprs);
for (int i = 0; i < n; ++i)
{
tree expr = TREE_VEC_ELT (exprs, i);
if (!constraint_expression_satisfied_p (expr, args))
inform (loc, " %qE was not satisfied", expr);
}
}
/* Diagnose an expression that would be characterized as
a predicate constraint. */
void
diagnose_other_expression (location_t loc, tree t, tree args)
{
if (constraint_expression_satisfied_p (t, args))
return;
inform (loc, " %qE evaluated to false", t);
}
void
diagnose_expression (location_t loc, tree t, tree args)
{
switch (TREE_CODE (t))
{
case TRUTH_ANDIF_EXPR:
diagnose_logical_operation (loc, t, args);
break;
case TRUTH_ORIF_EXPR:
diagnose_logical_operation (loc, t, args);
break;
case CALL_EXPR:
diagnose_call_expression (loc, t, args);
break;
case TEMPLATE_ID_EXPR:
diagnose_template_id (loc, t, args);
break;
case REQUIRES_EXPR:
diagnose_requires_expression (loc, t, args);
break;
case TRAIT_EXPR:
diagnose_trait_expression (loc, t, args);
break;
case EXPR_PACK_EXPANSION:
diagnose_pack_expansion (loc, t, args);
break;
default:
diagnose_other_expression (loc, t, args);
break;
}
}
inline void
diagnose_predicate_constraint (location_t loc, tree t, tree args)
{
diagnose_expression (loc, PRED_CONSTR_EXPR (t), args);
}
inline void
diagnose_conjunction (location_t loc, tree t, tree args)
{
diagnose_constraint (loc, TREE_OPERAND (t, 0), args);
diagnose_constraint (loc, TREE_OPERAND (t, 1), args);
}
/* Diagnose the constraint T for the given ARGS. This is only
ever invoked on the associated constraints, so we can
only have conjunctions of predicate constraints. */
void
diagnose_constraint (location_t loc, tree t, tree args)
{
switch (TREE_CODE (t))
{
case CONJ_CONSTR:
diagnose_conjunction (loc, t, args);
break;
case PRED_CONSTR:
diagnose_predicate_constraint (loc, t, args);
break;
default:
gcc_unreachable ();
break;
}
}
/* Diagnose the reason(s) why ARGS do not satisfy the constraints
of declaration DECL. */
void
diagnose_declaration_constraints (location_t loc, tree decl, tree args)
{
inform (loc, " constraints not satisfied");
/* Constraints are attached to the template. */
if (tree ti = DECL_TEMPLATE_INFO (decl))
{
decl = TI_TEMPLATE (ti);
if (!args)
args = TI_ARGS (ti);
}
/* Check that the constraints are actually valid. */
tree ci = get_constraints (decl);
if (!valid_constraints_p (ci))
{
inform (loc, " invalid constraints");
return;
}
/* Recursively diagnose the associated constraints. */
diagnose_constraint (loc, CI_ASSOCIATED_CONSTRAINTS (ci), args);
}
} // namespace
/* Emit diagnostics detailing the failure ARGS to satisfy the
constraints of T. Here, T can be either a constraint
or a declaration. */
void
diagnose_constraints (location_t loc, tree t, tree args)
{
if (constraint_p (t))
diagnose_constraint (loc, t, args);
else
diagnose_declaration_constraints (loc, t, args);
}
|