Skip to content

types

Provide the small AST-type predicates and promotion helpers that the semantic analyzer reuses across many node visitors.

Functions:

bit_width

bit_width(type_: DataType | None) -> int
Source code in packages/irx/src/irx/analysis/types.py
469
470
471
472
473
474
475
476
477
478
479
480
481
482
@public
@typechecked
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 packages/irx/src/irx/analysis/types.py
 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
@public
@typechecked
def clone_type(type_: astx.DataType) -> astx.DataType:
    """
    title: Clone an AST type by class.
    parameters:
      type_:
        type: astx.DataType
    returns:
      type: astx.DataType
    """
    if isinstance(type_, astx.UnionType):
        return astx.UnionType(
            tuple(clone_type(member) for member in type_.members),
            alias_name=type_.alias_name,
        )
    if isinstance(type_, astx.TemplateTypeVar):
        return astx.TemplateTypeVar(
            type_.name,
            bound=clone_type(type_.bound),
        )
    if isinstance(type_, astx.GeneratorType):
        return astx.GeneratorType(clone_type(type_.yield_type))
    if isinstance(type_, astx.StructType):
        return astx.StructType(
            type_.name,
            resolved_name=type_.resolved_name,
            module_key=type_.module_key,
            qualified_name=type_.qualified_name,
        )
    if isinstance(type_, astx.ClassType):
        return astx.ClassType(
            type_.name,
            resolved_name=type_.resolved_name,
            module_key=type_.module_key,
            qualified_name=type_.qualified_name,
            ancestor_qualified_names=type_.ancestor_qualified_names,
        )
    if isinstance(type_, astx.NamespaceType):
        return astx.NamespaceType(
            type_.namespace_key,
            namespace_kind=type_.namespace_kind,
            display_name=type_.display_name,
        )
    if isinstance(type_, astx.PointerType):
        pointee_type = (
            clone_type(type_.pointee_type)
            if type_.pointee_type is not None
            else None
        )
        return astx.PointerType(pointee_type)
    if isinstance(type_, astx.ListType):
        return astx.ListType(
            [
                clone_type(cast(astx.DataType, element_type))
                for element_type in type_.element_types
            ]
        )
    if isinstance(type_, astx.TupleType):
        return astx.TupleType(
            [
                clone_type(cast(astx.DataType, element_type))
                for element_type in type_.element_types
            ]
        )
    if isinstance(type_, astx.SetType):
        return astx.SetType(
            clone_type(cast(astx.DataType, type_.element_type))
        )
    if isinstance(type_, astx.DictType):
        return astx.DictType(
            clone_type(cast(astx.DataType, type_.key_type)),
            clone_type(cast(astx.DataType, type_.value_type)),
        )
    if isinstance(type_, astx.BufferOwnerType):
        return type_.__class__()
    if isinstance(type_, astx.OpaqueHandleType):
        return astx.OpaqueHandleType(type_.handle_name)
    if isinstance(type_, astx.BufferViewType):
        element_type = (
            clone_type(type_.element_type)
            if type_.element_type is not None
            else None
        )
        return astx.BufferViewType(element_type)
    if isinstance(type_, astx.TensorType):
        element_type = (
            clone_type(type_.element_type)
            if type_.element_type is not None
            else None
        )
        return astx.TensorType(element_type)
    return type_.__class__()

common_numeric_type

common_numeric_type(
    lhs: DataType | None, rhs: DataType | None
) -> DataType | None
Source code in packages/irx/src/irx/analysis/types.py
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
@public
@typechecked
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):
        return _common_float_type(lhs, rhs)
    return _common_integer_type(lhs, rhs)

display_type_name

display_type_name(type_: DataType | None) -> str
Source code in packages/irx/src/irx/analysis/types.py
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
@public
@typechecked
def display_type_name(type_: astx.DataType | None) -> str:
    """
    title: Return one stable human-facing type name.
    parameters:
      type_:
        type: astx.DataType | None
    returns:
      type: str
    """
    if type_ is None:
        return "<unknown>"
    if isinstance(type_, astx.UnionType):
        if type_.alias_name is not None:
            return type_.alias_name
        return " | ".join(
            display_type_name(member) for member in type_.members
        )
    if isinstance(type_, astx.TemplateTypeVar):
        return type_.name
    if isinstance(type_, astx.GeneratorType):
        return f"GeneratorType[{display_type_name(type_.yield_type)}]"
    if isinstance(type_, astx.StructType):
        return type_.qualified_name or type_.name
    if isinstance(type_, astx.ClassType):
        return type_.qualified_name or type_.name
    if isinstance(type_, astx.NamespaceType):
        visible_name = type_.display_name or type_.namespace_key
        return f"{type_.namespace_kind.value} namespace '{visible_name}'"
    if isinstance(type_, astx.PointerType):
        if type_.pointee_type is None:
            return "PointerType"
        return f"PointerType[{display_type_name(type_.pointee_type)}]"
    if isinstance(type_, astx.ListType):
        if not type_.element_types:
            return "ListType"
        return (
            "ListType["
            + ", ".join(
                display_type_name(cast(astx.DataType, member))
                for member in type_.element_types
            )
            + "]"
        )
    if isinstance(type_, astx.TupleType):
        if not type_.element_types:
            return "TupleType"
        return (
            "TupleType["
            + ", ".join(
                display_type_name(cast(astx.DataType, member))
                for member in type_.element_types
            )
            + "]"
        )
    if isinstance(type_, astx.SetType):
        return f"SetType[{display_type_name(type_.element_type)}]"
    if isinstance(type_, astx.DictType):
        return (
            "DictType["
            f"{display_type_name(type_.key_type)}, "
            f"{display_type_name(type_.value_type)}]"
        )
    if isinstance(type_, astx.OpaqueHandleType):
        return type_.handle_name
    if isinstance(type_, astx.BufferViewType):
        if type_.element_type is None:
            return "BufferViewType"
        return f"BufferViewType[{display_type_name(type_.element_type)}]"
    if isinstance(type_, astx.TensorType):
        if type_.element_type is None:
            return "TensorType"
        return f"TensorType[{display_type_name(type_.element_type)}]"
    return str(type_.__class__.__name__)

float_promotion_width_for_integer_width

float_promotion_width_for_integer_width(width: int) -> int
Source code in packages/irx/src/irx/analysis/types.py
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
@public
@typechecked
def float_promotion_width_for_integer_width(width: int) -> int:
    """
    title: Return the float width floor used when integers promote with floats.
    parameters:
      width:
        type: int
    returns:
      type: int
    """
    if width <= BIT_WIDTH_16:
        return BIT_WIDTH_16
    if width <= BIT_WIDTH_32:
        return BIT_WIDTH_32
    return BIT_WIDTH_64

is_assignable

is_assignable(
    target: DataType | None, value: DataType | None
) -> bool
Source code in packages/irx/src/irx/analysis/types.py
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
@public
@typechecked
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 isinstance(target, astx.UnionType):
        return any(is_assignable(member, value) for member in target.members)
    if isinstance(value, astx.UnionType):
        return all(is_assignable(target, member) for member in value.members)
    if isinstance(target, astx.TemplateTypeVar):
        return is_assignable(target.bound, value)
    if isinstance(value, astx.TemplateTypeVar):
        return is_assignable(target, value.bound)
    if isinstance(target, astx.GeneratorType) and isinstance(
        value,
        astx.GeneratorType,
    ):
        return is_assignable(target.yield_type, value.yield_type)
    if isinstance(target, astx.ListType) and isinstance(value, astx.ListType):
        if not target.element_types or not value.element_types:
            return True
        target_members = [
            cast(astx.DataType, member) for member in target.element_types
        ]
        value_members = [
            cast(astx.DataType, member) for member in value.element_types
        ]
        return all(
            any(
                is_assignable(target_member, value_member)
                for target_member in target_members
            )
            for value_member in value_members
        )
    if isinstance(target, astx.TupleType) and isinstance(
        value,
        astx.TupleType,
    ):
        if len(target.element_types) != len(value.element_types):
            return False
        return all(
            is_assignable(
                cast(astx.DataType, target_member),
                cast(astx.DataType, value_member),
            )
            for target_member, value_member in zip(
                target.element_types,
                value.element_types,
            )
        )
    if isinstance(target, astx.SetType) and isinstance(value, astx.SetType):
        return is_assignable(target.element_type, value.element_type)
    if isinstance(target, astx.DictType) and isinstance(value, astx.DictType):
        return is_assignable(
            target.key_type, value.key_type
        ) and is_assignable(
            target.value_type,
            value.value_type,
        )
    if isinstance(target, astx.ClassType) and isinstance(
        value, astx.ClassType
    ):
        target_identity = target.qualified_name or target.name
        value_identity = value.qualified_name or value.name
        if value_identity == target_identity:
            return True
        return target_identity in value.ancestor_qualified_names
    if is_integer_type(target) and is_integer_type(value):
        return _is_safe_integer_assignment(target, value)
    if is_float_type(target) and is_numeric_type(value):
        return _is_safe_float_assignment(target, value)
    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 packages/irx/src/irx/analysis/types.py
413
414
415
416
417
418
419
420
421
422
423
424
@public
@typechecked
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_explicitly_castable

is_explicitly_castable(
    source: DataType | None, target: DataType | None
) -> bool
Source code in packages/irx/src/irx/analysis/types.py
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
@public
@typechecked
def is_explicitly_castable(
    source: astx.DataType | None,
    target: astx.DataType | None,
) -> bool:
    """
    title: Return whether an explicit Cast expression is allowed.
    parameters:
      source:
        type: astx.DataType | None
      target:
        type: astx.DataType | None
    returns:
      type: bool
    """
    if source is None or target is None:
        return True
    if is_assignable(target, source):
        return True
    if (is_numeric_type(source) or is_boolean_type(source)) and (
        is_numeric_type(target) or is_boolean_type(target)
    ):
        return True
    if isinstance(target, (astx.String, astx.UTF8String)):
        return (
            is_string_type(source)
            or is_numeric_type(source)
            or (is_boolean_type(source))
        )
    return False

is_float_type

is_float_type(type_: DataType | None) -> bool
Source code in packages/irx/src/irx/analysis/types.py
385
386
387
388
389
390
391
392
393
394
395
396
@public
@typechecked
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 packages/irx/src/irx/analysis/types.py
343
344
345
346
347
348
349
350
351
352
353
354
@public
@typechecked
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 packages/irx/src/irx/analysis/types.py
455
456
457
458
459
460
461
462
463
464
465
466
@public
@typechecked
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 packages/irx/src/irx/analysis/types.py
399
400
401
402
403
404
405
406
407
408
409
410
@public
@typechecked
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 packages/irx/src/irx/analysis/types.py
357
358
359
360
361
362
363
364
365
366
367
368
@public
@typechecked
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 packages/irx/src/irx/analysis/types.py
427
428
429
430
431
432
433
434
435
436
437
438
@public
@typechecked
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 packages/irx/src/irx/analysis/types.py
441
442
443
444
445
446
447
448
449
450
451
452
@public
@typechecked
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 packages/irx/src/irx/analysis/types.py
371
372
373
374
375
376
377
378
379
380
381
382
@public
@typechecked
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 packages/irx/src/irx/analysis/types.py
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
@public
@typechecked
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
    if isinstance(lhs, astx.UnionType) and isinstance(rhs, astx.UnionType):
        if lhs.alias_name != rhs.alias_name:
            return False
        if len(lhs.members) != len(rhs.members):
            return False
        return all(
            same_type(left_member, right_member)
            for left_member, right_member in zip(lhs.members, rhs.members)
        )
    if isinstance(lhs, astx.TemplateTypeVar) and isinstance(
        rhs,
        astx.TemplateTypeVar,
    ):
        return lhs.name == rhs.name and same_type(lhs.bound, rhs.bound)
    if isinstance(lhs, astx.GeneratorType) and isinstance(
        rhs,
        astx.GeneratorType,
    ):
        return same_type(lhs.yield_type, rhs.yield_type)
    if isinstance(lhs, astx.StructType) and isinstance(rhs, astx.StructType):
        lhs_identity = lhs.qualified_name or lhs.name
        rhs_identity = rhs.qualified_name or rhs.name
        return lhs_identity == rhs_identity
    if isinstance(lhs, astx.ClassType) and isinstance(rhs, astx.ClassType):
        lhs_identity = lhs.qualified_name or lhs.name
        rhs_identity = rhs.qualified_name or rhs.name
        return lhs_identity == rhs_identity
    if isinstance(lhs, astx.NamespaceType) and isinstance(
        rhs,
        astx.NamespaceType,
    ):
        return (
            lhs.namespace_key == rhs.namespace_key
            and lhs.namespace_kind is rhs.namespace_kind
        )
    if isinstance(lhs, astx.PointerType) and isinstance(rhs, astx.PointerType):
        if lhs.pointee_type is None or rhs.pointee_type is None:
            return lhs.pointee_type is None and rhs.pointee_type is None
        return same_type(lhs.pointee_type, rhs.pointee_type)
    if isinstance(lhs, astx.ListType) and isinstance(rhs, astx.ListType):
        if len(lhs.element_types) != len(rhs.element_types):
            return False
        return all(
            same_type(
                cast(astx.DataType, left_member),
                cast(astx.DataType, right_member),
            )
            for left_member, right_member in zip(
                lhs.element_types,
                rhs.element_types,
            )
        )
    if isinstance(lhs, astx.TupleType) and isinstance(rhs, astx.TupleType):
        if len(lhs.element_types) != len(rhs.element_types):
            return False
        return all(
            same_type(
                cast(astx.DataType, left_member),
                cast(astx.DataType, right_member),
            )
            for left_member, right_member in zip(
                lhs.element_types,
                rhs.element_types,
            )
        )
    if isinstance(lhs, astx.SetType) and isinstance(rhs, astx.SetType):
        return same_type(lhs.element_type, rhs.element_type)
    if isinstance(lhs, astx.DictType) and isinstance(rhs, astx.DictType):
        return same_type(lhs.key_type, rhs.key_type) and same_type(
            lhs.value_type,
            rhs.value_type,
        )
    if isinstance(lhs, astx.OpaqueHandleType) and isinstance(
        rhs,
        astx.OpaqueHandleType,
    ):
        return lhs.handle_name == rhs.handle_name
    if isinstance(lhs, astx.BufferViewType) and isinstance(
        rhs,
        astx.BufferViewType,
    ):
        if lhs.element_type is None or rhs.element_type is None:
            return True
        return same_type(lhs.element_type, rhs.element_type)
    if isinstance(lhs, astx.TensorType) and isinstance(
        rhs,
        astx.TensorType,
    ):
        if lhs.element_type is None or rhs.element_type is None:
            return True
        return same_type(lhs.element_type, rhs.element_type)
    return lhs.__class__ is rhs.__class__