@@ -59,6 +59,21 @@ def _ensure_cleanup_function_registered():
5959 _CLEANUP_REGISTERED = True
6060
6161
62+ @register_object ("script.printer.RootNodeContainer" )
63+ class RootNodeContainer (Object ):
64+ """
65+ A wrapper object to provide injection point for printer of each IR.
66+
67+ This class shouldn't be used directly. `IRDocsifier.set_root_dispatch`
68+ should be used instead.
69+ """
70+
71+ root_node : Object
72+
73+ def __init__ (self , root_node : Object ):
74+ self .__init_handle_by_constructor__ (_ffi_api .RootNodeContainer , root_node ) # type: ignore # pylint: disable=no-member
75+
76+
6277@register_object ("script.printer.IRDocsifier" )
6378class IRDocsifier (Object ):
6479 """
@@ -91,7 +106,7 @@ def __init__(self, ir_prefix: Dict[str, str]):
91106 def set_dispatch (
92107 cls ,
93108 node_type : Type [_TObject ],
94- dispatch_function : Callable [[_TObject , "IRDocsifier" ], Doc ],
109+ dispatch_function : Callable [[_TObject , ObjectPath , "IRDocsifier" ], Doc ],
95110 dispatch_token : str = "" ,
96111 ) -> None :
97112 """
@@ -101,7 +116,7 @@ def set_dispatch(
101116 ----------
102117 node_type : Type[_TObject]
103118 The type of object to dispatch on.
104- dispatch_function : Callable[[_TObject, "IRDocsifier"], Doc]
119+ dispatch_function : Callable[[_TObject, ObjectPath, "IRDocsifier"], Doc]
105120 The dispatch function. It's called to transform IR node object to Doc.
106121 dispatch_token : str
107122 Function will only be called when this dispatch_token is the same as the one
@@ -119,6 +134,38 @@ def set_dispatch(
119134 )
120135 _REGISTERED_TYPES .add ((dispatch_token , type_index ))
121136
137+ @classmethod
138+ def set_root_dispatch (
139+ cls , dispatch_token : str , root_dispatch_function : Callable [[Object , "IRDocsifier" ], Doc ]
140+ ) -> None :
141+ """
142+ Set the root dispatch function for an IR.
143+
144+ The root dispatch function will be called with the root node of an IR graph
145+ that's being transformed to Doc. This provides an injection point for
146+ each IR's printer implemention to add specialized logic, for example,
147+ pushing a special Frame to the IRDocsifier before doing actual IR->Doc
148+ transformation.
149+
150+ The simplest root dispatch function is
151+ ```
152+ def f(obj, ir_docsifier)
153+ return ir_docsifier.as_doc(obj, ObjectPath.root())
154+ ```
155+
156+ Parameters
157+ ----------
158+ root_dispatch_function : Callable[[_TObject, "IRDocsifier"], Doc]
159+ The root dispatch function. It's called with the root node to be printed.
160+ dispatch_token : str
161+ The dispatch token of the IR that root_dispatch_funnction applies to.
162+ """
163+
164+ def dispatch_function (obj : RootNodeContainer , _ , ir_docsifier ):
165+ return root_dispatch_function (obj .root_node , ir_docsifier )
166+
167+ cls .set_dispatch (RootNodeContainer , dispatch_function , dispatch_token )
168+
122169 def as_doc (self , obj : Object , object_path : ObjectPath ) -> Doc :
123170 """
124171 Transform the input object into Doc.
0 commit comments