99# the GNU General License
1010# ----------------------------------------------------------------------
1111
12+ import enum
1213import os
1314
15+ from typing import Optional
16+
1417from .decorator import classproperty
1518
1619
2831# ----------------------------------------------------------------------
2932
3033
31- class CLASS ( object ) :
34+ class CLASS :
3235 """ Enums class constants
3336 """
3437 unknown = 'unknown' # 0
@@ -69,115 +72,114 @@ def to_string(cls, class_):
6972 return cls ._CLASS_NAMES [class_ ]
7073
7174
72- class ARRAY ( object ) :
75+ class ARRAY :
7376 """ Enums array constants
7477 """
7578 bound_size = 2 # This might change depending on arch, program, etc..
7679 bound_count = 2 # Size of bounds counter
7780 array_type_size = 1 # Size of array type
7881
7982
80- class TYPE (object ):
81- """ Enums type constants
83+ @enum .unique
84+ class TYPE (enum .IntEnum ):
85+ """ Enums primary type constants
8286 """
83- auto = unknown = None
84- byte_ = 1
87+ unknown = 0
88+ byte = 1
8589 ubyte = 2
8690 integer = 3
8791 uinteger = 4
88- long_ = 5
92+ long = 5
8993 ulong = 6
9094 fixed = 7
91- float_ = 8
95+ float = 8
9296 string = 9
9397
94- TYPE_SIZES = {
95- byte_ : 1 , ubyte : 1 ,
96- integer : 2 , uinteger : 2 ,
97- long_ : 4 , ulong : 4 ,
98- fixed : 4 , float_ : 5 ,
99- string : 2 , unknown : 0
100- }
101-
102- TYPE_NAMES = {
103- byte_ : 'byte' , ubyte : 'ubyte' ,
104- integer : 'integer' , uinteger : 'uinteger' ,
105- long_ : 'long' , ulong : 'ulong' ,
106- fixed : 'fixed' , float_ : 'float' ,
107- string : 'string' , unknown : 'none'
108- }
98+ @classmethod
99+ def type_size (cls , type_ : 'TYPE' ):
100+ type_sizes = {
101+ cls .byte : 1 , cls .ubyte : 1 ,
102+ cls .integer : 2 , cls .uinteger : 2 ,
103+ cls .long : 4 , cls .ulong : 4 ,
104+ cls .fixed : 4 , cls .float : 5 ,
105+ cls .string : 2 , cls .unknown : 0
106+ }
107+ return type_sizes [type_ ]
109108
110109 @classproperty
111110 def types (cls ):
112- return tuple ( cls . TYPE_SIZES . keys () )
111+ return set ( TYPE )
113112
114113 @classmethod
115- def size (cls , type_ ):
116- return cls .TYPE_SIZES . get (type_ , None )
114+ def size (cls , type_ : 'TYPE' ):
115+ return cls .type_size (type_ )
117116
118117 @classproperty
119118 def integral (cls ):
120- return ( cls .byte_ , cls .ubyte , cls .integer , cls .uinteger ,
121- cls .long_ , cls .ulong )
119+ return { cls .byte , cls .ubyte , cls .integer , cls .uinteger ,
120+ cls .long , cls .ulong }
122121
123122 @classproperty
124123 def signed (cls ):
125- return ( cls .byte_ , cls .integer , cls .long_ , cls .fixed , cls .float_ )
124+ return { cls .byte , cls .integer , cls .long , cls .fixed , cls .float }
126125
127126 @classproperty
128127 def unsigned (cls ):
129- return ( cls .ubyte , cls .uinteger , cls .ulong )
128+ return { cls .ubyte , cls .uinteger , cls .ulong }
130129
131130 @classproperty
132131 def decimals (cls ):
133- return ( cls .fixed , cls .float_ )
132+ return { cls .fixed , cls .float }
134133
135134 @classproperty
136135 def numbers (cls ):
137- return tuple ( list ( cls .integral ) + list (cls .decimals ) )
136+ return set ( cls .integral ) | set (cls .decimals )
138137
139138 @classmethod
140- def is_valid (cls , type_ ):
139+ def is_valid (cls , type_ : 'TYPE' ):
141140 """ Whether the given type is
142141 valid or not.
143142 """
144143 return type_ in cls .types
145144
146145 @classmethod
147- def is_signed (cls , type_ ):
146+ def is_signed (cls , type_ : 'TYPE' ):
148147 return type_ in cls .signed
149148
150149 @classmethod
151- def is_unsigned (cls , type_ ):
150+ def is_unsigned (cls , type_ : 'TYPE' ):
152151 return type_ in cls .unsigned
153152
154153 @classmethod
155- def to_signed (cls , type_ ):
154+ def to_signed (cls , type_ : 'TYPE' ):
156155 """ Return signed type or equivalent
157156 """
158157 if type_ in cls .unsigned :
159- return {TYPE .ubyte : TYPE .byte_ ,
158+ return {TYPE .ubyte : TYPE .byte ,
160159 TYPE .uinteger : TYPE .integer ,
161- TYPE .ulong : TYPE .long_ }[type_ ]
160+ TYPE .ulong : TYPE .long }[type_ ]
162161 if type_ in cls .decimals or type_ in cls .signed :
163162 return type_
164163 return cls .unknown
165164
166- @classmethod
167- def to_string (cls , type_ ):
165+ @staticmethod
166+ def to_string (type_ : 'TYPE' ):
168167 """ Return ID representation (string) of a type
169168 """
170- return cls . TYPE_NAMES [ type_ ]
169+ return type_ . name
171170
172- @classmethod
173- def to_type (cls , typename ) :
171+ @staticmethod
172+ def to_type (typename : str ) -> Optional [ 'TYPE' ] :
174173 """ Converts a type ID to name. On error returns None
175174 """
176- NAME_TYPES = {cls .TYPE_NAMES [x ]: x for x in cls .TYPE_NAMES }
177- return NAME_TYPES .get (typename , None )
175+ for t in TYPE :
176+ if t .name == typename :
177+ return t
178+
179+ return None
178180
179181
180- class SCOPE ( object ) :
182+ class SCOPE :
181183 """ Enum scopes
182184 """
183185 unknown = None
@@ -202,7 +204,7 @@ def to_string(cls, scope):
202204 return cls ._names [scope ]
203205
204206
205- class KIND ( object ) :
207+ class KIND :
206208 """ Enum kind
207209 """
208210 unknown = None
@@ -229,7 +231,7 @@ def to_string(cls, kind):
229231 return cls ._NAMES .get (kind )
230232
231233
232- class CONVENTION ( object ) :
234+ class CONVENTION :
233235 unknown = None
234236 fastcall = '__fastcall__'
235237 stdcall = '__stdcall__'
@@ -268,4 +270,4 @@ def to_string(cls, convention):
268270ID_TYPES = TYPE .types
269271
270272# Maps deprecated suffixes to types
271- SUFFIX_TYPE = {'$' : TYPE .string , '%' : TYPE .integer , '&' : TYPE .long_ }
273+ SUFFIX_TYPE = {'$' : TYPE .string , '%' : TYPE .integer , '&' : TYPE .long }
0 commit comments