loki.ir.nodes.abstract_nodes

Abstract base classes for node definitions in the Loki IR.

Classes

InternalNode([body, source, label])

Internal representation of a control flow node that has a traversable body property.

LeafNode([source, label])

Internal representation of a control flow node without a body.

Node([source, label])

Base class for all node types in Loki's internal representation.

ScopedNode([parent])

Mix-in to attache a scope to an IR Node

class Node(source: Source | str | None = None, label: str | None = None)

Bases: object

Base class for all node types in Loki’s internal representation.

Provides the common functionality shared by all node types; specifically, this comprises functionality to update or rebuild a node, and source metadata.

traversable

The traversable fields of the Node; that is, fields walked over by a Visitor. All arguments in __init__() whose name appear in this list are treated as traversable fields.

Type:

list of str

Parameters:
  • source (Source, optional) – the information about the original source for the Node.

  • label (str, optional) – the label assigned to the statement in the original source corresponding to the Node.

source: Source | str | None = None
label: str | None = None
property children

The traversable children of the node.

clone(*args, **kwargs)

Rebuild the node.

Constructs an identical copy of the node from when it was first created. Optionally, some or all of the arguments for it can be overwritten.

Parameters:
  • *args (optional) – The traversable arguments used to create the node. By default, args are used.

  • **kwargs (optional) – The non-traversable arguments used to create the node, By default, args_frozen are used.

property args

Arguments used to construct the Node.

property args_frozen

Arguments used to construct the Node that cannot be traversed.

view()

Pretty-print the node hierachy under this node.

ir_graph(show_comments=False, show_expressions=False, linewidth=40, symgen=<class 'str'>)

Get the IR graph to visualize the node hierachy under this node.

property live_symbols

Yield the list of live symbols at this node, i.e., variables that have been defined (potentially) prior to this point in the control flow graph.

This property is attached to the Node by loki.analyse.analyse_dataflow.attach_dataflow_analysis() or when using the loki.analyse.analyse_dataflow.dataflow_analysis_attached() context manager.

property defines_symbols

Yield the list of symbols (potentially) defined by this node.

This property is attached to the Node by loki.analyse.analyse_dataflow.attach_dataflow_analysis() or when using the loki.analyse.analyse_dataflow.dataflow_analysis_attached() context manager.

property uses_symbols

Yield the list of symbols used by this node before defining it.

This property is attached to the Node by loki.analyse.analyse_dataflow.attach_dataflow_analysis() or when using the loki.analyse.analyse_dataflow.dataflow_analysis_attached() context manager.

class InternalNode(body: Tuple[Node | Scope, ...] = (), source: Source | str | None = None, label: str | None = None)

Bases: Node, _InternalNode

Internal representation of a control flow node that has a traversable body property.

Parameters:

body (tuple) – The nodes that make up the body.

classmethod ensure_tuple(value)
class LeafNode(source: Source | str | None = None, label: str | None = None)

Bases: Node

Internal representation of a control flow node without a body.

class ScopedNode(parent: dataclasses.InitVar[object] | None = None)

Bases: Scope

Mix-in to attache a scope to an IR Node

Additionally, this specializes the node’s _update() and _rebuild() methods to make sure that an existing symbol table is carried over correctly.

property args

Arguments used to construct the ScopedNode, excluding the symbol table.

abstract property variables

Return the variables defined in this ScopedNode.

property variable_map

Map of variable names to Variable objects

get_symbol(name)

Returns the symbol for a given name as defined in its declaration.

The returned symbol might include dimension symbols if it was declared as an array.

Parameters:

name (str) – Base name of the symbol to be retrieved

Variable(**kwargs)

Factory method for TypedSymbol or MetaSymbol classes.

This invokes the Variable with this node as the scope.

Parameters:
  • name (str) – The name of the variable.

  • type (optional) – The type of that symbol. Defaults to BasicType.DEFERRED.

  • parent (Scalar or Array, optional) – The derived type variable this variable belongs to.

  • dimensions (ArraySubscript, optional) – The array subscript expression.

parse_expr(expr_str, strict=False, evaluate=False, context=None)

Uses parse_expr() to convert expression(s) represented in a string to Loki expression(s)/IR.

Parameters:
  • expr_str (str) – The expression as a string

  • strict (bool, optional) – Whether to raise exception for unknown variables/symbols when evaluating an expression (default: False)

  • evaluate (bool, optional) – Whether to evaluate the expression or not (default: False)

  • context (dict, optional) – Symbol context, defining variables/symbols/procedures to help/support evaluating an expression

Returns:

The expression tree corresponding to the expression

Return type:

Expression