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
|
/* SPDX-License-Identifier: copyleft-next-0.3.1 */
/* Copyright 2023 Kim Kuparinen < kimi.h.kuparinen@gmail.com > */
/**
* @file actualize.c
*
* Implementations for stuff actualization.
*/
#include <assert.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <ek/actualize.h>
#include <ek/compiler.h>
#include <ek/debug.h>
#define UNUSED(x) do { (void)(x); } while (0)
struct act_stack {
struct ast *node;
struct act_stack *next;
};
enum act_flags {
ACT_IN_LOOP = (1 << 0),
ACT_IN_SWITCH = (1 << 1),
ACT_HAS_RETURN = (1 << 2),
};
struct act_state {
enum act_flags flags;
struct ast *cur_proc;
struct act_stack *defer_stack;
struct act_stack *goto_stack;
struct act_stack *label_stack;
};
static int replace_type_id(struct ast *nodes, char *id,
struct type *replacement);
static int actualize(struct act_state *state, struct scope *scope,
struct ast *node);
static int actualize_list(struct act_state *state, struct scope *scope,
struct ast *l);
static int actualize_type(struct act_state *state, struct scope *scope,
struct type *node);
static int actualize_type_list(struct act_state *state, struct scope *scope,
struct type *node);
static int is_lvalue(struct ast *n)
{
if (n->k == AST_ARR)
return 1;
if (n->k == AST_ID)
return 1;
if (n->k == AST_DEREF)
return 1;
return 0;
}
static void act_set_flags(struct act_state *state, enum act_flags flags)
{
state->flags |= flags;
}
static enum act_flags act_flags(struct act_state *state, enum act_flags flags)
{
return state->flags & flags;
}
static bool is_void(struct type *t)
{
/* missing return is void */
if (!t)
return 1;
return t->k == TYPE_VOID;
}
static struct type *void_type()
{
return tgen_primitive(TYPE_VOID, strdup("void"), NULL, NULL_LOC());
}
static struct type *i27_type(struct scope *scope)
{
struct ast *def = file_scope_find_type(scope, "i27");
if (!def) {
error("missing definition of type 'i27'");
return NULL;
}
return tgen_primitive(TYPE_I27, strdup("i27"), def, def->loc);
}
static struct type *i9_type(struct scope *scope)
{
struct ast *def = file_scope_find_type(scope, "i9");
if (!def) {
error("missing definition of type 'i9'");
return NULL;
}
return tgen_primitive(TYPE_I9, strdup("i9"), def, def->loc);
}
static struct type *str_type(struct scope *scope)
{
struct ast *def = file_scope_find_type(scope, "str");
if (!def) {
error("missing definition of type 'str'");
return NULL;
}
return tgen_primitive(TYPE_STR, strdup("str"), def, def->loc);
}
static struct type *bool_type(struct scope *scope)
{
struct ast *def = file_scope_find_type(scope, "bool");
if (!def) {
error("missing definition of type 'bool'");
return NULL;
}
return tgen_primitive(TYPE_BOOL, strdup("bool"), def, def->loc);
}
static int push_defer(struct act_state *state, struct ast *expr)
{
struct act_stack *new = calloc(1, sizeof(struct act_stack));
if (!new) {
internal_error("failed allocating defer");
return -1;
}
new->node = expr;
new->next = state->defer_stack;
state->defer_stack = new;
return 0;
}
static int push_label(struct act_state *state, struct ast *label)
{
struct act_stack *new = calloc(1, sizeof(struct act_stack));
if (!new) {
internal_error("failed allocating label");
return -1;
}
new->node = label;
new->next = state->label_stack;
state->label_stack = new;
return 0;
}
static struct ast *find_label(struct act_state *state, char *label)
{
struct act_stack *prev = state->label_stack, *cur;
if (prev)
do {
cur = prev->next;
if (same_id(label_id(prev->node), label))
return prev->node;
} while ((prev = cur));
return NULL;
}
static int push_goto(struct act_state *state, struct ast *got)
{
struct act_stack *new = calloc(1, sizeof(struct act_stack));
if (!new) {
internal_error("failed allocating goto");
return -1;
}
new->node = got;
new->next = state->goto_stack;
state->goto_stack = new;
return 0;
}
static struct ast *clone_defers(struct act_state *state,
struct act_stack *to)
{
struct act_stack *from = state->defer_stack;
/* maintain reverse order */
struct ast *defers = NULL, *prev = NULL;
while (from != to) {
struct ast *defer = clone_ast(from->node);
if (prev)
prev->n = defer;
if (!defers)
defers = defer;
prev = defer;
from = from->next;
}
return defers;
}
static void clear_stack(struct act_stack *from, struct act_stack *to)
{
while (from != to) {
struct act_stack *next = from->next;
free(from);
from = next;
}
}
static void clear_defers(struct act_state *state, struct act_stack *to)
{
struct act_stack *from = state->defer_stack;
clear_stack(from, to);
state->defer_stack = to;
}
static void clear_gotos(struct act_state *state, struct act_stack *to)
{
struct act_stack *from = state->goto_stack;
clear_stack(from, to);
state->goto_stack = to;
}
static void clear_labels(struct act_state *state, struct act_stack *to)
{
struct act_stack *from = state->label_stack;
clear_stack(from, to);
state->label_stack = to;
}
static void destroy_act_state(struct act_state *state)
{
clear_defers(state, NULL);
clear_labels(state, NULL);
clear_gotos(state, NULL);
}
static void type_mismatch(struct scope *scope, char *s, struct ast *node, struct type *a, struct type *b)
{
char *left_type = type_str(a);
char *right_type = type_str(b);
semantic_error(scope->fctx, node,
"%s: %s vs %s",
s, left_type, right_type);
free(left_type);
free(right_type);
}
static int analyze(struct scope *scope, struct ast *tree);
static int eval_const_if(struct scope *scope, struct ast *node)
{
assert(node
&& node->k == AST_IF
&& ast_flags(node, AST_FLAG_CONST));
semantic_error(scope->fctx, node,
"const if unimplemented");
return -1;
}
static int analyze_visibility(struct scope *scope, struct ast *node)
{
if (!node)
return 0;
node->scope = scope;
switch (node->k) {
case AST_PROC_DEF: return scope_add_proc(scope, node);
case AST_MACRO_DEF: return scope_add_macro(scope, node);
case AST_VAR_DEF: return scope_add_var(scope, node);
case AST_IMPORT: {
const char *file = import_file(node);
return process_file(&scope,
(int)ast_flags(node, AST_FLAG_PUBLIC),
file);
}
case AST_IF: {
assert(ast_flags(node, AST_FLAG_CONST));
if (eval_const_if(scope, node))
return -1;
/* since a const if likely replaced the current node with
* something else, we have to analyze the replacement */
return analyze(scope, node);
}
case AST_STRUCT_DEF: {
/* we shouldn't get any anonymous structs at this stage */
char *id = struct_id(node);
return scope_add_type(scope, id, node);
}
case AST_ENUM_DEF: {
char *id = enum_id(node);
return scope_add_type(scope, id, node);
}
case AST_ALIAS_DEF: {
char *id = alias_id(node);
return scope_add_type(scope, id, node);
}
case AST_TRAIT_DEF: {
char *id = trait_id(node);
return scope_add_type(scope, id, node);
}
case AST_EMPTY: {
break;
}
default:
semantic_error(scope->fctx, node, "unknown top element");
return -1;
};
return 0;
}
static int analyze_var(struct scope *scope, struct ast *node)
{
struct act_state state = {0};
return actualize(&state, scope, node);
}
static void set_type(struct ast *node, struct type *type)
{
node->t = clone_type_list(type);
}
/* important to keep in mind that this does not maintain list state, must be
* done elsewhere */
static void replace_type(struct type *t, struct type *r)
{
/* free strings before they get overwritten */
if (t->id)
free(t->id);
struct src_loc loc = t->loc;
struct scope *scope = t->scope;
*t = *r;
/* clone so we don't accidentally free same string twice later */
if (t->id)
t->id = strdup(t->id);
/* these things we generally don't want to replace */
t->loc = loc;
t->scope = scope;
}
static void replace_ast(struct ast *n, struct ast *t)
{
if (n->s)
free(n->s);
struct src_loc loc = n->loc;
struct scope *scope = n->scope;
*n = *t;
if (n->s)
n->s = strdup(n->s);
n->loc = loc;
t->scope = scope;
}
static int analyze_proc(struct scope *scope, struct ast *node)
{
struct scope *proc_scope = create_scope();
scope_add_scope(scope, proc_scope);
node->scope = proc_scope;
struct ast *params = proc_params(node);
struct act_state state = {0};
if (actualize_list(&state, proc_scope, params))
return -1;
struct type *rtype = proc_rtype(node);
if (actualize_type_list(&state, proc_scope, rtype))
return -1;
if (!rtype)
proc_rtype(node) = void_type();
struct type *callable = tgen_callable(NULL, proc_rtype(node), node->loc);
foreach_node(p, params) {
/* we must manually 'start' the chain **/
if (!callable_ptypes(callable)) {
callable_ptypes(callable) = clone_type(p->t);
continue;
}
type_append(callable_ptypes(callable), p->t);
}
set_type(node, callable);
return 0;
}
static struct ast *analyze_type_expand(struct scope *scope,
struct ast *n)
{
assert(n->k == AST_TYPE_EXPAND);
struct ast *trait = file_scope_find_type(scope, type_expand_id(n));
if (!trait) {
semantic_error(scope->fctx, n, "no such type");
return NULL;
}
if (trait->k != AST_TRAIT_DEF) {
semantic_error(scope->fctx, n, "not a trait");
return NULL;
}
semantic_info(scope->fctx, n,
"FIXME: skipping type param check for now");
struct ast *body = trait_raw_body(trait);
body = clone_ast(body);
struct type *pa = type_expand_args(n);
foreach_node(pt, trait_params(trait)) {
replace_type_id(body, var_id(pt), pa);
pa = pa->n;
}
return body;
}
static int implements_trait(struct ast *body, char *id)
{
foreach_node(n, body) {
/* traits don't currently take generic parameters I guess? */
if (n->k != AST_ID)
continue;
if (same_id(id_str(n), id))
return 1;
}
return 0;
}
static int analyze_struct(struct scope *scope, struct ast *node)
{
assert(node->k == AST_STRUCT_DEF);
struct ast *params = struct_params(node);
struct scope *struct_scope = create_scope();
if (!struct_scope)
return -1;
scope_add_scope(node->scope, struct_scope);
node->scope = struct_scope;
if (params)
ast_set_flags(node, AST_FLAG_GENERIC);
struct type *type = tgen_type(TYPE_STRUCT,
NULL, NULL,
node, NULL,
strdup(struct_id(node)),
node->loc);
foreach_node(n, struct_body(node)) {
if (n->k != AST_TYPE_EXPAND)
continue;
if (implements_trait(struct_body(node), type_expand_id(n))) {
n->k = AST_EMPTY;
continue;
}
if (same_id(struct_id(node), type_expand_id(n))) {
semantic_error(scope->fctx, n,
"recursive trait implementations not allowed");
return -1;
}
struct ast *body = analyze_type_expand(scope, n);
if (!body) {
n->k = AST_EMPTY;
continue;
}
replace_type_id(body, type_expand_id(n), type);
ast_block_last(body)->n = n->n;
n->n = body;
}
foreach_node(n, struct_body(node)) {
switch (n->k) {
case AST_EMPTY: continue;
case AST_ID: continue;
/* prototypes are checked later */
case AST_PROC_DEF: if (!proc_body(n)) continue;
default:
}
if (analyze_visibility(struct_scope, n))
return -1;
}
foreach_node(n, struct_body(node)) {
/* also checks prototypes */
if (n->k != AST_PROC_DEF)
continue;
if (analyze_proc(struct_scope, n))
return -1;
}
/* check that all prototypes are implemented */
foreach_node(n, struct_body(node)) {
if (n->k != AST_PROC_DEF)
continue;
if (proc_body(n))
continue;
struct ast *proc = scope_find_proc(struct_scope, proc_id(n));
if (!proc) {
semantic_error(scope->fctx, n,
"missing implementation");
return -1;
}
if (!types_match(n->t, proc->t)) {
semantic_error(scope->fctx, n, "mismatched signatures");
semantic_info(scope->fctx, proc, "note: here");
return -1;
}
}
char *id = strdup(struct_id(node));
if (same_id(id, "i27"))
node->t = tgen_primitive(TYPE_I27, id, node, node->loc);
else if (same_id(id, "i9"))
node->t = tgen_primitive(TYPE_I9, id, node, node->loc);
else if (same_id(id, "bool"))
node->t = tgen_primitive(TYPE_BOOL, id, node, node->loc);
else
node->t = tgen_struct(id, node, node->loc);
/** @todo there is the possibility that two different traits add the
* same prototype, which is reported in traits but not structs? */
return 0;
}
/* quite a lot of overlap with analyze_struct, kind of ugly I guess */
static int analyze_trait(struct scope *scope, struct ast *node)
{
assert(node->k == AST_TRAIT_DEF);
struct ast *params = trait_params(node);
struct scope *trait_scope = create_scope();
if (!trait_scope)
return -1;
scope_add_scope(node->scope, trait_scope);
node->scope = trait_scope;
/** @todo should probably add in aliases for the traits in scope? */
if (params)
ast_set_flags(node, AST_FLAG_GENERIC);
struct type *type = tgen_type(TYPE_TRAIT, NULL, NULL,
node, NULL, strdup(trait_id(node)),
node->loc);
/* copy body */
node->a2 = clone_ast(trait_raw_body(node));
/* do type expansions */
foreach_node(n, trait_body(node)) {
if (n->k != AST_TYPE_EXPAND)
continue;
/* don't re-expand already implemented traits */
if (implements_trait(trait_body(node), type_expand_id(n))) {
/* not sure about this, but at least we don't have stray
* type expands everywhere */
n->k = AST_EMPTY;
continue;
}
if (same_id(trait_id(node), type_expand_id(n))) {
semantic_error(scope->fctx, n,
"recursive trait implementations not allowed");
return -1;
}
struct ast *body = analyze_type_expand(scope, n);
if (!body) {
n->k = AST_EMPTY;
continue;
}
replace_type_id(body, type_expand_id(n), type);
ast_last(body)->n = n->n;
n->n = body;
}
/* add all procedure definitions to scope */
foreach_node(n, trait_body(node)) {
/* kind of a hack but these shouldn't be shown to
* analyze_visibility */
switch (n->k) {
case AST_EMPTY: continue;
case AST_ID: continue;
/* prototypes are added later */
case AST_PROC_DEF: if (!proc_body(n)) continue;
default:
}
if (analyze_visibility(trait_scope, n))
return -1;
}
/** @todo I should really check that there's just one prototype and one
* implementation of that prototype, not sure what the best approach
* would be. Add a prototypes -list to scopes? */
/* add all prototypes that don't have matching definition to scope */
foreach_node(n, trait_body(node)) {
if (n->k != AST_PROC_DEF)
continue;
if (proc_body(n))
continue;
/* prototypes are checked only if there's no implementation */
if (scope_find_proc(trait_scope, proc_id(n)))
continue;
if (analyze_visibility(trait_scope, n))
return -1;
}
foreach_node(n, trait_body(node)) {
if (n->k != AST_PROC_DEF)
continue;
if (analyze_proc(trait_scope, n))
return -1;
}
node->t = tgen_trait(strdup(struct_id(node)), node, node->loc);
return 0;
}
static int analyze_signs(struct scope *scope, struct ast *node)
{
/** @todo aliases? */
switch (node->k) {
case AST_VAR_DEF: return analyze_var(scope, node); break;
case AST_PROC_DEF: return analyze_proc(scope, node); break;
case AST_STRUCT_DEF: return analyze_struct(scope, node); break;
case AST_TRAIT_DEF: return analyze_trait(scope, node); break;
default:
}
return 0;
}
static int analyze(struct scope *scope, struct ast *tree)
{
foreach_node(node, tree) {
if (analyze_visibility(scope, node))
return -1;
}
foreach_node(node, tree) {
if (analyze_signs(scope, node))
return -1;
}
foreach_node(node, tree){
struct act_state state = {0};
if (actualize(&state, scope, node))
return -1;
printf("//actualized:\n");
ast_dump(0, node);
}
return 0;
}
int analyze_root(struct scope *scope, struct ast *tree)
{
if (analyze(scope, tree))
return -1;
return 0;
}
static int structs_match(struct type *a, struct type *b)
{
/* dunno, let's go with this for now */
return a->d == b->d;
}
int types_match(struct type *a, struct type *b)
{
if (!a && !b)
return 1;
if (!a || !b)
return 0;
/* if the type kind doesn't match, we're done. */
if (a->k != b->k)
return 0;
if (a->k == TYPE_STRUCT)
return structs_match(a, b);
if (a->k == TYPE_PTR)
return types_match(ptr_base(a), ptr_base(b));
return 1;
}
static int _replace_id(struct ast *node, void *data)
{
if (!node)
return 0;
if (node->k != AST_ID)
return 0;
struct ast **pair = data;
struct ast *id = pair[0];
struct ast *expr = pair[1];
/* no match, continue */
if (!same_id(id_str(node), id_str(id)))
return 0;
struct ast *clone = clone_ast(expr);
if (!clone) {
internal_error("failed cloning replacement expr");
return -1;
}
clone->n = node->n;
clone->scope = node->scope;
replace_ast(node, clone);
/* a succesful replacement needs no futher replacements, I think */
return 0;
}
static int replace_id(struct ast *body, struct ast *id,
struct ast *expr)
{
struct ast *pair[2] = {id, expr};
return ast_visit(_replace_id, NULL, body, pair);
}
static int actualize_macro_def(struct act_state *state,
struct scope *scope, struct ast *n)
{
UNUSED(state);
/* macro bodies, arguments, etc aren't expanded upon until the macro is
* called, so just try to add it to the local scope */
assert(n && n->k == AST_MACRO_DEF);
return scope_add_macro(scope, n);
}
static int actualize_macro_expand(struct act_state *state,
struct scope *scope,
struct ast *macro_expand)
{
assert(macro_expand->k == AST_MACRO_EXPAND);
char *id = macro_expand_id(macro_expand);
struct ast *macro = file_scope_find_macro(scope, id);
if (!macro) {
semantic_error(scope->fctx, macro_expand, "no such macro");
return -1;
}
assert(macro->k == AST_MACRO_DEF);
if (ast_flags(macro, AST_FLAG_VARIADIC)) {
semantic_error(scope->fctx, macro,
"variadic macros not yet implemented");
return -1;
}
struct ast *body = clone_ast(macro_def_body(macro));
if (!body) {
internal_error("failed allocating body for macro expansion");
return -1;
}
/** @todo update all macro IDs to the correct scope */
body->scope = macro_expand->scope;
body->n = macro_expand->n;
struct ast *param = macro_def_params(macro);
struct ast *arg = macro_expand_args(macro_expand);
while (param && arg) {
/* feels slightly hacky, but essentially replace each individual
* component in the arg list by breaking it out of the list
* temporarily. After the replacement, insert it back into the
* list so the cleanup is easier. */
struct ast *next_arg = arg->n;
arg->n = NULL;
if (replace_id(body, param, arg)) {
semantic_error(scope->fctx, macro_expand,
"failed replacing params with args");
arg->n = next_arg;
return -1;
}
param = param->n;
arg = arg->n = next_arg;
}
free(macro_expand_id(macro_expand));
replace_ast(macro_expand, body);
/* actualize the new content */
return actualize(state, scope, macro_expand);
}
static int simplify_refderef(struct act_state *state, struct scope *scope, struct ast *n)
{
if (!is_unop(n))
return 0;
struct ast *e = unop_expr(n);
if (n->k == AST_REF && e->k == AST_DEREF) {
replace_ast(n, unop_expr(e));
return simplify_refderef(state, scope, n);
}
if (n->k == AST_DEREF && e->k == AST_REF) {
replace_ast(n, unop_expr(e));
return simplify_refderef(state, scope, n);
}
return 0;
}
static int maybe_ufcs(struct act_state *state, struct scope *scope, struct ast *call)
{
assert(call->k == AST_CALL);
struct ast *dot = call_expr(call);
if (dot->k != AST_DOT)
return 0;
struct ast *expr = dot_expr(dot);
call_expr(call) = gen_fetch(dot_id(dot), clone_type(expr->t), dot->loc);
/* hard core type */
struct ast *ref = gen_unop(AST_REF, expr, dot->loc);
ref->t = tgen_ptr(clone_type(expr->t), dot->loc);
ref->scope = scope;
if (simplify_refderef(state, scope, ref))
return -1;
ast_prepend(call_args(call), ref);
return 0;
}
static int actualize_call(struct act_state *state,
struct scope *scope, struct ast *call)
{
assert(call && call->k == AST_CALL);
if (actualize_list(state, scope, call_args(call)))
return -1;
if (actualize_list(state, scope, call_expr(call)))
return -1;
struct ast *expr = call_expr(call);
if (expr->t->k != TYPE_CALLABLE) {
char *tstr = type_str(expr->t);
semantic_error(scope->fctx, call, "not a callable type: %s",
tstr);
free(tstr);
return -1;
}
if (maybe_ufcs(state, scope, call))
return -1;
struct type *callable = expr->t;
struct type *ptypes = callable_ptypes(callable);
struct ast *arg = call_args(call);
foreach_type(p, ptypes) {
if (!arg) {
semantic_error(scope->fctx, call, "too many arguments");
return -1;
}
if (!types_match(p, arg->t)) {
type_mismatch(scope, "argument type mismatch", arg, p, arg->t);
return -1;
}
arg = arg->n;
}
if (arg) {
semantic_error(scope->fctx, arg, "too many arguments");
return -1;
}
set_type(call, callable_rtype(callable));
return 0;
}
static void warn_unused_labels(struct act_state *state, struct scope *scope)
{
struct act_stack *labels = state->label_stack;
if (!labels)
return;
do {
struct ast *label = labels->node;
if (!ast_flags(label, AST_FLAG_ACTUAL))
semantic_warn(scope->fctx, label,
"unused label");
} while ((labels = labels->next));
}
static int undefined_gotos(struct act_state *state, struct scope *scope)
{
int ret = 0;
struct act_stack *gotos = state->goto_stack;
if (!gotos)
return ret;
do {
struct ast *got = gotos->node;
if (!ast_flags(got, AST_FLAG_ACTUAL)) {
semantic_warn(scope->fctx, got,
"undefined label");
ret = -1;
}
} while ((gotos = gotos->next));
return ret;
}
static int actualize_proc(struct act_state *state,
struct scope *scope, struct ast *proc)
{
UNUSED(state);
/* actualize_proc is called on trait procs as well, but I believe
* that's fine? */
assert(proc && proc->k == AST_PROC_DEF);
struct act_state new_state = {0};
/* params should already have been actualized, should maybe check */
/* actualize body */
new_state.cur_proc = proc;
if (actualize(&new_state, proc->scope, proc_body(proc))) {
destroy_act_state(&new_state);
return -1;
}
if (!act_flags(&new_state, ACT_HAS_RETURN)) {
if (!is_void(proc_rtype(proc))) {
semantic_error(scope->fctx, proc,
"no return with non-void return type");
destroy_act_state(&new_state);
return -1;
}
/* add 'implicit' return */
struct ast *body = proc_body(proc);
struct ast *r = gen_return(NULL, NULL, NULL_LOC());
r->scope = body->scope;
ast_append(block_body(body), r);
}
else if (ast_block_last(proc_body(proc))->k != AST_RETURN) {
/* TODO: something more sophisticated than this */
semantic_warn(scope->fctx, proc,
"unable to determine explicit return for all branches");
destroy_act_state(&new_state);
return -1;
}
warn_unused_labels(&new_state, scope);
if (undefined_gotos(&new_state, scope)) {
destroy_act_state(&new_state);
return -1;
}
/* if we're main, don't mangle the entry point */
char *id = proc_id(proc);
if (strcmp("main", id) == 0)
ast_set_flags(proc, AST_FLAG_NOMANGLE);
/* we have successfully actualized the procedure */
destroy_act_state(&new_state);
return 0;
}
static int actualize_binop(struct act_state *state,
struct scope *scope, struct ast *binop)
{
assert(binop && is_binop(binop));
struct ast *left = binop_left(binop);
struct ast *right = binop_right(binop);
if (actualize(state, scope, left))
return -1;
if (actualize(state, scope, right))
return -1;
if (!types_match(left->t, right->t)) {
type_mismatch(scope, "op type mismatch", binop, left->t, right->t);
return -1;
}
/* types are the same, so the type of this expression is whichever */
set_type(binop, left->t);
return 0;
}
static int actualize_block(struct act_state *state,
struct scope *scope, struct ast *node)
{
struct scope *block_scope = scope;
if (!ast_flags(node, AST_FLAG_UNHYGIENIC)) {
if (!(block_scope = create_scope())) {
/* internal error */
internal_error("failed to allocate block scope");
return -1;
}
scope_add_scope(scope, block_scope);
}
struct act_stack *defers = state->defer_stack;
foreach_node(pt, block_body(node)) {
if (actualize(state, block_scope, pt))
return -1;
}
if (block_body(node) == NULL) {
node->t = void_type();
/* still not a huge fan of directly mucking about with ast slots */
block_body(node) = gen_empty(NULL_LOC());
return 0;
}
/* the block type is the last statement in the block's type */
set_type(node, ast_last(block_body(node))->t);
if (!node->t) {
semantic_error(scope->fctx, node,
"unable to detect block type");
return -1;
}
/* TODO: currently defers are sort of duplicated after a return, unsure
* if they should be handled here or somewhere else */
if (state->defer_stack) {
block_defers(node) = clone_defers(state, defers);
if (!block_defers(node)) {
internal_error("failed cloning defers");
return -1;
}
clear_defers(state, defers);
}
return 0;
}
static int actualize_id(struct act_state *state,
struct scope *scope, struct ast *id)
{
UNUSED(state);
assert(id && id->k == AST_ID);
id->scope = scope;
/** @todo vars and procs kind of override eachother, i.e.
* do_something(){..}
* ^() do_something;
*
* do_something_else(){
* ...
* do_something(); // calls variable at the moment
* }
*
* Either add in some syntax to distinguish procedure calls and
* pointer calls or make procs and vars share the same namespace.
* */
struct ast *decl = file_scope_find_var(scope, id_str(id));
if (decl) {
set_type(id, decl->t);
decl->uses++;
return 0;
}
decl = file_scope_find_proc(scope, id_str(id));
if (decl) {
set_type(id, decl->t);
decl->uses++;
return 0;
}
semantic_error(scope->fctx, id, "no such object");
return -1;
}
static int actualize_var(struct act_state *state,
struct scope *scope, struct ast *var)
{
assert(var && var->k == AST_VAR_DEF);
struct ast *init = var_init(var);
struct type *type = var_type(var);
/* one of these must be defined, otherwise the parser fucked up */
assert(type || init);
if (init && actualize_list(state, scope, init))
return -1;
if (type && actualize_type_list(state, scope, type))
return -1;
if (init && init->k == AST_INIT) {
assert(!init->t);
set_type(init, type);
/* TODO: some kind of check_init() */
}
if (init && type) {
/* make sure the asked type and the actualized types match */
if (!types_match(init->t, type)) {
type_mismatch(scope, "var type mismatch", var, init->t, type);
return -1;
}
}
var->scope = scope;
if (init)
/* infer */
set_type(var, init->t);
if (type)
/* TODO: should there be some default zero value? */
/* declare */
set_type(var, type);
/* an unnamed var is a var in a signature that should not produce a
* warning on not being used (if I ever get around to adding those kinds
* of warnings) */
if (var_id(var) && !ast_flags(var, AST_FLAG_MEMBER))
return scope_add_var(scope, var);
return 0;
}
static int actualize_tid(struct act_state *state, struct scope *scope, struct type *t)
{
UNUSED(state);
/* no id means void */
if (!t->id) {
replace_type(t, void_type());
return 0;
}
struct ast *def = file_scope_find_type(scope, t->id);
if (!def) {
type_error(scope->fctx, t, "no such type");
return -1;
}
assert(t->n == NULL);
assert(def->k != AST_TRAIT_DEF);
if (def->k == AST_ALIAS_DEF) {
replace_type(t, clone_type_list(def->t));
t->a = def;
return 0;
}
else if (def->k == AST_STRUCT_DEF) {
replace_type(t, clone_type_list(def->t));
return 0;
}
else if (def->k == AST_ENUM_DEF) {
replace_type(t, clone_type_list(def->t));
return 0;
}
return -1;
}
static int actualize_ptr(struct act_state *state, struct scope *scope, struct type *t)
{
assert(ptr_base(t));
if (actualize_type(state, scope, ptr_base(t)))
return -1;
return 0;
}
static int actualize_callable(struct act_state *state, struct scope *scope, struct type *t)
{
struct type *ptypes = callable_ptypes(t);
struct type *rtype = callable_rtype(t);
foreach_type(p, ptypes){
if (actualize_type(state, scope, p))
return -1;
}
if (!rtype)
callable_rtype(t) = void_type();
if (actualize_type(state, scope, rtype))
return -1;
return 0;
}
static int actualize_i27(struct act_state *state, struct scope *scope, struct type *t)
{
UNUSED(state);
/* not much to do */
if (t->d)
return 0;
struct ast *def = file_scope_find_type(scope, "i27");
if (!def) {
error("missing definition of type 'i27'");
return -1;
}
t->d = def;
return 0;
}
static int actualize_i9(struct act_state *state, struct scope *scope, struct type *t)
{
UNUSED(state);
/* not much to do */
if (t->d)
return 0;
struct ast *def = file_scope_find_type(scope, "i9");
if (!def) {
error("missing definition of type 'i9'");
return -1;
}
t->d = def;
return 0;
}
static int actualize_bool(struct act_state *state, struct scope *scope, struct type *t)
{
UNUSED(state);
/* not much to do */
if (t->d)
return 0;
struct ast *def = file_scope_find_type(scope, "bool");
if (!def) {
error("missing definition of type 'bool'");
return -1;
}
t->d = def;
return 0;
}
static int actualize_type(struct act_state *state,
struct scope *scope,
struct type *t)
{
if (!t)
return 0;
t->scope = scope;
if (actualize_type(state, scope, t->n))
return -1;
switch (t->k) {
case TYPE_I27: return actualize_i27(state, scope, t);
case TYPE_I9: return actualize_i9(state, scope, t);
case TYPE_BOOL: return actualize_bool(state, scope, t);
case TYPE_ID: return actualize_tid(state, scope, t);
case TYPE_PTR: return actualize_ptr(state, scope, t);
case TYPE_CALLABLE: return actualize_callable(state, scope, t);
case TYPE_VOID: return 0; /* void is by default actualized */
default:
type_info(scope->fctx, t,
"unimplemented type");
type_info(scope->fctx, t,
"continuing with compilation to see what breaks");
return 0;
}
return 0;
}
static int actualize_type_list(struct act_state *state,
struct scope *scope,
struct type *l)
{
foreach_type(t, l) {
if (actualize_type(state, scope, t))
return -1;
}
return 0;
}
static int actualize_empty(struct act_state *state,
struct scope *scope, struct ast *node)
{
UNUSED(state);
UNUSED(scope);
node->t = void_type();
return 0;
}
static int integral_type(struct type *type)
{
switch (type->k) {
case TYPE_I27:
case TYPE_I9:
case TYPE_BOOL:
return true; /* maybe? */
default:
}
return false;
}
static int pointer_type(struct type *type)
{
return type->k == TYPE_PTR;
}
static int pointer_conversion(struct type *a, struct type *b)
{
if (pointer_type(a)) {
if (!is_primitive(b))
return 0;
/* for now */
return b->k == TYPE_I27;
}
return 0;
}
static size_t member_count(struct ast *exists)
{
assert(exists->k == AST_STRUCT_DEF);
struct ast *body = struct_body(exists);
size_t count = 0;
foreach_node(n, body) {
if (n->k == AST_VAR_DEF)
count++;
}
return count;
}
static struct ast *lookup_member_idx(struct ast *body,
char *find,
size_t *idx)
{
/* micro-optimisation, likely way premature but speeds up selection
* between lookup_struct_member_idx and *_name by a tiny amount */
(void)(find);
assert(idx);
size_t i = *idx;
struct ast *m = body;
while (i != 0 && m) {
m = m->n;
i--;
}
return m;
}
static struct ast *lookup_member_name(struct ast *body,
char *find,
size_t *idx)
{
size_t i = 0;
struct ast *m = body;
while (m) {
assert(m->k == AST_VAR_DEF);
if (same_id(find, var_id(m)))
break;
m = m->n;
i++;
}
if (idx)
*idx = i;
return m;
}
static struct ast *lookup_struct_member(struct ast *struc,
char *find, size_t *idx)
{
if (find)
return lookup_member_name(struct_body(struc), find, idx);
return lookup_member_idx(struct_body(struc), find, idx);
}
static struct ast *lookup_enum_member(struct ast *enu,
char *find)
{
size_t i = 0;
struct ast *m = enum_body(enu);
while (m) {
assert(m->k == AST_VAL);
if (same_id(find, val_id(m)))
break;
m = m->n;
i++;
}
return m;
}
static int init_struct(struct act_state *state, struct scope *scope,
struct ast *exists, struct ast *init)
{
size_t i = 0;
size_t mcount = member_count(exists);
int ret = 0;
char *initd = calloc(sizeof(char), mcount);
if (!initd) {
internal_error("failed allocating initd array");
return -1;
}
struct ast *args = init_body(init);
while (args) {
if (i >= mcount) {
semantic_error(scope->fctx, args,
"excessive elements in initializer");
ret = -1;
break;
}
if (initd[i]) {
semantic_error(scope->fctx, args,
"overwriting already initialized elements");
ret = -1;
break;
}
if ((ret = actualize(state, scope, args)))
break;
char *find = NULL;
if (ast_flags(args, AST_FLAG_MEMBER))
find = var_id(args);
struct ast *member =
lookup_struct_member(exists, find, &i);
if (!member) {
char *sstr = type_str(exists->t);
semantic_error(scope->fctx, args,
"no such member in %s",
sstr);
free(sstr);
ret = -1;
break;
}
if (!types_match(args->t, member->t)) {
char *astr = type_str(args->t);
char *mstr = type_str(member->t);
semantic_error(scope->fctx, args,
"%s does not match %s", astr, mstr);
free(astr);
free(mstr);
ret = -1;
break;
}
initd[i] = 1;
args = args->n;
i++;
}
free(initd);
return ret;
}
static int actualize_struct_init_cast(struct act_state *state,
struct scope *scope,
struct ast *init,
struct type *type)
{
struct ast *def = type->d;
return init_struct(state, scope, def, init);
}
static int actualize_init_cast(struct act_state *state,
struct scope *scope, struct ast *init,
struct type *type)
{
if (type->k == TYPE_STRUCT)
return actualize_struct_init_cast(state, scope, init, type);
type_error(scope->fctx, type,
"type is not a struct");
return -1;
}
static int actualize_cast(struct act_state *state,
struct scope *scope, struct ast *cast)
{
assert(cast->k == AST_CAST);
struct ast *expr = cast_expr(cast);
struct type *type = cast_type(cast);
if (actualize_type(state, scope, type))
return -1;
if (actualize(state, scope, expr))
return -1;
if (expr->k == AST_INIT) {
set_type(cast, type);
return actualize_init_cast(state, scope, expr, type);
}
if (types_match(expr->t, type)) {
set_type(cast, type);
return 0;
}
if (integral_type(expr->t) && integral_type(type)) {
set_type(cast, type);
return 0;
}
if (pointer_type(expr->t) && pointer_type(type)) {
set_type(cast, type);
return 0;
}
if (pointer_conversion(expr->t, type)
|| pointer_conversion(type, expr->t)) {
set_type(cast, type);
return 0;
}
/* TODO: arrays? */
char *left_type = type_str(expr->t);
char *right_type = type_str(type);
semantic_error(scope->fctx, cast, "illegal cast: %s vs %s",
left_type, right_type);
free(left_type);
free(right_type);
return -1;
}
static int actualize_const(struct act_state *state, struct scope *scope,
struct ast *cons)
{
UNUSED(state);
if (cons->k == AST_CONST_INT)
cons->t = i27_type(scope);
else if (cons->k == AST_CONST_CHAR)
cons->t = i9_type(scope);
else if (cons->k == AST_CONST_BOOL)
cons->t = bool_type(scope);
else if (cons->k == AST_CONST_STR)
cons->t = str_type(scope);
if (cons->t)
return 0;
semantic_error(scope->fctx, cons, "unimplemented constant");
return 1;
}
static int actualize_alias(struct act_state *state, struct scope *scope,
struct ast *alias)
{
/* I shall have to think about things, as currently very deeply nested
* aliases might be a bit cumbersome to work with. Still, this works
* well enough I suppose. */
assert(alias->k == AST_ALIAS_DEF);
if (actualize_type(state, scope, alias_type(alias))) {
/* usually we don't want to output errors upon errors, but this
* is likely a useful message as it might show where a loop is
* occuring */
semantic_error(scope->fctx, alias, "failed actualizing alias");
return -1;
}
ast_set_flags(alias, AST_FLAG_ACTUAL);
return 0;
}
static int actualize_defer(struct act_state *state,
struct scope *scope, struct ast *node)
{
struct ast *expr = defer_expr(node);
/* TODO: should the actualization only happen when the defers are
* called? */
if (actualize(state, scope, expr))
return -1;
if (push_defer(state, expr))
return -1;
node->t = void_type();
return 0;
}
static int actualize_return(struct act_state *state, struct scope *scope,
struct ast *node)
{
act_set_flags(state, ACT_HAS_RETURN);
struct ast *expr = return_expr(node);
if (expr) {
if (actualize(state, scope, expr))
return -1;
set_type(node, expr->t);
}
else {
node->t = void_type();
}
assert(state->cur_proc);
struct ast *cur_proc = state->cur_proc;
struct type *rtype = proc_rtype(cur_proc);
if (!types_match(node->t, rtype)) {
type_mismatch(scope, "return type mismatch", node, rtype, node->t);
return -1;
}
if (state->defer_stack) {
return_defers(node) = clone_defers(state, NULL);
if (!return_defers(node)) {
internal_error("failed cloning return defers");
return -1;
}
}
return 0;
}
/* Still slightly unsure if this works in all cases, but a good start
* nonetheless. */
static void actualize_goto_defer(struct ast *got, struct ast *label)
{
struct ast *goto_defers = goto_defers(got);
struct ast *label_defers = label_defers(label);
/* since we're dealing with singly linked lists, keep a reference to one
* node before the current goto defer. */
struct ast *prev_defer = NULL;
/* this goto has a defined label */
ast_set_flags(got, AST_FLAG_ACTUAL);
ast_set_flags(label, AST_FLAG_ACTUAL);
size_t goto_len = ast_list_len(goto_defers);
size_t label_len = ast_list_len(label_defers);
/* find first common defer statement */
while (goto_len > label_len) {
prev_defer = goto_defers;
goto_defers = goto_defers->n;
goto_len--;
}
while (label_len > goto_len) {
label_defers = label_defers->n;
label_len--;
}
/* TODO: possible optimisation could be maintaining references to the
* defer statements themselves, building up a sort of tree, allowing us
* to directly compare pointers, likely being a bit quicker. Still,
* unlikely that goto stuff would be a major bottleneck. */
while (!equiv_nodes(goto_defers, label_defers)) {
prev_defer = goto_defers;
label_defers = label_defers->n;
goto_defers = goto_defers->n;
}
/* only the defers below the common defer should be executed by the goto */
if (goto_defers) {
/* fuck, I actually need the one previous to this */
assert(prev_defer->n == goto_defers);
prev_defer->n = NULL;
}
/* nothing to do */
}
static int actualize_goto(struct act_state *state, struct scope *scope,
struct ast *node)
{
UNUSED(scope);
assert(node->k == AST_GOTO);
push_goto(state, node);
/* clone all defers as we don't know where the label might be */
goto_defers(node) = clone_defers(state, NULL);
node->t = void_type();
struct ast *label = find_label(state, goto_label(node));
/* this is a jump backwards, i.e. we can already do it */
if (label)
actualize_goto_defer(node, label);
/* forward jump, handle stuff when we run into the corresponding label */
return 0;
}
static void actualize_goto_defers(struct act_state *state,
struct ast *label)
{
struct act_stack *prev = state->goto_stack, *cur;
if (prev)
do {
cur = prev->next;
struct ast *got = prev->node;
if (same_id(goto_label(got), label_id(label)))
actualize_goto_defer(got, label);
} while ((prev = cur));
}
static int actualize_label(struct act_state *state, struct scope *scope,
struct ast *node)
{
UNUSED(scope);
assert(node->k == AST_LABEL);
struct ast *prev = find_label(state, label_id(node));
if (prev) {
semantic_error(scope->fctx, node, "label redefined");
semantic_info(scope->fctx, prev, "previous definition");
return -1;
}
push_label(state, node);
/* clone all defers */
label_defers(node) = clone_defers(state, NULL);
node->t = void_type();
actualize_goto_defers(state, node);
return 0;
}
static int actualize_unop(struct act_state *state,
struct scope *scope, struct ast *node)
{
assert(is_unop(node));
struct ast *expr = unop_expr(node);
if (actualize(state, scope, expr))
return -1;
/* generally speaking */
set_type(node, expr->t);
switch (node->k) {
case AST_DEREF: {
struct type *type = expr->t;
if (type->k!= TYPE_PTR) {
/** @todo or array? */
char *tstr = type_str(type);
semantic_error(scope->fctx, expr,
"not a pointer: %s",
tstr);
free(tstr);
return -1;
}
set_type(node, ptr_base(type));
assert(node->t);
if (simplify_refderef(state, scope, node))
return -1;
break;
}
case AST_REF: {
/** @todo array pointer decay? */
node->t = tgen_ptr(clone_type(expr->t), node->loc);
if (simplify_refderef(state, scope, node))
return -1;
break;
}
case AST_LNOT: {
if (is_primitive(expr->t)) {
char *tstr = type_str(expr->t);
semantic_error(scope->fctx, node,
"'!' only implemented for primitive types: %s",
tstr);
free(tstr);
return -1;
}
if (expr->t->k == TYPE_VOID) {
semantic_error(scope->fctx, node,
"'!' not implemented for void");
return -1;
}
node->t = bool_type(scope);
break;
}
default: semantic_error(scope->fctx, node,
"unimplemented unary operation");
return -1;
}
return 0;
}
static int actualize_as(struct act_state *state,
struct scope *scope, struct ast *as)
{
assert(as->k == AST_AS);
struct type *type = as->t;
if (actualize_type(state, scope, type))
return -1;
set_type(as, type);
return 0;
}
struct replace_data {
char *id;
struct type *replacement;
};
static int _replace_type_id(struct type *type, void *data)
{
struct replace_data *pair = data;
struct type *replacement = pair->replacement;
char *id = pair->id;
switch (type->k) {
case TYPE_ID: {
replace_type(type, clone_type(replacement));
break;
}
case TYPE_TRAIT: {
struct ast *def = type->d;
assert(def);
char *name = trait_id(def);
if (same_id(id, name))
replace_type(type, clone_type(replacement));
break;
}
case TYPE_STRUCT: {
struct ast *def = type->d;
assert(def);
char *name = struct_id(def);
if (same_id(id, name))
replace_type(type, clone_type(replacement));
break;
}
default:
}
return 0;
}
static int _replace_ast_type_id(struct ast *node, void *data)
{
if (!node)
return 0;
return type_visit_list(_replace_type_id, NULL, node->t, data);
}
static int replace_type_id(struct ast *nodes, char *id,
struct type *replacement)
{
struct replace_data pair = {id, replacement};
return ast_visit(_replace_ast_type_id, NULL, nodes, &pair);
}
/* lots of overlap with actualize_struct, kind of ugly... */
static int actualize_trait(struct act_state *state, struct scope *scope,
struct ast *node)
{
UNUSED(scope);
assert(node->k == AST_TRAIT_DEF);
foreach_node(n, trait_body(node)) {
/* there's really only prodcedure body actualization left I
* guess, as type stuff was taken care of in the analysis phase
* */
if (n->k != AST_PROC_DEF)
continue;
/* don't actualize prototypes, duh */
if (!proc_body(n))
continue;
if (actualize(state, node->scope, n))
return -1;
}
return 0;
}
static int actualize_struct(struct act_state *state,
struct scope *scope, struct ast *node)
{
UNUSED(scope);
assert(node->k == AST_STRUCT_DEF);
foreach_node(n, struct_body(node)) {
/* there's really only prodcedure body actualization left I
* guess, as type stuff was taken care of in the analysis phase
* */
if (n->k != AST_PROC_DEF)
continue;
/* don't actualize prototypes, duh */
if (!proc_body(n))
continue;
if (actualize(state, node->scope, n))
return -1;
}
return 0;
}
static int actualize_dot(struct act_state *state,
struct scope *scope, struct ast *node)
{
assert(node->k == AST_DOT);
struct ast *expr = dot_expr(node);
if (actualize(state, scope, expr))
return -1;
char *id = dot_id(node);
struct type *type = expr->t;
struct ast *def = NULL;
switch (type->k) {
case TYPE_TRAIT:
case TYPE_STRUCT: def = type->d; break;
default: {
char *tstr = type_str(type);
semantic_error(scope->fctx, node,
"illegal type in dot expression: %s",
tstr);
free(tstr);
return -1;
}
}
struct ast *exists = scope_find_var(def->scope, id);
if (exists) {
assert(exists->t);
set_type(node, exists->t);
return 0;
}
exists = scope_find_proc(def->scope, id);
if (exists) {
assert(exists->t);
set_type(node, exists->t);
return 0;
}
semantic_error(scope->fctx, node,
"does not have member");
return -1;
}
static int actualize_init(struct act_state *state,
struct scope *scope, struct ast *node)
{
assert(node->k == AST_INIT);
/* for now just do the types, the named stuff will be checked later */
/* TODO: how to make sure all members are initialized? */
return actualize_list(state, scope, init_body(node));
}
static int actualize_assign(struct act_state *state, struct scope *scope,
struct ast *node)
{
assert(node->k == AST_ASSIGN);
struct ast *to = assign_to(node);
if (actualize(state, scope, to))
return -1;
struct ast *from = assign_from(node);
if (actualize_list(state, scope, from))
return -1;
if (from->k == AST_INIT) {
set_type(node, to->t);
return actualize_init_cast(state, scope, from, to->t);
}
if (!types_match(to->t, from->t)) {
type_mismatch(scope, "assign type mismatch", node, to->t, from->t);
return -1;
}
/** @todo rvalue vs lvalue? */
if (!is_lvalue(to)) {
semantic_error(scope->fctx, node,
"rvalue used where lvalue required");
return -1;
}
set_type(node, to->t);
return 0;
}
static int actualize_fetch(struct act_state *state, struct scope *scope,
struct ast *fetch)
{
assert(fetch->k == AST_FETCH);
struct type *type = fetch_type(fetch);
if (actualize_type(state, scope, type))
return -1;
if (type->k != TYPE_ENUM) {
type_error(scope->fctx, type, "type is not an enum");
return -1;
}
char *id = fetch_id(fetch);
struct ast *def = type->d;
assert(def);
struct ast *member = lookup_enum_member(def, id);
if (!member) {
char *estr = type_str(type);
semantic_error(scope->fctx, fetch, "no such member in enum %s");
free(estr);
return -1;
}
set_type(fetch, def->t);
return 0;
}
static int actualize_enum(struct act_state *state, struct scope *scope,
struct ast *node)
{
assert(node->k == AST_ENUM_DEF);
struct type *type = enum_type(node);
struct scope *enum_scope = node->scope;
/* TODO: here we could save space by choosing the smallest type that
* fits */
if (!type) {
type = i27_type(scope);
node->t = type;
} else if (actualize_type(state, enum_scope, type))
return -1;
long long counter = 0;
node->t = type;
struct ast *members = enum_body(node);
while (members) {
set_type(members, type);
if (val_val(members)) {
struct ast *val = val_val(members);
if (actualize(state, enum_scope, val))
return -1;
if (val->k != AST_CONST_INT) {
semantic_error(scope->fctx, members,
"unable to process nonconstant expression");
return -1;
}
counter = int_val(val);
}
else {
val_val(members) = gen_const_int(counter, NULL_LOC());
}
members = members->n;
counter++;
}
/* TODO: check that we don't go outside the limits of the type */
return 0;
}
static int actualize_if(struct act_state *state, struct scope *scope,
struct ast *node)
{
assert(node->k == AST_IF);
if (actualize(state, scope, if_cond(node)))
return -1;
if (actualize(state, scope, if_body(node)))
return -1;
if (actualize(state, scope, if_else(node)))
return -1;
if (ast_flags(node, AST_FLAG_DOEXPR)) {
struct type *tt = ast_last(if_body(node))->t;
struct type *ft = ast_last(if_else(node))->t;
if (!types_match(tt, ft)) {
semantic_error(scope->fctx, node,
"mismatched if/else body values");
return -1;
}
set_type(node, tt);
return 0;
}
node->t = void_type();
return 0;
}
static int actualize_for(struct act_state *state, struct scope *scope,
struct ast *node)
{
assert(node->k == AST_FOR);
if (actualize_list(state, scope, for_pre(node)))
return -1;
if (actualize_list(state, scope, for_post(node)))
return -1;
if (actualize_list(state, scope, for_cond(node)))
return -1;
if (actualize_list(state, scope, for_body(node)))
return -1;
node->t = void_type();
return 0;
}
static int actualize_comparison(struct act_state *state, struct scope *scope, struct ast *node)
{
assert(is_comparison(node));
struct ast *left = comparison_left(node);
struct ast *right = comparison_right(node);
if (actualize(state, scope, left))
return -1;
if (actualize(state, scope, right))
return -1;
if (!is_primitive(left->t)) {
type_error(scope->fctx, left->t, "primitive type required");
return -1;
}
if (!is_primitive(right->t)) {
type_error(scope->fctx, right->t, "primitive type required");
return -1;
}
if (!types_match(left->t, right->t)) {
type_mismatch(scope, "comparison type mismatch", node, left->t, right->t);
return -1;
}
set_type(node, bool_type(scope));
return 0;
}
static int actualize(struct act_state *state, struct scope *scope,
struct ast *node)
{
if (!node)
return 0;
if (!node->scope)
node->scope = scope;
if (is_unop(node))
return actualize_unop(state, scope, node);
if (is_binop(node))
return actualize_binop(state, scope, node);
if (is_comparison(node))
return actualize_comparison(state, scope, node);
if (is_const(node))
return actualize_const(state, scope, node);
switch (node->k) {
case AST_PROC_DEF: return actualize_proc(state, scope, node);
case AST_TRAIT_DEF: return actualize_trait(state, scope, node);
case AST_ALIAS_DEF: return actualize_alias(state, scope, node);
case AST_ENUM_DEF: return actualize_enum(state, scope, node);
case AST_MACRO_DEF: return actualize_macro_def(state, scope, node);
case AST_STRUCT_DEF: return actualize_struct(state, scope, node);
case AST_VAR_DEF: return actualize_var(state, scope, node);
case AST_CALL: return actualize_call(state, scope, node);
case AST_BLOCK: return actualize_block(state, scope, node);
case AST_ID: return actualize_id(state, scope, node);
case AST_EMPTY: return actualize_empty(state, scope, node);
case AST_CAST: return actualize_cast(state, scope, node);
case AST_DEFER: return actualize_defer(state, scope, node);
case AST_RETURN: return actualize_return(state, scope, node);
case AST_GOTO: return actualize_goto(state, scope, node);
case AST_LABEL: return actualize_label(state, scope, node);
case AST_AS: return actualize_as(state, scope, node);
case AST_DOT: return actualize_dot(state, scope, node);
case AST_INIT: return actualize_init(state, scope, node);
case AST_ASSIGN: return actualize_assign(state, scope, node);
case AST_FETCH: return actualize_fetch(state, scope, node);
case AST_IF: return actualize_if(state, scope, node);
case AST_FOR: return actualize_for(state, scope, node);
case AST_MACRO_EXPAND: return actualize_macro_expand(state, scope, node);
default:
/* more like internal_error, maybe? */
semantic_error(scope->fctx, node,
"unimplemented actualization");
return -1;
}
return 0;
}
static int actualize_list(struct act_state *state, struct scope *scope,
struct ast *l)
{
foreach_node(n, l) {
if (actualize(state, scope, n))
return -1;
}
return 0;
}
|