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
|
// Copyright (c) Herb Sutter
// SPDX-License-Identifier: CC-BY-NC-ND-4.0
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//===========================================================================
// Semantic analysis
//===========================================================================
#ifndef CPP2_SEMA_H
#define CPP2_SEMA_H
#include "reflect.h"
namespace cpp2 {
auto parser::apply_type_metafunctions( declaration_node& n )
-> bool
{
assert(n.is_type());
// Get the reflection state ready to pass to the function
auto cs = meta::compiler_services{ &errors, generated_tokens };
auto rtype = meta::type_declaration{ &n, cs };
return apply_metafunctions(
n,
rtype,
[&](std::string const& msg) { error( msg, false ); }
);
}
//-----------------------------------------------------------------------
//
// Symbol/scope table
//
//-----------------------------------------------------------------------
//
struct declaration_sym {
bool start = false;
declaration_node const* declaration = {};
token const* identifier = {};
statement_node const* initializer = {};
parameter_declaration_node const* parameter = {};
bool member = false;
declaration_sym(
bool s = false,
declaration_node const* decl = {},
token const* id = {},
statement_node const* init = {},
parameter_declaration_node const* param = {},
bool mem = false
)
: start{s}
, declaration{decl}
, identifier{id}
, initializer{init}
, parameter{param}
, member{mem}
{ }
auto position() const
-> source_position
{
assert (declaration);
return declaration->position();
}
};
struct identifier_sym {
bool standalone_assignment_to = false;
token const* identifier = {};
identifier_sym(
bool a,
token const* id
)
: standalone_assignment_to{a}
, identifier{id}
{ }
auto position() const
-> source_position
{
assert (identifier);
return identifier->position();
}
};
struct selection_sym {
bool start = false;
selection_statement_node const* selection = {};
selection_sym(
bool s,
selection_statement_node const* sel
)
: start{s}
, selection{sel}
{ }
auto position() const
-> source_position
{
assert (selection);
return selection->position();
}
};
struct compound_sym {
bool start = false;
compound_statement_node const* compound = {};
enum kind { is_scope, is_true, is_false } kind_ = is_scope;
compound_sym(
bool s,
compound_statement_node const* c,
kind k
)
: start{s}
, compound{c}
, kind_{k}
{ }
auto position() const
-> source_position
{
assert (compound);
return compound->position();
}
};
struct symbol {
int depth = -1;
enum active { declaration=0, identifier, selection, compound };
std::variant <
declaration_sym,
identifier_sym,
selection_sym,
compound_sym
> sym;
bool start = true;
symbol(int depth, declaration_sym const& sym) : depth{depth}, sym{sym}, start{sym.start} { }
symbol(int depth, identifier_sym const& sym) : depth{depth}, sym{sym} { }
symbol(int depth, selection_sym const& sym) : depth{depth}, sym{sym}, start{sym.start} { }
symbol(int depth, compound_sym const& sym) : depth{depth}, sym{sym}, start{sym.start} { }
auto position() const
-> source_position
{
switch (sym.index())
{
break;case declaration: {
auto const& s = std::get<declaration>(sym);
return s.position();
}
break;case identifier: {
auto const& s = std::get<identifier>(sym);
return s.position();
}
break;case selection: {
auto const& s = std::get<selection>(sym);
return s.position();
}
break;case compound: {
auto const& s = std::get<compound>(sym);
return s.position();
}
break;default:
assert (!"illegal symbol state");
return { 0, 0 };
}
}
};
// Keep a list of all token*'s found that are definite first uses
// of the form "x = expr;" for an uninitialized local variable x,
// which we will rewrite to construct the local variable.
//
std::vector<token const*> definite_initializations;
auto is_definite_initialization(token const* t)
-> bool
{
return
std::find(
definite_initializations.begin(),
definite_initializations.end(),
t
)
!= definite_initializations.end();
}
// Keep a list of all token*'s found that are definite last uses
// for a local variable or copy or forward parameter x, which we
// will rewrite to move or forward from the variable.
//
struct last_use {
token const* t;
bool is_forward;
last_use(
token const* t_,
bool is_forward_ = false
)
: t{t_}
, is_forward{is_forward_}
{ }
bool operator==(last_use const& that) { return t == that.t; }
};
std::vector<last_use> definite_last_uses;
auto is_definite_last_use(token const* t)
-> last_use const*
{
auto iter = std::find(
definite_last_uses.begin(),
definite_last_uses.end(),
t
);
if (iter != definite_last_uses.end()) {
return &*iter;
}
else {
return {};
}
}
//-----------------------------------------------------------------------
//
// sema: Semantic analysis
//
//-----------------------------------------------------------------------
//
class sema
{
public:
std::vector<error_entry>& errors;
std::vector<symbol> symbols;
std::vector<selection_statement_node const*> active_selections;
public:
//-----------------------------------------------------------------------
// Constructor
//
// errors error list
//
sema(
std::vector<error_entry>& errors_
)
: errors{ errors_ }
{
}
// Get the declaration of t within the same named function or beyond it
//
auto get_declaration_of(
token const* t,
bool look_beyond_current_function = false
)
-> declaration_sym const*
{
if (!t) {
return {};
}
return get_declaration_of(*t, look_beyond_current_function);
}
auto get_declaration_of(
token const& t,
bool look_beyond_current_function = false
)
-> declaration_sym const*
{
// First find the position the query is coming from
// and remember its depth
auto i = symbols.cbegin();
while (
i != symbols.cend()
&& i->position() < t.position()
)
{
++i;
}
while (
i == symbols.cend()
|| !i->start
)
{
if (i == symbols.cbegin()) {
return nullptr;
}
--i;
}
auto depth = i->depth;
// Then look backward to find the first declaration of
// this name that is not deeper (in a nested scope)
// and is in the same function
for (
auto ri = std::make_reverse_iterator(i+1);
ri != symbols.crend() && ri->position() <= t.position(); // TODO: See pure2-deducing-pointers-error.cpp2
++ri
)
{
if (
ri->sym.index() == symbol::active::declaration
&& ri->depth <= depth
)
{
auto const& decl = std::get<symbol::active::declaration>(ri->sym);
// Conditionally look beyond the start of the current named (has identifier) function
// (an unnamed function is ok to look beyond)
assert(decl.declaration);
if (
decl.declaration->type.index() == declaration_node::a_function
&& decl.declaration->identifier
&& !look_beyond_current_function
)
{
return nullptr;
}
// If the name matches, this is it
if (
decl.identifier
&& *decl.identifier == t
)
{
return &decl;
}
depth = ri->depth;
}
}
return nullptr;
}
//-----------------------------------------------------------------------
// Factor out the uninitialized var decl test
//
auto is_uninitialized_decl(declaration_sym const& sym)
-> bool
{
return
sym.start
&& !(sym.identifier && *sym.identifier == "this")
&& !sym.initializer
&& !(sym.parameter && sym.parameter->pass != passing_style::out)
;
}
auto debug_print(std::ostream& o)
-> void
{
for (auto const& s : symbols)
{
o << std::setw(3) << s.depth << " |";
o << std::setw(s.depth*2+1) << " ";
switch (s.sym.index()) {
break;case symbol::active::declaration: {
auto const& sym = std::get<symbol::active::declaration>(s.sym);
assert (sym.declaration);
if (sym.declaration->is_function()) {
if (sym.start) {
o << "function ";
}
else {
o << "/function";
}
}
else if (sym.declaration->is_object()) {
if (sym.start) {
o << "var ";
}
else {
o << "/var ";
}
}
if (sym.start && sym.identifier) {
o << sym.identifier->to_string();
}
if (is_uninitialized_decl(sym)) {
o << " *** UNINITIALIZED";
}
}
break;case symbol::active::identifier: {
auto const& sym = std::get<symbol::active::identifier>(s.sym);
assert (sym.identifier);
if (auto use = is_definite_last_use(sym.identifier)) {
o << "*** " << sym.identifier->position().to_string()
<< " DEFINITE LAST "
<< (use->is_forward ? "FORWARDING" : "POTENTIALLY MOVING")
<< " USE OF ";
}
if (is_definite_initialization(sym.identifier)) {
o << "*** " << sym.identifier->position().to_string()
<< " DEFINITE INITIALIZATION OF ";
}
else if (sym.standalone_assignment_to) {
o << "*** assignment to ";
}
else {
o << "*** use of ";
}
o << sym.identifier->to_string();
}
break;case symbol::active::selection: {
auto const& sym = std::get<symbol::active::selection>(s.sym);
if (!sym.start) {
o << "/";
}
o << "selection";
}
break;case symbol::active::compound: {
auto const& sym = std::get<symbol::active::compound>(s.sym);
if (!sym.start) {
o << "/";
--scope_depth;
}
if (sym.kind_ == sym.is_true) {
o << "true branch";
}
else if (sym.kind_ == sym.is_false) {
o << "false branch";
}
else {
o << "scope";
}
}
break;default:
o << "ERROR";
}
o << "\n";
}
}
//-----------------------------------------------------------------------
// Apply local first- and last-use rules
//
auto apply_local_rules()
-> bool
{
auto ret = true;
//-----------------------------------------------------------------------
// Helpers for readability
// It's an uninitialized variable (incl. named return values) if it's
// a non-namespace-scope non-parameter object with no initializer
//
auto is_uninitialized_variable_decl = [&](symbol const& s)
-> declaration_sym const*
{
if (auto const* sym = std::get_if<symbol::active::declaration>(&s.sym)) {
assert (sym);
if (is_uninitialized_decl(*sym)) {
if (
sym->declaration->is_object()
&& !sym->declaration->parent_is_namespace()
)
{
return sym;
}
else {
return {};
}
}
}
return {};
};
// It's a local (incl. named return value or copy or move or forward parameter)
//
auto is_potentially_movable_local = [&](symbol const& s)
-> declaration_sym const*
{
if (auto const* sym = std::get_if<symbol::active::declaration>(&s.sym)) {
if (
sym->start
&& sym->declaration->is_object()
&& (!sym->parameter
|| sym->parameter->pass == passing_style::copy
|| sym->parameter->pass == passing_style::move
|| sym->parameter->pass == passing_style::forward
)
)
{
// Must be in function scope
if (
sym->declaration->parent_declaration
&& sym->declaration->parent_declaration->is_function()
)
{
return sym;
}
else {
return {};
}
}
}
return {};
};
//-----------------------------------------------------------------------
// Function logic: For each entry in the table...
//
for (auto sympos = std::ssize(symbols) - 1; sympos >= 0; --sympos)
{
// If this is an uninitialized local variable,
// ensure it is definitely initialized and tag those initializations
//
if (auto decl = is_uninitialized_variable_decl(symbols[sympos])) {
assert(
decl->identifier
&& !decl->initializer
);
ret = ret
&& ensure_definitely_initialized(decl, sympos+1, symbols[sympos].depth)
;
}
// If this is a copy, move, or forward parameter or a local variable,
// identify and tag its definite last uses to `std::move` from them
//
if (auto decl = is_potentially_movable_local(symbols[sympos])) {
assert (decl->identifier);
find_definite_last_uses(
decl->identifier,
sympos,
decl->parameter && decl->parameter->pass == passing_style::forward
);
}
}
return ret;
}
private:
// Find the definite last uses for local variable *id starting at the
// given position and depth in the symbol/scope table
//
auto find_definite_last_uses(
token const* id,
int pos,
bool is_forward
) const
-> void
{
auto i = pos;
auto depth = symbols[pos].depth;
// Maintain a stack of the depths of the most recently seen
// selection statements, using the current depth-2 as a sentinel
auto selections = std::vector<int>{depth-2};
// Scan forward to the end of this scope, keeping track of
// the trailing nest of selection statements
while (
i+1 < std::ssize(symbols)
&& symbols[i+1].depth >= depth
)
{
assert (std::ssize(symbols) > 1);
if (symbols[i].sym.index() == symbol::selection) {
auto const& s = std::get<symbol::selection>(symbols[i].sym);
if (s.start) {
selections.push_back(symbols[i].depth);
}
//else {
// assert (symbols[i].depth-1 == selections.back());
// selections.pop_back();
//}
}
++i;
}
// i is now at the end of id's scope, so start scanning backwards
// until we find the first definite last use
for (auto found = false; i > pos; --i)
{
// Once we find something, don't continue back further
// than the closest enclosing selection statement
if (
found
&& symbols[i].depth <= selections.back()
)
{
break;
}
if (symbols[i].sym.index() == symbol::active::identifier)
{
auto const& sym = std::get<symbol::active::identifier>(symbols[i].sym);
assert (sym.identifier);
// If we find a use of this identifier
if (*sym.identifier == *id)
{
if (
!found
|| symbols[i].depth > selections.back()+1
)
{
definite_last_uses.emplace_back( sym.identifier, is_forward );
found = true;
}
// Pop any of the last branches that we're outside of
while (symbols[i].depth <= selections.back()) {
selections.pop_back();
assert (!selections.empty()); // won't remove sentinel
}
// Then skip over the earlier part of the current branch
while (
i > pos
&& symbols[i].depth > selections.back() + 1
)
{
--i;
}
}
}
}
// If we arrived back at the declaration without finding a use
// and this isn't generated code (ignore that for now)
// and this is a user-named object (not 'this', 'that', or '_')
if (
i == pos
&& id->position().lineno > 0
&& *id != "this"
&& *id != "that"
&& *id != "_"
)
{
errors.emplace_back(
id->position(),
"local variable " + id->to_string() + " is not used; consider changing its name to '_' to make it explicitly anonymous, or removing it entirely if its side effects are not needed"
);
}
}
// Check that local variable *id is initialized before use on all paths
// starting at the given position and depth in the symbol/scope table
//
// TODO: After writing the first version of this, I realized that it could be
// simplified a lot by using a sentinel value to represent the base case like
// the others instead of as a special case. It's tempting to rewrite this now
// to do that cleanup, but the code is working and fully localized, so
// rewriting it wouldn't give any benefit, and I need to resist the urge to
// be distracted by goldplating when I could be implementing a new feature.
//
auto ensure_definitely_initialized(
declaration_sym const* decl,
int pos,
int depth
) const
-> bool
{
// If this is a member variable in a constructor, the name doesn't
// appear lexically right in the constructor, so prepending "this."
// to the printed name might make the error more readable to the programmer
auto name = decl->identifier->to_string();
if (decl->declaration->parent_is_type()) {
name += " (aka this." + name + ")";
}
struct stack_entry{
int pos; // start of this selection statement
struct branch {
int start;
bool result = false;
branch(int s, bool r) : start{s}, result{r} { }
};
std::vector<branch> branches;
stack_entry(int p) : pos{p} { }
auto debug_print(std::ostream& o) const -> void
{
o << "Stack entry: " << pos << "\n";
for (auto const& e : branches) {
o << " ( " << e.start << " , " << e.result << " )\n";
}
}
};
std::vector<stack_entry> selection_stack;
for (
;
pos < std::ssize(symbols) && symbols[pos].depth >= depth;
++pos
)
{
switch (symbols[pos].sym.index()) {
break;case symbol::active::declaration: {
auto const& sym = std::get<symbol::active::declaration>(symbols[pos].sym);
if (
sym.start
&& sym.identifier
&& *sym.identifier == *decl->identifier
)
{
errors.emplace_back(
sym.identifier->position(),
"local variable " + sym.identifier->to_string()
+ " cannot have the same name as an uninitialized"
" variable in the same function");
}
}
break;case symbol::active::identifier: {
auto const& sym = std::get<symbol::active::identifier>(symbols[pos].sym);
assert (sym.identifier);
if (is_definite_initialization(sym.identifier)) {
errors.emplace_back(
sym.identifier->position(),
"local variable " + name
+ " must be initialized before " + sym.identifier->to_string()
+ " (local variables must be initialized in the order they are declared)"
);
return false;
}
// If we find a use of this identifier
if (*sym.identifier == *decl->identifier) {
// If we're not inside a selection statement, we're at the top level --
// just return true if it's an assignment to it, else return false
if (std::ssize(selection_stack) == 0) {
if (sym.standalone_assignment_to) {
definite_initializations.push_back( sym.identifier );
}
else {
errors.emplace_back(
sym.identifier->position(),
"local variable " + name
+ " is used before it was initialized");
}
return sym.standalone_assignment_to;
}
// Else if we're inside a selection statement but still in the condition
// portion (there are no branches entered yet)
else if (std::ssize(selection_stack.back().branches) == 0) {
// If this is a top-level selection statement, handle it the same as
// if we weren't an a selection statement
if (std::ssize(selection_stack) == 1) {
if (sym.standalone_assignment_to) {
definite_initializations.push_back( sym.identifier );
}
else {
errors.emplace_back(
sym.identifier->position(),
"local variable " + name
+ " is used in a condition before it was initialized");
}
return sym.standalone_assignment_to;
}
// Else we can skip the rest of this selection statement, and record
// this as the result of the next outer selection statement's current branch
else {
selection_stack.pop_back();
assert (std::ssize(selection_stack.back().branches) > 0);
selection_stack.back().branches.back().result = sym.standalone_assignment_to;
int this_depth = symbols[pos].depth;
while (symbols[pos + 1].depth >= this_depth) {
++pos;
}
}
}
// Else we're in a selection branch and can skip the rest of this branch
// and record this as the result for the current branch
else {
if (sym.standalone_assignment_to) {
definite_initializations.push_back( sym.identifier );
}
else {
errors.emplace_back(
sym.identifier->position(),
"local variable " + name
+ " is used in a branch before it was initialized");
}
selection_stack.back().branches.back().result = sym.standalone_assignment_to;
// The depth of this branch should always be the depth of
// the current selection statement + 1
int branch_depth = symbols[selection_stack.back().pos].depth + 1;
while (symbols[pos + 1].depth > branch_depth) {
++pos;
}
}
}
}
break;case symbol::active::selection: {
auto const& sym = std::get<symbol::active::selection>(symbols[pos].sym);
// If we're starting a new selection statement, add a stack entry for it
if (sym.start) {
selection_stack.emplace_back( pos );
}
// If we're ending a selection statement, look at the partial results --
// they must all be false or all true, if they're a mix we are missing
// initializations on some path(s)
else {
assert (std::ssize(selection_stack) > 0);
auto true_branches = std::string{};
auto false_branches = std::string{};
for (auto const& b : selection_stack.back().branches)
{
// If this is not an implicit 'else' branch (i.e., if lineno > 0)
if (symbols[b.start].position().lineno > 0) {
(b.result ? true_branches : false_branches)
+= "\n branch starting at line "
+ std::to_string(symbols[b.start].position().lineno);
}
else {
(b.result ? true_branches : false_branches)
+= "\n implicit else branch";
}
}
// If none of the branches was true
if (true_branches.length() == 0)
{
selection_stack.pop_back();
// Nothing else to do, just continue
}
// Else if all of the branches were true
else if (false_branches.length() == 0)
{
// If this is a top-level selection statement, handle it the same as
// if we weren't an a selection statement
if (std::ssize(selection_stack) == 1) {
return true;
}
// Else pop this selection statement, and record this as the result
// of the next outer selection statement's current branch
else {
selection_stack.pop_back();
assert (std::ssize(selection_stack.back().branches) > 0);
selection_stack.back().branches.back().result = true;
// And skip the rest of this branch
auto skip_depth = symbols[pos].depth - 1;
while (symbols[pos + 1].depth >= skip_depth) {
++pos;
}
}
}
// Else we found a missing initializion, report it and return false
else
{
errors.emplace_back(
decl->identifier->position(),
"local variable " + name
+ " must be initialized on both branches or neither branch");
assert (symbols[selection_stack.back().pos].sym.index() == symbol::active::selection);
auto const& sym = std::get<symbol::active::selection>(symbols[pos].sym);
errors.emplace_back(
sym.selection->identifier->position(),
"\"" + sym.selection->identifier->to_string()
+ "\" initializes " + name
+ " on:" + true_branches
+ "\nbut not on:" + false_branches
);
return false;
}
}
}
break;case symbol::active::compound: {
auto const& sym = std::get<symbol::active::compound>(symbols[pos].sym);
// If we're in a selection
if (std::ssize(selection_stack) > 0) {
// If this is a compound start with the current selection's depth
// plus one, it's the start of one of the branches of that selection
if (
sym.start
&& symbols[pos].depth == symbols[selection_stack.back().pos].depth+1
)
{
selection_stack.back().branches.emplace_back( pos, false );
}
}
}
break;default:
assert (!"illegal symbol");
}
}
errors.emplace_back(
decl->identifier->position(),
name
+ " - variable must be initialized on every branch path");
return false;
}
public:
//-----------------------------------------------------------------------
// Per-node sema rules
//
auto check(qualified_id_node const& n)
{
// Check for some incorrect uses of .
if (auto decl = get_declaration_of(n.get_first_token(), true);
decl && std::ssize(n.ids) > 1
)
{
assert (decl->declaration);
if (
decl->declaration->is_object()
&& n.ids[1].scope_op
&& n.ids[1].scope_op->type() == lexeme::Scope
)
{
errors.emplace_back(
n.position(),
"use '" + decl->identifier->to_string() + ".' to refer to an object member"
);
return false;
}
}
return true;
}
auto check(postfix_expression_node const& n)
{
// Check for some incorrect uses of :: or .
if (auto decl = get_declaration_of(n.get_first_token_ignoring_this(), true);
decl && !n.ops.empty()
)
{
assert (decl->declaration);
if (
decl->declaration->is_type()
&& n.ops[0].op
&& n.ops[0].op->type() == lexeme::Dot
)
{
errors.emplace_back(
n.position(),
"use '" + decl->identifier->to_string() + "::' to refer to a type member"
);
return false;
}
}
return true;
}
auto check(parameter_declaration_node const& n)
-> bool
{
auto type_name = std::string{};
if (n.declaration->has_declared_return_type()) {
type_name = n.declaration->get_object_type()->to_string();
}
if (
n.ordinal == 2
&& !n.has_name("that")
&& n.declaration->parent_declaration
&& n.declaration->parent_declaration->has_name("operator=")
&& n.declaration->parent_declaration->parent_declaration
&& n.declaration->parent_declaration->parent_declaration->name()
&& type_name == *n.declaration->parent_declaration->parent_declaration->name()
)
{
errors.emplace_back(
n.position(),
"if an 'operator=' second parameter is of the same type (here '" + type_name + "'), it must be named 'that'"
);
return false;
}
return true;
}
auto check(declaration_node const& n)
-> bool
{
// An object of deduced type must have an initializer
if (
n.is_object()
&& n.has_wildcard_type()
&& !n.has_initializer()
)
{
errors.emplace_back(
n.position(),
"an object with a deduced type must have an = initializer"
);
return false;
}
// An object initializer must be an expression
if (
n.is_object()
&& n.initializer
&& !n.initializer->is_expression()
)
{
errors.emplace_back(
n.position(),
"an object initializer must be an expression"
);
return false;
}
// A namespace must be initialized with a compound expression
if (
n.is_namespace()
&& (
!n.initializer
|| !n.initializer->is_compound()
)
)
{
errors.emplace_back(
n.position(),
"a namespace must be = initialized with a { } body containing declarations"
);
return false;
}
// A function body must be an expression-statement or a compound-statement
if (
n.is_function()
&& n.initializer
&& n.initializer->is_return()
)
{
errors.emplace_back(
n.position(),
"a function with a single-expression body doesn't need to say 'return' - either omit 'return' or write a full { }-enclosed function body"
);
return false;
}
// A nonvirtual and nondefaultable function must have an initializer
if (
n.is_function()
&& !n.is_virtual_function()
&& !n.is_defaultable_function()
&& !n.has_initializer()
)
{
errors.emplace_back(
n.position(),
"a function must have a body ('=' initializer), unless it is virtual (has a 'virtual this' parameter) or is defaultable (operator== or operator<=>)"
);
return false;
}
if (
n.is_type()
&& !n.parent_is_namespace()
&& !n.parent_is_type()
)
{
errors.emplace_back(
n.position(),
"(temporary alpha limitation) a type must be in a namespace or type scope - function-local types are not yet supported"
);
return false;
}
// A type scope variable must have a declared type
if (
n.parent_is_type()
&& n.has_wildcard_type()
)
{
errors.emplace_back(
n.position(),
"a type scope variable must have a declared type"
);
return false;
}
// A 'this' declaration must be an ordinary parameter or a type-scope object
if (n.identifier && *n.identifier->identifier == "this")
{
if (
n.is_template_parameter
|| (
!n.is_parameter
&& !n.parent_is_type()
)
)
{
errors.emplace_back(
n.identifier->position(),
"'this' may only be declared as an ordinary function parameter or type-scope (base) object"
);
return {};
}
}
{
auto this_index = n.index_of_parameter_named("this");
auto that_index = n.index_of_parameter_named("that");
if (this_index >= 0) {
if (!n.parent_is_type()) {
errors.emplace_back(
n.position(),
"'this' must be the first parameter of a type-scope function"
);
return false;
}
if (this_index != 0) {
errors.emplace_back(
n.position(),
"'this' must be the first parameter"
);
return false;
}
}
if (that_index >= 0) {
if (!n.parent_is_type()) {
errors.emplace_back(
n.position(),
"'that' must be the second parameter of a type-scope function"
);
return false;
}
if (that_index != 1) {
errors.emplace_back(
n.position(),
"'that' must be the second parameter"
);
return false;
}
}
}
if (
n.is_object()
&& n.has_wildcard_type()
&& n.parent_is_namespace()
)
{
errors.emplace_back(
n.identifier->position(),
"namespace scope objects must have a concrete type, not a deduced type"
);
return false;
}
if (
n.has_name("_")
&& !n.is_object()
&& !n.is_namespace()
&& !n.is_object_alias()
)
{
errors.emplace_back(
n.identifier->position(),
"'_' (wildcard) may not be the name of a function or type - it may only be used as the name of an anonymous object, object alias, or namespace"
);
return false;
}
if (
n.has_name("this")
&& n.parent_is_type()
)
{
if (!n.is_object()) {
errors.emplace_back(
n.position(),
"a member named 'this' declares a base subobject, and must be followed by a base type name"
);
return false;
}
if (
!n.is_public()
&& !n.is_default_access()
)
{
errors.emplace_back(
n.position(),
"a base type must be public (the default)"
);
return false;
}
if (n.has_wildcard_type())
{
errors.emplace_back(
n.position(),
"a base type must be a specific type, not a deduced type (omitted or '_'-wildcarded)"
);
return false;
}
}
if (
n.access != accessibility::default_
&& !n.parent_is_type()
)
{
errors.emplace_back(
n.position(),
"an access-specifier is only allowed on a type-scope (member) declaration"
);
return false;
}
if (n.is_constructor())
{
auto& func = std::get<declaration_node::a_function>(n.type);
assert(
func->parameters->ssize() > 0
&& (*func->parameters)[0]->has_name("this")
);
if ((*func->parameters)[0]->is_polymorphic()) {
errors.emplace_back(
n.position(),
"a constructor may not be declared virtual, override, or final"
);
return false;
}
}
if (
n.is_function()
&& n.has_name()
&& n.parent_is_function()
)
{
assert (n.identifier->get_token());
auto name = n.identifier->get_token()->to_string();
errors.emplace_back(
n.position(),
"(temporary alpha limitation) local functions like '" + name + ": (/*params*/) = {/*body*/}' are not currently supported - write a local variable initialized with an unnamed function like '" + name + " := :(/*params*/) = {/*body*/};' instead (add '=' and ';')"
);
return false;
}
// Ban overloading operators &&, ||, and , (comma)
if (
n.identifier
&& n.is_function()
&& (
n.has_name("operator&&")
|| n.has_name("operator||")
|| (n.has_name("operator&") && n.parameter_count() < 2)
|| n.has_name("operator,")
)
)
{
errors.emplace_back(
n.position(),
"overloading '" + n.name()->to_string() + "' is not allowed"
);
return false;
}
// Require that ~/comparison/assignment operators must be members
if (
n.identifier
&& !n.is_function_with_this()
&& (
// Note re comparisons: The reason I'm restricting comparisons to be members
// is because with comparison symmetry (since C++20, derived from Cpp2)
// there's no longer a need for a type author to write them as nonmembers,
// and I want to discourage that habit by banning nonmembers. However, there
// could be a motivation to write them as nonmembers in the case where the
// type author doesn't provide them -- if that turns out to be important we
// can remove the restriction on nonmember comparisons here
n.is_comparison()
// The following would be rejected anyway by the Cpp1 compiler,
// but including them here gives nicer and earlier error messages
|| n.has_name("operator~")
|| n.is_compound_assignment()
)
)
{
errors.emplace_back(
n.position(),
n.name()->to_string() + " must have 'this' as the first parameter"
);
return false;
}
// If this is the main function, it must be 'main: ()' or 'main: (args)'
if (
n.identifier
&& n.has_name("main")
&& n.is_function()
&& n.is_global()
)
{
auto& func = std::get<declaration_node::a_function>(n.type);
auto& params = func->parameters->parameters;
// It's more readable to express this as positive condition here...
if (
// There are no parameters
params.empty()
// Or there's a single wildcard in-param named 'args'
|| (
params.size() == 1
&& params[0]->has_name("args")
&& params[0]->pass == passing_style::in
&& params[0]->declaration->is_object()
&& std::get<declaration_node::an_object>(params[0]->declaration->type)->is_wildcard()
)
)
{
; // ok
}
// ... and if it isn't that, then complain
else
{
errors.emplace_back(
params[0]->position(),
"'main' must be declared as 'main: ()' with zero parameters, or 'main: (args)' with one parameter named 'args' for which the type 'std::vector<std::string_view>' will be deduced"
);
return false;
}
}
if (n.has_name("operator="))
{
if (!n.is_function())
{
errors.emplace_back(
n.position(),
"'operator=' must be a function"
);
return false;
}
auto& func = std::get<declaration_node::a_function>(n.type);
if (func->has_declared_return_type())
{
errors.emplace_back(
func->parameters->parameters[0]->position(),
"'operator=' may not have a declared return type"
);
return false;
}
if (func->parameters->ssize() == 0)
{
errors.emplace_back(
n.position(),
"an operator= function must have a parameter"
);
return false;
}
else if (
(*func->parameters)[0]->has_name("this")
&& (*func->parameters)[0]->pass != passing_style::inout
&& (*func->parameters)[0]->pass != passing_style::out
&& (*func->parameters)[0]->pass != passing_style::move
)
{
errors.emplace_back(
n.position(),
"an operator= function's 'this' parameter must be inout, out, or move"
);
return false;
}
if (
func->parameters->ssize() > 1
&& (*func->parameters)[1]->has_name("that")
&& (*func->parameters)[1]->pass != passing_style::in
&& (*func->parameters)[1]->pass != passing_style::move
)
{
errors.emplace_back(
n.position(),
"an operator= function's 'that' parameter must be in or move"
);
return false;
}
if (
func->parameters->ssize() > 1
&& (*func->parameters)[0]->has_name("this")
&& (*func->parameters)[0]->pass == passing_style::move
)
{
errors.emplace_back(
n.position(),
"a destructor may not have other parameters besides 'this'"
);
return false;
}
}
for (auto& decl : n.get_type_scope_declarations())
{
if (decl->has_name("that"))
{
errors.emplace_back(
n.position(),
"'that' may not be used as a type scope name"
);
return false;
}
}
if (
n.is_binary_comparison_function()
&& !n.has_bool_return_type()
)
{
errors.emplace_back(
n.position(),
n.name()->to_string() + " must return bool"
);
return false;
}
if (n.has_name("operator<=>")) {
auto return_name = n.unnamed_return_type_to_string();
if (
return_name != "_"
&& return_name.find("strong_ordering" ) == return_name.npos
&& return_name.find("weak_ordering" ) == return_name.npos
&& return_name.find("partial_ordering") == return_name.npos
)
{
errors.emplace_back(
n.position(),
"operator<=> must return std::strong_ordering, std::weak_ordering, or std::partial_ordering"
);
return false;
}
}
if (n.is_type()) {
auto compound_stmt = n.initializer->get_if<compound_statement_node>();
assert (compound_stmt);
for (auto& stmt : compound_stmt->statements) {
if (
!stmt->is_declaration()
&& !stmt->is_using()
)
{
errors.emplace_back(
stmt->position(),
"a user-defined type body must contain only declarations or 'using' statements, not other code"
);
return false;
}
}
}
return true;
}
auto check(function_type_node const& n)
-> bool
{
assert(n.parameters);
// An increment/decrement function must have a single 'inout' parameter,
// and if it's a member flag it if we know the type is not copyable
if (
n.my_decl->has_name("operator++")
|| n.my_decl->has_name("operator--")
)
{
if (
(*n.parameters).ssize() != 1
|| (*n.parameters)[0]->direction() != passing_style::inout
)
{
errors.emplace_back(
n.position(),
"a user-defined " + n.my_decl->name()->to_string() + " must have a single 'inout' parameter"
);
return false;
}
if (n.has_deduced_return_type()) {
errors.emplace_back(
n.position(),
"a user-defined " + n.my_decl->name()->to_string() + " must have a specific (not deduced) return type"
);
return false;
}
if (
n.my_decl->parent_declaration
&& n.my_decl->parent_declaration->cannot_be_a_copy_constructible_type()
)
{
errors.emplace_back(
n.position(),
"a user-defined " + n.my_decl->name()->to_string() + " in type scope must be a member of a copyable type"
);
return false;
}
}
return true;
}
auto check(statement_node const& n)
-> bool
{
if (auto expr_stmt = n.get_if<expression_statement_node>();
expr_stmt
&& n.compound_parent
&& (
expr_stmt->expr->is_identifier()
|| expr_stmt->expr->is_id_expression()
|| expr_stmt->expr->is_literal()
)
)
{
errors.emplace_back(
n.position(),
"unused literal or identifier"
);
return false;
}
return true;
}
//-----------------------------------------------------------------------
// Visitor functions
//
int scope_depth = 0;
bool started_standalone_assignment_expression = false;
bool started_postfix_expression = false;
bool is_out_expression = false;
bool inside_next_expression = false;
bool inside_parameter_list = false;
bool inside_parameter_identifier = false;
bool inside_returns_list = false;
bool just_entered_for = false;
parameter_declaration_node const* inside_out_parameter = {};
auto start(next_expression_tag const&, int) -> void
{
inside_next_expression = true;
}
auto end(next_expression_tag const&, int) -> void
{
inside_next_expression = false;
}
auto start(parameter_declaration_list_node const&, int) -> void
{
inside_parameter_list = true;
}
auto end(parameter_declaration_list_node const&, int) -> void
{
inside_parameter_list = false;
}
auto start(declaration_identifier_tag const&, int) -> void
{
inside_parameter_identifier = inside_parameter_list;
}
auto end(declaration_identifier_tag const&, int) -> void
{
inside_parameter_identifier = false;
}
auto start(parameter_declaration_node const& n, int) -> void
{
if (
// If it's an 'out' parameter
(
!inside_returns_list
&& n.pass == passing_style::out
)
// Or it's an uninitialized 'out' return value
|| (
inside_returns_list
&& n.pass == passing_style::out
&& !n.declaration->initializer
)
)
{
inside_out_parameter = &n;
}
if (
n.pass == passing_style::copy
|| n.pass == passing_style::move
|| n.pass == passing_style::forward
)
{
// Handle variables in unnamed functions. For such cases scope_depth is increased by +1
auto depth = scope_depth + ((n.declaration->parent_is_function() && n.declaration->parent_declaration->name() == nullptr) ? 1 : 0 );
symbols.emplace_back( depth, declaration_sym( true, n.declaration.get(), n.declaration->name(), n.declaration->initializer.get(), &n));
}
}
auto end(parameter_declaration_node const&, int) -> void
{
inside_out_parameter = {};
}
auto start(expression_list_node::term const&n, int) -> void
{
is_out_expression = (n.pass == passing_style::out);
}
auto start(function_returns_tag const&, int) -> void
{
inside_returns_list = true;
}
auto end(function_returns_tag const&, int) -> void
{
inside_returns_list = false;
}
auto start(loop_body_tag const &n, int) -> void
{
if (*n.identifier == "for") {
just_entered_for = true;
}
}
auto start(declaration_node const& n, int) -> void
{
// Skip the first declaration after entering a 'for',
// which is the for loop parameter - it's always
// guaranteed to be initialized by the language
if (just_entered_for) {
just_entered_for = false;
return;
}
if (
!n.is_alias()
// Skip type scope (member) variables
&& !(n.parent_is_type() && n.is_object())
// Skip unnamed variables
&& n.identifier
// Skip non-out parameters
&& (
!inside_parameter_list
|| inside_out_parameter
)
)
{
symbols.emplace_back( scope_depth, declaration_sym( true, &n, n.name(), n.initializer.get(), inside_out_parameter ) );
if (!n.is_object()) {
++scope_depth;
}
}
}
auto end(declaration_node const& n, int) -> void
{
if (
!n.is_alias()
// Skip type scope (member) variables
&& !(n.parent_is_type() && n.is_object())
// Skip unnamed variables
&& n.identifier
// Skip non-out parameters
&& (
!inside_parameter_list
|| inside_out_parameter
)
)
{
symbols.emplace_back( scope_depth, declaration_sym( false, &n, nullptr, nullptr, inside_out_parameter ) );
if (!n.is_object()) {
--scope_depth;
}
}
}
auto start(token const& t, int) -> void
{
// We currently only care to look at identifiers
if (t.type() != lexeme::Identifier) {
return;
}
// If this is the first identifier since we started a new assignment,
// expression, then it's the left-hand side (target) of the assignment
else if (started_standalone_assignment_expression)
{
symbols.emplace_back( scope_depth, identifier_sym( true, &t ) );
started_standalone_assignment_expression = false; // we were the consumer for this information
}
// If this is the first identifier since we saw an `out` expression,
// then it's the argument of the `out` expression
// TODO: for now we just take the first identifier, and we should make
// this an id-expression and add a sema rule to disallow complex expressions
else if (is_out_expression)
{
symbols.emplace_back( scope_depth, identifier_sym( true, &t ) );
is_out_expression = false;
}
// Otherwise it's just an identifier use (if it's not a parameter name) and
// it's the first identifier of a postfix_expressions (not a member name or something else)
else if (started_postfix_expression)
{
started_postfix_expression = false;
if (!inside_parameter_identifier && !inside_next_expression)
{
// Put this into the table if it's a use of an object in scope
// or it's a 'copy' parameter (but to be a use it must be after
// the declaration, not the token in the decl's name itself)
if (auto decl = get_declaration_of(t);
decl
&& decl->declaration->name() != &t
)
{
symbols.emplace_back( scope_depth, identifier_sym( false, &t ) );
}
}
}
}
auto start(selection_statement_node const& n, int) -> void
{
active_selections.push_back( &n );
symbols.emplace_back( scope_depth, selection_sym{ true, active_selections.back() } );
++scope_depth;
}
auto end(selection_statement_node const&, int) -> void
{
symbols.emplace_back( scope_depth, selection_sym{ false, active_selections.back() } );
active_selections.pop_back();
--scope_depth;
}
auto kind_of(compound_statement_node const& n)
-> compound_sym::kind
{
auto kind = compound_sym::is_scope;
if (!active_selections.empty())
{
assert(active_selections.back()->true_branch);
if (active_selections.back()->true_branch.get() == &n)
{
kind = compound_sym::is_true;
}
if (
active_selections.back()->false_branch
&& active_selections.back()->false_branch.get() == &n
)
{
kind = compound_sym::is_false;
}
}
return kind;
}
auto start(compound_statement_node const& n, int) -> void
{
symbols.emplace_back(
scope_depth,
compound_sym{ true, &n, kind_of(n) }
);
++scope_depth;
}
auto end(compound_statement_node const& n, int) -> void
{
symbols.emplace_back(
scope_depth,
compound_sym{ false, &n, kind_of(n) }
);
--scope_depth;
}
auto start(assignment_expression_node const& n, int)
{
if (
n.is_standalone_expression()
&& n.lhs_is_id_expression()
&& std::ssize(n.terms) > 0
)
{
assert (n.terms.front().op);
if (n.terms.front().op->type() == lexeme::Assignment) {
started_standalone_assignment_expression = true;
}
}
}
auto start(postfix_expression_node const&, int) {
started_postfix_expression = true;
}
auto start(auto const&, int) -> void
{
// Ignore other node types
}
auto end(auto const&, int) -> void
{
// Ignore other node types
}
};
}
#endif
|