aboutsummaryrefslogtreecommitdiff
path: root/mlir/lib/Bindings/Python/IRModule.h
diff options
context:
space:
mode:
Diffstat (limited to 'mlir/lib/Bindings/Python/IRModule.h')
-rw-r--r--mlir/lib/Bindings/Python/IRModule.h30
1 files changed, 30 insertions, 0 deletions
diff --git a/mlir/lib/Bindings/Python/IRModule.h b/mlir/lib/Bindings/Python/IRModule.h
index 013bb7b..225580f 100644
--- a/mlir/lib/Bindings/Python/IRModule.h
+++ b/mlir/lib/Bindings/Python/IRModule.h
@@ -986,6 +986,8 @@ public:
// const char *pyClassName
using ClassTy = pybind11::class_<DerivedTy, BaseTy>;
using IsAFunctionTy = bool (*)(MlirAttribute);
+ using GetTypeIDFunctionTy = MlirTypeID (*)();
+ static constexpr GetTypeIDFunctionTy getTypeIdFunction = nullptr;
PyConcreteAttribute() = default;
PyConcreteAttribute(PyMlirContextRef contextRef, MlirAttribute attr)
@@ -1017,6 +1019,34 @@ public:
pybind11::arg("other"));
cls.def_property_readonly(
"type", [](PyAttribute &attr) { return mlirAttributeGetType(attr); });
+ cls.def_property_readonly_static(
+ "static_typeid", [](py::object & /*class*/) -> MlirTypeID {
+ if (DerivedTy::getTypeIdFunction)
+ return DerivedTy::getTypeIdFunction();
+ throw py::attribute_error(
+ (DerivedTy::pyClassName + llvm::Twine(" has no typeid.")).str());
+ });
+ cls.def_property_readonly("typeid", [](PyAttribute &self) {
+ return py::cast(self).attr("typeid").cast<MlirTypeID>();
+ });
+ cls.def("__repr__", [](DerivedTy &self) {
+ PyPrintAccumulator printAccum;
+ printAccum.parts.append(DerivedTy::pyClassName);
+ printAccum.parts.append("(");
+ mlirAttributePrint(self, printAccum.getCallback(),
+ printAccum.getUserData());
+ printAccum.parts.append(")");
+ return printAccum.join();
+ });
+
+ if (DerivedTy::getTypeIdFunction) {
+ PyGlobals::get().registerTypeCaster(
+ DerivedTy::getTypeIdFunction(),
+ pybind11::cpp_function([](PyAttribute pyAttribute) -> DerivedTy {
+ return pyAttribute;
+ }));
+ }
+
DerivedTy::bindDerived(cls);
}