Skip to content

types

Functions:

bit_width

bit_width(type_: DataType | None) -> int
Source code in src/irx/analysis/types.py
185
186
187
188
189
190
191
192
193
194
195
196
197
@public
def bit_width(type_: astx.DataType | None) -> int:
    """
    title: Return the nominal bit width for numeric types.
    parameters:
      type_:
        type: astx.DataType | None
    returns:
      type: int
    """
    if type_ is None:
        return 0
    return _BIT_WIDTHS.get(type(type_), 0)

clone_type

clone_type(type_: DataType) -> DataType
Source code in src/irx/analysis/types.py
38
39
40
41
42
43
44
45
46
47
48
@public
def clone_type(type_: astx.DataType) -> astx.DataType:
    """
    title: Clone an AST type by class.
    parameters:
      type_:
        type: astx.DataType
    returns:
      type: astx.DataType
    """
    return type_.__class__()

common_numeric_type

common_numeric_type(
    lhs: DataType | None, rhs: DataType | None
) -> DataType | None
Source code in src/irx/analysis/types.py
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
@public
def common_numeric_type(
    lhs: astx.DataType | None,
    rhs: astx.DataType | None,
) -> astx.DataType | None:
    """
    title: Return a widened numeric type shared by both operands.
    parameters:
      lhs:
        type: astx.DataType | None
      rhs:
        type: astx.DataType | None
    returns:
      type: astx.DataType | None
    """
    if lhs is None or rhs is None:
        return None
    if not is_numeric_type(lhs) or not is_numeric_type(rhs):
        return None

    if is_float_type(lhs) or is_float_type(rhs):
        widest = max(bit_width(lhs), bit_width(rhs))
        if widest <= BIT_WIDTH_16:
            return astx.Float16()
        if widest <= BIT_WIDTH_32:
            return astx.Float32()
        return astx.Float64()

    width = max(bit_width(lhs), bit_width(rhs))
    use_unsigned = is_unsigned_type(lhs) or is_unsigned_type(rhs)
    if use_unsigned:
        if width <= BIT_WIDTH_8:
            return astx.UInt8()
        if width <= BIT_WIDTH_16:
            return astx.UInt16()
        if width <= BIT_WIDTH_32:
            return astx.UInt32()
        if width <= BIT_WIDTH_64:
            return astx.UInt64()
        return astx.UInt128()

    if width <= BIT_WIDTH_8:
        return astx.Int8()
    if width <= BIT_WIDTH_16:
        return astx.Int16()
    if width <= BIT_WIDTH_32:
        return astx.Int32()
    return astx.Int64()

is_assignable

is_assignable(
    target: DataType | None, value: DataType | None
) -> bool
Source code in src/irx/analysis/types.py
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
@public
def is_assignable(
    target: astx.DataType | None,
    value: astx.DataType | None,
) -> bool:
    """
    title: Return whether a value type can be assigned to a target type.
    parameters:
      target:
        type: astx.DataType | None
      value:
        type: astx.DataType | None
    returns:
      type: bool
    """
    if target is None or value is None:
        return True
    if same_type(target, value):
        return True
    if is_numeric_type(target) and is_numeric_type(value):
        common = common_numeric_type(target, value)
        return common is not None and same_type(target, common)
    if is_string_type(target) and is_string_type(value):
        return True
    if is_none_type(target) and is_none_type(value):
        return True
    return False

is_boolean_type

is_boolean_type(type_: DataType | None) -> bool
Source code in src/irx/analysis/types.py
133
134
135
136
137
138
139
140
141
142
143
@public
def is_boolean_type(type_: astx.DataType | None) -> bool:
    """
    title: Is boolean type.
    parameters:
      type_:
        type: astx.DataType | None
    returns:
      type: bool
    """
    return isinstance(type_, astx.Boolean)

is_float_type

is_float_type(type_: DataType | None) -> bool
Source code in src/irx/analysis/types.py
107
108
109
110
111
112
113
114
115
116
117
@public
def is_float_type(type_: astx.DataType | None) -> bool:
    """
    title: Is float type.
    parameters:
      type_:
        type: astx.DataType | None
    returns:
      type: bool
    """
    return isinstance(type_, FLOAT_TYPES)

is_integer_type

is_integer_type(type_: DataType | None) -> bool
Source code in src/irx/analysis/types.py
68
69
70
71
72
73
74
75
76
77
78
@public
def is_integer_type(type_: astx.DataType | None) -> bool:
    """
    title: Is integer type.
    parameters:
      type_:
        type: astx.DataType | None
    returns:
      type: bool
    """
    return isinstance(type_, INT_TYPES + UINT_TYPES)

is_none_type

is_none_type(type_: DataType | None) -> bool
Source code in src/irx/analysis/types.py
172
173
174
175
176
177
178
179
180
181
182
@public
def is_none_type(type_: astx.DataType | None) -> bool:
    """
    title: Is none type.
    parameters:
      type_:
        type: astx.DataType | None
    returns:
      type: bool
    """
    return isinstance(type_, astx.NoneType)

is_numeric_type

is_numeric_type(type_: DataType | None) -> bool
Source code in src/irx/analysis/types.py
120
121
122
123
124
125
126
127
128
129
130
@public
def is_numeric_type(type_: astx.DataType | None) -> bool:
    """
    title: Is numeric type.
    parameters:
      type_:
        type: astx.DataType | None
    returns:
      type: bool
    """
    return is_integer_type(type_) or is_float_type(type_)

is_signed_integer_type

is_signed_integer_type(type_: DataType | None) -> bool
Source code in src/irx/analysis/types.py
81
82
83
84
85
86
87
88
89
90
91
@public
def is_signed_integer_type(type_: astx.DataType | None) -> bool:
    """
    title: Is signed integer type.
    parameters:
      type_:
        type: astx.DataType | None
    returns:
      type: bool
    """
    return isinstance(type_, INT_TYPES)

is_string_type

is_string_type(type_: DataType | None) -> bool
Source code in src/irx/analysis/types.py
146
147
148
149
150
151
152
153
154
155
156
@public
def is_string_type(type_: astx.DataType | None) -> bool:
    """
    title: Is string type.
    parameters:
      type_:
        type: astx.DataType | None
    returns:
      type: bool
    """
    return isinstance(type_, STRING_TYPES)

is_temporal_type

is_temporal_type(type_: DataType | None) -> bool
Source code in src/irx/analysis/types.py
159
160
161
162
163
164
165
166
167
168
169
@public
def is_temporal_type(type_: astx.DataType | None) -> bool:
    """
    title: Is temporal type.
    parameters:
      type_:
        type: astx.DataType | None
    returns:
      type: bool
    """
    return isinstance(type_, TEMPORAL_TYPES)

is_unsigned_type

is_unsigned_type(type_: DataType | None) -> bool
Source code in src/irx/analysis/types.py
 94
 95
 96
 97
 98
 99
100
101
102
103
104
@public
def is_unsigned_type(type_: astx.DataType | None) -> bool:
    """
    title: Is unsigned type.
    parameters:
      type_:
        type: astx.DataType | None
    returns:
      type: bool
    """
    return isinstance(type_, UINT_TYPES)

same_type

same_type(
    lhs: DataType | None, rhs: DataType | None
) -> bool
Source code in src/irx/analysis/types.py
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
@public
def same_type(lhs: astx.DataType | None, rhs: astx.DataType | None) -> bool:
    """
    title: Return whether two AST types share the same class.
    parameters:
      lhs:
        type: astx.DataType | None
      rhs:
        type: astx.DataType | None
    returns:
      type: bool
    """
    if lhs is None or rhs is None:
        return False
    return lhs.__class__ is rhs.__class__