solang-parser 0.2.1

Solang Solidity Parser
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
/*
	This file is part of solidity.

	solidity is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	solidity is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with solidity.  If not, see <http://www.gnu.org/licenses/>.
*/
// SPDX-License-Identifier: GPL-3.0
/**
 * @author Christian <c@ethdev.com>
 * @date 2014
 * Solidity abstract syntax tree.
 */

#pragma once

#include <libsolidity/ast/ASTForward.h>
#include <libsolidity/ast/Types.h>
#include <libsolidity/ast/ASTAnnotations.h>
#include <libsolidity/ast/ASTEnums.h>
#include <libsolidity/parsing/Token.h>

#include <liblangutil/SourceLocation.h>
#include <libevmasm/Instruction.h>
#include <libsolutil/FixedHash.h>
#include <libsolutil/LazyInit.h>
#include <libsolutil/Visitor.h>

#include <json/json.h>

#include <range/v3/view/subrange.hpp>
#include <range/v3/view/map.hpp>

#include <memory>
#include <optional>
#include <string>
#include <utility>
#include <vector>

namespace solidity::yul
{
// Forward-declaration to <yul/AST.h>
struct Block;
struct Dialect;
}

namespace solidity::frontend
{

class ASTVisitor;
class ASTConstVisitor;


/**
 * The root (abstract) class of the AST inheritance tree.
 * It is possible to traverse all direct and indirect children of an AST node by calling
 * accept, providing an ASTVisitor.
 */
class ASTNode
{
public:
	/// Noncopyable.
	ASTNode(ASTNode const&) = delete;
	ASTNode& operator=(ASTNode const&) = delete;

	using CompareByID = frontend::ASTCompareByID<ASTNode>;
	using SourceLocation = langutil::SourceLocation;

	explicit ASTNode(int64_t _id, SourceLocation _location);
	virtual ~ASTNode() {}

	/// @returns an identifier of this AST node that is unique for a single compilation run.
	int64_t id() const { return int64_t(m_id); }

	virtual void accept(ASTVisitor& _visitor) = 0;
	virtual void accept(ASTConstVisitor& _visitor) const = 0;
	template <class T>
	static void listAccept(std::vector<T> const& _list, ASTVisitor& _visitor)
	{
		for (T const& element: _list)
			if (element)
				element->accept(_visitor);
	}
	template <class T>
	static void listAccept(std::vector<T> const& _list, ASTConstVisitor& _visitor)
	{
		for (T const& element: _list)
			if (element)
				element->accept(_visitor);
	}

	/// @returns a copy of the vector containing only the nodes which derive from T.
	template <class T>
	static std::vector<T const*> filteredNodes(std::vector<ASTPointer<ASTNode>> const& _nodes);

	/// Extracts the referenced declaration from all nodes whose annotations support
	/// `referencedDeclaration`.
	static Declaration const* referencedDeclaration(Expression const& _expression);
	/// Performs potential super or virtual lookup for a function call based on the most derived contract.
	static FunctionDefinition const* resolveFunctionCall(FunctionCall const& _functionCall, ContractDefinition const* _mostDerivedContract);

	/// Returns the source code location of this node.
	SourceLocation const& location() const { return m_location; }

	///@todo make this const-safe by providing a different way to access the annotation
	virtual ASTAnnotation& annotation() const;

	///@{
	///@name equality operators
	/// Equality relies on the fact that nodes cannot be copied.
	bool operator==(ASTNode const& _other) const { return this == &_other; }
	bool operator!=(ASTNode const& _other) const { return !operator==(_other); }
	///@}

protected:
	size_t const m_id = 0;

	template <class T>
	T& initAnnotation() const
	{
		if (!m_annotation)
			m_annotation = std::make_unique<T>();
		return dynamic_cast<T&>(*m_annotation);
	}

private:
	/// Annotation - is specialised in derived classes, is created upon request (because of polymorphism).
	mutable std::unique_ptr<ASTAnnotation> m_annotation;
	SourceLocation m_location;
};

template <class T>
std::vector<T const*> ASTNode::filteredNodes(std::vector<ASTPointer<ASTNode>> const& _nodes)
{
	std::vector<T const*> ret;
	for (auto const& n: _nodes)
		if (auto const* nt = dynamic_cast<T const*>(n.get()))
			ret.push_back(nt);
	return ret;
}

/**
 * Abstract marker class that specifies that this AST node opens a scope.
 */
class ScopeOpener
{
public:
	virtual ~ScopeOpener() = default;
};

/**
 * Source unit containing import directives and contract definitions.
 */
class SourceUnit: public ASTNode, public ScopeOpener
{
public:
	SourceUnit(
		int64_t _id,
		SourceLocation const& _location,
		std::optional<std::string> _licenseString,
		std::vector<ASTPointer<ASTNode>> _nodes
	):
		ASTNode(_id, _location), m_licenseString(std::move(_licenseString)), m_nodes(std::move(_nodes)) {}

	void accept(ASTVisitor& _visitor) override;
	void accept(ASTConstVisitor& _visitor) const override;
	SourceUnitAnnotation& annotation() const override;

	std::optional<std::string> const& licenseString() const { return m_licenseString; }
	std::vector<ASTPointer<ASTNode>> nodes() const { return m_nodes; }

	/// @returns a set of referenced SourceUnits. Recursively if @a _recurse is true.
	std::set<SourceUnit const*> referencedSourceUnits(bool _recurse = false, std::set<SourceUnit const*> _skipList = std::set<SourceUnit const*>()) const;

private:
	std::optional<std::string> m_licenseString;
	std::vector<ASTPointer<ASTNode>> m_nodes;
};

/**
 * Abstract class that is added to each AST node that is stored inside a scope
 * (including scopes).
 */
class Scopable
{
public:
	virtual ~Scopable() = default;
	/// @returns the scope this declaration resides in. Can be nullptr if it is the global scope.
	/// Available only after name and type resolution step.
	ASTNode const* scope() const { return annotation().scope; }

	/// @returns the source unit this scopable is present in.
	SourceUnit const& sourceUnit() const;

	/// @returns the function or modifier definition this scopable is present in or nullptr.
	CallableDeclaration const* functionOrModifierDefinition() const;

	/// @returns the source name this scopable is present in.
	/// Can be combined with annotation().canonicalName (if present) to form a globally unique name.
	std::string sourceUnitName() const;

	virtual ScopableAnnotation& annotation() const = 0;
};

/**
 * Abstract AST class for a declaration (contract, function, struct, variable, import directive).
 */
class Declaration: public ASTNode, public Scopable
{
public:

	static std::string visibilityToString(Visibility _visibility)
	{
		switch (_visibility)
		{
		case Visibility::Public:
			return "public";
		case Visibility::Internal:
			return "internal";
		case Visibility::Private:
			return "private";
		case Visibility::External:
			return "external";
		default:
			solAssert(false, "Invalid visibility specifier.");
		}
		return std::string();
	}

	Declaration(
		int64_t _id,
		SourceLocation const& _location,
		ASTPointer<ASTString> _name,
		SourceLocation _nameLocation,
		Visibility _visibility = Visibility::Default
	):
		ASTNode(_id, _location), m_name(std::move(_name)), m_nameLocation(std::move(_nameLocation)), m_visibility(_visibility) {}

	/// @returns the declared name.
	ASTString const& name() const { return *m_name; }

	/// @returns the location of the declared name itself or empty location if not available or unknown.
	SourceLocation const& nameLocation() const noexcept { return m_nameLocation; }

	bool noVisibilitySpecified() const { return m_visibility == Visibility::Default; }
	Visibility visibility() const { return m_visibility == Visibility::Default ? defaultVisibility() : m_visibility; }
	bool isPublic() const { return visibility() >= Visibility::Public; }
	virtual bool isVisibleInContract() const { return visibility() != Visibility::External; }
	virtual bool isVisibleInDerivedContracts() const { return isVisibleInContract() && visibility() >= Visibility::Internal; }
	bool isVisibleAsLibraryMember() const { return visibility() >= Visibility::Internal; }
	virtual bool isVisibleViaContractTypeAccess() const { return false; }

	virtual bool isLValue() const { return false; }
	virtual bool isPartOfExternalInterface() const { return false; }

	/// @returns true if this is a declaration of an enum member.
	bool isEnumValue() const;
	/// @returns true if this is a declaration of a struct member.
	bool isStructMember() const;
	/// @returns true if this is a declaration of a parameter of an event.
	bool isEventOrErrorParameter() const;

	/// @returns false if the declaration can never be referenced without being qualified with a scope.
	/// Usually the name alone can be used to refer to the corresponding entity.
	/// But, for example, struct member names or enum member names always require a prefix.
	/// Another example is event parameter names, which do not participate in any proper scope.
	bool isVisibleAsUnqualifiedName() const;

	/// @returns the type of expressions referencing this declaration.
	/// This can only be called once types of variable declarations have already been resolved.
	virtual Type const* type() const = 0;

	/// @returns the type for members of the containing contract type that refer to this declaration.
	/// This can only be called once types of variable declarations have already been resolved.
	virtual Type const* typeViaContractName() const { return type(); }

	/// @param _internal false indicates external interface is concerned, true indicates internal interface is concerned.
	/// @returns null when it is not accessible as a function.
	virtual FunctionTypePointer functionType(bool /*_internal*/) const { return {}; }

	DeclarationAnnotation& annotation() const override;

protected:
	virtual Visibility defaultVisibility() const { return Visibility::Public; }

private:
	ASTPointer<ASTString> m_name;
	SourceLocation m_nameLocation;
	Visibility m_visibility;
};

/**
 * Pragma directive, only version requirements in the form `pragma solidity "^0.4.0";` are
 * supported for now.
 */
class PragmaDirective: public ASTNode
{
public:
	PragmaDirective(
		int64_t _id,
		SourceLocation const& _location,
		std::vector<Token> _tokens,
		std::vector<ASTString> _literals
	): ASTNode(_id, _location), m_tokens(std::move(_tokens)), m_literals(std::move(_literals))
	{}

	void accept(ASTVisitor& _visitor) override;
	void accept(ASTConstVisitor& _visitor) const override;

	std::vector<Token> const& tokens() const { return m_tokens; }
	std::vector<ASTString> const& literals() const { return m_literals; }

private:

	/// Sequence of tokens following the "pragma" keyword.
	std::vector<Token> m_tokens;
	/// Sequence of literals following the "pragma" keyword.
	std::vector<ASTString> m_literals;
};

/**
 * Import directive for referencing other files / source objects.
 * Example: import "abc.sol" // imports all symbols of "abc.sol" into current scope
 * Source objects are identified by a string which can be a file name but does not have to be.
 * Other ways to use it:
 * import "abc" as x; // creates symbol "x" that contains all symbols in "abc"
 * import * as x from "abc"; // same as above
 * import {a as b, c} from "abc"; // creates new symbols "b" and "c" referencing "a" and "c" in "abc", respectively.
 */
class ImportDirective: public Declaration
{
public:
	struct SymbolAlias
	{
		ASTPointer<Identifier> symbol;
		ASTPointer<ASTString> alias;
		SourceLocation location;
	};

	using SymbolAliasList = std::vector<SymbolAlias>;

	ImportDirective(
		int64_t _id,
		SourceLocation const& _location,
		ASTPointer<ASTString> _path,
		ASTPointer<ASTString> const& _unitAlias,
		SourceLocation _unitAliasLocation,
		SymbolAliasList _symbolAliases
	):
		Declaration(_id, _location, _unitAlias, std::move(_unitAliasLocation)),
		m_path(std::move(_path)),
		m_symbolAliases(move(_symbolAliases))
	{ }

	void accept(ASTVisitor& _visitor) override;
	void accept(ASTConstVisitor& _visitor) const override;

	ASTString const& path() const { return *m_path; }
	SymbolAliasList const& symbolAliases() const
	{
		return m_symbolAliases;
	}
	ImportAnnotation& annotation() const override;

	Type const* type() const override;

private:
	ASTPointer<ASTString> m_path;
	/// The aliases for the specific symbols to import. If non-empty import the specific symbols.
	/// If the `alias` component is empty, import the identifier unchanged.
	/// If both m_unitAlias and m_symbolAlias are empty, import all symbols into the current scope.
	SymbolAliasList m_symbolAliases;
};

/**
 * Abstract class that is added to each AST node that can store local variables.
 * Local variables in functions are always added to functions, even though they are not
 * in scope for the whole function.
 */
class VariableScope
{
public:
	virtual ~VariableScope() = default;
	void addLocalVariable(VariableDeclaration const& _localVariable) { m_localVariables.push_back(&_localVariable); }
	std::vector<VariableDeclaration const*> const& localVariables() const { return m_localVariables; }

private:
	std::vector<VariableDeclaration const*> m_localVariables;
};

/**
 * The doxygen-style, structured documentation class that represents an AST node.
 */
class StructuredDocumentation: public ASTNode
{
public:
	StructuredDocumentation(
		int64_t _id,
		SourceLocation const& _location,
		ASTPointer<ASTString> _text
	): ASTNode(_id, _location), m_text(std::move(_text))
	{}

	void accept(ASTVisitor& _visitor) override;
	void accept(ASTConstVisitor& _visitor) const override;

	/// @return A shared pointer of an ASTString.
	/// Contains doxygen-style, structured documentation that is parsed later on.
	ASTPointer<ASTString> const& text() const { return m_text; }

private:
	ASTPointer<ASTString> m_text;
};

/**
 * Abstract class that is added to each AST node that can receive documentation.
 */
class Documented
{
public:
	virtual ~Documented() = default;
	explicit Documented(ASTPointer<ASTString> _documentation): m_documentation(std::move(_documentation)) {}

	/// @return A shared pointer of an ASTString.
	/// Can contain a nullptr in which case indicates absence of documentation
	ASTPointer<ASTString> const& documentation() const { return m_documentation; }

protected:
	ASTPointer<ASTString> m_documentation;
};

/**
 * Abstract class that is added to each AST node that can receive a structured documentation.
 */
class StructurallyDocumented
{
public:
	virtual ~StructurallyDocumented() = default;
	explicit StructurallyDocumented(ASTPointer<StructuredDocumentation> _documentation): m_documentation(std::move(_documentation)) {}

	/// @return A shared pointer of a FormalDocumentation.
	/// Can contain a nullptr in which case indicates absence of documentation
	ASTPointer<StructuredDocumentation> const& documentation() const { return m_documentation; }

protected:
	ASTPointer<StructuredDocumentation> m_documentation;
};


/**
 * Abstract class that is added to AST nodes that can be marked as not being fully implemented
 */
class ImplementationOptional
{
public:
	virtual ~ImplementationOptional() = default;
	explicit ImplementationOptional(bool _implemented): m_implemented(_implemented) {}

	/// @return whether this node is fully implemented or not
	bool isImplemented() const { return m_implemented; }

protected:
	bool m_implemented;
};

/// @}

/**
 * Definition of a contract or library. This is the only AST nodes where child nodes are not visited in
 * document order. It first visits all struct declarations, then all variable declarations and
 * finally all function declarations.
 */
class ContractDefinition: public Declaration, public StructurallyDocumented, public ScopeOpener
{
public:
	ContractDefinition(
		int64_t _id,
		SourceLocation const& _location,
		ASTPointer<ASTString> const& _name,
		SourceLocation _nameLocation,
		ASTPointer<StructuredDocumentation> const& _documentation,
		std::vector<ASTPointer<InheritanceSpecifier>> _baseContracts,
		std::vector<ASTPointer<ASTNode>> _subNodes,
		ContractKind _contractKind = ContractKind::Contract,
		bool _abstract = false
	):
		Declaration(_id, _location, _name, std::move(_nameLocation)),
		StructurallyDocumented(_documentation),
		m_baseContracts(std::move(_baseContracts)),
		m_subNodes(std::move(_subNodes)),
		m_contractKind(_contractKind),
		m_abstract(_abstract)
	{}

	void accept(ASTVisitor& _visitor) override;
	void accept(ASTConstVisitor& _visitor) const override;

	std::vector<ASTPointer<InheritanceSpecifier>> const& baseContracts() const { return m_baseContracts; }
	std::vector<ASTPointer<ASTNode>> const& subNodes() const { return m_subNodes; }
	std::vector<UsingForDirective const*> usingForDirectives() const { return filteredNodes<UsingForDirective>(m_subNodes); }
	std::vector<StructDefinition const*> definedStructs() const { return filteredNodes<StructDefinition>(m_subNodes); }
	std::vector<EnumDefinition const*> definedEnums() const { return filteredNodes<EnumDefinition>(m_subNodes); }
	std::vector<VariableDeclaration const*> stateVariables() const { return filteredNodes<VariableDeclaration>(m_subNodes); }
	std::vector<ModifierDefinition const*> functionModifiers() const { return filteredNodes<ModifierDefinition>(m_subNodes); }
	std::vector<FunctionDefinition const*> definedFunctions() const { return filteredNodes<FunctionDefinition>(m_subNodes); }
	/// @returns a view<FunctionDefinition const*> of all functions
	/// defined in this contract of the given name (excluding inherited functions).
	auto definedFunctions(std::string const& _name) const
	{
		auto&& [b, e] = definedFunctionsByName().equal_range(_name);
		return ranges::subrange<decltype(b)>(b, e) | ranges::views::values;
	}
	std::vector<EventDefinition const*> events() const { return filteredNodes<EventDefinition>(m_subNodes); }
	std::vector<EventDefinition const*> const& definedInterfaceEvents() const;
	std::vector<EventDefinition const*> const usedInterfaceEvents() const;
	/// @returns all errors defined in this contract or any base contract
	/// and all errors referenced during execution.
	/// @param _requireCallGraph if false, do not fail if the call graph has not been computed yet.
	std::vector<ErrorDefinition const*> interfaceErrors(bool _requireCallGraph = true) const;
	bool isInterface() const { return m_contractKind == ContractKind::Interface; }
	bool isLibrary() const { return m_contractKind == ContractKind::Library; }

	/// @returns true, if the contract derives from @arg _base.
	bool derivesFrom(ContractDefinition const& _base) const;

	/// @returns a map of canonical function signatures to FunctionDefinitions
	/// as intended for use by the ABI.
	std::map<util::FixedHash<4>, FunctionTypePointer> interfaceFunctions(bool _includeInheritedFunctions = true) const;
	std::vector<std::pair<util::FixedHash<4>, FunctionTypePointer>> const& interfaceFunctionList(bool _includeInheritedFunctions = true) const;
	/// @returns the EIP-165 compatible interface identifier. This will exclude inherited functions.
	uint32_t interfaceId() const;

	/// @returns a list of all declarations in this contract
	std::vector<Declaration const*> declarations() const { return filteredNodes<Declaration>(m_subNodes); }

	/// Returns the constructor or nullptr if no constructor was specified.
	FunctionDefinition const* constructor() const;
	/// @returns true iff the contract can be deployed, i.e. is not abstract and has a
	/// public constructor.
	/// Should only be called after the type checker has run.
	bool canBeDeployed() const;
	/// Returns the fallback function or nullptr if no fallback function was specified.
	FunctionDefinition const* fallbackFunction() const;

	/// Returns the ether receiver function or nullptr if no receive function was specified.
	FunctionDefinition const* receiveFunction() const;

	std::string fullyQualifiedName() const { return sourceUnitName() + ":" + name(); }

	Type const* type() const override;

	ContractDefinitionAnnotation& annotation() const override;

	ContractKind contractKind() const { return m_contractKind; }

	bool abstract() const { return m_abstract; }

	ContractDefinition const* superContract(ContractDefinition const& _mostDerivedContract) const;
	/// @returns the next constructor in the inheritance hierarchy.
	FunctionDefinition const* nextConstructor(ContractDefinition const& _mostDerivedContract) const;

private:
	std::multimap<std::string, FunctionDefinition const*> const& definedFunctionsByName() const;

	std::vector<ASTPointer<InheritanceSpecifier>> m_baseContracts;
	std::vector<ASTPointer<ASTNode>> m_subNodes;
	ContractKind m_contractKind;
	bool m_abstract{false};

	util::LazyInit<std::vector<std::pair<util::FixedHash<4>, FunctionTypePointer>>> m_interfaceFunctionList[2];
	util::LazyInit<std::vector<EventDefinition const*>> m_interfaceEvents;
	util::LazyInit<std::multimap<std::string, FunctionDefinition const*>> m_definedFunctionsByName;
};

/**
 * A sequence of identifiers separated by dots used outside the expression context. Inside the expression context, this is a sequence of Identifier and MemberAccess.
 */
class IdentifierPath: public ASTNode
{
public:
	IdentifierPath(int64_t _id, SourceLocation const& _location, std::vector<ASTString> _path):
		ASTNode(_id, _location), m_path(std::move(_path)) {}

	std::vector<ASTString> const& path() const { return m_path; }
	IdentifierPathAnnotation& annotation() const override
	{
		return initAnnotation<IdentifierPathAnnotation>();
	}

	void accept(ASTVisitor& _visitor) override;
	void accept(ASTConstVisitor& _visitor) const override;
private:
	std::vector<ASTString> m_path;
};

class InheritanceSpecifier: public ASTNode
{
public:
	InheritanceSpecifier(
		int64_t _id,
		SourceLocation const& _location,
		ASTPointer<IdentifierPath> _baseName,
		std::unique_ptr<std::vector<ASTPointer<Expression>>> _arguments
	):
		ASTNode(_id, _location), m_baseName(std::move(_baseName)), m_arguments(std::move(_arguments))
	{
		solAssert(m_baseName != nullptr, "Name cannot be null.");
	}

	void accept(ASTVisitor& _visitor) override;
	void accept(ASTConstVisitor& _visitor) const override;

	IdentifierPath const& name() const { return *m_baseName; }
	// Returns nullptr if no argument list was given (``C``).
	// If an argument list is given (``C(...)``), the arguments are returned
	// as a vector of expressions. Note that this vector can be empty (``C()``).
	std::vector<ASTPointer<Expression>> const* arguments() const { return m_arguments.get(); }

private:
	ASTPointer<IdentifierPath> m_baseName;
	std::unique_ptr<std::vector<ASTPointer<Expression>>> m_arguments;
};

/**
 * Using for directive:
 *
 * 1. `using LibraryName for T` attaches all functions from the library `LibraryName` to the type `T`
 * 2. `using LibraryName for *` attaches to all types.
 * 3. `using {f1, f2, ..., fn} for T` attaches the functions `f1`, `f2`, ...,
 *     `fn`, respectively to `T`.
 *
 * For version 3, T has to be implicitly convertible to the first parameter type of
 * all functions, and this is checked at the point of the using statement. For versions 1 and
 * 2, this check is only done when a function is called.
 *
 * Finally, `using {f1, f2, ..., fn} for T global` is also valid at file level, as long as T is
 * a user-defined type defined in the same file at file level. In this case, the methods are
 * attached to all objects of that type regardless of scope.
 */
class UsingForDirective: public ASTNode
{
public:
	UsingForDirective(
		int64_t _id,
		SourceLocation const& _location,
		std::vector<ASTPointer<IdentifierPath>> _functions,
		bool _usesBraces,
		ASTPointer<TypeName> _typeName,
		bool _global
	):
		ASTNode(_id, _location),
		m_functions(_functions),
		m_usesBraces(_usesBraces),
		m_typeName(std::move(_typeName)),
		m_global{_global}
	{
	}

	void accept(ASTVisitor& _visitor) override;
	void accept(ASTConstVisitor& _visitor) const override;

	/// @returns the type name the library is attached to, null for `*`.
	TypeName const* typeName() const { return m_typeName.get(); }

	/// @returns a list of functions or the single library.
	std::vector<ASTPointer<IdentifierPath>> const& functionsOrLibrary() const { return m_functions; }
	bool usesBraces() const { return m_usesBraces; }
	bool global() const { return m_global; }

private:
	/// Either the single library or a list of functions.
	std::vector<ASTPointer<IdentifierPath>> m_functions;
	bool m_usesBraces;
	ASTPointer<TypeName> m_typeName;
	bool m_global = false;
};

class StructDefinition: public Declaration, public ScopeOpener
{
public:
	StructDefinition(
		int64_t _id,
		SourceLocation const& _location,
		ASTPointer<ASTString> const& _name,
		SourceLocation _nameLocation,
		std::vector<ASTPointer<VariableDeclaration>> _members
	):
		Declaration(_id, _location, _name, std::move(_nameLocation)), m_members(std::move(_members)) {}

	void accept(ASTVisitor& _visitor) override;
	void accept(ASTConstVisitor& _visitor) const override;

	std::vector<ASTPointer<VariableDeclaration>> const& members() const { return m_members; }

	Type const* type() const override;

	bool isVisibleInDerivedContracts() const override { return true; }
	bool isVisibleViaContractTypeAccess() const override { return true; }

	StructDeclarationAnnotation& annotation() const override;

private:
	std::vector<ASTPointer<VariableDeclaration>> m_members;
};

class EnumDefinition: public Declaration, public ScopeOpener
{
public:
	EnumDefinition(
		int64_t _id,
		SourceLocation const& _location,
		ASTPointer<ASTString> const& _name,
		SourceLocation _nameLocation,
		std::vector<ASTPointer<EnumValue>> _members
	):
		Declaration(_id, _location, _name, std::move(_nameLocation)), m_members(std::move(_members)) {}
	void accept(ASTVisitor& _visitor) override;
	void accept(ASTConstVisitor& _visitor) const override;

	bool isVisibleInDerivedContracts() const override { return true; }
	bool isVisibleViaContractTypeAccess() const override { return true; }

	std::vector<ASTPointer<EnumValue>> const& members() const { return m_members; }

	Type const* type() const override;

	TypeDeclarationAnnotation& annotation() const override;

private:
	std::vector<ASTPointer<EnumValue>> m_members;
};

/**
 * Declaration of an Enum Value
 */
class EnumValue: public Declaration
{
public:
	EnumValue(int64_t _id, SourceLocation const& _location, ASTPointer<ASTString> const& _name):
		Declaration(_id, _location, _name, _location) {}

	void accept(ASTVisitor& _visitor) override;
	void accept(ASTConstVisitor& _visitor) const override;

	Type const* type() const override;
};

/**
 * User defined value types, i.e., custom types, for example, `type MyInt is int`. Allows creating a
 * zero cost abstraction over value type with stricter type requirements.
 */
class UserDefinedValueTypeDefinition: public Declaration
{
public:
	UserDefinedValueTypeDefinition(
		int64_t _id,
		SourceLocation const& _location,
		ASTPointer<ASTString> _name,
		SourceLocation _nameLocation,
		ASTPointer<TypeName> _underlyingType
	):
		Declaration(_id, _location, _name, std::move(_nameLocation), Visibility::Default),
		m_underlyingType(std::move(_underlyingType))
	{
	}

	void accept(ASTVisitor& _visitor) override;
	void accept(ASTConstVisitor& _visitor) const override;

	Type const* type() const override;

	TypeDeclarationAnnotation& annotation() const override;

	TypeName const* underlyingType() const { return m_underlyingType.get(); }
	bool isVisibleViaContractTypeAccess() const override { return true; }

private:
	/// The name of the underlying type
	ASTPointer<TypeName> m_underlyingType;
};

/**
 * Parameter list, used as function parameter list, return list and for try and catch.
 * None of the parameters is allowed to contain mappings (not even recursively
 * inside structs).
 */
class ParameterList: public ASTNode
{
public:
	ParameterList(
		int64_t _id,
		SourceLocation const& _location,
		std::vector<ASTPointer<VariableDeclaration>> _parameters
	):
		ASTNode(_id, _location), m_parameters(std::move(_parameters)) {}
	void accept(ASTVisitor& _visitor) override;
	void accept(ASTConstVisitor& _visitor) const override;

	std::vector<ASTPointer<VariableDeclaration>> const& parameters() const { return m_parameters; }

private:
	std::vector<ASTPointer<VariableDeclaration>> m_parameters;
};

/**
 * Base class for all nodes that define function-like objects, i.e. FunctionDefinition,
 * EventDefinition, ErrorDefinition and ModifierDefinition.
 */
class CallableDeclaration: public Declaration, public VariableScope
{
public:
	CallableDeclaration(
		int64_t _id,
		SourceLocation const& _location,
		ASTPointer<ASTString> const& _name,
		SourceLocation _nameLocation,
		Visibility _visibility,
		ASTPointer<ParameterList> _parameters,
		bool _isVirtual = false,
		ASTPointer<OverrideSpecifier> _overrides = nullptr,
		ASTPointer<ParameterList> _returnParameters = ASTPointer<ParameterList>()
	):
		Declaration(_id, _location, _name, std::move(_nameLocation), _visibility),
		m_parameters(std::move(_parameters)),
		m_overrides(std::move(_overrides)),
		m_returnParameters(std::move(_returnParameters)),
		m_isVirtual(_isVirtual)
	{
	}

	std::vector<ASTPointer<VariableDeclaration>> const& parameters() const { return m_parameters->parameters(); }
	ASTPointer<OverrideSpecifier> const& overrides() const { return m_overrides; }
	std::vector<ASTPointer<VariableDeclaration>> const& returnParameters() const { return m_returnParameters->parameters(); }
	ParameterList const& parameterList() const { return *m_parameters; }
	ASTPointer<ParameterList> const& returnParameterList() const { return m_returnParameters; }
	bool markedVirtual() const { return m_isVirtual; }
	virtual bool virtualSemantics() const { return markedVirtual(); }

	CallableDeclarationAnnotation& annotation() const override = 0;

	/// Performs virtual or super function/modifier lookup:
	/// If @a _searchStart is nullptr, performs virtual function lookup, i.e.
	/// searches the inheritance hierarchy of @a _mostDerivedContract towards the base
	/// and returns the first function/modifier definition that
	/// is overwritten by this callable.
	/// If @a _searchStart is non-null, starts searching only from that contract, but
	/// still in the hierarchy of @a _mostDerivedContract.
	virtual CallableDeclaration const& resolveVirtual(
		ContractDefinition const& _mostDerivedContract,
		ContractDefinition const* _searchStart = nullptr
	) const = 0;

protected:
	ASTPointer<ParameterList> m_parameters;
	ASTPointer<OverrideSpecifier> m_overrides;
	ASTPointer<ParameterList> m_returnParameters;
	bool m_isVirtual = false;
};

/**
 * Function override specifier. Consists of a single override keyword
 * potentially followed by a parenthesized list of base contract names.
 */
class OverrideSpecifier: public ASTNode
{
public:
	OverrideSpecifier(
		int64_t _id,
		SourceLocation const& _location,
		std::vector<ASTPointer<IdentifierPath>> _overrides
	):
		ASTNode(_id, _location),
		m_overrides(std::move(_overrides))
	{
	}

	void accept(ASTVisitor& _visitor) override;
	void accept(ASTConstVisitor& _visitor) const override;

	/// @returns the list of specific overrides, if any
	std::vector<ASTPointer<IdentifierPath>> const& overrides() const { return m_overrides; }

protected:
	std::vector<ASTPointer<IdentifierPath>> m_overrides;
};

class FunctionDefinition: public CallableDeclaration, public StructurallyDocumented, public ImplementationOptional, public ScopeOpener
{
public:
	FunctionDefinition(
		int64_t _id,
		SourceLocation const& _location,
		ASTPointer<ASTString> const& _name,
		SourceLocation const& _nameLocation,
		Visibility _visibility,
		StateMutability _stateMutability,
		bool _free,
		Token _kind,
		bool _isVirtual,
		ASTPointer<OverrideSpecifier> const& _overrides,
		ASTPointer<StructuredDocumentation> const& _documentation,
		ASTPointer<ParameterList> const& _parameters,
		std::vector<ASTPointer<ModifierInvocation>> _modifiers,
		ASTPointer<ParameterList> const& _returnParameters,
		ASTPointer<Block> const& _body
	):
		CallableDeclaration(_id, _location, _name, std::move(_nameLocation), _visibility, _parameters, _isVirtual, _overrides, _returnParameters),
		StructurallyDocumented(_documentation),
		ImplementationOptional(_body != nullptr),
		m_stateMutability(_stateMutability),
		m_free(_free),
		m_kind(_kind),
		m_functionModifiers(std::move(_modifiers)),
		m_body(_body)
	{
		solAssert(_kind == Token::Constructor || _kind == Token::Function || _kind == Token::Fallback || _kind == Token::Receive, "");
		solAssert(isOrdinary() == !name().empty(), "");
	}

	void accept(ASTVisitor& _visitor) override;
	void accept(ASTConstVisitor& _visitor) const override;

	StateMutability stateMutability() const { return m_stateMutability; }
	bool libraryFunction() const;
	bool isOrdinary() const { return m_kind == Token::Function; }
	bool isConstructor() const { return m_kind == Token::Constructor; }
	bool isFallback() const { return m_kind == Token::Fallback; }
	bool isReceive() const { return m_kind == Token::Receive; }
	bool isFree() const { return m_free; }
	Token kind() const { return m_kind; }
	bool isPayable() const { return m_stateMutability == StateMutability::Payable; }
	std::vector<ASTPointer<ModifierInvocation>> const& modifiers() const { return m_functionModifiers; }
	Block const& body() const { solAssert(m_body, ""); return *m_body; }
	Visibility defaultVisibility() const override;
	bool isVisibleInContract() const override
	{
		return isOrdinary() && Declaration::isVisibleInContract();
	}
	bool isVisibleViaContractTypeAccess() const override
	{
		solAssert(!isFree(), "");
		return isOrdinary() && visibility() >= Visibility::Public;
	}
	bool isPartOfExternalInterface() const override { return isOrdinary() && isPublic(); }

	/// @returns the external signature of the function
	/// That consists of the name of the function followed by the types of the
	/// arguments separated by commas all enclosed in parentheses without any spaces.
	std::string externalSignature() const;

	/// @returns the external identifier of this function (the hash of the signature) as a hex string.
	std::string externalIdentifierHex() const;

	Type const* type() const override;
	Type const* typeViaContractName() const override;

	/// @param _internal false indicates external interface is concerned, true indicates internal interface is concerned.
	/// @returns null when it is not accessible as a function.
	FunctionTypePointer functionType(bool /*_internal*/) const override;

	FunctionDefinitionAnnotation& annotation() const override;

	bool virtualSemantics() const override
	{
		return
			CallableDeclaration::virtualSemantics() ||
			(annotation().contract && annotation().contract->isInterface());
	}

	FunctionDefinition const& resolveVirtual(
		ContractDefinition const& _mostDerivedContract,
		ContractDefinition const* _searchStart = nullptr
	) const override;

private:
	StateMutability m_stateMutability;
	bool m_free;
	Token const m_kind;
	std::vector<ASTPointer<ModifierInvocation>> m_functionModifiers;
	ASTPointer<Block> m_body;
};

/**
 * Declaration of a variable. This can be used in various places, e.g. in function parameter
 * lists, struct definitions and even function bodies.
 */
class VariableDeclaration: public Declaration, public StructurallyDocumented
{
public:
	enum Location { Unspecified, Storage, Memory, CallData };
	enum class Mutability { Mutable, Immutable, Constant };
	static std::string mutabilityToString(Mutability _mutability)
	{
		switch (_mutability)
		{
		case Mutability::Mutable: return "mutable";
		case Mutability::Immutable: return "immutable";
		case Mutability::Constant: return "constant";
		}
		return {};
	}

	VariableDeclaration(
		int64_t _id,
		SourceLocation const& _location,
		ASTPointer<TypeName> _type,
		ASTPointer<ASTString> const& _name,
		SourceLocation _nameLocation,
		ASTPointer<Expression> _value,
		Visibility _visibility,
		ASTPointer<StructuredDocumentation> const _documentation = nullptr,
		bool _isIndexed = false,
		Mutability _mutability = Mutability::Mutable,
		ASTPointer<OverrideSpecifier> _overrides = nullptr,
		Location _referenceLocation = Location::Unspecified
	):
		Declaration(_id, _location, _name, std::move(_nameLocation), _visibility),
		StructurallyDocumented(std::move(_documentation)),
		m_typeName(std::move(_type)),
		m_value(std::move(_value)),
		m_isIndexed(_isIndexed),
		m_mutability(_mutability),
		m_overrides(std::move(_overrides)),
		m_location(_referenceLocation)
	{
		solAssert(m_typeName, "");
	}


	void accept(ASTVisitor& _visitor) override;
	void accept(ASTConstVisitor& _visitor) const override;

	TypeName const& typeName() const { return *m_typeName; }
	ASTPointer<Expression> const& value() const { return m_value; }

	bool isLValue() const override;
	bool isPartOfExternalInterface() const override { return isPublic(); }

	/// @returns true iff this variable is the parameter (or return parameter) of a function
	/// (or function type name or event) or declared inside a function body.
	bool isLocalVariable() const;
	/// @returns true if this variable is a parameter or return parameter of a function.
	bool isCallableOrCatchParameter() const;
	/// @returns true if this variable is a return parameter of a function.
	bool isReturnParameter() const;
	/// @returns true if this variable is a parameter of the success or failure clausse
	/// of a try/catch statement.
	bool isTryCatchParameter() const;
	/// @returns true if this variable is a local variable or return parameter.
	bool isLocalOrReturn() const;
	/// @returns true if this variable is a parameter (not return parameter) of an external function.
	/// This excludes parameters of external function type names.
	bool isExternalCallableParameter() const;
	/// @returns true if this variable is a parameter (not return parameter) of a public function.
	bool isPublicCallableParameter() const;
	/// @returns true if this variable is a parameter or return parameter of an internal function
	/// or a function type of internal visibility.
	bool isInternalCallableParameter() const;
	/// @returns true if this variable is the parameter of a constructor.
	bool isConstructorParameter() const;
	/// @returns true iff this variable is a parameter(or return parameter of a library function
	bool isLibraryFunctionParameter() const;
	/// @returns true if the type of the variable is a reference or mapping type, i.e.
	/// array, struct or mapping. These types can take a data location (and often require it).
	/// Can only be called after reference resolution.
	bool hasReferenceOrMappingType() const;
	bool isStateVariable() const;
	bool isFileLevelVariable() const;
	bool isIndexed() const { return m_isIndexed; }
	Mutability mutability() const { return m_mutability; }
	bool isConstant() const { return m_mutability == Mutability::Constant; }
	bool immutable() const { return m_mutability == Mutability::Immutable; }
	ASTPointer<OverrideSpecifier> const& overrides() const { return m_overrides; }
	Location referenceLocation() const { return m_location; }
	/// @returns a set of allowed storage locations for the variable.
	std::set<Location> allowedDataLocations() const;

	/// @returns the external identifier of this variable (the hash of the signature) as a hex string (works only for public state variables).
	std::string externalIdentifierHex() const;

	Type const* type() const override;

	/// @param _internal false indicates external interface is concerned, true indicates internal interface is concerned.
	/// @returns null when it is not accessible as a function.
	FunctionTypePointer functionType(bool /*_internal*/) const override;

	VariableDeclarationAnnotation& annotation() const override;

protected:
	Visibility defaultVisibility() const override { return Visibility::Internal; }

private:
	ASTPointer<TypeName> m_typeName;
	/// Initially assigned value, can be missing. For local variables, this is stored inside
	/// VariableDeclarationStatement and not here.
	ASTPointer<Expression> m_value;
	bool m_isIndexed = false; ///< Whether this is an indexed variable (used by events).
	/// Whether the variable is "constant", "immutable" or non-marked (mutable).
	Mutability m_mutability = Mutability::Mutable;
	ASTPointer<OverrideSpecifier> m_overrides; ///< Contains the override specifier node
	Location m_location = Location::Unspecified; ///< Location of the variable if it is of reference type.
};

/**
 * Definition of a function modifier.
 */
class ModifierDefinition: public CallableDeclaration, public StructurallyDocumented, public ImplementationOptional, public ScopeOpener
{
public:
	ModifierDefinition(
		int64_t _id,
		SourceLocation const& _location,
		ASTPointer<ASTString> const& _name,
		SourceLocation _nameLocation,
		ASTPointer<StructuredDocumentation> const& _documentation,
		ASTPointer<ParameterList> const& _parameters,
		bool _isVirtual,
		ASTPointer<OverrideSpecifier> const& _overrides,
		ASTPointer<Block> const& _body
	):
		CallableDeclaration(_id, _location, _name, std::move(_nameLocation), Visibility::Internal, _parameters, _isVirtual, _overrides),
		StructurallyDocumented(_documentation),
		ImplementationOptional(_body != nullptr),
		m_body(_body)
	{
	}

	void accept(ASTVisitor& _visitor) override;
	void accept(ASTConstVisitor& _visitor) const override;

	Block const& body() const { solAssert(m_body, ""); return *m_body; }

	Type const* type() const override;

	Visibility defaultVisibility() const override { return Visibility::Internal; }

	ModifierDefinitionAnnotation& annotation() const override;

	ModifierDefinition const& resolveVirtual(
		ContractDefinition const& _mostDerivedContract,
		ContractDefinition const* _searchStart = nullptr
	) const override;


private:
	ASTPointer<Block> m_body;
};

/**
 * Invocation/usage of a modifier in a function header or a base constructor call.
 */
class ModifierInvocation: public ASTNode
{
public:
	ModifierInvocation(
		int64_t _id,
		SourceLocation const& _location,
		ASTPointer<IdentifierPath> _name,
		std::unique_ptr<std::vector<ASTPointer<Expression>>> _arguments
	):
		ASTNode(_id, _location), m_modifierName(std::move(_name)), m_arguments(std::move(_arguments))
	{
		solAssert(m_modifierName != nullptr, "Name cannot be null.");
	}

	void accept(ASTVisitor& _visitor) override;
	void accept(ASTConstVisitor& _visitor) const override;

	IdentifierPath& name() const { return *m_modifierName; }
	// Returns nullptr if no argument list was given (``mod``).
	// If an argument list is given (``mod(...)``), the arguments are returned
	// as a vector of expressions. Note that this vector can be empty (``mod()``).
	std::vector<ASTPointer<Expression>> const* arguments() const { return m_arguments.get(); }

private:
	ASTPointer<IdentifierPath> m_modifierName;
	std::unique_ptr<std::vector<ASTPointer<Expression>>> m_arguments;
};

/**
 * Definition of a (loggable) event.
 */
class EventDefinition: public CallableDeclaration, public StructurallyDocumented, public ScopeOpener
{
public:
	EventDefinition(
		int64_t _id,
		SourceLocation const& _location,
		ASTPointer<ASTString> const& _name,
		SourceLocation _nameLocation,
		ASTPointer<StructuredDocumentation> const& _documentation,
		ASTPointer<ParameterList> const& _parameters,
		bool _anonymous = false
	):
		CallableDeclaration(_id, _location, _name, std::move(_nameLocation), Visibility::Default, _parameters),
		StructurallyDocumented(_documentation),
		m_anonymous(_anonymous)
	{
	}

	void accept(ASTVisitor& _visitor) override;
	void accept(ASTConstVisitor& _visitor) const override;

	bool isAnonymous() const { return m_anonymous; }

	Type const* type() const override;
	FunctionTypePointer functionType(bool /*_internal*/) const override;

	bool isVisibleInDerivedContracts() const override { return true; }
	bool isVisibleViaContractTypeAccess() const override { return false; /* TODO */ }

	EventDefinitionAnnotation& annotation() const override;

	CallableDeclaration const& resolveVirtual(
		ContractDefinition const&,
		ContractDefinition const*
	) const override
	{
		return *this;
	}

private:
	bool m_anonymous = false;
};

/**
 * Definition of an error type usable in ``revert(MyError(x))``, ``require(condition, MyError(x))``
 * and ``catch MyError(_x)``.
 */
class ErrorDefinition: public CallableDeclaration, public StructurallyDocumented, public ScopeOpener
{
public:
	ErrorDefinition(
		int64_t _id,
		SourceLocation const& _location,
		ASTPointer<ASTString> const& _name,
		SourceLocation _nameLocation,
		ASTPointer<StructuredDocumentation> const& _documentation,
		ASTPointer<ParameterList> const& _parameters
	):
		CallableDeclaration(_id, _location, _name, std::move(_nameLocation), Visibility::Default, _parameters),
		StructurallyDocumented(_documentation)
	{
	}

	void accept(ASTVisitor& _visitor) override;
	void accept(ASTConstVisitor& _visitor) const override;

	Type const* type() const override;

	FunctionTypePointer functionType(bool _internal) const override;

	bool isVisibleInDerivedContracts() const override { return true; }
	bool isVisibleViaContractTypeAccess() const override { return true; }

	ErrorDefinitionAnnotation& annotation() const override;

	CallableDeclaration const& resolveVirtual(
		ContractDefinition const&,
		ContractDefinition const*
	) const override
	{
		return *this;
	}
};

/**
 * Pseudo AST node that is used as declaration for "this", "msg", "tx", "block" and the global
 * functions when such an identifier is encountered. Will never have a valid location in the source code
 */
class MagicVariableDeclaration: public Declaration
{
public:
	MagicVariableDeclaration(int _id, ASTString const& _name, Type const* _type):
		Declaration(_id, SourceLocation(), std::make_shared<ASTString>(_name), {}), m_type(_type) { }

	void accept(ASTVisitor&) override
	{
		solAssert(false, "MagicVariableDeclaration used inside real AST.");
	}
	void accept(ASTConstVisitor&) const override
	{
		solAssert(false, "MagicVariableDeclaration used inside real AST.");
	}

	FunctionType const* functionType(bool) const override
	{
		solAssert(m_type->category() == Type::Category::Function, "");
		return dynamic_cast<FunctionType const*>(m_type);
	}
	Type const* type() const override { return m_type; }

private:
	Type const* m_type;
};

/// Types
/// @{

/**
 * Abstract base class of a type name, can be any built-in or user-defined type.
 */
class TypeName: public ASTNode
{
protected:
	explicit TypeName(int64_t _id, SourceLocation const& _location): ASTNode(_id, _location) {}

public:
	TypeNameAnnotation& annotation() const override;
};

/**
 * Any pre-defined type name represented by a single keyword (and possibly a state mutability for address types),
 * i.e. it excludes mappings, contracts, functions, etc.
 */
class ElementaryTypeName: public TypeName
{
public:
	ElementaryTypeName(
		int64_t _id,
		SourceLocation const& _location,
		ElementaryTypeNameToken const& _elem,
		std::optional<StateMutability> _stateMutability = {}
	): TypeName(_id, _location), m_type(_elem), m_stateMutability(_stateMutability)
	{
		solAssert(!_stateMutability.has_value() || _elem.token() == Token::Address, "");
	}

	void accept(ASTVisitor& _visitor) override;
	void accept(ASTConstVisitor& _visitor) const override;

	ElementaryTypeNameToken const& typeName() const { return m_type; }

	std::optional<StateMutability> const& stateMutability() const { return m_stateMutability; }

private:
	ElementaryTypeNameToken m_type;
	std::optional<StateMutability> m_stateMutability; ///< state mutability for address type
};

/**
 * Name referring to a user-defined type (i.e. a struct, contract, etc.).
 */
class UserDefinedTypeName: public TypeName
{
public:
	UserDefinedTypeName(int64_t _id, SourceLocation const& _location, ASTPointer<IdentifierPath> _namePath):
		TypeName(_id, _location), m_namePath(std::move(_namePath))
	{
		solAssert(m_namePath != nullptr, "Name cannot be null.");
	}
	void accept(ASTVisitor& _visitor) override;
	void accept(ASTConstVisitor& _visitor) const override;

	std::vector<ASTString> const& namePath() const { return m_namePath->path(); }
	IdentifierPath& pathNode() const { return *m_namePath; }

private:
	ASTPointer<IdentifierPath> m_namePath;
};

/**
 * A literal function type. Its source form is "function (paramType1, paramType2) internal / external returns (retType1, retType2)"
 */
class FunctionTypeName: public TypeName, public ScopeOpener
{
public:
	FunctionTypeName(
		int64_t _id,
		SourceLocation const& _location,
		ASTPointer<ParameterList> _parameterTypes,
		ASTPointer<ParameterList> _returnTypes,
		Visibility _visibility,
		StateMutability _stateMutability
	):
		TypeName(_id, _location), m_parameterTypes(std::move(_parameterTypes)), m_returnTypes(std::move(_returnTypes)),
		m_visibility(_visibility), m_stateMutability(_stateMutability)
	{}
	void accept(ASTVisitor& _visitor) override;
	void accept(ASTConstVisitor& _visitor) const override;

	std::vector<ASTPointer<VariableDeclaration>> const& parameterTypes() const { return m_parameterTypes->parameters(); }
	std::vector<ASTPointer<VariableDeclaration>> const& returnParameterTypes() const { return m_returnTypes->parameters(); }
	ASTPointer<ParameterList> const& parameterTypeList() const { return m_parameterTypes; }
	ASTPointer<ParameterList> const& returnParameterTypeList() const { return m_returnTypes; }

	Visibility visibility() const
	{
		return m_visibility == Visibility::Default ? Visibility::Internal : m_visibility;
	}
	StateMutability stateMutability() const { return m_stateMutability; }
	bool isPayable() const { return m_stateMutability == StateMutability::Payable; }

private:
	ASTPointer<ParameterList> m_parameterTypes;
	ASTPointer<ParameterList> m_returnTypes;
	Visibility m_visibility;
	StateMutability m_stateMutability;
};

/**
 * A mapping type. Its source form is "mapping('keyType' => 'valueType')"
 */
class Mapping: public TypeName
{
public:
	Mapping(
		int64_t _id,
		SourceLocation const& _location,
		ASTPointer<TypeName> _keyType,
		ASTPointer<TypeName> _valueType
	):
		TypeName(_id, _location), m_keyType(std::move(_keyType)), m_valueType(std::move(_valueType)) {}
	void accept(ASTVisitor& _visitor) override;
	void accept(ASTConstVisitor& _visitor) const override;

	TypeName const& keyType() const { return *m_keyType; }
	TypeName const& valueType() const { return *m_valueType; }

private:
	ASTPointer<TypeName> m_keyType;
	ASTPointer<TypeName> m_valueType;
};

/**
 * An array type, can be "typename[]" or "typename[<expression>]".
 */
class ArrayTypeName: public TypeName
{
public:
	ArrayTypeName(
		int64_t _id,
		SourceLocation const& _location,
		ASTPointer<TypeName> _baseType,
		ASTPointer<Expression> _length
	):
		TypeName(_id, _location), m_baseType(std::move(_baseType)), m_length(std::move(_length)) {}
	void accept(ASTVisitor& _visitor) override;
	void accept(ASTConstVisitor& _visitor) const override;

	TypeName const& baseType() const { return *m_baseType; }
	Expression const* length() const { return m_length.get(); }

private:
	ASTPointer<TypeName> m_baseType;
	ASTPointer<Expression> m_length; ///< Length of the array, might be empty.
};

/// @}

/// Statements
/// @{


/**
 * Abstract base class for statements.
 */
class Statement: public ASTNode, public Documented
{
public:
	explicit Statement(
		int64_t _id,
		SourceLocation const& _location,
		ASTPointer<ASTString> const& _docString
	): ASTNode(_id, _location), Documented(_docString) {}

	StatementAnnotation& annotation() const override;
};

/**
 * Inline assembly.
 */
class InlineAssembly: public Statement
{
public:
	InlineAssembly(
		int64_t _id,
		SourceLocation const& _location,
		ASTPointer<ASTString> const& _docString,
		yul::Dialect const& _dialect,
		ASTPointer<std::vector<ASTPointer<ASTString>>> _flags,
		std::shared_ptr<yul::Block> _operations
	):
		Statement(_id, _location, _docString),
		m_dialect(_dialect),
		m_flags(move(_flags)),
		m_operations(std::move(_operations))
	{}
	void accept(ASTVisitor& _visitor) override;
	void accept(ASTConstVisitor& _visitor) const override;

	yul::Dialect const& dialect() const { return m_dialect; }
	yul::Block const& operations() const { return *m_operations; }
	ASTPointer<std::vector<ASTPointer<ASTString>>> const& flags() const { return m_flags; }

	InlineAssemblyAnnotation& annotation() const override;

private:
	yul::Dialect const& m_dialect;
	ASTPointer<std::vector<ASTPointer<ASTString>>> m_flags;
	std::shared_ptr<yul::Block> m_operations;
};

/**
 * Brace-enclosed block containing zero or more statements.
 */
class Block: public Statement, public Scopable, public ScopeOpener
{
public:
	Block(
		int64_t _id,
		SourceLocation const& _location,
		ASTPointer<ASTString> const& _docString,
		bool _unchecked,
		std::vector<ASTPointer<Statement>> _statements
	):
		Statement(_id, _location, _docString),
		m_statements(std::move(_statements)),
		m_unchecked(_unchecked)
	{}
	void accept(ASTVisitor& _visitor) override;
	void accept(ASTConstVisitor& _visitor) const override;

	std::vector<ASTPointer<Statement>> const& statements() const { return m_statements; }
	bool unchecked() const { return m_unchecked; }

	BlockAnnotation& annotation() const override;

private:
	std::vector<ASTPointer<Statement>> m_statements;
	bool m_unchecked;
};

/**
 * Special placeholder statement denoted by "_" used in function modifiers. This is replaced by
 * the original function when the modifier is applied.
 */
class PlaceholderStatement: public Statement
{
public:
	explicit PlaceholderStatement(
		int64_t _id,
		SourceLocation const& _location,
		ASTPointer<ASTString> const& _docString
	): Statement(_id, _location, _docString) {}

	void accept(ASTVisitor& _visitor) override;
	void accept(ASTConstVisitor& _visitor) const override;
};

/**
 * If-statement with an optional "else" part. Note that "else if" is modeled by having a new
 * if-statement as the false (else) body.
 */
class IfStatement: public Statement
{
public:
	IfStatement(
		int64_t _id,
		SourceLocation const& _location,
		ASTPointer<ASTString> const& _docString,
		ASTPointer<Expression> _condition,
		ASTPointer<Statement> _trueBody,
		ASTPointer<Statement> _falseBody
	):
		Statement(_id, _location, _docString),
		m_condition(std::move(_condition)),
		m_trueBody(std::move(_trueBody)),
		m_falseBody(std::move(_falseBody))
	{}
	void accept(ASTVisitor& _visitor) override;
	void accept(ASTConstVisitor& _visitor) const override;

	Expression const& condition() const { return *m_condition; }
	Statement const& trueStatement() const { return *m_trueBody; }
	/// @returns the "else" part of the if statement or nullptr if there is no "else" part.
	Statement const* falseStatement() const { return m_falseBody.get(); }

private:
	ASTPointer<Expression> m_condition;
	ASTPointer<Statement> m_trueBody;
	ASTPointer<Statement> m_falseBody; ///< "else" part, optional
};

/**
 * Clause of a try-catch block. Includes both the successful case and the
 * unsuccessful cases.
 * Names are only allowed for the unsuccessful cases.
 */
class TryCatchClause: public ASTNode, public Scopable, public ScopeOpener
{
public:
	TryCatchClause(
		int64_t _id,
		SourceLocation const& _location,
		ASTPointer<ASTString> _errorName,
		ASTPointer<ParameterList> _parameters,
		ASTPointer<Block> _block
	):
		ASTNode(_id, _location),
		m_errorName(std::move(_errorName)),
		m_parameters(std::move(_parameters)),
		m_block(std::move(_block))
	{}
	void accept(ASTVisitor& _visitor) override;
	void accept(ASTConstVisitor& _visitor) const override;

	ASTString const& errorName() const { return *m_errorName; }
	ParameterList const* parameters() const { return m_parameters.get(); }
	Block const& block() const { return *m_block; }

	TryCatchClauseAnnotation& annotation() const override;

private:
	ASTPointer<ASTString> m_errorName;
	ASTPointer<ParameterList> m_parameters;
	ASTPointer<Block> m_block;
};

/**
 * Try-statement with a variable number of catch statements.
 * Syntax:
 * try <call> returns (uint x, uint y) {
 *   // success code
 * } catch Panic(uint errorCode) {
 *   // panic
 * } catch Error(string memory cause) {
 *   // error code, reason provided
 * } catch (bytes memory lowLevelData) {
 *   // error code, no reason provided or non-matching error signature.
 * }
 *
 * The last statement given above can also be specified as
 * } catch () {
 */
class TryStatement: public Statement
{
public:
	TryStatement(
		int64_t _id,
		SourceLocation const& _location,
		ASTPointer<ASTString> const& _docString,
		ASTPointer<Expression> _externalCall,
		std::vector<ASTPointer<TryCatchClause>> _clauses
	):
		Statement(_id, _location, _docString),
		m_externalCall(std::move(_externalCall)),
		m_clauses(std::move(_clauses))
	{}
	void accept(ASTVisitor& _visitor) override;
	void accept(ASTConstVisitor& _visitor) const override;

	Expression const& externalCall() const { return *m_externalCall; }
	std::vector<ASTPointer<TryCatchClause>> const& clauses() const { return m_clauses; }

	TryCatchClause const* successClause() const;
	TryCatchClause const* panicClause() const;
	TryCatchClause const* errorClause() const;
	TryCatchClause const* fallbackClause() const;

private:
	ASTPointer<Expression> m_externalCall;
	std::vector<ASTPointer<TryCatchClause>> m_clauses;
};

/**
 * Statement in which a break statement is legal (abstract class).
 */
class BreakableStatement: public Statement
{
public:
	explicit BreakableStatement(
		int64_t _id,
		SourceLocation const& _location,
		ASTPointer<ASTString> const& _docString
	): Statement(_id, _location, _docString) {}
};

class WhileStatement: public BreakableStatement
{
public:
	WhileStatement(
		int64_t _id,
		SourceLocation const& _location,
		ASTPointer<ASTString> const& _docString,
		ASTPointer<Expression> _condition,
		ASTPointer<Statement> _body,
		bool _isDoWhile
	):
		BreakableStatement(_id, _location, _docString), m_condition(std::move(_condition)), m_body(std::move(_body)),
		m_isDoWhile(_isDoWhile) {}
	void accept(ASTVisitor& _visitor) override;
	void accept(ASTConstVisitor& _visitor) const override;

	Expression const& condition() const { return *m_condition; }
	Statement const& body() const { return *m_body; }
	bool isDoWhile() const { return m_isDoWhile; }

private:
	ASTPointer<Expression> m_condition;
	ASTPointer<Statement> m_body;
	bool m_isDoWhile;
};

/**
 * For loop statement
 */
class ForStatement: public BreakableStatement, public Scopable, public ScopeOpener
{
public:
	ForStatement(
		int64_t _id,
		SourceLocation const& _location,
		ASTPointer<ASTString> const& _docString,
		ASTPointer<Statement> _initExpression,
		ASTPointer<Expression> _conditionExpression,
		ASTPointer<ExpressionStatement> _loopExpression,
		ASTPointer<Statement> _body
	):
		BreakableStatement(_id, _location, _docString),
		m_initExpression(std::move(_initExpression)),
		m_condExpression(std::move(_conditionExpression)),
		m_loopExpression(std::move(_loopExpression)),
		m_body(std::move(_body))
	{}
	void accept(ASTVisitor& _visitor) override;
	void accept(ASTConstVisitor& _visitor) const override;

	Statement const* initializationExpression() const { return m_initExpression.get(); }
	Expression const* condition() const { return m_condExpression.get(); }
	ExpressionStatement const* loopExpression() const { return m_loopExpression.get(); }
	Statement const& body() const { return *m_body; }

	ForStatementAnnotation& annotation() const override;

private:
	/// For statement's initialization expression. for (XXX; ; ). Can be empty
	ASTPointer<Statement> m_initExpression;
	/// For statement's condition expression. for (; XXX ; ). Can be empty
	ASTPointer<Expression> m_condExpression;
	/// For statement's loop expression. for (;;XXX). Can be empty
	ASTPointer<ExpressionStatement> m_loopExpression;
	/// The body of the loop
	ASTPointer<Statement> m_body;
};

class Continue: public Statement
{
public:
	explicit Continue(int64_t _id, SourceLocation const& _location, ASTPointer<ASTString> const& _docString):
		Statement(_id, _location, _docString) {}
	void accept(ASTVisitor& _visitor) override;
	void accept(ASTConstVisitor& _visitor) const override;
};

class Break: public Statement
{
public:
	explicit Break(int64_t _id, SourceLocation const& _location, ASTPointer<ASTString> const& _docString):
		Statement(_id, _location, _docString) {}
	void accept(ASTVisitor& _visitor) override;
	void accept(ASTConstVisitor& _visitor) const override;
};

class Return: public Statement
{
public:
	Return(
		int64_t _id,
		SourceLocation const& _location,
		ASTPointer<ASTString> const& _docString,
		ASTPointer<Expression> _expression
	): Statement(_id, _location, _docString), m_expression(std::move(_expression)) {}
	void accept(ASTVisitor& _visitor) override;
	void accept(ASTConstVisitor& _visitor) const override;

	Expression const* expression() const { return m_expression.get(); }

	ReturnAnnotation& annotation() const override;

private:
	ASTPointer<Expression> m_expression; ///< value to return, optional
};

/**
 * @brief The Throw statement to throw that triggers a solidity exception(jump to ErrorTag)
 */
class Throw: public Statement
{
public:
	explicit Throw(int64_t _id, SourceLocation const& _location, ASTPointer<ASTString> const& _docString):
		Statement(_id, _location, _docString) {}
	void accept(ASTVisitor& _visitor) override;
	void accept(ASTConstVisitor& _visitor) const override;
};

/**
 * The revert statement is used to revert state changes and return error data.
 */
class RevertStatement: public Statement
{
public:
	explicit RevertStatement(
		int64_t _id,
		SourceLocation const& _location,
		ASTPointer<ASTString> const& _docString,
		ASTPointer<FunctionCall> _functionCall
	):
		Statement(_id, _location, _docString), m_errorCall(std::move(_functionCall))
	{
		solAssert(m_errorCall, "");
	}
	void accept(ASTVisitor& _visitor) override;
	void accept(ASTConstVisitor& _visitor) const override;

	FunctionCall const& errorCall() const { return *m_errorCall; }

private:
	ASTPointer<FunctionCall> m_errorCall;
};

/**
 * The emit statement is used to emit events: emit EventName(arg1, ..., argn)
 */
class EmitStatement: public Statement
{
public:
	explicit EmitStatement(
		int64_t _id,
		SourceLocation const& _location,
		ASTPointer<ASTString> const& _docString,
		ASTPointer<FunctionCall> _functionCall
	):
		Statement(_id, _location, _docString), m_eventCall(std::move(_functionCall)) {}
	void accept(ASTVisitor& _visitor) override;
	void accept(ASTConstVisitor& _visitor) const override;

	FunctionCall const& eventCall() const { return *m_eventCall; }

private:
	ASTPointer<FunctionCall> m_eventCall;
};

/**
 * Definition of one or more variables as a statement inside a function.
 * If multiple variables are declared, a value has to be assigned directly.
 * If only a single variable is declared, the value can be missing.
 * Examples:
 * uint[] memory a; uint a = 2;
 * (uint a, bytes32 b, ) = f(); (, uint a, , StructName storage x) = g();
 */
class VariableDeclarationStatement: public Statement
{
public:
	VariableDeclarationStatement(
		int64_t _id,
		SourceLocation const& _location,
		ASTPointer<ASTString> const& _docString,
		std::vector<ASTPointer<VariableDeclaration>> _variables,
		ASTPointer<Expression> _initialValue
	):
		Statement(_id, _location, _docString), m_variables(std::move(_variables)), m_initialValue(std::move(_initialValue)) {}
	void accept(ASTVisitor& _visitor) override;
	void accept(ASTConstVisitor& _visitor) const override;

	std::vector<ASTPointer<VariableDeclaration>> const& declarations() const { return m_variables; }
	Expression const* initialValue() const { return m_initialValue.get(); }

private:
	/// List of variables, some of which can be empty pointers (unnamed components).
	/// Note that the ``m_value`` member of these is unused. Instead, ``m_initialValue``
	/// below is used, because the initial value can be a single expression assigned
	/// to all variables.
	std::vector<ASTPointer<VariableDeclaration>> m_variables;
	/// The assigned expression / initial value.
	ASTPointer<Expression> m_initialValue;
};

/**
 * A statement that contains only an expression (i.e. an assignment, function call, ...).
 */
class ExpressionStatement: public Statement
{
public:
	ExpressionStatement(
		int64_t _id,
		SourceLocation const& _location,
		ASTPointer<ASTString> const& _docString,
		ASTPointer<Expression> _expression
	):
		Statement(_id, _location, _docString), m_expression(std::move(_expression)) {}
	void accept(ASTVisitor& _visitor) override;
	void accept(ASTConstVisitor& _visitor) const override;

	Expression const& expression() const { return *m_expression; }

private:
	ASTPointer<Expression> m_expression;
};

/// @}

/// Expressions
/// @{

/**
 * An expression, i.e. something that has a value (which can also be of type "void" in case
 * of some function calls).
 * @abstract
 */
class Expression: public ASTNode
{
public:
	explicit Expression(int64_t _id, SourceLocation const& _location): ASTNode(_id, _location) {}

	ExpressionAnnotation& annotation() const override;
};

class Conditional: public Expression
{
public:
	Conditional(
		int64_t _id,
		SourceLocation const& _location,
		ASTPointer<Expression> _condition,
		ASTPointer<Expression> _trueExpression,
		ASTPointer<Expression> _falseExpression
	):
		Expression(_id, _location),
		m_condition(std::move(_condition)),
		m_trueExpression(std::move(_trueExpression)),
		m_falseExpression(std::move(_falseExpression))
	{}
	void accept(ASTVisitor& _visitor) override;
	void accept(ASTConstVisitor& _visitor) const override;

	Expression const& condition() const { return *m_condition; }
	Expression const& trueExpression() const { return *m_trueExpression; }
	Expression const& falseExpression() const { return *m_falseExpression; }

private:
	ASTPointer<Expression> m_condition;
	ASTPointer<Expression> m_trueExpression;
	ASTPointer<Expression> m_falseExpression;
};

/// Assignment, can also be a compound assignment.
/// Examples: (a = 7 + 8) or (a *= 2)
class Assignment: public Expression
{
public:
	Assignment(
		int64_t _id,
		SourceLocation const& _location,
		ASTPointer<Expression> _leftHandSide,
		Token _assignmentOperator,
		ASTPointer<Expression> _rightHandSide
	):
		Expression(_id, _location),
		m_leftHandSide(std::move(_leftHandSide)),
		m_assigmentOperator(_assignmentOperator),
		m_rightHandSide(std::move(_rightHandSide))
	{
		solAssert(TokenTraits::isAssignmentOp(_assignmentOperator), "");
	}
	void accept(ASTVisitor& _visitor) override;
	void accept(ASTConstVisitor& _visitor) const override;

	Expression const& leftHandSide() const { return *m_leftHandSide; }
	Token assignmentOperator() const { return m_assigmentOperator; }
	Expression const& rightHandSide() const { return *m_rightHandSide; }

private:
	ASTPointer<Expression> m_leftHandSide;
	Token m_assigmentOperator;
	ASTPointer<Expression> m_rightHandSide;
};


/**
 * Tuple, parenthesized expression, or bracketed expression.
 * Examples: (1, 2), (x,), (x), (), [1, 2],
 * Individual components might be empty shared pointers (as in the second example).
 * The respective types in lvalue context are: 2-tuple, 2-tuple (with wildcard), type of x, 0-tuple
 * Not in lvalue context: 2-tuple, _1_-tuple, type of x, 0-tuple.
 */
class TupleExpression: public Expression
{
public:
	TupleExpression(
		int64_t _id,
		SourceLocation const& _location,
		std::vector<ASTPointer<Expression>> _components,
		bool _isArray
	):
		Expression(_id, _location),
		m_components(std::move(_components)),
		m_isArray(_isArray) {}
	void accept(ASTVisitor& _visitor) override;
	void accept(ASTConstVisitor& _visitor) const override;

	std::vector<ASTPointer<Expression>> const& components() const { return m_components; }
	bool isInlineArray() const { return m_isArray; }

private:
	std::vector<ASTPointer<Expression>> m_components;
	bool m_isArray;
};

/**
 * Operation involving a unary operator, pre- or postfix.
 * Examples: ++i, delete x or !true
 */
class UnaryOperation: public Expression
{
public:
	UnaryOperation(
		int64_t _id,
		SourceLocation const& _location,
		Token _operator,
		ASTPointer<Expression> _subExpression,
		bool _isPrefix
	):
		Expression(_id, _location),
		m_operator(_operator),
		m_subExpression(std::move(_subExpression)),
		m_isPrefix(_isPrefix)
	{
		solAssert(TokenTraits::isUnaryOp(_operator), "");
	}
	void accept(ASTVisitor& _visitor) override;
	void accept(ASTConstVisitor& _visitor) const override;

	Token getOperator() const { return m_operator; }
	bool isPrefixOperation() const { return m_isPrefix; }
	Expression const& subExpression() const { return *m_subExpression; }

private:
	Token m_operator;
	ASTPointer<Expression> m_subExpression;
	bool m_isPrefix;
};

/**
 * Operation involving a binary operator.
 * Examples: 1 + 2, true && false or 1 <= 4
 */
class BinaryOperation: public Expression
{
public:
	BinaryOperation(
		int64_t _id,
		SourceLocation const& _location,
		ASTPointer<Expression> _left,
		Token _operator,
		ASTPointer<Expression> _right
	):
		Expression(_id, _location), m_left(std::move(_left)), m_operator(_operator), m_right(std::move(_right))
	{
		solAssert(TokenTraits::isBinaryOp(_operator) || TokenTraits::isCompareOp(_operator), "");
	}
	void accept(ASTVisitor& _visitor) override;
	void accept(ASTConstVisitor& _visitor) const override;

	Expression const& leftExpression() const { return *m_left; }
	Expression const& rightExpression() const { return *m_right; }
	Token getOperator() const { return m_operator; }

	BinaryOperationAnnotation& annotation() const override;

private:
	ASTPointer<Expression> m_left;
	Token m_operator;
	ASTPointer<Expression> m_right;
};

/**
 * Can be ordinary function call, type cast or struct construction.
 */
class FunctionCall: public Expression
{
public:
	FunctionCall(
		int64_t _id,
		SourceLocation const& _location,
		ASTPointer<Expression> _expression,
		std::vector<ASTPointer<Expression>> _arguments,
		std::vector<ASTPointer<ASTString>> _names
	):
		Expression(_id, _location), m_expression(std::move(_expression)), m_arguments(std::move(_arguments)), m_names(std::move(_names)) {}
	void accept(ASTVisitor& _visitor) override;
	void accept(ASTConstVisitor& _visitor) const override;

	Expression const& expression() const { return *m_expression; }
	/// @returns the given arguments in the order they were written.
	std::vector<ASTPointer<Expression const>> arguments() const { return {m_arguments.begin(), m_arguments.end()}; }
	/// @returns the given arguments sorted by how the called function takes them.
	std::vector<ASTPointer<Expression const>> sortedArguments() const;
	/// @returns the list of given argument names if this is a named call,
	/// in the order they were written.
	/// If this is not a named call, this is empty.
	std::vector<ASTPointer<ASTString>> const& names() const { return m_names; }

	FunctionCallAnnotation& annotation() const override;

private:
	ASTPointer<Expression> m_expression;
	std::vector<ASTPointer<Expression>> m_arguments;
	std::vector<ASTPointer<ASTString>> m_names;
};

/**
 * Expression that annotates a function call / a new expression with extra
 * options like gas, value, salt: new SomeContract{salt=123}(params)
 */
class FunctionCallOptions: public Expression
{
public:
	FunctionCallOptions(
		int64_t _id,
		SourceLocation const& _location,
		ASTPointer<Expression> _expression,
		std::vector<ASTPointer<Expression>> _options,
		std::vector<ASTPointer<ASTString>> _names
	):
		Expression(_id, _location), m_expression(std::move(_expression)), m_options(std::move(_options)), m_names(std::move(_names)) {}
	void accept(ASTVisitor& _visitor) override;
	void accept(ASTConstVisitor& _visitor) const override;

	Expression const& expression() const { return *m_expression; }
	std::vector<ASTPointer<Expression const>> options() const { return {m_options.begin(), m_options.end()}; }
	std::vector<ASTPointer<ASTString>> const& names() const { return m_names; }

private:
	ASTPointer<Expression> m_expression;
	std::vector<ASTPointer<Expression>> m_options;
	std::vector<ASTPointer<ASTString>> m_names;

};

/**
 * Expression that creates a new contract or memory-array,
 * e.g. the "new SomeContract" part in "new SomeContract(1, 2)".
 */
class NewExpression: public Expression
{
public:
	NewExpression(
		int64_t _id,
		SourceLocation const& _location,
		ASTPointer<TypeName> _typeName
	):
		Expression(_id, _location), m_typeName(std::move(_typeName)) {}
	void accept(ASTVisitor& _visitor) override;
	void accept(ASTConstVisitor& _visitor) const override;

	TypeName const& typeName() const { return *m_typeName; }

private:
	ASTPointer<TypeName> m_typeName;
};

/**
 * Access to a member of an object. Example: x.name
 */
class MemberAccess: public Expression
{
public:
	MemberAccess(
		int64_t _id,
		SourceLocation const& _location,
		ASTPointer<Expression> _expression,
		ASTPointer<ASTString> _memberName
	):
		Expression(_id, _location), m_expression(std::move(_expression)), m_memberName(std::move(_memberName)) {}
	void accept(ASTVisitor& _visitor) override;
	void accept(ASTConstVisitor& _visitor) const override;
	Expression const& expression() const { return *m_expression; }
	ASTString const& memberName() const { return *m_memberName; }

	MemberAccessAnnotation& annotation() const override;

private:
	ASTPointer<Expression> m_expression;
	ASTPointer<ASTString> m_memberName;
};

/**
 * Index access to an array or mapping. Example: a[2]
 */
class IndexAccess: public Expression
{
public:
	IndexAccess(
		int64_t _id,
		SourceLocation const& _location,
		ASTPointer<Expression> _base,
		ASTPointer<Expression> _index
	):
		Expression(_id, _location), m_base(std::move(_base)), m_index(std::move(_index)) {}
	void accept(ASTVisitor& _visitor) override;
	void accept(ASTConstVisitor& _visitor) const override;

	Expression const& baseExpression() const { return *m_base; }
	Expression const* indexExpression() const { return m_index.get(); }

private:
	ASTPointer<Expression> m_base;
	ASTPointer<Expression> m_index;
};

/**
 * Index range access to an array. Example: a[2:3]
 */
class IndexRangeAccess: public Expression
{
public:
	IndexRangeAccess(
		int64_t _id,
		SourceLocation const& _location,
		ASTPointer<Expression> _base,
		ASTPointer<Expression> _start,
		ASTPointer<Expression> _end
	):
		Expression(_id, _location), m_base(std::move(_base)), m_start(std::move(_start)), m_end(std::move(_end)) {}
	void accept(ASTVisitor& _visitor) override;
	void accept(ASTConstVisitor& _visitor) const override;

	Expression const& baseExpression() const { return *m_base; }
	Expression const* startExpression() const { return m_start.get(); }
	Expression const* endExpression() const { return m_end.get(); }

private:
	ASTPointer<Expression> m_base;
	ASTPointer<Expression> m_start;
	ASTPointer<Expression> m_end;
};

/**
 * Primary expression, i.e. an expression that cannot be divided any further. Examples are literals
 * or variable references.
 */
class PrimaryExpression: public Expression
{
public:
	PrimaryExpression(int64_t _id, SourceLocation const& _location): Expression(_id, _location) {}
};

/**
 * An identifier, i.e. a reference to a declaration by name like a variable or function.
 */
class Identifier: public PrimaryExpression
{
public:
	Identifier(
		int64_t _id,
		SourceLocation const& _location,
		ASTPointer<ASTString> _name
	):
		PrimaryExpression(_id, _location), m_name(std::move(_name)) {}
	void accept(ASTVisitor& _visitor) override;
	void accept(ASTConstVisitor& _visitor) const override;

	ASTString const& name() const { return *m_name; }

	IdentifierAnnotation& annotation() const override;

private:
	ASTPointer<ASTString> m_name;
};

/**
 * An elementary type name expression is used in expressions like "a = uint32(2)" to change the
 * type of an expression explicitly. Here, "uint32" is the elementary type name expression and
 * "uint32(2)" is a @ref FunctionCall.
 */
class ElementaryTypeNameExpression: public PrimaryExpression
{
public:
	ElementaryTypeNameExpression(
		int64_t _id,
		SourceLocation const& _location,
		ASTPointer<ElementaryTypeName> _type
	):
		PrimaryExpression(_id, _location),
		m_type(std::move(_type))
	{
	}
	void accept(ASTVisitor& _visitor) override;
	void accept(ASTConstVisitor& _visitor) const override;

	ElementaryTypeName const& type() const { return *m_type; }

private:
	ASTPointer<ElementaryTypeName> m_type;
};

/**
 * A literal string or number. @see ExpressionCompiler::endVisit() is used to actually parse its value.
 */
class Literal: public PrimaryExpression
{
public:
	enum class SubDenomination
	{
		None = static_cast<int>(Token::Illegal),
		Wei = static_cast<int>(Token::SubWei),
		Gwei = static_cast<int>(Token::SubGwei),
		Ether = static_cast<int>(Token::SubEther),
		Second = static_cast<int>(Token::SubSecond),
		Minute = static_cast<int>(Token::SubMinute),
		Hour = static_cast<int>(Token::SubHour),
		Day = static_cast<int>(Token::SubDay),
		Week = static_cast<int>(Token::SubWeek),
		Year = static_cast<int>(Token::SubYear)
	};
	Literal(
		int64_t _id,
		SourceLocation const& _location,
		Token _token,
		ASTPointer<ASTString> _value,
		SubDenomination _sub = SubDenomination::None
	):
		PrimaryExpression(_id, _location), m_token(_token), m_value(std::move(_value)), m_subDenomination(_sub) {}
	void accept(ASTVisitor& _visitor) override;
	void accept(ASTConstVisitor& _visitor) const override;

	Token token() const { return m_token; }
	/// @returns the non-parsed value of the literal
	ASTString const& value() const { return *m_value; }

	ASTString valueWithoutUnderscores() const;

	SubDenomination subDenomination() const { return m_subDenomination; }

	/// @returns true if this is a number with a hex prefix.
	bool isHexNumber() const;

	/// @returns true if this looks like a checksummed address.
	bool looksLikeAddress() const;
	/// @returns true if it passes the address checksum test.
	bool passesAddressChecksum() const;
	/// @returns the checksummed version of an address (or empty string if not valid)
	std::string getChecksummedAddress() const;

private:
	Token m_token;
	ASTPointer<ASTString> m_value;
	SubDenomination m_subDenomination;
};

/// @}

}