4
4
5
5
import re
6
6
import copy
7
+ from typing import Union , Type , Optional , Dict , Any
7
8
8
9
9
10
class ExpressionException (SmtlibError ):
@@ -17,10 +18,9 @@ class ExpressionException(SmtlibError):
17
18
class Expression :
18
19
""" Abstract taintable Expression. """
19
20
20
- def __init__ (self , taint = ()):
21
+ def __init__ (self , taint : Union [ tuple , frozenset ] = ()):
21
22
if self .__class__ is Expression :
22
23
raise TypeError
23
- assert isinstance (taint , (tuple , frozenset ))
24
24
super ().__init__ ()
25
25
self ._taint = frozenset (taint )
26
26
@@ -111,10 +111,10 @@ def taint_with(arg, *taints, value_bits=256, index_bits=256):
111
111
112
112
113
113
class Variable (Expression ):
114
- def __init__ (self , name , * args , ** kwargs ):
114
+ def __init__ (self , name : str , * args , ** kwargs ):
115
115
if self .__class__ is Variable :
116
116
raise TypeError
117
- assert isinstance ( name , str ) and " " not in name
117
+ assert " " not in name
118
118
super ().__init__ (* args , ** kwargs )
119
119
self ._name = name
120
120
@@ -137,10 +137,9 @@ def __repr__(self):
137
137
138
138
139
139
class Constant (Expression ):
140
- def __init__ (self , value , * args , ** kwargs ):
140
+ def __init__ (self , value : Union [ bool , int ] , * args , ** kwargs ):
141
141
if self .__class__ is Constant :
142
142
raise TypeError
143
- assert isinstance (value , (bool , int ))
144
143
super ().__init__ (* args , ** kwargs )
145
144
self ._value = value
146
145
@@ -174,10 +173,9 @@ class Bool(Expression):
174
173
def __init__ (self , * operands , ** kwargs ):
175
174
super ().__init__ (* operands , ** kwargs )
176
175
177
- def cast (self , value , ** kwargs ):
176
+ def cast (self , value : Union [ int , bool ], ** kwargs ) -> Union [ "BoolConstant" , "Bool" ] :
178
177
if isinstance (value , Bool ):
179
178
return value
180
- assert isinstance (value , (int , bool ))
181
179
return BoolConstant (bool (value ), ** kwargs )
182
180
183
181
def __cmp__ (self , * args ):
@@ -227,8 +225,7 @@ def declaration(self):
227
225
228
226
229
227
class BoolConstant (Bool , Constant ):
230
- def __init__ (self , value , * args , ** kwargs ):
231
- assert isinstance (value , bool )
228
+ def __init__ (self , value : bool , * args , ** kwargs ):
232
229
super ().__init__ (value , * args , ** kwargs )
233
230
234
231
def __bool__ (self ):
@@ -251,9 +248,7 @@ def __init__(self, a, b, **kwargs):
251
248
252
249
253
250
class BoolOr (BoolOperation ):
254
- def __init__ (self , a , b , ** kwargs ):
255
- assert isinstance (a , Bool )
256
- assert isinstance (b , Bool )
251
+ def __init__ (self , a : "Bool" , b : "Bool" , ** kwargs ):
257
252
super ().__init__ (a , b , ** kwargs )
258
253
259
254
@@ -263,10 +258,7 @@ def __init__(self, a, b, **kwargs):
263
258
264
259
265
260
class BoolITE (BoolOperation ):
266
- def __init__ (self , cond , true , false , ** kwargs ):
267
- assert isinstance (true , Bool )
268
- assert isinstance (false , Bool )
269
- assert isinstance (cond , Bool )
261
+ def __init__ (self , cond : "Bool" , true : "Bool" , false : "Bool" , ** kwargs ):
270
262
super ().__init__ (cond , true , false , ** kwargs )
271
263
272
264
@@ -285,7 +277,9 @@ def mask(self):
285
277
def signmask (self ):
286
278
return 1 << (self .size - 1 )
287
279
288
- def cast (self , value , ** kwargs ):
280
+ def cast (
281
+ self , value : Union ["BitVec" , str , int , bytes ], ** kwargs
282
+ ) -> Union ["BitVecConstant" , "BitVec" ]:
289
283
if isinstance (value , BitVec ):
290
284
assert value .size == self .size
291
285
return value
@@ -481,8 +475,7 @@ def declaration(self):
481
475
482
476
483
477
class BitVecConstant (BitVec , Constant ):
484
- def __init__ (self , size , value , * args , ** kwargs ):
485
- assert isinstance (value , int )
478
+ def __init__ (self , size : int , value : int , * args , ** kwargs ):
486
479
super ().__init__ (size , value , * args , ** kwargs )
487
480
488
481
def __bool__ (self ):
@@ -568,9 +561,7 @@ def __init__(self, a, b, *args, **kwargs):
568
561
569
562
570
563
class BitVecOr (BitVecOperation ):
571
- def __init__ (self , a , b , * args , ** kwargs ):
572
- assert isinstance (a , BitVec )
573
- assert isinstance (b , BitVec )
564
+ def __init__ (self , a : BitVec , b : BitVec , * args , ** kwargs ):
574
565
assert a .size == b .size
575
566
super ().__init__ (a .size , a , b , * args , ** kwargs )
576
567
@@ -647,10 +638,11 @@ def __init__(self, a, b, *args, **kwargs):
647
638
###############################################################################
648
639
# Array BV32 -> BV8 or BV64 -> BV8
649
640
class Array (Expression ):
650
- def __init__ (self , index_bits , index_max , value_bits , * operands , ** kwargs ):
641
+ def __init__ (
642
+ self , index_bits : int , index_max : Optional [int ], value_bits : int , * operands , ** kwargs
643
+ ):
651
644
assert index_bits in (32 , 64 , 256 )
652
645
assert value_bits in (8 , 16 , 32 , 64 , 256 )
653
- assert index_max is None or isinstance (index_max , int )
654
646
assert index_max is None or index_max >= 0 and index_max < 2 ** index_bits
655
647
self ._index_bits = index_bits
656
648
self ._index_max = index_max
@@ -690,21 +682,24 @@ def cast(self, possible_array):
690
682
return arr
691
683
raise ValueError # cast not implemented
692
684
693
- def cast_index (self , index ) :
685
+ def cast_index (self , index : Union [ int , "BitVec" ]) -> Union [ "BitVecConstant" , "BitVec" ] :
694
686
if isinstance (index , int ):
695
687
# assert self.index_max is None or index >= 0 and index < self.index_max
696
688
return BitVecConstant (self .index_bits , index )
697
- assert isinstance ( index , BitVec ) and index .size == self .index_bits
689
+ assert index .size == self .index_bits
698
690
return index
699
691
700
- def cast_value (self , value ):
692
+ def cast_value (
693
+ self , value : Union ["BitVec" , str , bytes , int ]
694
+ ) -> Union ["BitVecConstant" , "BitVec" ]:
695
+ if isinstance (value , BitVec ):
696
+ assert value .size == self .value_bits
697
+ return value
701
698
if isinstance (value , (str , bytes )) and len (value ) == 1 :
702
699
value = ord (value )
703
- if isinstance (value , int ):
704
- return BitVecConstant (self .value_bits , value )
705
- assert isinstance (value , BitVec )
706
- assert value .size == self .value_bits
707
- return value
700
+ if not isinstance (value , int ):
701
+ value = int (value )
702
+ return BitVecConstant (self .value_bits , value )
708
703
709
704
def __len__ (self ):
710
705
if self .index_max is None :
@@ -875,18 +870,16 @@ def declaration(self):
875
870
876
871
877
872
class ArrayOperation (Array , Operation ):
878
- def __init__ (self , array , * operands , ** kwargs ):
879
- assert isinstance (array , Array )
873
+ def __init__ (self , array : Array , * operands , ** kwargs ):
880
874
super ().__init__ (
881
875
array .index_bits , array .index_max , array .value_bits , array , * operands , ** kwargs
882
876
)
883
877
884
878
885
879
class ArrayStore (ArrayOperation ):
886
- def __init__ (self , array , index , value , * args , ** kwargs ):
887
- assert isinstance (array , Array )
888
- assert isinstance (index , BitVec ) and index .size == array .index_bits
889
- assert isinstance (value , BitVec ) and value .size == array .value_bits
880
+ def __init__ (self , array : "Array" , index : "BitVec" , value : "BitVec" , * args , ** kwargs ):
881
+ assert index .size == array .index_bits
882
+ assert value .size == array .value_bits
890
883
super ().__init__ (array , index , value , * args , ** kwargs )
891
884
892
885
@property
@@ -907,7 +900,9 @@ def value(self):
907
900
908
901
909
902
class ArraySlice (Array ):
910
- def __init__ (self , array , offset , size , * args , ** kwargs ):
903
+ def __init__ (
904
+ self , array : Union ["Array" , "ArrayProxy" ], offset : int , size : int , * args , ** kwargs
905
+ ):
911
906
if not isinstance (array , Array ):
912
907
raise ValueError ("Array expected" )
913
908
if isinstance (array , ArrayProxy ):
@@ -954,16 +949,15 @@ def store(self, index, value):
954
949
955
950
956
951
class ArrayProxy (Array ):
957
- def __init__ (self , array , default = None ):
958
- assert isinstance (array , Array )
952
+ def __init__ (self , array : Array , default : Optional [int ] = None ):
959
953
self ._default = default
960
- self ._concrete_cache = {}
954
+ self ._concrete_cache : Dict [ int , int ] = {}
961
955
self ._written = None
962
956
if isinstance (array , ArrayProxy ):
963
957
# copy constructor
964
958
super ().__init__ (array .index_bits , array .index_max , array .value_bits )
965
- self ._array = array ._array
966
- self ._name = array ._name
959
+ self ._array : Array = array ._array
960
+ self ._name : str = array ._name
967
961
if default is None :
968
962
self ._default = array ._default
969
963
self ._concrete_cache = dict (array ._concrete_cache )
@@ -1138,9 +1132,8 @@ def get(self, index, default=None):
1138
1132
1139
1133
1140
1134
class ArraySelect (BitVec , Operation ):
1141
- def __init__ (self , array , index , * args , ** kwargs ):
1142
- assert isinstance (array , Array )
1143
- assert isinstance (index , BitVec ) and index .size == array .index_bits
1135
+ def __init__ (self , array : "Array" , index : "BitVec" , * args , ** kwargs ):
1136
+ assert index .size == array .index_bits
1144
1137
super ().__init__ (array .value_bits , array , index , * args , ** kwargs )
1145
1138
1146
1139
@property
@@ -1156,27 +1149,21 @@ def __repr__(self):
1156
1149
1157
1150
1158
1151
class BitVecSignExtend (BitVecOperation ):
1159
- def __init__ (self , operand , size_dest , * args , ** kwargs ):
1160
- assert isinstance (operand , BitVec )
1161
- assert isinstance (size_dest , int )
1152
+ def __init__ (self , operand : "BitVec" , size_dest : int , * args , ** kwargs ):
1162
1153
assert size_dest >= operand .size
1163
1154
super ().__init__ (size_dest , operand , * args , ** kwargs )
1164
1155
self .extend = size_dest - operand .size
1165
1156
1166
1157
1167
1158
class BitVecZeroExtend (BitVecOperation ):
1168
- def __init__ (self , size_dest , operand , * args , ** kwargs ):
1169
- assert isinstance (operand , BitVec )
1170
- assert isinstance (size_dest , int )
1159
+ def __init__ (self , size_dest : int , operand : "BitVec" , * args , ** kwargs ):
1171
1160
assert size_dest >= operand .size
1172
1161
super ().__init__ (size_dest , operand , * args , ** kwargs )
1173
1162
self .extend = size_dest - operand .size
1174
1163
1175
1164
1176
1165
class BitVecExtract (BitVecOperation ):
1177
- def __init__ (self , operand , offset , size , * args , ** kwargs ):
1178
- assert isinstance (offset , int )
1179
- assert isinstance (size , int )
1166
+ def __init__ (self , operand : "BitVec" , offset : int , size : int , * args , ** kwargs ):
1180
1167
assert offset >= 0 and offset + size <= operand .size
1181
1168
super ().__init__ (size , operand , * args , ** kwargs )
1182
1169
self ._begining = offset
@@ -1196,17 +1183,22 @@ def end(self):
1196
1183
1197
1184
1198
1185
class BitVecConcat (BitVecOperation ):
1199
- def __init__ (self , size_dest , * operands , ** kwargs ):
1200
- assert isinstance (size_dest , int )
1186
+ def __init__ (self , size_dest : int , * operands , ** kwargs ):
1201
1187
assert all (isinstance (x , BitVec ) for x in operands )
1202
1188
assert size_dest == sum (x .size for x in operands )
1203
1189
super ().__init__ (size_dest , * operands , ** kwargs )
1204
1190
1205
1191
1206
1192
class BitVecITE (BitVecOperation ):
1207
- def __init__ (self , size , condition , true_value , false_value , * args , ** kwargs ):
1208
- assert isinstance (true_value , BitVec )
1209
- assert isinstance (false_value , BitVec )
1193
+ def __init__ (
1194
+ self ,
1195
+ size : int ,
1196
+ condition : Union ["Bool" , bool ],
1197
+ true_value : "BitVec" ,
1198
+ false_value : "BitVec" ,
1199
+ * args ,
1200
+ ** kwargs ,
1201
+ ):
1210
1202
assert true_value .size == size
1211
1203
assert false_value .size == size
1212
1204
super ().__init__ (size , condition , true_value , false_value , * args , ** kwargs )
0 commit comments