Renderer registry¶
The per-backend, per-node emit registry.
renderer_registry
¶
Per-(backend, IR-node) emit registry.
Third-party backends and third-party IR nodes plug into the
transpile pipeline through this registry without editing the
in-tree renderers. Every backend registers an emit function per
IRNode variant it supports; the
BackendRenderer
base class dispatches through the registry at render time.
Two entry points:
emit_hook(backend_name, node_type)is a decorator that binds an emit function at import time.BackendRenderer.emit_node(ctx, node)looks up the registered function for the concreteIRNodesubclass and calls it, raising a targeted diagnostic when no handler is registered.
An IRArg subclass plugs in the
same way: register the argument class's emit through emit_hook
under the backend name and it becomes visible to every renderer
that walks the argument tree through the registry.
The registry composes with
RendererBase:
backends can subclass RendererBase and hard-code their emit
tables, or drive dispatch through this registry. Both paths are
supported.
EmitFn
module-attribute
¶
An emit function: called with the renderer, its context, and one IR node or argument, returning the backend's fragment for it.
RendererLookupError
¶
Bases: LookupError
No emit function is registered for the requested
(backend_name, node_or_arg_type) combination.
RendererDuplicateError
¶
Bases: ValueError
A second emit function is being registered for a combination
that already has one, without replace=True. Silently shadowing
a registered emit is a subtle bug source; the registry rejects
the second registration unless the caller opts in.
BackendRenderer
¶
Bases: ABC
Abstract renderer that dispatches emit through the registry.
Concrete renderers set the backend_name class attribute and
call :meth:emit_node to route each
IRNode through the registered
emit function. Any IRArg a
renderer needs to walk is dispatched through :meth:emit_arg.
Existing renderers under quivers.transpile.renderers
subclass RendererBase instead
and hard-code their emit tables; both paths coexist. Third-
party renderers with a small backend surface pick this base
for the extensibility; large in-tree backends stay on
RendererBase until a migration is warranted.
emit_node
¶
emit_node(ctx, node: IRNode)
Dispatch a top-level IR node through the registry.
| PARAMETER | DESCRIPTION |
|---|---|
ctx
|
The renderer's emission context, passed through to the emit.
|
node
|
The IR node to emit.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
object
|
Whatever the registered emit returns. |
| RAISES | DESCRIPTION |
|---|---|
RendererLookupError
|
If no emit is registered for the node's class. |
Source code in src/quivers/transpile/renderer_registry.py
emit_arg
¶
emit_arg(ctx, arg: IRArg)
Dispatch an IR argument through the registry.
| PARAMETER | DESCRIPTION |
|---|---|
ctx
|
The renderer's emission context, passed through to the emit.
|
arg
|
The IR argument to emit.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
object
|
Whatever the registered emit returns. |
| RAISES | DESCRIPTION |
|---|---|
RendererLookupError
|
If no emit is registered for the argument's class. |
Source code in src/quivers/transpile/renderer_registry.py
emit_hook
¶
emit_hook(backend_name: str, node_type: type, *, replace: bool = False) -> Callable[[EmitFn], EmitFn]
Decorator that registers an emit function for a
(backend_name, node_type) combination.
node_type is an IRNode or
IRArg subclass. Registration
happens at import time of the module that declares the emit;
downstream libraries add support for a new backend or a new
node type by importing their module once.
replace=True allows re-registering over an existing entry.
Left unset, a second registration for the same key raises
:class:RendererDuplicateError so a user does not silently
shadow a built-in emit.
| PARAMETER | DESCRIPTION |
|---|---|
backend_name
|
The backend the emit renders for.
TYPE:
|
node_type
|
The IR node or argument class the emit handles.
TYPE:
|
replace
|
Whether to overwrite an existing registration.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Callable[[EmitFn], EmitFn]
|
A decorator that registers its argument and returns it unchanged. |
| RAISES | DESCRIPTION |
|---|---|
RendererDuplicateError
|
From the decorator, if the combination is registered and
|
Source code in src/quivers/transpile/renderer_registry.py
get_emit
¶
get_emit(backend_name: str, node_type: type) -> EmitFn
Look up the registered emit for a (backend_name, node_type)
combination. Raises :class:RendererLookupError with the list
of registered types for the backend when no emit is present.
| PARAMETER | DESCRIPTION |
|---|---|
backend_name
|
The backend to look in.
TYPE:
|
node_type
|
The IR node or argument class to look up.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
EmitFn
|
The registered emit function. |
| RAISES | DESCRIPTION |
|---|---|
RendererLookupError
|
If the backend has no registrations, or none for |
Source code in src/quivers/transpile/renderer_registry.py
registered_backends
¶
List every backend that has at least one registered emit.
| RETURNS | DESCRIPTION |
|---|---|
list[str]
|
The backend names, sorted. |
registered_node_types
¶
List the class names of every node type registered for
backend_name.
| PARAMETER | DESCRIPTION |
|---|---|
backend_name
|
The backend to list.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list[str]
|
The registered class names, sorted; empty for an unknown backend. |
Source code in src/quivers/transpile/renderer_registry.py
unregister
¶
Remove a registration. Used by tests to reset state; not typically called in production code.
| PARAMETER | DESCRIPTION |
|---|---|
backend_name
|
The backend the registration belongs to.
TYPE:
|
node_type
|
The IR node or argument class to remove.
TYPE:
|