-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathact_ast.py
1339 lines (1067 loc) · 35.6 KB
/
act_ast.py
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
from __future__ import annotations
from abc import ABCMeta, abstractmethod
from dataclasses import dataclass
from typing import Dict, List, Union, Generator
# --- top level classes ---
@dataclass
class Act:
store: Storage
contracts: List[Contract]
def find_maincontract(self)-> Generator[Contract, None, None]:
not_main = []
for key, value in self.store.items():
for nested_key, nested_value in value.items():
if isinstance(nested_value, ContractType):
not_main.append(nested_value.contract)
for c in self.contracts:
if c.name not in not_main:
yield c
def to_cnf(self):
for elem in self.contracts:
elem.to_cnf()
@dataclass
class Contract:
name: str
constructor: Constructor
behaviors: List[Behavior]
def to_cnf(self):
self.constructor.to_cnf()
for elem in self.behaviors:
elem.to_cnf()
@dataclass
class Constructor:
interface: Interface
initial_storage: List[Exp]
preConditions: List[Exp]
postConditions: List[Exp]
invariants: List[Exp]
def to_cnf(self):
cnf_pre = []
for elem in self.preConditions:
cnf_pre.extend(to_cnf(elem))
cnf_post = []
for elem in self.postConditions:
cnf_post.extend(to_cnf(elem))
cnf_inv = []
for elem in self.invariants:
cnf_inv.extend(to_cnf(elem))
self.preConditions = cnf_pre
self.postConditions = cnf_post
self.invariants = cnf_inv
@dataclass
class Behavior:
"""one function within a contract in a given case"""
name: str
interface: Interface
caseConditions: List[Exp]
preConditions: List[Exp]
postConditions: List[Exp]
returnValue: Exp | None
stateUpdates: List[Exp] #equality constraints e.g. update
def to_cnf(self):
cnf_case = []
for elem in self.caseConditions:
cnf_case.extend(to_cnf(elem))
cnf_pre = []
for elem in self.preConditions:
cnf_pre.extend(to_cnf(elem))
cnf_post = []
for elem in self.postConditions:
cnf_post.extend(to_cnf(elem))
cnf_updates = []
for elem in self.stateUpdates:
cnf_updates.extend(to_cnf(elem))
self.caseConditions = cnf_case
self.preConditions = cnf_pre
self.postConditions = cnf_post
self.stateUpdates = cnf_updates
# --- Interface ---
@dataclass
class Interface:
name: str
args: List[Decl]
@dataclass
class Decl:
name: str
type: AbiType
# --- Slot Types ---
class SlotType(metaclass=ABCMeta):
"""base class for storage variables"""
"""A description of the shape of global storage"""
Storage = Dict[str, Dict[str, SlotType]]
# --- Mapping Types ---
@dataclass
class MappingType(SlotType):
argsType: List[ValueType]
resultType: ValueType
class ValueType(SlotType, metaclass=ABCMeta):
"""base class for storage base variables"""
@dataclass
class ContractType(ValueType):
contract: str
# --- Abi Types ---
@dataclass
class AbiType(ValueType, metaclass=ABCMeta):
"""base class for solidity abi types"""
@abstractmethod
def is_equiv(self, other: AbiType) -> bool:
pass
@dataclass
class AbiUIntType(AbiType):
size: int
def is_equiv(self, other: AbiType):
if not isinstance(other, AbiUIntType):
return False
if self.size != other.size:
return False
return True
@dataclass
class AbiIntType(AbiType):
size: int
def is_equiv(self, other: AbiType):
if not isinstance(other, AbiIntType):
return False
if self.size != other.size:
return False
return True
@dataclass
class AbiAddressType(AbiType):
"""address type"""
def is_equiv(self, other: AbiType):
if not isinstance(other, AbiAddressType):
return False
return True
@dataclass
class AbiBoolType(AbiType):
"""bool type"""
def is_equiv(self, other: AbiType):
if not isinstance(other, AbiBoolType):
return False
return True
@dataclass
class AbiBytesType(AbiType):
size: int
def is_equiv(self, other: AbiType):
if not isinstance(other, AbiBytesType):
return False
if self.size != other.size:
return False
return True
@dataclass
class AbiBytesDynamicType(AbiType):
"""dynamic bytes type"""
def is_equiv(self, other: AbiType):
if not isinstance(other, AbiBytesDynamicType):
return False
return True
@dataclass
class AbiStringType(AbiType):
"""string type"""
def is_equiv(self, other: AbiType):
if not isinstance(other, AbiStringType):
return False
return True
@dataclass
class AbiArrayDynamicType(AbiType):
arraytype: AbiType
def is_equiv(self, other: AbiType):
if not isinstance(other, AbiArrayDynamicType):
return False
if self.arraytype.is_equiv(other.arraytype):
return False
return True
@dataclass
class AbiArrayType(AbiType):
size: int
arraytype: AbiType
def is_equiv(self, other: AbiType):
if not isinstance(other, AbiArrayType):
return False
if self.size != other.size:
return False
if self.arraytype.is_equiv(other.arraytype):
return False
return True
@dataclass
class AbiTupleType(AbiType):
tuple: List[AbiType]
def is_equiv(self, other: AbiType):
if not isinstance(other, AbiTupleType):
return False
if len(self.tuple) != len(other.tuple):
return False
for i in range(len(self.tuple)):
if self.tuple[i].is_equiv(other.tuple[i]):
return False
return True
@dataclass
class AbiFunctionType(AbiType):
"""function type"""
def is_equiv(self, other: AbiType):
if not isinstance(other, AbiFunctionType):
return False
return True
# --- expressions ---
class Exp(metaclass=ABCMeta):
"""base class for expressions"""
type: ActType
@abstractmethod
def is_equiv(self, other: Exp) -> bool:
pass
@abstractmethod
def copy_exp(self) -> Exp:
pass
@abstractmethod
def to_string(self) -> str:
pass
class ActType(metaclass=ABCMeta):
"""Base class for act types"""
@dataclass
class ActBool(ActType):
""""act bool type"""
@dataclass
class ActInt(ActType):
"""act int type"""
@dataclass
class ActByteStr(ActType):
"""act bytestring type"""
@dataclass
class Player(Exp):
name: str
constraints: List[Exp]
type: ActType = ActInt()
def copy_exp(self) -> Exp:
print("WARNING: players should not be copied!")
return self
def is_equiv(self, other: Exp) -> bool:
return self == other
def to_string(self) -> str:
return self.name
@dataclass
class Lit(Exp):
value: Union[bool, int, str]
type: ActType
def is_equiv(self, other: Exp) -> bool:
if not isinstance(other, Lit):
return False
if self.type != other.type:
return False
if self.value != other.value:
return False
return True
def copy_exp(self)-> Exp:
return Lit(self.value, self.type)
def to_string(self) -> str:
return str(self.value)
@dataclass
class Var(Exp):
name: str
type: ActType
def is_equiv(self, other: Exp) -> bool:
if not isinstance(other, Var):
return False
if self.type != other.type:
return False
if self.name != other.name:
return False
return True
def copy_exp(self)-> Exp:
return Var(self.name, self.type)
def to_string(self) -> str:
return self.name
@dataclass
class And(Exp):
"""conjunction of two boolean expressions"""
left: Exp
right: Exp
type: ActType = ActBool()
def is_equiv(self, other: Exp) -> bool:
if not isinstance(other, And):
return False
if self.type != other.type:
return False
if not self.left.is_equiv(other.left):
return False
if not self.right.is_equiv(other.right):
return False
return True
def copy_exp(self)-> Exp:
return And(self.left.copy_exp(), self.right.copy_exp(), self.type)
def to_string(self) -> str:
return "And(" + self.left.to_string() + "," + self.right.to_string() + ")"
@dataclass
class Or(Exp):
"""disjunction of two boolean expressions"""
left: Exp
right: Exp
type: ActType = ActBool()
def is_equiv(self, other: Exp) -> bool:
if not isinstance(other, Or):
return False
if self.type != other.type:
return False
if not self.left.is_equiv(other.left):
return False
if not self.right.is_equiv(other.right):
return False
return True
def copy_exp(self)-> Exp:
return Or(self.left.copy_exp(), self.right.copy_exp(), self.type)
def to_string(self) -> str:
return "Or(" + self.left.to_string() + "," + self.right.to_string() + ")"
@dataclass
class Not(Exp):
"""Negation of a boolean expression"""
value: Exp
type: ActType = ActBool()
def is_equiv(self, other: Exp) -> bool:
if not isinstance(other, Not):
return False
if self.type != other.type:
return False
if not self.value.is_equiv(other.value):
return False
return True
def copy_exp(self)-> Exp:
return Not(self.value.copy_exp(), self.type)
def to_string(self) -> str:
return "Not(" + self.value.to_string() + ")"
@dataclass
class Implies(Exp):
"""implication of two boolean expressions"""
left: Exp
right: Exp
type: ActType = ActBool()
def is_equiv(self, other: Exp) -> bool:
if not isinstance(other, Implies):
return False
if self.type != other.type:
return False
if not self.left.is_equiv(other.left):
return False
if not self.right.is_equiv(other.right):
return False
return True
def copy_exp(self)-> Exp:
return Implies(self.left.copy_exp(), self.right.copy_exp(), self.type)
def to_string(self) -> str:
return "Implies(" + self.left.to_string() + "," + self.right.to_string() + ")"
@dataclass
class ITE(Exp):
"""description"""
condition: Exp
left: Exp
right: Exp
type: ActType
def is_equiv(self, other: Exp) -> bool:
if not isinstance(other, ITE):
return False
if self.type != other.type:
return False
if not self.condition.is_equiv(other.condition):
return False
if not self.left.is_equiv(other.left):
return False
if not self.right.is_equiv(other.right):
return False
return True
def copy_exp(self)-> Exp:
return ITE(self.condition.copy_exp(), self.left.copy_exp(), self.right.copy_exp(), self.type)
def to_string(self) -> str:
return "ITE(" + self.condition.to_string() + "," + self.left.to_string() + "," + self.right.to_string() + ")"
@dataclass
class Eq(Exp):
left: Exp
right: Exp
type: ActType = ActBool()
def is_equiv(self, other: Exp) -> bool:
if not isinstance(other, Eq):
return False
if self.type != other.type:
return False
if not self.left.is_equiv(other.left):
return False
if not self.right.is_equiv(other.right):
return False
return True
def copy_exp(self)-> Exp:
left: Exp
right: Exp
if isinstance(self.left, Player):
left = self.left
else:
left = self.left.copy_exp()
if isinstance(self.right, Player):
right = self.right
else:
right = self.right.copy_exp()
return Eq(left, right, self.type)
def to_string(self) -> str:
return "Eq(" + self.left.to_string() + "," + self.right.to_string() + ")"
@dataclass
class Neq(Exp):
left: Exp
right: Exp
type: ActType = ActBool()
def is_equiv(self, other: Exp) -> bool:
if not isinstance(other, Neq):
return False
if self.type != other.type:
return False
if not self.left.is_equiv(other.left):
return False
if not self.right.is_equiv(other.right):
return False
return True
def copy_exp(self)-> Exp:
left: Exp
right: Exp
if isinstance(self.left, Player):
left = self.left
else:
left = self.left.copy_exp()
if isinstance(self.right, Player):
right = self.right
else:
right = self.right.copy_exp()
return Neq(left, right, self.type)
def to_string(self) -> str:
return "Neq(" + self.left.to_string() + "," + self.right.to_string() + ")"
@dataclass
class InRange(Exp):
expr: Exp
abitype: AbiType # only allow (int, uint, address, string)
type: ActType = ActBool()
def is_equiv(self, other: Exp) -> bool:
if not isinstance(other, InRange):
return False
if self.type != other.type:
return False
if not self.expr.is_equiv(other.expr):
return False
if not self.abitype.is_equiv(other.abitype):
return False
return True
def copy_exp(self)-> Exp:
return InRange(self.expr.copy_exp(), self.abitype, self.type)
def to_string(self) -> str:
assert False, "inrange constraints should not occur here"
# arithmetic
@dataclass
class Add(Exp):
"""addition of two integer expressions"""
left: Exp
right: Exp
type: ActType = ActInt()
def is_equiv(self, other: Exp) -> bool:
if not isinstance(other, Add):
return False
if self.type != other.type:
return False
if not self.left.is_equiv(other.left):
return False
if not self.right.is_equiv(other.right):
return False
return True
def copy_exp(self)-> Exp:
return Add(self.left.copy_exp(), self.right.copy_exp(), self.type)
def to_string(self) -> str:
return "Add(" + self.left.to_string() + "," + self.right.to_string() + ")"
@dataclass
class Sub(Exp):
"""subtraction of two integer expressions"""
left: Exp
right: Exp
type: ActType = ActInt()
def is_equiv(self, other: Exp) -> bool:
if not isinstance(other, Sub):
return False
if self.type != other.type:
return False
if not self.left.is_equiv(other.left):
return False
if not self.right.is_equiv(other.right):
return False
return True
def copy_exp(self)-> Exp:
return Sub(self.left.copy_exp(), self.right.copy_exp(), self.type)
def to_string(self) -> str:
return "Sub(" + self.left.to_string() + "," + self.right.to_string() + ")"
@dataclass
class Mul(Exp):
"""multiplication of two integer expressions"""
left: Exp
right: Exp
type: ActType = ActInt()
def is_equiv(self, other: Exp) -> bool:
if not isinstance(other, Mul):
return False
if self.type != other.type:
return False
if not self.left.is_equiv(other.left):
return False
if not self.right.is_equiv(other.right):
return False
return True
def copy_exp(self)-> Exp:
return Mul(self.left.copy_exp(), self.right.copy_exp(), self.type)
def to_string(self) -> str:
return "Mul(" + self.left.to_string() + "," + self.right.to_string() + ")"
@dataclass
class Div(Exp):
"""division of two integer expressions"""
left: Exp
right: Exp
type: ActType = ActInt()
def is_equiv(self, other: Exp) -> bool:
if not isinstance(other, Div):
return False
if self.type != other.type:
return False
if not self.left.is_equiv(other.left):
return False
if not self.right.is_equiv(other.right):
return False
return True
def copy_exp(self)-> Exp:
return Div(self.left.copy_exp(), self.right.copy_exp(), self.type)
def to_string(self) -> str:
return "Div(" + self.left.to_string() + "," + self.right.to_string() + ")"
@dataclass
class Pow(Exp):
"""division of two integer expressions"""
left: Exp
right: Exp
type: ActType = ActInt()
def is_equiv(self, other: Exp) -> bool:
if not isinstance(other, Pow):
return False
if self.type != other.type:
return False
if not self.left.is_equiv(other.left):
return False
if not self.right.is_equiv(other.right):
return False
return True
def copy_exp(self)-> Exp:
return Pow(self.left.copy_exp(), self.right.copy_exp(), self.type)
def to_string(self) -> str:
return "Pow(" + self.left.to_string() + "," + self.right.to_string() + ")"
# relations
@dataclass
class Lt(Exp):
"""less than comparison of two integer expressions"""
left: Exp
right: Exp
type: ActType = ActBool()
def is_equiv(self, other: Exp) -> bool:
if not isinstance(other, Lt):
return False
if self.type != other.type:
return False
if not self.left.is_equiv(other.left):
return False
if not self.right.is_equiv(other.right):
return False
return True
def copy_exp(self)-> Exp:
left: Exp
right: Exp
if isinstance(self.left, Player):
left = self.left
else:
left = self.left.copy_exp()
if isinstance(self.right, Player):
right = self.right
else:
right = self.right.copy_exp()
return Lt(left, right, self.type)
def to_string(self) -> str:
return "Lt(" + self.left.to_string() + "," + self.right.to_string() + ")"
@dataclass
class Le(Exp):
"""less than or equal comparison of two integer expressions"""
left: Exp
right: Exp
type: ActType = ActBool()
def is_equiv(self, other: Exp) -> bool:
if not isinstance(other, Le):
return False
if self.type != other.type:
return False
if not self.left.is_equiv(other.left):
return False
if not self.right.is_equiv(other.right):
return False
return True
def copy_exp(self)-> Exp:
left: Exp
right: Exp
if isinstance(self.left, Player):
left = self.left
else:
left = self.left.copy_exp()
if isinstance(self.right, Player):
right = self.right
else:
right = self.right.copy_exp()
return Le(left, right, self.type)
def to_string(self) -> str:
return "Le(" + self.left.to_string() + "," + self.right.to_string() + ")"
@dataclass
class Gt(Exp):
"""greater than comparison of two integer expressions"""
left: Exp
right: Exp
type: ActType = ActBool()
def is_equiv(self, other: Exp) -> bool:
if not isinstance(other, Gt):
return False
if self.type != other.type:
return False
if not self.left.is_equiv(other.left):
return False
if not self.right.is_equiv(other.right):
return False
return True
def copy_exp(self)-> Exp:
left: Exp
right: Exp
if isinstance(self.left, Player):
left = self.left
else:
left = self.left.copy_exp()
if isinstance(self.right, Player):
right = self.right
else:
right = self.right.copy_exp()
return Gt(left, right, self.type)
def to_string(self) -> str:
return "Gt(" + self.left.to_string() + "," + self.right.to_string() + ")"
@dataclass
class Ge(Exp):
"""greater than or equal comparison of two integer expressions"""
left: Exp
right: Exp
type: ActType = ActBool()
def is_equiv(self, other: Exp) -> bool:
if not isinstance(other, Ge):
return False
if self.type != other.type:
return False
if not self.left.is_equiv(other.left):
return False
if not self.right.is_equiv(other.right):
return False
return True
def copy_exp(self)-> Exp:
left: Exp
right: Exp
if isinstance(self.left, Player):
left = self.left
else:
left = self.left.copy_exp()
if isinstance(self.right, Player):
right = self.right
else:
right = self.right.copy_exp()
return Ge(left, right, self.type)
def to_string(self) -> str:
return "Ge(" + self.left.to_string() + "," + self.right.to_string() + ")"
# --- environment Variables ---
@dataclass
class EnvVar(Exp):
"""A reference to an environment variable (e.g. msg.sender)"""
name: str
type: ActType
def is_equiv(self, other: Exp) -> bool:
if not isinstance(other, EnvVar):
return False
if self.type != other.type:
return False
if self.name != other.name:
return False
return True
def copy_exp(self)-> Exp:
return EnvVar(self.name, self.type)
def to_string(self) -> str:
return self.name
@dataclass
class StorageItem(Exp):
"""This is TItem in TimeAgnostic.hs"""
loc: StorageLoc
time: Timing
type: ActType
def is_equiv(self, other: Exp) -> bool:
if not isinstance(other, StorageItem):
return False
if self.type != other.type:
return False
if self.time != other.time:
return False
if not self.loc.is_equiv(other.loc):
return False
return True
def copy_exp(self)-> Exp:
return StorageItem(self.loc.copy_loc(), self.time, self.type)
def to_string(self) -> str:
if self.time == Pre():
time = "Pre"
else:
time = "Post"
return time + "(" + self.loc.to_string() + ")"
# --- Storage Location ---
class StorageLoc(metaclass=ABCMeta):
"""A reference to an item in storage"""
@abstractmethod
def is_equiv(self, other: StorageLoc) -> bool:
pass
@abstractmethod
def copy_loc(self) -> StorageLoc:
pass
@abstractmethod
def to_string(self) -> str:
pass
@dataclass
class VarLoc(StorageLoc):
"""The base variable reference type
This can either be a value type, or the base of a longer chain of e.g. MappingLoc / ContractLoc expressions
"""
# the contract in which this storage location resides
contract: str
# the name of the storage location
name: str
def is_equiv(self, other: StorageLoc) -> bool:
if not isinstance(other, VarLoc):
return False
if self.contract != other.contract:
return False
if self.name != other.name:
return False
return True
def copy_loc(self) -> StorageLoc:
return VarLoc(self.contract, self.name)
def to_string(self) -> str:
return self.contract + "." + self.name
@dataclass
class MappingLoc(StorageLoc):
"""A fully applied lookup in a (potentially nested) mapping
e.g. m[4][3]
"""
# the location in storage that holds the mapping (e.g. the m in m[4][3])
loc: StorageLoc
# the arguments to the mapping that give us an actual location in storage (e.g. the [4][3] in m[4][3])
args: List[Exp]
def is_equiv(self, other: StorageLoc) -> bool:
if not isinstance(other, MappingLoc):
return False
if not self.loc.is_equiv(other.loc):
return False
if len(self.args) != len(other.args):
return False
for i in range(len(self.args)):
if not self.args[i].is_equiv(other.args[i]):
return False
return True
def copy_loc(self) -> StorageLoc:
return MappingLoc(self.loc.copy_loc(), [elem for elem in self.args])
def to_string(self) -> str:
arguments = ""
for elem in self.args:
arguments = arguments + "," + elem.to_string()
if len(arguments) > 0:
arguments = arguments[1:]
return self.loc.to_string() + "(" + arguments + ")"
@dataclass
class ContractLoc(StorageLoc):
"""A reference to a field on a contract that is held in storage
e.g. c.x.y[3]
"""
# the location in storage that holds the pointer to the contract (e.g. the c in c.x)
loc: StorageLoc
# the name of the contract that the field belongs to (e.g. the type of c in c.x)
contract: str
# the name of the field (e.g. the "x" in c.x)
field: str
def is_equiv(self, other: StorageLoc) -> bool:
if not isinstance(other, ContractLoc):
return False
if self.contract != other.contract:
return False
if self.field != other.field:
return False
if not self.loc.is_equiv(other.loc):
return False
return True
def copy_loc(self) -> StorageLoc:
return ContractLoc(self.loc.copy_loc(), self.contract, self.field)
def to_string(self) -> str:
return self.loc.to_string() + "." + self.field
class Timing(metaclass=ABCMeta):
"""Is the storage varaible refering to the pre or post state"""
@dataclass