pub trait ProgramVisitor {
    type Error;
Show 20 methods fn visit_program(&mut self, program: &Program) -> Result<(), Self::Error> { ... }
fn walk_program(&mut self, program: &Program) -> Result<(), Self::Error> { ... }
fn visit_decl(&mut self, decl: &Span<Decl>) -> Result<(), Self::Error> { ... }
fn walk_decl(&mut self, decl: &Span<Decl>) -> Result<(), Self::Error> { ... }
fn visit_include(&mut self, file: &Span<Symbol>) -> Result<(), Self::Error> { ... }
fn visit_qreg(&mut self, reg: &Span<Reg>) -> Result<(), Self::Error> { ... }
fn visit_creg(&mut self, reg: &Span<Reg>) -> Result<(), Self::Error> { ... }
fn visit_opaque_def(
        &mut self,
        name: &Span<Symbol>,
        params: &[Span<Symbol>],
        args: &[Span<Symbol>]
    ) -> Result<(), Self::Error> { ... }
fn visit_gate_def(
        &mut self,
        name: &Span<Symbol>,
        params: &[Span<Symbol>],
        args: &[Span<Symbol>],
        body: &[Span<Stmt>]
    ) -> Result<(), Self::Error> { ... }
fn walk_gate_def(
        &mut self,
        name: &Symbol,
        params: &[Span<Symbol>],
        args: &[Span<Symbol>],
        body: &[Span<Stmt>]
    ) -> Result<(), Self::Error> { ... }
fn visit_stmt(&mut self, stmt: &Span<Stmt>) -> Result<(), Self::Error> { ... }
fn walk_stmt(&mut self, stmt: &Span<Stmt>) -> Result<(), Self::Error> { ... }
fn visit_barrier(&mut self, regs: &[Span<Reg>]) -> Result<(), Self::Error> { ... }
fn visit_measure(
        &mut self,
        from: &Span<Reg>,
        to: &Span<Reg>
    ) -> Result<(), Self::Error> { ... }
fn visit_reset(&mut self, reg: &Span<Reg>) -> Result<(), Self::Error> { ... }
fn visit_cx(
        &mut self,
        copy: &Span<Reg>,
        xor: &Span<Reg>
    ) -> Result<(), Self::Error> { ... }
fn visit_u(
        &mut self,
        theta: &Span<Expr>,
        phi: &Span<Expr>,
        lambda: &Span<Expr>,
        reg: &Span<Reg>
    ) -> Result<(), Self::Error> { ... }
fn visit_gate(
        &mut self,
        name: &Span<Symbol>,
        params: &[Span<Expr>],
        args: &[Span<Reg>]
    ) -> Result<(), Self::Error> { ... }
fn visit_conditional(
        &mut self,
        reg: &Span<Reg>,
        val: &Span<usize>,
        then: &Span<Stmt>
    ) -> Result<(), Self::Error> { ... }
fn walk_conditional(
        &mut self,
        reg: &Span<Reg>,
        val: &Span<usize>,
        then: &Span<Stmt>
    ) -> Result<(), Self::Error> { ... }
}
Expand description

Low-level translation interface for definitions and declarations.

This trait allows you to walk over the structure of Program element by element. Each visit_ method defines the callback for what to do when a particular element is encountered.

Any unimplemented visit_ methods have a default which traverses all substructures in the element. Since this traversal does not happen automatically, if you override a visit_ method, you must either traverse the element manually, or call the corresponding walk_ method which does the default traversal.

Also note that the declarations are garuanteed to be visited in the following order by type: Include, Def, QReg, CReg, Stmt. The error type is provided to allow you to fail out at any time by returning an Err variant.

For example, to get the names of all gate definitions in a program, you might do the following:

struct DefFinder;

impl ProgramVisitor for DefFinder {
    fn visit_gate_def(
        &mut self,
        name: &Span<Symbol>,
        params: &[Span<Symbol>],
        args: &[Span<Symbol>],
        body: &[Span<Stmt>]
    ) {
        println!("Found definition of `{}`.", name.inner);
        self.walk_gate_def(name, params, args, body);
    }
}

fn main() {
    let program = ...; // aquire a program from somewhere
    DefFinder.visit_program(&program);
}

Associated Types

Provided methods

Implementors