forked from hyperledger-solang/solang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpt.rs
1831 lines (1656 loc) · 48.2 KB
/
pt.rs
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
// SPDX-License-Identifier: Apache-2.0
//! Solidity parse tree data structures.
//!
//! See also the [Solidity documentation][sol].
//!
//! [sol]: https://docs.soliditylang.org/en/latest/grammar.html
// backwards compatibility re-export
#[doc(hidden)]
pub use crate::helpers::{CodeLocation, OptionalCodeLocation};
#[cfg(feature = "pt-serde")]
use serde::{Deserialize, Serialize};
/// A code location.
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "pt-serde", derive(Serialize, Deserialize))]
pub enum Loc {
/// Builtin
Builtin,
/// Command line
CommandLine,
/// Implicit
Implicit,
/// Codegen
Codegen,
/// The file number, start offset and end offset in bytes of the source file.
File(usize, usize, usize),
}
impl Default for Loc {
fn default() -> Self {
Self::File(0, 0, 0)
}
}
#[inline(never)]
#[cold]
#[track_caller]
fn not_a_file() -> ! {
panic!("location is not a file")
}
impl Loc {
/// Returns this location's beginning range.
#[inline]
pub fn begin_range(&self) -> Self {
match self {
Loc::File(file_no, start, _) => Loc::File(*file_no, *start, *start),
loc => *loc,
}
}
/// Returns this location's end range.
#[inline]
pub fn end_range(&self) -> Self {
match self {
Loc::File(file_no, _, end) => Loc::File(*file_no, *end, *end),
loc => *loc,
}
}
/// Returns this location's file number.
///
/// # Panics
///
/// If this location is not a file.
#[track_caller]
#[inline]
pub fn file_no(&self) -> usize {
match self {
Loc::File(file_no, _, _) => *file_no,
_ => not_a_file(),
}
}
/// Returns this location's file number if it is a file, otherwise `None`.
#[inline]
pub fn try_file_no(&self) -> Option<usize> {
match self {
Loc::File(file_no, _, _) => Some(*file_no),
_ => None,
}
}
/// Returns this location's start.
///
/// # Panics
///
/// If this location is not a file.
#[track_caller]
#[inline]
pub fn start(&self) -> usize {
match self {
Loc::File(_, start, _) => *start,
_ => not_a_file(),
}
}
/// Returns this location's end.
///
/// # Panics
///
/// If this location is not a file.
#[track_caller]
#[inline]
pub fn end(&self) -> usize {
match self {
Loc::File(_, _, end) => *end,
_ => not_a_file(),
}
}
/// Returns this location's end.
/// The returned endpoint is not part of the range.
/// This is used when a half-open range is needed.
///
/// # Panics
///
/// If this location is not a file.
#[track_caller]
#[inline]
pub fn exclusive_end(&self) -> usize {
self.end() + 1
}
/// Replaces this location's start with `other`'s.
///
/// # Panics
///
/// If this location is not a file.
#[track_caller]
#[inline]
pub fn use_start_from(&mut self, other: &Loc) {
match (self, other) {
(Loc::File(_, start, _), Loc::File(_, other_start, _)) => {
*start = *other_start;
}
_ => not_a_file(),
}
}
/// Replaces this location's end with `other`'s.
///
/// # Panics
///
/// If this location is not a file.
#[track_caller]
#[inline]
pub fn use_end_from(&mut self, other: &Loc) {
match (self, other) {
(Loc::File(_, _, end), Loc::File(_, _, other_end)) => {
*end = *other_end;
}
_ => not_a_file(),
}
}
/// See [`Loc::use_start_from`].
///
/// # Panics
///
/// If this location is not a file.
#[track_caller]
#[inline]
pub fn with_start_from(mut self, other: &Self) -> Self {
self.use_start_from(other);
self
}
/// See [`Loc::use_end_from`].
///
/// # Panics
///
/// If this location is not a file.
#[track_caller]
#[inline]
pub fn with_end_from(mut self, other: &Self) -> Self {
self.use_end_from(other);
self
}
/// Creates a new `Loc::File` by replacing `start`.
///
/// # Panics
///
/// If this location is not a file.
#[track_caller]
#[inline]
pub fn with_start(self, start: usize) -> Self {
match self {
Self::File(no, _, end) => Self::File(no, start, end),
_ => not_a_file(),
}
}
/// Creates a new `Loc::File` by replacing `end`.
///
/// # Panics
///
/// If this location is not a file.
#[track_caller]
#[inline]
pub fn with_end(self, end: usize) -> Self {
match self {
Self::File(no, start, _) => Self::File(no, start, end),
_ => not_a_file(),
}
}
/// Returns this location's range.
///
/// # Panics
///
/// If this location is not a file.
#[track_caller]
#[inline]
pub fn range(self) -> std::ops::Range<usize> {
match self {
Self::File(_, start, end) => start..end,
_ => not_a_file(),
}
}
/// Performs the union of two locations
pub fn union(&mut self, other: &Self) {
match (self, other) {
(Self::File(r_file, r_start, r_end), Self::File(l_file, l_start, l_end)) => {
assert_eq!(r_file, l_file, "cannot perform union in different files");
*r_start = std::cmp::min(*r_start, *l_start);
*r_end = std::cmp::max(*r_end, *l_end);
}
_ => unimplemented!("cannot perform union in non File Loc"),
}
}
}
/// An identifier.
///
/// `<name>`
#[derive(Debug, PartialEq, Eq, Clone)]
#[cfg_attr(feature = "pt-serde", derive(Serialize, Deserialize))]
pub struct Identifier {
/// The code location.
pub loc: Loc,
/// The identifier string.
pub name: String,
}
impl Identifier {
/// Creates a new identifier.
pub fn new(s: impl Into<String>) -> Self {
Self {
loc: Loc::default(),
name: s.into(),
}
}
}
/// A qualified identifier.
///
/// `<identifiers>.*`
#[derive(Debug, PartialEq, Eq, Clone)]
#[cfg_attr(feature = "pt-serde", derive(Serialize, Deserialize))]
pub struct IdentifierPath {
/// The code location.
pub loc: Loc,
/// The list of identifiers.
pub identifiers: Vec<Identifier>,
}
/// A comment or [doc comment][natspec].
///
/// [natspec]: https://docs.soliditylang.org/en/latest/natspec-format.html
#[derive(Debug, PartialEq, Eq, Clone)]
#[cfg_attr(feature = "pt-serde", derive(Serialize, Deserialize))]
pub enum Comment {
/// A line comment.
///
/// `// line comment`
Line(Loc, String),
/// A block doc comment.
///
/// ` /* block comment */ `
Block(Loc, String),
/// A line doc comment.
///
/// `/// line doc comment`
DocLine(Loc, String),
/// A block doc comment.
///
/// ```text
/// /**
/// * block doc comment
/// */
/// ```
DocBlock(Loc, String),
}
impl Comment {
/// Returns the comment's value.
#[inline]
pub const fn value(&self) -> &String {
match self {
Self::Line(_, s) | Self::Block(_, s) | Self::DocLine(_, s) | Self::DocBlock(_, s) => s,
}
}
/// Returns whether this is a doc comment.
#[inline]
pub const fn is_doc(&self) -> bool {
matches!(self, Self::DocLine(..) | Self::DocBlock(..))
}
/// Returns whether this is a line comment.
#[inline]
pub const fn is_line(&self) -> bool {
matches!(self, Self::Line(..) | Self::DocLine(..))
}
/// Returns whether this is a block comment.
#[inline]
pub const fn is_block(&self) -> bool {
!self.is_line()
}
}
/// The source unit of the parse tree.
///
/// Contains all of the parse tree's parts in a vector.
#[derive(Debug, PartialEq, Eq, Clone)]
#[cfg_attr(feature = "pt-serde", derive(Serialize, Deserialize))]
pub struct SourceUnit(pub Vec<SourceUnitPart>);
/// A parse tree part.
#[derive(Debug, PartialEq, Eq, Clone)]
#[cfg_attr(feature = "pt-serde", derive(Serialize, Deserialize))]
pub enum SourceUnitPart {
/// A pragma directive.
///
/// `pragma <1> <2>;`
///
/// `1` and `2` are `None` only if an error occurred during parsing.
PragmaDirective(Box<PragmaDirective>),
/// An import directive.
ImportDirective(Import),
/// A contract definition.
ContractDefinition(Box<ContractDefinition>),
/// An enum definition.
EnumDefinition(Box<EnumDefinition>),
/// A struct definition.
StructDefinition(Box<StructDefinition>),
/// An event definition.
EventDefinition(Box<EventDefinition>),
/// An error definition.
ErrorDefinition(Box<ErrorDefinition>),
/// A function definition.
FunctionDefinition(Box<FunctionDefinition>),
/// A variable definition.
VariableDefinition(Box<VariableDefinition>),
/// A type definition.
TypeDefinition(Box<TypeDefinition>),
/// An annotation.
Annotation(Box<Annotation>),
/// A `using` directive.
Using(Box<Using>),
/// A stray semicolon.
StraySemicolon(Loc),
}
/// An import statement.
#[derive(Debug, PartialEq, Eq, Clone)]
#[cfg_attr(feature = "pt-serde", derive(Serialize, Deserialize))]
pub enum Import {
/// `import <0>;`
Plain(ImportPath, Loc),
/// `import * as <1> from <0>;`
///
/// or
///
/// `import <0> as <1>;`
GlobalSymbol(ImportPath, Identifier, Loc),
/// `import { <<1.0> [as <1.1>]>,* } from <0>;`
Rename(ImportPath, Vec<(Identifier, Option<Identifier>)>, Loc),
}
/// An import statement.
#[derive(Debug, PartialEq, Eq, Clone)]
#[cfg_attr(feature = "pt-serde", derive(Serialize, Deserialize))]
pub enum ImportPath {
/// "foo.sol"
Filename(StringLiteral),
/// std.stub (experimental Solidity feature)
Path(IdentifierPath),
}
impl Import {
/// Returns the import string.
#[inline]
pub const fn literal(&self) -> Option<&StringLiteral> {
match self {
Self::Plain(ImportPath::Filename(literal), _)
| Self::GlobalSymbol(ImportPath::Filename(literal), _, _)
| Self::Rename(ImportPath::Filename(literal), _, _) => Some(literal),
_ => None,
}
}
}
/// Type alias for a list of function parameters.
pub type ParameterList = Vec<(Loc, Option<Parameter>)>;
/// A type.
#[derive(Debug, PartialEq, Eq, Clone)]
#[cfg_attr(feature = "pt-serde", derive(Serialize, Deserialize))]
pub enum Type {
/// `address`
Address,
/// `address payable`
AddressPayable,
/// `payable`
///
/// Only used as a cast.
Payable,
/// `bool`
Bool,
/// `string`
String,
/// `int<n>`
Int(u16),
/// `uint<n>`
Uint(u16),
/// `bytes<n>`
Bytes(u8),
/// `fixed`
Rational,
/// `bytes`
DynamicBytes,
/// `mapping(<key> [key_name] => <value> [value_name])`
Mapping {
/// The code location.
loc: Loc,
/// The key expression.
///
/// This is only allowed to be an elementary type or a user defined type.
key: Box<Expression>,
/// The optional key identifier.
key_name: Option<Identifier>,
/// The value expression.
value: Box<Expression>,
/// The optional value identifier.
value_name: Option<Identifier>,
},
/// `function (<params>) <attributes> [returns]`
Function {
/// The list of parameters.
params: ParameterList,
/// The list of attributes.
attributes: Vec<FunctionAttribute>,
/// The optional list of return parameters.
returns: Option<(ParameterList, Vec<FunctionAttribute>)>,
},
}
/// Dynamic type location.
#[derive(Debug, PartialEq, Eq, Clone)]
#[cfg_attr(feature = "pt-serde", derive(Serialize, Deserialize))]
pub enum StorageLocation {
/// `memory`
Memory(Loc),
/// `storage`
Storage(Loc),
/// `calldata`
Calldata(Loc),
}
/// A variable declaration.
///
/// `<ty> [storage] <name>`
#[derive(Debug, PartialEq, Eq, Clone)]
#[cfg_attr(feature = "pt-serde", derive(Serialize, Deserialize))]
pub struct VariableDeclaration {
/// The code location.
pub loc: Loc,
/// The type.
pub ty: Expression,
/// The optional memory location.
pub storage: Option<StorageLocation>,
/// The identifier.
///
/// This field is `None` only if an error occurred during parsing.
pub name: Option<Identifier>,
}
/// A struct definition.
///
/// `struct <name> { <fields>;* }`
#[derive(Debug, PartialEq, Eq, Clone)]
#[cfg_attr(feature = "pt-serde", derive(Serialize, Deserialize))]
pub struct StructDefinition {
/// The code location.
pub loc: Loc,
/// The identifier.
///
/// This field is `None` only if an error occurred during parsing.
pub name: Option<Identifier>,
/// The list of fields.
pub fields: Vec<VariableDeclaration>,
}
/// A contract part.
#[derive(Debug, PartialEq, Eq, Clone)]
#[cfg_attr(feature = "pt-serde", derive(Serialize, Deserialize))]
pub enum ContractPart {
/// A struct definition.
StructDefinition(Box<StructDefinition>),
/// An event definition.
EventDefinition(Box<EventDefinition>),
/// An enum definition.
EnumDefinition(Box<EnumDefinition>),
/// An error definition.
ErrorDefinition(Box<ErrorDefinition>),
/// A variable definition.
VariableDefinition(Box<VariableDefinition>),
/// A function definition.
FunctionDefinition(Box<FunctionDefinition>),
/// A type definition.
TypeDefinition(Box<TypeDefinition>),
/// A definition.
Annotation(Box<Annotation>),
/// A `using` directive.
Using(Box<Using>),
/// A stray semicolon.
StraySemicolon(Loc),
}
/// A pragma directive
#[derive(Debug, PartialEq, Eq, Clone)]
#[cfg_attr(feature = "pt-serde", derive(Serialize, Deserialize))]
pub enum PragmaDirective {
/// pragma a b;
Identifier(Loc, Option<Identifier>, Option<Identifier>),
/// pragma a "b";
StringLiteral(Loc, Identifier, StringLiteral),
/// pragma version =0.5.16;
Version(Loc, Identifier, Vec<VersionComparator>),
}
/// A `version` list
#[derive(Debug, PartialEq, Eq, Clone)]
#[cfg_attr(feature = "pt-serde", derive(Serialize, Deserialize))]
pub enum VersionComparator {
/// 0.8.22
Plain {
/// The code location.
loc: Loc,
/// List of versions: major, minor, patch. minor and patch are optional
version: Vec<String>,
},
/// =0.5.16
Operator {
/// The code location.
loc: Loc,
/// Semver comparison operator
op: VersionOp,
/// version number
version: Vec<String>,
},
/// foo || bar
Or {
/// The code location.
loc: Loc,
/// left part
left: Box<VersionComparator>,
/// right part
right: Box<VersionComparator>,
},
/// 0.7.0 - 0.8.22
Range {
/// The code location.
loc: Loc,
/// start of range
from: Vec<String>,
/// end of range
to: Vec<String>,
},
}
/// Comparison operator
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
#[cfg_attr(feature = "pt-serde", derive(Serialize, Deserialize))]
pub enum VersionOp {
/// `=`
Exact,
/// `>`
Greater,
/// `>=`
GreaterEq,
/// `<`
Less,
/// `<=`
LessEq,
/// `~`
Tilde,
/// `^`
Caret,
/// `*`
Wildcard,
}
/// A `using` list. See [Using].
#[derive(Debug, PartialEq, Eq, Clone)]
#[cfg_attr(feature = "pt-serde", derive(Serialize, Deserialize))]
pub enum UsingList {
/// A single identifier path.
Library(IdentifierPath),
/// List of using functions.
///
/// `{ <<identifier path> [ as <operator> ]>,* }`
Functions(Vec<UsingFunction>),
/// An error occurred during parsing.
Error,
}
/// A `using` function. See [UsingList].
///
/// `<path> [ as <oper> ]`
#[derive(Debug, PartialEq, Eq, Clone)]
#[cfg_attr(feature = "pt-serde", derive(Serialize, Deserialize))]
pub struct UsingFunction {
/// The code location.
pub loc: Loc,
/// The identifier path.
pub path: IdentifierPath,
/// The optional user-defined operator.
pub oper: Option<UserDefinedOperator>,
}
/// A user-defined operator.
///
/// See also the [Solidity blog post][ref] on user-defined operators.
///
/// [ref]: https://blog.soliditylang.org/2023/02/22/user-defined-operators/
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "pt-serde", derive(Serialize, Deserialize))]
pub enum UserDefinedOperator {
/// `&`
BitwiseAnd,
/// `~`
///
BitwiseNot,
/// `-`
///
/// Note that this is the same as `Subtract`, and that it is currently not being parsed.
Negate,
/// `|`
BitwiseOr,
/// `^`
BitwiseXor,
/// `+`
Add,
/// `/`
Divide,
/// `%`
Modulo,
/// `*`
Multiply,
/// `-`
Subtract,
/// `==`
Equal,
/// `>`
More,
/// `>=`
MoreEqual,
/// `<`
Less,
/// `<=`
LessEqual,
/// `!=`
NotEqual,
}
impl UserDefinedOperator {
/// Returns the number of arguments needed for this operator's operation.
#[inline]
pub const fn args(&self) -> usize {
match self {
UserDefinedOperator::BitwiseNot | UserDefinedOperator::Negate => 1,
_ => 2,
}
}
/// Returns whether `self` is a unary operator.
#[inline]
pub const fn is_unary(&self) -> bool {
matches!(self, Self::BitwiseNot | Self::Negate)
}
/// Returns whether `self` is a binary operator.
#[inline]
pub const fn is_binary(&self) -> bool {
!self.is_unary()
}
/// Returns whether `self` is a bitwise operator.
#[inline]
pub const fn is_bitwise(&self) -> bool {
matches!(
self,
Self::BitwiseAnd | Self::BitwiseOr | Self::BitwiseXor | Self::BitwiseNot
)
}
/// Returns whether `self` is an arithmetic operator.
#[inline]
pub const fn is_arithmetic(&self) -> bool {
matches!(
self,
Self::Add | Self::Subtract | Self::Multiply | Self::Divide | Self::Modulo
)
}
/// Returns whether this is a comparison operator.
#[inline]
pub const fn is_comparison(&self) -> bool {
matches!(
self,
Self::Equal
| Self::NotEqual
| Self::Less
| Self::LessEqual
| Self::More
| Self::MoreEqual
)
}
}
/// A `using` directive.
///
/// Can occur within contracts and libraries and at the file level.
///
/// `using <list> for <type | '*'> [global];`
#[derive(Debug, PartialEq, Eq, Clone)]
#[cfg_attr(feature = "pt-serde", derive(Serialize, Deserialize))]
pub struct Using {
/// The code location.
pub loc: Loc,
/// The list of `using` functions or a single identifier path.
pub list: UsingList,
/// The type.
///
/// This field is `None` if an error occurred or the specified type is `*`.
pub ty: Option<Expression>,
/// The optional `global` identifier.
pub global: Option<Identifier>,
}
/// The contract type.
#[derive(Debug, PartialEq, Eq, Clone)]
#[cfg_attr(feature = "pt-serde", derive(Serialize, Deserialize))]
pub enum ContractTy {
/// `abstract contract`
Abstract(Loc),
/// `contract`
Contract(Loc),
/// `interface`
Interface(Loc),
/// `library`
Library(Loc),
}
/// A function modifier invocation (see [FunctionAttribute])
/// or a contract inheritance specifier (see [ContractDefinition]).
///
/// Both have the same semantics:
///
/// `<name>[(<args>,*)]`
#[derive(Debug, PartialEq, Eq, Clone)]
#[cfg_attr(feature = "pt-serde", derive(Serialize, Deserialize))]
pub struct Base {
/// The code location.
pub loc: Loc,
/// The identifier path.
pub name: IdentifierPath,
/// The optional arguments.
pub args: Option<Vec<Expression>>,
}
/// A contract definition.
///
/// `<ty> <name> [<base>,*] { <parts>,* }`
#[derive(Debug, PartialEq, Eq, Clone)]
#[cfg_attr(feature = "pt-serde", derive(Serialize, Deserialize))]
pub struct ContractDefinition {
/// The code location.
pub loc: Loc,
/// The contract type.
pub ty: ContractTy,
/// The identifier.
///
/// This field is `None` only if an error occurred during parsing.
pub name: Option<Identifier>,
/// The list of inheritance specifiers.
pub base: Vec<Base>,
/// The list of contract parts.
pub parts: Vec<ContractPart>,
}
/// An event parameter.
///
/// `<ty> [indexed] [name]`
#[derive(Debug, PartialEq, Eq, Clone)]
#[cfg_attr(feature = "pt-serde", derive(Serialize, Deserialize))]
pub struct EventParameter {
/// The code location.
pub loc: Loc,
/// The type.
pub ty: Expression,
/// Whether this parameter is indexed.
pub indexed: bool,
/// The optional identifier.
pub name: Option<Identifier>,
}
/// An event definition.
///
/// `event <name>(<fields>,*) [anonymous];`
#[derive(Debug, PartialEq, Eq, Clone)]
#[cfg_attr(feature = "pt-serde", derive(Serialize, Deserialize))]
pub struct EventDefinition {
/// The code location.
pub loc: Loc,
/// The identifier.
///
/// This field is `None` only if an error occurred during parsing.
pub name: Option<Identifier>,
/// The list of event parameters.
pub fields: Vec<EventParameter>,
/// Whether this event is anonymous.
pub anonymous: bool,
}
/// An error parameter.
///
/// `<ty> [name]`
#[derive(Debug, PartialEq, Eq, Clone)]
#[cfg_attr(feature = "pt-serde", derive(Serialize, Deserialize))]
pub struct ErrorParameter {
/// The code location.
pub loc: Loc,
/// The type.
pub ty: Expression,
/// The optional identifier.
pub name: Option<Identifier>,
}
/// An error definition.
///
/// `error <name> (<fields>,*);`
#[derive(Debug, PartialEq, Eq, Clone)]
#[cfg_attr(feature = "pt-serde", derive(Serialize, Deserialize))]
pub struct ErrorDefinition {
/// The code location.
pub loc: Loc,
/// The `error` keyword.
pub keyword: Expression,
/// The identifier.
///
/// This field is `None` only if an error occurred during parsing.
pub name: Option<Identifier>,
/// The list of error parameters.
pub fields: Vec<ErrorParameter>,
}
/// An enum definition.
///
/// `enum <name> { <values>,* }`
#[derive(Debug, PartialEq, Eq, Clone)]
#[cfg_attr(feature = "pt-serde", derive(Serialize, Deserialize))]
pub struct EnumDefinition {
/// The code location.
pub loc: Loc,
/// The identifier.
///
/// This field is `None` only if an error occurred during parsing.
pub name: Option<Identifier>,
/// The list of values.
///
/// This field contains `None` only if an error occurred during parsing.
pub values: Vec<Option<Identifier>>,
}
/// A variable attribute.
#[derive(Debug, PartialEq, Eq, Clone)]
#[cfg_attr(feature = "pt-serde", derive(Serialize, Deserialize))]
#[repr(u8)] // for cmp; order of variants is important
pub enum VariableAttribute {
/// The visibility.
///
/// Only used for storage variables.
Visibility(Visibility),
/// `constant`
Constant(Loc),
/// `immutable`
Immutable(Loc),
/// `ovveride(<1>,*)`
Override(Loc, Vec<IdentifierPath>),
/// Storage type.
StorageType(StorageType),
}
/// Soroban storage types.
#[derive(Debug, PartialEq, Eq, Clone)]
#[cfg_attr(feature = "pt-serde", derive(Serialize, Deserialize))]
#[repr(u8)] // for cmp; order of variants is important
pub enum StorageType {
/// `Temporary`
Temporary(Option<Loc>),
/// `persistent`
Persistent(Option<Loc>),
/// `Instance`
Instance(Option<Loc>),
}
/// A variable definition.
///
/// `<ty> <attrs>* <name> [= <initializer>]`
#[derive(Debug, PartialEq, Eq, Clone)]
#[cfg_attr(feature = "pt-serde", derive(Serialize, Deserialize))]
pub struct VariableDefinition {
/// The code location.
pub loc: Loc,
/// The type.
pub ty: Expression,
/// The list of variable attributes.
pub attrs: Vec<VariableAttribute>,
/// The identifier.
///
/// This field is `None` only if an error occurred during parsing.
pub name: Option<Identifier>,
/// The optional initializer.
pub initializer: Option<Expression>,
}
/// A user type definition.
///
/// `type <name> is <ty>;`