pyccel.ast.variable module#

This module contains all classes which are used to handle memory block labels at different stages of pyccel. Memory block labels are usually either Variables or Indexed variables

class pyccel.ast.variable.AnnotatedPyccelSymbol(name, annotation, is_temp=False)[source]#

Bases: PyccelAstNode

Class representing a symbol in the code which has an annotation.

Symbolic placeholder for a Python variable, which has a name but no type yet. This is very generic, and it can also represent a function or a module.

Parameters:
  • name (str) – Name of the symbol.

  • annotation (PyccelAstNode, optional) – The annotation describing the type that the object will have. This should be an object from the type_annotations or typingext module (e.g. SyntacticTypeAnnotation, FunctionTypeAnnotation, TypingFinal). The annotation may be None if the argument is a bound class argument whose type can be auto-deduced later.

  • is_temp (bool) – Indicates if the symbol is a temporary object. This either means that the symbol represents an object originally named _ in the code, or that the symbol represents an object created by Pyccel in order to assign a temporary object. This is sometimes necessary to facilitate the translation.

property annotation#

Get the annotation.

Get the annotation left on the symbol. This should be a type annotation.

property name#

Get the PyccelSymbol describing the name.

Get the PyccelSymbol describing the name of the symbol in the code.

class pyccel.ast.variable.Constant(*args, value=Literal(None), **kwargs)[source]#

Bases: Variable

Class for expressing constant values (e.g. pi).

Class for expressing constant values (e.g. pi).

Parameters:
  • *args (tuple) – See pyccel.ast.variable.Variable.

  • value (bool|int|float|complex) – The value that the constant represents.

  • **kwargs (dict) – See pyccel.ast.variable.Variable.

Examples

>>> from pyccel.ast.datatypes import PythonNativeFloat
>>> from pyccel.ast.variable import Constant
>>> import math
>>> Constant(PythonNativeFloat(), 'pi' , value=math.pi )
Constant('pi', type=NativeFloat())
property value#

Immutable value of the constant

class pyccel.ast.variable.DottedName(*args)[source]#

Bases: PyccelAstNode

Represents a dotted object.

Represents an object accessed via a dot. This usually means that the object belongs to a class or module.

Parameters:

*args (tuple of PyccelSymbol) – The different symbols making up the dotted name.

Examples

>>> from pyccel.ast.core import DottedName
>>> DottedName('matrix', 'n_rows')
matrix.n_rows
>>> DottedName('pyccel', 'stdlib', 'parallel')
pyccel.stdlib.parallel
property name#

The different components of the name (these were separated by dots)

class pyccel.ast.variable.DottedVariable(*args, lhs, **kwargs)[source]#

Bases: Variable

Class representing a dotted variable.

Represents a dotted variable. This is usually a variable which is a member of a class

E.g. a = AClass() a.b = 3

In this case b is a DottedVariable where the lhs is a.

Parameters:
  • *args (tuple) – See pyccel.ast.variable.Variable.

  • lhs (Variable) – The Variable on the right of the ‘.’.

  • **kwargs (dict) – See pyccel.ast.variable.Variable.

property lhs#

The object before the final dot in the dotted variable

e.g. for the DottedVariable: a.b The lhs is a

class pyccel.ast.variable.IndexedElement(base, *indices)[source]#

Bases: TypedAstNode

Represents an indexed object in the code.

Represents an object which is a subset of a base object. The indexed object is retrieved by passing indices to the base object using the [] syntax.

In the semantic stage, the base object is an array, tuple or list. This function then determines the new rank and shape of the data block.

In the syntactic stage, this object is more versatile, it stores anything which is indexed using [] syntax. This can additionally include classes, maps, etc.

Parameters:

Examples

>>> from pyccel.ast.core import Variable, IndexedElement
>>> from pyccel.ast.datatypes import PythonNativeInt
>>> A = Variable(PythonNativeInt(), 'A', shape=(2,3), rank=2)
>>> i = Variable(PythonNativeInt(), 'i')
>>> j = Variable(PythonNativeInt(), 'j')
>>> IndexedElement(A, (i, j))
IndexedElement(A, i, j)
>>> IndexedElement(A, i, j) == A[i, j]
True
property allows_negative_indexes#

Indicate whether variables used to index this Variable can be negative.

Indicate whether variables used to index this Variable can be negative.

property base#

The object which is indexed

property indices#

A tuple of indices used to index the variable

property is_const#

Indicates whether the Variable is constant within its context.

Indicates whether the Variable is constant within its context. True if the Variable is constant, false if it can be modified.

property is_slice#

Indicates whether this instance represents a slice.

Indicates whether this instance represents a slice or an element.

class pyccel.ast.variable.Variable(class_type, name, *, memory_handling='stack', is_const=False, is_target=False, is_optional=False, is_private=False, shape=None, cls_base=None, is_argument=False, is_temp=False, allows_negative_indexes=False)[source]#

Bases: TypedAstNode

Represents a typed variable.

Represents a variable in the code and stores all useful properties which allow for easy usage of this variable.

Parameters:
  • class_type (PyccelType) – The Python type of the variable.

  • name (str, list, DottedName) – The name of the variable represented. This can be either a string or a dotted name, when using a Class attribute.

  • memory_handling (str, default: 'stack') – ‘heap’ is used for arrays, if we need to allocate memory on the heap. ‘stack’ if memory should be allocated on the stack, represents stack arrays and scalars. ‘alias’ if object allows access to memory stored in another variable.

  • is_const (bool, default: False) – Indicates if object is a const argument of a function.

  • is_target (bool, default: False) – Indicates if object is pointed to by another variable.

  • is_optional (bool, default: False) – Indicates if object is an optional argument of a function.

  • is_private (bool, default: False) – Indicates if object is private within a Module.

  • shape (tuple, default: None) – The shape of the array. A tuple whose elements indicate the number of elements along each of the dimensions of an array. The elements of the tuple should be None or TypedAstNodes.

  • cls_base (class, default: None) – Class base if variable is an object or an object member.

  • is_argument (bool, default: False) – Indicates if object is the argument of a function.

  • is_temp (bool, default: False) – Indicates if this symbol represents a temporary variable created by Pyccel, and was not present in the original Python code.

  • allows_negative_indexes (bool, default: False) – Indicates if non-literal negative indexes should be correctly handled when indexing this variable. The default is False for performance reasons.

Examples

>>> from pyccel.ast.datatypes import PythonNativeInt, PythonNativeFloat
>>> from pyccel.ast.core import Variable
>>> Variable(PythonNativeInt(), 'n')
n
>>> n = 4
>>> Variable(PythonNativeFloat(), 'x', shape=(n,2), memory_handling='heap')
x
>>> Variable(PythonNativeInt(), DottedName('matrix', 'n_rows'))
matrix.n_rows
property alloc_shape#

Shape of the variable at allocation

The shape used in pyccel is usually simplified to contain only Literals and PyccelArraySizes but the shape for the allocation of x cannot be Shape(x)

property allows_negative_indexes#

Indicates whether variables used to index this Variable can be negative

clone(name, new_class=None, **kwargs)[source]#

Create a clone of the current variable.

Create a new Variable object of the chosen class with the provided name and options. All non-specified options will match the current instance.

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

  • new_class (type, optional) – The class type of the new Variable (e.g. DottedVariable). The default is the same class type.

  • **kwargs (dict) – Dictionary containing any keyword-value pairs which are valid constructor keywords.

Returns:

The cloned variable.

Return type:

Variable

property cls_base#

Class from which the Variable inherits

declare_as_argument()[source]#

Indicate that the variable is used as an argument.

This function is called by FunctionDefArgument to ensure that arguments are correctly flagged as such.

inspect()[source]#

Print a short summary of the Variable and its parameters.

Print a short summary of the Variable and its parameters. This function is useful for debugging.

invalidate_node()[source]#

Indicate that this node is no longer used.

Indicate that this node is temporary and is no longer used. This will allow it to remove itself from its attributes’ users. If an attribute subsequently has no users, invalidate_node is called recursively. This prevents the tree from becoming filled with temporary objects and prevents obsolete objects being retrieved when searching for attribute nodes.

property is_alias#

Indicates if variable is an alias

property is_argument#

Indicates whether the Variable is a function argument in this context

property is_const#

Indicates whether the Variable is constant within its context.

Indicates whether the Variable is constant within its context. True if the Variable is constant, false if it can be modified.

property is_ndarray#

User friendly method to check if the variable is a numpy.ndarray.

User friendly method to check if the variable is an ndarray.

property is_optional#

Indicates if the Variable is optional in this context

property is_private#

Indicates if the Variable is private within the Module

property is_stack_array#

Indicates if the variable is located on stack and is an array

property is_target#

Indicates if the data in this Variable is shared with (pointed at by) another Variable

property is_temp#

Indicates if this symbol represents a temporary variable created by Pyccel, and was not present in the original Python code [default value : False].

property memory_handling#

Indicates whether a Variable has a dynamic size

property name#

Name of the variable

property on_heap#

Indicates if memory is allocated on the heap

property on_stack#

Indicates if memory is allocated on the stack

process_shape(shape)[source]#

Simplify the provided shape and ensure it has the expected format.

The provided shape is the shape used to create the object, and it can be a long expression. In most cases where the shape is required the provided shape is inconvenient, or it might have become invalid. This function therefore replaces those expressions with calls to the function PyccelArrayShapeElement.

Parameters:

shape (iterable of int) – The array shape to be simplified.

Returns:

The simplified array shape.

Return type:

tuple

rename(newname)[source]#

Forbidden method for renaming the variable

set_changeable_shape()[source]#

Indicate that the Variable’s shape is unknown at compilation time.

Indicate that the exact shape is unknown, e.g. if the allocate is done in an If block.

set_init_shape(shape)[source]#

Set the shape that was passed to the variable upon creation.

Set the shape that was passed to the variable upon creation. Normally this can be deduced when the variable was created, however this may not be the case if the variable was predeclared via a header or an annotation.

Parameters:

shape (tuple) – The shape of the array. A tuple whose elements indicate the number of elements along each of the dimensions of an array. The elements of the tuple should be None or TypedAstNodes.

shape_can_change(i)[source]#

Indicate if the shape can change in the i-th dimension.

Indicate whether the Variable’s shape can change in the i-th dimension at run time.

Parameters:

i (int) – The dimension over which the shape can change at runtime.

Returns:

Whether or not the variable shape can change in the i-th dimension.

Return type:

bool