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
|
/* $OpenBSD: tree.c,v 1.41 2006/05/28 23:42:49 cloder Exp $ */
/* $NetBSD: tree.c,v 1.12 1995/10/02 17:37:57 jpo Exp $ */
/*
* Copyright (c) 1994, 1995 Jochen Pohl
* All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by Jochen Pohl for
* The NetBSD Project.
* 4. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef lint
static char rcsid[] = "$OpenBSD: tree.c,v 1.41 2006/05/28 23:42:49 cloder Exp $";
#endif
#include <stdlib.h>
#include <string.h>
#include <float.h>
#include <limits.h>
#include <math.h>
#include "lint1.h"
#include "y.tab.h"
/* Various flags for each operator. */
static mod_t modtab[NOPS];
static tnode_t *getinode(tspec_t, quad_t);
static void ptrcmpok(op_t, tnode_t *, tnode_t *);
static int asgntypok(op_t, farg_t *, tnode_t *, tnode_t *);
static void chkbeop(op_t, tnode_t *, tnode_t *);
static void chkeop2(op_t, farg_t *, tnode_t *, tnode_t *);
static void chkeop1(op_t, farg_t *, tnode_t *, tnode_t *);
static tnode_t *mktnode(op_t, type_t *, tnode_t *, tnode_t *);
static void balance(op_t, tnode_t **, tnode_t **);
static void incompat(op_t, tspec_t, tspec_t);
static void illptrc(mod_t *, type_t *, type_t *);
static void mrgqual(type_t **, type_t *, type_t *);
static int conmemb(type_t *);
static void ptconv(farg_t *, tspec_t, tspec_t, type_t *, tnode_t *);
static void iiconv(op_t, farg_t *, tspec_t, tspec_t, type_t *, tnode_t *);
static void piconv(op_t, tspec_t, type_t *, tnode_t *);
static void ppconv(op_t, tnode_t *, type_t *);
static tnode_t *bldstr(op_t, tnode_t *, tnode_t *);
static tnode_t *bldincdec(op_t, tnode_t *);
static tnode_t *bldamper(tnode_t *, int);
static tnode_t *bldplmi(op_t, tnode_t *, tnode_t *);
static tnode_t *bldshft(op_t, tnode_t *, tnode_t *);
static tnode_t *bldcol(tnode_t *, tnode_t *);
static tnode_t *bldasgn(op_t, tnode_t *, tnode_t *);
static tnode_t *plength(type_t *);
static tnode_t *fold(tnode_t *);
static tnode_t *foldtst(tnode_t *);
static tnode_t *foldflt(tnode_t *);
static tnode_t *chkfarg(tnode_t *, tnode_t *);
static tnode_t *parg(farg_t *, tnode_t *);
static int chkdbz(op_t, tnode_t *);
static void nulleff(tnode_t *);
static int nulleffexpr(tnode_t *);
static void chkaidx(tnode_t *, int);
static void chkcomp(op_t, tnode_t *, tnode_t *);
static void precconf(tnode_t *);
static const char *funcname(tnode_t *);
/*
* Initialize mods of operators.
*/
void
initmtab(void)
{
static struct {
op_t op;
mod_t m;
} imods[] = {
{ ARROW, { 1,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,
"->" } },
{ POINT, { 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
"." } },
{ NOT, { 0,1,0,1,0,1,0,1,0,0,0,0,0,0,0,1,0,
"!" } },
{ COMPL, { 0,0,1,0,0,1,1,0,0,0,0,0,0,0,0,1,1,
"~" } },
{ INCBEF, { 0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,1,0,
"prefix++" } },
{ DECBEF, { 0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,1,0,
"prefix--" } },
{ INCAFT, { 0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,1,0,
"postfix++" } },
{ DECAFT, { 0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,1,0,
"postfix--" } },
{ UPLUS, { 0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,1,1,
"unary +" } },
{ UMINUS, { 0,0,0,0,1,1,1,0,0,0,1,0,0,0,0,1,1,
"unary -" } },
{ STAR, { 0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,
"unary *" } },
{ AMPER, { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
"unary &" } },
{ MULT, { 1,0,0,0,1,1,1,0,1,0,0,1,0,0,0,1,1,
"*" } },
{ DIV, { 1,0,0,0,1,1,1,0,1,0,1,1,0,0,0,1,1,
"/" } },
{ MOD, { 1,0,1,0,0,1,1,0,1,0,1,1,0,0,0,1,1,
"%" } },
{ PLUS, { 1,0,0,1,0,1,1,0,1,0,0,0,0,0,0,1,0,
"+" } },
{ MINUS, { 1,0,0,1,0,1,1,0,1,0,0,0,0,0,0,1,0,
"-" } },
{ SHL, { 1,0,1,0,0,1,1,0,0,0,0,0,1,0,0,1,1,
"<<" } },
{ SHR, { 1,0,1,0,0,1,1,0,0,0,1,0,1,0,0,1,1,
">>" } },
{ LT, { 1,1,0,1,0,1,1,0,1,0,1,1,0,1,1,0,1,
"<" } },
{ LE, { 1,1,0,1,0,1,1,0,1,0,1,1,0,1,1,0,1,
"<=" } },
{ GT, { 1,1,0,1,0,1,1,0,1,0,1,1,0,1,1,0,1,
">" } },
{ GE, { 1,1,0,1,0,1,1,0,1,0,1,1,0,1,1,0,1,
">=" } },
{ EQ, { 1,1,0,1,0,1,1,0,1,0,0,0,0,1,1,0,0,
"==" } },
{ NE, { 1,1,0,1,0,1,1,0,1,0,0,0,0,1,1,0,1,
"!=" } },
{ AND, { 1,0,1,0,0,1,1,0,1,0,0,0,1,0,0,1,0,
"&" } },
{ XOR, { 1,0,1,0,0,1,1,0,1,0,0,0,1,0,0,1,0,
"^" } },
{ OR, { 1,0,1,0,0,1,1,0,1,0,0,0,1,0,0,1,0,
"|" } },
{ LOGAND, { 1,1,0,1,0,1,0,1,0,0,0,0,0,0,0,1,0,
"&&" } },
{ LOGOR, { 1,1,0,1,0,1,0,1,0,0,0,0,1,0,0,1,0,
"||" } },
{ QUEST, { 1,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,
"?" } },
{ COLON, { 1,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,
":" } },
{ ASSIGN, { 1,0,0,0,0,0,0,0,0,1,0,0,0,0,1,0,0,
"=" } },
{ MULASS, { 1,0,0,0,1,0,0,0,0,1,0,0,0,0,0,1,0,
"*=" } },
{ DIVASS, { 1,0,0,0,1,0,0,0,0,1,0,1,0,0,0,1,0,
"/=" } },
{ MODASS, { 1,0,1,0,0,0,0,0,0,1,0,1,0,0,0,1,0,
"%=" } },
{ ADDASS, { 1,0,0,1,0,0,0,0,0,1,0,0,0,0,0,1,0,
"+=" } },
{ SUBASS, { 1,0,0,1,0,0,0,0,0,1,0,0,0,0,0,1,0,
"-=" } },
{ SHLASS, { 1,0,1,0,0,0,0,0,0,1,0,0,0,0,0,1,0,
"<<=" } },
{ SHRASS, { 1,0,1,0,0,0,0,0,0,1,0,0,0,0,0,1,0,
">>=" } },
{ ANDASS, { 1,0,1,0,0,0,0,0,0,1,0,0,0,0,0,1,0,
"&=" } },
{ XORASS, { 1,0,1,0,0,0,0,0,0,1,0,0,0,0,0,1,0,
"^=" } },
{ ORASS, { 1,0,1,0,0,0,0,0,0,1,0,0,0,0,0,1,0,
"|=" } },
{ NAME, { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
"NAME" } },
{ CON, { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
"CON" } },
{ STRING, { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
"STRING" } },
{ FSEL, { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
"FSEL" } },
{ CALL, { 1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,
"CALL" } },
{ COMMA, { 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
"," } },
{ CVT, { 0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,
"CVT" } },
{ ICALL, { 1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,
"ICALL" } },
{ LOAD, { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
"LOAD" } },
{ PUSH, { 0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,
"PUSH" } },
{ RETURN, { 1,0,0,0,0,0,0,0,0,1,0,0,0,0,1,0,0,
"RETURN" } },
{ INIT, { 1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,
"INIT" } },
{ FARG, { 1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,
"FARG" } },
{ NOOP }
};
int i;
for (i = 0; imods[i].op != NOOP; i++)
STRUCT_ASSIGN(modtab[imods[i].op], imods[i].m);
}
/*
* Increase degree of reference.
* This is most often used to change type "T" in type "pointer to T".
*/
type_t *
incref(type_t *tp, tspec_t t)
{
type_t *tp2;
tp2 = getblk(sizeof (type_t));
tp2->t_tspec = t;
tp2->t_subt = tp;
return (tp2);
}
/*
* same for use in expressions
*/
type_t *
tincref(type_t *tp, tspec_t t)
{
type_t *tp2;
tp2 = tgetblk(sizeof (type_t));
tp2->t_tspec = t;
tp2->t_subt = tp;
return (tp2);
}
/*
* Create a node for a constant.
*/
tnode_t *
getcnode(type_t *tp, val_t *v)
{
tnode_t *n;
n = getnode();
n->tn_op = CON;
n->tn_type = tp;
n->tn_val = tgetblk(sizeof (val_t));
n->tn_val->v_tspec = tp->t_tspec;
n->tn_val->v_lspec = v->v_lspec;
n->tn_val->v_ansiu = v->v_ansiu;
n->tn_val->v_u = v->v_u;
free(v);
return (n);
}
/*
* Create a node for a integer constant.
*/
static tnode_t *
getinode(tspec_t t, quad_t q)
{
tnode_t *n;
n = getnode();
n->tn_op = CON;
n->tn_type = gettyp(t);
n->tn_val = tgetblk(sizeof (val_t));
n->tn_val->v_tspec = t;
n->tn_val->v_quad = q;
return (n);
}
/*
* Create a node for a name (symbol table entry).
* ntok is the token which follows the name.
*/
tnode_t *
getnnode(sym_t *sym, int ntok)
{
tnode_t *n;
if (sym->s_scl == NOSCL) {
sym->s_scl = EXTERN;
sym->s_def = DECL;
if (ntok == T_LPARN) {
if (sflag) {
/* function implicitly declared to ... */
warning(215);
}
/*
* XXX if tflag is set the symbol should be
* exported to level 0
*/
sym->s_type = incref(sym->s_type, FUNC);
} else {
/* %s undefined */
error(99, sym->s_name);
}
}
if (sym->s_kind != FVFT && sym->s_kind != FMOS)
lerror("getnnode() 1");
n = getnode();
n->tn_type = sym->s_type;
if (sym->s_scl != ENUMCON) {
n->tn_op = NAME;
n->tn_sym = sym;
if (sym->s_kind == FVFT && sym->s_type->t_tspec != FUNC)
n->tn_lvalue = 1;
} else {
n->tn_op = CON;
n->tn_val = tgetblk(sizeof (val_t));
*n->tn_val = sym->s_value;
}
return (n);
}
/*
* Create a node for a string.
*/
tnode_t *
getsnode(strg_t *strg)
{
size_t len;
tnode_t *n;
len = strg->st_len;
n = getnode();
n->tn_op = STRING;
n->tn_type = tincref(gettyp(strg->st_tspec), ARRAY);
n->tn_type->t_dim = len + 1;
n->tn_lvalue = 1;
n->tn_strg = tgetblk(sizeof (strg_t));
n->tn_strg->st_tspec = strg->st_tspec;
n->tn_strg->st_len = len;
if (strg->st_tspec == CHAR) {
n->tn_strg->st_cp = tgetblk(len + 1);
(void)memcpy(n->tn_strg->st_cp, strg->st_cp, len + 1);
free(strg->st_cp);
} else {
n->tn_strg->st_wcp = tgetblk((len + 1) * sizeof (wchar_t));
(void)memcpy(n->tn_strg->st_wcp, strg->st_wcp,
(len + 1) * sizeof (wchar_t));
free(strg->st_wcp);
}
free(strg);
return (n);
}
/*
* Returns a symbol which has the same name as the msym argument and is a
* member of the struct or union specified by the tn argument.
*/
sym_t *
strmemb(tnode_t *tn, op_t op, sym_t *msym)
{
str_t *str;
type_t *tp;
sym_t *sym, *csym;
int eq;
tspec_t t;
/*
* Remove the member if it was unknown until now (Which means
* that no defined struct or union has a member with the same name).
*/
if (msym->s_scl == NOSCL) {
/* undefined struct/union member: %s */
error(101, msym->s_name);
rmsym(msym);
msym->s_kind = FMOS;
msym->s_scl = MOS;
msym->s_styp = tgetblk(sizeof (str_t));
msym->s_styp->stag = tgetblk(sizeof (sym_t));
msym->s_styp->stag->s_name = unnamed;
msym->s_value.v_tspec = INT;
return (msym);
}
/* Set str to the tag of which msym is expected to be a member. */
str = NULL;
t = (tp = tn->tn_type)->t_tspec;
if (op == POINT) {
if (t == STRUCT || t == UNION)
str = tp->t_str;
} else if (op == ARROW && t == PTR) {
t = (tp = tp->t_subt)->t_tspec;
if (t == STRUCT || t == UNION)
str = tp->t_str;
}
/*
* If this struct/union has a member with the name of msym, return
* return this it.
*/
if (str != NULL) {
for (sym = msym; sym != NULL; sym = sym->s_link) {
if (sym->s_scl != MOS && sym->s_scl != MOU)
continue;
if (sym->s_styp != str)
continue;
if (strcmp(sym->s_name, msym->s_name) != 0)
continue;
return (sym);
}
}
/*
* Set eq to 0 if there are struct/union members with the same name
* and different types and/or offsets.
*/
eq = 1;
for (csym = msym; csym != NULL; csym = csym->s_link) {
if (csym->s_scl != MOS && csym->s_scl != MOU)
continue;
if (strcmp(msym->s_name, csym->s_name) != 0)
continue;
for (sym = csym->s_link ; sym != NULL; sym = sym->s_link) {
int w;
if (sym->s_scl != MOS && sym->s_scl != MOU)
continue;
if (strcmp(csym->s_name, sym->s_name) != 0)
continue;
if (csym->s_value.v_quad != sym->s_value.v_quad) {
eq = 0;
break;
}
w = 0;
eq = eqtype(csym->s_type, sym->s_type, 0, 0, &w) && !w;
if (!eq)
break;
if (csym->s_field != sym->s_field) {
eq = 0;
break;
}
if (csym->s_field) {
type_t *tp1, *tp2;
tp1 = csym->s_type;
tp2 = sym->s_type;
if (tp1->t_flen != tp2->t_flen) {
eq = 0;
break;
}
if (tp1->t_foffs != tp2->t_foffs) {
eq = 0;
break;
}
}
}
if (!eq)
break;
}
/*
* Now handle the case in which the left operand refers really
* to a struct/union, but the right operand is not member of it.
*/
if (str != NULL) {
/* illegal member use: %s */
if (eq && tflag) {
warning(102, msym->s_name);
} else {
error(102, msym->s_name);
}
return (msym);
}
/*
* Now the left operand of ARROW does not point to a struct/union
* or the left operand of POINT is no struct/union.
*/
if (eq) {
if (op == POINT) {
/* left operand of "." must be struct/union object */
if (tflag) {
warning(103);
} else {
error(103);
}
} else {
/* left operand of "->" must be pointer to ... */
if (tflag && tn->tn_type->t_tspec == PTR) {
warning(104);
} else {
error(104);
}
}
} else {
if (tflag) {
/* non-unique member requires struct/union %s */
error(105, op == POINT ? "object" : "pointer");
} else {
/* unacceptable operand of %s */
error(111, modtab[op].m_name);
}
}
return (msym);
}
/*
* Create a tree node. Called for most operands except function calls,
* sizeof and casts.
*
* op operator
* ln left operand
* rn if not NULL, right operand
*/
tnode_t *
build(op_t op, tnode_t *ln, tnode_t *rn)
{
mod_t *mp;
tnode_t *ntn;
type_t *rtp;
mp = &modtab[op];
/* If there was an error in one of the operands, return. */
if (ln == NULL || (mp->m_binary && rn == NULL))
return (NULL);
/*
* Apply class conversions to the left operand, but only if its
* value is needed or it is compared with null.
*/
if (mp->m_vctx || mp->m_tctx)
ln = cconv(ln);
/*
* The right operand is almost always in a test or value context,
* except if it is a struct or union member.
*/
if (mp->m_binary && op != ARROW && op != POINT)
rn = cconv(rn);
/*
* Print some warnings for comparisons of unsigned values with
* constants lower than or equal to null. This must be done
* before promote() because otherwise unsigned char and unsigned
* short would be promoted to int. Also types are tested to be
* CHAR, which would also become int.
*/
if (mp->m_comp)
chkcomp(op, ln, rn);
/*
* Promote the left operand if it is in a test or value context
*/
if (mp->m_vctx || mp->m_tctx)
ln = promote(op, 0, ln);
/*
* Promote the right operand, but only if it is no struct or
* union member, or if it is not to be assigned to the left operand
*/
if (mp->m_binary && op != ARROW && op != POINT &&
op != ASSIGN && op != RETURN) {
rn = promote(op, 0, rn);
}
/*
* If the result of the operation is different for signed or
* unsigned operands and one of the operands is signed only in
* ANSI C, print a warning.
*/
if (mp->m_tlansiu && ln->tn_op == CON && ln->tn_val->v_ansiu) {
/* ANSI C treats constant as unsigned, op %s */
warning(218, mp->m_name);
ln->tn_val->v_ansiu = 0;
}
if (mp->m_transiu && rn->tn_op == CON && rn->tn_val->v_ansiu) {
/* ANSI C treats constant as unsigned, op %s */
warning(218, mp->m_name);
rn->tn_val->v_ansiu = 0;
}
/* Make sure both operands are of the same type */
if (mp->m_balance || (tflag && (op == SHL || op == SHR)))
balance(op, &ln, &rn);
/*
* Check types for compatibility with the operation and mutual
* compatibility. Return if there are serious problems.
*/
if (!typeok(op, NULL, ln, rn))
return (NULL);
/* And now create the node. */
switch (op) {
case POINT:
case ARROW:
ntn = bldstr(op, ln, rn);
break;
case INCAFT:
case DECAFT:
case INCBEF:
case DECBEF:
ntn = bldincdec(op, ln);
break;
case AMPER:
ntn = bldamper(ln, 0);
break;
case STAR:
ntn = mktnode(STAR, ln->tn_type->t_subt, ln, NULL);
break;
case PLUS:
case MINUS:
ntn = bldplmi(op, ln, rn);
break;
case SHL:
case SHR:
ntn = bldshft(op, ln, rn);
break;
case COLON:
ntn = bldcol(ln, rn);
break;
case ASSIGN:
case MULASS:
case DIVASS:
case MODASS:
case ADDASS:
case SUBASS:
case SHLASS:
case SHRASS:
case ANDASS:
case XORASS:
case ORASS:
case RETURN:
ntn = bldasgn(op, ln, rn);
break;
case COMMA:
case QUEST:
ntn = mktnode(op, rn->tn_type, ln, rn);
break;
default:
rtp = mp->m_logop ? gettyp(INT) : ln->tn_type;
if (!mp->m_binary && rn != NULL)
lerror("build() 1");
ntn = mktnode(op, rtp, ln, rn);
break;
}
/* Return if an error occurred. */
if (ntn == NULL)
return (NULL);
/* Print a warning if precedence confusion is possible */
if (mp->m_tpconf)
precconf(ntn);
/*
* Print a warning if one of the operands is in a context where
* it is compared with null and if this operand is a constant.
*/
if (mp->m_tctx) {
if (ln->tn_op == CON ||
((mp->m_binary && op != QUEST) && rn->tn_op == CON)) {
if (hflag && !ccflg) {
/* constant in conditional context */
warning(161);
}
}
}
/* Fold if the operator requires it */
if (mp->m_fold) {
if (ln->tn_op == CON && (!mp->m_binary || rn->tn_op == CON)) {
if (mp->m_tctx) {
ntn = foldtst(ntn);
} else if (isftyp(ntn->tn_type->t_tspec)) {
ntn = foldflt(ntn);
} else {
ntn = fold(ntn);
}
} else if (op == QUEST && ln->tn_op == CON) {
ntn = ln->tn_val->v_quad ? rn->tn_left : rn->tn_right;
}
}
return (ntn);
}
/*
* Perform class conversions.
*
* Arrays of type T are converted into pointers to type T.
* Functions are converted to pointers to functions.
* Lvalues are converted to rvalues.
*/
tnode_t *
cconv(tnode_t *tn)
{
type_t *tp;
/*
* Array-lvalue (array of type T) is converted into rvalue
* (pointer to type T)
*/
if (tn->tn_type->t_tspec == ARRAY) {
if (!tn->tn_lvalue) {
/* operand of '%s' must be lvalue */
/* XXX print correct operator */
(void)gnuism(114, "", modtab[AMPER].m_name);
}
tn = mktnode(AMPER, tincref(tn->tn_type->t_subt, PTR),
tn, NULL);
}
/*
* Expression of type function (function with return value of type T)
* in rvalue-expression (pointer to function with return value
* of type T)
*/
if (tn->tn_type->t_tspec == FUNC)
tn = bldamper(tn, 1);
/* lvalue to rvalue */
if (tn->tn_lvalue) {
tp = tduptyp(tn->tn_type);
tp->t_const = tp->t_volatile = 0;
tn = mktnode(LOAD, tp, tn, NULL);
}
return (tn);
}
/*
* Perform most type checks. First the types are checked using
* information from modtab[]. After that it is done by hand for
* more complicated operators and type combinations.
*
* If the types are ok, typeok() returns 1, otherwise 0.
*/
int
typeok(op_t op, farg_t *farg, tnode_t *ln, tnode_t *rn)
{
mod_t *mp;
tspec_t lt, rt = NOTSPEC, lst = NOTSPEC, rst = NOTSPEC,
olt = NOTSPEC, ort = NOTSPEC;
type_t *ltp, *rtp, *lstp, *rstp;
tnode_t *tn;
int arg = (farg) ? farg->fa_num : 0;
mp = &modtab[op];
if ((lt = (ltp = ln->tn_type)->t_tspec) == PTR)
lst = (lstp = ltp->t_subt)->t_tspec;
if (mp->m_binary) {
if ((rt = (rtp = rn->tn_type)->t_tspec) == PTR)
rst = (rstp = rtp->t_subt)->t_tspec;
}
if (mp->m_rqint) {
/* integer types required */
if (!isityp(lt) || (mp->m_binary && !isityp(rt))) {
incompat(op, lt, rt);
return (0);
}
} else if (mp->m_rqsclt) {
/* scalar types required */
if (!issclt(lt) || (mp->m_binary && !issclt(rt))) {
incompat(op, lt, rt);
return (0);
}
} else if (mp->m_rqatyp) {
/* arithmetic types required */
if (!isatyp(lt) || (mp->m_binary && !isatyp(rt))) {
incompat(op, lt, rt);
return (0);
}
}
if (op == SHL || op == SHR || op == SHLASS || op == SHRASS) {
/*
* For these operations we need the types before promotion
* and balancing.
*/
for (tn=ln; tn->tn_op==CVT && !tn->tn_cast; tn=tn->tn_left) ;
olt = tn->tn_type->t_tspec;
for (tn=rn; tn->tn_op==CVT && !tn->tn_cast; tn=tn->tn_left) ;
ort = tn->tn_type->t_tspec;
}
switch (op) {
case POINT:
/*
* Most errors required by ANSI C are reported in strmemb().
* Here we only must check for totally wrong things.
*/
if (lt == FUNC || lt == VOID || ltp->t_isfield ||
((lt != STRUCT && lt != UNION) && !ln->tn_lvalue)) {
/* Without tflag we already have an error */
if (tflag)
/* unacceptable operand of %s */
error(111, mp->m_name);
return (0);
}
/* Now we have an object we can create a pointer to */
break;
case ARROW:
if (lt != PTR && !(tflag && isityp(lt))) {
/* Without tflag we already have an error */
if (tflag)
/* unacceptable operand of %s */
error(111, mp->m_name);
return (0);
}
break;
case INCAFT:
case DECAFT:
case INCBEF:
case DECBEF:
/* operands have scalar types (checked above) */
if (!ln->tn_lvalue) {
if (ln->tn_op == CVT && ln->tn_cast &&
ln->tn_left->tn_op == LOAD) {
/* a cast does not yield an lvalue */
error(163);
}
/* %soperand of %s must be lvalue */
error(114, "", mp->m_name);
return (0);
} else if (ltp->t_const) {
/* %soperand of %s must be modifiable lvalue */
if (!tflag)
warning(115, "", mp->m_name);
}
break;
case AMPER:
if (lt == ARRAY || lt == FUNC) {
/* ok, a warning comes later (in bldamper()) */
} else if (!ln->tn_lvalue) {
if (ln->tn_op == CVT && ln->tn_cast &&
ln->tn_left->tn_op == LOAD) {
/* a cast does not yield an lvalue */
error(163);
}
/* %soperand of %s must be lvalue */
error(114, "", mp->m_name);
return (0);
} else if (issclt(lt)) {
if (ltp->t_isfield) {
/* cannot take address of bit-field */
error(112);
return (0);
}
} else if (lt != STRUCT && lt != UNION) {
/* unacceptable operand of %s */
error(111, mp->m_name);
return (0);
}
if (ln->tn_op == NAME && ln->tn_sym->s_reg) {
/* cannot take address of register %s */
error(113, ln->tn_sym->s_name);
return (0);
}
break;
case STAR:
/* until now there were no type checks for this operator */
if (lt != PTR) {
/* cannot dereference non-pointer type */
error(96);
return (0);
}
break;
case PLUS:
/* operands have scalar types (checked above) */
if ((lt == PTR && !isityp(rt)) || (rt == PTR && !isityp(lt))) {
incompat(op, lt, rt);
return (0);
}
break;
case MINUS:
/* operands have scalar types (checked above) */
if (lt == PTR && (!isityp(rt) && rt != PTR)) {
incompat(op, lt, rt);
return (0);
} else if (rt == PTR && lt != PTR) {
incompat(op, lt, rt);
return (0);
}
if (lt == PTR && rt == PTR) {
if (!eqtype(lstp, rstp, 1, 0, NULL)) {
/* illegal pointer subtraction */
error(116);
}
}
break;
case SHR:
/* operands have integer types (checked above) */
if (pflag && !isutyp(lt)) {
/*
* The left operand is signed. This means that
* the operation is (possibly) nonportable.
*/
/* bitwise operation on signed value nonportable */
if (ln->tn_op != CON) {
/* possibly nonportable */
warning(117);
} else if (ln->tn_val->v_quad < 0) {
warning(120);
}
} else if (!tflag && !sflag && !isutyp(olt) && isutyp(ort)) {
/*
* The left operand would become unsigned in
* traditional C.
*/
if (hflag &&
(ln->tn_op != CON || ln->tn_val->v_quad < 0)) {
/* semantics of %s change in ANSI C; use ... */
warning(118, mp->m_name);
}
} else if (!tflag && !sflag && !isutyp(olt) && !isutyp(ort) &&
psize(lt) < psize(rt)) {
/*
* In traditional C the left operand would be extended,
* possibly with 1, and then shifted.
*/
if (hflag &&
(ln->tn_op != CON || ln->tn_val->v_quad < 0)) {
/* semantics of %s change in ANSI C; use ... */
warning(118, mp->m_name);
}
}
/* right shift of %d-bit quantity by %lld bits */
if (rn->tn_op == CON && size(olt) <= rn->tn_val->v_quad)
warning(310, size(olt), rn->tn_val->v_quad);
goto shift;
case SHL:
/*
* ANSI C does not perform balancing for shift operations,
* but traditional C does. If the width of the right operand
* is greather than the width of the left operand, than in
* traditional C the left operand would be extended to the
* width of the right operand. For SHL this may result in
* different results.
*/
if (psize(lt) < psize(rt)) {
/*
* XXX If both operands are constant make sure
* that there is really a difference between
* ANSI C and traditional C.
*/
if (hflag)
/* semantics of %s change in ANSI C; use ... */
warning(118, mp->m_name);
}
shift:
if (rn->tn_op == CON) {
if (!isutyp(rt) && rn->tn_val->v_quad < 0) {
/* negative shift */
warning(121);
} else if ((u_quad_t)rn->tn_val->v_quad == size(lt)) {
/* shift equal to size of object */
warning(267);
} else if ((u_quad_t)rn->tn_val->v_quad > size(lt)) {
/* shift greater than size of object */
warning(122);
}
}
break;
case EQ:
case NE:
/*
* Accept some things which are allowed with EQ and NE,
* but not with ordered comparisons.
*/
if (lt == PTR && ((rt == PTR && rst == VOID) || isityp(rt))) {
if (rn->tn_op == CON && rn->tn_val->v_quad == 0)
break;
}
if (rt == PTR && ((lt == PTR && lst == VOID) || isityp(lt))) {
if (ln->tn_op == CON && ln->tn_val->v_quad == 0)
break;
}
/* FALLTHROUGH */
case LT:
case GT:
case LE:
case GE:
if ((lt == PTR || rt == PTR) && lt != rt) {
if (isityp(lt) || isityp(rt)) {
/* illegal comb. of pointer and int., op %s */
warning(123, mp->m_name);
} else {
incompat(op, lt, rt);
return (0);
}
} else if (lt == PTR && rt == PTR) {
ptrcmpok(op, ln, rn);
}
break;
case QUEST:
if (!issclt(lt)) {
/* first operand must have scalar type, op ? : */
error(170);
return (0);
}
if (rn->tn_op != COLON)
lerror("typeok() 2");
break;
case COLON:
if (isatyp(lt) && isatyp(rt))
break;
if (lt == STRUCT && rt == STRUCT && ltp->t_str == rtp->t_str)
break;
if (lt == UNION && rt == UNION && ltp->t_str == rtp->t_str)
break;
/* combination of any pointer and 0, 0L or (void *)0 is ok */
if (lt == PTR && ((rt == PTR && rst == VOID) || isityp(rt))) {
if (rn->tn_op == CON && rn->tn_val->v_quad == 0)
break;
}
if (rt == PTR && ((lt == PTR && lst == VOID) || isityp(lt))) {
if (ln->tn_op == CON && ln->tn_val->v_quad == 0)
break;
}
if ((lt == PTR && isityp(rt)) || (isityp(lt) && rt == PTR)) {
/* illegal comb. of ptr. and int., op %s */
warning(123, mp->m_name);
break;
}
if (lt == VOID || rt == VOID) {
if (lt != VOID || rt != VOID)
/* incompatible types in conditional */
warning(126);
break;
}
if (lt == PTR && rt == PTR && ((lst == VOID && rst == FUNC) ||
(lst == FUNC && rst == VOID))) {
/* (void *)0 handled above */
if (sflag)
/* ANSI C forbids conv. of %s to %s, op %s */
warning(305, "function pointer", "'void *'",
mp->m_name);
break;
}
if (rt == PTR && lt == PTR) {
if (!eqtype(lstp, rstp, 1, 0, NULL))
illptrc(mp, ltp, rtp);
break;
}
/* incompatible types in conditional */
error(126);
return (0);
case ASSIGN:
case INIT:
case FARG:
case RETURN:
if (!asgntypok(op, farg, ln, rn))
return (0);
goto assign;
case MULASS:
case DIVASS:
case MODASS:
goto assign;
case ADDASS:
case SUBASS:
/* operands have scalar types (checked above) */
if ((lt == PTR && !isityp(rt)) || rt == PTR) {
incompat(op, lt, rt);
return (0);
}
goto assign;
case SHLASS:
goto assign;
case SHRASS:
if (pflag && !isutyp(lt) && !(tflag && isutyp(rt))) {
/* bitwise operation on s.v. possibly nonportable */
warning(117);
}
/* right shift of %d-bit quantity by %d bits */
if (rn->tn_op == CON && size(olt) <= rn->tn_val->v_quad)
warning(310, size(olt), rn->tn_val->v_quad);
goto assign;
case ANDASS:
case XORASS:
case ORASS:
goto assign;
assign:
if (!ln->tn_lvalue) {
if (ln->tn_op == CVT && ln->tn_cast &&
ln->tn_left->tn_op == LOAD) {
/* a cast does not yield an lvalue */
error(163);
}
/* %soperand of %s must be lvalue */
error(114, "left ", mp->m_name);
return (0);
} else if (ltp->t_const || ((lt == STRUCT || lt == UNION) &&
conmemb(ltp))) {
/* %soperand of %s must be modifiable lvalue */
if (!tflag)
warning(115, "left ", mp->m_name);
}
break;
case COMMA:
if (!modtab[ln->tn_op].m_sideeff)
nulleff(ln);
break;
/* LINTED (enumeration values not handled in switch) */
}
if (mp->m_badeop &&
(ltp->t_isenum || (mp->m_binary && rtp->t_isenum))) {
chkbeop(op, ln, rn);
} else if (mp->m_enumop && (ltp->t_isenum && rtp->t_isenum)) {
chkeop2(op, farg, ln, rn);
} else if (mp->m_enumop && (ltp->t_isenum || rtp->t_isenum)) {
chkeop1(op, farg, ln, rn);
}
return (1);
}
static void
ptrcmpok(op_t op, tnode_t *ln, tnode_t *rn)
{
type_t *ltp, *rtp;
tspec_t lt, rt;
const char *lts, *rts;
lt = (ltp = ln->tn_type)->t_subt->t_tspec;
rt = (rtp = rn->tn_type)->t_subt->t_tspec;
if (lt == VOID || rt == VOID) {
if (sflag && (lt == FUNC || rt == FUNC)) {
/* (void *)0 already handled in typeok() */
*(lt == FUNC ? <s : &rts) = "function pointer";
*(lt == VOID ? <s : &rts) = "'void *'";
/* ANSI C forbids comparison of %s with %s */
warning(274, lts, rts);
}
return;
}
if (!eqtype(ltp->t_subt, rtp->t_subt, 1, 0, NULL)) {
illptrc(&modtab[op], ltp, rtp);
return;
}
if (lt == FUNC && rt == FUNC) {
if (sflag && op != EQ && op != NE)
/* ANSI C forbids ordered comp. of func ptr */
warning(125);
}
}
/*
* Checks type compatibility for ASSIGN, INIT, FARG and RETURN
* and prints warnings/errors if necessary.
* If the types are (almost) compatible, 1 is returned, otherwise 0.
*/
static int
asgntypok(op_t op, farg_t *farg, tnode_t *ln, tnode_t *rn)
{
tspec_t lt, rt, lst = NOTSPEC, rst = NOTSPEC;
type_t *ltp, *rtp, *lstp, *rstp;
mod_t *mp;
const char *lts, *rts;
int arg = (farg) ? farg->fa_num : 0;
if ((lt = (ltp = ln->tn_type)->t_tspec) == PTR)
lst = (lstp = ltp->t_subt)->t_tspec;
if ((rt = (rtp = rn->tn_type)->t_tspec) == PTR)
rst = (rstp = rtp->t_subt)->t_tspec;
mp = &modtab[op];
/* be picky about conversion of function return types */
if (rn->tn_op == CALL) {
if (size(lt) < size(rt) ||
(size(lt) == size(rt) && isutyp(lt) && !isutyp(rt))) {
warning(313, funcname(rn),
tyname(rn->tn_type), tyname(ln->tn_type));
}
}
if (isatyp(lt) && isatyp(rt))
return (1);
if ((lt == STRUCT || lt == UNION) && (rt == STRUCT || rt == UNION))
/* both are struct or union */
return (ltp->t_str == rtp->t_str);
/* 0, 0L and (void *)0 may be assigned to any pointer */
if (lt == PTR && ((rt == PTR && rst == VOID) || isityp(rt))) {
if (rn->tn_op == CON && rn->tn_val->v_quad == 0)
return (1);
}
if (lt == PTR && rt == PTR && (lst == VOID || rst == VOID)) {
/* two pointers, at least one pointer to void */
if (sflag && (lst == FUNC || rst == FUNC)) {
/* comb. of ptr to func and ptr to void */
*(lst == FUNC ? <s : &rts) = "function pointer";
*(lst == VOID ? <s : &rts) = "'void *'";
switch (op) {
case INIT:
case RETURN:
/* ANSI C forbids conversion of %s to %s */
warning(303, rts, lts);
break;
case FARG:
/* ANSI C forbids conv. of %s to %s, arg #%d */
warning(304, rts, lts, arg);
break;
default:
/* ANSI C forbids conv. of %s to %s, op %s */
warning(305, rts, lts, mp->m_name);
break;
}
}
}
if (lt == PTR && rt == PTR && (lst == VOID || rst == VOID ||
eqtype(lstp, rstp, 1, 0, NULL))) {
/* compatible pointer types (qualifiers ignored) */
if (!tflag &&
((!lstp->t_const && rstp->t_const) ||
(!lstp->t_volatile && rstp->t_volatile))) {
/* left side has not all qualifiers of right */
switch (op) {
case INIT:
case RETURN:
/* incompatible pointer types */
warning(182);
break;
case FARG:
/* %s arg #%d: incompatible ptr. type */
warning(153, funcname(farg->fa_func), arg);
break;
default:
/* operands have incompat. ptr. types, op %s */
warning(128, mp->m_name);
break;
}
}
return (1);
}
if ((lt == PTR && isityp(rt)) || (isityp(lt) && rt == PTR)) {
switch (op) {
case INIT:
case RETURN:
/* illegal combination of pointer and integer */
warning(183);
break;
case FARG:
/* %s arg #%d: illegal comb. of ptr. and int. */
warning(154, funcname(farg->fa_func), arg);
break;
default:
/* illegal comb. of ptr. and int., op %s */
warning(123, mp->m_name);
break;
}
return (1);
}
if (lt == PTR && rt == PTR) {
switch (op) {
case INIT:
case RETURN:
illptrc(NULL, ltp, rtp);
break;
case FARG:
/* %s arg #%d: incompatible pointer type */
warning(153, funcname(farg->fa_func), arg);
break;
default:
illptrc(mp, ltp, rtp);
break;
}
return (1);
}
switch (op) {
case INIT:
/* initialisation type mismatch */
error(185);
break;
case RETURN:
/* return value type mismatch */
error(211);
break;
case FARG:
/* %s arg #%d: argument is incompatible with prototype */
warning(155, funcname(farg->fa_func), arg);
break;
default:
incompat(op, lt, rt);
break;
}
return (0);
}
/*
* Prints a warning if an operator, which should be senseless for an
* enum type, is applied to an enum type.
*/
static void
chkbeop(op_t op, tnode_t *ln, tnode_t *rn)
{
mod_t *mp;
if (!eflag)
return;
mp = &modtab[op];
if (!(ln->tn_type->t_isenum ||
(mp->m_binary && rn->tn_type->t_isenum))) {
return;
}
/*
* Enum as offset to a pointer is an exception (otherwise enums
* could not be used as array indizes).
*/
if (op == PLUS &&
((ln->tn_type->t_isenum && rn->tn_type->t_tspec == PTR) ||
(rn->tn_type->t_isenum && ln->tn_type->t_tspec == PTR))) {
return;
}
/* dubious operation on enum, op %s */
warning(241, mp->m_name);
}
/*
* Prints a warning if an operator is applied to two different enum types.
*/
static void
chkeop2(op_t op, farg_t *farg, tnode_t *ln, tnode_t *rn)
{
mod_t *mp;
int arg = (farg) ? farg->fa_num : 0;
mp = &modtab[op];
if (ln->tn_type->t_enum != rn->tn_type->t_enum) {
switch (op) {
case INIT:
/* enum type mismatch in initialisation */
warning(210);
break;
case FARG:
/* %s arg #%d: enum type mismatch */
warning(156, funcname(farg->fa_func), arg);
break;
case RETURN:
/* return value type mismatch */
warning(211);
break;
default:
/* enum type mismatch, op %s */
warning(130, mp->m_name);
break;
}
#if 0
} else if (mp->m_comp && op != EQ && op != NE) {
if (eflag)
/* dubious comparisons of enums */
warning(243, mp->m_name);
#endif
}
}
/*
* Prints a warning if an operator has both enum end other integer
* types.
*/
static void
chkeop1(op_t op, farg_t *farg, tnode_t *ln, tnode_t *rn)
{
int arg = (farg) ? farg->fa_num : 0;
if (!eflag)
return;
switch (op) {
case INIT:
/*
* Initializations with 0 should be allowed. Otherwise,
* we should complain about all uninitialized enums,
* consequently.
*/
if (!rn->tn_type->t_isenum && rn->tn_op == CON &&
isityp(rn->tn_type->t_tspec) && rn->tn_val->v_quad == 0) {
return;
}
/* initialisation of '%s' with '%s' */
warning(277, tyname(ln->tn_type), tyname(rn->tn_type));
break;
case FARG:
/* %s arg #%d: combination of '%s' and '%s' */
warning(278, funcname(farg->fa_func), arg, tyname(ln->tn_type), tyname(rn->tn_type));
break;
case RETURN:
/* combination of '%s' and '%s' in return */
warning(279, tyname(ln->tn_type), tyname(rn->tn_type));
break;
default:
/* combination of '%s' and %s, op %s */
warning(242, tyname(ln->tn_type), tyname(rn->tn_type),
modtab[op].m_name);
break;
}
}
/*
* Build and initialize a new node.
*/
static tnode_t *
mktnode(op_t op, type_t *type, tnode_t *ln, tnode_t *rn)
{
tnode_t *ntn;
tspec_t t;
ntn = getnode();
ntn->tn_op = op;
ntn->tn_type = type;
ntn->tn_left = ln;
ntn->tn_right = rn;
if (op == STAR || op == FSEL) {
if (ln->tn_type->t_tspec == PTR) {
t = ln->tn_type->t_subt->t_tspec;
if (t != FUNC && t != VOID)
ntn->tn_lvalue = 1;
} else {
lerror("mktnode() 2");
}
}
return (ntn);
}
/*
* Performs usual conversion of operands to (unsigned) int.
*
* If tflag is set or the operand is a function argument with no
* type information (no prototype or variable # of args), convert
* float to double.
*/
tnode_t *
promote(op_t op, int farg, tnode_t *tn)
{
tspec_t t;
type_t *ntp;
int len;
t = tn->tn_type->t_tspec;
if (!isatyp(t))
return (tn);
if (!tflag) {
/*
* ANSI C requires that the result is always of type INT
* if INT can represent all possible values of the previous
* type.
*/
if (tn->tn_type->t_isfield) {
len = tn->tn_type->t_flen;
if (size(INT) > len) {
t = INT;
} else {
if (size(INT) != len)
lerror("promote() 1");
if (isutyp(t)) {
t = UINT;
} else {
t = INT;
}
}
} else if (t == CHAR || t == UCHAR || t == SCHAR) {
t = (size(CHAR) < size(INT) || t != UCHAR) ?
INT : UINT;
} else if (t == SHORT || t == USHORT) {
t = (size(SHORT) < size(INT) || t == SHORT) ?
INT : UINT;
} else if (t == ENUM) {
t = INT;
} else if (farg && t == FLOAT) {
t = DOUBLE;
}
} else {
/*
* In traditional C, keep unsigned and promote FLOAT
* to DOUBLE.
*/
if (t == UCHAR || t == USHORT) {
t = UINT;
} else if (t == CHAR || t == SCHAR || t == SHORT) {
t = INT;
} else if (t == FLOAT) {
t = DOUBLE;
} else if (t == ENUM) {
t = INT;
}
}
if (t != tn->tn_type->t_tspec) {
ntp = tduptyp(tn->tn_type);
ntp->t_tspec = t;
/*
* Keep t_isenum so we are later able to check compatibility
* of enum types.
*/
tn = convert(op, NULL, ntp, tn);
}
return (tn);
}
/*
* Insert conversions which are necessary to give both operands the same
* type. This is done in different ways for traditional C and ANSI C.
*/
static void
balance(op_t op, tnode_t **lnp, tnode_t **rnp)
{
tspec_t lt, rt, t;
int i, u;
type_t *ntp;
static tspec_t tl[] = {
LDOUBLE, DOUBLE, FLOAT, UQUAD, QUAD, ULONG, LONG, UINT, INT,
};
lt = (*lnp)->tn_type->t_tspec;
rt = (*rnp)->tn_type->t_tspec;
if (!isatyp(lt) || !isatyp(rt))
return;
if (!tflag) {
if (lt == rt) {
t = lt;
} else if (lt == LDOUBLE || rt == LDOUBLE) {
t = LDOUBLE;
} else if (lt == DOUBLE || rt == DOUBLE) {
t = DOUBLE;
} else if (lt == FLOAT || rt == FLOAT) {
t = FLOAT;
} else {
/*
* If type A has more bits than type B it should
* be able to hold all possible values of type B.
*/
if (size(lt) > size(rt)) {
t = lt;
} else if (size(lt) < size(rt)) {
t = rt;
} else {
for (i = 3; tl[i] != INT; i++) {
if (tl[i] == lt || tl[i] == rt)
break;
}
if ((isutyp(lt) || isutyp(rt)) &&
!isutyp(tl[i])) {
i--;
}
t = tl[i];
}
}
} else {
/* Keep unsigned in traditional C */
u = isutyp(lt) || isutyp(rt);
for (i = 0; tl[i] != INT; i++) {
if (lt == tl[i] || rt == tl[i])
break;
}
t = tl[i];
if (u && isityp(t) && !isutyp(t))
t = utyp(t);
}
if (t != lt) {
ntp = tduptyp((*lnp)->tn_type);
ntp->t_tspec = t;
*lnp = convert(op, NULL, ntp, *lnp);
}
if (t != rt) {
ntp = tduptyp((*rnp)->tn_type);
ntp->t_tspec = t;
*rnp = convert(op, NULL, ntp, *rnp);
}
}
/*
* Insert a conversion operator, which converts the type of the node
* to another given type.
* If op is FARG, farg contains the passed argument information.
*/
tnode_t *
convert(op_t op, farg_t *farg, type_t *tp, tnode_t *tn)
{
tnode_t *ntn;
tspec_t nt, ot, ost = NOTSPEC;
int arg = (farg) ? farg->fa_num : 0;
if (tn->tn_lvalue)
lerror("convert() 1");
nt = tp->t_tspec;
if ((ot = tn->tn_type->t_tspec) == PTR)
ost = tn->tn_type->t_subt->t_tspec;
if (!tflag && !sflag && op == FARG)
ptconv(farg, nt, ot, tp, tn);
if (isityp(nt) && isityp(ot)) {
iiconv(op, farg, nt, ot, tp, tn);
} else if (nt == PTR && ((ot == PTR && ost == VOID) || isityp(ot)) &&
tn->tn_op == CON && tn->tn_val->v_quad == 0) {
/* 0, 0L and (void *)0 may be assigned to any pointer. */
} else if (isityp(nt) && ot == PTR) {
piconv(op, nt, tp, tn);
} else if (nt == PTR && ot == PTR) {
ppconv(op, tn, tp);
}
ntn = getnode();
ntn->tn_op = CVT;
ntn->tn_type = tp;
ntn->tn_cast = op == CVT;
if (tn->tn_op != CON || nt == VOID) {
ntn->tn_left = tn;
} else {
ntn->tn_op = CON;
ntn->tn_val = tgetblk(sizeof (val_t));
cvtcon(op, farg, ntn->tn_type, ntn->tn_val, tn->tn_val);
}
return (ntn);
}
/*
* Print a warning if a prototype causes a type conversion that is
* different from what would happen to the same argument in the
* absence of a prototype.
*
* Errors/Warnings about illegal type combinations are already printed
* in asgntypok().
*/
static void
ptconv(farg_t *farg, tspec_t nt, tspec_t ot, type_t *tp, tnode_t *tn)
{
tnode_t *ptn;
int arg = farg->fa_num;
if (!isatyp(nt) || !isatyp(ot))
return;
/*
* If the type of the formal parameter is char/short, a warning
* would be useless, because functions declared the old style
* can't expect char/short arguments.
*/
if (nt == CHAR || nt == UCHAR || nt == SHORT || nt == USHORT)
return;
/* get default promotion */
ptn = promote(NOOP, 1, tn);
ot = ptn->tn_type->t_tspec;
/* return if types are the same with and without prototype */
if (nt == ot || (nt == ENUM && ot == INT))
return;
if (isftyp(nt) != isftyp(ot) || psize(nt) != psize(ot)) {
/* representation and/or width change */
if (styp(nt) != SHORT || !isityp(ot) || psize(ot) > psize(INT)) {
if (ptn->tn_op == CON) {
/* ok. promote() warns if constant out of range */
} else {
/* %s arg #%d: converted from '%s' to '%s' */
warning(259, funcname(farg->fa_func), arg,
tyname(tn->tn_type), tyname(tp));
}
}
} else if (hflag) {
/*
* they differ in sign or base type (char, short, int,
* long, long long, float, double, long double)
*
* if they differ only in sign and the argument is a constant
* and the msb of the argument is not set, print no warning
*/
if (ptn->tn_op == CON && isityp(nt) && styp(nt) == styp(ot) &&
msb(ptn->tn_val->v_quad, ot, -1) == 0) {
/* ok */
} else if (ptn->tn_op != CON) {
/* %s arg #%d: converted from '%s' to '%s' */
warning(259, funcname(farg->fa_func), arg,
tyname(tn->tn_type), tyname(tp));
}
}
}
/*
* Print warnings for conversions of integer types which may cause
* problems.
*/
/* ARGSUSED */
static void
iiconv(op_t op, farg_t *farg, tspec_t nt, tspec_t ot, type_t *tp, tnode_t *tn)
{
int arg = (farg) ? farg->fa_num : 0;
if (tn->tn_op == CON)
return;
if (op == CVT)
return;
#if 0
if (psize(nt) > psize(ot) && isutyp(nt) != isutyp(ot)) {
/* conversion to %s may sign-extend incorrectly */
if (pflag) {
if (op == FARG) {
warning(297, funcname(farg->fa_func), arg,
tyname(tp));
} else {
warning(131, tyname(tp));
}
}
}
#endif
if (rank(nt) < rank(ot)) {
/* coercion from greater to lesser width */
if (op == FARG) {
warning(298, funcname(farg->fa_func), arg,
tyname(tn->tn_type), tyname(tp));
} else {
warning(132, tyname(tn->tn_type), tyname(tp));
}
} else if (isutyp(nt) != isutyp(ot) && rank(nt) == rank(ot)) {
/* coercion to same width but with signedness change */
if (op == FARG) {
warning(298, funcname(farg->fa_func), arg,
tyname(tn->tn_type), tyname(tp));
} else {
warning(132, tyname(tn->tn_type), tyname(tp));
}
}
}
/*
* Print warnings for dubious conversions of pointer to integer.
*/
static void
piconv(op_t op, tspec_t nt, type_t *tp, tnode_t *tn)
{
if (tn->tn_op == CON)
return;
if (op != CVT) {
/* We already have an error. */
return;
}
if (psize(nt) < psize(PTR)) {
if (pflag && size(nt) >= size(PTR)) {
/* conv. of pointer to %s may lose bits */
warning(134, tyname(tp));
} else {
/* conv. of pointer to %s loses bits */
warning(133, tyname(tp));
}
}
}
/*
* Print warnings for questionable pointer conversions.
*/
static void
ppconv(op_t op, tnode_t *tn, type_t *tp)
{
tspec_t nt, ot;
const char *nts, *ots;
/*
* We already have an error (pointers of different types
* without a cast) or we will not get a warning.
*/
if (op != CVT)
return;
nt = tp->t_subt->t_tspec;
ot = tn->tn_type->t_subt->t_tspec;
if (nt == VOID || ot == VOID) {
if (sflag && (nt == FUNC || ot == FUNC)) {
/* (void *)0 already handled in convert() */
*(nt == FUNC ? &nts : &ots) = "function pointer";
*(nt == VOID ? &nts : &ots) = "'void *'";
/* ANSI C forbids conversion of %s to %s */
warning(303, ots, nts);
}
return;
} else if (nt == FUNC && ot == FUNC) {
return;
} else if (nt == FUNC || ot == FUNC) {
/* questionable conversion of function pointer */
warning(229);
return;
}
if (getbound(tp->t_subt) > getbound(tn->tn_type->t_subt)) {
if (hflag)
/* possible pointer alignment problem */
warning(135);
}
if (((nt == STRUCT || nt == UNION) &&
tp->t_subt->t_str != tn->tn_type->t_subt->t_str) ||
(psize(nt) != CHAR_BIT && psize(nt) != psize(ot))) {
if (cflag) {
/* pointer casts may be troublesome */
warning(247);
}
}
}
/*
* Converts a typed constant to a constant of another type.
*
* op operator which requires conversion
* farg if op is FARG, the passed argument info (else NULL)
* tp type to which to convert the constant
* nv new constant
* v old constant
*/
void
cvtcon(op_t op, farg_t *farg, type_t *tp, val_t *nv, val_t *v)
{
tspec_t ot, nt;
ldbl_t max = 0, min = 0;
int sz, rchk;
quad_t xmask, xmsk1;
int osz, nsz, arg = (farg) ? farg->fa_num : 0;
ot = v->v_tspec;
nt = nv->v_tspec = tp->t_tspec;
rchk = 0;
if (ot == FLOAT || ot == DOUBLE || ot == LDOUBLE) {
switch (nt) {
case CHAR:
max = CHAR_MAX; min = CHAR_MIN; break;
case UCHAR:
max = UCHAR_MAX; min = 0; break;
case SCHAR:
max = SCHAR_MAX; min = SCHAR_MIN; break;
case SHORT:
max = SHRT_MAX; min = SHRT_MIN; break;
case USHORT:
max = USHRT_MAX; min = 0; break;
case ENUM:
case INT:
max = INT_MAX; min = INT_MIN; break;
case UINT:
max = (u_int)UINT_MAX; min = 0; break;
case LONG:
max = LONG_MAX; min = LONG_MIN; break;
case ULONG:
max = (u_long)ULONG_MAX; min = 0; break;
case QUAD:
max = QUAD_MAX; min = QUAD_MIN; break;
case UQUAD:
max = (u_quad_t)UQUAD_MAX; min = 0; break;
case FLOAT:
max = FLT_MAX; min = -FLT_MAX; break;
case DOUBLE:
max = DBL_MAX; min = -DBL_MAX; break;
case PTR:
/* Already an error because of float --> ptr */
case LDOUBLE:
max = LDBL_MAX; min = -LDBL_MAX; break;
default:
lerror("cvtcon() 1");
}
if (v->v_ldbl > max || v->v_ldbl < min) {
if (nt == LDOUBLE)
lerror("cvtcon() 2");
if (op == FARG) {
/* %s arg #%d: conv. of %s to %s is out of range */
warning(295, funcname(farg->fa_func), arg,
tyname(gettyp(ot)), tyname(tp));
} else {
/* conversion of %s to %s is out of range */
warning(119, tyname(gettyp(ot)), tyname(tp));
}
v->v_ldbl = v->v_ldbl > 0 ? max : min;
}
if (nt == FLOAT) {
nv->v_ldbl = (float)v->v_ldbl;
} else if (nt == DOUBLE) {
nv->v_ldbl = (double)v->v_ldbl;
} else if (nt == LDOUBLE) {
nv->v_ldbl = v->v_ldbl;
} else {
nv->v_quad = (nt == PTR || isutyp(nt)) ?
(u_quad_t)v->v_ldbl : (quad_t)v->v_ldbl;
}
} else {
if (nt == FLOAT) {
nv->v_ldbl = (ot == PTR || isutyp(ot)) ?
(float)(u_quad_t)v->v_quad : (float)v->v_quad;
} else if (nt == DOUBLE) {
nv->v_ldbl = (ot == PTR || isutyp(ot)) ?
(double)(u_quad_t)v->v_quad : (double)v->v_quad;
} else if (nt == LDOUBLE) {
nv->v_ldbl = (ot == PTR || isutyp(ot)) ?
(ldbl_t)(u_quad_t)v->v_quad : (ldbl_t)v->v_quad;
} else {
rchk = 1; /* Check for lost precision. */
nv->v_quad = v->v_quad;
}
}
if (v->v_ansiu && isftyp(nt)) {
/* ANSI C treats constant as unsigned */
warning(157);
v->v_ansiu = 0;
} else if (v->v_ansiu && (isityp(nt) && !isutyp(nt) &&
psize(nt) > psize(ot))) {
/* ANSI C treats constant as unsigned */
warning(157);
v->v_ansiu = 0;
}
if (nt != FLOAT && nt != DOUBLE && nt != LDOUBLE) {
sz = tp->t_isfield ? tp->t_flen : size(nt);
nv->v_quad = xsign(nv->v_quad, nt, sz);
}
if (rchk && op != CVT) {
osz = size(ot);
nsz = tp->t_isfield ? tp->t_flen : size(nt);
xmask = qlmasks[nsz] ^ qlmasks[osz];
xmsk1 = qlmasks[nsz] ^ qlmasks[osz - 1];
/*
* For bitwise operations we are not interested in the
* value, but in the bits itself.
*/
if (op == ORASS || op == OR || op == XOR) {
/*
* Print a warning if bits which were set are
* lost due to the conversion.
* This can happen with operator ORASS only.
*/
if (nsz < osz && (v->v_quad & xmask) != 0) {
/* constant truncated by conv., op %s */
warning(306, modtab[op].m_name);
}
} else if (op == ANDASS || op == AND) {
/*
* Print a warning if additional bits are not all 1
* and the most significant bit of the old value is 1,
* or if at least one (but not all) removed bit was 0.
*/
if (nsz > osz &&
(nv->v_quad & qbmasks[osz - 1]) != 0 &&
(nv->v_quad & xmask) != xmask) {
/*
* extra bits set to 0 in conversion
* of '%s' to '%s', op %s
*/
warning(309, tyname(gettyp(ot)),
tyname(tp), modtab[op].m_name);
} else if (nsz < osz &&
(v->v_quad & xmask) != xmask &&
(v->v_quad & xmask) != 0) {
/* const. truncated by conv., op %s */
warning(306, modtab[op].m_name);
}
} else if ((nt != PTR && isutyp(nt)) &&
(ot != PTR && !isutyp(ot)) && v->v_quad < 0) {
if ((nt == CHAR || nt == UCHAR || nt == SCHAR) &&
v->v_lspec == CHAR) {
/*
* Don't warn when assigning a character
* literal to any kind of character (signed,
* unsigned, or unspecified).
*/
} else if (op == ASSIGN) {
/* assignment of negative constant to ... */
warning(164);
} else if (op == INIT) {
/* initialisation of unsigned with neg. ... */
warning(221);
} else if (op == FARG) {
/* %s arg #%d: conversion of neg. const... */
warning(296, funcname(farg->fa_func), arg);
} else if (modtab[op].m_comp) {
/* we get this warning already in chkcomp() */
} else {
/* conversion of negative constant to ... */
warning(222);
}
} else if (nv->v_quad != v->v_quad && nsz <= osz &&
(v->v_quad & xmask) != 0 &&
(isutyp(ot) || (v->v_quad & xmsk1) != xmsk1)) {
/*
* Loss of significant bit(s). All truncated bits
* of unsigned types or all truncated bits plus the
* msb of the target for signed types are considered
* to be significant bits. Loss of significant bits
* means that at least on of the bits was set in an
* unsigned type or that at least one, but not all of
* the bits was set in an signed type.
* Loss of significant bits means that it is not
* possible, also not with necessary casts, to convert
* back to the original type. A example for a
* necessary cast is:
* char c; int i; c = 128;
* i = c; ** yields -128 **
* i = (unsigned char)c; ** yields 128 **
*/
if ((nt == CHAR || nt == UCHAR || nt == SCHAR) &&
v->v_lspec == CHAR) {
/*
* Don't warn when assigning a character
* literal to any kind of character (signed,
* unsigned, or unspecified).
*/
} else if (op == ASSIGN && tp->t_isfield) {
/* precision lost in bit-field assignment */
warning(166);
} else if (op == ASSIGN) {
/* constant truncated by assignment */
warning(165);
} else if (op == INIT && tp->t_isfield) {
/* bit-field initializer does not fit */
warning(180);
} else if (op == INIT) {
/* initializer does not fit */
warning(178);
} else if (op == CASE) {
/* case label affected by conversion */
warning(196);
} else if (op == FARG) {
/* %s arg #%d: conv. of %s to %s is out of rng. */
warning(295, funcname(farg->fa_func), arg,
tyname(gettyp(ot)), tyname(tp));
} else {
/* conversion of %s to %s is out of range */
warning(119, tyname(gettyp(ot)), tyname(tp));
}
} else if (nv->v_quad != v->v_quad) {
if ((nt == CHAR || nt == UCHAR || nt == SCHAR) &&
v->v_lspec == CHAR) {
/*
* Don't warn when assigning a character
* literal to any kind of character (signed,
* unsigned, or unspecified).
*/
} else if (op == ASSIGN && tp->t_isfield) {
/* precision lost in bit-field assignment */
warning(166);
} else if (op == INIT && tp->t_isfield) {
/* bit-field initializer out of range */
warning(11);
} else if (op == CASE) {
/* case label affected by conversion */
warning(196);
} else if (op == FARG) {
/* %s arg #%d: conv. of %s to %s is out of rng. */
warning(295, funcname(farg->fa_func), arg,
tyname(gettyp(ot)), tyname(tp));
} else {
/* conversion of %s to %s is out of range */
warning(119, tyname(gettyp(ot)), tyname(tp));
}
}
}
}
/*
* Called if incompatible types were detected.
* Prints a appropriate warning.
*/
static void
incompat(op_t op, tspec_t lt, tspec_t rt)
{
mod_t *mp;
mp = &modtab[op];
if (lt == VOID || (mp->m_binary && rt == VOID)) {
/* void type illegal in expression */
error(109);
} else if (op == ASSIGN) {
if ((lt == STRUCT || lt == UNION) &&
(rt == STRUCT || rt == UNION)) {
/* assignment of different structures */
error(240);
} else {
/* assignment type mismatch */
error(171);
}
} else if (mp->m_binary) {
/* operands of %s have incompatible types */
error(107, mp->m_name);
} else {
/* operand of %s has incompatible type */
error(108, mp->m_name);
}
}
/*
* Called if incompatible pointer types are detected.
* Print an appropriate warning.
*/
static void
illptrc(mod_t *mp, type_t *ltp, type_t *rtp)
{
tspec_t lt, rt;
if (ltp->t_tspec != PTR || rtp->t_tspec != PTR)
lerror("illptrc() 1");
lt = ltp->t_subt->t_tspec;
rt = rtp->t_subt->t_tspec;
if ((lt == STRUCT || lt == UNION) && (rt == STRUCT || rt == UNION)) {
if (mp == NULL) {
/* illegal structure pointer combination */
warning(244);
} else {
/* illegal structure pointer combination, op %s */
warning(245, mp->m_name);
}
} else {
if (mp == NULL) {
/* illegal pointer combination */
warning(184);
} else {
/* illegal pointer combination, op %s */
warning(124, mp->m_name);
}
}
}
/*
* Make sure type (*tpp)->t_subt has at least the qualifiers
* of tp1->t_subt and tp2->t_subt.
*/
static void
mrgqual(type_t **tpp, type_t *tp1, type_t *tp2)
{
if ((*tpp)->t_tspec != PTR ||
tp1->t_tspec != PTR || tp2->t_tspec != PTR) {
lerror("mrgqual()");
}
if ((*tpp)->t_subt->t_const ==
(tp1->t_subt->t_const | tp2->t_subt->t_const) &&
(*tpp)->t_subt->t_volatile ==
(tp1->t_subt->t_volatile | tp2->t_subt->t_volatile)) {
return;
}
*tpp = tduptyp(*tpp);
(*tpp)->t_subt = tduptyp((*tpp)->t_subt);
(*tpp)->t_subt->t_const =
tp1->t_subt->t_const | tp2->t_subt->t_const;
(*tpp)->t_subt->t_volatile =
tp1->t_subt->t_volatile | tp2->t_subt->t_volatile;
}
/*
* Returns 1 if the given structure or union has a constant member
* (maybe recursively).
*/
static int
conmemb(type_t *tp)
{
sym_t *m;
tspec_t t;
if ((t = tp->t_tspec) != STRUCT && t != UNION)
lerror("conmemb()");
for (m = tp->t_str->memb; m != NULL; m = m->s_nxt) {
tp = m->s_type;
if (tp->t_const)
return (1);
if ((t = tp->t_tspec) == STRUCT || t == UNION) {
if (conmemb(m->s_type))
return (1);
}
}
return (0);
}
const char *
tyname(type_t *tp)
{
tspec_t t;
const char *s;
if ((t = tp->t_tspec) == INT && tp->t_isenum)
t = ENUM;
switch (t) {
case CHAR: s = "char"; break;
case UCHAR: s = "unsigned char"; break;
case SCHAR: s = "signed char"; break;
case SHORT: s = "short"; break;
case USHORT: s = "unsigned short"; break;
case INT: s = "int"; break;
case UINT: s = "unsigned int"; break;
case LONG: s = "long"; break;
case ULONG: s = "unsigned long"; break;
case QUAD: s = "long long"; break;
case UQUAD: s = "unsigned long long"; break;
case FLOAT: s = "float"; break;
case DOUBLE: s = "double"; break;
case LDOUBLE: s = "long double"; break;
case PTR: s = "pointer"; break;
case ENUM: s = "enum"; break;
case STRUCT: s = "struct"; break;
case UNION: s = "union"; break;
case FUNC: s = "function"; break;
case ARRAY: s = "array"; break;
default:
lerror("tyname()");
}
return (s);
}
/*
* Create a new node for one of the operators POINT and ARROW.
*/
static tnode_t *
bldstr(op_t op, tnode_t *ln, tnode_t *rn)
{
tnode_t *ntn, *ctn;
int nolval;
if (rn->tn_op != NAME)
lerror("bldstr() 1");
if (rn->tn_sym->s_value.v_tspec != INT)
lerror("bldstr() 2");
if (rn->tn_sym->s_scl != MOS && rn->tn_sym->s_scl != MOU)
lerror("bldstr() 3");
/*
* Remember if the left operand is an lvalue (structure members
* are lvalues if and only if the structure itself is an lvalue).
*/
nolval = op == POINT && !ln->tn_lvalue;
if (op == POINT) {
ln = bldamper(ln, 1);
} else if (ln->tn_type->t_tspec != PTR) {
if (!tflag || !isityp(ln->tn_type->t_tspec))
lerror("bldstr() 4");
ln = convert(NOOP, NULL, tincref(gettyp(VOID), PTR), ln);
}
#if PTRDIFF_IS_LONG
ctn = getinode(LONG, rn->tn_sym->s_value.v_quad / CHAR_BIT);
#else
ctn = getinode(INT, rn->tn_sym->s_value.v_quad / CHAR_BIT);
#endif
ntn = mktnode(PLUS, tincref(rn->tn_type, PTR), ln, ctn);
if (ln->tn_op == CON)
ntn = fold(ntn);
if (rn->tn_type->t_isfield) {
ntn = mktnode(FSEL, ntn->tn_type->t_subt, ntn, NULL);
} else {
ntn = mktnode(STAR, ntn->tn_type->t_subt, ntn, NULL);
}
if (nolval)
ntn->tn_lvalue = 0;
return (ntn);
}
/*
* Create a node for INCAFT, INCBEF, DECAFT and DECBEF.
*/
static tnode_t *
bldincdec(op_t op, tnode_t *ln)
{
tnode_t *cn, *ntn;
if (ln == NULL)
lerror("bldincdec() 1");
if (ln->tn_type->t_tspec == PTR) {
cn = plength(ln->tn_type);
} else {
cn = getinode(INT, (quad_t)1);
}
ntn = mktnode(op, ln->tn_type, ln, cn);
return (ntn);
}
/*
* Create a tree node for the & operator
*/
static tnode_t *
bldamper(tnode_t *tn, int noign)
{
tnode_t *ntn;
tspec_t t;
if (!noign && ((t = tn->tn_type->t_tspec) == ARRAY || t == FUNC)) {
/* & before array or function: ignored */
if (tflag)
warning(127);
return (tn);
}
/* eliminate &* */
if (tn->tn_op == STAR &&
tn->tn_left->tn_type->t_tspec == PTR &&
tn->tn_left->tn_type->t_subt == tn->tn_type) {
return (tn->tn_left);
}
ntn = mktnode(AMPER, tincref(tn->tn_type, PTR), tn, NULL);
return (ntn);
}
/*
* Create a node for operators PLUS and MINUS.
*/
static tnode_t *
bldplmi(op_t op, tnode_t *ln, tnode_t *rn)
{
tnode_t *ntn, *ctn;
type_t *tp;
/* If pointer and integer, then pointer to the lhs. */
if (rn->tn_type->t_tspec == PTR && isityp(ln->tn_type->t_tspec)) {
ntn = ln;
ln = rn;
rn = ntn;
}
if (ln->tn_type->t_tspec == PTR && rn->tn_type->t_tspec != PTR) {
if (!isityp(rn->tn_type->t_tspec))
lerror("bldplmi() 1");
ctn = plength(ln->tn_type);
if (rn->tn_type->t_tspec != ctn->tn_type->t_tspec)
rn = convert(NOOP, NULL, ctn->tn_type, rn);
rn = mktnode(MULT, rn->tn_type, rn, ctn);
if (rn->tn_left->tn_op == CON)
rn = fold(rn);
ntn = mktnode(op, ln->tn_type, ln, rn);
} else if (rn->tn_type->t_tspec == PTR) {
if (ln->tn_type->t_tspec != PTR || op != MINUS)
lerror("bldplmi() 2");
#if PTRDIFF_IS_LONG
tp = gettyp(LONG);
#else
tp = gettyp(INT);
#endif
ntn = mktnode(op, tp, ln, rn);
if (ln->tn_op == CON && rn->tn_op == CON)
ntn = fold(ntn);
ctn = plength(ln->tn_type);
balance(NOOP, &ntn, &ctn);
ntn = mktnode(DIV, tp, ntn, ctn);
} else {
ntn = mktnode(op, ln->tn_type, ln, rn);
}
return (ntn);
}
/*
* Create a node for operators SHL and SHR.
*/
static tnode_t *
bldshft(op_t op, tnode_t *ln, tnode_t *rn)
{
tspec_t t;
tnode_t *ntn;
if ((t = rn->tn_type->t_tspec) != INT && t != UINT)
rn = convert(CVT, NULL, gettyp(INT), rn);
ntn = mktnode(op, ln->tn_type, ln, rn);
return (ntn);
}
/*
* Create a node for COLON.
*/
static tnode_t *
bldcol(tnode_t *ln, tnode_t *rn)
{
tspec_t lt, rt, pdt;
type_t *rtp;
tnode_t *ntn;
lt = ln->tn_type->t_tspec;
rt = rn->tn_type->t_tspec;
#if PTRDIFF_IS_LONG
pdt = LONG;
#else
pdt = INT;
#endif
/*
* Arithmetic types are balanced, all other type combinations
* still need to be handled.
*/
if (isatyp(lt) && isatyp(rt)) {
rtp = ln->tn_type;
} else if (lt == VOID || rt == VOID) {
rtp = gettyp(VOID);
} else if (lt == STRUCT || lt == UNION) {
/* Both types must be identical. */
if (rt != STRUCT && rt != UNION)
lerror("bldcol() 1");
if (ln->tn_type->t_str != rn->tn_type->t_str)
lerror("bldcol() 2");
if (incompl(ln->tn_type)) {
/* unknown operand size, op %s */
error(138, modtab[COLON].m_name);
return (NULL);
}
rtp = ln->tn_type;
} else if (lt == PTR && isityp(rt)) {
if (rt != pdt) {
rn = convert(NOOP, NULL, gettyp(pdt), rn);
rt = pdt;
}
rtp = ln->tn_type;
} else if (rt == PTR && isityp(lt)) {
if (lt != pdt) {
ln = convert(NOOP, NULL, gettyp(pdt), ln);
lt = pdt;
}
rtp = rn->tn_type;
} else if (lt == PTR && ln->tn_type->t_subt->t_tspec == VOID) {
if (rt != PTR)
lerror("bldcol() 4");
rtp = ln->tn_type;
mrgqual(&rtp, ln->tn_type, rn->tn_type);
} else if (rt == PTR && rn->tn_type->t_subt->t_tspec == VOID) {
if (lt != PTR)
lerror("bldcol() 5");
rtp = rn->tn_type;
mrgqual(&rtp, ln->tn_type, rn->tn_type);
} else {
if (lt != PTR || rt != PTR)
lerror("bldcol() 6");
/*
* XXX For now we simply take the left type. This is
* probably wrong, if one type contains a function prototype
* and the other one, at the same place, only an old style
* declaration.
*/
rtp = ln->tn_type;
mrgqual(&rtp, ln->tn_type, rn->tn_type);
}
ntn = mktnode(COLON, rtp, ln, rn);
return (ntn);
}
/*
* Create a node for an assignment operator (both = and op= ).
*/
static tnode_t *
bldasgn(op_t op, tnode_t *ln, tnode_t *rn)
{
tspec_t lt, rt;
tnode_t *ntn, *ctn;
if (ln == NULL || rn == NULL)
lerror("bldasgn() 1");
lt = ln->tn_type->t_tspec;
rt = rn->tn_type->t_tspec;
if ((op == ADDASS || op == SUBASS) && lt == PTR) {
if (!isityp(rt))
lerror("bldasgn() 2");
ctn = plength(ln->tn_type);
if (rn->tn_type->t_tspec != ctn->tn_type->t_tspec)
rn = convert(NOOP, NULL, ctn->tn_type, rn);
rn = mktnode(MULT, rn->tn_type, rn, ctn);
if (rn->tn_left->tn_op == CON)
rn = fold(rn);
}
if ((op == ASSIGN || op == RETURN) && (lt == STRUCT || rt == STRUCT)) {
if (rt != lt || ln->tn_type->t_str != rn->tn_type->t_str)
lerror("bldasgn() 3");
if (incompl(ln->tn_type)) {
if (op == RETURN) {
/* cannot return incomplete type */
error(212);
} else {
/* unknown operand size, op %s */
error(138, modtab[op].m_name);
}
return (NULL);
}
}
if (op == SHLASS || op == SHRASS) {
if (rt != INT) {
rn = convert(NOOP, NULL, gettyp(INT), rn);
rt = INT;
}
} else {
if (op == ASSIGN || lt != PTR) {
if (lt != rt ||
(ln->tn_type->t_isfield && rn->tn_op == CON)) {
rn = convert(op, NULL, ln->tn_type, rn);
rt = lt;
}
}
}
ntn = mktnode(op, ln->tn_type, ln, rn);
return (ntn);
}
/*
* Check for division by zero. Returns 1 if a division by zero is
* present, 0 otherwise.
*/
static int
chkdbz(op_t op, tnode_t *rn)
{
int code;
switch (op) {
case DIV:
case DIVASS:
code = 139; /* division by 0 */
break;
case MOD:
case MODASS:
code = 140; /* modular division by 0 */
break;
default:
return 0;
}
/* no way of checking unless right side is a constant */
if (rn->tn_op != CON)
return 0;
if (isftyp(rn->tn_type->t_tspec)) {
if (rn->tn_val->v_ldbl == 0.0) {
/* division by 0 */
warning(code);
return 1;
}
} else {
if (rn->tn_val->v_quad == 0) {
/* division by 0 */
warning(code);
return 1;
}
}
return 0;
}
/*
* Get length of type tp->t_subt.
*/
static tnode_t *
plength(type_t *tp)
{
int elem, elsz;
tspec_t st;
if (tp->t_tspec != PTR)
lerror("plength() 1");
tp = tp->t_subt;
elem = 1;
elsz = 0;
while (tp->t_tspec == ARRAY) {
elem *= tp->t_dim;
tp = tp->t_subt;
}
switch (tp->t_tspec) {
case FUNC:
/* pointer to function is not allowed here */
error(110);
break;
case VOID:
/* cannot do pointer arithmetic on operand of ... */
(void)gnuism(136);
break;
case STRUCT:
case UNION:
if ((elsz = tp->t_str->size) == 0)
/* cannot do pointer arithmetic on operand of ... */
error(136);
break;
case ENUM:
if (incompl(tp)) {
/* cannot do pointer arithmetic on operand of ... */
warning(136);
}
/* FALLTHROUGH */
default:
if ((elsz = size(tp->t_tspec)) == 0) {
/* cannot do pointer arithmetic on operand of ... */
error(136);
} else if (elsz == -1) {
lerror("plength() 2");
}
break;
}
if (elem == 0 && elsz != 0) {
/* cannot do pointer arithmetic on operand of ... */
error(136);
}
if (elsz == 0)
elsz = CHAR_BIT;
#if PTRDIFF_IS_LONG
st = LONG;
#else
st = INT;
#endif
return (getinode(st, (quad_t)(elem * elsz / CHAR_BIT)));
}
/*
* Do only as much as necessary to compute constant expressions.
* Called only if the operator allows folding and (both) operands
* are constants.
*/
static tnode_t *
fold(tnode_t *tn)
{
val_t *v;
tspec_t t;
int utyp, ovfl;
quad_t sl, sr, q, mask;
u_quad_t ul, ur;
tnode_t *cn;
v = xcalloc(1, sizeof (val_t));
v->v_tspec = t = tn->tn_type->t_tspec;
utyp = t == PTR || isutyp(t);
ul = sl = tn->tn_left->tn_val->v_quad;
if (modtab[tn->tn_op].m_binary)
ur = sr = tn->tn_right->tn_val->v_quad;
ovfl = 0;
switch (tn->tn_op) {
case UPLUS:
q = sl;
break;
case UMINUS:
q = -sl;
if (msb(q, t, -1) == msb(sl, t, -1))
ovfl = 1;
break;
case COMPL:
q = ~sl;
break;
case MULT:
q = utyp ? ul * ur : sl * sr;
if (msb(q, t, -1) != (msb(sl, t, -1) ^ msb(sr, t, -1)))
ovfl = 1;
break;
case DIV:
if (chkdbz(tn->tn_op, tn->tn_right)) {
q = utyp ? 0U: 0;
} else {
q = utyp ? ul / ur : sl / sr;
}
break;
case MOD:
if (chkdbz(tn->tn_op, tn->tn_right)) {
q = 0;
} else {
q = utyp ? ul % ur : sl % sr;
}
break;
case PLUS:
q = utyp ? ul + ur : sl + sr;
if (msb(sl, t, -1) != 0 && msb(sr, t, -1) != 0) {
if (msb(q, t, -1) == 0)
ovfl = 1;
} else if (msb(sl, t, -1) == 0 && msb(sr, t, -1) == 0) {
if (msb(q, t, -1) != 0)
ovfl = 1;
}
break;
case MINUS:
q = utyp ? ul - ur : sl - sr;
if (msb(sl, t, -1) != 0 && msb(sr, t, -1) == 0) {
if (msb(q, t, -1) == 0)
ovfl = 1;
} else if (msb(sl, t, -1) == 0 && msb(sr, t, -1) != 0) {
if (msb(q, t, -1) != 0)
ovfl = 1;
}
break;
case SHL:
q = utyp ? ul << sr : sl << sr;
break;
case SHR:
/*
* The sign must be explicitly extended because
* shifts of signed values are implementation dependent.
*/
q = ul >> sr;
q = xsign(q, t, size(t) - (int)sr);
break;
case LT:
q = utyp ? ul < ur : sl < sr;
break;
case LE:
q = utyp ? ul <= ur : sl <= sr;
break;
case GE:
q = utyp ? ul >= ur : sl >= sr;
break;
case GT:
q = utyp ? ul > ur : sl > sr;
break;
case EQ:
q = utyp ? ul == ur : sl == sr;
break;
case NE:
q = utyp ? ul != ur : sl != sr;
break;
case AND:
q = utyp ? ul & ur : sl & sr;
break;
case XOR:
q = utyp ? ul ^ ur : sl ^ sr;
break;
case OR:
q = utyp ? ul | ur : sl | sr;
break;
default:
lerror("fold() 5");
}
mask = qlmasks[size(t)];
/* XXX does not work for quads. */
if (ovfl || ((q | mask) != ~(u_quad_t)0 && (q & ~mask) != 0)) {
if (hflag)
/* integer overflow detected, op %s */
warning(141, modtab[tn->tn_op].m_name);
}
v->v_quad = xsign(q, t, -1);
cn = getcnode(tn->tn_type, v);
return (cn);
}
/*
* Same for operators whose operands are compared with 0 (test context).
*/
static tnode_t *
foldtst(tnode_t *tn)
{
int l, r;
val_t *v;
v = xcalloc(1, sizeof (val_t));
v->v_tspec = tn->tn_type->t_tspec;
if (tn->tn_type->t_tspec != INT)
lerror("foldtst() 1");
if (isftyp(tn->tn_left->tn_type->t_tspec)) {
l = tn->tn_left->tn_val->v_ldbl != 0.0;
} else {
l = tn->tn_left->tn_val->v_quad != 0;
}
if (modtab[tn->tn_op].m_binary) {
if (isftyp(tn->tn_right->tn_type->t_tspec)) {
r = tn->tn_right->tn_val->v_ldbl != 0.0;
} else {
r = tn->tn_right->tn_val->v_quad != 0;
}
}
switch (tn->tn_op) {
case NOT:
if (hflag)
/* constant argument to NOT */
warning(239);
v->v_quad = !l;
break;
case LOGAND:
v->v_quad = l && r;
break;
case LOGOR:
v->v_quad = l || r;
break;
default:
lerror("foldtst() 1");
}
return (getcnode(tn->tn_type, v));
}
/*
* Same for operands with floating point type.
*/
static tnode_t *
foldflt(tnode_t *tn)
{
val_t *v;
tspec_t t;
ldbl_t l, r;
v = xcalloc(1, sizeof (val_t));
v->v_tspec = t = tn->tn_type->t_tspec;
if (!isftyp(t))
lerror("foldflt() 1");
if (t != tn->tn_left->tn_type->t_tspec)
lerror("foldflt() 2");
if (modtab[tn->tn_op].m_binary && t != tn->tn_right->tn_type->t_tspec)
lerror("foldflt() 3");
l = tn->tn_left->tn_val->v_ldbl;
if (modtab[tn->tn_op].m_binary)
r = tn->tn_right->tn_val->v_ldbl;
switch (tn->tn_op) {
case UPLUS:
v->v_ldbl = l;
break;
case UMINUS:
v->v_ldbl = -l;
break;
case MULT:
v->v_ldbl = l * r;
break;
case DIV:
if (chkdbz(tn->tn_op, tn->tn_right)) {
if (t == FLOAT) {
v->v_ldbl = l < 0 ? -FLT_MAX : FLT_MAX;
} else if (t == DOUBLE) {
v->v_ldbl = l < 0 ? -DBL_MAX : DBL_MAX;
} else {
v->v_ldbl = l < 0 ? -LDBL_MAX : LDBL_MAX;
}
} else {
v->v_ldbl = l / r;
}
break;
case PLUS:
v->v_ldbl = l + r;
break;
case MINUS:
v->v_ldbl = l - r;
break;
case LT:
v->v_quad = l < r;
break;
case LE:
v->v_quad = l <= r;
break;
case GE:
v->v_quad = l >= r;
break;
case GT:
v->v_quad = l > r;
break;
case EQ:
v->v_quad = l == r;
break;
case NE:
v->v_quad = l != r;
break;
default:
lerror("foldflt() 4");
}
if (isnan((double)v->v_ldbl))
lerror("foldflt() 5");
if (isinf((double)v->v_ldbl) ||
(t == FLOAT &&
(v->v_ldbl > FLT_MAX || v->v_ldbl < -FLT_MAX)) ||
(t == DOUBLE &&
(v->v_ldbl > DBL_MAX || v->v_ldbl < -DBL_MAX))) {
/* floating point overflow detected, op %s */
warning(142, modtab[tn->tn_op].m_name);
if (t == FLOAT) {
v->v_ldbl = v->v_ldbl < 0 ? -FLT_MAX : FLT_MAX;
} else if (t == DOUBLE) {
v->v_ldbl = v->v_ldbl < 0 ? -DBL_MAX : DBL_MAX;
} else {
v->v_ldbl = v->v_ldbl < 0 ? -LDBL_MAX: LDBL_MAX;
}
}
return (getcnode(tn->tn_type, v));
}
/*
* Create a constant node for sizeof(term).
*/
tnode_t *
bldszoftrm(tnode_t *tn)
{
switch (tn->tn_op) {
case POINT:
case STAR:
case NAME:
case STRING:
break;
default:
warning(312, modtab[tn->tn_op].m_name);
}
return bldszof(tn->tn_type);
}
/*
* Create a constant node for sizeof.
*/
tnode_t *
bldszof(type_t *tp)
{
int elem, elsz;
tspec_t st;
elem = 1;
while (tp->t_tspec == ARRAY) {
elem *= tp->t_dim;
tp = tp->t_subt;
}
if (elem == 0) {
/* cannot take size of incomplete type */
error(143);
elem = 1;
}
switch (tp->t_tspec) {
case FUNC:
/* cannot take size of function */
error(144);
elsz = 1;
break;
case STRUCT:
case UNION:
if (incompl(tp)) {
/* cannot take size of incomplete type */
error(143);
elsz = 1;
} else {
elsz = tp->t_str->size;
}
break;
case ENUM:
if (incompl(tp)) {
/* cannot take size of incomplete type */
warning(143);
}
/* FALLTHROUGH */
default:
if (tp->t_isfield) {
/* cannot take size of bit-field */
error(145);
}
if (tp->t_tspec == VOID) {
/* cannot take size of void */
error(146);
elsz = 1;
} else {
elsz = size(tp->t_tspec);
if (elsz <= 0)
lerror("bldszof() 1");
}
break;
}
#if SIZEOF_IS_ULONG
st = ULONG;
#else
st = UINT;
#endif
return (getinode(st, (quad_t)(elem * elsz / CHAR_BIT)));
}
/*
* Type casts.
*/
tnode_t *
cast(tnode_t *tn, type_t *tp)
{
tspec_t nt, ot;
if (tn == NULL)
return (NULL);
tn = cconv(tn);
nt = tp->t_tspec;
ot = tn->tn_type->t_tspec;
if (nt == VOID) {
/*
* XXX ANSI C requires scalar types or void (Plauger&Brodie).
* But this seams really questionable.
*/
} else if (nt == STRUCT || nt == UNION || nt == ARRAY || nt == FUNC) {
/* invalid cast expression */
error(147);
return (NULL);
} else if (ot == STRUCT || ot == UNION) {
/* invalid cast expression */
error(147);
return (NULL);
} else if (ot == VOID) {
/* improper cast of void expression */
error(148);
return (NULL);
} else if (isityp(nt) && issclt(ot)) {
/* ok */
} else if (isftyp(nt) && isatyp(ot)) {
/* ok */
} else if (nt == PTR && isityp(ot)) {
/* ok */
} else if (nt == PTR && ot == PTR) {
if (!tp->t_subt->t_const && tn->tn_type->t_subt->t_const) {
if (hflag)
/* cast discards 'const' from ... */
warning(275);
}
} else {
/* invalid cast expression */
error(147);
return (NULL);
}
tn = convert(CVT, NULL, tp, tn);
tn->tn_cast = 1;
return (tn);
}
/*
* Create the node for a function argument.
* All necessary conversions and type checks are done in funccall(), because
* in funcarg() we have no information about expected argument types.
*/
tnode_t *
funcarg(tnode_t *args, tnode_t *arg)
{
tnode_t *ntn;
/*
* If there was a serious error in the expression for the argument,
* create a dummy argument so the positions of the remaining arguments
* will not change.
*/
if (arg == NULL)
arg = getinode(INT, (quad_t)0);
ntn = mktnode(PUSH, arg->tn_type, arg, args);
return (ntn);
}
/*
* Create the node for a function call. Also check types of
* function arguments and insert conversions, if necessary.
*/
tnode_t *
funccall(tnode_t *func, tnode_t *args)
{
tnode_t *ntn;
op_t fcop;
if (func == NULL)
return (NULL);
if (func->tn_op == NAME && func->tn_type->t_tspec == FUNC) {
fcop = CALL;
} else {
fcop = ICALL;
}
if (func->tn_sym->s_noreturn)
notreach(0);
/*
* after cconv() func will always be a pointer to a function
* if it is a valid function designator.
*/
func = cconv(func);
if (func->tn_type->t_tspec != PTR ||
func->tn_type->t_subt->t_tspec != FUNC) {
/* illegal function */
error(149);
return (NULL);
}
args = chkfarg(func, args);
ntn = mktnode(fcop, func->tn_type->t_subt->t_subt, func, args);
return (ntn);
}
/*
* Check types of all function arguments and insert conversions,
* if necessary.
*
* func: called function
* args: arguments to function
*/
static tnode_t *
chkfarg(tnode_t *func, tnode_t *args)
{
type_t *ftp;
tnode_t *arg;
sym_t *asym;
farg_t farg;
tspec_t at;
int narg, npar, n, i;
ftp = func->tn_type->t_subt;
/* get # of args in the prototype */
npar = 0;
for (asym = ftp->t_args; asym != NULL; asym = asym->s_nxt)
npar++;
/* get # of args in function call */
narg = 0;
for (arg = args; arg != NULL; arg = arg->tn_right)
narg++;
asym = ftp->t_args;
if (ftp->t_proto && npar != narg && !(ftp->t_vararg && npar < narg)) {
/* argument mismatch: %d arg%s passed, %d expected */
error(150, narg, narg > 1 ? "s" : "", npar);
asym = NULL;
}
for (n = 1; n <= narg; n++) {
/*
* The rightmost argument is at the top of the argument
* subtree.
*/
for (i = narg, arg = args; i > n; i--, arg = arg->tn_right) ;
/* some things which are never allowed */
if ((at = arg->tn_left->tn_type->t_tspec) == VOID) {
/* void expressions may not be arguments, arg #%d */
error(151, n);
return (NULL);
} else if ((at == STRUCT || at == UNION) &&
incompl(arg->tn_left->tn_type)) {
/* argument cannot have unknown size, arg #%d */
error(152, n);
return (NULL);
} else if (isityp(at) && arg->tn_left->tn_type->t_isenum &&
incompl(arg->tn_left->tn_type)) {
/* argument cannot have unknown size, arg #%d */
warning(152, n);
}
/* class conversions (arg in value context) */
arg->tn_left = cconv(arg->tn_left);
farg.fa_num = n;
farg.fa_sym = asym;
farg.fa_func = func;
if (asym != NULL) {
arg->tn_left = parg(&farg, arg->tn_left);
} else {
arg->tn_left = promote(NOOP, 1, arg->tn_left);
}
arg->tn_type = arg->tn_left->tn_type;
if (asym != NULL)
asym = asym->s_nxt;
}
return (args);
}
/*
* Compare the type of an argument with the corresponding type of a
* prototype parameter. If it is a valid combination, but both types
* are not the same, insert a conversion to convert the argument into
* the type of the parameter.
*
* n: position of arg
* tp: expected type (from prototype)
* tn: argument
*/
static tnode_t *
parg(farg_t *farg, tnode_t *tn)
{
tnode_t *ln;
type_t *tp;
int warn, n;
tp = farg->fa_sym->s_type;
n = farg->fa_num;
ln = xcalloc(1, sizeof (tnode_t));
ln->tn_type = tduptyp(tp);
ln->tn_type->t_const = 0;
ln->tn_lvalue = 1;
if (typeok(FARG, farg, ln, tn)) {
if (!eqtype(tp, tn->tn_type, 1, 0, (warn = 0, &warn)) || warn)
tn = convert(FARG, farg, tp, tn);
}
free(ln);
return (tn);
}
/*
* Return the value of an integral constant expression.
* If the expression is not constant or its type is not an integer
* type, an error message is printed.
*/
val_t *
constant(tnode_t *tn)
{
val_t *v;
if (tn != NULL)
tn = cconv(tn);
if (tn != NULL)
tn = promote(NOOP, 0, tn);
v = xcalloc(1, sizeof (val_t));
if (tn == NULL) {
if (nerr == 0)
lerror("constant() 1");
v->v_tspec = INT;
v->v_quad = 1;
return (v);
}
v->v_tspec = tn->tn_type->t_tspec;
if (tn->tn_op == CON) {
if (tn->tn_type->t_tspec != tn->tn_val->v_tspec)
lerror("constant() 2");
if (isityp(tn->tn_val->v_tspec)) {
v->v_ansiu = tn->tn_val->v_ansiu;
v->v_quad = tn->tn_val->v_quad;
return (v);
}
v->v_quad = tn->tn_val->v_ldbl;
} else {
v->v_quad = 1;
}
/* integral constant expression expected */
error(55);
if (!isityp(v->v_tspec))
v->v_tspec = INT;
return (v);
}
/*
* Perform some tests on expressions which can't be done in build() and
* functions called by build(). These tests must be done here because
* we need some information about the context in which the operations
* are performed.
* After all tests are performed, expr() frees the memory which is used
* for the expression.
*/
void
expr(tnode_t *tn, int vctx, int tctx)
{
if (tn == NULL && nerr == 0)
lerror("expr() 1");
if (tn == NULL) {
tfreeblk();
return;
}
/* expr() is also called in global initialisations */
if (dcs->d_ctx != EXTERN)
chkreach();
chkmisc(tn, vctx, tctx, !tctx, 0, 0, 0);
if (tn->tn_op == ASSIGN) {
if (hflag && tctx && !tn->tn_parn)
/* assignment in conditional context */
warning(159);
} else if (tn->tn_op == CON) {
if (hflag && tctx && !ccflg) {
if (!isityp(tn->tn_type->t_tspec) ||
(tn->tn_val->v_quad != 0 &&
tn->tn_val->v_quad != 1)) {
/* constant in conditional context */
warning(161);
}
}
}
if (!modtab[tn->tn_op].m_sideeff) {
/*
* for left operands of COMMA this warning is already
* printed
*/
if (tn->tn_op != COMMA && !vctx && !tctx)
nulleff(tn);
}
if (dflag)
displexpr(tn, 0);
/* free the tree memory */
tfreeblk();
}
static void
nulleff(tnode_t *tn)
{
if (!hflag)
return;
if (nulleffexpr(tn)) {
/* expression has null effect */
warning(129);
}
}
static int
nulleffexpr(tnode_t *tn)
{
int r1, r2;
if (modtab[tn->tn_op].m_sideeff)
return (0);
switch (tn->tn_op) {
case CVT:
if (tn->tn_type->t_tspec == VOID)
return (0);
else
return (nulleffexpr(tn->tn_left));
case LOGAND:
case LOGOR:
/*
* && and || have a null effect if the right operand
* has null effect.
*/
return (nulleffexpr(tn->tn_right));
case QUEST:
/*
* ? has null effect if both of its right * operands
* have null effect
*/
return (nulleffexpr(tn->tn_right));
case COLON:
case COMMA:
/*
* : and , have null effect if both of the operands have
* null effect
*/
r1 = nulleffexpr(tn->tn_left);
r2 = nulleffexpr(tn->tn_right);
return (r1 && r2);
default:
return (1);
}
}
/*
* Dump an expression to stdout
* only used for debugging
*/
void
displexpr(tnode_t *tn, int offs)
{
u_quad_t uq;
if (tn == NULL) {
(void)printf("%*s%s\n", offs, "", "NULL");
return;
}
(void)printf("%*sop %s ", offs, "", modtab[tn->tn_op].m_name);
if (tn->tn_op == NAME) {
(void)printf("%s: %s ",
tn->tn_sym->s_name, scltoa(tn->tn_sym->s_scl));
} else if (tn->tn_op == CON && isftyp(tn->tn_type->t_tspec)) {
(void)printf("%#g ", (double)tn->tn_val->v_ldbl);
} else if (tn->tn_op == CON && isityp(tn->tn_type->t_tspec)) {
uq = tn->tn_val->v_quad;
(void)printf("0x %08lx %08lx ", (long)(uq >> 32) & 0xffffffffl,
(long)uq & 0xffffffffl);
} else if (tn->tn_op == CON) {
if (tn->tn_type->t_tspec != PTR)
lerror("displexpr() 1");
(void)printf("0x%0*lx ", (int)(sizeof (void *) * CHAR_BIT / 4),
(u_long)tn->tn_val->v_quad);
} else if (tn->tn_op == STRING) {
if (tn->tn_strg->st_tspec == CHAR) {
(void)printf("\"%s\"", tn->tn_strg->st_cp);
} else {
char *s;
size_t n;
n = MB_CUR_MAX * (tn->tn_strg->st_len + 1);
s = xmalloc(n);
(void)wcstombs(s, tn->tn_strg->st_wcp, n);
(void)printf("L\"%s\"", s);
free(s);
}
(void)printf(" ");
} else if (tn->tn_op == FSEL) {
(void)printf("o=%d, l=%d ", tn->tn_type->t_foffs,
tn->tn_type->t_flen);
}
(void)printf("%s\n", ttos(tn->tn_type));
if (tn->tn_op == NAME || tn->tn_op == CON || tn->tn_op == STRING)
return;
displexpr(tn->tn_left, offs + 2);
if (modtab[tn->tn_op].m_binary ||
(tn->tn_op == PUSH && tn->tn_right != NULL)) {
displexpr(tn->tn_right, offs + 2);
}
}
/*
* Called by expr() to recursively perform some tests.
*/
/* ARGSUSED */
void
chkmisc(tnode_t *tn, int vctx, int tctx, int eqwarn, int fcall, int rvdisc,
int szof)
{
tnode_t *ln, *rn;
mod_t *mp;
int nrvdisc, cvctx, ctctx;
op_t op;
scl_t sc;
dinfo_t *di;
if (tn == NULL)
return;
ln = tn->tn_left;
rn = tn->tn_right;
mp = &modtab[op = tn->tn_op];
switch (op) {
case AMPER:
if (ln->tn_op == NAME && (reached || rchflg)) {
if (!szof)
setsflg(ln->tn_sym);
setuflg(ln->tn_sym, fcall, szof);
}
if (ln->tn_op == STAR && ln->tn_left->tn_op == PLUS)
/* check the range of array indices */
chkaidx(ln->tn_left, 1);
break;
case LOAD:
if (ln->tn_op == STAR && ln->tn_left->tn_op == PLUS)
/* check the range of array indices */
chkaidx(ln->tn_left, 0);
/* FALLTHROUGH */
case PUSH:
case INCBEF:
case DECBEF:
case INCAFT:
case DECAFT:
case ADDASS:
case SUBASS:
case MULASS:
case DIVASS:
case MODASS:
case ANDASS:
case ORASS:
case XORASS:
case SHLASS:
case SHRASS:
if (ln->tn_op == NAME && (reached || rchflg)) {
sc = ln->tn_sym->s_scl;
/*
* Look if there was an asm statement in one of the
* compound statements we are in. If not, we don't
* print a warning.
*/
for (di = dcs; di != NULL; di = di->d_nxt) {
if (di->d_asm)
break;
}
if (sc != EXTERN && sc != STATIC &&
!ln->tn_sym->s_set && !szof && di == NULL) {
/* %s may be used before set */
warning(158, ln->tn_sym->s_name);
setsflg(ln->tn_sym);
}
setuflg(ln->tn_sym, 0, 0);
}
break;
case ASSIGN:
if (ln->tn_op == NAME && !szof && (reached || rchflg)) {
setsflg(ln->tn_sym);
if (ln->tn_sym->s_scl == EXTERN)
outusg(ln->tn_sym);
}
if (ln->tn_op == STAR && ln->tn_left->tn_op == PLUS)
/* check the range of array indices */
chkaidx(ln->tn_left, 0);
break;
case CALL:
if (ln->tn_op != AMPER || ln->tn_left->tn_op != NAME)
lerror("chkmisc() 1");
if (!szof)
outcall(tn, vctx || tctx, rvdisc);
break;
case EQ:
/* equality operator "==" found where "=" was exp. */
if (hflag && eqwarn)
warning(160);
break;
case CON:
case NAME:
case STRING:
return;
/* LINTED (enumeration values not handled in switch) */
}
cvctx = mp->m_vctx;
ctctx = mp->m_tctx;
/*
* values of operands of ':' are not used if the type of at least
* one of the operands (for gcc compatibility) is void
* XXX test/value context of QUEST should probably be used as
* context for both operands of COLON
*/
if (op == COLON && tn->tn_type->t_tspec == VOID)
cvctx = ctctx = 0;
nrvdisc = op == CVT && tn->tn_type->t_tspec == VOID;
chkmisc(ln, cvctx, ctctx, mp->m_eqwarn, op == CALL, nrvdisc, szof);
switch (op) {
case PUSH:
if (rn != NULL)
chkmisc(rn, 0, 0, mp->m_eqwarn, 0, 0, szof);
break;
case LOGAND:
case LOGOR:
chkmisc(rn, 0, 1, mp->m_eqwarn, 0, 0, szof);
break;
case COLON:
chkmisc(rn, cvctx, ctctx, mp->m_eqwarn, 0, 0, szof);
break;
default:
if (mp->m_binary)
chkmisc(rn, 1, 0, mp->m_eqwarn, 0, 0, szof);
break;
}
}
/*
* Checks the range of array indices, if possible.
* amper is set if only the address of the element is used. This
* means that the index is allowd to refer to the first element
* after the array.
*/
static void
chkaidx(tnode_t *tn, int amper)
{
int dim;
tnode_t *ln, *rn;
int elsz;
quad_t con;
ln = tn->tn_left;
rn = tn->tn_right;
/* We can only check constant indices. */
if (rn->tn_op != CON)
return;
/* Return if the left node does not stem from an array. */
if (ln->tn_op != AMPER)
return;
if (ln->tn_left->tn_op != STRING && ln->tn_left->tn_op != NAME)
return;
if (ln->tn_left->tn_type->t_tspec != ARRAY)
return;
/*
* For incomplete array types, we can print a warning only if
* the index is negative.
*/
if (incompl(ln->tn_left->tn_type) && rn->tn_val->v_quad >= 0)
return;
/* Get the size of one array element */
if ((elsz = length(ln->tn_type->t_subt, NULL)) == 0)
return;
elsz /= CHAR_BIT;
/* Change the unit of the index from bytes to element size. */
if (isutyp(rn->tn_type->t_tspec)) {
con = (u_quad_t)rn->tn_val->v_quad / elsz;
} else {
con = rn->tn_val->v_quad / elsz;
}
dim = ln->tn_left->tn_type->t_dim + (amper ? 1 : 0);
if (!isutyp(rn->tn_type->t_tspec) && con < 0) {
/* array subscript cannot be negative: %ld */
warning(167, (long)con);
} else if (dim > 0 && (u_quad_t)con >= dim) {
/* array subscript cannot be > %d: %ld */
warning(168, dim - 1, (long)con);
}
}
/*
* Check for ordered comparisons of unsigned values with 0.
*/
static void
chkcomp(op_t op, tnode_t *ln, tnode_t *rn)
{
tspec_t lt, rt;
mod_t *mp;
lt = ln->tn_type->t_tspec;
rt = rn->tn_type->t_tspec;
mp = &modtab[op];
if (ln->tn_op != CON && rn->tn_op != CON)
return;
if (!isityp(lt) || !isityp(rt))
return;
if ((hflag || pflag) && lt == CHAR && rn->tn_op == CON &&
(rn->tn_val->v_quad < 0 ||
rn->tn_val->v_quad > ~(~0 << (CHAR_BIT - 1)))) {
/* nonportable character comparison, op %s */
warning(230, mp->m_name);
return;
}
if ((hflag || pflag) && rt == CHAR && ln->tn_op == CON &&
(ln->tn_val->v_quad < 0 ||
ln->tn_val->v_quad > ~(~0 << (CHAR_BIT - 1)))) {
/* nonportable character comparison, op %s */
warning(230, mp->m_name);
return;
}
if (isutyp(lt) && !isutyp(rt) &&
rn->tn_op == CON && rn->tn_val->v_quad <= 0) {
if (rn->tn_val->v_quad < 0) {
/* comparison of %s with %s, op %s */
warning(162, tyname(ln->tn_type), "negative constant",
mp->m_name);
} else if (op == LT || op == GE || (hflag && op == LE)) {
/* comparison of %s with %s, op %s */
warning(162, tyname(ln->tn_type), "0", mp->m_name);
}
return;
}
if (isutyp(rt) && !isutyp(lt) &&
ln->tn_op == CON && ln->tn_val->v_quad <= 0) {
if (ln->tn_val->v_quad < 0) {
/* comparison of %s with %s, op %s */
warning(162, "negative constant", tyname(rn->tn_type),
mp->m_name);
} else if (op == GT || op == LE || (hflag && op == GE)) {
/* comparison of %s with %s, op %s */
warning(162, "0", tyname(rn->tn_type), mp->m_name);
}
return;
}
}
/*
* Takes an expression and returns 0 if this expression can be used
* for static initialisation, otherwise -1.
*
* Constant initialisation expressions must be costant or an address
* of a static object with an optional offset. In the first case,
* the result is returned in *offsp. In the second case, the static
* object is returned in *symp and the offset in *offsp.
*
* The expression can consist of PLUS, MINUS, AMPER, NAME, STRING and
* CON. Type conversions are allowed if they do not change binary
* representation (including width).
*/
int
conaddr(tnode_t *tn, sym_t **symp, ptrdiff_t *offsp)
{
sym_t *sym;
ptrdiff_t offs1, offs2;
tspec_t t, ot;
switch (tn->tn_op) {
case MINUS:
if (tn->tn_right->tn_op != CON)
return (-1);
/* FALLTHROUGH */
case PLUS:
offs1 = offs2 = 0;
if (tn->tn_left->tn_op == CON) {
offs1 = (ptrdiff_t)tn->tn_left->tn_val->v_quad;
if (conaddr(tn->tn_right, &sym, &offs2) == -1)
return (-1);
} else if (tn->tn_right->tn_op == CON) {
offs2 = (ptrdiff_t)tn->tn_right->tn_val->v_quad;
if (tn->tn_op == MINUS)
offs2 = -offs2;
if (conaddr(tn->tn_left, &sym, &offs1) == -1)
return (-1);
} else {
return (-1);
}
*symp = sym;
*offsp = offs1 + offs2;
break;
case AMPER:
if (tn->tn_left->tn_op == NAME) {
*symp = tn->tn_left->tn_sym;
*offsp = 0;
} else if (tn->tn_left->tn_op == STRING) {
/*
* If this would be the front end of a compiler we
* would return a label instead of 0.
*/
*offsp = 0;
}
break;
case CVT:
t = tn->tn_type->t_tspec;
ot = tn->tn_left->tn_type->t_tspec;
if ((!isityp(t) && t != PTR) || (!isityp(ot) && ot != PTR)) {
return (-1);
} else if (psize(t) != psize(ot)) {
return (-1);
}
if (conaddr(tn->tn_left, symp, offsp) == -1)
return (-1);
break;
default:
return (-1);
}
return (0);
}
/*
* Concatenate two string constants.
*/
strg_t *
catstrg(strg_t *strg1, strg_t *strg2)
{
size_t len1, len2, len;
if (strg1->st_tspec != strg2->st_tspec) {
/* cannot concatenate wide and regular string literals */
error(292);
return (strg1);
}
len = (len1 = strg1->st_len) + (len2 = strg2->st_len);
if (strg1->st_tspec == CHAR) {
strg1->st_cp = xrealloc(strg1->st_cp, len + 1);
(void)memcpy(strg1->st_cp + len1, strg2->st_cp, len2 + 1);
free(strg2->st_cp);
} else {
strg1->st_wcp = xrealloc(strg1->st_wcp,
(len + 1) * sizeof (wchar_t));
(void)memcpy(strg1->st_wcp + len1, strg2->st_wcp,
(len2 + 1) * sizeof (wchar_t));
free(strg2->st_wcp);
}
strg1->st_len = len;
free(strg2);
return (strg1);
}
/*
* Print a warning if the given node has operands which should be
* parenthesized.
*
* XXX Does not work if an operand is a constant expression. Constant
* expressions are already folded.
*/
static void
precconf(tnode_t *tn)
{
tnode_t *ln, *rn;
op_t lop, rop = NOOP;
int lparn, rparn;
mod_t *mp;
int warn;
if (!hflag)
return;
mp = &modtab[tn->tn_op];
lparn = 0;
for (ln = tn->tn_left; ln->tn_op == CVT; ln = ln->tn_left)
lparn |= ln->tn_parn;
lparn |= ln->tn_parn;
lop = ln->tn_op;
if (mp->m_binary) {
rparn = 0;
for (rn = tn->tn_right; tn->tn_op == CVT; rn = rn->tn_left)
rparn |= rn->tn_parn;
rparn |= rn->tn_parn;
rop = rn->tn_op;
}
warn = 0;
switch (tn->tn_op) {
case SHL:
case SHR:
if (!lparn && (lop == PLUS || lop == MINUS)) {
warn = 1;
} else if (!rparn && (rop == PLUS || rop == MINUS)) {
warn = 1;
}
break;
case LOGOR:
if (!lparn && lop == LOGAND) {
warn = 1;
} else if (!rparn && rop == LOGAND) {
warn = 1;
}
break;
case AND:
case XOR:
case OR:
if (!lparn && lop != tn->tn_op) {
if (lop == PLUS || lop == MINUS) {
warn = 1;
} else if (lop == AND || lop == XOR) {
warn = 1;
}
}
if (!warn && !rparn && rop != tn->tn_op) {
if (rop == PLUS || rop == MINUS) {
warn = 1;
} else if (rop == AND || rop == XOR) {
warn = 1;
}
}
break;
/* LINTED (enumeration values not handled in switch) */
}
if (warn) {
/* precedence confusion possible: parenthesize! */
warning(169);
}
}
/*
* Return the function name of a function (or of a pointer to a
* function).
*/
const char *
funcname(tnode_t *tn)
{
/* dereference function pointer */
switch (tn->tn_op) {
case AMPER:
return funcname(tn->tn_left);
case LOAD:
/* called as (*fptr)(args) */
return funcname(tn->tn_left);
case CALL:
return funcname(tn->tn_left);
case NAME:
return tn->tn_sym->s_name;
default:
return "<unknown>";
}
}
|