Warning
This page is under construction. The content may be incomplete or incorrect. Submit an issue on GitHub if you need help or want to contribute.
Dialects for Python Syntax Sugar
This page contains the dialects designed to represent Python syntax sugar. They provide an implementation of lowering transform from the corresponding Python AST to the dialects' statements. All the statements are typed Any
thus one can always use a custom rewrite pass after type inference to support the desired syntax sugar.
Reference
Indexing
kirin.dialects.py.indexing
The indexing dialect for Python.
This module contains the dialect for the Python indexing syntax, including:
- The
GetItem
statement class. - A base class
Subscript
for indexing statements. - A trait
GetItemLike
for indexing statements. - The lowering pass for the indexing statement.
- The concrete implementation of the indexing statement.
- The constant propagation implementation (special case) of the indexing statement.
- The type inference implementation of the indexing statement.
- A canonical rewrite rule for the rewriting of a given getitem-like statement to another getitem-like statement.
GetItemLikeStmt module-attribute
GetItemLikeStmt = TypeVar(
"GetItemLikeStmt", bound=Statement
)
PyGetItemLikeStmt module-attribute
PyGetItemLikeStmt = TypeVar(
"PyGetItemLikeStmt", bound="GetItem"
)
dialect module-attribute
dialect = Dialect('py.indexing')
Concrete
Bases: MethodTable
getindex
getindex(interp, frame: interp.Frame, stmt: GetItem)
Source code in src/kirin/dialects/py/indexing.py
99 100 101 102 103 104 105 106 107 108 109 110 111 |
|
ConstProp
Bases: MethodTable
getitem
getitem(
_: const.Propagate, frame: const.Frame, stmt: GetItem
) -> interp.StatementResult[const.Result]
Source code in src/kirin/dialects/py/indexing.py
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 |
|
GetItem kirin-statement
GetItem(obj: ir.SSAValue, index: ir.SSAValue)
Bases: Subscript
index kirin-argument
index: SSAValue = argument(print=False)
name class-attribute
instance-attribute
name = 'getitem'
obj kirin-argument
obj: SSAValue = argument(print=False)
result kirin-result
result: ResultValue = result(Any)
traits class-attribute
instance-attribute
traits = frozenset(
{Pure(), PyGetItemLike(), FromPythonCall()}
)
GetItemLike dataclass
GetItemLike()
Bases: Trait[Statement]
, Generic[GetItemLikeStmt]
get_index abstractmethod
get_index(stmt: GetItemLikeStmt) -> ir.SSAValue
Source code in src/kirin/dialects/py/indexing.py
38 39 |
|
get_object abstractmethod
get_object(stmt: GetItemLikeStmt) -> ir.SSAValue
Source code in src/kirin/dialects/py/indexing.py
35 36 |
|
new abstractmethod
new(
stmt_type: type[GetItemLikeStmt],
obj: ir.SSAValue,
index: ir.SSAValue,
) -> GetItemLikeStmt
Source code in src/kirin/dialects/py/indexing.py
41 42 43 44 |
|
Lowering dataclass
Lowering()
Bases: FromPythonAST
lower_Subscript
lower_Subscript(
state: lowering.State, node: ast.Subscript
) -> lowering.Result
Source code in src/kirin/dialects/py/indexing.py
84 85 86 87 88 89 90 91 92 93 |
|
PyGetItemLike dataclass
PyGetItemLike()
Bases: GetItemLike[PyGetItemLikeStmt]
get_index
get_index(stmt: PyGetItemLikeStmt) -> ir.SSAValue
Source code in src/kirin/dialects/py/indexing.py
56 57 |
|
get_object
get_object(stmt: PyGetItemLikeStmt) -> ir.SSAValue
Source code in src/kirin/dialects/py/indexing.py
53 54 |
|
new
new(
stmt_type: type[PyGetItemLikeStmt],
obj: ir.SSAValue,
index: ir.SSAValue,
) -> PyGetItemLikeStmt
Source code in src/kirin/dialects/py/indexing.py
59 60 61 62 |
|
RewriteGetItem dataclass
RewriteGetItem(
stmt_type: type[GetItemLikeStmt],
obj_type: types.TypeAttribute,
)
Bases: RewriteRule
, Generic[GetItemLikeStmt]
Source code in src/kirin/dialects/py/indexing.py
250 251 252 253 254 255 256 257 |
|
getitem_like instance-attribute
getitem_like: GetItemLike[GetItemLikeStmt] = trait
obj_type instance-attribute
obj_type: TypeAttribute = obj_type
target_stmt_type instance-attribute
target_stmt_type: type[GetItemLikeStmt] = stmt_type
rewrite_Statement
rewrite_Statement(node: ir.Statement) -> RewriteResult
Source code in src/kirin/dialects/py/indexing.py
259 260 261 262 263 264 265 266 267 268 269 |
|
TypeInfer
Bases: MethodTable
getitem
getitem(
interp: TypeInference,
frame: interp.Frame[types.TypeAttribute],
stmt: GetItem,
)
Source code in src/kirin/dialects/py/indexing.py
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
|
getitem_tuple
getitem_tuple(
interp,
stmt: GetItem,
obj: types.TypeAttribute,
index: types.TypeAttribute,
)
Source code in src/kirin/dialects/py/indexing.py
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
|
getitem_tuple_index
getitem_tuple_index(
interp: TypeInference,
stmt: GetItem,
obj: types.Generic,
index: types.TypeAttribute,
)
Source code in src/kirin/dialects/py/indexing.py
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
|
getitem_tuple_slice
getitem_tuple_slice(
interp: TypeInference,
stmt: GetItem,
obj: types.Generic,
index: types.TypeAttribute,
)
Source code in src/kirin/dialects/py/indexing.py
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 |
|
getitem_tuple_union
getitem_tuple_union(obj: types.Generic)
Source code in src/kirin/dialects/py/indexing.py
198 199 200 201 202 |
|
Attribute
kirin.dialects.py.attr
Attribute access dialect for Python.
This module contains the dialect for the Python attribute access statement, including:
- The
GetAttr
statement class. - The lowering pass for the attribute access statement.
- The concrete implementation of the attribute access statement.
This dialect maps ast.Attribute
nodes to the GetAttr
statement.
dialect module-attribute
dialect = Dialect('py.attr')
Concrete
Bases: MethodTable
getattr
getattr(
interp: interp.Interpreter,
frame: interp.Frame,
stmt: GetAttr,
)
Source code in src/kirin/dialects/py/attr.py
32 33 34 |
|
GetAttr kirin-statement
GetAttr(obj: ir.SSAValue, *, attrname: str)
Bases: Statement
attrname kirin-attribute
kw-only
attrname: str = attribute()
name class-attribute
instance-attribute
name = 'getattr'
obj kirin-argument
obj: SSAValue = argument(print=False)
result kirin-result
result: ResultValue = result()
traits class-attribute
instance-attribute
traits = frozenset({FromPythonCall()})
Lowering dataclass
Lowering()
Bases: FromPythonAST
lower_Attribute
lower_Attribute(
state: lowering.State, node: ast.Attribute
) -> lowering.Result
Source code in src/kirin/dialects/py/attr.py
40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
|