Skip to content

Method

Method dataclass

Method(
    dialects: DialectGroup,
    code: Statement,
    *,
    nargs: int | None = None,
    mod: ModuleType | None = None,
    py_func: typing.Callable[Param, RetType] | None = None,
    sym_name: str | None = None,
    arg_names: list[str] | None = None,
    fields: tuple = (),
    file: str = "",
    lineno_begin: int = 0,
    inferred: bool = False
)

Bases: Printable, Generic[Param, RetType]

Source code in src/kirin/ir/method.py
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
def __init__(
    self,
    dialects: DialectGroup,
    code: Statement,
    *,
    nargs: int | None = None,
    mod: ModuleType | None = None,
    py_func: typing.Callable[Param, RetType] | None = None,
    sym_name: str | None = None,
    arg_names: list[str] | None = None,
    fields: tuple = (),
    file: str = "",
    lineno_begin: int = 0,
    inferred: bool = False,
):
    callable_node = code
    if entry_point := code.get_trait(EntryPointInterface):
        callable_node = entry_point.get_entry_point(code)

    trait = callable_node.get_present_trait(CallableStmtInterface)
    region = trait.get_callable_region(callable_node)
    if sym_name is None and (
        symbol_trait := callable_node.get_trait(SymbolOpInterface)
    ):
        sym_name = symbol_trait.get_sym_name(callable_node).data

    assert (
        len(region.blocks[0].args) > 0
    ), "Method must have at least have a self argument"

    self.dialects = dialects
    self.code = code
    self.nargs = nargs if nargs is not None else len(region.blocks[0].args)
    self.mod = mod
    self.py_func = py_func
    self.sym_name = sym_name
    self.arg_names = arg_names or [
        arg.name or f"arg{i}" for i, arg in enumerate(region.blocks[0].args)
    ]
    self.fields = fields
    self.file = file
    self.lineno_begin = lineno_begin
    self.inferred = inferred

arg_names class-attribute instance-attribute

arg_names: list[str] | None = arg_names or [
    name or f"arg{i}" for (i, arg) in enumerate(args)
]

The argument names of the callable statement. None if no keyword arguments allowed.

arg_types property

arg_types

Return the types of the arguments of the method. (excluding self)

args property

args

Return the arguments of the method. (excluding self)

code instance-attribute

code: Statement = code

The code of the method. This should be a statement with CallableStmtInterface trait.

dialects instance-attribute

dialects: 'DialectGroup' = dialects

The dialects that creates the method. This should be a DialectGroup.

fields class-attribute instance-attribute

fields: tuple = fields

values captured in the method if it is a closure.

file class-attribute instance-attribute

file: str = file

The file where the method is defined. Empty string if no file.

inferred class-attribute instance-attribute

inferred: bool = inferred

if typeinfer has been run on this method

lineno_begin class-attribute instance-attribute

lineno_begin: int = lineno_begin

The line number where the method is defined. 0 if no line number.

mod class-attribute instance-attribute

mod: ModuleType | None = mod

The module where the method is defined. None if no module.

nargs instance-attribute

nargs: int = nargs if nargs is not None else len(args)

The number of arguments of the method. 0 if no arguments.

py_func class-attribute instance-attribute

py_func: Callable[Param, RetType] | None = py_func

The original Python function. None if no Python function.

self_type property

self_type

Return the type of the self argument of the method.

sym_name class-attribute instance-attribute

sym_name: str | None = sym_name

The name of the method. None if no name.

verify

verify() -> None

verify the method body.

This will raise a ValidationError if the method body is not valid.

Source code in src/kirin/ir/method.py
164
165
166
167
168
169
170
171
172
173
def verify(self) -> None:
    """verify the method body.

    This will raise a ValidationError if the method body is not valid.
    """
    try:
        self.code.verify()
    except ValidationError as e:
        e.attach(self)
        raise e

verify_type

verify_type() -> None

verify the method type.

This will raise a ValidationError if the method type is not valid.

Source code in src/kirin/ir/method.py
175
176
177
178
179
180
181
182
183
184
185
186
187
def verify_type(self) -> None:
    """verify the method type.

    This will raise a ValidationError if the method type is not valid.
    """
    # NOTE: verify the method body
    self.verify()

    try:
        self.code.verify_type()
    except ValidationError as e:
        e.attach(self)
        raise e