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
|
/*
* cs_stmt.tc - Semantic analysis for C# statement node types.
*
* Copyright (C) 2001, 2002 Southern Storm Software, Pty Ltd.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
%{
/*
* Determine if a tree node corresponds to the empty statement.
*/
#define IsEmpty(node) ((node) == 0 || yykind((node)) == yykindof(ILNode_Empty))
/*
* determine if a type1 implements type2
*/
static int ILTypeImplements(ILGenInfo *info, ILType *type1, ILType *type2)
{
ILClass *classInfo1 = ILTypeToClass(info, type1);
ILClass *classInfo2 = ILTypeToClass(info, type2);
if(classInfo1 && classInfo2)
{
return ILClassImplements(classInfo1, classInfo2);
}
else
{
return 0;
}
}
/*
* determine if a type1 inherits type2
*/
static int ILTypeInherits(ILGenInfo *info, ILType *type1, ILType *type2)
{
ILClass *classInfo1 = ILTypeToClass(info, type1);
ILClass *classInfo2 = ILTypeToClass(info, type2);
if(classInfo1 && classInfo2)
{
return ILClassInheritsFrom(classInfo1, classInfo2);
}
else
{
return 0;
}
}
/*
* Obtain the default scope of the class
*/
#define GetDefaultScope(info) (((ILNode_ClassDefn*)(info->currentClass))->classInfo)
/*
* Perform semantic analysis on a sub-statement.
*/
static void StmtSem(ILNode *node, ILGenInfo *info, ILNode **parent)
{
CSSemValue value;
if(node)
{
value = ILNode_SemAnalysis(node, info, parent);
if(!CSSemIsVoid(value) && !CSSemIsValue(value))
{
CCErrorOnLine(yygetfilename(node), yygetlinenum(node),
"invalid statement");
}
}
}
/*
* Statement context flags.
*/
#define CS_STMT_LOOP 1
#define CS_STMT_SWITCH 2
#define CS_STMT_TRY 4
#define CS_STMT_CATCH 8
#define CS_STMT_FINALLY 16
/*
* Push a statement context.
*/
static void PushStmtContext(ILGenInfo *info, int context)
{
if(info->contextStackSize >= info->contextStackMax)
{
/* Increase the size of the context stack */
int *newstack;
newstack = (int *)ILRealloc(info->contextStack,
sizeof(int) * (info->contextStackMax + 4));
if(!newstack)
{
ILGenOutOfMemory(info);
}
info->contextStack = newstack;
info->contextStackMax += 4;
}
info->contextStack[info->contextStackSize] = context;
++(info->contextStackSize);
}
/*
* Pop the top-most statement context.
*/
static void PopStmtContext(ILGenInfo *info)
{
--(info->contextStackSize);
}
/*
* Determine if we are within a specific context, but not
* within a "stop" context. e.g. "break" can be used
* within a loop, but not in the body of a "finally"
* within the loop. "finally" is the "stop" context.
*/
static int StmtContextOK(ILGenInfo *info, int context, int stopContext)
{
long posn = info->contextStackSize;
while(posn > 0)
{
--posn;
if((info->contextStack[posn] & stopContext) != 0)
{
return 0;
}
if((info->contextStack[posn] & context) != 0)
{
return 1;
}
}
if(!context)
{
/* We are checking a "return" statement */
return 1;
}
else
{
/* We are checking some other kind of statement */
return 0;
}
}
/*
* Add a local variable to a method.
* Returns the index of the new local variable in the local signature.
*/
static unsigned long AddLocalToMethod(ILGenInfo *info,
ILNode_MethodDeclaration *method,
ILType *type)
{
/* Create the local variable signature for this method */
if(!(method->localVarSig))
{
method->localVarSig = ILTypeCreateLocalList(info->context);
if(!(method->localVarSig))
{
CCOutOfMemory();
}
}
if(!ILTypeAddLocal(info->context, method->localVarSig, type))
{
CCOutOfMemory();
}
return ILTypeNumLocals(method->localVarSig) - 1;
}
static ILNode *HandleFixedExpr(ILGenInfo *info, ILNode *nameNode,
ILNode *exprNode, ILNode **exprParent,
int isFixAddress)
{
CSSemValue value;
ILNode *expr;
/* Perform semantic analysis on the expression */
value = ILNode_SemAnalysis(exprNode, info, exprParent);
/* Assign the value to a pinned local. */
if(CSSemIsValue(value))
{
ILNode *pinnedVar;
unsigned long tempVar;
ILNode_MethodDeclaration *method;
ILType *pinnedType;
ILType *type = CSSemGetType(value);
/* Locate the method that this local is declared within */
method = (ILNode_MethodDeclaration *)(info->currentMethod);
expr = *exprParent;
if(isFixAddress && ILType_IsPointer(type))
{
type = ILTypeCreateRef(info->context, IL_TYPE_COMPLEX_BYREF,
ILType_Ref(type));
if(!type)
{
CCOutOfMemory();
}
CSSemSetValueKind(value, CSSemGetKind(value), type);
}
pinnedType = ILTypeCreateRef(info->context, IL_TYPE_COMPLEX_PINNED,
type);
if(!pinnedType)
{
ILGenOutOfMemory(info);
}
tempVar = AddLocalToMethod(info, method, pinnedType);
pinnedVar = ILNode_LocalVar_create(tempVar, ILTypeToMachineType(type));
/* Assign the value to the pinned variable */
expr = ILNode_Assign_create(pinnedVar, expr);
yysetfilename(expr, yygetfilename(exprNode));
yysetlinenum(expr, yygetlinenum(exprNode));
/* Wrap the expression to prevent double evaluation */
expr = ILNode_SemGuard_create(expr, value);
yysetfilename(expr, yygetfilename(expr));
yysetlinenum(expr, yygetlinenum(expr));
}
else
{
/* Wrap the expression to prevent double evaluation */
expr = ILNode_SemGuard_create(*exprParent, value);
yysetfilename(expr, yygetfilename(exprNode));
yysetlinenum(expr, yygetlinenum(exprNode));
}
/* Assign the value to the variable */
expr = ILNode_Assign_create(nameNode, expr);
yysetfilename(expr, yygetfilename(nameNode));
yysetlinenum(expr, yygetlinenum(nameNode));
return expr;
}
/*
* Check if a type implements the IDisposable interface
*/
static void CheckIfDisposable(ILGenInfo *info, ILNode *node, ILType *type)
{
ILType *disposableType;
disposableType=ILFindSystemType(info,"IDisposable");
if(!ILTypeImplements(info, type, disposableType) &&
!ILTypeInherits(info, type, disposableType))
{
CCErrorOnLine(yygetfilename(node), yygetlinenum(node),
"`%s' does not implement 'IDisposable' interface ",
CSTypeToName(type));
}
}
/*
* Resolve the type for a local variable declaration.
* Returns the type on success and defaults to ILType_Int32 on error.
* If the type is 'var' and to be the result of the initialization statement
* then Null is returned.
*/
static ILType *FindLocalType(ILNode *node, ILGenInfo *info, ILNode **parent,
int isUsing)
{
ILType *type;
#if IL_VERSION_MAJOR > 2
if(node && yykind(node) == yykindof(ILNode_Identifier))
{
const char *name;
name = ((ILNode_Identifier *)node)->name;
if(name && !strcmp(name, "var"))
{
CSSemValue value;
int save;
save = info->inSemType;
info->inSemType = info->typeGather;
value = ILNode_SemAnalysisType(node, info, parent);
info->inSemType = save;
if(!CSSemIsType(value))
{
/*
* This is a declaration where the type results from the result
* type of the initialization statement.
* We simply replace thy type node here and leave the real work
* for later.
*/
type = 0;
}
else
{
type = CSSemGetType(value);
if(type == ILType_Void)
{
CCErrorOnLine(yygetfilename(node),
yygetlinenum(node),
"`void' type is not allowed in this context");
type = ILType_Int32;
}
}
}
else
{
/* Perform semantic analysis on the local variable type */
type = CSSemType(node, info, parent);
}
}
else
#endif
{
/* Perform semantic analysis on the local variable type */
type = CSSemType(node, info, parent);
}
if(type && isUsing)
{
CheckIfDisposable(info, node, type);
}
return type;
}
/*
* If type is Null then the type results in the return type of the
* initialization expression.
* The resulting type of the local is returned.
*/
static ILType *HandleLocalDecl(ILNode *node, ILGenInfo *info, ILNode **parent,
ILType *type, int isUsing)
{
const char *name = 0;
ILNode *nameNode;
unsigned long varNum;
ILNode_MethodDeclaration *method;
ILType *resultType = type;
if(!node)
{
nameNode = 0;
}
if(yyisa(node, ILNode_VariableDeclarator))
{
nameNode = ((ILNode_VariableDeclarator *)node)->name;
parent = &(((ILNode_VariableDeclarator *)node)->name);
}
else
{
nameNode = node;
}
if(nameNode)
{
ILScopeData *data;
name = ILQualIdentName(nameNode, 0);
data = ILScopeLookup(info->currentScope, name, 0);
if(data)
{
ILNode *errorNode;
/* The name is already declared in this scope */
CCErrorOnLine(yygetfilename(nameNode), yygetlinenum(nameNode),
"`%s' is already declared in this scope", name);
errorNode = ILScopeDataGetNode(data);
if(errorNode)
{
CCErrorOnLine(yygetfilename(errorNode),
yygetlinenum(errorNode),
"previous declaration here");
}
nameNode = 0;
}
}
if(!type)
{
if(node && yyisa(node, ILNode_VariableDeclarator))
{
CSSemValue value;
ILNode_VariableDeclarator *declNode;
declNode = (ILNode_VariableDeclarator *)node;
if(declNode->init)
{
value = ILNode_SemAnalysis(declNode->init,
info, &(declNode->init));
if(!CSSemIsValue(value))
{
CCErrorOnLine(yygetfilename(node),
yygetlinenum(node),
"invalid lvalue in assignment");
if(!CSSemIsRValue(value))
{
CSSemSetRValue(value, ILType_Int32);
}
}
resultType = CSSemGetType(value);
/* Wrap the expression to prevent double evaluation */
declNode->init = ILNode_SemGuard_create(declNode->init,
value);
}
else
{
CCErrorOnLine(yygetfilename(node),
yygetlinenum(node),
"`var' type is not allowed without an initializer");
resultType = ILType_Int32;
}
}
else
{
CCErrorOnLine(yygetfilename(node),
yygetlinenum(node),
"`var' type is not allowed without an initializer");
resultType = ILType_Int32;
}
}
/* Locate the method that this local is declared within */
method = (ILNode_MethodDeclaration *)(info->currentMethod);
/* Add the type to the local variable signature for this method */
varNum = AddLocalToMethod(info, method, resultType);
if(nameNode)
{
/* Create a local variable entry in the current scope */
ILScopeDeclareLocal(info->currentScope, name, varNum, nameNode);
}
if(isUsing)
{
/* replace the name with the local variable */
*parent = ILNode_LocalVar_create(varNum,
ILTypeToMachineType(resultType));
}
if(isUsing && node &&
(!yyisa(node, ILNode_VariableDeclarator) ||
((ILNode_VariableDeclarator *)node)->init == 0))
{
CCErrorOnLine(yygetfilename(node),
yygetlinenum(node),
"resource declaration in a using statement is not allowed without an initializer");
}
return resultType;
}
static void HandleForeachVarDecl(ILNode_Foreach *node, ILGenInfo *info, ILType *type)
{
unsigned long varNum;
ILNode_MethodDeclaration *method;
/* Locate the method that this local is declared within */
method = (ILNode_MethodDeclaration *)(info->currentMethod);
varNum = AddLocalToMethod(info, method, type);
/* Create a local variable entry in the current scope */
/* TODO: the variable should be marked read-only */
ILScopeDeclareLocal(info->currentScope, node->varName, varNum,
node->varNameNode);
node->varIndex = varNum;
node->varType = ILTypeToMachineType(type);
}
%}
/*
* Perform semantic analysis for the empty statement.
*/
ILNode_SemAnalysis(ILNode_Empty)
{
return CSSemValueDefault;
}
/*
* Perform semantic analysis for the compound statement.
*/
ILNode_SemAnalysis(ILNode_Compound),
ILNode_SemAnalysis(ILNode_List)
{
ILNode_ListIter iter;
ILNode *child;
ILNode *replace;
ILNode_ListIter_Init(&iter, node);
while((child = ILNode_ListIter_Next(&iter)) != 0)
{
replace = child;
StmtSem(child, info, &replace);
if(replace != child)
{
*(iter.last) = replace;
}
}
return CSSemValueDefault;
}
/*
* Perform semantic analysis for the "if" statement.
*
* Note: we assume that the condition has been wrapped in
* a "ToBool" node to take care of the semantic analysis
* for the boolean cast.
*/
ILNode_SemAnalysis(ILNode_If)
{
ILEvalValue evalValue;
ILNode_SemAnalysis(node->expr, info, &(node->expr));
StmtSem(node->thenClause, info, &(node->thenClause));
StmtSem(node->elseClause, info, &(node->elseClause));
/* the compiler does do the same optimisation lower down
the codegenerator , but that is not used for return
value tracking */
if(ILNode_EvalConst(node->expr, info, &evalValue))
{
if(evalValue.un.i4Value)
{
*parent = node->thenClause;
}
else
{
*parent = node->elseClause;
}
}
return CSSemValueDefault;
}
/*
* Perform semantic analysis for the "while" statement.
*
* Note: we assume that the condition has been wrapped in
* a "ToBool" node to take care of the semantic analysis
* for the boolean cast.
*/
ILNode_SemAnalysis(ILNode_While)
{
ILNode_SemAnalysis(node->cond, info, &(node->cond));
PushStmtContext(info, CS_STMT_LOOP);
StmtSem(node->stmt, info, &(node->stmt));
PopStmtContext(info);
return CSSemValueDefault;
}
/*
* Perform semantic analysis for the "do" statement.
*
* Note: we assume that the condition has been wrapped in
* a "ToBool" node to take care of the semantic analysis
* for the boolean cast.
*/
ILNode_SemAnalysis(ILNode_Do)
{
PushStmtContext(info, CS_STMT_LOOP);
StmtSem(node->stmt, info, &(node->stmt));
PopStmtContext(info);
ILNode_SemAnalysis(node->cond, info, &(node->cond));
return CSSemValueDefault;
}
/*
* Perform semantic analysis for the "for" statement.
*
* Note: we assume that the condition has been wrapped in
* a "ToBool" node to take care of the semantic analysis
* for the boolean cast.
*/
ILNode_SemAnalysis(ILNode_For)
{
StmtSem(node->init, info, &(node->init));
ILNode_SemAnalysis(node->cond, info, &(node->cond));
StmtSem(node->incr, info, &(node->incr));
PushStmtContext(info, CS_STMT_LOOP);
StmtSem(node->stmt, info, &(node->stmt));
PopStmtContext(info);
return CSSemValueDefault;
}
/*
* Perform semantic analysis for the "foreach" statement.
*/
ILNode_SemAnalysis(ILNode_Foreach)
{
CSSemValue value;
ILScope *scope;
ILType *type;
/* Locate the scope and method for the iteration variable */
scope = info->currentScope;
/* Perform semantic analysis on the local variable type */
if(CSHasUnsafeType(node->type))
{
CCUnsafeTypeMessage(info, (ILNode *)node);
}
type = FindLocalType(node->type, info, &(node->type), 0);
/* Add the type to the local variable signature for this method */
if(type)
{
HandleForeachVarDecl(node, info, type);
}
/* Perform semantic analysis on the collection expression */
if(!CSSemExpectValue(node->expr, info, &(node->expr), &value))
{
CCErrorOnLine(yygetfilename(node->expr), yygetlinenum(node->expr),
"invalid collection expression in `foreach' statement");
if(type)
{
node->arrayType = ILTypeCreateArray(info->context, 1, type);
}
else
{
type = ILType_Int32;
HandleForeachVarDecl(node, info, type);
node->arrayType = ILTypeCreateArray(info->context, 1, type);
}
if(!(node->arrayType))
{
CCOutOfMemory();
}
}
else
{
node->arrayType = CSSemGetType(value);
}
/* Is this a simple array-based or collection-based "foreach" statement? */
if(ILType_IsSimpleArray(node->arrayType))
{
/*
* If the collection is a simple array and the iteration variable
* has the same type as the array elements. We can optimise
* this case in the code generator to scan the array efficiently.
* Nothing has to be done here in this case.
*/
node->elemType = ILTypeGetElemType(node->arrayType);
if(!type)
{
type = node->elemType;
HandleForeachVarDecl(node, info, type);
}
else if(!ILTypeIdentical(node->elemType, type))
{
ILNode *castNode;
castNode = ILNode_EmptyExpr_create
(ILTypeToMachineType(node->elemType));
if(!ILCast(info, castNode, &(castNode), node->elemType, type, 1))
{
CCErrorOnLine(yygetfilename(node), yygetlinenum(node),
"Cannot cast '%s' to '%s'",CSTypeToName(node->elemType),
CSTypeToName(type));
}
node->doCast = castNode;
}
}
else if(ILTypeIsStringClass(node->arrayType) &&
(!type || ILTypeIdentical(type, ILType_Char)))
{
/* The collection is a string, which we can special-case */
if(!type)
{
type = ILType_Char;
HandleForeachVarDecl(node, info, type);
}
*parent = ILNode_ForeachString_create(node->expr,
node->stmt,
node->varIndex,
node->varType);
}
else
{
ILMethod *getEnumerator = NULL;
ILType *ienumerableType;
ILClass *arrayClass;
arrayClass = ILTypeToClass(info, node->arrayType);
ienumerableType = ILFindNonSystemType(info,
"IEnumerable",
"System.Collections");
if(arrayClass)
{
getEnumerator = ILResolveInstanceMethod(info, arrayClass,
GetDefaultScope(info),
"GetEnumerator",
NULL, 0);
}
if(getEnumerator == NULL &&
ILTypeImplements(info, node->arrayType, ienumerableType))
{
ILClass * enumerable;
enumerable = ILTypeToClass(info, ienumerableType);
enumerable = ILClassResolve(enumerable);
getEnumerator = ILResolveInstanceMethod(info, enumerable,
GetDefaultScope(info),
"GetEnumerator",
NULL, 0);
}
if(getEnumerator)
{
ILProperty *property = NULL;
ILMethod *getCurrent = NULL;
ILMethod *moveNext = NULL;
ILType *retType;
ILType *enumerator;
enumerator = ILTypeGetReturn(ILMethod_Signature(getEnumerator));
property = ILResolveProperty(info,
ILType_ToClass(enumerator),
GetDefaultScope(info),
"Current");
getCurrent = (property ? ILProperty_Getter(property) : 0);
moveNext = ILResolveInstanceMethod(info,
ILType_ToClass(enumerator),
GetDefaultScope(info),
"MoveNext", NULL, 0);
if(!property)
{
CCErrorOnLine(yygetfilename(node), yygetlinenum(node),
"Could not locate 'Current' or 'IEnumerator.Current'"
"property in '%s'", CSTypeToName(enumerator));
}
else if(!getCurrent)
{
CCErrorOnLine(yygetfilename(node), yygetlinenum(node),
"'Current' property has no 'get' method in '%s'",
CSTypeToName(enumerator));
}
if(!moveNext)
{
CCErrorOnLine(yygetfilename(node), yygetlinenum(node),
"Cannot locate 'MoveNext()' or 'IEnumerator.MoveNext()' in '%s'",
CSTypeToName(enumerator));
}
if(getCurrent && moveNext)
{
retType = ILTypeGetReturn(ILMethod_Signature(getCurrent));
if(!type)
{
type = retType;
HandleForeachVarDecl(node, info, type);
*parent = ILNode_ForeachCollection_create(
NULL,
node->expr,
node->stmt,
node->arrayType,
node->varIndex,
ILTypeToMachineType(type),
enumerator,
getEnumerator,
moveNext,
getCurrent);
}
else if(ILTypeIdentical(type, retType))
{
*parent = ILNode_ForeachCollection_create(
NULL,
node->expr,
node->stmt,
node->arrayType,
node->varIndex,
ILTypeToMachineType(type),
enumerator,
getEnumerator,
moveNext,
getCurrent);
}
else
{
ILNode *castNode;
castNode = ILNode_EmptyExpr_create(ILTypeToMachineType(retType));
if(!ILCast(info, castNode, &(castNode), retType, type, 1))
{
CCErrorOnLine(yygetfilename(node), yygetlinenum(node),
"Cannot cast '%s' to '%s'",CSTypeToName(retType),
CSTypeToName(type));
}
*parent = ILNode_ForeachCollection_create(
castNode,
node->expr,
node->stmt,
node->arrayType,
node->varIndex,
ILTypeToMachineType(type),
enumerator,
getEnumerator,
moveNext,
getCurrent);
}
}
}
else
{
CCErrorOnLine(yygetfilename(node), yygetlinenum(node),
"Could not locate a 'GetEnumerator' or "
"'IEnumerable.GetEnumerator' in '%s'",
CSTypeToName(node->arrayType));
}
}
/* Perform semantic analysis on the body of the "foreach" statement */
PushStmtContext(info, CS_STMT_LOOP);
StmtSem(node->stmt, info, &(node->stmt));
PopStmtContext(info);
/* Done */
return CSSemValueDefault;
}
/*
* Perform semantic analysis for the "foreach string" and
* "foreach collection" statements.
*/
ILNode_SemAnalysis(ILNode_EmptyExpr),
ILNode_SemAnalysis(ILNode_ForeachString),
ILNode_SemAnalysis(ILNode_ForeachCollection)
{
/* We don't need to do anything here, as this node type is
synthesized by the "ILNode_Foreach" processing */
return CSSemValueDefault;
}
%{
/*
* Count the number of case labels in the body of a "switch" statement,
* and find the default case.
*/
static unsigned long CountLabelsAndFindDefault(ILNode *sections,
ILNode **defCase)
{
ILNode_ListIter iter1;
ILNode_ListIter iter2;
ILNode *section;
ILNode *caseNode;
unsigned long count;
ILNode *prevDefLabel;
/* Initialize the return information */
count = 0;
*defCase = 0;
prevDefLabel = 0;
/* Iterate through the sections */
ILNode_ListIter_Init(&iter1, sections);
while((section = ILNode_ListIter_Next(&iter1)) != 0)
{
/* Iterate through the case list for each section */
ILNode_ListIter_Init
(&iter2, ((ILNode_SwitchSection *)section)->caseList);
while((caseNode = ILNode_ListIter_Next(&iter2)) != 0)
{
if(yyisa(caseNode, ILNode_DefaultLabel))
{
/* This is the switch's default label */
if(prevDefLabel)
{
CCErrorOnLine(yygetfilename(caseNode),
yygetlinenum(caseNode),
"multiple `default' labels in `switch' statement");
CCErrorOnLine(yygetfilename(prevDefLabel),
yygetlinenum(prevDefLabel),
"previous `default' label here");
}
else
{
*defCase = section;
prevDefLabel = caseNode;
}
}
else
{
/* This is a particular case label */
++count;
}
}
}
/* Return the count to the caller */
return count;
}
/*
* Compare two case label values within "ILSwitchValue" objects.
*/
static int CompareCaseLabels(const void *e1, const void *e2)
{
const ILEvalValue *value1 = &(((const ILSwitchValue *)e1)->value);
const ILEvalValue *value2 = &(((const ILSwitchValue *)e2)->value);
switch(value1->valueType)
{
case ILMachineType_Int8:
case ILMachineType_UInt8:
case ILMachineType_Int16:
case ILMachineType_UInt16:
case ILMachineType_Char:
case ILMachineType_Int32:
{
if(value1->un.i4Value < value2->un.i4Value)
{
return -1;
}
else if(value1->un.i4Value > value2->un.i4Value)
{
return 1;
}
}
break;
case ILMachineType_UInt32:
{
if(((ILUInt32)(value1->un.i4Value)) <
((ILUInt32)(value2->un.i4Value)))
{
return -1;
}
else if(((ILUInt32)(value1->un.i4Value)) >
((ILUInt32)(value2->un.i4Value)))
{
return 1;
}
}
break;
case ILMachineType_Int64:
{
if(value1->un.i8Value < value2->un.i8Value)
{
return -1;
}
else if(value1->un.i8Value > value2->un.i8Value)
{
return 1;
}
}
break;
case ILMachineType_UInt64:
{
if(((ILUInt64)(value1->un.i8Value)) <
((ILUInt64)(value2->un.i8Value)))
{
return -1;
}
else if(((ILUInt64)(value1->un.i8Value)) >
((ILUInt64)(value2->un.i8Value)))
{
return 1;
}
}
break;
case ILMachineType_String:
{
const char *str1 = value1->un.strValue.str;
const char *str2 = value2->un.strValue.str;
int len1 = value1->un.strValue.len;
int len2 = value2->un.strValue.len;
unsigned char ch1;
unsigned char ch2;
if(!str1)
{
/* The first value is "null" */
return (str2 ? -1 : 0);
}
else if(!str2)
{
/* The second value is "null" */
return 1;
}
while(len1 > 0 && len2 > 0)
{
ch1 = (unsigned char)(*str1++);
ch2 = (unsigned char)(*str2++);
if(ch1 < ch2)
{
return -1;
}
else if(ch1 > ch2)
{
return 1;
}
--len1;
--len2;
}
if(len1 > 0)
{
return 1;
}
else if(len2 > 0)
{
return -1;
}
}
break;
default: break;
}
return 0;
}
/*
* Collect up all of the case labels for a "switch" statement,
* and sort them into ascending order. This also performs
* semantic analysis on each of the sections. Returns the new
* size of the case label list, after errors have been removed.
*/
static unsigned long CollectCaseLabels(ILGenInfo *info, ILNode *sections,
ILType *switchType,
ILSwitchValue *values)
{
ILNode_ListIter iter1;
ILNode_ListIter iter2;
ILNode *section;
ILNode *caseNode;
ILNode_CaseLabel *caseLabel;
unsigned long size;
unsigned long posn;
CSSemValue value;
ILEvalValue evalValue;
ILMachineType switchMachineType;
/* Initialize */
size = 0;
switchMachineType = ILTypeToMachineType(switchType);
/* Iterate through the sections */
ILNode_ListIter_Init(&iter1, sections);
while((section = ILNode_ListIter_Next(&iter1)) != 0)
{
/* Iterate through the case list for each section */
ILNode_ListIter_Init
(&iter2, ((ILNode_SwitchSection *)section)->caseList);
while((caseNode = ILNode_ListIter_Next(&iter2)) != 0)
{
if(yyisa(caseNode, ILNode_CaseLabel))
{
/* Perform semantic analysis on the case expression */
caseLabel = (ILNode_CaseLabel *)caseNode;
if(!CSSemExpectValue(caseLabel->expr, info,
&(caseLabel->expr), &value))
{
CCErrorOnLine(yygetfilename(caseLabel->expr),
yygetlinenum(caseLabel->expr),
"invalid case label expression");
continue;
}
/* Coerce the case expression to the governing type */
if(!ILCoerce(info, caseLabel->expr, &(caseLabel->expr),
CSSemGetType(value), switchType,1))
{
CCErrorOnLine(yygetfilename(caseLabel->expr),
yygetlinenum(caseLabel->expr),
"no conversion from `%s' to `%s'",
CSTypeToName(CSSemGetType(value)),
CSTypeToName(switchType));
continue;
}
/* Evaluate the constant value for the case expression */
if(!ILNode_EvalConst(caseLabel->expr, info, &evalValue) ||
!ILGenCastConst(info, &evalValue, evalValue.valueType,
switchMachineType))
{
CCErrorOnLine(yygetfilename(caseLabel->expr),
yygetlinenum(caseLabel->expr),
"case expression is not constant");
continue;
}
/* Record the case node information for later */
values[size].value = evalValue;
values[size].caseLabel = caseNode;
values[size].section = section;
++size;
}
}
}
/* split into 2 passes to facilitate goto case
* So SemAnalysis is performed after all the CaseLabels are
* collected.
*/
/* Iterate through the sections again */
ILNode_ListIter_Init(&iter1, sections);
while((section = ILNode_ListIter_Next(&iter1)) != 0)
{
((ILNode_SwitchSection*)section)->visited=ILVisitMode_Processing;
/* Perform semantic analysis on the section's body */
ILNode_SemAnalysis(((ILNode_SwitchSection *)section)->stmt, info,
&(((ILNode_SwitchSection *)section)->stmt));
((ILNode_SwitchSection*)section)->visited=ILVisitMode_Done;
/* The body must end in some kind of return or jump statement */
if(!ILNodeEndsInFlowChange(((ILNode_SwitchSection *)section)->stmt
,info))
{
CCErrorOnLine(yygetfilename(section), yygetlinenum(section),
"`switch' section falls through");
}
}
/* Sort the case label list into ascending order */
if(size > 1)
{
#ifdef HAVE_QSORT
qsort(values, (size_t)size, sizeof(ILSwitchValue), CompareCaseLabels);
#else
unsigned long i;
unsigned long j;
ILSwitchValue temp;
for(i = 0; i < size - 1; ++i)
{
for(j = i + 1; j < size; ++j)
{
if(CompareCaseLabels(&(values[i]), &(values[j])) > 0)
{
temp = values[i];
values[i] = values[j];
values[j] = temp;
}
}
}
#endif
}
/* Check for duplicates in the case label list */
if(size > 1)
{
for(posn = 0; posn < size - 1; ++posn)
{
if(CompareCaseLabels(&(values[posn]), &(values[posn + 1])) == 0)
{
CCErrorOnLine(yygetfilename(values[posn + 1].caseLabel),
yygetlinenum(values[posn + 1].caseLabel),
"duplicate case expression");
CCErrorOnLine(yygetfilename(values[posn].caseLabel),
yygetlinenum(values[posn].caseLabel),
"previous definition here");
}
}
}
/* Return the final table size to the caller */
return size;
}
%}
/*
* Perform semantic analysis for the "switch" statement.
*/
ILNode_SemAnalysis(ILNode_Switch)
{
static ILType * const governingTypes[] = {
ILType_Int8, ILType_UInt8, ILType_Int16, ILType_UInt16,
ILType_Int32, ILType_UInt32, ILType_Int64, ILType_UInt64, ILType_Char
};
#define numGoverningTypes (sizeof(governingTypes) / sizeof(ILType *))
CSSemValue value;
ILType *actualType;
ILType *switchType;
ILType *stringType;
int posn;
unsigned long size;
ILNode *savedSwitch;
/* Perform semantic analysis on the expression */
if(!CSSemExpectValue(node->expr, info, &(node->expr), &value))
{
CCErrorOnLine(yygetfilename(node->expr), yygetlinenum(node->expr),
"invalid expression for `switch' statement");
actualType = ILType_Int32;
}
else
{
actualType = CSSemGetType(value);
}
/* Determine the governing type for the switch */
if(ILTypeIsEnum(actualType))
{
switchType = actualType;
}
else
{
switchType = actualType;
for(posn = 0; posn < numGoverningTypes; ++posn)
{
if(ILCanCoerceKind(info, actualType, governingTypes[posn],
IL_CONVERT_USER_DEFINED,1))
{
switchType = governingTypes[posn];
break;
}
}
if(posn >= numGoverningTypes)
{
stringType = ILFindSystemType(info, "String");
if(ILCanCoerceKind(info, actualType, stringType,
IL_CONVERT_USER_DEFINED,1))
{
switchType = stringType;
}
else
{
CCErrorOnLine(yygetfilename(node->expr),
yygetlinenum(node->expr),
"`%s' is invalid as the governing type for a `switch'",
CSTypeToName(actualType));
return CSSemValueDefault;
}
}
}
/* Cast the switch expression to the governing type */
ILCast(info, node->expr, &(node->expr), actualType, switchType,1);
node->switchType=switchType;
/* Enter the switch context */
PushStmtContext(info, CS_STMT_SWITCH);
savedSwitch=info->currentSwitch;
info->currentSwitch=(ILNode*)node;
/* Count the number of case labels and find the default case */
node->numSwitchValues = CountLabelsAndFindDefault
(node->sections, &(node->defaultSection));
/* Allocate space for the switch value table */
size = node->numSwitchValues * sizeof(ILSwitchValue);
if(size > 1024)
{
/* Warning! This will leak memory! */
node->switchValues = (ILSwitchValue *)ILMalloc(size);
}
else
{
/* Allocate from the node pool, which will be cleaned up for us */
node->switchValues = (ILSwitchValue *)yynodealloc((unsigned int)size);
}
if(!(node->switchValues))
{
CCOutOfMemory();
}
/* Perform semantic analysis on the switch body and compute the values */
node->numSwitchValues = CollectCaseLabels(info, node->sections,
switchType, node->switchValues);
/* Determine the type of switch table to generate */
CGSetSwitchTableType(node);
info->currentSwitch=savedSwitch;
/* Leave the switch context */
PopStmtContext(info);
/* Done */
return CSSemValueDefault;
}
/*
* Perform semantic analysis for the switch helper nodes.
*/
ILNode_SemAnalysis(ILNode_SwitchSectList),
ILNode_SemAnalysis(ILNode_SwitchSection),
ILNode_SemAnalysis(ILNode_CaseList),
ILNode_SemAnalysis(ILNode_CaseLabel),
ILNode_SemAnalysis(ILNode_DefaultLabel)
{
/* Nothing to do here: the real work is done in "ILNode_Switch" */
return CSSemValueDefault;
}
/*
* Perform semantic analysis for the "goto" statement.
*/
ILNode_SemAnalysis(ILNode_Goto)
{
/* TODO */
return CSSemValueDefault;
}
/*
* Perform semantic analysis for the "goto label" statement.
*/
ILNode_SemAnalysis(ILNode_GotoLabel)
{
/* TODO */
return CSSemValueDefault;
}
/*
* Perform semantic analysis for the "goto case" statement.
*/
ILNode_SemAnalysis(ILNode_GotoCase)
{
CSSemValue value;
ILEvalValue evalValue;
ILNode_Switch *switchExpr;
ILMachineType switchMachineType;
ILSwitchValue switchValue;
unsigned long posn;
if(!StmtContextOK(info, CS_STMT_SWITCH, CS_STMT_FINALLY))
{
if(StmtContextOK(info, CS_STMT_FINALLY, 0))
{
CCErrorOnLine(yygetfilename(node), yygetlinenum(node),
"`goto' used inside `finally' clause");
return CSSemValueDefault;
}
else
{
CCErrorOnLine(yygetfilename(node), yygetlinenum(node),
"`goto' used outside switch");
return CSSemValueDefault;
}
}
/* if node->currentSwitch is NULL, then somebody messed up the
PushStmtContext . It is a "bug detection" bug */
/* Initialize */
switchExpr=(ILNode_Switch*)(info->currentSwitch);
switchMachineType = ILTypeToMachineType(switchExpr->switchType);
if(!CSSemExpectValue(node->expr, info,
&(node->expr), &value))
{
CCErrorOnLine(yygetfilename(node->expr),
yygetlinenum(node->expr),
"invalid case label expression");
return CSSemValueDefault;
}
/* Coerce the case expression to the governing type */
if(!ILCoerce(info, node->expr, &(node->expr),
CSSemGetType(value), switchExpr->switchType,1))
{
CCErrorOnLine(yygetfilename(node->expr),
yygetlinenum(node->expr),
"no conversion from `%s' to `%s'",
CSTypeToName(CSSemGetType(value)),
CSTypeToName(switchExpr->switchType));
return CSSemValueDefault;
}
/* Evaluate the constant value for the case expression */
if(!ILNode_EvalConst(node->expr, info, &evalValue) ||
!ILGenCastConst(info, &evalValue, evalValue.valueType,
switchMachineType))
{
CCErrorOnLine(yygetfilename(node->expr),
yygetlinenum(node->expr),
"case expression is not constant");
return CSSemValueDefault;
}
switchValue.value=evalValue;
for(posn = 0; posn < switchExpr->numSwitchValues ; ++posn)
{
if(CompareCaseLabels(&(switchExpr->switchValues[posn]),
&(switchValue)) == 0)
{
node->switchSection=(ILNode_SwitchSection*)
(switchExpr->switchValues[posn].section);
if(node->switchSection->visited==ILVisitMode_Processing)
{
CCWarningOnLine(yygetfilename(node), yygetlinenum(node),
"circularity in 'goto' detected");
}
return CSSemValueDefault;
}
}
/* if we get here , the case entry was not locatable */
CCErrorOnLine(yygetfilename(node), yygetlinenum(node),
"could not find a matching case for 'goto'");
return CSSemValueDefault;
}
/*
* Perform semantic analysis for the "goto default" statement.
*/
ILNode_SemAnalysis(ILNode_GotoDefault)
{
ILNode_SwitchSection *defaultSection;
if(!StmtContextOK(info, CS_STMT_SWITCH, CS_STMT_FINALLY))
{
if(StmtContextOK(info, CS_STMT_FINALLY, 0))
{
CCErrorOnLine(yygetfilename(node), yygetlinenum(node),
"`goto' used inside `finally' clause");
}
else
{
CCErrorOnLine(yygetfilename(node), yygetlinenum(node),
"`goto' used outside switch");
}
return CSSemValueDefault;
}
defaultSection=(ILNode_SwitchSection*)
(((ILNode_Switch*)info->currentSwitch)->defaultSection);
if(!defaultSection)
{
CCErrorOnLine(yygetfilename(node), yygetlinenum(node),
"no `default' case for switch");
return CSSemValueDefault;
}
if(defaultSection->visited == ILVisitMode_Processing)
{
CCWarningOnLine(yygetfilename(node), yygetlinenum(node),
"circularity in 'goto' detected");
}
return CSSemValueDefault;
}
/*
* Perform semantic analysis for the "break" statement.
*/
ILNode_SemAnalysis(ILNode_Break),
ILNode_SemAnalysis(ILNode_LabelledBreak) /* TODO */
{
PushStmtContext(info, CS_STMT_SWITCH);
if(!StmtContextOK(info, CS_STMT_LOOP | CS_STMT_SWITCH, CS_STMT_FINALLY))
{
if(StmtContextOK(info, CS_STMT_FINALLY, 0))
{
CCErrorOnLine(yygetfilename(node), yygetlinenum(node),
"`break' used inside `finally' clause");
}
else
{
CCErrorOnLine(yygetfilename(node), yygetlinenum(node),
"`break' used outside loop or switch");
}
}
return CSSemValueDefault;
}
/*
* Perform semantic analysis for the "continue" statement.
*/
ILNode_SemAnalysis(ILNode_Continue),
ILNode_SemAnalysis(ILNode_LabelledContinue) /* TODO */
{
if(!StmtContextOK(info, CS_STMT_LOOP, CS_STMT_FINALLY))
{
if(StmtContextOK(info, CS_STMT_FINALLY, 0))
{
CCErrorOnLine(yygetfilename(node), yygetlinenum(node),
"`continue' used inside `finally' clause");
}
else
{
CCErrorOnLine(yygetfilename(node), yygetlinenum(node),
"`continue' used outside loop");
}
}
return CSSemValueDefault;
}
/*
* Perform semantic analysis for the "return" statement.
*/
ILNode_SemAnalysis(ILNode_Return)
{
/* Emit an error if the return type is not "void" */
ILType *returnType = ILTypeGetReturn(ILMethod_Signature
(((ILNode_MethodDeclaration *)(info->currentMethod))->methodInfo));
if(returnType != ILType_Void)
{
CCErrorOnLine(yygetfilename(node), yygetlinenum(node),
"`return' with no value, in method returning non-void");
}
if(!StmtContextOK(info, 0, CS_STMT_FINALLY))
{
CCErrorOnLine(yygetfilename(node), yygetlinenum(node),
"`return' used inside `finally' clause");
}
return CSSemValueDefault;
}
/*
* Perform semantic analysis for the "return expression" statement.
*/
ILNode_SemAnalysis(ILNode_ReturnExpr)
{
CSSemValue value;
ILType *returnType;
/* Perform semantic analysis on the expression */
if(!CSSemExpectValue(node->expr, info, &(node->expr), &value))
{
CCErrorOnLine(yygetfilename(node), yygetlinenum(node),
"invalid return value");
return CSSemValueDefault;
}
/* Determine if the value is compatible with the return type */
returnType = ILTypeGetReturn(ILMethod_Signature
(((ILNode_MethodDeclaration *)(info->currentMethod))->methodInfo));
if(returnType == ILType_Void)
{
CCErrorOnLine(yygetfilename(node), yygetlinenum(node),
"`return' with a value, in method returning void");
}
else
{
/* Coerce to the return type */
if(!ILCoerce(info, node->expr, &(node->expr),
CSSemGetType(value), returnType,1))
{
CCErrorOnLine(yygetfilename(node), yygetlinenum(node),
"incompatible types in return: "
"no conversion from `%s' to `%s'",
CSTypeToName(CSSemGetType(value)),
CSTypeToName(returnType));
}
}
/* Check that the return was not used inside a "finally" clause */
if(!StmtContextOK(info, 0, CS_STMT_FINALLY))
{
CCErrorOnLine(yygetfilename(node), yygetlinenum(node),
"`return' used inside `finally' clause");
}
/* Done */
return CSSemValueDefault;
}
/*
* Perform semantic analysis for the "throw" statement.
*/
ILNode_SemAnalysis(ILNode_Throw)
{
if(!StmtContextOK(info, CS_STMT_CATCH, CS_STMT_FINALLY | CS_STMT_TRY))
{
CCErrorOnLine(yygetfilename(node), yygetlinenum(node),
"`throw' with no value must be used inside a `catch' clause");
}
return CSSemValueDefault;
}
/*
* Perform semantic analysis for the "throw expression" statement.
*/
ILNode_SemAnalysis(ILNode_ThrowExpr)
{
CSSemValue value;
ILType *exceptionClass;
/* Perform semantic analysis on the value */
if(!CSSemExpectValue(node->expr, info, &(node->expr), &value))
{
CCErrorOnLine(yygetfilename(node), yygetlinenum(node),
"invalid throw expression");
CSSemSetRValue(value, ILType_Null);
}
/* Attempt to coerce the value to "System.Exception" */
exceptionClass = ILFindSystemType(info, "Exception");
if(!ILCoerce(info, node->expr, &(node->expr),
CSSemGetType(value), exceptionClass,1))
{
CCErrorOnLine(yygetfilename(node), yygetlinenum(node),
"no conversion from `%s' to `%s'",
CSTypeToName(CSSemGetType(value)),
CSTypeToName(exceptionClass));
}
/* Done */
return CSSemValueDefault;
}
/*
* Perform semantic analysis for the "try" statement.
*/
ILNode_SemAnalysis(ILNode_Try)
{
/* Enter a "try" context */
PushStmtContext(info, CS_STMT_TRY);
/* Perform semantic analysis on the "try" body */
StmtSem(node->stmt, info, &(node->stmt));
/* Perform semantic analysis on the "catch" clauses */
StmtSem(node->catchClauses, info, &(node->catchClauses));
/* Perform semantic analysis on the "finally" clause */
StmtSem(node->finallyClause, info, &(node->finallyClause));
/* Exit from the "try" context */
PopStmtContext(info);
/* Done */
return CSSemValueDefault;
}
/*
* Perform semantic analysis for the "catch clause" statement.
*/
ILNode_SemAnalysis(ILNode_CatchClause)
{
ILType *exceptionType = ILFindSystemType(info, "Exception");
ILType *thrownType;
ILScope *newScope;
ILNode_MethodDeclaration *method =
(ILNode_MethodDeclaration *)(info->currentMethod);
/* Enter a "catch" context */
PushStmtContext(info, CS_STMT_CATCH);
/* Wrap the catch body in a new scope */
newScope = ILScopeCreate(info, info->currentScope);
node->stmt = ILNode_NewScope_create(node->stmt);
((ILNode_NewScope *)(node->stmt))->scope = newScope;
/* Validate the thrown value type */
if(node->type)
{
thrownType = CSSemType(node->type, info, &(node->type));
if(!ILCanCoerce(info, thrownType, exceptionType,0))
{
CCErrorOnLine(yygetfilename(node), yygetlinenum(node),
"`%s' does not inherit from `%s'",
CSTypeToName(thrownType),
CSTypeToName(exceptionType));
thrownType = exceptionType;
}
}
else
{
thrownType = exceptionType;
}
node->classInfo = ILTypeToClass(info, thrownType);
/* Declare a new local variable for the catch value */
if(node->name)
{
unsigned long varNum;
/* Add the type to the local variable signature for this method */
varNum = AddLocalToMethod(info, method, thrownType);
/* Create a local variable entry in the current scope */
ILScopeDeclareLocal(newScope, node->name, varNum, node->nameNode);
/* Record the local variable index for the code generator */
node->varIndex = varNum;
}
/* Perform semantic analysis on the catch body */
StmtSem(node->stmt, info, &(node->stmt));
/* Exit from the "catch" context */
PopStmtContext(info);
/* Done */
return CSSemValueDefault;
}
/*
* Perform semantic analysis for the "finally clause" statement.
*/
ILNode_SemAnalysis(ILNode_FinallyClause)
{
/* Enter a "finally" context */
PushStmtContext(info, CS_STMT_FINALLY);
/* Perform semantic analysis on the finally body */
StmtSem(node->stmt, info, &(node->stmt));
/* Exit from the "finally" context */
PopStmtContext(info);
/* Done */
return CSSemValueDefault;
}
/*
* Perform semantic analysis for the "lock" statement.
*/
ILNode_SemAnalysis(ILNode_Lock)
{
CSSemValue value;
ILType *objectType = ILFindSystemType(info, "Object");
/* Perform semantic analysis on the lock expression */
if(!CSSemExpectValue(node->expr, info, &(node->expr), &value))
{
CCErrorOnLine(yygetfilename(node->expr), yygetlinenum(node->expr),
"invalid lock expression");
CSSemSetRValue(value, objectType);
}
/* Coerce the lock expression to "System.Object" */
if(!ILCoerce(info, node->expr, &(node->expr),
CSSemGetType(value), objectType,1))
{
CCErrorOnLine(yygetfilename(node->expr), yygetlinenum(node->expr),
"no conversion from `%s' to `%s'",
CSTypeToName(CSSemGetType(value)),
CSTypeToName(objectType));
}
/* Enter a "try" context */
PushStmtContext(info, CS_STMT_TRY);
/* Perform semantic analysis on the lock statement body */
StmtSem(node->stmt, info, &(node->stmt));
/* Exit from the "try" context */
PopStmtContext(info);
/* Done */
return CSSemValueDefault;
}
/*
* Perform semantic analysis for the "using" statement.
*/
ILNode_SemAnalysis(ILNode_UsingStatement)
{
ILNode **stmtParent = 0;
if(yyisa(node->resource, ILNode_ResourceDeclaration))
{
ILType *type;
ILNode_ResourceDeclaration *resource;
resource = (ILNode_ResourceDeclaration *)(node->resource);
/* Perform semantic analysis on the resource variable type */
type = FindLocalType(resource->type, info, &(resource->type), 1);
if(yyisa(resource->variables, ILNode_List))
{
ILNode *varNode;
ILNode_ListIter iter;
/* Scan through the variable names and declare them in the current scope */
ILNode_ListIter_Init(&iter, resource->variables);
while((varNode = ILNode_ListIter_Next(&iter)) != 0)
{
ILNode *initNode;
ILType *newType;
initNode = varNode;
newType = HandleLocalDecl(initNode, info, &initNode, type, 1);
if(yyisa(initNode, ILNode_VariableDeclarator))
{
ILNode_VariableDeclarator *declNode;
ILNode_LocalVar *localNode;
ILNode_Using *using;
declNode = (ILNode_VariableDeclarator *)(initNode);
localNode = (ILNode_LocalVar *)(declNode->name);
StmtSem(initNode, info, &initNode);
if(stmtParent)
{
using = (ILNode_Using *)ILNode_Using_create(localNode,
initNode,
*stmtParent,
newType);
*stmtParent = (ILNode *)using;
}
else
{
using = (ILNode_Using *)ILNode_Using_create(localNode,
initNode,
node->stmt,
newType);
node->resource = (ILNode *)using;
}
stmtParent = &(using->stmt);
}
else
{
/* In using statements the variables shall have an initializer */
stmtParent = 0;
}
}
}
else
{
ILType *newType;
newType = HandleLocalDecl(resource->variables, info,
&(resource->variables), type, 1);
if(yyisa(resource->variables, ILNode_VariableDeclarator))
{
ILNode_VariableDeclarator *declNode;
ILNode *initNode;
ILNode_LocalVar *varNode;
ILNode_Using *using;
declNode = (ILNode_VariableDeclarator *)(resource->variables);
initNode = resource->variables;
varNode = (ILNode_LocalVar *)(declNode->name);
StmtSem(initNode, info, &(initNode));
using = (ILNode_Using *)ILNode_Using_create(varNode,
initNode,
node->stmt,
newType);
stmtParent = &(using->stmt);
node->resource = (ILNode *)using;
}
else
{
/* In using statements the variables shall have an initializer */
stmtParent = 0;
}
}
}
else
{
ILType *type;
CSSemValue value;
value = ILNode_SemAnalysis(node->resource, info, &(node->resource));
type = CSSemGetType(value);
if(!type)
{
CCErrorOnLine(yygetfilename(node->resource),
yygetlinenum(node->resource),
"invalid expression");
stmtParent = 0;
}
else
{
unsigned long varNum;
ILNode_MethodDeclaration *method;
ILNode_LocalVar *varNode;
ILNode *initNode;
ILNode_Using *using;
CheckIfDisposable(info, node->resource, type);
/* Locate the method that this local is declared within */
method = (ILNode_MethodDeclaration *)(info->currentMethod);
/* Add the type to the local variable signature for this method */
varNum = AddLocalToMethod(info, method, type);
/* create a nameless local variable */
varNode = (ILNode_LocalVar *)ILNode_LocalVar_create(varNum,
ILTypeToMachineType(type));
initNode = ILNode_SemGuard_create(node->resource, value);
initNode = ILNode_Assign_create((ILNode *)varNode, initNode);
/* Perform semantic analysis on the initializer */
ILNode_SemAnalysis(initNode, info, &initNode);
using = (ILNode_Using *)ILNode_Using_create(varNode,
initNode,
node->stmt,
type);
stmtParent = &(using->stmt);
node->resource = (ILNode *)using;
}
}
/* Now perform the semantic analysis on the statement itself */
if(stmtParent)
{
ILNode_SemAnalysis(*stmtParent, info, stmtParent);
}
else
{
ILNode_SemAnalysis(node->stmt, info, &(node->stmt));
}
return CSSemValueDefault;
}
/*
* Generate discard code for the "using" statement.
*/
ILNode_GenDiscard(ILNode_UsingStatement)
{
ILNode_GenDiscard(node->resource, info);
}
/*
* Perform semantic analysis for a "using" node.
*/
ILNode_SemAnalysis(ILNode_Using)
{
return CSSemValueDefault;
}
/*
* Generate discard code for the "using" node.
*/
ILNode_GenDiscard(ILNode_Using)
{
ILLabel label = ILLabel_Undefined;
ILLabel label1 = ILLabel_Undefined;
/* First run the initializer for the local */
ILNode_GenDiscard(node->init, info);
/* Encapsulate the statement in a "try" block so that the "Dispose"
method can be called in finally */
if(info->asmOutput)
{
fputs("\t.try {\n", info->asmOutput);
}
ILGenPushTry(info);
/* Generate the dicard code for the statement */
ILNode_GenDiscard(node->stmt, info);
/* Leave the try block */
ILGenJump(info, IL_OP_LEAVE, &label);
/* end the try block and start a finally block */
ILGenPopTry(info);
if(info->asmOutput)
{
fputs("\t} finally {\n", info->asmOutput);
}
/* Generate the dispose code */
/* TODO: handle value types */
ILGenLoadLocal(info, node->localVar->index);
ILGenAdjust(info, 1);
ILGenJump(info, IL_OP_BRFALSE, &label1);
ILGenAdjust(info,-1);
ILGenLoadLocal(info, node->localVar->index);
ILGenAdjust(info, 1);
ILGenCallVirtual(info,
"void [.library]System.IDisposable::Dispose()");
ILGenAdjust(info, -1);
/* Leave the finally block */
ILGenLabel(info, &label1);
ILGenSimple(info, IL_OP_ENDFINALLY);
if(info->asmOutput)
{
fputs("\t}\n",info->asmOutput);
}
ILGenLabel(info, &label);
}
/*
* Generate Java discard code for the "using" statement.
*/
JavaGenDiscard(ILNode_UsingStatement)
{
JavaGenDiscard(node->resource, info);
}
/*
* Generate Java discard code for the "using" node.
*/
JavaGenDiscard(ILNode_Using)
{
/* TODO */
}
/*
* Determine if a "using" statement ends in a return statement.
*/
ILNode_EndsInReturnImpl(ILNode_UsingStatement)
{
return ILNode_EndsInReturnImpl(node->stmt,info);
}
/*
* Perform semantic analysis for the "unsafe" statement.
*/
ILNode_SemAnalysis(ILNode_Unsafe)
{
CCUnsafeEnter(info, (ILNode *)node, "unsafe statement");
StmtSem(node->stmt, info, &(node->stmt));
CCUnsafeLeave(info);
return CSSemValueDefault;
}
/*
* Generate discard code for the "unsafe" statement.
*/
ILNode_GenDiscard(ILNode_Unsafe)
{
ILNode_GenDiscard(node->stmt, info);
}
/*
* Generate Java discard code for the "unsafe" statement.
*/
JavaGenDiscard(ILNode_Unsafe)
{
JavaGenDiscard(node->stmt, info);
}
/*
* Determine if an "unsafe" statement ends in a return statement.
*/
ILNode_EndsInReturnImpl(ILNode_Unsafe)
{
return ILNode_EndsInReturnImpl(node->stmt,info);
}
/*
* Perform semantic analysis for the "fixed" statement.
*/
ILNode_SemAnalysis(ILNode_Fixed)
{
ILType *type;
const char *name;
ILNode_MethodDeclaration *method;
ILNode *errorNode;
ILNode_ListIter iter;
ILNode *varNode;
ILNode *nameNode;
ILScopeData *data;
/* Locate the method that this local is declared within */
method = (ILNode_MethodDeclaration *)(info->currentMethod);
/* TODO */
CCUnsafeMessage(info, (ILNode *)node, "unsafe `fixed' statement");
if(info->outputIsJava)
{
CCErrorOnLine(yygetfilename(node), yygetlinenum(node),
"`fixed' disallowed when compiling to Java bytecode");
return CSSemValueDefault;
}
/* Perform semantic analysis on the local variable type */
type = CSSemType(node->type, info, &(node->type));
if(!ILType_IsPointer(type))
{
CCErrorOnLine(yygetfilename(node->type), yygetlinenum(node->type),
"only pointer types can be declared in fixed statements");
}
/* Scan through the variable names and declare them in the current scope */
ILNode_ListIter_Init(&iter, node->decls);
while((varNode = ILNode_ListIter_Next(&iter)) != 0)
{
nameNode=((ILNode_FixedVariable*)varNode)->name;
name = ILQualIdentName(nameNode,0);
data = ILScopeLookup(info->currentScope, name, 0);
if(data)
{
/* The name is already declared in this scope */
CCErrorOnLine(yygetfilename(nameNode), yygetlinenum(nameNode),
"`%s' is already declared in this scope", name);
errorNode = ILScopeDataGetNode(data);
if(errorNode)
{
CCErrorOnLine(yygetfilename(errorNode), yygetlinenum(errorNode),
"previous declaration here");
}
}
else
{
unsigned long varNum;
if(yyisa(varNode,ILNode_FixAddress))
{
/* TODO : figure the right way to pin variables */
}
/* Add the type to the local variable signature for this method */
varNum = AddLocalToMethod(info, method, type);
/* Create a local variable entry in the current scope */
ILScopeDeclareLocal(info->currentScope, name, varNum,
((ILNode_FixedVariable*)varNode)->name);
info->inFixed = -1;
ILNode_SemAnalysis(varNode,info,iter.last);
info->inFixed = 0;
}
}
ILNode_SemAnalysis(node->stmt,info,&(node->stmt));
return CSSemValueDefault;
}
/*
* Generate discard code for the "fixed" statement.
*/
ILNode_GenDiscard(ILNode_Fixed)
{
ILNode_GenDiscard(node->decls,info);
ILNode_GenDiscard(node->stmt,info);
}
/*
* Generate Java discard code for the "fixed" statement.
*/
JavaGenDiscard(ILNode_Fixed)
{
/* Nothing to do here: `fixed' is illegal for Java code */
}
/*
* Determine if a "fixed" statement ends in a return statement.
*/
ILNode_EndsInReturnImpl(ILNode_Fixed)
{
return ILNode_EndsInReturnImpl(node->stmt,info);
}
/*
* Perform semantic analysis for the "fix address" node.
*/
ILNode_SemAnalysis(ILNode_FixAddress)
{
*parent = HandleFixedExpr(info, node->name, node->address,
&(node->address), 1);
/* Perform semantic analysis on the assignment */
ILNode_SemAnalysis(*parent, info, parent);
return CSSemValueDefault;
}
/*
* Perform semantic analysis for the "fix expression" node.
*/
ILNode_SemAnalysis(ILNode_FixExpr)
{
*parent = HandleFixedExpr(info, node->name, node->expr, &(node->expr), 0);
/* Perform semantic analysis on the assignment */
ILNode_SemAnalysis(*parent, info, parent);
return CSSemValueDefault;
}
/*
* Perform semantic analysis for a variable declaration.
*/
ILNode_SemAnalysis(ILNode_VariableDeclarator)
{
if(node->init)
{
/*
* if we have an initializer then replace this node by an assignment
*/
*parent = ILNode_Assign_create(node->name, node->init);
/*
* and perform semantic analysis on the assignment node.
*/
return ILNode_SemAnalysis(*parent, info, parent);
}
/* Return the default value to the caller */
return CSSemValueDefault;
}
/*
* Perform semantic analysis for local variable declarations.
*/
ILNode_SemAnalysis(ILNode_LocalVarDeclaration)
{
ILType *type;
type = FindLocalType(node->type, info, &(node->type), 0);
if(yyisa(node->varNames, ILNode_List))
{
ILNode *varNode;
ILNode_ListIter iter;
/* Scan through the variable names and declare them in the current scope */
ILNode_ListIter_Init(&iter, node->varNames);
while((varNode = ILNode_ListIter_Next(&iter)) != 0)
{
ILNode *replace;
replace = varNode;
HandleLocalDecl(varNode, info, &replace, type, 0);
if(yyisa(replace, ILNode_VariableDeclarator))
{
StmtSem(replace, info, &replace);
}
if(replace != varNode)
{
*(iter.last) = replace;
}
}
}
else
{
HandleLocalDecl(node->varNames, info, &(node->varNames), type, 0);
if(yyisa(node->varNames, ILNode_VariableDeclarator))
{
StmtSem(node->varNames, info, &(node->varNames));
}
}
/* Return the default value to the caller */
return CSSemValueDefault;
}
/*
* Perform semantic analysis for local constant declarations.
*/
ILNode_SemAnalysis(ILNode_LocalConstDeclaration)
{
ILNode_ListIter iterator;
ILNode_FieldDeclarator *decl;
/* since Rhys said that this was a reuse of FieldDecl */
CSSemValue value;
ILEvalValue evalValue;
ILType *type = CSSemType(node->type, info, &(node->type));
const char *name;
ILScopeData *data;
ILNode_MethodDeclaration *method;
ILNode *errorNode;
/* Locate the method that this local is declared within */
method = (ILNode_MethodDeclaration *)(info->currentMethod);
/* Perform semantic analysis on the local variable type */
type = CSSemType(node->type, info, &(node->type));
ILNode_ListIter_Init(&iterator, node->decls);
while((decl = (ILNode_FieldDeclarator *)
ILNode_ListIter_Next(&iterator)) != 0)
{
name = ILQualIdentName(decl->name, 0);
if(!(decl->initializer) || !(decl->name))
{
CCErrorOnLine(yygetfilename(decl->initializer),
yygetlinenum(decl->initializer),
"no initializer value for const");
continue;
}
/* Perform semantic analysis on the initializer value */
value = ILNode_SemAnalysis(decl->initializer, info,
&(decl->initializer));
if(!CSSemIsValue(value))
{
CCErrorOnLine(yygetfilename(decl->initializer),
yygetlinenum(decl->initializer),
"initializer is not a value");
continue;
}
/* Make sure that the value is compatible with the field's type */
if(!ILCoerce(info, decl->initializer, &(decl->initializer),
CSSemGetType(value), type,1))
{
CCErrorOnLine(yygetfilename(decl->initializer),
yygetlinenum(decl->initializer),
"incompatible types in initialization: "
"no conversion from `%s' to `%s'",
CSTypeToName(CSSemGetType(value)),
CSTypeToName(type));
continue;
}
/* Check that the literal value is actually constant */
if(!ILNode_EvalConst(decl->initializer, info, &evalValue) ||
!ILGenCastConst(info, &evalValue, evalValue.valueType,
ILTypeToMachineType(type)))
{
CCErrorOnLine(yygetfilename(decl->initializer),
yygetlinenum(decl->initializer),
"initializer is not constant");
continue;
}
/* Recreate the value with the real type instead of using the type
of the expression */
CSSemSetValueKind(value, CSSemGetKind(value), type);
/* Guard the node from re-evaluation */
decl->initializer=ILNode_SemGuard_create(decl->initializer,value);
data = ILScopeLookup(info->currentScope, name, 0);
if(data)
{
/* The name is already declared in this scope */
CCErrorOnLine(yygetfilename(decl->name), yygetlinenum(decl->name),
"`%s' is already declared in this scope", name);
errorNode = ILScopeDataGetNode(data);
if(errorNode)
{
CCErrorOnLine(yygetfilename(errorNode), yygetlinenum(errorNode),
"previous declaration here");
}
}
else
{
/* Create a local variable entry in the current scope */
ILScopeDeclareLocalConst(info->currentScope, name,
decl->initializer,decl->name);
}
}
return CSSemValueDefault;
}
/*
* Perform semantic analysis for line number information blocks.
*/
ILNode_SemAnalysis(ILNode_LineInfo)
{
return ILNode_SemAnalysis(node->stmt, info, &(node->stmt));
}
/*
* Perform semantic analysis for an event add or remove operation.
*/
ILNode_SemAnalysis(ILNode_EventAddOrRemove)
{
/* Nothing to do here: this node is generated during semantic
analysis of the "+=" and "-=" operators */
return CSSemValueDefault;
}
/*
* Perform semantic analysis for a memcpy statement.
*/
ILNode_SemAnalysis(ILNode_Memcpy)
{
/* Nothing to do here: this node is generated during semantic
analysis of an invokation expression */
return CSSemValueDefault;
}
/*
* Perform semantic analysis for a memset statement.
*/
ILNode_SemAnalysis(ILNode_Memset)
{
/* Nothing to do here: this node is generated during semantic
analysis of an invokation expression */
return CSSemValueDefault;
}
/*
* Perform semantic analysis for a resource declaration.
*/
ILNode_SemAnalysis(ILNode_ResourceDeclaration)
{
/*
* This is handled completely in semanalyzing an ILNode_UsingStatement.
*/
return CSSemValueDefault;
}
ILNode_GenDiscard(ILNode_ResourceDeclaration)
{
ILNode_GenDiscard(node->variables, info);
}
|