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
|
------------------------------------------------------------------------------
-- --
-- GNAT COMPILER COMPONENTS --
-- --
-- C H E C K S --
-- --
-- B o d y --
-- --
-- Copyright (C) 1992-2003 Free Software Foundation, Inc. --
-- --
-- GNAT is free software; you can redistribute it and/or modify it under --
-- terms of the GNU General Public License as published by the Free Soft- --
-- ware Foundation; either version 2, or (at your option) any later ver- --
-- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
-- OUT 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 distributed with GNAT; see file COPYING. If not, write --
-- to the Free Software Foundation, 59 Temple Place - Suite 330, Boston, --
-- MA 02111-1307, USA. --
-- --
-- GNAT was originally developed by the GNAT team at New York University. --
-- Extensive contributions were provided by Ada Core Technologies Inc. --
-- --
------------------------------------------------------------------------------
with Atree; use Atree;
with Debug; use Debug;
with Einfo; use Einfo;
with Errout; use Errout;
with Exp_Ch2; use Exp_Ch2;
with Exp_Util; use Exp_Util;
with Elists; use Elists;
with Freeze; use Freeze;
with Lib; use Lib;
with Nlists; use Nlists;
with Nmake; use Nmake;
with Opt; use Opt;
with Output; use Output;
with Restrict; use Restrict;
with Rtsfind; use Rtsfind;
with Sem; use Sem;
with Sem_Eval; use Sem_Eval;
with Sem_Ch8; use Sem_Ch8;
with Sem_Res; use Sem_Res;
with Sem_Util; use Sem_Util;
with Sem_Warn; use Sem_Warn;
with Sinfo; use Sinfo;
with Sinput; use Sinput;
with Snames; use Snames;
with Sprint; use Sprint;
with Stand; use Stand;
with Targparm; use Targparm;
with Tbuild; use Tbuild;
with Ttypes; use Ttypes;
with Urealp; use Urealp;
with Validsw; use Validsw;
package body Checks is
-- General note: many of these routines are concerned with generating
-- checking code to make sure that constraint error is raised at runtime.
-- Clearly this code is only needed if the expander is active, since
-- otherwise we will not be generating code or going into the runtime
-- execution anyway.
-- We therefore disconnect most of these checks if the expander is
-- inactive. This has the additional benefit that we do not need to
-- worry about the tree being messed up by previous errors (since errors
-- turn off expansion anyway).
-- There are a few exceptions to the above rule. For instance routines
-- such as Apply_Scalar_Range_Check that do not insert any code can be
-- safely called even when the Expander is inactive (but Errors_Detected
-- is 0). The benefit of executing this code when expansion is off, is
-- the ability to emit constraint error warning for static expressions
-- even when we are not generating code.
-------------------------------------
-- Suppression of Redundant Checks --
-------------------------------------
-- This unit implements a limited circuit for removal of redundant
-- checks. The processing is based on a tracing of simple sequential
-- flow. For any sequence of statements, we save expressions that are
-- marked to be checked, and then if the same expression appears later
-- with the same check, then under certain circumstances, the second
-- check can be suppressed.
-- Basically, we can suppress the check if we know for certain that
-- the previous expression has been elaborated (together with its
-- check), and we know that the exception frame is the same, and that
-- nothing has happened to change the result of the exception.
-- Let us examine each of these three conditions in turn to describe
-- how we ensure that this condition is met.
-- First, we need to know for certain that the previous expression has
-- been executed. This is done principly by the mechanism of calling
-- Conditional_Statements_Begin at the start of any statement sequence
-- and Conditional_Statements_End at the end. The End call causes all
-- checks remembered since the Begin call to be discarded. This does
-- miss a few cases, notably the case of a nested BEGIN-END block with
-- no exception handlers. But the important thing is to be conservative.
-- The other protection is that all checks are discarded if a label
-- is encountered, since then the assumption of sequential execution
-- is violated, and we don't know enough about the flow.
-- Second, we need to know that the exception frame is the same. We
-- do this by killing all remembered checks when we enter a new frame.
-- Again, that's over-conservative, but generally the cases we can help
-- with are pretty local anyway (like the body of a loop for example).
-- Third, we must be sure to forget any checks which are no longer valid.
-- This is done by two mechanisms, first the Kill_Checks_Variable call is
-- used to note any changes to local variables. We only attempt to deal
-- with checks involving local variables, so we do not need to worry
-- about global variables. Second, a call to any non-global procedure
-- causes us to abandon all stored checks, since such a all may affect
-- the values of any local variables.
-- The following define the data structures used to deal with remembering
-- checks so that redundant checks can be eliminated as described above.
-- Right now, the only expressions that we deal with are of the form of
-- simple local objects (either declared locally, or IN parameters) or
-- such objects plus/minus a compile time known constant. We can do
-- more later on if it seems worthwhile, but this catches many simple
-- cases in practice.
-- The following record type reflects a single saved check. An entry
-- is made in the stack of saved checks if and only if the expression
-- has been elaborated with the indicated checks.
type Saved_Check is record
Killed : Boolean;
-- Set True if entry is killed by Kill_Checks
Entity : Entity_Id;
-- The entity involved in the expression that is checked
Offset : Uint;
-- A compile time value indicating the result of adding or
-- subtracting a compile time value. This value is to be
-- added to the value of the Entity. A value of zero is
-- used for the case of a simple entity reference.
Check_Type : Character;
-- This is set to 'R' for a range check (in which case Target_Type
-- is set to the target type for the range check) or to 'O' for an
-- overflow check (in which case Target_Type is set to Empty).
Target_Type : Entity_Id;
-- Used only if Do_Range_Check is set. Records the target type for
-- the check. We need this, because a check is a duplicate only if
-- it has a the same target type (or more accurately one with a
-- range that is smaller or equal to the stored target type of a
-- saved check).
end record;
-- The following table keeps track of saved checks. Rather than use an
-- extensible table. We just use a table of fixed size, and we discard
-- any saved checks that do not fit. That's very unlikely to happen and
-- this is only an optimization in any case.
Saved_Checks : array (Int range 1 .. 200) of Saved_Check;
-- Array of saved checks
Num_Saved_Checks : Nat := 0;
-- Number of saved checks
-- The following stack keeps track of statement ranges. It is treated
-- as a stack. When Conditional_Statements_Begin is called, an entry
-- is pushed onto this stack containing the value of Num_Saved_Checks
-- at the time of the call. Then when Conditional_Statements_End is
-- called, this value is popped off and used to reset Num_Saved_Checks.
-- Note: again, this is a fixed length stack with a size that should
-- always be fine. If the value of the stack pointer goes above the
-- limit, then we just forget all saved checks.
Saved_Checks_Stack : array (Int range 1 .. 100) of Nat;
Saved_Checks_TOS : Nat := 0;
-----------------------
-- Local Subprograms --
-----------------------
procedure Apply_Selected_Length_Checks
(Ck_Node : Node_Id;
Target_Typ : Entity_Id;
Source_Typ : Entity_Id;
Do_Static : Boolean);
-- This is the subprogram that does all the work for Apply_Length_Check
-- and Apply_Static_Length_Check. Expr, Target_Typ and Source_Typ are as
-- described for the above routines. The Do_Static flag indicates that
-- only a static check is to be done.
procedure Apply_Selected_Range_Checks
(Ck_Node : Node_Id;
Target_Typ : Entity_Id;
Source_Typ : Entity_Id;
Do_Static : Boolean);
-- This is the subprogram that does all the work for Apply_Range_Check.
-- Expr, Target_Typ and Source_Typ are as described for the above
-- routine. The Do_Static flag indicates that only a static check is
-- to be done.
procedure Find_Check
(Expr : Node_Id;
Check_Type : Character;
Target_Type : Entity_Id;
Entry_OK : out Boolean;
Check_Num : out Nat;
Ent : out Entity_Id;
Ofs : out Uint);
-- This routine is used by Enable_Range_Check and Enable_Overflow_Check
-- to see if a check is of the form for optimization, and if so, to see
-- if it has already been performed. Expr is the expression to check,
-- and Check_Type is 'R' for a range check, 'O' for an overflow check.
-- Target_Type is the target type for a range check, and Empty for an
-- overflow check. If the entry is not of the form for optimization,
-- then Entry_OK is set to False, and the remaining out parameters
-- are undefined. If the entry is OK, then Ent/Ofs are set to the
-- entity and offset from the expression. Check_Num is the number of
-- a matching saved entry in Saved_Checks, or zero if no such entry
-- is located.
function Get_Discriminal (E : Entity_Id; Bound : Node_Id) return Node_Id;
-- If a discriminal is used in constraining a prival, Return reference
-- to the discriminal of the protected body (which renames the parameter
-- of the enclosing protected operation). This clumsy transformation is
-- needed because privals are created too late and their actual subtypes
-- are not available when analysing the bodies of the protected operations.
-- To be cleaned up???
function Guard_Access
(Cond : Node_Id;
Loc : Source_Ptr;
Ck_Node : Node_Id)
return Node_Id;
-- In the access type case, guard the test with a test to ensure
-- that the access value is non-null, since the checks do not
-- not apply to null access values.
procedure Install_Static_Check (R_Cno : Node_Id; Loc : Source_Ptr);
-- Called by Apply_{Length,Range}_Checks to rewrite the tree with the
-- Constraint_Error node.
function Selected_Length_Checks
(Ck_Node : Node_Id;
Target_Typ : Entity_Id;
Source_Typ : Entity_Id;
Warn_Node : Node_Id)
return Check_Result;
-- Like Apply_Selected_Length_Checks, except it doesn't modify
-- anything, just returns a list of nodes as described in the spec of
-- this package for the Range_Check function.
function Selected_Range_Checks
(Ck_Node : Node_Id;
Target_Typ : Entity_Id;
Source_Typ : Entity_Id;
Warn_Node : Node_Id)
return Check_Result;
-- Like Apply_Selected_Range_Checks, except it doesn't modify anything,
-- just returns a list of nodes as described in the spec of this package
-- for the Range_Check function.
------------------------------
-- Access_Checks_Suppressed --
------------------------------
function Access_Checks_Suppressed (E : Entity_Id) return Boolean is
begin
if Present (E) and then Checks_May_Be_Suppressed (E) then
return Is_Check_Suppressed (E, Access_Check);
else
return Scope_Suppress (Access_Check);
end if;
end Access_Checks_Suppressed;
-------------------------------------
-- Accessibility_Checks_Suppressed --
-------------------------------------
function Accessibility_Checks_Suppressed (E : Entity_Id) return Boolean is
begin
if Present (E) and then Checks_May_Be_Suppressed (E) then
return Is_Check_Suppressed (E, Accessibility_Check);
else
return Scope_Suppress (Accessibility_Check);
end if;
end Accessibility_Checks_Suppressed;
-------------------------
-- Append_Range_Checks --
-------------------------
procedure Append_Range_Checks
(Checks : Check_Result;
Stmts : List_Id;
Suppress_Typ : Entity_Id;
Static_Sloc : Source_Ptr;
Flag_Node : Node_Id)
is
Internal_Flag_Node : constant Node_Id := Flag_Node;
Internal_Static_Sloc : constant Source_Ptr := Static_Sloc;
Checks_On : constant Boolean :=
(not Index_Checks_Suppressed (Suppress_Typ))
or else
(not Range_Checks_Suppressed (Suppress_Typ));
begin
-- For now we just return if Checks_On is false, however this should
-- be enhanced to check for an always True value in the condition
-- and to generate a compilation warning???
if not Checks_On then
return;
end if;
for J in 1 .. 2 loop
exit when No (Checks (J));
if Nkind (Checks (J)) = N_Raise_Constraint_Error
and then Present (Condition (Checks (J)))
then
if not Has_Dynamic_Range_Check (Internal_Flag_Node) then
Append_To (Stmts, Checks (J));
Set_Has_Dynamic_Range_Check (Internal_Flag_Node);
end if;
else
Append_To
(Stmts,
Make_Raise_Constraint_Error (Internal_Static_Sloc,
Reason => CE_Range_Check_Failed));
end if;
end loop;
end Append_Range_Checks;
------------------------
-- Apply_Access_Check --
------------------------
procedure Apply_Access_Check (N : Node_Id) is
P : constant Node_Id := Prefix (N);
begin
if Inside_A_Generic then
return;
end if;
if Is_Entity_Name (P) then
Check_Unset_Reference (P);
end if;
-- Don't need access check if prefix is known to be non-null
if Known_Non_Null (P) then
return;
-- Don't need access checks if they are suppressed on the type
elsif Access_Checks_Suppressed (Etype (P)) then
return;
end if;
-- Case where P is an entity name
if Is_Entity_Name (P) then
declare
Ent : constant Entity_Id := Entity (P);
begin
if Access_Checks_Suppressed (Ent) then
return;
end if;
-- Otherwise we are going to generate an access check, and
-- are we have done it, the entity will now be known non null
-- But we have to check for safe sequential semantics here!
if Safe_To_Capture_Value (N, Ent) then
Set_Is_Known_Non_Null (Ent);
end if;
end;
end if;
-- Access check is required
declare
Loc : constant Source_Ptr := Sloc (N);
begin
Insert_Action (N,
Make_Raise_Constraint_Error (Sloc (N),
Condition =>
Make_Op_Eq (Loc,
Left_Opnd => Duplicate_Subexpr_Move_Checks (P),
Right_Opnd =>
Make_Null (Loc)),
Reason => CE_Access_Check_Failed));
end;
end Apply_Access_Check;
-------------------------------
-- Apply_Accessibility_Check --
-------------------------------
procedure Apply_Accessibility_Check (N : Node_Id; Typ : Entity_Id) is
Loc : constant Source_Ptr := Sloc (N);
Param_Ent : constant Entity_Id := Param_Entity (N);
Param_Level : Node_Id;
Type_Level : Node_Id;
begin
if Inside_A_Generic then
return;
-- Only apply the run-time check if the access parameter
-- has an associated extra access level parameter and
-- when the level of the type is less deep than the level
-- of the access parameter.
elsif Present (Param_Ent)
and then Present (Extra_Accessibility (Param_Ent))
and then UI_Gt (Object_Access_Level (N),
Type_Access_Level (Typ))
and then not Accessibility_Checks_Suppressed (Param_Ent)
and then not Accessibility_Checks_Suppressed (Typ)
then
Param_Level :=
New_Occurrence_Of (Extra_Accessibility (Param_Ent), Loc);
Type_Level :=
Make_Integer_Literal (Loc, Type_Access_Level (Typ));
-- Raise Program_Error if the accessibility level of the
-- the access parameter is deeper than the level of the
-- target access type.
Insert_Action (N,
Make_Raise_Program_Error (Loc,
Condition =>
Make_Op_Gt (Loc,
Left_Opnd => Param_Level,
Right_Opnd => Type_Level),
Reason => PE_Accessibility_Check_Failed));
Analyze_And_Resolve (N);
end if;
end Apply_Accessibility_Check;
---------------------------
-- Apply_Alignment_Check --
---------------------------
procedure Apply_Alignment_Check (E : Entity_Id; N : Node_Id) is
AC : constant Node_Id := Address_Clause (E);
Expr : Node_Id;
Loc : Source_Ptr;
begin
-- See if check needed. Note that we never need a check if the
-- maximum alignment is one, since the check will always succeed
if No (AC)
or else not Check_Address_Alignment (AC)
or else Maximum_Alignment = 1
then
return;
end if;
Loc := Sloc (AC);
Expr := Expression (AC);
if Nkind (Expr) = N_Unchecked_Type_Conversion then
Expr := Expression (Expr);
elsif Nkind (Expr) = N_Function_Call
and then Is_RTE (Entity (Name (Expr)), RE_To_Address)
then
Expr := First (Parameter_Associations (Expr));
if Nkind (Expr) = N_Parameter_Association then
Expr := Explicit_Actual_Parameter (Expr);
end if;
end if;
-- Here Expr is the address value. See if we know that the
-- value is unacceptable at compile time.
if Compile_Time_Known_Value (Expr)
and then Known_Alignment (E)
then
if Expr_Value (Expr) mod Alignment (E) /= 0 then
Insert_Action (N,
Make_Raise_Program_Error (Loc,
Reason => PE_Misaligned_Address_Value));
Error_Msg_NE
("?specified address for& not " &
"consistent with alignment", Expr, E);
end if;
-- Here we do not know if the value is acceptable, generate
-- code to raise PE if alignment is inappropriate.
else
-- Skip generation of this code if we don't want elab code
if not Restrictions (No_Elaboration_Code) then
Insert_After_And_Analyze (N,
Make_Raise_Program_Error (Loc,
Condition =>
Make_Op_Ne (Loc,
Left_Opnd =>
Make_Op_Mod (Loc,
Left_Opnd =>
Unchecked_Convert_To
(RTE (RE_Integer_Address),
Duplicate_Subexpr_No_Checks (Expr)),
Right_Opnd =>
Make_Attribute_Reference (Loc,
Prefix => New_Occurrence_Of (E, Loc),
Attribute_Name => Name_Alignment)),
Right_Opnd => Make_Integer_Literal (Loc, Uint_0)),
Reason => PE_Misaligned_Address_Value),
Suppress => All_Checks);
end if;
end if;
return;
exception
when RE_Not_Available =>
return;
end Apply_Alignment_Check;
-------------------------------------
-- Apply_Arithmetic_Overflow_Check --
-------------------------------------
-- This routine is called only if the type is an integer type, and
-- a software arithmetic overflow check must be performed for op
-- (add, subtract, multiply). The check is performed only if
-- Software_Overflow_Checking is enabled and Do_Overflow_Check
-- is set. In this case we expand the operation into a more complex
-- sequence of tests that ensures that overflow is properly caught.
procedure Apply_Arithmetic_Overflow_Check (N : Node_Id) is
Loc : constant Source_Ptr := Sloc (N);
Typ : constant Entity_Id := Etype (N);
Rtyp : constant Entity_Id := Root_Type (Typ);
Siz : constant Int := UI_To_Int (Esize (Rtyp));
Dsiz : constant Int := Siz * 2;
Opnod : Node_Id;
Ctyp : Entity_Id;
Opnd : Node_Id;
Cent : RE_Id;
begin
-- Skip this if overflow checks are done in back end, or the overflow
-- flag is not set anyway, or we are not doing code expansion.
if Backend_Overflow_Checks_On_Target
or not Do_Overflow_Check (N)
or not Expander_Active
then
return;
end if;
-- Otherwise, we generate the full general code for front end overflow
-- detection, which works by doing arithmetic in a larger type:
-- x op y
-- is expanded into
-- Typ (Checktyp (x) op Checktyp (y));
-- where Typ is the type of the original expression, and Checktyp is
-- an integer type of sufficient length to hold the largest possible
-- result.
-- In the case where check type exceeds the size of Long_Long_Integer,
-- we use a different approach, expanding to:
-- typ (xxx_With_Ovflo_Check (Integer_64 (x), Integer (y)))
-- where xxx is Add, Multiply or Subtract as appropriate
-- Find check type if one exists
if Dsiz <= Standard_Integer_Size then
Ctyp := Standard_Integer;
elsif Dsiz <= Standard_Long_Long_Integer_Size then
Ctyp := Standard_Long_Long_Integer;
-- No check type exists, use runtime call
else
if Nkind (N) = N_Op_Add then
Cent := RE_Add_With_Ovflo_Check;
elsif Nkind (N) = N_Op_Multiply then
Cent := RE_Multiply_With_Ovflo_Check;
else
pragma Assert (Nkind (N) = N_Op_Subtract);
Cent := RE_Subtract_With_Ovflo_Check;
end if;
Rewrite (N,
OK_Convert_To (Typ,
Make_Function_Call (Loc,
Name => New_Reference_To (RTE (Cent), Loc),
Parameter_Associations => New_List (
OK_Convert_To (RTE (RE_Integer_64), Left_Opnd (N)),
OK_Convert_To (RTE (RE_Integer_64), Right_Opnd (N))))));
Analyze_And_Resolve (N, Typ);
return;
end if;
-- If we fall through, we have the case where we do the arithmetic in
-- the next higher type and get the check by conversion. In these cases
-- Ctyp is set to the type to be used as the check type.
Opnod := Relocate_Node (N);
Opnd := OK_Convert_To (Ctyp, Left_Opnd (Opnod));
Analyze (Opnd);
Set_Etype (Opnd, Ctyp);
Set_Analyzed (Opnd, True);
Set_Left_Opnd (Opnod, Opnd);
Opnd := OK_Convert_To (Ctyp, Right_Opnd (Opnod));
Analyze (Opnd);
Set_Etype (Opnd, Ctyp);
Set_Analyzed (Opnd, True);
Set_Right_Opnd (Opnod, Opnd);
-- The type of the operation changes to the base type of the check
-- type, and we reset the overflow check indication, since clearly
-- no overflow is possible now that we are using a double length
-- type. We also set the Analyzed flag to avoid a recursive attempt
-- to expand the node.
Set_Etype (Opnod, Base_Type (Ctyp));
Set_Do_Overflow_Check (Opnod, False);
Set_Analyzed (Opnod, True);
-- Now build the outer conversion
Opnd := OK_Convert_To (Typ, Opnod);
Analyze (Opnd);
Set_Etype (Opnd, Typ);
-- In the discrete type case, we directly generate the range check
-- for the outer operand. This range check will implement the required
-- overflow check.
if Is_Discrete_Type (Typ) then
Rewrite (N, Opnd);
Generate_Range_Check (Expression (N), Typ, CE_Overflow_Check_Failed);
-- For other types, we enable overflow checking on the conversion,
-- after setting the node as analyzed to prevent recursive attempts
-- to expand the conversion node.
else
Set_Analyzed (Opnd, True);
Enable_Overflow_Check (Opnd);
Rewrite (N, Opnd);
end if;
exception
when RE_Not_Available =>
return;
end Apply_Arithmetic_Overflow_Check;
----------------------------
-- Apply_Array_Size_Check --
----------------------------
-- Note: Really of course this entre check should be in the backend,
-- and perhaps this is not quite the right value, but it is good
-- enough to catch the normal cases (and the relevant ACVC tests!)
procedure Apply_Array_Size_Check (N : Node_Id; Typ : Entity_Id) is
Loc : constant Source_Ptr := Sloc (N);
Ctyp : constant Entity_Id := Component_Type (Typ);
Ent : constant Entity_Id := Defining_Identifier (N);
Decl : Node_Id;
Lo : Node_Id;
Hi : Node_Id;
Lob : Uint;
Hib : Uint;
Siz : Uint;
Xtyp : Entity_Id;
Indx : Node_Id;
Sizx : Node_Id;
Code : Node_Id;
Static : Boolean := True;
-- Set false if any index subtye bound is non-static
Umark : constant Uintp.Save_Mark := Uintp.Mark;
-- We can throw away all the Uint computations here, since they are
-- done only to generate boolean test results.
Check_Siz : Uint;
-- Size to check against
function Is_Address_Or_Import (Decl : Node_Id) return Boolean;
-- Determines if Decl is an address clause or Import/Interface pragma
-- that references the defining identifier of the current declaration.
--------------------------
-- Is_Address_Or_Import --
--------------------------
function Is_Address_Or_Import (Decl : Node_Id) return Boolean is
begin
if Nkind (Decl) = N_At_Clause then
return Chars (Identifier (Decl)) = Chars (Ent);
elsif Nkind (Decl) = N_Attribute_Definition_Clause then
return
Chars (Decl) = Name_Address
and then
Nkind (Name (Decl)) = N_Identifier
and then
Chars (Name (Decl)) = Chars (Ent);
elsif Nkind (Decl) = N_Pragma then
if (Chars (Decl) = Name_Import
or else
Chars (Decl) = Name_Interface)
and then Present (Pragma_Argument_Associations (Decl))
then
declare
F : constant Node_Id :=
First (Pragma_Argument_Associations (Decl));
begin
return
Present (F)
and then
Present (Next (F))
and then
Nkind (Expression (Next (F))) = N_Identifier
and then
Chars (Expression (Next (F))) = Chars (Ent);
end;
else
return False;
end if;
else
return False;
end if;
end Is_Address_Or_Import;
-- Start of processing for Apply_Array_Size_Check
begin
if not Expander_Active
or else Storage_Checks_Suppressed (Typ)
then
return;
end if;
-- It is pointless to insert this check inside an init proc, because
-- that's too late, we have already built the object to be the right
-- size, and if it's too large, too bad!
if Inside_Init_Proc then
return;
end if;
-- Look head for pragma interface/import or address clause applying
-- to this entity. If found, we suppress the check entirely. For now
-- we only look ahead 20 declarations to stop this becoming too slow
-- Note that eventually this whole routine gets moved to gigi.
Decl := N;
for Ctr in 1 .. 20 loop
Next (Decl);
exit when No (Decl);
if Is_Address_Or_Import (Decl) then
return;
end if;
end loop;
-- First step is to calculate the maximum number of elements. For this
-- calculation, we use the actual size of the subtype if it is static,
-- and if a bound of a subtype is non-static, we go to the bound of the
-- base type.
Siz := Uint_1;
Indx := First_Index (Typ);
while Present (Indx) loop
Xtyp := Etype (Indx);
Lo := Type_Low_Bound (Xtyp);
Hi := Type_High_Bound (Xtyp);
-- If any bound raises constraint error, we will never get this
-- far, so there is no need to generate any kind of check.
if Raises_Constraint_Error (Lo)
or else
Raises_Constraint_Error (Hi)
then
Uintp.Release (Umark);
return;
end if;
-- Otherwise get bounds values
if Is_Static_Expression (Lo) then
Lob := Expr_Value (Lo);
else
Lob := Expr_Value (Type_Low_Bound (Base_Type (Xtyp)));
Static := False;
end if;
if Is_Static_Expression (Hi) then
Hib := Expr_Value (Hi);
else
Hib := Expr_Value (Type_High_Bound (Base_Type (Xtyp)));
Static := False;
end if;
Siz := Siz * UI_Max (Hib - Lob + 1, Uint_0);
Next_Index (Indx);
end loop;
-- Compute the limit against which we want to check. For subprograms,
-- where the array will go on the stack, we use 8*2**24, which (in
-- bits) is the size of a 16 megabyte array.
if Is_Subprogram (Scope (Ent)) then
Check_Siz := Uint_2 ** 27;
else
Check_Siz := Uint_2 ** 31;
end if;
-- If we have all static bounds and Siz is too large, then we know we
-- know we have a storage error right now, so generate message
if Static and then Siz >= Check_Siz then
Insert_Action (N,
Make_Raise_Storage_Error (Loc,
Reason => SE_Object_Too_Large));
Error_Msg_N ("?Storage_Error will be raised at run-time", N);
Uintp.Release (Umark);
return;
end if;
-- Case of component size known at compile time. If the array
-- size is definitely in range, then we do not need a check.
if Known_Esize (Ctyp)
and then Siz * Esize (Ctyp) < Check_Siz
then
Uintp.Release (Umark);
return;
end if;
-- Here if a dynamic check is required
-- What we do is to build an expression for the size of the array,
-- which is computed as the 'Size of the array component, times
-- the size of each dimension.
Uintp.Release (Umark);
Sizx :=
Make_Attribute_Reference (Loc,
Prefix => New_Occurrence_Of (Ctyp, Loc),
Attribute_Name => Name_Size);
Indx := First_Index (Typ);
for J in 1 .. Number_Dimensions (Typ) loop
if Sloc (Etype (Indx)) = Sloc (N) then
Ensure_Defined (Etype (Indx), N);
end if;
Sizx :=
Make_Op_Multiply (Loc,
Left_Opnd => Sizx,
Right_Opnd =>
Make_Attribute_Reference (Loc,
Prefix => New_Occurrence_Of (Typ, Loc),
Attribute_Name => Name_Length,
Expressions => New_List (
Make_Integer_Literal (Loc, J))));
Next_Index (Indx);
end loop;
Code :=
Make_Raise_Storage_Error (Loc,
Condition =>
Make_Op_Ge (Loc,
Left_Opnd => Sizx,
Right_Opnd =>
Make_Integer_Literal (Loc, Check_Siz)),
Reason => SE_Object_Too_Large);
Set_Size_Check_Code (Defining_Identifier (N), Code);
Insert_Action (N, Code);
end Apply_Array_Size_Check;
----------------------------
-- Apply_Constraint_Check --
----------------------------
procedure Apply_Constraint_Check
(N : Node_Id;
Typ : Entity_Id;
No_Sliding : Boolean := False)
is
Desig_Typ : Entity_Id;
begin
if Inside_A_Generic then
return;
elsif Is_Scalar_Type (Typ) then
Apply_Scalar_Range_Check (N, Typ);
elsif Is_Array_Type (Typ) then
-- A useful optimization: an aggregate with only an Others clause
-- always has the right bounds.
if Nkind (N) = N_Aggregate
and then No (Expressions (N))
and then Nkind
(First (Choices (First (Component_Associations (N)))))
= N_Others_Choice
then
return;
end if;
if Is_Constrained (Typ) then
Apply_Length_Check (N, Typ);
if No_Sliding then
Apply_Range_Check (N, Typ);
end if;
else
Apply_Range_Check (N, Typ);
end if;
elsif (Is_Record_Type (Typ)
or else Is_Private_Type (Typ))
and then Has_Discriminants (Base_Type (Typ))
and then Is_Constrained (Typ)
then
Apply_Discriminant_Check (N, Typ);
elsif Is_Access_Type (Typ) then
Desig_Typ := Designated_Type (Typ);
-- No checks necessary if expression statically null
if Nkind (N) = N_Null then
null;
-- No sliding possible on access to arrays
elsif Is_Array_Type (Desig_Typ) then
if Is_Constrained (Desig_Typ) then
Apply_Length_Check (N, Typ);
end if;
Apply_Range_Check (N, Typ);
elsif Has_Discriminants (Base_Type (Desig_Typ))
and then Is_Constrained (Desig_Typ)
then
Apply_Discriminant_Check (N, Typ);
end if;
end if;
end Apply_Constraint_Check;
------------------------------
-- Apply_Discriminant_Check --
------------------------------
procedure Apply_Discriminant_Check
(N : Node_Id;
Typ : Entity_Id;
Lhs : Node_Id := Empty)
is
Loc : constant Source_Ptr := Sloc (N);
Do_Access : constant Boolean := Is_Access_Type (Typ);
S_Typ : Entity_Id := Etype (N);
Cond : Node_Id;
T_Typ : Entity_Id;
function Is_Aliased_Unconstrained_Component return Boolean;
-- It is possible for an aliased component to have a nominal
-- unconstrained subtype (through instantiation). If this is a
-- discriminated component assigned in the expansion of an aggregate
-- in an initialization, the check must be suppressed. This unusual
-- situation requires a predicate of its own (see 7503-008).
----------------------------------------
-- Is_Aliased_Unconstrained_Component --
----------------------------------------
function Is_Aliased_Unconstrained_Component return Boolean is
Comp : Entity_Id;
Pref : Node_Id;
begin
if Nkind (Lhs) /= N_Selected_Component then
return False;
else
Comp := Entity (Selector_Name (Lhs));
Pref := Prefix (Lhs);
end if;
if Ekind (Comp) /= E_Component
or else not Is_Aliased (Comp)
then
return False;
end if;
return not Comes_From_Source (Pref)
and then In_Instance
and then not Is_Constrained (Etype (Comp));
end Is_Aliased_Unconstrained_Component;
-- Start of processing for Apply_Discriminant_Check
begin
if Do_Access then
T_Typ := Designated_Type (Typ);
else
T_Typ := Typ;
end if;
-- Nothing to do if discriminant checks are suppressed or else no code
-- is to be generated
if not Expander_Active
or else Discriminant_Checks_Suppressed (T_Typ)
then
return;
end if;
-- No discriminant checks necessary for access when expression
-- is statically Null. This is not only an optimization, this is
-- fundamental because otherwise discriminant checks may be generated
-- in init procs for types containing an access to a non-frozen yet
-- record, causing a deadly forward reference.
-- Also, if the expression is of an access type whose designated
-- type is incomplete, then the access value must be null and
-- we suppress the check.
if Nkind (N) = N_Null then
return;
elsif Is_Access_Type (S_Typ) then
S_Typ := Designated_Type (S_Typ);
if Ekind (S_Typ) = E_Incomplete_Type then
return;
end if;
end if;
-- If an assignment target is present, then we need to generate
-- the actual subtype if the target is a parameter or aliased
-- object with an unconstrained nominal subtype.
if Present (Lhs)
and then (Present (Param_Entity (Lhs))
or else (not Is_Constrained (T_Typ)
and then Is_Aliased_View (Lhs)
and then not Is_Aliased_Unconstrained_Component))
then
T_Typ := Get_Actual_Subtype (Lhs);
end if;
-- Nothing to do if the type is unconstrained (this is the case
-- where the actual subtype in the RM sense of N is unconstrained
-- and no check is required).
if not Is_Constrained (T_Typ) then
return;
end if;
-- Suppress checks if the subtypes are the same.
-- the check must be preserved in an assignment to a formal, because
-- the constraint is given by the actual.
if Nkind (Original_Node (N)) /= N_Allocator
and then (No (Lhs)
or else not Is_Entity_Name (Lhs)
or else No (Param_Entity (Lhs)))
then
if (Etype (N) = Typ
or else (Do_Access and then Designated_Type (Typ) = S_Typ))
and then not Is_Aliased_View (Lhs)
then
return;
end if;
-- We can also eliminate checks on allocators with a subtype mark
-- that coincides with the context type. The context type may be a
-- subtype without a constraint (common case, a generic actual).
elsif Nkind (Original_Node (N)) = N_Allocator
and then Is_Entity_Name (Expression (Original_Node (N)))
then
declare
Alloc_Typ : constant Entity_Id :=
Entity (Expression (Original_Node (N)));
begin
if Alloc_Typ = T_Typ
or else (Nkind (Parent (T_Typ)) = N_Subtype_Declaration
and then Is_Entity_Name (
Subtype_Indication (Parent (T_Typ)))
and then Alloc_Typ = Base_Type (T_Typ))
then
return;
end if;
end;
end if;
-- See if we have a case where the types are both constrained, and
-- all the constraints are constants. In this case, we can do the
-- check successfully at compile time.
-- We skip this check for the case where the node is a rewritten`
-- allocator, because it already carries the context subtype, and
-- extracting the discriminants from the aggregate is messy.
if Is_Constrained (S_Typ)
and then Nkind (Original_Node (N)) /= N_Allocator
then
declare
DconT : Elmt_Id;
Discr : Entity_Id;
DconS : Elmt_Id;
ItemS : Node_Id;
ItemT : Node_Id;
begin
-- S_Typ may not have discriminants in the case where it is a
-- private type completed by a default discriminated type. In
-- that case, we need to get the constraints from the
-- underlying_type. If the underlying type is unconstrained (i.e.
-- has no default discriminants) no check is needed.
if Has_Discriminants (S_Typ) then
Discr := First_Discriminant (S_Typ);
DconS := First_Elmt (Discriminant_Constraint (S_Typ));
else
Discr := First_Discriminant (Underlying_Type (S_Typ));
DconS :=
First_Elmt
(Discriminant_Constraint (Underlying_Type (S_Typ)));
if No (DconS) then
return;
end if;
-- A further optimization: if T_Typ is derived from S_Typ
-- without imposing a constraint, no check is needed.
if Nkind (Original_Node (Parent (T_Typ))) =
N_Full_Type_Declaration
then
declare
Type_Def : Node_Id :=
Type_Definition
(Original_Node (Parent (T_Typ)));
begin
if Nkind (Type_Def) = N_Derived_Type_Definition
and then Is_Entity_Name (Subtype_Indication (Type_Def))
and then Entity (Subtype_Indication (Type_Def)) = S_Typ
then
return;
end if;
end;
end if;
end if;
DconT := First_Elmt (Discriminant_Constraint (T_Typ));
while Present (Discr) loop
ItemS := Node (DconS);
ItemT := Node (DconT);
exit when
not Is_OK_Static_Expression (ItemS)
or else
not Is_OK_Static_Expression (ItemT);
if Expr_Value (ItemS) /= Expr_Value (ItemT) then
if Do_Access then -- needs run-time check.
exit;
else
Apply_Compile_Time_Constraint_Error
(N, "incorrect value for discriminant&?",
CE_Discriminant_Check_Failed, Ent => Discr);
return;
end if;
end if;
Next_Elmt (DconS);
Next_Elmt (DconT);
Next_Discriminant (Discr);
end loop;
if No (Discr) then
return;
end if;
end;
end if;
-- Here we need a discriminant check. First build the expression
-- for the comparisons of the discriminants:
-- (n.disc1 /= typ.disc1) or else
-- (n.disc2 /= typ.disc2) or else
-- ...
-- (n.discn /= typ.discn)
Cond := Build_Discriminant_Checks (N, T_Typ);
-- If Lhs is set and is a parameter, then the condition is
-- guarded by: lhs'constrained and then (condition built above)
if Present (Param_Entity (Lhs)) then
Cond :=
Make_And_Then (Loc,
Left_Opnd =>
Make_Attribute_Reference (Loc,
Prefix => New_Occurrence_Of (Param_Entity (Lhs), Loc),
Attribute_Name => Name_Constrained),
Right_Opnd => Cond);
end if;
if Do_Access then
Cond := Guard_Access (Cond, Loc, N);
end if;
Insert_Action (N,
Make_Raise_Constraint_Error (Loc,
Condition => Cond,
Reason => CE_Discriminant_Check_Failed));
end Apply_Discriminant_Check;
------------------------
-- Apply_Divide_Check --
------------------------
procedure Apply_Divide_Check (N : Node_Id) is
Loc : constant Source_Ptr := Sloc (N);
Typ : constant Entity_Id := Etype (N);
Left : constant Node_Id := Left_Opnd (N);
Right : constant Node_Id := Right_Opnd (N);
LLB : Uint;
Llo : Uint;
Lhi : Uint;
LOK : Boolean;
Rlo : Uint;
Rhi : Uint;
ROK : Boolean;
begin
if Expander_Active
and not Backend_Divide_Checks_On_Target
then
Determine_Range (Right, ROK, Rlo, Rhi);
-- See if division by zero possible, and if so generate test. This
-- part of the test is not controlled by the -gnato switch.
if Do_Division_Check (N) then
if (not ROK) or else (Rlo <= 0 and then 0 <= Rhi) then
Insert_Action (N,
Make_Raise_Constraint_Error (Loc,
Condition =>
Make_Op_Eq (Loc,
Left_Opnd => Duplicate_Subexpr_Move_Checks (Right),
Right_Opnd => Make_Integer_Literal (Loc, 0)),
Reason => CE_Divide_By_Zero));
end if;
end if;
-- Test for extremely annoying case of xxx'First divided by -1
if Do_Overflow_Check (N) then
if Nkind (N) = N_Op_Divide
and then Is_Signed_Integer_Type (Typ)
then
Determine_Range (Left, LOK, Llo, Lhi);
LLB := Expr_Value (Type_Low_Bound (Base_Type (Typ)));
if ((not ROK) or else (Rlo <= (-1) and then (-1) <= Rhi))
and then
((not LOK) or else (Llo = LLB))
then
Insert_Action (N,
Make_Raise_Constraint_Error (Loc,
Condition =>
Make_And_Then (Loc,
Make_Op_Eq (Loc,
Left_Opnd =>
Duplicate_Subexpr_Move_Checks (Left),
Right_Opnd => Make_Integer_Literal (Loc, LLB)),
Make_Op_Eq (Loc,
Left_Opnd =>
Duplicate_Subexpr (Right),
Right_Opnd =>
Make_Integer_Literal (Loc, -1))),
Reason => CE_Overflow_Check_Failed));
end if;
end if;
end if;
end if;
end Apply_Divide_Check;
------------------------
-- Apply_Length_Check --
------------------------
procedure Apply_Length_Check
(Ck_Node : Node_Id;
Target_Typ : Entity_Id;
Source_Typ : Entity_Id := Empty)
is
begin
Apply_Selected_Length_Checks
(Ck_Node, Target_Typ, Source_Typ, Do_Static => False);
end Apply_Length_Check;
-----------------------
-- Apply_Range_Check --
-----------------------
procedure Apply_Range_Check
(Ck_Node : Node_Id;
Target_Typ : Entity_Id;
Source_Typ : Entity_Id := Empty)
is
begin
Apply_Selected_Range_Checks
(Ck_Node, Target_Typ, Source_Typ, Do_Static => False);
end Apply_Range_Check;
------------------------------
-- Apply_Scalar_Range_Check --
------------------------------
-- Note that Apply_Scalar_Range_Check never turns the Do_Range_Check
-- flag off if it is already set on.
procedure Apply_Scalar_Range_Check
(Expr : Node_Id;
Target_Typ : Entity_Id;
Source_Typ : Entity_Id := Empty;
Fixed_Int : Boolean := False)
is
Parnt : constant Node_Id := Parent (Expr);
S_Typ : Entity_Id;
Arr : Node_Id := Empty; -- initialize to prevent warning
Arr_Typ : Entity_Id := Empty; -- initialize to prevent warning
OK : Boolean;
Is_Subscr_Ref : Boolean;
-- Set true if Expr is a subscript
Is_Unconstrained_Subscr_Ref : Boolean;
-- Set true if Expr is a subscript of an unconstrained array. In this
-- case we do not attempt to do an analysis of the value against the
-- range of the subscript, since we don't know the actual subtype.
Int_Real : Boolean;
-- Set to True if Expr should be regarded as a real value
-- even though the type of Expr might be discrete.
procedure Bad_Value;
-- Procedure called if value is determined to be out of range
---------------
-- Bad_Value --
---------------
procedure Bad_Value is
begin
Apply_Compile_Time_Constraint_Error
(Expr, "value not in range of}?", CE_Range_Check_Failed,
Ent => Target_Typ,
Typ => Target_Typ);
end Bad_Value;
-- Start of processing for Apply_Scalar_Range_Check
begin
if Inside_A_Generic then
return;
-- Return if check obviously not needed. Note that we do not check
-- for the expander being inactive, since this routine does not
-- insert any code, but it does generate useful warnings sometimes,
-- which we would like even if we are in semantics only mode.
elsif Target_Typ = Any_Type
or else not Is_Scalar_Type (Target_Typ)
or else Raises_Constraint_Error (Expr)
then
return;
end if;
-- Now, see if checks are suppressed
Is_Subscr_Ref :=
Is_List_Member (Expr) and then Nkind (Parnt) = N_Indexed_Component;
if Is_Subscr_Ref then
Arr := Prefix (Parnt);
Arr_Typ := Get_Actual_Subtype_If_Available (Arr);
end if;
if not Do_Range_Check (Expr) then
-- Subscript reference. Check for Index_Checks suppressed
if Is_Subscr_Ref then
-- Check array type and its base type
if Index_Checks_Suppressed (Arr_Typ)
or else Index_Checks_Suppressed (Base_Type (Arr_Typ))
then
return;
-- Check array itself if it is an entity name
elsif Is_Entity_Name (Arr)
and then Index_Checks_Suppressed (Entity (Arr))
then
return;
-- Check expression itself if it is an entity name
elsif Is_Entity_Name (Expr)
and then Index_Checks_Suppressed (Entity (Expr))
then
return;
end if;
-- All other cases, check for Range_Checks suppressed
else
-- Check target type and its base type
if Range_Checks_Suppressed (Target_Typ)
or else Range_Checks_Suppressed (Base_Type (Target_Typ))
then
return;
-- Check expression itself if it is an entity name
elsif Is_Entity_Name (Expr)
and then Range_Checks_Suppressed (Entity (Expr))
then
return;
-- If Expr is part of an assignment statement, then check
-- left side of assignment if it is an entity name.
elsif Nkind (Parnt) = N_Assignment_Statement
and then Is_Entity_Name (Name (Parnt))
and then Range_Checks_Suppressed (Entity (Name (Parnt)))
then
return;
end if;
end if;
end if;
-- Do not set range checks if they are killed
if Nkind (Expr) = N_Unchecked_Type_Conversion
and then Kill_Range_Check (Expr)
then
return;
end if;
-- Do not set range checks for any values from System.Scalar_Values
-- since the whole idea of such values is to avoid checking them!
if Is_Entity_Name (Expr)
and then Is_RTU (Scope (Entity (Expr)), System_Scalar_Values)
then
return;
end if;
-- Now see if we need a check
if No (Source_Typ) then
S_Typ := Etype (Expr);
else
S_Typ := Source_Typ;
end if;
if not Is_Scalar_Type (S_Typ) or else S_Typ = Any_Type then
return;
end if;
Is_Unconstrained_Subscr_Ref :=
Is_Subscr_Ref and then not Is_Constrained (Arr_Typ);
-- Always do a range check if the source type includes infinities
-- and the target type does not include infinities. We do not do
-- this if range checks are killed.
if Is_Floating_Point_Type (S_Typ)
and then Has_Infinities (S_Typ)
and then not Has_Infinities (Target_Typ)
then
Enable_Range_Check (Expr);
end if;
-- Return if we know expression is definitely in the range of
-- the target type as determined by Determine_Range. Right now
-- we only do this for discrete types, and not fixed-point or
-- floating-point types.
-- The additional less-precise tests below catch these cases.
-- Note: skip this if we are given a source_typ, since the point
-- of supplying a Source_Typ is to stop us looking at the expression.
-- could sharpen this test to be out parameters only ???
if Is_Discrete_Type (Target_Typ)
and then Is_Discrete_Type (Etype (Expr))
and then not Is_Unconstrained_Subscr_Ref
and then No (Source_Typ)
then
declare
Tlo : constant Node_Id := Type_Low_Bound (Target_Typ);
Thi : constant Node_Id := Type_High_Bound (Target_Typ);
Lo : Uint;
Hi : Uint;
begin
if Compile_Time_Known_Value (Tlo)
and then Compile_Time_Known_Value (Thi)
then
declare
Lov : constant Uint := Expr_Value (Tlo);
Hiv : constant Uint := Expr_Value (Thi);
begin
-- If range is null, we for sure have a constraint error
-- (we don't even need to look at the value involved,
-- since all possible values will raise CE).
if Lov > Hiv then
Bad_Value;
return;
end if;
-- Otherwise determine range of value
Determine_Range (Expr, OK, Lo, Hi);
if OK then
-- If definitely in range, all OK
if Lo >= Lov and then Hi <= Hiv then
return;
-- If definitely not in range, warn
elsif Lov > Hi or else Hiv < Lo then
Bad_Value;
return;
-- Otherwise we don't know
else
null;
end if;
end if;
end;
end if;
end;
end if;
Int_Real :=
Is_Floating_Point_Type (S_Typ)
or else (Is_Fixed_Point_Type (S_Typ) and then not Fixed_Int);
-- Check if we can determine at compile time whether Expr is in the
-- range of the target type. Note that if S_Typ is within the bounds
-- of Target_Typ then this must be the case. This check is meaningful
-- only if this is not a conversion between integer and real types.
if not Is_Unconstrained_Subscr_Ref
and then
Is_Discrete_Type (S_Typ) = Is_Discrete_Type (Target_Typ)
and then
(In_Subrange_Of (S_Typ, Target_Typ, Fixed_Int)
or else
Is_In_Range (Expr, Target_Typ, Fixed_Int, Int_Real))
then
return;
elsif Is_Out_Of_Range (Expr, Target_Typ, Fixed_Int, Int_Real) then
Bad_Value;
return;
-- In the floating-point case, we only do range checks if the
-- type is constrained. We definitely do NOT want range checks
-- for unconstrained types, since we want to have infinities
elsif Is_Floating_Point_Type (S_Typ) then
if Is_Constrained (S_Typ) then
Enable_Range_Check (Expr);
end if;
-- For all other cases we enable a range check unconditionally
else
Enable_Range_Check (Expr);
return;
end if;
end Apply_Scalar_Range_Check;
----------------------------------
-- Apply_Selected_Length_Checks --
----------------------------------
procedure Apply_Selected_Length_Checks
(Ck_Node : Node_Id;
Target_Typ : Entity_Id;
Source_Typ : Entity_Id;
Do_Static : Boolean)
is
Cond : Node_Id;
R_Result : Check_Result;
R_Cno : Node_Id;
Loc : constant Source_Ptr := Sloc (Ck_Node);
Checks_On : constant Boolean :=
(not Index_Checks_Suppressed (Target_Typ))
or else
(not Length_Checks_Suppressed (Target_Typ));
begin
if not Expander_Active then
return;
end if;
R_Result :=
Selected_Length_Checks (Ck_Node, Target_Typ, Source_Typ, Empty);
for J in 1 .. 2 loop
R_Cno := R_Result (J);
exit when No (R_Cno);
-- A length check may mention an Itype which is attached to a
-- subsequent node. At the top level in a package this can cause
-- an order-of-elaboration problem, so we make sure that the itype
-- is referenced now.
if Ekind (Current_Scope) = E_Package
and then Is_Compilation_Unit (Current_Scope)
then
Ensure_Defined (Target_Typ, Ck_Node);
if Present (Source_Typ) then
Ensure_Defined (Source_Typ, Ck_Node);
elsif Is_Itype (Etype (Ck_Node)) then
Ensure_Defined (Etype (Ck_Node), Ck_Node);
end if;
end if;
-- If the item is a conditional raise of constraint error,
-- then have a look at what check is being performed and
-- ???
if Nkind (R_Cno) = N_Raise_Constraint_Error
and then Present (Condition (R_Cno))
then
Cond := Condition (R_Cno);
if not Has_Dynamic_Length_Check (Ck_Node)
and then Checks_On
then
Insert_Action (Ck_Node, R_Cno);
if not Do_Static then
Set_Has_Dynamic_Length_Check (Ck_Node);
end if;
end if;
-- Output a warning if the condition is known to be True
if Is_Entity_Name (Cond)
and then Entity (Cond) = Standard_True
then
Apply_Compile_Time_Constraint_Error
(Ck_Node, "wrong length for array of}?",
CE_Length_Check_Failed,
Ent => Target_Typ,
Typ => Target_Typ);
-- If we were only doing a static check, or if checks are not
-- on, then we want to delete the check, since it is not needed.
-- We do this by replacing the if statement by a null statement
elsif Do_Static or else not Checks_On then
Rewrite (R_Cno, Make_Null_Statement (Loc));
end if;
else
Install_Static_Check (R_Cno, Loc);
end if;
end loop;
end Apply_Selected_Length_Checks;
---------------------------------
-- Apply_Selected_Range_Checks --
---------------------------------
procedure Apply_Selected_Range_Checks
(Ck_Node : Node_Id;
Target_Typ : Entity_Id;
Source_Typ : Entity_Id;
Do_Static : Boolean)
is
Cond : Node_Id;
R_Result : Check_Result;
R_Cno : Node_Id;
Loc : constant Source_Ptr := Sloc (Ck_Node);
Checks_On : constant Boolean :=
(not Index_Checks_Suppressed (Target_Typ))
or else
(not Range_Checks_Suppressed (Target_Typ));
begin
if not Expander_Active or else not Checks_On then
return;
end if;
R_Result :=
Selected_Range_Checks (Ck_Node, Target_Typ, Source_Typ, Empty);
for J in 1 .. 2 loop
R_Cno := R_Result (J);
exit when No (R_Cno);
-- If the item is a conditional raise of constraint error,
-- then have a look at what check is being performed and
-- ???
if Nkind (R_Cno) = N_Raise_Constraint_Error
and then Present (Condition (R_Cno))
then
Cond := Condition (R_Cno);
if not Has_Dynamic_Range_Check (Ck_Node) then
Insert_Action (Ck_Node, R_Cno);
if not Do_Static then
Set_Has_Dynamic_Range_Check (Ck_Node);
end if;
end if;
-- Output a warning if the condition is known to be True
if Is_Entity_Name (Cond)
and then Entity (Cond) = Standard_True
then
-- Since an N_Range is technically not an expression, we
-- have to set one of the bounds to C_E and then just flag
-- the N_Range. The warning message will point to the
-- lower bound and complain about a range, which seems OK.
if Nkind (Ck_Node) = N_Range then
Apply_Compile_Time_Constraint_Error
(Low_Bound (Ck_Node), "static range out of bounds of}?",
CE_Range_Check_Failed,
Ent => Target_Typ,
Typ => Target_Typ);
Set_Raises_Constraint_Error (Ck_Node);
else
Apply_Compile_Time_Constraint_Error
(Ck_Node, "static value out of range of}?",
CE_Range_Check_Failed,
Ent => Target_Typ,
Typ => Target_Typ);
end if;
-- If we were only doing a static check, or if checks are not
-- on, then we want to delete the check, since it is not needed.
-- We do this by replacing the if statement by a null statement
elsif Do_Static or else not Checks_On then
Rewrite (R_Cno, Make_Null_Statement (Loc));
end if;
else
Install_Static_Check (R_Cno, Loc);
end if;
end loop;
end Apply_Selected_Range_Checks;
-------------------------------
-- Apply_Static_Length_Check --
-------------------------------
procedure Apply_Static_Length_Check
(Expr : Node_Id;
Target_Typ : Entity_Id;
Source_Typ : Entity_Id := Empty)
is
begin
Apply_Selected_Length_Checks
(Expr, Target_Typ, Source_Typ, Do_Static => True);
end Apply_Static_Length_Check;
-------------------------------------
-- Apply_Subscript_Validity_Checks --
-------------------------------------
procedure Apply_Subscript_Validity_Checks (Expr : Node_Id) is
Sub : Node_Id;
begin
pragma Assert (Nkind (Expr) = N_Indexed_Component);
-- Loop through subscripts
Sub := First (Expressions (Expr));
while Present (Sub) loop
-- Check one subscript. Note that we do not worry about
-- enumeration type with holes, since we will convert the
-- value to a Pos value for the subscript, and that convert
-- will do the necessary validity check.
Ensure_Valid (Sub, Holes_OK => True);
-- Move to next subscript
Sub := Next (Sub);
end loop;
end Apply_Subscript_Validity_Checks;
----------------------------------
-- Apply_Type_Conversion_Checks --
----------------------------------
procedure Apply_Type_Conversion_Checks (N : Node_Id) is
Target_Type : constant Entity_Id := Etype (N);
Target_Base : constant Entity_Id := Base_Type (Target_Type);
Expr : constant Node_Id := Expression (N);
Expr_Type : constant Entity_Id := Etype (Expr);
begin
if Inside_A_Generic then
return;
-- Skip these checks if serious errors detected, there are some nasty
-- situations of incomplete trees that blow things up.
elsif Serious_Errors_Detected > 0 then
return;
-- Scalar type conversions of the form Target_Type (Expr) require
-- a range check if we cannot be sure that Expr is in the base type
-- of Target_Typ and also that Expr is in the range of Target_Typ.
-- These are not quite the same condition from an implementation
-- point of view, but clearly the second includes the first.
elsif Is_Scalar_Type (Target_Type) then
declare
Conv_OK : constant Boolean := Conversion_OK (N);
-- If the Conversion_OK flag on the type conversion is set
-- and no floating point type is involved in the type conversion
-- then fixed point values must be read as integral values.
begin
if not Overflow_Checks_Suppressed (Target_Base)
and then not In_Subrange_Of (Expr_Type, Target_Base, Conv_OK)
then
Set_Do_Overflow_Check (N);
end if;
if not Range_Checks_Suppressed (Target_Type)
and then not Range_Checks_Suppressed (Expr_Type)
then
Apply_Scalar_Range_Check
(Expr, Target_Type, Fixed_Int => Conv_OK);
end if;
end;
elsif Comes_From_Source (N)
and then Is_Record_Type (Target_Type)
and then Is_Derived_Type (Target_Type)
and then not Is_Tagged_Type (Target_Type)
and then not Is_Constrained (Target_Type)
and then Present (Stored_Constraint (Target_Type))
then
-- An unconstrained derived type may have inherited discriminant
-- Build an actual discriminant constraint list using the stored
-- constraint, to verify that the expression of the parent type
-- satisfies the constraints imposed by the (unconstrained!)
-- derived type. This applies to value conversions, not to view
-- conversions of tagged types.
declare
Loc : constant Source_Ptr := Sloc (N);
Cond : Node_Id;
Constraint : Elmt_Id;
Discr_Value : Node_Id;
Discr : Entity_Id;
New_Constraints : constant Elist_Id := New_Elmt_List;
Old_Constraints : constant Elist_Id :=
Discriminant_Constraint (Expr_Type);
begin
Constraint := First_Elmt (Stored_Constraint (Target_Type));
while Present (Constraint) loop
Discr_Value := Node (Constraint);
if Is_Entity_Name (Discr_Value)
and then Ekind (Entity (Discr_Value)) = E_Discriminant
then
Discr := Corresponding_Discriminant (Entity (Discr_Value));
if Present (Discr)
and then Scope (Discr) = Base_Type (Expr_Type)
then
-- Parent is constrained by new discriminant. Obtain
-- Value of original discriminant in expression. If
-- the new discriminant has been used to constrain more
-- than one of the stored discriminants, this will
-- provide the required consistency check.
Append_Elmt (
Make_Selected_Component (Loc,
Prefix =>
Duplicate_Subexpr_No_Checks
(Expr, Name_Req => True),
Selector_Name =>
Make_Identifier (Loc, Chars (Discr))),
New_Constraints);
else
-- Discriminant of more remote ancestor ???
return;
end if;
-- Derived type definition has an explicit value for
-- this stored discriminant.
else
Append_Elmt
(Duplicate_Subexpr_No_Checks (Discr_Value),
New_Constraints);
end if;
Next_Elmt (Constraint);
end loop;
-- Use the unconstrained expression type to retrieve the
-- discriminants of the parent, and apply momentarily the
-- discriminant constraint synthesized above.
Set_Discriminant_Constraint (Expr_Type, New_Constraints);
Cond := Build_Discriminant_Checks (Expr, Expr_Type);
Set_Discriminant_Constraint (Expr_Type, Old_Constraints);
Insert_Action (N,
Make_Raise_Constraint_Error (Loc,
Condition => Cond,
Reason => CE_Discriminant_Check_Failed));
end;
-- For arrays, conversions are applied during expansion, to take
-- into accounts changes of representation. The checks become range
-- checks on the base type or length checks on the subtype, depending
-- on whether the target type is unconstrained or constrained.
else
null;
end if;
end Apply_Type_Conversion_Checks;
----------------------------------------------
-- Apply_Universal_Integer_Attribute_Checks --
----------------------------------------------
procedure Apply_Universal_Integer_Attribute_Checks (N : Node_Id) is
Loc : constant Source_Ptr := Sloc (N);
Typ : constant Entity_Id := Etype (N);
begin
if Inside_A_Generic then
return;
-- Nothing to do if checks are suppressed
elsif Range_Checks_Suppressed (Typ)
and then Overflow_Checks_Suppressed (Typ)
then
return;
-- Nothing to do if the attribute does not come from source. The
-- internal attributes we generate of this type do not need checks,
-- and furthermore the attempt to check them causes some circular
-- elaboration orders when dealing with packed types.
elsif not Comes_From_Source (N) then
return;
-- If the prefix is a selected component that depends on a discriminant
-- the check may improperly expose a discriminant instead of using
-- the bounds of the object itself. Set the type of the attribute to
-- the base type of the context, so that a check will be imposed when
-- needed (e.g. if the node appears as an index).
elsif Nkind (Prefix (N)) = N_Selected_Component
and then Ekind (Typ) = E_Signed_Integer_Subtype
and then Depends_On_Discriminant (Scalar_Range (Typ))
then
Set_Etype (N, Base_Type (Typ));
-- Otherwise, replace the attribute node with a type conversion
-- node whose expression is the attribute, retyped to universal
-- integer, and whose subtype mark is the target type. The call
-- to analyze this conversion will set range and overflow checks
-- as required for proper detection of an out of range value.
else
Set_Etype (N, Universal_Integer);
Set_Analyzed (N, True);
Rewrite (N,
Make_Type_Conversion (Loc,
Subtype_Mark => New_Occurrence_Of (Typ, Loc),
Expression => Relocate_Node (N)));
Analyze_And_Resolve (N, Typ);
return;
end if;
end Apply_Universal_Integer_Attribute_Checks;
-------------------------------
-- Build_Discriminant_Checks --
-------------------------------
function Build_Discriminant_Checks
(N : Node_Id;
T_Typ : Entity_Id)
return Node_Id
is
Loc : constant Source_Ptr := Sloc (N);
Cond : Node_Id;
Disc : Elmt_Id;
Disc_Ent : Entity_Id;
Dref : Node_Id;
Dval : Node_Id;
begin
Cond := Empty;
Disc := First_Elmt (Discriminant_Constraint (T_Typ));
-- For a fully private type, use the discriminants of the parent type
if Is_Private_Type (T_Typ)
and then No (Full_View (T_Typ))
then
Disc_Ent := First_Discriminant (Etype (Base_Type (T_Typ)));
else
Disc_Ent := First_Discriminant (T_Typ);
end if;
while Present (Disc) loop
Dval := Node (Disc);
if Nkind (Dval) = N_Identifier
and then Ekind (Entity (Dval)) = E_Discriminant
then
Dval := New_Occurrence_Of (Discriminal (Entity (Dval)), Loc);
else
Dval := Duplicate_Subexpr_No_Checks (Dval);
end if;
Dref :=
Make_Selected_Component (Loc,
Prefix =>
Duplicate_Subexpr_No_Checks (N, Name_Req => True),
Selector_Name =>
Make_Identifier (Loc, Chars (Disc_Ent)));
Set_Is_In_Discriminant_Check (Dref);
Evolve_Or_Else (Cond,
Make_Op_Ne (Loc,
Left_Opnd => Dref,
Right_Opnd => Dval));
Next_Elmt (Disc);
Next_Discriminant (Disc_Ent);
end loop;
return Cond;
end Build_Discriminant_Checks;
-----------------------------------
-- Check_Valid_Lvalue_Subscripts --
-----------------------------------
procedure Check_Valid_Lvalue_Subscripts (Expr : Node_Id) is
begin
-- Skip this if range checks are suppressed
if Range_Checks_Suppressed (Etype (Expr)) then
return;
-- Only do this check for expressions that come from source. We
-- assume that expander generated assignments explicitly include
-- any necessary checks. Note that this is not just an optimization,
-- it avoids infinite recursions!
elsif not Comes_From_Source (Expr) then
return;
-- For a selected component, check the prefix
elsif Nkind (Expr) = N_Selected_Component then
Check_Valid_Lvalue_Subscripts (Prefix (Expr));
return;
-- Case of indexed component
elsif Nkind (Expr) = N_Indexed_Component then
Apply_Subscript_Validity_Checks (Expr);
-- Prefix may itself be or contain an indexed component, and
-- these subscripts need checking as well
Check_Valid_Lvalue_Subscripts (Prefix (Expr));
end if;
end Check_Valid_Lvalue_Subscripts;
----------------------------------
-- Conditional_Statements_Begin --
----------------------------------
procedure Conditional_Statements_Begin is
begin
Saved_Checks_TOS := Saved_Checks_TOS + 1;
-- If stack overflows, kill all checks, that way we know to
-- simply reset the number of saved checks to zero on return.
-- This should never occur in practice.
if Saved_Checks_TOS > Saved_Checks_Stack'Last then
Kill_All_Checks;
-- In the normal case, we just make a new stack entry saving
-- the current number of saved checks for a later restore.
else
Saved_Checks_Stack (Saved_Checks_TOS) := Num_Saved_Checks;
if Debug_Flag_CC then
w ("Conditional_Statements_Begin: Num_Saved_Checks = ",
Num_Saved_Checks);
end if;
end if;
end Conditional_Statements_Begin;
--------------------------------
-- Conditional_Statements_End --
--------------------------------
procedure Conditional_Statements_End is
begin
pragma Assert (Saved_Checks_TOS > 0);
-- If the saved checks stack overflowed, then we killed all
-- checks, so setting the number of saved checks back to
-- zero is correct. This should never occur in practice.
if Saved_Checks_TOS > Saved_Checks_Stack'Last then
Num_Saved_Checks := 0;
-- In the normal case, restore the number of saved checks
-- from the top stack entry.
else
Num_Saved_Checks := Saved_Checks_Stack (Saved_Checks_TOS);
if Debug_Flag_CC then
w ("Conditional_Statements_End: Num_Saved_Checks = ",
Num_Saved_Checks);
end if;
end if;
Saved_Checks_TOS := Saved_Checks_TOS - 1;
end Conditional_Statements_End;
---------------------
-- Determine_Range --
---------------------
Cache_Size : constant := 2 ** 10;
type Cache_Index is range 0 .. Cache_Size - 1;
-- Determine size of below cache (power of 2 is more efficient!)
Determine_Range_Cache_N : array (Cache_Index) of Node_Id;
Determine_Range_Cache_Lo : array (Cache_Index) of Uint;
Determine_Range_Cache_Hi : array (Cache_Index) of Uint;
-- The above arrays are used to implement a small direct cache
-- for Determine_Range calls. Because of the way Determine_Range
-- recursively traces subexpressions, and because overflow checking
-- calls the routine on the way up the tree, a quadratic behavior
-- can otherwise be encountered in large expressions. The cache
-- entry for node N is stored in the (N mod Cache_Size) entry, and
-- can be validated by checking the actual node value stored there.
procedure Determine_Range
(N : Node_Id;
OK : out Boolean;
Lo : out Uint;
Hi : out Uint)
is
Typ : constant Entity_Id := Etype (N);
Lo_Left : Uint;
Hi_Left : Uint;
-- Lo and Hi bounds of left operand
Lo_Right : Uint;
Hi_Right : Uint;
-- Lo and Hi bounds of right (or only) operand
Bound : Node_Id;
-- Temp variable used to hold a bound node
Hbound : Uint;
-- High bound of base type of expression
Lor : Uint;
Hir : Uint;
-- Refined values for low and high bounds, after tightening
OK1 : Boolean;
-- Used in lower level calls to indicate if call succeeded
Cindex : Cache_Index;
-- Used to search cache
function OK_Operands return Boolean;
-- Used for binary operators. Determines the ranges of the left and
-- right operands, and if they are both OK, returns True, and puts
-- the results in Lo_Right, Hi_Right, Lo_Left, Hi_Left
-----------------
-- OK_Operands --
-----------------
function OK_Operands return Boolean is
begin
Determine_Range (Left_Opnd (N), OK1, Lo_Left, Hi_Left);
if not OK1 then
return False;
end if;
Determine_Range (Right_Opnd (N), OK1, Lo_Right, Hi_Right);
return OK1;
end OK_Operands;
-- Start of processing for Determine_Range
begin
-- Prevent junk warnings by initializing range variables
Lo := No_Uint;
Hi := No_Uint;
Lor := No_Uint;
Hir := No_Uint;
-- If the type is not discrete, or is undefined, then we can't
-- do anything about determining the range.
if No (Typ) or else not Is_Discrete_Type (Typ)
or else Error_Posted (N)
then
OK := False;
return;
end if;
-- For all other cases, we can determine the range
OK := True;
-- If value is compile time known, then the possible range is the
-- one value that we know this expression definitely has!
if Compile_Time_Known_Value (N) then
Lo := Expr_Value (N);
Hi := Lo;
return;
end if;
-- Return if already in the cache
Cindex := Cache_Index (N mod Cache_Size);
if Determine_Range_Cache_N (Cindex) = N then
Lo := Determine_Range_Cache_Lo (Cindex);
Hi := Determine_Range_Cache_Hi (Cindex);
return;
end if;
-- Otherwise, start by finding the bounds of the type of the
-- expression, the value cannot be outside this range (if it
-- is, then we have an overflow situation, which is a separate
-- check, we are talking here only about the expression value).
-- We use the actual bound unless it is dynamic, in which case
-- use the corresponding base type bound if possible. If we can't
-- get a bound then we figure we can't determine the range (a
-- peculiar case, that perhaps cannot happen, but there is no
-- point in bombing in this optimization circuit.
-- First the low bound
Bound := Type_Low_Bound (Typ);
if Compile_Time_Known_Value (Bound) then
Lo := Expr_Value (Bound);
elsif Compile_Time_Known_Value (Type_Low_Bound (Base_Type (Typ))) then
Lo := Expr_Value (Type_Low_Bound (Base_Type (Typ)));
else
OK := False;
return;
end if;
-- Now the high bound
Bound := Type_High_Bound (Typ);
-- We need the high bound of the base type later on, and this should
-- always be compile time known. Again, it is not clear that this
-- can ever be false, but no point in bombing.
if Compile_Time_Known_Value (Type_High_Bound (Base_Type (Typ))) then
Hbound := Expr_Value (Type_High_Bound (Base_Type (Typ)));
Hi := Hbound;
else
OK := False;
return;
end if;
-- If we have a static subtype, then that may have a tighter bound
-- so use the upper bound of the subtype instead in this case.
if Compile_Time_Known_Value (Bound) then
Hi := Expr_Value (Bound);
end if;
-- We may be able to refine this value in certain situations. If
-- refinement is possible, then Lor and Hir are set to possibly
-- tighter bounds, and OK1 is set to True.
case Nkind (N) is
-- For unary plus, result is limited by range of operand
when N_Op_Plus =>
Determine_Range (Right_Opnd (N), OK1, Lor, Hir);
-- For unary minus, determine range of operand, and negate it
when N_Op_Minus =>
Determine_Range (Right_Opnd (N), OK1, Lo_Right, Hi_Right);
if OK1 then
Lor := -Hi_Right;
Hir := -Lo_Right;
end if;
-- For binary addition, get range of each operand and do the
-- addition to get the result range.
when N_Op_Add =>
if OK_Operands then
Lor := Lo_Left + Lo_Right;
Hir := Hi_Left + Hi_Right;
end if;
-- Division is tricky. The only case we consider is where the
-- right operand is a positive constant, and in this case we
-- simply divide the bounds of the left operand
when N_Op_Divide =>
if OK_Operands then
if Lo_Right = Hi_Right
and then Lo_Right > 0
then
Lor := Lo_Left / Lo_Right;
Hir := Hi_Left / Lo_Right;
else
OK1 := False;
end if;
end if;
-- For binary subtraction, get range of each operand and do
-- the worst case subtraction to get the result range.
when N_Op_Subtract =>
if OK_Operands then
Lor := Lo_Left - Hi_Right;
Hir := Hi_Left - Lo_Right;
end if;
-- For MOD, if right operand is a positive constant, then
-- result must be in the allowable range of mod results.
when N_Op_Mod =>
if OK_Operands then
if Lo_Right = Hi_Right
and then Lo_Right /= 0
then
if Lo_Right > 0 then
Lor := Uint_0;
Hir := Lo_Right - 1;
else -- Lo_Right < 0
Lor := Lo_Right + 1;
Hir := Uint_0;
end if;
else
OK1 := False;
end if;
end if;
-- For REM, if right operand is a positive constant, then
-- result must be in the allowable range of mod results.
when N_Op_Rem =>
if OK_Operands then
if Lo_Right = Hi_Right
and then Lo_Right /= 0
then
declare
Dval : constant Uint := (abs Lo_Right) - 1;
begin
-- The sign of the result depends on the sign of the
-- dividend (but not on the sign of the divisor, hence
-- the abs operation above).
if Lo_Left < 0 then
Lor := -Dval;
else
Lor := Uint_0;
end if;
if Hi_Left < 0 then
Hir := Uint_0;
else
Hir := Dval;
end if;
end;
else
OK1 := False;
end if;
end if;
-- Attribute reference cases
when N_Attribute_Reference =>
case Attribute_Name (N) is
-- For Pos/Val attributes, we can refine the range using the
-- possible range of values of the attribute expression
when Name_Pos | Name_Val =>
Determine_Range (First (Expressions (N)), OK1, Lor, Hir);
-- For Length attribute, use the bounds of the corresponding
-- index type to refine the range.
when Name_Length =>
declare
Atyp : Entity_Id := Etype (Prefix (N));
Inum : Nat;
Indx : Node_Id;
LL, LU : Uint;
UL, UU : Uint;
begin
if Is_Access_Type (Atyp) then
Atyp := Designated_Type (Atyp);
end if;
-- For string literal, we know exact value
if Ekind (Atyp) = E_String_Literal_Subtype then
OK := True;
Lo := String_Literal_Length (Atyp);
Hi := String_Literal_Length (Atyp);
return;
end if;
-- Otherwise check for expression given
if No (Expressions (N)) then
Inum := 1;
else
Inum :=
UI_To_Int (Expr_Value (First (Expressions (N))));
end if;
Indx := First_Index (Atyp);
for J in 2 .. Inum loop
Indx := Next_Index (Indx);
end loop;
Determine_Range
(Type_Low_Bound (Etype (Indx)), OK1, LL, LU);
if OK1 then
Determine_Range
(Type_High_Bound (Etype (Indx)), OK1, UL, UU);
if OK1 then
-- The maximum value for Length is the biggest
-- possible gap between the values of the bounds.
-- But of course, this value cannot be negative.
Hir := UI_Max (Uint_0, UU - LL);
-- For constrained arrays, the minimum value for
-- Length is taken from the actual value of the
-- bounds, since the index will be exactly of
-- this subtype.
if Is_Constrained (Atyp) then
Lor := UI_Max (Uint_0, UL - LU);
-- For an unconstrained array, the minimum value
-- for length is always zero.
else
Lor := Uint_0;
end if;
end if;
end if;
end;
-- No special handling for other attributes
-- Probably more opportunities exist here ???
when others =>
OK1 := False;
end case;
-- For type conversion from one discrete type to another, we
-- can refine the range using the converted value.
when N_Type_Conversion =>
Determine_Range (Expression (N), OK1, Lor, Hir);
-- Nothing special to do for all other expression kinds
when others =>
OK1 := False;
Lor := No_Uint;
Hir := No_Uint;
end case;
-- At this stage, if OK1 is true, then we know that the actual
-- result of the computed expression is in the range Lor .. Hir.
-- We can use this to restrict the possible range of results.
if OK1 then
-- If the refined value of the low bound is greater than the
-- type high bound, then reset it to the more restrictive
-- value. However, we do NOT do this for the case of a modular
-- type where the possible upper bound on the value is above the
-- base type high bound, because that means the result could wrap.
if Lor > Lo
and then not (Is_Modular_Integer_Type (Typ)
and then Hir > Hbound)
then
Lo := Lor;
end if;
-- Similarly, if the refined value of the high bound is less
-- than the value so far, then reset it to the more restrictive
-- value. Again, we do not do this if the refined low bound is
-- negative for a modular type, since this would wrap.
if Hir < Hi
and then not (Is_Modular_Integer_Type (Typ)
and then Lor < Uint_0)
then
Hi := Hir;
end if;
end if;
-- Set cache entry for future call and we are all done
Determine_Range_Cache_N (Cindex) := N;
Determine_Range_Cache_Lo (Cindex) := Lo;
Determine_Range_Cache_Hi (Cindex) := Hi;
return;
-- If any exception occurs, it means that we have some bug in the compiler
-- possibly triggered by a previous error, or by some unforseen peculiar
-- occurrence. However, this is only an optimization attempt, so there is
-- really no point in crashing the compiler. Instead we just decide, too
-- bad, we can't figure out a range in this case after all.
exception
when others =>
-- Debug flag K disables this behavior (useful for debugging)
if Debug_Flag_K then
raise;
else
OK := False;
Lo := No_Uint;
Hi := No_Uint;
return;
end if;
end Determine_Range;
------------------------------------
-- Discriminant_Checks_Suppressed --
------------------------------------
function Discriminant_Checks_Suppressed (E : Entity_Id) return Boolean is
begin
if Present (E) then
if Is_Unchecked_Union (E) then
return True;
elsif Checks_May_Be_Suppressed (E) then
return Is_Check_Suppressed (E, Discriminant_Check);
end if;
end if;
return Scope_Suppress (Discriminant_Check);
end Discriminant_Checks_Suppressed;
--------------------------------
-- Division_Checks_Suppressed --
--------------------------------
function Division_Checks_Suppressed (E : Entity_Id) return Boolean is
begin
if Present (E) and then Checks_May_Be_Suppressed (E) then
return Is_Check_Suppressed (E, Division_Check);
else
return Scope_Suppress (Division_Check);
end if;
end Division_Checks_Suppressed;
-----------------------------------
-- Elaboration_Checks_Suppressed --
-----------------------------------
function Elaboration_Checks_Suppressed (E : Entity_Id) return Boolean is
begin
if Present (E) then
if Kill_Elaboration_Checks (E) then
return True;
elsif Checks_May_Be_Suppressed (E) then
return Is_Check_Suppressed (E, Elaboration_Check);
end if;
end if;
return Scope_Suppress (Elaboration_Check);
end Elaboration_Checks_Suppressed;
---------------------------
-- Enable_Overflow_Check --
---------------------------
procedure Enable_Overflow_Check (N : Node_Id) is
Typ : constant Entity_Id := Base_Type (Etype (N));
Chk : Nat;
OK : Boolean;
Ent : Entity_Id;
Ofs : Uint;
Lo : Uint;
Hi : Uint;
begin
if Debug_Flag_CC then
w ("Enable_Overflow_Check for node ", Int (N));
Write_Str (" Source location = ");
wl (Sloc (N));
pg (N);
end if;
-- Nothing to do if the range of the result is known OK. We skip
-- this for conversions, since the caller already did the check,
-- and in any case the condition for deleting the check for a
-- type conversion is different in any case.
if Nkind (N) /= N_Type_Conversion then
Determine_Range (N, OK, Lo, Hi);
-- Note in the test below that we assume that if a bound of the
-- range is equal to that of the type. That's not quite accurate
-- but we do this for the following reasons:
-- a) The way that Determine_Range works, it will typically report
-- the bounds of the value as being equal to the bounds of the
-- type, because it either can't tell anything more precise, or
-- does not think it is worth the effort to be more precise.
-- b) It is very unusual to have a situation in which this would
-- generate an unnecessary overflow check (an example would be
-- a subtype with a range 0 .. Integer'Last - 1 to which the
-- literal value one is added.
-- c) The alternative is a lot of special casing in this routine
-- which would partially duplicate Determine_Range processing.
if OK
and then Lo > Expr_Value (Type_Low_Bound (Typ))
and then Hi < Expr_Value (Type_High_Bound (Typ))
then
if Debug_Flag_CC then
w ("No overflow check required");
end if;
return;
end if;
end if;
-- If not in optimizing mode, set flag and we are done. We are also
-- done (and just set the flag) if the type is not a discrete type,
-- since it is not worth the effort to eliminate checks for other
-- than discrete types. In addition, we take this same path if we
-- have stored the maximum number of checks possible already (a
-- very unlikely situation, but we do not want to blow up!)
if Optimization_Level = 0
or else not Is_Discrete_Type (Etype (N))
or else Num_Saved_Checks = Saved_Checks'Last
then
Set_Do_Overflow_Check (N, True);
if Debug_Flag_CC then
w ("Optimization off");
end if;
return;
end if;
-- Otherwise evaluate and check the expression
Find_Check
(Expr => N,
Check_Type => 'O',
Target_Type => Empty,
Entry_OK => OK,
Check_Num => Chk,
Ent => Ent,
Ofs => Ofs);
if Debug_Flag_CC then
w ("Called Find_Check");
w (" OK = ", OK);
if OK then
w (" Check_Num = ", Chk);
w (" Ent = ", Int (Ent));
Write_Str (" Ofs = ");
pid (Ofs);
end if;
end if;
-- If check is not of form to optimize, then set flag and we are done
if not OK then
Set_Do_Overflow_Check (N, True);
return;
end if;
-- If check is already performed, then return without setting flag
if Chk /= 0 then
if Debug_Flag_CC then
w ("Check suppressed!");
end if;
return;
end if;
-- Here we will make a new entry for the new check
Set_Do_Overflow_Check (N, True);
Num_Saved_Checks := Num_Saved_Checks + 1;
Saved_Checks (Num_Saved_Checks) :=
(Killed => False,
Entity => Ent,
Offset => Ofs,
Check_Type => 'O',
Target_Type => Empty);
if Debug_Flag_CC then
w ("Make new entry, check number = ", Num_Saved_Checks);
w (" Entity = ", Int (Ent));
Write_Str (" Offset = ");
pid (Ofs);
w (" Check_Type = O");
w (" Target_Type = Empty");
end if;
-- If we get an exception, then something went wrong, probably because
-- of an error in the structure of the tree due to an incorrect program.
-- Or it may be a bug in the optimization circuit. In either case the
-- safest thing is simply to set the check flag unconditionally.
exception
when others =>
Set_Do_Overflow_Check (N, True);
if Debug_Flag_CC then
w (" exception occurred, overflow flag set");
end if;
return;
end Enable_Overflow_Check;
------------------------
-- Enable_Range_Check --
------------------------
procedure Enable_Range_Check (N : Node_Id) is
Chk : Nat;
OK : Boolean;
Ent : Entity_Id;
Ofs : Uint;
Ttyp : Entity_Id;
P : Node_Id;
begin
-- Return if unchecked type conversion with range check killed.
-- In this case we never set the flag (that's what Kill_Range_Check
-- is all about!)
if Nkind (N) = N_Unchecked_Type_Conversion
and then Kill_Range_Check (N)
then
return;
end if;
-- Debug trace output
if Debug_Flag_CC then
w ("Enable_Range_Check for node ", Int (N));
Write_Str (" Source location = ");
wl (Sloc (N));
pg (N);
end if;
-- If not in optimizing mode, set flag and we are done. We are also
-- done (and just set the flag) if the type is not a discrete type,
-- since it is not worth the effort to eliminate checks for other
-- than discrete types. In addition, we take this same path if we
-- have stored the maximum number of checks possible already (a
-- very unlikely situation, but we do not want to blow up!)
if Optimization_Level = 0
or else No (Etype (N))
or else not Is_Discrete_Type (Etype (N))
or else Num_Saved_Checks = Saved_Checks'Last
then
Set_Do_Range_Check (N, True);
if Debug_Flag_CC then
w ("Optimization off");
end if;
return;
end if;
-- Otherwise find out the target type
P := Parent (N);
-- For assignment, use left side subtype
if Nkind (P) = N_Assignment_Statement
and then Expression (P) = N
then
Ttyp := Etype (Name (P));
-- For indexed component, use subscript subtype
elsif Nkind (P) = N_Indexed_Component then
declare
Atyp : Entity_Id;
Indx : Node_Id;
Subs : Node_Id;
begin
Atyp := Etype (Prefix (P));
if Is_Access_Type (Atyp) then
Atyp := Designated_Type (Atyp);
end if;
Indx := First_Index (Atyp);
Subs := First (Expressions (P));
loop
if Subs = N then
Ttyp := Etype (Indx);
exit;
end if;
Next_Index (Indx);
Next (Subs);
end loop;
end;
-- For now, ignore all other cases, they are not so interesting
else
if Debug_Flag_CC then
w (" target type not found, flag set");
end if;
Set_Do_Range_Check (N, True);
return;
end if;
-- Evaluate and check the expression
Find_Check
(Expr => N,
Check_Type => 'R',
Target_Type => Ttyp,
Entry_OK => OK,
Check_Num => Chk,
Ent => Ent,
Ofs => Ofs);
if Debug_Flag_CC then
w ("Called Find_Check");
w ("Target_Typ = ", Int (Ttyp));
w (" OK = ", OK);
if OK then
w (" Check_Num = ", Chk);
w (" Ent = ", Int (Ent));
Write_Str (" Ofs = ");
pid (Ofs);
end if;
end if;
-- If check is not of form to optimize, then set flag and we are done
if not OK then
if Debug_Flag_CC then
w (" expression not of optimizable type, flag set");
end if;
Set_Do_Range_Check (N, True);
return;
end if;
-- If check is already performed, then return without setting flag
if Chk /= 0 then
if Debug_Flag_CC then
w ("Check suppressed!");
end if;
return;
end if;
-- Here we will make a new entry for the new check
Set_Do_Range_Check (N, True);
Num_Saved_Checks := Num_Saved_Checks + 1;
Saved_Checks (Num_Saved_Checks) :=
(Killed => False,
Entity => Ent,
Offset => Ofs,
Check_Type => 'R',
Target_Type => Ttyp);
if Debug_Flag_CC then
w ("Make new entry, check number = ", Num_Saved_Checks);
w (" Entity = ", Int (Ent));
Write_Str (" Offset = ");
pid (Ofs);
w (" Check_Type = R");
w (" Target_Type = ", Int (Ttyp));
pg (Ttyp);
end if;
-- If we get an exception, then something went wrong, probably because
-- of an error in the structure of the tree due to an incorrect program.
-- Or it may be a bug in the optimization circuit. In either case the
-- safest thing is simply to set the check flag unconditionally.
exception
when others =>
Set_Do_Range_Check (N, True);
if Debug_Flag_CC then
w (" exception occurred, range flag set");
end if;
return;
end Enable_Range_Check;
------------------
-- Ensure_Valid --
------------------
procedure Ensure_Valid (Expr : Node_Id; Holes_OK : Boolean := False) is
Typ : constant Entity_Id := Etype (Expr);
begin
-- Ignore call if we are not doing any validity checking
if not Validity_Checks_On then
return;
-- Ignore call if range checks suppressed on entity in question
elsif Is_Entity_Name (Expr)
and then Range_Checks_Suppressed (Entity (Expr))
then
return;
-- No check required if expression is from the expander, we assume
-- the expander will generate whatever checks are needed. Note that
-- this is not just an optimization, it avoids infinite recursions!
-- Unchecked conversions must be checked, unless they are initialized
-- scalar values, as in a component assignment in an init proc.
-- In addition, we force a check if Force_Validity_Checks is set
elsif not Comes_From_Source (Expr)
and then not Force_Validity_Checks
and then (Nkind (Expr) /= N_Unchecked_Type_Conversion
or else Kill_Range_Check (Expr))
then
return;
-- No check required if expression is known to have valid value
elsif Expr_Known_Valid (Expr) then
return;
-- No check required if checks off
elsif Range_Checks_Suppressed (Typ) then
return;
-- Ignore case of enumeration with holes where the flag is set not
-- to worry about holes, since no special validity check is needed
elsif Is_Enumeration_Type (Typ)
and then Has_Non_Standard_Rep (Typ)
and then Holes_OK
then
return;
-- No check required on the left-hand side of an assignment.
elsif Nkind (Parent (Expr)) = N_Assignment_Statement
and then Expr = Name (Parent (Expr))
then
return;
-- An annoying special case. If this is an out parameter of a scalar
-- type, then the value is not going to be accessed, therefore it is
-- inappropriate to do any validity check at the call site.
else
-- Only need to worry about scalar types
if Is_Scalar_Type (Typ) then
declare
P : Node_Id;
N : Node_Id;
E : Entity_Id;
F : Entity_Id;
A : Node_Id;
L : List_Id;
begin
-- Find actual argument (which may be a parameter association)
-- and the parent of the actual argument (the call statement)
N := Expr;
P := Parent (Expr);
if Nkind (P) = N_Parameter_Association then
N := P;
P := Parent (N);
end if;
-- Only need to worry if we are argument of a procedure
-- call since functions don't have out parameters. If this
-- is an indirect or dispatching call, get signature from
-- the subprogram type.
if Nkind (P) = N_Procedure_Call_Statement then
L := Parameter_Associations (P);
if Is_Entity_Name (Name (P)) then
E := Entity (Name (P));
else
pragma Assert (Nkind (Name (P)) = N_Explicit_Dereference);
E := Etype (Name (P));
end if;
-- Only need to worry if there are indeed actuals, and
-- if this could be a procedure call, otherwise we cannot
-- get a match (either we are not an argument, or the
-- mode of the formal is not OUT). This test also filters
-- out the generic case.
if Is_Non_Empty_List (L)
and then Is_Subprogram (E)
then
-- This is the loop through parameters, looking to
-- see if there is an OUT parameter for which we are
-- the argument.
F := First_Formal (E);
A := First (L);
while Present (F) loop
if Ekind (F) = E_Out_Parameter and then A = N then
return;
end if;
Next_Formal (F);
Next (A);
end loop;
end if;
end if;
end;
end if;
end if;
-- If we fall through, a validity check is required. Note that it would
-- not be good to set Do_Range_Check, even in contexts where this is
-- permissible, since this flag causes checking against the target type,
-- not the source type in contexts such as assignments
Insert_Valid_Check (Expr);
end Ensure_Valid;
----------------------
-- Expr_Known_Valid --
----------------------
function Expr_Known_Valid (Expr : Node_Id) return Boolean is
Typ : constant Entity_Id := Etype (Expr);
begin
-- Non-scalar types are always consdered valid, since they never
-- give rise to the issues of erroneous or bounded error behavior
-- that are the concern. In formal reference manual terms the
-- notion of validity only applies to scalar types.
if not Is_Scalar_Type (Typ) then
return True;
-- If no validity checking, then everything is considered valid
elsif not Validity_Checks_On then
return True;
-- Floating-point types are considered valid unless floating-point
-- validity checks have been specifically turned on.
elsif Is_Floating_Point_Type (Typ)
and then not Validity_Check_Floating_Point
then
return True;
-- If the expression is the value of an object that is known to
-- be valid, then clearly the expression value itself is valid.
elsif Is_Entity_Name (Expr)
and then Is_Known_Valid (Entity (Expr))
then
return True;
-- If the type is one for which all values are known valid, then
-- we are sure that the value is valid except in the slightly odd
-- case where the expression is a reference to a variable whose size
-- has been explicitly set to a value greater than the object size.
elsif Is_Known_Valid (Typ) then
if Is_Entity_Name (Expr)
and then Ekind (Entity (Expr)) = E_Variable
and then Esize (Entity (Expr)) > Esize (Typ)
then
return False;
else
return True;
end if;
-- Integer and character literals always have valid values, where
-- appropriate these will be range checked in any case.
elsif Nkind (Expr) = N_Integer_Literal
or else
Nkind (Expr) = N_Character_Literal
then
return True;
-- If we have a type conversion or a qualification of a known valid
-- value, then the result will always be valid.
elsif Nkind (Expr) = N_Type_Conversion
or else
Nkind (Expr) = N_Qualified_Expression
then
return Expr_Known_Valid (Expression (Expr));
-- The result of any function call or operator is always considered
-- valid, since we assume the necessary checks are done by the call.
elsif Nkind (Expr) in N_Binary_Op
or else
Nkind (Expr) in N_Unary_Op
or else
Nkind (Expr) = N_Function_Call
then
return True;
-- For all other cases, we do not know the expression is valid
else
return False;
end if;
end Expr_Known_Valid;
----------------
-- Find_Check --
----------------
procedure Find_Check
(Expr : Node_Id;
Check_Type : Character;
Target_Type : Entity_Id;
Entry_OK : out Boolean;
Check_Num : out Nat;
Ent : out Entity_Id;
Ofs : out Uint)
is
function Within_Range_Of
(Target_Type : Entity_Id;
Check_Type : Entity_Id)
return Boolean;
-- Given a requirement for checking a range against Target_Type, and
-- and a range Check_Type against which a check has already been made,
-- determines if the check against check type is sufficient to ensure
-- that no check against Target_Type is required.
---------------------
-- Within_Range_Of --
---------------------
function Within_Range_Of
(Target_Type : Entity_Id;
Check_Type : Entity_Id)
return Boolean
is
begin
if Target_Type = Check_Type then
return True;
else
declare
Tlo : constant Node_Id := Type_Low_Bound (Target_Type);
Thi : constant Node_Id := Type_High_Bound (Target_Type);
Clo : constant Node_Id := Type_Low_Bound (Check_Type);
Chi : constant Node_Id := Type_High_Bound (Check_Type);
begin
if (Tlo = Clo
or else (Compile_Time_Known_Value (Tlo)
and then
Compile_Time_Known_Value (Clo)
and then
Expr_Value (Clo) >= Expr_Value (Tlo)))
and then
(Thi = Chi
or else (Compile_Time_Known_Value (Thi)
and then
Compile_Time_Known_Value (Chi)
and then
Expr_Value (Chi) <= Expr_Value (Clo)))
then
return True;
else
return False;
end if;
end;
end if;
end Within_Range_Of;
-- Start of processing for Find_Check
begin
-- Establish default, to avoid warnings from GCC.
Check_Num := 0;
-- Case of expression is simple entity reference
if Is_Entity_Name (Expr) then
Ent := Entity (Expr);
Ofs := Uint_0;
-- Case of expression is entity + known constant
elsif Nkind (Expr) = N_Op_Add
and then Compile_Time_Known_Value (Right_Opnd (Expr))
and then Is_Entity_Name (Left_Opnd (Expr))
then
Ent := Entity (Left_Opnd (Expr));
Ofs := Expr_Value (Right_Opnd (Expr));
-- Case of expression is entity - known constant
elsif Nkind (Expr) = N_Op_Subtract
and then Compile_Time_Known_Value (Right_Opnd (Expr))
and then Is_Entity_Name (Left_Opnd (Expr))
then
Ent := Entity (Left_Opnd (Expr));
Ofs := UI_Negate (Expr_Value (Right_Opnd (Expr)));
-- Any other expression is not of the right form
else
Ent := Empty;
Ofs := Uint_0;
Entry_OK := False;
return;
end if;
-- Come here with expression of appropriate form, check if
-- entity is an appropriate one for our purposes.
if (Ekind (Ent) = E_Variable
or else
Ekind (Ent) = E_Constant
or else
Ekind (Ent) = E_Loop_Parameter
or else
Ekind (Ent) = E_In_Parameter)
and then not Is_Library_Level_Entity (Ent)
then
Entry_OK := True;
else
Entry_OK := False;
return;
end if;
-- See if there is matching check already
for J in reverse 1 .. Num_Saved_Checks loop
declare
SC : Saved_Check renames Saved_Checks (J);
begin
if SC.Killed = False
and then SC.Entity = Ent
and then SC.Offset = Ofs
and then SC.Check_Type = Check_Type
and then Within_Range_Of (Target_Type, SC.Target_Type)
then
Check_Num := J;
return;
end if;
end;
end loop;
-- If we fall through entry was not found
Check_Num := 0;
return;
end Find_Check;
---------------------------------
-- Generate_Discriminant_Check --
---------------------------------
-- Note: the code for this procedure is derived from the
-- emit_discriminant_check routine a-trans.c v1.659.
procedure Generate_Discriminant_Check (N : Node_Id) is
Loc : constant Source_Ptr := Sloc (N);
Pref : constant Node_Id := Prefix (N);
Sel : constant Node_Id := Selector_Name (N);
Orig_Comp : constant Entity_Id :=
Original_Record_Component (Entity (Sel));
-- The original component to be checked
Discr_Fct : constant Entity_Id :=
Discriminant_Checking_Func (Orig_Comp);
-- The discriminant checking function
Discr : Entity_Id;
-- One discriminant to be checked in the type
Real_Discr : Entity_Id;
-- Actual discriminant in the call
Pref_Type : Entity_Id;
-- Type of relevant prefix (ignoring private/access stuff)
Args : List_Id;
-- List of arguments for function call
Formal : Entity_Id;
-- Keep track of the formal corresponding to the actual we build
-- for each discriminant, in order to be able to perform the
-- necessary type conversions.
Scomp : Node_Id;
-- Selected component reference for checking function argument
begin
Pref_Type := Etype (Pref);
-- Force evaluation of the prefix, so that it does not get evaluated
-- twice (once for the check, once for the actual reference). Such a
-- double evaluation is always a potential source of inefficiency,
-- and is functionally incorrect in the volatile case, or when the
-- prefix may have side-effects. An entity or a component of an
-- entity requires no evaluation.
if Is_Entity_Name (Pref) then
if Treat_As_Volatile (Entity (Pref)) then
Force_Evaluation (Pref, Name_Req => True);
end if;
elsif Treat_As_Volatile (Etype (Pref)) then
Force_Evaluation (Pref, Name_Req => True);
elsif Nkind (Pref) = N_Selected_Component
and then Is_Entity_Name (Prefix (Pref))
then
null;
else
Force_Evaluation (Pref, Name_Req => True);
end if;
-- For a tagged type, use the scope of the original component to
-- obtain the type, because ???
if Is_Tagged_Type (Scope (Orig_Comp)) then
Pref_Type := Scope (Orig_Comp);
-- For an untagged derived type, use the discriminants of the
-- parent which have been renamed in the derivation, possibly
-- by a one-to-many discriminant constraint.
-- For non-tagged type, initially get the Etype of the prefix
else
if Is_Derived_Type (Pref_Type)
and then Number_Discriminants (Pref_Type) /=
Number_Discriminants (Etype (Base_Type (Pref_Type)))
then
Pref_Type := Etype (Base_Type (Pref_Type));
end if;
end if;
-- We definitely should have a checking function, This routine should
-- not be called if no discriminant checking function is present.
pragma Assert (Present (Discr_Fct));
-- Create the list of the actual parameters for the call. This list
-- is the list of the discriminant fields of the record expression to
-- be discriminant checked.
Args := New_List;
Formal := First_Formal (Discr_Fct);
Discr := First_Discriminant (Pref_Type);
while Present (Discr) loop
-- If we have a corresponding discriminant field, and a parent
-- subtype is present, then we want to use the corresponding
-- discriminant since this is the one with the useful value.
if Present (Corresponding_Discriminant (Discr))
and then Ekind (Pref_Type) = E_Record_Type
and then Present (Parent_Subtype (Pref_Type))
then
Real_Discr := Corresponding_Discriminant (Discr);
else
Real_Discr := Discr;
end if;
-- Construct the reference to the discriminant
Scomp :=
Make_Selected_Component (Loc,
Prefix =>
Unchecked_Convert_To (Pref_Type,
Duplicate_Subexpr (Pref)),
Selector_Name => New_Occurrence_Of (Real_Discr, Loc));
-- Manually analyze and resolve this selected component. We really
-- want it just as it appears above, and do not want the expander
-- playing discriminal games etc with this reference. Then we
-- append the argument to the list we are gathering.
Set_Etype (Scomp, Etype (Real_Discr));
Set_Analyzed (Scomp, True);
Append_To (Args, Convert_To (Etype (Formal), Scomp));
Next_Formal_With_Extras (Formal);
Next_Discriminant (Discr);
end loop;
-- Now build and insert the call
Insert_Action (N,
Make_Raise_Constraint_Error (Loc,
Condition =>
Make_Function_Call (Loc,
Name => New_Occurrence_Of (Discr_Fct, Loc),
Parameter_Associations => Args),
Reason => CE_Discriminant_Check_Failed));
end Generate_Discriminant_Check;
----------------------------
-- Generate_Index_Checks --
----------------------------
procedure Generate_Index_Checks (N : Node_Id) is
Loc : constant Source_Ptr := Sloc (N);
A : constant Node_Id := Prefix (N);
Sub : Node_Id;
Ind : Nat;
Num : List_Id;
begin
Sub := First (Expressions (N));
Ind := 1;
while Present (Sub) loop
if Do_Range_Check (Sub) then
Set_Do_Range_Check (Sub, False);
-- Force evaluation except for the case of a simple name of
-- a non-volatile entity.
if not Is_Entity_Name (Sub)
or else Treat_As_Volatile (Entity (Sub))
then
Force_Evaluation (Sub);
end if;
-- Generate a raise of constraint error with the appropriate
-- reason and a condition of the form:
-- Base_Type(Sub) not in array'range (subscript)
-- Note that the reason we generate the conversion to the
-- base type here is that we definitely want the range check
-- to take place, even if it looks like the subtype is OK.
-- Optimization considerations that allow us to omit the
-- check have already been taken into account in the setting
-- of the Do_Range_Check flag earlier on.
if Ind = 1 then
Num := No_List;
else
Num := New_List (Make_Integer_Literal (Loc, Ind));
end if;
Insert_Action (N,
Make_Raise_Constraint_Error (Loc,
Condition =>
Make_Not_In (Loc,
Left_Opnd =>
Convert_To (Base_Type (Etype (Sub)),
Duplicate_Subexpr_Move_Checks (Sub)),
Right_Opnd =>
Make_Attribute_Reference (Loc,
Prefix => Duplicate_Subexpr_Move_Checks (A),
Attribute_Name => Name_Range,
Expressions => Num)),
Reason => CE_Index_Check_Failed));
end if;
Ind := Ind + 1;
Next (Sub);
end loop;
end Generate_Index_Checks;
--------------------------
-- Generate_Range_Check --
--------------------------
procedure Generate_Range_Check
(N : Node_Id;
Target_Type : Entity_Id;
Reason : RT_Exception_Code)
is
Loc : constant Source_Ptr := Sloc (N);
Source_Type : constant Entity_Id := Etype (N);
Source_Base_Type : constant Entity_Id := Base_Type (Source_Type);
Target_Base_Type : constant Entity_Id := Base_Type (Target_Type);
begin
-- First special case, if the source type is already within the
-- range of the target type, then no check is needed (probably we
-- should have stopped Do_Range_Check from being set in the first
-- place, but better late than later in preventing junk code!
-- We do NOT apply this if the source node is a literal, since in
-- this case the literal has already been labeled as having the
-- subtype of the target.
if In_Subrange_Of (Source_Type, Target_Type)
and then not
(Nkind (N) = N_Integer_Literal
or else
Nkind (N) = N_Real_Literal
or else
Nkind (N) = N_Character_Literal
or else
(Is_Entity_Name (N)
and then Ekind (Entity (N)) = E_Enumeration_Literal))
then
return;
end if;
-- We need a check, so force evaluation of the node, so that it does
-- not get evaluated twice (once for the check, once for the actual
-- reference). Such a double evaluation is always a potential source
-- of inefficiency, and is functionally incorrect in the volatile case.
if not Is_Entity_Name (N)
or else Treat_As_Volatile (Entity (N))
then
Force_Evaluation (N);
end if;
-- The easiest case is when Source_Base_Type and Target_Base_Type
-- are the same since in this case we can simply do a direct
-- check of the value of N against the bounds of Target_Type.
-- [constraint_error when N not in Target_Type]
-- Note: this is by far the most common case, for example all cases of
-- checks on the RHS of assignments are in this category, but not all
-- cases are like this. Notably conversions can involve two types.
if Source_Base_Type = Target_Base_Type then
Insert_Action (N,
Make_Raise_Constraint_Error (Loc,
Condition =>
Make_Not_In (Loc,
Left_Opnd => Duplicate_Subexpr (N),
Right_Opnd => New_Occurrence_Of (Target_Type, Loc)),
Reason => Reason));
-- Next test for the case where the target type is within the bounds
-- of the base type of the source type, since in this case we can
-- simply convert these bounds to the base type of T to do the test.
-- [constraint_error when N not in
-- Source_Base_Type (Target_Type'First)
-- ..
-- Source_Base_Type(Target_Type'Last))]
-- The conversions will always work and need no check.
elsif In_Subrange_Of (Target_Type, Source_Base_Type) then
Insert_Action (N,
Make_Raise_Constraint_Error (Loc,
Condition =>
Make_Not_In (Loc,
Left_Opnd => Duplicate_Subexpr (N),
Right_Opnd =>
Make_Range (Loc,
Low_Bound =>
Convert_To (Source_Base_Type,
Make_Attribute_Reference (Loc,
Prefix =>
New_Occurrence_Of (Target_Type, Loc),
Attribute_Name => Name_First)),
High_Bound =>
Convert_To (Source_Base_Type,
Make_Attribute_Reference (Loc,
Prefix =>
New_Occurrence_Of (Target_Type, Loc),
Attribute_Name => Name_Last)))),
Reason => Reason));
-- Note that at this stage we now that the Target_Base_Type is
-- not in the range of the Source_Base_Type (since even the
-- Target_Type itself is not in this range). It could still be
-- the case that the Source_Type is in range of the target base
-- type, since we have not checked that case.
-- If that is the case, we can freely convert the source to the
-- target, and then test the target result against the bounds.
elsif In_Subrange_Of (Source_Type, Target_Base_Type) then
-- We make a temporary to hold the value of the converted
-- value (converted to the base type), and then we will
-- do the test against this temporary.
-- Tnn : constant Target_Base_Type := Target_Base_Type (N);
-- [constraint_error when Tnn not in Target_Type]
-- Then the conversion itself is replaced by an occurrence of Tnn
declare
Tnn : constant Entity_Id :=
Make_Defining_Identifier (Loc,
Chars => New_Internal_Name ('T'));
begin
Insert_Actions (N, New_List (
Make_Object_Declaration (Loc,
Defining_Identifier => Tnn,
Object_Definition =>
New_Occurrence_Of (Target_Base_Type, Loc),
Constant_Present => True,
Expression =>
Make_Type_Conversion (Loc,
Subtype_Mark => New_Occurrence_Of (Target_Base_Type, Loc),
Expression => Duplicate_Subexpr (N))),
Make_Raise_Constraint_Error (Loc,
Condition =>
Make_Not_In (Loc,
Left_Opnd => New_Occurrence_Of (Tnn, Loc),
Right_Opnd => New_Occurrence_Of (Target_Type, Loc)),
Reason => Reason)));
Rewrite (N, New_Occurrence_Of (Tnn, Loc));
end;
-- At this stage, we know that we have two scalar types, which are
-- directly convertible, and where neither scalar type has a base
-- range that is in the range of the other scalar type.
-- The only way this can happen is with a signed and unsigned type.
-- So test for these two cases:
else
-- Case of the source is unsigned and the target is signed
if Is_Unsigned_Type (Source_Base_Type)
and then not Is_Unsigned_Type (Target_Base_Type)
then
-- If the source is unsigned and the target is signed, then we
-- know that the source is not shorter than the target (otherwise
-- the source base type would be in the target base type range).
-- In other words, the unsigned type is either the same size
-- as the target, or it is larger. It cannot be smaller.
pragma Assert
(Esize (Source_Base_Type) >= Esize (Target_Base_Type));
-- We only need to check the low bound if the low bound of the
-- target type is non-negative. If the low bound of the target
-- type is negative, then we know that we will fit fine.
-- If the high bound of the target type is negative, then we
-- know we have a constraint error, since we can't possibly
-- have a negative source.
-- With these two checks out of the way, we can do the check
-- using the source type safely
-- This is definitely the most annoying case!
-- [constraint_error
-- when (Target_Type'First >= 0
-- and then
-- N < Source_Base_Type (Target_Type'First))
-- or else Target_Type'Last < 0
-- or else N > Source_Base_Type (Target_Type'Last)];
-- We turn off all checks since we know that the conversions
-- will work fine, given the guards for negative values.
Insert_Action (N,
Make_Raise_Constraint_Error (Loc,
Condition =>
Make_Or_Else (Loc,
Make_Or_Else (Loc,
Left_Opnd =>
Make_And_Then (Loc,
Left_Opnd => Make_Op_Ge (Loc,
Left_Opnd =>
Make_Attribute_Reference (Loc,
Prefix =>
New_Occurrence_Of (Target_Type, Loc),
Attribute_Name => Name_First),
Right_Opnd => Make_Integer_Literal (Loc, Uint_0)),
Right_Opnd =>
Make_Op_Lt (Loc,
Left_Opnd => Duplicate_Subexpr (N),
Right_Opnd =>
Convert_To (Source_Base_Type,
Make_Attribute_Reference (Loc,
Prefix =>
New_Occurrence_Of (Target_Type, Loc),
Attribute_Name => Name_First)))),
Right_Opnd =>
Make_Op_Lt (Loc,
Left_Opnd =>
Make_Attribute_Reference (Loc,
Prefix => New_Occurrence_Of (Target_Type, Loc),
Attribute_Name => Name_Last),
Right_Opnd => Make_Integer_Literal (Loc, Uint_0))),
Right_Opnd =>
Make_Op_Gt (Loc,
Left_Opnd => Duplicate_Subexpr (N),
Right_Opnd =>
Convert_To (Source_Base_Type,
Make_Attribute_Reference (Loc,
Prefix => New_Occurrence_Of (Target_Type, Loc),
Attribute_Name => Name_Last)))),
Reason => Reason),
Suppress => All_Checks);
-- Only remaining possibility is that the source is signed and
-- the target is unsigned
else
pragma Assert (not Is_Unsigned_Type (Source_Base_Type)
and then Is_Unsigned_Type (Target_Base_Type));
-- If the source is signed and the target is unsigned, then
-- we know that the target is not shorter than the source
-- (otherwise the target base type would be in the source
-- base type range).
-- In other words, the unsigned type is either the same size
-- as the target, or it is larger. It cannot be smaller.
-- Clearly we have an error if the source value is negative
-- since no unsigned type can have negative values. If the
-- source type is non-negative, then the check can be done
-- using the target type.
-- Tnn : constant Target_Base_Type (N) := Target_Type;
-- [constraint_error
-- when N < 0 or else Tnn not in Target_Type];
-- We turn off all checks for the conversion of N to the
-- target base type, since we generate the explicit check
-- to ensure that the value is non-negative
declare
Tnn : constant Entity_Id :=
Make_Defining_Identifier (Loc,
Chars => New_Internal_Name ('T'));
begin
Insert_Actions (N, New_List (
Make_Object_Declaration (Loc,
Defining_Identifier => Tnn,
Object_Definition =>
New_Occurrence_Of (Target_Base_Type, Loc),
Constant_Present => True,
Expression =>
Make_Type_Conversion (Loc,
Subtype_Mark =>
New_Occurrence_Of (Target_Base_Type, Loc),
Expression => Duplicate_Subexpr (N))),
Make_Raise_Constraint_Error (Loc,
Condition =>
Make_Or_Else (Loc,
Left_Opnd =>
Make_Op_Lt (Loc,
Left_Opnd => Duplicate_Subexpr (N),
Right_Opnd => Make_Integer_Literal (Loc, Uint_0)),
Right_Opnd =>
Make_Not_In (Loc,
Left_Opnd => New_Occurrence_Of (Tnn, Loc),
Right_Opnd =>
New_Occurrence_Of (Target_Type, Loc))),
Reason => Reason)),
Suppress => All_Checks);
-- Set the Etype explicitly, because Insert_Actions may
-- have placed the declaration in the freeze list for an
-- enclosing construct, and thus it is not analyzed yet.
Set_Etype (Tnn, Target_Base_Type);
Rewrite (N, New_Occurrence_Of (Tnn, Loc));
end;
end if;
end if;
end Generate_Range_Check;
---------------------
-- Get_Discriminal --
---------------------
function Get_Discriminal (E : Entity_Id; Bound : Node_Id) return Node_Id is
Loc : constant Source_Ptr := Sloc (E);
D : Entity_Id;
Sc : Entity_Id;
begin
-- The entity E is the type of a private component of the protected
-- type, or the type of a renaming of that component within a protected
-- operation of that type.
Sc := Scope (E);
if Ekind (Sc) /= E_Protected_Type then
Sc := Scope (Sc);
if Ekind (Sc) /= E_Protected_Type then
return Bound;
end if;
end if;
D := First_Discriminant (Sc);
while Present (D)
and then Chars (D) /= Chars (Bound)
loop
Next_Discriminant (D);
end loop;
return New_Occurrence_Of (Discriminal (D), Loc);
end Get_Discriminal;
------------------
-- Guard_Access --
------------------
function Guard_Access
(Cond : Node_Id;
Loc : Source_Ptr;
Ck_Node : Node_Id)
return Node_Id
is
begin
if Nkind (Cond) = N_Or_Else then
Set_Paren_Count (Cond, 1);
end if;
if Nkind (Ck_Node) = N_Allocator then
return Cond;
else
return
Make_And_Then (Loc,
Left_Opnd =>
Make_Op_Ne (Loc,
Left_Opnd => Duplicate_Subexpr_No_Checks (Ck_Node),
Right_Opnd => Make_Null (Loc)),
Right_Opnd => Cond);
end if;
end Guard_Access;
-----------------------------
-- Index_Checks_Suppressed --
-----------------------------
function Index_Checks_Suppressed (E : Entity_Id) return Boolean is
begin
if Present (E) and then Checks_May_Be_Suppressed (E) then
return Is_Check_Suppressed (E, Index_Check);
else
return Scope_Suppress (Index_Check);
end if;
end Index_Checks_Suppressed;
----------------
-- Initialize --
----------------
procedure Initialize is
begin
for J in Determine_Range_Cache_N'Range loop
Determine_Range_Cache_N (J) := Empty;
end loop;
end Initialize;
-------------------------
-- Insert_Range_Checks --
-------------------------
procedure Insert_Range_Checks
(Checks : Check_Result;
Node : Node_Id;
Suppress_Typ : Entity_Id;
Static_Sloc : Source_Ptr := No_Location;
Flag_Node : Node_Id := Empty;
Do_Before : Boolean := False)
is
Internal_Flag_Node : Node_Id := Flag_Node;
Internal_Static_Sloc : Source_Ptr := Static_Sloc;
Check_Node : Node_Id;
Checks_On : constant Boolean :=
(not Index_Checks_Suppressed (Suppress_Typ))
or else
(not Range_Checks_Suppressed (Suppress_Typ));
begin
-- For now we just return if Checks_On is false, however this should
-- be enhanced to check for an always True value in the condition
-- and to generate a compilation warning???
if not Expander_Active or else not Checks_On then
return;
end if;
if Static_Sloc = No_Location then
Internal_Static_Sloc := Sloc (Node);
end if;
if No (Flag_Node) then
Internal_Flag_Node := Node;
end if;
for J in 1 .. 2 loop
exit when No (Checks (J));
if Nkind (Checks (J)) = N_Raise_Constraint_Error
and then Present (Condition (Checks (J)))
then
if not Has_Dynamic_Range_Check (Internal_Flag_Node) then
Check_Node := Checks (J);
Mark_Rewrite_Insertion (Check_Node);
if Do_Before then
Insert_Before_And_Analyze (Node, Check_Node);
else
Insert_After_And_Analyze (Node, Check_Node);
end if;
Set_Has_Dynamic_Range_Check (Internal_Flag_Node);
end if;
else
Check_Node :=
Make_Raise_Constraint_Error (Internal_Static_Sloc,
Reason => CE_Range_Check_Failed);
Mark_Rewrite_Insertion (Check_Node);
if Do_Before then
Insert_Before_And_Analyze (Node, Check_Node);
else
Insert_After_And_Analyze (Node, Check_Node);
end if;
end if;
end loop;
end Insert_Range_Checks;
------------------------
-- Insert_Valid_Check --
------------------------
procedure Insert_Valid_Check (Expr : Node_Id) is
Loc : constant Source_Ptr := Sloc (Expr);
Exp : Node_Id;
begin
-- Do not insert if checks off, or if not checking validity
if Range_Checks_Suppressed (Etype (Expr))
or else (not Validity_Checks_On)
then
return;
end if;
-- If we have a checked conversion, then validity check applies to
-- the expression inside the conversion, not the result, since if
-- the expression inside is valid, then so is the conversion result.
Exp := Expr;
while Nkind (Exp) = N_Type_Conversion loop
Exp := Expression (Exp);
end loop;
-- Insert the validity check. Note that we do this with validity
-- checks turned off, to avoid recursion, we do not want validity
-- checks on the validity checking code itself!
Validity_Checks_On := False;
Insert_Action
(Expr,
Make_Raise_Constraint_Error (Loc,
Condition =>
Make_Op_Not (Loc,
Right_Opnd =>
Make_Attribute_Reference (Loc,
Prefix =>
Duplicate_Subexpr_No_Checks (Exp, Name_Req => True),
Attribute_Name => Name_Valid)),
Reason => CE_Invalid_Data),
Suppress => All_Checks);
Validity_Checks_On := True;
end Insert_Valid_Check;
--------------------------
-- Install_Static_Check --
--------------------------
procedure Install_Static_Check (R_Cno : Node_Id; Loc : Source_Ptr) is
Stat : constant Boolean := Is_Static_Expression (R_Cno);
Typ : constant Entity_Id := Etype (R_Cno);
begin
Rewrite (R_Cno,
Make_Raise_Constraint_Error (Loc,
Reason => CE_Range_Check_Failed));
Set_Analyzed (R_Cno);
Set_Etype (R_Cno, Typ);
Set_Raises_Constraint_Error (R_Cno);
Set_Is_Static_Expression (R_Cno, Stat);
end Install_Static_Check;
---------------------
-- Kill_All_Checks --
---------------------
procedure Kill_All_Checks is
begin
if Debug_Flag_CC then
w ("Kill_All_Checks");
end if;
-- We reset the number of saved checks to zero, and also modify
-- all stack entries for statement ranges to indicate that the
-- number of checks at each level is now zero.
Num_Saved_Checks := 0;
for J in 1 .. Saved_Checks_TOS loop
Saved_Checks_Stack (J) := 0;
end loop;
end Kill_All_Checks;
-----------------
-- Kill_Checks --
-----------------
procedure Kill_Checks (V : Entity_Id) is
begin
if Debug_Flag_CC then
w ("Kill_Checks for entity", Int (V));
end if;
for J in 1 .. Num_Saved_Checks loop
if Saved_Checks (J).Entity = V then
if Debug_Flag_CC then
w (" Checks killed for saved check ", J);
end if;
Saved_Checks (J).Killed := True;
end if;
end loop;
end Kill_Checks;
------------------------------
-- Length_Checks_Suppressed --
------------------------------
function Length_Checks_Suppressed (E : Entity_Id) return Boolean is
begin
if Present (E) and then Checks_May_Be_Suppressed (E) then
return Is_Check_Suppressed (E, Length_Check);
else
return Scope_Suppress (Length_Check);
end if;
end Length_Checks_Suppressed;
--------------------------------
-- Overflow_Checks_Suppressed --
--------------------------------
function Overflow_Checks_Suppressed (E : Entity_Id) return Boolean is
begin
if Present (E) and then Checks_May_Be_Suppressed (E) then
return Is_Check_Suppressed (E, Overflow_Check);
else
return Scope_Suppress (Overflow_Check);
end if;
end Overflow_Checks_Suppressed;
-----------------
-- Range_Check --
-----------------
function Range_Check
(Ck_Node : Node_Id;
Target_Typ : Entity_Id;
Source_Typ : Entity_Id := Empty;
Warn_Node : Node_Id := Empty)
return Check_Result
is
begin
return Selected_Range_Checks
(Ck_Node, Target_Typ, Source_Typ, Warn_Node);
end Range_Check;
-----------------------------
-- Range_Checks_Suppressed --
-----------------------------
function Range_Checks_Suppressed (E : Entity_Id) return Boolean is
begin
if Present (E) then
-- Note: for now we always suppress range checks on Vax float types,
-- since Gigi does not know how to generate these checks.
if Vax_Float (E) then
return True;
elsif Kill_Range_Checks (E) then
return True;
elsif Checks_May_Be_Suppressed (E) then
return Is_Check_Suppressed (E, Range_Check);
end if;
end if;
return Scope_Suppress (Range_Check);
end Range_Checks_Suppressed;
-------------------
-- Remove_Checks --
-------------------
procedure Remove_Checks (Expr : Node_Id) is
Discard : Traverse_Result;
pragma Warnings (Off, Discard);
function Process (N : Node_Id) return Traverse_Result;
-- Process a single node during the traversal
function Traverse is new Traverse_Func (Process);
-- The traversal function itself
-------------
-- Process --
-------------
function Process (N : Node_Id) return Traverse_Result is
begin
if Nkind (N) not in N_Subexpr then
return Skip;
end if;
Set_Do_Range_Check (N, False);
case Nkind (N) is
when N_And_Then =>
Discard := Traverse (Left_Opnd (N));
return Skip;
when N_Attribute_Reference =>
Set_Do_Overflow_Check (N, False);
when N_Function_Call =>
Set_Do_Tag_Check (N, False);
when N_Op =>
Set_Do_Overflow_Check (N, False);
case Nkind (N) is
when N_Op_Divide =>
Set_Do_Division_Check (N, False);
when N_Op_And =>
Set_Do_Length_Check (N, False);
when N_Op_Mod =>
Set_Do_Division_Check (N, False);
when N_Op_Or =>
Set_Do_Length_Check (N, False);
when N_Op_Rem =>
Set_Do_Division_Check (N, False);
when N_Op_Xor =>
Set_Do_Length_Check (N, False);
when others =>
null;
end case;
when N_Or_Else =>
Discard := Traverse (Left_Opnd (N));
return Skip;
when N_Selected_Component =>
Set_Do_Discriminant_Check (N, False);
when N_Type_Conversion =>
Set_Do_Length_Check (N, False);
Set_Do_Tag_Check (N, False);
Set_Do_Overflow_Check (N, False);
when others =>
null;
end case;
return OK;
end Process;
-- Start of processing for Remove_Checks
begin
Discard := Traverse (Expr);
end Remove_Checks;
----------------------------
-- Selected_Length_Checks --
----------------------------
function Selected_Length_Checks
(Ck_Node : Node_Id;
Target_Typ : Entity_Id;
Source_Typ : Entity_Id;
Warn_Node : Node_Id)
return Check_Result
is
Loc : constant Source_Ptr := Sloc (Ck_Node);
S_Typ : Entity_Id;
T_Typ : Entity_Id;
Expr_Actual : Node_Id;
Exptyp : Entity_Id;
Cond : Node_Id := Empty;
Do_Access : Boolean := False;
Wnode : Node_Id := Warn_Node;
Ret_Result : Check_Result := (Empty, Empty);
Num_Checks : Natural := 0;
procedure Add_Check (N : Node_Id);
-- Adds the action given to Ret_Result if N is non-Empty
function Get_E_Length (E : Entity_Id; Indx : Nat) return Node_Id;
function Get_N_Length (N : Node_Id; Indx : Nat) return Node_Id;
function Same_Bounds (L : Node_Id; R : Node_Id) return Boolean;
-- True for equal literals and for nodes that denote the same constant
-- entity, even if its value is not a static constant. This includes the
-- case of a discriminal reference within an init proc. Removes some
-- obviously superfluous checks.
function Length_E_Cond
(Exptyp : Entity_Id;
Typ : Entity_Id;
Indx : Nat)
return Node_Id;
-- Returns expression to compute:
-- Typ'Length /= Exptyp'Length
function Length_N_Cond
(Expr : Node_Id;
Typ : Entity_Id;
Indx : Nat)
return Node_Id;
-- Returns expression to compute:
-- Typ'Length /= Expr'Length
---------------
-- Add_Check --
---------------
procedure Add_Check (N : Node_Id) is
begin
if Present (N) then
-- For now, ignore attempt to place more than 2 checks ???
if Num_Checks = 2 then
return;
end if;
pragma Assert (Num_Checks <= 1);
Num_Checks := Num_Checks + 1;
Ret_Result (Num_Checks) := N;
end if;
end Add_Check;
------------------
-- Get_E_Length --
------------------
function Get_E_Length (E : Entity_Id; Indx : Nat) return Node_Id is
Pt : constant Entity_Id := Scope (Scope (E));
N : Node_Id;
E1 : Entity_Id := E;
begin
if Ekind (Scope (E)) = E_Record_Type
and then Has_Discriminants (Scope (E))
then
N := Build_Discriminal_Subtype_Of_Component (E);
if Present (N) then
Insert_Action (Ck_Node, N);
E1 := Defining_Identifier (N);
end if;
end if;
if Ekind (E1) = E_String_Literal_Subtype then
return
Make_Integer_Literal (Loc,
Intval => String_Literal_Length (E1));
elsif Ekind (Pt) = E_Protected_Type
and then Has_Discriminants (Pt)
and then Has_Completion (Pt)
and then not Inside_Init_Proc
then
-- If the type whose length is needed is a private component
-- constrained by a discriminant, we must expand the 'Length
-- attribute into an explicit computation, using the discriminal
-- of the current protected operation. This is because the actual
-- type of the prival is constructed after the protected opera-
-- tion has been fully expanded.
declare
Indx_Type : Node_Id;
Lo : Node_Id;
Hi : Node_Id;
Do_Expand : Boolean := False;
begin
Indx_Type := First_Index (E);
for J in 1 .. Indx - 1 loop
Next_Index (Indx_Type);
end loop;
Get_Index_Bounds (Indx_Type, Lo, Hi);
if Nkind (Lo) = N_Identifier
and then Ekind (Entity (Lo)) = E_In_Parameter
then
Lo := Get_Discriminal (E, Lo);
Do_Expand := True;
end if;
if Nkind (Hi) = N_Identifier
and then Ekind (Entity (Hi)) = E_In_Parameter
then
Hi := Get_Discriminal (E, Hi);
Do_Expand := True;
end if;
if Do_Expand then
if not Is_Entity_Name (Lo) then
Lo := Duplicate_Subexpr_No_Checks (Lo);
end if;
if not Is_Entity_Name (Hi) then
Lo := Duplicate_Subexpr_No_Checks (Hi);
end if;
N :=
Make_Op_Add (Loc,
Left_Opnd =>
Make_Op_Subtract (Loc,
Left_Opnd => Hi,
Right_Opnd => Lo),
Right_Opnd => Make_Integer_Literal (Loc, 1));
return N;
else
N :=
Make_Attribute_Reference (Loc,
Attribute_Name => Name_Length,
Prefix =>
New_Occurrence_Of (E1, Loc));
if Indx > 1 then
Set_Expressions (N, New_List (
Make_Integer_Literal (Loc, Indx)));
end if;
return N;
end if;
end;
else
N :=
Make_Attribute_Reference (Loc,
Attribute_Name => Name_Length,
Prefix =>
New_Occurrence_Of (E1, Loc));
if Indx > 1 then
Set_Expressions (N, New_List (
Make_Integer_Literal (Loc, Indx)));
end if;
return N;
end if;
end Get_E_Length;
------------------
-- Get_N_Length --
------------------
function Get_N_Length (N : Node_Id; Indx : Nat) return Node_Id is
begin
return
Make_Attribute_Reference (Loc,
Attribute_Name => Name_Length,
Prefix =>
Duplicate_Subexpr_No_Checks (N, Name_Req => True),
Expressions => New_List (
Make_Integer_Literal (Loc, Indx)));
end Get_N_Length;
-------------------
-- Length_E_Cond --
-------------------
function Length_E_Cond
(Exptyp : Entity_Id;
Typ : Entity_Id;
Indx : Nat)
return Node_Id
is
begin
return
Make_Op_Ne (Loc,
Left_Opnd => Get_E_Length (Typ, Indx),
Right_Opnd => Get_E_Length (Exptyp, Indx));
end Length_E_Cond;
-------------------
-- Length_N_Cond --
-------------------
function Length_N_Cond
(Expr : Node_Id;
Typ : Entity_Id;
Indx : Nat)
return Node_Id
is
begin
return
Make_Op_Ne (Loc,
Left_Opnd => Get_E_Length (Typ, Indx),
Right_Opnd => Get_N_Length (Expr, Indx));
end Length_N_Cond;
function Same_Bounds (L : Node_Id; R : Node_Id) return Boolean is
begin
return
(Nkind (L) = N_Integer_Literal
and then Nkind (R) = N_Integer_Literal
and then Intval (L) = Intval (R))
or else
(Is_Entity_Name (L)
and then Ekind (Entity (L)) = E_Constant
and then ((Is_Entity_Name (R)
and then Entity (L) = Entity (R))
or else
(Nkind (R) = N_Type_Conversion
and then Is_Entity_Name (Expression (R))
and then Entity (L) = Entity (Expression (R)))))
or else
(Is_Entity_Name (R)
and then Ekind (Entity (R)) = E_Constant
and then Nkind (L) = N_Type_Conversion
and then Is_Entity_Name (Expression (L))
and then Entity (R) = Entity (Expression (L)))
or else
(Is_Entity_Name (L)
and then Is_Entity_Name (R)
and then Entity (L) = Entity (R)
and then Ekind (Entity (L)) = E_In_Parameter
and then Inside_Init_Proc);
end Same_Bounds;
-- Start of processing for Selected_Length_Checks
begin
if not Expander_Active then
return Ret_Result;
end if;
if Target_Typ = Any_Type
or else Target_Typ = Any_Composite
or else Raises_Constraint_Error (Ck_Node)
then
return Ret_Result;
end if;
if No (Wnode) then
Wnode := Ck_Node;
end if;
T_Typ := Target_Typ;
if No (Source_Typ) then
S_Typ := Etype (Ck_Node);
else
S_Typ := Source_Typ;
end if;
if S_Typ = Any_Type or else S_Typ = Any_Composite then
return Ret_Result;
end if;
if Is_Access_Type (T_Typ) and then Is_Access_Type (S_Typ) then
S_Typ := Designated_Type (S_Typ);
T_Typ := Designated_Type (T_Typ);
Do_Access := True;
-- A simple optimization
if Nkind (Ck_Node) = N_Null then
return Ret_Result;
end if;
end if;
if Is_Array_Type (T_Typ) and then Is_Array_Type (S_Typ) then
if Is_Constrained (T_Typ) then
-- The checking code to be generated will freeze the
-- corresponding array type. However, we must freeze the
-- type now, so that the freeze node does not appear within
-- the generated condional expression, but ahead of it.
Freeze_Before (Ck_Node, T_Typ);
Expr_Actual := Get_Referenced_Object (Ck_Node);
Exptyp := Get_Actual_Subtype (Expr_Actual);
if Is_Access_Type (Exptyp) then
Exptyp := Designated_Type (Exptyp);
end if;
-- String_Literal case. This needs to be handled specially be-
-- cause no index types are available for string literals. The
-- condition is simply:
-- T_Typ'Length = string-literal-length
if Nkind (Expr_Actual) = N_String_Literal
and then Ekind (Etype (Expr_Actual)) = E_String_Literal_Subtype
then
Cond :=
Make_Op_Ne (Loc,
Left_Opnd => Get_E_Length (T_Typ, 1),
Right_Opnd =>
Make_Integer_Literal (Loc,
Intval =>
String_Literal_Length (Etype (Expr_Actual))));
-- General array case. Here we have a usable actual subtype for
-- the expression, and the condition is built from the two types
-- (Do_Length):
-- T_Typ'Length /= Exptyp'Length or else
-- T_Typ'Length (2) /= Exptyp'Length (2) or else
-- T_Typ'Length (3) /= Exptyp'Length (3) or else
-- ...
elsif Is_Constrained (Exptyp) then
declare
Ndims : constant Nat := Number_Dimensions (T_Typ);
L_Index : Node_Id;
R_Index : Node_Id;
L_Low : Node_Id;
L_High : Node_Id;
R_Low : Node_Id;
R_High : Node_Id;
L_Length : Uint;
R_Length : Uint;
Ref_Node : Node_Id;
begin
-- At the library level, we need to ensure that the
-- type of the object is elaborated before the check
-- itself is emitted. This is only done if the object
-- is in the current compilation unit, otherwise the
-- type is frozen and elaborated in its unit.
if Is_Itype (Exptyp)
and then
Ekind (Cunit_Entity (Current_Sem_Unit)) = E_Package
and then
not In_Package_Body (Cunit_Entity (Current_Sem_Unit))
and then In_Open_Scopes (Scope (Exptyp))
then
Ref_Node := Make_Itype_Reference (Sloc (Ck_Node));
Set_Itype (Ref_Node, Exptyp);
Insert_Action (Ck_Node, Ref_Node);
end if;
L_Index := First_Index (T_Typ);
R_Index := First_Index (Exptyp);
for Indx in 1 .. Ndims loop
if not (Nkind (L_Index) = N_Raise_Constraint_Error
or else
Nkind (R_Index) = N_Raise_Constraint_Error)
then
Get_Index_Bounds (L_Index, L_Low, L_High);
Get_Index_Bounds (R_Index, R_Low, R_High);
-- Deal with compile time length check. Note that we
-- skip this in the access case, because the access
-- value may be null, so we cannot know statically.
if not Do_Access
and then Compile_Time_Known_Value (L_Low)
and then Compile_Time_Known_Value (L_High)
and then Compile_Time_Known_Value (R_Low)
and then Compile_Time_Known_Value (R_High)
then
if Expr_Value (L_High) >= Expr_Value (L_Low) then
L_Length := Expr_Value (L_High) -
Expr_Value (L_Low) + 1;
else
L_Length := UI_From_Int (0);
end if;
if Expr_Value (R_High) >= Expr_Value (R_Low) then
R_Length := Expr_Value (R_High) -
Expr_Value (R_Low) + 1;
else
R_Length := UI_From_Int (0);
end if;
if L_Length > R_Length then
Add_Check
(Compile_Time_Constraint_Error
(Wnode, "too few elements for}?", T_Typ));
elsif L_Length < R_Length then
Add_Check
(Compile_Time_Constraint_Error
(Wnode, "too many elements for}?", T_Typ));
end if;
-- The comparison for an individual index subtype
-- is omitted if the corresponding index subtypes
-- statically match, since the result is known to
-- be true. Note that this test is worth while even
-- though we do static evaluation, because non-static
-- subtypes can statically match.
elsif not
Subtypes_Statically_Match
(Etype (L_Index), Etype (R_Index))
and then not
(Same_Bounds (L_Low, R_Low)
and then Same_Bounds (L_High, R_High))
then
Evolve_Or_Else
(Cond, Length_E_Cond (Exptyp, T_Typ, Indx));
end if;
Next (L_Index);
Next (R_Index);
end if;
end loop;
end;
-- Handle cases where we do not get a usable actual subtype that
-- is constrained. This happens for example in the function call
-- and explicit dereference cases. In these cases, we have to get
-- the length or range from the expression itself, making sure we
-- do not evaluate it more than once.
-- Here Ck_Node is the original expression, or more properly the
-- result of applying Duplicate_Expr to the original tree,
-- forcing the result to be a name.
else
declare
Ndims : constant Nat := Number_Dimensions (T_Typ);
begin
-- Build the condition for the explicit dereference case
for Indx in 1 .. Ndims loop
Evolve_Or_Else
(Cond, Length_N_Cond (Ck_Node, T_Typ, Indx));
end loop;
end;
end if;
end if;
end if;
-- Construct the test and insert into the tree
if Present (Cond) then
if Do_Access then
Cond := Guard_Access (Cond, Loc, Ck_Node);
end if;
Add_Check
(Make_Raise_Constraint_Error (Loc,
Condition => Cond,
Reason => CE_Length_Check_Failed));
end if;
return Ret_Result;
end Selected_Length_Checks;
---------------------------
-- Selected_Range_Checks --
---------------------------
function Selected_Range_Checks
(Ck_Node : Node_Id;
Target_Typ : Entity_Id;
Source_Typ : Entity_Id;
Warn_Node : Node_Id)
return Check_Result
is
Loc : constant Source_Ptr := Sloc (Ck_Node);
S_Typ : Entity_Id;
T_Typ : Entity_Id;
Expr_Actual : Node_Id;
Exptyp : Entity_Id;
Cond : Node_Id := Empty;
Do_Access : Boolean := False;
Wnode : Node_Id := Warn_Node;
Ret_Result : Check_Result := (Empty, Empty);
Num_Checks : Integer := 0;
procedure Add_Check (N : Node_Id);
-- Adds the action given to Ret_Result if N is non-Empty
function Discrete_Range_Cond
(Expr : Node_Id;
Typ : Entity_Id)
return Node_Id;
-- Returns expression to compute:
-- Low_Bound (Expr) < Typ'First
-- or else
-- High_Bound (Expr) > Typ'Last
function Discrete_Expr_Cond
(Expr : Node_Id;
Typ : Entity_Id)
return Node_Id;
-- Returns expression to compute:
-- Expr < Typ'First
-- or else
-- Expr > Typ'Last
function Get_E_First_Or_Last
(E : Entity_Id;
Indx : Nat;
Nam : Name_Id)
return Node_Id;
-- Returns expression to compute:
-- E'First or E'Last
function Get_N_First (N : Node_Id; Indx : Nat) return Node_Id;
function Get_N_Last (N : Node_Id; Indx : Nat) return Node_Id;
-- Returns expression to compute:
-- N'First or N'Last using Duplicate_Subexpr_No_Checks
function Range_E_Cond
(Exptyp : Entity_Id;
Typ : Entity_Id;
Indx : Nat)
return Node_Id;
-- Returns expression to compute:
-- Exptyp'First < Typ'First or else Exptyp'Last > Typ'Last
function Range_Equal_E_Cond
(Exptyp : Entity_Id;
Typ : Entity_Id;
Indx : Nat)
return Node_Id;
-- Returns expression to compute:
-- Exptyp'First /= Typ'First or else Exptyp'Last /= Typ'Last
function Range_N_Cond
(Expr : Node_Id;
Typ : Entity_Id;
Indx : Nat)
return Node_Id;
-- Return expression to compute:
-- Expr'First < Typ'First or else Expr'Last > Typ'Last
---------------
-- Add_Check --
---------------
procedure Add_Check (N : Node_Id) is
begin
if Present (N) then
-- For now, ignore attempt to place more than 2 checks ???
if Num_Checks = 2 then
return;
end if;
pragma Assert (Num_Checks <= 1);
Num_Checks := Num_Checks + 1;
Ret_Result (Num_Checks) := N;
end if;
end Add_Check;
-------------------------
-- Discrete_Expr_Cond --
-------------------------
function Discrete_Expr_Cond
(Expr : Node_Id;
Typ : Entity_Id)
return Node_Id
is
begin
return
Make_Or_Else (Loc,
Left_Opnd =>
Make_Op_Lt (Loc,
Left_Opnd =>
Convert_To (Base_Type (Typ),
Duplicate_Subexpr_No_Checks (Expr)),
Right_Opnd =>
Convert_To (Base_Type (Typ),
Get_E_First_Or_Last (Typ, 0, Name_First))),
Right_Opnd =>
Make_Op_Gt (Loc,
Left_Opnd =>
Convert_To (Base_Type (Typ),
Duplicate_Subexpr_No_Checks (Expr)),
Right_Opnd =>
Convert_To
(Base_Type (Typ),
Get_E_First_Or_Last (Typ, 0, Name_Last))));
end Discrete_Expr_Cond;
-------------------------
-- Discrete_Range_Cond --
-------------------------
function Discrete_Range_Cond
(Expr : Node_Id;
Typ : Entity_Id)
return Node_Id
is
LB : Node_Id := Low_Bound (Expr);
HB : Node_Id := High_Bound (Expr);
Left_Opnd : Node_Id;
Right_Opnd : Node_Id;
begin
if Nkind (LB) = N_Identifier
and then Ekind (Entity (LB)) = E_Discriminant then
LB := New_Occurrence_Of (Discriminal (Entity (LB)), Loc);
end if;
if Nkind (HB) = N_Identifier
and then Ekind (Entity (HB)) = E_Discriminant then
HB := New_Occurrence_Of (Discriminal (Entity (HB)), Loc);
end if;
Left_Opnd :=
Make_Op_Lt (Loc,
Left_Opnd =>
Convert_To
(Base_Type (Typ), Duplicate_Subexpr_No_Checks (LB)),
Right_Opnd =>
Convert_To
(Base_Type (Typ), Get_E_First_Or_Last (Typ, 0, Name_First)));
if Base_Type (Typ) = Typ then
return Left_Opnd;
elsif Compile_Time_Known_Value (High_Bound (Scalar_Range (Typ)))
and then
Compile_Time_Known_Value (High_Bound (Scalar_Range
(Base_Type (Typ))))
then
if Is_Floating_Point_Type (Typ) then
if Expr_Value_R (High_Bound (Scalar_Range (Typ))) =
Expr_Value_R (High_Bound (Scalar_Range (Base_Type (Typ))))
then
return Left_Opnd;
end if;
else
if Expr_Value (High_Bound (Scalar_Range (Typ))) =
Expr_Value (High_Bound (Scalar_Range (Base_Type (Typ))))
then
return Left_Opnd;
end if;
end if;
end if;
Right_Opnd :=
Make_Op_Gt (Loc,
Left_Opnd =>
Convert_To
(Base_Type (Typ), Duplicate_Subexpr_No_Checks (HB)),
Right_Opnd =>
Convert_To
(Base_Type (Typ),
Get_E_First_Or_Last (Typ, 0, Name_Last)));
return Make_Or_Else (Loc, Left_Opnd, Right_Opnd);
end Discrete_Range_Cond;
-------------------------
-- Get_E_First_Or_Last --
-------------------------
function Get_E_First_Or_Last
(E : Entity_Id;
Indx : Nat;
Nam : Name_Id)
return Node_Id
is
N : Node_Id;
LB : Node_Id;
HB : Node_Id;
Bound : Node_Id;
begin
if Is_Array_Type (E) then
N := First_Index (E);
for J in 2 .. Indx loop
Next_Index (N);
end loop;
else
N := Scalar_Range (E);
end if;
if Nkind (N) = N_Subtype_Indication then
LB := Low_Bound (Range_Expression (Constraint (N)));
HB := High_Bound (Range_Expression (Constraint (N)));
elsif Is_Entity_Name (N) then
LB := Type_Low_Bound (Etype (N));
HB := Type_High_Bound (Etype (N));
else
LB := Low_Bound (N);
HB := High_Bound (N);
end if;
if Nam = Name_First then
Bound := LB;
else
Bound := HB;
end if;
if Nkind (Bound) = N_Identifier
and then Ekind (Entity (Bound)) = E_Discriminant
then
-- If this is a task discriminant, and we are the body, we must
-- retrieve the corresponding body discriminal. This is another
-- consequence of the early creation of discriminals, and the
-- need to generate constraint checks before their declarations
-- are made visible.
if Is_Concurrent_Record_Type (Scope (Entity (Bound))) then
declare
Tsk : constant Entity_Id :=
Corresponding_Concurrent_Type
(Scope (Entity (Bound)));
Disc : Entity_Id;
begin
if In_Open_Scopes (Tsk)
and then Has_Completion (Tsk)
then
-- Find discriminant of original task, and use its
-- current discriminal, which is the renaming within
-- the task body.
Disc := First_Discriminant (Tsk);
while Present (Disc) loop
if Chars (Disc) = Chars (Entity (Bound)) then
Set_Scope (Discriminal (Disc), Tsk);
return New_Occurrence_Of (Discriminal (Disc), Loc);
end if;
Next_Discriminant (Disc);
end loop;
-- That loop should always succeed in finding a matching
-- entry and returning. Fatal error if not.
raise Program_Error;
else
return
New_Occurrence_Of (Discriminal (Entity (Bound)), Loc);
end if;
end;
else
return New_Occurrence_Of (Discriminal (Entity (Bound)), Loc);
end if;
elsif Nkind (Bound) = N_Identifier
and then Ekind (Entity (Bound)) = E_In_Parameter
and then not Inside_Init_Proc
then
return Get_Discriminal (E, Bound);
elsif Nkind (Bound) = N_Integer_Literal then
return Make_Integer_Literal (Loc, Intval (Bound));
else
return Duplicate_Subexpr_No_Checks (Bound);
end if;
end Get_E_First_Or_Last;
-----------------
-- Get_N_First --
-----------------
function Get_N_First (N : Node_Id; Indx : Nat) return Node_Id is
begin
return
Make_Attribute_Reference (Loc,
Attribute_Name => Name_First,
Prefix =>
Duplicate_Subexpr_No_Checks (N, Name_Req => True),
Expressions => New_List (
Make_Integer_Literal (Loc, Indx)));
end Get_N_First;
----------------
-- Get_N_Last --
----------------
function Get_N_Last (N : Node_Id; Indx : Nat) return Node_Id is
begin
return
Make_Attribute_Reference (Loc,
Attribute_Name => Name_Last,
Prefix =>
Duplicate_Subexpr_No_Checks (N, Name_Req => True),
Expressions => New_List (
Make_Integer_Literal (Loc, Indx)));
end Get_N_Last;
------------------
-- Range_E_Cond --
------------------
function Range_E_Cond
(Exptyp : Entity_Id;
Typ : Entity_Id;
Indx : Nat)
return Node_Id
is
begin
return
Make_Or_Else (Loc,
Left_Opnd =>
Make_Op_Lt (Loc,
Left_Opnd => Get_E_First_Or_Last (Exptyp, Indx, Name_First),
Right_Opnd => Get_E_First_Or_Last (Typ, Indx, Name_First)),
Right_Opnd =>
Make_Op_Gt (Loc,
Left_Opnd => Get_E_First_Or_Last (Exptyp, Indx, Name_Last),
Right_Opnd => Get_E_First_Or_Last (Typ, Indx, Name_Last)));
end Range_E_Cond;
------------------------
-- Range_Equal_E_Cond --
------------------------
function Range_Equal_E_Cond
(Exptyp : Entity_Id;
Typ : Entity_Id;
Indx : Nat)
return Node_Id
is
begin
return
Make_Or_Else (Loc,
Left_Opnd =>
Make_Op_Ne (Loc,
Left_Opnd => Get_E_First_Or_Last (Exptyp, Indx, Name_First),
Right_Opnd => Get_E_First_Or_Last (Typ, Indx, Name_First)),
Right_Opnd =>
Make_Op_Ne (Loc,
Left_Opnd => Get_E_First_Or_Last (Exptyp, Indx, Name_Last),
Right_Opnd => Get_E_First_Or_Last (Typ, Indx, Name_Last)));
end Range_Equal_E_Cond;
------------------
-- Range_N_Cond --
------------------
function Range_N_Cond
(Expr : Node_Id;
Typ : Entity_Id;
Indx : Nat)
return Node_Id
is
begin
return
Make_Or_Else (Loc,
Left_Opnd =>
Make_Op_Lt (Loc,
Left_Opnd => Get_N_First (Expr, Indx),
Right_Opnd => Get_E_First_Or_Last (Typ, Indx, Name_First)),
Right_Opnd =>
Make_Op_Gt (Loc,
Left_Opnd => Get_N_Last (Expr, Indx),
Right_Opnd => Get_E_First_Or_Last (Typ, Indx, Name_Last)));
end Range_N_Cond;
-- Start of processing for Selected_Range_Checks
begin
if not Expander_Active then
return Ret_Result;
end if;
if Target_Typ = Any_Type
or else Target_Typ = Any_Composite
or else Raises_Constraint_Error (Ck_Node)
then
return Ret_Result;
end if;
if No (Wnode) then
Wnode := Ck_Node;
end if;
T_Typ := Target_Typ;
if No (Source_Typ) then
S_Typ := Etype (Ck_Node);
else
S_Typ := Source_Typ;
end if;
if S_Typ = Any_Type or else S_Typ = Any_Composite then
return Ret_Result;
end if;
-- The order of evaluating T_Typ before S_Typ seems to be critical
-- because S_Typ can be derived from Etype (Ck_Node), if it's not passed
-- in, and since Node can be an N_Range node, it might be invalid.
-- Should there be an assert check somewhere for taking the Etype of
-- an N_Range node ???
if Is_Access_Type (T_Typ) and then Is_Access_Type (S_Typ) then
S_Typ := Designated_Type (S_Typ);
T_Typ := Designated_Type (T_Typ);
Do_Access := True;
-- A simple optimization
if Nkind (Ck_Node) = N_Null then
return Ret_Result;
end if;
end if;
-- For an N_Range Node, check for a null range and then if not
-- null generate a range check action.
if Nkind (Ck_Node) = N_Range then
-- There's no point in checking a range against itself
if Ck_Node = Scalar_Range (T_Typ) then
return Ret_Result;
end if;
declare
T_LB : constant Node_Id := Type_Low_Bound (T_Typ);
T_HB : constant Node_Id := Type_High_Bound (T_Typ);
LB : constant Node_Id := Low_Bound (Ck_Node);
HB : constant Node_Id := High_Bound (Ck_Node);
Null_Range : Boolean;
Out_Of_Range_L : Boolean;
Out_Of_Range_H : Boolean;
begin
-- Check for case where everything is static and we can
-- do the check at compile time. This is skipped if we
-- have an access type, since the access value may be null.
-- ??? This code can be improved since you only need to know
-- that the two respective bounds (LB & T_LB or HB & T_HB)
-- are known at compile time to emit pertinent messages.
if Compile_Time_Known_Value (LB)
and then Compile_Time_Known_Value (HB)
and then Compile_Time_Known_Value (T_LB)
and then Compile_Time_Known_Value (T_HB)
and then not Do_Access
then
-- Floating-point case
if Is_Floating_Point_Type (S_Typ) then
Null_Range := Expr_Value_R (HB) < Expr_Value_R (LB);
Out_Of_Range_L :=
(Expr_Value_R (LB) < Expr_Value_R (T_LB))
or else
(Expr_Value_R (LB) > Expr_Value_R (T_HB));
Out_Of_Range_H :=
(Expr_Value_R (HB) > Expr_Value_R (T_HB))
or else
(Expr_Value_R (HB) < Expr_Value_R (T_LB));
-- Fixed or discrete type case
else
Null_Range := Expr_Value (HB) < Expr_Value (LB);
Out_Of_Range_L :=
(Expr_Value (LB) < Expr_Value (T_LB))
or else
(Expr_Value (LB) > Expr_Value (T_HB));
Out_Of_Range_H :=
(Expr_Value (HB) > Expr_Value (T_HB))
or else
(Expr_Value (HB) < Expr_Value (T_LB));
end if;
if not Null_Range then
if Out_Of_Range_L then
if No (Warn_Node) then
Add_Check
(Compile_Time_Constraint_Error
(Low_Bound (Ck_Node),
"static value out of range of}?", T_Typ));
else
Add_Check
(Compile_Time_Constraint_Error
(Wnode,
"static range out of bounds of}?", T_Typ));
end if;
end if;
if Out_Of_Range_H then
if No (Warn_Node) then
Add_Check
(Compile_Time_Constraint_Error
(High_Bound (Ck_Node),
"static value out of range of}?", T_Typ));
else
Add_Check
(Compile_Time_Constraint_Error
(Wnode,
"static range out of bounds of}?", T_Typ));
end if;
end if;
end if;
else
declare
LB : Node_Id := Low_Bound (Ck_Node);
HB : Node_Id := High_Bound (Ck_Node);
begin
-- If either bound is a discriminant and we are within
-- the record declaration, it is a use of the discriminant
-- in a constraint of a component, and nothing can be
-- checked here. The check will be emitted within the
-- init proc. Before then, the discriminal has no real
-- meaning.
if Nkind (LB) = N_Identifier
and then Ekind (Entity (LB)) = E_Discriminant
then
if Current_Scope = Scope (Entity (LB)) then
return Ret_Result;
else
LB :=
New_Occurrence_Of (Discriminal (Entity (LB)), Loc);
end if;
end if;
if Nkind (HB) = N_Identifier
and then Ekind (Entity (HB)) = E_Discriminant
then
if Current_Scope = Scope (Entity (HB)) then
return Ret_Result;
else
HB :=
New_Occurrence_Of (Discriminal (Entity (HB)), Loc);
end if;
end if;
Cond := Discrete_Range_Cond (Ck_Node, T_Typ);
Set_Paren_Count (Cond, 1);
Cond :=
Make_And_Then (Loc,
Left_Opnd =>
Make_Op_Ge (Loc,
Left_Opnd => Duplicate_Subexpr_No_Checks (HB),
Right_Opnd => Duplicate_Subexpr_No_Checks (LB)),
Right_Opnd => Cond);
end;
end if;
end;
elsif Is_Scalar_Type (S_Typ) then
-- This somewhat duplicates what Apply_Scalar_Range_Check does,
-- except the above simply sets a flag in the node and lets
-- gigi generate the check base on the Etype of the expression.
-- Sometimes, however we want to do a dynamic check against an
-- arbitrary target type, so we do that here.
if Ekind (Base_Type (S_Typ)) /= Ekind (Base_Type (T_Typ)) then
Cond := Discrete_Expr_Cond (Ck_Node, T_Typ);
-- For literals, we can tell if the constraint error will be
-- raised at compile time, so we never need a dynamic check, but
-- if the exception will be raised, then post the usual warning,
-- and replace the literal with a raise constraint error
-- expression. As usual, skip this for access types
elsif Compile_Time_Known_Value (Ck_Node)
and then not Do_Access
then
declare
LB : constant Node_Id := Type_Low_Bound (T_Typ);
UB : constant Node_Id := Type_High_Bound (T_Typ);
Out_Of_Range : Boolean;
Static_Bounds : constant Boolean :=
Compile_Time_Known_Value (LB)
and Compile_Time_Known_Value (UB);
begin
-- Following range tests should use Sem_Eval routine ???
if Static_Bounds then
if Is_Floating_Point_Type (S_Typ) then
Out_Of_Range :=
(Expr_Value_R (Ck_Node) < Expr_Value_R (LB))
or else
(Expr_Value_R (Ck_Node) > Expr_Value_R (UB));
else -- fixed or discrete type
Out_Of_Range :=
Expr_Value (Ck_Node) < Expr_Value (LB)
or else
Expr_Value (Ck_Node) > Expr_Value (UB);
end if;
-- Bounds of the type are static and the literal is
-- out of range so make a warning message.
if Out_Of_Range then
if No (Warn_Node) then
Add_Check
(Compile_Time_Constraint_Error
(Ck_Node,
"static value out of range of}?", T_Typ));
else
Add_Check
(Compile_Time_Constraint_Error
(Wnode,
"static value out of range of}?", T_Typ));
end if;
end if;
else
Cond := Discrete_Expr_Cond (Ck_Node, T_Typ);
end if;
end;
-- Here for the case of a non-static expression, we need a runtime
-- check unless the source type range is guaranteed to be in the
-- range of the target type.
else
if not In_Subrange_Of (S_Typ, T_Typ) then
Cond := Discrete_Expr_Cond (Ck_Node, T_Typ);
end if;
end if;
end if;
if Is_Array_Type (T_Typ) and then Is_Array_Type (S_Typ) then
if Is_Constrained (T_Typ) then
Expr_Actual := Get_Referenced_Object (Ck_Node);
Exptyp := Get_Actual_Subtype (Expr_Actual);
if Is_Access_Type (Exptyp) then
Exptyp := Designated_Type (Exptyp);
end if;
-- String_Literal case. This needs to be handled specially be-
-- cause no index types are available for string literals. The
-- condition is simply:
-- T_Typ'Length = string-literal-length
if Nkind (Expr_Actual) = N_String_Literal then
null;
-- General array case. Here we have a usable actual subtype for
-- the expression, and the condition is built from the two types
-- T_Typ'First < Exptyp'First or else
-- T_Typ'Last > Exptyp'Last or else
-- T_Typ'First(1) < Exptyp'First(1) or else
-- T_Typ'Last(1) > Exptyp'Last(1) or else
-- ...
elsif Is_Constrained (Exptyp) then
declare
Ndims : constant Nat := Number_Dimensions (T_Typ);
L_Index : Node_Id;
R_Index : Node_Id;
L_Low : Node_Id;
L_High : Node_Id;
R_Low : Node_Id;
R_High : Node_Id;
begin
L_Index := First_Index (T_Typ);
R_Index := First_Index (Exptyp);
for Indx in 1 .. Ndims loop
if not (Nkind (L_Index) = N_Raise_Constraint_Error
or else
Nkind (R_Index) = N_Raise_Constraint_Error)
then
Get_Index_Bounds (L_Index, L_Low, L_High);
Get_Index_Bounds (R_Index, R_Low, R_High);
-- Deal with compile time length check. Note that we
-- skip this in the access case, because the access
-- value may be null, so we cannot know statically.
if not
Subtypes_Statically_Match
(Etype (L_Index), Etype (R_Index))
then
-- If the target type is constrained then we
-- have to check for exact equality of bounds
-- (required for qualified expressions).
if Is_Constrained (T_Typ) then
Evolve_Or_Else
(Cond,
Range_Equal_E_Cond (Exptyp, T_Typ, Indx));
else
Evolve_Or_Else
(Cond, Range_E_Cond (Exptyp, T_Typ, Indx));
end if;
end if;
Next (L_Index);
Next (R_Index);
end if;
end loop;
end;
-- Handle cases where we do not get a usable actual subtype that
-- is constrained. This happens for example in the function call
-- and explicit dereference cases. In these cases, we have to get
-- the length or range from the expression itself, making sure we
-- do not evaluate it more than once.
-- Here Ck_Node is the original expression, or more properly the
-- result of applying Duplicate_Expr to the original tree,
-- forcing the result to be a name.
else
declare
Ndims : constant Nat := Number_Dimensions (T_Typ);
begin
-- Build the condition for the explicit dereference case
for Indx in 1 .. Ndims loop
Evolve_Or_Else
(Cond, Range_N_Cond (Ck_Node, T_Typ, Indx));
end loop;
end;
end if;
else
-- Generate an Action to check that the bounds of the
-- source value are within the constraints imposed by the
-- target type for a conversion to an unconstrained type.
-- Rule is 4.6(38).
if Nkind (Parent (Ck_Node)) = N_Type_Conversion then
declare
Opnd_Index : Node_Id;
Targ_Index : Node_Id;
begin
Opnd_Index
:= First_Index (Get_Actual_Subtype (Ck_Node));
Targ_Index := First_Index (T_Typ);
while Opnd_Index /= Empty loop
if Nkind (Opnd_Index) = N_Range then
if Is_In_Range
(Low_Bound (Opnd_Index), Etype (Targ_Index))
and then
Is_In_Range
(High_Bound (Opnd_Index), Etype (Targ_Index))
then
null;
-- If null range, no check needed.
elsif
Compile_Time_Known_Value (High_Bound (Opnd_Index))
and then
Compile_Time_Known_Value (Low_Bound (Opnd_Index))
and then
Expr_Value (High_Bound (Opnd_Index)) <
Expr_Value (Low_Bound (Opnd_Index))
then
null;
elsif Is_Out_Of_Range
(Low_Bound (Opnd_Index), Etype (Targ_Index))
or else
Is_Out_Of_Range
(High_Bound (Opnd_Index), Etype (Targ_Index))
then
Add_Check
(Compile_Time_Constraint_Error
(Wnode, "value out of range of}?", T_Typ));
else
Evolve_Or_Else
(Cond,
Discrete_Range_Cond
(Opnd_Index, Etype (Targ_Index)));
end if;
end if;
Next_Index (Opnd_Index);
Next_Index (Targ_Index);
end loop;
end;
end if;
end if;
end if;
-- Construct the test and insert into the tree
if Present (Cond) then
if Do_Access then
Cond := Guard_Access (Cond, Loc, Ck_Node);
end if;
Add_Check
(Make_Raise_Constraint_Error (Loc,
Condition => Cond,
Reason => CE_Range_Check_Failed));
end if;
return Ret_Result;
end Selected_Range_Checks;
-------------------------------
-- Storage_Checks_Suppressed --
-------------------------------
function Storage_Checks_Suppressed (E : Entity_Id) return Boolean is
begin
if Present (E) and then Checks_May_Be_Suppressed (E) then
return Is_Check_Suppressed (E, Storage_Check);
else
return Scope_Suppress (Storage_Check);
end if;
end Storage_Checks_Suppressed;
---------------------------
-- Tag_Checks_Suppressed --
---------------------------
function Tag_Checks_Suppressed (E : Entity_Id) return Boolean is
begin
if Present (E) then
if Kill_Tag_Checks (E) then
return True;
elsif Checks_May_Be_Suppressed (E) then
return Is_Check_Suppressed (E, Tag_Check);
end if;
end if;
return Scope_Suppress (Tag_Check);
end Tag_Checks_Suppressed;
end Checks;
|