Traits
FromPythonCall dataclass
FromPythonCall()
Bases: PythonLoweringTrait[StatementType, Call]
Trait for customizing lowering of Python calls to a statement.
Declared in a statement definition to indicate that the statement can be constructed from a Python call (i.e., a function call ast.Call
in the Python AST).
Subclassing this trait allows for customizing the lowering of Python calls to the statement. The lower
method should be implemented to parse the arguments from the Python call and construct the statement instance.
FromPythonRangeLike dataclass
FromPythonRangeLike()
Bases: FromPythonCall[StatementType]
Provides a default lowering implementation for built-in range
-like function to a statement that takes three arguments: start, stop, and step.
FromPythonWith dataclass
FromPythonWith()
Bases: PythonLoweringTrait[StatementType, With]
Trait for customizing lowering of Python with statements to a statement.
Subclassing this trait allows for customizing the lowering of Python with statements to the statement. The lower
method should be implemented to parse the arguments from the Python with statement and construct the statement instance.
FromPythonWithSingleItem dataclass
FromPythonWithSingleItem()
Bases: FromPythonWith[StatementType]
Trait for customizing lowering of the following Python with syntax to a statement:
with <stmt>[ as <name>]:
<body>
where <stmt>
is the statement being lowered, <name>
is an optional name for the result of the statement, and <body>
is the body of the with statement. The optional as <name>
is not valid when the statement has no results.
This syntax is slightly different from the standard Python with
statement in that <name>
refers to the result of the statement, not the context manager. Thus typically one sould access <name>
in <body>
to use the result of the statement.
In some cases, however, <name>
may be used as a reference of a special value self
that is passed to the <body>
of the statement. This is useful for statements that have a similar behavior to a closure.
PythonLoweringTrait dataclass
PythonLoweringTrait()
Bases: Trait[Statement]
, Generic[StmtType, ASTNode]
A trait that indicates that a statement can be lowered from Python AST.
lower_Call_inputs classmethod
lower_Call_inputs(
stmt: type[StmtType],
state: State[ast.AST],
node: ast.Call,
) -> tuple[
dict[str, ir.SSAValue | tuple[ir.SSAValue, ...]],
dict[str, Any],
]
Lower the inputs of a Python call to corresponding SSA values or compile-time values (attributes).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
stmt | type[StmtType] | The statement class to lower to. | required |
state | State[AST] | The lowering state. | required |
node | Call | The Python call node to lower. | required |
Returns:
Type | Description |
---|---|
dict[str, SSAValue | tuple[SSAValue, ...]] | A tuple containing two dictionaries: |
dict[str, Any] |
|
tuple[dict[str, SSAValue | tuple[SSAValue, ...]], dict[str, Any]] |
|
Raises:
Type | Description |
---|---|
BuildError | If the Python call cannot be lowered to the statement. |
Source code in src/kirin/lowering/python/traits.py
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 |
|