loki.ir.find
Visitor classes that allow searching the IR
Functions
|
Utility function to test relationship between nodes. |
|
Utility function to test relationship between nodes. |
Classes
|
Find |
|
Find all parent nodes for node |
|
Find a pattern of nodes given as tuple/list of types within a given tree. |
|
Find repeated nodes of the same type in lists/tuples within a given tree. |
- class FindNodes(match, mode='type', greedy=False)
Bases:
Visitor
Find
Node
instances that match a given criterion.- Parameters:
match – Node type(s) or node instance to look for.
mode (optional) –
Drive the search. Accepted values are:
'type'
(default) : Collect all instances of typematch
.'scope'
: Return theInternalNode
in which the objectmatch
appears.
greedy (bool, optional) – Do not recurse for children of a matched node.
- Returns:
All nodes in the traversed IR that match the criteria.
- Return type:
- rules = {'scope': <function FindNodes.<lambda>>, 'type': <function FindNodes.<lambda>>}
Mapping of available
mode
selectors to match rules.
- 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()
- visit_tuple(o, **kwargs)
Visit all elements in the iterable and return the combined result.
- visit_list(o, **kwargs)
Visit all elements in the iterable and return the combined result.
- visit_Node(o, **kwargs)
Add the node to the returned list if it matches the criteria and visit all children.
- visit_TypeDef(o, **kwargs)
Custom handler for
TypeDef
nodes that does not traverse the body (reason being that discovering nodes such as declarations from inside the type definition would be unexpected if called on a containingSubroutine
orModule
)
- class SequenceFinder(node_type)
Bases:
Visitor
Find repeated nodes of the same type in lists/tuples within a given tree.
- Parameters:
node_type – The node type to look for.
- visit_tuple(o, **kwargs)
Visit all children and look for sequences of matching type.
- visit_list(o, **kwargs)
Visit all children and look for sequences of matching type.
- class PatternFinder(pattern)
Bases:
Visitor
Find a pattern of nodes given as tuple/list of types within a given tree.
- Parameters:
pattern (iterable of types) – The type pattern to look for.
- static match_indices(pattern, sequence)
Return indices of matched patterns in sequence.
- visit_tuple(o, **kwargs)
Visit all children and look for sequences of nodes with types matching the pattern.
- visit_list(o, **kwargs)
Visit all children and look for sequences of nodes with types matching the pattern.
- is_parent_of(node, other)
Utility function to test relationship between nodes.
Note that this can be expensive for large subtrees.
- Returns:
Return True if
other
is contained in the IR belownode
, otherwise return False.- Return type:
- is_child_of(node, other)
Utility function to test relationship between nodes.
Note that this can be expensive for large subtrees.
- Returns:
Return True if
node
is contained in the IR belowother
, otherwise return False.- Return type: