loki.ir.visitor

Visitor base classes for traversing the IR

Classes

GenericVisitor()

A generic visitor class to traverse the IR tree.

Visitor()

The basic visitor-class for traversing Loki's control flow tree.

class GenericVisitor

Bases: object

A generic visitor class to traverse the IR tree.

To define handlers, subclasses should define visit_Foo methods for each class Foo they want to handle. If a specific method for a class Foo is not found, the MRO of the class is walked in order until a matching method is found. The method signature is:

def visit_Foo(self, o, [*args, **kwargs]):
    pass

The handler is responsible for visiting the children (if any) of the node o. *args and **kwargs may be used to pass information up and down the call stack. You can also pass named keyword arguments, e.g.:

def visit_Foo(self, o, parent=None, *args, **kwargs):
    pass
default_args = {}

Dict of default keyword arguments for the visitor. These are not used by default in visit(), however, a caller may pass them explicitly to visit() by accessing default_args. For example:

v = FooVisitor()
v.visit(node, **v.default_args)
classmethod default_retval()

Default return value for handler methods.

This method returns an object to use to populate return values. If your visitor combines values in a tree-walk, it may be useful to provide an object to combine the results into. default_retval() may be defined by the visitor to be called to provide an empty object of appropriate type.

Return type:

None

lookup_method(instance)

Look up a handler method for a visitee.

Parameters:

instance – The instance to look up a method for.

visit(o, *args, **kwargs)

Apply this Visitor to an IR tree.

Parameters:
  • o (Node) – The node to visit.

  • *args – Optional arguments to pass to the visit methods.

  • **kwargs – Optional keyword arguments to pass to the visit methods.

visit_object(o, **kwargs)

Fallback method for objects that do not match any handler.

Parameters:
  • o – The object to visit.

  • **kwargs – Optional keyword arguments passed to the visit methods.

Returns:

The default return value.

Return type:

GenericVisitor.default_retval()

class Visitor

Bases: GenericVisitor

The basic visitor-class for traversing Loki’s control flow tree.

It enhances the generic visitor class GenericVisitor with the ability to recurse for all children of a Node.

visit_tuple(o, **kwargs)

Visit all elements in a tuple and return the results as a tuple.

visit_list(o, **kwargs)

Visit all elements in a tuple and return the results as a tuple.

visit_Node(o, **kwargs)

Visit all children of a Node.

static reuse(o, *args, **kwargs)

A visit method to reuse a node, ignoring children.

maybe_rebuild(o, *args, **kwargs)

A visit method that rebuilds nodes if their children have changed.

always_rebuild(o, *args, **kwargs)

A visit method that always rebuilds nodes.