psyclone.psyir.nodes.operation#

This module contains the implementation of the Operation class and its sub-classes.

Classes#

  • Operation: Abstract base class for PSyIR nodes representing operators.

  • UnaryOperation: Node representing a UnaryOperation expression. As such it has one operand

  • BinaryOperation: Node representing a BinaryOperation expression. As such it has two operands

class psyclone.psyir.nodes.operation.Operation(operator, parent=None)[source]#

Abstract base class for PSyIR nodes representing operators.

Parameters:
  • operator (Union[ psyclone.psyir.nodes.UnaryOperation.Operator, psyclone.psyir.nodes.BinaryOperation.Operator]) – the operator used in the operation.

  • parent (Optional[psyclone.psyir.nodes.Node]) – the parent node of this Operation in the PSyIR.

Raises:

TypeError – if the supplied operator is not an instance of self.Operator.

Inheritance

Inheritance diagram of Operation
Operator#

Must be overridden in sub-class to hold an Enumeration of the Operators that it can represent.

alias of object

node_str(colour=True)[source]#

Construct a text representation of this node, optionally with control codes for coloured display in a suitable terminal.

Parameters:

colour (bool) – whether or not to include colour control codes.

Returns:

description of this PSyIR node.

Return type:

str

property operator#

Return the operator.

Returns:

Enumerated type capturing the operator.

Return type:

Union[ psyclone.psyir.nodes.UnaryOperation.Operator psyclone.psyir.nodes.BinaryOperation.Operator]

class psyclone.psyir.nodes.operation.UnaryOperation(operator, parent=None)[source]#

Node representing a UnaryOperation expression. As such it has one operand as child 0, and an attribute with the operator type.

Inheritance

Inheritance diagram of UnaryOperation
static create(operator, operand)[source]#

Create a UnaryOperation instance given an operator and operand.

Parameters:
  • operator (psyclone.psyir.nodes.UnaryOperation.Operator) – the specified operator.

  • operand (Union[psyclone.psyir.nodes.Node, Tuple[str, psyclone.psyir.nodes.Node]]) – the PSyIR node that oper operates on, or a tuple containing the name of the argument and the PSyIR node.

Returns:

a UnaryOperation instance.

Return type:

psyclone.psyir.nodes.UnaryOperation

Raises:

GenerationError – if the arguments to the create method are not of the expected type.

property datatype#
Returns:

the datatype of the result of this UnaryOperation.

Return type:

psyclone.psyir.symbols.DataType

property operand: DataNode#
Returns:

the operand of this UnaryOperation.

class psyclone.psyir.nodes.operation.BinaryOperation(operator, has_explicit_grouping=False, parent=None)[source]#

Node representing a BinaryOperation expression. As such it has two operands as children 0 and 1, and an attribute with the operator type.

Parameters:
  • operator (psyclone.psyir.nodes.BinaryOperation.Operator) – the operator used in the operation.

  • has_explicit_grouping (bool) – Whether this operation should be surrounded by explicit grouping syntax (e.g. parenthesis) regardless of not breaking any other precedence rules. Defaults to False.

  • parent (Optional[psyclone.psyir.nodes.Node]) – the parent node of this Operation in the PSyIR.

Inheritance

Inheritance diagram of BinaryOperation
static create(operator, lhs, rhs, has_explicit_grouping=False)[source]#

Create a BinaryOperator instance given an operator and lhs and rhs child instances with optional names.

Parameters:
  • operator (psyclone.psyir.nodes.BinaryOperation.Operator) – the operator used in the operation.

  • lhs (Union[psyclone.psyir.nodes.Node, Tuple[str, psyclone.psyir.nodes.Node]]) – the PSyIR node containing the left hand side of the assignment, or a tuple containing the name of the argument and the PSyIR node.

  • rhs (Union[psyclone.psyir.nodes.Node, Tuple[str, psyclone.psyir.nodes.Node]]) – the PSyIR node containing the right hand side of the assignment, or a tuple containing the name of the argument and the PSyIR node.

  • has_explicit_grouping (bool) – Whether this operation should be surrounded by explicit grouping syntax (e.g. parenthesis) regardless of not breaking any other precedence rules. Defaults to False.

Returns:

a BinaryOperator instance.

Return type:

psyclone.psyir.nodes.BinaryOperation

Raises:

GenerationError – if the arguments to the create method are not of the expected type.

property datatype#

Determines the datatype of this operation. If it cannot be determined for any reason then an instance of UnresolvedType is returned.

Returns:

the datatype of the result of this BinaryOperation.

Return type:

psyclone.psyir.symbols.DataType

Raises:

InternalError – if the operands are both arrays but are of different shapes.

get_result_scalar_type(optypes)[source]#

Examines the two operand types to determine the base type of the operation using the rules in Section 7.2 of the Fortran2008 standard. If the type cannot be determined then an instance of UnresolvedType is returned.

Parameters:

optypes (list[DataType]) – the types of the two operands.

Return type:

DataType

Returns:

the base type of the result of the operation.

Raises:

TypeError – if an unexpected intrinsic type is found for either of the operands to a numeric operation.

property has_explicit_grouping: bool#
Returns:

Whether this operation should be surrounded by explicit grouping syntax (e.g. parenthesis) regardless of not breaking any other precedence rules.

property operands: Tuple[DataNode, DataNode]#
Returns:

the operands of this BinaryOperation.