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
|
- import enum
- import numpy as np
- from .abstract import Dummy, Hashable, Literal, Number, Type
- from functools import total_ordering, cached_property
- from numba.core import utils
- from numba.core.typeconv import Conversion
- from numba.np import npdatetime_helpers
- class Boolean(Hashable):
- def cast_python_value(self, value):
- return bool(value)
- def parse_integer_bitwidth(name):
- for prefix in ('int', 'uint'):
- if name.startswith(prefix):
- bitwidth = int(name[len(prefix):])
- return bitwidth
- def parse_integer_signed(name):
- signed = name.startswith('int')
- return signed
- @total_ordering
- class Integer(Number):
- def __init__(self, name, bitwidth=None, signed=None):
- super(Integer, self).__init__(name)
- if bitwidth is None:
- bitwidth = parse_integer_bitwidth(name)
- if signed is None:
- signed = parse_integer_signed(name)
- self.bitwidth = bitwidth
- self.signed = signed
- @classmethod
- def from_bitwidth(cls, bitwidth, signed=True):
- name = ('int%d' if signed else 'uint%d') % bitwidth
- return cls(name)
- def cast_python_value(self, value):
- return getattr(np, self.name)(value)
- def __lt__(self, other):
- if self.__class__ is not other.__class__:
- return NotImplemented
- if self.signed != other.signed:
- return NotImplemented
- return self.bitwidth < other.bitwidth
- @property
- def maxval(self):
- """
- The maximum value representable by this type.
- """
- if self.signed:
- return (1 << (self.bitwidth - 1)) - 1
- else:
- return (1 << self.bitwidth) - 1
- @property
- def minval(self):
- """
- The minimal value representable by this type.
- """
- if self.signed:
- return -(1 << (self.bitwidth - 1))
- else:
- return 0
- class IntegerLiteral(Literal, Integer):
- def __init__(self, value):
- self._literal_init(value)
- name = 'Literal[int]({})'.format(value)
- basetype = self.literal_type
- Integer.__init__(
- self,
- name=name,
- bitwidth=basetype.bitwidth,
- signed=basetype.signed,
- )
- def can_convert_to(self, typingctx, other):
- conv = typingctx.can_convert(self.literal_type, other)
- if conv is not None:
- return max(conv, Conversion.promote)
- Literal.ctor_map[int] = IntegerLiteral
- class BooleanLiteral(Literal, Boolean):
- def __init__(self, value):
- self._literal_init(value)
- name = 'Literal[bool]({})'.format(value)
- Boolean.__init__(
- self,
- name=name
- )
- def can_convert_to(self, typingctx, other):
- conv = typingctx.can_convert(self.literal_type, other)
- if conv is not None:
- return max(conv, Conversion.promote)
- Literal.ctor_map[bool] = BooleanLiteral
- @total_ordering
- class Float(Number):
- def __init__(self, *args, **kws):
- super(Float, self).__init__(*args, **kws)
- # Determine bitwidth
- assert self.name.startswith('float')
- bitwidth = int(self.name[5:])
- self.bitwidth = bitwidth
- def cast_python_value(self, value):
- return getattr(np, self.name)(value)
- def __lt__(self, other):
- if self.__class__ is not other.__class__:
- return NotImplemented
- return self.bitwidth < other.bitwidth
- @total_ordering
- class Complex(Number):
- def __init__(self, name, underlying_float, **kwargs):
- super(Complex, self).__init__(name, **kwargs)
- self.underlying_float = underlying_float
- # Determine bitwidth
- assert self.name.startswith('complex')
- bitwidth = int(self.name[7:])
- self.bitwidth = bitwidth
- def cast_python_value(self, value):
- return getattr(np, self.name)(value)
- def __lt__(self, other):
- if self.__class__ is not other.__class__:
- return NotImplemented
- return self.bitwidth < other.bitwidth
- class _NPDatetimeBase(Type):
- """
- Common base class for np.datetime64 and np.timedelta64.
- """
- def __init__(self, unit, *args, **kws):
- name = '%s[%s]' % (self.type_name, unit)
- self.unit = unit
- self.unit_code = npdatetime_helpers.DATETIME_UNITS[self.unit]
- super(_NPDatetimeBase, self).__init__(name, *args, **kws)
- def __lt__(self, other):
- if self.__class__ is not other.__class__:
- return NotImplemented
- # A coarser-grained unit is "smaller", i.e. less precise values
- # can be represented (but the magnitude of representable values is
- # also greater...).
- return self.unit_code < other.unit_code
- def cast_python_value(self, value):
- cls = getattr(np, self.type_name)
- if self.unit:
- return cls(value, self.unit)
- else:
- return cls(value)
- @total_ordering
- class NPTimedelta(_NPDatetimeBase):
- type_name = 'timedelta64'
- @total_ordering
- class NPDatetime(_NPDatetimeBase):
- type_name = 'datetime64'
- class EnumClass(Dummy):
- """
- Type class for Enum classes.
- """
- basename = "Enum class"
- def __init__(self, cls, dtype):
- assert isinstance(cls, type)
- assert isinstance(dtype, Type)
- self.instance_class = cls
- self.dtype = dtype
- name = "%s<%s>(%s)" % (self.basename, self.dtype, self.instance_class.__name__)
- super(EnumClass, self).__init__(name)
- @property
- def key(self):
- return self.instance_class, self.dtype
- @cached_property
- def member_type(self):
- """
- The type of this class' members.
- """
- return EnumMember(self.instance_class, self.dtype)
- class IntEnumClass(EnumClass):
- """
- Type class for IntEnum classes.
- """
- basename = "IntEnum class"
- @cached_property
- def member_type(self):
- """
- The type of this class' members.
- """
- return IntEnumMember(self.instance_class, self.dtype)
- class EnumMember(Type):
- """
- Type class for Enum members.
- """
- basename = "Enum"
- class_type_class = EnumClass
- def __init__(self, cls, dtype):
- assert isinstance(cls, type)
- assert isinstance(dtype, Type)
- self.instance_class = cls
- self.dtype = dtype
- name = "%s<%s>(%s)" % (self.basename, self.dtype, self.instance_class.__name__)
- super(EnumMember, self).__init__(name)
- @property
- def key(self):
- return self.instance_class, self.dtype
- @property
- def class_type(self):
- """
- The type of this member's class.
- """
- return self.class_type_class(self.instance_class, self.dtype)
- class IntEnumMember(EnumMember):
- """
- Type class for IntEnum members.
- """
- basename = "IntEnum"
- class_type_class = IntEnumClass
- def can_convert_to(self, typingctx, other):
- """
- Convert IntEnum members to plain integers.
- """
- if issubclass(self.instance_class, enum.IntEnum):
- conv = typingctx.can_convert(self.dtype, other)
- return max(conv, Conversion.safe)
|