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
|
void check_stmt_list(Checker *c, Array<AstNode *> stmts, u32 flags) {
if (stmts.count == 0) {
return;
}
if (flags&Stmt_CheckScopeDecls) {
check_scope_decls(c, stmts, 1.2*stmts.count);
}
bool ft_ok = (flags & Stmt_FallthroughAllowed) != 0;
flags &= ~Stmt_FallthroughAllowed;
isize max = stmts.count;
for (isize i = stmts.count-1; i >= 0; i--) {
if (stmts[i]->kind != AstNode_EmptyStmt) {
break;
}
max--;
}
for (isize i = 0; i < max; i++) {
AstNode *n = stmts[i];
if (n->kind == AstNode_EmptyStmt) {
continue;
}
u32 new_flags = flags;
if (ft_ok && i+1 == max) {
new_flags |= Stmt_FallthroughAllowed;
}
if (i+1 < max) {
switch (n->kind) {
case AstNode_ReturnStmt:
error(n, "Statements after this `return` are never executed");
break;
}
}
check_stmt(c, n, new_flags);
}
}
bool check_is_terminating_list(Array<AstNode *> stmts) {
// Iterate backwards
for (isize n = stmts.count-1; n >= 0; n--) {
AstNode *stmt = stmts[n];
if (stmt->kind != AstNode_EmptyStmt) {
return check_is_terminating(stmt);
}
}
return false;
}
bool check_has_break_list(Array<AstNode *> stmts, bool implicit) {
for_array(i, stmts) {
AstNode *stmt = stmts[i];
if (check_has_break(stmt, implicit)) {
return true;
}
}
return false;
}
bool check_has_break(AstNode *stmt, bool implicit) {
switch (stmt->kind) {
case AstNode_BranchStmt:
if (stmt->BranchStmt.token.kind == Token_break) {
return implicit;
}
break;
case AstNode_BlockStmt:
return check_has_break_list(stmt->BlockStmt.stmts, implicit);
case AstNode_IfStmt:
if (check_has_break(stmt->IfStmt.body, implicit) ||
(stmt->IfStmt.else_stmt != nullptr && check_has_break(stmt->IfStmt.else_stmt, implicit))) {
return true;
}
break;
case AstNode_CaseClause:
return check_has_break_list(stmt->CaseClause.stmts, implicit);
}
return false;
}
// NOTE(bill): The last expression has to be a `return` statement
// TODO(bill): This is a mild hack and should be probably handled properly
// TODO(bill): Warn/err against code after `return` that it won't be executed
bool check_is_terminating(AstNode *node) {
switch (node->kind) {
case_ast_node(rs, ReturnStmt, node);
return true;
case_end;
case_ast_node(bs, BlockStmt, node);
return check_is_terminating_list(bs->stmts);
case_end;
case_ast_node(es, ExprStmt, node);
return check_is_terminating(es->expr);
case_end;
case_ast_node(is, IfStmt, node);
if (is->else_stmt != nullptr) {
if (check_is_terminating(is->body) &&
check_is_terminating(is->else_stmt)) {
return true;
}
}
case_end;
case_ast_node(ws, WhenStmt, node);
if (ws->else_stmt != nullptr) {
if (check_is_terminating(ws->body) &&
check_is_terminating(ws->else_stmt)) {
return true;
}
}
case_end;
case_ast_node(fs, ForStmt, node);
if (fs->cond == nullptr && !check_has_break(fs->body, true)) {
return check_is_terminating(fs->body);
}
case_end;
case_ast_node(rs, RangeStmt, node);
return false;
case_end;
case_ast_node(ms, MatchStmt, node);
bool has_default = false;
for_array(i, ms->body->BlockStmt.stmts) {
AstNode *clause = ms->body->BlockStmt.stmts[i];
ast_node(cc, CaseClause, clause);
if (cc->list.count == 0) {
has_default = true;
}
if (!check_is_terminating_list(cc->stmts) ||
check_has_break_list(cc->stmts, true)) {
return false;
}
}
return has_default;
case_end;
case_ast_node(ms, TypeMatchStmt, node);
bool has_default = false;
for_array(i, ms->body->BlockStmt.stmts) {
AstNode *clause = ms->body->BlockStmt.stmts[i];
ast_node(cc, CaseClause, clause);
if (cc->list.count == 0) {
has_default = true;
}
if (!check_is_terminating_list(cc->stmts) ||
check_has_break_list(cc->stmts, true)) {
return false;
}
}
return has_default;
case_end;
case_ast_node(pa, PushAllocator, node);
return check_is_terminating(pa->body);
case_end;
case_ast_node(pc, PushContext, node);
return check_is_terminating(pc->body);
case_end;
}
return false;
}
Type *check_assignment_variable(Checker *c, Operand *rhs, AstNode *lhs_node) {
if (rhs->mode == Addressing_Invalid ||
(rhs->type == t_invalid && rhs->mode != Addressing_Overload)) {
return nullptr;
}
AstNode *node = unparen_expr(lhs_node);
// NOTE(bill): Ignore assignments to `_`
if (is_blank_ident(node)) {
add_entity_definition(&c->info, node, nullptr);
check_assignment(c, rhs, nullptr, str_lit("assignment to `_` identifier"));
if (rhs->mode == Addressing_Invalid) {
return nullptr;
}
return rhs->type;
}
Entity *e = nullptr;
bool used = false;
Operand lhs = {Addressing_Invalid};
check_expr(c, &lhs, lhs_node);
if (lhs.mode == Addressing_Invalid ||
lhs.type == t_invalid) {
return nullptr;
}
if (rhs->mode == Addressing_Overload) {
isize overload_count = rhs->overload_count;
Entity **procs = rhs->overload_entities;
GB_ASSERT(procs != nullptr && overload_count > 0);
// NOTE(bill): These should be done
for (isize i = 0; i < overload_count; i++) {
Type *t = base_type(procs[i]->type);
if (t == t_invalid) {
continue;
}
Operand x = {};
x.mode = Addressing_Value;
x.type = t;
if (check_is_assignable_to(c, &x, lhs.type)) {
e = procs[i];
add_entity_use(c, rhs->expr, e);
break;
}
}
if (e != nullptr) {
// HACK TODO(bill): Should the entities be freed as it's technically a leak
rhs->mode = Addressing_Value;
rhs->type = e->type;
rhs->overload_count = 0;
rhs->overload_entities = nullptr;
}
} else {
if (node->kind == AstNode_Ident) {
ast_node(i, Ident, node);
e = scope_lookup_entity(c->context.scope, i->token.string);
if (e != nullptr && e->kind == Entity_Variable) {
used = (e->flags & EntityFlag_Used) != 0; // TODO(bill): Make backup just in case
}
}
}
if (e != nullptr && used) {
e->flags |= EntityFlag_Used;
}
Type *assignment_type = lhs.type;
switch (lhs.mode) {
case Addressing_Invalid:
return nullptr;
case Addressing_Variable: {
if (is_type_bit_field_value(lhs.type)) {
Type *lt = base_type(lhs.type);
i64 lhs_bits = lt->BitFieldValue.bits;
if (rhs->mode == Addressing_Constant) {
ExactValue v = exact_value_to_integer(rhs->value);
if (v.kind == ExactValue_Integer) {
i128 i = v.value_integer;
u128 u = *cast(u128 *)&i;
u128 umax = U128_NEG_ONE;
if (lhs_bits < 128) {
umax = u128_sub(u128_shl(U128_ONE, lhs_bits), U128_ONE);
}
i128 imax = i128_shl(I128_ONE, lhs_bits-1ll);
bool ok = false;
ok = !(u128_lt(u, U128_ZERO) || u128_gt(u, umax));
if (ok) {
return rhs->type;
}
}
} else if (is_type_integer(rhs->type)) {
// TODO(bill): Any other checks?
return rhs->type;
}
gbString lhs_expr = expr_to_string(lhs.expr);
gbString rhs_expr = expr_to_string(rhs->expr);
error(rhs->expr, "Cannot assign `%s` to bit field `%s`", rhs_expr, lhs_expr);
gb_string_free(rhs_expr);
gb_string_free(lhs_expr);
return nullptr;
}
break;
}
case Addressing_MapIndex: {
AstNode *ln = unparen_expr(lhs_node);
if (ln->kind == AstNode_IndexExpr) {
AstNode *x = ln->IndexExpr.expr;
TypeAndValue tav = type_and_value_of_expr(&c->info, x);
GB_ASSERT(tav.mode != Addressing_Invalid);
if (tav.mode != Addressing_Variable) {
if (!is_type_pointer(tav.type)) {
gbString str = expr_to_string(lhs.expr);
error(lhs.expr, "Cannot assign to the value of a map `%s`", str);
gb_string_free(str);
return nullptr;
}
}
}
} break;
default: {
if (lhs.expr->kind == AstNode_SelectorExpr) {
// NOTE(bill): Extra error checks
Operand op_c = {Addressing_Invalid};
ast_node(se, SelectorExpr, lhs.expr);
check_expr(c, &op_c, se->expr);
if (op_c.mode == Addressing_MapIndex) {
gbString str = expr_to_string(lhs.expr);
error(lhs.expr, "Cannot assign to struct field `%s` in map", str);
gb_string_free(str);
return nullptr;
}
}
gbString str = expr_to_string(lhs.expr);
if (lhs.mode == Addressing_Immutable) {
error(lhs.expr, "Cannot assign to an immutable: `%s`", str);
} else {
error(lhs.expr, "Cannot assign to `%s`", str);
}
gb_string_free(str);
} break;
}
check_assignment(c, rhs, assignment_type, str_lit("assignment"));
if (rhs->mode == Addressing_Invalid) {
return nullptr;
}
return rhs->type;
}
enum MatchTypeKind {
MatchType_Invalid,
MatchType_Union,
MatchType_Any,
};
MatchTypeKind check_valid_type_match_type(Type *type) {
type = type_deref(type);
if (is_type_union(type)) {
return MatchType_Union;
}
if (is_type_any(type)) {
return MatchType_Any;
}
return MatchType_Invalid;
}
void check_stmt_internal(Checker *c, AstNode *node, u32 flags);
void check_stmt(Checker *c, AstNode *node, u32 flags) {
u32 prev_stmt_state_flags = c->context.stmt_state_flags;
if (node->stmt_state_flags != 0) {
u32 in = node->stmt_state_flags;
u32 out = c->context.stmt_state_flags;
if (in & StmtStateFlag_no_bounds_check) {
out |= StmtStateFlag_no_bounds_check;
out &= ~StmtStateFlag_bounds_check;
} else {
// if (in & StmtStateFlag_bounds_check) {
out |= StmtStateFlag_bounds_check;
out &= ~StmtStateFlag_no_bounds_check;
}
c->context.stmt_state_flags = out;
}
check_stmt_internal(c, node, flags);
c->context.stmt_state_flags = prev_stmt_state_flags;
}
struct TypeAndToken {
Type *type;
Token token;
};
void check_when_stmt(Checker *c, AstNodeWhenStmt *ws, u32 flags) {
flags &= ~Stmt_CheckScopeDecls;
Operand operand = {Addressing_Invalid};
check_expr(c, &operand, ws->cond);
if (operand.mode != Addressing_Constant || !is_type_boolean(operand.type)) {
error(ws->cond, "Non-constant boolean `when` condition");
return;
}
if (ws->body == nullptr || ws->body->kind != AstNode_BlockStmt) {
error(ws->cond, "Invalid body for `when` statement");
return;
}
if (operand.value.kind == ExactValue_Bool &&
operand.value.value_bool) {
check_stmt_list(c, ws->body->BlockStmt.stmts, flags);
} else if (ws->else_stmt) {
switch (ws->else_stmt->kind) {
case AstNode_BlockStmt:
check_stmt_list(c, ws->else_stmt->BlockStmt.stmts, flags);
break;
case AstNode_WhenStmt:
check_when_stmt(c, &ws->else_stmt->WhenStmt, flags);
break;
default:
error(ws->else_stmt, "Invalid `else` statement in `when` statement");
break;
}
}
}
void check_label(Checker *c, AstNode *label) {
if (label == nullptr) {
return;
}
ast_node(l, Label, label);
if (l->name->kind != AstNode_Ident) {
error(l->name, "A label's name must be an identifier");
return;
}
String name = l->name->Ident.token.string;
if (is_blank_ident(name)) {
error(l->name, "A label's name cannot be a blank identifier");
return;
}
if (c->proc_stack.count == 0) {
error(l->name, "A label is only allowed within a procedure");
return;
}
GB_ASSERT(c->context.decl != nullptr);
bool ok = true;
for_array(i, c->context.decl->labels) {
BlockLabel bl = c->context.decl->labels[i];
if (bl.name == name) {
error(label, "Duplicate label with the name `%.*s`", LIT(name));
ok = false;
break;
}
}
Entity *e = make_entity_label(c->allocator, c->context.scope, l->name->Ident.token, t_invalid, label);
add_entity(c, c->context.scope, l->name, e);
e->parent_proc_decl = c->context.curr_proc_decl;
if (ok) {
BlockLabel bl = {name, label};
array_add(&c->context.decl->labels, bl);
}
}
// Returns `true` for `continue`, `false` for `return`
bool check_using_stmt_entity(Checker *c, AstNodeUsingStmt *us, AstNode *expr, bool is_selector, Entity *e) {
if (e == nullptr) {
error(us->token, "`using` applied to an unknown entity");
return true;
}
add_entity_use(c, expr, e);
switch (e->kind) {
case Entity_TypeName: {
Type *t = base_type(e->type);
if (t->kind == Type_Struct) {
Scope *s = t->Struct.scope;
if (s != nullptr) {
for_array(i, s->elements.entries) {
Entity *f = s->elements.entries[i].value;
if (f->kind != Entity_Variable) {
Entity *found = scope_insert_entity(c->context.scope, f);
if (found != nullptr) {
gbString expr_str = expr_to_string(expr);
error(us->token, "Namespace collision while `using` `%s` of: %.*s", expr_str, LIT(found->token.string));
gb_string_free(expr_str);
return false;
}
f->using_parent = e;
}
}
}
} else if (t->kind == Type_Enum) {
for (isize i = 0; i < t->Enum.field_count; i++) {
Entity *f = t->Enum.fields[i];
Entity *found = scope_insert_entity(c->context.scope, f);
if (found != nullptr) {
gbString expr_str = expr_to_string(expr);
error(us->token, "Namespace collision while `using` `%s` of: %.*s", expr_str, LIT(found->token.string));
gb_string_free(expr_str);
return false;
}
f->using_parent = e;
}
} else {
error(us->token, "`using` can be only applied to struct type entities");
}
} break;
case Entity_ImportName: {
Scope *scope = e->ImportName.scope;
for_array(i, scope->elements.entries) {
Entity *decl = scope->elements.entries[i].value;
Entity *found = scope_insert_entity(c->context.scope, decl);
if (found != nullptr) {
gbString expr_str = expr_to_string(expr);
error(us->token,
"Namespace collision while `using` `%s` of: %.*s\n"
"\tat %.*s(%td:%td)\n"
"\tat %.*s(%td:%td)",
expr_str, LIT(found->token.string),
LIT(found->token.pos.file), found->token.pos.line, found->token.pos.column,
LIT(decl->token.pos.file), decl->token.pos.line, decl->token.pos.column
);
gb_string_free(expr_str);
return false;
}
}
} break;
case Entity_Variable: {
Type *t = base_type(type_deref(e->type));
if (is_type_struct(t) || is_type_raw_union(t) || is_type_union(t)) {
// TODO(bill): Make it work for unions too
Scope *found = scope_of_node(&c->info, t->Struct.node);
for_array(i, found->elements.entries) {
Entity *f = found->elements.entries[i].value;
if (f->kind == Entity_Variable) {
Entity *uvar = make_entity_using_variable(c->allocator, e, f->token, f->type);
// if (is_selector) {
uvar->using_expr = expr;
// }
Entity *prev = scope_insert_entity(c->context.scope, uvar);
if (prev != nullptr) {
gbString expr_str = expr_to_string(expr);
error(us->token, "Namespace collision while `using` `%s` of: %.*s", expr_str, LIT(prev->token.string));
gb_string_free(expr_str);
return false;
}
}
}
} else {
error(us->token, "`using` can only be applied to variables of type struct or raw_union");
return false;
}
} break;
case Entity_Constant:
error(us->token, "`using` cannot be applied to a constant");
break;
case Entity_Procedure:
case Entity_Builtin:
error(us->token, "`using` cannot be applied to a procedure");
break;
case Entity_Nil:
error(us->token, "`using` cannot be applied to `nil`");
break;
case Entity_Label:
error(us->token, "`using` cannot be applied to a label");
break;
case Entity_Invalid:
error(us->token, "`using` cannot be applied to an invalid entity");
break;
default:
GB_PANIC("TODO(bill): `using` other expressions?");
}
return true;
}
void check_stmt_internal(Checker *c, AstNode *node, u32 flags) {
u32 mod_flags = flags & (~Stmt_FallthroughAllowed);
switch (node->kind) {
case_ast_node(_, EmptyStmt, node); case_end;
case_ast_node(_, BadStmt, node); case_end;
case_ast_node(_, BadDecl, node); case_end;
case_ast_node(es, ExprStmt, node)
Operand operand = {Addressing_Invalid};
ExprKind kind = check_expr_base(c, &operand, es->expr, nullptr);
switch (operand.mode) {
case Addressing_Type: {
gbString str = type_to_string(operand.type);
error(node, "`%s` is not an expression", str);
gb_string_free(str);
} break;
case Addressing_NoValue:
return;
default: {
if (kind == Expr_Stmt) {
return;
}
if (operand.expr->kind == AstNode_CallExpr) {
AstNodeCallExpr *ce = &operand.expr->CallExpr;
Type *t = type_of_expr(&c->info, ce->proc);
if (is_type_proc(t)) {
if (t->Proc.require_results) {
gbString expr_str = expr_to_string(ce->proc);
error(node, "`%s` requires that its results must be handled", expr_str);
gb_string_free(expr_str);
}
}
return;
}
gbString expr_str = expr_to_string(operand.expr);
error(node, "Expression is not used: `%s`", expr_str);
gb_string_free(expr_str);
} break;
}
case_end;
case_ast_node(ts, TagStmt, node);
// TODO(bill): Tag Statements
error(node, "Tag statements are not supported yet");
check_stmt(c, ts->stmt, flags);
case_end;
#if 0
case_ast_node(s, IncDecStmt, node);
TokenKind op = s->op.kind;
switch (op) {
case Token_Inc: op = Token_Add; break;
case Token_Dec: op = Token_Sub; break;
default:
error(node, "Invalid inc/dec operation");
return;
}
Operand x = {};
check_expr(c, &x, s->expr);
if (x.mode == Addressing_Invalid) {
return;
}
if (!is_type_integer(x.type) && !is_type_float(x.type)) {
gbString e = expr_to_string(s->expr);
gbString t = type_to_string(x.type);
error(node, "%s%.*s used on non-numeric type %s", e, LIT(s->op.string), t);
gb_string_free(t);
gb_string_free(e);
return;
}
AstNode *left = s->expr;
AstNode *right = gb_alloc_item(c->allocator, AstNode);
right->kind = AstNode_BasicLit;
right->BasicLit.pos = s->op.pos;
right->BasicLit.kind = Token_Integer;
right->BasicLit.string = str_lit("1");
AstNode *be = gb_alloc_item(c->allocator, AstNode);
be->kind = AstNode_BinaryExpr;
be->BinaryExpr.op = s->op;
be->BinaryExpr.op.kind = op;
be->BinaryExpr.left = left;
be->BinaryExpr.right = right;
check_binary_expr(c, &x, be);
if (x.mode == Addressing_Invalid) {
return;
}
check_assignment_variable(c, &x, left);
case_end;
#endif
case_ast_node(as, AssignStmt, node);
switch (as->op.kind) {
case Token_Eq: {
// a, b, c = 1, 2, 3; // Multisided
isize lhs_count = as->lhs.count;
if (lhs_count == 0) {
error(as->op, "Missing lhs in assignment statement");
return;
}
gbTempArenaMemory tmp = gb_temp_arena_memory_begin(&c->tmp_arena);
defer (gb_temp_arena_memory_end(tmp));
// NOTE(bill): If there is a bad syntax error, rhs > lhs which would mean there would need to be
// an extra allocation
Array<Operand> operands = {};
array_init(&operands, c->tmp_allocator, 2 * lhs_count);
check_unpack_arguments(c, lhs_count, &operands, as->rhs, true);
isize rhs_count = operands.count;
for_array(i, operands) {
if (operands[i].mode == Addressing_Invalid) {
rhs_count--;
}
}
isize max = gb_min(lhs_count, rhs_count);
for (isize i = 0; i < max; i++) {
check_assignment_variable(c, &operands[i], as->lhs[i]);
}
if (lhs_count != rhs_count) {
error(as->lhs[0], "Assignment count mismatch `%td` = `%td`", lhs_count, rhs_count);
}
} break;
default: {
// a += 1; // Single-sided
Token op = as->op;
if (as->lhs.count != 1 || as->rhs.count != 1) {
error(op, "Assignment operation `%.*s` requires single-valued expressions", LIT(op.string));
return;
}
if (!gb_is_between(op.kind, Token__AssignOpBegin+1, Token__AssignOpEnd-1)) {
error(op, "Unknown Assignment operation `%.*s`", LIT(op.string));
return;
}
Operand operand = {Addressing_Invalid};
AstNode binary_expr = {AstNode_BinaryExpr};
ast_node(be, BinaryExpr, &binary_expr);
be->op = op;
be->op.kind = cast(TokenKind)(cast(i32)be->op.kind - (Token_AddEq - Token_Add));
// NOTE(bill): Only use the first one will be used
be->left = as->lhs[0];
be->right = as->rhs[0];
check_binary_expr(c, &operand, &binary_expr);
if (operand.mode == Addressing_Invalid) {
return;
}
// NOTE(bill): Only use the first one will be used
check_assignment_variable(c, &operand, as->lhs[0]);
} break;
}
case_end;
case_ast_node(bs, BlockStmt, node);
check_open_scope(c, node);
check_stmt_list(c, bs->stmts, mod_flags);
check_close_scope(c);
case_end;
case_ast_node(is, IfStmt, node);
check_open_scope(c, node);
if (is->init != nullptr) {
check_stmt(c, is->init, 0);
}
Operand operand = {Addressing_Invalid};
check_expr(c, &operand, is->cond);
if (operand.mode != Addressing_Invalid && !is_type_boolean(operand.type)) {
error(is->cond, "Non-boolean condition in `if` statement");
}
check_stmt(c, is->body, mod_flags);
if (is->else_stmt != nullptr) {
switch (is->else_stmt->kind) {
case AstNode_IfStmt:
case AstNode_BlockStmt:
check_stmt(c, is->else_stmt, mod_flags);
break;
default:
error(is->else_stmt, "Invalid `else` statement in `if` statement");
break;
}
}
check_close_scope(c);
case_end;
case_ast_node(ws, WhenStmt, node);
check_when_stmt(c, ws, flags);
case_end;
case_ast_node(rs, ReturnStmt, node);
GB_ASSERT(c->proc_stack.count > 0);
if (c->context.in_defer) {
error(rs->token, "You cannot `return` within a defer statement");
break;
}
bool first_is_field_value = false;
if (rs->results.count > 0) {
bool fail = false;
first_is_field_value = (rs->results[0]->kind == AstNode_FieldValue);
for_array(i, rs->results) {
AstNode *arg = rs->results[i];
bool mix = false;
if (first_is_field_value) {
mix = arg->kind != AstNode_FieldValue;
} else {
mix = arg->kind == AstNode_FieldValue;
}
if (mix) {
error(arg, "Mixture of `field = value` and value elements in a procedure all is not allowed");
fail = true;
}
}
if (fail) {
return;
}
}
Type *proc_type = c->proc_stack[c->proc_stack.count-1];
TypeProc *pt = &proc_type->Proc;
isize result_count = 0;
if (pt->results) {
result_count = proc_type->Proc.results->Tuple.variables.count;
}
isize result_count_excluding_defaults = result_count;
for (isize i = result_count-1; i >= 0; i--) {
Entity *e = pt->results->Tuple.variables[i];
if (e->kind == Entity_TypeName) {
break;
}
GB_ASSERT(e->kind == Entity_Variable);
if (e->Variable.default_value.kind != ExactValue_Invalid ||
e->Variable.default_is_nil) {
result_count_excluding_defaults--;
continue;
}
break;
}
Array<Operand> operands = {};
defer (array_free(&operands));
if (first_is_field_value) {
array_init_count(&operands, heap_allocator(), rs->results.count);
for_array(i, rs->results) {
AstNode *arg = rs->results[i];
ast_node(fv, FieldValue, arg);
check_expr(c, &operands[i], fv->value);
}
} else {
array_init(&operands, heap_allocator(), 2*rs->results.count);
check_unpack_arguments(c, -1, &operands, rs->results, false);
}
if (first_is_field_value) {
bool *visited = gb_alloc_array(c->allocator, bool, result_count);
for_array(i, rs->results) {
AstNode *arg = rs->results[i];
ast_node(fv, FieldValue, arg);
if (fv->field->kind != AstNode_Ident) {
gbString expr_str = expr_to_string(fv->field);
error(arg, "Invalid parameter name `%s` in return statement", expr_str);
gb_string_free(expr_str);
continue;
}
String name = fv->field->Ident.token.string;
isize index = lookup_procedure_result(pt, name);
if (index < 0) {
error(arg, "No result named `%.*s` for this procedure type", LIT(name));
continue;
}
if (visited[index]) {
error(arg, "Duplicate result `%.*s` in return statement", LIT(name));
continue;
}
visited[index] = true;
Operand *o = &operands[i];
Entity *e = pt->results->Tuple.variables[index];
check_assignment(c, &operands[i], e->type, str_lit("return statement"));
}
for (isize i = 0; i < result_count; i++) {
if (!visited[i]) {
Entity *e = pt->results->Tuple.variables[i];
if (is_blank_ident(e->token)) {
continue;
}
GB_ASSERT(e->kind == Entity_Variable);
if (e->Variable.default_value.kind != ExactValue_Invalid) {
continue;
}
if (e->Variable.default_is_nil) {
continue;
}
gbString str = type_to_string(e->type);
error(node, "Return value `%.*s` of type `%s` is missing in return statement",
LIT(e->token.string), str);
gb_string_free(str);
}
}
} else if (result_count == 0 && rs->results.count > 0) {
error(rs->results[0], "No return values expected");
} else if (operands.count > result_count) {
if (result_count_excluding_defaults < result_count) {
error(node, "Expected a maximum of %td return values, got %td", result_count, operands.count);
} else {
error(node, "Expected %td return values, got %td", result_count, operands.count);
}
} else if (operands.count < result_count_excluding_defaults) {
if (result_count_excluding_defaults < result_count) {
error(node, "Expected a minimum of %td return values, got %td", result_count_excluding_defaults, operands.count);
} else {
error(node, "Expected %td return values, got %td", result_count_excluding_defaults, operands.count);
}
} else {
isize max_count = rs->results.count;
for (isize i = 0; i < max_count; i++) {
Entity *e = pt->results->Tuple.variables[i];
check_assignment(c, &operands[i], e->type, str_lit("return statement"));
}
}
case_end;
case_ast_node(fs, ForStmt, node);
u32 new_flags = mod_flags | Stmt_BreakAllowed | Stmt_ContinueAllowed;
check_open_scope(c, node);
check_label(c, fs->label); // TODO(bill): What should the label's "scope" be?
if (fs->init != nullptr) {
check_stmt(c, fs->init, 0);
}
if (fs->cond != nullptr) {
Operand o = {Addressing_Invalid};
check_expr(c, &o, fs->cond);
if (o.mode != Addressing_Invalid && !is_type_boolean(o.type)) {
error(fs->cond, "Non-boolean condition in `for` statement");
}
}
if (fs->post != nullptr) {
check_stmt(c, fs->post, 0);
if (fs->post->kind != AstNode_AssignStmt &&
fs->post->kind != AstNode_IncDecStmt) {
error(fs->post, "`for` statement post statement must be a simple statement");
}
}
check_stmt(c, fs->body, new_flags);
check_close_scope(c);
case_end;
case_ast_node(rs, RangeStmt, node);
u32 new_flags = mod_flags | Stmt_BreakAllowed | Stmt_ContinueAllowed;
check_open_scope(c, node);
check_label(c, rs->label);
Type *val = nullptr;
Type *idx = nullptr;
Entity *entities[2] = {};
isize entity_count = 0;
AstNode *expr = unparen_expr(rs->expr);
if (is_ast_node_a_range(expr)) {
ast_node(ie, BinaryExpr, expr);
Operand x = {Addressing_Invalid};
Operand y = {Addressing_Invalid};
check_expr(c, &x, ie->left);
if (x.mode == Addressing_Invalid) {
goto skip_expr;
}
check_expr(c, &y, ie->right);
if (y.mode == Addressing_Invalid) {
goto skip_expr;
}
convert_to_typed(c, &x, y.type, 0);
if (x.mode == Addressing_Invalid) {
goto skip_expr;
}
convert_to_typed(c, &y, x.type, 0);
if (y.mode == Addressing_Invalid) {
goto skip_expr;
}
convert_to_typed(c, &x, default_type(y.type), 0);
if (x.mode == Addressing_Invalid) {
goto skip_expr;
}
convert_to_typed(c, &y, default_type(x.type), 0);
if (y.mode == Addressing_Invalid) {
goto skip_expr;
}
if (!are_types_identical(x.type, y.type)) {
if (x.type != t_invalid &&
y.type != t_invalid) {
gbString xt = type_to_string(x.type);
gbString yt = type_to_string(y.type);
gbString expr_str = expr_to_string(x.expr);
error(ie->op, "Mismatched types in interval expression `%s` : `%s` vs `%s`", expr_str, xt, yt);
gb_string_free(expr_str);
gb_string_free(yt);
gb_string_free(xt);
}
goto skip_expr;
}
Type *type = x.type;
if (!is_type_integer(type) && !is_type_float(type) && !is_type_pointer(type)) {
error(ie->op, "Only numerical and pointer types are allowed within interval expressions");
goto skip_expr;
}
if (x.mode == Addressing_Constant &&
y.mode == Addressing_Constant) {
ExactValue a = x.value;
ExactValue b = y.value;
GB_ASSERT(are_types_identical(x.type, y.type));
TokenKind op = Token_Lt;
switch (ie->op.kind) {
case Token_Ellipsis: op = Token_LtEq; break;
case Token_HalfClosed: op = Token_Lt; break;
default: error(ie->op, "Invalid range operator"); break;
}
bool ok = compare_exact_values(op, a, b);
if (!ok) {
// TODO(bill): Better error message
error(ie->op, "Invalid interval range");
goto skip_expr;
}
}
if (x.mode != Addressing_Constant) {
x.value = empty_exact_value;
}
if (y.mode != Addressing_Constant) {
y.value = empty_exact_value;
}
add_type_and_value(&c->info, ie->left, x.mode, x.type, x.value);
add_type_and_value(&c->info, ie->right, y.mode, y.type, y.value);
val = type;
idx = t_int;
} else {
Operand operand = {Addressing_Invalid};
check_expr_or_type(c, &operand, rs->expr);
if (operand.mode == Addressing_Type) {
if (!is_type_enum(operand.type)) {
gbString t = type_to_string(operand.type);
error(operand.expr, "Cannot iterate over the type `%s`", t);
gb_string_free(t);
goto skip_expr;
} else {
val = operand.type;
idx = t_int;
add_type_info_type(c, operand.type);
goto skip_expr;
}
} else if (operand.mode != Addressing_Invalid) {
Type *t = base_type(type_deref(operand.type));
switch (t->kind) {
case Type_Basic:
if (is_type_string(t)) {
val = t_rune;
idx = t_int;
}
break;
case Type_Array:
val = t->Array.elem;
idx = t_int;
break;
case Type_DynamicArray:
val = t->DynamicArray.elem;
idx = t_int;
break;
case Type_Slice:
val = t->Slice.elem;
idx = t_int;
break;
case Type_Vector:
val = t->Vector.elem;
idx = t_int;
break;
case Type_Map:
val = t->Map.value;
idx = t->Map.key;
break;
}
}
if (val == nullptr) {
gbString s = expr_to_string(operand.expr);
gbString t = type_to_string(operand.type);
error(operand.expr, "Cannot iterate over `%s` of type `%s`", s, t);
gb_string_free(t);
gb_string_free(s);
}
}
skip_expr:; // NOTE(zhiayang): again, declaring a variable immediately after a label... weird.
AstNode *lhs[2] = {rs->value, rs->index};
Type * rhs[2] = {val, idx};
for (isize i = 0; i < 2; i++) {
if (lhs[i] == nullptr) {
continue;
}
AstNode *name = lhs[i];
Type * type = rhs[i];
Entity *entity = nullptr;
if (name->kind == AstNode_Ident) {
Token token = name->Ident.token;
String str = token.string;
Entity *found = nullptr;
if (!is_blank_ident(str)) {
found = current_scope_lookup_entity(c->context.scope, str);
}
if (found == nullptr) {
entity = make_entity_variable(c->allocator, c->context.scope, token, type, true);
add_entity_definition(&c->info, name, entity);
} else {
TokenPos pos = found->token.pos;
error(token,
"Redeclaration of `%.*s` in this scope\n"
"\tat %.*s(%td:%td)",
LIT(str), LIT(pos.file), pos.line, pos.column);
entity = found;
}
} else {
error(name, "A variable declaration must be an identifier");
}
if (entity == nullptr) {
entity = make_entity_dummy_variable(c->allocator, c->global_scope, ast_node_token(name));
}
entities[entity_count++] = entity;
if (type == nullptr) {
entity->type = t_invalid;
entity->flags |= EntityFlag_Used;
}
}
for (isize i = 0; i < entity_count; i++) {
add_entity(c, c->context.scope, entities[i]->identifier, entities[i]);
}
check_stmt(c, rs->body, new_flags);
check_close_scope(c);
case_end;
case_ast_node(ms, MatchStmt, node);
Operand x = {};
mod_flags |= Stmt_BreakAllowed;
check_open_scope(c, node);
check_label(c, ms->label); // TODO(bill): What should the label's "scope" be?
if (ms->init != nullptr) {
check_stmt(c, ms->init, 0);
}
if (ms->tag != nullptr) {
check_expr(c, &x, ms->tag);
check_assignment(c, &x, nullptr, str_lit("match expression"));
} else {
x.mode = Addressing_Constant;
x.type = t_bool;
x.value = exact_value_bool(true);
Token token = {};
token.pos = ast_node_token(ms->body).pos;
token.string = str_lit("true");
x.expr = ast_ident(c->curr_ast_file, token);
}
if (is_type_vector(x.type)) {
gbString str = type_to_string(x.type);
error(x.expr, "Invalid match expression type: %s", str);
gb_string_free(str);
break;
}
// NOTE(bill): Check for multiple defaults
AstNode *first_default = nullptr;
ast_node(bs, BlockStmt, ms->body);
for_array(i, bs->stmts) {
AstNode *stmt = bs->stmts[i];
AstNode *default_stmt = nullptr;
if (stmt->kind == AstNode_CaseClause) {
ast_node(cc, CaseClause, stmt);
if (cc->list.count == 0) {
default_stmt = stmt;
}
} else {
error(stmt, "Invalid AST - expected case clause");
}
if (default_stmt != nullptr) {
if (first_default != nullptr) {
TokenPos pos = ast_node_token(first_default).pos;
error(stmt,
"multiple `default` clauses\n"
"\tfirst at %.*s(%td:%td)",
LIT(pos.file), pos.line, pos.column);
} else {
first_default = default_stmt;
}
}
}
Map<TypeAndToken> seen = {}; // NOTE(bill): Multimap
map_init(&seen, heap_allocator());
for_array(i, bs->stmts) {
AstNode *stmt = bs->stmts[i];
if (stmt->kind != AstNode_CaseClause) {
// NOTE(bill): error handled by above multiple default checker
continue;
}
ast_node(cc, CaseClause, stmt);
for_array(j, cc->list) {
AstNode *expr = unparen_expr(cc->list[j]);
if (is_ast_node_a_range(expr)) {
ast_node(ie, BinaryExpr, expr);
Operand lhs = {};
Operand rhs = {};
check_expr(c, &lhs, ie->left);
if (x.mode == Addressing_Invalid) {
continue;
}
if (lhs.mode == Addressing_Invalid) {
continue;
}
check_expr(c, &rhs, ie->right);
if (rhs.mode == Addressing_Invalid) {
continue;
}
if (!is_type_ordered(x.type)) {
gbString str = type_to_string(x.type);
error(x.expr, "Unordered type `%s`, is invalid for an interval expression", str);
gb_string_free(str);
continue;
}
TokenKind op = Token_Invalid;
Operand a = lhs;
Operand b = rhs;
check_comparison(c, &a, &x, Token_LtEq);
if (a.mode == Addressing_Invalid) {
continue;
}
switch (ie->op.kind) {
case Token_Ellipsis: op = Token_GtEq; break;
case Token_HalfClosed: op = Token_Gt; break;
default: error(ie->op, "Invalid interval operator"); continue;
}
check_comparison(c, &b, &x, op);
if (b.mode == Addressing_Invalid) {
continue;
}
switch (ie->op.kind) {
case Token_Ellipsis: op = Token_LtEq; break;
case Token_HalfClosed: op = Token_Lt; break;
default: error(ie->op, "Invalid interval operator"); continue;
}
Operand a1 = lhs;
Operand b1 = rhs;
check_comparison(c, &a1, &b1, op);
} else {
Operand y = {};
check_expr(c, &y, expr);
if (x.mode == Addressing_Invalid ||
y.mode == Addressing_Invalid) {
continue;
}
convert_to_typed(c, &y, x.type, 0);
if (y.mode == Addressing_Invalid) {
continue;
}
// NOTE(bill): the ordering here matters
Operand z = y;
check_comparison(c, &z, &x, Token_CmpEq);
if (z.mode == Addressing_Invalid) {
continue;
}
if (y.mode != Addressing_Constant) {
continue;
}
if (y.value.kind != ExactValue_Invalid) {
HashKey key = hash_exact_value(y.value);
TypeAndToken *found = map_get(&seen, key);
if (found != nullptr) {
gbTempArenaMemory tmp = gb_temp_arena_memory_begin(&c->tmp_arena);
defer (gb_temp_arena_memory_end(tmp));
isize count = multi_map_count(&seen, key);
TypeAndToken *taps = gb_alloc_array(c->tmp_allocator, TypeAndToken, count);
multi_map_get_all(&seen, key, taps);
bool continue_outer = false;
for (isize i = 0; i < count; i++) {
TypeAndToken tap = taps[i];
if (are_types_identical(y.type, tap.type)) {
TokenPos pos = tap.token.pos;
gbString expr_str = expr_to_string(y.expr);
error(y.expr,
"Duplicate case `%s`\n"
"\tprevious case at %.*s(%td:%td)",
expr_str,
LIT(pos.file), pos.line, pos.column);
gb_string_free(expr_str);
continue_outer = true;
break;
}
}
if (continue_outer) {
continue;
}
}
TypeAndToken tap = {y.type, ast_node_token(y.expr)};
multi_map_insert(&seen, key, tap);
}
}
}
check_open_scope(c, stmt);
u32 ft_flags = mod_flags;
if (i+1 < bs->stmts.count) {
ft_flags |= Stmt_FallthroughAllowed;
}
check_stmt_list(c, cc->stmts, ft_flags);
check_close_scope(c);
}
map_destroy(&seen);
check_close_scope(c);
case_end;
case_ast_node(ms, TypeMatchStmt, node);
Operand x = {};
mod_flags |= Stmt_BreakAllowed;
check_open_scope(c, node);
check_label(c, ms->label); // TODO(bill): What should the label's "scope" be?
MatchTypeKind match_type_kind = MatchType_Invalid;
if (ms->tag->kind != AstNode_AssignStmt) {
error(ms->tag, "Expected an `in` assignment for this type match statement");
break;
}
ast_node(as, AssignStmt, ms->tag);
Token as_token = ast_node_token(ms->tag);
if (as->lhs.count != 1) {
syntax_error(as_token, "Expected 1 name before `in`");
break;
}
if (as->rhs.count != 1) {
syntax_error(as_token, "Expected 1 expression after `in`");
break;
}
AstNode *lhs = as->lhs[0];
AstNode *rhs = as->rhs[0];
check_expr(c, &x, rhs);
check_assignment(c, &x, nullptr, str_lit("type match expression"));
match_type_kind = check_valid_type_match_type(x.type);
if (check_valid_type_match_type(x.type) == MatchType_Invalid) {
gbString str = type_to_string(x.type);
error(x.expr, "Invalid type for this type match expression, got `%s`", str);
gb_string_free(str);
break;
}
bool is_ptr = is_type_pointer(x.type);
// NOTE(bill): Check for multiple defaults
AstNode *first_default = nullptr;
ast_node(bs, BlockStmt, ms->body);
for_array(i, bs->stmts) {
AstNode *stmt = bs->stmts[i];
AstNode *default_stmt = nullptr;
if (stmt->kind == AstNode_CaseClause) {
ast_node(cc, CaseClause, stmt);
if (cc->list.count == 0) {
default_stmt = stmt;
}
} else {
error(stmt, "Invalid AST - expected case clause");
}
if (default_stmt != nullptr) {
if (first_default != nullptr) {
TokenPos pos = ast_node_token(first_default).pos;
error(stmt,
"Multiple `default` clauses\n"
"\tfirst at %.*s(%td:%td)", LIT(pos.file), pos.line, pos.column);
} else {
first_default = default_stmt;
}
}
}
if (lhs->kind != AstNode_Ident) {
error(rhs, "Expected an identifier, got `%.*s`", LIT(ast_node_strings[rhs->kind]));
break;
}
Map<bool> seen = {}; // Multimap
map_init(&seen, heap_allocator());
for_array(i, bs->stmts) {
AstNode *stmt = bs->stmts[i];
if (stmt->kind != AstNode_CaseClause) {
// NOTE(bill): error handled by above multiple default checker
continue;
}
ast_node(cc, CaseClause, stmt);
// TODO(bill): Make robust
Type *bt = base_type(type_deref(x.type));
Type *case_type = nullptr;
for_array(type_index, cc->list) {
AstNode *type_expr = cc->list[type_index];
if (type_expr != nullptr) { // Otherwise it's a default expression
Operand y = {};
check_expr_or_type(c, &y, type_expr);
if (match_type_kind == MatchType_Union) {
GB_ASSERT(is_type_union(bt));
bool tag_type_found = false;
for_array(i, bt->Union.variants) {
Type *vt = bt->Union.variants[i];
if (are_types_identical(vt, y.type)) {
tag_type_found = true;
break;
}
}
if (!tag_type_found) {
gbString type_str = type_to_string(y.type);
error(y.expr, "Unknown tag type, got `%s`", type_str);
gb_string_free(type_str);
continue;
}
case_type = y.type;
} else if (match_type_kind == MatchType_Any) {
case_type = y.type;
} else {
GB_PANIC("Unknown type to type match statement");
}
HashKey key = hash_type(y.type);
bool *found = map_get(&seen, key);
if (found) {
TokenPos pos = cc->token.pos;
gbString expr_str = expr_to_string(y.expr);
error(y.expr,
"Duplicate type case `%s`\n"
"\tprevious type case at %.*s(%td:%td)",
expr_str,
LIT(pos.file), pos.line, pos.column);
gb_string_free(expr_str);
break;
}
map_set(&seen, key, cast(bool)true);
}
}
if (is_ptr &&
!is_type_any(type_deref(x.type)) &&
cc->list.count == 1 &&
case_type != nullptr) {
case_type = make_type_pointer(c->allocator, case_type);
}
if (cc->list.count > 1) {
case_type = nullptr;
}
if (case_type == nullptr) {
case_type = x.type;
}
add_type_info_type(c, case_type);
check_open_scope(c, stmt);
{
Entity *tag_var = make_entity_variable(c->allocator, c->context.scope, lhs->Ident.token, case_type, false);
tag_var->flags |= EntityFlag_Used;
tag_var->flags |= EntityFlag_Value;
add_entity(c, c->context.scope, lhs, tag_var);
add_entity_use(c, lhs, tag_var);
add_implicit_entity(c, stmt, tag_var);
}
check_stmt_list(c, cc->stmts, mod_flags);
check_close_scope(c);
}
map_destroy(&seen);
check_close_scope(c);
case_end;
case_ast_node(ds, DeferStmt, node);
if (is_ast_node_decl(ds->stmt)) {
error(ds->token, "You cannot defer a declaration");
} else {
bool out_in_defer = c->context.in_defer;
c->context.in_defer = true;
check_stmt(c, ds->stmt, 0);
c->context.in_defer = out_in_defer;
}
case_end;
case_ast_node(bs, BranchStmt, node);
Token token = bs->token;
switch (token.kind) {
case Token_break:
if ((flags & Stmt_BreakAllowed) == 0) {
error(token, "`break` only allowed in loops or `match` statements");
}
break;
case Token_continue:
if ((flags & Stmt_ContinueAllowed) == 0) {
error(token, "`continue` only allowed in loops");
}
break;
case Token_fallthrough:
if ((flags & Stmt_FallthroughAllowed) == 0) {
error(token, "`fallthrough` statement in illegal position");
}
break;
default:
error(token, "Invalid AST: Branch Statement `%.*s`", LIT(token.string));
break;
}
if (bs->label != nullptr) {
if (bs->label->kind != AstNode_Ident) {
error(bs->label, "A branch statement's label name must be an identifier");
return;
}
AstNode *ident = bs->label;
String name = ident->Ident.token.string;
Operand o = {};
Entity *e = check_ident(c, &o, ident, nullptr, nullptr, false);
if (e == nullptr) {
error(ident, "Undeclared label name: %.*s", LIT(name));
return;
}
add_entity_use(c, ident, e);
if (e->kind != Entity_Label) {
error(ident, "`%.*s` is not a label", LIT(name));
return;
}
}
case_end;
case_ast_node(us, UsingStmt, node);
if (us->list.count == 0) {
error(us->token, "Empty `using` list");
return;
}
for_array(i, us->list) {
AstNode *expr = unparen_expr(us->list[0]);
Entity *e = nullptr;
bool is_selector = false;
Operand o = {};
switch (expr->kind) {
case AstNode_Ident:
e = check_ident(c, &o, expr, nullptr, nullptr, true);
break;
case AstNode_SelectorExpr:
e = check_selector(c, &o, expr, nullptr);
is_selector = true;
break;
case AstNode_Implicit:
error(us->token, "`using` applied to an implicit value");
continue;
default:
error(us->token, "`using` can only be applied to an entity, got %.*s", LIT(ast_node_strings[expr->kind]));
continue;
}
if (!check_using_stmt_entity(c, us, expr, is_selector, e)) {
return;
}
}
case_end;
case_ast_node(pa, PushAllocator, node);
Operand op = {};
check_expr(c, &op, pa->expr);
check_assignment(c, &op, t_allocator, str_lit("argument to push_allocator"));
check_stmt(c, pa->body, mod_flags);
case_end;
case_ast_node(pa, PushContext, node);
Operand op = {};
check_expr(c, &op, pa->expr);
check_assignment(c, &op, t_context, str_lit("argument to push_context"));
check_stmt(c, pa->body, mod_flags);
case_end;
case_ast_node(fb, ForeignBlockDecl, node);
AstNode *foreign_library = fb->foreign_library;
bool ok = true;
if (foreign_library->kind != AstNode_Ident) {
error(foreign_library, "foreign library name must be an identifier");
ok = false;
}
CheckerContext prev_context = c->context;
if (ok) {
c->context.curr_foreign_library = foreign_library;
}
for_array(i, fb->decls) {
AstNode *decl = fb->decls[i];
if (decl->kind == AstNode_ValueDecl && decl->ValueDecl.is_mutable) {
check_stmt(c, decl, flags);
}
}
c->context = prev_context;
case_end;
case_ast_node(vd, ValueDecl, node);
if (!vd->is_mutable) {
break;
}
Entity **entities = gb_alloc_array(c->allocator, Entity *, vd->names.count);
isize entity_count = 0;
if (vd->flags & VarDeclFlag_thread_local) {
vd->flags &= ~VarDeclFlag_thread_local;
error(node, "`thread_local` may only be applied to a variable declaration");
}
for_array(i, vd->names) {
AstNode *name = vd->names[i];
Entity *entity = nullptr;
if (name->kind != AstNode_Ident) {
error(name, "A variable declaration must be an identifier");
} else {
Token token = name->Ident.token;
String str = token.string;
Entity *found = nullptr;
// NOTE(bill): Ignore assignments to `_`
if (!is_blank_ident(str)) {
found = current_scope_lookup_entity(c->context.scope, str);
}
if (found == nullptr) {
entity = make_entity_variable(c->allocator, c->context.scope, token, nullptr, false);
entity->identifier = name;
AstNode *fl = c->context.curr_foreign_library;
if (fl != nullptr) {
GB_ASSERT(fl->kind == AstNode_Ident);
entity->Variable.is_foreign = true;
entity->Variable.foreign_library_ident = fl;
}
} else {
TokenPos pos = found->token.pos;
error(token,
"Redeclaration of `%.*s` in this scope\n"
"\tat %.*s(%td:%td)",
LIT(str), LIT(pos.file), pos.line, pos.column);
entity = found;
}
}
if (entity == nullptr) {
entity = make_entity_dummy_variable(c->allocator, c->global_scope, ast_node_token(name));
}
entity->parent_proc_decl = c->context.curr_proc_decl;
entities[entity_count++] = entity;
}
Type *init_type = nullptr;
if (vd->type != nullptr) {
init_type = check_type(c, vd->type, nullptr);
if (init_type == nullptr) {
init_type = t_invalid;
} else if (is_type_polymorphic(base_type(init_type))) {
gbString str = type_to_string(init_type);
error(vd->type, "Invalid use of a polymorphic type `%s` in variable declaration", str);
gb_string_free(str);
init_type = t_invalid;
}
}
for (isize i = 0; i < entity_count; i++) {
Entity *e = entities[i];
GB_ASSERT(e != nullptr);
if (e->flags & EntityFlag_Visited) {
e->type = t_invalid;
continue;
}
e->flags |= EntityFlag_Visited;
if (e->type == nullptr) {
e->type = init_type;
}
}
check_arity_match(c, vd);
check_init_variables(c, entities, entity_count, vd->values, str_lit("variable declaration"));
for (isize i = 0; i < entity_count; i++) {
Entity *e = entities[i];
if (e->Variable.is_foreign) {
if (vd->values.count > 0) {
error(e->token, "A foreign variable declaration cannot have a default value");
}
init_entity_foreign_library(c, e);
String name = e->token.string;
auto *fp = &c->info.foreigns;
HashKey key = hash_string(name);
Entity **found = map_get(fp, key);
if (found) {
Entity *f = *found;
TokenPos pos = f->token.pos;
Type *this_type = base_type(e->type);
Type *other_type = base_type(f->type);
if (!are_types_identical(this_type, other_type)) {
error(e->token,
"Foreign entity `%.*s` previously declared elsewhere with a different type\n"
"\tat %.*s(%td:%td)",
LIT(name), LIT(pos.file), pos.line, pos.column);
}
} else {
map_set(fp, key, e);
}
}
add_entity(c, c->context.scope, e->identifier, e);
}
if ((vd->flags & VarDeclFlag_using) != 0) {
Token token = ast_node_token(node);
if (vd->type != nullptr && entity_count > 1) {
error(token, "`using` can only be applied to one variable of the same type");
// TODO(bill): Should a `continue` happen here?
}
for (isize entity_index = 0; entity_index < entity_count; entity_index++) {
Entity *e = entities[entity_index];
if (e == nullptr) {
continue;
}
if (e->kind != Entity_Variable) {
continue;
}
bool is_immutable = e->Variable.is_immutable;
String name = e->token.string;
Type *t = base_type(type_deref(e->type));
if (is_type_struct(t) || is_type_raw_union(t)) {
Scope *scope = scope_of_node(&c->info, t->Struct.node);
for_array(i, scope->elements.entries) {
Entity *f = scope->elements.entries[i].value;
if (f->kind == Entity_Variable) {
Entity *uvar = make_entity_using_variable(c->allocator, e, f->token, f->type);
uvar->Variable.is_immutable = is_immutable;
Entity *prev = scope_insert_entity(c->context.scope, uvar);
if (prev != nullptr) {
error(token, "Namespace collision while `using` `%.*s` of: %.*s", LIT(name), LIT(prev->token.string));
return;
}
}
}
} else {
// NOTE(bill): skip the rest to remove extra errors
error(token, "`using` can only be applied to variables of type struct or raw_union");
return;
}
}
}
case_end;
}
}
|