# loki.ir.nodes.internal_nodes module

Intermediate node classes for nested node definitions in the Loki IR.

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

  Bases: `InternalNode`, `_SectionBase`

  Internal representation of a single code region.

  index(item, start=0, stop=None)
  append(node)

    Append the given node(s) to the section’s body.

    Parameters:

      node (`Node` or tuple of `Node`) – The node(s) to append to the section.

  insert(pos, node)

    Insert the given node(s) into the section’s body at a specific position.

    Parameters:

      - pos (int) – The position at which the node(s) should be inserted. Any existing nodes at this or after this position are shifted back.

      - node (`Node` or tuple of `Node`) – The node(s) to append to the section.

  prepend(node)

    Insert the given node(s) at the beginning of the section’s body.

    Parameters:

      node (`Node` or tuple of `Node`) – The node(s) to insert into the section.

class Associate(associations: Tuple[Tuple[Expression, Expression], ...], body: Tuple[Node | Scope, ...] = (), source: Source | str | None = None, label: str | None = None, parent: dataclasses.InitVar[object] = None)

  Bases: `ScopedNode`, `Section`, `_AssociateBase`

  Internal representation of a code region in which names are associated with expressions or variables.

  Parameters:

    - body (tuple) – The associate’s body.

    - associations (dict or collections.OrderedDict) – The mapping of names to expressions or variables valid inside the associate’s body.

    - parent (`Scope`, optional) – The parent scope in which the associate appears

    - symbol_attrs (`SymbolTable`, optional) – An existing symbol table to use

    - **kwargs (optional) – Other parameters that are passed on to the parent class constructor.

  property association_map

    An `collections.OrderedDict` of associated expressions.

  property inverse_map

    An `collections.OrderedDict` of associated expressions.

  property variables

    Return the variables defined in this `ScopedNode`.

class Loop(variable: Expression, bounds: Expression, body: Tuple[Node | Scope, ...] = (), pragma: Tuple[Node, ...] | None = None, pragma_post: Tuple[Node, ...] | None = None, loop_label: Any | None = None, name: str | None = None, has_end_do: bool | None = True, source: Source | str | None = None, label: str | None = None)

  Bases: `InternalNode`, `_LoopBase`

  Internal representation of a loop with induction variable and range.

  Parameters:

    - variable (`Scalar`) – The induction variable of the loop.

    - bounds (`LoopRange`) – The range of the loop, defining the iteration space.

    - body (tuple) – The loop body.

    - pragma (tuple of `Pragma`, optional) – Pragma(s) that appear in front of the loop. By default `Pragma` nodes appear as standalone nodes in the IR before the `Loop` node. Only a bespoke context created by `pragmas_attached()` attaches them for convenience.

    - pragma_post (tuple of `Pragma`, optional) – Pragma(s) that appear after the loop. The same applies as for pragma.

    - loop_label (str, optional) – The Fortran label for that loop. Importantly, this is an intrinsic Fortran feature and different from the statement label that can be attached to other nodes.

    - name (str, optional) – The Fortran construct name for that loop.

    - has_end_do (bool, optional) – In Fortran, loop blocks can be closed off by a `CONTINUE` statement (which we retain as an `Intrinsic` node) and therefore `END DO` can be omitted. For string reproducibility this parameter can be set False to indicate that this loop did not have an `END DO` statement in the original source.

    - **kwargs (optional) – Other parameters that are passed on to the parent class constructor.

class WhileLoop(condition: Expression | None, body: Tuple[Node | Scope, ...] = (), pragma: Node | None = None, pragma_post: Node | None = None, loop_label: Any | None = None, name: str | None = None, has_end_do: bool | None = True, source: Source | str | None = None, label: str | None = None)

  Bases: `InternalNode`, `_WhileLoopBase`

  Internal representation of a while loop in source code.

  Importantly, this is different from a `DO` (Fortran) or `for` (C) loop, as we do not have a specified induction variable with explicit iteration range.

  Parameters:

    - condition (`pymbolic.primitives.Expression`) – The condition evaluated before executing the loop body.

    - body (tuple) – The loop body.

    - pragma (tuple of `Pragma`, optional) – Pragma(s) that appear in front of the loop. By default `Pragma` nodes appear as standalone nodes in the IR before the `Loop` node. Only a bespoke context created by `pragmas_attached()` attaches them for convenience.

    - pragma_post (tuple of `Pragma`, optional) – Pragma(s) that appear after the loop. The same applies as for pragma.

    - loop_label (str, optional) – The Fortran label for that loop. Importantly, this is an intrinsic Fortran feature and different from the statement label that can be attached to other nodes.

    - name (str, optional) – The Fortran construct name for that loop.

    - has_end_do (bool, optional) – In Fortran, loop blocks can be closed off by a `CONTINUE` statement (which we retain as an `Intrinsic` node) and therefore `END DO` can be omitted. For string reproducibility this parameter can be set False to indicate that this loop did not have an `END DO` statement in the original source.

    - **kwargs (optional) – Other parameters that are passed on to the parent class constructor.

class Conditional(condition: Expression, body: Tuple[Node | Scope, ...] = (), else_body: Tuple[Node, ...] | None = (), inline: bool = False, has_elseif: bool = False, name: str | None = None, source: Source | str | None = None, label: str | None = None)

  Bases: `InternalNode`, `_ConditionalBase`

  Internal representation of a conditional branching construct.

  Parameters:

    - condition (`pymbolic.primitives.Expression`) – The condition evaluated before executing the body.

    - body (tuple) – The conditional’s body.

    - else_body (tuple) – The body of the else branch. Can be empty.

    - inline (bool, optional) – Flag that marks this conditional as inline, i.e., it s body consists only of a single statement that appeared immediately after the `IF` statement and it does not have an `else_body`.

    - has_elseif (bool, optional) – Flag that indicates that this conditional has an `ELSE IF` branch in the original source. In Loki’s IR these are represented as a chain of `Conditional` but for string reproducibility this flag can be provided to enable backends to reproduce the original appearance.

    - name (str, optional) – The Fortran construct name for that conditional.

    - **kwargs (optional) – Other parameters that are passed on to the parent class constructor.

  classmethod ensure_tuple(value)
  property else_bodies

    Return all nested node tuples in the `ELSEIF`/`ELSE` part of the conditional chain.

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

  Bases: `InternalNode`, `_PragmaRegionBase`

  Internal representation of a block of code defined by two matching pragmas.

  Generally, the pair of pragmas are assumed to be of the form `!$<keyword> <marker>` and `!$<keyword> end <marker>`.

  This node type is injected into the IR within a context created by `pragma_regions_attached()`.

  Parameters:

    - body (tuple) – The statements appearing between opening and closing pragma.

    - pragma (`Pragma`) – The opening pragma declaring that region.

    - pragma_post (`Pragma`) – The closing pragma for that region.

    - **kwargs (optional) – Other parameters that are passed on to the parent class constructor.

  append(node)
  insert(pos, node)

    Insert at given position

  prepend(node)

class Interface(body: Tuple[Node | Scope, ...] = (), abstract: bool = False, spec: Expression | str | None = None, source: Source | str | None = None, label: str | None = None)

  Bases: `InternalNode`, `_InterfaceBase`

  Internal representation of a Fortran interface block.

  Parameters:

    - body (tuple) – The body of the interface block, containing function and subroutine specifications or procedure statements

    - abstract (bool, optional) – Flag to indicate that this is an abstract interface

    - spec (str, optional) – A generic name, operator, assignment, or I/O specification

    - **kwargs (optional) – Other parameters that are passed on to the parent class constructor.

  property symbols

    The list of symbol names declared by this interface

  property symbol_map

    Map symbol name to symbol declared by this interface