solang 0.3.4

Solang Solidity Compiler
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
// SPDX-License-Identifier: Apache-2.0

use super::symtable::Symtable;
use crate::abi::anchor::function_discriminator;
use crate::codegen::cfg::{ControlFlowGraph, Instr};
use crate::diagnostics::Diagnostics;
use crate::sema::ast::ExternalCallAccounts::{AbsentArgument, NoAccount};
use crate::sema::yul::ast::{InlineAssembly, YulFunction};
use crate::sema::Recurse;
use crate::{codegen, Target};
use indexmap::IndexMap;
use num_bigint::BigInt;
use num_rational::BigRational;
use once_cell::unsync::OnceCell;
pub use solang_parser::diagnostics::*;
use solang_parser::pt;
use solang_parser::pt::{CodeLocation, FunctionTy, OptionalCodeLocation};
use std::cell::RefCell;
use std::fmt::Write;
use std::{
    collections::HashSet,
    collections::{BTreeMap, HashMap},
    fmt, hash,
    path::PathBuf,
    sync::Arc,
};
use tiny_keccak::{Hasher, Keccak};

#[derive(PartialEq, Eq, Clone, Hash, Debug)]
pub enum Type {
    Address(bool),
    Bool,
    Int(u16),
    Uint(u16),
    Rational,
    Bytes(u8),
    DynamicBytes,
    String,
    Array(Box<Type>, Vec<ArrayLength>),
    /// The usize is an index into enums in the namespace
    Enum(usize),
    /// The usize is an index into contracts in the namespace
    Struct(StructType),
    Mapping(Mapping),
    /// The usize is an index into contracts in the namespace
    Contract(usize),
    Ref(Box<Type>),
    /// Reference to storage, first bool is true for immutables
    StorageRef(bool, Box<Type>),
    InternalFunction {
        mutability: Mutability,
        params: Vec<Type>,
        returns: Vec<Type>,
    },
    ExternalFunction {
        mutability: Mutability,
        params: Vec<Type>,
        returns: Vec<Type>,
    },
    /// User type definitions, e.g. `type Foo is int128;`. The usize
    /// is an index into user_types in the namespace.
    UserType(usize),
    /// There is no way to declare value in Solidity (should there be?)
    Value,
    Void,
    Unreachable,
    /// DynamicBytes and String are lowered to a vector.
    Slice(Box<Type>),
    /// We could not resolve this type
    Unresolved,
    /// When we advance a pointer, it cannot be any of the previous types.
    /// e.g. Type::Bytes is a pointer to struct.vector. When we advance it, it is a pointer
    /// to latter's data region.
    BufferPointer,
    /// The function selector (or discriminator) type is 4 bytes on Polkadot and 8 bytes on Solana
    FunctionSelector,
}

#[derive(Eq, Clone, Debug)]
pub struct Mapping {
    pub key: Box<Type>,
    pub key_name: Option<pt::Identifier>,
    pub value: Box<Type>,
    pub value_name: Option<pt::Identifier>,
}

// Ensure the key_name and value_name is not used for comparison or hashing
impl PartialEq for Mapping {
    fn eq(&self, other: &Mapping) -> bool {
        self.key == other.key && self.value == other.value
    }
}

impl hash::Hash for Mapping {
    fn hash<H: hash::Hasher>(&self, hasher: &mut H) {
        self.key.hash(hasher);
        self.value.hash(hasher);
    }
}

#[derive(PartialEq, Eq, Clone, Hash, Debug)]
pub enum ArrayLength {
    Fixed(BigInt),
    Dynamic,
    /// Fixed length arrays, any length permitted. This is useful for when we
    /// do not want dynamic length, but want to permit any length. For example
    /// the create_program_address() call takes any number of seeds as its
    /// first argument, and we don't want to allocate a dynamic array for
    /// this parameter as this would be wasteful to allocate a vector for
    /// this argument.
    AnyFixed,
}

impl ArrayLength {
    /// Get the length, if fixed
    pub fn array_length(&self) -> Option<&BigInt> {
        match self {
            ArrayLength::Fixed(len) => Some(len),
            _ => None,
        }
    }
}

pub trait RetrieveType {
    /// Return the type for this expression. This assumes the expression has a single value,
    /// panics will occur otherwise
    fn ty(&self) -> Type;
}

impl Type {
    pub fn get_type_size(&self) -> u16 {
        match self {
            Type::Int(n) | Type::Uint(n) => *n,
            Type::Bool => 1,
            _ => unimplemented!("size of type not known"),
        }
    }

    pub fn unwrap_user_type(self, ns: &Namespace) -> Type {
        if let Type::UserType(type_no) = self {
            ns.user_types[type_no].ty.clone()
        } else {
            self
        }
    }
}

#[derive(PartialEq, Eq, Clone, Debug, Copy, Hash)]
pub enum StructType {
    UserDefined(usize),
    AccountInfo,
    AccountMeta,
    ExternalFunction,
    SolParameters,
}

#[derive(PartialEq, Eq, Clone, Debug)]
pub struct StructDecl {
    pub tags: Vec<Tag>,
    pub id: pt::Identifier,
    pub loc: pt::Loc,
    pub contract: Option<String>,
    pub fields: Vec<Parameter<Type>>,
    // List of offsets of the fields, last entry is the offset for the struct overall size
    pub offsets: Vec<BigInt>,
    // Same, but now in storage
    pub storage_offsets: Vec<BigInt>,
}

#[derive(PartialEq, Eq, Clone, Debug)]
pub struct EventDecl {
    pub tags: Vec<Tag>,
    pub id: pt::Identifier,
    pub loc: pt::Loc,
    pub contract: Option<usize>,
    pub fields: Vec<Parameter<Type>>,
    pub signature: String,
    pub anonymous: bool,
    pub used: bool,
}

impl EventDecl {
    pub fn symbol_name(&self, ns: &Namespace) -> String {
        match &self.contract {
            Some(c) => format!("{}.{}", ns.contracts[*c].id, self.id),
            None => self.id.to_string(),
        }
    }
}

#[derive(Default, PartialEq, Eq, Clone, Debug)]
pub struct ErrorDecl {
    pub tags: Vec<Tag>,
    pub name: String,
    pub loc: pt::Loc,
    pub contract: Option<usize>,
    pub fields: Vec<Parameter<Type>>,
    pub used: bool,
}

impl ErrorDecl {
    pub fn symbol_name(&self, ns: &Namespace) -> String {
        match &self.contract {
            Some(c) => format!("{}.{}", ns.contracts[*c].id, self.name),
            None => self.name.to_string(),
        }
    }
}

impl fmt::Display for StructDecl {
    /// Make the struct name into a string for printing. The struct can be declared either
    /// inside or outside a contract.
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match &self.contract {
            Some(c) => write!(f, "{}.{}", c, self.id),
            None => write!(f, "{}", self.id),
        }
    }
}

#[derive(Debug)]
pub struct EnumDecl {
    pub tags: Vec<Tag>,
    pub id: pt::Identifier,
    pub contract: Option<String>,
    pub loc: pt::Loc,
    pub ty: Type,
    pub values: IndexMap<String, pt::Loc>,
}

impl fmt::Display for EnumDecl {
    /// Make the enum name into a string for printing. The enum can be declared either
    /// inside or outside a contract.
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match &self.contract {
            Some(c) => write!(f, "{}.{}", c, self.id),
            None => write!(f, "{}", self.id),
        }
    }
}

#[derive(PartialEq, Eq, Clone, Debug)]
pub struct Parameter<Type> {
    pub loc: pt::Loc,
    /// The name can empty (e.g. in an event field or unnamed parameter/return)
    pub id: Option<pt::Identifier>,
    pub ty: Type,
    /// Yul function parameters may not have a type identifier
    pub ty_loc: Option<pt::Loc>,
    /// Event fields may indexed, which means they are sent to the log
    pub indexed: bool,
    /// Some builtin structs have readonly fields
    pub readonly: bool,
    /// A recursive struct may contain itself which make the struct infinite size in memory.
    pub infinite_size: bool,
    /// Is this struct field recursive. Recursive does not mean infinite size in all cases:
    /// `struct S { S[] s }` is recursive but not of infinite size.
    pub recursive: bool,

    pub annotation: Option<ParameterAnnotation>,
}

#[derive(Debug, Eq, Clone, PartialEq)]
pub struct ParameterAnnotation {
    pub loc: pt::Loc,
    pub id: pt::Identifier,
}

impl Parameter<Type> {
    /// Create a new instance of the given `Type`, with all other values set to their default.
    pub fn new_default(ty: Type) -> Self {
        Self {
            ty,
            loc: Default::default(),
            id: Default::default(),
            ty_loc: Default::default(),
            indexed: Default::default(),
            readonly: Default::default(),
            infinite_size: Default::default(),
            recursive: Default::default(),
            annotation: Default::default(),
        }
    }

    pub fn name_as_str(&self) -> &str {
        if let Some(name) = &self.id {
            name.name.as_str()
        } else {
            ""
        }
    }
}

#[derive(PartialEq, Eq, Clone, Hash, Debug)]
pub enum Mutability {
    Payable(pt::Loc),
    Nonpayable(pt::Loc),
    View(pt::Loc),
    Pure(pt::Loc),
}

impl Mutability {
    pub fn is_default(&self) -> bool {
        matches!(self, Mutability::Nonpayable(_))
    }
}

impl fmt::Display for Mutability {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Mutability::Pure(_) => write!(f, "pure"),
            Mutability::View(_) => write!(f, "view"),
            Mutability::Nonpayable(_) => write!(f, "nonpayable"),
            Mutability::Payable(_) => write!(f, "payable"),
        }
    }
}

#[derive(Debug)]
pub struct Function {
    pub tags: Vec<Tag>,
    /// The location of the prototype (not body)
    pub loc_prototype: pt::Loc,
    pub loc: pt::Loc,
    pub id: pt::Identifier,
    pub contract_no: Option<usize>,
    pub ty: pt::FunctionTy,
    pub signature: String,
    pub mutability: Mutability,
    pub visibility: pt::Visibility,
    pub params: Arc<Vec<Parameter<Type>>>,
    pub returns: Arc<Vec<Parameter<Type>>>,
    /// Constructor arguments for base contracts, only present on constructors
    pub bases: BTreeMap<usize, (pt::Loc, usize, Vec<Expression>)>,
    /// Modifiers for functions
    pub modifiers: Vec<Expression>,
    pub is_virtual: bool,
    /// Is this function an acccesor function created by a public variable
    pub is_accessor: bool,
    pub is_override: Option<(pt::Loc, Vec<usize>)>,
    /// The selector (known as discriminator on Solana/Anchor)
    pub selector: Option<(pt::Loc, Vec<u8>)>,
    /// Was the function declared with a body
    pub has_body: bool,
    /// The resolved body (if any)
    pub body: Vec<Statement>,
    pub symtable: Symtable,
    /// What events are emitted by the body of this function
    pub emits_events: Vec<usize>,
    /// For overloaded functions this is the mangled (unique) name.
    pub mangled_name: String,
    /// Solana constructors may have seeds specified using @seed tags
    pub annotations: ConstructorAnnotations,
    /// Which contracts should we use the mangled name in?
    pub mangled_name_contracts: HashSet<usize>,
    /// This indexmap stores the accounts this functions needs to be called on Solana
    /// The string is the account's name
    pub solana_accounts: RefCell<IndexMap<String, SolanaAccount>>,
    /// List of contracts this function creates
    pub creates: Vec<(pt::Loc, usize)>,
}

/// This struct represents a Solana account. There is no name field, because
/// it is stored in a IndexMap<String, SolanaAccount> (see above)
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct SolanaAccount {
    pub loc: pt::Loc,
    pub is_signer: bool,
    pub is_writer: bool,
    /// Has the compiler automatically generated this account entry?
    pub generated: bool,
}

#[derive(Debug, Default)]
pub struct ConstructorAnnotations {
    // (annotation location, annotation expression)
    pub seeds: Vec<(pt::Loc, Expression)>,
    pub space: Option<(pt::Loc, Expression)>,
    pub bump: Option<(pt::Loc, Expression)>,
    // (annotation location, account name)
    pub payer: Option<(pt::Loc, String)>,
}

/// This trait provides a single interface for fetching parameters, returns and the symbol table
/// for both yul and solidity functions
pub trait FunctionAttributes {
    fn get_symbol_table(&self) -> &Symtable;
    fn get_parameters(&self) -> &Vec<Parameter<Type>>;
    fn get_returns(&self) -> &Vec<Parameter<Type>>;
}

impl FunctionAttributes for Function {
    fn get_symbol_table(&self) -> &Symtable {
        &self.symtable
    }

    fn get_parameters(&self) -> &Vec<Parameter<Type>> {
        &self.params
    }

    fn get_returns(&self) -> &Vec<Parameter<Type>> {
        &self.returns
    }
}

impl Function {
    pub fn new(
        loc_prototype: pt::Loc,
        loc: pt::Loc,
        id: pt::Identifier,
        contract_no: Option<usize>,
        tags: Vec<Tag>,
        ty: pt::FunctionTy,
        mutability: Option<pt::Mutability>,
        visibility: pt::Visibility,
        params: Vec<Parameter<Type>>,
        returns: Vec<Parameter<Type>>,
        ns: &Namespace,
    ) -> Self {
        let signature = match ty {
            pt::FunctionTy::Fallback => String::from("@fallback"),
            pt::FunctionTy::Receive => String::from("@receive"),
            _ => ns.signature(&id.name, &params),
        };

        let mutability = match mutability {
            None => Mutability::Nonpayable(loc_prototype),
            Some(pt::Mutability::Payable(loc)) => Mutability::Payable(loc),
            Some(pt::Mutability::Pure(loc)) => Mutability::Pure(loc),
            Some(pt::Mutability::View(loc)) => Mutability::View(loc),
            Some(pt::Mutability::Constant(loc)) => Mutability::View(loc),
        };

        let mangled_name = signature
            .replace('(', "_")
            .replace(')', "")
            .replace(',', "_")
            .replace("[]", "Array")
            .replace('[', "Array")
            .replace(']', "");

        Function {
            tags,
            loc_prototype,
            loc,
            id,
            contract_no,
            ty,
            signature,
            mutability,
            visibility,
            params: Arc::new(params),
            returns: Arc::new(returns),
            bases: BTreeMap::new(),
            modifiers: Vec::new(),
            selector: None,
            is_virtual: false,
            is_accessor: false,
            has_body: false,
            is_override: None,
            body: Vec::new(),
            symtable: Symtable::default(),
            emits_events: Vec::new(),
            mangled_name,
            annotations: ConstructorAnnotations::default(),
            mangled_name_contracts: HashSet::new(),
            solana_accounts: IndexMap::new().into(),
            creates: Vec::new(),
        }
    }

    /// Generate selector for this function
    pub fn selector(&self, ns: &Namespace, contract_no: &usize) -> Vec<u8> {
        if let Some((_, selector)) = &self.selector {
            selector.clone()
        } else if ns.target == Target::Solana {
            match self.ty {
                FunctionTy::Constructor => function_discriminator("new"),
                _ => {
                    let discriminator_image = if self.mangled_name_contracts.contains(contract_no) {
                        &self.mangled_name
                    } else {
                        &self.id.name
                    };
                    function_discriminator(discriminator_image.as_str())
                }
            }
        } else {
            let mut res = [0u8; 32];

            let mut hasher = Keccak::v256();
            hasher.update(self.signature.as_bytes());
            hasher.finalize(&mut res);

            res[..4].to_vec()
        }
    }

    /// Is this a constructor
    pub fn is_constructor(&self) -> bool {
        self.ty == pt::FunctionTy::Constructor
    }

    /// Does this function have the payable state
    pub fn is_payable(&self) -> bool {
        matches!(self.mutability, Mutability::Payable(_))
    }

    /// Does this function have an @payer annotation?
    pub fn has_payer_annotation(&self) -> bool {
        self.annotations.payer.is_some()
    }

    /// Does this function have an @seed annotation?
    pub fn has_seed_annotation(&self) -> bool {
        !self.annotations.seeds.is_empty()
    }

    /// Does this function have the pure state
    pub fn is_pure(&self) -> bool {
        matches!(self.mutability, Mutability::Pure(_))
    }

    /// Is this function visible externally, based on it's visibilty modifiers.
    ///
    /// Due to inheritance, this alone does not determine whether a function is
    /// externally callable in the final contract artifact; for that, use
    /// `Namespace::function_externally_callable()` instead.
    pub fn is_public(&self) -> bool {
        matches!(
            self.visibility,
            pt::Visibility::Public(_) | pt::Visibility::External(_)
        )
    }

    /// Is this function accessable only from same contract
    pub fn is_private(&self) -> bool {
        matches!(self.visibility, pt::Visibility::Private(_))
    }
}

impl From<&pt::Type> for Type {
    fn from(p: &pt::Type) -> Type {
        match p {
            pt::Type::Bool => Type::Bool,
            pt::Type::Address => Type::Address(false),
            pt::Type::AddressPayable => Type::Address(true),
            pt::Type::Payable => Type::Address(true),
            pt::Type::Int(n) => Type::Int(*n),
            pt::Type::Uint(n) => Type::Uint(*n),
            pt::Type::Bytes(n) => Type::Bytes(*n),
            pt::Type::String => Type::String,
            pt::Type::Rational => Type::Rational,
            pt::Type::DynamicBytes => Type::DynamicBytes,
            // needs special casing
            pt::Type::Function { .. } | pt::Type::Mapping { .. } => unimplemented!(),
        }
    }
}

#[derive(PartialEq, Eq, Clone, Debug)]
pub struct UserTypeDecl {
    pub tags: Vec<Tag>,
    pub loc: pt::Loc,
    pub name: String,
    pub ty: Type,
    pub contract: Option<String>,
}

impl fmt::Display for UserTypeDecl {
    /// Make the user type name into a string for printing. The user type can
    /// be declared either inside or outside a contract.
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match &self.contract {
            Some(c) => write!(f, "{}.{}", c, self.name),
            None => write!(f, "{}", self.name),
        }
    }
}

#[derive(Debug)]
pub struct Variable {
    pub tags: Vec<Tag>,
    pub name: String,
    pub loc: pt::Loc,
    pub ty: Type,
    pub visibility: pt::Visibility,
    pub constant: bool,
    pub immutable: bool,
    pub initializer: Option<Expression>,
    pub assigned: bool,
    pub read: bool,
    pub storage_type: Option<pt::StorageType>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Symbol {
    Enum(pt::Loc, usize),
    Function(Vec<(pt::Loc, usize)>),
    Variable(pt::Loc, Option<usize>, usize),
    Struct(pt::Loc, StructType),
    Event(Vec<(pt::Loc, usize)>),
    Error(pt::Loc, usize),
    Contract(pt::Loc, usize),
    Import(pt::Loc, usize),
    UserType(pt::Loc, usize),
}

impl CodeLocation for Symbol {
    fn loc(&self) -> pt::Loc {
        match self {
            Symbol::Enum(loc, _)
            | Symbol::Variable(loc, ..)
            | Symbol::Struct(loc, _)
            | Symbol::Contract(loc, _)
            | Symbol::Import(loc, _)
            | Symbol::Error(loc, _)
            | Symbol::UserType(loc, _) => *loc,
            Symbol::Event(items) | Symbol::Function(items) => items[0].0,
        }
    }
}

impl Symbol {
    /// Is this symbol for an event
    pub fn is_event(&self) -> bool {
        matches!(self, Symbol::Event(_))
    }

    /// Does this symbol have an accessor function
    pub fn has_accessor(&self, ns: &Namespace) -> bool {
        if let Symbol::Variable(_, Some(contract_no), var_no) = self {
            matches!(
                ns.contracts[*contract_no].variables[*var_no].visibility,
                pt::Visibility::Public(_)
            )
        } else {
            false
        }
    }

    /// Is this a private symbol
    pub fn is_private_variable(&self, ns: &Namespace) -> bool {
        match self {
            Symbol::Variable(_, Some(contract_no), var_no) => {
                let visibility = &ns.contracts[*contract_no].variables[*var_no].visibility;

                matches!(visibility, pt::Visibility::Private(_))
            }
            _ => false,
        }
    }
}

/// Any Solidity file, either the main file or anything that was imported
#[derive(Clone, Debug)]
pub struct File {
    /// The on-disk filename
    pub path: PathBuf,
    /// Used for offset to line-column conversions
    pub line_starts: Vec<usize>,
    /// Indicates the file number in FileResolver.files
    pub cache_no: Option<usize>,
    /// Index into FileResolver.import_paths. This is `None` when this File was
    /// created not during `parse_and_resolve` (e.g., builtins)
    pub import_no: Option<usize>,
}

/// When resolving a Solidity file, this holds all the resolved items
#[derive(Debug)]
pub struct Namespace {
    pub target: Target,
    pub pragmas: Vec<Pragma>,
    pub files: Vec<File>,
    pub enums: Vec<EnumDecl>,
    pub structs: Vec<StructDecl>,
    pub events: Vec<EventDecl>,
    pub errors: Vec<ErrorDecl>,
    pub contracts: Vec<Contract>,
    /// Global using declarations
    pub using: Vec<Using>,
    /// All type declarations
    pub user_types: Vec<UserTypeDecl>,
    /// All functions
    pub functions: Vec<Function>,
    /// Yul functions
    pub yul_functions: Vec<YulFunction>,
    /// Global constants
    pub constants: Vec<Variable>,
    /// address length in bytes
    pub address_length: usize,
    /// value length in bytes
    pub value_length: usize,
    pub diagnostics: Diagnostics,
    /// There is a separate namespace for functions and non-functions
    pub function_symbols: HashMap<(usize, Option<usize>, String), Symbol>,
    /// Symbol key is file_no, contract, identifier
    pub variable_symbols: HashMap<(usize, Option<usize>, String), Symbol>,
    // each variable in the symbol table should have a unique number
    pub next_id: usize,
    /// For a variable reference at a location, give the constant value
    /// This for use by the language server to show the value of a variable at a location
    pub var_constants: HashMap<pt::Loc, codegen::Expression>,
    /// Overrides for hover in the language server
    pub hover_overrides: HashMap<pt::Loc, String>,
}

#[derive(Debug)]
pub enum Pragma {
    Identifier {
        loc: pt::Loc,
        name: pt::Identifier,
        value: pt::Identifier,
    },
    StringLiteral {
        loc: pt::Loc,
        name: pt::Identifier,
        value: pt::StringLiteral,
    },
    SolidityVersion {
        loc: pt::Loc,
        versions: Vec<VersionReq>,
    },
}

#[derive(Debug)]
pub enum VersionReq {
    Plain {
        loc: pt::Loc,
        version: Version,
    },
    Operator {
        loc: pt::Loc,
        op: pt::VersionOp,
        version: Version,
    },
    Range {
        loc: pt::Loc,
        from: Version,
        to: Version,
    },
    Or {
        loc: pt::Loc,
        left: Box<VersionReq>,
        right: Box<VersionReq>,
    },
}

#[derive(Debug, Clone, PartialEq)]
pub struct Version {
    pub major: u32,
    pub minor: Option<u32>,
    pub patch: Option<u32>,
}

impl fmt::Display for Version {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.major.fmt(f)?;
        if let Some(minor) = self.minor {
            f.write_char('.')?;
            minor.fmt(f)?
        }
        if let Some(patch) = self.patch {
            f.write_char('.')?;
            patch.fmt(f)?;
        }
        Ok(())
    }
}

#[derive(Debug)]
pub struct Layout {
    pub slot: BigInt,
    pub contract_no: usize,
    pub var_no: usize,
    pub ty: Type,
}

#[derive(Debug)]
pub struct Base {
    pub loc: pt::Loc,
    pub contract_no: usize,
    pub constructor: Option<(usize, Vec<Expression>)>,
}

#[derive(Debug)]
pub struct Using {
    pub list: UsingList,
    pub ty: Option<Type>,
    pub file_no: Option<usize>,
}

#[derive(Debug)]
pub enum UsingList {
    Library(usize),
    Functions(Vec<UsingFunction>),
}

/// Using binding for a function, optionally for an operator
#[derive(Debug)]
pub struct UsingFunction {
    pub loc: pt::Loc,
    pub function_no: usize,
    pub oper: Option<pt::UserDefinedOperator>,
}

#[derive(Debug)]
pub struct Contract {
    pub tags: Vec<Tag>,
    pub loc: pt::Loc,
    pub ty: pt::ContractTy,
    pub id: pt::Identifier,
    pub bases: Vec<Base>,
    pub using: Vec<Using>,
    pub layout: Vec<Layout>,
    pub fixed_layout_size: BigInt,
    pub functions: Vec<usize>,
    pub all_functions: BTreeMap<usize, usize>,
    /// maps the name of virtual functions to a vector of overriden functions.
    /// Each time a virtual function is overriden, there will be an entry pushed to the vector. The last
    /// element represents the current overriding function - there will be at least one entry in this vector.
    pub virtual_functions: HashMap<String, Vec<usize>>,
    pub yul_functions: Vec<usize>,
    pub variables: Vec<Variable>,
    /// List of contracts this contract instantiates
    pub creates: Vec<usize>,
    /// List of events this contract may emit
    pub emits_events: Vec<usize>,
    pub initializer: Option<usize>,
    pub default_constructor: Option<(Function, usize)>,
    pub cfg: Vec<ControlFlowGraph>,
    /// Compiled program. Only available after emit.
    pub code: OnceCell<Vec<u8>>,
    /// Can the contract be instantiated, i.e. not abstract, no errors, etc.
    pub instantiable: bool,
    /// Account of deployed program code on Solana
    pub program_id: Option<Vec<u8>>,
}

impl Contract {
    // Is this a concrete contract, which can be instantiated
    pub fn is_concrete(&self) -> bool {
        matches!(self.ty, pt::ContractTy::Contract(_))
    }

    // Is this an interface
    pub fn is_interface(&self) -> bool {
        matches!(self.ty, pt::ContractTy::Interface(_))
    }

    // Is this an library
    pub fn is_library(&self) -> bool {
        matches!(self.ty, pt::ContractTy::Library(_))
    }

    /// Does the constructor require arguments. Should be false is there is no constructor
    pub fn constructor_needs_arguments(&self, ns: &Namespace) -> bool {
        !self.constructors(ns).is_empty() && self.no_args_constructor(ns).is_none()
    }

    /// Does the contract have a constructor defined?
    /// Returns all the constructor function numbers if any
    pub fn constructors(&self, ns: &Namespace) -> Vec<usize> {
        self.functions
            .iter()
            .copied()
            .filter(|func_no| ns.functions[*func_no].is_constructor())
            .collect::<Vec<usize>>()
    }

    /// Return the constructor with no arguments
    pub fn no_args_constructor(&self, ns: &Namespace) -> Option<usize> {
        self.functions
            .iter()
            .find(|func_no| {
                let func = &ns.functions[**func_no];

                func.is_constructor() && func.params.is_empty()
            })
            .cloned()
    }
}

#[derive(PartialEq, Eq, Clone, Debug)]
pub enum Expression {
    BoolLiteral {
        loc: pt::Loc,
        value: bool,
    },
    BytesLiteral {
        loc: pt::Loc,
        ty: Type,
        value: Vec<u8>,
    },
    NumberLiteral {
        loc: pt::Loc,
        ty: Type,
        value: BigInt,
    },
    RationalNumberLiteral {
        loc: pt::Loc,
        ty: Type,
        value: BigRational,
    },
    StructLiteral {
        loc: pt::Loc,
        id: pt::IdentifierPath,
        ty: Type,
        /// pt::Identifier represents the field name
        values: Vec<(Option<pt::Identifier>, Expression)>,
    },
    ArrayLiteral {
        loc: pt::Loc,
        ty: Type,
        dimensions: Vec<u32>,
        values: Vec<Expression>,
    },
    ConstArrayLiteral {
        loc: pt::Loc,
        ty: Type,
        dimensions: Vec<u32>,
        values: Vec<Expression>,
    },
    Add {
        loc: pt::Loc,
        ty: Type,
        /// Do not check for overflow, i.e. in `unchecked {}` block
        unchecked: bool,
        left: Box<Expression>,
        right: Box<Expression>,
    },
    Subtract {
        loc: pt::Loc,
        ty: Type,
        /// Do not check for overflow, i.e. in `unchecked {}` block
        unchecked: bool,
        left: Box<Expression>,
        right: Box<Expression>,
    },
    Multiply {
        loc: pt::Loc,
        ty: Type,
        /// Do not check for overflow, i.e. in `unchecked {}` block
        unchecked: bool,
        left: Box<Expression>,
        right: Box<Expression>,
    },
    Divide {
        loc: pt::Loc,
        ty: Type,
        left: Box<Expression>,
        right: Box<Expression>,
    },
    Modulo {
        loc: pt::Loc,
        ty: Type,
        left: Box<Expression>,
        right: Box<Expression>,
    },
    Power {
        loc: pt::Loc,
        ty: Type,
        /// Do not check for overflow, i.e. in `unchecked {}` block
        unchecked: bool,
        base: Box<Expression>,
        exp: Box<Expression>,
    },
    BitwiseOr {
        loc: pt::Loc,
        ty: Type,
        left: Box<Expression>,
        right: Box<Expression>,
    },
    BitwiseAnd {
        loc: pt::Loc,
        ty: Type,
        left: Box<Expression>,
        right: Box<Expression>,
    },
    BitwiseXor {
        loc: pt::Loc,
        ty: Type,
        left: Box<Expression>,
        right: Box<Expression>,
    },
    ShiftLeft {
        loc: pt::Loc,
        ty: Type,
        left: Box<Expression>,
        right: Box<Expression>,
    },
    ShiftRight {
        loc: pt::Loc,
        ty: Type,
        left: Box<Expression>,
        right: Box<Expression>,
        sign: bool,
    },
    Variable {
        loc: pt::Loc,
        ty: Type,
        var_no: usize,
    },
    ConstantVariable {
        loc: pt::Loc,
        ty: Type,
        contract_no: Option<usize>,
        var_no: usize,
    },
    StorageVariable {
        loc: pt::Loc,
        ty: Type,
        contract_no: usize,
        var_no: usize,
    },
    Load {
        loc: pt::Loc,
        ty: Type,
        expr: Box<Expression>,
    },
    GetRef {
        loc: pt::Loc,
        ty: Type,
        expr: Box<Expression>,
    },
    StorageLoad {
        loc: pt::Loc,
        ty: Type,
        expr: Box<Expression>,
    },
    ZeroExt {
        loc: pt::Loc,
        to: Type,
        expr: Box<Expression>,
    },
    SignExt {
        loc: pt::Loc,
        to: Type,
        expr: Box<Expression>,
    },
    Trunc {
        loc: pt::Loc,
        to: Type,
        expr: Box<Expression>,
    },
    CheckingTrunc {
        loc: pt::Loc,
        to: Type,
        expr: Box<Expression>,
    },
    Cast {
        loc: pt::Loc,
        to: Type,
        expr: Box<Expression>,
    },
    BytesCast {
        loc: pt::Loc,
        from: Type,
        to: Type,
        expr: Box<Expression>,
    },
    PreIncrement {
        loc: pt::Loc,
        ty: Type,
        /// Do not check for overflow, i.e. in `unchecked {}` block
        unchecked: bool,
        expr: Box<Expression>,
    },
    PreDecrement {
        loc: pt::Loc,
        ty: Type,
        /// Do not check for overflow, i.e. in `unchecked {}` block
        unchecked: bool,
        expr: Box<Expression>,
    },
    PostIncrement {
        loc: pt::Loc,
        ty: Type,
        /// Do not check for overflow, i.e. in `unchecked {}` block
        unchecked: bool,
        expr: Box<Expression>,
    },
    PostDecrement {
        loc: pt::Loc,
        ty: Type,
        /// Do not check for overflow, i.e. in `unchecked {}` block
        unchecked: bool,
        expr: Box<Expression>,
    },
    Assign {
        loc: pt::Loc,
        ty: Type,
        left: Box<Expression>,
        right: Box<Expression>,
    },
    More {
        loc: pt::Loc,
        left: Box<Expression>,
        right: Box<Expression>,
    },
    Less {
        loc: pt::Loc,
        left: Box<Expression>,
        right: Box<Expression>,
    },
    MoreEqual {
        loc: pt::Loc,
        left: Box<Expression>,
        right: Box<Expression>,
    },
    LessEqual {
        loc: pt::Loc,
        left: Box<Expression>,
        right: Box<Expression>,
    },
    Equal {
        loc: pt::Loc,
        left: Box<Expression>,
        right: Box<Expression>,
    },
    NotEqual {
        loc: pt::Loc,
        left: Box<Expression>,
        right: Box<Expression>,
    },

    Not {
        loc: pt::Loc,
        expr: Box<Expression>,
    },
    BitwiseNot {
        loc: pt::Loc,
        ty: Type,
        expr: Box<Expression>,
    },
    Negate {
        loc: pt::Loc,
        ty: Type,
        /// Do not check for overflow, i.e. in `unchecked {}` block
        unchecked: bool,
        expr: Box<Expression>,
    },

    ConditionalOperator {
        loc: pt::Loc,
        ty: Type,
        cond: Box<Expression>,
        true_option: Box<Expression>,
        false_option: Box<Expression>,
    },
    Subscript {
        loc: pt::Loc,
        ty: Type,
        array_ty: Type,
        array: Box<Expression>,
        index: Box<Expression>,
    },
    NamedMember {
        loc: pt::Loc,
        ty: Type,
        array: Box<Expression>,
        name: String,
    },
    StructMember {
        loc: pt::Loc,
        ty: Type,
        expr: Box<Expression>,
        field: usize,
    },

    AllocDynamicBytes {
        loc: pt::Loc,
        ty: Type,
        length: Box<Expression>,
        init: Option<Vec<u8>>,
    },
    StorageArrayLength {
        loc: pt::Loc,
        ty: Type,
        array: Box<Expression>,
        elem_ty: Type,
    },
    StringCompare {
        loc: pt::Loc,
        left: StringLocation<Expression>,
        right: StringLocation<Expression>,
    },

    Or {
        loc: pt::Loc,
        left: Box<Expression>,
        right: Box<Expression>,
    },
    And {
        loc: pt::Loc,
        left: Box<Expression>,
        right: Box<Expression>,
    },
    InternalFunction {
        loc: pt::Loc,
        id: pt::IdentifierPath,
        ty: Type,
        function_no: usize,
        signature: Option<String>,
    },
    ExternalFunction {
        loc: pt::Loc,
        ty: Type,
        address: Box<Expression>,
        function_no: usize,
    },
    InternalFunctionCall {
        loc: pt::Loc,
        returns: Vec<Type>,
        function: Box<Expression>,
        args: Vec<Expression>,
    },
    ExternalFunctionCall {
        loc: pt::Loc,
        returns: Vec<Type>,
        function: Box<Expression>,
        args: Vec<Expression>,
        call_args: CallArgs,
    },
    ExternalFunctionCallRaw {
        loc: pt::Loc,
        ty: CallTy,
        address: Box<Expression>,
        args: Box<Expression>,
        call_args: CallArgs,
    },
    Constructor {
        loc: pt::Loc,
        contract_no: usize,
        constructor_no: Option<usize>,
        args: Vec<Expression>,
        call_args: CallArgs,
    },
    FormatString {
        loc: pt::Loc,
        format: Vec<(FormatArg, Expression)>,
    },
    Builtin {
        loc: pt::Loc,
        tys: Vec<Type>,
        kind: Builtin,
        args: Vec<Expression>,
    },
    List {
        loc: pt::Loc,
        list: Vec<Expression>,
    },
    UserDefinedOperator {
        loc: pt::Loc,
        ty: Type,
        oper: pt::UserDefinedOperator,
        function_no: usize,
        args: Vec<Expression>,
    },
    EventSelector {
        loc: pt::Loc,
        ty: Type,
        event_no: usize,
    },
    TypeOperator {
        loc: pt::Loc,
        ty: Type,
    },
}

#[derive(PartialEq, Eq, Clone, Default, Debug)]
pub struct CallArgs {
    pub gas: Option<Box<Expression>>,
    pub salt: Option<Box<Expression>>,
    pub value: Option<Box<Expression>>,
    pub accounts: ExternalCallAccounts<Box<Expression>>,
    pub seeds: Option<Box<Expression>>,
    pub flags: Option<Box<Expression>>,
    pub program_id: Option<Box<Expression>>,
}

/// This enum manages the accounts in an external call on Solana. There can be three options:
/// 1. The developer explicitly specifies there are not accounts for the call (`NoAccount`).
/// 2. The accounts call argument is absent, in which case we attempt to generate the AccountMetas
///    vector automatically (`AbsentArgumet`).
/// 3. There are accounts specified in the accounts call argument (Present).
#[derive(PartialEq, Eq, Clone, Debug, Default)]
pub enum ExternalCallAccounts<T> {
    NoAccount,
    #[default]
    AbsentArgument,
    Present(T),
}

impl<T> ExternalCallAccounts<T> {
    /// Is the accounts call argument missing?
    pub fn is_absent(&self) -> bool {
        matches!(self, ExternalCallAccounts::AbsentArgument)
    }

    /// Returns if the accounts call argument was present in the call
    pub fn argument_provided(&self) -> bool {
        matches!(
            self,
            ExternalCallAccounts::Present(_) | ExternalCallAccounts::NoAccount
        )
    }

    /// Applies a function on the nested objects
    pub fn map<P, F>(&self, func: F) -> ExternalCallAccounts<P>
    where
        F: FnOnce(&T) -> P,
    {
        match self {
            NoAccount => NoAccount,
            AbsentArgument => AbsentArgument,
            ExternalCallAccounts::Present(value) => ExternalCallAccounts::Present(func(value)),
        }
    }

    /// Transform the nested object into a reference
    pub const fn as_ref(&self) -> ExternalCallAccounts<&T> {
        match self {
            ExternalCallAccounts::Present(value) => ExternalCallAccounts::Present(value),
            NoAccount => NoAccount,
            AbsentArgument => AbsentArgument,
        }
    }

    /// Return a reference to the nested object
    pub fn unwrap(&self) -> &T {
        match self {
            ExternalCallAccounts::Present(value) => value,
            _ => panic!("unwrap called at variant without a nested object"),
        }
    }
}

impl Recurse for CallArgs {
    type ArgType = Expression;
    fn recurse<T>(&self, cx: &mut T, f: fn(expr: &Expression, ctx: &mut T) -> bool) {
        if let Some(gas) = &self.gas {
            gas.recurse(cx, f);
        }
        if let Some(salt) = &self.salt {
            salt.recurse(cx, f);
        }
        if let Some(value) = &self.value {
            value.recurse(cx, f);
        }
        if let ExternalCallAccounts::Present(accounts) = &self.accounts {
            accounts.recurse(cx, f);
        }
        if let Some(flags) = &self.flags {
            flags.recurse(cx, f);
        }
    }
}

impl Recurse for Expression {
    type ArgType = Expression;
    fn recurse<T>(&self, cx: &mut T, f: fn(expr: &Expression, ctx: &mut T) -> bool) {
        if f(self, cx) {
            match self {
                Expression::StructLiteral { values, .. } => {
                    for (_, e) in values {
                        e.recurse(cx, f);
                    }
                }

                Expression::ArrayLiteral { values, .. }
                | Expression::ConstArrayLiteral { values, .. } => {
                    for e in values {
                        e.recurse(cx, f);
                    }
                }

                Expression::Load { expr, .. }
                | Expression::StorageLoad { expr, .. }
                | Expression::ZeroExt { expr, .. }
                | Expression::SignExt { expr, .. }
                | Expression::Trunc { expr, .. }
                | Expression::CheckingTrunc { expr, .. }
                | Expression::Cast { expr, .. }
                | Expression::BytesCast { expr, .. }
                | Expression::PreIncrement { expr, .. }
                | Expression::PreDecrement { expr, .. }
                | Expression::PostIncrement { expr, .. }
                | Expression::PostDecrement { expr, .. }
                | Expression::Not { expr, .. }
                | Expression::BitwiseNot { expr, .. }
                | Expression::Negate { expr, .. }
                | Expression::GetRef { expr, .. }
                | Expression::NamedMember { array: expr, .. }
                | Expression::StructMember { expr, .. } => expr.recurse(cx, f),

                Expression::Add { left, right, .. }
                | Expression::Subtract { left, right, .. }
                | Expression::Multiply { left, right, .. }
                | Expression::Divide { left, right, .. }
                | Expression::Modulo { left, right, .. }
                | Expression::Power {
                    base: left,
                    exp: right,
                    ..
                }
                | Expression::BitwiseOr { left, right, .. }
                | Expression::BitwiseAnd { left, right, .. }
                | Expression::BitwiseXor { left, right, .. }
                | Expression::ShiftLeft { left, right, .. }
                | Expression::ShiftRight { left, right, .. }
                | Expression::Assign { left, right, .. }
                | Expression::More { left, right, .. }
                | Expression::Less { left, right, .. }
                | Expression::MoreEqual { left, right, .. }
                | Expression::LessEqual { left, right, .. }
                | Expression::Equal { left, right, .. }
                | Expression::NotEqual { left, right, .. }
                | Expression::Or { left, right, .. }
                | Expression::And { left, right, .. } => {
                    left.recurse(cx, f);
                    right.recurse(cx, f);
                }

                Expression::ConditionalOperator {
                    cond,
                    true_option: left,
                    false_option: right,
                    ..
                } => {
                    cond.recurse(cx, f);
                    left.recurse(cx, f);
                    right.recurse(cx, f);
                }
                Expression::Subscript {
                    array: left,
                    index: right,
                    ..
                } => {
                    left.recurse(cx, f);
                    right.recurse(cx, f);
                }

                Expression::AllocDynamicBytes { length, .. } => length.recurse(cx, f),
                Expression::StorageArrayLength { array, .. } => array.recurse(cx, f),
                Expression::StringCompare { left, right, .. } => {
                    if let StringLocation::RunTime(expr) = left {
                        expr.recurse(cx, f);
                    }
                    if let StringLocation::RunTime(expr) = right {
                        expr.recurse(cx, f);
                    }
                }
                Expression::InternalFunctionCall { function, args, .. } => {
                    function.recurse(cx, f);

                    for e in args {
                        e.recurse(cx, f);
                    }
                }
                Expression::ExternalFunction { address, .. } => {
                    address.recurse(cx, f);
                }
                Expression::ExternalFunctionCall {
                    function,
                    args,
                    call_args,
                    ..
                } => {
                    for e in args {
                        e.recurse(cx, f);
                    }
                    function.recurse(cx, f);
                    call_args.recurse(cx, f);
                }
                Expression::ExternalFunctionCallRaw {
                    address,
                    args,
                    call_args,
                    ..
                } => {
                    args.recurse(cx, f);
                    address.recurse(cx, f);
                    call_args.recurse(cx, f);
                }
                Expression::Constructor {
                    args, call_args, ..
                } => {
                    for e in args {
                        e.recurse(cx, f);
                    }
                    call_args.recurse(cx, f);
                }
                Expression::UserDefinedOperator { args: exprs, .. }
                | Expression::Builtin { args: exprs, .. }
                | Expression::List { list: exprs, .. } => {
                    for e in exprs {
                        e.recurse(cx, f);
                    }
                }

                Expression::FormatString { format, .. } => {
                    for (_, arg) in format {
                        arg.recurse(cx, f);
                    }
                }

                Expression::NumberLiteral { .. }
                | Expression::InternalFunction { .. }
                | Expression::ConstantVariable { .. }
                | Expression::StorageVariable { .. }
                | Expression::Variable { .. }
                | Expression::RationalNumberLiteral { .. }
                | Expression::BytesLiteral { .. }
                | Expression::BoolLiteral { .. }
                | Expression::EventSelector { .. }
                | Expression::TypeOperator { .. } => (),
            }
        }
    }
}

impl CodeLocation for Expression {
    fn loc(&self) -> pt::Loc {
        match self {
            Expression::BoolLiteral { loc, .. }
            | Expression::BytesLiteral { loc, .. }
            | Expression::NumberLiteral { loc, .. }
            | Expression::RationalNumberLiteral { loc, .. }
            | Expression::StructLiteral { loc, .. }
            | Expression::ArrayLiteral { loc, .. }
            | Expression::ConstArrayLiteral { loc, .. }
            | Expression::Add { loc, .. }
            | Expression::Subtract { loc, .. }
            | Expression::Multiply { loc, .. }
            | Expression::Divide { loc, .. }
            | Expression::Modulo { loc, .. }
            | Expression::Power { loc, .. }
            | Expression::BitwiseOr { loc, .. }
            | Expression::BitwiseAnd { loc, .. }
            | Expression::BitwiseXor { loc, .. }
            | Expression::ShiftLeft { loc, .. }
            | Expression::ShiftRight { loc, .. }
            | Expression::Variable { loc, .. }
            | Expression::ConstantVariable { loc, .. }
            | Expression::StorageVariable { loc, .. }
            | Expression::Load { loc, .. }
            | Expression::GetRef { loc, .. }
            | Expression::StorageLoad { loc, .. }
            | Expression::ZeroExt { loc, .. }
            | Expression::SignExt { loc, .. }
            | Expression::Trunc { loc, .. }
            | Expression::CheckingTrunc { loc, .. }
            | Expression::Cast { loc, .. }
            | Expression::BytesCast { loc, .. }
            | Expression::More { loc, .. }
            | Expression::Less { loc, .. }
            | Expression::MoreEqual { loc, .. }
            | Expression::LessEqual { loc, .. }
            | Expression::Equal { loc, .. }
            | Expression::NotEqual { loc, .. }
            | Expression::Not { loc, expr: _ }
            | Expression::BitwiseNot { loc, .. }
            | Expression::Negate { loc, .. }
            | Expression::ConditionalOperator { loc, .. }
            | Expression::Subscript { loc, .. }
            | Expression::StructMember { loc, .. }
            | Expression::Or { loc, .. }
            | Expression::AllocDynamicBytes { loc, .. }
            | Expression::StorageArrayLength { loc, .. }
            | Expression::StringCompare { loc, .. }
            | Expression::InternalFunction { loc, .. }
            | Expression::ExternalFunction { loc, .. }
            | Expression::InternalFunctionCall { loc, .. }
            | Expression::ExternalFunctionCall { loc, .. }
            | Expression::ExternalFunctionCallRaw { loc, .. }
            | Expression::Constructor { loc, .. }
            | Expression::PreIncrement { loc, .. }
            | Expression::PreDecrement { loc, .. }
            | Expression::PostIncrement { loc, .. }
            | Expression::PostDecrement { loc, .. }
            | Expression::Builtin { loc, .. }
            | Expression::Assign { loc, .. }
            | Expression::List { loc, list: _ }
            | Expression::FormatString { loc, format: _ }
            | Expression::And { loc, .. }
            | Expression::NamedMember { loc, .. }
            | Expression::UserDefinedOperator { loc, .. }
            | Expression::EventSelector { loc, .. }
            | Expression::TypeOperator { loc, .. } => *loc,
        }
    }
}

impl CodeLocation for Statement {
    fn loc(&self) -> pt::Loc {
        match self {
            Statement::Block { loc, .. }
            | Statement::VariableDecl(loc, ..)
            | Statement::If(loc, ..)
            | Statement::While(loc, ..)
            | Statement::For { loc, .. }
            | Statement::DoWhile(loc, ..)
            | Statement::Expression(loc, ..)
            | Statement::Delete(loc, ..)
            | Statement::Destructure(loc, ..)
            | Statement::Continue(loc, ..)
            | Statement::Break(loc, ..)
            | Statement::Revert { loc, .. }
            | Statement::Return(loc, ..)
            | Statement::Emit { loc, .. }
            | Statement::TryCatch(loc, ..)
            | Statement::Underscore(loc, ..) => *loc,
            Statement::Assembly(ia, _) => ia.loc,
        }
    }
}

impl CodeLocation for Instr {
    fn loc(&self) -> pt::Loc {
        match self {
            Instr::Set { loc, expr, .. } => match loc {
                pt::Loc::File(_, _, _) => *loc,
                _ => expr.loc(),
            },
            Instr::Call { args, .. } if args.is_empty() => pt::Loc::Codegen,
            Instr::Return { value } if value.is_empty() => pt::Loc::Codegen,
            Instr::Call { args: arr, .. } | Instr::Return { value: arr } => arr[0].loc(),
            Instr::EmitEvent { data: expr, .. }
            | Instr::BranchCond { cond: expr, .. }
            | Instr::Store { dest: expr, .. }
            | Instr::SetStorageBytes { storage: expr, .. }
            | Instr::PushStorage { storage: expr, .. }
            | Instr::PopStorage { storage: expr, .. }
            | Instr::LoadStorage { storage: expr, .. }
            | Instr::ClearStorage { storage: expr, .. }
            | Instr::ExternalCall { value: expr, .. }
            | Instr::SetStorage { value: expr, .. }
            | Instr::Constructor { gas: expr, .. }
            | Instr::ValueTransfer { address: expr, .. }
            | Instr::SelfDestruct { recipient: expr }
            | Instr::WriteBuffer { buf: expr, .. }
            | Instr::Switch { cond: expr, .. }
            | Instr::ReturnData { data: expr, .. }
            | Instr::Print { expr } => expr.loc(),

            Instr::PushMemory { value: expr, .. } => expr.loc(),

            Instr::MemCopy {
                source,
                destination,
                ..
            } => match source.loc() {
                pt::Loc::File(_, _, _) => source.loc(),
                _ => destination.loc(),
            },
            Instr::Branch { .. }
            | Instr::ReturnCode { .. }
            | Instr::Nop
            | Instr::AssertFailure { .. }
            | Instr::PopMemory { .. }
            | Instr::Unimplemented { .. } => pt::Loc::Codegen,

            Instr::AccountAccess { loc, .. } => *loc,
        }
    }
}

#[derive(PartialEq, Clone, Copy, Debug, Eq)]
pub enum FormatArg {
    StringLiteral,
    Default,
    Binary,
    Hex,
}

impl fmt::Display for FormatArg {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            FormatArg::StringLiteral => write!(f, ""),
            FormatArg::Default => write!(f, ""),
            FormatArg::Binary => write!(f, ":b"),
            FormatArg::Hex => write!(f, ":x"),
        }
    }
}

#[derive(PartialEq, Eq, Clone, Debug)]
pub enum StringLocation<T> {
    CompileTime(Vec<u8>),
    RunTime(Box<T>),
}

#[derive(PartialEq, Eq, Clone, Copy, Debug)]
pub enum Builtin {
    ContractCode,
    GetAddress,
    Balance,
    PayableSend,
    PayableTransfer,
    ArrayPush,
    ArrayPop,
    ArrayLength,
    Assert,
    Print,
    Require,
    SelfDestruct,
    Keccak256,
    Ripemd160,
    Sha256,
    Blake2_128,
    Blake2_256,
    BaseFee,
    PrevRandao,
    Gasleft,
    BlockCoinbase,
    BlockDifficulty,
    GasLimit,
    BlockNumber,
    Slot,
    Timestamp,
    Calldata,
    Sender,
    Signature,
    Value,
    Gasprice,
    Origin,
    BlockHash,
    MinimumBalance,
    AbiDecode,
    AbiEncode,
    AbiEncodePacked,
    AbiEncodeWithSelector,
    AbiEncodeWithSignature,
    AbiEncodeCall,
    MulMod,
    AddMod,
    ChainId,
    ExternalFunctionAddress,
    FunctionSelector,
    SignatureVerify,
    ReadInt8,
    ReadInt16LE,
    ReadInt32LE,
    ReadInt64LE,
    ReadInt128LE,
    ReadInt256LE,
    ReadUint8,
    ReadUint16LE,
    ReadUint32LE,
    ReadUint64LE,
    ReadUint128LE,
    ReadUint256LE,
    ReadAddress,
    WriteInt8,
    WriteInt16LE,
    WriteInt32LE,
    WriteInt64LE,
    WriteInt128LE,
    WriteInt256LE,
    WriteUint8,
    WriteUint16LE,
    WriteUint32LE,
    WriteUint64LE,
    WriteUint128LE,
    WriteUint256LE,
    WriteAddress,
    WriteString,
    WriteBytes,
    Accounts,
    UserTypeWrap,
    UserTypeUnwrap,
    ECRecover,
    StringConcat,
    BytesConcat,
    TypeMin,
    TypeMax,
    TypeName,
    TypeInterfaceId,
    TypeRuntimeCode,
    TypeCreatorCode,
    RequireAuth,
    AuthAsCurrContract,
    ExtendTtl,
    ExtendInstanceTtl,
}

#[derive(PartialEq, Eq, Clone, Debug)]
pub enum CallTy {
    Regular,
    Delegate,
    Static,
}

impl fmt::Display for CallTy {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            CallTy::Regular => write!(f, "regular"),
            CallTy::Static => write!(f, "static"),
            CallTy::Delegate => write!(f, "delegate"),
        }
    }
}

#[derive(Clone, Debug)]
#[allow(clippy::large_enum_variant)]
pub enum Statement {
    Block {
        loc: pt::Loc,
        unchecked: bool,
        statements: Vec<Statement>,
    },
    VariableDecl(pt::Loc, usize, Parameter<Type>, Option<Arc<Expression>>),
    If(pt::Loc, bool, Expression, Vec<Statement>, Vec<Statement>),
    While(pt::Loc, bool, Expression, Vec<Statement>),
    For {
        loc: pt::Loc,
        reachable: bool,
        init: Vec<Statement>,
        cond: Option<Expression>,
        next: Option<Expression>,
        body: Vec<Statement>,
    },
    DoWhile(pt::Loc, bool, Vec<Statement>, Expression),
    Expression(pt::Loc, bool, Expression),
    Delete(pt::Loc, Type, Expression),
    Destructure(pt::Loc, Vec<DestructureField>, Expression),
    Continue(pt::Loc),
    Break(pt::Loc),
    Return(pt::Loc, Option<Expression>),
    Revert {
        loc: pt::Loc,
        error_no: Option<usize>,
        args: Vec<Expression>,
    },
    Emit {
        loc: pt::Loc,
        event_no: usize,
        event_loc: pt::Loc,
        args: Vec<Expression>,
    },
    TryCatch(pt::Loc, bool, TryCatch),
    Underscore(pt::Loc),
    Assembly(InlineAssembly, bool),
}

#[derive(Clone, Debug)]
pub struct TryCatch {
    pub expr: Expression,
    pub returns: Vec<(Option<usize>, Parameter<Type>)>,
    pub ok_stmt: Vec<Statement>,
    pub errors: Vec<CatchClause>,
    pub catch_all: Option<CatchClause>,
}

#[derive(Clone, Debug)]
pub struct CatchClause {
    pub param: Option<Parameter<Type>>,
    pub param_pos: Option<usize>,
    pub stmt: Vec<Statement>,
}

#[derive(Clone, Debug)]
#[allow(clippy::large_enum_variant)]
pub enum DestructureField {
    None,
    Expression(Expression),
    VariableDecl(usize, Parameter<Type>),
}

impl OptionalCodeLocation for DestructureField {
    fn loc_opt(&self) -> Option<pt::Loc> {
        match self {
            DestructureField::None => None,
            DestructureField::Expression(e) => Some(e.loc()),
            DestructureField::VariableDecl(_, p) => Some(p.loc),
        }
    }
}

impl Recurse for Statement {
    type ArgType = Statement;
    fn recurse<T>(&self, cx: &mut T, f: fn(stmt: &Statement, ctx: &mut T) -> bool) {
        if f(self, cx) {
            match self {
                Statement::Block { statements, .. } => {
                    for stmt in statements {
                        stmt.recurse(cx, f);
                    }
                }
                Statement::If(_, _, _, then_stmt, else_stmt) => {
                    for stmt in then_stmt {
                        stmt.recurse(cx, f);
                    }

                    for stmt in else_stmt {
                        stmt.recurse(cx, f);
                    }
                }
                Statement::For { init, body, .. } => {
                    for stmt in init {
                        stmt.recurse(cx, f);
                    }

                    for stmt in body {
                        stmt.recurse(cx, f);
                    }
                }
                Statement::While(_, _, _, body) => {
                    for stmt in body {
                        stmt.recurse(cx, f);
                    }
                }
                Statement::DoWhile(_, _, body, _) => {
                    for stmt in body {
                        stmt.recurse(cx, f);
                    }
                }
                Statement::TryCatch(_, _, try_catch) => {
                    for stmt in &try_catch.ok_stmt {
                        stmt.recurse(cx, f);
                    }

                    for clause in &try_catch.errors {
                        for stmt in &clause.stmt {
                            stmt.recurse(cx, f);
                        }
                    }

                    if let Some(clause) = try_catch.catch_all.as_ref() {
                        for stmt in &clause.stmt {
                            stmt.recurse(cx, f);
                        }
                    }
                }
                _ => (),
            }
        }
    }
}

impl Statement {
    /// Shorthand for checking underscore
    pub fn is_underscore(&self) -> bool {
        matches!(&self, Statement::Underscore(_))
    }

    pub fn reachable(&self) -> bool {
        match self {
            Statement::Block { statements, .. } => statements.iter().all(|s| s.reachable()),
            Statement::Underscore(_)
            | Statement::Destructure(..)
            | Statement::VariableDecl(..)
            | Statement::Emit { .. }
            | Statement::Delete(..) => true,

            Statement::Continue(_)
            | Statement::Break(_)
            | Statement::Return(..)
            | Statement::Revert { .. } => false,

            Statement::If(_, reachable, ..)
            | Statement::While(_, reachable, ..)
            | Statement::DoWhile(_, reachable, ..)
            | Statement::Expression(_, reachable, _)
            | Statement::For { reachable, .. }
            | Statement::TryCatch(_, reachable, _)
            | Statement::Assembly(_, reachable) => *reachable,
        }
    }
}

#[derive(PartialEq, Eq, Clone, Debug)]
pub struct Tag {
    pub loc: pt::Loc,
    pub tag: String,
    pub no: usize,
    pub value: String,
}