Skip to content

Types

AnyType dataclass

AnyType()

Bases: TypeAttribute

name class-attribute instance-attribute

name = 'Any'

Name of the attribute in printing and other text format.

BottomType dataclass

BottomType()

Bases: TypeAttribute

name class-attribute instance-attribute

name = 'Bottom'

Name of the attribute in printing and other text format.

is_subseteq

is_subseteq(other: TypeAttribute) -> bool

Subseteq operation.

Source code in src/kirin/ir/attrs/types.py
102
103
104
105
def is_subseteq(self, other: TypeAttribute) -> bool:
    if isinstance(other, TypeVar):
        return self.is_subseteq(other.bound)
    return True

Generic dataclass

Generic(
    body: type[PyClassType] | PyClass[PyClassType],
    *vars: TypeAttribute | list | Vararg
)

Bases: TypeAttribute, Generic[PyClassType]

Source code in src/kirin/ir/attrs/types.py
379
380
381
382
383
384
385
386
387
388
def __init__(
    self,
    body: type[PyClassType] | PyClass[PyClassType],
    *vars: TypeAttribute | list | Vararg,
):
    if isinstance(body, PyClass):
        self.body = body
    else:
        self.body = PyClass(body)
    self.vars, self.vararg = _split_type_args(vars)

name class-attribute instance-attribute

name = 'Generic'

Name of the attribute in printing and other text format.

Literal dataclass

Literal(
    data: LiteralType, datatype: TypeAttribute | None = None
)

Bases: TypeAttribute, Generic[LiteralType]

Source code in src/kirin/ir/attrs/types.py
229
230
231
def __init__(self, data: LiteralType, datatype: TypeAttribute | None = None):
    self.data = data
    self.type = datatype or PyClass(type(data))

name class-attribute instance-attribute

name = 'Literal'

Name of the attribute in printing and other text format.

type instance-attribute

type: TypeAttribute = datatype or PyClass(type(data))

type of the literal, this is useful when the Python type of data does not represent the type in IR, e.g Literal(1, types.Int32)

is_equal

is_equal(other: TypeAttribute) -> bool

Check if two lattices are equal.

Source code in src/kirin/ir/attrs/types.py
233
234
def is_equal(self, other: TypeAttribute) -> bool:
    return self is other

PyClass dataclass

PyClass(
    typ: type[PyClassType],
    *,
    display_name: str | None = None,
    prefix: str = "py"
)

Bases: TypeAttribute, Generic[PyClassType]

Source code in src/kirin/ir/attrs/types.py
158
159
160
161
162
163
164
165
166
167
def __init__(
    self,
    typ: type[PyClassType],
    *,
    display_name: str | None = None,
    prefix: str = "py",
) -> None:
    self.typ = typ
    self.display_name = display_name if display_name is not None else typ.__name__
    self.prefix = prefix

name class-attribute instance-attribute

name = 'PyClass'

Name of the attribute in printing and other text format.

SingletonTypeMeta

SingletonTypeMeta(name, bases, attrs)

Bases: TypeAttributeMeta, SingletonMeta

Metaclass for singleton type attributes.

Singleton type attributes are attributes that have only one instance.

Examples: - AnyType - BottomType

Source code in src/kirin/lattice/abc.py
16
17
18
def __init__(cls, name, bases, attrs):
    super().__init__(name, bases, attrs)
    cls._instance = None

TypeAttribute dataclass

TypeAttribute()

Bases: _TypeAttribute, SimpleMeetMixin['TypeAttribute'], IsSubsetEqMixin['TypeAttribute'], BoundedLattice['TypeAttribute']

join

join(other: TypeAttribute) -> TypeAttribute

Join operation.

Source code in src/kirin/ir/attrs/types.py
63
64
65
66
67
68
69
70
def join(self, other: "TypeAttribute") -> "TypeAttribute":
    if self.is_subseteq(other):
        return other
    elif other.is_subseteq(self):
        return self
    elif isinstance(other, TypeAttribute):
        return Union(self, other)
    return AnyType()  # don't know how to join

TypeAttributeMeta

Bases: LatticeAttributeMeta

Metaclass for type attributes.

TypeVar dataclass

TypeVar(name: str, bound: TypeAttribute | None = None)

Bases: TypeAttribute

Source code in src/kirin/ir/attrs/types.py
323
324
325
def __init__(self, name: str, bound: TypeAttribute | None = None):
    self.varname = name
    self.bound = bound or AnyType()

name class-attribute instance-attribute

name = 'TypeVar'

Name of the attribute in printing and other text format.

is_equal

is_equal(other: TypeAttribute) -> bool

Check if two lattices are equal.

Source code in src/kirin/ir/attrs/types.py
327
328
329
330
331
332
def is_equal(self, other: TypeAttribute) -> bool:
    return (
        isinstance(other, TypeVar)
        and self.varname == other.varname
        and self.bound.is_equal(other.bound)
    )

Union dataclass

Union(
    typ_or_set: (
        TypeAttribute | typing.Iterable[TypeAttribute]
    ),
    *typs: TypeAttribute
)

Bases: TypeAttribute

Source code in src/kirin/ir/attrs/types.py
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
def __init__(
    self,
    typ_or_set: TypeAttribute | typing.Iterable[TypeAttribute],
    *typs: TypeAttribute,
):
    if isinstance(typ_or_set, TypeAttribute):
        params: typing.Iterable[TypeAttribute] = (typ_or_set, *typs)
    else:
        params = typ_or_set
        assert not typs, "Cannot pass multiple arguments when passing a set"

    types: frozenset[TypeAttribute] = frozenset()
    for typ in params:
        if isinstance(typ, Union):
            types = types.union(typ.types)
        else:
            types = types.union({typ})
    self.types = types

name class-attribute instance-attribute

name = 'Union'

Name of the attribute in printing and other text format.

is_equal

is_equal(other: TypeAttribute) -> bool

Check if two lattices are equal.

Source code in src/kirin/ir/attrs/types.py
280
281
def is_equal(self, other: TypeAttribute) -> bool:
    return isinstance(other, Union) and self.types == other.types

join

join(other: TypeAttribute) -> TypeAttribute

Join operation.

Source code in src/kirin/ir/attrs/types.py
286
287
288
289
290
291
292
293
294
295
def join(self, other: TypeAttribute) -> TypeAttribute:
    if self.is_subseteq(other):
        return other
    elif other.is_subseteq(self):
        return self
    elif isinstance(other, Union):
        return Union(self.types | other.types)
    elif isinstance(other, TypeAttribute):
        return Union(self.types | {other})
    return BottomType()

meet

meet(other: TypeAttribute) -> TypeAttribute

Meet operation.

Source code in src/kirin/ir/attrs/types.py
297
298
299
300
301
302
303
304
305
306
def meet(self, other: TypeAttribute) -> TypeAttribute:
    if self.is_subseteq(other):
        return self
    elif other.is_subseteq(self):
        return other
    elif isinstance(other, Union):
        return Union(self.types & other.types)
    elif isinstance(other, TypeAttribute):
        return Union(self.types & {other})
    return BottomType()

Vararg dataclass

Vararg(typ: TypeAttribute)

Bases: Attribute

name class-attribute instance-attribute

name = 'Vararg'

Name of the attribute in printing and other text format.